Code

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