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 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
249 repr = xml_doc->createElement("svg:clipPath");
250 }
252 if (((SPObjectClass *) (parent_class))->write)
253 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
255 return repr;
256 }
258 NRArenaItem *
259 sp_clippath_show(SPClipPath *cp, NRArena *arena, unsigned int key)
260 {
261 g_return_val_if_fail(cp != NULL, NULL);
262 g_return_val_if_fail(SP_IS_CLIPPATH(cp), NULL);
263 g_return_val_if_fail(arena != NULL, NULL);
264 g_return_val_if_fail(NR_IS_ARENA(arena), NULL);
266 NRArenaItem *ai = NRArenaGroup::create(arena);
267 cp->display = sp_clippath_view_new_prepend(cp->display, key, ai);
269 for (SPObject *child = sp_object_first_child(SP_OBJECT(cp)) ; child != NULL; child = SP_OBJECT_NEXT(child)) {
270 if (SP_IS_ITEM(child)) {
271 NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(child), arena, key, SP_ITEM_REFERENCE_FLAGS);
272 if (ac) {
273 /* The order is not important in clippath */
274 nr_arena_item_add_child(ai, ac, NULL);
275 nr_arena_item_unref(ac);
276 }
277 }
278 }
280 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
281 NRMatrix t;
282 nr_matrix_set_scale(&t, cp->display->bbox.x1 - cp->display->bbox.x0, cp->display->bbox.y1 - cp->display->bbox.y0);
283 t.c[4] = cp->display->bbox.x0;
284 t.c[5] = cp->display->bbox.y0;
285 nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), &t);
286 }
288 return ai;
289 }
291 void
292 sp_clippath_hide(SPClipPath *cp, unsigned int key)
293 {
294 g_return_if_fail(cp != NULL);
295 g_return_if_fail(SP_IS_CLIPPATH(cp));
297 for (SPObject *child = sp_object_first_child(SP_OBJECT(cp)) ; child != NULL; child = SP_OBJECT_NEXT(child)) {
298 if (SP_IS_ITEM(child)) {
299 sp_item_invoke_hide(SP_ITEM(child), key);
300 }
301 }
303 for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
304 if (v->key == key) {
305 /* We simply unref and let item to manage this in handler */
306 cp->display = sp_clippath_view_list_remove(cp->display, v);
307 return;
308 }
309 }
311 g_assert_not_reached();
312 }
314 void
315 sp_clippath_set_bbox(SPClipPath *cp, unsigned int key, NRRect *bbox)
316 {
317 for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
318 if (v->key == key) {
319 if (!NR_DF_TEST_CLOSE(v->bbox.x0, bbox->x0, NR_EPSILON) ||
320 !NR_DF_TEST_CLOSE(v->bbox.y0, bbox->y0, NR_EPSILON) ||
321 !NR_DF_TEST_CLOSE(v->bbox.x1, bbox->x1, NR_EPSILON) ||
322 !NR_DF_TEST_CLOSE(v->bbox.y1, bbox->y1, NR_EPSILON)) {
323 v->bbox = *bbox;
324 }
325 break;
326 }
327 }
328 }
330 void
331 sp_clippath_get_bbox(SPClipPath *cp, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
332 {
333 SPObject *i;
334 for (i = sp_object_first_child(SP_OBJECT(cp)); i && !SP_IS_ITEM(i); i = SP_OBJECT_NEXT(i));
335 if (!i) return;
337 sp_item_invoke_bbox_full(SP_ITEM(i), bbox, NR::Matrix(SP_ITEM(i)->transform) * transform, flags, FALSE);
338 SPObject *i_start = i;
340 while (i != NULL) {
341 if (i != i_start) {
342 NRRect i_box;
343 sp_item_invoke_bbox_full(SP_ITEM(i), &i_box, NR::Matrix(SP_ITEM(i)->transform) * transform, flags, FALSE);
344 nr_rect_d_union (bbox, bbox, &i_box);
345 }
346 i = SP_OBJECT_NEXT(i);
347 for (; i && !SP_IS_ITEM(i); i = SP_OBJECT_NEXT(i));
348 }
349 }
351 /* ClipPath views */
353 SPClipPathView *
354 sp_clippath_view_new_prepend(SPClipPathView *list, unsigned int key, NRArenaItem *arenaitem)
355 {
356 SPClipPathView *new_path_view = g_new(SPClipPathView, 1);
358 new_path_view->next = list;
359 new_path_view->key = key;
360 new_path_view->arenaitem = nr_arena_item_ref(arenaitem);
361 new_path_view->bbox.x0 = new_path_view->bbox.x1 = 0.0;
362 new_path_view->bbox.y0 = new_path_view->bbox.y1 = 0.0;
364 return new_path_view;
365 }
367 SPClipPathView *
368 sp_clippath_view_list_remove(SPClipPathView *list, SPClipPathView *view)
369 {
370 if (view == list) {
371 list = list->next;
372 } else {
373 SPClipPathView *prev;
374 prev = list;
375 while (prev->next != view) prev = prev->next;
376 prev->next = view->next;
377 }
379 nr_arena_item_unref(view->arenaitem);
380 g_free(view);
382 return list;
383 }
385 // Create a mask element (using passed elements), add it to <defs>
386 const gchar *
387 sp_clippath_create (GSList *reprs, SPDocument *document, NR::Matrix const* applyTransform)
388 {
389 Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
391 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
392 Inkscape::XML::Node *repr = xml_doc->createElement("svg:clipPath");
393 repr->setAttribute("clipPathUnits", "userSpaceOnUse");
395 defsrepr->appendChild(repr);
396 const gchar *id = repr->attribute("id");
397 SPObject *clip_path_object = document->getObjectById(id);
399 for (GSList *it = reprs; it != NULL; it = it->next) {
400 Inkscape::XML::Node *node = (Inkscape::XML::Node *)(it->data);
401 SPItem *item = SP_ITEM(clip_path_object->appendChildRepr(node));
403 if (NULL != applyTransform) {
404 NR::Matrix transform (item->transform);
405 transform *= (*applyTransform);
406 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
407 }
408 }
410 Inkscape::GC::release(repr);
411 return id;
412 }
414 /*
415 Local Variables:
416 mode:c++
417 c-file-style:"stroustrup"
418 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
419 indent-tabs-mode:nil
420 fill-column:99
421 End:
422 */
423 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :