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 "sp-clippath.h"
29 struct SPClipPathView {
30 SPClipPathView *next;
31 unsigned int key;
32 NRArenaItem *arenaitem;
33 NRRect bbox;
34 };
36 static void sp_clippath_class_init(SPClipPathClass *klass);
37 static void sp_clippath_init(SPClipPath *clippath);
39 static void sp_clippath_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
40 static void sp_clippath_release(SPObject * object);
41 static void sp_clippath_set(SPObject *object, unsigned int key, gchar const *value);
42 static void sp_clippath_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
43 static void sp_clippath_update(SPObject *object, SPCtx *ctx, guint flags);
44 static void sp_clippath_modified(SPObject *object, guint flags);
45 static Inkscape::XML::Node *sp_clippath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
47 SPClipPathView *sp_clippath_view_new_prepend(SPClipPathView *list, unsigned int key, NRArenaItem *arenaitem);
48 SPClipPathView *sp_clippath_view_list_remove(SPClipPathView *list, SPClipPathView *view);
50 static SPObjectGroupClass *parent_class;
52 GType
53 sp_clippath_get_type(void)
54 {
55 static GType type = 0;
56 if (!type) {
57 GTypeInfo info = {
58 sizeof(SPClipPathClass),
59 NULL, NULL,
60 (GClassInitFunc) sp_clippath_class_init,
61 NULL, NULL,
62 sizeof(SPClipPath),
63 16,
64 (GInstanceInitFunc) sp_clippath_init,
65 NULL, /* value_table */
66 };
67 type = g_type_register_static(SP_TYPE_OBJECTGROUP, "SPClipPath", &info, (GTypeFlags)0);
68 }
69 return type;
70 }
72 static void
73 sp_clippath_class_init(SPClipPathClass *klass)
74 {
75 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
77 parent_class = (SPObjectGroupClass*)g_type_class_ref(SP_TYPE_OBJECTGROUP);
79 sp_object_class->build = sp_clippath_build;
80 sp_object_class->release = sp_clippath_release;
81 sp_object_class->set = sp_clippath_set;
82 sp_object_class->child_added = sp_clippath_child_added;
83 sp_object_class->update = sp_clippath_update;
84 sp_object_class->modified = sp_clippath_modified;
85 sp_object_class->write = sp_clippath_write;
86 }
88 static void
89 sp_clippath_init(SPClipPath *cp)
90 {
91 cp->clipPathUnits_set = FALSE;
92 cp->clipPathUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
94 cp->display = NULL;
95 }
97 static void
98 sp_clippath_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
99 {
100 if (((SPObjectClass *) parent_class)->build)
101 ((SPObjectClass *) parent_class)->build(object, document, repr);
103 sp_object_read_attr(object, "clipPathUnits");
105 /* Register ourselves */
106 sp_document_add_resource(document, "clipPath", object);
107 }
109 static void
110 sp_clippath_release(SPObject * object)
111 {
112 if (SP_OBJECT_DOCUMENT(object)) {
113 /* Unregister ourselves */
114 sp_document_remove_resource(SP_OBJECT_DOCUMENT(object), "clipPath", object);
115 }
117 SPClipPath *cp = SP_CLIPPATH(object);
118 while (cp->display) {
119 /* We simply unref and let item manage this in handler */
120 cp->display = sp_clippath_view_list_remove(cp->display, cp->display);
121 }
123 if (((SPObjectClass *) (parent_class))->release) {
124 ((SPObjectClass *) parent_class)->release(object);
125 }
126 }
128 static void
129 sp_clippath_set(SPObject *object, unsigned int key, gchar const *value)
130 {
131 SPClipPath *cp = SP_CLIPPATH(object);
133 switch (key) {
134 case SP_ATTR_CLIPPATHUNITS:
135 cp->clipPathUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
136 cp->clipPathUnits_set = FALSE;
137 if (value) {
138 if (!strcmp(value, "userSpaceOnUse")) {
139 cp->clipPathUnits_set = TRUE;
140 } else if (!strcmp(value, "objectBoundingBox")) {
141 cp->clipPathUnits = SP_CONTENT_UNITS_OBJECTBOUNDINGBOX;
142 cp->clipPathUnits_set = TRUE;
143 }
144 }
145 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
146 break;
147 default:
148 if (((SPObjectClass *) parent_class)->set)
149 ((SPObjectClass *) parent_class)->set(object, key, value);
150 break;
151 }
152 }
154 static void
155 sp_clippath_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
156 {
157 /* Invoke SPObjectGroup implementation */
158 ((SPObjectClass *) (parent_class))->child_added(object, child, ref);
160 /* Show new object */
161 SPObject *ochild = SP_OBJECT_DOCUMENT(object)->getObjectByRepr(child);
162 if (SP_IS_ITEM(ochild)) {
163 SPClipPath *cp = SP_CLIPPATH(object);
164 for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
165 NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(ochild),
166 NR_ARENA_ITEM_ARENA(v->arenaitem),
167 v->key,
168 SP_ITEM_REFERENCE_FLAGS);
169 if (ac) {
170 nr_arena_item_add_child(v->arenaitem, ac, NULL);
171 nr_arena_item_unref(ac);
172 }
173 }
174 }
175 }
177 static void
178 sp_clippath_update(SPObject *object, SPCtx *ctx, guint flags)
179 {
180 if (flags & SP_OBJECT_MODIFIED_FLAG) {
181 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
182 }
184 flags &= SP_OBJECT_MODIFIED_CASCADE;
186 SPObjectGroup *og = SP_OBJECTGROUP(object);
187 GSList *l = NULL;
188 for (SPObject *child = sp_object_first_child(SP_OBJECT(og)); child != NULL; child = SP_OBJECT_NEXT(child)) {
189 g_object_ref(G_OBJECT(child));
190 l = g_slist_prepend(l, child);
191 }
192 l = g_slist_reverse(l);
193 while (l) {
194 SPObject *child = SP_OBJECT(l->data);
195 l = g_slist_remove(l, child);
196 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
197 child->updateDisplay(ctx, flags);
198 }
199 g_object_unref(G_OBJECT(child));
200 }
202 SPClipPath *cp = SP_CLIPPATH(object);
203 for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
204 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
205 NRMatrix t;
206 nr_matrix_set_scale(&t, v->bbox.x1 - v->bbox.x0, v->bbox.y1 - v->bbox.y0);
207 t.c[4] = v->bbox.x0;
208 t.c[5] = v->bbox.y0;
209 nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), &t);
210 } else {
211 nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), NULL);
212 }
213 }
214 }
216 static void
217 sp_clippath_modified(SPObject *object, guint flags)
218 {
219 if (flags & SP_OBJECT_MODIFIED_FLAG) {
220 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
221 }
223 flags &= SP_OBJECT_MODIFIED_CASCADE;
225 SPObjectGroup *og = SP_OBJECTGROUP(object);
226 GSList *l = NULL;
227 for (SPObject *child = sp_object_first_child(SP_OBJECT(og)); child != NULL; child = SP_OBJECT_NEXT(child)) {
228 g_object_ref(G_OBJECT(child));
229 l = g_slist_prepend(l, child);
230 }
231 l = g_slist_reverse(l);
232 while (l) {
233 SPObject *child = SP_OBJECT(l->data);
234 l = g_slist_remove(l, child);
235 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
236 child->emitModified(flags);
237 }
238 g_object_unref(G_OBJECT(child));
239 }
240 }
242 static Inkscape::XML::Node *
243 sp_clippath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
244 {
245 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
246 repr = sp_repr_new("svg:clipPath");
247 }
249 if (((SPObjectClass *) (parent_class))->write)
250 ((SPObjectClass *) (parent_class))->write(object, repr, flags);
252 return repr;
253 }
255 NRArenaItem *
256 sp_clippath_show(SPClipPath *cp, NRArena *arena, unsigned int key)
257 {
258 g_return_val_if_fail(cp != NULL, NULL);
259 g_return_val_if_fail(SP_IS_CLIPPATH(cp), NULL);
260 g_return_val_if_fail(arena != NULL, NULL);
261 g_return_val_if_fail(NR_IS_ARENA(arena), NULL);
263 NRArenaItem *ai = NRArenaGroup::create(arena);
264 cp->display = sp_clippath_view_new_prepend(cp->display, key, ai);
266 for (SPObject *child = sp_object_first_child(SP_OBJECT(cp)) ; child != NULL; child = SP_OBJECT_NEXT(child)) {
267 if (SP_IS_ITEM(child)) {
268 NRArenaItem *ac = sp_item_invoke_show(SP_ITEM(child), arena, key, SP_ITEM_REFERENCE_FLAGS);
269 if (ac) {
270 /* The order is not important in clippath */
271 nr_arena_item_add_child(ai, ac, NULL);
272 nr_arena_item_unref(ac);
273 }
274 }
275 }
277 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
278 NRMatrix t;
279 nr_matrix_set_scale(&t, cp->display->bbox.x1 - cp->display->bbox.x0, cp->display->bbox.y1 - cp->display->bbox.y0);
280 t.c[4] = cp->display->bbox.x0;
281 t.c[5] = cp->display->bbox.y0;
282 nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), &t);
283 }
285 return ai;
286 }
288 void
289 sp_clippath_hide(SPClipPath *cp, unsigned int key)
290 {
291 g_return_if_fail(cp != NULL);
292 g_return_if_fail(SP_IS_CLIPPATH(cp));
294 for (SPObject *child = sp_object_first_child(SP_OBJECT(cp)) ; child != NULL; child = SP_OBJECT_NEXT(child)) {
295 if (SP_IS_ITEM(child)) {
296 sp_item_invoke_hide(SP_ITEM(child), key);
297 }
298 }
300 for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
301 if (v->key == key) {
302 /* We simply unref and let item to manage this in handler */
303 cp->display = sp_clippath_view_list_remove(cp->display, v);
304 return;
305 }
306 }
308 g_assert_not_reached();
309 }
311 void
312 sp_clippath_set_bbox(SPClipPath *cp, unsigned int key, NRRect *bbox)
313 {
314 for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
315 if (v->key == key) {
316 if (!NR_DF_TEST_CLOSE(v->bbox.x0, bbox->x0, NR_EPSILON) ||
317 !NR_DF_TEST_CLOSE(v->bbox.y0, bbox->y0, NR_EPSILON) ||
318 !NR_DF_TEST_CLOSE(v->bbox.x1, bbox->x1, NR_EPSILON) ||
319 !NR_DF_TEST_CLOSE(v->bbox.y1, bbox->y1, NR_EPSILON)) {
320 v->bbox = *bbox;
321 }
322 break;
323 }
324 }
325 }
327 void
328 sp_clippath_get_bbox(SPClipPath *cp, NRRect *bbox, NR::Matrix const &transform, unsigned const flags)
329 {
330 for (SPObject *o = sp_object_first_child(SP_OBJECT(cp)); o != NULL; o = SP_OBJECT_NEXT(o)) {
331 if (SP_IS_ITEM(o)) {
332 SPItem *child = SP_ITEM(o);
333 sp_item_invoke_bbox_full(child, bbox, transform, flags, FALSE);
334 }
335 }
336 }
338 /* ClipPath views */
340 SPClipPathView *
341 sp_clippath_view_new_prepend(SPClipPathView *list, unsigned int key, NRArenaItem *arenaitem)
342 {
343 SPClipPathView *new_path_view = g_new(SPClipPathView, 1);
345 new_path_view->next = list;
346 new_path_view->key = key;
347 new_path_view->arenaitem = nr_arena_item_ref(arenaitem);
348 new_path_view->bbox.x0 = new_path_view->bbox.x1 = 0.0;
349 new_path_view->bbox.y0 = new_path_view->bbox.y1 = 0.0;
351 return new_path_view;
352 }
354 SPClipPathView *
355 sp_clippath_view_list_remove(SPClipPathView *list, SPClipPathView *view)
356 {
357 if (view == list) {
358 list = list->next;
359 } else {
360 SPClipPathView *prev;
361 prev = list;
362 while (prev->next != view) prev = prev->next;
363 prev->next = view->next;
364 }
366 nr_arena_item_unref(view->arenaitem);
367 g_free(view);
369 return list;
370 }
372 // Create a mask element (using passed elements), add it to <defs>
373 const gchar *
374 sp_clippath_create (GSList *reprs, SPDocument *document, NR::Matrix const* applyTransform)
375 {
376 Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
378 Inkscape::XML::Node *repr = sp_repr_new ("svg:clipPath");
379 repr->setAttribute("clipPathUnits", "userSpaceOnUse");
381 defsrepr->appendChild(repr);
382 const gchar *id = repr->attribute("id");
383 SPObject *clip_path_object = document->getObjectById(id);
385 for (GSList *it = reprs; it != NULL; it = it->next) {
386 Inkscape::XML::Node *node = (Inkscape::XML::Node *)(it->data);
387 SPItem *item = SP_ITEM(clip_path_object->appendChildRepr(node));
389 if (NULL != applyTransform) {
390 NR::Matrix transform (item->transform);
391 transform *= (*applyTransform);
392 sp_item_write_transform(item, SP_OBJECT_REPR(item), transform);
393 }
394 }
396 Inkscape::GC::release(repr);
397 return id;
398 }
400 /*
401 Local Variables:
402 mode:c++
403 c-file-style:"stroustrup"
404 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
405 indent-tabs-mode:nil
406 fill-column:99
407 End:
408 */
409 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :