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"
38 #include "attributes.h"
40 #include "live_effects/effect.h"
41 #include "live_effects/lpeobject.h"
42 #include "live_effects/lpeobject-reference.h"
43 #include "uri.h"
44 #include "extract-uri.h"
45 #include "uri-references.h"
46 #include "bad-uri-exception.h"
47 #include "xml/repr.h"
49 #define noSHAPE_VERBOSE
51 static void sp_shape_class_init (SPShapeClass *klass);
52 static void sp_shape_init (SPShape *shape);
53 static void sp_shape_finalize (GObject *object);
55 static void sp_shape_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
56 static void sp_shape_release (SPObject *object);
58 static void sp_shape_set(SPObject *object, unsigned key, gchar const *value);
59 static void sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags);
60 static void sp_shape_modified (SPObject *object, unsigned int flags);
61 static Inkscape::XML::Node *sp_shape_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
63 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
64 void sp_shape_print (SPItem * item, SPPrintContext * ctx);
65 static NRArenaItem *sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
66 static void sp_shape_hide (SPItem *item, unsigned int key);
67 static void sp_shape_snappoints (SPItem const *item, SnapPointsIter p);
69 static void sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai);
71 static void lpeobject_ref_changed(SPObject *old_ref, SPObject *ref, SPShape *shape);
72 static void lpeobject_ref_modified(SPObject *href, guint flags, SPShape *shape);
74 static SPItemClass *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_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;
107 SPObjectClass *sp_object_class;
108 SPItemClass * item_class;
109 SPPathClass * path_class;
111 gobject_class = (GObjectClass *) klass;
112 sp_object_class = (SPObjectClass *) klass;
113 item_class = (SPItemClass *) klass;
114 path_class = (SPPathClass *) klass;
116 parent_class = (SPItemClass *)g_type_class_peek_parent (klass);
118 gobject_class->finalize = sp_shape_finalize;
120 sp_object_class->build = sp_shape_build;
121 sp_object_class->release = sp_shape_release;
122 sp_object_class->set = sp_shape_set;
123 sp_object_class->update = sp_shape_update;
124 sp_object_class->modified = sp_shape_modified;
125 sp_object_class->write = sp_shape_write;
127 item_class->bbox = sp_shape_bbox;
128 item_class->print = sp_shape_print;
129 item_class->show = sp_shape_show;
130 item_class->hide = sp_shape_hide;
131 item_class->snappoints = sp_shape_snappoints;
133 klass->set_shape = NULL;
134 klass->update_patheffect = NULL;
135 }
137 /**
138 * Initializes an SPShape object.
139 */
140 static void
141 sp_shape_init (SPShape *shape)
142 {
143 shape->path_effect_href = NULL;
144 shape->path_effect_ref = new Inkscape::LivePathEffect::LPEObjectReference(SP_OBJECT(shape));
145 shape->path_effect_ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(lpeobject_ref_changed), shape));
147 for ( int i = 0 ; i < SP_MARKER_LOC_QTY ; i++ ) {
148 new (&shape->release_connect[i]) sigc::connection();
149 new (&shape->modified_connect[i]) sigc::connection();
150 }
151 }
153 static void
154 sp_shape_finalize (GObject *object)
155 {
156 SPShape *shape=(SPShape *)object;
158 for ( int i = 0 ; i < SP_MARKER_LOC_QTY ; i++ ) {
159 shape->release_connect[i].disconnect();
160 shape->release_connect[i].~connection();
161 shape->modified_connect[i].disconnect();
162 shape->modified_connect[i].~connection();
163 }
165 if (((GObjectClass *) (parent_class))->finalize) {
166 (* ((GObjectClass *) (parent_class))->finalize)(object);
167 }
168 }
170 /**
171 * Virtual build callback for SPMarker.
172 *
173 * This is to be invoked immediately after creation of an SPShape.
174 *
175 * \see sp_object_build()
176 */
177 static void
178 sp_shape_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
179 {
180 sp_object_read_attr(object, "inkscape:path-effect");
182 if (((SPObjectClass *) (parent_class))->build) {
183 (*((SPObjectClass *) (parent_class))->build) (object, document, repr);
184 }
185 }
187 /**
188 * Removes, releases and unrefs all children of object
189 *
190 * This is the inverse of sp_shape_build(). It must be invoked as soon
191 * as the shape is removed from the tree, even if it is still referenced
192 * by other objects. This routine also disconnects/unrefs markers and
193 * curves attached to it.
194 *
195 * \see sp_object_release()
196 */
197 static void
198 sp_shape_release (SPObject *object)
199 {
200 SPItem *item;
201 SPShape *shape;
202 SPItemView *v;
203 int i;
205 item = (SPItem *) object;
206 shape = (SPShape *) object;
208 for (i=SP_MARKER_LOC_START; i<SP_MARKER_LOC_QTY; i++) {
209 if (shape->marker[i]) {
210 sp_signal_disconnect_by_data (shape->marker[i], object);
211 for (v = item->display; v != NULL; v = v->next) {
212 sp_marker_hide ((SPMarker *) shape->marker[i], NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
213 }
214 shape->marker[i] = sp_object_hunref (shape->marker[i], object);
215 }
216 }
217 if (shape->curve) {
218 shape->curve = sp_curve_unref (shape->curve);
219 }
221 if (shape->path_effect_href) {
222 g_free(shape->path_effect_href);
223 }
224 shape->path_effect_ref->detach();
226 if (((SPObjectClass *) parent_class)->release) {
227 ((SPObjectClass *) parent_class)->release (object);
228 }
229 }
233 static void
234 sp_shape_set(SPObject *object, unsigned int key, gchar const *value)
235 {
236 SPShape *shape = (SPShape *) object;
238 switch (key) {
239 case SP_ATTR_INKSCAPE_PATH_EFFECT:
240 if ( value && shape->path_effect_href && ( strcmp(value, shape->path_effect_href) == 0 ) ) {
241 /* No change, do nothing. */
242 } else {
243 if (shape->path_effect_href) {
244 g_free(shape->path_effect_href);
245 shape->path_effect_href = NULL;
246 }
247 if (value) {
248 shape->path_effect_href = g_strdup(value);
250 // Now do the attaching, which emits the changed signal.
251 try {
252 shape->path_effect_ref->attach(Inkscape::URI(value));
253 } catch (Inkscape::BadURIException &e) {
254 g_warning("%s", e.what());
255 shape->path_effect_ref->detach();
256 }
257 } else {
258 // Detach, which emits the changed signal.
259 shape->path_effect_ref->detach();
260 }
261 }
262 break;
263 default:
264 if (((SPObjectClass *) parent_class)->set) {
265 ((SPObjectClass *) parent_class)->set(object, key, value);
266 }
267 break;
268 }
269 }
271 static Inkscape::XML::Node *
272 sp_shape_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
273 {
274 SPShape *shape = (SPShape *) object;
276 if ( shape->path_effect_href ) {
277 repr->setAttribute("inkscape:path-effect", shape->path_effect_href);
278 } else {
279 repr->setAttribute("inkscape:path-effect", NULL);
280 }
282 if (((SPObjectClass *)(parent_class))->write) {
283 ((SPObjectClass *)(parent_class))->write(object, repr, flags);
284 }
286 return repr;
287 }
289 /**
290 * Updates the shape when its attributes have changed. Also establishes
291 * marker objects to match the style settings.
292 */
293 static void
294 sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags)
295 {
296 SPItem *item = (SPItem *) object;
297 SPShape *shape = (SPShape *) object;
299 if (((SPObjectClass *) (parent_class))->update) {
300 (* ((SPObjectClass *) (parent_class))->update) (object, ctx, flags);
301 }
303 /* This stanza checks that an object's marker style agrees with
304 * the marker objects it has allocated. sp_shape_set_marker ensures
305 * that the appropriate marker objects are present (or absent) to
306 * match the style.
307 */
308 /* TODO: It would be nice if this could be done at an earlier level */
309 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
310 sp_shape_set_marker (object, i, object->style->marker[i].value);
311 }
313 if (flags & (SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
314 SPStyle *style;
315 style = SP_OBJECT_STYLE (object);
316 if (style->stroke_width.unit == SP_CSS_UNIT_PERCENT) {
317 SPItemCtx *ictx = (SPItemCtx *) ctx;
318 double const aw = 1.0 / NR::expansion(ictx->i2vp);
319 style->stroke_width.computed = style->stroke_width.value * aw;
320 for (SPItemView *v = ((SPItem *) (shape))->display; v != NULL; v = v->next) {
321 nr_arena_shape_set_style ((NRArenaShape *) v->arenaitem, style);
322 }
323 }
324 }
326 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
327 /* This is suboptimal, because changing parent style schedules recalculation */
328 /* But on the other hand - how can we know that parent does not tie style and transform */
329 NR::Maybe<NR::Rect> paintbox = SP_ITEM(object)->getBounds(NR::identity());
330 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
331 NRArenaShape * const s = NR_ARENA_SHAPE(v->arenaitem);
332 if (flags & SP_OBJECT_MODIFIED_FLAG) {
333 nr_arena_shape_set_path(s, shape->curve, (flags & SP_OBJECT_USER_MODIFIED_FLAG_B));
334 }
335 if (paintbox) {
336 s->setPaintBox(*paintbox);
337 }
338 }
339 }
341 if (sp_shape_has_markers (shape)) {
343 /* Dimension marker views */
344 for (SPItemView *v = item->display; v != NULL; v = v->next) {
346 if (!v->arenaitem->key) {
347 /* Get enough keys for all, start, mid and end marker types,
348 ** and set this view's arenaitem key to the first of these keys.
349 */
350 NR_ARENA_ITEM_SET_KEY (
351 v->arenaitem,
352 sp_item_display_key_new (SP_MARKER_LOC_QTY)
353 );
354 }
356 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
357 if (shape->marker[i]) {
358 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
359 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i - SP_MARKER_LOC,
360 sp_shape_number_of_markers (shape, i));
361 }
362 }
363 }
365 /* Update marker views */
366 for (SPItemView *v = item->display; v != NULL; v = v->next) {
367 sp_shape_update_marker_view (shape, v->arenaitem);
368 }
369 }
370 }
373 /**
374 * Works out whether a marker of a given type is required at a particular
375 * point on a shape.
376 *
377 * \param shape Shape of interest.
378 * \param m Marker type (e.g. SP_MARKER_LOC_START)
379 * \param bp Path segment.
380 * \return 1 if a marker is required here, otherwise 0.
381 */
382 bool
383 sp_shape_marker_required(SPShape const *shape, int const m, NArtBpath *bp)
384 {
385 if (shape->marker[m] == NULL) {
386 return false;
387 }
389 if (bp == SP_CURVE_BPATH(shape->curve))
390 return m == SP_MARKER_LOC_START;
391 else if (bp[1].code == NR_END)
392 return m == SP_MARKER_LOC_END;
393 else
394 return m == SP_MARKER_LOC_MID;
395 }
397 static bool
398 is_moveto(NRPathcode const c)
399 {
400 return c == NR_MOVETO || c == NR_MOVETO_OPEN;
401 }
403 /**
404 * Helper function that advances a subpath's bpath to the first subpath
405 * by checking for moveto segments.
406 *
407 * \pre The bpath[] containing bp begins with a moveto.
408 */
409 static NArtBpath const *
410 first_seg_in_subpath(NArtBpath const *bp)
411 {
412 while (!is_moveto(bp->code)) {
413 --bp;
414 }
415 return bp;
416 }
418 /**
419 * Advances the bpath to the last segment in the subpath.
420 */
421 static NArtBpath const *
422 last_seg_in_subpath(NArtBpath const *bp)
423 {
424 for(;;) {
425 ++bp;
426 switch (bp->code) {
427 case NR_MOVETO:
428 case NR_MOVETO_OPEN:
429 case NR_END:
430 --bp;
431 return bp;
433 default: continue;
434 }
435 }
436 }
439 /* A subpath begins with a moveto and ends immediately before the next moveto or NR_END.
440 * (`moveto' here means either NR_MOVETO or NR_MOVETO_OPEN.) I'm assuming that non-empty
441 * paths always begin with a moveto.
442 *
443 * The control points of the subpath are the control points of the path elements of the subpath.
444 *
445 * As usual, the control points of a moveto or NR_LINETO are {c(3)}, and
446 * the control points of a NR_CURVETO are {c(1), c(2), c(3)}.
447 * (It follows from the definition that NR_END isn't part of a subpath.)
448 *
449 * The initial control point is bpath[bi0].c(3).
450 *
451 * Reference: http://www.w3.org/TR/SVG11/painting.html#MarkerElement, the `orient' attribute.
452 * Reference for behaviour of zero-length segments:
453 * http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
454 */
456 static double const no_tangent = 128.0; /* arbitrarily-chosen value outside the range of atan2, i.e. outside of [-pi, pi]. */
458 /**
459 * Helper function to calculate the outgoing tangent of a path
460 * ( atan2(other - p0) )
461 * \pre The bpath[] containing bp0 begins with a moveto.
462 */
463 static double
464 outgoing_tangent(NArtBpath const *bp0)
465 {
466 /* See notes in comment block above. */
468 g_assert(bp0->code != NR_END);
469 NR::Point const &p0 = bp0->c(3);
470 NR::Point other;
471 for (NArtBpath const *bp = bp0;;) {
472 ++bp;
473 switch (bp->code) {
474 case NR_LINETO:
475 other = bp->c(3);
476 if (other != p0) {
477 goto found;
478 }
479 break;
481 case NR_CURVETO:
482 for (unsigned ci = 1; ci <= 3; ++ci) {
483 other = bp->c(ci);
484 if (other != p0) {
485 goto found;
486 }
487 }
488 break;
490 case NR_MOVETO_OPEN:
491 case NR_END:
492 case NR_MOVETO:
493 bp = first_seg_in_subpath(bp0);
494 if (bp == bp0) {
495 /* Gone right around the subpath without finding any different point since the
496 * initial moveto. */
497 return no_tangent;
498 }
499 if (bp->code != NR_MOVETO) {
500 /* Open subpath. */
501 return no_tangent;
502 }
503 other = bp->c(3);
504 if (other != p0) {
505 goto found;
506 }
507 break;
508 }
510 if (bp == bp0) {
511 /* Back where we started, so zero-length subpath. */
512 return no_tangent;
514 /* Note: this test must come after we've looked at element bp, in case bp0 is a curve:
515 * we must look at c(1) and c(2). (E.g. single-curve subpath.)
516 */
517 }
518 }
520 found:
521 return atan2( other - p0 );
522 }
524 /**
525 * Helper function to calculate the incoming tangent of a path
526 * ( atan2(p0 - other) )
527 *
528 * \pre The bpath[] containing bp0 begins with a moveto.
529 */
530 static double
531 incoming_tangent(NArtBpath const *bp0)
532 {
533 /* See notes in comment block before outgoing_tangent. */
535 g_assert(bp0->code != NR_END);
536 NR::Point const &p0 = bp0->c(3);
537 NR::Point other;
538 for (NArtBpath const *bp = bp0;;) {
539 switch (bp->code) {
540 case NR_LINETO:
541 other = bp->c(3);
542 if (other != p0) {
543 goto found;
544 }
545 --bp;
546 break;
548 case NR_CURVETO:
549 for (unsigned ci = 3; ci != 0; --ci) {
550 other = bp->c(ci);
551 if (other != p0) {
552 goto found;
553 }
554 }
555 --bp;
556 break;
558 case NR_MOVETO:
559 case NR_MOVETO_OPEN:
560 other = bp->c(3);
561 if (other != p0) {
562 goto found;
563 }
564 if (bp->code != NR_MOVETO) {
565 /* Open subpath. */
566 return no_tangent;
567 }
568 bp = last_seg_in_subpath(bp0);
569 break;
571 default: /* includes NR_END */
572 g_error("Found invalid path code %u in middle of path.", bp->code);
573 return no_tangent;
574 }
576 if (bp == bp0) {
577 /* Back where we started from: zero-length subpath. */
578 return no_tangent;
579 }
580 }
582 found:
583 return atan2( p0 - other );
584 }
587 /**
588 * Calculate the transform required to get a marker's path object in the
589 * right place for particular path segment on a shape. You should
590 * call sp_shape_marker_required first to see if a marker is required
591 * at this point.
592 *
593 * \see sp_shape_marker_required.
594 *
595 * \param shape Shape which the marker is for.
596 * \param m Marker type (e.g. SP_MARKER_LOC_START)
597 * \param bp Path segment which the arrow is for.
598 * \return Transform matrix.
599 */
600 NR::Matrix
601 sp_shape_marker_get_transform(SPShape const *shape, NArtBpath const *bp)
602 {
603 g_return_val_if_fail(( is_moveto(SP_CURVE_BPATH(shape->curve)[0].code)
604 && ( 0 < shape->curve->end )
605 && ( SP_CURVE_BPATH(shape->curve)[shape->curve->end].code == NR_END ) ),
606 NR::Matrix(NR::translate(bp->c(3))));
607 double const angle1 = incoming_tangent(bp);
608 double const angle2 = outgoing_tangent(bp);
610 /* angle1 and angle2 are now each either unset (i.e. still 100 from their initialization) or in
611 [-pi, pi] from atan2. */
612 g_assert((-3.15 < angle1 && angle1 < 3.15) || (angle1 == no_tangent));
613 g_assert((-3.15 < angle2 && angle2 < 3.15) || (angle2 == no_tangent));
615 double ret_angle;
616 if (angle1 == no_tangent) {
617 /* First vertex of an open subpath. */
618 ret_angle = ( angle2 == no_tangent
619 ? 0.
620 : angle2 );
621 } else if (angle2 == no_tangent) {
622 /* Last vertex of an open subpath. */
623 ret_angle = angle1;
624 } else {
625 ret_angle = .5 * (angle1 + angle2);
627 if ( fabs( angle2 - angle1 ) > M_PI ) {
628 /* ret_angle is in the middle of the larger of the two sectors between angle1 and
629 * angle2, so flip it by 180degrees to force it to the middle of the smaller sector.
630 *
631 * (Imagine a circle with rays drawn at angle1 and angle2 from the centre of the
632 * circle. Those two rays divide the circle into two sectors.)
633 */
634 ret_angle += M_PI;
635 }
636 }
638 return NR::Matrix(NR::rotate(ret_angle)) * NR::translate(bp->c(3));
639 }
641 /**
642 * Updates the instances (views) of a given marker in a shape.
643 * Marker views have to be scaled already. The transformation
644 * is retrieved and then shown by calling sp_marker_show_instance.
645 */
646 static void
647 sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai)
648 {
649 SPStyle *style = ((SPObject *) shape)->style;
651 for (int i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
652 if (shape->marker[i] == NULL) {
653 continue;
654 }
656 int n = 0;
658 for (NArtBpath *bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
659 if (sp_shape_marker_required (shape, i, bp)) {
660 NR::Matrix const m(sp_shape_marker_get_transform(shape, bp));
661 sp_marker_show_instance ((SPMarker* ) shape->marker[i], ai,
662 NR_ARENA_ITEM_GET_KEY(ai) + i, n, m,
663 style->stroke_width.computed);
664 n++;
665 }
666 }
667 }
668 }
670 /**
671 * Sets modified flag for all sub-item views.
672 */
673 static void
674 sp_shape_modified (SPObject *object, unsigned int flags)
675 {
676 SPShape *shape = SP_SHAPE (object);
678 if (((SPObjectClass *) (parent_class))->modified) {
679 (* ((SPObjectClass *) (parent_class))->modified) (object, flags);
680 }
682 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
683 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
684 nr_arena_shape_set_style (NR_ARENA_SHAPE (v->arenaitem), object->style);
685 }
686 }
687 }
689 /**
690 * Calculates the bounding box for item, storing it into bbox.
691 * This also includes the bounding boxes of any markers included in the shape.
692 */
693 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
694 {
695 SPShape const *shape = SP_SHAPE (item);
697 if (shape->curve) {
699 NRRect cbbox;
700 NRBPath bp;
702 bp.path = SP_CURVE_BPATH (shape->curve);
704 cbbox.x0 = cbbox.y0 = NR_HUGE;
705 cbbox.x1 = cbbox.y1 = -NR_HUGE;
707 nr_path_matrix_bbox_union(&bp, transform, &cbbox);
709 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
711 SPStyle* style=SP_OBJECT_STYLE (item);
712 if (style->stroke.type != SP_PAINT_TYPE_NONE) {
713 double const scale = expansion(transform);
714 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
715 double const width = MAX(0.125, style->stroke_width.computed * scale);
716 if ( fabs(cbbox.x1-cbbox.x0) > -0.00001 && fabs(cbbox.y1-cbbox.y0) > -0.00001 ) {
717 cbbox.x0-=0.5*width;
718 cbbox.x1+=0.5*width;
719 cbbox.y0-=0.5*width;
720 cbbox.y1+=0.5*width;
721 }
722 }
723 }
725 // Union with bboxes of the markers, if any
726 if (sp_shape_has_markers (shape)) {
727 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
728 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
729 if (sp_shape_marker_required (shape, m, bp)) {
731 SPMarker* marker = SP_MARKER (shape->marker[m]);
732 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
734 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
736 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
737 tr = NR::scale(style->stroke_width.computed) * tr;
738 }
740 // total marker transform
741 tr = marker_item->transform * marker->c2p * tr * transform;
743 // get bbox of the marker with that transform
744 NRRect marker_bbox;
745 sp_item_invoke_bbox (marker_item, &marker_bbox, tr, true);
746 // union it with the shape bbox
747 nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
748 }
749 }
750 }
751 }
752 }
754 // copy our bbox to the variable we're given
755 *bbox = cbbox;
756 }
757 }
759 /**
760 * Prepares shape for printing. Handles printing of comments for printing
761 * debugging, sizes the item to fit into the document width/height,
762 * applies print fill/stroke, sets transforms for markers, and adds
763 * comment labels.
764 */
765 void
766 sp_shape_print (SPItem *item, SPPrintContext *ctx)
767 {
768 NRRect pbox, dbox, bbox;
770 SPShape *shape = SP_SHAPE(item);
772 if (!shape->curve) return;
774 gint add_comments = prefs_get_int_attribute_limited ("printing.debug", "add-label-comments", 0, 0, 1);
775 if (add_comments) {
776 gchar * comment = g_strdup_printf("begin '%s'",
777 SP_OBJECT(item)->defaultLabel());
778 sp_print_comment(ctx, comment);
779 g_free(comment);
780 }
782 /* fixme: Think (Lauris) */
783 sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
784 dbox.x0 = 0.0;
785 dbox.y0 = 0.0;
786 dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
787 dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
788 sp_item_bbox_desktop (item, &bbox);
789 NR::Matrix const i2d = sp_item_i2d_affine(item);
791 SPStyle* style = SP_OBJECT_STYLE (item);
793 if (style->fill.type != SP_PAINT_TYPE_NONE) {
794 NRBPath bp;
795 bp.path = SP_CURVE_BPATH(shape->curve);
796 sp_print_fill (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
797 }
799 if (style->stroke.type != SP_PAINT_TYPE_NONE) {
800 NRBPath bp;
801 bp.path = SP_CURVE_BPATH(shape->curve);
802 sp_print_stroke (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
803 }
805 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
806 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
807 if (sp_shape_marker_required (shape, m, bp)) {
809 SPMarker* marker = SP_MARKER (shape->marker[m]);
810 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
812 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
814 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
815 tr = NR::scale(style->stroke_width.computed) * tr;
816 }
818 tr = marker_item->transform * marker->c2p * tr;
820 NR::Matrix old_tr = marker_item->transform;
821 marker_item->transform = tr;
822 sp_item_invoke_print (marker_item, ctx);
823 marker_item->transform = old_tr;
824 }
825 }
826 }
828 if (add_comments) {
829 gchar * comment = g_strdup_printf("end '%s'",
830 SP_OBJECT(item)->defaultLabel());
831 sp_print_comment(ctx, comment);
832 g_free(comment);
833 }
834 }
836 /**
837 * Sets style, path, and paintbox. Updates marker views, including dimensions.
838 */
839 static NRArenaItem *
840 sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
841 {
842 SPObject *object = SP_OBJECT(item);
843 SPShape *shape = SP_SHAPE(item);
845 NRArenaItem *arenaitem = NRArenaShape::create(arena);
846 NRArenaShape * const s = NR_ARENA_SHAPE(arenaitem);
847 nr_arena_shape_set_style(s, object->style);
848 nr_arena_shape_set_path(s, shape->curve, false);
849 NR::Maybe<NR::Rect> paintbox = item->getBounds(NR::identity());
850 if (paintbox) {
851 s->setPaintBox(*paintbox);
852 }
854 if (sp_shape_has_markers (shape)) {
856 /* Dimension the marker views */
857 if (!arenaitem->key) {
858 NR_ARENA_ITEM_SET_KEY (arenaitem, sp_item_display_key_new (SP_MARKER_LOC_QTY));
859 }
861 for (int i = 0; i < SP_MARKER_LOC_QTY; i++) {
862 if (shape->marker[i]) {
863 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
864 NR_ARENA_ITEM_GET_KEY (arenaitem) + i - SP_MARKER_LOC,
865 sp_shape_number_of_markers (shape, i));
866 }
867 }
870 /* Update marker views */
871 sp_shape_update_marker_view (shape, arenaitem);
872 }
874 return arenaitem;
875 }
877 /**
878 * Hides/removes marker views from the shape.
879 */
880 static void
881 sp_shape_hide (SPItem *item, unsigned int key)
882 {
883 SPShape *shape;
884 SPItemView *v;
885 int i;
887 shape = (SPShape *) item;
889 for (i=0; i<SP_MARKER_LOC_QTY; i++) {
890 if (shape->marker[i]) {
891 for (v = item->display; v != NULL; v = v->next) {
892 if (key == v->key) {
893 sp_marker_hide ((SPMarker *) shape->marker[i],
894 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
895 }
896 }
897 }
898 }
900 if (((SPItemClass *) parent_class)->hide) {
901 ((SPItemClass *) parent_class)->hide (item, key);
902 }
903 }
905 /**
906 * \param shape Shape.
907 * \return TRUE if the shape has any markers, or FALSE if not.
908 */
909 int
910 sp_shape_has_markers (SPShape const *shape)
911 {
912 /* Note, we're ignoring 'marker' settings, which technically should apply for
913 all three settings. This should be fixed later such that if 'marker' is
914 specified, then all three should appear. */
916 return (
917 shape->curve &&
918 (shape->marker[SP_MARKER_LOC_START] ||
919 shape->marker[SP_MARKER_LOC_MID] ||
920 shape->marker[SP_MARKER_LOC_END])
921 );
922 }
925 /**
926 * \param shape Shape.
927 * \param type Marker type (e.g. SP_MARKER_LOC_START)
928 * \return Number of markers that the shape has of this type.
929 */
930 int
931 sp_shape_number_of_markers (SPShape *shape, int type)
932 {
933 int n = 0;
934 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
935 if (sp_shape_marker_required (shape, type, bp)) {
936 n++;
937 }
938 }
940 return n;
941 }
943 /**
944 * Checks if the given marker is used in the shape, and if so, it
945 * releases it by calling sp_marker_hide. Also detaches signals
946 * and unrefs the marker from the shape.
947 */
948 static void
949 sp_shape_marker_release (SPObject *marker, SPShape *shape)
950 {
951 SPItem *item;
952 int i;
954 item = (SPItem *) shape;
956 for (i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
957 if (marker == shape->marker[i]) {
958 SPItemView *v;
959 /* Hide marker */
960 for (v = item->display; v != NULL; v = v->next) {
961 sp_marker_hide ((SPMarker *) (shape->marker[i]), NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
962 /* fixme: Do we need explicit remove here? (Lauris) */
963 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
964 }
965 /* Detach marker */
966 sp_signal_disconnect_by_data (shape->marker[i], item);
967 shape->marker[i] = sp_object_hunref (shape->marker[i], item);
968 }
969 }
970 }
972 /**
973 * No-op. Exists for handling 'modified' messages
974 */
975 static void
976 sp_shape_marker_modified (SPObject *marker, guint flags, SPItem *item)
977 {
978 /* I think mask does update automagically */
979 /* g_warning ("Item %s mask %s modified", SP_OBJECT_ID (item), SP_OBJECT_ID (mask)); */
980 }
982 /**
983 * Adds a new marker to shape object at the location indicated by key. value
984 * must be a valid URI reference resolvable from the shape object (i.e., present
985 * in the document <defs>). If the shape object already has a marker
986 * registered at the given position, it is removed first. Then the
987 * new marker is hrefed and its signals connected.
988 */
989 void
990 sp_shape_set_marker (SPObject *object, unsigned int key, const gchar *value)
991 {
992 SPItem *item = (SPItem *) object;
993 SPShape *shape = (SPShape *) object;
995 if (key < SP_MARKER_LOC_START || key > SP_MARKER_LOC_END) {
996 return;
997 }
999 SPObject *mrk = sp_uri_reference_resolve (SP_OBJECT_DOCUMENT (object), value);
1000 if (mrk != shape->marker[key]) {
1001 if (shape->marker[key]) {
1002 SPItemView *v;
1004 /* Detach marker */
1005 shape->release_connect[key].disconnect();
1006 shape->modified_connect[key].disconnect();
1008 /* Hide marker */
1009 for (v = item->display; v != NULL; v = v->next) {
1010 sp_marker_hide ((SPMarker *) (shape->marker[key]),
1011 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + key);
1012 /* fixme: Do we need explicit remove here? (Lauris) */
1013 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
1014 }
1016 /* Unref marker */
1017 shape->marker[key] = sp_object_hunref (shape->marker[key], object);
1018 }
1019 if (SP_IS_MARKER (mrk)) {
1020 shape->marker[key] = sp_object_href (mrk, object);
1021 shape->release_connect[key] = mrk->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_shape_marker_release), shape));
1022 shape->modified_connect[key] = mrk->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_shape_marker_modified), shape));
1023 }
1024 }
1025 }
1029 /* Shape section */
1031 /**
1032 * Calls any registered handlers for the set_shape action
1033 */
1034 void
1035 sp_shape_set_shape (SPShape *shape)
1036 {
1037 g_return_if_fail (shape != NULL);
1038 g_return_if_fail (SP_IS_SHAPE (shape));
1040 if (SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape) {
1041 SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape (shape);
1042 }
1043 }
1045 /**
1046 * Adds a curve to the shape. If owner is specified, a reference
1047 * will be made, otherwise the curve will be copied into the shape.
1048 * Any existing curve in the shape will be unreferenced first.
1049 * This routine also triggers a request to update the display.
1050 */
1051 void
1052 sp_shape_set_curve (SPShape *shape, SPCurve *curve, unsigned int owner)
1053 {
1054 if (shape->curve) {
1055 shape->curve = sp_curve_unref (shape->curve);
1056 }
1057 if (curve) {
1058 if (owner) {
1059 shape->curve = sp_curve_ref (curve);
1060 } else {
1061 shape->curve = sp_curve_copy (curve);
1062 }
1063 }
1064 SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1065 }
1067 /**
1068 * Return duplicate of curve (if any exists) or NULL if there is no curve
1069 */
1070 SPCurve *
1071 sp_shape_get_curve (SPShape *shape)
1072 {
1073 if (shape->curve) {
1074 return sp_curve_copy (shape->curve);
1075 }
1076 return NULL;
1077 }
1079 /* NOT FOR GENERAL PUBLIC UNTIL SORTED OUT (Lauris) */
1080 void
1081 sp_shape_set_curve_insync (SPShape *shape, SPCurve *curve, unsigned int owner)
1082 {
1083 if (shape->curve) {
1084 shape->curve = sp_curve_unref (shape->curve);
1085 }
1086 if (curve) {
1087 if (owner) {
1088 shape->curve = sp_curve_ref (curve);
1089 } else {
1090 shape->curve = sp_curve_copy (curve);
1091 }
1092 }
1093 }
1095 /**
1096 * Sets the snappoint p to the end point of the path segment
1097 */
1098 static void sp_shape_snappoints(SPItem const *item, SnapPointsIter p)
1099 {
1100 g_assert(item != NULL);
1101 g_assert(SP_IS_SHAPE(item));
1103 SPShape const *shape = SP_SHAPE(item);
1104 if (shape->curve == NULL) {
1105 return;
1106 }
1108 NR::Matrix const i2d (sp_item_i2d_affine (item));
1110 /* Use the end points of each segment of the path */
1111 NArtBpath const *bp = SP_CURVE_BPATH(shape->curve);
1113 if (bp->code == NR_MOVETO) { // Indicates the start of a closed subpath, see nr-path-code.h
1114 bp++; //The first point of a closed path is coincident with the end point. Skip the first point as we need only one
1115 }
1117 while (bp->code != NR_END) {
1118 *p = bp->c(3) * i2d;
1119 bp++;
1120 }
1121 }
1124 LivePathEffectObject *
1125 sp_shape_get_livepatheffectobject(SPShape *shape) {
1126 if (!shape) return NULL;
1128 return shape->path_effect_ref->lpeobject;
1129 }
1131 /**
1132 * Calls any registered handlers for the update_patheffect action
1133 */
1134 void
1135 sp_shape_update_patheffect (SPShape *shape, bool write)
1136 {
1137 #ifdef SHAPE_VERBOSE
1138 g_message("sp_shape_update_patheffect");
1139 #endif
1140 g_return_if_fail (shape != NULL);
1141 g_return_if_fail (SP_IS_SHAPE (shape));
1143 if (SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->update_patheffect) {
1144 SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->update_patheffect (shape, write);
1145 }
1146 }
1148 void sp_shape_perform_path_effect(SPCurve *curve, SPShape *shape) {
1149 if (!shape) return;
1150 if (!curve) return;
1152 LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(shape);
1153 if (lpeobj && lpeobj->lpe) {
1154 lpeobj->lpe->doEffect(curve);
1155 }
1156 }
1158 /**
1159 * Gets called when (re)attached to another lpeobject.
1160 */
1161 static void
1162 lpeobject_ref_changed(SPObject *old_ref, SPObject *ref, SPShape *shape)
1163 {
1164 if (old_ref) {
1165 sp_signal_disconnect_by_data(old_ref, shape);
1166 }
1167 if ( IS_LIVEPATHEFFECT(ref) && ref != shape )
1168 {
1169 ref->connectModified(sigc::bind(sigc::ptr_fun(&lpeobject_ref_modified), shape));
1170 lpeobject_ref_modified(ref, 0, shape);
1171 }
1172 }
1174 /**
1175 * Gets called when lpeobject repr contents change: i.e. parameter change.
1176 */
1177 static void
1178 lpeobject_ref_modified(SPObject *href, guint flags, SPShape *shape)
1179 {
1180 sp_shape_update_patheffect (shape, true);
1181 }
1183 void sp_shape_set_path_effect(SPShape *shape, gchar *value)
1184 {
1185 if (!value) {
1186 sp_shape_remove_path_effect(shape);
1187 } else {
1188 SP_OBJECT_REPR(shape)->setAttribute("inkscape:path-effect", value);
1189 }
1190 }
1192 void sp_shape_remove_path_effect(SPShape *shape)
1193 {
1194 Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
1195 repr->setAttribute("inkscape:path-effect", NULL);
1196 if (SP_IS_PATH(shape)) {
1197 repr->setAttribute("d", repr->attribute("inkscape:original-d"));
1198 repr->setAttribute("inkscape:original-d", NULL);
1199 }
1200 }
1203 /*
1204 Local Variables:
1205 mode:c++
1206 c-file-style:"stroustrup"
1207 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1208 indent-tabs-mode:nil
1209 fill-column:99
1210 End:
1211 */
1212 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :