Code

fix vector, power, and fidelity for paths in transformed groups, also normalize fidel...
[inkscape.git] / src / tweak-context.cpp
1 #define __SP_TWEAK_CONTEXT_C__
3 /*
4  * tweaking paths without node editing
5  *
6  * Authors:
7  *   bulia byak
8  *
9  * Copyright (C) 2007 authors
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #include "config.h"
16 #include <gtk/gtk.h>
17 #include <gdk/gdkkeysyms.h>
18 #include <glibmm/i18n.h>
20 #include <numeric>
22 #include "svg/svg.h"
23 #include "display/canvas-bpath.h"
24 #include "display/bezier-utils.h"
26 #include <glib/gmem.h>
27 #include "macros.h"
28 #include "document.h"
29 #include "selection.h"
30 #include "desktop.h"
31 #include "desktop-events.h"
32 #include "desktop-handles.h"
33 #include "desktop-affine.h"
34 #include "desktop-style.h"
35 #include "message-context.h"
36 #include "pixmaps/cursor-thin.xpm"
37 #include "pixmaps/cursor-thicken.xpm"
38 #include "pixmaps/cursor-attract.xpm"
39 #include "pixmaps/cursor-repel.xpm"
40 #include "pixmaps/cursor-push.xpm"
41 #include "pixmaps/cursor-roughen.xpm"
42 #include "pixmaps/cursor-color.xpm"
43 #include "libnr/n-art-bpath.h"
44 #include "libnr/nr-path.h"
45 #include "libnr/nr-maybe.h"
46 #include "libnr/nr-matrix-ops.h"
47 #include "libnr/nr-scale-translate-ops.h"
48 #include "xml/repr.h"
49 #include "context-fns.h"
50 #include "sp-item.h"
51 #include "inkscape.h"
52 #include "color.h"
53 #include "svg/svg-color.h"
54 #include "splivarot.h"
55 #include "sp-item-group.h"
56 #include "sp-shape.h"
57 #include "sp-path.h"
58 #include "path-chemistry.h"
59 #include "sp-gradient.h"
60 #include "sp-stop.h"
61 #include "sp-stop-fns.h"
62 #include "sp-gradient-reference.h"
63 #include "sp-linear-gradient.h"
64 #include "sp-radial-gradient.h"
65 #include "gradient-chemistry.h"
66 #include "sp-text.h"
67 #include "sp-flowtext.h"
68 #include "display/canvas-bpath.h"
69 #include "display/canvas-arena.h"
70 #include "livarot/Shape.h"
71 #include "isnan.h"
72 #include "prefs-utils.h"
73 #include "style.h"
74 #include "box3d.h"
76 #include "tweak-context.h"
78 #define DDC_RED_RGBA 0xff0000ff
80 #define DYNA_MIN_WIDTH 1.0e-6
82 // FIXME: move it to some shared file to be reused by both calligraphy and dropper
83 #define C1 0.552
84 static NArtBpath const hatch_area_circle[] = {
85     { NR_MOVETO, 0, 0, 0, 0, -1, 0 },
86     { NR_CURVETO, -1, C1, -C1, 1, 0, 1 },
87     { NR_CURVETO, C1, 1, 1, C1, 1, 0 },
88     { NR_CURVETO, 1, -C1, C1, -1, 0, -1 },
89     { NR_CURVETO, -C1, -1, -1, -C1, -1, 0 },
90     { NR_END, 0, 0, 0, 0, 0, 0 }
91 };
92 #undef C1
95 static void sp_tweak_context_class_init(SPTweakContextClass *klass);
96 static void sp_tweak_context_init(SPTweakContext *ddc);
97 static void sp_tweak_context_dispose(GObject *object);
99 static void sp_tweak_context_setup(SPEventContext *ec);
100 static void sp_tweak_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
101 static gint sp_tweak_context_root_handler(SPEventContext *ec, GdkEvent *event);
103 static SPEventContextClass *parent_class;
105 GtkType
106 sp_tweak_context_get_type(void)
108     static GType type = 0;
109     if (!type) {
110         GTypeInfo info = {
111             sizeof(SPTweakContextClass),
112             NULL, NULL,
113             (GClassInitFunc) sp_tweak_context_class_init,
114             NULL, NULL,
115             sizeof(SPTweakContext),
116             4,
117             (GInstanceInitFunc) sp_tweak_context_init,
118             NULL,   /* value_table */
119         };
120         type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPTweakContext", &info, (GTypeFlags)0);
121     }
122     return type;
125 static void
126 sp_tweak_context_class_init(SPTweakContextClass *klass)
128     GObjectClass *object_class = (GObjectClass *) klass;
129     SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
131     parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
133     object_class->dispose = sp_tweak_context_dispose;
135     event_context_class->setup = sp_tweak_context_setup;
136     event_context_class->set = sp_tweak_context_set;
137     event_context_class->root_handler = sp_tweak_context_root_handler;
140 static void
141 sp_tweak_context_init(SPTweakContext *tc)
143     SPEventContext *event_context = SP_EVENT_CONTEXT(tc);
145     event_context->cursor_shape = cursor_push_xpm;
146     event_context->hot_x = 4;
147     event_context->hot_y = 4;
149     /* attributes */
150     tc->dragging = FALSE;
152     tc->width = 0.2;
153     tc->force = 0.2;
154     tc->pressure = TC_DEFAULT_PRESSURE;
156     tc->is_dilating = false;
157     tc->has_dilated = false;
159     tc->do_h = true;
160     tc->do_s = true;
161     tc->do_l = true;
162     tc->do_o = false;
164     new (&tc->style_set_connection) sigc::connection();
167 static void
168 sp_tweak_context_dispose(GObject *object)
170     SPTweakContext *tc = SP_TWEAK_CONTEXT(object);
172     tc->style_set_connection.disconnect();
173     tc->style_set_connection.~connection();
175     if (tc->dilate_area) {
176         gtk_object_destroy(GTK_OBJECT(tc->dilate_area));
177         tc->dilate_area = NULL;
178     }
180     if (tc->_message_context) {
181         delete tc->_message_context;
182     }
184     G_OBJECT_CLASS(parent_class)->dispose(object);
187 void
188 sp_tweak_update_cursor (SPTweakContext *tc, gint mode)
190     SPEventContext *event_context = SP_EVENT_CONTEXT(tc);
191    switch (mode) {
192        case TWEAK_MODE_PUSH:
193            event_context->cursor_shape = cursor_push_xpm;
194            break;
195        case TWEAK_MODE_SHRINK:
196            event_context->cursor_shape = cursor_thin_xpm;
197            break;
198        case TWEAK_MODE_GROW:
199            event_context->cursor_shape = cursor_thicken_xpm;
200            break;
201        case TWEAK_MODE_ATTRACT:
202            event_context->cursor_shape = cursor_attract_xpm;
203            break;
204        case TWEAK_MODE_REPEL:
205            event_context->cursor_shape = cursor_repel_xpm;
206            break;
207        case TWEAK_MODE_ROUGHEN:
208            event_context->cursor_shape = cursor_roughen_xpm;
209            break;
210        case TWEAK_MODE_COLORPAINT:
211        case TWEAK_MODE_COLORJITTER:
212            event_context->cursor_shape = cursor_color_xpm;
213            break;
214    }
215    sp_event_context_update_cursor(event_context);
218 static bool
219 sp_tweak_context_style_set(SPCSSAttr const *css, SPTweakContext *tc)
221     if (tc->mode == TWEAK_MODE_COLORPAINT) { // intercept color setting only in this mode
222         // we cannot store properties with uris
223         css = sp_css_attr_unset_uris ((SPCSSAttr *) css);
225         sp_repr_css_change (inkscape_get_repr (INKSCAPE, "tools.tweak"), (SPCSSAttr *) css, "style");
227         return true;
228     }
229     return false;
233 static void
234 sp_tweak_context_setup(SPEventContext *ec)
236     SPTweakContext *tc = SP_TWEAK_CONTEXT(ec);
238     if (((SPEventContextClass *) parent_class)->setup)
239         ((SPEventContextClass *) parent_class)->setup(ec);
241     {
242         SPCurve *c = sp_curve_new_from_foreign_bpath(hatch_area_circle);
243         tc->dilate_area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
244         sp_curve_unref(c);
245         sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(tc->dilate_area), 0x00000000,(SPWindRule)0);
246         sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(tc->dilate_area), 0xff9900ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
247         sp_canvas_item_hide(tc->dilate_area);
248     }
250     sp_event_context_read(ec, "width");
251     sp_event_context_read(ec, "mode");
252     sp_event_context_read(ec, "fidelity");
253     sp_event_context_read(ec, "force");
254     sp_event_context_read(ec, "usepressure");
255     sp_event_context_read(ec, "doh");
256     sp_event_context_read(ec, "dol");
257     sp_event_context_read(ec, "dos");
258     sp_event_context_read(ec, "doo");
260     tc->is_drawing = false;
262     tc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
264     tc->style_set_connection = ec->desktop->connectSetStyle( // catch style-setting signal in this tool
265         sigc::bind(sigc::ptr_fun(&sp_tweak_context_style_set), tc)
266     );
268     if (prefs_get_int_attribute("tools.tweak", "selcue", 0) != 0) {
269         ec->enableSelectionCue();
270     }
272     if (prefs_get_int_attribute("tools.tweak", "gradientdrag", 0) != 0) {
273         ec->enableGrDrag();
274     }
277 static void
278 sp_tweak_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
280     SPTweakContext *tc = SP_TWEAK_CONTEXT(ec);
282     if (!strcmp(key, "width")) {
283         double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.1 );
284         tc->width = CLAMP(dval, -1000.0, 1000.0);
285     } else if (!strcmp(key, "mode")) {
286         gint64 const dval = ( val ? g_ascii_strtoll (val, NULL, 10) : 0 );
287         tc->mode = dval;
288         sp_tweak_update_cursor(tc, tc->mode);
289     } else if (!strcmp(key, "fidelity")) {
290         double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.0 );
291         tc->fidelity = CLAMP(dval, 0.0, 1.0);
292     } else if (!strcmp(key, "force")) {
293         double const dval = ( val ? g_ascii_strtod (val, NULL) : 1.0 );
294         tc->force = CLAMP(dval, 0, 1.0);
295     } else if (!strcmp(key, "usepressure")) {
296         tc->usepressure = (val && strcmp(val, "0"));
297     } else if (!strcmp(key, "doh")) {
298         tc->do_h = (val && strcmp(val, "0"));
299     } else if (!strcmp(key, "dos")) {
300         tc->do_s = (val && strcmp(val, "0"));
301     } else if (!strcmp(key, "dol")) {
302         tc->do_l = (val && strcmp(val, "0"));
303     } else if (!strcmp(key, "doo")) {
304         tc->do_o = (val && strcmp(val, "0"));
305     }
308 static void
309 sp_tweak_extinput(SPTweakContext *tc, GdkEvent *event)
311     if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &tc->pressure))
312         tc->pressure = CLAMP (tc->pressure, TC_MIN_PRESSURE, TC_MAX_PRESSURE);
313     else
314         tc->pressure = TC_DEFAULT_PRESSURE;
317 double
318 get_dilate_radius (SPTweakContext *tc)
320     // 10 times the pen width:
321     return 500 * tc->width/SP_EVENT_CONTEXT(tc)->desktop->current_zoom();
324 double
325 get_dilate_force (SPTweakContext *tc)
327     double force = 8 * (tc->usepressure? tc->pressure : TC_DEFAULT_PRESSURE)
328         /sqrt(SP_EVENT_CONTEXT(tc)->desktop->current_zoom());
329     if (force > 3) {
330         force += 4 * (force - 3);
331     }
332     return force * tc->force;
335 bool
336 sp_tweak_dilate_recursive (Inkscape::Selection *selection, SPItem *item, NR::Point p, NR::Point vector, gint mode, double radius, double force, double fidelity)
338     bool did = false;
340     if (SP_IS_BOX3D(item)) {
341         // convert 3D boxes to ordinary groups before tweaking their shapes
342         item = SP_ITEM(box3d_convert_to_group(SP_BOX3D(item)));
343         selection->add(item);
344     }
346     if (SP_IS_GROUP(item)) {
347         for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
348             if (SP_IS_ITEM(child)) {
349                 if (sp_tweak_dilate_recursive (selection, SP_ITEM(child), p, vector, mode, radius, force, fidelity))
350                     did = true;
351             }
352         }
354     } else if (SP_IS_PATH(item) || SP_IS_SHAPE(item) || SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
356         Inkscape::XML::Node *newrepr = NULL;
357         gint pos = 0;
358         Inkscape::XML::Node *parent = NULL;
359         char const *id = NULL;
360         if (!SP_IS_PATH(item)) {
361             newrepr = sp_selected_item_to_curved_repr(item, 0);
362             if (!newrepr)
363                 return false;
365             // remember the position of the item
366             pos = SP_OBJECT_REPR(item)->position();
367             // remember parent
368             parent = SP_OBJECT_REPR(item)->parent();
369             // remember id
370             id = SP_OBJECT_REPR(item)->attribute("id");
371         }
374         // skip those paths whose bboxes are entirely out of reach with our radius
375         NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2doc_affine(item));
376         if (bbox) {
377             bbox->growBy(radius);
378             if (!bbox->contains(p)) {
379                 return false;
380             }
381         }
383         Path *orig = Path_for_item(item, false);
384         if (orig == NULL) {
385             return false;
386         }
388         Path *res = new Path;
389         res->SetBackData(false);
391         Shape *theShape = new Shape;
392         Shape *theRes = new Shape;
393         NR::Matrix i2doc(sp_item_i2doc_affine(item));
395         orig->ConvertWithBackData((0.08 - (0.07 * fidelity)) / i2doc.expansion()); // default 0.059
396         orig->Fill(theShape, 0);
398         SPCSSAttr *css = sp_repr_css_attr(SP_OBJECT_REPR(item), "style");
399         gchar const *val = sp_repr_css_property(css, "fill-rule", NULL);
400         if (val && strcmp(val, "nonzero") == 0)
401         {
402             theRes->ConvertToShape(theShape, fill_nonZero);
403         }
404         else if (val && strcmp(val, "evenodd") == 0)
405         {
406             theRes->ConvertToShape(theShape, fill_oddEven);
407         }
408         else
409         {
410             theRes->ConvertToShape(theShape, fill_nonZero);
411         }
413         if (NR::L2(vector) != 0)
414             vector = 1/NR::L2(vector) * vector;
416         bool did_this = false;
417         if (mode == TWEAK_MODE_SHRINK || mode == TWEAK_MODE_GROW) {
418             if (theShape->MakeTweak(tweak_mode_grow, theRes,
419                                  mode == TWEAK_MODE_GROW? force : -force,
420                                  join_straight, 4.0,
421                                  true, p, NR::Point(0,0), radius, &i2doc) == 0) // 0 means the shape was actually changed
422               did_this = true;
423         } else if (mode == TWEAK_MODE_ATTRACT || mode == TWEAK_MODE_REPEL) {
424             if (theShape->MakeTweak(tweak_mode_repel, theRes,
425                                  mode == TWEAK_MODE_REPEL? force : -force,
426                                  join_straight, 4.0,
427                                  true, p, NR::Point(0,0), radius, &i2doc) == 0)
428               did_this = true;
429         } else if (mode == TWEAK_MODE_PUSH) {
430             if (theShape->MakeTweak(tweak_mode_push, theRes,
431                                  1.0,
432                                  join_straight, 4.0,
433                                  true, p, force*2*vector, radius, &i2doc) == 0)
434               did_this = true;
435         } else if (mode == TWEAK_MODE_ROUGHEN) {
436             if (theShape->MakeTweak(tweak_mode_roughen, theRes,
437                                  force,
438                                  join_straight, 4.0,
439                                  true, p, NR::Point(0,0), radius, &i2doc) == 0)
440               did_this = true;
441         }
443         // the rest only makes sense if we actually changed the path
444         if (did_this) {
445             theRes->ConvertToShape(theShape, fill_positive);
447             res->Reset();
448             theRes->ConvertToForme(res);
450             double th_max = (0.6 - 0.59*sqrt(fidelity)) / i2doc.expansion();
451             double threshold = MAX(th_max, th_max*force);
452             res->ConvertEvenLines(threshold);
453             res->Simplify(threshold / (SP_ACTIVE_DESKTOP->current_zoom()));
455             if (newrepr) { // converting to path, need to replace the repr
456                 bool is_selected = selection->includes(item);
457                 if (is_selected)
458                     selection->remove(item);
460                 // It's going to resurrect, so we delete without notifying listeners.
461                 SP_OBJECT(item)->deleteObject(false);
463                 // restore id
464                 newrepr->setAttribute("id", id);
465                 // add the new repr to the parent
466                 parent->appendChild(newrepr);
467                 // move to the saved position
468                 newrepr->setPosition(pos > 0 ? pos : 0);
470                 if (is_selected)
471                     selection->add(newrepr);
472             }
474             if (res->descr_cmd.size() > 1) {
475                 gchar *str = res->svg_dump_path();
476                 if (newrepr) {
477                     newrepr->setAttribute("d", str);
478                 } else {
479                     if (SP_IS_SHAPE(item) && SP_SHAPE(item)->path_effect_href) {
480                         SP_OBJECT_REPR(item)->setAttribute("inkscape:original-d", str);
481                     } else {
482                         SP_OBJECT_REPR(item)->setAttribute("d", str);
483                     }
484                 }
485                 g_free(str);
486             } else {
487                 // TODO: if there's 0 or 1 node left, delete this path altogether
488             }
490             if (newrepr) {
491                 Inkscape::GC::release(newrepr);
492                 newrepr = NULL;
493             }
494         }
496         delete theShape;
497         delete theRes;
498         delete orig;
499         delete res;
501         if (did_this)
502             did = true;
503     }
505     return did;
508 void
509 tweak_colorpaint (float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
511     float rgb_g[3];
513     if (!do_h || !do_s || !do_l) {
514         float hsl_g[3];
515         sp_color_rgb_to_hsl_floatv (hsl_g, SP_RGBA32_R_F(goal), SP_RGBA32_G_F(goal), SP_RGBA32_B_F(goal));
516         float hsl_c[3];
517         sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
518         if (!do_h)
519             hsl_g[0] = hsl_c[0];
520         if (!do_s)
521             hsl_g[1] = hsl_c[1];
522         if (!do_l)
523             hsl_g[2] = hsl_c[2];
524         sp_color_hsl_to_rgb_floatv (rgb_g, hsl_g[0], hsl_g[1], hsl_g[2]);
525     } else {
526         rgb_g[0] = SP_RGBA32_R_F(goal);
527         rgb_g[1] = SP_RGBA32_G_F(goal);
528         rgb_g[2] = SP_RGBA32_B_F(goal);
529     }
531     for (int i = 0; i < 3; i++) {
532         double d = rgb_g[i] - color[i];
533         color[i] += d * force;
534     }
537 void
538 tweak_colorjitter (float *color, double force, bool do_h, bool do_s, bool do_l)
540     float hsl_c[3];
541     sp_color_rgb_to_hsl_floatv (hsl_c, color[0], color[1], color[2]);
543     if (do_h) {
544         hsl_c[0] += g_random_double_range(-0.5, 0.5) * force;
545         if (hsl_c[0] > 1)
546             hsl_c[0] -= 1;
547         if (hsl_c[0] < 0)
548             hsl_c[0] += 1;
549     }
550     if (do_s) {
551         hsl_c[1] += g_random_double_range(-hsl_c[1], 1 - hsl_c[1]) * force;
552     }
553     if (do_l) {
554         hsl_c[2] += g_random_double_range(-hsl_c[2], 1 - hsl_c[2]) * force;
555     }
557     sp_color_hsl_to_rgb_floatv (color, hsl_c[0], hsl_c[1], hsl_c[2]);
560 void
561 tweak_color (guint mode, float *color, guint32 goal, double force, bool do_h, bool do_s, bool do_l)
563     if (mode == TWEAK_MODE_COLORPAINT) {
564         tweak_colorpaint (color, goal, force, do_h, do_s, do_l);
565     } else if (mode == TWEAK_MODE_COLORJITTER) {
566         tweak_colorjitter (color, force, do_h, do_s, do_l);
567     }
570 void
571 tweak_opacity (guint mode, SPIScale24 *style_opacity, double opacity_goal, double force)
573     double opacity = SP_SCALE24_TO_FLOAT (style_opacity->value);
575     if (mode == TWEAK_MODE_COLORPAINT) {
576         double d = opacity_goal - opacity;
577         opacity += d * force;
578     } else if (mode == TWEAK_MODE_COLORJITTER) {
579         opacity += g_random_double_range(-opacity, 1 - opacity) * force;
580     }
582     style_opacity->value = SP_SCALE24_FROM_FLOAT(opacity);
586 double
587 tweak_profile (double dist, double radius)
589     if (radius == 0)
590         return 0;
591     double x = dist / radius;
592     double alpha = 1;
593     if (x >= 1) {
594         return 0;
595     } else if (x <= 0) {
596         return 1;
597     } else {
598         return (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5);
599     }
602 void
603 tweak_colors_in_gradient (SPItem *item, bool fill_or_stroke,
604                           guint32 const rgb_goal, NR::Point p_w, double radius, double force, guint mode,
605                           bool do_h, bool do_s, bool do_l, bool /*do_o*/)
607     SPGradient *gradient = sp_item_gradient (item, fill_or_stroke);
609     if (!gradient || !SP_IS_GRADIENT(gradient))
610         return;
612     NR::Matrix i2d = sp_item_i2doc_affine (item);
613     NR::Point p = p_w * i2d.inverse();
614     p *= (gradient->gradientTransform).inverse();
615     // now p is in gradient's original coordinates
617     double pos = 0;
618     double r = 0;
619     if (SP_IS_LINEARGRADIENT(gradient)) {
620         SPLinearGradient *lg = SP_LINEARGRADIENT(gradient);
622         NR::Point p1(lg->x1.computed, lg->y1.computed);
623         NR::Point p2(lg->x2.computed, lg->y2.computed);
624         NR::Point pdiff(p2 - p1);
625         double vl = NR::L2(pdiff);
627         // This is the matrix which moves and rotates the gradient line
628         // so it's oriented along the X axis:
629         NR::Matrix norm = NR::Matrix(NR::translate(-p1)) * NR::Matrix(NR::rotate(-atan2(pdiff[NR::Y], pdiff[NR::X])));
631         // Transform the mouse point by it to find out its projection onto the gradient line:
632         NR::Point pnorm = p * norm;
634         // Scale its X coordinate to match the length of the gradient line:
635         pos = pnorm[NR::X] / vl;
636         // Calculate radius in lenfth-of-gradient-line units
637         r = radius / vl;
639     } else if (SP_IS_RADIALGRADIENT(gradient)) {
640         SPRadialGradient *rg = SP_RADIALGRADIENT(gradient);
641         NR::Point c (rg->cx.computed, rg->cy.computed);
642         pos = NR::L2(p - c) / rg->r.computed;
643         r = radius / rg->r.computed;
644     }
646     // Normalize pos to 0..1, taking into accound gradient spread:
647     double pos_e = pos;
648     if (gradient->spread == SP_GRADIENT_SPREAD_PAD) {
649         if (pos > 1)
650             pos_e = 1;
651         if (pos < 0)
652             pos_e = 0;
653     } else if (gradient->spread == SP_GRADIENT_SPREAD_REPEAT) {
654         if (pos > 1 || pos < 0)
655             pos_e = pos - floor(pos);
656     } else if (gradient->spread == SP_GRADIENT_SPREAD_REFLECT) {
657         if (pos > 1 || pos < 0) {
658             bool odd = ((int)(floor(pos)) % 2 == 1);
659             pos_e = pos - floor(pos);
660             if (odd)
661                 pos_e = 1 - pos_e;
662         }
663     }
665     SPGradient *vector = sp_gradient_get_forked_vector_if_necessary(gradient, false);
667     double offset_l = 0;
668     double offset_h = 0;
669     SPObject *child_prev = NULL;
670     for (SPObject *child = sp_object_first_child(vector);
671          child != NULL; child = SP_OBJECT_NEXT(child)) {
672         if (!SP_IS_STOP(child))
673             continue;
674         SPStop *stop = SP_STOP (child);
676         offset_h = stop->offset;
678         if (child_prev) {
680             if (offset_h - offset_l > r && pos_e >= offset_l && pos_e <= offset_h) {
681                 // the summit falls in this interstop, and the radius is small,
682                 // so it only affects the ends of this interstop;
683                 // distribute the force between the two endstops so that they
684                 // get all the painting even if they are not touched by the brush
685                 tweak_color (mode, stop->specified_color.v.c, rgb_goal,
686                                   force * (pos_e - offset_l) / (offset_h - offset_l),
687                                   do_h, do_s, do_l);
688                 tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal,
689                                   force * (offset_h - pos_e) / (offset_h - offset_l),
690                                   do_h, do_s, do_l);
691                 SP_OBJECT(stop)->updateRepr();
692                 child_prev->updateRepr();
693                 break;
694             } else {
695                 // wide brush, may affect more than 2 stops,
696                 // paint each stop by the force from the profile curve
697                 if (offset_l <= pos_e && offset_l > pos_e - r) {
698                     tweak_color (mode, SP_STOP(child_prev)->specified_color.v.c, rgb_goal,
699                                  force * tweak_profile (fabs (pos_e - offset_l), r),
700                                  do_h, do_s, do_l);
701                     child_prev->updateRepr();
702                 }
704                 if (offset_h >= pos_e && offset_h < pos_e + r) {
705                     tweak_color (mode, stop->specified_color.v.c, rgb_goal,
706                                  force * tweak_profile (fabs (pos_e - offset_h), r),
707                                  do_h, do_s, do_l);
708                     SP_OBJECT(stop)->updateRepr();
709                 }
710             }
711         }
713         offset_l = offset_h;
714         child_prev = child;
715     }
718 bool
719 sp_tweak_color_recursive (guint mode, SPItem *item, SPItem *item_at_point,
720                           guint32 fill_goal, bool do_fill,
721                           guint32 stroke_goal, bool do_stroke,
722                           float opacity_goal, bool do_opacity,
723                           NR::Point p, double radius, double force,
724                           bool do_h, bool do_s, bool do_l, bool do_o)
726     bool did = false;
728     if (SP_IS_GROUP(item)) {
729         for (SPObject *child = sp_object_first_child(SP_OBJECT(item)) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
730             if (SP_IS_ITEM(child)) {
731                 if (sp_tweak_color_recursive (mode, SP_ITEM(child), item_at_point,
732                                           fill_goal, do_fill,
733                                           stroke_goal, do_stroke,
734                                           opacity_goal, do_opacity,
735                                           p, radius, force, do_h, do_s, do_l, do_o));
736                     did = true;
737             }
738         }
740     } else {
741         SPStyle *style = SP_OBJECT_STYLE(item);
742         if (!style) {
743             return false;
744         }
745         NR::Maybe<NR::Rect> bbox = item->getBounds(sp_item_i2doc_affine(item),
746                                                         SPItem::GEOMETRIC_BBOX);
747         if (!bbox) {
748             return false;
749         }
751         NR::Rect brush(p - NR::Point(radius, radius), p + NR::Point(radius, radius));
753         NR::Point center = bbox->midpoint();
754         double this_force;
756 // if item == item_at_point, use max force
757         if (item == item_at_point) {
758             this_force = force;
759 // else if no overlap of bbox and brush box, skip:
760         } else if (!bbox->intersects(brush)) {
761             return false;
762 //TODO:
763 // else if object > 1.5 brush: test 4/8/16 points in the brush on hitting the object, choose max
764         //} else if (bbox->maxExtent() > 3 * radius) {
765         //}
766 // else if object > 0.5 brush: test 4 corners of bbox and center on being in the brush, choose max
767 // else if still smaller, then check only the object center:
768         } else {
769             this_force = force * tweak_profile (NR::L2 (p - center), radius);
770         }
772         if (this_force > 0.002) {
774             if (do_fill) {
775                 if (style->fill.isPaintserver()) {
776                     tweak_colors_in_gradient (item, true, fill_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
777                     did = true;
778                 } else if (style->fill.isColor()) {
779                     tweak_color (mode, style->fill.value.color.v.c, fill_goal, this_force, do_h, do_s, do_l);
780                     item->updateRepr();
781                     did = true;
782                 }
783             }
784             if (do_stroke) {
785                 if (style->stroke.isPaintserver()) {
786                     tweak_colors_in_gradient (item, false, stroke_goal, p, radius, this_force, mode, do_h, do_s, do_l, do_o);
787                     did = true;
788                 } else if (style->stroke.isColor()) {
789                     tweak_color (mode, style->stroke.value.color.v.c, stroke_goal, this_force, do_h, do_s, do_l);
790                     item->updateRepr();
791                     did = true;
792                 }
793             }
794             if (do_opacity && do_o) {
795                 tweak_opacity (mode, &style->opacity, opacity_goal, this_force);
796             }
797         }
798     }
800     return did;
804 bool
805 sp_tweak_dilate (SPTweakContext *tc, NR::Point event_p, NR::Point p, NR::Point vector)
807     Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(tc)->desktop);
809     if (selection->isEmpty()) {
810         return false;
811     }
813     bool did = false;
814     double radius = get_dilate_radius(tc);
815     double force = get_dilate_force(tc);
816     if (radius == 0 || force == 0) {
817         return false;
818     }
819     double color_force = MIN(sqrt(force)/20.0, 1);
821     SPItem *item_at_point = SP_EVENT_CONTEXT(tc)->desktop->item_at_point(event_p, TRUE);
822     Inkscape::XML::Node *tool_repr = inkscape_get_repr(INKSCAPE, "tools.tweak");
823     SPCSSAttr *css = sp_repr_css_attr_inherited(tool_repr, "style");
824     if (tc->mode == TWEAK_MODE_COLORPAINT && !css)
825         return false;
827     bool do_fill = false, do_stroke = false, do_opacity = false;
828     guint32 fill_goal = 0, stroke_goal = 0;
829     float opacity_goal = 1;
831     const gchar *fill_prop = sp_repr_css_property(css, "fill", NULL);
832     if (fill_prop && strcmp(fill_prop, "none")) {
833         do_fill = true;
834         fill_goal = sp_svg_read_color(fill_prop, 0);
835     }
836     const gchar *stroke_prop = sp_repr_css_property(css, "stroke", NULL);
837     if (stroke_prop && strcmp(stroke_prop, "none")) {
838         do_stroke = true;
839         stroke_goal = sp_svg_read_color(stroke_prop, 0);
840     }
841     const gchar *opacity_prop = sp_repr_css_property(css, "opacity", NULL);
842     if (opacity_prop) {
843         do_opacity = true;
844         sp_svg_number_read_f(opacity_prop, &opacity_goal);
845     }
847     for (GSList *items = g_slist_copy((GSList *) selection->itemList());
848          items != NULL;
849          items = items->next) {
851         SPItem *item = (SPItem *) items->data;
853         if (tc->mode == TWEAK_MODE_COLORPAINT || tc->mode == TWEAK_MODE_COLORJITTER) {
854             if (do_fill || do_stroke || do_opacity) {
855                 if (sp_tweak_color_recursive (tc->mode, item, item_at_point,
856                                           fill_goal, do_fill,
857                                           stroke_goal, do_stroke,
858                                           opacity_goal, do_opacity,
859                                           p, radius, color_force, tc->do_h, tc->do_s, tc->do_l, tc->do_o))
860                 did = true;
861             }
862         } else {
863             if (sp_tweak_dilate_recursive (selection, item, p, vector, tc->mode, radius, force, tc->fidelity))
864                 did = true;
865         }
866     }
868     return did;
871 void
872 sp_tweak_update_area (SPTweakContext *tc)
874         double radius = get_dilate_radius(tc);
875         NR::Matrix const sm (NR::scale(radius, radius) * NR::translate(SP_EVENT_CONTEXT(tc)->desktop->point()));
876         sp_canvas_item_affine_absolute(tc->dilate_area, sm);
877         sp_canvas_item_show(tc->dilate_area);
880 void
881 sp_tweak_switch_mode (SPTweakContext *tc, gint mode)
883     SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
884     // need to set explicitly, because the prefs may not have changed by the previous
885     tc->mode = mode;
886     sp_tweak_update_cursor (tc, mode);
889 void
890 sp_tweak_switch_mode_temporarily (SPTweakContext *tc, gint mode)
892    // Juggling about so that prefs have the old value but tc->mode and the button show new mode:
893    gint now_mode = prefs_get_int_attribute("tools.tweak", "mode", 0);
894    SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("tweak_tool_mode", mode);
895    // button has changed prefs, restore
896    prefs_set_int_attribute("tools.tweak", "mode", now_mode);
897    // changing prefs changed tc->mode, restore back :)
898    tc->mode = mode;
899    sp_tweak_update_cursor (tc, mode);
902 gint
903 sp_tweak_context_root_handler(SPEventContext *event_context,
904                                   GdkEvent *event)
906     SPTweakContext *tc = SP_TWEAK_CONTEXT(event_context);
907     SPDesktop *desktop = event_context->desktop;
909     gint ret = FALSE;
911     switch (event->type) {
912         case GDK_ENTER_NOTIFY:
913             sp_canvas_item_show(tc->dilate_area);
914             break;
915         case GDK_LEAVE_NOTIFY:
916             sp_canvas_item_hide(tc->dilate_area);
917             break;
918         case GDK_BUTTON_PRESS:
919             if (event->button.button == 1 && !event_context->space_panning) {
921                 if (Inkscape::have_viable_layer(desktop, tc->_message_context) == false) {
922                     return TRUE;
923                 }
925                 NR::Point const button_w(event->button.x,
926                                          event->button.y);
927                 NR::Point const button_dt(desktop->w2d(button_w));
928                 tc->last_push = desktop->dt2doc(button_dt);
930                 sp_tweak_extinput(tc, event);
932                 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
933                 tc->is_drawing = true;
934                 tc->is_dilating = true;
935                 tc->has_dilated = false;
937                 ret = TRUE;
938             }
939             break;
940         case GDK_MOTION_NOTIFY:
941         {
942             NR::Point const motion_w(event->motion.x,
943                                      event->motion.y);
944             NR::Point motion_dt(desktop->w2d(motion_w));
945             NR::Point motion_doc(desktop->dt2doc(motion_dt));
946             sp_tweak_extinput(tc, event);
948             // draw the dilating cursor
949                 double radius = get_dilate_radius(tc);
950                 NR::Matrix const sm (NR::scale(radius, radius) * NR::translate(desktop->w2d(motion_w)));
951                 sp_canvas_item_affine_absolute(tc->dilate_area, sm);
952                 sp_canvas_item_show(tc->dilate_area);
954                 guint num = 0;
955                 if (!desktop->selection->isEmpty()) {
956                     num = g_slist_length((GSList *) desktop->selection->itemList());
957                 }
958                 if (num == 0) {
959                     tc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Nothing selected!</b> Select objects to tweak."));
960                 } else {
961                     switch (tc->mode) {
962                         case TWEAK_MODE_PUSH:
963                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
964                                                       ngettext("<b>Pushing %d</b> selected object",
965                                                       "<b>Pushing %d</b> selected objects", num), num);
966                            break;
967                         case TWEAK_MODE_SHRINK:
968                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
969                                                       ngettext("<b>Shrinking %d</b> selected object",
970                                                       "<b>Shrinking %d</b> selected objects", num), num);
971                            break;
972                         case TWEAK_MODE_GROW:
973                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
974                                                       ngettext("<b>Growing %d</b> selected object",
975                                                       "<b>Growing %d</b> selected objects", num), num);
976                            break;
977                         case TWEAK_MODE_ATTRACT:
978                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
979                                                       ngettext("<b>Attracting %d</b> selected object",
980                                                       "<b>Attracting %d</b> selected objects", num), num);
981                            break;
982                         case TWEAK_MODE_REPEL:
983                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
984                                                       ngettext("<b>Repelling %d</b> selected object",
985                                                       "<b>Repelling %d</b> selected objects", num), num);
986                            break;
987                         case TWEAK_MODE_ROUGHEN:
988                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
989                                                       ngettext("<b>Roughening %d</b> selected object",
990                                                       "<b>Roughening %d</b> selected objects", num), num);
991                         case TWEAK_MODE_COLORPAINT:
992                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
993                                                       ngettext("<b>Painting %d</b> selected object",
994                                                       "<b>Painting %d</b> selected objects", num), num);
995                            break;
996                         case TWEAK_MODE_COLORJITTER:
997                            tc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
998                                                       ngettext("<b>Jittering colors in %d</b> selected object",
999                                                       "<b>Jittering colors in %d</b> selected objects", num), num);
1000                            break;
1001                     }
1002                 }
1005             // dilating:
1006             if (tc->is_drawing && ( event->motion.state & GDK_BUTTON1_MASK )) {
1007                 sp_tweak_dilate (tc, motion_w, motion_doc, motion_doc - tc->last_push);
1008                 //tc->last_push = motion_doc;
1009                 tc->has_dilated = true;
1010                 // it's slow, so prevent clogging up with events
1011                 gobble_motion_events(GDK_BUTTON1_MASK);
1012                 return TRUE;
1013             }
1015         }
1016         break;
1019     case GDK_BUTTON_RELEASE:
1020     {
1021         NR::Point const motion_w(event->button.x, event->button.y);
1022         NR::Point const motion_dt(desktop->w2d(motion_w));
1024         sp_canvas_end_forced_full_redraws(desktop->canvas);
1025         tc->is_drawing = false;
1027         if (tc->is_dilating && event->button.button == 1 && !event_context->space_panning) {
1028             if (!tc->has_dilated) {
1029                 // if we did not rub, do a light tap
1030                 tc->pressure = 0.03;
1031                 sp_tweak_dilate (tc, motion_w, desktop->dt2doc(motion_dt), NR::Point(0,0));
1032             }
1033             tc->is_dilating = false;
1034             tc->has_dilated = false;
1035             switch (tc->mode) {
1036                 case TWEAK_MODE_PUSH:
1037                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1038                                      SP_VERB_CONTEXT_TWEAK, _("Push tweak"));
1039                     break;
1040                 case TWEAK_MODE_SHRINK:
1041                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1042                                      SP_VERB_CONTEXT_TWEAK, _("Shrink tweak"));
1043                     break;
1044                 case TWEAK_MODE_GROW:
1045                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1046                                      SP_VERB_CONTEXT_TWEAK, _("Grow tweak"));
1047                     break;
1048                 case TWEAK_MODE_ATTRACT:
1049                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1050                                      SP_VERB_CONTEXT_TWEAK, _("Attract tweak"));
1051                     break;
1052                 case TWEAK_MODE_REPEL:
1053                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1054                                      SP_VERB_CONTEXT_TWEAK, _("Repel tweak"));
1055                     break;
1056                 case TWEAK_MODE_ROUGHEN:
1057                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1058                                      SP_VERB_CONTEXT_TWEAK, _("Roughen tweak"));
1059                     break;
1060                 case TWEAK_MODE_COLORPAINT:
1061                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1062                                      SP_VERB_CONTEXT_TWEAK, _("Color paint tweak"));
1063                 case TWEAK_MODE_COLORJITTER:
1064                     sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
1065                                      SP_VERB_CONTEXT_TWEAK, _("Color jitter tweak"));
1066                     break;
1067             }
1068         }
1069         break;
1070     }
1072     case GDK_KEY_PRESS:
1073         switch (get_group0_keyval (&event->key)) {
1074         case GDK_p:
1075         case GDK_P:
1076         case GDK_1:
1077             if (MOD__SHIFT_ONLY) {
1078                 sp_tweak_switch_mode(tc, TWEAK_MODE_PUSH);
1079                 ret = TRUE;
1080             }
1081             break;
1082         case GDK_s:
1083         case GDK_S:
1084         case GDK_2:
1085             if (MOD__SHIFT_ONLY) {
1086                 sp_tweak_switch_mode(tc, TWEAK_MODE_SHRINK);
1087                 ret = TRUE;
1088             }
1089             break;
1090         case GDK_g:
1091         case GDK_G:
1092         case GDK_3:
1093             if (MOD__SHIFT_ONLY) {
1094                 sp_tweak_switch_mode(tc, TWEAK_MODE_GROW);
1095                 ret = TRUE;
1096             }
1097             break;
1098         case GDK_a:
1099         case GDK_A:
1100         case GDK_4:
1101             if (MOD__SHIFT_ONLY) {
1102                 sp_tweak_switch_mode(tc, TWEAK_MODE_ATTRACT);
1103                 ret = TRUE;
1104             }
1105             break;
1106         case GDK_e:
1107         case GDK_E:
1108         case GDK_5:
1109             if (MOD__SHIFT_ONLY) {
1110                 sp_tweak_switch_mode(tc, TWEAK_MODE_REPEL);
1111                 ret = TRUE;
1112             }
1113             break;
1114         case GDK_r:
1115         case GDK_R:
1116         case GDK_6:
1117             if (MOD__SHIFT_ONLY) {
1118                 sp_tweak_switch_mode(tc, TWEAK_MODE_ROUGHEN);
1119                 ret = TRUE;
1120             }
1121             break;
1122         case GDK_c:
1123         case GDK_C:
1124         case GDK_7:
1125             if (MOD__SHIFT_ONLY) {
1126                 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORPAINT);
1127                 ret = TRUE;
1128             }
1129             break;
1130         case GDK_j:
1131         case GDK_J:
1132         case GDK_8:
1133             if (MOD__SHIFT_ONLY) {
1134                 sp_tweak_switch_mode(tc, TWEAK_MODE_COLORJITTER);
1135                 ret = TRUE;
1136             }
1137             break;
1139         case GDK_Up:
1140         case GDK_KP_Up:
1141             if (!MOD__CTRL_ONLY) {
1142                 tc->force += 0.05;
1143                 if (tc->force > 1.0)
1144                     tc->force = 1.0;
1145                 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1146                 ret = TRUE;
1147             }
1148             break;
1149         case GDK_Down:
1150         case GDK_KP_Down:
1151             if (!MOD__CTRL_ONLY) {
1152                 tc->force -= 0.05;
1153                 if (tc->force < 0.0)
1154                     tc->force = 0.0;
1155                 desktop->setToolboxAdjustmentValue ("tweak-force", tc->force * 100);
1156                 ret = TRUE;
1157             }
1158             break;
1159         case GDK_Right:
1160         case GDK_KP_Right:
1161             if (!MOD__CTRL_ONLY) {
1162                 tc->width += 0.01;
1163                 if (tc->width > 1.0)
1164                     tc->width = 1.0;
1165                 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100); // the same spinbutton is for alt+x
1166                 sp_tweak_update_area(tc);
1167                 ret = TRUE;
1168             }
1169             break;
1170         case GDK_Left:
1171         case GDK_KP_Left:
1172             if (!MOD__CTRL_ONLY) {
1173                 tc->width -= 0.01;
1174                 if (tc->width < 0.01)
1175                     tc->width = 0.01;
1176                 desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1177                 sp_tweak_update_area(tc);
1178                 ret = TRUE;
1179             }
1180             break;
1181         case GDK_Home:
1182         case GDK_KP_Home:
1183             tc->width = 0.01;
1184             desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1185             sp_tweak_update_area(tc);
1186             ret = TRUE;
1187             break;
1188         case GDK_End:
1189         case GDK_KP_End:
1190             tc->width = 1.0;
1191             desktop->setToolboxAdjustmentValue ("altx-tweak", tc->width * 100);
1192             sp_tweak_update_area(tc);
1193             ret = TRUE;
1194             break;
1195         case GDK_x:
1196         case GDK_X:
1197             if (MOD__ALT_ONLY) {
1198                 desktop->setToolboxFocusTo ("altx-tweak");
1199                 ret = TRUE;
1200             }
1201             break;
1203         case GDK_Control_L:
1204         case GDK_Control_R:
1205             if (MOD__SHIFT) {
1206                 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_GROW);
1207             } else {
1208                 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_SHRINK);
1209             }
1210             break;
1211         case GDK_Shift_L:
1212         case GDK_Shift_R:
1213             if (MOD__CTRL) {
1214                 sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_GROW);
1215             }
1216             break;
1217         default:
1218             break;
1219         }
1220         break;
1222     case GDK_KEY_RELEASE:
1223         switch (get_group0_keyval(&event->key)) {
1224             case GDK_Control_L:
1225             case GDK_Control_R:
1226                 sp_tweak_switch_mode (tc, prefs_get_int_attribute("tools.tweak", "mode", 0));
1227                 tc->_message_context->clear();
1228                 break;
1229             case GDK_Shift_L:
1230             case GDK_Shift_R:
1231                 if (MOD__CTRL) {
1232                     sp_tweak_switch_mode_temporarily(tc, TWEAK_MODE_SHRINK);
1233                 }
1234                 break;
1235             break;
1236             default:
1237                 sp_tweak_switch_mode (tc, prefs_get_int_attribute("tools.tweak", "mode", 0));
1238                 break;
1239         }
1241     default:
1242         break;
1243     }
1245     if (!ret) {
1246         if (((SPEventContextClass *) parent_class)->root_handler) {
1247             ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
1248         }
1249     }
1251     return ret;
1255 /*
1256   Local Variables:
1257   mode:c++
1258   c-file-style:"stroustrup"
1259   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1260   indent-tabs-mode:nil
1261   fill-column:99
1262   End:
1263 */
1264 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :