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 // viewbox transformation and reference translation
444 marker->c2p = Geom::Translate(-marker->refX.computed, -marker->refY.computed) *
445 Geom::Scale(width / vb.width(), height / vb.height());
447 rctx.i2doc = marker->c2p * rctx.i2doc;
449 /* If viewBox is set reinitialize child viewport */
450 /* Otherwise it already correct */
451 if (marker->viewBox) {
452 rctx.vp.x0 = marker->viewBox->min()[Geom::X];
453 rctx.vp.y0 = marker->viewBox->min()[Geom::Y];
454 rctx.vp.x1 = marker->viewBox->max()[Geom::X];
455 rctx.vp.y1 = marker->viewBox->max()[Geom::Y];
456 rctx.i2vp = Geom::identity();
457 }
459 /* And invoke parent method */
460 if (((SPObjectClass *) (parent_class))->update)
461 ((SPObjectClass *) (parent_class))->update (object, (SPCtx *) &rctx, flags);
463 /* As last step set additional transform of arena group */
464 for (v = marker->views; v != NULL; v = v->next) {
465 for (unsigned i = 0 ; i < v->items.size() ; i++) {
466 if (v->items[i]) {
467 Geom::Matrix tmp = marker->c2p;
468 nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->items[i]), &tmp);
469 }
470 }
471 }
472 }
474 /**
475 * Writes the object's properties into its repr object.
476 */
477 static Inkscape::XML::Node *
478 sp_marker_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
479 {
480 SPMarker *marker;
482 marker = SP_MARKER (object);
484 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
485 repr = xml_doc->createElement("svg:marker");
486 }
488 if (marker->markerUnits_set) {
489 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
490 repr->setAttribute("markerUnits", "strokeWidth");
491 } else {
492 repr->setAttribute("markerUnits", "userSpaceOnUse");
493 }
494 } else {
495 repr->setAttribute("markerUnits", NULL);
496 }
497 if (marker->refX._set) {
498 sp_repr_set_svg_double(repr, "refX", marker->refX.computed);
499 } else {
500 repr->setAttribute("refX", NULL);
501 }
502 if (marker->refY._set) {
503 sp_repr_set_svg_double (repr, "refY", marker->refY.computed);
504 } else {
505 repr->setAttribute("refY", NULL);
506 }
507 if (marker->markerWidth._set) {
508 sp_repr_set_svg_double (repr, "markerWidth", marker->markerWidth.computed);
509 } else {
510 repr->setAttribute("markerWidth", NULL);
511 }
512 if (marker->markerHeight._set) {
513 sp_repr_set_svg_double (repr, "markerHeight", marker->markerHeight.computed);
514 } else {
515 repr->setAttribute("markerHeight", NULL);
516 }
517 if (marker->orient_set) {
518 if (marker->orient_auto) {
519 repr->setAttribute("orient", "auto");
520 } else {
521 sp_repr_set_css_double(repr, "orient", marker->orient);
522 }
523 } else {
524 repr->setAttribute("orient", NULL);
525 }
526 /* fixme: */
527 repr->setAttribute("viewBox", object->repr->attribute("viewBox"));
528 repr->setAttribute("preserveAspectRatio", object->repr->attribute("preserveAspectRatio"));
530 if (((SPObjectClass *) (parent_class))->write)
531 ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
533 return repr;
534 }
536 /**
537 * This routine is disabled to break propagation.
538 */
539 static NRArenaItem *
540 sp_marker_private_show (SPItem */*item*/, NRArena */*arena*/, unsigned int /*key*/, unsigned int /*flags*/)
541 {
542 /* Break propagation */
543 return NULL;
544 }
546 /**
547 * This routine is disabled to break propagation.
548 */
549 static void
550 sp_marker_private_hide (SPItem */*item*/, unsigned int /*key*/)
551 {
552 /* Break propagation */
553 }
555 /**
556 * This routine is disabled to break propagation.
557 */
558 static void
559 sp_marker_bbox(SPItem const *, NRRect *, Geom::Matrix const &, unsigned const)
560 {
561 /* Break propagation */
562 }
564 /**
565 * This routine is disabled to break propagation.
566 */
567 static void
568 sp_marker_print (SPItem */*item*/, SPPrintContext */*ctx*/)
569 {
570 /* Break propagation */
571 }
573 /* fixme: Remove link if zero-sized (Lauris) */
575 /**
576 * Removes any SPMarkerViews that a marker has with a specific key.
577 * Set up the NRArenaItem array's size in the specified SPMarker's SPMarkerView.
578 * This is called from sp_shape_update() for shapes that have markers. It
579 * removes the old view of the marker and establishes a new one, registering
580 * it with the marker's list of views for future updates.
581 *
582 * \param marker Marker to create views in.
583 * \param key Key to give each SPMarkerView.
584 * \param size Number of NRArenaItems to put in the SPMarkerView.
585 */
586 void
587 sp_marker_show_dimension (SPMarker *marker, unsigned int key, unsigned int size)
588 {
589 SPMarkerView *view;
590 unsigned int i;
592 for (view = marker->views; view != NULL; view = view->next) {
593 if (view->key == key) break;
594 }
595 if (view && (view->items.size() != size)) {
596 /* Free old view and allocate new */
597 /* Parent class ::hide method */
598 ((SPItemClass *) parent_class)->hide ((SPItem *) marker, key);
599 sp_marker_view_remove (marker, view, TRUE);
600 view = NULL;
601 }
602 if (!view) {
603 view = new SPMarkerView();
604 view->items.clear();
605 for (i = 0; i < size; i++) {
606 view->items.push_back(NULL);
607 }
608 view->next = marker->views;
609 marker->views = view;
610 view->key = key;
611 }
612 }
614 /**
615 * Shows an instance of a marker. This is called during sp_shape_update_marker_view()
616 * show and transform a child item in the arena for all views with the given key.
617 */
618 NRArenaItem *
619 sp_marker_show_instance ( SPMarker *marker, NRArenaItem *parent,
620 unsigned int key, unsigned int pos,
621 Geom::Matrix const &base, float linewidth)
622 {
623 for (SPMarkerView *v = marker->views; v != NULL; v = v->next) {
624 if (v->key == key) {
625 if (pos >= v->items.size()) {
626 return NULL;
627 }
628 if (!v->items[pos]) {
629 /* Parent class ::show method */
630 v->items[pos] = ((SPItemClass *) parent_class)->show ((SPItem *) marker,
631 parent->arena, key,
632 SP_ITEM_REFERENCE_FLAGS);
633 if (v->items[pos]) {
634 /* fixme: Position (Lauris) */
635 nr_arena_item_add_child (parent, v->items[pos], NULL);
636 /* nr_arena_item_unref (v->items[pos]); */
637 Geom::Matrix tmp = marker->c2p;
638 nr_arena_group_set_child_transform((NRArenaGroup *) v->items[pos], &tmp);
639 }
640 }
641 if (v->items[pos]) {
642 Geom::Matrix m;
643 if (marker->orient_auto) {
644 m = base;
645 } else {
646 /* fixme: Orient units (Lauris) */
647 m = Geom::Rotate::from_degrees(marker->orient);
648 m *= Geom::Translate(base.translation());
649 }
650 if (marker->markerUnits == SP_MARKER_UNITS_STROKEWIDTH) {
651 m = Geom::Scale(linewidth) * m;
652 }
654 nr_arena_item_set_transform(v->items[pos], m);
655 }
656 return v->items[pos];
657 }
658 }
660 return NULL;
661 }
663 /**
664 * Hides/removes all views of the given marker that have key 'key'.
665 * This replaces SPItem implementation because we have our own views
666 * \param key SPMarkerView key to hide.
667 */
668 void
669 sp_marker_hide (SPMarker *marker, unsigned int key)
670 {
671 SPMarkerView *v;
673 v = marker->views;
674 while (v != NULL) {
675 SPMarkerView *next;
676 next = v->next;
677 if (v->key == key) {
678 /* Parent class ::hide method */
679 ((SPItemClass *) parent_class)->hide ((SPItem *) marker, key);
680 sp_marker_view_remove (marker, v, TRUE);
681 return;
682 }
683 v = next;
684 }
685 }
687 /**
688 * Removes a given view. Also will destroy sub-items in the view if destroyitems
689 * is set to a non-zero value.
690 */
691 static void
692 sp_marker_view_remove (SPMarker *marker, SPMarkerView *view, unsigned int destroyitems)
693 {
694 unsigned int i;
695 if (view == marker->views) {
696 marker->views = view->next;
697 } else {
698 SPMarkerView *v;
699 for (v = marker->views; v->next != view; v = v->next) if (!v->next) return;
700 v->next = view->next;
701 }
702 if (destroyitems) {
703 for (i = 0; i < view->items.size(); i++) {
704 /* We have to walk through the whole array because there may be hidden items */
705 if (view->items[i]) nr_arena_item_unref (view->items[i]);
706 }
707 }
708 view->items.clear();
709 delete view;
710 }
712 const gchar *
713 generate_marker (GSList *reprs, Geom::Rect bounds, SPDocument *document, Geom::Matrix /*transform*/, Geom::Matrix move)
714 {
715 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
716 Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
718 Inkscape::XML::Node *repr = xml_doc->createElement("svg:marker");
720 // Uncommenting this will make the marker fixed-size independent of stroke width.
721 // Commented out for consistency with standard markers which scale when you change
722 // stroke width:
723 //repr->setAttribute("markerUnits", "userSpaceOnUse");
725 sp_repr_set_svg_double(repr, "markerWidth", bounds.dimensions()[Geom::X]);
726 sp_repr_set_svg_double(repr, "markerHeight", bounds.dimensions()[Geom::Y]);
728 repr->setAttribute("orient", "auto");
730 defsrepr->appendChild(repr);
731 const gchar *mark_id = repr->attribute("id");
732 SPObject *mark_object = document->getObjectById(mark_id);
734 for (GSList *i = reprs; i != NULL; i = i->next) {
735 Inkscape::XML::Node *node = (Inkscape::XML::Node *)(i->data);
736 SPItem *copy = SP_ITEM(mark_object->appendChildRepr(node));
738 Geom::Matrix dup_transform;
739 if (!sp_svg_transform_read (node->attribute("transform"), &dup_transform))
740 dup_transform = Geom::identity();
741 dup_transform *= move;
743 sp_item_write_transform(copy, SP_OBJECT_REPR(copy), dup_transform);
744 }
746 Inkscape::GC::release(repr);
747 return mark_id;
748 }
750 /*
751 Local Variables:
752 mode:c++
753 c-file-style:"stroustrup"
754 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
755 indent-tabs-mode:nil
756 fill-column:99
757 End:
758 */
759 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :