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 new (&shape->lpe_modified_connection) sigc::connection();
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_SHAPE(object)->path_effect_ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(lpeobject_ref_changed), SP_SHAPE(object)));
182 sp_object_read_attr(object, "inkscape:path-effect");
184 if (((SPObjectClass *) (parent_class))->build) {
185 (*((SPObjectClass *) (parent_class))->build) (object, document, repr);
186 }
187 }
189 /**
190 * Removes, releases and unrefs all children of object
191 *
192 * This is the inverse of sp_shape_build(). It must be invoked as soon
193 * as the shape is removed from the tree, even if it is still referenced
194 * by other objects. This routine also disconnects/unrefs markers and
195 * curves attached to it.
196 *
197 * \see sp_object_release()
198 */
199 static void
200 sp_shape_release (SPObject *object)
201 {
202 SPItem *item;
203 SPShape *shape;
204 SPItemView *v;
205 int i;
207 item = (SPItem *) object;
208 shape = (SPShape *) object;
210 for (i=SP_MARKER_LOC_START; i<SP_MARKER_LOC_QTY; i++) {
211 if (shape->marker[i]) {
212 sp_signal_disconnect_by_data (shape->marker[i], object);
213 for (v = item->display; v != NULL; v = v->next) {
214 sp_marker_hide ((SPMarker *) shape->marker[i], NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
215 }
216 shape->marker[i] = sp_object_hunref (shape->marker[i], object);
217 }
218 }
219 if (shape->curve) {
220 shape->curve = sp_curve_unref (shape->curve);
221 }
223 if (shape->path_effect_href) {
224 g_free(shape->path_effect_href);
225 }
226 shape->path_effect_ref->detach();
228 shape->lpe_modified_connection.disconnect();
229 shape->lpe_modified_connection.~connection();
231 if (((SPObjectClass *) parent_class)->release) {
232 ((SPObjectClass *) parent_class)->release (object);
233 }
234 }
238 static void
239 sp_shape_set(SPObject *object, unsigned int key, gchar const *value)
240 {
241 SPShape *shape = (SPShape *) object;
243 switch (key) {
244 case SP_ATTR_INKSCAPE_PATH_EFFECT:
245 if ( value && shape->path_effect_href && ( strcmp(value, shape->path_effect_href) == 0 ) ) {
246 /* No change, do nothing. */
247 } else {
248 if (shape->path_effect_href) {
249 g_free(shape->path_effect_href);
250 shape->path_effect_href = NULL;
251 }
252 if (value) {
253 shape->path_effect_href = g_strdup(value);
255 // Now do the attaching, which emits the changed signal.
256 try {
257 shape->path_effect_ref->attach(Inkscape::URI(value));
258 } catch (Inkscape::BadURIException &e) {
259 g_warning("%s", e.what());
260 shape->path_effect_ref->detach();
261 }
262 } else {
263 // Detach, which emits the changed signal.
264 shape->path_effect_ref->detach();
265 }
266 }
267 break;
268 default:
269 if (((SPObjectClass *) parent_class)->set) {
270 ((SPObjectClass *) parent_class)->set(object, key, value);
271 }
272 break;
273 }
274 }
276 static Inkscape::XML::Node *
277 sp_shape_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
278 {
279 SPShape *shape = (SPShape *) object;
281 if ( shape->path_effect_href ) {
282 repr->setAttribute("inkscape:path-effect", shape->path_effect_href);
283 } else {
284 repr->setAttribute("inkscape:path-effect", NULL);
285 }
287 if (((SPObjectClass *)(parent_class))->write) {
288 ((SPObjectClass *)(parent_class))->write(object, repr, flags);
289 }
291 return repr;
292 }
294 /**
295 * Updates the shape when its attributes have changed. Also establishes
296 * marker objects to match the style settings.
297 */
298 static void
299 sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags)
300 {
301 SPItem *item = (SPItem *) object;
302 SPShape *shape = (SPShape *) object;
304 if (((SPObjectClass *) (parent_class))->update) {
305 (* ((SPObjectClass *) (parent_class))->update) (object, ctx, flags);
306 }
308 /* This stanza checks that an object's marker style agrees with
309 * the marker objects it has allocated. sp_shape_set_marker ensures
310 * that the appropriate marker objects are present (or absent) to
311 * match the style.
312 */
313 /* TODO: It would be nice if this could be done at an earlier level */
314 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
315 sp_shape_set_marker (object, i, object->style->marker[i].value);
316 }
318 if (flags & (SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
319 SPStyle *style;
320 style = SP_OBJECT_STYLE (object);
321 if (style->stroke_width.unit == SP_CSS_UNIT_PERCENT) {
322 SPItemCtx *ictx = (SPItemCtx *) ctx;
323 double const aw = 1.0 / NR::expansion(ictx->i2vp);
324 style->stroke_width.computed = style->stroke_width.value * aw;
325 for (SPItemView *v = ((SPItem *) (shape))->display; v != NULL; v = v->next) {
326 nr_arena_shape_set_style ((NRArenaShape *) v->arenaitem, style);
327 }
328 }
329 }
331 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
332 /* This is suboptimal, because changing parent style schedules recalculation */
333 /* But on the other hand - how can we know that parent does not tie style and transform */
334 NR::Maybe<NR::Rect> paintbox = SP_ITEM(object)->getBounds(NR::identity());
335 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
336 NRArenaShape * const s = NR_ARENA_SHAPE(v->arenaitem);
337 if (flags & SP_OBJECT_MODIFIED_FLAG) {
338 nr_arena_shape_set_path(s, shape->curve, (flags & SP_OBJECT_USER_MODIFIED_FLAG_B));
339 }
340 if (paintbox) {
341 s->setPaintBox(*paintbox);
342 }
343 }
344 }
346 if (sp_shape_has_markers (shape)) {
348 /* Dimension marker views */
349 for (SPItemView *v = item->display; v != NULL; v = v->next) {
351 if (!v->arenaitem->key) {
352 /* Get enough keys for all, start, mid and end marker types,
353 ** and set this view's arenaitem key to the first of these keys.
354 */
355 NR_ARENA_ITEM_SET_KEY (
356 v->arenaitem,
357 sp_item_display_key_new (SP_MARKER_LOC_QTY)
358 );
359 }
361 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
362 if (shape->marker[i]) {
363 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
364 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i - SP_MARKER_LOC,
365 sp_shape_number_of_markers (shape, i));
366 }
367 }
368 }
370 /* Update marker views */
371 for (SPItemView *v = item->display; v != NULL; v = v->next) {
372 sp_shape_update_marker_view (shape, v->arenaitem);
373 }
374 }
375 }
378 /**
379 * Works out whether a marker of a given type is required at a particular
380 * point on a shape.
381 *
382 * \param shape Shape of interest.
383 * \param m Marker type (e.g. SP_MARKER_LOC_START)
384 * \param bp Path segment.
385 * \return 1 if a marker is required here, otherwise 0.
386 */
387 bool
388 sp_shape_marker_required(SPShape const *shape, int const m, NArtBpath *bp)
389 {
390 if (shape->marker[m] == NULL) {
391 return false;
392 }
394 if (bp == SP_CURVE_BPATH(shape->curve))
395 return m == SP_MARKER_LOC_START;
396 else if (bp[1].code == NR_END)
397 return m == SP_MARKER_LOC_END;
398 else
399 return m == SP_MARKER_LOC_MID;
400 }
402 static bool
403 is_moveto(NRPathcode const c)
404 {
405 return c == NR_MOVETO || c == NR_MOVETO_OPEN;
406 }
408 /**
409 * Helper function that advances a subpath's bpath to the first subpath
410 * by checking for moveto segments.
411 *
412 * \pre The bpath[] containing bp begins with a moveto.
413 */
414 static NArtBpath const *
415 first_seg_in_subpath(NArtBpath const *bp)
416 {
417 while (!is_moveto(bp->code)) {
418 --bp;
419 }
420 return bp;
421 }
423 /**
424 * Advances the bpath to the last segment in the subpath.
425 */
426 static NArtBpath const *
427 last_seg_in_subpath(NArtBpath const *bp)
428 {
429 for(;;) {
430 ++bp;
431 switch (bp->code) {
432 case NR_MOVETO:
433 case NR_MOVETO_OPEN:
434 case NR_END:
435 --bp;
436 return bp;
438 default: continue;
439 }
440 }
441 }
444 /* A subpath begins with a moveto and ends immediately before the next moveto or NR_END.
445 * (`moveto' here means either NR_MOVETO or NR_MOVETO_OPEN.) I'm assuming that non-empty
446 * paths always begin with a moveto.
447 *
448 * The control points of the subpath are the control points of the path elements of the subpath.
449 *
450 * As usual, the control points of a moveto or NR_LINETO are {c(3)}, and
451 * the control points of a NR_CURVETO are {c(1), c(2), c(3)}.
452 * (It follows from the definition that NR_END isn't part of a subpath.)
453 *
454 * The initial control point is bpath[bi0].c(3).
455 *
456 * Reference: http://www.w3.org/TR/SVG11/painting.html#MarkerElement, the `orient' attribute.
457 * Reference for behaviour of zero-length segments:
458 * http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
459 */
461 static double const no_tangent = 128.0; /* arbitrarily-chosen value outside the range of atan2, i.e. outside of [-pi, pi]. */
463 /**
464 * Helper function to calculate the outgoing tangent of a path
465 * ( atan2(other - p0) )
466 * \pre The bpath[] containing bp0 begins with a moveto.
467 */
468 static double
469 outgoing_tangent(NArtBpath const *bp0)
470 {
471 /* See notes in comment block above. */
473 g_assert(bp0->code != NR_END);
474 NR::Point const &p0 = bp0->c(3);
475 NR::Point other;
476 for (NArtBpath const *bp = bp0;;) {
477 ++bp;
478 switch (bp->code) {
479 case NR_LINETO:
480 other = bp->c(3);
481 if (other != p0) {
482 goto found;
483 }
484 break;
486 case NR_CURVETO:
487 for (unsigned ci = 1; ci <= 3; ++ci) {
488 other = bp->c(ci);
489 if (other != p0) {
490 goto found;
491 }
492 }
493 break;
495 case NR_MOVETO_OPEN:
496 case NR_END:
497 case NR_MOVETO:
498 bp = first_seg_in_subpath(bp0);
499 if (bp == bp0) {
500 /* Gone right around the subpath without finding any different point since the
501 * initial moveto. */
502 return no_tangent;
503 }
504 if (bp->code != NR_MOVETO) {
505 /* Open subpath. */
506 return no_tangent;
507 }
508 other = bp->c(3);
509 if (other != p0) {
510 goto found;
511 }
512 break;
513 }
515 if (bp == bp0) {
516 /* Back where we started, so zero-length subpath. */
517 return no_tangent;
519 /* Note: this test must come after we've looked at element bp, in case bp0 is a curve:
520 * we must look at c(1) and c(2). (E.g. single-curve subpath.)
521 */
522 }
523 }
525 found:
526 return atan2( other - p0 );
527 }
529 /**
530 * Helper function to calculate the incoming tangent of a path
531 * ( atan2(p0 - other) )
532 *
533 * \pre The bpath[] containing bp0 begins with a moveto.
534 */
535 static double
536 incoming_tangent(NArtBpath const *bp0)
537 {
538 /* See notes in comment block before outgoing_tangent. */
540 g_assert(bp0->code != NR_END);
541 NR::Point const &p0 = bp0->c(3);
542 NR::Point other;
543 for (NArtBpath const *bp = bp0;;) {
544 switch (bp->code) {
545 case NR_LINETO:
546 other = bp->c(3);
547 if (other != p0) {
548 goto found;
549 }
550 --bp;
551 break;
553 case NR_CURVETO:
554 for (unsigned ci = 3; ci != 0; --ci) {
555 other = bp->c(ci);
556 if (other != p0) {
557 goto found;
558 }
559 }
560 --bp;
561 break;
563 case NR_MOVETO:
564 case NR_MOVETO_OPEN:
565 other = bp->c(3);
566 if (other != p0) {
567 goto found;
568 }
569 if (bp->code != NR_MOVETO) {
570 /* Open subpath. */
571 return no_tangent;
572 }
573 bp = last_seg_in_subpath(bp0);
574 break;
576 default: /* includes NR_END */
577 g_error("Found invalid path code %u in middle of path.", bp->code);
578 return no_tangent;
579 }
581 if (bp == bp0) {
582 /* Back where we started from: zero-length subpath. */
583 return no_tangent;
584 }
585 }
587 found:
588 return atan2( p0 - other );
589 }
592 /**
593 * Calculate the transform required to get a marker's path object in the
594 * right place for particular path segment on a shape. You should
595 * call sp_shape_marker_required first to see if a marker is required
596 * at this point.
597 *
598 * \see sp_shape_marker_required.
599 *
600 * \param shape Shape which the marker is for.
601 * \param m Marker type (e.g. SP_MARKER_LOC_START)
602 * \param bp Path segment which the arrow is for.
603 * \return Transform matrix.
604 */
605 NR::Matrix
606 sp_shape_marker_get_transform(SPShape const *shape, NArtBpath const *bp)
607 {
608 g_return_val_if_fail(( is_moveto(SP_CURVE_BPATH(shape->curve)[0].code)
609 && ( 0 < shape->curve->end )
610 && ( SP_CURVE_BPATH(shape->curve)[shape->curve->end].code == NR_END ) ),
611 NR::Matrix(NR::translate(bp->c(3))));
612 double const angle1 = incoming_tangent(bp);
613 double const angle2 = outgoing_tangent(bp);
615 /* angle1 and angle2 are now each either unset (i.e. still 100 from their initialization) or in
616 [-pi, pi] from atan2. */
617 g_assert((-3.15 < angle1 && angle1 < 3.15) || (angle1 == no_tangent));
618 g_assert((-3.15 < angle2 && angle2 < 3.15) || (angle2 == no_tangent));
620 double ret_angle;
621 if (angle1 == no_tangent) {
622 /* First vertex of an open subpath. */
623 ret_angle = ( angle2 == no_tangent
624 ? 0.
625 : angle2 );
626 } else if (angle2 == no_tangent) {
627 /* Last vertex of an open subpath. */
628 ret_angle = angle1;
629 } else {
630 ret_angle = .5 * (angle1 + angle2);
632 if ( fabs( angle2 - angle1 ) > M_PI ) {
633 /* ret_angle is in the middle of the larger of the two sectors between angle1 and
634 * angle2, so flip it by 180degrees to force it to the middle of the smaller sector.
635 *
636 * (Imagine a circle with rays drawn at angle1 and angle2 from the centre of the
637 * circle. Those two rays divide the circle into two sectors.)
638 */
639 ret_angle += M_PI;
640 }
641 }
643 return NR::Matrix(NR::rotate(ret_angle)) * NR::translate(bp->c(3));
644 }
646 /**
647 * Updates the instances (views) of a given marker in a shape.
648 * Marker views have to be scaled already. The transformation
649 * is retrieved and then shown by calling sp_marker_show_instance.
650 */
651 static void
652 sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai)
653 {
654 SPStyle *style = ((SPObject *) shape)->style;
656 for (int i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
657 if (shape->marker[i] == NULL) {
658 continue;
659 }
661 int n = 0;
663 for (NArtBpath *bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
664 if (sp_shape_marker_required (shape, i, bp)) {
665 NR::Matrix const m(sp_shape_marker_get_transform(shape, bp));
666 sp_marker_show_instance ((SPMarker* ) shape->marker[i], ai,
667 NR_ARENA_ITEM_GET_KEY(ai) + i, n, m,
668 style->stroke_width.computed);
669 n++;
670 }
671 }
672 }
673 }
675 /**
676 * Sets modified flag for all sub-item views.
677 */
678 static void
679 sp_shape_modified (SPObject *object, unsigned int flags)
680 {
681 SPShape *shape = SP_SHAPE (object);
683 if (((SPObjectClass *) (parent_class))->modified) {
684 (* ((SPObjectClass *) (parent_class))->modified) (object, flags);
685 }
687 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
688 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
689 nr_arena_shape_set_style (NR_ARENA_SHAPE (v->arenaitem), object->style);
690 }
691 }
692 }
694 /**
695 * Calculates the bounding box for item, storing it into bbox.
696 * This also includes the bounding boxes of any markers included in the shape.
697 */
698 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
699 {
700 SPShape const *shape = SP_SHAPE (item);
702 if (shape->curve) {
704 NRRect cbbox;
705 NRBPath bp;
707 bp.path = SP_CURVE_BPATH (shape->curve);
709 cbbox.x0 = cbbox.y0 = NR_HUGE;
710 cbbox.x1 = cbbox.y1 = -NR_HUGE;
712 nr_path_matrix_bbox_union(&bp, transform, &cbbox);
714 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
716 SPStyle* style=SP_OBJECT_STYLE (item);
717 if (!style->stroke.isNone()) {
718 double const scale = expansion(transform);
719 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
720 double const width = MAX(0.125, style->stroke_width.computed * scale);
721 if ( fabs(cbbox.x1-cbbox.x0) > -0.00001 && fabs(cbbox.y1-cbbox.y0) > -0.00001 ) {
722 cbbox.x0-=0.5*width;
723 cbbox.x1+=0.5*width;
724 cbbox.y0-=0.5*width;
725 cbbox.y1+=0.5*width;
726 }
727 }
728 }
730 // Union with bboxes of the markers, if any
731 if (sp_shape_has_markers (shape)) {
732 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
733 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
734 if (sp_shape_marker_required (shape, m, bp)) {
736 SPMarker* marker = SP_MARKER (shape->marker[m]);
737 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
739 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
741 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
742 tr = NR::scale(style->stroke_width.computed) * tr;
743 }
745 // total marker transform
746 tr = marker_item->transform * marker->c2p * tr * transform;
748 // get bbox of the marker with that transform
749 NRRect marker_bbox;
750 sp_item_invoke_bbox (marker_item, &marker_bbox, tr, true);
751 // union it with the shape bbox
752 nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
753 }
754 }
755 }
756 }
757 }
759 // copy our bbox to the variable we're given
760 *bbox = cbbox;
761 }
762 }
764 /**
765 * Prepares shape for printing. Handles printing of comments for printing
766 * debugging, sizes the item to fit into the document width/height,
767 * applies print fill/stroke, sets transforms for markers, and adds
768 * comment labels.
769 */
770 void
771 sp_shape_print (SPItem *item, SPPrintContext *ctx)
772 {
773 NRRect pbox, dbox, bbox;
775 SPShape *shape = SP_SHAPE(item);
777 if (!shape->curve) return;
779 gint add_comments = prefs_get_int_attribute_limited ("printing.debug", "add-label-comments", 0, 0, 1);
780 if (add_comments) {
781 gchar * comment = g_strdup_printf("begin '%s'",
782 SP_OBJECT(item)->defaultLabel());
783 sp_print_comment(ctx, comment);
784 g_free(comment);
785 }
787 /* fixme: Think (Lauris) */
788 sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
789 dbox.x0 = 0.0;
790 dbox.y0 = 0.0;
791 dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
792 dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
793 sp_item_bbox_desktop (item, &bbox);
794 NR::Matrix const i2d = sp_item_i2d_affine(item);
796 SPStyle* style = SP_OBJECT_STYLE (item);
798 if (!style->fill.isNone()) {
799 NRBPath bp;
800 bp.path = SP_CURVE_BPATH(shape->curve);
801 sp_print_fill (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
802 }
804 if (!style->stroke.isNone()) {
805 NRBPath bp;
806 bp.path = SP_CURVE_BPATH(shape->curve);
807 sp_print_stroke (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
808 }
810 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
811 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
812 if (sp_shape_marker_required (shape, m, bp)) {
814 SPMarker* marker = SP_MARKER (shape->marker[m]);
815 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
817 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
819 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
820 tr = NR::scale(style->stroke_width.computed) * tr;
821 }
823 tr = marker_item->transform * marker->c2p * tr;
825 NR::Matrix old_tr = marker_item->transform;
826 marker_item->transform = tr;
827 sp_item_invoke_print (marker_item, ctx);
828 marker_item->transform = old_tr;
829 }
830 }
831 }
833 if (add_comments) {
834 gchar * comment = g_strdup_printf("end '%s'",
835 SP_OBJECT(item)->defaultLabel());
836 sp_print_comment(ctx, comment);
837 g_free(comment);
838 }
839 }
841 /**
842 * Sets style, path, and paintbox. Updates marker views, including dimensions.
843 */
844 static NRArenaItem *
845 sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
846 {
847 SPObject *object = SP_OBJECT(item);
848 SPShape *shape = SP_SHAPE(item);
850 NRArenaItem *arenaitem = NRArenaShape::create(arena);
851 NRArenaShape * const s = NR_ARENA_SHAPE(arenaitem);
852 nr_arena_shape_set_style(s, object->style);
853 nr_arena_shape_set_path(s, shape->curve, false);
854 NR::Maybe<NR::Rect> paintbox = item->getBounds(NR::identity());
855 if (paintbox) {
856 s->setPaintBox(*paintbox);
857 }
859 if (sp_shape_has_markers (shape)) {
861 /* Dimension the marker views */
862 if (!arenaitem->key) {
863 NR_ARENA_ITEM_SET_KEY (arenaitem, sp_item_display_key_new (SP_MARKER_LOC_QTY));
864 }
866 for (int i = 0; i < SP_MARKER_LOC_QTY; i++) {
867 if (shape->marker[i]) {
868 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
869 NR_ARENA_ITEM_GET_KEY (arenaitem) + i - SP_MARKER_LOC,
870 sp_shape_number_of_markers (shape, i));
871 }
872 }
875 /* Update marker views */
876 sp_shape_update_marker_view (shape, arenaitem);
877 }
879 return arenaitem;
880 }
882 /**
883 * Hides/removes marker views from the shape.
884 */
885 static void
886 sp_shape_hide (SPItem *item, unsigned int key)
887 {
888 SPShape *shape;
889 SPItemView *v;
890 int i;
892 shape = (SPShape *) item;
894 for (i=0; i<SP_MARKER_LOC_QTY; i++) {
895 if (shape->marker[i]) {
896 for (v = item->display; v != NULL; v = v->next) {
897 if (key == v->key) {
898 sp_marker_hide ((SPMarker *) shape->marker[i],
899 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
900 }
901 }
902 }
903 }
905 if (((SPItemClass *) parent_class)->hide) {
906 ((SPItemClass *) parent_class)->hide (item, key);
907 }
908 }
910 /**
911 * \param shape Shape.
912 * \return TRUE if the shape has any markers, or FALSE if not.
913 */
914 int
915 sp_shape_has_markers (SPShape const *shape)
916 {
917 /* Note, we're ignoring 'marker' settings, which technically should apply for
918 all three settings. This should be fixed later such that if 'marker' is
919 specified, then all three should appear. */
921 return (
922 shape->curve &&
923 (shape->marker[SP_MARKER_LOC_START] ||
924 shape->marker[SP_MARKER_LOC_MID] ||
925 shape->marker[SP_MARKER_LOC_END])
926 );
927 }
930 /**
931 * \param shape Shape.
932 * \param type Marker type (e.g. SP_MARKER_LOC_START)
933 * \return Number of markers that the shape has of this type.
934 */
935 int
936 sp_shape_number_of_markers (SPShape *shape, int type)
937 {
938 int n = 0;
939 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
940 if (sp_shape_marker_required (shape, type, bp)) {
941 n++;
942 }
943 }
945 return n;
946 }
948 /**
949 * Checks if the given marker is used in the shape, and if so, it
950 * releases it by calling sp_marker_hide. Also detaches signals
951 * and unrefs the marker from the shape.
952 */
953 static void
954 sp_shape_marker_release (SPObject *marker, SPShape *shape)
955 {
956 SPItem *item;
957 int i;
959 item = (SPItem *) shape;
961 for (i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
962 if (marker == shape->marker[i]) {
963 SPItemView *v;
964 /* Hide marker */
965 for (v = item->display; v != NULL; v = v->next) {
966 sp_marker_hide ((SPMarker *) (shape->marker[i]), NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
967 /* fixme: Do we need explicit remove here? (Lauris) */
968 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
969 }
970 /* Detach marker */
971 sp_signal_disconnect_by_data (shape->marker[i], item);
972 shape->marker[i] = sp_object_hunref (shape->marker[i], item);
973 }
974 }
975 }
977 /**
978 * No-op. Exists for handling 'modified' messages
979 */
980 static void
981 sp_shape_marker_modified (SPObject *marker, guint flags, SPItem *item)
982 {
983 /* I think mask does update automagically */
984 /* g_warning ("Item %s mask %s modified", SP_OBJECT_ID (item), SP_OBJECT_ID (mask)); */
985 }
987 /**
988 * Adds a new marker to shape object at the location indicated by key. value
989 * must be a valid URI reference resolvable from the shape object (i.e., present
990 * in the document <defs>). If the shape object already has a marker
991 * registered at the given position, it is removed first. Then the
992 * new marker is hrefed and its signals connected.
993 */
994 void
995 sp_shape_set_marker (SPObject *object, unsigned int key, const gchar *value)
996 {
997 SPItem *item = (SPItem *) object;
998 SPShape *shape = (SPShape *) object;
1000 if (key < SP_MARKER_LOC_START || key > SP_MARKER_LOC_END) {
1001 return;
1002 }
1004 SPObject *mrk = sp_css_uri_reference_resolve (SP_OBJECT_DOCUMENT (object), value);
1005 if (mrk != shape->marker[key]) {
1006 if (shape->marker[key]) {
1007 SPItemView *v;
1009 /* Detach marker */
1010 shape->release_connect[key].disconnect();
1011 shape->modified_connect[key].disconnect();
1013 /* Hide marker */
1014 for (v = item->display; v != NULL; v = v->next) {
1015 sp_marker_hide ((SPMarker *) (shape->marker[key]),
1016 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + key);
1017 /* fixme: Do we need explicit remove here? (Lauris) */
1018 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
1019 }
1021 /* Unref marker */
1022 shape->marker[key] = sp_object_hunref (shape->marker[key], object);
1023 }
1024 if (SP_IS_MARKER (mrk)) {
1025 shape->marker[key] = sp_object_href (mrk, object);
1026 shape->release_connect[key] = mrk->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_shape_marker_release), shape));
1027 shape->modified_connect[key] = mrk->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_shape_marker_modified), shape));
1028 }
1029 }
1030 }
1034 /* Shape section */
1036 /**
1037 * Calls any registered handlers for the set_shape action
1038 */
1039 void
1040 sp_shape_set_shape (SPShape *shape)
1041 {
1042 g_return_if_fail (shape != NULL);
1043 g_return_if_fail (SP_IS_SHAPE (shape));
1045 if (SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape) {
1046 SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape (shape);
1047 }
1048 }
1050 /**
1051 * Adds a curve to the shape. If owner is specified, a reference
1052 * will be made, otherwise the curve will be copied into the shape.
1053 * Any existing curve in the shape will be unreferenced first.
1054 * This routine also triggers a request to update the display.
1055 */
1056 void
1057 sp_shape_set_curve (SPShape *shape, SPCurve *curve, unsigned int owner)
1058 {
1059 if (shape->curve) {
1060 shape->curve = sp_curve_unref (shape->curve);
1061 }
1062 if (curve) {
1063 if (owner) {
1064 shape->curve = sp_curve_ref (curve);
1065 } else {
1066 shape->curve = sp_curve_copy (curve);
1067 }
1068 }
1069 SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1070 }
1072 /**
1073 * Return duplicate of curve (if any exists) or NULL if there is no curve
1074 */
1075 SPCurve *
1076 sp_shape_get_curve (SPShape *shape)
1077 {
1078 if (shape->curve) {
1079 return sp_curve_copy (shape->curve);
1080 }
1081 return NULL;
1082 }
1084 /* NOT FOR GENERAL PUBLIC UNTIL SORTED OUT (Lauris) */
1085 void
1086 sp_shape_set_curve_insync (SPShape *shape, SPCurve *curve, unsigned int owner)
1087 {
1088 if (shape->curve) {
1089 shape->curve = sp_curve_unref (shape->curve);
1090 }
1091 if (curve) {
1092 if (owner) {
1093 shape->curve = sp_curve_ref (curve);
1094 } else {
1095 shape->curve = sp_curve_copy (curve);
1096 }
1097 }
1098 }
1100 /**
1101 * Sets the snappoint p to the end point of the path segment
1102 */
1103 static void sp_shape_snappoints(SPItem const *item, SnapPointsIter p)
1104 {
1105 g_assert(item != NULL);
1106 g_assert(SP_IS_SHAPE(item));
1108 SPShape const *shape = SP_SHAPE(item);
1109 if (shape->curve == NULL) {
1110 return;
1111 }
1113 NR::Matrix const i2d (sp_item_i2d_affine (item));
1115 /* Use the end points of each segment of the path */
1116 NArtBpath const *bp = SP_CURVE_BPATH(shape->curve);
1118 if (bp->code == NR_MOVETO) { // Indicates the start of a closed subpath, see nr-path-code.h
1119 bp++; //The first point of a closed path is coincident with the end point. Skip the first point as we need only one
1120 }
1122 while (bp->code != NR_END) {
1123 *p = bp->c(3) * i2d;
1124 bp++;
1125 }
1126 }
1129 LivePathEffectObject *
1130 sp_shape_get_livepatheffectobject(SPShape *shape) {
1131 if (!shape) return NULL;
1133 if (sp_shape_has_path_effect(shape)) {
1134 return shape->path_effect_ref->lpeobject;
1135 } else {
1136 return NULL;
1137 }
1138 }
1140 /**
1141 * Calls any registered handlers for the update_patheffect action
1142 */
1143 void
1144 sp_shape_update_patheffect (SPShape *shape, bool write)
1145 {
1146 #ifdef SHAPE_VERBOSE
1147 g_message("sp_shape_update_patheffect: %p\n", shape);
1148 #endif
1149 g_return_if_fail (shape != NULL);
1150 g_return_if_fail (SP_IS_SHAPE (shape));
1152 if (SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->update_patheffect) {
1153 SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->update_patheffect (shape, write);
1154 }
1155 }
1157 void sp_shape_perform_path_effect(SPCurve *curve, SPShape *shape) {
1158 if (!shape) return;
1159 if (!curve) return;
1161 LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(shape);
1162 if (lpeobj && lpeobj->lpe) {
1163 lpeobj->lpe->doEffect(curve);
1164 }
1165 }
1167 /**
1168 * Gets called when (re)attached to another lpeobject.
1169 */
1170 static void
1171 lpeobject_ref_changed(SPObject *old_ref, SPObject *ref, SPShape *shape)
1172 {
1173 if (old_ref) {
1174 sp_signal_disconnect_by_data(old_ref, shape);
1175 }
1176 if ( IS_LIVEPATHEFFECT(ref) && ref != shape )
1177 {
1178 shape->lpe_modified_connection.disconnect();
1179 shape->lpe_modified_connection = ref->connectModified(sigc::bind(sigc::ptr_fun(&lpeobject_ref_modified), shape));
1180 lpeobject_ref_modified(ref, 0, shape);
1181 }
1182 }
1184 /**
1185 * Gets called when lpeobject repr contents change: i.e. parameter change.
1186 */
1187 static void
1188 lpeobject_ref_modified(SPObject *href, guint flags, SPShape *shape)
1189 {
1190 sp_shape_update_patheffect (shape, true);
1191 }
1193 void sp_shape_set_path_effect(SPShape *shape, gchar *value)
1194 {
1195 if (!value) {
1196 sp_shape_remove_path_effect(shape);
1197 } else {
1198 SP_OBJECT_REPR(shape)->setAttribute("inkscape:path-effect", value);
1199 }
1200 }
1202 void sp_shape_remove_path_effect(SPShape *shape)
1203 {
1204 Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
1205 repr->setAttribute("inkscape:path-effect", NULL);
1206 if (SP_IS_PATH(shape)) {
1207 repr->setAttribute("d", repr->attribute("inkscape:original-d"));
1208 repr->setAttribute("inkscape:original-d", NULL);
1209 }
1210 }
1212 bool sp_shape_has_path_effect(SPShape *shape)
1213 {
1214 return (shape->path_effect_href != NULL);
1215 }
1217 /*
1218 Local Variables:
1219 mode:c++
1220 c-file-style:"stroustrup"
1221 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1222 indent-tabs-mode:nil
1223 fill-column:99
1224 End:
1225 */
1226 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :