1 #define __MARKER_C__
3 /*
4 * SVG <marker> implementation
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * Bryce Harrington <bryce@bryceharrington.org>
9 *
10 * Copyright (C) 1999-2003 Lauris Kaplinski
11 * 2004-2006 Bryce Harrington
12 * 2008 Johan Engelen
13 *
14 * Released under GNU GPL, read the file 'COPYING' for more information
15 */
17 #include <cstring>
18 #include <string>
19 #include "config.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"
26 #include "libnr/nr-translate-matrix-ops.h"
27 #include "libnr/nr-rotate-fns.h"
28 #include "libnr/nr-convert2geom.h"
29 #include <2geom/matrix.h>
30 #include "svg/svg.h"
31 #include "display/nr-arena-group.h"
32 #include "xml/repr.h"
33 #include "attributes.h"
34 #include "marker.h"
35 #include "document.h"
36 #include "document-private.h"
38 struct SPMarkerView {
39 SPMarkerView *next;
40 unsigned int key;
41 unsigned int size;
42 NRArenaItem *items[1];
43 };
45 static void sp_marker_class_init (SPMarkerClass *klass);
46 static void sp_marker_init (SPMarker *marker);
48 static void sp_marker_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
49 static void sp_marker_release (SPObject *object);
50 static void sp_marker_set (SPObject *object, unsigned int key, const gchar *value);
51 static void sp_marker_update (SPObject *object, SPCtx *ctx, guint flags);
52 static Inkscape::XML::Node *sp_marker_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
54 static NRArenaItem *sp_marker_private_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
55 static void sp_marker_private_hide (SPItem *item, unsigned int key);
56 static void sp_marker_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
57 static void sp_marker_print (SPItem *item, SPPrintContext *ctx);
59 static void sp_marker_view_remove (SPMarker *marker, SPMarkerView *view, unsigned int destroyitems);
61 static SPGroupClass *parent_class;
63 /**
64 * Registers the SPMarker class with Gdk and returns its type number.
65 */
66 GType
67 sp_marker_get_type (void)
68 {
69 static GType type = 0;
70 if (!type) {
71 GTypeInfo info = {
72 sizeof (SPMarkerClass),
73 NULL, NULL,
74 (GClassInitFunc) sp_marker_class_init,
75 NULL, NULL,
76 sizeof (SPMarker),
77 16,
78 (GInstanceInitFunc) sp_marker_init,
79 NULL, /* value_table */
80 };
81 type = g_type_register_static (SP_TYPE_GROUP, "SPMarker", &info, (GTypeFlags)0);
82 }
83 return type;
84 }
86 /**
87 * Initializes a SPMarkerClass object. Establishes the function pointers to the class'
88 * member routines in the class vtable, and sets pointers to parent classes.
89 */
90 static void
91 sp_marker_class_init (SPMarkerClass *klass)
92 {
93 GObjectClass *object_class;
94 SPObjectClass *sp_object_class;
95 SPItemClass *sp_item_class;
97 object_class = G_OBJECT_CLASS (klass);
98 sp_object_class = (SPObjectClass *) klass;
99 sp_item_class = (SPItemClass *) klass;
101 parent_class = (SPGroupClass *)g_type_class_ref (SP_TYPE_GROUP);
103 sp_object_class->build = sp_marker_build;
104 sp_object_class->release = sp_marker_release;
105 sp_object_class->set = sp_marker_set;
106 sp_object_class->update = sp_marker_update;
107 sp_object_class->write = sp_marker_write;
109 sp_item_class->show = sp_marker_private_show;
110 sp_item_class->hide = sp_marker_private_hide;
111 sp_item_class->bbox = sp_marker_bbox;
112 sp_item_class->print = sp_marker_print;
113 }
115 /**
116 * Initializes an SPMarker object. This notes the marker's viewBox is
117 * not set and initializes the marker's c2p identity matrix.
118 */
119 static void
120 sp_marker_init (SPMarker *marker)
121 {
122 marker->viewBox_set = FALSE;
124 marker->c2p.set_identity();
125 }
127 /**
128 * Virtual build callback for SPMarker.
129 *
130 * This is to be invoked immediately after creation of an SPMarker. This
131 * method fills an SPMarker object with its SVG attributes, and calls the
132 * parent class' build routine to attach the object to its document and
133 * repr. The result will be creation of the whole document tree.
134 *
135 * \see sp_object_build()
136 */
137 static void
138 sp_marker_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
139 {
140 SPGroup *group;
141 SPMarker *marker;
143 group = (SPGroup *) object;
144 marker = (SPMarker *) object;
146 sp_object_read_attr (object, "markerUnits");
147 sp_object_read_attr (object, "refX");
148 sp_object_read_attr (object, "refY");
149 sp_object_read_attr (object, "markerWidth");
150 sp_object_read_attr (object, "markerHeight");
151 sp_object_read_attr (object, "orient");
152 sp_object_read_attr (object, "viewBox");
153 sp_object_read_attr (object, "preserveAspectRatio");
155 if (((SPObjectClass *) parent_class)->build)
156 ((SPObjectClass *) parent_class)->build (object, document, repr);
157 }
159 /**
160 * Removes, releases and unrefs all children of object
161 *
162 * This is the inverse of sp_marker_build(). It must be invoked as soon
163 * as the marker is removed from the tree, even if it is still referenced
164 * by other objects. It hides and removes any views of the marker, then
165 * calls the parent classes' release function to deregister the object
166 * and release its SPRepr bindings. The result will be the destruction
167 * of the entire document tree.
168 *
169 * \see sp_object_release()
170 */
171 static void
172 sp_marker_release (SPObject *object)
173 {
174 SPMarker *marker;
176 marker = (SPMarker *) object;
178 while (marker->views) {
179 /* Destroy all NRArenaitems etc. */
180 /* Parent class ::hide method */
181 ((SPItemClass *) parent_class)->hide ((SPItem *) marker, marker->views->key);
182 sp_marker_view_remove (marker, marker->views, TRUE);
183 }
185 if (((SPObjectClass *) parent_class)->release)
186 ((SPObjectClass *) parent_class)->release (object);
187 }
189 /**
190 * Sets an attribute, 'key', of a marker object to 'value'. Supported
191 * attributes that can be set with this routine include:
192 *
193 * SP_ATTR_MARKERUNITS
194 * SP_ATTR_REFX
195 * SP_ATTR_REFY
196 * SP_ATTR_MARKERWIDTH
197 * SP_ATTR_MARKERHEIGHT
198 * SP_ATTR_ORIENT
199 * SP_ATTR_VIEWBOX
200 * SP_ATTR_PRESERVEASPECTRATIO
201 */
202 static void
203 sp_marker_set (SPObject *object, unsigned int key, const gchar *value)
204 {
205 SPItem *item;
206 SPMarker *marker;
208 item = SP_ITEM (object);
209 marker = SP_MARKER (object);
211 switch (key) {
212 case SP_ATTR_MARKERUNITS:
213 marker->markerUnits_set = FALSE;
214 marker->markerUnits = SP_MARKER_UNITS_STROKEWIDTH;
215 if (value) {
216 if (!strcmp (value, "strokeWidth")) {
217 marker->markerUnits_set = TRUE;
218 } else if (!strcmp (value, "userSpaceOnUse")) {
219 marker->markerUnits = SP_MARKER_UNITS_USERSPACEONUSE;
220 marker->markerUnits_set = TRUE;
221 }
222 }
223 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
224 break;
225 case SP_ATTR_REFX:
226 marker->refX.readOrUnset(value);
227 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
228 break;
229 case SP_ATTR_REFY:
230 marker->refY.readOrUnset(value);
231 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
232 break;
233 case SP_ATTR_MARKERWIDTH:
234 marker->markerWidth.readOrUnset(value, SVGLength::NONE, 3.0, 3.0);
235 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
236 break;
237 case SP_ATTR_MARKERHEIGHT:
238 marker->markerHeight.readOrUnset(value, SVGLength::NONE, 3.0, 3.0);
239 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
240 break;
241 case SP_ATTR_ORIENT:
242 marker->orient_set = FALSE;
243 marker->orient_auto = FALSE;
244 marker->orient = 0.0;
245 if (value) {
246 if (!strcmp (value, "auto")) {
247 marker->orient_auto = TRUE;
248 marker->orient_set = TRUE;
249 } else if (sp_svg_number_read_f (value, &marker->orient)) {
250 marker->orient_set = TRUE;
251 }
252 }
253 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
254 break;
255 case SP_ATTR_VIEWBOX:
256 marker->viewBox_set = FALSE;
257 if (value) {
258 double x, y, width, height;
259 char *eptr;
260 /* fixme: We have to take original item affine into account */
261 /* fixme: Think (Lauris) */
262 eptr = (gchar *) value;
263 x = g_ascii_strtod (eptr, &eptr);
264 while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
265 y = g_ascii_strtod (eptr, &eptr);
266 while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
267 width = g_ascii_strtod (eptr, &eptr);
268 while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
269 height = g_ascii_strtod (eptr, &eptr);
270 while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
271 if ((width > 0) && (height > 0)) {
272 /* Set viewbox */
273 marker->viewBox.x0 = x;
274 marker->viewBox.y0 = y;
275 marker->viewBox.x1 = x + width;
276 marker->viewBox.y1 = y + height;
277 marker->viewBox_set = TRUE;
278 }
279 }
280 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
281 break;
282 case SP_ATTR_PRESERVEASPECTRATIO:
283 /* Do setup before, so we can use break to escape */
284 marker->aspect_set = FALSE;
285 marker->aspect_align = SP_ASPECT_NONE;
286 marker->aspect_clip = SP_ASPECT_MEET;
287 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
288 if (value) {
289 int len;
290 gchar c[256];
291 const gchar *p, *e;
292 unsigned int align, clip;
293 p = value;
294 while (*p && *p == 32) p += 1;
295 if (!*p) break;
296 e = p;
297 while (*e && *e != 32) e += 1;
298 len = e - p;
299 if (len > 8) break;
300 memcpy (c, value, len);
301 c[len] = 0;
302 /* Now the actual part */
303 if (!strcmp (c, "none")) {
304 align = SP_ASPECT_NONE;
305 } else if (!strcmp (c, "xMinYMin")) {
306 align = SP_ASPECT_XMIN_YMIN;
307 } else if (!strcmp (c, "xMidYMin")) {
308 align = SP_ASPECT_XMID_YMIN;
309 } else if (!strcmp (c, "xMaxYMin")) {
310 align = SP_ASPECT_XMAX_YMIN;
311 } else if (!strcmp (c, "xMinYMid")) {
312 align = SP_ASPECT_XMIN_YMID;
313 } else if (!strcmp (c, "xMidYMid")) {
314 align = SP_ASPECT_XMID_YMID;
315 } else if (!strcmp (c, "xMaxYMin")) {
316 align = SP_ASPECT_XMAX_YMID;
317 } else if (!strcmp (c, "xMinYMax")) {
318 align = SP_ASPECT_XMIN_YMAX;
319 } else if (!strcmp (c, "xMidYMax")) {
320 align = SP_ASPECT_XMID_YMAX;
321 } else if (!strcmp (c, "xMaxYMax")) {
322 align = SP_ASPECT_XMAX_YMAX;
323 } else {
324 break;
325 }
326 clip = SP_ASPECT_MEET;
327 while (*e && *e == 32) e += 1;
328 if (e) {
329 if (!strcmp (e, "meet")) {
330 clip = SP_ASPECT_MEET;
331 } else if (!strcmp (e, "slice")) {
332 clip = SP_ASPECT_SLICE;
333 } else {
334 break;
335 }
336 }
337 marker->aspect_set = TRUE;
338 marker->aspect_align = align;
339 marker->aspect_clip = clip;
340 }
341 break;
342 default:
343 if (((SPObjectClass *) parent_class)->set)
344 ((SPObjectClass *) parent_class)->set (object, key, value);
345 break;
346 }
347 }
349 /**
350 * Updates <marker> when its attributes have changed. Takes care of setting up
351 * transformations and viewBoxes.
352 */
353 static void
354 sp_marker_update (SPObject *object, SPCtx *ctx, guint flags)
355 {
356 SPItem *item;
357 SPMarker *marker;
358 SPItemCtx rctx;
359 NRRect *vb;
360 double x, y, width, height;
361 SPMarkerView *v;
363 item = SP_ITEM (object);
364 marker = SP_MARKER (object);
366 /* fixme: We have to set up clip here too */
368 /* Copy parent context */
369 rctx.ctx = *ctx;
370 /* Initialize tranformations */
371 rctx.i2doc = NR::identity();
372 rctx.i2vp = NR::identity();
373 /* Set up viewport */
374 rctx.vp.x0 = 0.0;
375 rctx.vp.y0 = 0.0;
376 rctx.vp.x1 = marker->markerWidth.computed;
377 rctx.vp.y1 = marker->markerHeight.computed;
379 /* Start with identity transform */
380 marker->c2p.set_identity();
382 /* Viewbox is always present, either implicitly or explicitly */
383 if (marker->viewBox_set) {
384 vb = &marker->viewBox;
385 } else {
386 vb = &rctx.vp;
387 }
388 /* Now set up viewbox transformation */
389 /* Determine actual viewbox in viewport coordinates */
390 if (marker->aspect_align == SP_ASPECT_NONE) {
391 x = 0.0;
392 y = 0.0;
393 width = rctx.vp.x1 - rctx.vp.x0;
394 height = rctx.vp.y1 - rctx.vp.y0;
395 } else {
396 double scalex, scaley, scale;
397 /* Things are getting interesting */
398 scalex = (rctx.vp.x1 - rctx.vp.x0) / (vb->x1 - vb->x0);
399 scaley = (rctx.vp.y1 - rctx.vp.y0) / (vb->y1 - vb->y0);
400 scale = (marker->aspect_clip == SP_ASPECT_MEET) ? MIN (scalex, scaley) : MAX (scalex, scaley);
401 width = (vb->x1 - vb->x0) * scale;
402 height = (vb->y1 - vb->y0) * scale;
403 /* Now place viewbox to requested position */
404 switch (marker->aspect_align) {
405 case SP_ASPECT_XMIN_YMIN:
406 x = 0.0;
407 y = 0.0;
408 break;
409 case SP_ASPECT_XMID_YMIN:
410 x = 0.5 * ((rctx.vp.x1 - rctx.vp.x0) - width);
411 y = 0.0;
412 break;
413 case SP_ASPECT_XMAX_YMIN:
414 x = 1.0 * ((rctx.vp.x1 - rctx.vp.x0) - width);
415 y = 0.0;
416 break;
417 case SP_ASPECT_XMIN_YMID:
418 x = 0.0;
419 y = 0.5 * ((rctx.vp.y1 - rctx.vp.y0) - height);
420 break;
421 case SP_ASPECT_XMID_YMID:
422 x = 0.5 * ((rctx.vp.x1 - rctx.vp.x0) - width);
423 y = 0.5 * ((rctx.vp.y1 - rctx.vp.y0) - height);
424 break;
425 case SP_ASPECT_XMAX_YMID:
426 x = 1.0 * ((rctx.vp.x1 - rctx.vp.x0) - width);
427 y = 0.5 * ((rctx.vp.y1 - rctx.vp.y0) - height);
428 break;
429 case SP_ASPECT_XMIN_YMAX:
430 x = 0.0;
431 y = 1.0 * ((rctx.vp.y1 - rctx.vp.y0) - height);
432 break;
433 case SP_ASPECT_XMID_YMAX:
434 x = 0.5 * ((rctx.vp.x1 - rctx.vp.x0) - width);
435 y = 1.0 * ((rctx.vp.y1 - rctx.vp.y0) - height);
436 break;
437 case SP_ASPECT_XMAX_YMAX:
438 x = 1.0 * ((rctx.vp.x1 - rctx.vp.x0) - width);
439 y = 1.0 * ((rctx.vp.y1 - rctx.vp.y0) - height);
440 break;
441 default:
442 x = 0.0;
443 y = 0.0;
444 break;
445 }
446 }
448 {
449 NR::Matrix q;
450 /* Compose additional transformation from scale and position */
451 q[0] = width / (vb->x1 - vb->x0);
452 q[1] = 0.0;
453 q[2] = 0.0;
454 q[3] = height / (vb->y1 - vb->y0);
455 q[4] = -vb->x0 * q[0] + x;
456 q[5] = -vb->y0 * q[3] + y;
457 /* Append viewbox transformation */
458 marker->c2p = q * marker->c2p;
459 }
461 /* Append reference translation */
462 /* fixme: lala (Lauris) */
463 marker->c2p = NR::translate(-marker->refX.computed, -marker->refY.computed) * marker->c2p;
465 rctx.i2doc = marker->c2p * rctx.i2doc;
467 /* If viewBox is set reinitialize child viewport */
468 /* Otherwise it already correct */
469 if (marker->viewBox_set) {
470 rctx.vp.x0 = marker->viewBox.x0;
471 rctx.vp.y0 = marker->viewBox.y0;
472 rctx.vp.x1 = marker->viewBox.x1;
473 rctx.vp.y1 = marker->viewBox.y1;
474 rctx.i2vp = NR::identity();
475 }
477 /* And invoke parent method */
478 if (((SPObjectClass *) (parent_class))->update)
479 ((SPObjectClass *) (parent_class))->update (object, (SPCtx *) &rctx, flags);
481 /* As last step set additional transform of arena group */
482 for (v = marker->views; v != NULL; v = v->next) {
483 for (unsigned i = 0 ; i < v->size ; i++) {
484 if (v->items[i]) {
485 nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->items[i]), &marker->c2p);
486 }
487 }
488 }
489 }
491 /**
492 * Writes the object's properties into its repr object.
493 */
494 static Inkscape::XML::Node *
495 sp_marker_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
496 {
497 SPMarker *marker;
499 marker = SP_MARKER (object);
501 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
502 repr = xml_doc->createElement("svg:marker");
503 }
505 if (marker->markerUnits_set) {
506 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
507 repr->setAttribute("markerUnits", "strokeWidth");
508 } else {
509 repr->setAttribute("markerUnits", "userSpaceOnUse");
510 }
511 } else {
512 repr->setAttribute("markerUnits", NULL);
513 }
514 if (marker->refX._set) {
515 sp_repr_set_svg_double(repr, "refX", marker->refX.computed);
516 } else {
517 repr->setAttribute("refX", NULL);
518 }
519 if (marker->refY._set) {
520 sp_repr_set_svg_double (repr, "refY", marker->refY.computed);
521 } else {
522 repr->setAttribute("refY", NULL);
523 }
524 if (marker->markerWidth._set) {
525 sp_repr_set_svg_double (repr, "markerWidth", marker->markerWidth.computed);
526 } else {
527 repr->setAttribute("markerWidth", NULL);
528 }
529 if (marker->markerHeight._set) {
530 sp_repr_set_svg_double (repr, "markerHeight", marker->markerHeight.computed);
531 } else {
532 repr->setAttribute("markerHeight", NULL);
533 }
534 if (marker->orient_set) {
535 if (marker->orient_auto) {
536 repr->setAttribute("orient", "auto");
537 } else {
538 sp_repr_set_css_double(repr, "orient", marker->orient);
539 }
540 } else {
541 repr->setAttribute("orient", NULL);
542 }
543 /* fixme: */
544 repr->setAttribute("viewBox", object->repr->attribute("viewBox"));
545 repr->setAttribute("preserveAspectRatio", object->repr->attribute("preserveAspectRatio"));
547 if (((SPObjectClass *) (parent_class))->write)
548 ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
550 return repr;
551 }
553 /**
554 * This routine is disabled to break propagation.
555 */
556 static NRArenaItem *
557 sp_marker_private_show (SPItem */*item*/, NRArena */*arena*/, unsigned int /*key*/, unsigned int /*flags*/)
558 {
559 /* Break propagation */
560 return NULL;
561 }
563 /**
564 * This routine is disabled to break propagation.
565 */
566 static void
567 sp_marker_private_hide (SPItem */*item*/, unsigned int /*key*/)
568 {
569 /* Break propagation */
570 }
572 /**
573 * This routine is disabled to break propagation.
574 */
575 static void
576 sp_marker_bbox(SPItem const *, NRRect *, NR::Matrix const &, unsigned const)
577 {
578 /* Break propagation */
579 }
581 /**
582 * This routine is disabled to break propagation.
583 */
584 static void
585 sp_marker_print (SPItem */*item*/, SPPrintContext */*ctx*/)
586 {
587 /* Break propagation */
588 }
590 /* fixme: Remove link if zero-sized (Lauris) */
592 /**
593 * Removes any SPMarkerViews that a marker has with a specific key.
594 * Set up the NRArenaItem array's size in the specified SPMarker's SPMarkerView.
595 * This is called from sp_shape_update() for shapes that have markers. It
596 * removes the old view of the marker and establishes a new one, registering
597 * it with the marker's list of views for future updates.
598 *
599 * \param marker Marker to create views in.
600 * \param key Key to give each SPMarkerView.
601 * \param size Number of NRArenaItems to put in the SPMarkerView.
602 */
603 void
604 sp_marker_show_dimension (SPMarker *marker, unsigned int key, unsigned int size)
605 {
606 SPMarkerView *view;
607 unsigned int i;
609 for (view = marker->views; view != NULL; view = view->next) {
610 if (view->key == key) break;
611 }
612 if (view && (view->size != size)) {
613 /* Free old view and allocate new */
614 /* Parent class ::hide method */
615 ((SPItemClass *) parent_class)->hide ((SPItem *) marker, key);
616 sp_marker_view_remove (marker, view, TRUE);
617 view = NULL;
618 }
619 if (!view) {
620 view = (SPMarkerView *)g_malloc (sizeof (SPMarkerView) + (size) * sizeof (NRArenaItem *));
621 for (i = 0; i < size; i++) view->items[i] = NULL;
622 view->next = marker->views;
623 marker->views = view;
624 view->key = key;
625 view->size = size;
626 }
627 }
629 /**
630 * Shows an instance of a marker. This is called during sp_shape_update_marker_view()
631 * show and transform a child item in the arena for all views with the given key.
632 */
633 NRArenaItem *
634 sp_marker_show_instance ( SPMarker *marker, NRArenaItem *parent,
635 unsigned int key, unsigned int pos,
636 Geom::Matrix const &base, float linewidth)
637 {
638 for (SPMarkerView *v = marker->views; v != NULL; v = v->next) {
639 if (v->key == key) {
640 if (pos >= v->size) {
641 return NULL;
642 }
643 if (!v->items[pos]) {
644 /* Parent class ::show method */
645 v->items[pos] = ((SPItemClass *) parent_class)->show ((SPItem *) marker,
646 parent->arena, key,
647 SP_ITEM_REFERENCE_FLAGS);
648 if (v->items[pos]) {
649 /* fixme: Position (Lauris) */
650 nr_arena_item_add_child (parent, v->items[pos], NULL);
651 /* nr_arena_item_unref (v->items[pos]); */
652 nr_arena_group_set_child_transform((NRArenaGroup *) v->items[pos], &marker->c2p);
653 }
654 }
655 if (v->items[pos]) {
656 NR::Matrix m;
657 if (marker->orient_auto) {
658 m = from_2geom(base);
659 } else {
660 /* fixme: Orient units (Lauris) */
661 m = NR::Matrix(rotate_degrees(marker->orient));
662 m *= get_translation(from_2geom(base));
663 }
664 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
665 m = NR::scale(linewidth) * m;
666 }
668 nr_arena_item_set_transform(v->items[pos], m);
669 }
670 return v->items[pos];
671 }
672 }
674 return NULL;
675 }
677 /**
678 * Hides/removes all views of the given marker that have key 'key'.
679 * This replaces SPItem implementation because we have our own views
680 * \param key SPMarkerView key to hide.
681 */
682 void
683 sp_marker_hide (SPMarker *marker, unsigned int key)
684 {
685 SPMarkerView *v;
687 v = marker->views;
688 while (v != NULL) {
689 SPMarkerView *next;
690 next = v->next;
691 if (v->key == key) {
692 /* Parent class ::hide method */
693 ((SPItemClass *) parent_class)->hide ((SPItem *) marker, key);
694 sp_marker_view_remove (marker, v, TRUE);
695 return;
696 }
697 v = next;
698 }
699 }
701 /**
702 * Removes a given view. Also will destroy sub-items in the view if destroyitems
703 * is set to a non-zero value.
704 */
705 static void
706 sp_marker_view_remove (SPMarker *marker, SPMarkerView *view, unsigned int destroyitems)
707 {
708 unsigned int i;
709 if (view == marker->views) {
710 marker->views = view->next;
711 } else {
712 SPMarkerView *v;
713 for (v = marker->views; v->next != view; v = v->next) if (!v->next) return;
714 v->next = view->next;
715 }
716 if (destroyitems) {
717 for (i = 0; i < view->size; i++) {
718 /* We have to walk through the whole array because there may be hidden items */
719 if (view->items[i]) nr_arena_item_unref (view->items[i]);
720 }
721 }
722 g_free (view);
723 }
725 const gchar *
726 generate_marker (GSList *reprs, NR::Rect bounds, SPDocument *document, NR::Matrix /*transform*/, NR::Matrix move)
727 {
728 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
729 Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
731 Inkscape::XML::Node *repr = xml_doc->createElement("svg:marker");
733 // Uncommenting this will make the marker fixed-size independent of stroke width.
734 // Commented out for consistency with standard markers which scale when you change
735 // stroke width:
736 //repr->setAttribute("markerUnits", "userSpaceOnUse");
738 sp_repr_set_svg_double(repr, "markerWidth", bounds.extent(NR::X));
739 sp_repr_set_svg_double(repr, "markerHeight", bounds.extent(NR::Y));
741 repr->setAttribute("orient", "auto");
743 defsrepr->appendChild(repr);
744 const gchar *mark_id = repr->attribute("id");
745 SPObject *mark_object = document->getObjectById(mark_id);
747 for (GSList *i = reprs; i != NULL; i = i->next) {
748 Inkscape::XML::Node *node = (Inkscape::XML::Node *)(i->data);
749 SPItem *copy = SP_ITEM(mark_object->appendChildRepr(node));
751 NR::Matrix dup_transform;
752 if (!sp_svg_transform_read (node->attribute("transform"), &dup_transform))
753 dup_transform = NR::identity();
754 dup_transform *= move;
756 sp_item_write_transform(copy, SP_OBJECT_REPR(copy), dup_transform);
757 }
759 Inkscape::GC::release(repr);
760 return mark_id;
761 }