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/n-art-bpath.h>
20 #include <libnr/nr-matrix-fns.h>
21 #include <libnr/nr-matrix-ops.h>
22 #include <libnr/nr-matrix-translate-ops.h>
23 #include <libnr/nr-scale-matrix-ops.h>
24 #include <2geom/rect.h>
25 #include <2geom/transforms.h>
26 #include <2geom/pathvector.h>
27 #include "helper/geom.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)
105 {
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;
130 }
132 /**
133 * Initializes an SPShape object.
134 */
135 static void
136 sp_shape_init (SPShape *shape)
137 {
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 }
142 }
144 static void
145 sp_shape_finalize (GObject *object)
146 {
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 }
159 }
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)
170 {
171 if (((SPObjectClass *) (parent_class))->build) {
172 (*((SPObjectClass *) (parent_class))->build) (object, document, repr);
173 }
174 }
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)
188 {
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 }
210 if (((SPObjectClass *) parent_class)->release) {
211 ((SPObjectClass *) parent_class)->release (object);
212 }
213 }
217 static void
218 sp_shape_set(SPObject *object, unsigned int key, gchar const *value)
219 {
220 if (((SPObjectClass *) parent_class)->set) {
221 ((SPObjectClass *) parent_class)->set(object, key, value);
222 }
223 }
225 static Inkscape::XML::Node *
226 sp_shape_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
227 {
228 if (((SPObjectClass *)(parent_class))->write) {
229 ((SPObjectClass *)(parent_class))->write(object, doc, repr, flags);
230 }
232 return repr;
233 }
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)
241 {
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 NR::Maybe<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 }
316 }
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)
336 {
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);
360 }
361 Geom::Matrix
362 sp_shape_marker_get_transform_at_start(Geom::Curve const & c)
363 {
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);
370 }
371 Geom::Matrix
372 sp_shape_marker_get_transform_at_end(Geom::Curve const & c)
373 {
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);
382 }
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)
395 {
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 }
441 }
443 /**
444 * Sets modified flag for all sub-item views.
445 */
446 static void
447 sp_shape_modified (SPObject *object, unsigned int flags)
448 {
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 }
460 }
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)
467 {
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(), to_2geom(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) {
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(from_2geom(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(from_2geom(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(from_2geom(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 }
579 }
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)
589 {
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 = from_2geom(sp_item_i2d_affine(item));
613 SPStyle* style = SP_OBJECT_STYLE (item);
615 if (!style->fill.isNone()) {
616 const_NRBPath bp;
617 bp.path = shape->curve->get_bpath();
618 sp_print_fill (ctx, &bp, &i2d, style, &pbox, &dbox, &bbox);
619 }
621 if (!style->stroke.isNone()) {
622 const_NRBPath bp;
623 bp.path = shape->curve->get_bpath();
624 sp_print_stroke (ctx, &bp, &i2d, style, &pbox, &dbox, &bbox);
625 }
627 /* TODO: make code prettier: lots of variables can be taken out of the loop! */
628 Geom::PathVector const & pathv = shape->curve->get_pathvector();
629 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
630 if ( shape->marker[SP_MARKER_LOC_START] ) {
631 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
632 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
634 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform_at_start(path_it->front())));
636 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
637 tr = NR::scale(style->stroke_width.computed) * tr;
638 }
640 tr = marker_item->transform * marker->c2p * tr;
642 NR::Matrix old_tr = marker_item->transform;
643 marker_item->transform = tr;
644 sp_item_invoke_print (marker_item, ctx);
645 marker_item->transform = old_tr;
646 }
648 if ( shape->marker[SP_MARKER_LOC_MID] ) {
649 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
650 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
651 while (curve_it2 != path_it->end_default())
652 {
653 /* Put marker between curve_it1 and curve_it2.
654 * Loop to end_default (so including closing segment), because when a path is closed,
655 * there should be a midpoint marker between last segment and closing straight line segment */
657 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
658 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
660 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform(*curve_it1, *curve_it2)));
662 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
663 tr = NR::scale(style->stroke_width.computed) * tr;
664 }
666 tr = marker_item->transform * marker->c2p * tr;
668 NR::Matrix old_tr = marker_item->transform;
669 marker_item->transform = tr;
670 sp_item_invoke_print (marker_item, ctx);
671 marker_item->transform = old_tr;
673 ++curve_it1;
674 ++curve_it2;
675 }
676 }
678 if ( shape->marker[SP_MARKER_LOC_END] ) {
679 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
680 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
682 NR::Matrix tr(from_2geom(sp_shape_marker_get_transform_at_end(path_it->back_default())));
684 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
685 tr = NR::scale(style->stroke_width.computed) * tr;
686 }
688 tr = marker_item->transform * marker->c2p * tr;
690 NR::Matrix old_tr = marker_item->transform;
691 marker_item->transform = tr;
692 sp_item_invoke_print (marker_item, ctx);
693 marker_item->transform = old_tr;
694 }
695 }
697 if (add_comments) {
698 gchar * comment = g_strdup_printf("end '%s'",
699 SP_OBJECT(item)->defaultLabel());
700 sp_print_comment(ctx, comment);
701 g_free(comment);
702 }
703 }
705 /**
706 * Sets style, path, and paintbox. Updates marker views, including dimensions.
707 */
708 static NRArenaItem *
709 sp_shape_show (SPItem *item, NRArena *arena, unsigned int /*key*/, unsigned int /*flags*/)
710 {
711 SPObject *object = SP_OBJECT(item);
712 SPShape *shape = SP_SHAPE(item);
714 NRArenaItem *arenaitem = NRArenaShape::create(arena);
715 NRArenaShape * const s = NR_ARENA_SHAPE(arenaitem);
716 nr_arena_shape_set_style(s, object->style);
717 nr_arena_shape_set_path(s, shape->curve, false);
718 NR::Maybe<NR::Rect> paintbox = item->getBounds(NR::identity());
719 if (paintbox) {
720 s->setPaintBox(*paintbox);
721 }
723 if (sp_shape_has_markers (shape)) {
725 /* Dimension the marker views */
726 if (!arenaitem->key) {
727 NR_ARENA_ITEM_SET_KEY (arenaitem, sp_item_display_key_new (SP_MARKER_LOC_QTY));
728 }
730 for (int i = 0; i < SP_MARKER_LOC_QTY; i++) {
731 if (shape->marker[i]) {
732 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
733 NR_ARENA_ITEM_GET_KEY (arenaitem) + i - SP_MARKER_LOC,
734 sp_shape_number_of_markers (shape, i));
735 }
736 }
739 /* Update marker views */
740 sp_shape_update_marker_view (shape, arenaitem);
741 }
743 return arenaitem;
744 }
746 /**
747 * Hides/removes marker views from the shape.
748 */
749 static void
750 sp_shape_hide (SPItem *item, unsigned int key)
751 {
752 SPShape *shape;
753 SPItemView *v;
754 int i;
756 shape = (SPShape *) item;
758 for (i=0; i<SP_MARKER_LOC_QTY; i++) {
759 if (shape->marker[i]) {
760 for (v = item->display; v != NULL; v = v->next) {
761 if (key == v->key) {
762 sp_marker_hide ((SPMarker *) shape->marker[i],
763 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
764 }
765 }
766 }
767 }
769 if (((SPItemClass *) parent_class)->hide) {
770 ((SPItemClass *) parent_class)->hide (item, key);
771 }
772 }
774 /**
775 * \param shape Shape.
776 * \return TRUE if the shape has any markers, or FALSE if not.
777 */
778 int
779 sp_shape_has_markers (SPShape const *shape)
780 {
781 /* Note, we're ignoring 'marker' settings, which technically should apply for
782 all three settings. This should be fixed later such that if 'marker' is
783 specified, then all three should appear. */
785 return (
786 shape->curve &&
787 (shape->marker[SP_MARKER_LOC_START] ||
788 shape->marker[SP_MARKER_LOC_MID] ||
789 shape->marker[SP_MARKER_LOC_END])
790 );
791 }
794 /**
795 * \param shape Shape.
796 * \param type Marker type (e.g. SP_MARKER_LOC_START)
797 * \return Number of markers that the shape has of this type.
798 */
799 int
800 sp_shape_number_of_markers (SPShape *shape, int type)
801 {
802 Geom::PathVector const & pathv = shape->curve->get_pathvector();
804 switch(type) {
805 case SP_MARKER_LOC_START:
806 return shape->marker[SP_MARKER_LOC_START] ? pathv.size() : 0;
808 case SP_MARKER_LOC_MID:
809 {
810 if ( shape->marker[SP_MARKER_LOC_MID] ) {
811 guint n = 0;
812 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
813 n += path_it->size();
814 n += path_it->closed() ? 1 : 0;
815 }
816 return n;
817 } else {
818 return 0;
819 }
820 }
822 case SP_MARKER_LOC_END:
823 {
824 if ( shape->marker[SP_MARKER_LOC_END] ) {
825 guint n = 0;
826 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
827 if (!path_it->empty()) {
828 n++;
829 }
830 }
831 return n;
832 } else {
833 return 0;
834 }
835 }
837 default:
838 return 0;
839 }
840 }
842 /**
843 * Checks if the given marker is used in the shape, and if so, it
844 * releases it by calling sp_marker_hide. Also detaches signals
845 * and unrefs the marker from the shape.
846 */
847 static void
848 sp_shape_marker_release (SPObject *marker, SPShape *shape)
849 {
850 SPItem *item;
851 int i;
853 item = (SPItem *) shape;
855 for (i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
856 if (marker == shape->marker[i]) {
857 SPItemView *v;
858 /* Hide marker */
859 for (v = item->display; v != NULL; v = v->next) {
860 sp_marker_hide ((SPMarker *) (shape->marker[i]), NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
861 /* fixme: Do we need explicit remove here? (Lauris) */
862 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
863 }
864 /* Detach marker */
865 sp_signal_disconnect_by_data (shape->marker[i], item);
866 shape->marker[i] = sp_object_hunref (shape->marker[i], item);
867 }
868 }
869 }
871 /**
872 * No-op. Exists for handling 'modified' messages
873 */
874 static void
875 sp_shape_marker_modified (SPObject */*marker*/, guint /*flags*/, SPItem */*item*/)
876 {
877 /* I think mask does update automagically */
878 /* g_warning ("Item %s mask %s modified", SP_OBJECT_ID (item), SP_OBJECT_ID (mask)); */
879 }
881 /**
882 * Adds a new marker to shape object at the location indicated by key. value
883 * must be a valid URI reference resolvable from the shape object (i.e., present
884 * in the document <defs>). If the shape object already has a marker
885 * registered at the given position, it is removed first. Then the
886 * new marker is hrefed and its signals connected.
887 */
888 void
889 sp_shape_set_marker (SPObject *object, unsigned int key, const gchar *value)
890 {
891 SPItem *item = (SPItem *) object;
892 SPShape *shape = (SPShape *) object;
894 if (key < SP_MARKER_LOC_START || key > SP_MARKER_LOC_END) {
895 return;
896 }
898 SPObject *mrk = sp_css_uri_reference_resolve (SP_OBJECT_DOCUMENT (object), value);
899 if (mrk != shape->marker[key]) {
900 if (shape->marker[key]) {
901 SPItemView *v;
903 /* Detach marker */
904 shape->release_connect[key].disconnect();
905 shape->modified_connect[key].disconnect();
907 /* Hide marker */
908 for (v = item->display; v != NULL; v = v->next) {
909 sp_marker_hide ((SPMarker *) (shape->marker[key]),
910 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + key);
911 /* fixme: Do we need explicit remove here? (Lauris) */
912 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
913 }
915 /* Unref marker */
916 shape->marker[key] = sp_object_hunref (shape->marker[key], object);
917 }
918 if (SP_IS_MARKER (mrk)) {
919 shape->marker[key] = sp_object_href (mrk, object);
920 shape->release_connect[key] = mrk->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_shape_marker_release), shape));
921 shape->modified_connect[key] = mrk->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_shape_marker_modified), shape));
922 }
923 }
924 }
928 /* Shape section */
930 /**
931 * Calls any registered handlers for the set_shape action
932 */
933 void
934 sp_shape_set_shape (SPShape *shape)
935 {
936 g_return_if_fail (shape != NULL);
937 g_return_if_fail (SP_IS_SHAPE (shape));
939 if (SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape) {
940 SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape (shape);
941 }
942 }
944 /**
945 * Adds a curve to the shape. If owner is specified, a reference
946 * will be made, otherwise the curve will be copied into the shape.
947 * Any existing curve in the shape will be unreferenced first.
948 * This routine also triggers a request to update the display.
949 */
950 void
951 sp_shape_set_curve (SPShape *shape, SPCurve *curve, unsigned int owner)
952 {
953 if (shape->curve) {
954 shape->curve = shape->curve->unref();
955 }
956 if (curve) {
957 if (owner) {
958 shape->curve = curve->ref();
959 } else {
960 shape->curve = curve->copy();
961 }
962 }
963 SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
964 }
966 /**
967 * Return duplicate of curve (if any exists) or NULL if there is no curve
968 */
969 SPCurve *
970 sp_shape_get_curve (SPShape *shape)
971 {
972 if (shape->curve) {
973 return shape->curve->copy();
974 }
975 return NULL;
976 }
978 /**
979 * Same as sp_shape_set_curve but without updating the display
980 */
981 void
982 sp_shape_set_curve_insync (SPShape *shape, SPCurve *curve, unsigned int owner)
983 {
984 if (shape->curve) {
985 shape->curve = shape->curve->unref();
986 }
987 if (curve) {
988 if (owner) {
989 shape->curve = curve->ref();
990 } else {
991 shape->curve = curve->copy();
992 }
993 }
994 }
996 /**
997 * Return all nodes in a path that are to be considered for snapping
998 */
999 static void sp_shape_snappoints(SPItem const *item, SnapPointsIter p)
1000 {
1001 g_assert(item != NULL);
1002 g_assert(SP_IS_SHAPE(item));
1004 SPShape const *shape = SP_SHAPE(item);
1005 if (shape->curve == NULL) {
1006 return;
1007 }
1009 NR::Matrix const i2d (from_2geom(sp_item_i2d_affine (item)));
1010 NArtBpath const *b = SP_CURVE_BPATH(shape->curve);
1012 // Cycle through the nodes in the concatenated subpaths
1013 while (b->code != NR_END) {
1014 NR::Point pos = b->c(3) * i2d; // this is the current node
1016 // NR_MOVETO Indicates the start of a closed subpath, see nr-path-code.h
1017 // If we're looking at a closed subpath, then we can skip this first
1018 // point of the subpath because it's coincident with the last point.
1019 if (b->code != NR_MOVETO) {
1020 if (b->code == NR_MOVETO_OPEN || b->code == NR_LINETO || b[1].code == NR_LINETO || b[1].code == NR_END) {
1021 // end points of a line segment are always considered for snapping
1022 *p = pos;
1023 } else {
1024 // g_assert(b->code == NR_CURVETO);
1025 NR::Point ppos, npos;
1026 ppos = b->code == NR_CURVETO ? b->c(2) * i2d : pos; // backward handle
1027 npos = b[1].code == NR_CURVETO ? b[1].c(1) * i2d : pos; // forward handle
1028 // Determine whether a node is at a smooth part of the path, by
1029 // calculating a measure for the collinearity of the handles
1030 bool c1 = fabs (Inkscape::Util::triangle_area (pos, ppos, npos)) < 1; // points are (almost) collinear
1031 bool c2 = NR::L2(pos - ppos) < 1e-6 || NR::L2(pos - npos) < 1e-6; // endnode, or a node with a retracted handle
1032 if (!(c1 & !c2)) {
1033 *p = pos; // only return non-smooth nodes ("cusps")
1034 }
1035 }
1036 }
1038 b++;
1039 }
1040 }
1042 /*
1043 Local Variables:
1044 mode:c++
1045 c-file-style:"stroustrup"
1046 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1047 indent-tabs-mode:nil
1048 fill-column:99
1049 End:
1050 */
1051 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :