f56564cbee6a4712212c94d42084e87ab6184fb7
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::Document *doc, 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 NR::Matrix t(NR::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 }
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::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
246 {
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;
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 NR::Matrix t(NR::scale(cp->display->bbox.x1 - cp->display->bbox.x0, cp->display->bbox.y1 - cp->display->bbox.y0));
281 t[4] = cp->display->bbox.x0;
282 t[5] = cp->display->bbox.y0;
283 nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), &t);
284 }
286 return ai;
287 }
289 void
290 sp_clippath_hide(SPClipPath *cp, unsigned int key)
291 {
292 g_return_if_fail(cp != NULL);
293 g_return_if_fail(SP_IS_CLIPPATH(cp));
295 for (SPObject *child = sp_object_first_child(SP_OBJECT(cp)) ; child != NULL; child = SP_OBJECT_NEXT(child)) {
296 if (SP_IS_ITEM(child)) {
297 sp_item_invoke_hide(SP_ITEM(child), key);
298 }
299 }
301 for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
302 if (v->key == key) {
303 /* We simply unref and let item to manage this in handler */
304 cp->display = sp_clippath_view_list_remove(cp->display, v);
305 return;
306 }
307 }
309 g_assert_not_reached();
310 }
312 void
313 sp_clippath_set_bbox(SPClipPath *cp, unsigned int key, NRRect *bbox)
314 {
315 for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
316 if (v->key == key) {
317 if (!NR_DF_TEST_CLOSE(v->bbox.x0, bbox->x0, NR_EPSILON) ||
318 !NR_DF_TEST_CLOSE(v->bbox.y0, bbox->y0, NR_EPSILON) ||
319 !NR_DF_TEST_CLOSE(v->bbox.x1, bbox->x1, NR_EPSILON) ||
320 !NR_DF_TEST_CLOSE(v->bbox.y1, bbox->y1, NR_EPSILON)) {
321 v->bbox = *bbox;
322 }
323 break;
324 }
325 }
326 }
328 void
329 sp_clippath_get_bbox(SPClipPath *cp, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
330 {
331 SPObject *i;
332 for (i = sp_object_first_child(SP_OBJECT(cp)); i && !SP_IS_ITEM(i); i = SP_OBJECT_NEXT(i));
333 if (!i) return;
335 sp_item_invoke_bbox_full(SP_ITEM(i), bbox, NR::Matrix(SP_ITEM(i)->transform) * transform, SPItem::GEOMETRIC_BBOX, FALSE);
336 SPObject *i_start = i;
338 while (i != NULL) {
339 if (i != i_start) {
340 NRRect i_box;
341 sp_item_invoke_bbox_full(SP_ITEM(i), &i_box, NR::Matrix(SP_ITEM(i)->transform) * transform, SPItem::GEOMETRIC_BBOX, FALSE);
342 nr_rect_d_union (bbox, bbox, &i_box);
343 }
344 i = SP_OBJECT_NEXT(i);
345 for (; i && !SP_IS_ITEM(i); i = SP_OBJECT_NEXT(i));
346 }
347 }
349 /* ClipPath views */
351 SPClipPathView *
352 sp_clippath_view_new_prepend(SPClipPathView *list, unsigned int key, NRArenaItem *arenaitem)
353 {
354 SPClipPathView *new_path_view = g_new(SPClipPathView, 1);
356 new_path_view->next = list;
357 new_path_view->key = key;
358 new_path_view->arenaitem = nr_arena_item_ref(arenaitem);
359 new_path_view->bbox.x0 = new_path_view->bbox.x1 = 0.0;
360 new_path_view->bbox.y0 = new_path_view->bbox.y1 = 0.0;
362 return new_path_view;
363 }
365 SPClipPathView *
366 sp_clippath_view_list_remove(SPClipPathView *list, SPClipPathView *view)
367 {
368 if (view == list) {
369 list = list->next;
370 } else {
371 SPClipPathView *prev;
372 prev = list;
373 while (prev->next != view) prev = prev->next;
374 prev->next = view->next;
375 }
377 nr_arena_item_unref(view->arenaitem);
378 g_free(view);
380 return list;
381 }
383 // Create a mask element (using passed elements), add it to <defs>
384 const gchar *
385 sp_clippath_create (GSList *reprs, SPDocument *document, NR::Matrix const* applyTransform)
386 {
387 Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
389 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
390 Inkscape::XML::Node *repr = xml_doc->createElement("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 :