304d481bd625806ffafddb2b914f1f20a2016563
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);
57 static SPItemClass *parent_class;
59 GType
60 sp_shape_get_type (void)
61 {
62 static GType type = 0;
63 if (!type) {
64 GTypeInfo info = {
65 sizeof (SPShapeClass),
66 NULL, NULL,
67 (GClassInitFunc) sp_shape_class_init,
68 NULL, NULL,
69 sizeof (SPShape),
70 16,
71 (GInstanceInitFunc) sp_shape_init,
72 NULL, /* value_table */
73 };
74 type = g_type_register_static (SP_TYPE_ITEM, "SPShape", &info, (GTypeFlags)0);
75 }
76 return type;
77 }
79 static void
80 sp_shape_class_init (SPShapeClass *klass)
81 {
82 SPObjectClass *sp_object_class;
83 SPItemClass * item_class;
84 SPPathClass * path_class;
86 sp_object_class = (SPObjectClass *) klass;
87 item_class = (SPItemClass *) klass;
88 path_class = (SPPathClass *) klass;
90 parent_class = (SPItemClass *)g_type_class_peek_parent (klass);
92 sp_object_class->build = sp_shape_build;
93 sp_object_class->release = sp_shape_release;
94 sp_object_class->update = sp_shape_update;
95 sp_object_class->modified = sp_shape_modified;
97 item_class->bbox = sp_shape_bbox;
98 item_class->print = sp_shape_print;
99 item_class->show = sp_shape_show;
100 item_class->hide = sp_shape_hide;
101 item_class->snappoints = sp_shape_snappoints;
102 }
104 static void
105 sp_shape_init (SPShape *shape)
106 {
107 /* Nothing here */
108 }
110 static void
111 sp_shape_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
112 {
113 if (((SPObjectClass *) (parent_class))->build) {
114 (*((SPObjectClass *) (parent_class))->build) (object, document, repr);
115 }
116 }
118 static void
119 sp_shape_release (SPObject *object)
120 {
121 SPItem *item;
122 SPShape *shape;
123 SPItemView *v;
124 int i;
126 item = (SPItem *) object;
127 shape = (SPShape *) object;
129 for (i=SP_MARKER_LOC_START; i<SP_MARKER_LOC_QTY; i++) {
130 if (shape->marker[i]) {
131 sp_signal_disconnect_by_data (shape->marker[i], object);
132 for (v = item->display; v != NULL; v = v->next) {
133 sp_marker_hide ((SPMarker *) shape->marker[i], NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
134 }
135 shape->marker[i] = sp_object_hunref (shape->marker[i], object);
136 }
137 }
138 if (shape->curve) {
139 shape->curve = sp_curve_unref (shape->curve);
140 }
142 if (((SPObjectClass *) parent_class)->release) {
143 ((SPObjectClass *) parent_class)->release (object);
144 }
145 }
147 static void
148 sp_shape_update (SPObject *object, SPCtx *ctx, unsigned int flags)
149 {
150 SPItem *item = (SPItem *) object;
151 SPShape *shape = (SPShape *) object;
153 if (((SPObjectClass *) (parent_class))->update) {
154 (* ((SPObjectClass *) (parent_class))->update) (object, ctx, flags);
155 }
157 /* This stanza checks that an object's marker style agrees with
158 * the marker objects it has allocated. sp_shape_set_marker ensures
159 * that the appropriate marker objects are present (or absent) to
160 * match the style.
161 */
162 /* TODO: It would be nice if this could be done at an earlier level */
163 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
164 sp_shape_set_marker (object, i, object->style->marker[i].value);
165 }
167 if (flags & (SP_OBJECT_STYLE_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG)) {
168 SPStyle *style;
169 style = SP_OBJECT_STYLE (object);
170 if (style->stroke_width.unit == SP_CSS_UNIT_PERCENT) {
171 SPItemCtx *ictx = (SPItemCtx *) ctx;
172 double const aw = 1.0 / NR::expansion(ictx->i2vp);
173 style->stroke_width.computed = style->stroke_width.value * aw;
174 for (SPItemView *v = ((SPItem *) (shape))->display; v != NULL; v = v->next) {
175 nr_arena_shape_set_style ((NRArenaShape *) v->arenaitem, style);
176 }
177 }
178 }
180 if (flags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_PARENT_MODIFIED_FLAG)) {
181 /* This is suboptimal, because changing parent style schedules recalculation */
182 /* But on the other hand - how can we know that parent does not tie style and transform */
183 NR::Rect const paintbox = SP_ITEM(object)->invokeBbox(NR::identity());
184 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
185 NRArenaShape * const s = NR_ARENA_SHAPE(v->arenaitem);
186 if (flags & SP_OBJECT_MODIFIED_FLAG) {
187 nr_arena_shape_set_path(s, shape->curve, (flags & SP_OBJECT_USER_MODIFIED_FLAG_B));
188 }
189 s->setPaintBox(paintbox);
190 }
191 }
193 if (sp_shape_has_markers (shape)) {
195 /* Dimension marker views */
196 for (SPItemView *v = item->display; v != NULL; v = v->next) {
198 if (!v->arenaitem->key) {
199 /* Get enough keys for all, start, mid and end marker types,
200 ** and set this view's arenaitem key to the first of these keys.
201 */
202 NR_ARENA_ITEM_SET_KEY (
203 v->arenaitem,
204 sp_item_display_key_new (SP_MARKER_LOC_QTY)
205 );
206 }
208 for (int i = 0 ; i < SP_MARKER_LOC_QTY ; i++) {
209 if (shape->marker[i]) {
210 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
211 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i - SP_MARKER_LOC,
212 sp_shape_number_of_markers (shape, i));
213 }
214 }
215 }
217 /* Update marker views */
218 for (SPItemView *v = item->display; v != NULL; v = v->next) {
219 sp_shape_update_marker_view (shape, v->arenaitem);
220 }
221 }
222 }
225 /**
226 * Works out whether a marker of a given type is required at a particular
227 * point on a shape.
228 *
229 * \param shape Shape of interest.
230 * \param m Marker type (e.g. SP_MARKER_LOC_START)
231 * \param bp Path segment.
232 * \return 1 if a marker is required here, otherwise 0.
233 */
234 bool
235 sp_shape_marker_required(SPShape const *shape, int const m, NArtBpath *bp)
236 {
237 if (shape->marker[m] == NULL) {
238 return false;
239 }
241 if (bp == SP_CURVE_BPATH(shape->curve))
242 return m == SP_MARKER_LOC_START;
243 else if (bp[1].code == NR_END)
244 return m == SP_MARKER_LOC_END;
245 else
246 return m == SP_MARKER_LOC_MID;
247 }
249 static bool
250 is_moveto(NRPathcode const c)
251 {
252 return c == NR_MOVETO || c == NR_MOVETO_OPEN;
253 }
255 /** \pre The bpath[] containing bp begins with a moveto. */
256 static NArtBpath const *
257 first_seg_in_subpath(NArtBpath const *bp)
258 {
259 while (!is_moveto(bp->code)) {
260 --bp;
261 }
262 return bp;
263 }
265 static NArtBpath const *
266 last_seg_in_subpath(NArtBpath const *bp)
267 {
268 for(;;) {
269 ++bp;
270 switch (bp->code) {
271 case NR_MOVETO:
272 case NR_MOVETO_OPEN:
273 case NR_END:
274 --bp;
275 return bp;
277 default: continue;
278 }
279 }
280 }
283 /* A subpath begins with a moveto and ends immediately before the next moveto or NR_END.
284 * (`moveto' here means either NR_MOVETO or NR_MOVETO_OPEN.) I'm assuming that non-empty
285 * paths always begin with a moveto.
286 *
287 * The control points of the subpath are the control points of the path elements of the subpath.
288 *
289 * As usual, the control points of a moveto or NR_LINETO are {c(3)}, and
290 * the control points of a NR_CURVETO are {c(1), c(2), c(3)}.
291 * (It follows from the definition that NR_END isn't part of a subpath.)
292 *
293 * The initial control point is bpath[bi0].c(3).
294 *
295 * Reference: http://www.w3.org/TR/SVG11/painting.html#MarkerElement, the `orient' attribute.
296 * Reference for behaviour of zero-length segments:
297 * http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
298 */
300 static double const no_tangent = 128.0; /* arbitrarily-chosen value outside the range of atan2, i.e. outside of [-pi, pi]. */
302 /** \pre The bpath[] containing bp0 begins with a moveto. */
303 static double
304 outgoing_tangent(NArtBpath const *bp0)
305 {
306 /* See notes in comment block above. */
308 g_assert(bp0->code != NR_END);
309 NR::Point const &p0 = bp0->c(3);
310 NR::Point other;
311 for (NArtBpath const *bp = bp0;;) {
312 ++bp;
313 switch (bp->code) {
314 case NR_LINETO:
315 other = bp->c(3);
316 if (other != p0) {
317 goto found;
318 }
319 break;
321 case NR_CURVETO:
322 for (unsigned ci = 1; ci <= 3; ++ci) {
323 other = bp->c(ci);
324 if (other != p0) {
325 goto found;
326 }
327 }
328 break;
330 case NR_MOVETO_OPEN:
331 case NR_END:
332 case NR_MOVETO:
333 bp = first_seg_in_subpath(bp0);
334 if (bp == bp0) {
335 /* Gone right around the subpath without finding any different point since the
336 * initial moveto. */
337 return no_tangent;
338 }
339 if (bp->code != NR_MOVETO) {
340 /* Open subpath. */
341 return no_tangent;
342 }
343 other = bp->c(3);
344 if (other != p0) {
345 goto found;
346 }
347 break;
348 }
350 if (bp == bp0) {
351 /* Back where we started, so zero-length subpath. */
352 return no_tangent;
354 /* Note: this test must come after we've looked at element bp, in case bp0 is a curve:
355 * we must look at c(1) and c(2). (E.g. single-curve subpath.)
356 */
357 }
358 }
360 found:
361 return atan2( other - p0 );
362 }
364 /** \pre The bpath[] containing bp0 begins with a moveto. */
365 static double
366 incoming_tangent(NArtBpath const *bp0)
367 {
368 /* See notes in comment block before outgoing_tangent. */
370 g_assert(bp0->code != NR_END);
371 NR::Point const &p0 = bp0->c(3);
372 NR::Point other;
373 for (NArtBpath const *bp = bp0;;) {
374 switch (bp->code) {
375 case NR_LINETO:
376 other = bp->c(3);
377 if (other != p0) {
378 goto found;
379 }
380 --bp;
381 break;
383 case NR_CURVETO:
384 for (unsigned ci = 3; ci != 0; --ci) {
385 other = bp->c(ci);
386 if (other != p0) {
387 goto found;
388 }
389 }
390 --bp;
391 break;
393 case NR_MOVETO:
394 case NR_MOVETO_OPEN:
395 other = bp->c(3);
396 if (other != p0) {
397 goto found;
398 }
399 if (bp->code != NR_MOVETO) {
400 /* Open subpath. */
401 return no_tangent;
402 }
403 bp = last_seg_in_subpath(bp0);
404 break;
406 default: /* includes NR_END */
407 g_error("Found invalid path code %u in middle of path.", bp->code);
408 return no_tangent;
409 }
411 if (bp == bp0) {
412 /* Back where we started from: zero-length subpath. */
413 return no_tangent;
414 }
415 }
417 found:
418 return atan2( p0 - other );
419 }
422 /**
423 * Calculate the transform required to get a marker's path object in the
424 * right place for particular path segment on a shape. You should
425 * call sp_shape_marker_required first to see if a marker is required
426 * at this point.
427 *
428 * \see sp_shape_marker_required.
429 *
430 * \param shape Shape which the marker is for.
431 * \param m Marker type (e.g. SP_MARKER_LOC_START)
432 * \param bp Path segment which the arrow is for.
433 * \return Transform matrix.
434 */
436 NR::Matrix
437 sp_shape_marker_get_transform(SPShape const *shape, NArtBpath const *bp)
438 {
439 g_return_val_if_fail(( is_moveto(SP_CURVE_BPATH(shape->curve)[0].code)
440 && ( 0 < shape->curve->end )
441 && ( SP_CURVE_BPATH(shape->curve)[shape->curve->end].code == NR_END ) ),
442 NR::Matrix(NR::translate(bp->c(3))));
443 double const angle1 = incoming_tangent(bp);
444 double const angle2 = outgoing_tangent(bp);
446 /* angle1 and angle2 are now each either unset (i.e. still 100 from their initialization) or in
447 [-pi, pi] from atan2. */
448 g_assert((-3.15 < angle1 && angle1 < 3.15) || (angle1 == no_tangent));
449 g_assert((-3.15 < angle2 && angle2 < 3.15) || (angle2 == no_tangent));
451 double ret_angle;
452 if (angle1 == no_tangent) {
453 /* First vertex of an open subpath. */
454 ret_angle = ( angle2 == no_tangent
455 ? 0.
456 : angle2 );
457 } else if (angle2 == no_tangent) {
458 /* Last vertex of an open subpath. */
459 ret_angle = angle1;
460 } else {
461 ret_angle = .5 * (angle1 + angle2);
463 if ( fabs( angle2 - angle1 ) > M_PI ) {
464 /* ret_angle is in the middle of the larger of the two sectors between angle1 and
465 * angle2, so flip it by 180degrees to force it to the middle of the smaller sector.
466 *
467 * (Imagine a circle with rays drawn at angle1 and angle2 from the centre of the
468 * circle. Those two rays divide the circle into two sectors.)
469 */
470 ret_angle += M_PI;
471 }
472 }
474 return NR::Matrix(NR::rotate(ret_angle)) * NR::translate(bp->c(3));
475 }
477 /* Marker views have to be scaled already */
479 static void
480 sp_shape_update_marker_view (SPShape *shape, NRArenaItem *ai)
481 {
482 SPStyle *style = ((SPObject *) shape)->style;
484 marker_status("sp_shape_update_marker_view: Updating views of markers");
486 for (int i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
487 if (shape->marker[i] == NULL) {
488 continue;
489 }
491 int n = 0;
493 for (NArtBpath *bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
494 if (sp_shape_marker_required (shape, i, bp)) {
495 NR::Matrix const m(sp_shape_marker_get_transform(shape, bp));
496 sp_marker_show_instance ((SPMarker* ) shape->marker[i], ai,
497 NR_ARENA_ITEM_GET_KEY(ai) + i, n, m,
498 style->stroke_width.computed);
499 n++;
500 }
501 }
502 }
503 }
505 static void
506 sp_shape_modified (SPObject *object, unsigned int flags)
507 {
508 SPShape *shape = SP_SHAPE (object);
510 if (((SPObjectClass *) (parent_class))->modified) {
511 (* ((SPObjectClass *) (parent_class))->modified) (object, flags);
512 }
514 if (flags & SP_OBJECT_STYLE_MODIFIED_FLAG) {
515 for (SPItemView *v = SP_ITEM (shape)->display; v != NULL; v = v->next) {
516 nr_arena_shape_set_style (NR_ARENA_SHAPE (v->arenaitem), object->style);
517 }
518 }
519 }
521 static void sp_shape_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
522 {
523 SPShape const *shape = SP_SHAPE (item);
525 if (shape->curve) {
527 NRRect cbbox;
528 NRBPath bp;
530 bp.path = SP_CURVE_BPATH (shape->curve);
532 cbbox.x0 = cbbox.y0 = NR_HUGE;
533 cbbox.x1 = cbbox.y1 = -NR_HUGE;
535 nr_path_matrix_bbox_union(&bp, transform, &cbbox);
537 SPStyle* style=SP_OBJECT_STYLE (item);
538 if (style->stroke.type != SP_PAINT_TYPE_NONE) {
539 double const scale = expansion(transform);
540 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
541 double const width = MAX(0.125, style->stroke_width.computed * scale);
542 if ( fabs(cbbox.x1-cbbox.x0) > -0.00001 && fabs(cbbox.y1-cbbox.y0) > -0.00001 ) {
543 cbbox.x0-=0.5*width;
544 cbbox.x1+=0.5*width;
545 cbbox.y0-=0.5*width;
546 cbbox.y1+=0.5*width;
547 }
548 }
549 }
551 // Union with bboxes of the markers, if any
552 if (sp_shape_has_markers (shape)) {
553 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
554 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
555 if (sp_shape_marker_required (shape, m, bp)) {
557 SPMarker* marker = SP_MARKER (shape->marker[m]);
558 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
560 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
562 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
563 tr = NR::scale(style->stroke_width.computed) * tr;
564 }
566 // total marker transform
567 tr = marker_item->transform * marker->c2p * tr * transform;
569 // get bbox of the marker with that transform
570 NRRect marker_bbox;
571 sp_item_invoke_bbox (marker_item, &marker_bbox, tr, true);
572 // union it with the shape bbox
573 nr_rect_d_union (&cbbox, &cbbox, &marker_bbox);
574 }
575 }
576 }
577 }
579 // copy our bbox to the variable we're given
580 *bbox = cbbox;
581 }
582 }
584 void
585 sp_shape_print (SPItem *item, SPPrintContext *ctx)
586 {
587 NRRect pbox, dbox, bbox;
589 SPShape *shape = SP_SHAPE(item);
591 if (!shape->curve) return;
593 gint add_comments = prefs_get_int_attribute_limited ("printing.debug", "add-label-comments", 0, 0, 1);
594 if (add_comments) {
595 gchar * comment = g_strdup_printf("begin '%s'",
596 SP_OBJECT(item)->defaultLabel());
597 sp_print_comment(ctx, comment);
598 g_free(comment);
599 }
601 /* fixme: Think (Lauris) */
602 sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
603 dbox.x0 = 0.0;
604 dbox.y0 = 0.0;
605 dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
606 dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
607 sp_item_bbox_desktop (item, &bbox);
608 NR::Matrix const i2d = sp_item_i2d_affine(item);
610 SPStyle* style = SP_OBJECT_STYLE (item);
612 if (style->fill.type != SP_PAINT_TYPE_NONE) {
613 NRBPath bp;
614 bp.path = SP_CURVE_BPATH(shape->curve);
615 sp_print_fill (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
616 }
618 if (style->stroke.type != SP_PAINT_TYPE_NONE) {
619 NRBPath bp;
620 bp.path = SP_CURVE_BPATH(shape->curve);
621 sp_print_stroke (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
622 }
624 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
625 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
626 if (sp_shape_marker_required (shape, m, bp)) {
628 SPMarker* marker = SP_MARKER (shape->marker[m]);
629 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
631 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
633 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
634 tr = NR::scale(style->stroke_width.computed) * tr;
635 }
637 tr = marker_item->transform * marker->c2p * tr;
639 NR::Matrix old_tr = marker_item->transform;
640 marker_item->transform = tr;
641 sp_item_invoke_print (marker_item, ctx);
642 marker_item->transform = old_tr;
643 }
644 }
645 }
647 if (add_comments) {
648 gchar * comment = g_strdup_printf("end '%s'",
649 SP_OBJECT(item)->defaultLabel());
650 sp_print_comment(ctx, comment);
651 g_free(comment);
652 }
653 }
655 static NRArenaItem *
656 sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
657 {
658 SPObject *object = SP_OBJECT(item);
659 SPShape *shape = SP_SHAPE(item);
661 NRArenaItem *arenaitem = NRArenaShape::create(arena);
662 NRArenaShape * const s = NR_ARENA_SHAPE(arenaitem);
663 nr_arena_shape_set_style(s, object->style);
664 nr_arena_shape_set_path(s, shape->curve, false);
665 NR::Rect const paintbox = item->invokeBbox(NR::identity());
666 s->setPaintBox(paintbox);
668 if (sp_shape_has_markers (shape)) {
670 /* Dimension marker views */
671 if (!arenaitem->key) {
672 NR_ARENA_ITEM_SET_KEY (arenaitem, sp_item_display_key_new (SP_MARKER_LOC_QTY));
673 }
675 for (int i = 0; i < SP_MARKER_LOC_QTY; i++) {
676 if (shape->marker[i]) {
677 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
678 NR_ARENA_ITEM_GET_KEY (arenaitem) + i - SP_MARKER_LOC,
679 sp_shape_number_of_markers (shape, i));
680 }
681 }
684 /* Update marker views */
685 sp_shape_update_marker_view (shape, arenaitem);
686 }
688 return arenaitem;
689 }
691 static void
692 sp_shape_hide (SPItem *item, unsigned int key)
693 {
694 SPShape *shape;
695 SPItemView *v;
696 int i;
698 shape = (SPShape *) item;
700 for (i=0; i<SP_MARKER_LOC_QTY; i++) {
701 if (shape->marker[i]) {
702 for (v = item->display; v != NULL; v = v->next) {
703 if (key == v->key) {
704 sp_marker_hide ((SPMarker *) shape->marker[i],
705 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
706 }
707 }
708 }
709 }
711 if (((SPItemClass *) parent_class)->hide) {
712 ((SPItemClass *) parent_class)->hide (item, key);
713 }
714 }
716 /* Marker stuff */
718 /**
719 * \param shape Shape.
720 * \return TRUE if the shape has any markers, or FALSE if not.
721 */
722 int
723 sp_shape_has_markers (SPShape const *shape)
724 {
725 /* Note, we're ignoring 'marker' settings, which technically should apply for
726 all three settings. This should be fixed later such that if 'marker' is
727 specified, then all three should appear. */
729 return (
730 shape->curve &&
731 (shape->marker[SP_MARKER_LOC_START] ||
732 shape->marker[SP_MARKER_LOC_MID] ||
733 shape->marker[SP_MARKER_LOC_END])
734 );
735 }
738 /**
739 * \param shape Shape.
740 * \param type Marker type (e.g. SP_MARKER_LOC_START)
741 * \return Number of markers that the shape has of this type.
742 */
743 int
744 sp_shape_number_of_markers (SPShape *shape, int type)
745 {
746 int n = 0;
747 for (NArtBpath* bp = SP_CURVE_BPATH(shape->curve); bp->code != NR_END; bp++) {
748 if (sp_shape_marker_required (shape, type, bp)) {
749 n++;
750 }
751 }
753 return n;
754 }
756 static void
757 sp_shape_marker_release (SPObject *marker, SPShape *shape)
758 {
759 SPItem *item;
760 int i;
762 item = (SPItem *) shape;
764 marker_status("sp_shape_marker_release: Releasing markers");
765 for (i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
766 if (marker == shape->marker[i]) {
767 SPItemView *v;
768 /* Hide marker */
769 for (v = item->display; v != NULL; v = v->next) {
770 sp_marker_hide ((SPMarker *) (shape->marker[i]), NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
771 /* fixme: Do we need explicit remove here? (Lauris) */
772 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
773 }
774 /* Detach marker */
775 sp_signal_disconnect_by_data (shape->marker[i], item);
776 shape->marker[i] = sp_object_hunref (shape->marker[i], item);
777 }
778 }
779 }
781 static void
782 sp_shape_marker_modified (SPObject *marker, guint flags, SPItem *item)
783 {
784 /* I think mask does update automagically */
785 /* g_warning ("Item %s mask %s modified", SP_OBJECT_ID (item), SP_OBJECT_ID (mask)); */
786 }
788 void
789 sp_shape_set_marker (SPObject *object, unsigned int key, const gchar *value)
790 {
791 SPItem *item = (SPItem *) object;
792 SPShape *shape = (SPShape *) object;
794 if (key < SP_MARKER_LOC_START || key > SP_MARKER_LOC_END) {
795 return;
796 }
798 SPObject *mrk = sp_uri_reference_resolve (SP_OBJECT_DOCUMENT (object), value);
799 if (mrk != shape->marker[key]) {
800 if (shape->marker[key]) {
801 SPItemView *v;
803 /* Detach marker */
804 g_signal_handler_disconnect (shape->marker[key], shape->release_connect[key]);
805 g_signal_handler_disconnect (shape->marker[key], shape->modified_connect[key]);
807 /* Hide marker */
808 for (v = item->display; v != NULL; v = v->next) {
809 sp_marker_hide ((SPMarker *) (shape->marker[key]),
810 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + key);
811 /* fixme: Do we need explicit remove here? (Lauris) */
812 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
813 }
815 /* Unref marker */
816 shape->marker[key] = sp_object_hunref (shape->marker[key], object);
817 }
818 if (SP_IS_MARKER (mrk)) {
819 shape->marker[key] = sp_object_href (mrk, object);
820 shape->release_connect[key] = g_signal_connect (G_OBJECT (shape->marker[key]), "release",
821 G_CALLBACK (sp_shape_marker_release), shape);
822 shape->modified_connect[key] = g_signal_connect (G_OBJECT (shape->marker[key]), "modified",
823 G_CALLBACK (sp_shape_marker_modified), shape);
824 }
825 }
826 }
830 /* Shape section */
832 void
833 sp_shape_set_shape (SPShape *shape)
834 {
835 g_return_if_fail (shape != NULL);
836 g_return_if_fail (SP_IS_SHAPE (shape));
838 if (SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape) {
839 SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape (shape);
840 }
841 }
843 void
844 sp_shape_set_curve (SPShape *shape, SPCurve *curve, unsigned int owner)
845 {
846 if (shape->curve) {
847 shape->curve = sp_curve_unref (shape->curve);
848 }
849 if (curve) {
850 if (owner) {
851 shape->curve = sp_curve_ref (curve);
852 } else {
853 shape->curve = sp_curve_copy (curve);
854 }
855 }
856 SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
857 }
859 /* Return duplicate of curve or NULL */
860 SPCurve *
861 sp_shape_get_curve (SPShape *shape)
862 {
863 if (shape->curve) {
864 return sp_curve_copy (shape->curve);
865 }
866 return NULL;
867 }
869 /* NOT FOR GENERAL PUBLIC UNTIL SORTED OUT (Lauris) */
870 void
871 sp_shape_set_curve_insync (SPShape *shape, SPCurve *curve, unsigned int owner)
872 {
873 if (shape->curve) {
874 shape->curve = sp_curve_unref (shape->curve);
875 }
876 if (curve) {
877 if (owner) {
878 shape->curve = sp_curve_ref (curve);
879 } else {
880 shape->curve = sp_curve_copy (curve);
881 }
882 }
883 }
885 static void sp_shape_snappoints(SPItem const *item, SnapPointsIter p)
886 {
887 g_assert(item != NULL);
888 g_assert(SP_IS_SHAPE(item));
890 SPShape const *shape = SP_SHAPE(item);
891 if (shape->curve == NULL) {
892 return;
893 }
895 NR::Matrix const i2d (sp_item_i2d_affine (item));
897 /* Use the end points of each segment of the path */
898 NArtBpath const *bp = SP_CURVE_BPATH(shape->curve);
899 while (bp->code != NR_END) {
900 *p = bp->c(3) * i2d;
901 bp++;
902 }
903 }
906 /*
907 Local Variables:
908 mode:c++
909 c-file-style:"stroustrup"
910 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
911 indent-tabs-mode:nil
912 fill-column:99
913 End:
914 */
915 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :