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>
25 #include <sigc++/functors/ptr_fun.h>
26 #include <sigc++/adaptors/bind.h>
28 #include "macros.h"
29 #include "display/nr-arena-shape.h"
30 #include "display/curve.h"
31 #include "print.h"
32 #include "document.h"
33 #include "style.h"
34 #include "marker.h"
35 #include "sp-path.h"
36 #include "prefs-utils.h"
37 #include "attributes.h"
39 #include "live_effects/lpeobject.h"
40 #include "uri.h"
41 #include "extract-uri.h"
42 #include "uri-references.h"
43 #include "bad-uri-exception.h"
44 #include "xml/repr.h"
46 #include "util/mathfns.h" // for triangle_area()
48 #define noSHAPE_VERBOSE
50 static void sp_shape_class_init (SPShapeClass *klass);
51 static void sp_shape_init (SPShape *shape);
52 static void sp_shape_finalize (GObject *object);
54 static void sp_shape_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
55 static void sp_shape_release (SPObject *object);
57 static void sp_shape_set(SPObject *object, unsigned key, gchar const *value);
58 static void sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags);
59 static void sp_shape_modified (SPObject *object, unsigned int flags);
60 static Inkscape::XML::Node *sp_shape_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
62 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
63 void sp_shape_print (SPItem * item, SPPrintContext * ctx);
64 static NRArenaItem *sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
65 static void sp_shape_hide (SPItem *item, unsigned int key);
66 static void sp_shape_snappoints (SPItem const *item, SnapPointsIter p);
68 static void sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai);
70 static SPLPEItemClass *parent_class;
72 /**
73 * Registers the SPShape class with Gdk and returns its type number.
74 */
75 GType
76 sp_shape_get_type (void)
77 {
78 static GType type = 0;
79 if (!type) {
80 GTypeInfo info = {
81 sizeof (SPShapeClass),
82 NULL, NULL,
83 (GClassInitFunc) sp_shape_class_init,
84 NULL, NULL,
85 sizeof (SPShape),
86 16,
87 (GInstanceInitFunc) sp_shape_init,
88 NULL, /* value_table */
89 };
90 type = g_type_register_static (SP_TYPE_LPE_ITEM, "SPShape", &info, (GTypeFlags)0);
91 }
92 return type;
93 }
95 /**
96 * Initializes a SPShapeClass object. Establishes the function pointers to the class'
97 * member routines in the class vtable, and sets pointers to parent classes.
98 */
99 static void
100 sp_shape_class_init (SPShapeClass *klass)
101 {
102 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
103 SPObjectClass *sp_object_class = SP_OBJECT_CLASS(klass);
104 SPItemClass * item_class = SP_ITEM_CLASS(klass);
105 SPLPEItemClass * lpe_item_class = SP_LPE_ITEM_CLASS(klass);
107 parent_class = (SPLPEItemClass *)g_type_class_peek_parent (klass);
109 gobject_class->finalize = sp_shape_finalize;
111 sp_object_class->build = sp_shape_build;
112 sp_object_class->release = sp_shape_release;
113 sp_object_class->set = sp_shape_set;
114 sp_object_class->update = sp_shape_update;
115 sp_object_class->modified = sp_shape_modified;
116 sp_object_class->write = sp_shape_write;
118 item_class->bbox = sp_shape_bbox;
119 item_class->print = sp_shape_print;
120 item_class->show = sp_shape_show;
121 item_class->hide = sp_shape_hide;
122 item_class->snappoints = sp_shape_snappoints;
123 lpe_item_class->update_patheffect = NULL;
125 klass->set_shape = NULL;
126 }
128 /**
129 * Initializes an SPShape object.
130 */
131 static void
132 sp_shape_init (SPShape *shape)
133 {
134 for ( int i = 0 ; i < SP_MARKER_LOC_QTY ; i++ ) {
135 new (&shape->release_connect[i]) sigc::connection();
136 new (&shape->modified_connect[i]) sigc::connection();
137 }
138 }
140 static void
141 sp_shape_finalize (GObject *object)
142 {
143 SPShape *shape=(SPShape *)object;
145 for ( int i = 0 ; i < SP_MARKER_LOC_QTY ; i++ ) {
146 shape->release_connect[i].disconnect();
147 shape->release_connect[i].~connection();
148 shape->modified_connect[i].disconnect();
149 shape->modified_connect[i].~connection();
150 }
152 if (((GObjectClass *) (parent_class))->finalize) {
153 (* ((GObjectClass *) (parent_class))->finalize)(object);
154 }
155 }
157 /**
158 * Virtual build callback for SPMarker.
159 *
160 * This is to be invoked immediately after creation of an SPShape.
161 *
162 * \see sp_object_build()
163 */
164 static void
165 sp_shape_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
166 {
167 if (((SPObjectClass *) (parent_class))->build) {
168 (*((SPObjectClass *) (parent_class))->build) (object, document, repr);
169 }
170 }
172 /**
173 * Removes, releases and unrefs all children of object
174 *
175 * This is the inverse of sp_shape_build(). It must be invoked as soon
176 * as the shape is removed from the tree, even if it is still referenced
177 * by other objects. This routine also disconnects/unrefs markers and
178 * curves attached to it.
179 *
180 * \see sp_object_release()
181 */
182 static void
183 sp_shape_release (SPObject *object)
184 {
185 SPItem *item;
186 SPShape *shape;
187 SPItemView *v;
188 int i;
190 item = (SPItem *) object;
191 shape = (SPShape *) object;
193 for (i=SP_MARKER_LOC_START; i<SP_MARKER_LOC_QTY; i++) {
194 if (shape->marker[i]) {
195 sp_signal_disconnect_by_data (shape->marker[i], object);
196 for (v = item->display; v != NULL; v = v->next) {
197 sp_marker_hide ((SPMarker *) shape->marker[i], NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
198 }
199 shape->marker[i] = sp_object_hunref (shape->marker[i], object);
200 }
201 }
202 if (shape->curve) {
203 shape->curve = shape->curve->unref();
204 }
206 if (((SPObjectClass *) parent_class)->release) {
207 ((SPObjectClass *) parent_class)->release (object);
208 }
209 }
213 static void
214 sp_shape_set(SPObject *object, unsigned int key, gchar const *value)
215 {
216 if (((SPObjectClass *) parent_class)->set) {
217 ((SPObjectClass *) parent_class)->set(object, key, value);
218 }
219 }
221 static Inkscape::XML::Node *
222 sp_shape_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
223 {
224 if (((SPObjectClass *)(parent_class))->write) {
225 ((SPObjectClass *)(parent_class))->write(object, repr, flags);
226 }
228 return repr;
229 }
231 /**
232 * Updates the shape when its attributes have changed. Also establishes
233 * marker objects to match the style settings.
234 */
235 static void
236 sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags)
237 {
238 SPItem *item = (SPItem *) object;
239 SPShape *shape = (SPShape *) object;
241 if (((SPObjectClass *) (parent_class))->update) {
242 (* ((SPObjectClass *) (parent_class))->update) (object, ctx, flags);
243 }
245 /* This stanza checks that an object's marker style agrees with
246 * the marker objects it has allocated. sp_shape_set_marker ensures
247 * that the appropriate marker objects are present (or absent) to
248 * match the style.
249 */
250 /* TODO: It would be nice if this could be done at an earlier level */
251 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
252 sp_shape_set_marker (object, i, object->style->marker[i].value);
253 }
255 if (flags & (SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
256 SPStyle *style;
257 style = SP_OBJECT_STYLE (object);
258 if (style->stroke_width.unit == SP_CSS_UNIT_PERCENT) {
259 SPItemCtx *ictx = (SPItemCtx *) ctx;
260 double const aw = 1.0 / NR::expansion(ictx->i2vp);
261 style->stroke_width.computed = style->stroke_width.value * aw;
262 for (SPItemView *v = ((SPItem *) (shape))->display; v != NULL; v = v->next) {
263 nr_arena_shape_set_style ((NRArenaShape *) v->arenaitem, style);
264 }
265 }
266 }
268 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
269 /* This is suboptimal, because changing parent style schedules recalculation */
270 /* But on the other hand - how can we know that parent does not tie style and transform */
271 NR::Maybe<NR::Rect> paintbox = SP_ITEM(object)->getBounds(NR::identity());
272 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
273 NRArenaShape * const s = NR_ARENA_SHAPE(v->arenaitem);
274 if (flags & SP_OBJECT_MODIFIED_FLAG) {
275 nr_arena_shape_set_path(s, shape->curve, (flags & SP_OBJECT_USER_MODIFIED_FLAG_B));
276 }
277 if (paintbox) {
278 s->setPaintBox(*paintbox);
279 }
280 }
281 }
283 if (sp_shape_has_markers (shape)) {
285 /* Dimension marker views */
286 for (SPItemView *v = item->display; v != NULL; v = v->next) {
288 if (!v->arenaitem->key) {
289 /* Get enough keys for all, start, mid and end marker types,
290 ** and set this view's arenaitem key to the first of these keys.
291 */
292 NR_ARENA_ITEM_SET_KEY (
293 v->arenaitem,
294 sp_item_display_key_new (SP_MARKER_LOC_QTY)
295 );
296 }
298 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
299 if (shape->marker[i]) {
300 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
301 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i - SP_MARKER_LOC,
302 sp_shape_number_of_markers (shape, i));
303 }
304 }
305 }
307 /* Update marker views */
308 for (SPItemView *v = item->display; v != NULL; v = v->next) {
309 sp_shape_update_marker_view (shape, v->arenaitem);
310 }
311 }
312 }
315 /**
316 * Works out whether a marker of a given type is required at a particular
317 * point on a shape.
318 *
319 * \param shape Shape of interest.
320 * \param m Marker type (e.g. SP_MARKER_LOC_START)
321 * \param bp Path segment.
322 * \return 1 if a marker is required here, otherwise 0.
323 */
324 bool
325 sp_shape_marker_required(SPShape const *shape, int const m, NArtBpath const *bp)
326 {
327 if (shape->marker[m] == NULL) {
328 return false;
329 }
331 if (bp == SP_CURVE_BPATH(shape->curve))
332 return m == SP_MARKER_LOC_START;
333 else if (bp[1].code == NR_END)
334 return m == SP_MARKER_LOC_END;
335 else
336 return m == SP_MARKER_LOC_MID;
337 }
339 static bool
340 is_moveto(NRPathcode const c)
341 {
342 return c == NR_MOVETO || c == NR_MOVETO_OPEN;
343 }
345 /**
346 * Helper function that advances a subpath's bpath to the first subpath
347 * by checking for moveto segments.
348 *
349 * \pre The bpath[] containing bp begins with a moveto.
350 */
351 static NArtBpath const *
352 first_seg_in_subpath(NArtBpath const *bp)
353 {
354 while (!is_moveto(bp->code)) {
355 --bp;
356 }
357 return bp;
358 }
360 /**
361 * Advances the bpath to the last segment in the subpath.
362 */
363 static NArtBpath const *
364 last_seg_in_subpath(NArtBpath const *bp)
365 {
366 for(;;) {
367 ++bp;
368 switch (bp->code) {
369 case NR_MOVETO:
370 case NR_MOVETO_OPEN:
371 case NR_END:
372 --bp;
373 return bp;
375 default: continue;
376 }
377 }
378 }
381 /* A subpath begins with a moveto and ends immediately before the next moveto or NR_END.
382 * (`moveto' here means either NR_MOVETO or NR_MOVETO_OPEN.) I'm assuming that non-empty
383 * paths always begin with a moveto.
384 *
385 * The control points of the subpath are the control points of the path elements of the subpath.
386 *
387 * As usual, the control points of a moveto or NR_LINETO are {c(3)}, and
388 * the control points of a NR_CURVETO are {c(1), c(2), c(3)}.
389 * (It follows from the definition that NR_END isn't part of a subpath.)
390 *
391 * The initial control point is bpath[bi0].c(3).
392 *
393 * Reference: http://www.w3.org/TR/SVG11/painting.html#MarkerElement, the `orient' attribute.
394 * Reference for behaviour of zero-length segments:
395 * http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
396 */
398 static double const no_tangent = 128.0; /* arbitrarily-chosen value outside the range of atan2,
399 * i.e. outside of [-pi, pi]. This value is incremented by
400 * 1 and checked using > to be safe from floating-point
401 * equality comparison madness.*/
403 /**
404 * Helper function to calculate the outgoing tangent of a path
405 * ( atan2(other - p0) )
406 * \pre The bpath[] containing bp0 begins with a moveto.
407 */
408 static double
409 outgoing_tangent(NArtBpath const *bp0)
410 {
411 /* See notes in comment block above. */
413 g_assert(bp0->code != NR_END);
414 NR::Point const &p0 = bp0->c(3);
415 NR::Point other;
416 for (NArtBpath const *bp = bp0;;) {
417 ++bp;
418 switch (bp->code) {
419 case NR_LINETO:
420 other = bp->c(3);
421 if (other != p0) {
422 goto found;
423 }
424 break;
426 case NR_CURVETO:
427 for (unsigned ci = 1; ci <= 3; ++ci) {
428 other = bp->c(ci);
429 if (other != p0) {
430 goto found;
431 }
432 }
433 break;
435 case NR_MOVETO_OPEN:
436 case NR_END:
437 case NR_MOVETO:
438 bp = first_seg_in_subpath(bp0);
439 if (bp == bp0) {
440 /* Gone right around the subpath without finding any different point since the
441 * initial moveto. */
442 return no_tangent + 1;
443 }
444 if (bp->code != NR_MOVETO) {
445 /* Open subpath. */
446 return no_tangent + 1;
447 }
448 other = bp->c(3);
449 if (other != p0) {
450 goto found;
451 }
452 break;
453 }
455 if (bp == bp0) {
456 /* Back where we started, so zero-length subpath. */
457 return no_tangent + 1;
459 /* Note: this test must come after we've looked at element bp, in case bp0 is a curve:
460 * we must look at c(1) and c(2). (E.g. single-curve subpath.)
461 */
462 }
463 }
465 found:
466 return atan2( other - p0 );
467 }
469 /**
470 * Helper function to calculate the incoming tangent of a path
471 * ( atan2(p0 - other) )
472 *
473 * \pre The bpath[] containing bp0 begins with a moveto.
474 */
475 static double
476 incoming_tangent(NArtBpath const *bp0)
477 {
478 /* See notes in comment block before outgoing_tangent. */
480 g_assert(bp0->code != NR_END);
481 NR::Point const &p0 = bp0->c(3);
482 NR::Point other;
483 for (NArtBpath const *bp = bp0;;) {
484 switch (bp->code) {
485 case NR_LINETO:
486 other = bp->c(3);
487 if (other != p0) {
488 goto found;
489 }
490 --bp;
491 break;
493 case NR_CURVETO:
494 for (unsigned ci = 3; ci != 0; --ci) {
495 other = bp->c(ci);
496 if (other != p0) {
497 goto found;
498 }
499 }
500 --bp;
501 break;
503 case NR_MOVETO:
504 case NR_MOVETO_OPEN:
505 other = bp->c(3);
506 if (other != p0) {
507 goto found;
508 }
509 if (bp->code != NR_MOVETO) {
510 /* Open subpath. */
511 return no_tangent + 1;
512 }
513 bp = last_seg_in_subpath(bp0);
514 break;
516 default: /* includes NR_END */
517 g_error("Found invalid path code %u in middle of path.", bp->code);
518 return no_tangent + 1;
519 }
521 if (bp == bp0) {
522 /* Back where we started from: zero-length subpath. */
523 return no_tangent + 1;
524 }
525 }
527 found:
528 return atan2( p0 - other );
529 }
532 /**
533 * Calculate the transform required to get a marker's path object in the
534 * right place for particular path segment on a shape. You should
535 * call sp_shape_marker_required first to see if a marker is required
536 * at this point.
537 *
538 * \see sp_shape_marker_required.
539 *
540 * \param shape Shape which the marker is for.
541 * \param m Marker type (e.g. SP_MARKER_LOC_START)
542 * \param bp Path segment which the arrow is for.
543 * \return Transform matrix.
544 */
545 NR::Matrix
546 sp_shape_marker_get_transform(SPShape const *shape, NArtBpath const *bp)
547 {
548 g_return_val_if_fail(( is_moveto(SP_CURVE_BPATH(shape->curve)[0].code)
549 && ( 0 < shape->curve->_end )
550 && ( SP_CURVE_BPATH(shape->curve)[shape->curve->_end].code == NR_END ) ),
551 NR::Matrix(NR::translate(bp->c(3))));
552 double const angle1 = incoming_tangent(bp);
553 double const angle2 = outgoing_tangent(bp);
555 double ret_angle;
556 if (angle1 > no_tangent) {
557 /* First vertex of an open subpath. */
558 ret_angle = ( angle2 > no_tangent
559 ? 0.
560 : angle2 );
561 } else if (angle2 > no_tangent) {
562 /* Last vertex of an open subpath. */
563 ret_angle = angle1;
564 } else {
565 ret_angle = .5 * (angle1 + angle2);
567 if ( fabs( angle2 - angle1 ) > M_PI ) {
568 /* ret_angle is in the middle of the larger of the two sectors between angle1 and
569 * angle2, so flip it by 180degrees to force it to the middle of the smaller sector.
570 *
571 * (Imagine a circle with rays drawn at angle1 and angle2 from the centre of the
572 * circle. Those two rays divide the circle into two sectors.)
573 */
574 ret_angle += M_PI;
575 }
576 }
578 return NR::Matrix(NR::rotate(ret_angle)) * NR::translate(bp->c(3));
579 }
581 /**
582 * Updates the instances (views) of a given marker in a shape.
583 * Marker views have to be scaled already. The transformation
584 * is retrieved and then shown by calling sp_marker_show_instance.
585 */
586 static void
587 sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai)
588 {
589 SPStyle *style = ((SPObject *) shape)->style;
591 for (int i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
592 if (shape->marker[i] == NULL) {
593 continue;
594 }
596 int n = 0;
598 for (NArtBpath const *bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
599 if (sp_shape_marker_required (shape, i, bp)) {
600 NR::Matrix const m(sp_shape_marker_get_transform(shape, bp));
601 sp_marker_show_instance ((SPMarker* ) shape->marker[i], ai,
602 NR_ARENA_ITEM_GET_KEY(ai) + i, n, m,
603 style->stroke_width.computed);
604 n++;
605 }
606 }
607 }
608 }
610 /**
611 * Sets modified flag for all sub-item views.
612 */
613 static void
614 sp_shape_modified (SPObject *object, unsigned int flags)
615 {
616 SPShape *shape = SP_SHAPE (object);
618 if (((SPObjectClass *) (parent_class))->modified) {
619 (* ((SPObjectClass *) (parent_class))->modified) (object, flags);
620 }
622 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
623 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
624 nr_arena_shape_set_style (NR_ARENA_SHAPE (v->arenaitem), object->style);
625 }
626 }
627 }
629 /**
630 * Calculates the bounding box for item, storing it into bbox.
631 * This also includes the bounding boxes of any markers included in the shape.
632 */
633 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
634 {
635 SPShape const *shape = SP_SHAPE (item);
637 if (shape->curve) {
639 NRRect cbbox;
640 const_NRBPath bp;
642 bp.path = SP_CURVE_BPATH (shape->curve);
644 cbbox.x0 = cbbox.y0 = NR_HUGE;
645 cbbox.x1 = cbbox.y1 = -NR_HUGE;
647 nr_path_matrix_bbox_union(&bp, transform, &cbbox);
649 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
651 SPStyle* style=SP_OBJECT_STYLE (item);
652 if (!style->stroke.isNone()) {
653 double const scale = expansion(transform);
654 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
655 double const width = MAX(0.125, style->stroke_width.computed * scale);
656 if ( fabs(cbbox.x1-cbbox.x0) > -0.00001 && fabs(cbbox.y1-cbbox.y0) > -0.00001 ) {
657 cbbox.x0-=0.5*width;
658 cbbox.x1+=0.5*width;
659 cbbox.y0-=0.5*width;
660 cbbox.y1+=0.5*width;
661 }
662 }
663 }
665 // Union with bboxes of the markers, if any
666 if (sp_shape_has_markers (shape)) {
667 for (NArtBpath const* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
668 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
669 if (sp_shape_marker_required (shape, m, bp)) {
671 SPMarker* marker = SP_MARKER (shape->marker[m]);
672 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
674 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
676 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
677 tr = NR::scale(style->stroke_width.computed) * tr;
678 }
680 // total marker transform
681 tr = marker_item->transform * marker->c2p * tr * transform;
683 // get bbox of the marker with that transform
684 NRRect marker_bbox;
685 sp_item_invoke_bbox (marker_item, &marker_bbox, tr, true);
686 // union it with the shape bbox
687 nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
688 }
689 }
690 }
691 }
692 }
694 // copy our bbox to the variable we're given
695 *bbox = cbbox;
696 }
697 }
699 /**
700 * Prepares shape for printing. Handles printing of comments for printing
701 * debugging, sizes the item to fit into the document width/height,
702 * applies print fill/stroke, sets transforms for markers, and adds
703 * comment labels.
704 */
705 void
706 sp_shape_print (SPItem *item, SPPrintContext *ctx)
707 {
708 NRRect pbox, dbox, bbox;
710 SPShape *shape = SP_SHAPE(item);
712 if (!shape->curve) return;
714 gint add_comments = prefs_get_int_attribute_limited ("printing.debug", "add-label-comments", 0, 0, 1);
715 if (add_comments) {
716 gchar * comment = g_strdup_printf("begin '%s'",
717 SP_OBJECT(item)->defaultLabel());
718 sp_print_comment(ctx, comment);
719 g_free(comment);
720 }
722 /* fixme: Think (Lauris) */
723 sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
724 dbox.x0 = 0.0;
725 dbox.y0 = 0.0;
726 dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
727 dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
728 sp_item_bbox_desktop (item, &bbox);
729 NR::Matrix const i2d = sp_item_i2d_affine(item);
731 SPStyle* style = SP_OBJECT_STYLE (item);
733 if (!style->fill.isNone()) {
734 const_NRBPath bp;
735 bp.path = SP_CURVE_BPATH(shape->curve);
736 sp_print_fill (ctx, &bp, &i2d, style, &pbox, &dbox, &bbox);
737 }
739 if (!style->stroke.isNone()) {
740 const_NRBPath bp;
741 bp.path = SP_CURVE_BPATH(shape->curve);
742 sp_print_stroke (ctx, &bp, &i2d, style, &pbox, &dbox, &bbox);
743 }
745 for (NArtBpath const* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
746 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
747 if (sp_shape_marker_required (shape, m, bp)) {
749 SPMarker* marker = SP_MARKER (shape->marker[m]);
750 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
752 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
754 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
755 tr = NR::scale(style->stroke_width.computed) * tr;
756 }
758 tr = marker_item->transform * marker->c2p * tr;
760 NR::Matrix old_tr = marker_item->transform;
761 marker_item->transform = tr;
762 sp_item_invoke_print (marker_item, ctx);
763 marker_item->transform = old_tr;
764 }
765 }
766 }
768 if (add_comments) {
769 gchar * comment = g_strdup_printf("end '%s'",
770 SP_OBJECT(item)->defaultLabel());
771 sp_print_comment(ctx, comment);
772 g_free(comment);
773 }
774 }
776 /**
777 * Sets style, path, and paintbox. Updates marker views, including dimensions.
778 */
779 static NRArenaItem *
780 sp_shape_show (SPItem *item, NRArena *arena, unsigned int /*key*/, unsigned int /*flags*/)
781 {
782 SPObject *object = SP_OBJECT(item);
783 SPShape *shape = SP_SHAPE(item);
785 NRArenaItem *arenaitem = NRArenaShape::create(arena);
786 NRArenaShape * const s = NR_ARENA_SHAPE(arenaitem);
787 nr_arena_shape_set_style(s, object->style);
788 nr_arena_shape_set_path(s, shape->curve, false);
789 NR::Maybe<NR::Rect> paintbox = item->getBounds(NR::identity());
790 if (paintbox) {
791 s->setPaintBox(*paintbox);
792 }
794 if (sp_shape_has_markers (shape)) {
796 /* Dimension the marker views */
797 if (!arenaitem->key) {
798 NR_ARENA_ITEM_SET_KEY (arenaitem, sp_item_display_key_new (SP_MARKER_LOC_QTY));
799 }
801 for (int i = 0; i < SP_MARKER_LOC_QTY; i++) {
802 if (shape->marker[i]) {
803 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
804 NR_ARENA_ITEM_GET_KEY (arenaitem) + i - SP_MARKER_LOC,
805 sp_shape_number_of_markers (shape, i));
806 }
807 }
810 /* Update marker views */
811 sp_shape_update_marker_view (shape, arenaitem);
812 }
814 return arenaitem;
815 }
817 /**
818 * Hides/removes marker views from the shape.
819 */
820 static void
821 sp_shape_hide (SPItem *item, unsigned int key)
822 {
823 SPShape *shape;
824 SPItemView *v;
825 int i;
827 shape = (SPShape *) item;
829 for (i=0; i<SP_MARKER_LOC_QTY; i++) {
830 if (shape->marker[i]) {
831 for (v = item->display; v != NULL; v = v->next) {
832 if (key == v->key) {
833 sp_marker_hide ((SPMarker *) shape->marker[i],
834 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
835 }
836 }
837 }
838 }
840 if (((SPItemClass *) parent_class)->hide) {
841 ((SPItemClass *) parent_class)->hide (item, key);
842 }
843 }
845 /**
846 * \param shape Shape.
847 * \return TRUE if the shape has any markers, or FALSE if not.
848 */
849 int
850 sp_shape_has_markers (SPShape const *shape)
851 {
852 /* Note, we're ignoring 'marker' settings, which technically should apply for
853 all three settings. This should be fixed later such that if 'marker' is
854 specified, then all three should appear. */
856 return (
857 shape->curve &&
858 (shape->marker[SP_MARKER_LOC_START] ||
859 shape->marker[SP_MARKER_LOC_MID] ||
860 shape->marker[SP_MARKER_LOC_END])
861 );
862 }
865 /**
866 * \param shape Shape.
867 * \param type Marker type (e.g. SP_MARKER_LOC_START)
868 * \return Number of markers that the shape has of this type.
869 */
870 int
871 sp_shape_number_of_markers (SPShape *shape, int type)
872 {
873 int n = 0;
874 for (NArtBpath const* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
875 if (sp_shape_marker_required (shape, type, bp)) {
876 n++;
877 }
878 }
880 return n;
881 }
883 /**
884 * Checks if the given marker is used in the shape, and if so, it
885 * releases it by calling sp_marker_hide. Also detaches signals
886 * and unrefs the marker from the shape.
887 */
888 static void
889 sp_shape_marker_release (SPObject *marker, SPShape *shape)
890 {
891 SPItem *item;
892 int i;
894 item = (SPItem *) shape;
896 for (i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
897 if (marker == shape->marker[i]) {
898 SPItemView *v;
899 /* Hide marker */
900 for (v = item->display; v != NULL; v = v->next) {
901 sp_marker_hide ((SPMarker *) (shape->marker[i]), NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
902 /* fixme: Do we need explicit remove here? (Lauris) */
903 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
904 }
905 /* Detach marker */
906 sp_signal_disconnect_by_data (shape->marker[i], item);
907 shape->marker[i] = sp_object_hunref (shape->marker[i], item);
908 }
909 }
910 }
912 /**
913 * No-op. Exists for handling 'modified' messages
914 */
915 static void
916 sp_shape_marker_modified (SPObject */*marker*/, guint /*flags*/, SPItem */*item*/)
917 {
918 /* I think mask does update automagically */
919 /* g_warning ("Item %s mask %s modified", SP_OBJECT_ID (item), SP_OBJECT_ID (mask)); */
920 }
922 /**
923 * Adds a new marker to shape object at the location indicated by key. value
924 * must be a valid URI reference resolvable from the shape object (i.e., present
925 * in the document <defs>). If the shape object already has a marker
926 * registered at the given position, it is removed first. Then the
927 * new marker is hrefed and its signals connected.
928 */
929 void
930 sp_shape_set_marker (SPObject *object, unsigned int key, const gchar *value)
931 {
932 SPItem *item = (SPItem *) object;
933 SPShape *shape = (SPShape *) object;
935 if (key < SP_MARKER_LOC_START || key > SP_MARKER_LOC_END) {
936 return;
937 }
939 SPObject *mrk = sp_css_uri_reference_resolve (SP_OBJECT_DOCUMENT (object), value);
940 if (mrk != shape->marker[key]) {
941 if (shape->marker[key]) {
942 SPItemView *v;
944 /* Detach marker */
945 shape->release_connect[key].disconnect();
946 shape->modified_connect[key].disconnect();
948 /* Hide marker */
949 for (v = item->display; v != NULL; v = v->next) {
950 sp_marker_hide ((SPMarker *) (shape->marker[key]),
951 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + key);
952 /* fixme: Do we need explicit remove here? (Lauris) */
953 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
954 }
956 /* Unref marker */
957 shape->marker[key] = sp_object_hunref (shape->marker[key], object);
958 }
959 if (SP_IS_MARKER (mrk)) {
960 shape->marker[key] = sp_object_href (mrk, object);
961 shape->release_connect[key] = mrk->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_shape_marker_release), shape));
962 shape->modified_connect[key] = mrk->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_shape_marker_modified), shape));
963 }
964 }
965 }
969 /* Shape section */
971 /**
972 * Calls any registered handlers for the set_shape action
973 */
974 void
975 sp_shape_set_shape (SPShape *shape)
976 {
977 g_return_if_fail (shape != NULL);
978 g_return_if_fail (SP_IS_SHAPE (shape));
980 if (SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape) {
981 SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape (shape);
982 }
983 }
985 /**
986 * Adds a curve to the shape. If owner is specified, a reference
987 * will be made, otherwise the curve will be copied into the shape.
988 * Any existing curve in the shape will be unreferenced first.
989 * This routine also triggers a request to update the display.
990 */
991 void
992 sp_shape_set_curve (SPShape *shape, SPCurve *curve, unsigned int owner)
993 {
994 if (shape->curve) {
995 shape->curve = shape->curve->unref();
996 }
997 if (curve) {
998 if (owner) {
999 shape->curve = curve->ref();
1000 } else {
1001 shape->curve = curve->copy();
1002 }
1003 }
1004 SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1005 }
1007 /**
1008 * Return duplicate of curve (if any exists) or NULL if there is no curve
1009 */
1010 SPCurve *
1011 sp_shape_get_curve (SPShape *shape)
1012 {
1013 if (shape->curve) {
1014 return shape->curve->copy();
1015 }
1016 return NULL;
1017 }
1019 /**
1020 * Same as sp_shape_set_curve but without updating the display
1021 */
1022 void
1023 sp_shape_set_curve_insync (SPShape *shape, SPCurve *curve, unsigned int owner)
1024 {
1025 if (shape->curve) {
1026 shape->curve = shape->curve->unref();
1027 }
1028 if (curve) {
1029 if (owner) {
1030 shape->curve = curve->ref();
1031 } else {
1032 shape->curve = curve->copy();
1033 }
1034 }
1035 }
1037 /**
1038 * Return all nodes in a path that are to be considered for snapping
1039 */
1040 static void sp_shape_snappoints(SPItem const *item, SnapPointsIter p)
1041 {
1042 g_assert(item != NULL);
1043 g_assert(SP_IS_SHAPE(item));
1045 SPShape const *shape = SP_SHAPE(item);
1046 if (shape->curve == NULL) {
1047 return;
1048 }
1050 NR::Matrix const i2d (sp_item_i2d_affine (item));
1051 NArtBpath const *b = SP_CURVE_BPATH(shape->curve);
1053 // Cycle through the nodes in the concatenated subpaths
1054 while (b->code != NR_END) {
1055 NR::Point pos = b->c(3) * i2d; // this is the current node
1057 // NR_MOVETO Indicates the start of a closed subpath, see nr-path-code.h
1058 // If we're looking at a closed subpath, then we can skip this first
1059 // point of the subpath because it's coincident with the last point.
1060 if (b->code != NR_MOVETO) {
1061 if (b->code == NR_MOVETO_OPEN || b->code == NR_LINETO || b[1].code == NR_LINETO || b[1].code == NR_END) {
1062 // end points of a line segment are always considered for snapping
1063 *p = pos;
1064 } else {
1065 // g_assert(b->code == NR_CURVETO);
1066 NR::Point ppos, npos;
1067 ppos = b->code == NR_CURVETO ? b->c(2) * i2d : pos; // backward handle
1068 npos = b[1].code == NR_CURVETO ? b[1].c(1) * i2d : pos; // forward handle
1069 // Determine whether a node is at a smooth part of the path, by
1070 // calculating a measure for the collinearity of the handles
1071 bool c1 = fabs (Inkscape::Util::triangle_area (pos, ppos, npos)) < 1; // points are (almost) collinear
1072 bool c2 = NR::L2(pos - ppos) < 1e-6 || NR::L2(pos - npos) < 1e-6; // endnode, or a node with a retracted handle
1073 if (!(c1 & !c2)) {
1074 *p = pos; // only return non-smooth nodes ("cusps")
1075 }
1076 }
1077 }
1079 b++;
1080 }
1081 }
1083 /*
1084 Local Variables:
1085 mode:c++
1086 c-file-style:"stroustrup"
1087 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1088 indent-tabs-mode:nil
1089 fill-column:99
1090 End:
1091 */
1092 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :