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/n-art-bpath.h"
52 #include "libnr/nr-path.h"
53 #include "libnr/nr-matrix-ops.h"
54 #include "libnr/nr-scale-translate-ops.h"
55 #include "libnr/nr-convert2geom.h"
56 #include "xml/repr.h"
57 #include "context-fns.h"
58 #include "sp-item.h"
59 #include "inkscape.h"
60 #include "color.h"
61 #include "splivarot.h"
62 #include "sp-item-group.h"
63 #include "sp-shape.h"
64 #include "sp-path.h"
65 #include "sp-text.h"
66 #include "display/canvas-bpath.h"
67 #include "display/canvas-arena.h"
68 #include "livarot/Shape.h"
69 #include <2geom/isnan.h>
70 #include <2geom/pathvector.h>
72 #include "dyna-draw-context.h"
74 #define DDC_RED_RGBA 0xff0000ff
76 #define TOLERANCE_CALLIGRAPHIC 0.1
78 #define DYNA_EPSILON 0.5e-6
79 #define DYNA_EPSILON_START 0.5e-2
80 #define DYNA_VEL_START 1e-5
82 #define DYNA_MIN_WIDTH 1.0e-6
85 // FIXME: move it to some shared file to be reused by both calligraphy and dropper
86 #define C1 0.552
87 static NArtBpath const hatch_area_circle[] = {
88 { NR_MOVETO, 0, 0, 0, 0, -1, 0 },
89 { NR_CURVETO, -1, C1, -C1, 1, 0, 1 },
90 { NR_CURVETO, C1, 1, 1, C1, 1, 0 },
91 { NR_CURVETO, 1, -C1, C1, -1, 0, -1 },
92 { NR_CURVETO, -C1, -1, -1, -C1, -1, 0 },
93 { NR_END, 0, 0, 0, 0, 0, 0 }
94 };
95 #undef C1
98 static void sp_dyna_draw_context_class_init(SPDynaDrawContextClass *klass);
99 static void sp_dyna_draw_context_init(SPDynaDrawContext *ddc);
100 static void sp_dyna_draw_context_dispose(GObject *object);
102 static void sp_dyna_draw_context_setup(SPEventContext *ec);
103 static void sp_dyna_draw_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
104 static gint sp_dyna_draw_context_root_handler(SPEventContext *ec, GdkEvent *event);
106 static void clear_current(SPDynaDrawContext *dc);
107 static void set_to_accumulated(SPDynaDrawContext *dc, bool unionize);
108 static void add_cap(SPCurve *curve, NR::Point const &from, NR::Point const &to, double rounding);
109 static void accumulate_calligraphic(SPDynaDrawContext *dc);
111 static void fit_and_split(SPDynaDrawContext *ddc, gboolean release);
113 static void sp_dyna_draw_reset(SPDynaDrawContext *ddc, NR::Point p);
114 static NR::Point sp_dyna_draw_get_npoint(SPDynaDrawContext const *ddc, NR::Point v);
115 static NR::Point sp_dyna_draw_get_vpoint(SPDynaDrawContext const *ddc, NR::Point n);
116 static void draw_temporary_box(SPDynaDrawContext *dc);
119 static SPEventContextClass *dd_parent_class = 0;
121 GType sp_dyna_draw_context_get_type(void)
122 {
123 static GType type = 0;
124 if (!type) {
125 GTypeInfo info = {
126 sizeof(SPDynaDrawContextClass),
127 0, // base_init
128 0, // base_finalize
129 (GClassInitFunc)sp_dyna_draw_context_class_init,
130 0, // class_finalize
131 0, // class_data
132 sizeof(SPDynaDrawContext),
133 0, // n_preallocs
134 (GInstanceInitFunc)sp_dyna_draw_context_init,
135 0 // value_table
136 };
137 type = g_type_register_static(SP_TYPE_COMMON_CONTEXT, "SPDynaDrawContext", &info, static_cast<GTypeFlags>(0));
138 }
139 return type;
140 }
142 static void
143 sp_dyna_draw_context_class_init(SPDynaDrawContextClass *klass)
144 {
145 GObjectClass *object_class = (GObjectClass *) klass;
146 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
148 dd_parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
150 object_class->dispose = sp_dyna_draw_context_dispose;
152 event_context_class->setup = sp_dyna_draw_context_setup;
153 event_context_class->set = sp_dyna_draw_context_set;
154 event_context_class->root_handler = sp_dyna_draw_context_root_handler;
155 }
157 static void
158 sp_dyna_draw_context_init(SPDynaDrawContext *ddc)
159 {
160 ddc->cursor_shape = cursor_calligraphy_xpm;
161 ddc->hot_x = 4;
162 ddc->hot_y = 4;
164 ddc->vel_thin = 0.1;
165 ddc->flatness = 0.9;
166 ddc->cap_rounding = 0.0;
168 ddc->abs_width = false;
169 ddc->keep_selected = true;
171 ddc->hatch_spacing = 0;
172 ddc->hatch_spacing_step = 0;
173 new (&ddc->hatch_pointer_past) std::list<double>();
174 new (&ddc->hatch_nearest_past) std::list<double>();
175 ddc->hatch_last_nearest = NR::Point(0,0);
176 ddc->hatch_last_pointer = NR::Point(0,0);
177 ddc->hatch_vector_accumulated = NR::Point(0,0);
178 ddc->hatch_escaped = false;
179 ddc->hatch_area = NULL;
180 ddc->hatch_item = NULL;
181 ddc->hatch_livarot_path = NULL;
183 ddc->trace_bg = false;
184 }
186 static void
187 sp_dyna_draw_context_dispose(GObject *object)
188 {
189 SPDynaDrawContext *ddc = SP_DYNA_DRAW_CONTEXT(object);
191 if (ddc->hatch_area) {
192 gtk_object_destroy(GTK_OBJECT(ddc->hatch_area));
193 ddc->hatch_area = NULL;
194 }
197 G_OBJECT_CLASS(dd_parent_class)->dispose(object);
199 ddc->hatch_pointer_past.~list();
200 ddc->hatch_nearest_past.~list();
201 }
203 static void
204 sp_dyna_draw_context_setup(SPEventContext *ec)
205 {
206 SPDynaDrawContext *ddc = SP_DYNA_DRAW_CONTEXT(ec);
208 if (((SPEventContextClass *) dd_parent_class)->setup)
209 ((SPEventContextClass *) dd_parent_class)->setup(ec);
211 ddc->accumulated = new SPCurve(32);
212 ddc->currentcurve = new SPCurve(4);
214 ddc->cal1 = new SPCurve(32);
215 ddc->cal2 = new SPCurve(32);
217 ddc->currentshape = sp_canvas_item_new(sp_desktop_sketch(ec->desktop), SP_TYPE_CANVAS_BPATH, NULL);
218 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(ddc->currentshape), DDC_RED_RGBA, SP_WIND_RULE_EVENODD);
219 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(ddc->currentshape), 0x00000000, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
220 /* fixme: Cannot we cascade it to root more clearly? */
221 g_signal_connect(G_OBJECT(ddc->currentshape), "event", G_CALLBACK(sp_desktop_root_handler), ec->desktop);
223 {
224 SPCurve *c = SPCurve::new_from_foreign_bpath(hatch_area_circle);
225 ddc->hatch_area = sp_canvas_bpath_new(sp_desktop_controls(ec->desktop), c);
226 c->unref();
227 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(ddc->hatch_area), 0x00000000,(SPWindRule)0);
228 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(ddc->hatch_area), 0x0000007f, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
229 sp_canvas_item_hide(ddc->hatch_area);
230 }
232 sp_event_context_read(ec, "mass");
233 sp_event_context_read(ec, "wiggle");
234 sp_event_context_read(ec, "angle");
235 sp_event_context_read(ec, "width");
236 sp_event_context_read(ec, "thinning");
237 sp_event_context_read(ec, "tremor");
238 sp_event_context_read(ec, "flatness");
239 sp_event_context_read(ec, "tracebackground");
240 sp_event_context_read(ec, "usepressure");
241 sp_event_context_read(ec, "usetilt");
242 sp_event_context_read(ec, "abs_width");
243 sp_event_context_read(ec, "keep_selected");
244 sp_event_context_read(ec, "cap_rounding");
246 ddc->is_drawing = false;
248 ddc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
250 if (prefs_get_int_attribute("tools.calligraphic", "selcue", 0) != 0) {
251 ec->enableSelectionCue();
252 }
253 }
255 static void
256 sp_dyna_draw_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
257 {
258 SPDynaDrawContext *ddc = SP_DYNA_DRAW_CONTEXT(ec);
260 if (!strcmp(key, "tracebackground")) {
261 ddc->trace_bg = (val && strcmp(val, "0"));
262 } else if (!strcmp(key, "keep_selected")) {
263 ddc->keep_selected = (val && strcmp(val, "0"));
264 } else {
265 //pass on up to parent class to handle common attributes.
266 if ( dd_parent_class->set ) {
267 dd_parent_class->set(ec, key, val);
268 }
269 }
271 //g_print("DDC: %g %g %g %g\n", ddc->mass, ddc->drag, ddc->angle, ddc->width);
272 }
274 static double
275 flerp(double f0, double f1, double p)
276 {
277 return f0 + ( f1 - f0 ) * p;
278 }
280 /* Get normalized point */
281 static NR::Point
282 sp_dyna_draw_get_npoint(SPDynaDrawContext const *dc, NR::Point v)
283 {
284 NR::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area();
285 double const max = MAX ( drect.dimensions()[NR::X], drect.dimensions()[NR::Y] );
286 return NR::Point(( v[NR::X] - drect.min()[NR::X] ) / max, ( v[NR::Y] - drect.min()[NR::Y] ) / max);
287 }
289 /* Get view point */
290 static NR::Point
291 sp_dyna_draw_get_vpoint(SPDynaDrawContext const *dc, NR::Point n)
292 {
293 NR::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area();
294 double const max = MAX ( drect.dimensions()[NR::X], drect.dimensions()[NR::Y] );
295 return NR::Point(n[NR::X] * max + drect.min()[NR::X], n[NR::Y] * max + drect.min()[NR::Y]);
296 }
298 static void
299 sp_dyna_draw_reset(SPDynaDrawContext *dc, NR::Point p)
300 {
301 dc->last = dc->cur = sp_dyna_draw_get_npoint(dc, p);
302 dc->vel = NR::Point(0,0);
303 dc->vel_max = 0;
304 dc->acc = NR::Point(0,0);
305 dc->ang = NR::Point(0,0);
306 dc->del = NR::Point(0,0);
307 }
309 static void
310 sp_dyna_draw_extinput(SPDynaDrawContext *dc, GdkEvent *event)
311 {
312 if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &dc->pressure))
313 dc->pressure = CLAMP (dc->pressure, DDC_MIN_PRESSURE, DDC_MAX_PRESSURE);
314 else
315 dc->pressure = DDC_DEFAULT_PRESSURE;
317 if (gdk_event_get_axis (event, GDK_AXIS_XTILT, &dc->xtilt))
318 dc->xtilt = CLAMP (dc->xtilt, DDC_MIN_TILT, DDC_MAX_TILT);
319 else
320 dc->xtilt = DDC_DEFAULT_TILT;
322 if (gdk_event_get_axis (event, GDK_AXIS_YTILT, &dc->ytilt))
323 dc->ytilt = CLAMP (dc->ytilt, DDC_MIN_TILT, DDC_MAX_TILT);
324 else
325 dc->ytilt = DDC_DEFAULT_TILT;
326 }
329 static gboolean
330 sp_dyna_draw_apply(SPDynaDrawContext *dc, NR::Point p)
331 {
332 NR::Point n = sp_dyna_draw_get_npoint(dc, p);
334 /* Calculate mass and drag */
335 double const mass = flerp(1.0, 160.0, dc->mass);
336 double const drag = flerp(0.0, 0.5, dc->drag * dc->drag);
338 /* Calculate force and acceleration */
339 NR::Point force = n - dc->cur;
341 // If force is below the absolute threshold DYNA_EPSILON,
342 // or we haven't yet reached DYNA_VEL_START (i.e. at the beginning of stroke)
343 // _and_ the force is below the (higher) DYNA_EPSILON_START threshold,
344 // discard this move.
345 // This prevents flips, blobs, and jerks caused by microscopic tremor of the tablet pen,
346 // especially bothersome at the start of the stroke where we don't yet have the inertia to
347 // smooth them out.
348 if ( NR::L2(force) < DYNA_EPSILON || (dc->vel_max < DYNA_VEL_START && NR::L2(force) < DYNA_EPSILON_START)) {
349 return FALSE;
350 }
352 dc->acc = force / mass;
354 /* Calculate new velocity */
355 dc->vel += dc->acc;
357 if (NR::L2(dc->vel) > dc->vel_max)
358 dc->vel_max = NR::L2(dc->vel);
360 /* Calculate angle of drawing tool */
362 double a1;
363 if (dc->usetilt) {
364 // 1a. calculate nib angle from input device tilt:
365 gdouble length = std::sqrt(dc->xtilt*dc->xtilt + dc->ytilt*dc->ytilt);;
367 if (length > 0) {
368 NR::Point ang1 = NR::Point(dc->ytilt/length, dc->xtilt/length);
369 a1 = atan2(ang1);
370 }
371 else
372 a1 = 0.0;
373 }
374 else {
375 // 1b. fixed dc->angle (absolutely flat nib):
376 double const radians = ( (dc->angle - 90) / 180.0 ) * M_PI;
377 NR::Point ang1 = NR::Point(-sin(radians), cos(radians));
378 a1 = atan2(ang1);
379 }
381 // 2. perpendicular to dc->vel (absolutely non-flat nib):
382 gdouble const mag_vel = NR::L2(dc->vel);
383 if ( mag_vel < DYNA_EPSILON ) {
384 return FALSE;
385 }
386 NR::Point ang2 = NR::rot90(dc->vel) / mag_vel;
388 // 3. Average them using flatness parameter:
389 // calculate angles
390 double a2 = atan2(ang2);
391 // flip a2 to force it to be in the same half-circle as a1
392 bool flipped = false;
393 if (fabs (a2-a1) > 0.5*M_PI) {
394 a2 += M_PI;
395 flipped = true;
396 }
397 // normalize a2
398 if (a2 > M_PI)
399 a2 -= 2*M_PI;
400 if (a2 < -M_PI)
401 a2 += 2*M_PI;
402 // find the flatness-weighted bisector angle, unflip if a2 was flipped
403 // FIXME: when dc->vel is oscillating around the fixed angle, the new_ang flips back and forth. How to avoid this?
404 double new_ang = a1 + (1 - dc->flatness) * (a2 - a1) - (flipped? M_PI : 0);
406 // Try to detect a sudden flip when the new angle differs too much from the previous for the
407 // current velocity; in that case discard this move
408 double angle_delta = NR::L2(NR::Point (cos (new_ang), sin (new_ang)) - dc->ang);
409 if ( angle_delta / NR::L2(dc->vel) > 4000 ) {
410 return FALSE;
411 }
413 // convert to point
414 dc->ang = NR::Point (cos (new_ang), sin (new_ang));
416 // 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);
418 /* Apply drag */
419 dc->vel *= 1.0 - drag;
421 /* Update position */
422 dc->last = dc->cur;
423 dc->cur += dc->vel;
425 return TRUE;
426 }
428 static void
429 sp_dyna_draw_brush(SPDynaDrawContext *dc)
430 {
431 g_assert( dc->npoints >= 0 && dc->npoints < SAMPLING_SIZE );
433 // How much velocity thins strokestyle
434 double vel_thin = flerp (0, 160, dc->vel_thin);
436 // Influence of pressure on thickness
437 double pressure_thick = (dc->usepressure ? dc->pressure : 1.0);
439 // get the real brush point, not the same as pointer (affected by hatch tracking and/or mass
440 // drag)
441 NR::Point brush = sp_dyna_draw_get_vpoint(dc, dc->cur);
442 NR::Point brush_w = SP_EVENT_CONTEXT(dc)->desktop->d2w(brush);
444 double trace_thick = 1;
445 if (dc->trace_bg) {
446 // pick single pixel
447 NRPixBlock pb;
448 int x = (int) floor(brush_w[NR::X]);
449 int y = (int) floor(brush_w[NR::Y]);
450 nr_pixblock_setup_fast(&pb, NR_PIXBLOCK_MODE_R8G8B8A8P, x, y, x+1, y+1, TRUE);
451 sp_canvas_arena_render_pixblock(SP_CANVAS_ARENA(sp_desktop_drawing(SP_EVENT_CONTEXT(dc)->desktop)), &pb);
452 const unsigned char *s = NR_PIXBLOCK_PX(&pb);
453 double R = s[0] / 255.0;
454 double G = s[1] / 255.0;
455 double B = s[2] / 255.0;
456 double A = s[3] / 255.0;
457 double max = MAX (MAX (R, G), B);
458 double min = MIN (MIN (R, G), B);
459 double L = A * (max + min)/2 + (1 - A); // blend with white bg
460 trace_thick = 1 - L;
461 //g_print ("L %g thick %g\n", L, trace_thick);
462 }
464 double width = (pressure_thick * trace_thick - vel_thin * NR::L2(dc->vel)) * dc->width;
466 double tremble_left = 0, tremble_right = 0;
467 if (dc->tremor > 0) {
468 // obtain two normally distributed random variables, using polar Box-Muller transform
469 double x1, x2, w, y1, y2;
470 do {
471 x1 = 2.0 * g_random_double_range(0,1) - 1.0;
472 x2 = 2.0 * g_random_double_range(0,1) - 1.0;
473 w = x1 * x1 + x2 * x2;
474 } while ( w >= 1.0 );
475 w = sqrt( (-2.0 * log( w ) ) / w );
476 y1 = x1 * w;
477 y2 = x2 * w;
479 // deflect both left and right edges randomly and independently, so that:
480 // (1) dc->tremor=1 corresponds to sigma=1, decreasing dc->tremor narrows the bell curve;
481 // (2) deflection depends on width, but is upped for small widths for better visual uniformity across widths;
482 // (3) deflection somewhat depends on speed, to prevent fast strokes looking
483 // comparatively smooth and slow ones excessively jittery
484 tremble_left = (y1)*dc->tremor * (0.15 + 0.8*width) * (0.35 + 14*NR::L2(dc->vel));
485 tremble_right = (y2)*dc->tremor * (0.15 + 0.8*width) * (0.35 + 14*NR::L2(dc->vel));
486 }
488 if ( width < 0.02 * dc->width ) {
489 width = 0.02 * dc->width;
490 }
492 double dezoomify_factor = 0.05 * 1000;
493 if (!dc->abs_width) {
494 dezoomify_factor /= SP_EVENT_CONTEXT(dc)->desktop->current_zoom();
495 }
497 NR::Point del_left = dezoomify_factor * (width + tremble_left) * dc->ang;
498 NR::Point del_right = dezoomify_factor * (width + tremble_right) * dc->ang;
500 dc->point1[dc->npoints] = brush + del_left;
501 dc->point2[dc->npoints] = brush - del_right;
503 dc->del = 0.5*(del_left + del_right);
505 dc->npoints++;
506 }
508 void
509 sp_ddc_update_toolbox (SPDesktop *desktop, const gchar *id, double value)
510 {
511 desktop->setToolboxAdjustmentValue (id, value);
512 }
514 static void
515 calligraphic_cancel(SPDynaDrawContext *dc)
516 {
517 SPDesktop *desktop = SP_EVENT_CONTEXT(dc)->desktop;
518 dc->dragging = FALSE;
519 dc->is_drawing = false;
520 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
521 /* Remove all temporary line segments */
522 while (dc->segments) {
523 gtk_object_destroy(GTK_OBJECT(dc->segments->data));
524 dc->segments = g_slist_remove(dc->segments, dc->segments->data);
525 }
526 /* reset accumulated curve */
527 dc->accumulated->reset();
528 clear_current(dc);
529 if (dc->repr) {
530 dc->repr = NULL;
531 }
532 }
535 gint
536 sp_dyna_draw_context_root_handler(SPEventContext *event_context,
537 GdkEvent *event)
538 {
539 SPDynaDrawContext *dc = SP_DYNA_DRAW_CONTEXT(event_context);
540 SPDesktop *desktop = event_context->desktop;
542 gint ret = FALSE;
544 switch (event->type) {
545 case GDK_BUTTON_PRESS:
546 if (event->button.button == 1 && !event_context->space_panning) {
548 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
550 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
551 return TRUE;
552 }
554 NR::Point const button_w(event->button.x,
555 event->button.y);
556 NR::Point const button_dt(desktop->w2d(button_w));
557 sp_dyna_draw_reset(dc, button_dt);
558 sp_dyna_draw_extinput(dc, event);
559 sp_dyna_draw_apply(dc, button_dt);
560 dc->accumulated->reset();
561 if (dc->repr) {
562 dc->repr = NULL;
563 }
565 /* initialize first point */
566 dc->npoints = 0;
568 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
569 ( GDK_KEY_PRESS_MASK |
570 GDK_BUTTON_RELEASE_MASK |
571 GDK_POINTER_MOTION_MASK |
572 GDK_BUTTON_PRESS_MASK ),
573 NULL,
574 event->button.time);
576 ret = TRUE;
578 sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 3);
579 dc->is_drawing = true;
580 }
581 break;
582 case GDK_MOTION_NOTIFY:
583 {
584 NR::Point const motion_w(event->motion.x,
585 event->motion.y);
586 NR::Point motion_dt(desktop->w2d(motion_w));
587 sp_dyna_draw_extinput(dc, event);
589 dc->_message_context->clear();
591 // for hatching:
592 double hatch_dist = 0;
593 NR::Point hatch_unit_vector(0,0);
594 NR::Point nearest(0,0);
595 NR::Point pointer(0,0);
596 NR::Matrix motion_to_curve(NR::identity());
598 if (event->motion.state & GDK_CONTROL_MASK) { // hatching - sense the item
600 SPItem *selected = sp_desktop_selection(desktop)->singleItem();
601 if (selected && (SP_IS_SHAPE(selected) || SP_IS_TEXT(selected))) {
602 // One item selected, and it's a path;
603 // let's try to track it as a guide
605 if (selected != dc->hatch_item) {
606 dc->hatch_item = selected;
607 if (dc->hatch_livarot_path)
608 delete dc->hatch_livarot_path;
609 dc->hatch_livarot_path = Path_for_item (dc->hatch_item, true, true);
610 dc->hatch_livarot_path->ConvertWithBackData(0.01);
611 }
613 // calculate pointer point in the guide item's coords
614 motion_to_curve = from_2geom(sp_item_dt2i_affine(selected) * sp_item_i2doc_affine(selected));
615 pointer = motion_dt * motion_to_curve;
617 // calculate the nearest point on the guide path
618 NR::Maybe<Path::cut_position> position = get_nearest_position_on_Path(dc->hatch_livarot_path, pointer);
619 nearest = get_point_on_Path(dc->hatch_livarot_path, position->piece, position->t);
622 // distance from pointer to nearest
623 hatch_dist = NR::L2(pointer - nearest);
624 // unit-length vector
625 hatch_unit_vector = (pointer - nearest)/hatch_dist;
627 dc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Guide path selected</b>; start drawing along the guide with <b>Ctrl</b>"));
628 } else {
629 dc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Select a guide path</b> to track with <b>Ctrl</b>"));
630 }
631 }
633 if ( dc->is_drawing && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
634 dc->dragging = TRUE;
636 if (event->motion.state & GDK_CONTROL_MASK && dc->hatch_item) { // hatching
638 #define SPEED_ELEMENTS 12
639 #define SPEED_MIN 0.12
640 #define SPEED_NORMAL 0.65
642 // speed is the movement of the nearest point along the guide path, divided by
643 // the movement of the pointer at the same period; it is averaged for the last
644 // SPEED_ELEMENTS motion events. Normally, as you track the guide path, speed
645 // is about 1, i.e. the nearest point on the path is moved by about the same
646 // distance as the pointer. If the speed starts to decrease, we are losing
647 // contact with the guide; if it drops below SPEED_MIN, we are on our own and
648 // not attracted to guide anymore. Most often this happens when you have
649 // tracked to the end of a guide calligraphic stroke and keep moving
650 // further. We try to handle this situation gracefully: not stick with the
651 // guide forever but let go of it smoothly and without sharp jerks (non-zero
652 // mass recommended; with zero mass, jerks are still quite noticeable).
654 double speed = 1;
655 if (NR::L2(dc->hatch_last_nearest) != 0) {
656 // the distance nearest moved since the last motion event
657 double nearest_moved = NR::L2(nearest - dc->hatch_last_nearest);
658 // the distance pointer moved since the last motion event
659 double pointer_moved = NR::L2(pointer - dc->hatch_last_pointer);
660 // store them in stacks limited to SPEED_ELEMENTS
661 dc->hatch_nearest_past.push_front(nearest_moved);
662 if (dc->hatch_nearest_past.size() > SPEED_ELEMENTS)
663 dc->hatch_nearest_past.pop_back();
664 dc->hatch_pointer_past.push_front(pointer_moved);
665 if (dc->hatch_pointer_past.size() > SPEED_ELEMENTS)
666 dc->hatch_pointer_past.pop_back();
668 // If the stacks are full,
669 if (dc->hatch_nearest_past.size() == SPEED_ELEMENTS) {
670 // calculate the sums of all stored movements
671 double nearest_sum = std::accumulate (dc->hatch_nearest_past.begin(), dc->hatch_nearest_past.end(), 0.0);
672 double pointer_sum = std::accumulate (dc->hatch_pointer_past.begin(), dc->hatch_pointer_past.end(), 0.0);
673 // and divide to get the speed
674 speed = nearest_sum/pointer_sum;
675 //g_print ("nearest sum %g pointer_sum %g speed %g\n", nearest_sum, pointer_sum, speed);
676 }
677 }
679 if ( dc->hatch_escaped // already escaped, do not reattach
680 || (speed < SPEED_MIN) // stuck; most likely reached end of traced stroke
681 || (dc->hatch_spacing > 0 && hatch_dist > 50 * dc->hatch_spacing) // went too far from the guide
682 ) {
683 // We are NOT attracted to the guide!
685 //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);
687 // Remember hatch_escaped so we don't get
688 // attracted again until the end of this stroke
689 dc->hatch_escaped = true;
691 } else {
693 // Calculate angle cosine of this vector-to-guide and all past vectors
694 // summed, to detect if we accidentally flipped to the other side of the
695 // guide
696 double dot = NR::dot (pointer - nearest, dc->hatch_vector_accumulated);
697 dot /= NR::L2(pointer - nearest) * NR::L2(dc->hatch_vector_accumulated);
699 if (dc->hatch_spacing != 0) { // spacing was already set
700 double target;
701 if (speed > SPEED_NORMAL) {
702 // all ok, strictly obey the spacing
703 target = dc->hatch_spacing;
704 } else {
705 // looks like we're starting to lose speed,
706 // so _gradually_ let go attraction to prevent jerks
707 target = (dc->hatch_spacing * speed + hatch_dist * (SPEED_NORMAL - speed))/SPEED_NORMAL;
708 }
709 if (!IS_NAN(dot) && dot < -0.5) {// flip
710 target = -target;
711 }
713 // This is the track pointer that we will use instead of the real one
714 NR::Point new_pointer = nearest + target * hatch_unit_vector;
716 // some limited feedback: allow persistent pulling to slightly change
717 // the spacing
718 dc->hatch_spacing += (hatch_dist - dc->hatch_spacing)/3500;
720 // return it to the desktop coords
721 motion_dt = new_pointer * motion_to_curve.inverse();
723 } else {
724 // this is the first motion event, set the dist
725 dc->hatch_spacing = hatch_dist;
726 }
728 // remember last points
729 dc->hatch_last_pointer = pointer;
730 dc->hatch_last_nearest = nearest;
731 dc->hatch_vector_accumulated += (pointer - nearest);
732 }
734 dc->_message_context->set(Inkscape::NORMAL_MESSAGE, dc->hatch_escaped? _("Tracking: <b>connection to guide path lost!</b>") : _("<b>Tracking</b> a guide path"));
736 } else {
737 dc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drawing</b> a calligraphic stroke"));
738 }
740 if (!sp_dyna_draw_apply(dc, motion_dt)) {
741 ret = TRUE;
742 break;
743 }
745 if ( dc->cur != dc->last ) {
746 sp_dyna_draw_brush(dc);
747 g_assert( dc->npoints > 0 );
748 fit_and_split(dc, FALSE);
749 }
750 ret = TRUE;
751 }
753 // Draw the hatching circle if necessary
754 if (event->motion.state & GDK_CONTROL_MASK) {
755 if (dc->hatch_spacing == 0 && hatch_dist != 0) {
756 // Haven't set spacing yet: gray, center free, update radius live
757 NR::Point c = desktop->w2d(motion_w);
758 NR::Matrix const sm (NR::scale(hatch_dist, hatch_dist) * NR::translate(c));
759 sp_canvas_item_affine_absolute(dc->hatch_area, sm);
760 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->hatch_area), 0x7f7f7fff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
761 sp_canvas_item_show(dc->hatch_area);
762 } else if (dc->dragging && !dc->hatch_escaped) {
763 // Tracking: green, center snapped, fixed radius
764 NR::Point c = motion_dt;
765 NR::Matrix const sm (NR::scale(dc->hatch_spacing, dc->hatch_spacing) * NR::translate(c));
766 sp_canvas_item_affine_absolute(dc->hatch_area, sm);
767 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->hatch_area), 0x00FF00ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
768 sp_canvas_item_show(dc->hatch_area);
769 } else if (dc->dragging && dc->hatch_escaped) {
770 // Tracking escaped: red, center free, fixed radius
771 NR::Point c = desktop->w2d(motion_w);
772 NR::Matrix const sm (NR::scale(dc->hatch_spacing, dc->hatch_spacing) * NR::translate(c));
774 sp_canvas_item_affine_absolute(dc->hatch_area, sm);
775 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->hatch_area), 0xFF0000ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
776 sp_canvas_item_show(dc->hatch_area);
777 } else {
778 // Not drawing but spacing set: gray, center snapped, fixed radius
779 NR::Point c = (nearest + dc->hatch_spacing * hatch_unit_vector) * motion_to_curve.inverse();
780 if (!IS_NAN(c[NR::X]) && !IS_NAN(c[NR::Y])) {
781 NR::Matrix const sm (NR::scale(dc->hatch_spacing, dc->hatch_spacing) * NR::translate(c));
782 sp_canvas_item_affine_absolute(dc->hatch_area, sm);
783 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(dc->hatch_area), 0x7f7f7fff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
784 sp_canvas_item_show(dc->hatch_area);
785 }
786 }
787 } else {
788 sp_canvas_item_hide(dc->hatch_area);
789 }
790 }
791 break;
794 case GDK_BUTTON_RELEASE:
795 {
796 NR::Point const motion_w(event->button.x, event->button.y);
797 NR::Point const motion_dt(desktop->w2d(motion_w));
799 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
800 sp_canvas_end_forced_full_redraws(desktop->canvas);
801 dc->is_drawing = false;
803 if (dc->dragging && event->button.button == 1 && !event_context->space_panning) {
804 dc->dragging = FALSE;
806 sp_dyna_draw_apply(dc, motion_dt);
808 /* Remove all temporary line segments */
809 while (dc->segments) {
810 gtk_object_destroy(GTK_OBJECT(dc->segments->data));
811 dc->segments = g_slist_remove(dc->segments, dc->segments->data);
812 }
814 /* Create object */
815 fit_and_split(dc, TRUE);
816 accumulate_calligraphic(dc);
817 set_to_accumulated(dc, event->button.state & GDK_SHIFT_MASK); // performs document_done
819 /* reset accumulated curve */
820 dc->accumulated->reset();
822 clear_current(dc);
823 if (dc->repr) {
824 dc->repr = NULL;
825 }
827 if (!dc->hatch_pointer_past.empty()) dc->hatch_pointer_past.clear();
828 if (!dc->hatch_nearest_past.empty()) dc->hatch_nearest_past.clear();
829 dc->hatch_last_nearest = NR::Point(0,0);
830 dc->hatch_last_pointer = NR::Point(0,0);
831 dc->hatch_vector_accumulated = NR::Point(0,0);
832 dc->hatch_escaped = false;
833 dc->hatch_item = NULL;
834 dc->hatch_livarot_path = NULL;
836 if (dc->hatch_spacing != 0 && !dc->keep_selected) {
837 // we do not select the newly drawn path, so increase spacing by step
838 if (dc->hatch_spacing_step == 0) {
839 dc->hatch_spacing_step = dc->hatch_spacing;
840 }
841 dc->hatch_spacing += dc->hatch_spacing_step;
842 }
844 dc->_message_context->clear();
845 ret = TRUE;
846 }
847 break;
848 }
850 case GDK_KEY_PRESS:
851 switch (get_group0_keyval (&event->key)) {
852 case GDK_Up:
853 case GDK_KP_Up:
854 if (!MOD__CTRL_ONLY) {
855 dc->angle += 5.0;
856 if (dc->angle > 90.0)
857 dc->angle = 90.0;
858 sp_ddc_update_toolbox (desktop, "calligraphy-angle", dc->angle);
859 ret = TRUE;
860 }
861 break;
862 case GDK_Down:
863 case GDK_KP_Down:
864 if (!MOD__CTRL_ONLY) {
865 dc->angle -= 5.0;
866 if (dc->angle < -90.0)
867 dc->angle = -90.0;
868 sp_ddc_update_toolbox (desktop, "calligraphy-angle", dc->angle);
869 ret = TRUE;
870 }
871 break;
872 case GDK_Right:
873 case GDK_KP_Right:
874 if (!MOD__CTRL_ONLY) {
875 dc->width += 0.01;
876 if (dc->width > 1.0)
877 dc->width = 1.0;
878 sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width * 100); // the same spinbutton is for alt+x
879 ret = TRUE;
880 }
881 break;
882 case GDK_Left:
883 case GDK_KP_Left:
884 if (!MOD__CTRL_ONLY) {
885 dc->width -= 0.01;
886 if (dc->width < 0.01)
887 dc->width = 0.01;
888 sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width * 100);
889 ret = TRUE;
890 }
891 break;
892 case GDK_Home:
893 case GDK_KP_Home:
894 dc->width = 0.01;
895 sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width * 100);
896 ret = TRUE;
897 break;
898 case GDK_End:
899 case GDK_KP_End:
900 dc->width = 1.0;
901 sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width * 100);
902 ret = TRUE;
903 break;
904 case GDK_x:
905 case GDK_X:
906 if (MOD__ALT_ONLY) {
907 desktop->setToolboxFocusTo ("altx-calligraphy");
908 ret = TRUE;
909 }
910 break;
911 case GDK_Escape:
912 if (dc->is_drawing) {
913 // if drawing, cancel, otherwise pass it up for deselecting
914 calligraphic_cancel (dc);
915 ret = TRUE;
916 }
917 break;
918 case GDK_z:
919 case GDK_Z:
920 if (MOD__CTRL_ONLY && dc->is_drawing) {
921 // if drawing, cancel, otherwise pass it up for undo
922 calligraphic_cancel (dc);
923 ret = TRUE;
924 }
925 break;
926 default:
927 break;
928 }
929 break;
931 case GDK_KEY_RELEASE:
932 switch (get_group0_keyval(&event->key)) {
933 case GDK_Control_L:
934 case GDK_Control_R:
935 dc->_message_context->clear();
936 dc->hatch_spacing = 0;
937 dc->hatch_spacing_step = 0;
938 break;
939 default:
940 break;
941 }
943 default:
944 break;
945 }
947 if (!ret) {
948 if (((SPEventContextClass *) dd_parent_class)->root_handler) {
949 ret = ((SPEventContextClass *) dd_parent_class)->root_handler(event_context, event);
950 }
951 }
953 return ret;
954 }
957 static void
958 clear_current(SPDynaDrawContext *dc)
959 {
960 /* reset bpath */
961 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), NULL);
962 /* reset curve */
963 dc->currentcurve->reset();
964 dc->cal1->reset();
965 dc->cal2->reset();
966 /* reset points */
967 dc->npoints = 0;
968 }
970 static void
971 set_to_accumulated(SPDynaDrawContext *dc, bool unionize)
972 {
973 SPDesktop *desktop = SP_EVENT_CONTEXT(dc)->desktop;
975 if (!dc->accumulated->is_empty()) {
976 if (!dc->repr) {
977 /* Create object */
978 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(desktop->doc());
979 Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
981 /* Set style */
982 sp_desktop_apply_style_tool (desktop, repr, "tools.calligraphic", false);
984 dc->repr = repr;
986 SPItem *item=SP_ITEM(desktop->currentLayer()->appendChildRepr(dc->repr));
987 Inkscape::GC::release(dc->repr);
988 item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
989 item->updateRepr();
990 }
991 Geom::PathVector pathv = dc->accumulated->get_pathvector() * to_2geom(sp_desktop_dt2root_affine(desktop));
992 gchar *str = sp_svg_write_path(pathv);
993 g_assert( str != NULL );
994 dc->repr->setAttribute("d", str);
995 g_free(str);
997 if (unionize) {
998 sp_desktop_selection(desktop)->add(dc->repr);
999 sp_selected_path_union_skip_undo();
1000 } else {
1001 if (dc->keep_selected) {
1002 sp_desktop_selection(desktop)->set(dc->repr);
1003 }
1004 }
1006 } else {
1007 if (dc->repr) {
1008 sp_repr_unparent(dc->repr);
1009 }
1010 dc->repr = NULL;
1011 }
1013 sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_CALLIGRAPHIC,
1014 _("Draw calligraphic stroke"));
1015 }
1017 static void
1018 add_cap(SPCurve *curve,
1019 NR::Point const &from,
1020 NR::Point const &to,
1021 double rounding)
1022 {
1023 if (NR::L2( to - from ) > DYNA_EPSILON) {
1024 NR::Point vel = rounding * NR::rot90( to - from ) / sqrt(2.0);
1025 double mag = NR::L2(vel);
1027 NR::Point v = mag * NR::rot90( to - from ) / NR::L2( to - from );
1028 curve->curveto(from + v, to + v, to);
1029 }
1030 }
1032 static void
1033 accumulate_calligraphic(SPDynaDrawContext *dc)
1034 {
1035 if ( !dc->cal1->is_empty() && !dc->cal2->is_empty() ) {
1036 dc->accumulated->reset(); /* Is this required ?? */
1037 SPCurve *rev_cal2 = dc->cal2->create_reverse();
1039 g_assert(dc->cal1->get_segment_count() > 0);
1040 g_assert(rev_cal2->get_segment_count() > 0);
1041 g_assert( ! dc->cal1->first_path()->closed() );
1042 g_assert( ! rev_cal2->first_path()->closed() );
1044 Geom::CubicBezier const * dc_cal1_firstseg = dynamic_cast<Geom::CubicBezier const *>( dc->cal1->first_segment() );
1045 Geom::CubicBezier const * rev_cal2_firstseg = dynamic_cast<Geom::CubicBezier const *>( rev_cal2->first_segment() );
1046 Geom::CubicBezier const * dc_cal1_lastseg = dynamic_cast<Geom::CubicBezier const *>( dc->cal1->last_segment() );
1047 Geom::CubicBezier const * rev_cal2_lastseg = dynamic_cast<Geom::CubicBezier const *>( rev_cal2->last_segment() );
1048 g_assert( dc_cal1_firstseg );
1049 g_assert( rev_cal2_firstseg );
1050 g_assert( dc_cal1_lastseg );
1051 g_assert( rev_cal2_lastseg );
1053 dc->accumulated->append(dc->cal1, false);
1055 add_cap(dc->accumulated, (*dc_cal1_lastseg)[3], (*rev_cal2_firstseg)[3], dc->cap_rounding);
1057 dc->accumulated->append(rev_cal2, true);
1059 add_cap(dc->accumulated, (*rev_cal2_lastseg)[3], (*dc_cal1_firstseg)[3], dc->cap_rounding);
1061 dc->accumulated->closepath();
1063 rev_cal2->unref();
1065 dc->cal1->reset();
1066 dc->cal2->reset();
1067 }
1068 }
1070 static double square(double const x)
1071 {
1072 return x * x;
1073 }
1075 static void
1076 fit_and_split(SPDynaDrawContext *dc, gboolean release)
1077 {
1078 double const tolerance_sq = square( NR::expansion(SP_EVENT_CONTEXT(dc)->desktop->w2d()) * TOLERANCE_CALLIGRAPHIC );
1080 #ifdef DYNA_DRAW_VERBOSE
1081 g_print("[F&S:R=%c]", release?'T':'F');
1082 #endif
1084 if (!( dc->npoints > 0 && dc->npoints < SAMPLING_SIZE ))
1085 return; // just clicked
1087 if ( dc->npoints == SAMPLING_SIZE - 1 || release ) {
1088 #define BEZIER_SIZE 4
1089 #define BEZIER_MAX_BEZIERS 8
1090 #define BEZIER_MAX_LENGTH ( BEZIER_SIZE * BEZIER_MAX_BEZIERS )
1092 #ifdef DYNA_DRAW_VERBOSE
1093 g_print("[F&S:#] dc->npoints:%d, release:%s\n",
1094 dc->npoints, release ? "TRUE" : "FALSE");
1095 #endif
1097 /* Current calligraphic */
1098 if ( dc->cal1->get_length() == 0 || dc->cal2->get_length() == 0 ) {
1099 /* dc->npoints > 0 */
1100 /* g_print("calligraphics(1|2) reset\n"); */
1101 dc->cal1->reset();
1102 dc->cal2->reset();
1104 dc->cal1->moveto(dc->point1[0]);
1105 dc->cal2->moveto(dc->point2[0]);
1106 }
1108 NR::Point b1[BEZIER_MAX_LENGTH];
1109 gint const nb1 = sp_bezier_fit_cubic_r(b1, dc->point1, dc->npoints,
1110 tolerance_sq, BEZIER_MAX_BEZIERS);
1111 g_assert( nb1 * BEZIER_SIZE <= gint(G_N_ELEMENTS(b1)) );
1113 NR::Point b2[BEZIER_MAX_LENGTH];
1114 gint const nb2 = sp_bezier_fit_cubic_r(b2, dc->point2, dc->npoints,
1115 tolerance_sq, BEZIER_MAX_BEZIERS);
1116 g_assert( nb2 * BEZIER_SIZE <= gint(G_N_ELEMENTS(b2)) );
1118 if ( nb1 != -1 && nb2 != -1 ) {
1119 /* Fit and draw and reset state */
1120 #ifdef DYNA_DRAW_VERBOSE
1121 g_print("nb1:%d nb2:%d\n", nb1, nb2);
1122 #endif
1123 /* CanvasShape */
1124 if (! release) {
1125 dc->currentcurve->reset();
1126 dc->currentcurve->moveto(b1[0]);
1127 for (NR::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) {
1128 dc->currentcurve->curveto(bp1[1],
1129 bp1[2], bp1[3]);
1130 }
1131 dc->currentcurve->lineto(b2[BEZIER_SIZE*(nb2-1) + 3]);
1132 for (NR::Point *bp2 = b2 + BEZIER_SIZE * ( nb2 - 1 ); bp2 >= b2; bp2 -= BEZIER_SIZE) {
1133 dc->currentcurve->curveto(bp2[2], bp2[1], bp2[0]);
1134 }
1135 // FIXME: dc->segments is always NULL at this point??
1136 if (!dc->segments) { // first segment
1137 add_cap(dc->currentcurve, b2[0], b1[0], dc->cap_rounding);
1138 }
1139 dc->currentcurve->closepath();
1140 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), dc->currentcurve);
1141 }
1143 /* Current calligraphic */
1144 for (NR::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) {
1145 dc->cal1->curveto(bp1[1], bp1[2], bp1[3]);
1146 }
1147 for (NR::Point *bp2 = b2; bp2 < b2 + BEZIER_SIZE * nb2; bp2 += BEZIER_SIZE) {
1148 dc->cal2->curveto(bp2[1], bp2[2], bp2[3]);
1149 }
1150 } else {
1151 /* fixme: ??? */
1152 #ifdef DYNA_DRAW_VERBOSE
1153 g_print("[fit_and_split] failed to fit-cubic.\n");
1154 #endif
1155 draw_temporary_box(dc);
1157 for (gint i = 1; i < dc->npoints; i++) {
1158 dc->cal1->lineto(dc->point1[i]);
1159 }
1160 for (gint i = 1; i < dc->npoints; i++) {
1161 dc->cal2->lineto(dc->point2[i]);
1162 }
1163 }
1165 /* Fit and draw and copy last point */
1166 #ifdef DYNA_DRAW_VERBOSE
1167 g_print("[%d]Yup\n", dc->npoints);
1168 #endif
1169 if (!release) {
1170 g_assert(!dc->currentcurve->is_empty());
1172 SPCanvasItem *cbp = sp_canvas_item_new(sp_desktop_sketch(SP_EVENT_CONTEXT(dc)->desktop),
1173 SP_TYPE_CANVAS_BPATH,
1174 NULL);
1175 SPCurve *curve = dc->currentcurve->copy();
1176 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH (cbp), curve);
1177 curve->unref();
1179 guint32 fillColor = sp_desktop_get_color_tool (SP_ACTIVE_DESKTOP, "tools.calligraphic", true);
1180 //guint32 strokeColor = sp_desktop_get_color_tool (SP_ACTIVE_DESKTOP, "tools.calligraphic", false);
1181 double opacity = sp_desktop_get_master_opacity_tool (SP_ACTIVE_DESKTOP, "tools.calligraphic");
1182 double fillOpacity = sp_desktop_get_opacity_tool (SP_ACTIVE_DESKTOP, "tools.calligraphic", true);
1183 //double strokeOpacity = sp_desktop_get_opacity_tool (SP_ACTIVE_DESKTOP, "tools.calligraphic", false);
1184 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cbp), ((fillColor & 0xffffff00) | SP_COLOR_F_TO_U(opacity*fillOpacity)), SP_WIND_RULE_EVENODD);
1185 //on second thougtht don't do stroke yet because we don't have stoke-width yet and because stoke appears between segments while drawing
1186 //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);
1187 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cbp), 0x00000000, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
1188 /* fixme: Cannot we cascade it to root more clearly? */
1189 g_signal_connect(G_OBJECT(cbp), "event", G_CALLBACK(sp_desktop_root_handler), SP_EVENT_CONTEXT(dc)->desktop);
1191 dc->segments = g_slist_prepend(dc->segments, cbp);
1192 }
1194 dc->point1[0] = dc->point1[dc->npoints - 1];
1195 dc->point2[0] = dc->point2[dc->npoints - 1];
1196 dc->npoints = 1;
1197 } else {
1198 draw_temporary_box(dc);
1199 }
1200 }
1202 static void
1203 draw_temporary_box(SPDynaDrawContext *dc)
1204 {
1205 dc->currentcurve->reset();
1207 dc->currentcurve->moveto(dc->point2[dc->npoints-1]);
1208 for (gint i = dc->npoints-2; i >= 0; i--) {
1209 dc->currentcurve->lineto(dc->point2[i]);
1210 }
1211 for (gint i = 0; i < dc->npoints; i++) {
1212 dc->currentcurve->lineto(dc->point1[i]);
1213 }
1215 if (dc->npoints >= 2) {
1216 add_cap(dc->currentcurve, dc->point1[dc->npoints-1], dc->point2[dc->npoints-1], dc->cap_rounding);
1217 }
1219 dc->currentcurve->closepath();
1220 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), dc->currentcurve);
1221 }
1223 /*
1224 Local Variables:
1225 mode:c++
1226 c-file-style:"stroustrup"
1227 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1228 indent-tabs-mode:nil
1229 fill-column:99
1230 End:
1231 */
1232 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :