1 #define __SP_SYMBOL_C__
3 /*
4 * SVG <symbol> implementation
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 1999-2003 Lauris Kaplinski
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
18 #include <cstring>
19 #include <string>
21 #include "libnr/nr-matrix-fns.h"
22 #include "libnr/nr-matrix-ops.h"
23 #include <2geom/transforms.h>
24 #include "display/nr-arena-group.h"
25 #include "xml/repr.h"
26 #include "attributes.h"
27 #include "print.h"
28 #include "sp-symbol.h"
29 #include "document.h"
31 static void sp_symbol_class_init (SPSymbolClass *klass);
32 static void sp_symbol_init (SPSymbol *symbol);
34 static void sp_symbol_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
35 static void sp_symbol_release (SPObject *object);
36 static void sp_symbol_set (SPObject *object, unsigned int key, const gchar *value);
37 static void sp_symbol_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
38 static void sp_symbol_update (SPObject *object, SPCtx *ctx, guint flags);
39 static void sp_symbol_modified (SPObject *object, guint flags);
40 static Inkscape::XML::Node *sp_symbol_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
42 static NRArenaItem *sp_symbol_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags);
43 static void sp_symbol_hide (SPItem *item, unsigned int key);
44 static void sp_symbol_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags);
45 static void sp_symbol_print (SPItem *item, SPPrintContext *ctx);
47 static SPGroupClass *parent_class;
49 GType
50 sp_symbol_get_type (void)
51 {
52 static GType type = 0;
53 if (!type) {
54 GTypeInfo info = {
55 sizeof (SPSymbolClass),
56 NULL, NULL,
57 (GClassInitFunc) sp_symbol_class_init,
58 NULL, NULL,
59 sizeof (SPSymbol),
60 16,
61 (GInstanceInitFunc) sp_symbol_init,
62 NULL, /* value_table */
63 };
64 type = g_type_register_static (SP_TYPE_GROUP, "SPSymbol", &info, (GTypeFlags)0);
65 }
66 return type;
67 }
69 static void
70 sp_symbol_class_init (SPSymbolClass *klass)
71 {
72 GObjectClass *object_class;
73 SPObjectClass *sp_object_class;
74 SPItemClass *sp_item_class;
76 object_class = G_OBJECT_CLASS (klass);
77 sp_object_class = (SPObjectClass *) klass;
78 sp_item_class = (SPItemClass *) klass;
80 parent_class = (SPGroupClass *)g_type_class_ref (SP_TYPE_GROUP);
82 sp_object_class->build = sp_symbol_build;
83 sp_object_class->release = sp_symbol_release;
84 sp_object_class->set = sp_symbol_set;
85 sp_object_class->child_added = sp_symbol_child_added;
86 sp_object_class->update = sp_symbol_update;
87 sp_object_class->modified = sp_symbol_modified;
88 sp_object_class->write = sp_symbol_write;
90 sp_item_class->show = sp_symbol_show;
91 sp_item_class->hide = sp_symbol_hide;
92 sp_item_class->bbox = sp_symbol_bbox;
93 sp_item_class->print = sp_symbol_print;
94 }
96 static void
97 sp_symbol_init (SPSymbol *symbol)
98 {
99 symbol->viewBox_set = FALSE;
101 symbol->c2p = Geom::identity();
102 }
104 static void
105 sp_symbol_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
106 {
107 SPGroup *group;
108 SPSymbol *symbol;
110 group = (SPGroup *) object;
111 symbol = (SPSymbol *) object;
113 sp_object_read_attr (object, "viewBox");
114 sp_object_read_attr (object, "preserveAspectRatio");
116 if (((SPObjectClass *) parent_class)->build)
117 ((SPObjectClass *) parent_class)->build (object, document, repr);
118 }
120 static void
121 sp_symbol_release (SPObject *object)
122 {
123 SPSymbol * symbol;
125 symbol = (SPSymbol *) object;
127 if (((SPObjectClass *) parent_class)->release)
128 ((SPObjectClass *) parent_class)->release (object);
129 }
131 static void
132 sp_symbol_set (SPObject *object, unsigned int key, const gchar *value)
133 {
134 SPItem *item;
135 SPSymbol *symbol;
137 item = SP_ITEM (object);
138 symbol = SP_SYMBOL (object);
140 switch (key) {
141 case SP_ATTR_VIEWBOX:
142 if (value) {
143 double x, y, width, height;
144 char *eptr;
145 /* fixme: We have to take original item affine into account */
146 /* fixme: Think (Lauris) */
147 eptr = (gchar *) value;
148 x = g_ascii_strtod (eptr, &eptr);
149 while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
150 y = g_ascii_strtod (eptr, &eptr);
151 while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
152 width = g_ascii_strtod (eptr, &eptr);
153 while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
154 height = g_ascii_strtod (eptr, &eptr);
155 while (*eptr && ((*eptr == ',') || (*eptr == ' '))) eptr++;
156 if ((width > 0) && (height > 0)) {
157 /* Set viewbox */
158 symbol->viewBox.x0 = x;
159 symbol->viewBox.y0 = y;
160 symbol->viewBox.x1 = x + width;
161 symbol->viewBox.y1 = y + height;
162 symbol->viewBox_set = TRUE;
163 } else {
164 symbol->viewBox_set = FALSE;
165 }
166 } else {
167 symbol->viewBox_set = FALSE;
168 }
169 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
170 break;
171 case SP_ATTR_PRESERVEASPECTRATIO:
172 /* Do setup before, so we can use break to escape */
173 symbol->aspect_set = FALSE;
174 symbol->aspect_align = SP_ASPECT_NONE;
175 symbol->aspect_clip = SP_ASPECT_MEET;
176 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_VIEWPORT_MODIFIED_FLAG);
177 if (value) {
178 int len;
179 gchar c[256];
180 const gchar *p, *e;
181 unsigned int align, clip;
182 p = value;
183 while (*p && *p == 32) p += 1;
184 if (!*p) break;
185 e = p;
186 while (*e && *e != 32) e += 1;
187 len = e - p;
188 if (len > 8) break;
189 memcpy (c, value, len);
190 c[len] = 0;
191 /* Now the actual part */
192 if (!strcmp (c, "none")) {
193 align = SP_ASPECT_NONE;
194 } else if (!strcmp (c, "xMinYMin")) {
195 align = SP_ASPECT_XMIN_YMIN;
196 } else if (!strcmp (c, "xMidYMin")) {
197 align = SP_ASPECT_XMID_YMIN;
198 } else if (!strcmp (c, "xMaxYMin")) {
199 align = SP_ASPECT_XMAX_YMIN;
200 } else if (!strcmp (c, "xMinYMid")) {
201 align = SP_ASPECT_XMIN_YMID;
202 } else if (!strcmp (c, "xMidYMid")) {
203 align = SP_ASPECT_XMID_YMID;
204 } else if (!strcmp (c, "xMaxYMid")) {
205 align = SP_ASPECT_XMAX_YMID;
206 } else if (!strcmp (c, "xMinYMax")) {
207 align = SP_ASPECT_XMIN_YMAX;
208 } else if (!strcmp (c, "xMidYMax")) {
209 align = SP_ASPECT_XMID_YMAX;
210 } else if (!strcmp (c, "xMaxYMax")) {
211 align = SP_ASPECT_XMAX_YMAX;
212 } else {
213 break;
214 }
215 clip = SP_ASPECT_MEET;
216 while (*e && *e == 32) e += 1;
217 if (*e) {
218 if (!strcmp (e, "meet")) {
219 clip = SP_ASPECT_MEET;
220 } else if (!strcmp (e, "slice")) {
221 clip = SP_ASPECT_SLICE;
222 } else {
223 break;
224 }
225 }
226 symbol->aspect_set = TRUE;
227 symbol->aspect_align = align;
228 symbol->aspect_clip = clip;
229 }
230 break;
231 default:
232 if (((SPObjectClass *) parent_class)->set)
233 ((SPObjectClass *) parent_class)->set (object, key, value);
234 break;
235 }
236 }
238 static void
239 sp_symbol_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
240 {
241 SPSymbol *symbol;
242 SPGroup *group;
244 symbol = (SPSymbol *) object;
245 group = (SPGroup *) object;
247 if (((SPObjectClass *) (parent_class))->child_added)
248 ((SPObjectClass *) (parent_class))->child_added (object, child, ref);
249 }
251 static void
252 sp_symbol_update (SPObject *object, SPCtx *ctx, guint flags)
253 {
254 SPItem *item;
255 SPSymbol *symbol;
256 SPItemCtx *ictx, rctx;
257 SPItemView *v;
259 item = SP_ITEM (object);
260 symbol = SP_SYMBOL (object);
261 ictx = (SPItemCtx *) ctx;
263 if (SP_OBJECT_IS_CLONED (object)) {
264 /* Cloned <symbol> is actually renderable */
266 /* fixme: We have to set up clip here too */
268 /* Create copy of item context */
269 rctx = *ictx;
271 /* Calculate child to parent transformation */
272 /* Apply parent <use> translation (set up as vewport) */
273 symbol->c2p = Geom::Matrix(Geom::Translate(rctx.vp.x0, rctx.vp.y0));
275 if (symbol->viewBox_set) {
276 double x, y, width, height;
277 /* Determine actual viewbox in viewport coordinates */
278 if (symbol->aspect_align == SP_ASPECT_NONE) {
279 x = 0.0;
280 y = 0.0;
281 width = rctx.vp.x1 - rctx.vp.x0;
282 height = rctx.vp.y1 - rctx.vp.y0;
283 } else {
284 double scalex, scaley, scale;
285 /* Things are getting interesting */
286 scalex = (rctx.vp.x1 - rctx.vp.x0) / (symbol->viewBox.x1 - symbol->viewBox.x0);
287 scaley = (rctx.vp.y1 - rctx.vp.y0) / (symbol->viewBox.y1 - symbol->viewBox.y0);
288 scale = (symbol->aspect_clip == SP_ASPECT_MEET) ? MIN (scalex, scaley) : MAX (scalex, scaley);
289 width = (symbol->viewBox.x1 - symbol->viewBox.x0) * scale;
290 height = (symbol->viewBox.y1 - symbol->viewBox.y0) * scale;
291 /* Now place viewbox to requested position */
292 switch (symbol->aspect_align) {
293 case SP_ASPECT_XMIN_YMIN:
294 x = 0.0;
295 y = 0.0;
296 break;
297 case SP_ASPECT_XMID_YMIN:
298 x = 0.5 * ((rctx.vp.x1 - rctx.vp.x0) - width);
299 y = 0.0;
300 break;
301 case SP_ASPECT_XMAX_YMIN:
302 x = 1.0 * ((rctx.vp.x1 - rctx.vp.x0) - width);
303 y = 0.0;
304 break;
305 case SP_ASPECT_XMIN_YMID:
306 x = 0.0;
307 y = 0.5 * ((rctx.vp.y1 - rctx.vp.y0) - height);
308 break;
309 case SP_ASPECT_XMID_YMID:
310 x = 0.5 * ((rctx.vp.x1 - rctx.vp.x0) - width);
311 y = 0.5 * ((rctx.vp.y1 - rctx.vp.y0) - height);
312 break;
313 case SP_ASPECT_XMAX_YMID:
314 x = 1.0 * ((rctx.vp.x1 - rctx.vp.x0) - width);
315 y = 0.5 * ((rctx.vp.y1 - rctx.vp.y0) - height);
316 break;
317 case SP_ASPECT_XMIN_YMAX:
318 x = 0.0;
319 y = 1.0 * ((rctx.vp.y1 - rctx.vp.y0) - height);
320 break;
321 case SP_ASPECT_XMID_YMAX:
322 x = 0.5 * ((rctx.vp.x1 - rctx.vp.x0) - width);
323 y = 1.0 * ((rctx.vp.y1 - rctx.vp.y0) - height);
324 break;
325 case SP_ASPECT_XMAX_YMAX:
326 x = 1.0 * ((rctx.vp.x1 - rctx.vp.x0) - width);
327 y = 1.0 * ((rctx.vp.y1 - rctx.vp.y0) - height);
328 break;
329 default:
330 x = 0.0;
331 y = 0.0;
332 break;
333 }
334 }
335 /* Compose additional transformation from scale and position */
336 Geom::Matrix q;
337 q[0] = width / (symbol->viewBox.x1 - symbol->viewBox.x0);
338 q[1] = 0.0;
339 q[2] = 0.0;
340 q[3] = height / (symbol->viewBox.y1 - symbol->viewBox.y0);
341 q[4] = -symbol->viewBox.x0 * q[0] + x;
342 q[5] = -symbol->viewBox.y0 * q[3] + y;
343 /* Append viewbox transformation */
344 symbol->c2p = q * symbol->c2p;
345 }
347 rctx.i2doc = symbol->c2p * (Geom::Matrix)rctx.i2doc;
349 /* If viewBox is set initialize child viewport */
350 /* Otherwise <use> has set it up already */
351 if (symbol->viewBox_set) {
352 rctx.vp.x0 = symbol->viewBox.x0;
353 rctx.vp.y0 = symbol->viewBox.y0;
354 rctx.vp.x1 = symbol->viewBox.x1;
355 rctx.vp.y1 = symbol->viewBox.y1;
356 rctx.i2vp = Geom::identity();
357 }
359 /* And invoke parent method */
360 if (((SPObjectClass *) (parent_class))->update)
361 ((SPObjectClass *) (parent_class))->update (object, (SPCtx *) &rctx, flags);
363 /* As last step set additional transform of arena group */
364 for (v = item->display; v != NULL; v = v->next) {
365 nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), symbol->c2p);
366 }
367 } else {
368 /* No-op */
369 if (((SPObjectClass *) (parent_class))->update)
370 ((SPObjectClass *) (parent_class))->update (object, ctx, flags);
371 }
372 }
374 static void
375 sp_symbol_modified (SPObject *object, guint flags)
376 {
377 SPSymbol *symbol;
379 symbol = SP_SYMBOL (object);
381 if (((SPObjectClass *) (parent_class))->modified)
382 (* ((SPObjectClass *) (parent_class))->modified) (object, flags);
383 }
385 static Inkscape::XML::Node *
386 sp_symbol_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
387 {
388 SPSymbol *symbol;
390 symbol = SP_SYMBOL (object);
392 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
393 repr = xml_doc->createElement("svg:symbol");
394 }
396 repr->setAttribute("viewBox", object->repr->attribute("viewBox"));
397 repr->setAttribute("preserveAspectRatio", object->repr->attribute("preserveAspectRatio"));
399 if (((SPObjectClass *) (parent_class))->write)
400 ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
402 return repr;
403 }
405 static NRArenaItem *
406 sp_symbol_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
407 {
408 SPSymbol *symbol;
409 NRArenaItem *ai;
411 symbol = SP_SYMBOL (item);
413 if (SP_OBJECT_IS_CLONED (symbol)) {
414 /* Cloned <symbol> is actually renderable */
415 if (((SPItemClass *) (parent_class))->show) {
416 ai = ((SPItemClass *) (parent_class))->show (item, arena, key, flags);
417 if (ai) {
418 nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), symbol->c2p);
419 }
420 } else {
421 ai = NULL;
422 }
423 } else {
424 ai = NULL;
425 }
427 return ai;
428 }
430 static void
431 sp_symbol_hide (SPItem *item, unsigned int key)
432 {
433 SPSymbol *symbol;
435 symbol = SP_SYMBOL (item);
437 if (SP_OBJECT_IS_CLONED (symbol)) {
438 /* Cloned <symbol> is actually renderable */
439 if (((SPItemClass *) (parent_class))->hide)
440 ((SPItemClass *) (parent_class))->hide (item, key);
441 }
442 }
444 static void
445 sp_symbol_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags)
446 {
447 SPSymbol const *symbol = SP_SYMBOL(item);
449 if (SP_OBJECT_IS_CLONED (symbol)) {
450 /* Cloned <symbol> is actually renderable */
452 if (((SPItemClass *) (parent_class))->bbox) {
453 Geom::Matrix const a( symbol->c2p * transform );
454 ((SPItemClass *) (parent_class))->bbox(item, bbox, a, flags);
455 }
456 }
457 }
459 static void
460 sp_symbol_print (SPItem *item, SPPrintContext *ctx)
461 {
462 SPSymbol *symbol = SP_SYMBOL(item);
463 if (SP_OBJECT_IS_CLONED (symbol)) {
464 /* Cloned <symbol> is actually renderable */
466 sp_print_bind(ctx, &symbol->c2p, 1.0);
468 if (((SPItemClass *) (parent_class))->print) {
469 ((SPItemClass *) (parent_class))->print (item, ctx);
470 }
472 sp_print_release (ctx);
473 }
474 }