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 */
14 #include <cstring>
15 #include <string>
17 #include "display/nr-arena.h"
18 #include "display/nr-arena-group.h"
19 #include "libnr/nr-matrix-ops.h"
20 #include <xml/repr.h>
22 #include "enums.h"
23 #include "attributes.h"
24 #include "document.h"
25 #include "document-private.h"
26 #include "sp-item.h"
28 #include "sp-mask.h"
30 struct SPMaskView {
31 SPMaskView *next;
32 unsigned int key;
33 NRArenaItem *arenaitem;
34 NRRect bbox;
35 };
37 static void sp_mask_class_init (SPMaskClass *klass);
38 static void sp_mask_init (SPMask *mask);
40 static void sp_mask_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
41 static void sp_mask_release (SPObject * object);
42 static void sp_mask_set (SPObject *object, unsigned int key, const gchar *value);
43 static void sp_mask_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
44 static void sp_mask_update (SPObject *object, SPCtx *ctx, guint flags);
45 static void sp_mask_modified (SPObject *object, guint flags);
46 static Inkscape::XML::Node *sp_mask_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
48 SPMaskView *sp_mask_view_new_prepend (SPMaskView *list, unsigned int key, NRArenaItem *arenaitem);
49 SPMaskView *sp_mask_view_list_remove (SPMaskView *list, SPMaskView *view);
51 static SPObjectGroupClass *parent_class;
53 GType
54 sp_mask_get_type (void)
55 {
56 static GType type = 0;
57 if (!type) {
58 GTypeInfo info = {
59 sizeof (SPMaskClass),
60 NULL, NULL,
61 (GClassInitFunc) sp_mask_class_init,
62 NULL, NULL,
63 sizeof (SPMask),
64 16,
65 (GInstanceInitFunc) sp_mask_init,
66 NULL, /* value_table */
67 };
68 type = g_type_register_static (SP_TYPE_OBJECTGROUP, "SPMask", &info, (GTypeFlags)0);
69 }
70 return type;
71 }
73 static void
74 sp_mask_class_init (SPMaskClass *klass)
75 {
76 parent_class = (SPObjectGroupClass*) g_type_class_ref (SP_TYPE_OBJECTGROUP);
78 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
79 sp_object_class->build = sp_mask_build;
80 sp_object_class->release = sp_mask_release;
81 sp_object_class->set = sp_mask_set;
82 sp_object_class->child_added = sp_mask_child_added;
83 sp_object_class->update = sp_mask_update;
84 sp_object_class->modified = sp_mask_modified;
85 sp_object_class->write = sp_mask_write;
86 }
88 static void
89 sp_mask_init (SPMask *mask)
90 {
91 mask->maskUnits_set = FALSE;
92 mask->maskUnits = SP_CONTENT_UNITS_OBJECTBOUNDINGBOX;
94 mask->maskContentUnits_set = FALSE;
95 mask->maskContentUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
97 mask->display = NULL;
98 }
100 static void
101 sp_mask_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
102 {
103 if (((SPObjectClass *) parent_class)->build) {
104 ((SPObjectClass *) parent_class)->build (object, document, repr);
105 }
107 sp_object_read_attr (object, "maskUnits");
108 sp_object_read_attr (object, "maskContentUnits");
110 /* Register ourselves */
111 sp_document_add_resource (document, "mask", object);
112 }
114 static void
115 sp_mask_release (SPObject * object)
116 {
117 if (SP_OBJECT_DOCUMENT (object)) {
118 /* Unregister ourselves */
119 sp_document_remove_resource (SP_OBJECT_DOCUMENT (object), "mask", object);
120 }
122 SPMask *cp = SP_MASK (object);
123 while (cp->display) {
124 /* We simply unref and let item manage this in handler */
125 cp->display = sp_mask_view_list_remove (cp->display, cp->display);
126 }
128 if (((SPObjectClass *) (parent_class))->release) {
129 ((SPObjectClass *) parent_class)->release (object);
130 }
131 }
133 static void
134 sp_mask_set (SPObject *object, unsigned int key, const gchar *value)
135 {
136 SPMask *mask = SP_MASK (object);
138 switch (key) {
139 case SP_ATTR_MASKUNITS:
140 mask->maskUnits = SP_CONTENT_UNITS_OBJECTBOUNDINGBOX;
141 mask->maskUnits_set = FALSE;
142 if (value) {
143 if (!strcmp (value, "userSpaceOnUse")) {
144 mask->maskUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
145 mask->maskUnits_set = TRUE;
146 } else if (!strcmp (value, "objectBoundingBox")) {
147 mask->maskUnits_set = TRUE;
148 }
149 }
150 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
151 break;
152 case SP_ATTR_MASKCONTENTUNITS:
153 mask->maskContentUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
154 mask->maskContentUnits_set = FALSE;
155 if (value) {
156 if (!strcmp (value, "userSpaceOnUse")) {
157 mask->maskContentUnits_set = TRUE;
158 } else if (!strcmp (value, "objectBoundingBox")) {
159 mask->maskContentUnits = SP_CONTENT_UNITS_OBJECTBOUNDINGBOX;
160 mask->maskContentUnits_set = TRUE;
161 }
162 }
163 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
164 break;
165 default:
166 if (((SPObjectClass *) parent_class)->set)
167 ((SPObjectClass *) parent_class)->set (object, key, value);
168 break;
169 }
170 }
172 static void
173 sp_mask_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
174 {
175 /* Invoke SPObjectGroup implementation */
176 ((SPObjectClass *) (parent_class))->child_added (object, child, ref);
178 /* Show new object */
179 SPObject *ochild = SP_OBJECT_DOCUMENT (object)->getObjectByRepr(child);
180 if (SP_IS_ITEM (ochild)) {
181 SPMask *cp = SP_MASK (object);
182 for (SPMaskView *v = cp->display; v != NULL; v = v->next) {
183 NRArenaItem *ac = sp_item_invoke_show (SP_ITEM (ochild),
184 NR_ARENA_ITEM_ARENA (v->arenaitem),
185 v->key,
186 SP_ITEM_REFERENCE_FLAGS);
187 if (ac) {
188 nr_arena_item_add_child (v->arenaitem, ac, NULL);
189 }
190 }
191 }
192 }
194 static void
195 sp_mask_update (SPObject *object, SPCtx *ctx, guint flags)
196 {
197 if (flags & SP_OBJECT_MODIFIED_FLAG) {
198 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
199 }
201 flags &= SP_OBJECT_MODIFIED_CASCADE;
203 SPObjectGroup *og = SP_OBJECTGROUP (object);
204 GSList *l = NULL;
205 for (SPObject *child = sp_object_first_child(SP_OBJECT(og)); child != NULL; child = SP_OBJECT_NEXT(child)) {
206 g_object_ref (G_OBJECT (child));
207 l = g_slist_prepend (l, child);
208 }
209 l = g_slist_reverse (l);
210 while (l) {
211 SPObject *child = SP_OBJECT (l->data);
212 l = g_slist_remove (l, child);
213 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
214 child->updateDisplay(ctx, flags);
215 }
216 g_object_unref (G_OBJECT (child));
217 }
219 SPMask *mask = SP_MASK (object);
220 for (SPMaskView *v = mask->display; v != NULL; v = v->next) {
221 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
222 Geom::Matrix t(Geom::Scale(v->bbox.x1 - v->bbox.x0, v->bbox.y1 - v->bbox.y0));
223 t[4] = v->bbox.x0;
224 t[5] = v->bbox.y0;
225 nr_arena_group_set_child_transform (NR_ARENA_GROUP (v->arenaitem), &t);
226 } else {
227 nr_arena_group_set_child_transform (NR_ARENA_GROUP (v->arenaitem), NULL);
228 }
229 }
230 }
232 static void
233 sp_mask_modified (SPObject *object, guint flags)
234 {
235 if (flags & SP_OBJECT_MODIFIED_FLAG) {
236 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
237 }
239 flags &= SP_OBJECT_MODIFIED_CASCADE;
241 SPObjectGroup *og = SP_OBJECTGROUP (object);
242 GSList *l = NULL;
243 for (SPObject *child = sp_object_first_child(SP_OBJECT(og)); child != NULL; child = SP_OBJECT_NEXT(child)) {
244 g_object_ref (G_OBJECT (child));
245 l = g_slist_prepend (l, child);
246 }
247 l = g_slist_reverse (l);
248 while (l) {
249 SPObject *child = SP_OBJECT (l->data);
250 l = g_slist_remove (l, child);
251 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
252 child->emitModified(flags);
253 }
254 g_object_unref (G_OBJECT (child));
255 }
256 }
258 static Inkscape::XML::Node *
259 sp_mask_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
260 {
261 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
262 repr = xml_doc->createElement("svg:mask");
263 }
265 if (((SPObjectClass *) (parent_class))->write)
266 ((SPObjectClass *) (parent_class))->write (object, xml_doc, repr, flags);
268 return repr;
269 }
271 // Create a mask element (using passed elements), add it to <defs>
272 const gchar *
273 sp_mask_create (GSList *reprs, SPDocument *document, Geom::Matrix const* applyTransform)
274 {
275 Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
277 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
278 Inkscape::XML::Node *repr = xml_doc->createElement("svg:mask");
279 repr->setAttribute("maskUnits", "userSpaceOnUse");
281 defsrepr->appendChild(repr);
282 const gchar *mask_id = repr->attribute("id");
283 SPObject *mask_object = document->getObjectById(mask_id);
285 for (GSList *it = reprs; it != NULL; it = it->next) {
286 Inkscape::XML::Node *node = (Inkscape::XML::Node *)(it->data);
287 SPItem *item = SP_ITEM(mask_object->appendChildRepr(node));
289 if (NULL != applyTransform) {
290 Geom::Matrix transform (item->transform);
291 transform *= (*applyTransform);
292 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
293 }
294 }
296 if (repr != defsrepr->lastChild())
297 defsrepr->changeOrder(repr, defsrepr->lastChild()); // workaround for bug 989084
299 Inkscape::GC::release(repr);
300 return mask_id;
301 }
303 NRArenaItem *
304 sp_mask_show (SPMask *mask, NRArena *arena, unsigned int key)
305 {
306 g_return_val_if_fail (mask != NULL, NULL);
307 g_return_val_if_fail (SP_IS_MASK (mask), NULL);
308 g_return_val_if_fail (arena != NULL, NULL);
309 g_return_val_if_fail (NR_IS_ARENA (arena), NULL);
311 NRArenaItem *ai = NRArenaGroup::create(arena);
312 mask->display = sp_mask_view_new_prepend (mask->display, key, ai);
314 for (SPObject *child = sp_object_first_child(SP_OBJECT(mask)) ; child != NULL; child = SP_OBJECT_NEXT(child)) {
315 if (SP_IS_ITEM (child)) {
316 NRArenaItem *ac = sp_item_invoke_show (SP_ITEM (child), arena, key, SP_ITEM_REFERENCE_FLAGS);
317 if (ac) {
318 /* The order is not important in mask */
319 nr_arena_item_add_child (ai, ac, NULL);
320 }
321 }
322 }
324 if (mask->maskContentUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
325 Geom::Matrix t(Geom::Scale(mask->display->bbox.x1 - mask->display->bbox.x0, mask->display->bbox.y1 - mask->display->bbox.y0));
326 t[4] = mask->display->bbox.x0;
327 t[5] = mask->display->bbox.y0;
328 nr_arena_group_set_child_transform (NR_ARENA_GROUP (ai), &t);
329 }
331 return ai;
332 }
334 void
335 sp_mask_hide (SPMask *cp, unsigned int key)
336 {
337 g_return_if_fail (cp != NULL);
338 g_return_if_fail (SP_IS_MASK (cp));
340 for (SPObject *child = sp_object_first_child(SP_OBJECT(cp)); child != NULL; child = SP_OBJECT_NEXT(child)) {
341 if (SP_IS_ITEM (child)) {
342 sp_item_invoke_hide (SP_ITEM (child), key);
343 }
344 }
346 for (SPMaskView *v = cp->display; v != NULL; v = v->next) {
347 if (v->key == key) {
348 /* We simply unref and let item to manage this in handler */
349 cp->display = sp_mask_view_list_remove (cp->display, v);
350 return;
351 }
352 }
354 g_assert_not_reached ();
355 }
357 void
358 sp_mask_set_bbox (SPMask *mask, unsigned int key, NRRect *bbox)
359 {
360 for (SPMaskView *v = mask->display; v != NULL; v = v->next) {
361 if (v->key == key) {
362 if (!NR_DF_TEST_CLOSE (v->bbox.x0, bbox->x0, NR_EPSILON) ||
363 !NR_DF_TEST_CLOSE (v->bbox.y0, bbox->y0, NR_EPSILON) ||
364 !NR_DF_TEST_CLOSE (v->bbox.x1, bbox->x1, NR_EPSILON) ||
365 !NR_DF_TEST_CLOSE (v->bbox.y1, bbox->y1, NR_EPSILON)) {
366 v->bbox = *bbox;
367 }
368 break;
369 }
370 }
371 }
373 /* Mask views */
375 SPMaskView *
376 sp_mask_view_new_prepend (SPMaskView *list, unsigned int key, NRArenaItem *arenaitem)
377 {
378 SPMaskView *new_mask_view = g_new (SPMaskView, 1);
380 new_mask_view->next = list;
381 new_mask_view->key = key;
382 new_mask_view->arenaitem = nr_arena_item_ref(arenaitem);
383 new_mask_view->bbox.x0 = new_mask_view->bbox.x1 = 0.0;
384 new_mask_view->bbox.y0 = new_mask_view->bbox.y1 = 0.0;
386 return new_mask_view;
387 }
389 SPMaskView *
390 sp_mask_view_list_remove (SPMaskView *list, SPMaskView *view)
391 {
392 if (view == list) {
393 list = list->next;
394 } else {
395 SPMaskView *prev;
396 prev = list;
397 while (prev->next != view) prev = prev->next;
398 prev->next = view->next;
399 }
401 nr_arena_item_unref (view->arenaitem);
402 g_free (view);
404 return list;
405 }
407 /*
408 Local Variables:
409 mode:c++
410 c-file-style:"stroustrup"
411 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
412 indent-tabs-mode:nil
413 fill-column:99
414 End:
415 */
416 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :