a80f565faf4725ea5059647b311ae59ccc705cbd
1 /** \file
2 * Pencil event context implementation.
3 */
5 /*
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * bulia byak <buliabyak@users.sf.net>
9 *
10 * Copyright (C) 2000 Lauris Kaplinski
11 * Copyright (C) 2000-2001 Ximian, Inc.
12 * Copyright (C) 2002 Lauris Kaplinski
13 * Copyright (C) 2004 Monash University
14 *
15 * Released under GNU GPL, read the file 'COPYING' for more information
16 */
18 #include <gdk/gdkkeysyms.h>
20 #include "pencil-context.h"
21 #include "desktop.h"
22 #include "desktop-handles.h"
23 #include "selection.h"
24 #include "selection-chemistry.h"
25 #include "draw-anchor.h"
26 #include "message-stack.h"
27 #include "message-context.h"
28 #include "modifier-fns.h"
29 #include "sp-path.h"
30 #include "preferences.h"
31 #include "snap.h"
32 #include "pixmaps/cursor-pencil.xpm"
33 #include "display/bezier-utils.h"
34 #include "display/canvas-bpath.h"
35 #include <glibmm/i18n.h>
36 #include "libnr/in-svg-plane.h"
37 #include "context-fns.h"
38 #include "sp-namedview.h"
39 #include "xml/repr.h"
40 #include "document.h"
41 #include "desktop-style.h"
42 #include "macros.h"
43 #include "display/curve.h"
45 static void sp_pencil_context_class_init(SPPencilContextClass *klass);
46 static void sp_pencil_context_init(SPPencilContext *pc);
47 static void sp_pencil_context_setup(SPEventContext *ec);
48 static void sp_pencil_context_dispose(GObject *object);
50 static gint sp_pencil_context_root_handler(SPEventContext *event_context, GdkEvent *event);
51 static gint pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &bevent);
52 static gint pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent);
53 static gint pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &revent);
54 static gint pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint const state);
56 static void spdc_set_startpoint(SPPencilContext *pc, Geom::Point const p);
57 static void spdc_set_endpoint(SPPencilContext *pc, Geom::Point const p);
58 static void spdc_finish_endpoint(SPPencilContext *pc);
59 static void spdc_add_freehand_point(SPPencilContext *pc, Geom::Point p, guint state);
60 static void fit_and_split(SPPencilContext *pc);
61 static void interpolate(SPPencilContext *pc);
63 static SPDrawContextClass *pencil_parent_class;
64 static Geom::Point pencil_drag_origin_w(0, 0);
65 static bool pencil_within_tolerance = false;
67 /**
68 * Register SPPencilContext class with Gdk and return its type number.
69 */
70 GType
71 sp_pencil_context_get_type()
72 {
73 static GType type = 0;
74 if (!type) {
75 GTypeInfo info = {
76 sizeof(SPPencilContextClass),
77 NULL, NULL,
78 (GClassInitFunc) sp_pencil_context_class_init,
79 NULL, NULL,
80 sizeof(SPPencilContext),
81 4,
82 (GInstanceInitFunc) sp_pencil_context_init,
83 NULL, /* value_table */
84 };
85 type = g_type_register_static(SP_TYPE_DRAW_CONTEXT, "SPPencilContext", &info, (GTypeFlags)0);
86 }
87 return type;
88 }
90 /**
91 * Initialize SPPencilContext vtable.
92 */
93 static void
94 sp_pencil_context_class_init(SPPencilContextClass *klass)
95 {
96 GObjectClass *object_class;
97 SPEventContextClass *event_context_class;
99 object_class = (GObjectClass *) klass;
100 event_context_class = (SPEventContextClass *) klass;
102 pencil_parent_class = (SPDrawContextClass*)g_type_class_peek_parent(klass);
104 object_class->dispose = sp_pencil_context_dispose;
106 event_context_class->setup = sp_pencil_context_setup;
107 event_context_class->root_handler = sp_pencil_context_root_handler;
108 }
110 /**
111 * Callback to initialize SPPencilContext object.
112 */
113 static void
114 sp_pencil_context_init(SPPencilContext *pc)
115 {
116 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
118 event_context->cursor_shape = cursor_pencil_xpm;
119 event_context->hot_x = 4;
120 event_context->hot_y = 4;
122 pc->npoints = 0;
123 pc->state = SP_PENCIL_CONTEXT_IDLE;
124 pc->req_tangent = Geom::Point(0, 0);
125 }
127 /**
128 * Callback to setup SPPencilContext object.
129 */
130 static void
131 sp_pencil_context_setup(SPEventContext *ec)
132 {
133 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
134 if (prefs->getBool("/tools/freehand/pencil/selcue")) {
135 ec->enableSelectionCue();
136 }
138 if (((SPEventContextClass *) pencil_parent_class)->setup) {
139 ((SPEventContextClass *) pencil_parent_class)->setup(ec);
140 }
142 SPPencilContext *const pc = SP_PENCIL_CONTEXT(ec);
143 pc->is_drawing = false;
145 pc->anchor_statusbar = false;
146 }
148 static void
149 sp_pencil_context_dispose(GObject *object)
150 {
151 G_OBJECT_CLASS(pencil_parent_class)->dispose(object);
152 }
154 /** Snaps new node relative to the previous node. */
155 static void
156 spdc_endpoint_snap(SPPencilContext const *pc, Geom::Point &p, guint const state)
157 {
158 if ((state & GDK_CONTROL_MASK)) { //CTRL enables constrained snapping
159 spdc_endpoint_snap_rotation(pc, p, pc->p[0], state);
160 } else {
161 if (!(state & GDK_SHIFT_MASK)) { //SHIFT disables all snapping, except the angular snapping above
162 //After all, the user explicitely asked for angular snapping by
163 //pressing CTRL
164 spdc_endpoint_snap_free(pc, p, state);
165 }
166 }
167 }
169 /**
170 * Callback for handling all pencil context events.
171 */
172 gint
173 sp_pencil_context_root_handler(SPEventContext *const ec, GdkEvent *event)
174 {
175 SPPencilContext *const pc = SP_PENCIL_CONTEXT(ec);
177 gint ret = FALSE;
179 switch (event->type) {
180 case GDK_BUTTON_PRESS:
181 ret = pencil_handle_button_press(pc, event->button);
182 break;
184 case GDK_MOTION_NOTIFY:
185 ret = pencil_handle_motion_notify(pc, event->motion);
186 break;
188 case GDK_BUTTON_RELEASE:
189 ret = pencil_handle_button_release(pc, event->button);
190 break;
192 case GDK_KEY_PRESS:
193 ret = pencil_handle_key_press(pc, get_group0_keyval (&event->key), event->key.state);
194 break;
196 default:
197 break;
198 }
200 if (!ret) {
201 gint (*const parent_root_handler)(SPEventContext *, GdkEvent *)
202 = ((SPEventContextClass *) pencil_parent_class)->root_handler;
203 if (parent_root_handler) {
204 ret = parent_root_handler(ec, event);
205 }
206 }
208 return ret;
209 }
211 static gint
212 pencil_handle_button_press(SPPencilContext *const pc, GdkEventButton const &bevent)
213 {
214 gint ret = FALSE;
215 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
216 if ( bevent.button == 1 && !event_context->space_panning) {
218 SPDrawContext *dc = SP_DRAW_CONTEXT (pc);
219 SPDesktop *desktop = SP_EVENT_CONTEXT_DESKTOP(dc);
220 Inkscape::Selection *selection = sp_desktop_selection(desktop);
222 if (Inkscape::have_viable_layer(desktop, dc->_message_context) == false) {
223 return TRUE;
224 }
226 Geom::Point const button_w(bevent.x, bevent.y);
228 /* Find desktop coordinates */
229 Geom::Point p = pc->desktop->w2d(button_w);
231 /* Test whether we hit any anchor. */
232 SPDrawAnchor *anchor = spdc_test_inside(pc, button_w);
234 pencil_drag_origin_w = Geom::Point(bevent.x,bevent.y);
235 pencil_within_tolerance = true;
237 switch (pc->state) {
238 case SP_PENCIL_CONTEXT_ADDLINE:
239 /* Current segment will be finished with release */
240 ret = TRUE;
241 break;
242 default:
243 /* Set first point of sequence */
244 if (bevent.state & GDK_CONTROL_MASK) {
245 spdc_create_single_dot(event_context, from_2geom(p), "/tools/freehand/pencil", bevent.state);
246 ret = true;
247 break;
248 }
249 if (anchor) {
250 p = to_2geom(anchor->dp);
251 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Continuing selected path"));
252 } else {
254 if (!(bevent.state & GDK_SHIFT_MASK)) {
256 // This is the first click of a new curve; deselect item so that
257 // this curve is not combined with it (unless it is drawn from its
258 // anchor, which is handled by the sibling branch above)
259 selection->clear();
260 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Creating new path"));
261 SnapManager &m = desktop->namedview->snap_manager;
262 m.setup(desktop);
263 Inkscape::SnappedPoint const s = m.freeSnap(Inkscape::SnapPreferences::SNAPPOINT_NODE, p);
264 if (s.getSnapped()) {
265 s.getPoint(p);
266 pc->prev_snap_was_succesful = true;
267 } else {
268 pc->prev_snap_was_succesful = false;
269 }
270 } else if (selection->singleItem() && SP_IS_PATH(selection->singleItem())) {
271 desktop->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Appending to selected path"));
272 }
273 }
274 pc->sa = anchor;
275 spdc_set_startpoint(pc, from_2geom(p));
276 ret = TRUE;
277 break;
278 }
280 pc->is_drawing = true;
281 }
282 return ret;
283 }
285 static gint
286 pencil_handle_motion_notify(SPPencilContext *const pc, GdkEventMotion const &mevent)
287 {
288 if ((mevent.state & GDK_CONTROL_MASK) && (mevent.state & GDK_BUTTON1_MASK)) {
289 // mouse was accidentally moved during Ctrl+click;
290 // ignore the motion and create a single point
291 pc->is_drawing = false;
292 return TRUE;
293 }
294 gint ret = FALSE;
295 SPDesktop *const dt = pc->desktop;
297 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
298 if (event_context->space_panning || mevent.state & GDK_BUTTON2_MASK || mevent.state & GDK_BUTTON3_MASK) {
299 // allow scrolling
300 return FALSE;
301 }
303 if ( ( mevent.state & GDK_BUTTON1_MASK ) && !pc->grab && pc->is_drawing) {
304 /* Grab mouse, so release will not pass unnoticed */
305 pc->grab = SP_CANVAS_ITEM(dt->acetate);
306 sp_canvas_item_grab(pc->grab, ( GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK |
307 GDK_BUTTON_RELEASE_MASK |
308 GDK_POINTER_MOTION_MASK ),
309 NULL, mevent.time);
310 }
312 /* Find desktop coordinates */
313 Geom::Point p = to_2geom(dt->w2d(Geom::Point(mevent.x, mevent.y)));
315 /* Test whether we hit any anchor. */
316 SPDrawAnchor *anchor = spdc_test_inside(pc, Geom::Point(mevent.x, mevent.y));
318 if (pencil_within_tolerance) {
319 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
320 gint const tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
321 if ( Geom::LInfty( Geom::Point(mevent.x,mevent.y) - pencil_drag_origin_w ) < tolerance ) {
322 return FALSE; // Do not drag if we're within tolerance from origin.
323 }
324 }
326 // Once the user has moved farther than tolerance from the original location
327 // (indicating they intend to move the object, not click), then always process the
328 // motion notify coordinates as given (no snapping back to origin)
329 pencil_within_tolerance = false;
331 switch (pc->state) {
332 case SP_PENCIL_CONTEXT_ADDLINE:
333 /* Set red endpoint */
334 if (anchor) {
335 p = to_2geom(anchor->dp);
336 } else {
337 Geom::Point ptnr = from_2geom(p);
338 spdc_endpoint_snap(pc, ptnr, mevent.state);
339 p = to_2geom(ptnr);
340 }
341 spdc_set_endpoint(pc, from_2geom(p));
342 ret = TRUE;
343 break;
344 default:
345 /* We may be idle or already freehand */
346 if ( mevent.state & GDK_BUTTON1_MASK && pc->is_drawing ) {
347 pc->state = SP_PENCIL_CONTEXT_FREEHAND;
348 if ( !pc->sa && !pc->green_anchor ) {
349 /* Create green anchor */
350 pc->green_anchor = sp_draw_anchor_new(pc, pc->green_curve, TRUE, pc->p[0]);
351 }
352 /** \todo
353 * fixme: I am not sure whether we want to snap to anchors
354 * in middle of freehand (Lauris)
355 */
356 SnapManager &m = dt->namedview->snap_manager;
358 if (anchor) {
359 p = to_2geom(anchor->dp);
360 } else if ((mevent.state & GDK_SHIFT_MASK) == 0) {
361 m.setup(dt);
362 Inkscape::SnappedPoint const s = m.freeSnap(Inkscape::SnapPreferences::SNAPPOINT_NODE, p);
363 if (s.getSnapped()) {
364 s.getPoint(p);
365 pc->prev_snap_was_succesful = true;
366 } else {
367 pc->prev_snap_was_succesful = false;
368 }
369 }
370 if ( pc->npoints != 0) { // buttonpress may have happened before we entered draw context!
371 if (!(pc->prev_snap_was_succesful && m.snapprefs.getSnapPostponedGlobally())) {
372 // When snapping is enabled but temporarily on hold because the mouse is moving
373 // fast, then we don't want to add nodes off-grid
374 spdc_add_freehand_point(pc, from_2geom(p), mevent.state);
375 ret = TRUE;
376 }
377 }
379 if (anchor && !pc->anchor_statusbar) {
380 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Release</b> here to close and finish the path."));
381 pc->anchor_statusbar = true;
382 } else if (!anchor && pc->anchor_statusbar) {
383 pc->_message_context->clear();
384 pc->anchor_statusbar = false;
385 } else if (!anchor) {
386 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("Drawing a freehand path"));
387 }
389 } else {
390 if (anchor && !pc->anchor_statusbar) {
391 pc->_message_context->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to continue the path from this point."));
392 pc->anchor_statusbar = true;
393 } else if (!anchor && pc->anchor_statusbar) {
394 pc->_message_context->clear();
395 pc->anchor_statusbar = false;
396 }
397 }
398 break;
399 }
400 return ret;
401 }
403 static gint
404 pencil_handle_button_release(SPPencilContext *const pc, GdkEventButton const &revent)
405 {
406 gint ret = FALSE;
408 SPEventContext *event_context = SP_EVENT_CONTEXT(pc);
409 if ( revent.button == 1 && pc->is_drawing && !event_context->space_panning) {
410 SPDesktop *const dt = pc->desktop;
412 pc->is_drawing = false;
414 /* Find desktop coordinates */
415 Geom::Point p = dt->w2d(Geom::Point(revent.x, revent.y));
417 /* Test whether we hit any anchor. */
418 SPDrawAnchor *anchor = spdc_test_inside(pc, Geom::Point(revent.x,
419 revent.y));
421 switch (pc->state) {
422 case SP_PENCIL_CONTEXT_IDLE:
423 /* Releasing button in idle mode means single click */
424 /* We have already set up start point/anchor in button_press */
425 if (!(revent.state & GDK_CONTROL_MASK)) {
426 // Ctrl+click creates a single point so only set context in ADDLINE mode when Ctrl isn't pressed
427 pc->state = SP_PENCIL_CONTEXT_ADDLINE;
428 }
429 ret = TRUE;
430 break;
431 case SP_PENCIL_CONTEXT_ADDLINE:
432 /* Finish segment now */
433 if (anchor) {
434 p = anchor->dp;
435 } else {
436 spdc_endpoint_snap(pc, p, revent.state);
437 }
438 pc->ea = anchor;
439 spdc_set_endpoint(pc, p);
440 spdc_finish_endpoint(pc);
441 pc->state = SP_PENCIL_CONTEXT_IDLE;
442 ret = TRUE;
443 break;
444 case SP_PENCIL_CONTEXT_FREEHAND:
445 /* Finish segment now */
446 /// \todo fixme: Clean up what follows (Lauris)
447 if (anchor) {
448 p = anchor->dp;
449 }
450 pc->ea = anchor;
451 /* Write curves to object */
453 dt->messageStack()->flash(Inkscape::NORMAL_MESSAGE, _("Finishing freehand"));
455 interpolate(pc);
456 spdc_concat_colors_and_flush(pc, FALSE);
457 pc->sa = NULL;
458 pc->ea = NULL;
459 if (pc->green_anchor) {
460 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
461 }
462 pc->state = SP_PENCIL_CONTEXT_IDLE;
463 ret = TRUE;
464 break;
465 default:
466 break;
467 }
469 if (pc->grab) {
470 /* Release grab now */
471 sp_canvas_item_ungrab(pc->grab, revent.time);
472 pc->grab = NULL;
473 }
475 ret = TRUE;
476 }
477 return ret;
478 }
480 static void
481 pencil_cancel (SPPencilContext *const pc)
482 {
483 if (pc->grab) {
484 /* Release grab now */
485 sp_canvas_item_ungrab(pc->grab, 0);
486 pc->grab = NULL;
487 }
489 pc->is_drawing = false;
491 pc->state = SP_PENCIL_CONTEXT_IDLE;
493 pc->red_curve->reset();
494 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
495 while (pc->green_bpaths) {
496 gtk_object_destroy(GTK_OBJECT(pc->green_bpaths->data));
497 pc->green_bpaths = g_slist_remove(pc->green_bpaths, pc->green_bpaths->data);
498 }
499 pc->green_curve->reset();
500 if (pc->green_anchor) {
501 pc->green_anchor = sp_draw_anchor_destroy(pc->green_anchor);
502 }
504 pc->_message_context->clear();
505 pc->_message_context->flash(Inkscape::NORMAL_MESSAGE, _("Drawing cancelled"));
507 sp_canvas_end_forced_full_redraws(pc->desktop->canvas);
508 }
510 static gint
511 pencil_handle_key_press(SPPencilContext *const pc, guint const keyval, guint const state)
512 {
513 gint ret = FALSE;
514 switch (keyval) {
515 case GDK_Up:
516 case GDK_Down:
517 case GDK_KP_Up:
518 case GDK_KP_Down:
519 // Prevent the zoom field from activation.
520 if (!mod_ctrl_only(state)) {
521 ret = TRUE;
522 }
523 break;
524 case GDK_Escape:
525 if (pc->npoints != 0) {
526 // if drawing, cancel, otherwise pass it up for deselecting
527 if (pc->state != SP_PENCIL_CONTEXT_IDLE) {
528 pencil_cancel (pc);
529 ret = TRUE;
530 }
531 }
532 break;
533 case GDK_z:
534 case GDK_Z:
535 if (mod_ctrl_only(state) && pc->npoints != 0) {
536 // if drawing, cancel, otherwise pass it up for undo
537 if (pc->state != SP_PENCIL_CONTEXT_IDLE) {
538 pencil_cancel (pc);
539 ret = TRUE;
540 }
541 }
542 break;
543 case GDK_g:
544 case GDK_G:
545 if (mod_shift_only(state)) {
546 sp_selection_to_guides(SP_EVENT_CONTEXT(pc)->desktop);
547 ret = true;
548 }
549 break;
550 default:
551 break;
552 }
553 return ret;
554 }
556 /**
557 * Reset points and set new starting point.
558 */
559 static void
560 spdc_set_startpoint(SPPencilContext *const pc, Geom::Point const p)
561 {
562 pc->npoints = 0;
563 pc->red_curve_is_valid = false;
564 if (in_svg_plane(p)) {
565 pc->p[pc->npoints++] = p;
566 }
567 }
569 /**
570 * Change moving endpoint position.
571 * <ul>
572 * <li>Ctrl constrains to moving to H/V direction, snapping in given direction.
573 * <li>Otherwise we snap freely to whatever attractors are available.
574 * </ul>
575 *
576 * Number of points is (re)set to 2 always, 2nd point is modified.
577 * We change RED curve.
578 */
579 static void
580 spdc_set_endpoint(SPPencilContext *const pc, Geom::Point const p)
581 {
582 if (pc->npoints == 0) {
583 return;
584 /* May occur if first point wasn't in SVG plane (e.g. weird w2d transform, perhaps from bad
585 * zoom setting).
586 */
587 }
588 g_return_if_fail( pc->npoints > 0 );
590 pc->red_curve->reset();
591 if ( ( p == pc->p[0] )
592 || !in_svg_plane(p) )
593 {
594 pc->npoints = 1;
595 } else {
596 pc->p[1] = p;
597 pc->npoints = 2;
599 pc->red_curve->moveto(pc->p[0]);
600 pc->red_curve->lineto(pc->p[1]);
601 pc->red_curve_is_valid = true;
603 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
604 }
605 }
607 /**
608 * Finalize addline.
609 *
610 * \todo
611 * fixme: I'd like remove red reset from concat colors (lauris).
612 * Still not sure, how it will make most sense.
613 */
614 static void
615 spdc_finish_endpoint(SPPencilContext *const pc)
616 {
617 if ( ( pc->red_curve->is_empty() )
618 || ( *(pc->red_curve->first_point()) == *(pc->red_curve->second_point()) ) )
619 {
620 pc->red_curve->reset();
621 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), NULL);
622 } else {
623 /* Write curves to object. */
624 spdc_concat_colors_and_flush(pc, FALSE);
625 pc->sa = NULL;
626 pc->ea = NULL;
627 }
628 }
631 static void
632 spdc_add_freehand_point(SPPencilContext *pc, Geom::Point p, guint /*state*/)
633 {
634 g_assert( pc->npoints > 0 );
635 g_return_if_fail(unsigned(pc->npoints) < G_N_ELEMENTS(pc->p));
637 if ( ( p != pc->p[ pc->npoints - 1 ] )
638 && in_svg_plane(p) )
639 {
640 pc->ps.push_back(p);
641 pc->p[pc->npoints++] = p;
642 fit_and_split(pc);
643 }
644 }
646 static inline double
647 square(double const x)
648 {
649 return x * x;
650 }
652 static void
653 interpolate(SPPencilContext *pc)
654 {
656 g_assert( pc->ps.size() > 1 );
658 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
659 double const tol = prefs->getDoubleLimited("/tools/freehand/pencil/tolerance", 10.0, 1.0, 100.0) * 0.4;
660 double const tolerance_sq = 0.02 * square( pc->desktop->w2d().descrim() *
661 tol) * exp(0.2*tol - 2);
663 g_assert(is_zero(pc->req_tangent)
664 || is_unit_vector(pc->req_tangent));
665 Geom::Point const tHatEnd(0, 0);
667 guint n_points = pc->ps.size();
668 pc->green_curve->reset();
669 pc->red_curve->reset();
670 pc->red_curve_is_valid = false;
672 Geom::Point * b = g_new(Geom::Point, 4*n_points);
673 Geom::Point * points = g_new(Geom::Point, 4*n_points);
674 for (int i = 0; i < pc->ps.size(); i++) {
675 points[i] = pc->ps[i];
676 }
678 // worst case gives us a segment per point
679 int max_segs = 4*n_points;
681 int const n_segs = sp_bezier_fit_cubic_r(b, points, n_points,
682 tolerance_sq, max_segs);
684 if ( n_segs > 0)
685 {
686 /* Fit and draw and reset state */
687 pc->green_curve->moveto(b[0]);
688 for (int c = 0; c < n_segs; c++) {
689 pc->green_curve->curveto(b[4*c+1], b[4*c+2], b[4*c+3]);
690 }
691 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->green_curve);
693 /* Fit and draw and copy last point */
694 g_assert(!pc->green_curve->is_empty());
696 /* Set up direction of next curve. */
697 {
698 Geom::CubicBezier const * last_seg = dynamic_cast<Geom::CubicBezier const *>(pc->green_curve->last_segment());
699 g_assert( last_seg ); // Relevance: validity of (*last_seg)[2]
700 pc->p[0] = last_seg->finalPoint();
701 pc->npoints = 1;
702 Geom::Point const req_vec( pc->p[0] - (*last_seg)[2] );
703 pc->req_tangent = ( ( Geom::is_zero(req_vec) || !in_svg_plane(req_vec) )
704 ? Geom::Point(0, 0)
705 : Geom::unit_vector(req_vec) );
706 }
707 }
708 g_free(b);
709 g_free(points);
710 pc->ps.clear();
711 }
714 static void
715 fit_and_split(SPPencilContext *pc)
716 {
717 g_assert( pc->npoints > 1 );
719 double const tolerance_sq = 0;
721 Geom::Point b[4];
722 g_assert(is_zero(pc->req_tangent)
723 || is_unit_vector(pc->req_tangent));
724 Geom::Point const tHatEnd(0, 0);
725 int const n_segs = sp_bezier_fit_cubic_full(b, NULL, pc->p, pc->npoints,
726 pc->req_tangent, tHatEnd,
727 tolerance_sq, 1);
728 if ( n_segs > 0
729 && unsigned(pc->npoints) < G_N_ELEMENTS(pc->p) )
730 {
731 /* Fit and draw and reset state */
732 pc->red_curve->reset();
733 pc->red_curve->moveto(b[0]);
734 pc->red_curve->curveto(b[1], b[2], b[3]);
735 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(pc->red_bpath), pc->red_curve);
736 pc->red_curve_is_valid = true;
737 } else {
738 /* Fit and draw and copy last point */
740 g_assert(!pc->red_curve->is_empty());
742 /* Set up direction of next curve. */
743 {
744 Geom::CubicBezier const * last_seg = dynamic_cast<Geom::CubicBezier const *>(pc->red_curve->last_segment());
745 g_assert( last_seg ); // Relevance: validity of (*last_seg)[2]
746 pc->p[0] = last_seg->finalPoint();
747 pc->npoints = 1;
748 Geom::Point const req_vec( pc->p[0] - (*last_seg)[2] );
749 pc->req_tangent = ( ( Geom::is_zero(req_vec) || !in_svg_plane(req_vec) )
750 ? Geom::Point(0, 0)
751 : Geom::unit_vector(req_vec) );
752 }
754 pc->green_curve->append_continuous(pc->red_curve, 0.0625);
755 SPCurve *curve = pc->red_curve->copy();
757 /// \todo fixme:
758 SPCanvasItem *cshape = sp_canvas_bpath_new(sp_desktop_sketch(pc->desktop), curve);
759 curve->unref();
760 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(cshape), pc->green_color, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
762 pc->green_bpaths = g_slist_prepend(pc->green_bpaths, cshape);
764 pc->red_curve_is_valid = false;
765 }
766 }
769 /*
770 Local Variables:
771 mode:c++
772 c-file-style:"stroustrup"
773 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
774 indent-tabs-mode:nil
775 fill-column:99
776 End:
777 */
778 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :