1 #define __SP_DYNA_DRAW_CONTEXT_C__
3 /*
4 * Handwriting-like drawing mode
5 *
6 * Authors:
7 * Mitsuru Oka <oka326@parkcity.ne.jp>
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 * bulia byak <buliabyak@users.sf.net>
10 * MenTaLguY <mental@rydia.net>
11 *
12 * The original dynadraw code:
13 * Paul Haeberli <paul@sgi.com>
14 *
15 * Copyright (C) 1998 The Free Software Foundation
16 * Copyright (C) 1999-2005 authors
17 * Copyright (C) 2001-2002 Ximian, Inc.
18 * Copyright (C) 2005-2007 bulia byak
19 * Copyright (C) 2006 MenTaLguY
20 *
21 * Released under GNU GPL, read the file 'COPYING' for more information
22 */
24 #define noDYNA_DRAW_VERBOSE
26 #include "config.h"
28 #include <gtk/gtk.h>
29 #include <gdk/gdkkeysyms.h>
30 #include <glibmm/i18n.h>
31 #include <string>
32 #include <cstring>
33 #include <numeric>
35 #include "svg/svg.h"
36 #include "display/canvas-bpath.h"
37 #include "display/bezier-utils.h"
38 #include "display/curve.h"
39 #include <glib/gmem.h>
40 #include "macros.h"
41 #include "document.h"
42 #include "selection.h"
43 #include "desktop.h"
44 #include "desktop-events.h"
45 #include "desktop-handles.h"
46 #include "desktop-affine.h"
47 #include "desktop-style.h"
48 #include "message-context.h"
49 #include "prefs-utils.h"
50 #include "pixmaps/cursor-calligraphy.xpm"
51 #include "libnr/nr-matrix-ops.h"
52 #include "libnr/nr-scale-translate-ops.h"
53 #include "libnr/nr-convert2geom.h"
54 #include "xml/repr.h"
55 #include "context-fns.h"
56 #include "sp-item.h"
57 #include "inkscape.h"
58 #include "color.h"
59 #include "splivarot.h"
60 #include "sp-item-group.h"
61 #include "sp-shape.h"
62 #include "sp-path.h"
63 #include "sp-text.h"
64 #include "display/canvas-bpath.h"
65 #include "display/canvas-arena.h"
66 #include "livarot/Shape.h"
67 #include <2geom/isnan.h>
68 #include <2geom/pathvector.h>
70 #include "dyna-draw-context.h"
72 #define DDC_RED_RGBA 0xff0000ff
74 #define TOLERANCE_CALLIGRAPHIC 0.1
76 #define DYNA_EPSILON 0.5e-6
77 #define DYNA_EPSILON_START 0.5e-2
78 #define DYNA_VEL_START 1e-5
80 #define DYNA_MIN_WIDTH 1.0e-6
82 static void sp_dyna_draw_context_class_init(SPDynaDrawContextClass *klass);
83 static void sp_dyna_draw_context_init(SPDynaDrawContext *ddc);
84 static void sp_dyna_draw_context_dispose(GObject *object);
86 static void sp_dyna_draw_context_setup(SPEventContext *ec);
87 static void sp_dyna_draw_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
88 static gint sp_dyna_draw_context_root_handler(SPEventContext *ec, GdkEvent *event);
90 static void clear_current(SPDynaDrawContext *dc);
91 static void set_to_accumulated(SPDynaDrawContext *dc, bool unionize);
92 static void add_cap(SPCurve *curve, Geom::Point const &from, Geom::Point const &to, double rounding);
93 static bool accumulate_calligraphic(SPDynaDrawContext *dc);
95 static void fit_and_split(SPDynaDrawContext *ddc, gboolean release);
97 static void sp_dyna_draw_reset(SPDynaDrawContext *ddc, Geom::Point p);
98 static Geom::Point sp_dyna_draw_get_npoint(SPDynaDrawContext const *ddc, Geom::Point v);
99 static Geom::Point sp_dyna_draw_get_vpoint(SPDynaDrawContext const *ddc, Geom::Point n);
100 static void draw_temporary_box(SPDynaDrawContext *dc);
103 static SPEventContextClass *dd_parent_class = 0;
105 GType sp_dyna_draw_context_get_type(void)
106 {
107 static GType type = 0;
108 if (!type) {
109 GTypeInfo info = {
110 sizeof(SPDynaDrawContextClass),
111 0, // base_init
112 0, // base_finalize
113 (GClassInitFunc)sp_dyna_draw_context_class_init,
114 0, // class_finalize
115 0, // class_data
116 sizeof(SPDynaDrawContext),
117 0, // n_preallocs
118 (GInstanceInitFunc)sp_dyna_draw_context_init,
119 0 // value_table
120 };
121 type = g_type_register_static(SP_TYPE_COMMON_CONTEXT, "SPDynaDrawContext", &info, static_cast<GTypeFlags>(0));
122 }
123 return type;
124 }
126 static void
127 sp_dyna_draw_context_class_init(SPDynaDrawContextClass *klass)
128 {
129 GObjectClass *object_class = (GObjectClass *) klass;
130 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
132 dd_parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
134 object_class->dispose = sp_dyna_draw_context_dispose;
136 event_context_class->setup = sp_dyna_draw_context_setup;
137 event_context_class->set = sp_dyna_draw_context_set;
138 event_context_class->root_handler = sp_dyna_draw_context_root_handler;
139 }
141 static void
142 sp_dyna_draw_context_init(SPDynaDrawContext *ddc)
143 {
144 ddc->cursor_shape = cursor_calligraphy_xpm;
145 ddc->hot_x = 4;
146 ddc->hot_y = 4;
148 ddc->vel_thin = 0.1;
149 ddc->flatness = 0.9;
150 ddc->cap_rounding = 0.0;
152 ddc->abs_width = false;
153 ddc->keep_selected = true;
155 ddc->hatch_spacing = 0;
156 ddc->hatch_spacing_step = 0;
157 new (&ddc->hatch_pointer_past) std::list<double>();
158 new (&ddc->hatch_nearest_past) std::list<double>();
159 ddc->hatch_last_nearest = Geom::Point(0,0);
160 ddc->hatch_last_pointer = Geom::Point(0,0);
161 ddc->hatch_vector_accumulated = Geom::Point(0,0);
162 ddc->hatch_escaped = false;
163 ddc->hatch_area = NULL;
164 ddc->hatch_item = NULL;
165 ddc->hatch_livarot_path = NULL;
167 ddc->trace_bg = false;
168 }
170 static void
171 sp_dyna_draw_context_dispose(GObject *object)
172 {
173 SPDynaDrawContext *ddc = SP_DYNA_DRAW_CONTEXT(object);
175 if (ddc->hatch_area) {
176 gtk_object_destroy(GTK_OBJECT(ddc->hatch_area));
177 ddc->hatch_area = NULL;
178 }
181 G_OBJECT_CLASS(dd_parent_class)->dispose(object);
183 ddc->hatch_pointer_past.~list();
184 ddc->hatch_nearest_past.~list();
185 }
187 static void
188 sp_dyna_draw_context_setup(SPEventContext *ec)
189 {
190 SPDynaDrawContext *ddc = SP_DYNA_DRAW_CONTEXT(ec);
192 if (((SPEventContextClass *) dd_parent_class)->setup)
193 ((SPEventContextClass *) dd_parent_class)->setup(ec);
195 ddc->accumulated = new SPCurve();
196 ddc->currentcurve = new SPCurve();
198 ddc->cal1 = new SPCurve();
199 ddc->cal2 = new SPCurve();
201 ddc->currentshape = sp_canvas_item_new(sp_desktop_sketch(ec->desktop), SP_TYPE_CANVAS_BPATH, NULL);
202 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(ddc->currentshape), DDC_RED_RGBA, SP_WIND_RULE_EVENODD);
203 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(ddc->currentshape), 0x00000000, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
204 /* fixme: Cannot we cascade it to root more clearly? */
205 g_signal_connect(G_OBJECT(ddc->currentshape), "event", G_CALLBACK(sp_desktop_root_handler), ec->desktop);
207 {
208 /* TODO: this can be done either with an arcto, and should maybe also be put in a general file (other tools use this as well) */
209 SPCurve *c = new SPCurve();
210 const double C1 = 0.552;
211 c->moveto(-1,0);
212 c->curveto(-1, C1, -C1, 1, 0, 1 );
213 c->curveto(C1, 1, 1, C1, 1, 0 );
214 c->curveto(1, -C1, C1, -1, 0, -1 );
215 c->curveto(-C1, -1, -1, -C1, -1, 0 );
216 c->closepath();
217 ddc->hatch_area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
218 c->unref();
219 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(ddc->hatch_area), 0x00000000,(SPWindRule)0);
220 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(ddc->hatch_area), 0x0000007f, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
221 sp_canvas_item_hide(ddc->hatch_area);
222 }
224 sp_event_context_read(ec, "mass");
225 sp_event_context_read(ec, "wiggle");
226 sp_event_context_read(ec, "angle");
227 sp_event_context_read(ec, "width");
228 sp_event_context_read(ec, "thinning");
229 sp_event_context_read(ec, "tremor");
230 sp_event_context_read(ec, "flatness");
231 sp_event_context_read(ec, "tracebackground");
232 sp_event_context_read(ec, "usepressure");
233 sp_event_context_read(ec, "usetilt");
234 sp_event_context_read(ec, "abs_width");
235 sp_event_context_read(ec, "keep_selected");
236 sp_event_context_read(ec, "cap_rounding");
238 ddc->is_drawing = false;
240 ddc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
242 if (prefs_get_int_attribute("tools.calligraphic", "selcue", 0) != 0) {
243 ec->enableSelectionCue();
244 }
245 }
247 static void
248 sp_dyna_draw_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
249 {
250 SPDynaDrawContext *ddc = SP_DYNA_DRAW_CONTEXT(ec);
252 if (!strcmp(key, "tracebackground")) {
253 ddc->trace_bg = (val && strcmp(val, "0"));
254 } else if (!strcmp(key, "keep_selected")) {
255 ddc->keep_selected = (val && strcmp(val, "0"));
256 } else {
257 //pass on up to parent class to handle common attributes.
258 if ( dd_parent_class->set ) {
259 dd_parent_class->set(ec, key, val);
260 }
261 }
263 //g_print("DDC: %g %g %g %g\n", ddc->mass, ddc->drag, ddc->angle, ddc->width);
264 }
266 static double
267 flerp(double f0, double f1, double p)
268 {
269 return f0 + ( f1 - f0 ) * p;
270 }
272 /* Get normalized point */
273 static Geom::Point
274 sp_dyna_draw_get_npoint(SPDynaDrawContext const *dc, Geom::Point v)
275 {
276 Geom::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area();
277 double const max = MAX ( drect.dimensions()[Geom::X], drect.dimensions()[Geom::Y] );
278 return Geom::Point(( v[Geom::X] - drect.min()[Geom::X] ) / max, ( v[Geom::Y] - drect.min()[Geom::Y] ) / max);
279 }
281 /* Get view point */
282 static Geom::Point
283 sp_dyna_draw_get_vpoint(SPDynaDrawContext const *dc, Geom::Point n)
284 {
285 Geom::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area();
286 double const max = MAX ( drect.dimensions()[Geom::X], drect.dimensions()[Geom::Y] );
287 return Geom::Point(n[Geom::X] * max + drect.min()[Geom::X], n[Geom::Y] * max + drect.min()[Geom::Y]);
288 }
290 static void
291 sp_dyna_draw_reset(SPDynaDrawContext *dc, Geom::Point p)
292 {
293 dc->last = dc->cur = sp_dyna_draw_get_npoint(dc, p);
294 dc->vel = Geom::Point(0,0);
295 dc->vel_max = 0;
296 dc->acc = Geom::Point(0,0);
297 dc->ang = Geom::Point(0,0);
298 dc->del = Geom::Point(0,0);
299 }
301 static void
302 sp_dyna_draw_extinput(SPDynaDrawContext *dc, GdkEvent *event)
303 {
304 if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &dc->pressure))
305 dc->pressure = CLAMP (dc->pressure, DDC_MIN_PRESSURE, DDC_MAX_PRESSURE);
306 else
307 dc->pressure = DDC_DEFAULT_PRESSURE;
309 if (gdk_event_get_axis (event, GDK_AXIS_XTILT, &dc->xtilt))
310 dc->xtilt = CLAMP (dc->xtilt, DDC_MIN_TILT, DDC_MAX_TILT);
311 else
312 dc->xtilt = DDC_DEFAULT_TILT;
314 if (gdk_event_get_axis (event, GDK_AXIS_YTILT, &dc->ytilt))
315 dc->ytilt = CLAMP (dc->ytilt, DDC_MIN_TILT, DDC_MAX_TILT);
316 else
317 dc->ytilt = DDC_DEFAULT_TILT;
318 }
321 static gboolean
322 sp_dyna_draw_apply(SPDynaDrawContext *dc, Geom::Point p)
323 {
324 Geom::Point n = sp_dyna_draw_get_npoint(dc, p);
326 /* Calculate mass and drag */
327 double const mass = flerp(1.0, 160.0, dc->mass);
328 double const drag = flerp(0.0, 0.5, dc->drag * dc->drag);
330 /* Calculate force and acceleration */
331 Geom::Point force = n - dc->cur;
333 // If force is below the absolute threshold DYNA_EPSILON,
334 // or we haven't yet reached DYNA_VEL_START (i.e. at the beginning of stroke)
335 // _and_ the force is below the (higher) DYNA_EPSILON_START threshold,
336 // discard this move.
337 // This prevents flips, blobs, and jerks caused by microscopic tremor of the tablet pen,
338 // especially bothersome at the start of the stroke where we don't yet have the inertia to
339 // smooth them out.
340 if ( NR::L2(force) < DYNA_EPSILON || (dc->vel_max < DYNA_VEL_START && NR::L2(force) < DYNA_EPSILON_START)) {
341 return FALSE;
342 }
344 dc->acc = force / mass;
346 /* Calculate new velocity */
347 dc->vel += dc->acc;
349 if (NR::L2(dc->vel) > dc->vel_max)
350 dc->vel_max = NR::L2(dc->vel);
352 /* Calculate angle of drawing tool */
354 double a1;
355 if (dc->usetilt) {
356 // 1a. calculate nib angle from input device tilt:
357 gdouble length = std::sqrt(dc->xtilt*dc->xtilt + dc->ytilt*dc->ytilt);;
359 if (length > 0) {
360 Geom::Point ang1 = Geom::Point(dc->ytilt/length, dc->xtilt/length);
361 a1 = atan2(ang1);
362 }
363 else
364 a1 = 0.0;
365 }
366 else {
367 // 1b. fixed dc->angle (absolutely flat nib):
368 double const radians = ( (dc->angle - 90) / 180.0 ) * M_PI;
369 Geom::Point ang1 = Geom::Point(-sin(radians), cos(radians));
370 a1 = atan2(ang1);
371 }
373 // 2. perpendicular to dc->vel (absolutely non-flat nib):
374 gdouble const mag_vel = NR::L2(dc->vel);
375 if ( mag_vel < DYNA_EPSILON ) {
376 return FALSE;
377 }
378 Geom::Point ang2 = NR::rot90(dc->vel) / mag_vel;
380 // 3. Average them using flatness parameter:
381 // calculate angles
382 double a2 = atan2(ang2);
383 // flip a2 to force it to be in the same half-circle as a1
384 bool flipped = false;
385 if (fabs (a2-a1) > 0.5*M_PI) {
386 a2 += M_PI;
387 flipped = true;
388 }
389 // normalize a2
390 if (a2 > M_PI)
391 a2 -= 2*M_PI;
392 if (a2 < -M_PI)
393 a2 += 2*M_PI;
394 // find the flatness-weighted bisector angle, unflip if a2 was flipped
395 // FIXME: when dc->vel is oscillating around the fixed angle, the new_ang flips back and forth. How to avoid this?
396 double new_ang = a1 + (1 - dc->flatness) * (a2 - a1) - (flipped? M_PI : 0);
398 // Try to detect a sudden flip when the new angle differs too much from the previous for the
399 // current velocity; in that case discard this move
400 double angle_delta = NR::L2(Geom::Point (cos (new_ang), sin (new_ang)) - dc->ang);
401 if ( angle_delta / NR::L2(dc->vel) > 4000 ) {
402 return FALSE;
403 }
405 // convert to point
406 dc->ang = Geom::Point (cos (new_ang), sin (new_ang));
408 // g_print ("force %g acc %g vel_max %g vel %g a1 %g a2 %g new_ang %g\n", NR::L2(force), NR::L2(dc->acc), dc->vel_max, NR::L2(dc->vel), a1, a2, new_ang);
410 /* Apply drag */
411 dc->vel *= 1.0 - drag;
413 /* Update position */
414 dc->last = dc->cur;
415 dc->cur += dc->vel;
417 return TRUE;
418 }
420 static void
421 sp_dyna_draw_brush(SPDynaDrawContext *dc)
422 {
423 g_assert( dc->npoints >= 0 && dc->npoints < SAMPLING_SIZE );
425 // How much velocity thins strokestyle
426 double vel_thin = flerp (0, 160, dc->vel_thin);
428 // Influence of pressure on thickness
429 double pressure_thick = (dc->usepressure ? dc->pressure : 1.0);
431 // get the real brush point, not the same as pointer (affected by hatch tracking and/or mass
432 // drag)
433 Geom::Point brush = sp_dyna_draw_get_vpoint(dc, dc->cur);
434 Geom::Point brush_w = SP_EVENT_CONTEXT(dc)->desktop->d2w(brush);
436 double trace_thick = 1;
437 if (dc->trace_bg) {
438 // pick single pixel
439 NRPixBlock pb;
440 int x = (int) floor(brush_w[NR::X]);
441 int y = (int) floor(brush_w[NR::Y]);
442 nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x, y, x+1, y+1, TRUE);
443 sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(SP_EVENT_CONTEXT(dc)->desktop)), &pb);
444 const unsigned char *s = NR_PIXBLOCK_PX(&pb);
445 double R = s[0] / 255.0;
446 double G = s[1] / 255.0;
447 double B = s[2] / 255.0;
448 double A = s[3] / 255.0;
449 double max = MAX (MAX (R, G), B);
450 double min = MIN (MIN (R, G), B);
451 double L = A * (max + min)/2 + (1 - A); // blend with white bg
452 trace_thick = 1 - L;
453 //g_print ("L %g thick %g\n", L, trace_thick);
454 }
456 double width = (pressure_thick * trace_thick - vel_thin * NR::L2(dc->vel)) * dc->width;
458 double tremble_left = 0, tremble_right = 0;
459 if (dc->tremor > 0) {
460 // obtain two normally distributed random variables, using polar Box-Muller transform
461 double x1, x2, w, y1, y2;
462 do {
463 x1 = 2.0 * g_random_double_range(0,1) - 1.0;
464 x2 = 2.0 * g_random_double_range(0,1) - 1.0;
465 w = x1 * x1 + x2 * x2;
466 } while ( w >= 1.0 );
467 w = sqrt( (-2.0 * log( w ) ) / w );
468 y1 = x1 * w;
469 y2 = x2 * w;
471 // deflect both left and right edges randomly and independently, so that:
472 // (1) dc->tremor=1 corresponds to sigma=1, decreasing dc->tremor narrows the bell curve;
473 // (2) deflection depends on width, but is upped for small widths for better visual uniformity across widths;
474 // (3) deflection somewhat depends on speed, to prevent fast strokes looking
475 // comparatively smooth and slow ones excessively jittery
476 tremble_left = (y1)*dc->tremor * (0.15 + 0.8*width) * (0.35 + 14*NR::L2(dc->vel));
477 tremble_right = (y2)*dc->tremor * (0.15 + 0.8*width) * (0.35 + 14*NR::L2(dc->vel));
478 }
480 if ( width < 0.02 * dc->width ) {
481 width = 0.02 * dc->width;
482 }
484 double dezoomify_factor = 0.05 * 1000;
485 if (!dc->abs_width) {
486 dezoomify_factor /= SP_EVENT_CONTEXT(dc)->desktop->current_zoom();
487 }
489 Geom::Point del_left = dezoomify_factor * (width + tremble_left) * dc->ang;
490 Geom::Point del_right = dezoomify_factor * (width + tremble_right) * dc->ang;
492 dc->point1[dc->npoints] = brush + del_left;
493 dc->point2[dc->npoints] = brush - del_right;
495 dc->del = 0.5*(del_left + del_right);
497 dc->npoints++;
498 }
500 void
501 sp_ddc_update_toolbox (SPDesktop *desktop, const gchar *id, double value)
502 {
503 desktop->setToolboxAdjustmentValue (id, value);
504 }
506 static void
507 calligraphic_cancel(SPDynaDrawContext *dc)
508 {
509 SPDesktop *desktop = SP_EVENT_CONTEXT(dc)->desktop;
510 dc->dragging = FALSE;
511 dc->is_drawing = false;
512 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
513 /* Remove all temporary line segments */
514 while (dc->segments) {
515 gtk_object_destroy(GTK_OBJECT(dc->segments->data));
516 dc->segments = g_slist_remove(dc->segments, dc->segments->data);
517 }
518 /* reset accumulated curve */
519 dc->accumulated->reset();
520 clear_current(dc);
521 if (dc->repr) {
522 dc->repr = NULL;
523 }
524 }
527 gint
528 sp_dyna_draw_context_root_handler(SPEventContext *event_context,
529 GdkEvent *event)
530 {
531 SPDynaDrawContext *dc = SP_DYNA_DRAW_CONTEXT(event_context);
532 SPDesktop *desktop = event_context->desktop;
534 gint ret = FALSE;
536 switch (event->type) {
537 case GDK_BUTTON_PRESS:
538 if (event->button.button == 1 && !event_context->space_panning) {
540 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
542 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
543 return TRUE;
544 }
546 Geom::Point const button_w(event->button.x,
547 event->button.y);
548 Geom::Point const button_dt(desktop->w2d(button_w));
549 sp_dyna_draw_reset(dc, button_dt);
550 sp_dyna_draw_extinput(dc, event);
551 sp_dyna_draw_apply(dc, button_dt);
552 dc->accumulated->reset();
553 if (dc->repr) {
554 dc->repr = NULL;
555 }
557 /* initialize first point */
558 dc->npoints = 0;
560 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
561 ( GDK_KEY_PRESS_MASK |
562 GDK_BUTTON_RELEASE_MASK |
563 GDK_POINTER_MOTION_MASK |
564 GDK_BUTTON_PRESS_MASK ),
565 NULL,
566 event->button.time);
568 ret = TRUE;
570 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
571 dc->is_drawing = true;
572 }
573 break;
574 case GDK_MOTION_NOTIFY:
575 {
576 Geom::Point const motion_w(event->motion.x,
577 event->motion.y);
578 Geom::Point motion_dt(desktop->w2d(motion_w));
579 sp_dyna_draw_extinput(dc, event);
581 dc->_message_context->clear();
583 // for hatching:
584 double hatch_dist = 0;
585 Geom::Point hatch_unit_vector(0,0);
586 Geom::Point nearest(0,0);
587 Geom::Point pointer(0,0);
588 Geom::Matrix motion_to_curve(Geom::identity());
590 if (event->motion.state & GDK_CONTROL_MASK) { // hatching - sense the item
592 SPItem *selected = sp_desktop_selection(desktop)->singleItem();
593 if (selected && (SP_IS_SHAPE(selected) || SP_IS_TEXT(selected))) {
594 // One item selected, and it's a path;
595 // let's try to track it as a guide
597 if (selected != dc->hatch_item) {
598 dc->hatch_item = selected;
599 if (dc->hatch_livarot_path)
600 delete dc->hatch_livarot_path;
601 dc->hatch_livarot_path = Path_for_item (dc->hatch_item, true, true);
602 dc->hatch_livarot_path->ConvertWithBackData(0.01);
603 }
605 // calculate pointer point in the guide item's coords
606 motion_to_curve = sp_item_dt2i_affine(selected) * sp_item_i2doc_affine(selected);
607 pointer = motion_dt * motion_to_curve;
609 // calculate the nearest point on the guide path
610 boost::optional<Path::cut_position> position = get_nearest_position_on_Path(dc->hatch_livarot_path, pointer);
611 nearest = get_point_on_Path(dc->hatch_livarot_path, position->piece, position->t);
614 // distance from pointer to nearest
615 hatch_dist = NR::L2(pointer - nearest);
616 // unit-length vector
617 hatch_unit_vector = (pointer - nearest)/hatch_dist;
619 dc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Guide path selected</b>; start drawing along the guide with <b>Ctrl</b>"));
620 } else {
621 dc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Select a guide path</b> to track with <b>Ctrl</b>"));
622 }
623 }
625 if ( dc->is_drawing && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
626 dc->dragging = TRUE;
628 if (event->motion.state & GDK_CONTROL_MASK && dc->hatch_item) { // hatching
630 #define SPEED_ELEMENTS 12
631 #define SPEED_MIN 0.12
632 #define SPEED_NORMAL 0.65
634 // speed is the movement of the nearest point along the guide path, divided by
635 // the movement of the pointer at the same period; it is averaged for the last
636 // SPEED_ELEMENTS motion events. Normally, as you track the guide path, speed
637 // is about 1, i.e. the nearest point on the path is moved by about the same
638 // distance as the pointer. If the speed starts to decrease, we are losing
639 // contact with the guide; if it drops below SPEED_MIN, we are on our own and
640 // not attracted to guide anymore. Most often this happens when you have
641 // tracked to the end of a guide calligraphic stroke and keep moving
642 // further. We try to handle this situation gracefully: not stick with the
643 // guide forever but let go of it smoothly and without sharp jerks (non-zero
644 // mass recommended; with zero mass, jerks are still quite noticeable).
646 double speed = 1;
647 if (NR::L2(dc->hatch_last_nearest) != 0) {
648 // the distance nearest moved since the last motion event
649 double nearest_moved = NR::L2(nearest - dc->hatch_last_nearest);
650 // the distance pointer moved since the last motion event
651 double pointer_moved = NR::L2(pointer - dc->hatch_last_pointer);
652 // store them in stacks limited to SPEED_ELEMENTS
653 dc->hatch_nearest_past.push_front(nearest_moved);
654 if (dc->hatch_nearest_past.size() > SPEED_ELEMENTS)
655 dc->hatch_nearest_past.pop_back();
656 dc->hatch_pointer_past.push_front(pointer_moved);
657 if (dc->hatch_pointer_past.size() > SPEED_ELEMENTS)
658 dc->hatch_pointer_past.pop_back();
660 // If the stacks are full,
661 if (dc->hatch_nearest_past.size() == SPEED_ELEMENTS) {
662 // calculate the sums of all stored movements
663 double nearest_sum = std::accumulate (dc->hatch_nearest_past.begin(), dc->hatch_nearest_past.end(), 0.0);
664 double pointer_sum = std::accumulate (dc->hatch_pointer_past.begin(), dc->hatch_pointer_past.end(), 0.0);
665 // and divide to get the speed
666 speed = nearest_sum/pointer_sum;
667 //g_print ("nearest sum %g pointer_sum %g speed %g\n", nearest_sum, pointer_sum, speed);
668 }
669 }
671 if ( dc->hatch_escaped // already escaped, do not reattach
672 || (speed < SPEED_MIN) // stuck; most likely reached end of traced stroke
673 || (dc->hatch_spacing > 0 && hatch_dist > 50 * dc->hatch_spacing) // went too far from the guide
674 ) {
675 // We are NOT attracted to the guide!
677 //g_print ("\nlast_nearest %g %g nearest %g %g pointer %g %g pos %d %g\n", dc->last_nearest[NR::X], dc->last_nearest[NR::Y], nearest[NR::X], nearest[NR::Y], pointer[NR::X], pointer[NR::Y], position->piece, position->t);
679 // Remember hatch_escaped so we don't get
680 // attracted again until the end of this stroke
681 dc->hatch_escaped = true;
683 } else {
685 // Calculate angle cosine of this vector-to-guide and all past vectors
686 // summed, to detect if we accidentally flipped to the other side of the
687 // guide
688 double dot = NR::dot (pointer - nearest, dc->hatch_vector_accumulated);
689 dot /= NR::L2(pointer - nearest) * NR::L2(dc->hatch_vector_accumulated);
691 if (dc->hatch_spacing != 0) { // spacing was already set
692 double target;
693 if (speed > SPEED_NORMAL) {
694 // all ok, strictly obey the spacing
695 target = dc->hatch_spacing;
696 } else {
697 // looks like we're starting to lose speed,
698 // so _gradually_ let go attraction to prevent jerks
699 target = (dc->hatch_spacing * speed + hatch_dist * (SPEED_NORMAL - speed))/SPEED_NORMAL;
700 }
701 if (!IS_NAN(dot) && dot < -0.5) {// flip
702 target = -target;
703 }
705 // This is the track pointer that we will use instead of the real one
706 Geom::Point new_pointer = nearest + target * hatch_unit_vector;
708 // some limited feedback: allow persistent pulling to slightly change
709 // the spacing
710 dc->hatch_spacing += (hatch_dist - dc->hatch_spacing)/3500;
712 // return it to the desktop coords
713 motion_dt = new_pointer * motion_to_curve.inverse();
715 } else {
716 // this is the first motion event, set the dist
717 dc->hatch_spacing = hatch_dist;
718 }
720 // remember last points
721 dc->hatch_last_pointer = pointer;
722 dc->hatch_last_nearest = nearest;
723 dc->hatch_vector_accumulated += (pointer - nearest);
724 }
726 dc->_message_context->set(Inkscape::NORMAL_MESSAGE, dc->hatch_escaped? _("Tracking: <b>connection to guide path lost!</b>") : _("<b>Tracking</b> a guide path"));
728 } else {
729 dc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drawing</b> a calligraphic stroke"));
730 }
732 if (!sp_dyna_draw_apply(dc, motion_dt)) {
733 ret = TRUE;
734 break;
735 }
737 if ( dc->cur != dc->last ) {
738 sp_dyna_draw_brush(dc);
739 g_assert( dc->npoints > 0 );
740 fit_and_split(dc, FALSE);
741 }
742 ret = TRUE;
743 }
745 // Draw the hatching circle if necessary
746 if (event->motion.state & GDK_CONTROL_MASK) {
747 if (dc->hatch_spacing == 0 && hatch_dist != 0) {
748 // Haven't set spacing yet: gray, center free, update radius live
749 Geom::Point c = desktop->w2d(motion_w);
750 NR::Matrix const sm (Geom::Scale(hatch_dist, hatch_dist) * Geom::Translate(c));
751 sp_canvas_item_affine_absolute(dc->hatch_area, sm);
752 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->hatch_area), 0x7f7f7fff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
753 sp_canvas_item_show(dc->hatch_area);
754 } else if (dc->dragging && !dc->hatch_escaped) {
755 // Tracking: green, center snapped, fixed radius
756 Geom::Point c = motion_dt;
757 NR::Matrix const sm (Geom::Scale(dc->hatch_spacing, dc->hatch_spacing) * Geom::Translate(c));
758 sp_canvas_item_affine_absolute(dc->hatch_area, sm);
759 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->hatch_area), 0x00FF00ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
760 sp_canvas_item_show(dc->hatch_area);
761 } else if (dc->dragging && dc->hatch_escaped) {
762 // Tracking escaped: red, center free, fixed radius
763 Geom::Point c = desktop->w2d(motion_w);
764 NR::Matrix const sm (Geom::Scale(dc->hatch_spacing, dc->hatch_spacing) * Geom::Translate(c));
766 sp_canvas_item_affine_absolute(dc->hatch_area, sm);
767 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->hatch_area), 0xFF0000ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
768 sp_canvas_item_show(dc->hatch_area);
769 } else {
770 // Not drawing but spacing set: gray, center snapped, fixed radius
771 Geom::Point c = (nearest + dc->hatch_spacing * hatch_unit_vector) * motion_to_curve.inverse();
772 if (!IS_NAN(c[NR::X]) && !IS_NAN(c[NR::Y])) {
773 NR::Matrix const sm (Geom::Scale(dc->hatch_spacing, dc->hatch_spacing) * Geom::Translate(c));
774 sp_canvas_item_affine_absolute(dc->hatch_area, sm);
775 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->hatch_area), 0x7f7f7fff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
776 sp_canvas_item_show(dc->hatch_area);
777 }
778 }
779 } else {
780 sp_canvas_item_hide(dc->hatch_area);
781 }
782 }
783 break;
786 case GDK_BUTTON_RELEASE:
787 {
788 Geom::Point const motion_w(event->button.x, event->button.y);
789 Geom::Point const motion_dt(desktop->w2d(motion_w));
791 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
792 sp_canvas_end_forced_full_redraws(desktop->canvas);
793 dc->is_drawing = false;
795 if (dc->dragging && event->button.button == 1 && !event_context->space_panning) {
796 dc->dragging = FALSE;
798 sp_dyna_draw_apply(dc, motion_dt);
800 /* Remove all temporary line segments */
801 while (dc->segments) {
802 gtk_object_destroy(GTK_OBJECT(dc->segments->data));
803 dc->segments = g_slist_remove(dc->segments, dc->segments->data);
804 }
806 /* Create object */
807 fit_and_split(dc, TRUE);
808 if (accumulate_calligraphic(dc))
809 set_to_accumulated(dc, event->button.state & GDK_SHIFT_MASK); // performs document_done
810 else
811 g_warning ("Failed to create path: invalid data in dc->cal1 or dc->cal2");
813 /* reset accumulated curve */
814 dc->accumulated->reset();
816 clear_current(dc);
817 if (dc->repr) {
818 dc->repr = NULL;
819 }
821 if (!dc->hatch_pointer_past.empty()) dc->hatch_pointer_past.clear();
822 if (!dc->hatch_nearest_past.empty()) dc->hatch_nearest_past.clear();
823 dc->hatch_last_nearest = Geom::Point(0,0);
824 dc->hatch_last_pointer = Geom::Point(0,0);
825 dc->hatch_vector_accumulated = Geom::Point(0,0);
826 dc->hatch_escaped = false;
827 dc->hatch_item = NULL;
828 dc->hatch_livarot_path = NULL;
830 if (dc->hatch_spacing != 0 && !dc->keep_selected) {
831 // we do not select the newly drawn path, so increase spacing by step
832 if (dc->hatch_spacing_step == 0) {
833 dc->hatch_spacing_step = dc->hatch_spacing;
834 }
835 dc->hatch_spacing += dc->hatch_spacing_step;
836 }
838 dc->_message_context->clear();
839 ret = TRUE;
840 }
841 break;
842 }
844 case GDK_KEY_PRESS:
845 switch (get_group0_keyval (&event->key)) {
846 case GDK_Up:
847 case GDK_KP_Up:
848 if (!MOD__CTRL_ONLY) {
849 dc->angle += 5.0;
850 if (dc->angle > 90.0)
851 dc->angle = 90.0;
852 sp_ddc_update_toolbox (desktop, "calligraphy-angle", dc->angle);
853 ret = TRUE;
854 }
855 break;
856 case GDK_Down:
857 case GDK_KP_Down:
858 if (!MOD__CTRL_ONLY) {
859 dc->angle -= 5.0;
860 if (dc->angle < -90.0)
861 dc->angle = -90.0;
862 sp_ddc_update_toolbox (desktop, "calligraphy-angle", dc->angle);
863 ret = TRUE;
864 }
865 break;
866 case GDK_Right:
867 case GDK_KP_Right:
868 if (!MOD__CTRL_ONLY) {
869 dc->width += 0.01;
870 if (dc->width > 1.0)
871 dc->width = 1.0;
872 sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width * 100); // the same spinbutton is for alt+x
873 ret = TRUE;
874 }
875 break;
876 case GDK_Left:
877 case GDK_KP_Left:
878 if (!MOD__CTRL_ONLY) {
879 dc->width -= 0.01;
880 if (dc->width < 0.01)
881 dc->width = 0.01;
882 sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width * 100);
883 ret = TRUE;
884 }
885 break;
886 case GDK_Home:
887 case GDK_KP_Home:
888 dc->width = 0.01;
889 sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width * 100);
890 ret = TRUE;
891 break;
892 case GDK_End:
893 case GDK_KP_End:
894 dc->width = 1.0;
895 sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width * 100);
896 ret = TRUE;
897 break;
898 case GDK_x:
899 case GDK_X:
900 if (MOD__ALT_ONLY) {
901 desktop->setToolboxFocusTo ("altx-calligraphy");
902 ret = TRUE;
903 }
904 break;
905 case GDK_Escape:
906 if (dc->is_drawing) {
907 // if drawing, cancel, otherwise pass it up for deselecting
908 calligraphic_cancel (dc);
909 ret = TRUE;
910 }
911 break;
912 case GDK_z:
913 case GDK_Z:
914 if (MOD__CTRL_ONLY && dc->is_drawing) {
915 // if drawing, cancel, otherwise pass it up for undo
916 calligraphic_cancel (dc);
917 ret = TRUE;
918 }
919 break;
920 default:
921 break;
922 }
923 break;
925 case GDK_KEY_RELEASE:
926 switch (get_group0_keyval(&event->key)) {
927 case GDK_Control_L:
928 case GDK_Control_R:
929 dc->_message_context->clear();
930 dc->hatch_spacing = 0;
931 dc->hatch_spacing_step = 0;
932 break;
933 default:
934 break;
935 }
937 default:
938 break;
939 }
941 if (!ret) {
942 if (((SPEventContextClass *) dd_parent_class)->root_handler) {
943 ret = ((SPEventContextClass *) dd_parent_class)->root_handler(event_context, event);
944 }
945 }
947 return ret;
948 }
951 static void
952 clear_current(SPDynaDrawContext *dc)
953 {
954 /* reset bpath */
955 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), NULL);
956 /* reset curve */
957 dc->currentcurve->reset();
958 dc->cal1->reset();
959 dc->cal2->reset();
960 /* reset points */
961 dc->npoints = 0;
962 }
964 static void
965 set_to_accumulated(SPDynaDrawContext *dc, bool unionize)
966 {
967 SPDesktop *desktop = SP_EVENT_CONTEXT(dc)->desktop;
969 if (!dc->accumulated->is_empty()) {
970 if (!dc->repr) {
971 /* Create object */
972 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
973 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
975 /* Set style */
976 sp_desktop_apply_style_tool (desktop, repr, "tools.calligraphic", false);
978 dc->repr = repr;
980 SPItem *item=SP_ITEM(desktop->currentLayer()->appendChildRepr(dc->repr));
981 Inkscape::GC::release(dc->repr);
982 item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
983 item->updateRepr();
984 }
985 Geom::PathVector pathv = dc->accumulated->get_pathvector() * sp_desktop_dt2root_affine(desktop);
986 gchar *str = sp_svg_write_path(pathv);
987 g_assert( str != NULL );
988 dc->repr->setAttribute("d", str);
989 g_free(str);
991 if (unionize) {
992 sp_desktop_selection(desktop)->add(dc->repr);
993 sp_selected_path_union_skip_undo(desktop);
994 } else {
995 if (dc->keep_selected) {
996 sp_desktop_selection(desktop)->set(dc->repr);
997 }
998 }
1000 } else {
1001 if (dc->repr) {
1002 sp_repr_unparent(dc->repr);
1003 }
1004 dc->repr = NULL;
1005 }
1007 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_CALLIGRAPHIC,
1008 _("Draw calligraphic stroke"));
1009 }
1011 static void
1012 add_cap(SPCurve *curve,
1013 Geom::Point const &from,
1014 Geom::Point const &to,
1015 double rounding)
1016 {
1017 if (NR::L2( to - from ) > DYNA_EPSILON) {
1018 Geom::Point vel = rounding * NR::rot90( to - from ) / sqrt(2.0);
1019 double mag = NR::L2(vel);
1021 Geom::Point v = mag * NR::rot90( to - from ) / NR::L2( to - from );
1022 curve->curveto(from + v, to + v, to);
1023 }
1024 }
1026 static bool
1027 accumulate_calligraphic(SPDynaDrawContext *dc)
1028 {
1029 if (
1030 dc->cal1->is_empty() ||
1031 dc->cal2->is_empty() ||
1032 (dc->cal1->get_segment_count() <= 0) ||
1033 dc->cal1->first_path()->closed()
1034 ) {
1035 dc->cal1->reset();
1036 dc->cal2->reset();
1037 return false; // failure
1038 }
1040 SPCurve *rev_cal2 = dc->cal2->create_reverse();
1041 if (
1042 (rev_cal2->get_segment_count() <= 0) ||
1043 rev_cal2->first_path()->closed()
1044 ) {
1045 rev_cal2->unref();
1046 dc->cal1->reset();
1047 dc->cal2->reset();
1048 return false; // failure
1049 }
1051 Geom::CubicBezier const * dc_cal1_firstseg = dynamic_cast<Geom::CubicBezier const *>( dc->cal1->first_segment() );
1052 Geom::CubicBezier const * rev_cal2_firstseg = dynamic_cast<Geom::CubicBezier const *>( rev_cal2->first_segment() );
1053 Geom::CubicBezier const * dc_cal1_lastseg = dynamic_cast<Geom::CubicBezier const *>( dc->cal1->last_segment() );
1054 Geom::CubicBezier const * rev_cal2_lastseg = dynamic_cast<Geom::CubicBezier const *>( rev_cal2->last_segment() );
1056 if (
1057 !dc_cal1_firstseg ||
1058 !rev_cal2_firstseg ||
1059 !dc_cal1_lastseg ||
1060 !rev_cal2_lastseg
1061 ) {
1062 rev_cal2->unref();
1063 dc->cal1->reset();
1064 dc->cal2->reset();
1065 return false; // failure
1066 }
1068 dc->accumulated->reset(); /* Is this required ?? */
1070 dc->accumulated->append(dc->cal1, false);
1072 add_cap(dc->accumulated, (*dc_cal1_lastseg)[3], (*rev_cal2_firstseg)[0], dc->cap_rounding);
1074 dc->accumulated->append(rev_cal2, true);
1076 add_cap(dc->accumulated, (*rev_cal2_lastseg)[3], (*dc_cal1_firstseg)[0], dc->cap_rounding);
1078 dc->accumulated->closepath();
1080 rev_cal2->unref();
1082 dc->cal1->reset();
1083 dc->cal2->reset();
1085 return true; // success
1086 }
1088 static double square(double const x)
1089 {
1090 return x * x;
1091 }
1093 static void
1094 fit_and_split(SPDynaDrawContext *dc, gboolean release)
1095 {
1096 SPDesktop *desktop = SP_EVENT_CONTEXT(dc)->desktop;
1098 double const tolerance_sq = square( desktop->w2d().descrim() * TOLERANCE_CALLIGRAPHIC );
1100 #ifdef DYNA_DRAW_VERBOSE
1101 g_print("[F&S:R=%c]", release?'T':'F');
1102 #endif
1104 if (!( dc->npoints > 0 && dc->npoints < SAMPLING_SIZE ))
1105 return; // just clicked
1107 if ( dc->npoints == SAMPLING_SIZE - 1 || release ) {
1108 #define BEZIER_SIZE 4
1109 #define BEZIER_MAX_BEZIERS 8
1110 #define BEZIER_MAX_LENGTH ( BEZIER_SIZE * BEZIER_MAX_BEZIERS )
1112 #ifdef DYNA_DRAW_VERBOSE
1113 g_print("[F&S:#] dc->npoints:%d, release:%s\n",
1114 dc->npoints, release ? "TRUE" : "FALSE");
1115 #endif
1117 /* Current calligraphic */
1118 if ( dc->cal1->is_empty() || dc->cal2->is_empty() ) {
1119 /* dc->npoints > 0 */
1120 /* g_print("calligraphics(1|2) reset\n"); */
1121 dc->cal1->reset();
1122 dc->cal2->reset();
1124 dc->cal1->moveto(dc->point1[0]);
1125 dc->cal2->moveto(dc->point2[0]);
1126 }
1128 Geom::Point b1[BEZIER_MAX_LENGTH];
1129 gint const nb1 = sp_bezier_fit_cubic_r(b1, dc->point1, dc->npoints,
1130 tolerance_sq, BEZIER_MAX_BEZIERS);
1131 g_assert( nb1 * BEZIER_SIZE <= gint(G_N_ELEMENTS(b1)) );
1133 Geom::Point b2[BEZIER_MAX_LENGTH];
1134 gint const nb2 = sp_bezier_fit_cubic_r(b2, dc->point2, dc->npoints,
1135 tolerance_sq, BEZIER_MAX_BEZIERS);
1136 g_assert( nb2 * BEZIER_SIZE <= gint(G_N_ELEMENTS(b2)) );
1138 if ( nb1 != -1 && nb2 != -1 ) {
1139 /* Fit and draw and reset state */
1140 #ifdef DYNA_DRAW_VERBOSE
1141 g_print("nb1:%d nb2:%d\n", nb1, nb2);
1142 #endif
1143 /* CanvasShape */
1144 if (! release) {
1145 dc->currentcurve->reset();
1146 dc->currentcurve->moveto(b1[0]);
1147 for (Geom::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) {
1148 dc->currentcurve->curveto(bp1[1], bp1[2], bp1[3]);
1149 }
1150 dc->currentcurve->lineto(b2[BEZIER_SIZE*(nb2-1) + 3]);
1151 for (Geom::Point *bp2 = b2 + BEZIER_SIZE * ( nb2 - 1 ); bp2 >= b2; bp2 -= BEZIER_SIZE) {
1152 dc->currentcurve->curveto(bp2[2], bp2[1], bp2[0]);
1153 }
1154 // FIXME: dc->segments is always NULL at this point??
1155 if (!dc->segments) { // first segment
1156 add_cap(dc->currentcurve, b2[0], b1[0], dc->cap_rounding);
1157 }
1158 dc->currentcurve->closepath();
1159 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), dc->currentcurve);
1160 }
1162 /* Current calligraphic */
1163 for (Geom::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) {
1164 dc->cal1->curveto(bp1[1], bp1[2], bp1[3]);
1165 }
1166 for (Geom::Point *bp2 = b2; bp2 < b2 + BEZIER_SIZE * nb2; bp2 += BEZIER_SIZE) {
1167 dc->cal2->curveto(bp2[1], bp2[2], bp2[3]);
1168 }
1169 } else {
1170 /* fixme: ??? */
1171 #ifdef DYNA_DRAW_VERBOSE
1172 g_print("[fit_and_split] failed to fit-cubic.\n");
1173 #endif
1174 draw_temporary_box(dc);
1176 for (gint i = 1; i < dc->npoints; i++) {
1177 dc->cal1->lineto(dc->point1[i]);
1178 }
1179 for (gint i = 1; i < dc->npoints; i++) {
1180 dc->cal2->lineto(dc->point2[i]);
1181 }
1182 }
1184 /* Fit and draw and copy last point */
1185 #ifdef DYNA_DRAW_VERBOSE
1186 g_print("[%d]Yup\n", dc->npoints);
1187 #endif
1188 if (!release) {
1189 g_assert(!dc->currentcurve->is_empty());
1191 SPCanvasItem *cbp = sp_canvas_item_new(sp_desktop_sketch(desktop),
1192 SP_TYPE_CANVAS_BPATH,
1193 NULL);
1194 SPCurve *curve = dc->currentcurve->copy();
1195 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH (cbp), curve);
1196 curve->unref();
1198 guint32 fillColor = sp_desktop_get_color_tool (desktop, "tools.calligraphic", true);
1199 //guint32 strokeColor = sp_desktop_get_color_tool (desktop, "tools.calligraphic", false);
1200 double opacity = sp_desktop_get_master_opacity_tool (desktop, "tools.calligraphic");
1201 double fillOpacity = sp_desktop_get_opacity_tool (desktop, "tools.calligraphic", true);
1202 //double strokeOpacity = sp_desktop_get_opacity_tool (desktop, "tools.calligraphic", false);
1203 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cbp), ((fillColor & 0xffffff00) | SP_COLOR_F_TO_U(opacity*fillOpacity)), SP_WIND_RULE_EVENODD);
1204 //on second thougtht don't do stroke yet because we don't have stoke-width yet and because stoke appears between segments while drawing
1205 //sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cbp), ((strokeColor & 0xffffff00) | SP_COLOR_F_TO_U(opacity*strokeOpacity)), 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1206 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cbp), 0x00000000, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1207 /* fixme: Cannot we cascade it to root more clearly? */
1208 g_signal_connect(G_OBJECT(cbp), "event", G_CALLBACK(sp_desktop_root_handler), desktop);
1210 dc->segments = g_slist_prepend(dc->segments, cbp);
1211 }
1213 dc->point1[0] = dc->point1[dc->npoints - 1];
1214 dc->point2[0] = dc->point2[dc->npoints - 1];
1215 dc->npoints = 1;
1216 } else {
1217 draw_temporary_box(dc);
1218 }
1219 }
1221 static void
1222 draw_temporary_box(SPDynaDrawContext *dc)
1223 {
1224 dc->currentcurve->reset();
1226 dc->currentcurve->moveto(dc->point2[dc->npoints-1]);
1227 for (gint i = dc->npoints-2; i >= 0; i--) {
1228 dc->currentcurve->lineto(dc->point2[i]);
1229 }
1230 for (gint i = 0; i < dc->npoints; i++) {
1231 dc->currentcurve->lineto(dc->point1[i]);
1232 }
1234 if (dc->npoints >= 2) {
1235 add_cap(dc->currentcurve, dc->point1[dc->npoints-1], dc->point2[dc->npoints-1], dc->cap_rounding);
1236 }
1238 dc->currentcurve->closepath();
1239 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), dc->currentcurve);
1240 }
1242 /*
1243 Local Variables:
1244 mode:c++
1245 c-file-style:"stroustrup"
1246 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1247 indent-tabs-mode:nil
1248 fill-column:99
1249 End:
1250 */
1251 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :