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