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 <2geom/path-intersection.h>
27 #include "helper/geom.h"
28 #include "helper/geom-nodetype.h"
30 #include <sigc++/functors/ptr_fun.h>
31 #include <sigc++/adaptors/bind.h>
33 #include "macros.h"
34 #include "display/nr-arena-shape.h"
35 #include "display/curve.h"
36 #include "print.h"
37 #include "document.h"
38 #include "style.h"
39 #include "marker.h"
40 #include "sp-path.h"
41 #include "preferences.h"
42 #include "attributes.h"
44 #include "live_effects/lpeobject.h"
45 #include "uri.h"
46 #include "extract-uri.h"
47 #include "uri-references.h"
48 #include "bad-uri-exception.h"
49 #include "xml/repr.h"
51 #include "util/mathfns.h" // for triangle_area()
53 #define noSHAPE_VERBOSE
55 static void sp_shape_class_init (SPShapeClass *klass);
56 static void sp_shape_init (SPShape *shape);
57 static void sp_shape_finalize (GObject *object);
59 static void sp_shape_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
60 static void sp_shape_release (SPObject *object);
62 static void sp_shape_set(SPObject *object, unsigned key, gchar const *value);
63 static void sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags);
64 static void sp_shape_modified (SPObject *object, unsigned int flags);
65 static Inkscape::XML::Node *sp_shape_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
67 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags);
68 void sp_shape_print (SPItem * item, SPPrintContext * ctx);
69 static NRArenaItem *sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
70 static void sp_shape_hide (SPItem *item, unsigned int key);
71 static void sp_shape_snappoints (SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs);
73 static void sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai);
75 static SPLPEItemClass *parent_class;
77 /**
78 * Registers the SPShape class with Gdk and returns its type number.
79 */
80 GType
81 sp_shape_get_type (void)
82 {
83 static GType type = 0;
84 if (!type) {
85 GTypeInfo info = {
86 sizeof (SPShapeClass),
87 NULL, NULL,
88 (GClassInitFunc) sp_shape_class_init,
89 NULL, NULL,
90 sizeof (SPShape),
91 16,
92 (GInstanceInitFunc) sp_shape_init,
93 NULL, /* value_table */
94 };
95 type = g_type_register_static (SP_TYPE_LPE_ITEM, "SPShape", &info, (GTypeFlags)0);
96 }
97 return type;
98 }
100 /**
101 * Initializes a SPShapeClass object. Establishes the function pointers to the class'
102 * member routines in the class vtable, and sets pointers to parent classes.
103 */
104 static void
105 sp_shape_class_init (SPShapeClass *klass)
106 {
107 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
108 SPObjectClass *sp_object_class = SP_OBJECT_CLASS(klass);
109 SPItemClass * item_class = SP_ITEM_CLASS(klass);
110 SPLPEItemClass * lpe_item_class = SP_LPE_ITEM_CLASS(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;
131 }
133 /**
134 * Initializes an SPShape object.
135 */
136 static void
137 sp_shape_init (SPShape *shape)
138 {
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 }
143 }
145 static void
146 sp_shape_finalize (GObject *object)
147 {
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 }
160 }
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)
171 {
172 if (((SPObjectClass *) (parent_class))->build) {
173 (*((SPObjectClass *) (parent_class))->build) (object, document, repr);
174 }
176 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
177 sp_shape_set_marker (object, i, object->style->marker[i].value);
178 }
179 }
181 /**
182 * Removes, releases and unrefs all children of object
183 *
184 * This is the inverse of sp_shape_build(). It must be invoked as soon
185 * as the shape is removed from the tree, even if it is still referenced
186 * by other objects. This routine also disconnects/unrefs markers and
187 * curves attached to it.
188 *
189 * \see sp_object_release()
190 */
191 static void
192 sp_shape_release (SPObject *object)
193 {
194 SPItem *item;
195 SPShape *shape;
196 SPItemView *v;
197 int i;
199 item = (SPItem *) object;
200 shape = (SPShape *) object;
202 for (i=SP_MARKER_LOC_START; i<SP_MARKER_LOC_QTY; i++) {
203 if (shape->marker[i]) {
204 for (v = item->display; v != NULL; v = v->next) {
205 sp_marker_hide ((SPMarker *) shape->marker[i], NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
206 }
207 shape->release_connect[i].disconnect();
208 shape->modified_connect[i].disconnect();
209 shape->marker[i] = sp_object_hunref (shape->marker[i], object);
210 }
211 }
212 if (shape->curve) {
213 shape->curve = shape->curve->unref();
214 }
216 if (((SPObjectClass *) parent_class)->release) {
217 ((SPObjectClass *) parent_class)->release (object);
218 }
219 }
223 static void
224 sp_shape_set(SPObject *object, unsigned int key, gchar const *value)
225 {
226 if (((SPObjectClass *) parent_class)->set) {
227 ((SPObjectClass *) parent_class)->set(object, key, value);
228 }
229 }
231 static Inkscape::XML::Node *
232 sp_shape_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags)
233 {
234 if (((SPObjectClass *)(parent_class))->write) {
235 ((SPObjectClass *)(parent_class))->write(object, doc, repr, flags);
236 }
238 return repr;
239 }
241 /**
242 * Updates the shape when its attributes have changed. Also establishes
243 * marker objects to match the style settings.
244 */
245 static void
246 sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags)
247 {
248 SPItem *item = (SPItem *) object;
249 SPShape *shape = (SPShape *) object;
251 if (((SPObjectClass *) (parent_class))->update) {
252 (* ((SPObjectClass *) (parent_class))->update) (object, ctx, flags);
253 }
255 /* This stanza checks that an object's marker style agrees with
256 * the marker objects it has allocated. sp_shape_set_marker ensures
257 * that the appropriate marker objects are present (or absent) to
258 * match the style.
259 */
260 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
261 sp_shape_set_marker (object, i, object->style->marker[i].value);
262 }
264 if (flags & (SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
265 SPStyle *style;
266 style = SP_OBJECT_STYLE (object);
267 if (style->stroke_width.unit == SP_CSS_UNIT_PERCENT) {
268 SPItemCtx *ictx = (SPItemCtx *) ctx;
269 double const aw = 1.0 / NR::expansion(ictx->i2vp);
270 style->stroke_width.computed = style->stroke_width.value * aw;
271 for (SPItemView *v = ((SPItem *) (shape))->display; v != NULL; v = v->next) {
272 nr_arena_shape_set_style ((NRArenaShape *) v->arenaitem, style);
273 }
274 }
275 }
277 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
278 /* This is suboptimal, because changing parent style schedules recalculation */
279 /* But on the other hand - how can we know that parent does not tie style and transform */
280 Geom::OptRect paintbox = SP_ITEM(object)->getBounds(Geom::identity(), SPItem::GEOMETRIC_BBOX);
281 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
282 NRArenaShape * const s = NR_ARENA_SHAPE(v->arenaitem);
283 if (flags & SP_OBJECT_MODIFIED_FLAG) {
284 nr_arena_shape_set_path(s, shape->curve, (flags & SP_OBJECT_USER_MODIFIED_FLAG_B));
285 }
286 if (paintbox) {
287 s->setPaintBox(*paintbox);
288 }
289 }
290 }
292 if (sp_shape_has_markers (shape)) {
293 /* Dimension marker views */
294 for (SPItemView *v = item->display; v != NULL; v = v->next) {
295 if (!v->arenaitem->key) {
296 NR_ARENA_ITEM_SET_KEY (v->arenaitem, sp_item_display_key_new (SP_MARKER_LOC_QTY));
297 }
298 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
299 if (shape->marker[i]) {
300 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
301 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i - SP_MARKER_LOC,
302 sp_shape_number_of_markers (shape, i));
303 }
304 }
305 }
307 /* Update marker views */
308 for (SPItemView *v = item->display; v != NULL; v = v->next) {
309 sp_shape_update_marker_view (shape, v->arenaitem);
310 }
311 }
312 }
314 /**
315 * Calculate the transform required to get a marker's path object in the
316 * right place for particular path segment on a shape.
317 *
318 * \see sp_shape_marker_update_marker_view.
319 *
320 * From SVG spec:
321 * The axes of the temporary new user coordinate system are aligned according to the orient attribute on the 'marker'
322 * element and the slope of the curve at the given vertex. (Note: if there is a discontinuity at a vertex, the slope
323 * is the average of the slopes of the two segments of the curve that join at the given vertex. If a slope cannot be
324 * determined, the slope is assumed to be zero.)
325 *
326 * Reference: http://www.w3.org/TR/SVG11/painting.html#MarkerElement, the `orient' attribute.
327 * Reference for behaviour of zero-length segments:
328 * http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
329 */
330 Geom::Matrix
331 sp_shape_marker_get_transform(Geom::Curve const & c1, Geom::Curve const & c2)
332 {
333 Geom::Point p = c1.pointAt(1);
334 Geom::Curve * c1_reverse = c1.reverse();
335 Geom::Point tang1 = - c1_reverse->unitTangentAt(0);
336 delete c1_reverse;
337 Geom::Point tang2 = c2.unitTangentAt(0);
339 double const angle1 = Geom::atan2(tang1);
340 double const angle2 = Geom::atan2(tang2);
342 double ret_angle;
343 ret_angle = .5 * (angle1 + angle2);
345 if ( fabs( angle2 - angle1 ) > M_PI ) {
346 /* ret_angle is in the middle of the larger of the two sectors between angle1 and
347 * angle2, so flip it by 180degrees to force it to the middle of the smaller sector.
348 *
349 * (Imagine a circle with rays drawn at angle1 and angle2 from the centre of the
350 * circle. Those two rays divide the circle into two sectors.)
351 */
352 ret_angle += M_PI;
353 }
355 return Geom::Rotate(ret_angle) * Geom::Translate(p);
356 }
357 Geom::Matrix
358 sp_shape_marker_get_transform_at_start(Geom::Curve const & c)
359 {
360 Geom::Point p = c.pointAt(0);
361 Geom::Matrix ret = Geom::Translate(p);
363 if ( !c.isDegenerate() ) {
364 Geom::Point tang = c.unitTangentAt(0);
365 double const angle = Geom::atan2(tang);
366 ret = Geom::Rotate(angle) * Geom::Translate(p);
367 } else {
368 /* FIXME: the svg spec says to search for a better alternative than zero angle directionality:
369 * http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes */
370 }
372 return ret;
373 }
374 Geom::Matrix
375 sp_shape_marker_get_transform_at_end(Geom::Curve const & c)
376 {
377 Geom::Point p = c.pointAt(1);
378 Geom::Matrix ret = Geom::Translate(p);
380 if ( !c.isDegenerate() ) {
381 Geom::Curve * c_reverse = c.reverse();
382 Geom::Point tang = - c_reverse->unitTangentAt(0);
383 delete c_reverse;
384 double const angle = Geom::atan2(tang);
385 ret = Geom::Rotate(angle) * Geom::Translate(p);
386 } else {
387 /* FIXME: the svg spec says to search for a better alternative than zero angle directionality:
388 * http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes */
389 }
391 return ret;
392 }
394 /**
395 * Updates the instances (views) of a given marker in a shape.
396 * Marker views have to be scaled already. The transformation
397 * is retrieved and then shown by calling sp_marker_show_instance.
398 *
399 * TODO: correctly handle the 'marker' attribute.
400 * "Using the marker property from a style sheet is equivalent to using all three (start, mid, end)."
401 * See painting-marker-03-f.svg in SVG 1.1 Full test suite.
402 */
403 static void
404 sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai)
405 {
406 SPStyle *style = ((SPObject *) shape)->style;
408 // position arguments to sp_marker_show_instance, basically counts the amount of markers.
409 int start_pos = 0;
410 int mid_pos = 0;
411 int end_pos = 0;
413 Geom::PathVector const & pathv = shape->curve->get_pathvector();
414 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
415 if ( shape->marker[SP_MARKER_LOC_START] ) {
416 Geom::Matrix const m (sp_shape_marker_get_transform_at_start(path_it->front()));
417 sp_marker_show_instance ((SPMarker* ) shape->marker[SP_MARKER_LOC_START], ai,
418 NR_ARENA_ITEM_GET_KEY(ai) + SP_MARKER_LOC_START, start_pos, m,
419 style->stroke_width.computed);
420 start_pos++;
421 }
423 if ( shape->marker[SP_MARKER_LOC_MID] && (path_it->size_default() > 1) ) {
424 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
425 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
426 while (curve_it2 != path_it->end_default())
427 {
428 /* Put marker between curve_it1 and curve_it2.
429 * Loop to end_default (so including closing segment), because when a path is closed,
430 * there should be a midpoint marker between last segment and closing straight line segment
431 */
432 Geom::Matrix const m (sp_shape_marker_get_transform(*curve_it1, *curve_it2));
433 sp_marker_show_instance ((SPMarker* ) shape->marker[SP_MARKER_LOC_MID], ai,
434 NR_ARENA_ITEM_GET_KEY(ai) + SP_MARKER_LOC_MID, mid_pos, m,
435 style->stroke_width.computed);
436 mid_pos++;
438 ++curve_it1;
439 ++curve_it2;
440 }
441 }
443 if ( shape->marker[SP_MARKER_LOC_END] ) {
444 /* Get reference to last curve in the path.
445 * For moveto-only path, this returns the "closing line segment". */
446 unsigned int index = path_it->size_default();
447 if (index > 0) {
448 index--;
449 }
450 Geom::Curve const &lastcurve = (*path_it)[index];
452 Geom::Matrix const m = sp_shape_marker_get_transform_at_end(lastcurve);
453 sp_marker_show_instance ((SPMarker* ) shape->marker[SP_MARKER_LOC_END], ai,
454 NR_ARENA_ITEM_GET_KEY(ai) + SP_MARKER_LOC_END, end_pos, m,
455 style->stroke_width.computed);
456 end_pos++;
457 }
458 }
459 }
461 /**
462 * Sets modified flag for all sub-item views.
463 */
464 static void
465 sp_shape_modified (SPObject *object, unsigned int flags)
466 {
467 SPShape *shape = SP_SHAPE (object);
469 if (((SPObjectClass *) (parent_class))->modified) {
470 (* ((SPObjectClass *) (parent_class))->modified) (object, flags);
471 }
473 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
474 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
475 nr_arena_shape_set_style (NR_ARENA_SHAPE (v->arenaitem), object->style);
476 }
477 }
478 }
480 /**
481 * Calculates the bounding box for item, storing it into bbox.
482 * This also includes the bounding boxes of any markers included in the shape.
483 */
484 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags)
485 {
486 SPShape const *shape = SP_SHAPE (item);
487 if (shape->curve) {
488 Geom::OptRect geombbox = bounds_exact_transformed(shape->curve->get_pathvector(), transform);
489 if (geombbox) {
490 NRRect cbbox;
491 cbbox.x0 = (*geombbox)[0][0];
492 cbbox.y0 = (*geombbox)[1][0];
493 cbbox.x1 = (*geombbox)[0][1];
494 cbbox.y1 = (*geombbox)[1][1];
496 if ((SPItem::BBoxType) flags != SPItem::GEOMETRIC_BBOX) {
498 SPStyle* style=SP_OBJECT_STYLE (item);
499 if (!style->stroke.isNone()) {
500 double const scale = transform.descrim();
501 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
502 double const width = MAX(0.125, style->stroke_width.computed * scale);
503 if ( fabs(cbbox.x1-cbbox.x0) > -0.00001 && fabs(cbbox.y1-cbbox.y0) > -0.00001 ) {
504 cbbox.x0-=0.5*width;
505 cbbox.x1+=0.5*width;
506 cbbox.y0-=0.5*width;
507 cbbox.y1+=0.5*width;
508 }
509 }
510 }
512 // Union with bboxes of the markers, if any
513 if (sp_shape_has_markers (shape)) {
514 /* TODO: make code prettier: lots of variables can be taken out of the loop! */
515 Geom::PathVector const & pathv = shape->curve->get_pathvector();
516 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
517 if ( shape->marker[SP_MARKER_LOC_START] ) {
518 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
519 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
521 Geom::Matrix tr(sp_shape_marker_get_transform_at_start(path_it->front()));
523 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
524 tr = Geom::Scale(style->stroke_width.computed) * tr;
525 }
527 // total marker transform
528 tr = marker_item->transform * marker->c2p * tr * transform;
530 // get bbox of the marker with that transform
531 NRRect marker_bbox;
532 sp_item_invoke_bbox (marker_item, &marker_bbox, from_2geom(tr), true);
533 // union it with the shape bbox
534 nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
535 }
537 if ( shape->marker[SP_MARKER_LOC_MID] && (path_it->size_default() > 1) ) {
538 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
539 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
540 while (curve_it2 != path_it->end_default())
541 {
542 /* Put marker between curve_it1 and curve_it2.
543 * Loop to end_default (so including closing segment), because when a path is closed,
544 * there should be a midpoint marker between last segment and closing straight line segment */
546 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
547 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
549 Geom::Matrix tr(sp_shape_marker_get_transform(*curve_it1, *curve_it2));
551 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
552 tr = Geom::Scale(style->stroke_width.computed) * tr;
553 }
555 // total marker transform
556 tr = marker_item->transform * marker->c2p * tr * transform;
558 // get bbox of the marker with that transform
559 NRRect marker_bbox;
560 sp_item_invoke_bbox (marker_item, &marker_bbox, from_2geom(tr), true);
561 // union it with the shape bbox
562 nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
564 ++curve_it1;
565 ++curve_it2;
566 }
567 }
569 if ( shape->marker[SP_MARKER_LOC_END] ) {
570 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
571 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
573 /* Get reference to last curve in the path.
574 * For moveto-only path, this returns the "closing line segment". */
575 unsigned int index = path_it->size_default();
576 if (index > 0) {
577 index--;
578 }
579 Geom::Curve const &lastcurve = (*path_it)[index];
581 Geom::Matrix tr = sp_shape_marker_get_transform_at_end(lastcurve);
583 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
584 tr = Geom::Scale(style->stroke_width.computed) * tr;
585 }
587 // total marker transform
588 tr = marker_item->transform * marker->c2p * tr * transform;
590 // get bbox of the marker with that transform
591 NRRect marker_bbox;
592 sp_item_invoke_bbox (marker_item, &marker_bbox, tr, true);
593 // union it with the shape bbox
594 nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
595 }
596 }
597 }
598 }
600 // copy our bbox to the variable we're given
601 *bbox = cbbox;
602 }
603 }
604 }
606 /**
607 * Prepares shape for printing. Handles printing of comments for printing
608 * debugging, sizes the item to fit into the document width/height,
609 * applies print fill/stroke, sets transforms for markers, and adds
610 * comment labels.
611 */
612 void
613 sp_shape_print (SPItem *item, SPPrintContext *ctx)
614 {
615 NRRect pbox, dbox, bbox;
617 SPShape *shape = SP_SHAPE(item);
619 if (!shape->curve) return;
621 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
622 gint add_comments = prefs->getBool("/printing/debug/add-label-comments");
623 if (add_comments) {
624 gchar * comment = g_strdup_printf("begin '%s'",
625 SP_OBJECT(item)->defaultLabel());
626 sp_print_comment(ctx, comment);
627 g_free(comment);
628 }
630 /* fixme: Think (Lauris) */
631 sp_item_invoke_bbox(item, &pbox, Geom::identity(), TRUE);
632 dbox.x0 = 0.0;
633 dbox.y0 = 0.0;
634 dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
635 dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
636 sp_item_bbox_desktop (item, &bbox);
637 Geom::Matrix const i2d(sp_item_i2d_affine(item));
639 SPStyle* style = SP_OBJECT_STYLE (item);
641 if (!style->fill.isNone()) {
642 sp_print_fill (ctx, shape->curve->get_pathvector(), &i2d, style, &pbox, &dbox, &bbox);
643 }
645 if (!style->stroke.isNone()) {
646 sp_print_stroke (ctx, shape->curve->get_pathvector(), &i2d, style, &pbox, &dbox, &bbox);
647 }
649 /* TODO: make code prettier: lots of variables can be taken out of the loop! */
650 Geom::PathVector const & pathv = shape->curve->get_pathvector();
651 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
652 if ( shape->marker[SP_MARKER_LOC_START] ) {
653 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_START]);
654 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_START]));
656 Geom::Matrix tr(sp_shape_marker_get_transform_at_start(path_it->front()));
658 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
659 tr = Geom::Scale(style->stroke_width.computed) * tr;
660 }
662 tr = marker_item->transform * marker->c2p * tr;
664 Geom::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;
668 }
670 if ( shape->marker[SP_MARKER_LOC_MID] && (path_it->size_default() > 1) ) {
671 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
672 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
673 while (curve_it2 != path_it->end_default())
674 {
675 /* Put marker between curve_it1 and curve_it2.
676 * Loop to end_default (so including closing segment), because when a path is closed,
677 * there should be a midpoint marker between last segment and closing straight line segment */
679 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_MID]);
680 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_MID]));
682 Geom::Matrix tr(sp_shape_marker_get_transform(*curve_it1, *curve_it2));
684 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
685 tr = Geom::Scale(style->stroke_width.computed) * tr;
686 }
688 tr = marker_item->transform * marker->c2p * tr;
690 Geom::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;
695 ++curve_it1;
696 ++curve_it2;
697 }
698 }
700 if ( shape->marker[SP_MARKER_LOC_END] ) {
701 SPMarker* marker = SP_MARKER (shape->marker[SP_MARKER_LOC_END]);
702 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[SP_MARKER_LOC_END]));
704 /* Get reference to last curve in the path.
705 * For moveto-only path, this returns the "closing line segment". */
706 unsigned int index = path_it->size_default();
707 if (index > 0) {
708 index--;
709 }
710 Geom::Curve const &lastcurve = (*path_it)[index];
712 Geom::Matrix tr = sp_shape_marker_get_transform_at_end(lastcurve);
714 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
715 tr = Geom::Scale(style->stroke_width.computed) * tr;
716 }
718 tr = marker_item->transform * marker->c2p * tr;
720 Geom::Matrix old_tr = marker_item->transform;
721 marker_item->transform = tr;
722 sp_item_invoke_print (marker_item, ctx);
723 marker_item->transform = old_tr;
724 }
725 }
727 if (add_comments) {
728 gchar * comment = g_strdup_printf("end '%s'",
729 SP_OBJECT(item)->defaultLabel());
730 sp_print_comment(ctx, comment);
731 g_free(comment);
732 }
733 }
735 /**
736 * Sets style, path, and paintbox. Updates marker views, including dimensions.
737 */
738 static NRArenaItem *
739 sp_shape_show (SPItem *item, NRArena *arena, unsigned int /*key*/, unsigned int /*flags*/)
740 {
741 SPObject *object = SP_OBJECT(item);
742 SPShape *shape = SP_SHAPE(item);
744 NRArenaItem *arenaitem = NRArenaShape::create(arena);
745 NRArenaShape * const s = NR_ARENA_SHAPE(arenaitem);
746 nr_arena_shape_set_style(s, object->style);
747 nr_arena_shape_set_path(s, shape->curve, false);
748 Geom::OptRect paintbox = item->getBounds(Geom::identity());
749 if (paintbox) {
750 s->setPaintBox(*paintbox);
751 }
753 /* This stanza checks that an object's marker style agrees with
754 * the marker objects it has allocated. sp_shape_set_marker ensures
755 * that the appropriate marker objects are present (or absent) to
756 * match the style.
757 */
758 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
759 sp_shape_set_marker (object, i, object->style->marker[i].value);
760 }
762 if (sp_shape_has_markers (shape)) {
764 /* provide key and dimension the marker views */
765 if (!arenaitem->key) {
766 NR_ARENA_ITEM_SET_KEY (arenaitem, sp_item_display_key_new (SP_MARKER_LOC_QTY));
767 }
769 for (int i = 0; i < SP_MARKER_LOC_QTY; i++) {
770 if (shape->marker[i]) {
771 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
772 NR_ARENA_ITEM_GET_KEY (arenaitem) + i - SP_MARKER_LOC,
773 sp_shape_number_of_markers (shape, i));
774 }
775 }
777 /* Update marker views */
778 sp_shape_update_marker_view (shape, arenaitem);
779 }
781 return arenaitem;
782 }
784 /**
785 * Hides/removes marker views from the shape.
786 */
787 static void
788 sp_shape_hide (SPItem *item, unsigned int key)
789 {
790 SPShape *shape;
791 SPItemView *v;
792 int i;
794 shape = (SPShape *) item;
796 for (i=0; i<SP_MARKER_LOC_QTY; i++) {
797 if (shape->marker[i]) {
798 for (v = item->display; v != NULL; v = v->next) {
799 if (key == v->key) {
800 sp_marker_hide ((SPMarker *) shape->marker[i],
801 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
802 }
803 }
804 }
805 }
807 if (((SPItemClass *) parent_class)->hide) {
808 ((SPItemClass *) parent_class)->hide (item, key);
809 }
810 }
812 /**
813 * \param shape Shape.
814 * \return TRUE if the shape has any markers, or FALSE if not.
815 */
816 int
817 sp_shape_has_markers (SPShape const *shape)
818 {
819 /* Note, we're ignoring 'marker' settings, which technically should apply for
820 all three settings. This should be fixed later such that if 'marker' is
821 specified, then all three should appear. */
823 return (
824 shape->curve &&
825 (shape->marker[SP_MARKER_LOC_START] ||
826 shape->marker[SP_MARKER_LOC_MID] ||
827 shape->marker[SP_MARKER_LOC_END])
828 );
829 }
832 /**
833 * \param shape Shape.
834 * \param type Marker type (e.g. SP_MARKER_LOC_START)
835 * \return Number of markers that the shape has of this type.
836 */
837 int
838 sp_shape_number_of_markers (SPShape *shape, int type)
839 {
840 Geom::PathVector const & pathv = shape->curve->get_pathvector();
842 switch(type) {
843 case SP_MARKER_LOC_START:
844 return shape->marker[SP_MARKER_LOC_START] ? pathv.size() : 0;
846 case SP_MARKER_LOC_MID:
847 {
848 if ( shape->marker[SP_MARKER_LOC_MID] ) {
849 guint n = 0;
850 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
851 n += path_it->size();
852 n += path_it->closed() ? 1 : 0;
853 }
854 return n;
855 } else {
856 return 0;
857 }
858 }
860 case SP_MARKER_LOC_END:
861 {
862 if ( shape->marker[SP_MARKER_LOC_END] ) {
863 guint n = 0;
864 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
865 if (!path_it->empty()) {
866 n++;
867 }
868 }
869 return n;
870 } else {
871 return 0;
872 }
873 }
875 default:
876 return 0;
877 }
878 }
880 /**
881 * Checks if the given marker is used in the shape, and if so, it
882 * releases it by calling sp_marker_hide. Also detaches signals
883 * and unrefs the marker from the shape.
884 */
885 static void
886 sp_shape_marker_release (SPObject *marker, SPShape *shape)
887 {
888 SPItem *item;
889 int i;
891 item = (SPItem *) shape;
893 for (i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
894 if (marker == shape->marker[i]) {
895 SPItemView *v;
896 /* Hide marker */
897 for (v = item->display; v != NULL; v = v->next) {
898 sp_marker_hide ((SPMarker *) (shape->marker[i]), NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
899 /* fixme: Do we need explicit remove here? (Lauris) */
900 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
901 }
902 /* Detach marker */
903 shape->release_connect[i].disconnect();
904 shape->modified_connect[i].disconnect();
905 shape->marker[i] = sp_object_hunref (shape->marker[i], item);
906 }
907 }
908 }
910 /**
911 * No-op. Exists for handling 'modified' messages
912 */
913 static void
914 sp_shape_marker_modified (SPObject */*marker*/, guint /*flags*/, SPItem */*item*/)
915 {
916 /* I think mask does update automagically */
917 /* g_warning ("Item %s mask %s modified", SP_OBJECT_ID (item), SP_OBJECT_ID (mask)); */
918 }
920 /**
921 * Adds a new marker to shape object at the location indicated by key. value
922 * must be a valid URI reference resolvable from the shape object (i.e., present
923 * in the document <defs>). If the shape object already has a marker
924 * registered at the given position, it is removed first. Then the
925 * new marker is hrefed and its signals connected.
926 */
927 void
928 sp_shape_set_marker (SPObject *object, unsigned int key, const gchar *value)
929 {
930 SPItem *item = (SPItem *) object;
931 SPShape *shape = (SPShape *) object;
933 if (key < SP_MARKER_LOC_START || key > SP_MARKER_LOC_END) {
934 return;
935 }
937 SPObject *mrk = sp_css_uri_reference_resolve (SP_OBJECT_DOCUMENT (object), value);
938 if (mrk != shape->marker[key]) {
939 if (shape->marker[key]) {
940 SPItemView *v;
942 /* Detach marker */
943 shape->release_connect[key].disconnect();
944 shape->modified_connect[key].disconnect();
946 /* Hide marker */
947 for (v = item->display; v != NULL; v = v->next) {
948 sp_marker_hide ((SPMarker *) (shape->marker[key]),
949 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + key);
950 /* fixme: Do we need explicit remove here? (Lauris) */
951 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
952 }
954 /* Unref marker */
955 shape->marker[key] = sp_object_hunref (shape->marker[key], object);
956 }
957 if (SP_IS_MARKER (mrk)) {
958 shape->marker[key] = sp_object_href (mrk, object);
959 shape->release_connect[key] = mrk->connectRelease(sigc::bind<1>(sigc::ptr_fun(&sp_shape_marker_release), shape));
960 shape->modified_connect[key] = mrk->connectModified(sigc::bind<2>(sigc::ptr_fun(&sp_shape_marker_modified), shape));
961 }
962 }
963 }
967 /* Shape section */
969 /**
970 * Calls any registered handlers for the set_shape action
971 */
972 void
973 sp_shape_set_shape (SPShape *shape)
974 {
975 g_return_if_fail (shape != NULL);
976 g_return_if_fail (SP_IS_SHAPE (shape));
978 if (SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape) {
979 SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape (shape);
980 }
981 }
983 /**
984 * Adds a curve to the shape. If owner is specified, a reference
985 * will be made, otherwise the curve will be copied into the shape.
986 * Any existing curve in the shape will be unreferenced first.
987 * This routine also triggers a request to update the display.
988 */
989 void
990 sp_shape_set_curve (SPShape *shape, SPCurve *curve, unsigned int owner)
991 {
992 if (shape->curve) {
993 shape->curve = shape->curve->unref();
994 }
995 if (curve) {
996 if (owner) {
997 shape->curve = curve->ref();
998 } else {
999 shape->curve = curve->copy();
1000 }
1001 }
1002 SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
1003 }
1005 /**
1006 * Return duplicate of curve (if any exists) or NULL if there is no curve
1007 */
1008 SPCurve *
1009 sp_shape_get_curve (SPShape *shape)
1010 {
1011 if (shape->curve) {
1012 return shape->curve->copy();
1013 }
1014 return NULL;
1015 }
1017 /**
1018 * Same as sp_shape_set_curve but without updating the display
1019 */
1020 void
1021 sp_shape_set_curve_insync (SPShape *shape, SPCurve *curve, unsigned int owner)
1022 {
1023 if (shape->curve) {
1024 shape->curve = shape->curve->unref();
1025 }
1026 if (curve) {
1027 if (owner) {
1028 shape->curve = curve->ref();
1029 } else {
1030 shape->curve = curve->copy();
1031 }
1032 }
1033 }
1035 /**
1036 * Return all nodes in a path that are to be considered for snapping
1037 */
1038 static void sp_shape_snappoints(SPItem const *item, SnapPointsIter p, Inkscape::SnapPreferences const *snapprefs)
1039 {
1040 g_assert(item != NULL);
1041 g_assert(SP_IS_SHAPE(item));
1043 SPShape const *shape = SP_SHAPE(item);
1044 if (shape->curve == NULL) {
1045 return;
1046 }
1048 Geom::PathVector const &pathv = shape->curve->get_pathvector();
1049 if (pathv.empty())
1050 return;
1052 Geom::Matrix const i2d (sp_item_i2d_affine (item));
1054 for(Geom::PathVector::const_iterator path_it = pathv.begin(); path_it != pathv.end(); ++path_it) {
1055 *p = from_2geom(path_it->initialPoint() * i2d);
1057 Geom::Path::const_iterator curve_it1 = path_it->begin(); // incoming curve
1058 Geom::Path::const_iterator curve_it2 = ++(path_it->begin()); // outgoing curve
1059 while (curve_it2 != path_it->end_closed())
1060 {
1061 /* Test whether to add the node between curve_it1 and curve_it2.
1062 * Loop to end_closed (so always including closing segment); the last node to be added
1063 * is the node between the closing segment and the segment before that, regardless
1064 * of the path being closed or not. If the path is closed, the final point was already added by
1065 * adding the initial point. */
1067 Geom::NodeType nodetype = Geom::get_nodetype(*curve_it1, *curve_it2);
1069 // Depending on the snapping preferences, either add only cusp nodes, or add add both cusp and smooth nodes
1070 if (snapprefs->getSnapSmoothNodes() || nodetype == Geom::NODE_NONE || nodetype == Geom::NODE_CUSP) {
1071 *p = from_2geom(curve_it1->finalPoint() * i2d);
1072 }
1074 // Consider midpoints of line segments for snapping
1075 if (snapprefs->getSnapMidpoints()) {
1076 if (Geom::LineSegment const* line_segment = dynamic_cast<Geom::LineSegment const*>(&(*curve_it1))) {
1077 *p = from_2geom(Geom::middle_point(*line_segment) * i2d);
1078 }
1079 }
1081 ++curve_it1;
1082 ++curve_it2;
1083 }
1085 // Find the internal intersections of each path and consider these for snapping (using "Method 1" as desciribed in Inkscape::ObjectSnapper::_collectNodes())
1086 if (snapprefs->getSnapIntersectionCS()) {
1087 Geom::Crossings cs;
1088 cs = self_crossings(*path_it);
1089 if (cs.size() > 0) { // There might be multiple intersections...
1090 for (Geom::Crossings::const_iterator i = cs.begin(); i != cs.end(); i++) {
1091 Geom::Point p_ix = (*path_it).pointAt((*i).ta);
1092 *p = from_2geom(p_ix * i2d);
1093 }
1094 }
1095 }
1096 }
1100 }
1102 /*
1103 Local Variables:
1104 mode:c++
1105 c-file-style:"stroustrup"
1106 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
1107 indent-tabs-mode:nil
1108 fill-column:99
1109 End:
1110 */
1111 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :