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"
29 #include <2geom/transforms.h>
31 #include "sp-clippath.h"
33 struct SPClipPathView {
34 SPClipPathView *next;
35 unsigned int key;
36 NRArenaItem *arenaitem;
37 NRRect bbox;
38 };
40 /*static void sp_clippath_class_init(SPClipPathClass *klass);
41 static void sp_clippath_init(SPClipPath *clippath);
43 static void sp_clippath_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
44 static void sp_clippath_release(SPObject * object);
45 static void sp_clippath_set(SPObject *object, unsigned int key, gchar const *value);
46 static void sp_clippath_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
47 static void sp_clippath_update(SPObject *object, SPCtx *ctx, guint flags);
48 static void sp_clippath_modified(SPObject *object, guint flags);
49 static Inkscape::XML::Node *sp_clippath_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);*/
51 SPClipPathView *sp_clippath_view_new_prepend(SPClipPathView *list, unsigned int key, NRArenaItem *arenaitem);
52 SPClipPathView *sp_clippath_view_list_remove(SPClipPathView *list, SPClipPathView *view);
54 //static SPObjectGroupClass *parent_class;
55 SPObjectGroupClass * SPClipPathClass::static_parent_class = 0;
57 GType
58 SPClipPath::sp_clippath_get_type(void)
59 {
60 static GType type = 0;
61 if (!type) {
62 GTypeInfo info = {
63 sizeof(SPClipPathClass),
64 NULL, NULL,
65 (GClassInitFunc) SPClipPathClass::sp_clippath_class_init,
66 NULL, NULL,
67 sizeof(SPClipPath),
68 16,
69 (GInstanceInitFunc) sp_clippath_init,
70 NULL, /* value_table */
71 };
72 type = g_type_register_static(SP_TYPE_OBJECTGROUP, "SPClipPath", &info, (GTypeFlags)0);
73 }
74 return type;
75 }
77 void
78 SPClipPathClass::sp_clippath_class_init(SPClipPathClass *klass)
79 {
80 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
82 static_parent_class = (SPObjectGroupClass*)g_type_class_ref(SP_TYPE_OBJECTGROUP);
84 sp_object_class->build = SPClipPath::sp_clippath_build;
85 sp_object_class->release = SPClipPath::sp_clippath_release;
86 sp_object_class->set = SPClipPath::sp_clippath_set;
87 sp_object_class->child_added = SPClipPath::sp_clippath_child_added;
88 sp_object_class->update = SPClipPath::sp_clippath_update;
89 sp_object_class->modified = SPClipPath::sp_clippath_modified;
90 sp_object_class->write = SPClipPath::sp_clippath_write;
91 }
93 void
94 SPClipPath::sp_clippath_init(SPClipPath *cp)
95 {
96 cp->clipPathUnits_set = FALSE;
97 cp->clipPathUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
99 cp->display = NULL;
100 }
102 void
103 SPClipPath::sp_clippath_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
104 {
105 if (((SPObjectClass *) SPClipPathClass::static_parent_class)->build)
106 ((SPObjectClass *) SPClipPathClass::static_parent_class)->build(object, document, repr);
108 object->readAttr( "clipPathUnits");
110 /* Register ourselves */
111 document->add_resource("clipPath", object);
112 }
114 void
115 SPClipPath::sp_clippath_release(SPObject * object)
116 {
117 if (SP_OBJECT_DOCUMENT(object)) {
118 /* Unregister ourselves */
119 SP_OBJECT_DOCUMENT(object)->remove_resource("clipPath", object);
120 }
122 SPClipPath *cp = SP_CLIPPATH(object);
123 while (cp->display) {
124 /* We simply unref and let item manage this in handler */
125 cp->display = sp_clippath_view_list_remove(cp->display, cp->display);
126 }
128 if (((SPObjectClass *) (SPClipPathClass::static_parent_class))->release) {
129 ((SPObjectClass *) SPClipPathClass::static_parent_class)->release(object);
130 }
131 }
133 void
134 SPClipPath::sp_clippath_set(SPObject *object, unsigned int key, gchar const *value)
135 {
136 SPClipPath *cp = SP_CLIPPATH(object);
138 switch (key) {
139 case SP_ATTR_CLIPPATHUNITS:
140 cp->clipPathUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
141 cp->clipPathUnits_set = FALSE;
142 if (value) {
143 if (!strcmp(value, "userSpaceOnUse")) {
144 cp->clipPathUnits_set = TRUE;
145 } else if (!strcmp(value, "objectBoundingBox")) {
146 cp->clipPathUnits = SP_CONTENT_UNITS_OBJECTBOUNDINGBOX;
147 cp->clipPathUnits_set = TRUE;
148 }
149 }
150 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
151 break;
152 default:
153 if (((SPObjectClass *) SPClipPathClass::static_parent_class)->set)
154 ((SPObjectClass *) SPClipPathClass::static_parent_class)->set(object, key, value);
155 break;
156 }
157 }
159 void
160 SPClipPath::sp_clippath_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
161 {
162 /* Invoke SPObjectGroup implementation */
163 ((SPObjectClass *) (SPClipPathClass::static_parent_class))->child_added(object, child, ref);
165 /* Show new object */
166 SPObject *ochild = SP_OBJECT_DOCUMENT(object)->getObjectByRepr(child);
167 if (SP_IS_ITEM(ochild)) {
168 SPClipPath *cp = SP_CLIPPATH(object);
169 for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
170 NRArenaItem *ac = SP_ITEM(ochild)->invoke_show( NR_ARENA_ITEM_ARENA(v->arenaitem),
171 v->key,
172 SP_ITEM_REFERENCE_FLAGS);
173 if (ac) {
174 nr_arena_item_add_child(v->arenaitem, ac, NULL);
175 }
176 }
177 }
178 }
180 void
181 SPClipPath::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(og)->first_child(); 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 Geom::Matrix t(Geom::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 void
219 SPClipPath::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(og)->first_child(); 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 Inkscape::XML::Node *
245 SPClipPath::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 *) (SPClipPathClass::static_parent_class))->write)
252 ((SPObjectClass *) (SPClipPathClass::static_parent_class))->write(object, xml_doc, repr, flags);
254 return repr;
255 }
257 NRArenaItem *
258 SPClipPath::sp_clippath_show(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 this->display = sp_clippath_view_new_prepend(this->display, key, ai);
268 for (SPObject *child = this->first_child() ; child != NULL; child = SP_OBJECT_NEXT(child)) {
269 if (SP_IS_ITEM(child)) {
270 NRArenaItem *ac = SP_ITEM(child)->invoke_show(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 }
275 }
276 }
278 if (this->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
279 Geom::Matrix t(Geom::Scale(this->display->bbox.x1 - this->display->bbox.x0, this->display->bbox.y1 - this->display->bbox.y0));
280 t[4] = this->display->bbox.x0;
281 t[5] = this->display->bbox.y0;
282 nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), &t);
283 }
285 return ai;
286 }
288 void
289 SPClipPath::sp_clippath_hide(unsigned int key)
290 {
291 //g_return_if_fail(cp != NULL);
292 //g_return_if_fail(SP_IS_CLIPPATH(cp));
294 for (SPObject *child = this->first_child() ; child != NULL; child = SP_OBJECT_NEXT(child)) {
295 if (SP_IS_ITEM(child)) {
296 SP_ITEM(child)->invoke_hide(key);
297 }
298 }
300 for (SPClipPathView *v = this->display; v != NULL; v = v->next) {
301 if (v->key == key) {
302 /* We simply unref and let item to manage this in handler */
303 this->display = sp_clippath_view_list_remove(this->display, v);
304 return;
305 }
306 }
308 g_assert_not_reached();
309 }
311 void
312 SPClipPath::sp_clippath_set_bbox(unsigned int key, NRRect *bbox)
313 {
314 for (SPClipPathView *v = this->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 SPClipPath::sp_clippath_get_bbox(NRRect *bbox, Geom::Matrix const &transform, unsigned const /*flags*/)
329 {
330 SPObject *i;
331 for (i = this->first_child(); i && !SP_IS_ITEM(i); i = SP_OBJECT_NEXT(i)){};
332 if (!i) return;
334 SP_ITEM(i)->invoke_bbox_full( bbox, Geom::Matrix(SP_ITEM(i)->transform) * transform, SPItem::GEOMETRIC_BBOX, FALSE);
335 SPObject *i_start = i;
337 while (i != NULL) {
338 if (i != i_start) {
339 NRRect i_box;
340 SP_ITEM(i)->invoke_bbox_full( &i_box, Geom::Matrix(SP_ITEM(i)->transform) * transform, SPItem::GEOMETRIC_BBOX, FALSE);
341 nr_rect_d_union (bbox, bbox, &i_box);
342 }
343 i = SP_OBJECT_NEXT(i);
344 for (; i && !SP_IS_ITEM(i); i = SP_OBJECT_NEXT(i)){};
345 }
346 }
348 /* ClipPath views */
350 SPClipPathView *
351 sp_clippath_view_new_prepend(SPClipPathView *list, unsigned int key, NRArenaItem *arenaitem)
352 {
353 SPClipPathView *new_path_view = g_new(SPClipPathView, 1);
355 new_path_view->next = list;
356 new_path_view->key = key;
357 new_path_view->arenaitem = nr_arena_item_ref(arenaitem);
358 new_path_view->bbox.x0 = new_path_view->bbox.x1 = 0.0;
359 new_path_view->bbox.y0 = new_path_view->bbox.y1 = 0.0;
361 return new_path_view;
362 }
364 SPClipPathView *
365 sp_clippath_view_list_remove(SPClipPathView *list, SPClipPathView *view)
366 {
367 if (view == list) {
368 list = list->next;
369 } else {
370 SPClipPathView *prev;
371 prev = list;
372 while (prev->next != view) prev = prev->next;
373 prev->next = view->next;
374 }
376 nr_arena_item_unref(view->arenaitem);
377 g_free(view);
379 return list;
380 }
382 // Create a mask element (using passed elements), add it to <defs>
383 const gchar *
384 SPClipPath::create (GSList *reprs, SPDocument *document, Geom::Matrix const* applyTransform)
385 {
386 Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
388 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(document);
389 Inkscape::XML::Node *repr = xml_doc->createElement("svg:clipPath");
390 repr->setAttribute("clipPathUnits", "userSpaceOnUse");
392 defsrepr->appendChild(repr);
393 const gchar *id = repr->attribute("id");
394 SPObject *clip_path_object = document->getObjectById(id);
396 for (GSList *it = reprs; it != NULL; it = it->next) {
397 Inkscape::XML::Node *node = (Inkscape::XML::Node *)(it->data);
398 SPItem *item = SP_ITEM(clip_path_object->appendChildRepr(node));
400 if (NULL != applyTransform) {
401 Geom::Matrix transform (item->transform);
402 transform *= (*applyTransform);
403 item->doWriteTransform(SP_OBJECT_REPR(item), transform);
404 }
405 }
407 Inkscape::GC::release(repr);
408 return id;
409 }
411 /*
412 Local Variables:
413 mode:c++
414 c-file-style:"stroustrup"
415 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
416 indent-tabs-mode:nil
417 fill-column:99
418 End:
419 */
420 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :