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 *
11 * The original dynadraw code:
12 * Paul Haeberli <paul@sgi.com>
13 *
14 * Copyright (C) 1998 The Free Software Foundation
15 * Copyright (C) 1999-2005 authors
16 * Copyright (C) 2001-2002 Ximian, Inc.
17 *
18 * Released under GNU GPL, read the file 'COPYING' for more information
19 */
21 #define noDYNA_DRAW_VERBOSE
23 #include "config.h"
25 #include <gtk/gtk.h>
26 #include <gdk/gdkkeysyms.h>
28 #include "svg/svg.h"
29 #include "display/canvas-bpath.h"
30 #include "display/bezier-utils.h"
32 #include "macros.h"
33 #include "document.h"
34 #include "selection.h"
35 #include "desktop.h"
36 #include "desktop-events.h"
37 #include "desktop-handles.h"
38 #include "desktop-affine.h"
39 #include "desktop-style.h"
40 #include "message-context.h"
41 #include "pixmaps/cursor-calligraphy.xpm"
42 #include "dyna-draw-context.h"
43 #include "libnr/n-art-bpath.h"
44 #include "libnr/nr-path.h"
45 #include "xml/repr.h"
46 #include "context-fns.h"
47 #include "sp-item.h"
48 #include "inkscape.h"
50 #define DDC_RED_RGBA 0xff0000ff
52 #define SAMPLE_TIMEOUT 10
53 #define TOLERANCE_LINE 1.0
54 #define TOLERANCE_CALLIGRAPHIC 3.0
55 #define DYNA_EPSILON 1.0e-6
57 #define DYNA_MIN_WIDTH 1.0e-6
59 #define DRAG_MIN 0.0
60 #define DRAG_DEFAULT 1.0
61 #define DRAG_MAX 1.0
63 static void sp_dyna_draw_context_class_init(SPDynaDrawContextClass *klass);
64 static void sp_dyna_draw_context_init(SPDynaDrawContext *ddc);
65 static void sp_dyna_draw_context_dispose(GObject *object);
67 static void sp_dyna_draw_context_setup(SPEventContext *ec);
68 static void sp_dyna_draw_context_set(SPEventContext *ec, gchar const *key, gchar const *val);
69 static gint sp_dyna_draw_context_root_handler(SPEventContext *ec, GdkEvent *event);
71 static void clear_current(SPDynaDrawContext *dc);
72 static void set_to_accumulated(SPDynaDrawContext *dc);
73 static void accumulate_calligraphic(SPDynaDrawContext *dc);
75 static void fit_and_split(SPDynaDrawContext *ddc, gboolean release);
76 static void fit_and_split_calligraphics(SPDynaDrawContext *ddc, gboolean release);
78 static void sp_dyna_draw_reset(SPDynaDrawContext *ddc, NR::Point p);
79 static NR::Point sp_dyna_draw_get_npoint(SPDynaDrawContext const *ddc, NR::Point v);
80 static NR::Point sp_dyna_draw_get_vpoint(SPDynaDrawContext const *ddc, NR::Point n);
81 static void draw_temporary_box(SPDynaDrawContext *dc);
84 static SPEventContextClass *parent_class;
86 GtkType
87 sp_dyna_draw_context_get_type(void)
88 {
89 static GType type = 0;
90 if (!type) {
91 GTypeInfo info = {
92 sizeof(SPDynaDrawContextClass),
93 NULL, NULL,
94 (GClassInitFunc) sp_dyna_draw_context_class_init,
95 NULL, NULL,
96 sizeof(SPDynaDrawContext),
97 4,
98 (GInstanceInitFunc) sp_dyna_draw_context_init,
99 NULL, /* value_table */
100 };
101 type = g_type_register_static(SP_TYPE_EVENT_CONTEXT, "SPDynaDrawContext", &info, (GTypeFlags)0);
102 }
103 return type;
104 }
106 static void
107 sp_dyna_draw_context_class_init(SPDynaDrawContextClass *klass)
108 {
109 GObjectClass *object_class = (GObjectClass *) klass;
110 SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
112 parent_class = (SPEventContextClass*)g_type_class_peek_parent(klass);
114 object_class->dispose = sp_dyna_draw_context_dispose;
116 event_context_class->setup = sp_dyna_draw_context_setup;
117 event_context_class->set = sp_dyna_draw_context_set;
118 event_context_class->root_handler = sp_dyna_draw_context_root_handler;
119 }
121 static void
122 sp_dyna_draw_context_init(SPDynaDrawContext *ddc)
123 {
124 SPEventContext *event_context = SP_EVENT_CONTEXT(ddc);
126 event_context->cursor_shape = cursor_calligraphy_xpm;
127 event_context->hot_x = 4;
128 event_context->hot_y = 4;
130 ddc->accumulated = NULL;
131 ddc->segments = NULL;
132 ddc->currentcurve = NULL;
133 ddc->currentshape = NULL;
134 ddc->npoints = 0;
135 ddc->cal1 = NULL;
136 ddc->cal2 = NULL;
137 ddc->repr = NULL;
139 /* DynaDraw values */
140 ddc->cur = NR::Point(0,0);
141 ddc->last = NR::Point(0,0);
142 ddc->vel = NR::Point(0,0);
143 ddc->acc = NR::Point(0,0);
144 ddc->ang = NR::Point(0,0);
145 ddc->del = NR::Point(0,0);
147 /* attributes */
148 ddc->dragging = FALSE;
150 ddc->mass = 0.3;
151 ddc->drag = DRAG_DEFAULT;
152 ddc->angle = 30.0;
153 ddc->width = 0.2;
155 ddc->vel_thin = 0.1;
156 ddc->flatness = 0.9;
158 ddc->abs_width = false;
159 ddc->keep_selected = true;
160 }
162 static void
163 sp_dyna_draw_context_dispose(GObject *object)
164 {
165 SPDynaDrawContext *ddc = SP_DYNA_DRAW_CONTEXT(object);
167 if (ddc->accumulated) {
168 ddc->accumulated = sp_curve_unref(ddc->accumulated);
169 }
171 while (ddc->segments) {
172 gtk_object_destroy(GTK_OBJECT(ddc->segments->data));
173 ddc->segments = g_slist_remove(ddc->segments, ddc->segments->data);
174 }
176 if (ddc->currentcurve) ddc->currentcurve = sp_curve_unref(ddc->currentcurve);
177 if (ddc->cal1) ddc->cal1 = sp_curve_unref(ddc->cal1);
178 if (ddc->cal2) ddc->cal2 = sp_curve_unref(ddc->cal2);
180 if (ddc->currentshape) {
181 gtk_object_destroy(GTK_OBJECT(ddc->currentshape));
182 ddc->currentshape = NULL;
183 }
185 if (ddc->_message_context) {
186 delete ddc->_message_context;
187 }
189 G_OBJECT_CLASS(parent_class)->dispose(object);
190 }
192 static void
193 sp_dyna_draw_context_setup(SPEventContext *ec)
194 {
195 SPDynaDrawContext *ddc = SP_DYNA_DRAW_CONTEXT(ec);
197 if (((SPEventContextClass *) parent_class)->setup)
198 ((SPEventContextClass *) parent_class)->setup(ec);
200 ddc->accumulated = sp_curve_new_sized(32);
201 ddc->currentcurve = sp_curve_new_sized(4);
203 ddc->cal1 = sp_curve_new_sized(32);
204 ddc->cal2 = sp_curve_new_sized(32);
206 ddc->currentshape = sp_canvas_item_new(sp_desktop_sketch(ec->desktop), SP_TYPE_CANVAS_BPATH, NULL);
207 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(ddc->currentshape), DDC_RED_RGBA, SP_WIND_RULE_EVENODD);
208 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(ddc->currentshape), 0x00000000, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
209 /* fixme: Cannot we cascade it to root more clearly? */
210 g_signal_connect(G_OBJECT(ddc->currentshape), "event", G_CALLBACK(sp_desktop_root_handler), ec->desktop);
212 sp_event_context_read(ec, "mass");
213 sp_event_context_read(ec, "drag");
214 sp_event_context_read(ec, "angle");
215 sp_event_context_read(ec, "width");
216 sp_event_context_read(ec, "thinning");
217 sp_event_context_read(ec, "tremor");
218 sp_event_context_read(ec, "flatness");
219 sp_event_context_read(ec, "usepressure");
220 sp_event_context_read(ec, "usetilt");
221 sp_event_context_read(ec, "abs_width");
222 sp_event_context_read(ec, "keep_selected");
224 ddc->is_drawing = false;
226 ddc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
227 }
229 static void
230 sp_dyna_draw_context_set(SPEventContext *ec, gchar const *key, gchar const *val)
231 {
232 SPDynaDrawContext *ddc = SP_DYNA_DRAW_CONTEXT(ec);
234 if (!strcmp(key, "mass")) {
235 double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.2 );
236 ddc->mass = CLAMP(dval, -1000.0, 1000.0);
237 } else if (!strcmp(key, "drag")) {
238 double const dval = ( val ? g_ascii_strtod (val, NULL) : DRAG_DEFAULT );
239 ddc->drag = CLAMP(dval, DRAG_MIN, DRAG_MAX);
240 } else if (!strcmp(key, "angle")) {
241 double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.0);
242 ddc->angle = CLAMP (dval, -90, 90);
243 } else if (!strcmp(key, "width")) {
244 double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.1 );
245 ddc->width = CLAMP(dval, -1000.0, 1000.0);
246 } else if (!strcmp(key, "thinning")) {
247 double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.1 );
248 ddc->vel_thin = CLAMP(dval, -1.0, 1.0);
249 } else if (!strcmp(key, "tremor")) {
250 double const dval = ( val ? g_ascii_strtod (val, NULL) : 0.0 );
251 ddc->tremor = CLAMP(dval, 0.0, 1.0);
252 } else if (!strcmp(key, "flatness")) {
253 double const dval = ( val ? g_ascii_strtod (val, NULL) : 1.0 );
254 ddc->flatness = CLAMP(dval, 0, 1.0);
255 } else if (!strcmp(key, "usepressure")) {
256 ddc->usepressure = (val && strcmp(val, "0"));
257 } else if (!strcmp(key, "usetilt")) {
258 ddc->usetilt = (val && strcmp(val, "0"));
259 } else if (!strcmp(key, "abs_width")) {
260 ddc->abs_width = (val && strcmp(val, "0"));
261 } else if (!strcmp(key, "keep_selected")) {
262 ddc->keep_selected = (val && strcmp(val, "0"));
263 }
265 //g_print("DDC: %g %g %g %g\n", ddc->mass, ddc->drag, ddc->angle, ddc->width);
266 }
268 static double
269 flerp(double f0, double f1, double p)
270 {
271 return f0 + ( f1 - f0 ) * p;
272 }
274 /* Get normalized point */
275 static NR::Point
276 sp_dyna_draw_get_npoint(SPDynaDrawContext const *dc, NR::Point v)
277 {
278 NR::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area();
279 double const max = MAX ( drect.dimensions()[NR::X], drect.dimensions()[NR::Y] );
280 return NR::Point(( v[NR::X] - drect.min()[NR::X] ) / max, ( v[NR::Y] - drect.min()[NR::Y] ) / max);
281 }
283 /* Get view point */
284 static NR::Point
285 sp_dyna_draw_get_vpoint(SPDynaDrawContext const *dc, NR::Point n)
286 {
287 NR::Rect drect = SP_EVENT_CONTEXT(dc)->desktop->get_display_area();
288 double const max = MAX ( drect.dimensions()[NR::X], drect.dimensions()[NR::Y] );
289 return NR::Point(n[NR::X] * max + drect.min()[NR::X], n[NR::Y] * max + drect.min()[NR::Y]);
290 }
292 static void
293 sp_dyna_draw_reset(SPDynaDrawContext *dc, NR::Point p)
294 {
295 dc->last = dc->cur = sp_dyna_draw_get_npoint(dc, p);
296 dc->vel = NR::Point(0,0);
297 dc->acc = NR::Point(0,0);
298 dc->ang = NR::Point(0,0);
299 dc->del = NR::Point(0,0);
300 }
302 static void
303 sp_dyna_draw_extinput(SPDynaDrawContext *dc, GdkEvent *event)
304 {
305 if (gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &dc->pressure))
306 dc->pressure = CLAMP (dc->pressure, DDC_MIN_PRESSURE, DDC_MAX_PRESSURE);
307 else
308 dc->pressure = DDC_DEFAULT_PRESSURE;
310 if (gdk_event_get_axis (event, GDK_AXIS_XTILT, &dc->xtilt))
311 dc->xtilt = CLAMP (dc->xtilt, DDC_MIN_TILT, DDC_MAX_TILT);
312 else
313 dc->xtilt = DDC_DEFAULT_TILT;
315 if (gdk_event_get_axis (event, GDK_AXIS_YTILT, &dc->ytilt))
316 dc->ytilt = CLAMP (dc->ytilt, DDC_MIN_TILT, DDC_MAX_TILT);
317 else
318 dc->ytilt = DDC_DEFAULT_TILT;
319 }
322 static gboolean
323 sp_dyna_draw_apply(SPDynaDrawContext *dc, NR::Point p)
324 {
325 NR::Point n = sp_dyna_draw_get_npoint(dc, p);
327 /* Calculate mass and drag */
328 double const mass = flerp(1.0, 160.0, dc->mass);
329 double const drag = flerp(0.0, 0.5, dc->drag * dc->drag);
331 /* Calculate force and acceleration */
332 NR::Point force = n - dc->cur;
333 if ( NR::L2(force) < DYNA_EPSILON ) {
334 return FALSE;
335 }
337 dc->acc = force / mass;
339 /* Calculate new velocity */
340 dc->vel += dc->acc;
342 /* Calculate angle of drawing tool */
344 double a1;
345 if (dc->usetilt) {
346 // 1a. calculate nib angle from input device tilt:
347 gdouble length = std::sqrt(dc->xtilt*dc->xtilt + dc->ytilt*dc->ytilt);;
349 if (length > 0) {
350 NR::Point ang1 = NR::Point(dc->ytilt/length, dc->xtilt/length);
351 a1 = atan2(ang1);
352 }
353 else
354 a1 = 0.0;
355 }
356 else {
357 // 1b. fixed dc->angle (absolutely flat nib):
358 double const radians = ( (dc->angle - 90) / 180.0 ) * M_PI;
359 NR::Point ang1 = NR::Point(-sin(radians), cos(radians));
360 a1 = atan2(ang1);
361 }
363 // 2. perpendicular to dc->vel (absolutely non-flat nib):
364 gdouble const mag_vel = NR::L2(dc->vel);
365 if ( mag_vel < DYNA_EPSILON ) {
366 return FALSE;
367 }
368 NR::Point ang2 = NR::rot90(dc->vel) / mag_vel;
370 // 3. Average them using flatness parameter:
371 // calculate angles
372 double a2 = atan2(ang2);
373 // flip a2 to force it to be in the same half-circle as a1
374 bool flipped = false;
375 if (fabs (a2-a1) > 0.5*M_PI) {
376 a2 += M_PI;
377 flipped = true;
378 }
379 // normalize a2
380 if (a2 > M_PI)
381 a2 -= 2*M_PI;
382 if (a2 < -M_PI)
383 a2 += 2*M_PI;
384 // find the flatness-weighted bisector angle, unflip if a2 was flipped
385 // FIXME: when dc->vel is oscillating around the fixed angle, the new_ang flips back and forth. How to avoid this?
386 double new_ang = a1 + (1 - dc->flatness) * (a2 - a1) - (flipped? M_PI : 0);
387 // convert to point
388 dc->ang = NR::Point (cos (new_ang), sin (new_ang));
390 /* Apply drag */
391 dc->vel *= 1.0 - drag;
393 /* Update position */
394 dc->last = dc->cur;
395 dc->cur += dc->vel;
397 return TRUE;
398 }
400 static void
401 sp_dyna_draw_brush(SPDynaDrawContext *dc)
402 {
403 g_assert( dc->npoints >= 0 && dc->npoints < SAMPLING_SIZE );
405 // How much velocity thins strokestyle
406 double vel_thin = flerp (0, 160, dc->vel_thin);
408 // Influence of pressure on thickness
409 double pressure_thick = (dc->usepressure ? dc->pressure : 1.0);
411 double width = ( pressure_thick - vel_thin * NR::L2(dc->vel) ) * dc->width;
413 double tremble_left = 0, tremble_right = 0;
414 if (dc->tremor > 0) {
415 // obtain two normally distributed random variables, using polar Box-Muller transform
416 double x1, x2, w, y1, y2;
417 do {
418 x1 = 2.0 * g_random_double_range(0,1) - 1.0;
419 x2 = 2.0 * g_random_double_range(0,1) - 1.0;
420 w = x1 * x1 + x2 * x2;
421 } while ( w >= 1.0 );
422 w = sqrt( (-2.0 * log( w ) ) / w );
423 y1 = x1 * w;
424 y2 = x2 * w;
426 // deflect both left and right edges randomly and independently, so that:
427 // (1) dc->tremor=1 corresponds to sigma=1, decreasing dc->tremor narrows the bell curve;
428 // (2) deflection depends on width, but is upped for small widths for better visual uniformity across widths;
429 // (3) deflection somewhat depends on speed, to prevent fast strokes looking
430 // comparatively smooth and slow ones excessively jittery
431 tremble_left = (y1)*dc->tremor * (0.15 + 0.8*width) * (0.35 + 14*NR::L2(dc->vel));
432 tremble_right = (y2)*dc->tremor * (0.15 + 0.8*width) * (0.35 + 14*NR::L2(dc->vel));
433 }
435 if ( width < 0.02 * dc->width ) {
436 width = 0.02 * dc->width;
437 }
439 double dezoomify_factor = 0.05 * 1000;
440 if (!dc->abs_width) {
441 dezoomify_factor /= SP_EVENT_CONTEXT(dc)->desktop->current_zoom();
442 }
444 NR::Point del_left = dezoomify_factor * (width + tremble_left) * dc->ang;
445 NR::Point del_right = dezoomify_factor * (width + tremble_right) * dc->ang;
447 NR::Point abs_middle = sp_dyna_draw_get_vpoint(dc, dc->cur);
449 dc->point1[dc->npoints] = abs_middle + del_left;
450 dc->point2[dc->npoints] = abs_middle - del_right;
452 dc->del = 0.5*(del_left + del_right);
454 dc->npoints++;
455 }
457 void
458 sp_ddc_update_toolbox (SPDesktop *desktop, const gchar *id, double value)
459 {
460 desktop->setToolboxAdjustmentValue (id, value);
461 }
463 gint
464 sp_dyna_draw_context_root_handler(SPEventContext *event_context,
465 GdkEvent *event)
466 {
467 SPDynaDrawContext *dc = SP_DYNA_DRAW_CONTEXT(event_context);
468 SPDesktop *desktop = event_context->desktop;
470 gint ret = FALSE;
472 switch (event->type) {
473 case GDK_BUTTON_PRESS:
474 if ( event->button.button == 1 ) {
476 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
478 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
479 return TRUE;
480 }
482 NR::Point const button_w(event->button.x,
483 event->button.y);
484 NR::Point const button_dt(desktop->w2d(button_w));
485 sp_dyna_draw_reset(dc, button_dt);
486 sp_dyna_draw_extinput(dc, event);
487 sp_dyna_draw_apply(dc, button_dt);
488 sp_curve_reset(dc->accumulated);
489 if (dc->repr) {
490 dc->repr = NULL;
491 }
493 /* initialize first point */
494 dc->npoints = 0;
496 sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
497 ( GDK_KEY_PRESS_MASK |
498 GDK_BUTTON_RELEASE_MASK |
499 GDK_POINTER_MOTION_MASK |
500 GDK_BUTTON_PRESS_MASK ),
501 NULL,
502 event->button.time);
504 ret = TRUE;
506 dc->is_drawing = true;
507 }
508 break;
509 case GDK_MOTION_NOTIFY:
510 if ( dc->is_drawing && ( event->motion.state & GDK_BUTTON1_MASK ) ) {
511 dc->dragging = TRUE;
513 NR::Point const motion_w(event->motion.x,
514 event->motion.y);
515 NR::Point const motion_dt(desktop->w2d(motion_w));
517 sp_dyna_draw_extinput(dc, event);
518 if (!sp_dyna_draw_apply(dc, motion_dt)) {
519 ret = TRUE;
520 break;
521 }
523 if ( dc->cur != dc->last ) {
524 sp_dyna_draw_brush(dc);
525 g_assert( dc->npoints > 0 );
526 fit_and_split(dc, FALSE);
527 }
528 ret = TRUE;
529 }
530 break;
532 case GDK_BUTTON_RELEASE:
533 sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
534 dc->is_drawing = false;
535 if ( dc->dragging && event->button.button == 1 ) {
536 dc->dragging = FALSE;
538 /* Remove all temporary line segments */
539 while (dc->segments) {
540 gtk_object_destroy(GTK_OBJECT(dc->segments->data));
541 dc->segments = g_slist_remove(dc->segments, dc->segments->data);
542 }
543 /* Create object */
544 fit_and_split(dc, TRUE);
545 accumulate_calligraphic(dc);
546 set_to_accumulated(dc); /* temporal implementation */
547 /* reset accumulated curve */
548 sp_curve_reset(dc->accumulated);
549 clear_current(dc);
550 if (dc->repr) {
551 dc->repr = NULL;
552 }
553 ret = TRUE;
554 }
555 break;
556 case GDK_KEY_PRESS:
557 switch (get_group0_keyval (&event->key)) {
558 case GDK_Up:
559 case GDK_KP_Up:
560 if (!MOD__CTRL_ONLY) {
561 dc->angle += 5.0;
562 if (dc->angle > 90.0)
563 dc->angle = 90.0;
564 sp_ddc_update_toolbox (desktop, "calligraphy-angle", dc->angle);
565 ret = TRUE;
566 }
567 break;
568 case GDK_Down:
569 case GDK_KP_Down:
570 if (!MOD__CTRL_ONLY) {
571 dc->angle -= 5.0;
572 if (dc->angle < -90.0)
573 dc->angle = -90.0;
574 sp_ddc_update_toolbox (desktop, "calligraphy-angle", dc->angle);
575 ret = TRUE;
576 }
577 break;
578 case GDK_Right:
579 case GDK_KP_Right:
580 if (!MOD__CTRL_ONLY) {
581 dc->width += 0.01;
582 if (dc->width > 1.0)
583 dc->width = 1.0;
584 sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width * 100); // the same spinbutton is for alt+x
585 ret = TRUE;
586 }
587 break;
588 case GDK_Left:
589 case GDK_KP_Left:
590 if (!MOD__CTRL_ONLY) {
591 dc->width -= 0.01;
592 if (dc->width < 0.01)
593 dc->width = 0.01;
594 sp_ddc_update_toolbox (desktop, "altx-calligraphy", dc->width * 100);
595 ret = TRUE;
596 }
597 break;
598 case GDK_x:
599 case GDK_X:
600 if (MOD__ALT_ONLY) {
601 desktop->setToolboxFocusTo ("altx-calligraphy");
602 ret = TRUE;
603 }
604 break;
605 case GDK_Escape:
606 sp_desktop_selection(desktop)->clear();
607 break;
609 default:
610 break;
611 }
612 default:
613 break;
614 }
616 if (!ret) {
617 if (((SPEventContextClass *) parent_class)->root_handler) {
618 ret = ((SPEventContextClass *) parent_class)->root_handler(event_context, event);
619 }
620 }
622 return ret;
623 }
626 static void
627 clear_current(SPDynaDrawContext *dc)
628 {
629 /* reset bpath */
630 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), NULL);
631 /* reset curve */
632 sp_curve_reset(dc->currentcurve);
633 sp_curve_reset(dc->cal1);
634 sp_curve_reset(dc->cal2);
635 /* reset points */
636 dc->npoints = 0;
637 }
639 static void
640 set_to_accumulated(SPDynaDrawContext *dc)
641 {
642 SPDesktop *desktop = SP_EVENT_CONTEXT(dc)->desktop;
644 if (!sp_curve_empty(dc->accumulated)) {
645 NArtBpath *abp;
646 gchar *str;
648 if (!dc->repr) {
649 /* Create object */
650 Inkscape::XML::Node *repr = sp_repr_new("svg:path");
652 /* Set style */
653 sp_desktop_apply_style_tool (desktop, repr, "tools.calligraphic", false);
655 dc->repr = repr;
657 SPItem *item=SP_ITEM(desktop->currentLayer()->appendChildRepr(dc->repr));
658 Inkscape::GC::release(dc->repr);
659 item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
660 item->updateRepr();
661 if (dc->keep_selected)
662 sp_desktop_selection(desktop)->set(dc->repr);
663 }
664 abp = nr_artpath_affine(sp_curve_first_bpath(dc->accumulated), sp_desktop_dt2root_affine(desktop));
665 str = sp_svg_write_path(abp);
666 g_assert( str != NULL );
667 nr_free(abp);
668 dc->repr->setAttribute("d", str);
669 g_free(str);
670 } else {
671 if (dc->repr) {
672 sp_repr_unparent(dc->repr);
673 }
674 dc->repr = NULL;
675 }
677 sp_document_done(sp_desktop_document(desktop));
678 }
680 static void
681 accumulate_calligraphic(SPDynaDrawContext *dc)
682 {
683 if ( !sp_curve_empty(dc->cal1) && !sp_curve_empty(dc->cal2) ) {
684 sp_curve_reset(dc->accumulated); /* Is this required ?? */
685 SPCurve *rev_cal2 = sp_curve_reverse(dc->cal2);
686 sp_curve_append(dc->accumulated, dc->cal1, FALSE);
687 sp_curve_append(dc->accumulated, rev_cal2, TRUE);
688 sp_curve_closepath(dc->accumulated);
690 sp_curve_unref(rev_cal2);
692 sp_curve_reset(dc->cal1);
693 sp_curve_reset(dc->cal2);
694 }
695 }
697 static void
698 fit_and_split(SPDynaDrawContext *dc,
699 gboolean release)
700 {
701 fit_and_split_calligraphics(dc, release);
702 }
704 static double square(double const x)
705 {
706 return x * x;
707 }
709 static void
710 fit_and_split_calligraphics(SPDynaDrawContext *dc, gboolean release)
711 {
712 double const tolerance_sq = square( NR::expansion(SP_EVENT_CONTEXT(dc)->desktop->w2d()) * TOLERANCE_CALLIGRAPHIC );
714 #ifdef DYNA_DRAW_VERBOSE
715 g_print("[F&S:R=%c]", release?'T':'F');
716 #endif
718 if (!( dc->npoints > 0 && dc->npoints < SAMPLING_SIZE ))
719 return; // just clicked
721 if ( dc->npoints == SAMPLING_SIZE - 1 || release ) {
722 #define BEZIER_SIZE 4
723 #define BEZIER_MAX_BEZIERS 8
724 #define BEZIER_MAX_LENGTH ( BEZIER_SIZE * BEZIER_MAX_BEZIERS )
726 #ifdef DYNA_DRAW_VERBOSE
727 g_print("[F&S:#] dc->npoints:%d, release:%s\n",
728 dc->npoints, release ? "TRUE" : "FALSE");
729 #endif
731 /* Current calligraphic */
732 if ( dc->cal1->end == 0 || dc->cal2->end == 0 ) {
733 /* dc->npoints > 0 */
734 /* g_print("calligraphics(1|2) reset\n"); */
735 sp_curve_reset(dc->cal1);
736 sp_curve_reset(dc->cal2);
738 sp_curve_moveto(dc->cal1, dc->point1[0]);
739 sp_curve_moveto(dc->cal2, dc->point2[0]);
740 }
742 NR::Point b1[BEZIER_MAX_LENGTH];
743 gint const nb1 = sp_bezier_fit_cubic_r(b1, dc->point1, dc->npoints,
744 tolerance_sq, BEZIER_MAX_BEZIERS);
745 g_assert( nb1 * BEZIER_SIZE <= gint(G_N_ELEMENTS(b1)) );
747 NR::Point b2[BEZIER_MAX_LENGTH];
748 gint const nb2 = sp_bezier_fit_cubic_r(b2, dc->point2, dc->npoints,
749 tolerance_sq, BEZIER_MAX_BEZIERS);
750 g_assert( nb2 * BEZIER_SIZE <= gint(G_N_ELEMENTS(b2)) );
752 if ( nb1 != -1 && nb2 != -1 ) {
753 /* Fit and draw and reset state */
754 #ifdef DYNA_DRAW_VERBOSE
755 g_print("nb1:%d nb2:%d\n", nb1, nb2);
756 #endif
757 /* CanvasShape */
758 if (! release) {
759 sp_curve_reset(dc->currentcurve);
760 sp_curve_moveto(dc->currentcurve, b1[0]);
761 for (NR::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) {
762 sp_curve_curveto(dc->currentcurve, bp1[1],
763 bp1[2], bp1[3]);
764 }
765 sp_curve_lineto(dc->currentcurve,
766 b2[BEZIER_SIZE*(nb2-1) + 3]);
767 for (NR::Point *bp2 = b2 + BEZIER_SIZE * ( nb2 - 1 ); bp2 >= b2; bp2 -= BEZIER_SIZE) {
768 sp_curve_curveto(dc->currentcurve, bp2[2], bp2[1], bp2[0]);
769 }
770 sp_curve_closepath(dc->currentcurve);
771 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), dc->currentcurve);
772 }
774 /* Current calligraphic */
775 for (NR::Point *bp1 = b1; bp1 < b1 + BEZIER_SIZE * nb1; bp1 += BEZIER_SIZE) {
776 sp_curve_curveto(dc->cal1, bp1[1], bp1[2], bp1[3]);
777 }
778 for (NR::Point *bp2 = b2; bp2 < b2 + BEZIER_SIZE * nb2; bp2 += BEZIER_SIZE) {
779 sp_curve_curveto(dc->cal2, bp2[1], bp2[2], bp2[3]);
780 }
781 } else {
782 /* fixme: ??? */
783 #ifdef DYNA_DRAW_VERBOSE
784 g_print("[fit_and_split_calligraphics] failed to fit-cubic.\n");
785 #endif
786 draw_temporary_box(dc);
788 for (gint i = 1; i < dc->npoints; i++) {
789 sp_curve_lineto(dc->cal1, dc->point1[i]);
790 }
791 for (gint i = 1; i < dc->npoints; i++) {
792 sp_curve_lineto(dc->cal2, dc->point2[i]);
793 }
794 }
796 /* Fit and draw and copy last point */
797 #ifdef DYNA_DRAW_VERBOSE
798 g_print("[%d]Yup\n", dc->npoints);
799 #endif
800 if (!release) {
801 g_assert(!sp_curve_empty(dc->currentcurve));
803 SPCanvasItem *cbp = sp_canvas_item_new(sp_desktop_sketch(SP_EVENT_CONTEXT(dc)->desktop),
804 SP_TYPE_CANVAS_BPATH,
805 NULL);
806 SPCurve *curve = sp_curve_copy(dc->currentcurve);
807 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH (cbp), curve);
808 sp_curve_unref(curve);
809 guint32 fillColor = sp_desktop_get_color_tool(SP_ACTIVE_DESKTOP, "tools.calligraphic", true);
810 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(cbp), fillColor , SP_WIND_RULE_EVENODD);
812 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cbp), 0x00000000, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
813 /* fixme: Cannot we cascade it to root more clearly? */
814 g_signal_connect(G_OBJECT(cbp), "event", G_CALLBACK(sp_desktop_root_handler), SP_EVENT_CONTEXT(dc)->desktop);
816 dc->segments = g_slist_prepend(dc->segments, cbp);
817 }
819 dc->point1[0] = dc->point1[dc->npoints - 1];
820 dc->point2[0] = dc->point2[dc->npoints - 1];
821 dc->npoints = 1;
822 } else {
823 draw_temporary_box(dc);
824 }
825 }
827 static void
828 draw_temporary_box(SPDynaDrawContext *dc)
829 {
830 sp_curve_reset(dc->currentcurve);
831 sp_curve_moveto(dc->currentcurve, dc->point1[0]);
832 for (gint i = 1; i < dc->npoints; i++) {
833 sp_curve_lineto(dc->currentcurve, dc->point1[i]);
834 }
835 for (gint i = dc->npoints-1; i >= 0; i--) {
836 sp_curve_lineto(dc->currentcurve, dc->point2[i]);
837 }
838 sp_curve_closepath(dc->currentcurve);
839 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(dc->currentshape), dc->currentcurve);
840 }
842 /*
843 Local Variables:
844 mode:c++
845 c-file-style:"stroustrup"
846 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
847 indent-tabs-mode:nil
848 fill-column:99
849 End:
850 */
851 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :