Code

This is the first c++ification commit from me. It handles sp-line, sp-polyline, sp...
[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(ochild)->invoke_show(                                                  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             Geom::Matrix t(Geom::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(child)->invoke_show(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         Geom::Matrix t(Geom::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(child)->invoke_hide(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, Geom::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(i)->invoke_bbox_full( bbox, Geom::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(i)->invoke_bbox_full( &i_box, Geom::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, Geom::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             Geom::Matrix transform (item->transform);
401             transform *= (*applyTransform);
402             item->doWriteTransform(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 :