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 if ( fabs(cbbox.x1-cbbox.x0) > -0.00001 && fabs(cbbox.y1-cbbox.y0) > -0.00001 ) {
582 NRRect tbbox=*bbox;
583 nr_rect_d_union (bbox, &cbbox, &tbbox);
584 }
585 }
586 }
588 void
589 sp_shape_print (SPItem *item, SPPrintContext *ctx)
590 {
591 NRRect pbox, dbox, bbox;
593 SPShape *shape = SP_SHAPE(item);
595 if (!shape->curve) return;
597 gint add_comments = prefs_get_int_attribute_limited ("printing.debug", "add-label-comments", 0, 0, 1);
598 if (add_comments) {
599 gchar * comment = g_strdup_printf("begin '%s'",
600 SP_OBJECT(item)->defaultLabel());
601 sp_print_comment(ctx, comment);
602 g_free(comment);
603 }
605 /* fixme: Think (Lauris) */
606 sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
607 dbox.x0 = 0.0;
608 dbox.y0 = 0.0;
609 dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
610 dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
611 sp_item_bbox_desktop (item, &bbox);
612 NR::Matrix const i2d = sp_item_i2d_affine(item);
614 SPStyle* style = SP_OBJECT_STYLE (item);
616 if (style->fill.type != SP_PAINT_TYPE_NONE) {
617 NRBPath bp;
618 bp.path = shape->curve->bpath;
619 sp_print_fill (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
620 }
622 if (style->stroke.type != SP_PAINT_TYPE_NONE) {
623 NRBPath bp;
624 bp.path = shape->curve->bpath;
625 sp_print_stroke (ctx, &bp, i2d, style, &pbox, &dbox, &bbox);
626 }
628 for (NArtBpath* bp = shape->curve->bpath; bp->code != NR_END; bp++) {
629 for (int m = SP_MARKER_LOC_START; m < SP_MARKER_LOC_QTY; m++) {
630 if (sp_shape_marker_required (shape, m, bp)) {
632 SPMarker* marker = SP_MARKER (shape->marker[m]);
633 SPItem* marker_item = sp_item_first_item_child (SP_OBJECT (shape->marker[m]));
635 NR::Matrix tr(sp_shape_marker_get_transform(shape, bp));
637 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
638 tr = NR::scale(style->stroke_width.computed) * tr;
639 }
641 tr = marker_item->transform * marker->c2p * tr;
643 NR::Matrix old_tr = marker_item->transform;
644 marker_item->transform = tr;
645 sp_item_invoke_print (marker_item, ctx);
646 marker_item->transform = old_tr;
647 }
648 }
649 }
651 if (add_comments) {
652 gchar * comment = g_strdup_printf("end '%s'",
653 SP_OBJECT(item)->defaultLabel());
654 sp_print_comment(ctx, comment);
655 g_free(comment);
656 }
657 }
659 static NRArenaItem *
660 sp_shape_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
661 {
662 SPObject *object = SP_OBJECT(item);
663 SPShape *shape = SP_SHAPE(item);
665 NRArenaItem *arenaitem = NRArenaShape::create(arena);
666 NRArenaShape * const s = NR_ARENA_SHAPE(arenaitem);
667 nr_arena_shape_set_style(s, object->style);
668 nr_arena_shape_set_path(s, shape->curve, false);
669 NR::Rect const paintbox = item->invokeBbox(NR::identity());
670 s->setPaintBox(paintbox);
672 if (sp_shape_has_markers (shape)) {
674 /* Dimension marker views */
675 if (!arenaitem->key) {
676 NR_ARENA_ITEM_SET_KEY (arenaitem, sp_item_display_key_new (SP_MARKER_LOC_QTY));
677 }
679 for (int i = 0; i < SP_MARKER_LOC_QTY; i++) {
680 if (shape->marker[i]) {
681 sp_marker_show_dimension ((SPMarker *) shape->marker[i],
682 NR_ARENA_ITEM_GET_KEY (arenaitem) + i - SP_MARKER_LOC,
683 sp_shape_number_of_markers (shape, i));
684 }
685 }
688 /* Update marker views */
689 sp_shape_update_marker_view (shape, arenaitem);
690 }
692 return arenaitem;
693 }
695 static void
696 sp_shape_hide (SPItem *item, unsigned int key)
697 {
698 SPShape *shape;
699 SPItemView *v;
700 int i;
702 shape = (SPShape *) item;
704 for (i=0; i<SP_MARKER_LOC_QTY; i++) {
705 if (shape->marker[i]) {
706 for (v = item->display; v != NULL; v = v->next) {
707 if (key == v->key) {
708 sp_marker_hide ((SPMarker *) shape->marker[i],
709 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
710 }
711 }
712 }
713 }
715 if (((SPItemClass *) parent_class)->hide) {
716 ((SPItemClass *) parent_class)->hide (item, key);
717 }
718 }
720 /* Marker stuff */
722 /**
723 * \param shape Shape.
724 * \return TRUE if the shape has any markers, or FALSE if not.
725 */
726 static int
727 sp_shape_has_markers (SPShape const *shape)
728 {
729 /* Note, we're ignoring 'marker' settings, which technically should apply for
730 all three settings. This should be fixed later such that if 'marker' is
731 specified, then all three should appear. */
733 return (
734 shape->curve &&
735 (shape->marker[SP_MARKER_LOC_START] ||
736 shape->marker[SP_MARKER_LOC_MID] ||
737 shape->marker[SP_MARKER_LOC_END])
738 );
739 }
742 /**
743 * \param shape Shape.
744 * \param type Marker type (e.g. SP_MARKER_LOC_START)
745 * \return Number of markers that the shape has of this type.
746 */
747 static int
748 sp_shape_number_of_markers (SPShape *shape, int type)
749 {
750 int n = 0;
751 for (NArtBpath* bp = shape->curve->bpath; bp->code != NR_END; bp++) {
752 if (sp_shape_marker_required (shape, type, bp)) {
753 n++;
754 }
755 }
757 return n;
758 }
760 static void
761 sp_shape_marker_release (SPObject *marker, SPShape *shape)
762 {
763 SPItem *item;
764 int i;
766 item = (SPItem *) shape;
768 marker_status("sp_shape_marker_release: Releasing markers");
769 for (i = SP_MARKER_LOC_START; i < SP_MARKER_LOC_QTY; i++) {
770 if (marker == shape->marker[i]) {
771 SPItemView *v;
772 /* Hide marker */
773 for (v = item->display; v != NULL; v = v->next) {
774 sp_marker_hide ((SPMarker *) (shape->marker[i]), NR_ARENA_ITEM_GET_KEY (v->arenaitem) + i);
775 /* fixme: Do we need explicit remove here? (Lauris) */
776 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
777 }
778 /* Detach marker */
779 sp_signal_disconnect_by_data (shape->marker[i], item);
780 shape->marker[i] = sp_object_hunref (shape->marker[i], item);
781 }
782 }
783 }
785 static void
786 sp_shape_marker_modified (SPObject *marker, guint flags, SPItem *item)
787 {
788 /* I think mask does update automagically */
789 /* g_warning ("Item %s mask %s modified", SP_OBJECT_ID (item), SP_OBJECT_ID (mask)); */
790 }
792 void
793 sp_shape_set_marker (SPObject *object, unsigned int key, const gchar *value)
794 {
795 SPItem *item = (SPItem *) object;
796 SPShape *shape = (SPShape *) object;
798 if (key < SP_MARKER_LOC_START || key > SP_MARKER_LOC_END) {
799 return;
800 }
802 SPObject *mrk = sp_uri_reference_resolve (SP_OBJECT_DOCUMENT (object), value);
803 if (mrk != shape->marker[key]) {
804 if (shape->marker[key]) {
805 SPItemView *v;
807 /* Detach marker */
808 g_signal_handler_disconnect (shape->marker[key], shape->release_connect[key]);
809 g_signal_handler_disconnect (shape->marker[key], shape->modified_connect[key]);
811 /* Hide marker */
812 for (v = item->display; v != NULL; v = v->next) {
813 sp_marker_hide ((SPMarker *) (shape->marker[key]),
814 NR_ARENA_ITEM_GET_KEY (v->arenaitem) + key);
815 /* fixme: Do we need explicit remove here? (Lauris) */
816 /* nr_arena_item_set_mask (v->arenaitem, NULL); */
817 }
819 /* Unref marker */
820 shape->marker[key] = sp_object_hunref (shape->marker[key], object);
821 }
822 if (SP_IS_MARKER (mrk)) {
823 shape->marker[key] = sp_object_href (mrk, object);
824 shape->release_connect[key] = g_signal_connect (G_OBJECT (shape->marker[key]), "release",
825 G_CALLBACK (sp_shape_marker_release), shape);
826 shape->modified_connect[key] = g_signal_connect (G_OBJECT (shape->marker[key]), "modified",
827 G_CALLBACK (sp_shape_marker_modified), shape);
828 }
829 }
830 }
834 /* Shape section */
836 void
837 sp_shape_set_shape (SPShape *shape)
838 {
839 g_return_if_fail (shape != NULL);
840 g_return_if_fail (SP_IS_SHAPE (shape));
842 if (SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape) {
843 SP_SHAPE_CLASS (G_OBJECT_GET_CLASS (shape))->set_shape (shape);
844 }
845 }
847 void
848 sp_shape_set_curve (SPShape *shape, SPCurve *curve, unsigned int owner)
849 {
850 if (shape->curve) {
851 shape->curve = sp_curve_unref (shape->curve);
852 }
853 if (curve) {
854 if (owner) {
855 shape->curve = sp_curve_ref (curve);
856 } else {
857 shape->curve = sp_curve_copy (curve);
858 }
859 }
860 SP_OBJECT(shape)->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
861 }
863 /* Return duplicate of curve or NULL */
864 SPCurve *
865 sp_shape_get_curve (SPShape *shape)
866 {
867 if (shape->curve) {
868 return sp_curve_copy (shape->curve);
869 }
870 return NULL;
871 }
873 /* NOT FOR GENERAL PUBLIC UNTIL SORTED OUT (Lauris) */
874 void
875 sp_shape_set_curve_insync (SPShape *shape, SPCurve *curve, unsigned int owner)
876 {
877 if (shape->curve) {
878 shape->curve = sp_curve_unref (shape->curve);
879 }
880 if (curve) {
881 if (owner) {
882 shape->curve = sp_curve_ref (curve);
883 } else {
884 shape->curve = sp_curve_copy (curve);
885 }
886 }
887 }
889 static void sp_shape_snappoints(SPItem const *item, SnapPointsIter p)
890 {
891 g_assert(item != NULL);
892 g_assert(SP_IS_SHAPE(item));
894 SPShape const *shape = SP_SHAPE(item);
895 if (shape->curve == NULL) {
896 return;
897 }
899 NR::Matrix const i2d (sp_item_i2d_affine (item));
901 /* Use the end points of each segment of the path */
902 NArtBpath const *bp = shape->curve->bpath;
903 while (bp->code != NR_END) {
904 *p = bp->c(3) * i2d;
905 bp++;
906 }
907 }
910 /*
911 Local Variables:
912 mode:c++
913 c-file-style:"stroustrup"
914 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
915 indent-tabs-mode:nil
916 fill-column:99
917 End:
918 */
919 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :