Code

- Created a SPLPEItem class that handles applying a LPE to an Item
[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  * Copyright (C) 2007-2008 Johan Engelen
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  */
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
21 #include <libnr/n-art-bpath.h>
22 #include <libnr/nr-matrix-fns.h>
23 #include <libnr/nr-matrix-ops.h>
24 #include <libnr/nr-matrix-translate-ops.h>
25 #include <libnr/nr-scale-matrix-ops.h>
27 #include <sigc++/functors/ptr_fun.h>
28 #include <sigc++/adaptors/bind.h>
30 #include "macros.h"
31 #include "display/nr-arena-shape.h"
32 #include "print.h"
33 #include "document.h"
34 #include "style.h"
35 #include "marker.h"
36 #include "sp-path.h"
37 #include "prefs-utils.h"
38 #include "attributes.h"
40 #include "live_effects/lpeobject.h"
41 #include "uri.h"
42 #include "extract-uri.h"
43 #include "uri-references.h"
44 #include "bad-uri-exception.h"
45 #include "xml/repr.h"
47 #include "util/mathfns.h" // for triangle_area()
49 #define noSHAPE_VERBOSE
51 static void sp_shape_class_init (SPShapeClass *klass);
52 static void sp_shape_init (SPShape *shape);
53 static void sp_shape_finalize (GObject *object);
55 static void sp_shape_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
56 static void sp_shape_release (SPObject *object);
58 static void sp_shape_set(SPObject *object, unsigned key, gchar const *value);
59 static void sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags);
60 static void sp_shape_modified (SPObject *object, unsigned int flags);
61 static Inkscape::XML::Node *sp_shape_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
63 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
64 void sp_shape_print (SPItem * item, SPPrintContext * ctx);
65 static NRArenaItem *sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
66 static void sp_shape_hide (SPItem *item, unsigned int key);
67 static void sp_shape_snappoints (SPItem const *item, SnapPointsIter p);
69 static void sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai);
71 static SPLPEItemClass *parent_class;
73 /**
74  * Registers the SPShape class with Gdk and returns its type number.
75  */
76 GType
77 sp_shape_get_type (void)
78 {
79         static GType type = 0;
80         if (!type) {
81                 GTypeInfo info = {
82                         sizeof (SPShapeClass),
83                         NULL, NULL,
84                         (GClassInitFunc) sp_shape_class_init,
85                         NULL, NULL,
86                         sizeof (SPShape),
87                         16,
88                         (GInstanceInitFunc) sp_shape_init,
89                         NULL,   /* value_table */
90                 };
91                 type = g_type_register_static (SP_TYPE_LPE_ITEM, "SPShape", &info, (GTypeFlags)0);
92         }
93         return type;
94 }
96 /**
97  * Initializes a SPShapeClass object.  Establishes the function pointers to the class'
98  * member routines in the class vtable, and sets pointers to parent classes.
99  */
100 static void
101 sp_shape_class_init (SPShapeClass *klass)
103     GObjectClass *gobject_class;
104         SPObjectClass *sp_object_class;
105         SPItemClass * item_class;
106     SPLPEItemClass * lpe_item_class;
108     gobject_class = (GObjectClass *) klass;
109         sp_object_class = (SPObjectClass *) klass;
110         item_class = (SPItemClass *) klass;
112         parent_class = (SPLPEItemClass *)g_type_class_peek_parent (klass);
114     gobject_class->finalize = sp_shape_finalize;
116         sp_object_class->build = sp_shape_build;
117         sp_object_class->release = sp_shape_release;
118     sp_object_class->set = sp_shape_set;
119         sp_object_class->update = sp_shape_update;
120         sp_object_class->modified = sp_shape_modified;
121     sp_object_class->write = sp_shape_write;
123         item_class->bbox = sp_shape_bbox;
124         item_class->print = sp_shape_print;
125         item_class->show = sp_shape_show;
126         item_class->hide = sp_shape_hide;
127     item_class->snappoints = sp_shape_snappoints;
128     lpe_item_class->update_patheffect = NULL;
130     klass->set_shape = NULL;
133 /**
134  * Initializes an SPShape object.
135  */
136 static void
137 sp_shape_init (SPShape *shape)
139     for ( int i = 0 ; i < SP_MARKER_LOC_QTY ; i++ ) {
140         new (&shape->release_connect[i]) sigc::connection();
141         new (&shape->modified_connect[i]) sigc::connection();
142     }
145 static void
146 sp_shape_finalize (GObject *object)
148     SPShape *shape=(SPShape *)object;
150     for ( int i = 0 ; i < SP_MARKER_LOC_QTY ; i++ ) {
151         shape->release_connect[i].disconnect();
152         shape->release_connect[i].~connection();
153         shape->modified_connect[i].disconnect();
154         shape->modified_connect[i].~connection();
155     }
157     if (((GObjectClass *) (parent_class))->finalize) {
158         (* ((GObjectClass *) (parent_class))->finalize)(object);
159     }
162 /**
163  * Virtual build callback for SPMarker.
164  *
165  * This is to be invoked immediately after creation of an SPShape.
166  *
167  * \see sp_object_build()
168  */
169 static void
170 sp_shape_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
172     if (((SPObjectClass *) (parent_class))->build) {
173        (*((SPObjectClass *) (parent_class))->build) (object, document, repr);
174     }
177 /**
178  * Removes, releases and unrefs all children of object
179  *
180  * This is the inverse of sp_shape_build().  It must be invoked as soon
181  * as the shape is removed from the tree, even if it is still referenced
182  * by other objects.  This routine also disconnects/unrefs markers and
183  * curves attached to it.
184  *
185  * \see sp_object_release()
186  */
187 static void
188 sp_shape_release (SPObject *object)
190         SPItem *item;
191         SPShape *shape;
192         SPItemView *v;
193         int i;
195         item = (SPItem *) object;
196         shape = (SPShape *) object;
198         for (i=SP_MARKER_LOC_START; i<SP_MARKER_LOC_QTY; i++) {
199           if (shape->marker[i]) {
200             sp_signal_disconnect_by_data (shape->marker[i], object);
201             for (v = item->display; v != NULL; v = v->next) {
202               sp_marker_hide ((SPMarker *) shape->marker[i], NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
203             }
204             shape->marker[i] = sp_object_hunref (shape->marker[i], object);
205           }
206         }
207         if (shape->curve) {
208                 shape->curve = sp_curve_unref (shape->curve);
209         }
210     
211         if (((SPObjectClass *) parent_class)->release) {
212           ((SPObjectClass *) parent_class)->release (object);
213         }
218 static void
219 sp_shape_set(SPObject *object, unsigned int key, gchar const *value)
221     if (((SPObjectClass *) parent_class)->set) {
222         ((SPObjectClass *) parent_class)->set(object, key, value);
223     }
226 static Inkscape::XML::Node *
227 sp_shape_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
229     if (((SPObjectClass *)(parent_class))->write) {
230         ((SPObjectClass *)(parent_class))->write(object, repr, flags);
231     }
233     return repr;
236 /** 
237  * Updates the shape when its attributes have changed.  Also establishes
238  * marker objects to match the style settings.  
239  */
240 static void
241 sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags)
243     SPItem *item = (SPItem *) object;
244     SPShape *shape = (SPShape *) object;
246         if (((SPObjectClass *) (parent_class))->update) {
247           (* ((SPObjectClass *) (parent_class))->update) (object, ctx, flags);
248         }
250         /* This stanza checks that an object's marker style agrees with
251          * the marker objects it has allocated.  sp_shape_set_marker ensures
252          * that the appropriate marker objects are present (or absent) to
253          * match the style.
254          */
255         /* TODO:  It would be nice if this could be done at an earlier level */
256         for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
257             sp_shape_set_marker (object, i, object->style->marker[i].value);
258           }
260         if (flags & (SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
261                 SPStyle *style;
262                 style = SP_OBJECT_STYLE (object);
263                 if (style->stroke_width.unit == SP_CSS_UNIT_PERCENT) {
264                         SPItemCtx *ictx = (SPItemCtx *) ctx;
265                         double const aw = 1.0 / NR::expansion(ictx->i2vp);
266                         style->stroke_width.computed = style->stroke_width.value * aw;
267                         for (SPItemView *v = ((SPItem *) (shape))->display; v != NULL; v = v->next) {
268                                 nr_arena_shape_set_style ((NRArenaShape *) v->arenaitem, style);
269                         }
270                 }
271         }
273         if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
274                 /* This is suboptimal, because changing parent style schedules recalculation */
275                 /* But on the other hand - how can we know that parent does not tie style and transform */
276                 NR::Maybe<NR::Rect> paintbox = SP_ITEM(object)->getBounds(NR::identity());
277                 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
278                     NRArenaShape * const s = NR_ARENA_SHAPE(v->arenaitem);
279                     if (flags & SP_OBJECT_MODIFIED_FLAG) {
280                         nr_arena_shape_set_path(s, shape->curve, (flags & SP_OBJECT_USER_MODIFIED_FLAG_B));
281                     }
282                     if (paintbox) {
283                         s->setPaintBox(*paintbox);
284                     }
285                 }
286         }
288         if (sp_shape_has_markers (shape)) {
290             /* Dimension marker views */
291             for (SPItemView *v = item->display; v != NULL; v = v->next) {
293                 if (!v->arenaitem->key) {
294                     /* Get enough keys for all, start, mid and end marker types,
295                     ** and set this view's arenaitem key to the first of these keys.
296                     */
297                     NR_ARENA_ITEM_SET_KEY (
298                         v->arenaitem,
299                         sp_item_display_key_new (SP_MARKER_LOC_QTY)
300                         );
301                 }
303                 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
304                     if (shape->marker[i]) {
305                         sp_marker_show_dimension ((SPMarker *) shape->marker[i],
306                                                   NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i - SP_MARKER_LOC,
307                                                   sp_shape_number_of_markers (shape, i));
308                     }
309                 }
310             }
312             /* Update marker views */
313             for (SPItemView *v = item->display; v != NULL; v = v->next) {
314                 sp_shape_update_marker_view (shape, v->arenaitem);
315             }
316         }
320 /**
321 * Works out whether a marker of a given type is required at a particular
322 * point on a shape.
324 * \param shape Shape of interest.
325 * \param m Marker type (e.g. SP_MARKER_LOC_START)
326 * \param bp Path segment.
327 * \return 1 if a marker is required here, otherwise 0.
328 */
329 bool
330 sp_shape_marker_required(SPShape const *shape, int const m, NArtBpath *bp)
332     if (shape->marker[m] == NULL) {
333         return false;
334     }
336     if (bp == SP_CURVE_BPATH(shape->curve))
337         return m == SP_MARKER_LOC_START;
338     else if (bp[1].code == NR_END)
339         return m == SP_MARKER_LOC_END;
340     else
341         return m == SP_MARKER_LOC_MID;
344 static bool
345 is_moveto(NRPathcode const c)
347     return c == NR_MOVETO || c == NR_MOVETO_OPEN;
350 /** 
351  * Helper function that advances a subpath's bpath to the first subpath
352  * by checking for moveto segments.
353  *
354  * \pre The bpath[] containing bp begins with a moveto. 
355  */
356 static NArtBpath const *
357 first_seg_in_subpath(NArtBpath const *bp)
359     while (!is_moveto(bp->code)) {
360         --bp;
361     }
362     return bp;
365 /**
366  * Advances the bpath to the last segment in the subpath.
367  */
368 static NArtBpath const *
369 last_seg_in_subpath(NArtBpath const *bp)
371     for(;;) {
372         ++bp;
373         switch (bp->code) {
374             case NR_MOVETO:
375             case NR_MOVETO_OPEN:
376             case NR_END:
377                 --bp;
378                 return bp;
380             default: continue;
381         }
382     }
386 /* A subpath begins with a moveto and ends immediately before the next moveto or NR_END.
387  * (`moveto' here means either NR_MOVETO or NR_MOVETO_OPEN.)  I'm assuming that non-empty
388  * paths always begin with a moveto.
389  *
390  * The control points of the subpath are the control points of the path elements of the subpath.
391  *
392  * As usual, the control points of a moveto or NR_LINETO are {c(3)}, and
393  * the control points of a NR_CURVETO are {c(1), c(2), c(3)}.
394  * (It follows from the definition that NR_END isn't part of a subpath.)
395  *
396  * The initial control point is bpath[bi0].c(3).
397  *
398  * Reference: http://www.w3.org/TR/SVG11/painting.html#MarkerElement, the `orient' attribute.
399  * Reference for behaviour of zero-length segments:
400  * http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
401  */
403 static double const no_tangent = 128.0;  /* arbitrarily-chosen value outside the range of atan2,
404                                           * i.e. outside of [-pi, pi]. This value is incremented by
405                                           * 1 and checked using > to be safe from floating-point
406                                           * equality comparison madness.*/
408 /**
409  * Helper function to calculate the outgoing tangent of a path 
410  * ( atan2(other - p0) )
411  * \pre The bpath[] containing bp0 begins with a moveto. 
412  */
413 static double
414 outgoing_tangent(NArtBpath const *bp0)
416     /* See notes in comment block above. */
418     g_assert(bp0->code != NR_END);
419     NR::Point const &p0 = bp0->c(3);
420     NR::Point other;
421     for (NArtBpath const *bp = bp0;;) {
422         ++bp;
423         switch (bp->code) {
424             case NR_LINETO:
425                 other = bp->c(3);
426                 if (other != p0) {
427                     goto found;
428                 }
429                 break;
431             case NR_CURVETO:
432                 for (unsigned ci = 1; ci <= 3; ++ci) {
433                     other = bp->c(ci);
434                     if (other != p0) {
435                         goto found;
436                     }
437                 }
438                 break;
440             case NR_MOVETO_OPEN:
441             case NR_END:
442             case NR_MOVETO:
443                 bp = first_seg_in_subpath(bp0);
444                 if (bp == bp0) {
445                     /* Gone right around the subpath without finding any different point since the
446                      * initial moveto. */
447                     return no_tangent + 1;
448                 }
449                 if (bp->code != NR_MOVETO) {
450                     /* Open subpath. */
451                     return no_tangent + 1;
452                 }
453                 other = bp->c(3);
454                 if (other != p0) {
455                     goto found;
456                 }
457                 break;
458         }
460         if (bp == bp0) {
461             /* Back where we started, so zero-length subpath. */
462             return no_tangent + 1;
464             /* Note: this test must come after we've looked at element bp, in case bp0 is a curve:
465              * we must look at c(1) and c(2).  (E.g. single-curve subpath.)
466              */
467         }
468     }
470 found:
471     return atan2( other - p0 );
474 /**
475  * Helper function to calculate the incoming tangent of a path
476  * ( atan2(p0 - other) )
477  * 
478  * \pre The bpath[] containing bp0 begins with a moveto. 
479  */
480 static double
481 incoming_tangent(NArtBpath const *bp0)
483     /* See notes in comment block before outgoing_tangent. */
485     g_assert(bp0->code != NR_END);
486     NR::Point const &p0 = bp0->c(3);
487     NR::Point other;
488     for (NArtBpath const *bp = bp0;;) {
489         switch (bp->code) {
490             case NR_LINETO:
491                 other = bp->c(3);
492                 if (other != p0) {
493                     goto found;
494                 }
495                 --bp;
496                 break;
498             case NR_CURVETO:
499                 for (unsigned ci = 3; ci != 0; --ci) {
500                     other = bp->c(ci);
501                     if (other != p0) {
502                         goto found;
503                     }
504                 }
505                 --bp;
506                 break;
508             case NR_MOVETO:
509             case NR_MOVETO_OPEN:
510                 other = bp->c(3);
511                 if (other != p0) {
512                     goto found;
513                 }
514                 if (bp->code != NR_MOVETO) {
515                     /* Open subpath. */
516                     return no_tangent + 1;
517                 }
518                 bp = last_seg_in_subpath(bp0);
519                 break;
521             default: /* includes NR_END */
522                 g_error("Found invalid path code %u in middle of path.", bp->code);
523                 return no_tangent + 1;
524         }
526         if (bp == bp0) {
527             /* Back where we started from: zero-length subpath. */
528             return no_tangent + 1;
529         }
530     }
532 found:
533     return atan2( p0 - other );
537 /**
538  * Calculate the transform required to get a marker's path object in the
539  * right place for particular path segment on a shape.  You should
540  * call sp_shape_marker_required first to see if a marker is required
541  * at this point.
542  *
543  * \see sp_shape_marker_required.
544  *
545  * \param shape Shape which the marker is for.
546  * \param m Marker type (e.g. SP_MARKER_LOC_START)
547  * \param bp Path segment which the arrow is for.
548  * \return Transform matrix.
549  */
550 NR::Matrix
551 sp_shape_marker_get_transform(SPShape const *shape, NArtBpath const *bp)
553     g_return_val_if_fail(( is_moveto(SP_CURVE_BPATH(shape->curve)[0].code)
554                            && ( 0 < shape->curve->end )
555                            && ( SP_CURVE_BPATH(shape->curve)[shape->curve->end].code == NR_END ) ),
556                          NR::Matrix(NR::translate(bp->c(3))));
557     double const angle1 = incoming_tangent(bp);
558     double const angle2 = outgoing_tangent(bp);
560     double ret_angle;
561     if (angle1 > no_tangent) {
562         /* First vertex of an open subpath. */
563         ret_angle = ( angle2 > no_tangent
564                       ? 0.
565                       : angle2 );
566     } else if (angle2 > no_tangent) {
567         /* Last vertex of an open subpath. */
568         ret_angle = angle1;
569     } else {
570         ret_angle = .5 * (angle1 + angle2);
572         if ( fabs( angle2 - angle1 ) > M_PI ) {
573             /* ret_angle is in the middle of the larger of the two sectors between angle1 and
574              * angle2, so flip it by 180degrees to force it to the middle of the smaller sector.
575              *
576              * (Imagine a circle with rays drawn at angle1 and angle2 from the centre of the
577              * circle.  Those two rays divide the circle into two sectors.)
578              */
579             ret_angle += M_PI;
580         }
581     }
583     return NR::Matrix(NR::rotate(ret_angle)) * NR::translate(bp->c(3));
586 /**
587  * Updates the instances (views) of a given marker in a shape.
588  * Marker views have to be scaled already.  The transformation
589  * is retrieved and then shown by calling sp_marker_show_instance.
590  */
591 static void
592 sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai)
594         SPStyle *style = ((SPObject *) shape)->style;
596         for (int i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
597             if (shape->marker[i] == NULL) {
598                 continue;
599             }
601             int n = 0;
603             for (NArtBpath *bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
604                 if (sp_shape_marker_required (shape, i, bp)) {
605                     NR::Matrix const m(sp_shape_marker_get_transform(shape, bp));
606                     sp_marker_show_instance ((SPMarker* ) shape->marker[i], ai,
607                                              NR_ARENA_ITEM_GET_KEY(ai) + i, n, m,
608                                              style->stroke_width.computed);
609                     n++;
610                 }
611             }
612         }
615 /**
616  * Sets modified flag for all sub-item views.
617  */
618 static void
619 sp_shape_modified (SPObject *object, unsigned int flags)
621         SPShape *shape = SP_SHAPE (object);
623         if (((SPObjectClass *) (parent_class))->modified) {
624           (* ((SPObjectClass *) (parent_class))->modified) (object, flags);
625         }
627         if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
628                 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
629                         nr_arena_shape_set_style (NR_ARENA_SHAPE (v->arenaitem), object->style);
630                 }
631         }
634 /**
635  * Calculates the bounding box for item, storing it into bbox.
636  * This also includes the bounding boxes of any markers included in the shape.
637  */
638 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
640     SPShape const *shape = SP_SHAPE (item);
642     if (shape->curve) {
644         NRRect  cbbox;
645         NRBPath bp;
647         bp.path = SP_CURVE_BPATH (shape->curve);
649         cbbox.x0 = cbbox.y0 = NR_HUGE;
650         cbbox.x1 = cbbox.y1 = -NR_HUGE;
652         nr_path_matrix_bbox_union(&bp, transform, &cbbox);
654         if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
655             
656             SPStyle* style=SP_OBJECT_STYLE (item);
657             if (!style->stroke.isNone()) {
658                 double const scale = expansion(transform);
659                 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
660                     double const width = MAX(0.125, style->stroke_width.computed * scale);
661                     if ( fabs(cbbox.x1-cbbox.x0) > -0.00001 && fabs(cbbox.y1-cbbox.y0) > -0.00001 ) {
662                         cbbox.x0-=0.5*width;
663                         cbbox.x1+=0.5*width;
664                         cbbox.y0-=0.5*width;
665                         cbbox.y1+=0.5*width;
666                     }
667                 }
668             }
670             // Union with bboxes of the markers, if any
671             if (sp_shape_has_markers (shape)) {
672                 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
673                     for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
674                         if (sp_shape_marker_required (shape, m, bp)) {
676                             SPMarker* marker = SP_MARKER (shape->marker[m]);
677                             SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
679                             NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
681                             if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
682                                 tr = NR::scale(style->stroke_width.computed) * tr;
683                             }
685                             // total marker transform
686                             tr = marker_item->transform * marker->c2p * tr * transform;
688                             // get bbox of the marker with that transform
689                             NRRect marker_bbox;
690                             sp_item_invoke_bbox (marker_item, &marker_bbox, tr, true);
691                             // union it with the shape bbox
692                             nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
693                         }
694                     }
695                 }
696             }
697         }
699         // copy our bbox to the variable we're given
700         *bbox = cbbox;
701     }
704 /**
705  * Prepares shape for printing.  Handles printing of comments for printing
706  * debugging, sizes the item to fit into the document width/height,
707  * applies print fill/stroke, sets transforms for markers, and adds
708  * comment labels.
709  */
710 void
711 sp_shape_print (SPItem *item, SPPrintContext *ctx)
713         NRRect pbox, dbox, bbox;
715         SPShape *shape = SP_SHAPE(item);
717         if (!shape->curve) return;
719         gint add_comments = prefs_get_int_attribute_limited ("printing.debug", "add-label-comments", 0, 0, 1);
720         if (add_comments) {
721             gchar * comment = g_strdup_printf("begin '%s'",
722                                               SP_OBJECT(item)->defaultLabel());
723             sp_print_comment(ctx, comment);
724             g_free(comment);
725         }
727         /* fixme: Think (Lauris) */
728         sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
729         dbox.x0 = 0.0;
730         dbox.y0 = 0.0;
731         dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
732         dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
733         sp_item_bbox_desktop (item, &bbox);
734         NR::Matrix const i2d = sp_item_i2d_affine(item);
736         SPStyle* style = SP_OBJECT_STYLE (item);
738         if (!style->fill.isNone()) {
739                 NRBPath bp;
740                 bp.path = SP_CURVE_BPATH(shape->curve);
741                 sp_print_fill (ctx, &bp, &i2d, style, &pbox, &dbox, &bbox);
742         }
744         if (!style->stroke.isNone()) {
745                 NRBPath bp;
746                 bp.path = SP_CURVE_BPATH(shape->curve);
747                 sp_print_stroke (ctx, &bp, &i2d, style, &pbox, &dbox, &bbox);
748         }
750         for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
751             for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
752                 if (sp_shape_marker_required (shape, m, bp)) {
754                     SPMarker* marker = SP_MARKER (shape->marker[m]);
755                     SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
757                     NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
759                     if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
760                         tr = NR::scale(style->stroke_width.computed) * tr;
761                     }
763                     tr = marker_item->transform * marker->c2p * tr;
765                     NR::Matrix old_tr = marker_item->transform;
766                     marker_item->transform = tr;
767                     sp_item_invoke_print (marker_item, ctx);
768                     marker_item->transform = old_tr;
769                 }
770             }
771         }
773         if (add_comments) {
774             gchar * comment = g_strdup_printf("end '%s'",
775                                               SP_OBJECT(item)->defaultLabel());
776             sp_print_comment(ctx, comment);
777             g_free(comment);
778         }
781 /**
782  * Sets style, path, and paintbox.  Updates marker views, including dimensions.
783  */
784 static NRArenaItem *
785 sp_shape_show (SPItem *item, NRArena *arena, unsigned int /*key*/, unsigned int /*flags*/)
787         SPObject *object = SP_OBJECT(item);
788         SPShape *shape = SP_SHAPE(item);
790         NRArenaItem *arenaitem = NRArenaShape::create(arena);
791         NRArenaShape * const s = NR_ARENA_SHAPE(arenaitem);
792         nr_arena_shape_set_style(s, object->style);
793         nr_arena_shape_set_path(s, shape->curve, false);
794         NR::Maybe<NR::Rect> paintbox = item->getBounds(NR::identity());
795         if (paintbox) {
796             s->setPaintBox(*paintbox);
797         }
799         if (sp_shape_has_markers (shape)) {
801             /* Dimension the marker views */
802             if (!arenaitem->key) {
803                 NR_ARENA_ITEM_SET_KEY (arenaitem, sp_item_display_key_new (SP_MARKER_LOC_QTY));
804             }
806             for (int i = 0; i < SP_MARKER_LOC_QTY; i++) {
807                 if (shape->marker[i]) {
808                     sp_marker_show_dimension ((SPMarker *) shape->marker[i],
809                                               NR_ARENA_ITEM_GET_KEY (arenaitem) + i - SP_MARKER_LOC,
810                                               sp_shape_number_of_markers (shape, i));
811                 }
812             }
815             /* Update marker views */
816             sp_shape_update_marker_view (shape, arenaitem);
817         }
819         return arenaitem;
822 /**
823  * Hides/removes marker views from the shape.
824  */
825 static void
826 sp_shape_hide (SPItem *item, unsigned int key)
828         SPShape *shape;
829         SPItemView *v;
830         int i;
832         shape = (SPShape *) item;
834         for (i=0; i<SP_MARKER_LOC_QTY; i++) {
835           if (shape->marker[i]) {
836             for (v = item->display; v != NULL; v = v->next) {
837                 if (key == v->key) {
838               sp_marker_hide ((SPMarker *) shape->marker[i],
839                                     NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
840                 }
841             }
842           }
843         }
845         if (((SPItemClass *) parent_class)->hide) {
846           ((SPItemClass *) parent_class)->hide (item, key);
847         }
850 /**
851 * \param shape Shape.
852 * \return TRUE if the shape has any markers, or FALSE if not.
853 */
854 int
855 sp_shape_has_markers (SPShape const *shape)
857     /* Note, we're ignoring 'marker' settings, which technically should apply for
858        all three settings.  This should be fixed later such that if 'marker' is
859        specified, then all three should appear. */
861     return (
862         shape->curve &&
863         (shape->marker[SP_MARKER_LOC_START] ||
864          shape->marker[SP_MARKER_LOC_MID] ||
865          shape->marker[SP_MARKER_LOC_END])
866         );
870 /**
871 * \param shape Shape.
872 * \param type Marker type (e.g. SP_MARKER_LOC_START)
873 * \return Number of markers that the shape has of this type.
874 */
875 int
876 sp_shape_number_of_markers (SPShape *shape, int type)
878     int n = 0;
879     for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
880         if (sp_shape_marker_required (shape, type, bp)) {
881             n++;
882         }
883     }
885     return n;
888 /**
889  * Checks if the given marker is used in the shape, and if so, it
890  * releases it by calling sp_marker_hide.  Also detaches signals
891  * and unrefs the marker from the shape.
892  */
893 static void
894 sp_shape_marker_release (SPObject *marker, SPShape *shape)
896         SPItem *item;
897         int i;
899         item = (SPItem *) shape;
901         for (i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
902           if (marker == shape->marker[i]) {
903             SPItemView *v;
904             /* Hide marker */
905             for (v = item->display; v != NULL; v = v->next) {
906               sp_marker_hide ((SPMarker *) (shape->marker[i]), NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
907               /* fixme: Do we need explicit remove here? (Lauris) */
908               /* nr_arena_item_set_mask (v->arenaitem, NULL); */
909             }
910             /* Detach marker */
911             sp_signal_disconnect_by_data (shape->marker[i], item);
912             shape->marker[i] = sp_object_hunref (shape->marker[i], item);
913           }
914         }
917 /**
918  * No-op.  Exists for handling 'modified' messages
919  */
920 static void
921 sp_shape_marker_modified (SPObject */*marker*/, guint /*flags*/, SPItem */*item*/)
923         /* I think mask does update automagically */
924         /* g_warning ("Item %s mask %s modified", SP_OBJECT_ID (item), SP_OBJECT_ID (mask)); */
927 /**
928  * Adds a new marker to shape object at the location indicated by key.  value 
929  * must be a valid URI reference resolvable from the shape object (i.e., present
930  * in the document <defs>).  If the shape object already has a marker
931  * registered at the given position, it is removed first.  Then the
932  * new marker is hrefed and its signals connected.
933  */
934 void
935 sp_shape_set_marker (SPObject *object, unsigned int key, const gchar *value)
937     SPItem *item = (SPItem *) object;
938     SPShape *shape = (SPShape *) object;
940     if (key < SP_MARKER_LOC_START || key > SP_MARKER_LOC_END) {
941         return;
942     }
944     SPObject *mrk = sp_css_uri_reference_resolve (SP_OBJECT_DOCUMENT (object), value);
945     if (mrk != shape->marker[key]) {
946         if (shape->marker[key]) {
947             SPItemView *v;
949             /* Detach marker */
950             shape->release_connect[key].disconnect();
951             shape->modified_connect[key].disconnect();
953             /* Hide marker */
954             for (v = item->display; v != NULL; v = v->next) {
955                 sp_marker_hide ((SPMarker *) (shape->marker[key]),
956                                 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + key);
957                 /* fixme: Do we need explicit remove here? (Lauris) */
958                 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
959             }
961             /* Unref marker */
962             shape->marker[key] = sp_object_hunref (shape->marker[key], object);
963         }
964         if (SP_IS_MARKER (mrk)) {
965             shape->marker[key] = sp_object_href (mrk, object);
966             shape->release_connect[key] = mrk->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_shape_marker_release), shape));
967             shape->modified_connect[key] = mrk->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_shape_marker_modified), shape));
968         }
969     }
974 /* Shape section */
976 /**
977  * Calls any registered handlers for the set_shape action
978  */
979 void
980 sp_shape_set_shape (SPShape *shape)
982         g_return_if_fail (shape != NULL);
983         g_return_if_fail (SP_IS_SHAPE (shape));
985         if (SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape) {
986           SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape (shape);
987         }
990 /**
991  * Adds a curve to the shape.  If owner is specified, a reference
992  * will be made, otherwise the curve will be copied into the shape.
993  * Any existing curve in the shape will be unreferenced first.
994  * This routine also triggers a request to update the display.
995  */
996 void
997 sp_shape_set_curve (SPShape *shape, SPCurve *curve, unsigned int owner)
999         if (shape->curve) {
1000                 shape->curve = sp_curve_unref (shape->curve);
1001         }
1002         if (curve) {
1003                 if (owner) {
1004                         shape->curve = sp_curve_ref (curve);
1005                 } else {
1006                         shape->curve = sp_curve_copy (curve);
1007                 }
1008         }
1009         SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1012 /**
1013  * Return duplicate of curve (if any exists) or NULL if there is no curve
1014  */
1015 SPCurve *
1016 sp_shape_get_curve (SPShape *shape)
1018         if (shape->curve) {
1019                 return sp_curve_copy (shape->curve);
1020         }
1021         return NULL;
1024 /**
1025  * Same as sp_shape_set_curve but without updating the display
1026  */
1027 void
1028 sp_shape_set_curve_insync (SPShape *shape, SPCurve *curve, unsigned int owner)
1030         if (shape->curve) {
1031                 shape->curve = sp_curve_unref (shape->curve);
1032         }
1033         if (curve) {
1034                 if (owner) {
1035                         shape->curve = sp_curve_ref (curve);
1036                 } else {
1037                         shape->curve = sp_curve_copy (curve);
1038                 }
1039         }
1042 /**
1043  * Return all nodes in a path that are to be considered for snapping
1044  */
1045 static void sp_shape_snappoints(SPItem const *item, SnapPointsIter p)
1047     g_assert(item != NULL);
1048     g_assert(SP_IS_SHAPE(item));
1050     SPShape const *shape = SP_SHAPE(item);
1051     if (shape->curve == NULL) {
1052         return;
1053     }
1054     
1055     NR::Matrix const i2d (sp_item_i2d_affine (item));
1056     NArtBpath const *b = SP_CURVE_BPATH(shape->curve);    
1057     
1058     // Cycle through the nodes in the concatenated subpaths
1059     while (b->code != NR_END) {
1060         NR::Point pos = b->c(3) * i2d; // this is the current node
1061         
1062         // NR_MOVETO Indicates the start of a closed subpath, see nr-path-code.h
1063         // If we're looking at a closed subpath, then we can skip this first 
1064         // point of the subpath because it's coincident with the last point.  
1065         if (b->code != NR_MOVETO) {
1066             if (b->code == NR_MOVETO_OPEN || b->code == NR_LINETO || b[1].code == NR_LINETO || b[1].code == NR_END) {
1067                 // end points of a line segment are always considered for snapping
1068                 *p = pos; 
1069             } else {        
1070                 // g_assert(b->code == NR_CURVETO);
1071                 NR::Point ppos, npos;
1072                 ppos = b->code == NR_CURVETO ? b->c(2) * i2d : pos; // backward handle 
1073                 npos = b[1].code == NR_CURVETO ? b[1].c(1) * i2d : pos; // forward handle            
1074                 // Determine whether a node is at a smooth part of the path, by 
1075                 // calculating a measure for the collinearity of the handles
1076                 bool c1 = fabs (Inkscape::Util::triangle_area (pos, ppos, npos)) < 1; // points are (almost) collinear
1077                 bool c2 = NR::L2(pos - ppos) < 1e-6 || NR::L2(pos - npos) < 1e-6; // endnode, or a node with a retracted handle
1078                 if (!(c1 & !c2)) {
1079                     *p = pos; // only return non-smooth nodes ("cusps")
1080                 }
1081             }
1082         }
1083         
1084         b++;
1085     }
1088 /*
1089   Local Variables:
1090   mode:c++
1091   c-file-style:"stroustrup"
1092   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1093   indent-tabs-mode:nil
1094   fill-column:99
1095   End:
1096 */
1097 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :