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