Code

remove extra unref; now ref/unref is done by show/hide; this used to stop this arenai...
[inkscape.git] / src / sp-clippath.cpp
1 #define __SP_CLIPPATH_C__
3 /*
4  * SVG <clipPath> implementation
5  *
6  * Authors:
7  *   Lauris Kaplinski <lauris@kaplinski.com>
8  *
9  * Copyright (C) 2001-2002 authors
10  * Copyright (C) 2001 Ximian, Inc.
11  *
12  * Released under GNU GPL, read the file 'COPYING' for more information
13  */
15 #include <cstring>
16 #include <string>
18 #include "display/nr-arena.h"
19 #include "display/nr-arena-group.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 "libnr/nr-matrix-ops.h"
30 #include "sp-clippath.h"
32 struct SPClipPathView {
33     SPClipPathView *next;
34     unsigned int key;
35     NRArenaItem *arenaitem;
36     NRRect bbox;
37 };
39 static void sp_clippath_class_init(SPClipPathClass *klass);
40 static void sp_clippath_init(SPClipPath *clippath);
42 static void sp_clippath_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
43 static void sp_clippath_release(SPObject * object);
44 static void sp_clippath_set(SPObject *object, unsigned int key, gchar const *value);
45 static void sp_clippath_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
46 static void sp_clippath_update(SPObject *object, SPCtx *ctx, guint flags);
47 static void sp_clippath_modified(SPObject *object, guint flags);
48 static Inkscape::XML::Node *sp_clippath_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
50 SPClipPathView *sp_clippath_view_new_prepend(SPClipPathView *list, unsigned int key, NRArenaItem *arenaitem);
51 SPClipPathView *sp_clippath_view_list_remove(SPClipPathView *list, SPClipPathView *view);
53 static SPObjectGroupClass *parent_class;
55 GType
56 sp_clippath_get_type(void)
57 {
58     static GType type = 0;
59     if (!type) {
60         GTypeInfo info = {
61             sizeof(SPClipPathClass),
62             NULL, NULL,
63             (GClassInitFunc) sp_clippath_class_init,
64             NULL, NULL,
65             sizeof(SPClipPath),
66             16,
67             (GInstanceInitFunc) sp_clippath_init,
68             NULL,       /* value_table */
69         };
70         type = g_type_register_static(SP_TYPE_OBJECTGROUP, "SPClipPath", &info, (GTypeFlags)0);
71     }
72     return type;
73 }
75 static void
76 sp_clippath_class_init(SPClipPathClass *klass)
77 {
78     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
80     parent_class = (SPObjectGroupClass*)g_type_class_ref(SP_TYPE_OBJECTGROUP);
82     sp_object_class->build = sp_clippath_build;
83     sp_object_class->release = sp_clippath_release;
84     sp_object_class->set = sp_clippath_set;
85     sp_object_class->child_added = sp_clippath_child_added;
86     sp_object_class->update = sp_clippath_update;
87     sp_object_class->modified = sp_clippath_modified;
88     sp_object_class->write = sp_clippath_write;
89 }
91 static void
92 sp_clippath_init(SPClipPath *cp)
93 {
94     cp->clipPathUnits_set = FALSE;
95     cp->clipPathUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
97     cp->display = NULL;
98 }
100 static void
101 sp_clippath_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
103     if (((SPObjectClass *) parent_class)->build)
104         ((SPObjectClass *) parent_class)->build(object, document, repr);
106     sp_object_read_attr(object, "clipPathUnits");
108     /* Register ourselves */
109     sp_document_add_resource(document, "clipPath", object);
112 static void
113 sp_clippath_release(SPObject * object)
115     if (SP_OBJECT_DOCUMENT(object)) {
116         /* Unregister ourselves */
117         sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "clipPath", object);
118     }
120     SPClipPath *cp = SP_CLIPPATH(object);
121     while (cp->display) {
122         /* We simply unref and let item manage this in handler */
123         cp->display = sp_clippath_view_list_remove(cp->display, cp->display);
124     }
126     if (((SPObjectClass *) (parent_class))->release) {
127         ((SPObjectClass *) parent_class)->release(object);
128     }
131 static void
132 sp_clippath_set(SPObject *object, unsigned int key, gchar const *value)
134     SPClipPath *cp = SP_CLIPPATH(object);
136     switch (key) {
137         case SP_ATTR_CLIPPATHUNITS:
138             cp->clipPathUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
139             cp->clipPathUnits_set = FALSE;
140             if (value) {
141                 if (!strcmp(value, "userSpaceOnUse")) {
142                     cp->clipPathUnits_set = TRUE;
143                 } else if (!strcmp(value, "objectBoundingBox")) {
144                     cp->clipPathUnits = SP_CONTENT_UNITS_OBJECTBOUNDINGBOX;
145                     cp->clipPathUnits_set = TRUE;
146                 }
147             }
148             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
149             break;
150         default:
151             if (((SPObjectClass *) parent_class)->set)
152                 ((SPObjectClass *) parent_class)->set(object, key, value);
153             break;
154     }
157 static void
158 sp_clippath_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
160     /* Invoke SPObjectGroup implementation */
161     ((SPObjectClass *) (parent_class))->child_added(object, child, ref);
163     /* Show new object */
164     SPObject *ochild = SP_OBJECT_DOCUMENT(object)->getObjectByRepr(child);
165     if (SP_IS_ITEM(ochild)) {
166         SPClipPath *cp = SP_CLIPPATH(object);
167         for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
168             NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(ochild),
169                                                   NR_ARENA_ITEM_ARENA(v->arenaitem),
170                                                   v->key,
171                                                   SP_ITEM_REFERENCE_FLAGS);
172             if (ac) {
173                 nr_arena_item_add_child(v->arenaitem, ac, NULL);
174             }
175         }
176     }
179 static void
180 sp_clippath_update(SPObject *object, SPCtx *ctx, guint flags)
182     if (flags & SP_OBJECT_MODIFIED_FLAG) {
183         flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
184     }
186     flags &= SP_OBJECT_MODIFIED_CASCADE;
188     SPObjectGroup *og = SP_OBJECTGROUP(object);
189     GSList *l = NULL;
190     for (SPObject *child = sp_object_first_child(SP_OBJECT(og)); child != NULL; child = SP_OBJECT_NEXT(child)) {
191         g_object_ref(G_OBJECT(child));
192         l = g_slist_prepend(l, child);
193     }
194     l = g_slist_reverse(l);
195     while (l) {
196         SPObject *child = SP_OBJECT(l->data);
197         l = g_slist_remove(l, child);
198         if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
199             child->updateDisplay(ctx, flags);
200         }
201         g_object_unref(G_OBJECT(child));
202     }
204     SPClipPath *cp = SP_CLIPPATH(object);
205     for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
206         if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
207             NR::Matrix t(NR::scale(v->bbox.x1 - v->bbox.x0, v->bbox.y1 - v->bbox.y0));
208             t[4] = v->bbox.x0;
209             t[5] = v->bbox.y0;
210             nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), &t);
211         } else {
212             nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), NULL);
213         }
214     }
217 static void
218 sp_clippath_modified(SPObject *object, guint flags)
220     if (flags & SP_OBJECT_MODIFIED_FLAG) {
221         flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
222     }
224     flags &= SP_OBJECT_MODIFIED_CASCADE;
226     SPObjectGroup *og = SP_OBJECTGROUP(object);
227     GSList *l = NULL;
228     for (SPObject *child = sp_object_first_child(SP_OBJECT(og)); child != NULL; child = SP_OBJECT_NEXT(child)) {
229         g_object_ref(G_OBJECT(child));
230         l = g_slist_prepend(l, child);
231     }
232     l = g_slist_reverse(l);
233     while (l) {
234         SPObject *child = SP_OBJECT(l->data);
235         l = g_slist_remove(l, child);
236         if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
237             child->emitModified(flags);
238         }
239         g_object_unref(G_OBJECT(child));
240     }
243 static Inkscape::XML::Node *
244 sp_clippath_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
246     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
247         repr = xml_doc->createElement("svg:clipPath");
248     }
250     if (((SPObjectClass *) (parent_class))->write)
251         ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
253     return repr;
256 NRArenaItem *
257 sp_clippath_show(SPClipPath *cp, NRArena *arena, unsigned int key)
259     g_return_val_if_fail(cp != NULL, NULL);
260     g_return_val_if_fail(SP_IS_CLIPPATH(cp), NULL);
261     g_return_val_if_fail(arena != NULL, NULL);
262     g_return_val_if_fail(NR_IS_ARENA(arena), NULL);
264     NRArenaItem *ai = NRArenaGroup::create(arena);
265     cp->display = sp_clippath_view_new_prepend(cp->display, key, ai);
267     for (SPObject *child = sp_object_first_child(SP_OBJECT(cp)) ; child != NULL; child = SP_OBJECT_NEXT(child)) {
268         if (SP_IS_ITEM(child)) {
269             NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(child), arena, key, SP_ITEM_REFERENCE_FLAGS);
270             if (ac) {
271                 /* The order is not important in clippath */
272                 nr_arena_item_add_child(ai, ac, NULL);
273             }
274         }
275     }
277     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
278         NR::Matrix t(NR::scale(cp->display->bbox.x1 - cp->display->bbox.x0, cp->display->bbox.y1 - cp->display->bbox.y0));
279         t[4] = cp->display->bbox.x0;
280         t[5] = cp->display->bbox.y0;
281         nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), &t);
282     }
284     return ai;
287 void
288 sp_clippath_hide(SPClipPath *cp, unsigned int key)
290     g_return_if_fail(cp != NULL);
291     g_return_if_fail(SP_IS_CLIPPATH(cp));
293     for (SPObject *child = sp_object_first_child(SP_OBJECT(cp)) ; child != NULL; child = SP_OBJECT_NEXT(child)) {
294         if (SP_IS_ITEM(child)) {
295             sp_item_invoke_hide(SP_ITEM(child), key);
296         }
297     }
299     for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
300         if (v->key == key) {
301             /* We simply unref and let item to manage this in handler */
302             cp->display = sp_clippath_view_list_remove(cp->display, v);
303             return;
304         }
305     }
307     g_assert_not_reached();
310 void
311 sp_clippath_set_bbox(SPClipPath *cp, unsigned int key, NRRect *bbox)
313     for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
314         if (v->key == key) {
315             if (!NR_DF_TEST_CLOSE(v->bbox.x0, bbox->x0, NR_EPSILON) ||
316                 !NR_DF_TEST_CLOSE(v->bbox.y0, bbox->y0, NR_EPSILON) ||
317                 !NR_DF_TEST_CLOSE(v->bbox.x1, bbox->x1, NR_EPSILON) ||
318                 !NR_DF_TEST_CLOSE(v->bbox.y1, bbox->y1, NR_EPSILON)) {
319                 v->bbox = *bbox;
320             }
321             break;
322         }
323     }
326 void
327 sp_clippath_get_bbox(SPClipPath *cp, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
329     SPObject *i;
330     for (i = sp_object_first_child(SP_OBJECT(cp)); i && !SP_IS_ITEM(i); i = SP_OBJECT_NEXT(i));
331     if (!i) return;
333     sp_item_invoke_bbox_full(SP_ITEM(i), bbox, NR::Matrix(SP_ITEM(i)->transform) * transform, SPItem::GEOMETRIC_BBOX, FALSE);
334     SPObject *i_start = i;
336     while (i != NULL) {
337         if (i != i_start) {
338             NRRect i_box;
339             sp_item_invoke_bbox_full(SP_ITEM(i), &i_box, NR::Matrix(SP_ITEM(i)->transform) * transform, SPItem::GEOMETRIC_BBOX, FALSE);
340             nr_rect_d_union (bbox, bbox, &i_box);
341         }
342         i = SP_OBJECT_NEXT(i);
343         for (; i && !SP_IS_ITEM(i); i = SP_OBJECT_NEXT(i));
344     }
347 /* ClipPath views */
349 SPClipPathView *
350 sp_clippath_view_new_prepend(SPClipPathView *list, unsigned int key, NRArenaItem *arenaitem)
352     SPClipPathView *new_path_view = g_new(SPClipPathView, 1);
354     new_path_view->next = list;
355     new_path_view->key = key;
356     new_path_view->arenaitem = nr_arena_item_ref(arenaitem);
357     new_path_view->bbox.x0 = new_path_view->bbox.x1 = 0.0;
358     new_path_view->bbox.y0 = new_path_view->bbox.y1 = 0.0;
360     return new_path_view;
363 SPClipPathView *
364 sp_clippath_view_list_remove(SPClipPathView *list, SPClipPathView *view)
366     if (view == list) {
367         list = list->next;
368     } else {
369         SPClipPathView *prev;
370         prev = list;
371         while (prev->next != view) prev = prev->next;
372         prev->next = view->next;
373     }
375     nr_arena_item_unref(view->arenaitem);
376     g_free(view);
378     return list;
381 // Create a mask element (using passed elements), add it to <defs>
382 const gchar *
383 sp_clippath_create (GSList *reprs, SPDocument *document, NR::Matrix const* applyTransform)
385     Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
387     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
388     Inkscape::XML::Node *repr = xml_doc->createElement("svg:clipPath");
389     repr->setAttribute("clipPathUnits", "userSpaceOnUse");
391     defsrepr->appendChild(repr);
392     const gchar *id = repr->attribute("id");
393     SPObject *clip_path_object = document->getObjectById(id);
395     for (GSList *it = reprs; it != NULL; it = it->next) {
396         Inkscape::XML::Node *node = (Inkscape::XML::Node *)(it->data);
397         SPItem *item = SP_ITEM(clip_path_object->appendChildRepr(node));
399         if (NULL != applyTransform) {
400             NR::Matrix transform (item->transform);
401             transform *= (*applyTransform);
402             sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
403         }
404     }
406     Inkscape::GC::release(repr);
407     return id;
410 /*
411   Local Variables:
412   mode:c++
413   c-file-style:"stroustrup"
414   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
415   indent-tabs-mode:nil
416   fill-column:99
417   End:
418 */
419 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :