Code

remove many unnecessary to_2geom and from_2geom calls
[inkscape.git] / src / sp-shape.cpp
1 /*
2  * Base class for shapes, including <path> element
3  *
4  * Author:
5  *   Lauris Kaplinski <lauris@kaplinski.com>
6  *
7  * Copyright (C) 1999-2002 Lauris Kaplinski
8  * Copyright (C) 2000-2001 Ximian, Inc.
9  * Copyright (C) 2004 John Cliff
10  * Copyright (C) 2007-2008 Johan Engelen
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
19 #include <libnr/nr-matrix-fns.h>
20 #include <libnr/nr-matrix-ops.h>
21 #include <libnr/nr-matrix-translate-ops.h>
22 #include <libnr/nr-scale-matrix-ops.h>
23 #include <2geom/rect.h>
24 #include <2geom/transforms.h>
25 #include <2geom/pathvector.h>
26 #include "helper/geom.h"
27 #include "helper/geom-nodetype.h"
29 #include <sigc++/functors/ptr_fun.h>
30 #include <sigc++/adaptors/bind.h>
32 #include "macros.h"
33 #include "display/nr-arena-shape.h"
34 #include "display/curve.h"
35 #include "print.h"
36 #include "document.h"
37 #include "style.h"
38 #include "marker.h"
39 #include "sp-path.h"
40 #include "prefs-utils.h"
41 #include "attributes.h"
43 #include "live_effects/lpeobject.h"
44 #include "uri.h"
45 #include "extract-uri.h"
46 #include "uri-references.h"
47 #include "bad-uri-exception.h"
48 #include "xml/repr.h"
50 #include "util/mathfns.h" // for triangle_area()
52 #define noSHAPE_VERBOSE
54 static void sp_shape_class_init (SPShapeClass *klass);
55 static void sp_shape_init (SPShape *shape);
56 static void sp_shape_finalize (GObject *object);
58 static void sp_shape_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
59 static void sp_shape_release (SPObject *object);
61 static void sp_shape_set(SPObject *object, unsigned key, gchar const *value);
62 static void sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags);
63 static void sp_shape_modified (SPObject *object, unsigned int flags);
64 static Inkscape::XML::Node *sp_shape_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
66 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
67 void sp_shape_print (SPItem * item, SPPrintContext * ctx);
68 static NRArenaItem *sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
69 static void sp_shape_hide (SPItem *item, unsigned int key);
70 static void sp_shape_snappoints (SPItem const *item, SnapPointsIter p);
72 static void sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai);
74 static SPLPEItemClass *parent_class;
76 /**
77  * Registers the SPShape class with Gdk and returns its type number.
78  */
79 GType
80 sp_shape_get_type (void)
81 {
82         static GType type = 0;
83         if (!type) {
84                 GTypeInfo info = {
85                         sizeof (SPShapeClass),
86                         NULL, NULL,
87                         (GClassInitFunc) sp_shape_class_init,
88                         NULL, NULL,
89                         sizeof (SPShape),
90                         16,
91                         (GInstanceInitFunc) sp_shape_init,
92                         NULL,   /* value_table */
93                 };
94                 type = g_type_register_static (SP_TYPE_LPE_ITEM, "SPShape", &info, (GTypeFlags)0);
95         }
96         return type;
97 }
99 /**
100  * Initializes a SPShapeClass object.  Establishes the function pointers to the class'
101  * member routines in the class vtable, and sets pointers to parent classes.
102  */
103 static void
104 sp_shape_class_init (SPShapeClass *klass)
106     GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
107     SPObjectClass *sp_object_class = SP_OBJECT_CLASS(klass);
108     SPItemClass * item_class = SP_ITEM_CLASS(klass);
109     SPLPEItemClass * lpe_item_class = SP_LPE_ITEM_CLASS(klass);
111     parent_class = (SPLPEItemClass *)g_type_class_peek_parent (klass);
113     gobject_class->finalize = sp_shape_finalize;
115         sp_object_class->build = sp_shape_build;
116         sp_object_class->release = sp_shape_release;
117     sp_object_class->set = sp_shape_set;
118         sp_object_class->update = sp_shape_update;
119         sp_object_class->modified = sp_shape_modified;
120     sp_object_class->write = sp_shape_write;
122         item_class->bbox = sp_shape_bbox;
123         item_class->print = sp_shape_print;
124         item_class->show = sp_shape_show;
125         item_class->hide = sp_shape_hide;
126     item_class->snappoints = sp_shape_snappoints;
127     lpe_item_class->update_patheffect = NULL;
129     klass->set_shape = NULL;
132 /**
133  * Initializes an SPShape object.
134  */
135 static void
136 sp_shape_init (SPShape *shape)
138     for ( int i = 0 ; i < SP_MARKER_LOC_QTY ; i++ ) {
139         new (&shape->release_connect[i]) sigc::connection();
140         new (&shape->modified_connect[i]) sigc::connection();
141     }
144 static void
145 sp_shape_finalize (GObject *object)
147     SPShape *shape=(SPShape *)object;
149     for ( int i = 0 ; i < SP_MARKER_LOC_QTY ; i++ ) {
150         shape->release_connect[i].disconnect();
151         shape->release_connect[i].~connection();
152         shape->modified_connect[i].disconnect();
153         shape->modified_connect[i].~connection();
154     }
156     if (((GObjectClass *) (parent_class))->finalize) {
157         (* ((GObjectClass *) (parent_class))->finalize)(object);
158     }
161 /**
162  * Virtual build callback for SPMarker.
163  *
164  * This is to be invoked immediately after creation of an SPShape.
165  *
166  * \see sp_object_build()
167  */
168 static void
169 sp_shape_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
171     if (((SPObjectClass *) (parent_class))->build) {
172        (*((SPObjectClass *) (parent_class))->build) (object, document, repr);
173     }
176 /**
177  * Removes, releases and unrefs all children of object
178  *
179  * This is the inverse of sp_shape_build().  It must be invoked as soon
180  * as the shape is removed from the tree, even if it is still referenced
181  * by other objects.  This routine also disconnects/unrefs markers and
182  * curves attached to it.
183  *
184  * \see sp_object_release()
185  */
186 static void
187 sp_shape_release (SPObject *object)
189         SPItem *item;
190         SPShape *shape;
191         SPItemView *v;
192         int i;
194         item = (SPItem *) object;
195         shape = (SPShape *) object;
197         for (i=SP_MARKER_LOC_START; i<SP_MARKER_LOC_QTY; i++) {
198           if (shape->marker[i]) {
199             sp_signal_disconnect_by_data (shape->marker[i], object);
200             for (v = item->display; v != NULL; v = v->next) {
201               sp_marker_hide ((SPMarker *) shape->marker[i], NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
202             }
203             shape->marker[i] = sp_object_hunref (shape->marker[i], object);
204           }
205         }
206         if (shape->curve) {
207                 shape->curve = shape->curve->unref();
208         }
209     
210         if (((SPObjectClass *) parent_class)->release) {
211           ((SPObjectClass *) parent_class)->release (object);
212         }
217 static void
218 sp_shape_set(SPObject *object, unsigned int key, gchar const *value)
220     if (((SPObjectClass *) parent_class)->set) {
221         ((SPObjectClass *) parent_class)->set(object, key, value);
222     }
225 static Inkscape::XML::Node *
226 sp_shape_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
228     if (((SPObjectClass *)(parent_class))->write) {
229         ((SPObjectClass *)(parent_class))->write(object, doc, repr, flags);
230     }
232     return repr;
235 /** 
236  * Updates the shape when its attributes have changed.  Also establishes
237  * marker objects to match the style settings.  
238  */
239 static void
240 sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags)
242     SPItem *item = (SPItem *) object;
243     SPShape *shape = (SPShape *) object;
245         if (((SPObjectClass *) (parent_class))->update) {
246           (* ((SPObjectClass *) (parent_class))->update) (object, ctx, flags);
247         }
249         /* This stanza checks that an object's marker style agrees with
250          * the marker objects it has allocated.  sp_shape_set_marker ensures
251          * that the appropriate marker objects are present (or absent) to
252          * match the style.
253          */
254         /* TODO:  It would be nice if this could be done at an earlier level */
255         for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
256             sp_shape_set_marker (object, i, object->style->marker[i].value);
257           }
259         if (flags & (SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
260                 SPStyle *style;
261                 style = SP_OBJECT_STYLE (object);
262                 if (style->stroke_width.unit == SP_CSS_UNIT_PERCENT) {
263                         SPItemCtx *ictx = (SPItemCtx *) ctx;
264                         double const aw = 1.0 / NR::expansion(ictx->i2vp);
265                         style->stroke_width.computed = style->stroke_width.value * aw;
266                         for (SPItemView *v = ((SPItem *) (shape))->display; v != NULL; v = v->next) {
267                                 nr_arena_shape_set_style ((NRArenaShape *) v->arenaitem, style);
268                         }
269                 }
270         }
272         if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
273                 /* This is suboptimal, because changing parent style schedules recalculation */
274                 /* But on the other hand - how can we know that parent does not tie style and transform */
275                 boost::optional<NR::Rect> paintbox = SP_ITEM(object)->getBounds(NR::identity());
276                 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
277                     NRArenaShape * const s = NR_ARENA_SHAPE(v->arenaitem);
278                     if (flags & SP_OBJECT_MODIFIED_FLAG) {
279                         nr_arena_shape_set_path(s, shape->curve, (flags & SP_OBJECT_USER_MODIFIED_FLAG_B));
280                     }
281                     if (paintbox) {
282                         s->setPaintBox(*paintbox);
283                     }
284                 }
285         }
287         if (sp_shape_has_markers (shape)) {
289             /* Dimension marker views */
290             for (SPItemView *v = item->display; v != NULL; v = v->next) {
292                 if (!v->arenaitem->key) {
293                     /* Get enough keys for all, start, mid and end marker types,
294                     ** and set this view's arenaitem key to the first of these keys.
295                     */
296                     NR_ARENA_ITEM_SET_KEY (
297                         v->arenaitem,
298                         sp_item_display_key_new (SP_MARKER_LOC_QTY)
299                         );
300                 }
302                 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
303                     if (shape->marker[i]) {
304                         sp_marker_show_dimension ((SPMarker *) shape->marker[i],
305                                                   NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i - SP_MARKER_LOC,
306                                                   sp_shape_number_of_markers (shape, i));
307                     }
308                 }
309             }
311             /* Update marker views */
312             for (SPItemView *v = item->display; v != NULL; v = v->next) {
313                 sp_shape_update_marker_view (shape, v->arenaitem);
314             }
315         }
318 /**
319  * Calculate the transform required to get a marker's path object in the
320  * right place for particular path segment on a shape.
321  *
322  * \see sp_shape_marker_update_marker_view.
323  *
324  * From SVG spec:
325  * The axes of the temporary new user coordinate system are aligned according to the orient attribute on the 'marker'
326  * element and the slope of the curve at the given vertex. (Note: if there is a discontinuity at a vertex, the slope
327  * is the average of the slopes of the two segments of the curve that join at the given vertex. If a slope cannot be
328  * determined, the slope is assumed to be zero.)
329  *
330  * Reference: http://www.w3.org/TR/SVG11/painting.html#MarkerElement, the `orient' attribute.
331  * Reference for behaviour of zero-length segments:
332  * http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
333  */
334 Geom::Matrix
335 sp_shape_marker_get_transform(Geom::Curve const & c1, Geom::Curve const & c2)
337     Geom::Point p = c1.pointAt(1);
338     Geom::Curve * c1_reverse = c1.reverse();
339     Geom::Point tang1 = - c1_reverse->unitTangentAt(0);
340     delete c1_reverse;
341     Geom::Point tang2 = c2.unitTangentAt(0);
343     double const angle1 = Geom::atan2(tang1);
344     double const angle2 = Geom::atan2(tang2);
346     double ret_angle;
347     ret_angle = .5 * (angle1 + angle2);
349     if ( fabs( angle2 - angle1 ) > M_PI ) {
350         /* ret_angle is in the middle of the larger of the two sectors between angle1 and
351          * angle2, so flip it by 180degrees to force it to the middle of the smaller sector.
352          *
353          * (Imagine a circle with rays drawn at angle1 and angle2 from the centre of the
354          * circle.  Those two rays divide the circle into two sectors.)
355          */
356         ret_angle += M_PI;
357     }
359     return Geom::Rotate(ret_angle) * Geom::Translate(p);
361 Geom::Matrix
362 sp_shape_marker_get_transform_at_start(Geom::Curve const & c)
364     Geom::Point p = c.pointAt(0);
365     Geom::Point tang = c.unitTangentAt(0);
367     double const angle = Geom::atan2(tang);
369     return Geom::Rotate(angle) * Geom::Translate(p);
371 Geom::Matrix
372 sp_shape_marker_get_transform_at_end(Geom::Curve const & c)
374     Geom::Point p = c.pointAt(1);
375     Geom::Curve * c_reverse = c.reverse();
376     Geom::Point tang = - c_reverse->unitTangentAt(0);
377     delete c_reverse;
379     double const angle = Geom::atan2(tang);
381     return Geom::Rotate(angle) * Geom::Translate(p);
384 /**
385  * Updates the instances (views) of a given marker in a shape.
386  * Marker views have to be scaled already.  The transformation
387  * is retrieved and then shown by calling sp_marker_show_instance.
388  *
389  * TODO: correctly handle the 'marker' attribute.
390  * "Using the marker property from a style sheet is equivalent to using all three (start, mid, end)."
391  * See painting-marker-03-f.svg in SVG 1.1 Full test suite.
392  */
393 static void
394 sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai)
396     SPStyle *style = ((SPObject *) shape)->style;
398     // position arguments to sp_marker_show_instance, basically counts the amount of markers.
399     int start_pos = 0;
400     int mid_pos = 0;
401     int end_pos = 0;
403     Geom::PathVector const & pathv = shape->curve->get_pathvector();
404     for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
405         if ( shape->marker[SP_MARKER_LOC_START] ) {
406             Geom::Matrix const m (sp_shape_marker_get_transform_at_start(path_it->front()));
407             sp_marker_show_instance ((SPMarker* ) shape->marker[SP_MARKER_LOC_START], ai,
408                                      NR_ARENA_ITEM_GET_KEY(ai) + SP_MARKER_LOC_START, start_pos, m,
409                                      style->stroke_width.computed);
410              start_pos++;
411         }
413         if ( shape->marker[SP_MARKER_LOC_MID] ) {
414             Geom::Path::const_iterator curve_it1 = path_it->begin();      // incoming curve
415             Geom::Path::const_iterator curve_it2 = ++(path_it->begin());  // outgoing curve
416             while (curve_it2 != path_it->end_default())
417             {
418                 /* Put marker between curve_it1 and curve_it2.
419                  * Loop to end_default (so including closing segment), because when a path is closed,
420                  * there should be a midpoint marker between last segment and closing straight line segment
421                  */
422                 Geom::Matrix const m (sp_shape_marker_get_transform(*curve_it1, *curve_it2));
423                 sp_marker_show_instance ((SPMarker* ) shape->marker[SP_MARKER_LOC_MID], ai,
424                                          NR_ARENA_ITEM_GET_KEY(ai) + SP_MARKER_LOC_MID, mid_pos, m,
425                                          style->stroke_width.computed);
426                 mid_pos++;
428                 ++curve_it1;
429                 ++curve_it2;
430             }
431         }
433         if ( shape->marker[SP_MARKER_LOC_END] ) {
434             Geom::Matrix const m (sp_shape_marker_get_transform_at_end(path_it->back_default()));
435             sp_marker_show_instance ((SPMarker* ) shape->marker[SP_MARKER_LOC_END], ai,
436                                      NR_ARENA_ITEM_GET_KEY(ai) + SP_MARKER_LOC_END, end_pos, m,
437                                      style->stroke_width.computed);
438             end_pos++;
439         }
440     }
443 /**
444  * Sets modified flag for all sub-item views.
445  */
446 static void
447 sp_shape_modified (SPObject *object, unsigned int flags)
449         SPShape *shape = SP_SHAPE (object);
451         if (((SPObjectClass *) (parent_class))->modified) {
452           (* ((SPObjectClass *) (parent_class))->modified) (object, flags);
453         }
455         if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
456                 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
457                         nr_arena_shape_set_style (NR_ARENA_SHAPE (v->arenaitem), object->style);
458                 }
459         }
462 /**
463  * Calculates the bounding box for item, storing it into bbox.
464  * This also includes the bounding boxes of any markers included in the shape.
465  */
466 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
468     SPShape const *shape = SP_SHAPE (item);
470     if (shape->curve) {
472         NRRect  cbbox;
474         Geom::Rect geombbox = bounds_exact_transformed(shape->curve->get_pathvector(), transform);
475         cbbox.x0 = geombbox[0][0];
476         cbbox.y0 = geombbox[1][0];
477         cbbox.x1 = geombbox[0][1];
478         cbbox.y1 = geombbox[1][1];
480         if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
481             
482             SPStyle* style=SP_OBJECT_STYLE (item);
483             if (!style->stroke.isNone()) {
484                 double const scale = expansion(transform);
485                 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
486                     double const width = MAX(0.125, style->stroke_width.computed * scale);
487                     if ( fabs(cbbox.x1-cbbox.x0) > -0.00001 && fabs(cbbox.y1-cbbox.y0) > -0.00001 ) {
488                         cbbox.x0-=0.5*width;
489                         cbbox.x1+=0.5*width;
490                         cbbox.y0-=0.5*width;
491                         cbbox.y1+=0.5*width;
492                     }
493                 }
494             }
496             // Union with bboxes of the markers, if any
497             if (sp_shape_has_markers (shape)) {
498                 /* TODO: make code prettier: lots of variables can be taken out of the loop! */
499                 Geom::PathVector const & pathv = shape->curve->get_pathvector();
500                 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
501                     if ( shape->marker[SP_MARKER_LOC_START] ) {
502                         SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
503                         SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
505                         NR::Matrix tr(sp_shape_marker_get_transform_at_start(path_it->front()));
507                         if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
508                             tr = NR::scale(style->stroke_width.computed) * tr;
509                         }
511                         // total marker transform
512                         tr = marker_item->transform * marker->c2p * tr * transform;
514                         // get bbox of the marker with that transform
515                         NRRect marker_bbox;
516                         sp_item_invoke_bbox (marker_item, &marker_bbox, tr, true);
517                         // union it with the shape bbox
518                         nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
519                     }
521                     if ( shape->marker[SP_MARKER_LOC_MID] ) {
522                         Geom::Path::const_iterator curve_it1 = path_it->begin();      // incoming curve
523                         Geom::Path::const_iterator curve_it2 = ++(path_it->begin());  // outgoing curve
524                         while (curve_it2 != path_it->end_default())
525                         {
526                             /* Put marker between curve_it1 and curve_it2.
527                              * Loop to end_default (so including closing segment), because when a path is closed,
528                              * there should be a midpoint marker between last segment and closing straight line segment */
530                             SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
531                             SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
533                             NR::Matrix tr(sp_shape_marker_get_transform(*curve_it1, *curve_it2));
535                             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
536                                 tr = NR::scale(style->stroke_width.computed) * tr;
537                             }
539                             // total marker transform
540                             tr = marker_item->transform * marker->c2p * tr * transform;
542                             // get bbox of the marker with that transform
543                             NRRect marker_bbox;
544                             sp_item_invoke_bbox (marker_item, &marker_bbox, tr, true);
545                             // union it with the shape bbox
546                             nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
548                             ++curve_it1;
549                             ++curve_it2;
550                         }
551                     }
553                     if ( shape->marker[SP_MARKER_LOC_END] ) {
554                         SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
555                         SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
557                         NR::Matrix tr(sp_shape_marker_get_transform_at_end(path_it->back_default()));
559                         if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
560                             tr = NR::scale(style->stroke_width.computed) * tr;
561                         }
563                         // total marker transform
564                         tr = marker_item->transform * marker->c2p * tr * transform;
566                         // get bbox of the marker with that transform
567                         NRRect marker_bbox;
568                         sp_item_invoke_bbox (marker_item, &marker_bbox, tr, true);
569                         // union it with the shape bbox
570                         nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
571                     }
572                 }
573             }
574         }
576         // copy our bbox to the variable we're given
577         *bbox = cbbox;
578     }
581 /**
582  * Prepares shape for printing.  Handles printing of comments for printing
583  * debugging, sizes the item to fit into the document width/height,
584  * applies print fill/stroke, sets transforms for markers, and adds
585  * comment labels.
586  */
587 void
588 sp_shape_print (SPItem *item, SPPrintContext *ctx)
590         NRRect pbox, dbox, bbox;
592         SPShape *shape = SP_SHAPE(item);
594         if (!shape->curve) return;
596         gint add_comments = prefs_get_int_attribute_limited ("printing.debug", "add-label-comments", 0, 0, 1);
597         if (add_comments) {
598             gchar * comment = g_strdup_printf("begin '%s'",
599                                               SP_OBJECT(item)->defaultLabel());
600             sp_print_comment(ctx, comment);
601             g_free(comment);
602         }
604         /* fixme: Think (Lauris) */
605         sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
606         dbox.x0 = 0.0;
607         dbox.y0 = 0.0;
608         dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
609         dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
610         sp_item_bbox_desktop (item, &bbox);
611         NR::Matrix const i2d(sp_item_i2d_affine(item));
613         SPStyle* style = SP_OBJECT_STYLE (item);
615     if (!style->fill.isNone()) {
616         sp_print_fill (ctx, shape->curve->get_pathvector(), &i2d, style, &pbox, &dbox, &bbox);
617     }
619     if (!style->stroke.isNone()) {
620         sp_print_stroke (ctx, shape->curve->get_pathvector(), &i2d, style, &pbox, &dbox, &bbox);
621     }
623     /* TODO: make code prettier: lots of variables can be taken out of the loop! */
624     Geom::PathVector const & pathv = shape->curve->get_pathvector();
625     for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
626         if ( shape->marker[SP_MARKER_LOC_START] ) {
627             SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
628             SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
630             NR::Matrix tr(sp_shape_marker_get_transform_at_start(path_it->front()));
632             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
633                 tr = NR::scale(style->stroke_width.computed) * tr;
634             }
636             tr = marker_item->transform * marker->c2p * tr;
638             NR::Matrix old_tr = marker_item->transform;
639             marker_item->transform = tr;
640             sp_item_invoke_print (marker_item, ctx);
641             marker_item->transform = old_tr;
642         }
644         if ( shape->marker[SP_MARKER_LOC_MID] ) {
645             Geom::Path::const_iterator curve_it1 = path_it->begin();      // incoming curve
646             Geom::Path::const_iterator curve_it2 = ++(path_it->begin());  // outgoing curve
647             while (curve_it2 != path_it->end_default())
648             {
649                 /* Put marker between curve_it1 and curve_it2.
650                  * Loop to end_default (so including closing segment), because when a path is closed,
651                  * there should be a midpoint marker between last segment and closing straight line segment */
653                 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
654                 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
656                 NR::Matrix tr(sp_shape_marker_get_transform(*curve_it1, *curve_it2));
658                 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
659                     tr = NR::scale(style->stroke_width.computed) * tr;
660                 }
662                 tr = marker_item->transform * marker->c2p * tr;
664                 NR::Matrix old_tr = marker_item->transform;
665                 marker_item->transform = tr;
666                 sp_item_invoke_print (marker_item, ctx);
667                 marker_item->transform = old_tr;
669                 ++curve_it1;
670                 ++curve_it2;
671             }
672         }
674         if ( shape->marker[SP_MARKER_LOC_END] ) {
675             SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
676             SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
678             NR::Matrix tr(sp_shape_marker_get_transform_at_end(path_it->back_default()));
680             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
681                 tr = NR::scale(style->stroke_width.computed) * tr;
682             }
684             tr = marker_item->transform * marker->c2p * tr;
686             NR::Matrix old_tr = marker_item->transform;
687             marker_item->transform = tr;
688             sp_item_invoke_print (marker_item, ctx);
689             marker_item->transform = old_tr;
690         }
691     }
693         if (add_comments) {
694             gchar * comment = g_strdup_printf("end '%s'",
695                                               SP_OBJECT(item)->defaultLabel());
696             sp_print_comment(ctx, comment);
697             g_free(comment);
698         }
701 /**
702  * Sets style, path, and paintbox.  Updates marker views, including dimensions.
703  */
704 static NRArenaItem *
705 sp_shape_show (SPItem *item, NRArena *arena, unsigned int /*key*/, unsigned int /*flags*/)
707         SPObject *object = SP_OBJECT(item);
708         SPShape *shape = SP_SHAPE(item);
710         NRArenaItem *arenaitem = NRArenaShape::create(arena);
711         NRArenaShape * const s = NR_ARENA_SHAPE(arenaitem);
712         nr_arena_shape_set_style(s, object->style);
713         nr_arena_shape_set_path(s, shape->curve, false);
714         boost::optional<NR::Rect> paintbox = item->getBounds(NR::identity());
715         if (paintbox) {
716             s->setPaintBox(*paintbox);
717         }
719         if (sp_shape_has_markers (shape)) {
721             /* Dimension the marker views */
722             if (!arenaitem->key) {
723                 NR_ARENA_ITEM_SET_KEY (arenaitem, sp_item_display_key_new (SP_MARKER_LOC_QTY));
724             }
726             for (int i = 0; i < SP_MARKER_LOC_QTY; i++) {
727                 if (shape->marker[i]) {
728                     sp_marker_show_dimension ((SPMarker *) shape->marker[i],
729                                               NR_ARENA_ITEM_GET_KEY (arenaitem) + i - SP_MARKER_LOC,
730                                               sp_shape_number_of_markers (shape, i));
731                 }
732             }
735             /* Update marker views */
736             sp_shape_update_marker_view (shape, arenaitem);
737         }
739         return arenaitem;
742 /**
743  * Hides/removes marker views from the shape.
744  */
745 static void
746 sp_shape_hide (SPItem *item, unsigned int key)
748         SPShape *shape;
749         SPItemView *v;
750         int i;
752         shape = (SPShape *) item;
754         for (i=0; i<SP_MARKER_LOC_QTY; i++) {
755           if (shape->marker[i]) {
756             for (v = item->display; v != NULL; v = v->next) {
757                 if (key == v->key) {
758               sp_marker_hide ((SPMarker *) shape->marker[i],
759                                     NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
760                 }
761             }
762           }
763         }
765         if (((SPItemClass *) parent_class)->hide) {
766           ((SPItemClass *) parent_class)->hide (item, key);
767         }
770 /**
771 * \param shape Shape.
772 * \return TRUE if the shape has any markers, or FALSE if not.
773 */
774 int
775 sp_shape_has_markers (SPShape const *shape)
777     /* Note, we're ignoring 'marker' settings, which technically should apply for
778        all three settings.  This should be fixed later such that if 'marker' is
779        specified, then all three should appear. */
781     return (
782         shape->curve &&
783         (shape->marker[SP_MARKER_LOC_START] ||
784          shape->marker[SP_MARKER_LOC_MID] ||
785          shape->marker[SP_MARKER_LOC_END])
786         );
790 /**
791 * \param shape Shape.
792 * \param type Marker type (e.g. SP_MARKER_LOC_START)
793 * \return Number of markers that the shape has of this type.
794 */
795 int
796 sp_shape_number_of_markers (SPShape *shape, int type)
798     Geom::PathVector const & pathv = shape->curve->get_pathvector();
800     switch(type) {
801         case SP_MARKER_LOC_START:
802             return shape->marker[SP_MARKER_LOC_START] ? pathv.size() : 0;
804         case SP_MARKER_LOC_MID:
805         {
806             if ( shape->marker[SP_MARKER_LOC_MID] ) {
807             guint n = 0;
808                 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
809                     n += path_it->size();
810                     n += path_it->closed() ? 1 : 0;
811                 }
812                 return n;
813             } else {
814                 return 0;
815             }
816         }
818         case SP_MARKER_LOC_END:
819         {
820             if ( shape->marker[SP_MARKER_LOC_END] ) {
821                 guint n = 0;
822                 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
823                     if (!path_it->empty()) {
824                         n++;
825                     }
826                 }
827                 return n;
828             } else {
829                 return 0;
830             }
831         }
833         default:
834             return 0;
835     }
838 /**
839  * Checks if the given marker is used in the shape, and if so, it
840  * releases it by calling sp_marker_hide.  Also detaches signals
841  * and unrefs the marker from the shape.
842  */
843 static void
844 sp_shape_marker_release (SPObject *marker, SPShape *shape)
846         SPItem *item;
847         int i;
849         item = (SPItem *) shape;
851         for (i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
852           if (marker == shape->marker[i]) {
853             SPItemView *v;
854             /* Hide marker */
855             for (v = item->display; v != NULL; v = v->next) {
856               sp_marker_hide ((SPMarker *) (shape->marker[i]), NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
857               /* fixme: Do we need explicit remove here? (Lauris) */
858               /* nr_arena_item_set_mask (v->arenaitem, NULL); */
859             }
860             /* Detach marker */
861             sp_signal_disconnect_by_data (shape->marker[i], item);
862             shape->marker[i] = sp_object_hunref (shape->marker[i], item);
863           }
864         }
867 /**
868  * No-op.  Exists for handling 'modified' messages
869  */
870 static void
871 sp_shape_marker_modified (SPObject */*marker*/, guint /*flags*/, SPItem */*item*/)
873         /* I think mask does update automagically */
874         /* g_warning ("Item %s mask %s modified", SP_OBJECT_ID (item), SP_OBJECT_ID (mask)); */
877 /**
878  * Adds a new marker to shape object at the location indicated by key.  value 
879  * must be a valid URI reference resolvable from the shape object (i.e., present
880  * in the document <defs>).  If the shape object already has a marker
881  * registered at the given position, it is removed first.  Then the
882  * new marker is hrefed and its signals connected.
883  */
884 void
885 sp_shape_set_marker (SPObject *object, unsigned int key, const gchar *value)
887     SPItem *item = (SPItem *) object;
888     SPShape *shape = (SPShape *) object;
890     if (key < SP_MARKER_LOC_START || key > SP_MARKER_LOC_END) {
891         return;
892     }
894     SPObject *mrk = sp_css_uri_reference_resolve (SP_OBJECT_DOCUMENT (object), value);
895     if (mrk != shape->marker[key]) {
896         if (shape->marker[key]) {
897             SPItemView *v;
899             /* Detach marker */
900             shape->release_connect[key].disconnect();
901             shape->modified_connect[key].disconnect();
903             /* Hide marker */
904             for (v = item->display; v != NULL; v = v->next) {
905                 sp_marker_hide ((SPMarker *) (shape->marker[key]),
906                                 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + key);
907                 /* fixme: Do we need explicit remove here? (Lauris) */
908                 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
909             }
911             /* Unref marker */
912             shape->marker[key] = sp_object_hunref (shape->marker[key], object);
913         }
914         if (SP_IS_MARKER (mrk)) {
915             shape->marker[key] = sp_object_href (mrk, object);
916             shape->release_connect[key] = mrk->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_shape_marker_release), shape));
917             shape->modified_connect[key] = mrk->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_shape_marker_modified), shape));
918         }
919     }
924 /* Shape section */
926 /**
927  * Calls any registered handlers for the set_shape action
928  */
929 void
930 sp_shape_set_shape (SPShape *shape)
932         g_return_if_fail (shape != NULL);
933         g_return_if_fail (SP_IS_SHAPE (shape));
935         if (SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape) {
936           SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape (shape);
937         }
940 /**
941  * Adds a curve to the shape.  If owner is specified, a reference
942  * will be made, otherwise the curve will be copied into the shape.
943  * Any existing curve in the shape will be unreferenced first.
944  * This routine also triggers a request to update the display.
945  */
946 void
947 sp_shape_set_curve (SPShape *shape, SPCurve *curve, unsigned int owner)
949         if (shape->curve) {
950                 shape->curve = shape->curve->unref();
951         }
952         if (curve) {
953                 if (owner) {
954                         shape->curve = curve->ref();
955                 } else {
956                         shape->curve = curve->copy();
957                 }
958         }
959         SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
962 /**
963  * Return duplicate of curve (if any exists) or NULL if there is no curve
964  */
965 SPCurve *
966 sp_shape_get_curve (SPShape *shape)
968         if (shape->curve) {
969                 return shape->curve->copy();
970         }
971         return NULL;
974 /**
975  * Same as sp_shape_set_curve but without updating the display
976  */
977 void
978 sp_shape_set_curve_insync (SPShape *shape, SPCurve *curve, unsigned int owner)
980         if (shape->curve) {
981                 shape->curve = shape->curve->unref();
982         }
983         if (curve) {
984                 if (owner) {
985                         shape->curve = curve->ref();
986                 } else {
987                         shape->curve = curve->copy();
988                 }
989         }
992 /**
993  * Return all nodes in a path that are to be considered for snapping
994  */
995 static void sp_shape_snappoints(SPItem const *item, SnapPointsIter p)
997     g_assert(item != NULL);
998     g_assert(SP_IS_SHAPE(item));
1000     SPShape const *shape = SP_SHAPE(item);
1001     if (shape->curve == NULL) {
1002         return;
1003     }
1005     Geom::PathVector const &pathv = shape->curve->get_pathvector();
1006     if (pathv.empty())
1007         return;
1009     Geom::Matrix const i2d (sp_item_i2d_affine (item));
1011     for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
1012         *p = from_2geom(path_it->initialPoint() * i2d);
1014         Geom::Path::const_iterator curve_it1 = path_it->begin();      // incoming curve
1015         Geom::Path::const_iterator curve_it2 = ++(path_it->begin());  // outgoing curve
1016         while (curve_it2 != path_it->end_closed())
1017         {
1018             /* Test whether to add the node between curve_it1 and curve_it2.
1019              * Loop to end_closed (so always including closing segment), the last node to be added
1020              * is the node between the closing segment and the segment before that one. Regardless
1021              * of the path being closed. If the path is closed, the final point was already added by
1022              * adding the initial point. */
1024             Geom::NodeType nodetype = Geom::get_nodetype(*curve_it1, *curve_it2);
1026             // Only add cusp nodes. TODO: Shouldn't this be a preference instead?
1027             if (nodetype == Geom::NODE_NONE) {
1028                 *p = from_2geom(curve_it1->finalPoint() * i2d);
1029             }
1030             if (nodetype == Geom::NODE_CUSP) {
1031                 *p = from_2geom(curve_it1->finalPoint() * i2d);
1032             }
1034             ++curve_it1;
1035             ++curve_it2;
1036         }
1037     }
1040 /*
1041   Local Variables:
1042   mode:c++
1043   c-file-style:"stroustrup"
1044   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1045   indent-tabs-mode:nil
1046   fill-column:99
1047   End:
1048 */
1049 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :