1 /*
2 * Base class for shapes, including <path> element
3 *
4 * Author:
5 * Lauris Kaplinski <lauris@kaplinski.com>
6 *
7 * Copyright (C) 1999-2002 Lauris Kaplinski
8 * Copyright (C) 2000-2001 Ximian, Inc.
9 * Copyright (C) 2004 John Cliff
10 * Copyright (C) 2007-2008 Johan Engelen
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
19 #include <libnr/n-art-bpath.h>
20 #include <libnr/nr-matrix-fns.h>
21 #include <libnr/nr-matrix-ops.h>
22 #include <libnr/nr-matrix-translate-ops.h>
23 #include <libnr/nr-scale-matrix-ops.h>
24 #include <2geom/rect.h>
25 #include <2geom/transforms.h>
26 #include <2geom/pathvector.h>
27 #include "helper/geom.h"
29 #include <sigc++/functors/ptr_fun.h>
30 #include <sigc++/adaptors/bind.h>
32 #include "macros.h"
33 #include "display/nr-arena-shape.h"
34 #include "display/curve.h"
35 #include "print.h"
36 #include "document.h"
37 #include "style.h"
38 #include "marker.h"
39 #include "sp-path.h"
40 #include "prefs-utils.h"
41 #include "attributes.h"
43 #include "live_effects/lpeobject.h"
44 #include "uri.h"
45 #include "extract-uri.h"
46 #include "uri-references.h"
47 #include "bad-uri-exception.h"
48 #include "xml/repr.h"
50 #include "util/mathfns.h" // for triangle_area()
52 #define noSHAPE_VERBOSE
54 static void sp_shape_class_init (SPShapeClass *klass);
55 static void sp_shape_init (SPShape *shape);
56 static void sp_shape_finalize (GObject *object);
58 static void sp_shape_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
59 static void sp_shape_release (SPObject *object);
61 static void sp_shape_set(SPObject *object, unsigned key, gchar const *value);
62 static void sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags);
63 static void sp_shape_modified (SPObject *object, unsigned int flags);
64 static Inkscape::XML::Node *sp_shape_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
66 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
67 void sp_shape_print (SPItem * item, SPPrintContext * ctx);
68 static NRArenaItem *sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
69 static void sp_shape_hide (SPItem *item, unsigned int key);
70 static void sp_shape_snappoints (SPItem const *item, SnapPointsIter p);
72 static void sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai);
74 static SPLPEItemClass *parent_class;
76 /**
77 * Registers the SPShape class with Gdk and returns its type number.
78 */
79 GType
80 sp_shape_get_type (void)
81 {
82 static GType type = 0;
83 if (!type) {
84 GTypeInfo info = {
85 sizeof (SPShapeClass),
86 NULL, NULL,
87 (GClassInitFunc) sp_shape_class_init,
88 NULL, NULL,
89 sizeof (SPShape),
90 16,
91 (GInstanceInitFunc) sp_shape_init,
92 NULL, /* value_table */
93 };
94 type = g_type_register_static (SP_TYPE_LPE_ITEM, "SPShape", &info, (GTypeFlags)0);
95 }
96 return type;
97 }
99 /**
100 * Initializes a SPShapeClass object. Establishes the function pointers to the class'
101 * member routines in the class vtable, and sets pointers to parent classes.
102 */
103 static void
104 sp_shape_class_init (SPShapeClass *klass)
105 {
106 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
107 SPObjectClass *sp_object_class = SP_OBJECT_CLASS(klass);
108 SPItemClass * item_class = SP_ITEM_CLASS(klass);
109 SPLPEItemClass * lpe_item_class = SP_LPE_ITEM_CLASS(klass);
111 parent_class = (SPLPEItemClass *)g_type_class_peek_parent (klass);
113 gobject_class->finalize = sp_shape_finalize;
115 sp_object_class->build = sp_shape_build;
116 sp_object_class->release = sp_shape_release;
117 sp_object_class->set = sp_shape_set;
118 sp_object_class->update = sp_shape_update;
119 sp_object_class->modified = sp_shape_modified;
120 sp_object_class->write = sp_shape_write;
122 item_class->bbox = sp_shape_bbox;
123 item_class->print = sp_shape_print;
124 item_class->show = sp_shape_show;
125 item_class->hide = sp_shape_hide;
126 item_class->snappoints = sp_shape_snappoints;
127 lpe_item_class->update_patheffect = NULL;
129 klass->set_shape = NULL;
130 }
132 /**
133 * Initializes an SPShape object.
134 */
135 static void
136 sp_shape_init (SPShape *shape)
137 {
138 for ( int i = 0 ; i < SP_MARKER_LOC_QTY ; i++ ) {
139 new (&shape->release_connect[i]) sigc::connection();
140 new (&shape->modified_connect[i]) sigc::connection();
141 }
142 }
144 static void
145 sp_shape_finalize (GObject *object)
146 {
147 SPShape *shape=(SPShape *)object;
149 for ( int i = 0 ; i < SP_MARKER_LOC_QTY ; i++ ) {
150 shape->release_connect[i].disconnect();
151 shape->release_connect[i].~connection();
152 shape->modified_connect[i].disconnect();
153 shape->modified_connect[i].~connection();
154 }
156 if (((GObjectClass *) (parent_class))->finalize) {
157 (* ((GObjectClass *) (parent_class))->finalize)(object);
158 }
159 }
161 /**
162 * Virtual build callback for SPMarker.
163 *
164 * This is to be invoked immediately after creation of an SPShape.
165 *
166 * \see sp_object_build()
167 */
168 static void
169 sp_shape_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
170 {
171 if (((SPObjectClass *) (parent_class))->build) {
172 (*((SPObjectClass *) (parent_class))->build) (object, document, repr);
173 }
174 }
176 /**
177 * Removes, releases and unrefs all children of object
178 *
179 * This is the inverse of sp_shape_build(). It must be invoked as soon
180 * as the shape is removed from the tree, even if it is still referenced
181 * by other objects. This routine also disconnects/unrefs markers and
182 * curves attached to it.
183 *
184 * \see sp_object_release()
185 */
186 static void
187 sp_shape_release (SPObject *object)
188 {
189 SPItem *item;
190 SPShape *shape;
191 SPItemView *v;
192 int i;
194 item = (SPItem *) object;
195 shape = (SPShape *) object;
197 for (i=SP_MARKER_LOC_START; i<SP_MARKER_LOC_QTY; i++) {
198 if (shape->marker[i]) {
199 sp_signal_disconnect_by_data (shape->marker[i], object);
200 for (v = item->display; v != NULL; v = v->next) {
201 sp_marker_hide ((SPMarker *) shape->marker[i], NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
202 }
203 shape->marker[i] = sp_object_hunref (shape->marker[i], object);
204 }
205 }
206 if (shape->curve) {
207 shape->curve = shape->curve->unref();
208 }
210 if (((SPObjectClass *) parent_class)->release) {
211 ((SPObjectClass *) parent_class)->release (object);
212 }
213 }
217 static void
218 sp_shape_set(SPObject *object, unsigned int key, gchar const *value)
219 {
220 if (((SPObjectClass *) parent_class)->set) {
221 ((SPObjectClass *) parent_class)->set(object, key, value);
222 }
223 }
225 static Inkscape::XML::Node *
226 sp_shape_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
227 {
228 if (((SPObjectClass *)(parent_class))->write) {
229 ((SPObjectClass *)(parent_class))->write(object, doc, repr, flags);
230 }
232 return repr;
233 }
235 /**
236 * Updates the shape when its attributes have changed. Also establishes
237 * marker objects to match the style settings.
238 */
239 static void
240 sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags)
241 {
242 SPItem *item = (SPItem *) object;
243 SPShape *shape = (SPShape *) object;
245 if (((SPObjectClass *) (parent_class))->update) {
246 (* ((SPObjectClass *) (parent_class))->update) (object, ctx, flags);
247 }
249 /* This stanza checks that an object's marker style agrees with
250 * the marker objects it has allocated. sp_shape_set_marker ensures
251 * that the appropriate marker objects are present (or absent) to
252 * match the style.
253 */
254 /* TODO: It would be nice if this could be done at an earlier level */
255 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
256 sp_shape_set_marker (object, i, object->style->marker[i].value);
257 }
259 if (flags & (SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
260 SPStyle *style;
261 style = SP_OBJECT_STYLE (object);
262 if (style->stroke_width.unit == SP_CSS_UNIT_PERCENT) {
263 SPItemCtx *ictx = (SPItemCtx *) ctx;
264 double const aw = 1.0 / NR::expansion(ictx->i2vp);
265 style->stroke_width.computed = style->stroke_width.value * aw;
266 for (SPItemView *v = ((SPItem *) (shape))->display; v != NULL; v = v->next) {
267 nr_arena_shape_set_style ((NRArenaShape *) v->arenaitem, style);
268 }
269 }
270 }
272 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
273 /* This is suboptimal, because changing parent style schedules recalculation */
274 /* But on the other hand - how can we know that parent does not tie style and transform */
275 NR::Maybe<NR::Rect> paintbox = SP_ITEM(object)->getBounds(NR::identity());
276 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
277 NRArenaShape * const s = NR_ARENA_SHAPE(v->arenaitem);
278 if (flags & SP_OBJECT_MODIFIED_FLAG) {
279 nr_arena_shape_set_path(s, shape->curve, (flags & SP_OBJECT_USER_MODIFIED_FLAG_B));
280 }
281 if (paintbox) {
282 s->setPaintBox(*paintbox);
283 }
284 }
285 }
287 if (sp_shape_has_markers (shape)) {
289 /* Dimension marker views */
290 for (SPItemView *v = item->display; v != NULL; v = v->next) {
292 if (!v->arenaitem->key) {
293 /* Get enough keys for all, start, mid and end marker types,
294 ** and set this view's arenaitem key to the first of these keys.
295 */
296 NR_ARENA_ITEM_SET_KEY (
297 v->arenaitem,
298 sp_item_display_key_new (SP_MARKER_LOC_QTY)
299 );
300 }
302 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
303 if (shape->marker[i]) {
304 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
305 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i - SP_MARKER_LOC,
306 sp_shape_number_of_markers (shape, i));
307 }
308 }
309 }
311 /* Update marker views */
312 for (SPItemView *v = item->display; v != NULL; v = v->next) {
313 sp_shape_update_marker_view (shape, v->arenaitem);
314 }
315 }
316 }
319 /**
320 * Works out whether a marker of a given type is required at a particular
321 * point on a shape.
322 *
323 * \param shape Shape of interest.
324 * \param m Marker type (e.g. SP_MARKER_LOC_START)
325 * \param bp Path segment.
326 * \return 1 if a marker is required here, otherwise 0.
327 */
328 bool
329 sp_shape_marker_required(SPShape const *shape, int const m, NArtBpath const *bp)
330 {
331 if (shape->marker[m] == NULL) {
332 return false;
333 }
335 if (bp == SP_CURVE_BPATH(shape->curve))
336 return m == SP_MARKER_LOC_START;
337 else if (bp[1].code == NR_END)
338 return m == SP_MARKER_LOC_END;
339 else
340 return m == SP_MARKER_LOC_MID;
341 }
343 static bool
344 is_moveto(NRPathcode const c)
345 {
346 return c == NR_MOVETO || c == NR_MOVETO_OPEN;
347 }
349 /**
350 * Helper function that advances a subpath's bpath to the first subpath
351 * by checking for moveto segments.
352 *
353 * \pre The bpath[] containing bp begins with a moveto.
354 */
355 static NArtBpath const *
356 first_seg_in_subpath(NArtBpath const *bp)
357 {
358 while (!is_moveto(bp->code)) {
359 --bp;
360 }
361 return bp;
362 }
364 /**
365 * Advances the bpath to the last segment in the subpath.
366 */
367 static NArtBpath const *
368 last_seg_in_subpath(NArtBpath const *bp)
369 {
370 for(;;) {
371 ++bp;
372 switch (bp->code) {
373 case NR_MOVETO:
374 case NR_MOVETO_OPEN:
375 case NR_END:
376 --bp;
377 return bp;
379 default: continue;
380 }
381 }
382 }
385 /* A subpath begins with a moveto and ends immediately before the next moveto or NR_END.
386 * (`moveto' here means either NR_MOVETO or NR_MOVETO_OPEN.) I'm assuming that non-empty
387 * paths always begin with a moveto.
388 *
389 * The control points of the subpath are the control points of the path elements of the subpath.
390 *
391 * As usual, the control points of a moveto or NR_LINETO are {c(3)}, and
392 * the control points of a NR_CURVETO are {c(1), c(2), c(3)}.
393 * (It follows from the definition that NR_END isn't part of a subpath.)
394 *
395 * The initial control point is bpath[bi0].c(3).
396 *
397 * Reference: http://www.w3.org/TR/SVG11/painting.html#MarkerElement, the `orient' attribute.
398 * Reference for behaviour of zero-length segments:
399 * http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
400 */
402 static double const no_tangent = 128.0; /* arbitrarily-chosen value outside the range of atan2,
403 * i.e. outside of [-pi, pi]. This value is incremented by
404 * 1 and checked using > to be safe from floating-point
405 * equality comparison madness.*/
407 /**
408 * Helper function to calculate the outgoing tangent of a path
409 * ( atan2(other - p0) )
410 * \pre The bpath[] containing bp0 begins with a moveto.
411 */
412 static double
413 outgoing_tangent(NArtBpath const *bp0)
414 {
415 /* See notes in comment block above. */
417 g_assert(bp0->code != NR_END);
418 NR::Point const &p0 = bp0->c(3);
419 NR::Point other;
420 for (NArtBpath const *bp = bp0;;) {
421 ++bp;
422 switch (bp->code) {
423 case NR_LINETO:
424 other = bp->c(3);
425 if (other != p0) {
426 goto found;
427 }
428 break;
430 case NR_CURVETO:
431 for (unsigned ci = 1; ci <= 3; ++ci) {
432 other = bp->c(ci);
433 if (other != p0) {
434 goto found;
435 }
436 }
437 break;
439 case NR_MOVETO_OPEN:
440 case NR_END:
441 case NR_MOVETO:
442 bp = first_seg_in_subpath(bp0);
443 if (bp == bp0) {
444 /* Gone right around the subpath without finding any different point since the
445 * initial moveto. */
446 return no_tangent + 1;
447 }
448 if (bp->code != NR_MOVETO) {
449 /* Open subpath. */
450 return no_tangent + 1;
451 }
452 other = bp->c(3);
453 if (other != p0) {
454 goto found;
455 }
456 break;
457 }
459 if (bp == bp0) {
460 /* Back where we started, so zero-length subpath. */
461 return no_tangent + 1;
463 /* Note: this test must come after we've looked at element bp, in case bp0 is a curve:
464 * we must look at c(1) and c(2). (E.g. single-curve subpath.)
465 */
466 }
467 }
469 found:
470 return atan2( other - p0 );
471 }
473 /**
474 * Helper function to calculate the incoming tangent of a path
475 * ( atan2(p0 - other) )
476 *
477 * \pre The bpath[] containing bp0 begins with a moveto.
478 */
479 static double
480 incoming_tangent(NArtBpath const *bp0)
481 {
482 /* See notes in comment block before outgoing_tangent. */
484 g_assert(bp0->code != NR_END);
485 NR::Point const &p0 = bp0->c(3);
486 NR::Point other;
487 for (NArtBpath const *bp = bp0;;) {
488 switch (bp->code) {
489 case NR_LINETO:
490 other = bp->c(3);
491 if (other != p0) {
492 goto found;
493 }
494 --bp;
495 break;
497 case NR_CURVETO:
498 for (unsigned ci = 3; ci != 0; --ci) {
499 other = bp->c(ci);
500 if (other != p0) {
501 goto found;
502 }
503 }
504 --bp;
505 break;
507 case NR_MOVETO:
508 case NR_MOVETO_OPEN:
509 other = bp->c(3);
510 if (other != p0) {
511 goto found;
512 }
513 if (bp->code != NR_MOVETO) {
514 /* Open subpath. */
515 return no_tangent + 1;
516 }
517 bp = last_seg_in_subpath(bp0);
518 break;
520 default: /* includes NR_END */
521 g_error("Found invalid path code %u in middle of path.", bp->code);
522 return no_tangent + 1;
523 }
525 if (bp == bp0) {
526 /* Back where we started from: zero-length subpath. */
527 return no_tangent + 1;
528 }
529 }
531 found:
532 return atan2( p0 - other );
533 }
536 /**
537 * Calculate the transform required to get a marker's path object in the
538 * right place for particular path segment on a shape. You should
539 * call sp_shape_marker_required first to see if a marker is required
540 * at this point.
541 *
542 * \see sp_shape_marker_required.
543 *
544 * \param shape Shape which the marker is for.
545 * \param m Marker type (e.g. SP_MARKER_LOC_START)
546 * \param bp Path segment which the arrow is for.
547 * \return Transform matrix.
548 */
549 NR::Matrix
550 sp_shape_marker_get_transform(SPShape const *shape, NArtBpath const *bp)
551 {
552 g_return_val_if_fail(( is_moveto(SP_CURVE_BPATH(shape->curve)[0].code)
553 && ( 0 < shape->curve->get_length() )
554 && ( SP_CURVE_BPATH(shape->curve)[shape->curve->get_length()].code == NR_END ) ),
555 NR::Matrix(NR::translate(bp->c(3))));
556 double const angle1 = incoming_tangent(bp);
557 double const angle2 = outgoing_tangent(bp);
559 double ret_angle;
560 if (angle1 > no_tangent) {
561 /* First vertex of an open subpath. */
562 ret_angle = ( angle2 > no_tangent
563 ? 0.
564 : angle2 );
565 } else if (angle2 > no_tangent) {
566 /* Last vertex of an open subpath. */
567 ret_angle = angle1;
568 } else {
569 ret_angle = .5 * (angle1 + angle2);
571 if ( fabs( angle2 - angle1 ) > M_PI ) {
572 /* ret_angle is in the middle of the larger of the two sectors between angle1 and
573 * angle2, so flip it by 180degrees to force it to the middle of the smaller sector.
574 *
575 * (Imagine a circle with rays drawn at angle1 and angle2 from the centre of the
576 * circle. Those two rays divide the circle into two sectors.)
577 */
578 ret_angle += M_PI;
579 }
580 }
582 return NR::Matrix(NR::rotate(ret_angle)) * NR::translate(bp->c(3));
583 }
585 /**
586 * Calculate the transform required to get a marker's path object in the
587 * right place for particular path segment on a shape.
588 *
589 * \see sp_shape_marker_update_marker_view.
590 *
591 * From SVG spec:
592 * The axes of the temporary new user coordinate system are aligned according to the orient attribute on the 'marker'
593 * element and the slope of the curve at the given vertex. (Note: if there is a discontinuity at a vertex, the slope
594 * is the average of the slopes of the two segments of the curve that join at the given vertex. If a slope cannot be
595 * determined, the slope is assumed to be zero.)
596 */
597 Geom::Matrix
598 sp_shape_marker_get_transform(Geom::Curve const & c1, Geom::Curve const & c2)
599 {
600 Geom::Point p = c1.pointAt(1);
601 Geom::Curve * c1_reverse = c1.reverse();
602 Geom::Point tang1 = - c1_reverse->unitTangentAt(0);
603 delete c1_reverse;
604 Geom::Point tang2 = c2.unitTangentAt(0);
606 double const angle1 = Geom::atan2(tang1);
607 double const angle2 = Geom::atan2(tang2);
609 double ret_angle;
610 ret_angle = .5 * (angle1 + angle2);
612 if ( fabs( angle2 - angle1 ) > M_PI ) {
613 /* ret_angle is in the middle of the larger of the two sectors between angle1 and
614 * angle2, so flip it by 180degrees to force it to the middle of the smaller sector.
615 *
616 * (Imagine a circle with rays drawn at angle1 and angle2 from the centre of the
617 * circle. Those two rays divide the circle into two sectors.)
618 */
619 ret_angle += M_PI;
620 }
622 return Geom::Rotate(ret_angle) * Geom::Translate(p);
623 }
624 Geom::Matrix
625 sp_shape_marker_get_transform_at_start(Geom::Curve const & c)
626 {
627 Geom::Point p = c.pointAt(0);
628 Geom::Point tang = c.unitTangentAt(0);
630 double const angle = Geom::atan2(tang);
632 return Geom::Rotate(angle) * Geom::Translate(p);
633 }
634 Geom::Matrix
635 sp_shape_marker_get_transform_at_end(Geom::Curve const & c)
636 {
637 Geom::Point p = c.pointAt(1);
638 Geom::Curve * c_reverse = c.reverse();
639 Geom::Point tang = - c_reverse->unitTangentAt(0);
640 delete c_reverse;
642 double const angle = Geom::atan2(tang);
644 return Geom::Rotate(angle) * Geom::Translate(p);
645 }
647 /**
648 * Updates the instances (views) of a given marker in a shape.
649 * Marker views have to be scaled already. The transformation
650 * is retrieved and then shown by calling sp_marker_show_instance.
651 *
652 * TODO: correctly handle the 'marker' attribute.
653 * "Using the marker property from a style sheet is equivalent to using all three (start, mid, end)."
654 * See painting-marker-03-f.svg in SVG 1.1 Full test suite.
655 */
656 static void
657 sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai)
658 {
659 SPStyle *style = ((SPObject *) shape)->style;
661 // position arguments to sp_marker_show_instance, basically counts the amount of markers.
662 int start_pos = 0;
663 int mid_pos = 0;
664 int end_pos = 0;
666 Geom::PathVector const & pathv = shape->curve->get_pathvector();
667 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
668 if ( shape->marker[SP_MARKER_LOC_START] ) {
669 Geom::Matrix const m (sp_shape_marker_get_transform_at_start(path_it->front()));
670 sp_marker_show_instance ((SPMarker* ) shape->marker[SP_MARKER_LOC_START], ai,
671 NR_ARENA_ITEM_GET_KEY(ai) + SP_MARKER_LOC_START, start_pos, m,
672 style->stroke_width.computed);
673 start_pos++;
674 }
676 if ( shape->marker[SP_MARKER_LOC_MID] ) {
677 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
678 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
679 while (curve_it2 != path_it->end_default())
680 {
681 /* Put marker between curve_it1 and curve_it2.
682 * Loop to end_default (so including closing segment), because when a path is closed,
683 * there should be a midpoint marker between last segment and closing straight line segment
684 */
685 Geom::Matrix const m (sp_shape_marker_get_transform(*curve_it1, *curve_it2));
686 sp_marker_show_instance ((SPMarker* ) shape->marker[SP_MARKER_LOC_MID], ai,
687 NR_ARENA_ITEM_GET_KEY(ai) + SP_MARKER_LOC_MID, mid_pos, m,
688 style->stroke_width.computed);
689 mid_pos++;
691 ++curve_it1;
692 ++curve_it2;
693 }
694 }
696 if ( shape->marker[SP_MARKER_LOC_END] ) {
697 Geom::Matrix const m (sp_shape_marker_get_transform_at_end(path_it->back_default()));
698 sp_marker_show_instance ((SPMarker* ) shape->marker[SP_MARKER_LOC_END], ai,
699 NR_ARENA_ITEM_GET_KEY(ai) + SP_MARKER_LOC_END, end_pos, m,
700 style->stroke_width.computed);
701 end_pos++;
702 }
703 }
704 }
706 /**
707 * Sets modified flag for all sub-item views.
708 */
709 static void
710 sp_shape_modified (SPObject *object, unsigned int flags)
711 {
712 SPShape *shape = SP_SHAPE (object);
714 if (((SPObjectClass *) (parent_class))->modified) {
715 (* ((SPObjectClass *) (parent_class))->modified) (object, flags);
716 }
718 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
719 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
720 nr_arena_shape_set_style (NR_ARENA_SHAPE (v->arenaitem), object->style);
721 }
722 }
723 }
725 /**
726 * Calculates the bounding box for item, storing it into bbox.
727 * This also includes the bounding boxes of any markers included in the shape.
728 */
729 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
730 {
731 SPShape const *shape = SP_SHAPE (item);
733 if (shape->curve) {
735 NRRect cbbox;
737 Geom::Rect geombbox = bounds_exact_transformed(shape->curve->get_pathvector(), to_2geom(transform));
738 cbbox.x0 = geombbox[0][0];
739 cbbox.y0 = geombbox[1][0];
740 cbbox.x1 = geombbox[0][1];
741 cbbox.y1 = geombbox[1][1];
743 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
745 SPStyle* style=SP_OBJECT_STYLE (item);
746 if (!style->stroke.isNone()) {
747 double const scale = expansion(transform);
748 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
749 double const width = MAX(0.125, style->stroke_width.computed * scale);
750 if ( fabs(cbbox.x1-cbbox.x0) > -0.00001 && fabs(cbbox.y1-cbbox.y0) > -0.00001 ) {
751 cbbox.x0-=0.5*width;
752 cbbox.x1+=0.5*width;
753 cbbox.y0-=0.5*width;
754 cbbox.y1+=0.5*width;
755 }
756 }
757 }
759 // Union with bboxes of the markers, if any
760 if (sp_shape_has_markers (shape)) {
761 /* TODO: make code prettier: lots of variables can be taken out of the loop! */
762 Geom::PathVector const & pathv = shape->curve->get_pathvector();
763 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
764 if ( shape->marker[SP_MARKER_LOC_START] ) {
765 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
766 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
768 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform_at_start(path_it->front())));
770 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
771 tr = NR::scale(style->stroke_width.computed) * tr;
772 }
774 // total marker transform
775 tr = marker_item->transform * marker->c2p * tr * transform;
777 // get bbox of the marker with that transform
778 NRRect marker_bbox;
779 sp_item_invoke_bbox (marker_item, &marker_bbox, tr, true);
780 // union it with the shape bbox
781 nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
782 }
784 if ( shape->marker[SP_MARKER_LOC_MID] ) {
785 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
786 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
787 while (curve_it2 != path_it->end_default())
788 {
789 /* Put marker between curve_it1 and curve_it2.
790 * Loop to end_default (so including closing segment), because when a path is closed,
791 * there should be a midpoint marker between last segment and closing straight line segment */
793 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
794 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
796 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform(*curve_it1, *curve_it2)));
798 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
799 tr = NR::scale(style->stroke_width.computed) * tr;
800 }
802 // total marker transform
803 tr = marker_item->transform * marker->c2p * tr * transform;
805 // get bbox of the marker with that transform
806 NRRect marker_bbox;
807 sp_item_invoke_bbox (marker_item, &marker_bbox, tr, true);
808 // union it with the shape bbox
809 nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
811 ++curve_it1;
812 ++curve_it2;
813 }
814 }
816 if ( shape->marker[SP_MARKER_LOC_END] ) {
817 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
818 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
820 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform_at_end(path_it->back_default())));
822 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
823 tr = NR::scale(style->stroke_width.computed) * tr;
824 }
826 // total marker transform
827 tr = marker_item->transform * marker->c2p * tr * transform;
829 // get bbox of the marker with that transform
830 NRRect marker_bbox;
831 sp_item_invoke_bbox (marker_item, &marker_bbox, tr, true);
832 // union it with the shape bbox
833 nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
834 }
835 }
836 }
837 }
839 // copy our bbox to the variable we're given
840 *bbox = cbbox;
841 }
842 }
844 /**
845 * Prepares shape for printing. Handles printing of comments for printing
846 * debugging, sizes the item to fit into the document width/height,
847 * applies print fill/stroke, sets transforms for markers, and adds
848 * comment labels.
849 */
850 void
851 sp_shape_print (SPItem *item, SPPrintContext *ctx)
852 {
853 NRRect pbox, dbox, bbox;
855 SPShape *shape = SP_SHAPE(item);
857 if (!shape->curve) return;
859 gint add_comments = prefs_get_int_attribute_limited ("printing.debug", "add-label-comments", 0, 0, 1);
860 if (add_comments) {
861 gchar * comment = g_strdup_printf("begin '%s'",
862 SP_OBJECT(item)->defaultLabel());
863 sp_print_comment(ctx, comment);
864 g_free(comment);
865 }
867 /* fixme: Think (Lauris) */
868 sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
869 dbox.x0 = 0.0;
870 dbox.y0 = 0.0;
871 dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
872 dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
873 sp_item_bbox_desktop (item, &bbox);
874 NR::Matrix const i2d = from_2geom(sp_item_i2d_affine(item));
876 SPStyle* style = SP_OBJECT_STYLE (item);
878 if (!style->fill.isNone()) {
879 const_NRBPath bp;
880 bp.path = SP_CURVE_BPATH(shape->curve);
881 sp_print_fill (ctx, &bp, &i2d, style, &pbox, &dbox, &bbox);
882 }
884 if (!style->stroke.isNone()) {
885 const_NRBPath bp;
886 bp.path = SP_CURVE_BPATH(shape->curve);
887 sp_print_stroke (ctx, &bp, &i2d, style, &pbox, &dbox, &bbox);
888 }
890 /* TODO: make code prettier: lots of variables can be taken out of the loop! */
891 Geom::PathVector const & pathv = shape->curve->get_pathvector();
892 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
893 if ( shape->marker[SP_MARKER_LOC_START] ) {
894 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
895 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
897 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform_at_start(path_it->front())));
899 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
900 tr = NR::scale(style->stroke_width.computed) * tr;
901 }
903 tr = marker_item->transform * marker->c2p * tr;
905 NR::Matrix old_tr = marker_item->transform;
906 marker_item->transform = tr;
907 sp_item_invoke_print (marker_item, ctx);
908 marker_item->transform = old_tr;
909 }
911 if ( shape->marker[SP_MARKER_LOC_MID] ) {
912 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
913 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
914 while (curve_it2 != path_it->end_default())
915 {
916 /* Put marker between curve_it1 and curve_it2.
917 * Loop to end_default (so including closing segment), because when a path is closed,
918 * there should be a midpoint marker between last segment and closing straight line segment */
920 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
921 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
923 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform(*curve_it1, *curve_it2)));
925 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
926 tr = NR::scale(style->stroke_width.computed) * tr;
927 }
929 tr = marker_item->transform * marker->c2p * tr;
931 NR::Matrix old_tr = marker_item->transform;
932 marker_item->transform = tr;
933 sp_item_invoke_print (marker_item, ctx);
934 marker_item->transform = old_tr;
936 ++curve_it1;
937 ++curve_it2;
938 }
939 }
941 if ( shape->marker[SP_MARKER_LOC_END] ) {
942 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
943 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
945 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform_at_end(path_it->back_default())));
947 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
948 tr = NR::scale(style->stroke_width.computed) * tr;
949 }
951 tr = marker_item->transform * marker->c2p * tr;
953 NR::Matrix old_tr = marker_item->transform;
954 marker_item->transform = tr;
955 sp_item_invoke_print (marker_item, ctx);
956 marker_item->transform = old_tr;
957 }
958 }
960 if (add_comments) {
961 gchar * comment = g_strdup_printf("end '%s'",
962 SP_OBJECT(item)->defaultLabel());
963 sp_print_comment(ctx, comment);
964 g_free(comment);
965 }
966 }
968 /**
969 * Sets style, path, and paintbox. Updates marker views, including dimensions.
970 */
971 static NRArenaItem *
972 sp_shape_show (SPItem *item, NRArena *arena, unsigned int /*key*/, unsigned int /*flags*/)
973 {
974 SPObject *object = SP_OBJECT(item);
975 SPShape *shape = SP_SHAPE(item);
977 NRArenaItem *arenaitem = NRArenaShape::create(arena);
978 NRArenaShape * const s = NR_ARENA_SHAPE(arenaitem);
979 nr_arena_shape_set_style(s, object->style);
980 nr_arena_shape_set_path(s, shape->curve, false);
981 NR::Maybe<NR::Rect> paintbox = item->getBounds(NR::identity());
982 if (paintbox) {
983 s->setPaintBox(*paintbox);
984 }
986 if (sp_shape_has_markers (shape)) {
988 /* Dimension the marker views */
989 if (!arenaitem->key) {
990 NR_ARENA_ITEM_SET_KEY (arenaitem, sp_item_display_key_new (SP_MARKER_LOC_QTY));
991 }
993 for (int i = 0; i < SP_MARKER_LOC_QTY; i++) {
994 if (shape->marker[i]) {
995 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
996 NR_ARENA_ITEM_GET_KEY (arenaitem) + i - SP_MARKER_LOC,
997 sp_shape_number_of_markers (shape, i));
998 }
999 }
1002 /* Update marker views */
1003 sp_shape_update_marker_view (shape, arenaitem);
1004 }
1006 return arenaitem;
1007 }
1009 /**
1010 * Hides/removes marker views from the shape.
1011 */
1012 static void
1013 sp_shape_hide (SPItem *item, unsigned int key)
1014 {
1015 SPShape *shape;
1016 SPItemView *v;
1017 int i;
1019 shape = (SPShape *) item;
1021 for (i=0; i<SP_MARKER_LOC_QTY; i++) {
1022 if (shape->marker[i]) {
1023 for (v = item->display; v != NULL; v = v->next) {
1024 if (key == v->key) {
1025 sp_marker_hide ((SPMarker *) shape->marker[i],
1026 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
1027 }
1028 }
1029 }
1030 }
1032 if (((SPItemClass *) parent_class)->hide) {
1033 ((SPItemClass *) parent_class)->hide (item, key);
1034 }
1035 }
1037 /**
1038 * \param shape Shape.
1039 * \return TRUE if the shape has any markers, or FALSE if not.
1040 */
1041 int
1042 sp_shape_has_markers (SPShape const *shape)
1043 {
1044 /* Note, we're ignoring 'marker' settings, which technically should apply for
1045 all three settings. This should be fixed later such that if 'marker' is
1046 specified, then all three should appear. */
1048 return (
1049 shape->curve &&
1050 (shape->marker[SP_MARKER_LOC_START] ||
1051 shape->marker[SP_MARKER_LOC_MID] ||
1052 shape->marker[SP_MARKER_LOC_END])
1053 );
1054 }
1057 /**
1058 * \param shape Shape.
1059 * \param type Marker type (e.g. SP_MARKER_LOC_START)
1060 * \return Number of markers that the shape has of this type.
1061 */
1062 int
1063 sp_shape_number_of_markers (SPShape *shape, int type)
1064 {
1065 // TODO fixme: this looks very bad that the type parameter is ignored.
1066 Geom::PathVector const & pathv = shape->curve->get_pathvector();
1068 guint n = shape->marker[SP_MARKER_LOC_START] ? pathv.size() : 0;
1070 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
1071 n += shape->marker[SP_MARKER_LOC_MID] ? path_it->size() : 0;
1073 if ( shape->marker[SP_MARKER_LOC_END] && !path_it->empty()) {
1074 n++;
1075 }
1076 }
1078 return n;
1079 }
1081 /**
1082 * Checks if the given marker is used in the shape, and if so, it
1083 * releases it by calling sp_marker_hide. Also detaches signals
1084 * and unrefs the marker from the shape.
1085 */
1086 static void
1087 sp_shape_marker_release (SPObject *marker, SPShape *shape)
1088 {
1089 SPItem *item;
1090 int i;
1092 item = (SPItem *) shape;
1094 for (i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
1095 if (marker == shape->marker[i]) {
1096 SPItemView *v;
1097 /* Hide marker */
1098 for (v = item->display; v != NULL; v = v->next) {
1099 sp_marker_hide ((SPMarker *) (shape->marker[i]), NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
1100 /* fixme: Do we need explicit remove here? (Lauris) */
1101 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
1102 }
1103 /* Detach marker */
1104 sp_signal_disconnect_by_data (shape->marker[i], item);
1105 shape->marker[i] = sp_object_hunref (shape->marker[i], item);
1106 }
1107 }
1108 }
1110 /**
1111 * No-op. Exists for handling 'modified' messages
1112 */
1113 static void
1114 sp_shape_marker_modified (SPObject */*marker*/, guint /*flags*/, SPItem */*item*/)
1115 {
1116 /* I think mask does update automagically */
1117 /* g_warning ("Item %s mask %s modified", SP_OBJECT_ID (item), SP_OBJECT_ID (mask)); */
1118 }
1120 /**
1121 * Adds a new marker to shape object at the location indicated by key. value
1122 * must be a valid URI reference resolvable from the shape object (i.e., present
1123 * in the document <defs>). If the shape object already has a marker
1124 * registered at the given position, it is removed first. Then the
1125 * new marker is hrefed and its signals connected.
1126 */
1127 void
1128 sp_shape_set_marker (SPObject *object, unsigned int key, const gchar *value)
1129 {
1130 SPItem *item = (SPItem *) object;
1131 SPShape *shape = (SPShape *) object;
1133 if (key < SP_MARKER_LOC_START || key > SP_MARKER_LOC_END) {
1134 return;
1135 }
1137 SPObject *mrk = sp_css_uri_reference_resolve (SP_OBJECT_DOCUMENT (object), value);
1138 if (mrk != shape->marker[key]) {
1139 if (shape->marker[key]) {
1140 SPItemView *v;
1142 /* Detach marker */
1143 shape->release_connect[key].disconnect();
1144 shape->modified_connect[key].disconnect();
1146 /* Hide marker */
1147 for (v = item->display; v != NULL; v = v->next) {
1148 sp_marker_hide ((SPMarker *) (shape->marker[key]),
1149 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + key);
1150 /* fixme: Do we need explicit remove here? (Lauris) */
1151 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
1152 }
1154 /* Unref marker */
1155 shape->marker[key] = sp_object_hunref (shape->marker[key], object);
1156 }
1157 if (SP_IS_MARKER (mrk)) {
1158 shape->marker[key] = sp_object_href (mrk, object);
1159 shape->release_connect[key] = mrk->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_shape_marker_release), shape));
1160 shape->modified_connect[key] = mrk->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_shape_marker_modified), shape));
1161 }
1162 }
1163 }
1167 /* Shape section */
1169 /**
1170 * Calls any registered handlers for the set_shape action
1171 */
1172 void
1173 sp_shape_set_shape (SPShape *shape)
1174 {
1175 g_return_if_fail (shape != NULL);
1176 g_return_if_fail (SP_IS_SHAPE (shape));
1178 if (SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape) {
1179 SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape (shape);
1180 }
1181 }
1183 /**
1184 * Adds a curve to the shape. If owner is specified, a reference
1185 * will be made, otherwise the curve will be copied into the shape.
1186 * Any existing curve in the shape will be unreferenced first.
1187 * This routine also triggers a request to update the display.
1188 */
1189 void
1190 sp_shape_set_curve (SPShape *shape, SPCurve *curve, unsigned int owner)
1191 {
1192 if (shape->curve) {
1193 shape->curve = shape->curve->unref();
1194 }
1195 if (curve) {
1196 if (owner) {
1197 shape->curve = curve->ref();
1198 } else {
1199 shape->curve = curve->copy();
1200 }
1201 }
1202 SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1203 }
1205 /**
1206 * Return duplicate of curve (if any exists) or NULL if there is no curve
1207 */
1208 SPCurve *
1209 sp_shape_get_curve (SPShape *shape)
1210 {
1211 if (shape->curve) {
1212 return shape->curve->copy();
1213 }
1214 return NULL;
1215 }
1217 /**
1218 * Same as sp_shape_set_curve but without updating the display
1219 */
1220 void
1221 sp_shape_set_curve_insync (SPShape *shape, SPCurve *curve, unsigned int owner)
1222 {
1223 if (shape->curve) {
1224 shape->curve = shape->curve->unref();
1225 }
1226 if (curve) {
1227 if (owner) {
1228 shape->curve = curve->ref();
1229 } else {
1230 shape->curve = curve->copy();
1231 }
1232 }
1233 }
1235 /**
1236 * Return all nodes in a path that are to be considered for snapping
1237 */
1238 static void sp_shape_snappoints(SPItem const *item, SnapPointsIter p)
1239 {
1240 g_assert(item != NULL);
1241 g_assert(SP_IS_SHAPE(item));
1243 SPShape const *shape = SP_SHAPE(item);
1244 if (shape->curve == NULL) {
1245 return;
1246 }
1248 NR::Matrix const i2d (from_2geom(sp_item_i2d_affine (item)));
1249 NArtBpath const *b = SP_CURVE_BPATH(shape->curve);
1251 // Cycle through the nodes in the concatenated subpaths
1252 while (b->code != NR_END) {
1253 NR::Point pos = b->c(3) * i2d; // this is the current node
1255 // NR_MOVETO Indicates the start of a closed subpath, see nr-path-code.h
1256 // If we're looking at a closed subpath, then we can skip this first
1257 // point of the subpath because it's coincident with the last point.
1258 if (b->code != NR_MOVETO) {
1259 if (b->code == NR_MOVETO_OPEN || b->code == NR_LINETO || b[1].code == NR_LINETO || b[1].code == NR_END) {
1260 // end points of a line segment are always considered for snapping
1261 *p = pos;
1262 } else {
1263 // g_assert(b->code == NR_CURVETO);
1264 NR::Point ppos, npos;
1265 ppos = b->code == NR_CURVETO ? b->c(2) * i2d : pos; // backward handle
1266 npos = b[1].code == NR_CURVETO ? b[1].c(1) * i2d : pos; // forward handle
1267 // Determine whether a node is at a smooth part of the path, by
1268 // calculating a measure for the collinearity of the handles
1269 bool c1 = fabs (Inkscape::Util::triangle_area (pos, ppos, npos)) < 1; // points are (almost) collinear
1270 bool c2 = NR::L2(pos - ppos) < 1e-6 || NR::L2(pos - npos) < 1e-6; // endnode, or a node with a retracted handle
1271 if (!(c1 & !c2)) {
1272 *p = pos; // only return non-smooth nodes ("cusps")
1273 }
1274 }
1275 }
1277 b++;
1278 }
1279 }
1281 /*
1282 Local Variables:
1283 mode:c++
1284 c-file-style:"stroustrup"
1285 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1286 indent-tabs-mode:nil
1287 fill-column:99
1288 End:
1289 */
1290 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :