Code

switch to sigc++ SPObject signals for SPShape
[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 "marker-status.h"
35 #include "style.h"
36 #include "sp-marker.h"
37 #include "sp-path.h"
38 #include "prefs-utils.h"
40 #define noSHAPE_VERBOSE
42 static void sp_shape_class_init (SPShapeClass *klass);
43 static void sp_shape_init (SPShape *shape);
44 static void sp_shape_finalize (GObject *object);
46 static void sp_shape_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
47 static void sp_shape_release (SPObject *object);
49 static void sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags);
50 static void sp_shape_modified (SPObject *object, unsigned int flags);
52 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
53 void sp_shape_print (SPItem * item, SPPrintContext * ctx);
54 static NRArenaItem *sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
55 static void sp_shape_hide (SPItem *item, unsigned int key);
56 static void sp_shape_snappoints (SPItem const *item, SnapPointsIter p);
58 static void sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai);
60 static SPItemClass *parent_class;
62 /**
63  * Registers the SPShape class with Gdk and returns its type number.
64  */
65 GType
66 sp_shape_get_type (void)
67 {
68         static GType type = 0;
69         if (!type) {
70                 GTypeInfo info = {
71                         sizeof (SPShapeClass),
72                         NULL, NULL,
73                         (GClassInitFunc) sp_shape_class_init,
74                         NULL, NULL,
75                         sizeof (SPShape),
76                         16,
77                         (GInstanceInitFunc) sp_shape_init,
78                         NULL,   /* value_table */
79                 };
80                 type = g_type_register_static (SP_TYPE_ITEM, "SPShape", &info, (GTypeFlags)0);
81         }
82         return type;
83 }
85 /**
86  * Initializes a SPShapeClass object.  Establishes the function pointers to the class'
87  * member routines in the class vtable, and sets pointers to parent classes.
88  */
89 static void
90 sp_shape_class_init (SPShapeClass *klass)
91 {
92         GObjectClass *gobject_class;
93         SPObjectClass *sp_object_class;
94         SPItemClass * item_class;
95         SPPathClass * path_class;
97         gobject_class = (GObjectClass *) klass;
98         sp_object_class = (SPObjectClass *) klass;
99         item_class = (SPItemClass *) klass;
100         path_class = (SPPathClass *) klass;
102         parent_class = (SPItemClass *)g_type_class_peek_parent (klass);
104         gobject_class->finalize = sp_shape_finalize;
106         sp_object_class->build = sp_shape_build;
107         sp_object_class->release = sp_shape_release;
108         sp_object_class->update = sp_shape_update;
109         sp_object_class->modified = sp_shape_modified;
111         item_class->bbox = sp_shape_bbox;
112         item_class->print = sp_shape_print;
113         item_class->show = sp_shape_show;
114         item_class->hide = sp_shape_hide;
115         item_class->snappoints = sp_shape_snappoints;
118 /**
119  * Initializes an SPShape object.
120  */
121 static void
122 sp_shape_init (SPShape *shape)
124     for ( int i = 0 ; i < SP_MARKER_LOC_QTY ; i++ ) {
125         new (&shape->release_connect[i]) sigc::connection();
126         new (&shape->modified_connect[i]) sigc::connection();
127     }
130 static void
131 sp_shape_finalize (GObject *object)
133     SPShape *shape=(SPShape *)object;
135     for ( int i = 0 ; i < SP_MARKER_LOC_QTY ; i++ ) {
136         shape->release_connect[i].disconnect();
137         shape->release_connect[i].~connection();
138         shape->modified_connect[i].disconnect();
139         shape->modified_connect[i].~connection();
140     }
142     if (((GObjectClass *) (parent_class))->finalize) {
143         (* ((GObjectClass *) (parent_class))->finalize)(object);
144     }
147 /**
148  * Virtual build callback for SPMarker.
149  *
150  * This is to be invoked immediately after creation of an SPShape.  This is 
151  * just a stub.
152  *
153  * \see sp_object_build()
154  */
155 static void
156 sp_shape_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
158         if (((SPObjectClass *) (parent_class))->build) {
159           (*((SPObjectClass *) (parent_class))->build) (object, document, repr);
160         }
163 /**
164  * Removes, releases and unrefs all children of object
165  *
166  * This is the inverse of sp_shape_build().  It must be invoked as soon
167  * as the shape is removed from the tree, even if it is still referenced
168  * by other objects.  This routine also disconnects/unrefs markers and
169  * curves attached to it.
170  *
171  * \see sp_object_release()
172  */
173 static void
174 sp_shape_release (SPObject *object)
176         SPItem *item;
177         SPShape *shape;
178         SPItemView *v;
179         int i;
181         item = (SPItem *) object;
182         shape = (SPShape *) object;
184         for (i=SP_MARKER_LOC_START; i<SP_MARKER_LOC_QTY; i++) {
185           if (shape->marker[i]) {
186             sp_signal_disconnect_by_data (shape->marker[i], object);
187             for (v = item->display; v != NULL; v = v->next) {
188               sp_marker_hide ((SPMarker *) shape->marker[i], NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
189             }
190             shape->marker[i] = sp_object_hunref (shape->marker[i], object);
191           }
192         }
193         if (shape->curve) {
194                 shape->curve = sp_curve_unref (shape->curve);
195         }
197         if (((SPObjectClass *) parent_class)->release) {
198           ((SPObjectClass *) parent_class)->release (object);
199         }
202 /** 
203  * Updates the shape when its attributes have changed.  Also establishes
204  * marker objects to match the style settings.  
205  */
206 static void
207 sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags)
209     SPItem *item = (SPItem *) object;
210     SPShape *shape = (SPShape *) object;
212         if (((SPObjectClass *) (parent_class))->update) {
213           (* ((SPObjectClass *) (parent_class))->update) (object, ctx, flags);
214         }
216         /* This stanza checks that an object's marker style agrees with
217          * the marker objects it has allocated.  sp_shape_set_marker ensures
218          * that the appropriate marker objects are present (or absent) to
219          * match the style.
220          */
221         /* TODO:  It would be nice if this could be done at an earlier level */
222         for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
223             sp_shape_set_marker (object, i, object->style->marker[i].value);
224           }
226         if (flags & (SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
227                 SPStyle *style;
228                 style = SP_OBJECT_STYLE (object);
229                 if (style->stroke_width.unit == SP_CSS_UNIT_PERCENT) {
230                         SPItemCtx *ictx = (SPItemCtx *) ctx;
231                         double const aw = 1.0 / NR::expansion(ictx->i2vp);
232                         style->stroke_width.computed = style->stroke_width.value * aw;
233                         for (SPItemView *v = ((SPItem *) (shape))->display; v != NULL; v = v->next) {
234                                 nr_arena_shape_set_style ((NRArenaShape *) v->arenaitem, style);
235                         }
236                 }
237         }
239         if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
240                 /* This is suboptimal, because changing parent style schedules recalculation */
241                 /* But on the other hand - how can we know that parent does not tie style and transform */
242                 NR::Rect const paintbox = SP_ITEM(object)->invokeBbox(NR::identity());
243                 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
244                     NRArenaShape * const s = NR_ARENA_SHAPE(v->arenaitem);
245                     if (flags & SP_OBJECT_MODIFIED_FLAG) {
246                         nr_arena_shape_set_path(s, shape->curve, (flags & SP_OBJECT_USER_MODIFIED_FLAG_B));
247                     }
248                     s->setPaintBox(paintbox);
249                 }
250         }
252         if (sp_shape_has_markers (shape)) {
254             /* Dimension marker views */
255             for (SPItemView *v = item->display; v != NULL; v = v->next) {
257                 if (!v->arenaitem->key) {
258                     /* Get enough keys for all, start, mid and end marker types,
259                     ** and set this view's arenaitem key to the first of these keys.
260                     */
261                     NR_ARENA_ITEM_SET_KEY (
262                         v->arenaitem,
263                         sp_item_display_key_new (SP_MARKER_LOC_QTY)
264                         );
265                 }
267                 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
268                     if (shape->marker[i]) {
269                         sp_marker_show_dimension ((SPMarker *) shape->marker[i],
270                                                   NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i - SP_MARKER_LOC,
271                                                   sp_shape_number_of_markers (shape, i));
272                     }
273                 }
274             }
276             /* Update marker views */
277             for (SPItemView *v = item->display; v != NULL; v = v->next) {
278                 sp_shape_update_marker_view (shape, v->arenaitem);
279             }
280         }
284 /**
285 * Works out whether a marker of a given type is required at a particular
286 * point on a shape.
288 * \param shape Shape of interest.
289 * \param m Marker type (e.g. SP_MARKER_LOC_START)
290 * \param bp Path segment.
291 * \return 1 if a marker is required here, otherwise 0.
292 */
293 bool
294 sp_shape_marker_required(SPShape const *shape, int const m, NArtBpath *bp)
296     if (shape->marker[m] == NULL) {
297         return false;
298     }
300     if (bp == SP_CURVE_BPATH(shape->curve))
301         return m == SP_MARKER_LOC_START;
302     else if (bp[1].code == NR_END)
303         return m == SP_MARKER_LOC_END;
304     else
305         return m == SP_MARKER_LOC_MID;
308 static bool
309 is_moveto(NRPathcode const c)
311     return c == NR_MOVETO || c == NR_MOVETO_OPEN;
314 /** 
315  * Helper function that advances a subpath's bpath to the first subpath
316  * by checking for moveto segments.
317  *
318  * \pre The bpath[] containing bp begins with a moveto. 
319  */
320 static NArtBpath const *
321 first_seg_in_subpath(NArtBpath const *bp)
323     while (!is_moveto(bp->code)) {
324         --bp;
325     }
326     return bp;
329 /**
330  * Advances the bpath to the last segment in the subpath.
331  */
332 static NArtBpath const *
333 last_seg_in_subpath(NArtBpath const *bp)
335     for(;;) {
336         ++bp;
337         switch (bp->code) {
338             case NR_MOVETO:
339             case NR_MOVETO_OPEN:
340             case NR_END:
341                 --bp;
342                 return bp;
344             default: continue;
345         }
346     }
350 /* A subpath begins with a moveto and ends immediately before the next moveto or NR_END.
351  * (`moveto' here means either NR_MOVETO or NR_MOVETO_OPEN.)  I'm assuming that non-empty
352  * paths always begin with a moveto.
353  *
354  * The control points of the subpath are the control points of the path elements of the subpath.
355  *
356  * As usual, the control points of a moveto or NR_LINETO are {c(3)}, and
357  * the control points of a NR_CURVETO are {c(1), c(2), c(3)}.
358  * (It follows from the definition that NR_END isn't part of a subpath.)
359  *
360  * The initial control point is bpath[bi0].c(3).
361  *
362  * Reference: http://www.w3.org/TR/SVG11/painting.html#MarkerElement, the `orient' attribute.
363  * Reference for behaviour of zero-length segments:
364  * http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
365  */
367 static double const no_tangent = 128.0;  /* arbitrarily-chosen value outside the range of atan2, i.e. outside of [-pi, pi]. */
369 /**
370  * Helper function to calculate the outgoing tangent of a path 
371  * ( atan2(other - p0) )
372  * \pre The bpath[] containing bp0 begins with a moveto. 
373  */
374 static double
375 outgoing_tangent(NArtBpath const *bp0)
377     /* See notes in comment block above. */
379     g_assert(bp0->code != NR_END);
380     NR::Point const &p0 = bp0->c(3);
381     NR::Point other;
382     for (NArtBpath const *bp = bp0;;) {
383         ++bp;
384         switch (bp->code) {
385             case NR_LINETO:
386                 other = bp->c(3);
387                 if (other != p0) {
388                     goto found;
389                 }
390                 break;
392             case NR_CURVETO:
393                 for (unsigned ci = 1; ci <= 3; ++ci) {
394                     other = bp->c(ci);
395                     if (other != p0) {
396                         goto found;
397                     }
398                 }
399                 break;
401             case NR_MOVETO_OPEN:
402             case NR_END:
403             case NR_MOVETO:
404                 bp = first_seg_in_subpath(bp0);
405                 if (bp == bp0) {
406                     /* Gone right around the subpath without finding any different point since the
407                      * initial moveto. */
408                     return no_tangent;
409                 }
410                 if (bp->code != NR_MOVETO) {
411                     /* Open subpath. */
412                     return no_tangent;
413                 }
414                 other = bp->c(3);
415                 if (other != p0) {
416                     goto found;
417                 }
418                 break;
419         }
421         if (bp == bp0) {
422             /* Back where we started, so zero-length subpath. */
423             return no_tangent;
425             /* Note: this test must come after we've looked at element bp, in case bp0 is a curve:
426              * we must look at c(1) and c(2).  (E.g. single-curve subpath.)
427              */
428         }
429     }
431 found:
432     return atan2( other - p0 );
435 /**
436  * Helper function to calculate the incoming tangent of a path
437  * ( atan2(p0 - other) )
438  * 
439  * \pre The bpath[] containing bp0 begins with a moveto. 
440  */
441 static double
442 incoming_tangent(NArtBpath const *bp0)
444     /* See notes in comment block before outgoing_tangent. */
446     g_assert(bp0->code != NR_END);
447     NR::Point const &p0 = bp0->c(3);
448     NR::Point other;
449     for (NArtBpath const *bp = bp0;;) {
450         switch (bp->code) {
451             case NR_LINETO:
452                 other = bp->c(3);
453                 if (other != p0) {
454                     goto found;
455                 }
456                 --bp;
457                 break;
459             case NR_CURVETO:
460                 for (unsigned ci = 3; ci != 0; --ci) {
461                     other = bp->c(ci);
462                     if (other != p0) {
463                         goto found;
464                     }
465                 }
466                 --bp;
467                 break;
469             case NR_MOVETO:
470             case NR_MOVETO_OPEN:
471                 other = bp->c(3);
472                 if (other != p0) {
473                     goto found;
474                 }
475                 if (bp->code != NR_MOVETO) {
476                     /* Open subpath. */
477                     return no_tangent;
478                 }
479                 bp = last_seg_in_subpath(bp0);
480                 break;
482             default: /* includes NR_END */
483                 g_error("Found invalid path code %u in middle of path.", bp->code);
484                 return no_tangent;
485         }
487         if (bp == bp0) {
488             /* Back where we started from: zero-length subpath. */
489             return no_tangent;
490         }
491     }
493 found:
494     return atan2( p0 - other );
498 /**
499  * Calculate the transform required to get a marker's path object in the
500  * right place for particular path segment on a shape.  You should
501  * call sp_shape_marker_required first to see if a marker is required
502  * at this point.
503  *
504  * \see sp_shape_marker_required.
505  *
506  * \param shape Shape which the marker is for.
507  * \param m Marker type (e.g. SP_MARKER_LOC_START)
508  * \param bp Path segment which the arrow is for.
509  * \return Transform matrix.
510  */
511 NR::Matrix
512 sp_shape_marker_get_transform(SPShape const *shape, NArtBpath const *bp)
514     g_return_val_if_fail(( is_moveto(SP_CURVE_BPATH(shape->curve)[0].code)
515                            && ( 0 < shape->curve->end )
516                            && ( SP_CURVE_BPATH(shape->curve)[shape->curve->end].code == NR_END ) ),
517                          NR::Matrix(NR::translate(bp->c(3))));
518     double const angle1 = incoming_tangent(bp);
519     double const angle2 = outgoing_tangent(bp);
521     /* angle1 and angle2 are now each either unset (i.e. still 100 from their initialization) or in
522        [-pi, pi] from atan2. */
523     g_assert((-3.15 < angle1 && angle1 < 3.15) || (angle1 == no_tangent));
524     g_assert((-3.15 < angle2 && angle2 < 3.15) || (angle2 == no_tangent));
526     double ret_angle;
527     if (angle1 == no_tangent) {
528         /* First vertex of an open subpath. */
529         ret_angle = ( angle2 == no_tangent
530                       ? 0.
531                       : angle2 );
532     } else if (angle2 == no_tangent) {
533         /* Last vertex of an open subpath. */
534         ret_angle = angle1;
535     } else {
536         ret_angle = .5 * (angle1 + angle2);
538         if ( fabs( angle2 - angle1 ) > M_PI ) {
539             /* ret_angle is in the middle of the larger of the two sectors between angle1 and
540              * angle2, so flip it by 180degrees to force it to the middle of the smaller sector.
541              *
542              * (Imagine a circle with rays drawn at angle1 and angle2 from the centre of the
543              * circle.  Those two rays divide the circle into two sectors.)
544              */
545             ret_angle += M_PI;
546         }
547     }
549     return NR::Matrix(NR::rotate(ret_angle)) * NR::translate(bp->c(3));
552 /**
553  * Updates the instances (views) of a given marker in a shape.
554  * Marker views have to be scaled already.  The transformation
555  * is retrieved and then shown by calling sp_marker_show_instance.
556  */
557 static void
558 sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai)
560         SPStyle *style = ((SPObject *) shape)->style;
562         marker_status("sp_shape_update_marker_view:  Updating views of markers");
564         for (int i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
565             if (shape->marker[i] == NULL) {
566                 continue;
567             }
569             int n = 0;
571             for (NArtBpath *bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
572                 if (sp_shape_marker_required (shape, i, bp)) {
573                     NR::Matrix const m(sp_shape_marker_get_transform(shape, bp));
574                     sp_marker_show_instance ((SPMarker* ) shape->marker[i], ai,
575                                              NR_ARENA_ITEM_GET_KEY(ai) + i, n, m,
576                                              style->stroke_width.computed);
577                     n++;
578                 }
579             }
580         }
583 /**
584  * Sets modified flag for all sub-item views.
585  */
586 static void
587 sp_shape_modified (SPObject *object, unsigned int flags)
589         SPShape *shape = SP_SHAPE (object);
591         if (((SPObjectClass *) (parent_class))->modified) {
592           (* ((SPObjectClass *) (parent_class))->modified) (object, flags);
593         }
595         if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
596                 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
597                         nr_arena_shape_set_style (NR_ARENA_SHAPE (v->arenaitem), object->style);
598                 }
599         }
602 /**
603  * Calculates the bounding box for item, storing it into bbox.
604  * This also includes the bounding boxes of any markers included in the shape.
605  */
606 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
608     SPShape const *shape = SP_SHAPE (item);
610     if (shape->curve) {
612         NRRect  cbbox;
613         NRBPath bp;
615         bp.path = SP_CURVE_BPATH (shape->curve);
617         cbbox.x0 = cbbox.y0 = NR_HUGE;
618         cbbox.x1 = cbbox.y1 = -NR_HUGE;
620         nr_path_matrix_bbox_union(&bp, transform, &cbbox);
622         SPStyle* style=SP_OBJECT_STYLE (item);
623         if (style->stroke.type != SP_PAINT_TYPE_NONE) {
624             double const scale = expansion(transform);
625             if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
626                 double const width = MAX(0.125, style->stroke_width.computed * scale);
627                 if ( fabs(cbbox.x1-cbbox.x0) > -0.00001 && fabs(cbbox.y1-cbbox.y0) > -0.00001 ) {
628                     cbbox.x0-=0.5*width;
629                     cbbox.x1+=0.5*width;
630                     cbbox.y0-=0.5*width;
631                     cbbox.y1+=0.5*width;
632                 }
633             }
634         }
636         // Union with bboxes of the markers, if any
637         if (sp_shape_has_markers (shape)) {
638             for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
639                 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
640                     if (sp_shape_marker_required (shape, m, bp)) {
642                         SPMarker* marker = SP_MARKER (shape->marker[m]);
643                         SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
645                         NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
647                         if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
648                             tr = NR::scale(style->stroke_width.computed) * tr;
649                         }
651                         // total marker transform
652                         tr = marker_item->transform * marker->c2p * tr * transform;
654                         // get bbox of the marker with that transform
655                         NRRect marker_bbox;
656                         sp_item_invoke_bbox (marker_item, &marker_bbox, tr, true);
657                         // union it with the shape bbox
658                         nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
659                     }
660                 }
661             }
662         }
664         // copy our bbox to the variable we're given
665         *bbox = cbbox;
666     }
669 /**
670  * Prepares shape for printing.  Handles printing of comments for printing
671  * debugging, sizes the item to fit into the document width/height,
672  * applies print fill/stroke, sets transforms for markers, and adds
673  * comment labels.
674  */
675 void
676 sp_shape_print (SPItem *item, SPPrintContext *ctx)
678         NRRect pbox, dbox, bbox;
680         SPShape *shape = SP_SHAPE(item);
682         if (!shape->curve) return;
684         gint add_comments = prefs_get_int_attribute_limited ("printing.debug", "add-label-comments", 0, 0, 1);
685         if (add_comments) {
686             gchar * comment = g_strdup_printf("begin '%s'",
687                                               SP_OBJECT(item)->defaultLabel());
688             sp_print_comment(ctx, comment);
689             g_free(comment);
690         }
692         /* fixme: Think (Lauris) */
693         sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
694         dbox.x0 = 0.0;
695         dbox.y0 = 0.0;
696         dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
697         dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
698         sp_item_bbox_desktop (item, &bbox);
699         NR::Matrix const i2d = sp_item_i2d_affine(item);
701         SPStyle* style = SP_OBJECT_STYLE (item);
703         if (style->fill.type != SP_PAINT_TYPE_NONE) {
704                 NRBPath bp;
705                 bp.path = SP_CURVE_BPATH(shape->curve);
706                 sp_print_fill (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
707         }
709         if (style->stroke.type != SP_PAINT_TYPE_NONE) {
710                 NRBPath bp;
711                 bp.path = SP_CURVE_BPATH(shape->curve);
712                 sp_print_stroke (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
713         }
715         for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
716             for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
717                 if (sp_shape_marker_required (shape, m, bp)) {
719                     SPMarker* marker = SP_MARKER (shape->marker[m]);
720                     SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
722                     NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
724                     if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
725                         tr = NR::scale(style->stroke_width.computed) * tr;
726                     }
728                     tr = marker_item->transform * marker->c2p * tr;
730                     NR::Matrix old_tr = marker_item->transform;
731                     marker_item->transform = tr;
732                     sp_item_invoke_print (marker_item, ctx);
733                     marker_item->transform = old_tr;
734                 }
735             }
736         }
738         if (add_comments) {
739             gchar * comment = g_strdup_printf("end '%s'",
740                                               SP_OBJECT(item)->defaultLabel());
741             sp_print_comment(ctx, comment);
742             g_free(comment);
743         }
746 /**
747  * Sets style, path, and paintbox.  Updates marker views, including dimensions.
748  */
749 static NRArenaItem *
750 sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
752         SPObject *object = SP_OBJECT(item);
753         SPShape *shape = SP_SHAPE(item);
755         NRArenaItem *arenaitem = NRArenaShape::create(arena);
756         NRArenaShape * const s = NR_ARENA_SHAPE(arenaitem);
757         nr_arena_shape_set_style(s, object->style);
758         nr_arena_shape_set_path(s, shape->curve, false);
759         NR::Rect const paintbox = item->invokeBbox(NR::identity());
760         s->setPaintBox(paintbox);
762         if (sp_shape_has_markers (shape)) {
764             /* Dimension the marker views */
765             if (!arenaitem->key) {
766                 NR_ARENA_ITEM_SET_KEY (arenaitem, sp_item_display_key_new (SP_MARKER_LOC_QTY));
767             }
769             for (int i = 0; i < SP_MARKER_LOC_QTY; i++) {
770                 if (shape->marker[i]) {
771                     sp_marker_show_dimension ((SPMarker *) shape->marker[i],
772                                               NR_ARENA_ITEM_GET_KEY (arenaitem) + i - SP_MARKER_LOC,
773                                               sp_shape_number_of_markers (shape, i));
774                 }
775             }
778             /* Update marker views */
779             sp_shape_update_marker_view (shape, arenaitem);
780         }
782         return arenaitem;
785 /**
786  * Hides/removes marker views from the shape.
787  */
788 static void
789 sp_shape_hide (SPItem *item, unsigned int key)
791         SPShape *shape;
792         SPItemView *v;
793         int i;
795         shape = (SPShape *) item;
797         for (i=0; i<SP_MARKER_LOC_QTY; i++) {
798           if (shape->marker[i]) {
799             for (v = item->display; v != NULL; v = v->next) {
800                 if (key == v->key) {
801               sp_marker_hide ((SPMarker *) shape->marker[i],
802                                     NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
803                 }
804             }
805           }
806         }
808         if (((SPItemClass *) parent_class)->hide) {
809           ((SPItemClass *) parent_class)->hide (item, key);
810         }
813 /**
814 * \param shape Shape.
815 * \return TRUE if the shape has any markers, or FALSE if not.
816 */
817 int
818 sp_shape_has_markers (SPShape const *shape)
820     /* Note, we're ignoring 'marker' settings, which technically should apply for
821        all three settings.  This should be fixed later such that if 'marker' is
822        specified, then all three should appear. */
824     return (
825         shape->curve &&
826         (shape->marker[SP_MARKER_LOC_START] ||
827          shape->marker[SP_MARKER_LOC_MID] ||
828          shape->marker[SP_MARKER_LOC_END])
829         );
833 /**
834 * \param shape Shape.
835 * \param type Marker type (e.g. SP_MARKER_LOC_START)
836 * \return Number of markers that the shape has of this type.
837 */
838 int
839 sp_shape_number_of_markers (SPShape *shape, int type)
841     int n = 0;
842     for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
843         if (sp_shape_marker_required (shape, type, bp)) {
844             n++;
845         }
846     }
848     return n;
851 /**
852  * Checks if the given marker is used in the shape, and if so, it
853  * releases it by calling sp_marker_hide.  Also detaches signals
854  * and unrefs the marker from the shape.
855  */
856 static void
857 sp_shape_marker_release (SPObject *marker, SPShape *shape)
859         SPItem *item;
860         int i;
862         item = (SPItem *) shape;
864         marker_status("sp_shape_marker_release:  Releasing markers");
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 :