1 #define __SP_SHAPE_C__
3 /*
4 * Base class for shapes, including <path> element
5 *
6 * Author:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 1999-2002 Lauris Kaplinski
10 * Copyright (C) 2000-2001 Ximian, Inc.
11 * Copyright (C) 2004 John Cliff
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
21 #include <libnr/n-art-bpath.h>
22 #include <libnr/nr-matrix-fns.h>
23 #include <libnr/nr-matrix-ops.h>
24 #include <libnr/nr-matrix-translate-ops.h>
25 #include <libnr/nr-scale-matrix-ops.h>
28 #include "macros.h"
29 #include "display/nr-arena-shape.h"
30 #include "print.h"
31 #include "document.h"
32 #include "marker-status.h"
33 #include "style.h"
34 #include "sp-marker.h"
35 #include "sp-path.h"
36 #include "prefs-utils.h"
38 #define noSHAPE_VERBOSE
40 static void sp_shape_class_init (SPShapeClass *klass);
41 static void sp_shape_init (SPShape *shape);
43 static void sp_shape_build (SPObject * object, SPDocument * document, Inkscape::XML::Node * repr);
44 static void sp_shape_release (SPObject *object);
46 static void sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags);
47 static void sp_shape_modified (SPObject *object, unsigned int flags);
49 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
50 void sp_shape_print (SPItem * item, SPPrintContext * ctx);
51 static NRArenaItem *sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
52 static void sp_shape_hide (SPItem *item, unsigned int key);
53 static void sp_shape_snappoints (SPItem const *item, SnapPointsIter p);
55 static void sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai);
56 static int sp_shape_has_markers (SPShape const *shape);
57 static int sp_shape_number_of_markers (SPShape* Shape, int type);
59 static SPItemClass *parent_class;
61 GType
62 sp_shape_get_type (void)
63 {
64 static GType type = 0;
65 if (!type) {
66 GTypeInfo info = {
67 sizeof (SPShapeClass),
68 NULL, NULL,
69 (GClassInitFunc) sp_shape_class_init,
70 NULL, NULL,
71 sizeof (SPShape),
72 16,
73 (GInstanceInitFunc) sp_shape_init,
74 NULL, /* value_table */
75 };
76 type = g_type_register_static (SP_TYPE_ITEM, "SPShape", &info, (GTypeFlags)0);
77 }
78 return type;
79 }
81 static void
82 sp_shape_class_init (SPShapeClass *klass)
83 {
84 SPObjectClass *sp_object_class;
85 SPItemClass * item_class;
86 SPPathClass * path_class;
88 sp_object_class = (SPObjectClass *) klass;
89 item_class = (SPItemClass *) klass;
90 path_class = (SPPathClass *) klass;
92 parent_class = (SPItemClass *)g_type_class_peek_parent (klass);
94 sp_object_class->build = sp_shape_build;
95 sp_object_class->release = sp_shape_release;
96 sp_object_class->update = sp_shape_update;
97 sp_object_class->modified = sp_shape_modified;
99 item_class->bbox = sp_shape_bbox;
100 item_class->print = sp_shape_print;
101 item_class->show = sp_shape_show;
102 item_class->hide = sp_shape_hide;
103 item_class->snappoints = sp_shape_snappoints;
104 }
106 static void
107 sp_shape_init (SPShape *shape)
108 {
109 /* Nothing here */
110 }
112 static void
113 sp_shape_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
114 {
115 if (((SPObjectClass *) (parent_class))->build) {
116 (*((SPObjectClass *) (parent_class))->build) (object, document, repr);
117 }
118 }
120 static void
121 sp_shape_release (SPObject *object)
122 {
123 SPItem *item;
124 SPShape *shape;
125 SPItemView *v;
126 int i;
128 item = (SPItem *) object;
129 shape = (SPShape *) object;
131 for (i=SP_MARKER_LOC_START; i<SP_MARKER_LOC_QTY; i++) {
132 if (shape->marker[i]) {
133 sp_signal_disconnect_by_data (shape->marker[i], object);
134 for (v = item->display; v != NULL; v = v->next) {
135 sp_marker_hide ((SPMarker *) shape->marker[i], NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
136 }
137 shape->marker[i] = sp_object_hunref (shape->marker[i], object);
138 }
139 }
140 if (shape->curve) {
141 shape->curve = sp_curve_unref (shape->curve);
142 }
144 if (((SPObjectClass *) parent_class)->release) {
145 ((SPObjectClass *) parent_class)->release (object);
146 }
147 }
149 static void
150 sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags)
151 {
152 SPItem *item = (SPItem *) object;
153 SPShape *shape = (SPShape *) object;
155 if (((SPObjectClass *) (parent_class))->update) {
156 (* ((SPObjectClass *) (parent_class))->update) (object, ctx, flags);
157 }
159 /* This stanza checks that an object's marker style agrees with
160 * the marker objects it has allocated. sp_shape_set_marker ensures
161 * that the appropriate marker objects are present (or absent) to
162 * match the style.
163 */
164 /* TODO: It would be nice if this could be done at an earlier level */
165 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
166 sp_shape_set_marker (object, i, object->style->marker[i].value);
167 }
169 if (flags & (SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
170 SPStyle *style;
171 style = SP_OBJECT_STYLE (object);
172 if (style->stroke_width.unit == SP_CSS_UNIT_PERCENT) {
173 SPItemCtx *ictx = (SPItemCtx *) ctx;
174 double const aw = 1.0 / NR::expansion(ictx->i2vp);
175 style->stroke_width.computed = style->stroke_width.value * aw;
176 for (SPItemView *v = ((SPItem *) (shape))->display; v != NULL; v = v->next) {
177 nr_arena_shape_set_style ((NRArenaShape *) v->arenaitem, style);
178 }
179 }
180 }
182 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
183 /* This is suboptimal, because changing parent style schedules recalculation */
184 /* But on the other hand - how can we know that parent does not tie style and transform */
185 NR::Rect const paintbox = SP_ITEM(object)->invokeBbox(NR::identity());
186 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
187 NRArenaShape * const s = NR_ARENA_SHAPE(v->arenaitem);
188 if (flags & SP_OBJECT_MODIFIED_FLAG) {
189 nr_arena_shape_set_path(s, shape->curve, (flags & SP_OBJECT_USER_MODIFIED_FLAG_B));
190 }
191 s->setPaintBox(paintbox);
192 }
193 }
195 if (sp_shape_has_markers (shape)) {
197 /* Dimension marker views */
198 for (SPItemView *v = item->display; v != NULL; v = v->next) {
200 if (!v->arenaitem->key) {
201 /* Get enough keys for all, start, mid and end marker types,
202 ** and set this view's arenaitem key to the first of these keys.
203 */
204 NR_ARENA_ITEM_SET_KEY (
205 v->arenaitem,
206 sp_item_display_key_new (SP_MARKER_LOC_QTY)
207 );
208 }
210 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
211 if (shape->marker[i]) {
212 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
213 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i - SP_MARKER_LOC,
214 sp_shape_number_of_markers (shape, i));
215 }
216 }
217 }
219 /* Update marker views */
220 for (SPItemView *v = item->display; v != NULL; v = v->next) {
221 sp_shape_update_marker_view (shape, v->arenaitem);
222 }
223 }
224 }
227 /**
228 * Works out whether a marker of a given type is required at a particular
229 * point on a shape.
230 *
231 * \param shape Shape of interest.
232 * \param m Marker type (e.g. SP_MARKER_LOC_START)
233 * \param bp Path segment.
234 * \return 1 if a marker is required here, otherwise 0.
235 */
236 static bool
237 sp_shape_marker_required(SPShape const *shape, int const m, NArtBpath *bp)
238 {
239 if (shape->marker[m] == NULL) {
240 return false;
241 }
243 if (bp == shape->curve->bpath)
244 return m == SP_MARKER_LOC_START;
245 else if (bp[1].code == NR_END)
246 return m == SP_MARKER_LOC_END;
247 else
248 return m == SP_MARKER_LOC_MID;
249 }
251 static bool
252 is_moveto(NRPathcode const c)
253 {
254 return c == NR_MOVETO || c == NR_MOVETO_OPEN;
255 }
257 /** \pre The bpath[] containing bp begins with a moveto. */
258 static NArtBpath const *
259 first_seg_in_subpath(NArtBpath const *bp)
260 {
261 while (!is_moveto(bp->code)) {
262 --bp;
263 }
264 return bp;
265 }
267 static NArtBpath const *
268 last_seg_in_subpath(NArtBpath const *bp)
269 {
270 for(;;) {
271 ++bp;
272 switch (bp->code) {
273 case NR_MOVETO:
274 case NR_MOVETO_OPEN:
275 case NR_END:
276 --bp;
277 return bp;
279 default: continue;
280 }
281 }
282 }
285 /* A subpath begins with a moveto and ends immediately before the next moveto or NR_END.
286 * (`moveto' here means either NR_MOVETO or NR_MOVETO_OPEN.) I'm assuming that non-empty
287 * paths always begin with a moveto.
288 *
289 * The control points of the subpath are the control points of the path elements of the subpath.
290 *
291 * As usual, the control points of a moveto or NR_LINETO are {c(3)}, and
292 * the control points of a NR_CURVETO are {c(1), c(2), c(3)}.
293 * (It follows from the definition that NR_END isn't part of a subpath.)
294 *
295 * The initial control point is bpath[bi0].c(3).
296 *
297 * Reference: http://www.w3.org/TR/SVG11/painting.html#MarkerElement, the `orient' attribute.
298 * Reference for behaviour of zero-length segments:
299 * http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
300 */
302 static double const no_tangent = 128.0; /* arbitrarily-chosen value outside the range of atan2, i.e. outside of [-pi, pi]. */
304 /** \pre The bpath[] containing bp0 begins with a moveto. */
305 static double
306 outgoing_tangent(NArtBpath const *bp0)
307 {
308 /* See notes in comment block above. */
310 g_assert(bp0->code != NR_END);
311 NR::Point const &p0 = bp0->c(3);
312 NR::Point other;
313 for (NArtBpath const *bp = bp0;;) {
314 ++bp;
315 switch (bp->code) {
316 case NR_LINETO:
317 other = bp->c(3);
318 if (other != p0) {
319 goto found;
320 }
321 break;
323 case NR_CURVETO:
324 for (unsigned ci = 1; ci <= 3; ++ci) {
325 other = bp->c(ci);
326 if (other != p0) {
327 goto found;
328 }
329 }
330 break;
332 case NR_MOVETO_OPEN:
333 case NR_END:
334 case NR_MOVETO:
335 bp = first_seg_in_subpath(bp0);
336 if (bp == bp0) {
337 /* Gone right around the subpath without finding any different point since the
338 * initial moveto. */
339 return no_tangent;
340 }
341 if (bp->code != NR_MOVETO) {
342 /* Open subpath. */
343 return no_tangent;
344 }
345 other = bp->c(3);
346 if (other != p0) {
347 goto found;
348 }
349 break;
350 }
352 if (bp == bp0) {
353 /* Back where we started, so zero-length subpath. */
354 return no_tangent;
356 /* Note: this test must come after we've looked at element bp, in case bp0 is a curve:
357 * we must look at c(1) and c(2). (E.g. single-curve subpath.)
358 */
359 }
360 }
362 found:
363 return atan2( other - p0 );
364 }
366 /** \pre The bpath[] containing bp0 begins with a moveto. */
367 static double
368 incoming_tangent(NArtBpath const *bp0)
369 {
370 /* See notes in comment block before outgoing_tangent. */
372 g_assert(bp0->code != NR_END);
373 NR::Point const &p0 = bp0->c(3);
374 NR::Point other;
375 for (NArtBpath const *bp = bp0;;) {
376 switch (bp->code) {
377 case NR_LINETO:
378 other = bp->c(3);
379 if (other != p0) {
380 goto found;
381 }
382 --bp;
383 break;
385 case NR_CURVETO:
386 for (unsigned ci = 3; ci != 0; --ci) {
387 other = bp->c(ci);
388 if (other != p0) {
389 goto found;
390 }
391 }
392 --bp;
393 break;
395 case NR_MOVETO:
396 case NR_MOVETO_OPEN:
397 other = bp->c(3);
398 if (other != p0) {
399 goto found;
400 }
401 if (bp->code != NR_MOVETO) {
402 /* Open subpath. */
403 return no_tangent;
404 }
405 bp = last_seg_in_subpath(bp0);
406 break;
408 default: /* includes NR_END */
409 g_error("Found invalid path code %u in middle of path.", bp->code);
410 return no_tangent;
411 }
413 if (bp == bp0) {
414 /* Back where we started from: zero-length subpath. */
415 return no_tangent;
416 }
417 }
419 found:
420 return atan2( p0 - other );
421 }
424 /**
425 * Calculate the transform required to get a marker's path object in the
426 * right place for particular path segment on a shape. You should
427 * call sp_shape_marker_required first to see if a marker is required
428 * at this point.
429 *
430 * \see sp_shape_marker_required.
431 *
432 * \param shape Shape which the marker is for.
433 * \param m Marker type (e.g. SP_MARKER_LOC_START)
434 * \param bp Path segment which the arrow is for.
435 * \return Transform matrix.
436 */
438 static NR::Matrix
439 sp_shape_marker_get_transform(SPShape const *shape, NArtBpath const *bp)
440 {
441 g_return_val_if_fail(( is_moveto(shape->curve->bpath[0].code)
442 && ( 0 < shape->curve->end )
443 && ( shape->curve->bpath[shape->curve->end].code == NR_END ) ),
444 NR::Matrix(NR::translate(bp->c(3))));
445 double const angle1 = incoming_tangent(bp);
446 double const angle2 = outgoing_tangent(bp);
448 /* angle1 and angle2 are now each either unset (i.e. still 100 from their initialization) or in
449 [-pi, pi] from atan2. */
450 g_assert((-3.15 < angle1 && angle1 < 3.15) || (angle1 == no_tangent));
451 g_assert((-3.15 < angle2 && angle2 < 3.15) || (angle2 == no_tangent));
453 double ret_angle;
454 if (angle1 == no_tangent) {
455 /* First vertex of an open subpath. */
456 ret_angle = ( angle2 == no_tangent
457 ? 0.
458 : angle2 );
459 } else if (angle2 == no_tangent) {
460 /* Last vertex of an open subpath. */
461 ret_angle = angle1;
462 } else {
463 ret_angle = .5 * (angle1 + angle2);
465 if ( fabs( angle2 - angle1 ) > M_PI ) {
466 /* ret_angle is in the middle of the larger of the two sectors between angle1 and
467 * angle2, so flip it by 180degrees to force it to the middle of the smaller sector.
468 *
469 * (Imagine a circle with rays drawn at angle1 and angle2 from the centre of the
470 * circle. Those two rays divide the circle into two sectors.)
471 */
472 ret_angle += M_PI;
473 }
474 }
476 return NR::Matrix(NR::rotate(ret_angle)) * NR::translate(bp->c(3));
477 }
479 /* Marker views have to be scaled already */
481 static void
482 sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai)
483 {
484 SPStyle *style = ((SPObject *) shape)->style;
486 marker_status("sp_shape_update_marker_view: Updating views of markers");
488 for (int i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
489 if (shape->marker[i] == NULL) {
490 continue;
491 }
493 int n = 0;
495 for (NArtBpath *bp = shape->curve->bpath; bp->code != NR_END; bp++) {
496 if (sp_shape_marker_required (shape, i, bp)) {
497 NR::Matrix const m(sp_shape_marker_get_transform(shape, bp));
498 sp_marker_show_instance ((SPMarker* ) shape->marker[i], ai,
499 NR_ARENA_ITEM_GET_KEY(ai) + i, n, m,
500 style->stroke_width.computed);
501 n++;
502 }
503 }
504 }
505 }
507 static void
508 sp_shape_modified (SPObject *object, unsigned int flags)
509 {
510 SPShape *shape = SP_SHAPE (object);
512 if (((SPObjectClass *) (parent_class))->modified) {
513 (* ((SPObjectClass *) (parent_class))->modified) (object, flags);
514 }
516 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
517 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
518 nr_arena_shape_set_style (NR_ARENA_SHAPE (v->arenaitem), object->style);
519 }
520 }
521 }
523 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
524 {
525 SPShape const *shape = SP_SHAPE (item);
527 if (shape->curve) {
529 NRRect cbbox;
530 NRBPath bp;
532 bp.path = SP_CURVE_BPATH (shape->curve);
534 cbbox.x0 = cbbox.y0 = NR_HUGE;
535 cbbox.x1 = cbbox.y1 = -NR_HUGE;
537 nr_path_matrix_bbox_union(&bp, transform, &cbbox);
539 SPStyle* style=SP_OBJECT_STYLE (item);
540 if (style->stroke.type != SP_PAINT_TYPE_NONE) {
541 double const scale = expansion(transform);
542 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
543 double const width = MAX(0.125, style->stroke_width.computed * scale);
544 if ( fabs(cbbox.x1-cbbox.x0) > -0.00001 && fabs(cbbox.y1-cbbox.y0) > -0.00001 ) {
545 cbbox.x0-=0.5*width;
546 cbbox.x1+=0.5*width;
547 cbbox.y0-=0.5*width;
548 cbbox.y1+=0.5*width;
549 }
550 }
551 }
553 // Union with bboxes of the markers, if any
554 if (sp_shape_has_markers (shape)) {
555 for (NArtBpath* bp = shape->curve->bpath; bp->code != NR_END; bp++) {
556 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
557 if (sp_shape_marker_required (shape, m, bp)) {
559 SPMarker* marker = SP_MARKER (shape->marker[m]);
560 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
562 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
564 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
565 tr = NR::scale(style->stroke_width.computed) * tr;
566 }
568 // total marker transform
569 tr = marker_item->transform * marker->c2p * tr * transform;
571 // get bbox of the marker with that transform
572 NRRect marker_bbox;
573 sp_item_invoke_bbox (marker_item, &marker_bbox, tr, true);
574 // union it with the shape bbox
575 nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
576 }
577 }
578 }
579 }
581 // copy our bbox to the variable we're given
582 *bbox = cbbox;
583 }
584 }
586 void
587 sp_shape_print (SPItem *item, SPPrintContext *ctx)
588 {
589 NRRect pbox, dbox, bbox;
591 SPShape *shape = SP_SHAPE(item);
593 if (!shape->curve) return;
595 gint add_comments = prefs_get_int_attribute_limited ("printing.debug", "add-label-comments", 0, 0, 1);
596 if (add_comments) {
597 gchar * comment = g_strdup_printf("begin '%s'",
598 SP_OBJECT(item)->defaultLabel());
599 sp_print_comment(ctx, comment);
600 g_free(comment);
601 }
603 /* fixme: Think (Lauris) */
604 sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
605 dbox.x0 = 0.0;
606 dbox.y0 = 0.0;
607 dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
608 dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
609 sp_item_bbox_desktop (item, &bbox);
610 NR::Matrix const i2d = sp_item_i2d_affine(item);
612 SPStyle* style = SP_OBJECT_STYLE (item);
614 if (style->fill.type != SP_PAINT_TYPE_NONE) {
615 NRBPath bp;
616 bp.path = shape->curve->bpath;
617 sp_print_fill (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
618 }
620 if (style->stroke.type != SP_PAINT_TYPE_NONE) {
621 NRBPath bp;
622 bp.path = shape->curve->bpath;
623 sp_print_stroke (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
624 }
626 for (NArtBpath* bp = shape->curve->bpath; bp->code != NR_END; bp++) {
627 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
628 if (sp_shape_marker_required (shape, m, bp)) {
630 SPMarker* marker = SP_MARKER (shape->marker[m]);
631 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
633 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
635 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
636 tr = NR::scale(style->stroke_width.computed) * tr;
637 }
639 tr = marker_item->transform * marker->c2p * tr;
641 NR::Matrix old_tr = marker_item->transform;
642 marker_item->transform = tr;
643 sp_item_invoke_print (marker_item, ctx);
644 marker_item->transform = old_tr;
645 }
646 }
647 }
649 if (add_comments) {
650 gchar * comment = g_strdup_printf("end '%s'",
651 SP_OBJECT(item)->defaultLabel());
652 sp_print_comment(ctx, comment);
653 g_free(comment);
654 }
655 }
657 static NRArenaItem *
658 sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
659 {
660 SPObject *object = SP_OBJECT(item);
661 SPShape *shape = SP_SHAPE(item);
663 NRArenaItem *arenaitem = NRArenaShape::create(arena);
664 NRArenaShape * const s = NR_ARENA_SHAPE(arenaitem);
665 nr_arena_shape_set_style(s, object->style);
666 nr_arena_shape_set_path(s, shape->curve, false);
667 NR::Rect const paintbox = item->invokeBbox(NR::identity());
668 s->setPaintBox(paintbox);
670 if (sp_shape_has_markers (shape)) {
672 /* Dimension marker views */
673 if (!arenaitem->key) {
674 NR_ARENA_ITEM_SET_KEY (arenaitem, sp_item_display_key_new (SP_MARKER_LOC_QTY));
675 }
677 for (int i = 0; i < SP_MARKER_LOC_QTY; i++) {
678 if (shape->marker[i]) {
679 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
680 NR_ARENA_ITEM_GET_KEY (arenaitem) + i - SP_MARKER_LOC,
681 sp_shape_number_of_markers (shape, i));
682 }
683 }
686 /* Update marker views */
687 sp_shape_update_marker_view (shape, arenaitem);
688 }
690 return arenaitem;
691 }
693 static void
694 sp_shape_hide (SPItem *item, unsigned int key)
695 {
696 SPShape *shape;
697 SPItemView *v;
698 int i;
700 shape = (SPShape *) item;
702 for (i=0; i<SP_MARKER_LOC_QTY; i++) {
703 if (shape->marker[i]) {
704 for (v = item->display; v != NULL; v = v->next) {
705 if (key == v->key) {
706 sp_marker_hide ((SPMarker *) shape->marker[i],
707 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
708 }
709 }
710 }
711 }
713 if (((SPItemClass *) parent_class)->hide) {
714 ((SPItemClass *) parent_class)->hide (item, key);
715 }
716 }
718 /* Marker stuff */
720 /**
721 * \param shape Shape.
722 * \return TRUE if the shape has any markers, or FALSE if not.
723 */
724 static int
725 sp_shape_has_markers (SPShape const *shape)
726 {
727 /* Note, we're ignoring 'marker' settings, which technically should apply for
728 all three settings. This should be fixed later such that if 'marker' is
729 specified, then all three should appear. */
731 return (
732 shape->curve &&
733 (shape->marker[SP_MARKER_LOC_START] ||
734 shape->marker[SP_MARKER_LOC_MID] ||
735 shape->marker[SP_MARKER_LOC_END])
736 );
737 }
740 /**
741 * \param shape Shape.
742 * \param type Marker type (e.g. SP_MARKER_LOC_START)
743 * \return Number of markers that the shape has of this type.
744 */
745 static int
746 sp_shape_number_of_markers (SPShape *shape, int type)
747 {
748 int n = 0;
749 for (NArtBpath* bp = shape->curve->bpath; bp->code != NR_END; bp++) {
750 if (sp_shape_marker_required (shape, type, bp)) {
751 n++;
752 }
753 }
755 return n;
756 }
758 static void
759 sp_shape_marker_release (SPObject *marker, SPShape *shape)
760 {
761 SPItem *item;
762 int i;
764 item = (SPItem *) shape;
766 marker_status("sp_shape_marker_release: Releasing markers");
767 for (i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
768 if (marker == shape->marker[i]) {
769 SPItemView *v;
770 /* Hide marker */
771 for (v = item->display; v != NULL; v = v->next) {
772 sp_marker_hide ((SPMarker *) (shape->marker[i]), NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
773 /* fixme: Do we need explicit remove here? (Lauris) */
774 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
775 }
776 /* Detach marker */
777 sp_signal_disconnect_by_data (shape->marker[i], item);
778 shape->marker[i] = sp_object_hunref (shape->marker[i], item);
779 }
780 }
781 }
783 static void
784 sp_shape_marker_modified (SPObject *marker, guint flags, SPItem *item)
785 {
786 /* I think mask does update automagically */
787 /* g_warning ("Item %s mask %s modified", SP_OBJECT_ID (item), SP_OBJECT_ID (mask)); */
788 }
790 void
791 sp_shape_set_marker (SPObject *object, unsigned int key, const gchar *value)
792 {
793 SPItem *item = (SPItem *) object;
794 SPShape *shape = (SPShape *) object;
796 if (key < SP_MARKER_LOC_START || key > SP_MARKER_LOC_END) {
797 return;
798 }
800 SPObject *mrk = sp_uri_reference_resolve (SP_OBJECT_DOCUMENT (object), value);
801 if (mrk != shape->marker[key]) {
802 if (shape->marker[key]) {
803 SPItemView *v;
805 /* Detach marker */
806 g_signal_handler_disconnect (shape->marker[key], shape->release_connect[key]);
807 g_signal_handler_disconnect (shape->marker[key], shape->modified_connect[key]);
809 /* Hide marker */
810 for (v = item->display; v != NULL; v = v->next) {
811 sp_marker_hide ((SPMarker *) (shape->marker[key]),
812 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + key);
813 /* fixme: Do we need explicit remove here? (Lauris) */
814 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
815 }
817 /* Unref marker */
818 shape->marker[key] = sp_object_hunref (shape->marker[key], object);
819 }
820 if (SP_IS_MARKER (mrk)) {
821 shape->marker[key] = sp_object_href (mrk, object);
822 shape->release_connect[key] = g_signal_connect (G_OBJECT (shape->marker[key]), "release",
823 G_CALLBACK (sp_shape_marker_release), shape);
824 shape->modified_connect[key] = g_signal_connect (G_OBJECT (shape->marker[key]), "modified",
825 G_CALLBACK (sp_shape_marker_modified), shape);
826 }
827 }
828 }
832 /* Shape section */
834 void
835 sp_shape_set_shape (SPShape *shape)
836 {
837 g_return_if_fail (shape != NULL);
838 g_return_if_fail (SP_IS_SHAPE (shape));
840 if (SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape) {
841 SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape (shape);
842 }
843 }
845 void
846 sp_shape_set_curve (SPShape *shape, SPCurve *curve, unsigned int owner)
847 {
848 if (shape->curve) {
849 shape->curve = sp_curve_unref (shape->curve);
850 }
851 if (curve) {
852 if (owner) {
853 shape->curve = sp_curve_ref (curve);
854 } else {
855 shape->curve = sp_curve_copy (curve);
856 }
857 }
858 SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
859 }
861 /* Return duplicate of curve or NULL */
862 SPCurve *
863 sp_shape_get_curve (SPShape *shape)
864 {
865 if (shape->curve) {
866 return sp_curve_copy (shape->curve);
867 }
868 return NULL;
869 }
871 /* NOT FOR GENERAL PUBLIC UNTIL SORTED OUT (Lauris) */
872 void
873 sp_shape_set_curve_insync (SPShape *shape, SPCurve *curve, unsigned int owner)
874 {
875 if (shape->curve) {
876 shape->curve = sp_curve_unref (shape->curve);
877 }
878 if (curve) {
879 if (owner) {
880 shape->curve = sp_curve_ref (curve);
881 } else {
882 shape->curve = sp_curve_copy (curve);
883 }
884 }
885 }
887 static void sp_shape_snappoints(SPItem const *item, SnapPointsIter p)
888 {
889 g_assert(item != NULL);
890 g_assert(SP_IS_SHAPE(item));
892 SPShape const *shape = SP_SHAPE(item);
893 if (shape->curve == NULL) {
894 return;
895 }
897 NR::Matrix const i2d (sp_item_i2d_affine (item));
899 /* Use the end points of each segment of the path */
900 NArtBpath const *bp = shape->curve->bpath;
901 while (bp->code != NR_END) {
902 *p = bp->c(3) * i2d;
903 bp++;
904 }
905 }
908 /*
909 Local Variables:
910 mode:c++
911 c-file-style:"stroustrup"
912 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
913 indent-tabs-mode:nil
914 fill-column:99
915 End:
916 */
917 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :