1 #define __SP_MASK_C__
3 /*
4 * SVG <mask> implementation
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 *
9 * Copyright (C) 2003 authors
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
15 #include "display/nr-arena.h"
16 #include "display/nr-arena-group.h"
17 #include <xml/repr.h>
19 #include "enums.h"
20 #include "attributes.h"
21 #include "document.h"
22 #include "document-private.h"
23 #include "sp-item.h"
25 #include "sp-mask.h"
27 struct SPMaskView {
28 SPMaskView *next;
29 unsigned int key;
30 NRArenaItem *arenaitem;
31 NRRect bbox;
32 };
34 static void sp_mask_class_init (SPMaskClass *klass);
35 static void sp_mask_init (SPMask *mask);
37 static void sp_mask_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
38 static void sp_mask_release (SPObject * object);
39 static void sp_mask_set (SPObject *object, unsigned int key, const gchar *value);
40 static void sp_mask_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
41 static void sp_mask_update (SPObject *object, SPCtx *ctx, guint flags);
42 static void sp_mask_modified (SPObject *object, guint flags);
43 static Inkscape::XML::Node *sp_mask_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
45 SPMaskView *sp_mask_view_new_prepend (SPMaskView *list, unsigned int key, NRArenaItem *arenaitem);
46 SPMaskView *sp_mask_view_list_remove (SPMaskView *list, SPMaskView *view);
48 static SPObjectGroupClass *parent_class;
50 GType
51 sp_mask_get_type (void)
52 {
53 static GType type = 0;
54 if (!type) {
55 GTypeInfo info = {
56 sizeof (SPMaskClass),
57 NULL, NULL,
58 (GClassInitFunc) sp_mask_class_init,
59 NULL, NULL,
60 sizeof (SPMask),
61 16,
62 (GInstanceInitFunc) sp_mask_init,
63 NULL, /* value_table */
64 };
65 type = g_type_register_static (SP_TYPE_OBJECTGROUP, "SPMask", &info, (GTypeFlags)0);
66 }
67 return type;
68 }
70 static void
71 sp_mask_class_init (SPMaskClass *klass)
72 {
73 parent_class = (SPObjectGroupClass*) g_type_class_ref (SP_TYPE_OBJECTGROUP);
75 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
76 sp_object_class->build = sp_mask_build;
77 sp_object_class->release = sp_mask_release;
78 sp_object_class->set = sp_mask_set;
79 sp_object_class->child_added = sp_mask_child_added;
80 sp_object_class->update = sp_mask_update;
81 sp_object_class->modified = sp_mask_modified;
82 sp_object_class->write = sp_mask_write;
83 }
85 static void
86 sp_mask_init (SPMask *mask)
87 {
88 mask->maskUnits_set = FALSE;
89 mask->maskUnits = SP_CONTENT_UNITS_OBJECTBOUNDINGBOX;
91 mask->maskContentUnits_set = FALSE;
92 mask->maskContentUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
94 mask->display = NULL;
95 }
97 static void
98 sp_mask_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
99 {
100 if (((SPObjectClass *) parent_class)->build) {
101 ((SPObjectClass *) parent_class)->build (object, document, repr);
102 }
104 sp_object_read_attr (object, "maskUnits");
105 sp_object_read_attr (object, "maskContentUnits");
107 /* Register ourselves */
108 sp_document_add_resource (document, "mask", object);
109 }
111 static void
112 sp_mask_release (SPObject * object)
113 {
114 if (SP_OBJECT_DOCUMENT (object)) {
115 /* Unregister ourselves */
116 sp_document_remove_resource (SP_OBJECT_DOCUMENT (object), "mask", object);
117 }
119 SPMask *cp = SP_MASK (object);
120 while (cp->display) {
121 /* We simply unref and let item manage this in handler */
122 cp->display = sp_mask_view_list_remove (cp->display, cp->display);
123 }
125 if (((SPObjectClass *) (parent_class))->release) {
126 ((SPObjectClass *) parent_class)->release (object);
127 }
128 }
130 static void
131 sp_mask_set (SPObject *object, unsigned int key, const gchar *value)
132 {
133 SPMask *mask = SP_MASK (object);
135 switch (key) {
136 case SP_ATTR_MASKUNITS:
137 mask->maskUnits = SP_CONTENT_UNITS_OBJECTBOUNDINGBOX;
138 mask->maskUnits_set = FALSE;
139 if (value) {
140 if (!strcmp (value, "userSpaceOnUse")) {
141 mask->maskUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
142 mask->maskUnits_set = TRUE;
143 } else if (!strcmp (value, "objectBoundingBox")) {
144 mask->maskUnits_set = TRUE;
145 }
146 }
147 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
148 break;
149 case SP_ATTR_MASKCONTENTUNITS:
150 mask->maskContentUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
151 mask->maskContentUnits_set = FALSE;
152 if (value) {
153 if (!strcmp (value, "userSpaceOnUse")) {
154 mask->maskContentUnits_set = TRUE;
155 } else if (!strcmp (value, "objectBoundingBox")) {
156 mask->maskContentUnits = SP_CONTENT_UNITS_OBJECTBOUNDINGBOX;
157 mask->maskContentUnits_set = TRUE;
158 }
159 }
160 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
161 break;
162 default:
163 if (((SPObjectClass *) parent_class)->set)
164 ((SPObjectClass *) parent_class)->set (object, key, value);
165 break;
166 }
167 }
169 static void
170 sp_mask_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
171 {
172 /* Invoke SPObjectGroup implementation */
173 ((SPObjectClass *) (parent_class))->child_added (object, child, ref);
175 /* Show new object */
176 SPObject *ochild = SP_OBJECT_DOCUMENT (object)->getObjectByRepr(child);
177 if (SP_IS_ITEM (ochild)) {
178 SPMask *cp = SP_MASK (object);
179 for (SPMaskView *v = cp->display; v != NULL; v = v->next) {
180 NRArenaItem *ac = sp_item_invoke_show (SP_ITEM (ochild),
181 NR_ARENA_ITEM_ARENA (v->arenaitem),
182 v->key,
183 SP_ITEM_REFERENCE_FLAGS);
184 if (ac) {
185 nr_arena_item_add_child (v->arenaitem, ac, NULL);
186 nr_arena_item_unref (ac);
187 }
188 }
189 }
190 }
192 static void
193 sp_mask_update (SPObject *object, SPCtx *ctx, guint flags)
194 {
195 if (flags & SP_OBJECT_MODIFIED_FLAG) {
196 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
197 }
199 flags &= SP_OBJECT_MODIFIED_CASCADE;
201 SPObjectGroup *og = SP_OBJECTGROUP (object);
202 GSList *l = NULL;
203 for (SPObject *child = sp_object_first_child(SP_OBJECT(og)); child != NULL; child = SP_OBJECT_NEXT(child)) {
204 g_object_ref (G_OBJECT (child));
205 l = g_slist_prepend (l, child);
206 }
207 l = g_slist_reverse (l);
208 while (l) {
209 SPObject *child = SP_OBJECT (l->data);
210 l = g_slist_remove (l, child);
211 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
212 child->updateDisplay(ctx, flags);
213 }
214 g_object_unref (G_OBJECT (child));
215 }
217 SPMask *mask = SP_MASK (object);
218 for (SPMaskView *v = mask->display; v != NULL; v = v->next) {
219 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
220 NRMatrix t;
221 nr_matrix_set_scale (&t, v->bbox.x1 - v->bbox.x0, v->bbox.y1 - v->bbox.y0);
222 t.c[4] = v->bbox.x0;
223 t.c[5] = v->bbox.y0;
224 nr_arena_group_set_child_transform (NR_ARENA_GROUP (v->arenaitem), &t);
225 } else {
226 nr_arena_group_set_child_transform (NR_ARENA_GROUP (v->arenaitem), NULL);
227 }
228 }
229 }
231 static void
232 sp_mask_modified (SPObject *object, guint flags)
233 {
234 if (flags & SP_OBJECT_MODIFIED_FLAG) {
235 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
236 }
238 flags &= SP_OBJECT_MODIFIED_CASCADE;
240 SPObjectGroup *og = SP_OBJECTGROUP (object);
241 GSList *l = NULL;
242 for (SPObject *child = sp_object_first_child(SP_OBJECT(og)); child != NULL; child = SP_OBJECT_NEXT(child)) {
243 g_object_ref (G_OBJECT (child));
244 l = g_slist_prepend (l, child);
245 }
246 l = g_slist_reverse (l);
247 while (l) {
248 SPObject *child = SP_OBJECT (l->data);
249 l = g_slist_remove (l, child);
250 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
251 child->emitModified(flags);
252 }
253 g_object_unref (G_OBJECT (child));
254 }
255 }
257 static Inkscape::XML::Node *
258 sp_mask_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
259 {
260 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
261 repr = sp_repr_new ("svg:mask");
262 }
264 if (((SPObjectClass *) (parent_class))->write)
265 ((SPObjectClass *) (parent_class))->write (object, repr, flags);
267 return repr;
268 }
270 // Create a mask element (using passed elements), add it to <defs>
271 const gchar *
272 sp_mask_create (GSList *reprs, SPDocument *document, NR::Matrix const* applyTransform)
273 {
274 Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
276 Inkscape::XML::Node *repr = sp_repr_new ("svg:mask");
277 repr->setAttribute("maskUnits", "userSpaceOnUse");
279 defsrepr->appendChild(repr);
280 const gchar *mask_id = repr->attribute("id");
281 SPObject *mask_object = document->getObjectById(mask_id);
283 for (GSList *it = reprs; it != NULL; it = it->next) {
284 Inkscape::XML::Node *node = (Inkscape::XML::Node *)(it->data);
285 SPItem *item = SP_ITEM(mask_object->appendChildRepr(node));
287 if (NULL != applyTransform) {
288 NR::Matrix transform (item->transform);
289 transform *= (*applyTransform);
290 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
291 }
292 }
294 defsrepr->changeOrder(repr, defsrepr->lastChild()); // workaround for bug 989084
296 Inkscape::GC::release(repr);
297 return mask_id;
298 }
300 NRArenaItem *
301 sp_mask_show (SPMask *mask, NRArena *arena, unsigned int key)
302 {
303 g_return_val_if_fail (mask != NULL, NULL);
304 g_return_val_if_fail (SP_IS_MASK (mask), NULL);
305 g_return_val_if_fail (arena != NULL, NULL);
306 g_return_val_if_fail (NR_IS_ARENA (arena), NULL);
308 NRArenaItem *ai = NRArenaGroup::create(arena);
309 mask->display = sp_mask_view_new_prepend (mask->display, key, ai);
311 for (SPObject *child = sp_object_first_child(SP_OBJECT(mask)) ; child != NULL; child = SP_OBJECT_NEXT(child)) {
312 if (SP_IS_ITEM (child)) {
313 NRArenaItem *ac = sp_item_invoke_show (SP_ITEM (child), arena, key, SP_ITEM_REFERENCE_FLAGS);
314 if (ac) {
315 /* The order is not important in mask */
316 nr_arena_item_add_child (ai, ac, NULL);
317 nr_arena_item_unref (ac);
318 }
319 }
320 }
322 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
323 NRMatrix t;
324 nr_matrix_set_scale (&t, mask->display->bbox.x1 - mask->display->bbox.x0, mask->display->bbox.y1 - mask->display->bbox.y0);
325 t.c[4] = mask->display->bbox.x0;
326 t.c[5] = mask->display->bbox.y0;
327 nr_arena_group_set_child_transform (NR_ARENA_GROUP (ai), &t);
328 }
330 return ai;
331 }
333 void
334 sp_mask_hide (SPMask *cp, unsigned int key)
335 {
336 g_return_if_fail (cp != NULL);
337 g_return_if_fail (SP_IS_MASK (cp));
339 for (SPObject *child = sp_object_first_child(SP_OBJECT(cp)); child != NULL; child = SP_OBJECT_NEXT(child)) {
340 if (SP_IS_ITEM (child)) {
341 sp_item_invoke_hide (SP_ITEM (child), key);
342 }
343 }
345 for (SPMaskView *v = cp->display; v != NULL; v = v->next) {
346 if (v->key == key) {
347 /* We simply unref and let item to manage this in handler */
348 cp->display = sp_mask_view_list_remove (cp->display, v);
349 return;
350 }
351 }
353 g_assert_not_reached ();
354 }
356 void
357 sp_mask_set_bbox (SPMask *mask, unsigned int key, NRRect *bbox)
358 {
359 for (SPMaskView *v = mask->display; v != NULL; v = v->next) {
360 if (v->key == key) {
361 if (!NR_DF_TEST_CLOSE (v->bbox.x0, bbox->x0, NR_EPSILON) ||
362 !NR_DF_TEST_CLOSE (v->bbox.y0, bbox->y0, NR_EPSILON) ||
363 !NR_DF_TEST_CLOSE (v->bbox.x1, bbox->x1, NR_EPSILON) ||
364 !NR_DF_TEST_CLOSE (v->bbox.y1, bbox->y1, NR_EPSILON)) {
365 v->bbox = *bbox;
366 }
367 break;
368 }
369 }
370 }
372 /* Mask views */
374 SPMaskView *
375 sp_mask_view_new_prepend (SPMaskView *list, unsigned int key, NRArenaItem *arenaitem)
376 {
377 SPMaskView *new_mask_view = g_new (SPMaskView, 1);
379 new_mask_view->next = list;
380 new_mask_view->key = key;
381 new_mask_view->arenaitem = nr_arena_item_ref(arenaitem);
382 new_mask_view->bbox.x0 = new_mask_view->bbox.x1 = 0.0;
383 new_mask_view->bbox.y0 = new_mask_view->bbox.y1 = 0.0;
385 return new_mask_view;
386 }
388 SPMaskView *
389 sp_mask_view_list_remove (SPMaskView *list, SPMaskView *view)
390 {
391 if (view == list) {
392 list = list->next;
393 } else {
394 SPMaskView *prev;
395 prev = list;
396 while (prev->next != view) prev = prev->next;
397 prev->next = view->next;
398 }
400 nr_arena_item_unref (view->arenaitem);
401 g_free (view);
403 return list;
404 }
406 /*
407 Local Variables:
408 mode:c++
409 c-file-style:"stroustrup"
410 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
411 indent-tabs-mode:nil
412 fill-column:99
413 End:
414 */
415 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :