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