Code

1) solve bug #177995
[inkscape.git] / src / sp-shape.cpp
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 #include "util/mathfns.h" // for triangle_area()
51 #define noSHAPE_VERBOSE
53 static void sp_shape_class_init (SPShapeClass *klass);
54 static void sp_shape_init (SPShape *shape);
55 static void sp_shape_finalize (GObject *object);
57 static void sp_shape_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
58 static void sp_shape_release (SPObject *object);
60 static void sp_shape_set(SPObject *object, unsigned key, gchar const *value);
61 static void sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags);
62 static void sp_shape_modified (SPObject *object, unsigned int flags);
63 static Inkscape::XML::Node *sp_shape_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
65 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
66 void sp_shape_print (SPItem * item, SPPrintContext * ctx);
67 static NRArenaItem *sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
68 static void sp_shape_hide (SPItem *item, unsigned int key);
69 static void sp_shape_snappoints (SPItem const *item, SnapPointsIter p);
71 static void sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai);
73 static void lpeobject_ref_changed(SPObject *old_ref, SPObject *ref, SPShape *shape);
74 static void lpeobject_ref_modified(SPObject *href, guint flags, SPShape *shape);
76 static SPItemClass *parent_class;
78 /**
79  * Registers the SPShape class with Gdk and returns its type number.
80  */
81 GType
82 sp_shape_get_type (void)
83 {
84         static GType type = 0;
85         if (!type) {
86                 GTypeInfo info = {
87                         sizeof (SPShapeClass),
88                         NULL, NULL,
89                         (GClassInitFunc) sp_shape_class_init,
90                         NULL, NULL,
91                         sizeof (SPShape),
92                         16,
93                         (GInstanceInitFunc) sp_shape_init,
94                         NULL,   /* value_table */
95                 };
96                 type = g_type_register_static (SP_TYPE_ITEM, "SPShape", &info, (GTypeFlags)0);
97         }
98         return type;
99 }
101 /**
102  * Initializes a SPShapeClass object.  Establishes the function pointers to the class'
103  * member routines in the class vtable, and sets pointers to parent classes.
104  */
105 static void
106 sp_shape_class_init (SPShapeClass *klass)
108     GObjectClass *gobject_class;
109         SPObjectClass *sp_object_class;
110         SPItemClass * item_class;
111         SPPathClass * path_class;
113     gobject_class = (GObjectClass *) klass;
114         sp_object_class = (SPObjectClass *) klass;
115         item_class = (SPItemClass *) klass;
116         path_class = (SPPathClass *) klass;
118         parent_class = (SPItemClass *)g_type_class_peek_parent (klass);
120     gobject_class->finalize = sp_shape_finalize;
122         sp_object_class->build = sp_shape_build;
123         sp_object_class->release = sp_shape_release;
124     sp_object_class->set = sp_shape_set;
125         sp_object_class->update = sp_shape_update;
126         sp_object_class->modified = sp_shape_modified;
127     sp_object_class->write = sp_shape_write;
129         item_class->bbox = sp_shape_bbox;
130         item_class->print = sp_shape_print;
131         item_class->show = sp_shape_show;
132         item_class->hide = sp_shape_hide;
133     item_class->snappoints = sp_shape_snappoints;
135     klass->set_shape = NULL;
136     klass->update_patheffect = NULL;
139 /**
140  * Initializes an SPShape object.
141  */
142 static void
143 sp_shape_init (SPShape *shape)
145     shape->path_effect_href = NULL;
146     shape->path_effect_ref  = new Inkscape::LivePathEffect::LPEObjectReference(SP_OBJECT(shape));
147         new (&shape->lpe_modified_connection) sigc::connection();
149     for ( int i = 0 ; i < SP_MARKER_LOC_QTY ; i++ ) {
150         new (&shape->release_connect[i]) sigc::connection();
151         new (&shape->modified_connect[i]) sigc::connection();
152     }
155 static void
156 sp_shape_finalize (GObject *object)
158     SPShape *shape=(SPShape *)object;
160     for ( int i = 0 ; i < SP_MARKER_LOC_QTY ; i++ ) {
161         shape->release_connect[i].disconnect();
162         shape->release_connect[i].~connection();
163         shape->modified_connect[i].disconnect();
164         shape->modified_connect[i].~connection();
165     }
167     if (((GObjectClass *) (parent_class))->finalize) {
168         (* ((GObjectClass *) (parent_class))->finalize)(object);
169     }
172 /**
173  * Virtual build callback for SPMarker.
174  *
175  * This is to be invoked immediately after creation of an SPShape.
176  *
177  * \see sp_object_build()
178  */
179 static void
180 sp_shape_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
182     SP_SHAPE(object)->path_effect_ref->changedSignal().connect(sigc::bind(sigc::ptr_fun(lpeobject_ref_changed), SP_SHAPE(object)));
184     sp_object_read_attr(object, "inkscape:path-effect");
185  
186     if (((SPObjectClass *) (parent_class))->build) {
187        (*((SPObjectClass *) (parent_class))->build) (object, document, repr);
188     }
191 /**
192  * Removes, releases and unrefs all children of object
193  *
194  * This is the inverse of sp_shape_build().  It must be invoked as soon
195  * as the shape is removed from the tree, even if it is still referenced
196  * by other objects.  This routine also disconnects/unrefs markers and
197  * curves attached to it.
198  *
199  * \see sp_object_release()
200  */
201 static void
202 sp_shape_release (SPObject *object)
204         SPItem *item;
205         SPShape *shape;
206         SPItemView *v;
207         int i;
209         item = (SPItem *) object;
210         shape = (SPShape *) object;
212         for (i=SP_MARKER_LOC_START; i<SP_MARKER_LOC_QTY; i++) {
213           if (shape->marker[i]) {
214             sp_signal_disconnect_by_data (shape->marker[i], object);
215             for (v = item->display; v != NULL; v = v->next) {
216               sp_marker_hide ((SPMarker *) shape->marker[i], NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
217             }
218             shape->marker[i] = sp_object_hunref (shape->marker[i], object);
219           }
220         }
221         if (shape->curve) {
222                 shape->curve = sp_curve_unref (shape->curve);
223         }
225     if (shape->path_effect_href) {
226         g_free(shape->path_effect_href);
227     }
228     shape->path_effect_ref->detach();
230     shape->lpe_modified_connection.disconnect();
231     shape->lpe_modified_connection.~connection();
232     
233         if (((SPObjectClass *) parent_class)->release) {
234           ((SPObjectClass *) parent_class)->release (object);
235         }
240 static void
241 sp_shape_set(SPObject *object, unsigned int key, gchar const *value)
243     SPShape *shape = (SPShape *) object;
245     switch (key) {
246         case SP_ATTR_INKSCAPE_PATH_EFFECT:
247             if ( value && shape->path_effect_href && ( strcmp(value, shape->path_effect_href) == 0 ) ) {
248                 /* No change, do nothing. */
249             } else {
250                 if (shape->path_effect_href) {
251                     g_free(shape->path_effect_href);
252                     shape->path_effect_href = NULL;
253                 }
254                 if (value) {
255                     shape->path_effect_href = g_strdup(value);
257                     // Now do the attaching, which emits the changed signal.
258                     try {
259                         shape->path_effect_ref->attach(Inkscape::URI(value));
260                     } catch (Inkscape::BadURIException &e) {
261                         g_warning("%s", e.what());
262                         shape->path_effect_ref->detach();
263                     }
264                 } else {
265                     // Detach, which emits the changed signal.
266                     shape->path_effect_ref->detach();
267                 }
268             }
269             break;
270         default:
271             if (((SPObjectClass *) parent_class)->set) {
272                 ((SPObjectClass *) parent_class)->set(object, key, value);
273             }
274             break;
275     }
278 static Inkscape::XML::Node *
279 sp_shape_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
281     SPShape *shape = (SPShape *) object;
283     if ( shape->path_effect_href ) {
284         repr->setAttribute("inkscape:path-effect", shape->path_effect_href);
285     } else {
286         repr->setAttribute("inkscape:path-effect", NULL);
287     }
289     if (((SPObjectClass *)(parent_class))->write) {
290         ((SPObjectClass *)(parent_class))->write(object, repr, flags);
291     }
293     return repr;
296 /** 
297  * Updates the shape when its attributes have changed.  Also establishes
298  * marker objects to match the style settings.  
299  */
300 static void
301 sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags)
303     SPItem *item = (SPItem *) object;
304     SPShape *shape = (SPShape *) object;
306         if (((SPObjectClass *) (parent_class))->update) {
307           (* ((SPObjectClass *) (parent_class))->update) (object, ctx, flags);
308         }
310         /* This stanza checks that an object's marker style agrees with
311          * the marker objects it has allocated.  sp_shape_set_marker ensures
312          * that the appropriate marker objects are present (or absent) to
313          * match the style.
314          */
315         /* TODO:  It would be nice if this could be done at an earlier level */
316         for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
317             sp_shape_set_marker (object, i, object->style->marker[i].value);
318           }
320         if (flags & (SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
321                 SPStyle *style;
322                 style = SP_OBJECT_STYLE (object);
323                 if (style->stroke_width.unit == SP_CSS_UNIT_PERCENT) {
324                         SPItemCtx *ictx = (SPItemCtx *) ctx;
325                         double const aw = 1.0 / NR::expansion(ictx->i2vp);
326                         style->stroke_width.computed = style->stroke_width.value * aw;
327                         for (SPItemView *v = ((SPItem *) (shape))->display; v != NULL; v = v->next) {
328                                 nr_arena_shape_set_style ((NRArenaShape *) v->arenaitem, style);
329                         }
330                 }
331         }
333         if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
334                 /* This is suboptimal, because changing parent style schedules recalculation */
335                 /* But on the other hand - how can we know that parent does not tie style and transform */
336                 NR::Maybe<NR::Rect> paintbox = SP_ITEM(object)->getBounds(NR::identity());
337                 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
338                     NRArenaShape * const s = NR_ARENA_SHAPE(v->arenaitem);
339                     if (flags & SP_OBJECT_MODIFIED_FLAG) {
340                         nr_arena_shape_set_path(s, shape->curve, (flags & SP_OBJECT_USER_MODIFIED_FLAG_B));
341                     }
342                     if (paintbox) {
343                         s->setPaintBox(*paintbox);
344                     }
345                 }
346         }
348         if (sp_shape_has_markers (shape)) {
350             /* Dimension marker views */
351             for (SPItemView *v = item->display; v != NULL; v = v->next) {
353                 if (!v->arenaitem->key) {
354                     /* Get enough keys for all, start, mid and end marker types,
355                     ** and set this view's arenaitem key to the first of these keys.
356                     */
357                     NR_ARENA_ITEM_SET_KEY (
358                         v->arenaitem,
359                         sp_item_display_key_new (SP_MARKER_LOC_QTY)
360                         );
361                 }
363                 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
364                     if (shape->marker[i]) {
365                         sp_marker_show_dimension ((SPMarker *) shape->marker[i],
366                                                   NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i - SP_MARKER_LOC,
367                                                   sp_shape_number_of_markers (shape, i));
368                     }
369                 }
370             }
372             /* Update marker views */
373             for (SPItemView *v = item->display; v != NULL; v = v->next) {
374                 sp_shape_update_marker_view (shape, v->arenaitem);
375             }
376         }
380 /**
381 * Works out whether a marker of a given type is required at a particular
382 * point on a shape.
384 * \param shape Shape of interest.
385 * \param m Marker type (e.g. SP_MARKER_LOC_START)
386 * \param bp Path segment.
387 * \return 1 if a marker is required here, otherwise 0.
388 */
389 bool
390 sp_shape_marker_required(SPShape const *shape, int const m, NArtBpath *bp)
392     if (shape->marker[m] == NULL) {
393         return false;
394     }
396     if (bp == SP_CURVE_BPATH(shape->curve))
397         return m == SP_MARKER_LOC_START;
398     else if (bp[1].code == NR_END)
399         return m == SP_MARKER_LOC_END;
400     else
401         return m == SP_MARKER_LOC_MID;
404 static bool
405 is_moveto(NRPathcode const c)
407     return c == NR_MOVETO || c == NR_MOVETO_OPEN;
410 /** 
411  * Helper function that advances a subpath's bpath to the first subpath
412  * by checking for moveto segments.
413  *
414  * \pre The bpath[] containing bp begins with a moveto. 
415  */
416 static NArtBpath const *
417 first_seg_in_subpath(NArtBpath const *bp)
419     while (!is_moveto(bp->code)) {
420         --bp;
421     }
422     return bp;
425 /**
426  * Advances the bpath to the last segment in the subpath.
427  */
428 static NArtBpath const *
429 last_seg_in_subpath(NArtBpath const *bp)
431     for(;;) {
432         ++bp;
433         switch (bp->code) {
434             case NR_MOVETO:
435             case NR_MOVETO_OPEN:
436             case NR_END:
437                 --bp;
438                 return bp;
440             default: continue;
441         }
442     }
446 /* A subpath begins with a moveto and ends immediately before the next moveto or NR_END.
447  * (`moveto' here means either NR_MOVETO or NR_MOVETO_OPEN.)  I'm assuming that non-empty
448  * paths always begin with a moveto.
449  *
450  * The control points of the subpath are the control points of the path elements of the subpath.
451  *
452  * As usual, the control points of a moveto or NR_LINETO are {c(3)}, and
453  * the control points of a NR_CURVETO are {c(1), c(2), c(3)}.
454  * (It follows from the definition that NR_END isn't part of a subpath.)
455  *
456  * The initial control point is bpath[bi0].c(3).
457  *
458  * Reference: http://www.w3.org/TR/SVG11/painting.html#MarkerElement, the `orient' attribute.
459  * Reference for behaviour of zero-length segments:
460  * http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
461  */
463 static double const no_tangent = 128.0;  /* arbitrarily-chosen value outside the range of atan2, i.e. outside of [-pi, pi]. */
465 /**
466  * Helper function to calculate the outgoing tangent of a path 
467  * ( atan2(other - p0) )
468  * \pre The bpath[] containing bp0 begins with a moveto. 
469  */
470 static double
471 outgoing_tangent(NArtBpath const *bp0)
473     /* See notes in comment block above. */
475     g_assert(bp0->code != NR_END);
476     NR::Point const &p0 = bp0->c(3);
477     NR::Point other;
478     for (NArtBpath const *bp = bp0;;) {
479         ++bp;
480         switch (bp->code) {
481             case NR_LINETO:
482                 other = bp->c(3);
483                 if (other != p0) {
484                     goto found;
485                 }
486                 break;
488             case NR_CURVETO:
489                 for (unsigned ci = 1; ci <= 3; ++ci) {
490                     other = bp->c(ci);
491                     if (other != p0) {
492                         goto found;
493                     }
494                 }
495                 break;
497             case NR_MOVETO_OPEN:
498             case NR_END:
499             case NR_MOVETO:
500                 bp = first_seg_in_subpath(bp0);
501                 if (bp == bp0) {
502                     /* Gone right around the subpath without finding any different point since the
503                      * initial moveto. */
504                     return no_tangent;
505                 }
506                 if (bp->code != NR_MOVETO) {
507                     /* Open subpath. */
508                     return no_tangent;
509                 }
510                 other = bp->c(3);
511                 if (other != p0) {
512                     goto found;
513                 }
514                 break;
515         }
517         if (bp == bp0) {
518             /* Back where we started, so zero-length subpath. */
519             return no_tangent;
521             /* Note: this test must come after we've looked at element bp, in case bp0 is a curve:
522              * we must look at c(1) and c(2).  (E.g. single-curve subpath.)
523              */
524         }
525     }
527 found:
528     return atan2( other - p0 );
531 /**
532  * Helper function to calculate the incoming tangent of a path
533  * ( atan2(p0 - other) )
534  * 
535  * \pre The bpath[] containing bp0 begins with a moveto. 
536  */
537 static double
538 incoming_tangent(NArtBpath const *bp0)
540     /* See notes in comment block before outgoing_tangent. */
542     g_assert(bp0->code != NR_END);
543     NR::Point const &p0 = bp0->c(3);
544     NR::Point other;
545     for (NArtBpath const *bp = bp0;;) {
546         switch (bp->code) {
547             case NR_LINETO:
548                 other = bp->c(3);
549                 if (other != p0) {
550                     goto found;
551                 }
552                 --bp;
553                 break;
555             case NR_CURVETO:
556                 for (unsigned ci = 3; ci != 0; --ci) {
557                     other = bp->c(ci);
558                     if (other != p0) {
559                         goto found;
560                     }
561                 }
562                 --bp;
563                 break;
565             case NR_MOVETO:
566             case NR_MOVETO_OPEN:
567                 other = bp->c(3);
568                 if (other != p0) {
569                     goto found;
570                 }
571                 if (bp->code != NR_MOVETO) {
572                     /* Open subpath. */
573                     return no_tangent;
574                 }
575                 bp = last_seg_in_subpath(bp0);
576                 break;
578             default: /* includes NR_END */
579                 g_error("Found invalid path code %u in middle of path.", bp->code);
580                 return no_tangent;
581         }
583         if (bp == bp0) {
584             /* Back where we started from: zero-length subpath. */
585             return no_tangent;
586         }
587     }
589 found:
590     return atan2( p0 - other );
594 /**
595  * Calculate the transform required to get a marker's path object in the
596  * right place for particular path segment on a shape.  You should
597  * call sp_shape_marker_required first to see if a marker is required
598  * at this point.
599  *
600  * \see sp_shape_marker_required.
601  *
602  * \param shape Shape which the marker is for.
603  * \param m Marker type (e.g. SP_MARKER_LOC_START)
604  * \param bp Path segment which the arrow is for.
605  * \return Transform matrix.
606  */
607 NR::Matrix
608 sp_shape_marker_get_transform(SPShape const *shape, NArtBpath const *bp)
610     g_return_val_if_fail(( is_moveto(SP_CURVE_BPATH(shape->curve)[0].code)
611                            && ( 0 < shape->curve->end )
612                            && ( SP_CURVE_BPATH(shape->curve)[shape->curve->end].code == NR_END ) ),
613                          NR::Matrix(NR::translate(bp->c(3))));
614     double const angle1 = incoming_tangent(bp);
615     double const angle2 = outgoing_tangent(bp);
617     /* angle1 and angle2 are now each either unset (i.e. still 100 from their initialization) or in
618        [-pi, pi] from atan2. */
619     g_assert((-3.15 < angle1 && angle1 < 3.15) || (angle1 == no_tangent));
620     g_assert((-3.15 < angle2 && angle2 < 3.15) || (angle2 == no_tangent));
622     double ret_angle;
623     if (angle1 == no_tangent) {
624         /* First vertex of an open subpath. */
625         ret_angle = ( angle2 == no_tangent
626                       ? 0.
627                       : angle2 );
628     } else if (angle2 == no_tangent) {
629         /* Last vertex of an open subpath. */
630         ret_angle = angle1;
631     } else {
632         ret_angle = .5 * (angle1 + angle2);
634         if ( fabs( angle2 - angle1 ) > M_PI ) {
635             /* ret_angle is in the middle of the larger of the two sectors between angle1 and
636              * angle2, so flip it by 180degrees to force it to the middle of the smaller sector.
637              *
638              * (Imagine a circle with rays drawn at angle1 and angle2 from the centre of the
639              * circle.  Those two rays divide the circle into two sectors.)
640              */
641             ret_angle += M_PI;
642         }
643     }
645     return NR::Matrix(NR::rotate(ret_angle)) * NR::translate(bp->c(3));
648 /**
649  * Updates the instances (views) of a given marker in a shape.
650  * Marker views have to be scaled already.  The transformation
651  * is retrieved and then shown by calling sp_marker_show_instance.
652  */
653 static void
654 sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai)
656         SPStyle *style = ((SPObject *) shape)->style;
658         for (int i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
659             if (shape->marker[i] == NULL) {
660                 continue;
661             }
663             int n = 0;
665             for (NArtBpath *bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
666                 if (sp_shape_marker_required (shape, i, bp)) {
667                     NR::Matrix const m(sp_shape_marker_get_transform(shape, bp));
668                     sp_marker_show_instance ((SPMarker* ) shape->marker[i], ai,
669                                              NR_ARENA_ITEM_GET_KEY(ai) + i, n, m,
670                                              style->stroke_width.computed);
671                     n++;
672                 }
673             }
674         }
677 /**
678  * Sets modified flag for all sub-item views.
679  */
680 static void
681 sp_shape_modified (SPObject *object, unsigned int flags)
683         SPShape *shape = SP_SHAPE (object);
685         if (((SPObjectClass *) (parent_class))->modified) {
686           (* ((SPObjectClass *) (parent_class))->modified) (object, flags);
687         }
689         if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
690                 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
691                         nr_arena_shape_set_style (NR_ARENA_SHAPE (v->arenaitem), object->style);
692                 }
693         }
696 /**
697  * Calculates the bounding box for item, storing it into bbox.
698  * This also includes the bounding boxes of any markers included in the shape.
699  */
700 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
702     SPShape const *shape = SP_SHAPE (item);
704     if (shape->curve) {
706         NRRect  cbbox;
707         NRBPath bp;
709         bp.path = SP_CURVE_BPATH (shape->curve);
711         cbbox.x0 = cbbox.y0 = NR_HUGE;
712         cbbox.x1 = cbbox.y1 = -NR_HUGE;
714         nr_path_matrix_bbox_union(&bp, transform, &cbbox);
716         if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
717             
718             SPStyle* style=SP_OBJECT_STYLE (item);
719             if (!style->stroke.isNone()) {
720                 double const scale = expansion(transform);
721                 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
722                     double const width = MAX(0.125, style->stroke_width.computed * scale);
723                     if ( fabs(cbbox.x1-cbbox.x0) > -0.00001 && fabs(cbbox.y1-cbbox.y0) > -0.00001 ) {
724                         cbbox.x0-=0.5*width;
725                         cbbox.x1+=0.5*width;
726                         cbbox.y0-=0.5*width;
727                         cbbox.y1+=0.5*width;
728                     }
729                 }
730             }
732             // Union with bboxes of the markers, if any
733             if (sp_shape_has_markers (shape)) {
734                 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
735                     for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
736                         if (sp_shape_marker_required (shape, m, bp)) {
738                             SPMarker* marker = SP_MARKER (shape->marker[m]);
739                             SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
741                             NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
743                             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
744                                 tr = NR::scale(style->stroke_width.computed) * tr;
745                             }
747                             // total marker transform
748                             tr = marker_item->transform * marker->c2p * tr * transform;
750                             // get bbox of the marker with that transform
751                             NRRect marker_bbox;
752                             sp_item_invoke_bbox (marker_item, &marker_bbox, tr, true);
753                             // union it with the shape bbox
754                             nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
755                         }
756                     }
757                 }
758             }
759         }
761         // copy our bbox to the variable we're given
762         *bbox = cbbox;
763     }
766 /**
767  * Prepares shape for printing.  Handles printing of comments for printing
768  * debugging, sizes the item to fit into the document width/height,
769  * applies print fill/stroke, sets transforms for markers, and adds
770  * comment labels.
771  */
772 void
773 sp_shape_print (SPItem *item, SPPrintContext *ctx)
775         NRRect pbox, dbox, bbox;
777         SPShape *shape = SP_SHAPE(item);
779         if (!shape->curve) return;
781         gint add_comments = prefs_get_int_attribute_limited ("printing.debug", "add-label-comments", 0, 0, 1);
782         if (add_comments) {
783             gchar * comment = g_strdup_printf("begin '%s'",
784                                               SP_OBJECT(item)->defaultLabel());
785             sp_print_comment(ctx, comment);
786             g_free(comment);
787         }
789         /* fixme: Think (Lauris) */
790         sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
791         dbox.x0 = 0.0;
792         dbox.y0 = 0.0;
793         dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
794         dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
795         sp_item_bbox_desktop (item, &bbox);
796         NR::Matrix const i2d = sp_item_i2d_affine(item);
798         SPStyle* style = SP_OBJECT_STYLE (item);
800         if (!style->fill.isNone()) {
801                 NRBPath bp;
802                 bp.path = SP_CURVE_BPATH(shape->curve);
803                 sp_print_fill (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
804         }
806         if (!style->stroke.isNone()) {
807                 NRBPath bp;
808                 bp.path = SP_CURVE_BPATH(shape->curve);
809                 sp_print_stroke (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
810         }
812         for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
813             for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
814                 if (sp_shape_marker_required (shape, m, bp)) {
816                     SPMarker* marker = SP_MARKER (shape->marker[m]);
817                     SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
819                     NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
821                     if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
822                         tr = NR::scale(style->stroke_width.computed) * tr;
823                     }
825                     tr = marker_item->transform * marker->c2p * tr;
827                     NR::Matrix old_tr = marker_item->transform;
828                     marker_item->transform = tr;
829                     sp_item_invoke_print (marker_item, ctx);
830                     marker_item->transform = old_tr;
831                 }
832             }
833         }
835         if (add_comments) {
836             gchar * comment = g_strdup_printf("end '%s'",
837                                               SP_OBJECT(item)->defaultLabel());
838             sp_print_comment(ctx, comment);
839             g_free(comment);
840         }
843 /**
844  * Sets style, path, and paintbox.  Updates marker views, including dimensions.
845  */
846 static NRArenaItem *
847 sp_shape_show (SPItem *item, NRArena *arena, unsigned int /*key*/, unsigned int /*flags*/)
849         SPObject *object = SP_OBJECT(item);
850         SPShape *shape = SP_SHAPE(item);
852         NRArenaItem *arenaitem = NRArenaShape::create(arena);
853         NRArenaShape * const s = NR_ARENA_SHAPE(arenaitem);
854         nr_arena_shape_set_style(s, object->style);
855         nr_arena_shape_set_path(s, shape->curve, false);
856         NR::Maybe<NR::Rect> paintbox = item->getBounds(NR::identity());
857         if (paintbox) {
858             s->setPaintBox(*paintbox);
859         }
861         if (sp_shape_has_markers (shape)) {
863             /* Dimension the marker views */
864             if (!arenaitem->key) {
865                 NR_ARENA_ITEM_SET_KEY (arenaitem, sp_item_display_key_new (SP_MARKER_LOC_QTY));
866             }
868             for (int i = 0; i < SP_MARKER_LOC_QTY; i++) {
869                 if (shape->marker[i]) {
870                     sp_marker_show_dimension ((SPMarker *) shape->marker[i],
871                                               NR_ARENA_ITEM_GET_KEY (arenaitem) + i - SP_MARKER_LOC,
872                                               sp_shape_number_of_markers (shape, i));
873                 }
874             }
877             /* Update marker views */
878             sp_shape_update_marker_view (shape, arenaitem);
879         }
881         return arenaitem;
884 /**
885  * Hides/removes marker views from the shape.
886  */
887 static void
888 sp_shape_hide (SPItem *item, unsigned int key)
890         SPShape *shape;
891         SPItemView *v;
892         int i;
894         shape = (SPShape *) item;
896         for (i=0; i<SP_MARKER_LOC_QTY; i++) {
897           if (shape->marker[i]) {
898             for (v = item->display; v != NULL; v = v->next) {
899                 if (key == v->key) {
900               sp_marker_hide ((SPMarker *) shape->marker[i],
901                                     NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
902                 }
903             }
904           }
905         }
907         if (((SPItemClass *) parent_class)->hide) {
908           ((SPItemClass *) parent_class)->hide (item, key);
909         }
912 /**
913 * \param shape Shape.
914 * \return TRUE if the shape has any markers, or FALSE if not.
915 */
916 int
917 sp_shape_has_markers (SPShape const *shape)
919     /* Note, we're ignoring 'marker' settings, which technically should apply for
920        all three settings.  This should be fixed later such that if 'marker' is
921        specified, then all three should appear. */
923     return (
924         shape->curve &&
925         (shape->marker[SP_MARKER_LOC_START] ||
926          shape->marker[SP_MARKER_LOC_MID] ||
927          shape->marker[SP_MARKER_LOC_END])
928         );
932 /**
933 * \param shape Shape.
934 * \param type Marker type (e.g. SP_MARKER_LOC_START)
935 * \return Number of markers that the shape has of this type.
936 */
937 int
938 sp_shape_number_of_markers (SPShape *shape, int type)
940     int n = 0;
941     for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
942         if (sp_shape_marker_required (shape, type, bp)) {
943             n++;
944         }
945     }
947     return n;
950 /**
951  * Checks if the given marker is used in the shape, and if so, it
952  * releases it by calling sp_marker_hide.  Also detaches signals
953  * and unrefs the marker from the shape.
954  */
955 static void
956 sp_shape_marker_release (SPObject *marker, SPShape *shape)
958         SPItem *item;
959         int i;
961         item = (SPItem *) shape;
963         for (i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
964           if (marker == shape->marker[i]) {
965             SPItemView *v;
966             /* Hide marker */
967             for (v = item->display; v != NULL; v = v->next) {
968               sp_marker_hide ((SPMarker *) (shape->marker[i]), NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
969               /* fixme: Do we need explicit remove here? (Lauris) */
970               /* nr_arena_item_set_mask (v->arenaitem, NULL); */
971             }
972             /* Detach marker */
973             sp_signal_disconnect_by_data (shape->marker[i], item);
974             shape->marker[i] = sp_object_hunref (shape->marker[i], item);
975           }
976         }
979 /**
980  * No-op.  Exists for handling 'modified' messages
981  */
982 static void
983 sp_shape_marker_modified (SPObject */*marker*/, guint /*flags*/, SPItem */*item*/)
985         /* I think mask does update automagically */
986         /* g_warning ("Item %s mask %s modified", SP_OBJECT_ID (item), SP_OBJECT_ID (mask)); */
989 /**
990  * Adds a new marker to shape object at the location indicated by key.  value 
991  * must be a valid URI reference resolvable from the shape object (i.e., present
992  * in the document <defs>).  If the shape object already has a marker
993  * registered at the given position, it is removed first.  Then the
994  * new marker is hrefed and its signals connected.
995  */
996 void
997 sp_shape_set_marker (SPObject *object, unsigned int key, const gchar *value)
999     SPItem *item = (SPItem *) object;
1000     SPShape *shape = (SPShape *) object;
1002     if (key < SP_MARKER_LOC_START || key > SP_MARKER_LOC_END) {
1003         return;
1004     }
1006     SPObject *mrk = sp_css_uri_reference_resolve (SP_OBJECT_DOCUMENT (object), value);
1007     if (mrk != shape->marker[key]) {
1008         if (shape->marker[key]) {
1009             SPItemView *v;
1011             /* Detach marker */
1012             shape->release_connect[key].disconnect();
1013             shape->modified_connect[key].disconnect();
1015             /* Hide marker */
1016             for (v = item->display; v != NULL; v = v->next) {
1017                 sp_marker_hide ((SPMarker *) (shape->marker[key]),
1018                                 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + key);
1019                 /* fixme: Do we need explicit remove here? (Lauris) */
1020                 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
1021             }
1023             /* Unref marker */
1024             shape->marker[key] = sp_object_hunref (shape->marker[key], object);
1025         }
1026         if (SP_IS_MARKER (mrk)) {
1027             shape->marker[key] = sp_object_href (mrk, object);
1028             shape->release_connect[key] = mrk->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_shape_marker_release), shape));
1029             shape->modified_connect[key] = mrk->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_shape_marker_modified), shape));
1030         }
1031     }
1036 /* Shape section */
1038 /**
1039  * Calls any registered handlers for the set_shape action
1040  */
1041 void
1042 sp_shape_set_shape (SPShape *shape)
1044         g_return_if_fail (shape != NULL);
1045         g_return_if_fail (SP_IS_SHAPE (shape));
1047         if (SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape) {
1048           SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape (shape);
1049         }
1052 /**
1053  * Adds a curve to the shape.  If owner is specified, a reference
1054  * will be made, otherwise the curve will be copied into the shape.
1055  * Any existing curve in the shape will be unreferenced first.
1056  * This routine also triggers a request to update the display.
1057  */
1058 void
1059 sp_shape_set_curve (SPShape *shape, SPCurve *curve, unsigned int owner)
1061         if (shape->curve) {
1062                 shape->curve = sp_curve_unref (shape->curve);
1063         }
1064         if (curve) {
1065                 if (owner) {
1066                         shape->curve = sp_curve_ref (curve);
1067                 } else {
1068                         shape->curve = sp_curve_copy (curve);
1069                 }
1070         }
1071         SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1074 /**
1075  * Return duplicate of curve (if any exists) or NULL if there is no curve
1076  */
1077 SPCurve *
1078 sp_shape_get_curve (SPShape *shape)
1080         if (shape->curve) {
1081                 return sp_curve_copy (shape->curve);
1082         }
1083         return NULL;
1086 /* NOT FOR GENERAL PUBLIC UNTIL SORTED OUT (Lauris) */
1087 void
1088 sp_shape_set_curve_insync (SPShape *shape, SPCurve *curve, unsigned int owner)
1090         if (shape->curve) {
1091                 shape->curve = sp_curve_unref (shape->curve);
1092         }
1093         if (curve) {
1094                 if (owner) {
1095                         shape->curve = sp_curve_ref (curve);
1096                 } else {
1097                         shape->curve = sp_curve_copy (curve);
1098                 }
1099         }
1102 /**
1103  * Return all nodes in a path that are to be considered for snapping
1104  */
1105 static void sp_shape_snappoints(SPItem const *item, SnapPointsIter p)
1107     g_assert(item != NULL);
1108     g_assert(SP_IS_SHAPE(item));
1110     SPShape const *shape = SP_SHAPE(item);
1111     if (shape->curve == NULL) {
1112         return;
1113     }
1114     
1115     NR::Matrix const i2d (sp_item_i2d_affine (item));
1116     NArtBpath const *b = SP_CURVE_BPATH(shape->curve);    
1117     
1118     // Cycle through the nodes in the concatenated subpaths
1119     while (b->code != NR_END) {
1120         NR::Point pos = b->c(3) * i2d; // this is the current node
1121         
1122         // NR_MOVETO Indicates the start of a closed subpath, see nr-path-code.h
1123         // If we're looking at a closed subpath, then we can skip this first 
1124         // point of the subpath because it's coincident with the last point.  
1125         if (b->code != NR_MOVETO) {
1126             if (b->code == NR_MOVETO_OPEN || b->code == NR_LINETO || b[1].code == NR_LINETO || b[1].code == NR_END) {
1127                 // end points of a line segment are always considered for snapping
1128                 *p = pos; 
1129             } else {        
1130                 // g_assert(b->code == NR_CURVETO);
1131                 NR::Point ppos, npos;
1132                 ppos = b->code == NR_CURVETO ? b->c(2) * i2d : pos; // backward handle 
1133                 npos = b[1].code == NR_CURVETO ? b[1].c(1) * i2d : pos; // forward handle            
1134                 // Determine whether a node is at a smooth part of the path, by 
1135                 // calculating a measure for the collinearity of the handles
1136                 bool c1 = fabs (Inkscape::Util::triangle_area (pos, ppos, npos)) < 1; // points are (almost) collinear
1137                 bool c2 = NR::L2(pos - ppos) < 1e-6 || NR::L2(pos - npos) < 1e-6; // endnode, or a node with a retracted handle
1138                 if (!(c1 & !c2)) {
1139                     *p = pos; // only return non-smooth nodes ("cusps")
1140                 }
1141             }
1142         }
1143         
1144         b++;
1145     }
1149 LivePathEffectObject *
1150 sp_shape_get_livepatheffectobject(SPShape *shape) {
1151     if (!shape) return NULL;
1153     if (sp_shape_has_path_effect(shape)) {
1154         return shape->path_effect_ref->lpeobject;
1155     } else {
1156         return NULL;
1157     }
1160 /**
1161  * Calls any registered handlers for the update_patheffect action
1162  */
1163 void
1164 sp_shape_update_patheffect (SPShape *shape, bool write)
1166 #ifdef SHAPE_VERBOSE
1167     g_message("sp_shape_update_patheffect: %p\n", shape);
1168 #endif
1169     g_return_if_fail (shape != NULL);
1170     g_return_if_fail (SP_IS_SHAPE (shape));
1172     if (SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->update_patheffect) {
1173         SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->update_patheffect (shape, write);
1174     }
1177 void sp_shape_perform_path_effect(SPCurve *curve, SPShape *shape) {
1178     if (!shape) return;
1179     if (!curve) return;
1181     LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(shape);
1182     if (lpeobj && lpeobj->lpe) {
1183         lpeobj->lpe->doEffect(curve);
1184     }
1187 /**
1188  * Gets called when (re)attached to another lpeobject.
1189  */
1190 static void
1191 lpeobject_ref_changed(SPObject *old_ref, SPObject *ref, SPShape *shape)
1193     if (old_ref) {
1194         sp_signal_disconnect_by_data(old_ref, shape);
1195     }
1196     if ( IS_LIVEPATHEFFECT(ref) && ref != shape )
1197     {
1198         shape->lpe_modified_connection.disconnect();
1199         shape->lpe_modified_connection = ref->connectModified(sigc::bind(sigc::ptr_fun(&lpeobject_ref_modified), shape));
1200         lpeobject_ref_modified(ref, 0, shape);
1201     }
1204 /**
1205  * Gets called when lpeobject repr contents change: i.e. parameter change.
1206  */
1207 static void
1208 lpeobject_ref_modified(SPObject */*href*/, guint /*flags*/, SPShape *shape)
1210     sp_shape_update_patheffect (shape, true);
1213 void sp_shape_set_path_effect(SPShape *shape, gchar *value)
1215     if (!value) {
1216         sp_shape_remove_path_effect(shape);
1217     } else {
1218         SP_OBJECT_REPR(shape)->setAttribute("inkscape:path-effect", value);
1219     }
1222 void sp_shape_remove_path_effect(SPShape *shape)
1224     Inkscape::XML::Node *repr = SP_OBJECT_REPR(shape);
1225     repr->setAttribute("inkscape:path-effect", NULL);
1226     if (SP_IS_PATH(shape)) {
1227         repr->setAttribute("d", repr->attribute("inkscape:original-d"));
1228         repr->setAttribute("inkscape:original-d", NULL);
1229     }
1232 bool sp_shape_has_path_effect(SPShape *shape)
1234     return (shape->path_effect_href != NULL);
1237 void sp_shape_edit_next_param_oncanvas(SPShape *shape, SPDesktop *dt)
1239     LivePathEffectObject *lpeobj = sp_shape_get_livepatheffectobject(shape);
1240     if (lpeobj && lpeobj->lpe) {
1241         lpeobj->lpe->editNextParamOncanvas(SP_ITEM(shape), dt);
1242     }
1245 /*
1246   Local Variables:
1247   mode:c++
1248   c-file-style:"stroustrup"
1249   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1250   indent-tabs-mode:nil
1251   fill-column:99
1252   End:
1253 */
1254 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :