Code

switch from invokeBbox to getBounds (need to fix problems with empty
[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::Rect const 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                     s->setPaintBox(paintbox);
248                 }
249         }
251         if (sp_shape_has_markers (shape)) {
253             /* Dimension marker views */
254             for (SPItemView *v = item->display; v != NULL; v = v->next) {
256                 if (!v->arenaitem->key) {
257                     /* Get enough keys for all, start, mid and end marker types,
258                     ** and set this view's arenaitem key to the first of these keys.
259                     */
260                     NR_ARENA_ITEM_SET_KEY (
261                         v->arenaitem,
262                         sp_item_display_key_new (SP_MARKER_LOC_QTY)
263                         );
264                 }
266                 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
267                     if (shape->marker[i]) {
268                         sp_marker_show_dimension ((SPMarker *) shape->marker[i],
269                                                   NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i - SP_MARKER_LOC,
270                                                   sp_shape_number_of_markers (shape, i));
271                     }
272                 }
273             }
275             /* Update marker views */
276             for (SPItemView *v = item->display; v != NULL; v = v->next) {
277                 sp_shape_update_marker_view (shape, v->arenaitem);
278             }
279         }
283 /**
284 * Works out whether a marker of a given type is required at a particular
285 * point on a shape.
287 * \param shape Shape of interest.
288 * \param m Marker type (e.g. SP_MARKER_LOC_START)
289 * \param bp Path segment.
290 * \return 1 if a marker is required here, otherwise 0.
291 */
292 bool
293 sp_shape_marker_required(SPShape const *shape, int const m, NArtBpath *bp)
295     if (shape->marker[m] == NULL) {
296         return false;
297     }
299     if (bp == SP_CURVE_BPATH(shape->curve))
300         return m == SP_MARKER_LOC_START;
301     else if (bp[1].code == NR_END)
302         return m == SP_MARKER_LOC_END;
303     else
304         return m == SP_MARKER_LOC_MID;
307 static bool
308 is_moveto(NRPathcode const c)
310     return c == NR_MOVETO || c == NR_MOVETO_OPEN;
313 /** 
314  * Helper function that advances a subpath's bpath to the first subpath
315  * by checking for moveto segments.
316  *
317  * \pre The bpath[] containing bp begins with a moveto. 
318  */
319 static NArtBpath const *
320 first_seg_in_subpath(NArtBpath const *bp)
322     while (!is_moveto(bp->code)) {
323         --bp;
324     }
325     return bp;
328 /**
329  * Advances the bpath to the last segment in the subpath.
330  */
331 static NArtBpath const *
332 last_seg_in_subpath(NArtBpath const *bp)
334     for(;;) {
335         ++bp;
336         switch (bp->code) {
337             case NR_MOVETO:
338             case NR_MOVETO_OPEN:
339             case NR_END:
340                 --bp;
341                 return bp;
343             default: continue;
344         }
345     }
349 /* A subpath begins with a moveto and ends immediately before the next moveto or NR_END.
350  * (`moveto' here means either NR_MOVETO or NR_MOVETO_OPEN.)  I'm assuming that non-empty
351  * paths always begin with a moveto.
352  *
353  * The control points of the subpath are the control points of the path elements of the subpath.
354  *
355  * As usual, the control points of a moveto or NR_LINETO are {c(3)}, and
356  * the control points of a NR_CURVETO are {c(1), c(2), c(3)}.
357  * (It follows from the definition that NR_END isn't part of a subpath.)
358  *
359  * The initial control point is bpath[bi0].c(3).
360  *
361  * Reference: http://www.w3.org/TR/SVG11/painting.html#MarkerElement, the `orient' attribute.
362  * Reference for behaviour of zero-length segments:
363  * http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
364  */
366 static double const no_tangent = 128.0;  /* arbitrarily-chosen value outside the range of atan2, i.e. outside of [-pi, pi]. */
368 /**
369  * Helper function to calculate the outgoing tangent of a path 
370  * ( atan2(other - p0) )
371  * \pre The bpath[] containing bp0 begins with a moveto. 
372  */
373 static double
374 outgoing_tangent(NArtBpath const *bp0)
376     /* See notes in comment block above. */
378     g_assert(bp0->code != NR_END);
379     NR::Point const &p0 = bp0->c(3);
380     NR::Point other;
381     for (NArtBpath const *bp = bp0;;) {
382         ++bp;
383         switch (bp->code) {
384             case NR_LINETO:
385                 other = bp->c(3);
386                 if (other != p0) {
387                     goto found;
388                 }
389                 break;
391             case NR_CURVETO:
392                 for (unsigned ci = 1; ci <= 3; ++ci) {
393                     other = bp->c(ci);
394                     if (other != p0) {
395                         goto found;
396                     }
397                 }
398                 break;
400             case NR_MOVETO_OPEN:
401             case NR_END:
402             case NR_MOVETO:
403                 bp = first_seg_in_subpath(bp0);
404                 if (bp == bp0) {
405                     /* Gone right around the subpath without finding any different point since the
406                      * initial moveto. */
407                     return no_tangent;
408                 }
409                 if (bp->code != NR_MOVETO) {
410                     /* Open subpath. */
411                     return no_tangent;
412                 }
413                 other = bp->c(3);
414                 if (other != p0) {
415                     goto found;
416                 }
417                 break;
418         }
420         if (bp == bp0) {
421             /* Back where we started, so zero-length subpath. */
422             return no_tangent;
424             /* Note: this test must come after we've looked at element bp, in case bp0 is a curve:
425              * we must look at c(1) and c(2).  (E.g. single-curve subpath.)
426              */
427         }
428     }
430 found:
431     return atan2( other - p0 );
434 /**
435  * Helper function to calculate the incoming tangent of a path
436  * ( atan2(p0 - other) )
437  * 
438  * \pre The bpath[] containing bp0 begins with a moveto. 
439  */
440 static double
441 incoming_tangent(NArtBpath const *bp0)
443     /* See notes in comment block before outgoing_tangent. */
445     g_assert(bp0->code != NR_END);
446     NR::Point const &p0 = bp0->c(3);
447     NR::Point other;
448     for (NArtBpath const *bp = bp0;;) {
449         switch (bp->code) {
450             case NR_LINETO:
451                 other = bp->c(3);
452                 if (other != p0) {
453                     goto found;
454                 }
455                 --bp;
456                 break;
458             case NR_CURVETO:
459                 for (unsigned ci = 3; ci != 0; --ci) {
460                     other = bp->c(ci);
461                     if (other != p0) {
462                         goto found;
463                     }
464                 }
465                 --bp;
466                 break;
468             case NR_MOVETO:
469             case NR_MOVETO_OPEN:
470                 other = bp->c(3);
471                 if (other != p0) {
472                     goto found;
473                 }
474                 if (bp->code != NR_MOVETO) {
475                     /* Open subpath. */
476                     return no_tangent;
477                 }
478                 bp = last_seg_in_subpath(bp0);
479                 break;
481             default: /* includes NR_END */
482                 g_error("Found invalid path code %u in middle of path.", bp->code);
483                 return no_tangent;
484         }
486         if (bp == bp0) {
487             /* Back where we started from: zero-length subpath. */
488             return no_tangent;
489         }
490     }
492 found:
493     return atan2( p0 - other );
497 /**
498  * Calculate the transform required to get a marker's path object in the
499  * right place for particular path segment on a shape.  You should
500  * call sp_shape_marker_required first to see if a marker is required
501  * at this point.
502  *
503  * \see sp_shape_marker_required.
504  *
505  * \param shape Shape which the marker is for.
506  * \param m Marker type (e.g. SP_MARKER_LOC_START)
507  * \param bp Path segment which the arrow is for.
508  * \return Transform matrix.
509  */
510 NR::Matrix
511 sp_shape_marker_get_transform(SPShape const *shape, NArtBpath const *bp)
513     g_return_val_if_fail(( is_moveto(SP_CURVE_BPATH(shape->curve)[0].code)
514                            && ( 0 < shape->curve->end )
515                            && ( SP_CURVE_BPATH(shape->curve)[shape->curve->end].code == NR_END ) ),
516                          NR::Matrix(NR::translate(bp->c(3))));
517     double const angle1 = incoming_tangent(bp);
518     double const angle2 = outgoing_tangent(bp);
520     /* angle1 and angle2 are now each either unset (i.e. still 100 from their initialization) or in
521        [-pi, pi] from atan2. */
522     g_assert((-3.15 < angle1 && angle1 < 3.15) || (angle1 == no_tangent));
523     g_assert((-3.15 < angle2 && angle2 < 3.15) || (angle2 == no_tangent));
525     double ret_angle;
526     if (angle1 == no_tangent) {
527         /* First vertex of an open subpath. */
528         ret_angle = ( angle2 == no_tangent
529                       ? 0.
530                       : angle2 );
531     } else if (angle2 == no_tangent) {
532         /* Last vertex of an open subpath. */
533         ret_angle = angle1;
534     } else {
535         ret_angle = .5 * (angle1 + angle2);
537         if ( fabs( angle2 - angle1 ) > M_PI ) {
538             /* ret_angle is in the middle of the larger of the two sectors between angle1 and
539              * angle2, so flip it by 180degrees to force it to the middle of the smaller sector.
540              *
541              * (Imagine a circle with rays drawn at angle1 and angle2 from the centre of the
542              * circle.  Those two rays divide the circle into two sectors.)
543              */
544             ret_angle += M_PI;
545         }
546     }
548     return NR::Matrix(NR::rotate(ret_angle)) * NR::translate(bp->c(3));
551 /**
552  * Updates the instances (views) of a given marker in a shape.
553  * Marker views have to be scaled already.  The transformation
554  * is retrieved and then shown by calling sp_marker_show_instance.
555  */
556 static void
557 sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai)
559         SPStyle *style = ((SPObject *) shape)->style;
561         for (int i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
562             if (shape->marker[i] == NULL) {
563                 continue;
564             }
566             int n = 0;
568             for (NArtBpath *bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
569                 if (sp_shape_marker_required (shape, i, bp)) {
570                     NR::Matrix const m(sp_shape_marker_get_transform(shape, bp));
571                     sp_marker_show_instance ((SPMarker* ) shape->marker[i], ai,
572                                              NR_ARENA_ITEM_GET_KEY(ai) + i, n, m,
573                                              style->stroke_width.computed);
574                     n++;
575                 }
576             }
577         }
580 /**
581  * Sets modified flag for all sub-item views.
582  */
583 static void
584 sp_shape_modified (SPObject *object, unsigned int flags)
586         SPShape *shape = SP_SHAPE (object);
588         if (((SPObjectClass *) (parent_class))->modified) {
589           (* ((SPObjectClass *) (parent_class))->modified) (object, flags);
590         }
592         if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
593                 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
594                         nr_arena_shape_set_style (NR_ARENA_SHAPE (v->arenaitem), object->style);
595                 }
596         }
599 /**
600  * Calculates the bounding box for item, storing it into bbox.
601  * This also includes the bounding boxes of any markers included in the shape.
602  */
603 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
605     SPShape const *shape = SP_SHAPE (item);
607     if (shape->curve) {
609         NRRect  cbbox;
610         NRBPath bp;
612         bp.path = SP_CURVE_BPATH (shape->curve);
614         cbbox.x0 = cbbox.y0 = NR_HUGE;
615         cbbox.x1 = cbbox.y1 = -NR_HUGE;
617         nr_path_matrix_bbox_union(&bp, transform, &cbbox);
619         SPStyle* style=SP_OBJECT_STYLE (item);
620         if (style->stroke.type != SP_PAINT_TYPE_NONE) {
621             double const scale = expansion(transform);
622             if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
623                 double const width = MAX(0.125, style->stroke_width.computed * scale);
624                 if ( fabs(cbbox.x1-cbbox.x0) > -0.00001 && fabs(cbbox.y1-cbbox.y0) > -0.00001 ) {
625                     cbbox.x0-=0.5*width;
626                     cbbox.x1+=0.5*width;
627                     cbbox.y0-=0.5*width;
628                     cbbox.y1+=0.5*width;
629                 }
630             }
631         }
633         // Union with bboxes of the markers, if any
634         if (sp_shape_has_markers (shape)) {
635             for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
636                 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
637                     if (sp_shape_marker_required (shape, m, bp)) {
639                         SPMarker* marker = SP_MARKER (shape->marker[m]);
640                         SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
642                         NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
644                         if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
645                             tr = NR::scale(style->stroke_width.computed) * tr;
646                         }
648                         // total marker transform
649                         tr = marker_item->transform * marker->c2p * tr * transform;
651                         // get bbox of the marker with that transform
652                         NRRect marker_bbox;
653                         sp_item_invoke_bbox (marker_item, &marker_bbox, tr, true);
654                         // union it with the shape bbox
655                         nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
656                     }
657                 }
658             }
659         }
661         // copy our bbox to the variable we're given
662         *bbox = cbbox;
663     }
666 /**
667  * Prepares shape for printing.  Handles printing of comments for printing
668  * debugging, sizes the item to fit into the document width/height,
669  * applies print fill/stroke, sets transforms for markers, and adds
670  * comment labels.
671  */
672 void
673 sp_shape_print (SPItem *item, SPPrintContext *ctx)
675         NRRect pbox, dbox, bbox;
677         SPShape *shape = SP_SHAPE(item);
679         if (!shape->curve) return;
681         gint add_comments = prefs_get_int_attribute_limited ("printing.debug", "add-label-comments", 0, 0, 1);
682         if (add_comments) {
683             gchar * comment = g_strdup_printf("begin '%s'",
684                                               SP_OBJECT(item)->defaultLabel());
685             sp_print_comment(ctx, comment);
686             g_free(comment);
687         }
689         /* fixme: Think (Lauris) */
690         sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
691         dbox.x0 = 0.0;
692         dbox.y0 = 0.0;
693         dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
694         dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
695         sp_item_bbox_desktop (item, &bbox);
696         NR::Matrix const i2d = sp_item_i2d_affine(item);
698         SPStyle* style = SP_OBJECT_STYLE (item);
700         if (style->fill.type != SP_PAINT_TYPE_NONE) {
701                 NRBPath bp;
702                 bp.path = SP_CURVE_BPATH(shape->curve);
703                 sp_print_fill (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
704         }
706         if (style->stroke.type != SP_PAINT_TYPE_NONE) {
707                 NRBPath bp;
708                 bp.path = SP_CURVE_BPATH(shape->curve);
709                 sp_print_stroke (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
710         }
712         for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
713             for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
714                 if (sp_shape_marker_required (shape, m, bp)) {
716                     SPMarker* marker = SP_MARKER (shape->marker[m]);
717                     SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
719                     NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
721                     if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
722                         tr = NR::scale(style->stroke_width.computed) * tr;
723                     }
725                     tr = marker_item->transform * marker->c2p * tr;
727                     NR::Matrix old_tr = marker_item->transform;
728                     marker_item->transform = tr;
729                     sp_item_invoke_print (marker_item, ctx);
730                     marker_item->transform = old_tr;
731                 }
732             }
733         }
735         if (add_comments) {
736             gchar * comment = g_strdup_printf("end '%s'",
737                                               SP_OBJECT(item)->defaultLabel());
738             sp_print_comment(ctx, comment);
739             g_free(comment);
740         }
743 /**
744  * Sets style, path, and paintbox.  Updates marker views, including dimensions.
745  */
746 static NRArenaItem *
747 sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
749         SPObject *object = SP_OBJECT(item);
750         SPShape *shape = SP_SHAPE(item);
752         NRArenaItem *arenaitem = NRArenaShape::create(arena);
753         NRArenaShape * const s = NR_ARENA_SHAPE(arenaitem);
754         nr_arena_shape_set_style(s, object->style);
755         nr_arena_shape_set_path(s, shape->curve, false);
756         NR::Rect const paintbox = item->getBounds(NR::identity());
757         s->setPaintBox(paintbox);
759         if (sp_shape_has_markers (shape)) {
761             /* Dimension the marker views */
762             if (!arenaitem->key) {
763                 NR_ARENA_ITEM_SET_KEY (arenaitem, sp_item_display_key_new (SP_MARKER_LOC_QTY));
764             }
766             for (int i = 0; i < SP_MARKER_LOC_QTY; i++) {
767                 if (shape->marker[i]) {
768                     sp_marker_show_dimension ((SPMarker *) shape->marker[i],
769                                               NR_ARENA_ITEM_GET_KEY (arenaitem) + i - SP_MARKER_LOC,
770                                               sp_shape_number_of_markers (shape, i));
771                 }
772             }
775             /* Update marker views */
776             sp_shape_update_marker_view (shape, arenaitem);
777         }
779         return arenaitem;
782 /**
783  * Hides/removes marker views from the shape.
784  */
785 static void
786 sp_shape_hide (SPItem *item, unsigned int key)
788         SPShape *shape;
789         SPItemView *v;
790         int i;
792         shape = (SPShape *) item;
794         for (i=0; i<SP_MARKER_LOC_QTY; i++) {
795           if (shape->marker[i]) {
796             for (v = item->display; v != NULL; v = v->next) {
797                 if (key == v->key) {
798               sp_marker_hide ((SPMarker *) shape->marker[i],
799                                     NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
800                 }
801             }
802           }
803         }
805         if (((SPItemClass *) parent_class)->hide) {
806           ((SPItemClass *) parent_class)->hide (item, key);
807         }
810 /**
811 * \param shape Shape.
812 * \return TRUE if the shape has any markers, or FALSE if not.
813 */
814 int
815 sp_shape_has_markers (SPShape const *shape)
817     /* Note, we're ignoring 'marker' settings, which technically should apply for
818        all three settings.  This should be fixed later such that if 'marker' is
819        specified, then all three should appear. */
821     return (
822         shape->curve &&
823         (shape->marker[SP_MARKER_LOC_START] ||
824          shape->marker[SP_MARKER_LOC_MID] ||
825          shape->marker[SP_MARKER_LOC_END])
826         );
830 /**
831 * \param shape Shape.
832 * \param type Marker type (e.g. SP_MARKER_LOC_START)
833 * \return Number of markers that the shape has of this type.
834 */
835 int
836 sp_shape_number_of_markers (SPShape *shape, int type)
838     int n = 0;
839     for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
840         if (sp_shape_marker_required (shape, type, bp)) {
841             n++;
842         }
843     }
845     return n;
848 /**
849  * Checks if the given marker is used in the shape, and if so, it
850  * releases it by calling sp_marker_hide.  Also detaches signals
851  * and unrefs the marker from the shape.
852  */
853 static void
854 sp_shape_marker_release (SPObject *marker, SPShape *shape)
856         SPItem *item;
857         int i;
859         item = (SPItem *) shape;
861         for (i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
862           if (marker == shape->marker[i]) {
863             SPItemView *v;
864             /* Hide marker */
865             for (v = item->display; v != NULL; v = v->next) {
866               sp_marker_hide ((SPMarker *) (shape->marker[i]), NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
867               /* fixme: Do we need explicit remove here? (Lauris) */
868               /* nr_arena_item_set_mask (v->arenaitem, NULL); */
869             }
870             /* Detach marker */
871             sp_signal_disconnect_by_data (shape->marker[i], item);
872             shape->marker[i] = sp_object_hunref (shape->marker[i], item);
873           }
874         }
877 /**
878  * No-op.  Exists for handling 'modified' messages
879  */
880 static void
881 sp_shape_marker_modified (SPObject *marker, guint flags, SPItem *item)
883         /* I think mask does update automagically */
884         /* g_warning ("Item %s mask %s modified", SP_OBJECT_ID (item), SP_OBJECT_ID (mask)); */
887 /**
888  * Adds a new marker to shape object at the location indicated by key.  value 
889  * must be a valid URI reference resolvable from the shape object (i.e., present
890  * in the document <defs>).  If the shape object already has a marker
891  * registered at the given position, it is removed first.  Then the
892  * new marker is hrefed and its signals connected.
893  */
894 void
895 sp_shape_set_marker (SPObject *object, unsigned int key, const gchar *value)
897     SPItem *item = (SPItem *) object;
898     SPShape *shape = (SPShape *) object;
900     if (key < SP_MARKER_LOC_START || key > SP_MARKER_LOC_END) {
901         return;
902     }
904     SPObject *mrk = sp_uri_reference_resolve (SP_OBJECT_DOCUMENT (object), value);
905     if (mrk != shape->marker[key]) {
906         if (shape->marker[key]) {
907             SPItemView *v;
909             /* Detach marker */
910             shape->release_connect[key].disconnect();
911             shape->modified_connect[key].disconnect();
913             /* Hide marker */
914             for (v = item->display; v != NULL; v = v->next) {
915                 sp_marker_hide ((SPMarker *) (shape->marker[key]),
916                                 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + key);
917                 /* fixme: Do we need explicit remove here? (Lauris) */
918                 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
919             }
921             /* Unref marker */
922             shape->marker[key] = sp_object_hunref (shape->marker[key], object);
923         }
924         if (SP_IS_MARKER (mrk)) {
925             shape->marker[key] = sp_object_href (mrk, object);
926             shape->release_connect[key] = mrk->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_shape_marker_release), shape));
927             shape->modified_connect[key] = mrk->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_shape_marker_modified), shape));
928         }
929     }
934 /* Shape section */
936 /**
937  * Calls any registered handlers for the set_shape action
938  */
939 void
940 sp_shape_set_shape (SPShape *shape)
942         g_return_if_fail (shape != NULL);
943         g_return_if_fail (SP_IS_SHAPE (shape));
945         if (SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape) {
946           SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape (shape);
947         }
950 /**
951  * Adds a curve to the shape.  If owner is specified, a reference
952  * will be made, otherwise the curve will be copied into the shape.
953  * Any existing curve in the shape will be unreferenced first.
954  * This routine also triggers a request to update the display.
955  */
956 void
957 sp_shape_set_curve (SPShape *shape, SPCurve *curve, unsigned int owner)
959         if (shape->curve) {
960                 shape->curve = sp_curve_unref (shape->curve);
961         }
962         if (curve) {
963                 if (owner) {
964                         shape->curve = sp_curve_ref (curve);
965                 } else {
966                         shape->curve = sp_curve_copy (curve);
967                 }
968         }
969         SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
972 /**
973  * Return duplicate of curve (if any exists) or NULL if there is no curve
974  */
975 SPCurve *
976 sp_shape_get_curve (SPShape *shape)
978         if (shape->curve) {
979                 return sp_curve_copy (shape->curve);
980         }
981         return NULL;
984 /* NOT FOR GENERAL PUBLIC UNTIL SORTED OUT (Lauris) */
985 void
986 sp_shape_set_curve_insync (SPShape *shape, SPCurve *curve, unsigned int owner)
988         if (shape->curve) {
989                 shape->curve = sp_curve_unref (shape->curve);
990         }
991         if (curve) {
992                 if (owner) {
993                         shape->curve = sp_curve_ref (curve);
994                 } else {
995                         shape->curve = sp_curve_copy (curve);
996                 }
997         }
1000 /**
1001  * Sets the snappoint p to the end point of the path segment
1002  */
1003 static void sp_shape_snappoints(SPItem const *item, SnapPointsIter p)
1005     g_assert(item != NULL);
1006     g_assert(SP_IS_SHAPE(item));
1008     SPShape const *shape = SP_SHAPE(item);
1009     if (shape->curve == NULL) {
1010         return;
1011     }
1013     NR::Matrix const i2d (sp_item_i2d_affine (item));
1015     /* Use the end points of each segment of the path */
1016     NArtBpath const *bp = SP_CURVE_BPATH(shape->curve);
1017     while (bp->code != NR_END) {
1018         *p = bp->c(3) * i2d;
1019         bp++;
1020     }
1024 /*
1025   Local Variables:
1026   mode:c++
1027   c-file-style:"stroustrup"
1028   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1029   indent-tabs-mode:nil
1030   fill-column:99
1031   End:
1032 */
1033 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :