Code

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