1 #define __SP_SHAPE_C__
3 /*
4 * Base class for shapes, including <path> element
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 1999-2002 Lauris Kaplinski
10 * Copyright (C) 2000-2001 Ximian, Inc.
11 * Copyright (C) 2004 John Cliff
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
21 #include <libnr/n-art-bpath.h>
22 #include <libnr/nr-matrix-fns.h>
23 #include <libnr/nr-matrix-ops.h>
24 #include <libnr/nr-matrix-translate-ops.h>
25 #include <libnr/nr-scale-matrix-ops.h>
27 #include <sigc++/functors/ptr_fun.h>
28 #include <sigc++/adaptors/bind.h>
30 #include "macros.h"
31 #include "display/nr-arena-shape.h"
32 #include "print.h"
33 #include "document.h"
34 #include "style.h"
35 #include "marker.h"
36 #include "sp-path.h"
37 #include "prefs-utils.h"
39 #define noSHAPE_VERBOSE
41 static void sp_shape_class_init (SPShapeClass *klass);
42 static void sp_shape_init (SPShape *shape);
43 static void sp_shape_finalize (GObject *object);
45 static void sp_shape_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
46 static void sp_shape_release (SPObject *object);
48 static void sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags);
49 static void sp_shape_modified (SPObject *object, unsigned int flags);
51 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
52 void sp_shape_print (SPItem * item, SPPrintContext * ctx);
53 static NRArenaItem *sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
54 static void sp_shape_hide (SPItem *item, unsigned int key);
55 static void sp_shape_snappoints (SPItem const *item, SnapPointsIter p);
57 static void sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai);
59 static SPItemClass *parent_class;
61 /**
62 * Registers the SPShape class with Gdk and returns its type number.
63 */
64 GType
65 sp_shape_get_type (void)
66 {
67 static GType type = 0;
68 if (!type) {
69 GTypeInfo info = {
70 sizeof (SPShapeClass),
71 NULL, NULL,
72 (GClassInitFunc) sp_shape_class_init,
73 NULL, NULL,
74 sizeof (SPShape),
75 16,
76 (GInstanceInitFunc) sp_shape_init,
77 NULL, /* value_table */
78 };
79 type = g_type_register_static (SP_TYPE_ITEM, "SPShape", &info, (GTypeFlags)0);
80 }
81 return type;
82 }
84 /**
85 * Initializes a SPShapeClass object. Establishes the function pointers to the class'
86 * member routines in the class vtable, and sets pointers to parent classes.
87 */
88 static void
89 sp_shape_class_init (SPShapeClass *klass)
90 {
91 GObjectClass *gobject_class;
92 SPObjectClass *sp_object_class;
93 SPItemClass * item_class;
94 SPPathClass * path_class;
96 gobject_class = (GObjectClass *) klass;
97 sp_object_class = (SPObjectClass *) klass;
98 item_class = (SPItemClass *) klass;
99 path_class = (SPPathClass *) klass;
101 parent_class = (SPItemClass *)g_type_class_peek_parent (klass);
103 gobject_class->finalize = sp_shape_finalize;
105 sp_object_class->build = sp_shape_build;
106 sp_object_class->release = sp_shape_release;
107 sp_object_class->update = sp_shape_update;
108 sp_object_class->modified = sp_shape_modified;
110 item_class->bbox = sp_shape_bbox;
111 item_class->print = sp_shape_print;
112 item_class->show = sp_shape_show;
113 item_class->hide = sp_shape_hide;
114 item_class->snappoints = sp_shape_snappoints;
115 }
117 /**
118 * Initializes an SPShape object.
119 */
120 static void
121 sp_shape_init (SPShape *shape)
122 {
123 for ( int i = 0 ; i < SP_MARKER_LOC_QTY ; i++ ) {
124 new (&shape->release_connect[i]) sigc::connection();
125 new (&shape->modified_connect[i]) sigc::connection();
126 }
127 }
129 static void
130 sp_shape_finalize (GObject *object)
131 {
132 SPShape *shape=(SPShape *)object;
134 for ( int i = 0 ; i < SP_MARKER_LOC_QTY ; i++ ) {
135 shape->release_connect[i].disconnect();
136 shape->release_connect[i].~connection();
137 shape->modified_connect[i].disconnect();
138 shape->modified_connect[i].~connection();
139 }
141 if (((GObjectClass *) (parent_class))->finalize) {
142 (* ((GObjectClass *) (parent_class))->finalize)(object);
143 }
144 }
146 /**
147 * Virtual build callback for SPMarker.
148 *
149 * This is to be invoked immediately after creation of an SPShape. This is
150 * just a stub.
151 *
152 * \see sp_object_build()
153 */
154 static void
155 sp_shape_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
156 {
157 if (((SPObjectClass *) (parent_class))->build) {
158 (*((SPObjectClass *) (parent_class))->build) (object, document, repr);
159 }
160 }
162 /**
163 * Removes, releases and unrefs all children of object
164 *
165 * This is the inverse of sp_shape_build(). It must be invoked as soon
166 * as the shape is removed from the tree, even if it is still referenced
167 * by other objects. This routine also disconnects/unrefs markers and
168 * curves attached to it.
169 *
170 * \see sp_object_release()
171 */
172 static void
173 sp_shape_release (SPObject *object)
174 {
175 SPItem *item;
176 SPShape *shape;
177 SPItemView *v;
178 int i;
180 item = (SPItem *) object;
181 shape = (SPShape *) object;
183 for (i=SP_MARKER_LOC_START; i<SP_MARKER_LOC_QTY; i++) {
184 if (shape->marker[i]) {
185 sp_signal_disconnect_by_data (shape->marker[i], object);
186 for (v = item->display; v != NULL; v = v->next) {
187 sp_marker_hide ((SPMarker *) shape->marker[i], NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
188 }
189 shape->marker[i] = sp_object_hunref (shape->marker[i], object);
190 }
191 }
192 if (shape->curve) {
193 shape->curve = sp_curve_unref (shape->curve);
194 }
196 if (((SPObjectClass *) parent_class)->release) {
197 ((SPObjectClass *) parent_class)->release (object);
198 }
199 }
201 /**
202 * Updates the shape when its attributes have changed. Also establishes
203 * marker objects to match the style settings.
204 */
205 static void
206 sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags)
207 {
208 SPItem *item = (SPItem *) object;
209 SPShape *shape = (SPShape *) object;
211 if (((SPObjectClass *) (parent_class))->update) {
212 (* ((SPObjectClass *) (parent_class))->update) (object, ctx, flags);
213 }
215 /* This stanza checks that an object's marker style agrees with
216 * the marker objects it has allocated. sp_shape_set_marker ensures
217 * that the appropriate marker objects are present (or absent) to
218 * match the style.
219 */
220 /* TODO: It would be nice if this could be done at an earlier level */
221 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
222 sp_shape_set_marker (object, i, object->style->marker[i].value);
223 }
225 if (flags & (SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
226 SPStyle *style;
227 style = SP_OBJECT_STYLE (object);
228 if (style->stroke_width.unit == SP_CSS_UNIT_PERCENT) {
229 SPItemCtx *ictx = (SPItemCtx *) ctx;
230 double const aw = 1.0 / NR::expansion(ictx->i2vp);
231 style->stroke_width.computed = style->stroke_width.value * aw;
232 for (SPItemView *v = ((SPItem *) (shape))->display; v != NULL; v = v->next) {
233 nr_arena_shape_set_style ((NRArenaShape *) v->arenaitem, style);
234 }
235 }
236 }
238 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
239 /* This is suboptimal, because changing parent style schedules recalculation */
240 /* But on the other hand - how can we know that parent does not tie style and transform */
241 NR::Maybe<NR::Rect> paintbox = SP_ITEM(object)->getBounds(NR::identity());
242 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
243 NRArenaShape * const s = NR_ARENA_SHAPE(v->arenaitem);
244 if (flags & SP_OBJECT_MODIFIED_FLAG) {
245 nr_arena_shape_set_path(s, shape->curve, (flags & SP_OBJECT_USER_MODIFIED_FLAG_B));
246 }
247 if (paintbox) {
248 s->setPaintBox(*paintbox);
249 }
250 }
251 }
253 if (sp_shape_has_markers (shape)) {
255 /* Dimension marker views */
256 for (SPItemView *v = item->display; v != NULL; v = v->next) {
258 if (!v->arenaitem->key) {
259 /* Get enough keys for all, start, mid and end marker types,
260 ** and set this view's arenaitem key to the first of these keys.
261 */
262 NR_ARENA_ITEM_SET_KEY (
263 v->arenaitem,
264 sp_item_display_key_new (SP_MARKER_LOC_QTY)
265 );
266 }
268 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
269 if (shape->marker[i]) {
270 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
271 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i - SP_MARKER_LOC,
272 sp_shape_number_of_markers (shape, i));
273 }
274 }
275 }
277 /* Update marker views */
278 for (SPItemView *v = item->display; v != NULL; v = v->next) {
279 sp_shape_update_marker_view (shape, v->arenaitem);
280 }
281 }
282 }
285 /**
286 * Works out whether a marker of a given type is required at a particular
287 * point on a shape.
288 *
289 * \param shape Shape of interest.
290 * \param m Marker type (e.g. SP_MARKER_LOC_START)
291 * \param bp Path segment.
292 * \return 1 if a marker is required here, otherwise 0.
293 */
294 bool
295 sp_shape_marker_required(SPShape const *shape, int const m, NArtBpath *bp)
296 {
297 if (shape->marker[m] == NULL) {
298 return false;
299 }
301 if (bp == SP_CURVE_BPATH(shape->curve))
302 return m == SP_MARKER_LOC_START;
303 else if (bp[1].code == NR_END)
304 return m == SP_MARKER_LOC_END;
305 else
306 return m == SP_MARKER_LOC_MID;
307 }
309 static bool
310 is_moveto(NRPathcode const c)
311 {
312 return c == NR_MOVETO || c == NR_MOVETO_OPEN;
313 }
315 /**
316 * Helper function that advances a subpath's bpath to the first subpath
317 * by checking for moveto segments.
318 *
319 * \pre The bpath[] containing bp begins with a moveto.
320 */
321 static NArtBpath const *
322 first_seg_in_subpath(NArtBpath const *bp)
323 {
324 while (!is_moveto(bp->code)) {
325 --bp;
326 }
327 return bp;
328 }
330 /**
331 * Advances the bpath to the last segment in the subpath.
332 */
333 static NArtBpath const *
334 last_seg_in_subpath(NArtBpath const *bp)
335 {
336 for(;;) {
337 ++bp;
338 switch (bp->code) {
339 case NR_MOVETO:
340 case NR_MOVETO_OPEN:
341 case NR_END:
342 --bp;
343 return bp;
345 default: continue;
346 }
347 }
348 }
351 /* A subpath begins with a moveto and ends immediately before the next moveto or NR_END.
352 * (`moveto' here means either NR_MOVETO or NR_MOVETO_OPEN.) I'm assuming that non-empty
353 * paths always begin with a moveto.
354 *
355 * The control points of the subpath are the control points of the path elements of the subpath.
356 *
357 * As usual, the control points of a moveto or NR_LINETO are {c(3)}, and
358 * the control points of a NR_CURVETO are {c(1), c(2), c(3)}.
359 * (It follows from the definition that NR_END isn't part of a subpath.)
360 *
361 * The initial control point is bpath[bi0].c(3).
362 *
363 * Reference: http://www.w3.org/TR/SVG11/painting.html#MarkerElement, the `orient' attribute.
364 * Reference for behaviour of zero-length segments:
365 * http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
366 */
368 static double const no_tangent = 128.0; /* arbitrarily-chosen value outside the range of atan2, i.e. outside of [-pi, pi]. */
370 /**
371 * Helper function to calculate the outgoing tangent of a path
372 * ( atan2(other - p0) )
373 * \pre The bpath[] containing bp0 begins with a moveto.
374 */
375 static double
376 outgoing_tangent(NArtBpath const *bp0)
377 {
378 /* See notes in comment block above. */
380 g_assert(bp0->code != NR_END);
381 NR::Point const &p0 = bp0->c(3);
382 NR::Point other;
383 for (NArtBpath const *bp = bp0;;) {
384 ++bp;
385 switch (bp->code) {
386 case NR_LINETO:
387 other = bp->c(3);
388 if (other != p0) {
389 goto found;
390 }
391 break;
393 case NR_CURVETO:
394 for (unsigned ci = 1; ci <= 3; ++ci) {
395 other = bp->c(ci);
396 if (other != p0) {
397 goto found;
398 }
399 }
400 break;
402 case NR_MOVETO_OPEN:
403 case NR_END:
404 case NR_MOVETO:
405 bp = first_seg_in_subpath(bp0);
406 if (bp == bp0) {
407 /* Gone right around the subpath without finding any different point since the
408 * initial moveto. */
409 return no_tangent;
410 }
411 if (bp->code != NR_MOVETO) {
412 /* Open subpath. */
413 return no_tangent;
414 }
415 other = bp->c(3);
416 if (other != p0) {
417 goto found;
418 }
419 break;
420 }
422 if (bp == bp0) {
423 /* Back where we started, so zero-length subpath. */
424 return no_tangent;
426 /* Note: this test must come after we've looked at element bp, in case bp0 is a curve:
427 * we must look at c(1) and c(2). (E.g. single-curve subpath.)
428 */
429 }
430 }
432 found:
433 return atan2( other - p0 );
434 }
436 /**
437 * Helper function to calculate the incoming tangent of a path
438 * ( atan2(p0 - other) )
439 *
440 * \pre The bpath[] containing bp0 begins with a moveto.
441 */
442 static double
443 incoming_tangent(NArtBpath const *bp0)
444 {
445 /* See notes in comment block before outgoing_tangent. */
447 g_assert(bp0->code != NR_END);
448 NR::Point const &p0 = bp0->c(3);
449 NR::Point other;
450 for (NArtBpath const *bp = bp0;;) {
451 switch (bp->code) {
452 case NR_LINETO:
453 other = bp->c(3);
454 if (other != p0) {
455 goto found;
456 }
457 --bp;
458 break;
460 case NR_CURVETO:
461 for (unsigned ci = 3; ci != 0; --ci) {
462 other = bp->c(ci);
463 if (other != p0) {
464 goto found;
465 }
466 }
467 --bp;
468 break;
470 case NR_MOVETO:
471 case NR_MOVETO_OPEN:
472 other = bp->c(3);
473 if (other != p0) {
474 goto found;
475 }
476 if (bp->code != NR_MOVETO) {
477 /* Open subpath. */
478 return no_tangent;
479 }
480 bp = last_seg_in_subpath(bp0);
481 break;
483 default: /* includes NR_END */
484 g_error("Found invalid path code %u in middle of path.", bp->code);
485 return no_tangent;
486 }
488 if (bp == bp0) {
489 /* Back where we started from: zero-length subpath. */
490 return no_tangent;
491 }
492 }
494 found:
495 return atan2( p0 - other );
496 }
499 /**
500 * Calculate the transform required to get a marker's path object in the
501 * right place for particular path segment on a shape. You should
502 * call sp_shape_marker_required first to see if a marker is required
503 * at this point.
504 *
505 * \see sp_shape_marker_required.
506 *
507 * \param shape Shape which the marker is for.
508 * \param m Marker type (e.g. SP_MARKER_LOC_START)
509 * \param bp Path segment which the arrow is for.
510 * \return Transform matrix.
511 */
512 NR::Matrix
513 sp_shape_marker_get_transform(SPShape const *shape, NArtBpath const *bp)
514 {
515 g_return_val_if_fail(( is_moveto(SP_CURVE_BPATH(shape->curve)[0].code)
516 && ( 0 < shape->curve->end )
517 && ( SP_CURVE_BPATH(shape->curve)[shape->curve->end].code == NR_END ) ),
518 NR::Matrix(NR::translate(bp->c(3))));
519 double const angle1 = incoming_tangent(bp);
520 double const angle2 = outgoing_tangent(bp);
522 /* angle1 and angle2 are now each either unset (i.e. still 100 from their initialization) or in
523 [-pi, pi] from atan2. */
524 g_assert((-3.15 < angle1 && angle1 < 3.15) || (angle1 == no_tangent));
525 g_assert((-3.15 < angle2 && angle2 < 3.15) || (angle2 == no_tangent));
527 double ret_angle;
528 if (angle1 == no_tangent) {
529 /* First vertex of an open subpath. */
530 ret_angle = ( angle2 == no_tangent
531 ? 0.
532 : angle2 );
533 } else if (angle2 == no_tangent) {
534 /* Last vertex of an open subpath. */
535 ret_angle = angle1;
536 } else {
537 ret_angle = .5 * (angle1 + angle2);
539 if ( fabs( angle2 - angle1 ) > M_PI ) {
540 /* ret_angle is in the middle of the larger of the two sectors between angle1 and
541 * angle2, so flip it by 180degrees to force it to the middle of the smaller sector.
542 *
543 * (Imagine a circle with rays drawn at angle1 and angle2 from the centre of the
544 * circle. Those two rays divide the circle into two sectors.)
545 */
546 ret_angle += M_PI;
547 }
548 }
550 return NR::Matrix(NR::rotate(ret_angle)) * NR::translate(bp->c(3));
551 }
553 /**
554 * Updates the instances (views) of a given marker in a shape.
555 * Marker views have to be scaled already. The transformation
556 * is retrieved and then shown by calling sp_marker_show_instance.
557 */
558 static void
559 sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai)
560 {
561 SPStyle *style = ((SPObject *) shape)->style;
563 for (int i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
564 if (shape->marker[i] == NULL) {
565 continue;
566 }
568 int n = 0;
570 for (NArtBpath *bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
571 if (sp_shape_marker_required (shape, i, bp)) {
572 NR::Matrix const m(sp_shape_marker_get_transform(shape, bp));
573 sp_marker_show_instance ((SPMarker* ) shape->marker[i], ai,
574 NR_ARENA_ITEM_GET_KEY(ai) + i, n, m,
575 style->stroke_width.computed);
576 n++;
577 }
578 }
579 }
580 }
582 /**
583 * Sets modified flag for all sub-item views.
584 */
585 static void
586 sp_shape_modified (SPObject *object, unsigned int flags)
587 {
588 SPShape *shape = SP_SHAPE (object);
590 if (((SPObjectClass *) (parent_class))->modified) {
591 (* ((SPObjectClass *) (parent_class))->modified) (object, flags);
592 }
594 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
595 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
596 nr_arena_shape_set_style (NR_ARENA_SHAPE (v->arenaitem), object->style);
597 }
598 }
599 }
601 /**
602 * Calculates the bounding box for item, storing it into bbox.
603 * This also includes the bounding boxes of any markers included in the shape.
604 */
605 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
606 {
607 SPShape const *shape = SP_SHAPE (item);
609 if (shape->curve) {
611 NRRect cbbox;
612 NRBPath bp;
614 bp.path = SP_CURVE_BPATH (shape->curve);
616 cbbox.x0 = cbbox.y0 = NR_HUGE;
617 cbbox.x1 = cbbox.y1 = -NR_HUGE;
619 nr_path_matrix_bbox_union(&bp, transform, &cbbox);
621 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
623 SPStyle* style=SP_OBJECT_STYLE (item);
624 if (style->stroke.type != SP_PAINT_TYPE_NONE) {
625 double const scale = expansion(transform);
626 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
627 double const width = MAX(0.125, style->stroke_width.computed * scale);
628 if ( fabs(cbbox.x1-cbbox.x0) > -0.00001 && fabs(cbbox.y1-cbbox.y0) > -0.00001 ) {
629 cbbox.x0-=0.5*width;
630 cbbox.x1+=0.5*width;
631 cbbox.y0-=0.5*width;
632 cbbox.y1+=0.5*width;
633 }
634 }
635 }
637 // Union with bboxes of the markers, if any
638 if (sp_shape_has_markers (shape)) {
639 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
640 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
641 if (sp_shape_marker_required (shape, m, bp)) {
643 SPMarker* marker = SP_MARKER (shape->marker[m]);
644 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
646 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
648 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
649 tr = NR::scale(style->stroke_width.computed) * tr;
650 }
652 // total marker transform
653 tr = marker_item->transform * marker->c2p * tr * transform;
655 // get bbox of the marker with that transform
656 NRRect marker_bbox;
657 sp_item_invoke_bbox (marker_item, &marker_bbox, tr, true);
658 // union it with the shape bbox
659 nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
660 }
661 }
662 }
663 }
664 }
666 // copy our bbox to the variable we're given
667 *bbox = cbbox;
668 }
669 }
671 /**
672 * Prepares shape for printing. Handles printing of comments for printing
673 * debugging, sizes the item to fit into the document width/height,
674 * applies print fill/stroke, sets transforms for markers, and adds
675 * comment labels.
676 */
677 void
678 sp_shape_print (SPItem *item, SPPrintContext *ctx)
679 {
680 NRRect pbox, dbox, bbox;
682 SPShape *shape = SP_SHAPE(item);
684 if (!shape->curve) return;
686 gint add_comments = prefs_get_int_attribute_limited ("printing.debug", "add-label-comments", 0, 0, 1);
687 if (add_comments) {
688 gchar * comment = g_strdup_printf("begin '%s'",
689 SP_OBJECT(item)->defaultLabel());
690 sp_print_comment(ctx, comment);
691 g_free(comment);
692 }
694 /* fixme: Think (Lauris) */
695 sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
696 dbox.x0 = 0.0;
697 dbox.y0 = 0.0;
698 dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
699 dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
700 sp_item_bbox_desktop (item, &bbox);
701 NR::Matrix const i2d = sp_item_i2d_affine(item);
703 SPStyle* style = SP_OBJECT_STYLE (item);
705 if (style->fill.type != SP_PAINT_TYPE_NONE) {
706 NRBPath bp;
707 bp.path = SP_CURVE_BPATH(shape->curve);
708 sp_print_fill (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
709 }
711 if (style->stroke.type != SP_PAINT_TYPE_NONE) {
712 NRBPath bp;
713 bp.path = SP_CURVE_BPATH(shape->curve);
714 sp_print_stroke (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
715 }
717 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
718 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
719 if (sp_shape_marker_required (shape, m, bp)) {
721 SPMarker* marker = SP_MARKER (shape->marker[m]);
722 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
724 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
726 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
727 tr = NR::scale(style->stroke_width.computed) * tr;
728 }
730 tr = marker_item->transform * marker->c2p * tr;
732 NR::Matrix old_tr = marker_item->transform;
733 marker_item->transform = tr;
734 sp_item_invoke_print (marker_item, ctx);
735 marker_item->transform = old_tr;
736 }
737 }
738 }
740 if (add_comments) {
741 gchar * comment = g_strdup_printf("end '%s'",
742 SP_OBJECT(item)->defaultLabel());
743 sp_print_comment(ctx, comment);
744 g_free(comment);
745 }
746 }
748 /**
749 * Sets style, path, and paintbox. Updates marker views, including dimensions.
750 */
751 static NRArenaItem *
752 sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
753 {
754 SPObject *object = SP_OBJECT(item);
755 SPShape *shape = SP_SHAPE(item);
757 NRArenaItem *arenaitem = NRArenaShape::create(arena);
758 NRArenaShape * const s = NR_ARENA_SHAPE(arenaitem);
759 nr_arena_shape_set_style(s, object->style);
760 nr_arena_shape_set_path(s, shape->curve, false);
761 NR::Maybe<NR::Rect> paintbox = item->getBounds(NR::identity());
762 if (paintbox) {
763 s->setPaintBox(*paintbox);
764 }
766 if (sp_shape_has_markers (shape)) {
768 /* Dimension the marker views */
769 if (!arenaitem->key) {
770 NR_ARENA_ITEM_SET_KEY (arenaitem, sp_item_display_key_new (SP_MARKER_LOC_QTY));
771 }
773 for (int i = 0; i < SP_MARKER_LOC_QTY; i++) {
774 if (shape->marker[i]) {
775 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
776 NR_ARENA_ITEM_GET_KEY (arenaitem) + i - SP_MARKER_LOC,
777 sp_shape_number_of_markers (shape, i));
778 }
779 }
782 /* Update marker views */
783 sp_shape_update_marker_view (shape, arenaitem);
784 }
786 return arenaitem;
787 }
789 /**
790 * Hides/removes marker views from the shape.
791 */
792 static void
793 sp_shape_hide (SPItem *item, unsigned int key)
794 {
795 SPShape *shape;
796 SPItemView *v;
797 int i;
799 shape = (SPShape *) item;
801 for (i=0; i<SP_MARKER_LOC_QTY; i++) {
802 if (shape->marker[i]) {
803 for (v = item->display; v != NULL; v = v->next) {
804 if (key == v->key) {
805 sp_marker_hide ((SPMarker *) shape->marker[i],
806 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
807 }
808 }
809 }
810 }
812 if (((SPItemClass *) parent_class)->hide) {
813 ((SPItemClass *) parent_class)->hide (item, key);
814 }
815 }
817 /**
818 * \param shape Shape.
819 * \return TRUE if the shape has any markers, or FALSE if not.
820 */
821 int
822 sp_shape_has_markers (SPShape const *shape)
823 {
824 /* Note, we're ignoring 'marker' settings, which technically should apply for
825 all three settings. This should be fixed later such that if 'marker' is
826 specified, then all three should appear. */
828 return (
829 shape->curve &&
830 (shape->marker[SP_MARKER_LOC_START] ||
831 shape->marker[SP_MARKER_LOC_MID] ||
832 shape->marker[SP_MARKER_LOC_END])
833 );
834 }
837 /**
838 * \param shape Shape.
839 * \param type Marker type (e.g. SP_MARKER_LOC_START)
840 * \return Number of markers that the shape has of this type.
841 */
842 int
843 sp_shape_number_of_markers (SPShape *shape, int type)
844 {
845 int n = 0;
846 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
847 if (sp_shape_marker_required (shape, type, bp)) {
848 n++;
849 }
850 }
852 return n;
853 }
855 /**
856 * Checks if the given marker is used in the shape, and if so, it
857 * releases it by calling sp_marker_hide. Also detaches signals
858 * and unrefs the marker from the shape.
859 */
860 static void
861 sp_shape_marker_release (SPObject *marker, SPShape *shape)
862 {
863 SPItem *item;
864 int i;
866 item = (SPItem *) shape;
868 for (i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
869 if (marker == shape->marker[i]) {
870 SPItemView *v;
871 /* Hide marker */
872 for (v = item->display; v != NULL; v = v->next) {
873 sp_marker_hide ((SPMarker *) (shape->marker[i]), NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
874 /* fixme: Do we need explicit remove here? (Lauris) */
875 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
876 }
877 /* Detach marker */
878 sp_signal_disconnect_by_data (shape->marker[i], item);
879 shape->marker[i] = sp_object_hunref (shape->marker[i], item);
880 }
881 }
882 }
884 /**
885 * No-op. Exists for handling 'modified' messages
886 */
887 static void
888 sp_shape_marker_modified (SPObject *marker, guint flags, SPItem *item)
889 {
890 /* I think mask does update automagically */
891 /* g_warning ("Item %s mask %s modified", SP_OBJECT_ID (item), SP_OBJECT_ID (mask)); */
892 }
894 /**
895 * Adds a new marker to shape object at the location indicated by key. value
896 * must be a valid URI reference resolvable from the shape object (i.e., present
897 * in the document <defs>). If the shape object already has a marker
898 * registered at the given position, it is removed first. Then the
899 * new marker is hrefed and its signals connected.
900 */
901 void
902 sp_shape_set_marker (SPObject *object, unsigned int key, const gchar *value)
903 {
904 SPItem *item = (SPItem *) object;
905 SPShape *shape = (SPShape *) object;
907 if (key < SP_MARKER_LOC_START || key > SP_MARKER_LOC_END) {
908 return;
909 }
911 SPObject *mrk = sp_uri_reference_resolve (SP_OBJECT_DOCUMENT (object), value);
912 if (mrk != shape->marker[key]) {
913 if (shape->marker[key]) {
914 SPItemView *v;
916 /* Detach marker */
917 shape->release_connect[key].disconnect();
918 shape->modified_connect[key].disconnect();
920 /* Hide marker */
921 for (v = item->display; v != NULL; v = v->next) {
922 sp_marker_hide ((SPMarker *) (shape->marker[key]),
923 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + key);
924 /* fixme: Do we need explicit remove here? (Lauris) */
925 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
926 }
928 /* Unref marker */
929 shape->marker[key] = sp_object_hunref (shape->marker[key], object);
930 }
931 if (SP_IS_MARKER (mrk)) {
932 shape->marker[key] = sp_object_href (mrk, object);
933 shape->release_connect[key] = mrk->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_shape_marker_release), shape));
934 shape->modified_connect[key] = mrk->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_shape_marker_modified), shape));
935 }
936 }
937 }
941 /* Shape section */
943 /**
944 * Calls any registered handlers for the set_shape action
945 */
946 void
947 sp_shape_set_shape (SPShape *shape)
948 {
949 g_return_if_fail (shape != NULL);
950 g_return_if_fail (SP_IS_SHAPE (shape));
952 if (SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape) {
953 SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape (shape);
954 }
955 }
957 /**
958 * Adds a curve to the shape. If owner is specified, a reference
959 * will be made, otherwise the curve will be copied into the shape.
960 * Any existing curve in the shape will be unreferenced first.
961 * This routine also triggers a request to update the display.
962 */
963 void
964 sp_shape_set_curve (SPShape *shape, SPCurve *curve, unsigned int owner)
965 {
966 if (shape->curve) {
967 shape->curve = sp_curve_unref (shape->curve);
968 }
969 if (curve) {
970 if (owner) {
971 shape->curve = sp_curve_ref (curve);
972 } else {
973 shape->curve = sp_curve_copy (curve);
974 }
975 }
976 SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
977 }
979 /**
980 * Return duplicate of curve (if any exists) or NULL if there is no curve
981 */
982 SPCurve *
983 sp_shape_get_curve (SPShape *shape)
984 {
985 if (shape->curve) {
986 return sp_curve_copy (shape->curve);
987 }
988 return NULL;
989 }
991 /* NOT FOR GENERAL PUBLIC UNTIL SORTED OUT (Lauris) */
992 void
993 sp_shape_set_curve_insync (SPShape *shape, SPCurve *curve, unsigned int owner)
994 {
995 if (shape->curve) {
996 shape->curve = sp_curve_unref (shape->curve);
997 }
998 if (curve) {
999 if (owner) {
1000 shape->curve = sp_curve_ref (curve);
1001 } else {
1002 shape->curve = sp_curve_copy (curve);
1003 }
1004 }
1005 }
1007 /**
1008 * Sets the snappoint p to the end point of the path segment
1009 */
1010 static void sp_shape_snappoints(SPItem const *item, SnapPointsIter p)
1011 {
1012 g_assert(item != NULL);
1013 g_assert(SP_IS_SHAPE(item));
1015 SPShape const *shape = SP_SHAPE(item);
1016 if (shape->curve == NULL) {
1017 return;
1018 }
1020 NR::Matrix const i2d (sp_item_i2d_affine (item));
1022 /* Use the end points of each segment of the path */
1023 NArtBpath const *bp = SP_CURVE_BPATH(shape->curve);
1025 if (bp->code == NR_MOVETO) { // Indicates the start of a closed subpath, see nr-path-code.h
1026 bp++; //The first point of a closed path is coincident with the end point. Skip the first point as we need only one
1027 }
1029 while (bp->code != NR_END) {
1030 *p = bp->c(3) * i2d;
1031 bp++;
1032 }
1033 }
1036 /*
1037 Local Variables:
1038 mode:c++
1039 c-file-style:"stroustrup"
1040 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1041 indent-tabs-mode:nil
1042 fill-column:99
1043 End:
1044 */
1045 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :