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::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)
102 {
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);
110 }
112 static void
113 sp_clippath_release(SPObject * object)
114 {
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 }
129 }
131 static void
132 sp_clippath_set(SPObject *object, unsigned int key, gchar const *value)
133 {
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 }
155 }
157 static void
158 sp_clippath_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
159 {
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 nr_arena_item_unref(ac);
175 }
176 }
177 }
178 }
180 static void
181 sp_clippath_update(SPObject *object, SPCtx *ctx, guint flags)
182 {
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 NRMatrix t;
209 nr_matrix_set_scale(&t, v->bbox.x1 - v->bbox.x0, v->bbox.y1 - v->bbox.y0);
210 t.c[4] = v->bbox.x0;
211 t.c[5] = v->bbox.y0;
212 nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), &t);
213 } else {
214 nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), NULL);
215 }
216 }
217 }
219 static void
220 sp_clippath_modified(SPObject *object, guint flags)
221 {
222 if (flags & SP_OBJECT_MODIFIED_FLAG) {
223 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
224 }
226 flags &= SP_OBJECT_MODIFIED_CASCADE;
228 SPObjectGroup *og = SP_OBJECTGROUP(object);
229 GSList *l = NULL;
230 for (SPObject *child = sp_object_first_child(SP_OBJECT(og)); child != NULL; child = SP_OBJECT_NEXT(child)) {
231 g_object_ref(G_OBJECT(child));
232 l = g_slist_prepend(l, child);
233 }
234 l = g_slist_reverse(l);
235 while (l) {
236 SPObject *child = SP_OBJECT(l->data);
237 l = g_slist_remove(l, child);
238 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
239 child->emitModified(flags);
240 }
241 g_object_unref(G_OBJECT(child));
242 }
243 }
245 static Inkscape::XML::Node *
246 sp_clippath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
247 {
248 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
249 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
250 repr = xml_doc->createElement("svg:clipPath");
251 }
253 if (((SPObjectClass *) (parent_class))->write)
254 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
256 return repr;
257 }
259 NRArenaItem *
260 sp_clippath_show(SPClipPath *cp, NRArena *arena, unsigned int key)
261 {
262 g_return_val_if_fail(cp != NULL, NULL);
263 g_return_val_if_fail(SP_IS_CLIPPATH(cp), NULL);
264 g_return_val_if_fail(arena != NULL, NULL);
265 g_return_val_if_fail(NR_IS_ARENA(arena), NULL);
267 NRArenaItem *ai = NRArenaGroup::create(arena);
268 cp->display = sp_clippath_view_new_prepend(cp->display, key, ai);
270 for (SPObject *child = sp_object_first_child(SP_OBJECT(cp)) ; child != NULL; child = SP_OBJECT_NEXT(child)) {
271 if (SP_IS_ITEM(child)) {
272 NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(child), arena, key, SP_ITEM_REFERENCE_FLAGS);
273 if (ac) {
274 /* The order is not important in clippath */
275 nr_arena_item_add_child(ai, ac, NULL);
276 nr_arena_item_unref(ac);
277 }
278 }
279 }
281 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
282 NRMatrix t;
283 nr_matrix_set_scale(&t, cp->display->bbox.x1 - cp->display->bbox.x0, cp->display->bbox.y1 - cp->display->bbox.y0);
284 t.c[4] = cp->display->bbox.x0;
285 t.c[5] = cp->display->bbox.y0;
286 nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), &t);
287 }
289 return ai;
290 }
292 void
293 sp_clippath_hide(SPClipPath *cp, unsigned int key)
294 {
295 g_return_if_fail(cp != NULL);
296 g_return_if_fail(SP_IS_CLIPPATH(cp));
298 for (SPObject *child = sp_object_first_child(SP_OBJECT(cp)) ; child != NULL; child = SP_OBJECT_NEXT(child)) {
299 if (SP_IS_ITEM(child)) {
300 sp_item_invoke_hide(SP_ITEM(child), key);
301 }
302 }
304 for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
305 if (v->key == key) {
306 /* We simply unref and let item to manage this in handler */
307 cp->display = sp_clippath_view_list_remove(cp->display, v);
308 return;
309 }
310 }
312 g_assert_not_reached();
313 }
315 void
316 sp_clippath_set_bbox(SPClipPath *cp, unsigned int key, NRRect *bbox)
317 {
318 for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
319 if (v->key == key) {
320 if (!NR_DF_TEST_CLOSE(v->bbox.x0, bbox->x0, NR_EPSILON) ||
321 !NR_DF_TEST_CLOSE(v->bbox.y0, bbox->y0, NR_EPSILON) ||
322 !NR_DF_TEST_CLOSE(v->bbox.x1, bbox->x1, NR_EPSILON) ||
323 !NR_DF_TEST_CLOSE(v->bbox.y1, bbox->y1, NR_EPSILON)) {
324 v->bbox = *bbox;
325 }
326 break;
327 }
328 }
329 }
331 void
332 sp_clippath_get_bbox(SPClipPath *cp, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
333 {
334 SPObject *i;
335 for (i = sp_object_first_child(SP_OBJECT(cp)); i && !SP_IS_ITEM(i); i = SP_OBJECT_NEXT(i));
336 if (!i) return;
338 sp_item_invoke_bbox_full(SP_ITEM(i), bbox, NR::Matrix(SP_ITEM(i)->transform) * transform, SPItem::GEOMETRIC_BBOX, FALSE);
339 SPObject *i_start = i;
341 while (i != NULL) {
342 if (i != i_start) {
343 NRRect i_box;
344 sp_item_invoke_bbox_full(SP_ITEM(i), &i_box, NR::Matrix(SP_ITEM(i)->transform) * transform, SPItem::GEOMETRIC_BBOX, FALSE);
345 nr_rect_d_union (bbox, bbox, &i_box);
346 }
347 i = SP_OBJECT_NEXT(i);
348 for (; i && !SP_IS_ITEM(i); i = SP_OBJECT_NEXT(i));
349 }
350 }
352 /* ClipPath views */
354 SPClipPathView *
355 sp_clippath_view_new_prepend(SPClipPathView *list, unsigned int key, NRArenaItem *arenaitem)
356 {
357 SPClipPathView *new_path_view = g_new(SPClipPathView, 1);
359 new_path_view->next = list;
360 new_path_view->key = key;
361 new_path_view->arenaitem = nr_arena_item_ref(arenaitem);
362 new_path_view->bbox.x0 = new_path_view->bbox.x1 = 0.0;
363 new_path_view->bbox.y0 = new_path_view->bbox.y1 = 0.0;
365 return new_path_view;
366 }
368 SPClipPathView *
369 sp_clippath_view_list_remove(SPClipPathView *list, SPClipPathView *view)
370 {
371 if (view == list) {
372 list = list->next;
373 } else {
374 SPClipPathView *prev;
375 prev = list;
376 while (prev->next != view) prev = prev->next;
377 prev->next = view->next;
378 }
380 nr_arena_item_unref(view->arenaitem);
381 g_free(view);
383 return list;
384 }
386 // Create a mask element (using passed elements), add it to <defs>
387 const gchar *
388 sp_clippath_create (GSList *reprs, SPDocument *document, NR::Matrix const* applyTransform)
389 {
390 Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
392 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
393 Inkscape::XML::Node *repr = xml_doc->createElement("svg:clipPath");
394 repr->setAttribute("clipPathUnits", "userSpaceOnUse");
396 defsrepr->appendChild(repr);
397 const gchar *id = repr->attribute("id");
398 SPObject *clip_path_object = document->getObjectById(id);
400 for (GSList *it = reprs; it != NULL; it = it->next) {
401 Inkscape::XML::Node *node = (Inkscape::XML::Node *)(it->data);
402 SPItem *item = SP_ITEM(clip_path_object->appendChildRepr(node));
404 if (NULL != applyTransform) {
405 NR::Matrix transform (item->transform);
406 transform *= (*applyTransform);
407 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
408 }
409 }
411 Inkscape::GC::release(repr);
412 return id;
413 }
415 /*
416 Local Variables:
417 mode:c++
418 c-file-style:"stroustrup"
419 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
420 indent-tabs-mode:nil
421 fill-column:99
422 End:
423 */
424 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :