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 */
17 #include "display/nr-arena.h"
18 #include "display/nr-arena-group.h"
19 #include "xml/repr.h"
21 #include "enums.h"
22 #include "attributes.h"
23 #include "document.h"
24 #include "document-private.h"
25 #include "sp-item.h"
27 #include "libnr/nr-matrix-ops.h"
29 #include "sp-clippath.h"
31 struct SPClipPathView {
32 SPClipPathView *next;
33 unsigned int key;
34 NRArenaItem *arenaitem;
35 NRRect bbox;
36 };
38 static void sp_clippath_class_init(SPClipPathClass *klass);
39 static void sp_clippath_init(SPClipPath *clippath);
41 static void sp_clippath_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
42 static void sp_clippath_release(SPObject * object);
43 static void sp_clippath_set(SPObject *object, unsigned int key, gchar const *value);
44 static void sp_clippath_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
45 static void sp_clippath_update(SPObject *object, SPCtx *ctx, guint flags);
46 static void sp_clippath_modified(SPObject *object, guint flags);
47 static Inkscape::XML::Node *sp_clippath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
49 SPClipPathView *sp_clippath_view_new_prepend(SPClipPathView *list, unsigned int key, NRArenaItem *arenaitem);
50 SPClipPathView *sp_clippath_view_list_remove(SPClipPathView *list, SPClipPathView *view);
52 static SPObjectGroupClass *parent_class;
54 GType
55 sp_clippath_get_type(void)
56 {
57 static GType type = 0;
58 if (!type) {
59 GTypeInfo info = {
60 sizeof(SPClipPathClass),
61 NULL, NULL,
62 (GClassInitFunc) sp_clippath_class_init,
63 NULL, NULL,
64 sizeof(SPClipPath),
65 16,
66 (GInstanceInitFunc) sp_clippath_init,
67 NULL, /* value_table */
68 };
69 type = g_type_register_static(SP_TYPE_OBJECTGROUP, "SPClipPath", &info, (GTypeFlags)0);
70 }
71 return type;
72 }
74 static void
75 sp_clippath_class_init(SPClipPathClass *klass)
76 {
77 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
79 parent_class = (SPObjectGroupClass*)g_type_class_ref(SP_TYPE_OBJECTGROUP);
81 sp_object_class->build = sp_clippath_build;
82 sp_object_class->release = sp_clippath_release;
83 sp_object_class->set = sp_clippath_set;
84 sp_object_class->child_added = sp_clippath_child_added;
85 sp_object_class->update = sp_clippath_update;
86 sp_object_class->modified = sp_clippath_modified;
87 sp_object_class->write = sp_clippath_write;
88 }
90 static void
91 sp_clippath_init(SPClipPath *cp)
92 {
93 cp->clipPathUnits_set = FALSE;
94 cp->clipPathUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
96 cp->display = NULL;
97 }
99 static void
100 sp_clippath_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
101 {
102 if (((SPObjectClass *) parent_class)->build)
103 ((SPObjectClass *) parent_class)->build(object, document, repr);
105 sp_object_read_attr(object, "clipPathUnits");
107 /* Register ourselves */
108 sp_document_add_resource(document, "clipPath", object);
109 }
111 static void
112 sp_clippath_release(SPObject * object)
113 {
114 if (SP_OBJECT_DOCUMENT(object)) {
115 /* Unregister ourselves */
116 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "clipPath", object);
117 }
119 SPClipPath *cp = SP_CLIPPATH(object);
120 while (cp->display) {
121 /* We simply unref and let item manage this in handler */
122 cp->display = sp_clippath_view_list_remove(cp->display, cp->display);
123 }
125 if (((SPObjectClass *) (parent_class))->release) {
126 ((SPObjectClass *) parent_class)->release(object);
127 }
128 }
130 static void
131 sp_clippath_set(SPObject *object, unsigned int key, gchar const *value)
132 {
133 SPClipPath *cp = SP_CLIPPATH(object);
135 switch (key) {
136 case SP_ATTR_CLIPPATHUNITS:
137 cp->clipPathUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
138 cp->clipPathUnits_set = FALSE;
139 if (value) {
140 if (!strcmp(value, "userSpaceOnUse")) {
141 cp->clipPathUnits_set = TRUE;
142 } else if (!strcmp(value, "objectBoundingBox")) {
143 cp->clipPathUnits = SP_CONTENT_UNITS_OBJECTBOUNDINGBOX;
144 cp->clipPathUnits_set = TRUE;
145 }
146 }
147 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
148 break;
149 default:
150 if (((SPObjectClass *) parent_class)->set)
151 ((SPObjectClass *) parent_class)->set(object, key, value);
152 break;
153 }
154 }
156 static void
157 sp_clippath_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
158 {
159 /* Invoke SPObjectGroup implementation */
160 ((SPObjectClass *) (parent_class))->child_added(object, child, ref);
162 /* Show new object */
163 SPObject *ochild = SP_OBJECT_DOCUMENT(object)->getObjectByRepr(child);
164 if (SP_IS_ITEM(ochild)) {
165 SPClipPath *cp = SP_CLIPPATH(object);
166 for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
167 NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(ochild),
168 NR_ARENA_ITEM_ARENA(v->arenaitem),
169 v->key,
170 SP_ITEM_REFERENCE_FLAGS);
171 if (ac) {
172 nr_arena_item_add_child(v->arenaitem, ac, NULL);
173 nr_arena_item_unref(ac);
174 }
175 }
176 }
177 }
179 static void
180 sp_clippath_update(SPObject *object, SPCtx *ctx, guint flags)
181 {
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 NRMatrix t;
208 nr_matrix_set_scale(&t, v->bbox.x1 - v->bbox.x0, v->bbox.y1 - v->bbox.y0);
209 t.c[4] = v->bbox.x0;
210 t.c[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 }
216 }
218 static void
219 sp_clippath_modified(SPObject *object, guint flags)
220 {
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 }
242 }
244 static Inkscape::XML::Node *
245 sp_clippath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
246 {
247 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
248 repr = sp_repr_new("svg:clipPath");
249 }
251 if (((SPObjectClass *) (parent_class))->write)
252 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
254 return repr;
255 }
257 NRArenaItem *
258 sp_clippath_show(SPClipPath *cp, NRArena *arena, unsigned int key)
259 {
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 nr_arena_item_unref(ac);
275 }
276 }
277 }
279 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
280 NRMatrix t;
281 nr_matrix_set_scale(&t, cp->display->bbox.x1 - cp->display->bbox.x0, cp->display->bbox.y1 - cp->display->bbox.y0);
282 t.c[4] = cp->display->bbox.x0;
283 t.c[5] = cp->display->bbox.y0;
284 nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), &t);
285 }
287 return ai;
288 }
290 void
291 sp_clippath_hide(SPClipPath *cp, unsigned int key)
292 {
293 g_return_if_fail(cp != NULL);
294 g_return_if_fail(SP_IS_CLIPPATH(cp));
296 for (SPObject *child = sp_object_first_child(SP_OBJECT(cp)) ; child != NULL; child = SP_OBJECT_NEXT(child)) {
297 if (SP_IS_ITEM(child)) {
298 sp_item_invoke_hide(SP_ITEM(child), key);
299 }
300 }
302 for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
303 if (v->key == key) {
304 /* We simply unref and let item to manage this in handler */
305 cp->display = sp_clippath_view_list_remove(cp->display, v);
306 return;
307 }
308 }
310 g_assert_not_reached();
311 }
313 void
314 sp_clippath_set_bbox(SPClipPath *cp, unsigned int key, NRRect *bbox)
315 {
316 for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
317 if (v->key == key) {
318 if (!NR_DF_TEST_CLOSE(v->bbox.x0, bbox->x0, NR_EPSILON) ||
319 !NR_DF_TEST_CLOSE(v->bbox.y0, bbox->y0, NR_EPSILON) ||
320 !NR_DF_TEST_CLOSE(v->bbox.x1, bbox->x1, NR_EPSILON) ||
321 !NR_DF_TEST_CLOSE(v->bbox.y1, bbox->y1, NR_EPSILON)) {
322 v->bbox = *bbox;
323 }
324 break;
325 }
326 }
327 }
329 void
330 sp_clippath_get_bbox(SPClipPath *cp, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
331 {
332 SPObject *i;
333 for (i = sp_object_first_child(SP_OBJECT(cp)); i && !SP_IS_ITEM(i); i = SP_OBJECT_NEXT(i));
334 if (!i) return;
336 sp_item_invoke_bbox_full(SP_ITEM(i), bbox, NR::Matrix(SP_ITEM(i)->transform) * transform, flags, FALSE);
337 SPObject *i_start = i;
339 while (i != NULL) {
340 if (i != i_start) {
341 NRRect i_box;
342 sp_item_invoke_bbox_full(SP_ITEM(i), &i_box, NR::Matrix(SP_ITEM(i)->transform) * transform, flags, FALSE);
343 nr_rect_d_union (bbox, bbox, &i_box);
344 }
345 i = SP_OBJECT_NEXT(i);
346 for (; i && !SP_IS_ITEM(i); i = SP_OBJECT_NEXT(i));
347 }
348 }
350 /* ClipPath views */
352 SPClipPathView *
353 sp_clippath_view_new_prepend(SPClipPathView *list, unsigned int key, NRArenaItem *arenaitem)
354 {
355 SPClipPathView *new_path_view = g_new(SPClipPathView, 1);
357 new_path_view->next = list;
358 new_path_view->key = key;
359 new_path_view->arenaitem = nr_arena_item_ref(arenaitem);
360 new_path_view->bbox.x0 = new_path_view->bbox.x1 = 0.0;
361 new_path_view->bbox.y0 = new_path_view->bbox.y1 = 0.0;
363 return new_path_view;
364 }
366 SPClipPathView *
367 sp_clippath_view_list_remove(SPClipPathView *list, SPClipPathView *view)
368 {
369 if (view == list) {
370 list = list->next;
371 } else {
372 SPClipPathView *prev;
373 prev = list;
374 while (prev->next != view) prev = prev->next;
375 prev->next = view->next;
376 }
378 nr_arena_item_unref(view->arenaitem);
379 g_free(view);
381 return list;
382 }
384 // Create a mask element (using passed elements), add it to <defs>
385 const gchar *
386 sp_clippath_create (GSList *reprs, SPDocument *document, NR::Matrix const* applyTransform)
387 {
388 Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
390 Inkscape::XML::Node *repr = sp_repr_new ("svg:clipPath");
391 repr->setAttribute("clipPathUnits", "userSpaceOnUse");
393 defsrepr->appendChild(repr);
394 const gchar *id = repr->attribute("id");
395 SPObject *clip_path_object = document->getObjectById(id);
397 for (GSList *it = reprs; it != NULL; it = it->next) {
398 Inkscape::XML::Node *node = (Inkscape::XML::Node *)(it->data);
399 SPItem *item = SP_ITEM(clip_path_object->appendChildRepr(node));
401 if (NULL != applyTransform) {
402 NR::Matrix transform (item->transform);
403 transform *= (*applyTransform);
404 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
405 }
406 }
408 Inkscape::GC::release(repr);
409 return id;
410 }
412 /*
413 Local Variables:
414 mode:c++
415 c-file-style:"stroustrup"
416 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
417 indent-tabs-mode:nil
418 fill-column:99
419 End:
420 */
421 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :