Code

0a7fca4d212dad84bd385221196fd46537e7e20a
[inkscape.git] / src / sp-symbol.cpp
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 "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::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, NR::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         nr_matrix_set_identity (&symbol->c2p);
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         sp_object_read_attr (object, "viewBox");
113         sp_object_read_attr (object, "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, "xMaxYMin")) {
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                 nr_matrix_set_translate (&symbol->c2p, rctx.vp.x0, rctx.vp.y0);
274                 if (symbol->viewBox_set) {
275                         double x, y, width, height;
276                         NRMatrix q;
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                         q.c[0] = width / (symbol->viewBox.x1 - symbol->viewBox.x0);
337                         q.c[1] = 0.0;
338                         q.c[2] = 0.0;
339                         q.c[3] = height / (symbol->viewBox.y1 - symbol->viewBox.y0);
340                         q.c[4] = -symbol->viewBox.x0 * q.c[0] + x;
341                         q.c[5] = -symbol->viewBox.y0 * q.c[3] + y;
342                         /* Append viewbox transformation */
343                         nr_matrix_multiply (&symbol->c2p, &q, &symbol->c2p);
344                 }
346                 rctx.i2doc = symbol->c2p * 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 = NR::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::Node *repr, guint flags)
387         SPSymbol *symbol;
389         symbol = SP_SYMBOL (object);
391         if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
392                 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
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, repr, flags);
402         return repr;
405 static NRArenaItem *
406 sp_symbol_show (SPItem *item, NRArena *arena, unsigned int key, unsigned int flags)
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;
430 static void
431 sp_symbol_hide (SPItem *item, unsigned int key)
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         }
444 static void
445 sp_symbol_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
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                         NR::Matrix const a( symbol->c2p * transform );
454                         ((SPItemClass *) (parent_class))->bbox(item, bbox, a, flags);
455                 }
456         }
459 static void
460 sp_symbol_print (SPItem *item, SPPrintContext *ctx)
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         }