Code

Pot and Dutch translation update
[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"
29 #include <2geom/transforms.h>
31 #include "sp-clippath.h"
33 struct SPClipPathView {
34     SPClipPathView *next;
35     unsigned int key;
36     NRArenaItem *arenaitem;
37     NRRect bbox;
38 };
40 static void sp_clippath_class_init(SPClipPathClass *klass);
41 static void sp_clippath_init(SPClipPath *clippath);
43 static void sp_clippath_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
44 static void sp_clippath_release(SPObject * object);
45 static void sp_clippath_set(SPObject *object, unsigned int key, gchar const *value);
46 static void sp_clippath_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
47 static void sp_clippath_update(SPObject *object, SPCtx *ctx, guint flags);
48 static void sp_clippath_modified(SPObject *object, guint flags);
49 static Inkscape::XML::Node *sp_clippath_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
51 SPClipPathView *sp_clippath_view_new_prepend(SPClipPathView *list, unsigned int key, NRArenaItem *arenaitem);
52 SPClipPathView *sp_clippath_view_list_remove(SPClipPathView *list, SPClipPathView *view);
54 static SPObjectGroupClass *parent_class;
56 GType
57 sp_clippath_get_type(void)
58 {
59     static GType type = 0;
60     if (!type) {
61         GTypeInfo info = {
62             sizeof(SPClipPathClass),
63             NULL, NULL,
64             (GClassInitFunc) sp_clippath_class_init,
65             NULL, NULL,
66             sizeof(SPClipPath),
67             16,
68             (GInstanceInitFunc) sp_clippath_init,
69             NULL,       /* value_table */
70         };
71         type = g_type_register_static(SP_TYPE_OBJECTGROUP, "SPClipPath", &info, (GTypeFlags)0);
72     }
73     return type;
74 }
76 static void
77 sp_clippath_class_init(SPClipPathClass *klass)
78 {
79     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
81     parent_class = (SPObjectGroupClass*)g_type_class_ref(SP_TYPE_OBJECTGROUP);
83     sp_object_class->build = sp_clippath_build;
84     sp_object_class->release = sp_clippath_release;
85     sp_object_class->set = sp_clippath_set;
86     sp_object_class->child_added = sp_clippath_child_added;
87     sp_object_class->update = sp_clippath_update;
88     sp_object_class->modified = sp_clippath_modified;
89     sp_object_class->write = sp_clippath_write;
90 }
92 static void
93 sp_clippath_init(SPClipPath *cp)
94 {
95     cp->clipPathUnits_set = FALSE;
96     cp->clipPathUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
98     cp->display = NULL;
99 }
101 static void
102 sp_clippath_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
104     if (((SPObjectClass *) parent_class)->build)
105         ((SPObjectClass *) parent_class)->build(object, document, repr);
107     sp_object_read_attr(object, "clipPathUnits");
109     /* Register ourselves */
110     sp_document_add_resource(document, "clipPath", object);
113 static void
114 sp_clippath_release(SPObject * object)
116     if (SP_OBJECT_DOCUMENT(object)) {
117         /* Unregister ourselves */
118         sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "clipPath", object);
119     }
121     SPClipPath *cp = SP_CLIPPATH(object);
122     while (cp->display) {
123         /* We simply unref and let item manage this in handler */
124         cp->display = sp_clippath_view_list_remove(cp->display, cp->display);
125     }
127     if (((SPObjectClass *) (parent_class))->release) {
128         ((SPObjectClass *) parent_class)->release(object);
129     }
132 static void
133 sp_clippath_set(SPObject *object, unsigned int key, gchar const *value)
135     SPClipPath *cp = SP_CLIPPATH(object);
137     switch (key) {
138         case SP_ATTR_CLIPPATHUNITS:
139             cp->clipPathUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
140             cp->clipPathUnits_set = FALSE;
141             if (value) {
142                 if (!strcmp(value, "userSpaceOnUse")) {
143                     cp->clipPathUnits_set = TRUE;
144                 } else if (!strcmp(value, "objectBoundingBox")) {
145                     cp->clipPathUnits = SP_CONTENT_UNITS_OBJECTBOUNDINGBOX;
146                     cp->clipPathUnits_set = TRUE;
147                 }
148             }
149             object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
150             break;
151         default:
152             if (((SPObjectClass *) parent_class)->set)
153                 ((SPObjectClass *) parent_class)->set(object, key, value);
154             break;
155     }
158 static void
159 sp_clippath_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
161     /* Invoke SPObjectGroup implementation */
162     ((SPObjectClass *) (parent_class))->child_added(object, child, ref);
164     /* Show new object */
165     SPObject *ochild = SP_OBJECT_DOCUMENT(object)->getObjectByRepr(child);
166     if (SP_IS_ITEM(ochild)) {
167         SPClipPath *cp = SP_CLIPPATH(object);
168         for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
169             NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(ochild),
170                                                   NR_ARENA_ITEM_ARENA(v->arenaitem),
171                                                   v->key,
172                                                   SP_ITEM_REFERENCE_FLAGS);
173             if (ac) {
174                 nr_arena_item_add_child(v->arenaitem, ac, NULL);
175             }
176         }
177     }
180 static void
181 sp_clippath_update(SPObject *object, SPCtx *ctx, guint flags)
183     if (flags & SP_OBJECT_MODIFIED_FLAG) {
184         flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
185     }
187     flags &= SP_OBJECT_MODIFIED_CASCADE;
189     SPObjectGroup *og = SP_OBJECTGROUP(object);
190     GSList *l = NULL;
191     for (SPObject *child = sp_object_first_child(SP_OBJECT(og)); child != NULL; child = SP_OBJECT_NEXT(child)) {
192         g_object_ref(G_OBJECT(child));
193         l = g_slist_prepend(l, child);
194     }
195     l = g_slist_reverse(l);
196     while (l) {
197         SPObject *child = SP_OBJECT(l->data);
198         l = g_slist_remove(l, child);
199         if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
200             child->updateDisplay(ctx, flags);
201         }
202         g_object_unref(G_OBJECT(child));
203     }
205     SPClipPath *cp = SP_CLIPPATH(object);
206     for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
207         if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
208             Geom::Matrix t(Geom::Scale(v->bbox.x1 - v->bbox.x0, v->bbox.y1 - v->bbox.y0));
209             t[4] = v->bbox.x0;
210             t[5] = v->bbox.y0;
211             nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), &t);
212         } else {
213             nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), NULL);
214         }
215     }
218 static void
219 sp_clippath_modified(SPObject *object, guint flags)
221     if (flags & SP_OBJECT_MODIFIED_FLAG) {
222         flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
223     }
225     flags &= SP_OBJECT_MODIFIED_CASCADE;
227     SPObjectGroup *og = SP_OBJECTGROUP(object);
228     GSList *l = NULL;
229     for (SPObject *child = sp_object_first_child(SP_OBJECT(og)); child != NULL; child = SP_OBJECT_NEXT(child)) {
230         g_object_ref(G_OBJECT(child));
231         l = g_slist_prepend(l, child);
232     }
233     l = g_slist_reverse(l);
234     while (l) {
235         SPObject *child = SP_OBJECT(l->data);
236         l = g_slist_remove(l, child);
237         if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
238             child->emitModified(flags);
239         }
240         g_object_unref(G_OBJECT(child));
241     }
244 static Inkscape::XML::Node *
245 sp_clippath_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
247     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
248         repr = xml_doc->createElement("svg:clipPath");
249     }
251     if (((SPObjectClass *) (parent_class))->write)
252         ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
254     return repr;
257 NRArenaItem *
258 sp_clippath_show(SPClipPath *cp, NRArena *arena, unsigned int key)
260     g_return_val_if_fail(cp != NULL, NULL);
261     g_return_val_if_fail(SP_IS_CLIPPATH(cp), NULL);
262     g_return_val_if_fail(arena != NULL, NULL);
263     g_return_val_if_fail(NR_IS_ARENA(arena), NULL);
265     NRArenaItem *ai = NRArenaGroup::create(arena);
266     cp->display = sp_clippath_view_new_prepend(cp->display, key, ai);
268     for (SPObject *child = sp_object_first_child(SP_OBJECT(cp)) ; child != NULL; child = SP_OBJECT_NEXT(child)) {
269         if (SP_IS_ITEM(child)) {
270             NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(child), arena, key, SP_ITEM_REFERENCE_FLAGS);
271             if (ac) {
272                 /* The order is not important in clippath */
273                 nr_arena_item_add_child(ai, ac, NULL);
274             }
275         }
276     }
278     if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
279         Geom::Matrix t(Geom::Scale(cp->display->bbox.x1 - cp->display->bbox.x0, cp->display->bbox.y1 - cp->display->bbox.y0));
280         t[4] = cp->display->bbox.x0;
281         t[5] = cp->display->bbox.y0;
282         nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), &t);
283     }
285     return ai;
288 void
289 sp_clippath_hide(SPClipPath *cp, unsigned int key)
291     g_return_if_fail(cp != NULL);
292     g_return_if_fail(SP_IS_CLIPPATH(cp));
294     for (SPObject *child = sp_object_first_child(SP_OBJECT(cp)) ; child != NULL; child = SP_OBJECT_NEXT(child)) {
295         if (SP_IS_ITEM(child)) {
296             sp_item_invoke_hide(SP_ITEM(child), key);
297         }
298     }
300     for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
301         if (v->key == key) {
302             /* We simply unref and let item to manage this in handler */
303             cp->display = sp_clippath_view_list_remove(cp->display, v);
304             return;
305         }
306     }
308     g_assert_not_reached();
311 void
312 sp_clippath_set_bbox(SPClipPath *cp, unsigned int key, NRRect *bbox)
314     for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
315         if (v->key == key) {
316             if (!NR_DF_TEST_CLOSE(v->bbox.x0, bbox->x0, NR_EPSILON) ||
317                 !NR_DF_TEST_CLOSE(v->bbox.y0, bbox->y0, NR_EPSILON) ||
318                 !NR_DF_TEST_CLOSE(v->bbox.x1, bbox->x1, NR_EPSILON) ||
319                 !NR_DF_TEST_CLOSE(v->bbox.y1, bbox->y1, NR_EPSILON)) {
320                 v->bbox = *bbox;
321             }
322             break;
323         }
324     }
327 void
328 sp_clippath_get_bbox(SPClipPath *cp, NRRect *bbox, Geom::Matrix const &transform, unsigned const /*flags*/)
330     SPObject *i;
331     for (i = sp_object_first_child(SP_OBJECT(cp)); i && !SP_IS_ITEM(i); i = SP_OBJECT_NEXT(i)){};
332     if (!i) return;
334     sp_item_invoke_bbox_full(SP_ITEM(i), bbox, Geom::Matrix(SP_ITEM(i)->transform) * transform, SPItem::GEOMETRIC_BBOX, FALSE);
335     SPObject *i_start = i;
337     while (i != NULL) {
338         if (i != i_start) {
339             NRRect i_box;
340             sp_item_invoke_bbox_full(SP_ITEM(i), &i_box, Geom::Matrix(SP_ITEM(i)->transform) * transform, SPItem::GEOMETRIC_BBOX, FALSE);
341             nr_rect_d_union (bbox, bbox, &i_box);
342         }
343         i = SP_OBJECT_NEXT(i);
344         for (; i && !SP_IS_ITEM(i); i = SP_OBJECT_NEXT(i)){};
345     }
348 /* ClipPath views */
350 SPClipPathView *
351 sp_clippath_view_new_prepend(SPClipPathView *list, unsigned int key, NRArenaItem *arenaitem)
353     SPClipPathView *new_path_view = g_new(SPClipPathView, 1);
355     new_path_view->next = list;
356     new_path_view->key = key;
357     new_path_view->arenaitem = nr_arena_item_ref(arenaitem);
358     new_path_view->bbox.x0 = new_path_view->bbox.x1 = 0.0;
359     new_path_view->bbox.y0 = new_path_view->bbox.y1 = 0.0;
361     return new_path_view;
364 SPClipPathView *
365 sp_clippath_view_list_remove(SPClipPathView *list, SPClipPathView *view)
367     if (view == list) {
368         list = list->next;
369     } else {
370         SPClipPathView *prev;
371         prev = list;
372         while (prev->next != view) prev = prev->next;
373         prev->next = view->next;
374     }
376     nr_arena_item_unref(view->arenaitem);
377     g_free(view);
379     return list;
382 // Create a mask element (using passed elements), add it to <defs>
383 const gchar *
384 sp_clippath_create (GSList *reprs, SPDocument *document, Geom::Matrix const* applyTransform)
386     Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
388     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
389     Inkscape::XML::Node *repr = xml_doc->createElement("svg:clipPath");
390     repr->setAttribute("clipPathUnits", "userSpaceOnUse");
392     defsrepr->appendChild(repr);
393     const gchar *id = repr->attribute("id");
394     SPObject *clip_path_object = document->getObjectById(id);
396     for (GSList *it = reprs; it != NULL; it = it->next) {
397         Inkscape::XML::Node *node = (Inkscape::XML::Node *)(it->data);
398         SPItem *item = SP_ITEM(clip_path_object->appendChildRepr(node));
400         if (NULL != applyTransform) {
401             Geom::Matrix transform (item->transform);
402             transform *= (*applyTransform);
403             sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
404         }
405     }
407     Inkscape::GC::release(repr);
408     return id;
411 /*
412   Local Variables:
413   mode:c++
414   c-file-style:"stroustrup"
415   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
416   indent-tabs-mode:nil
417   fill-column:99
418   End:
419 */
420 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :