1 /*
2 * SVG <clipPath> implementation
3 *
4 * Authors:
5 * Lauris Kaplinski <lauris@kaplinski.com>
6 * Jon A. Cruz <jon@joncruz.org>
7 * Abhishek Sharma
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 SPClipPathView *sp_clippath_view_new_prepend(SPClipPathView *list, unsigned int key, NRArenaItem *arenaitem);
41 SPClipPathView *sp_clippath_view_list_remove(SPClipPathView *list, SPClipPathView *view);
43 SPObjectGroupClass * SPClipPathClass::static_parent_class = 0;
45 GType
46 SPClipPath::sp_clippath_get_type(void)
47 {
48 static GType type = 0;
49 if (!type) {
50 GTypeInfo info = {
51 sizeof(SPClipPathClass),
52 NULL, NULL,
53 (GClassInitFunc) SPClipPathClass::sp_clippath_class_init,
54 NULL, NULL,
55 sizeof(SPClipPath),
56 16,
57 (GInstanceInitFunc) SPClipPath::init,
58 NULL, /* value_table */
59 };
60 type = g_type_register_static(SP_TYPE_OBJECTGROUP, "SPClipPath", &info, (GTypeFlags)0);
61 }
62 return type;
63 }
65 void SPClipPathClass::sp_clippath_class_init(SPClipPathClass *klass)
66 {
67 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
69 static_parent_class = (SPObjectGroupClass*)g_type_class_ref(SP_TYPE_OBJECTGROUP);
71 sp_object_class->build = SPClipPath::build;
72 sp_object_class->release = SPClipPath::release;
73 sp_object_class->set = SPClipPath::set;
74 sp_object_class->child_added = SPClipPath::childAdded;
75 sp_object_class->update = SPClipPath::update;
76 sp_object_class->modified = SPClipPath::modified;
77 sp_object_class->write = SPClipPath::write;
78 }
80 void SPClipPath::init(SPClipPath *cp)
81 {
82 cp->clipPathUnits_set = FALSE;
83 cp->clipPathUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
85 cp->display = NULL;
86 }
88 void SPClipPath::build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
89 {
90 if (((SPObjectClass *) SPClipPathClass::static_parent_class)->build)
91 ((SPObjectClass *) SPClipPathClass::static_parent_class)->build(object, document, repr);
93 object->readAttr( "clipPathUnits" );
95 /* Register ourselves */
96 document->addResource("clipPath", object);
97 }
99 void SPClipPath::release(SPObject * object)
100 {
101 if (SP_OBJECT_DOCUMENT(object)) {
102 /* Unregister ourselves */
103 SP_OBJECT_DOCUMENT(object)->removeResource("clipPath", object);
104 }
106 SPClipPath *cp = SP_CLIPPATH(object);
107 while (cp->display) {
108 /* We simply unref and let item manage this in handler */
109 cp->display = sp_clippath_view_list_remove(cp->display, cp->display);
110 }
112 if (((SPObjectClass *) (SPClipPathClass::static_parent_class))->release) {
113 ((SPObjectClass *) SPClipPathClass::static_parent_class)->release(object);
114 }
115 }
117 void SPClipPath::set(SPObject *object, unsigned int key, gchar const *value)
118 {
119 SPClipPath *cp = SP_CLIPPATH(object);
121 switch (key) {
122 case SP_ATTR_CLIPPATHUNITS:
123 cp->clipPathUnits = SP_CONTENT_UNITS_USERSPACEONUSE;
124 cp->clipPathUnits_set = FALSE;
125 if (value) {
126 if (!strcmp(value, "userSpaceOnUse")) {
127 cp->clipPathUnits_set = TRUE;
128 } else if (!strcmp(value, "objectBoundingBox")) {
129 cp->clipPathUnits = SP_CONTENT_UNITS_OBJECTBOUNDINGBOX;
130 cp->clipPathUnits_set = TRUE;
131 }
132 }
133 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
134 break;
135 default:
136 if (((SPObjectClass *) SPClipPathClass::static_parent_class)->set) {
137 ((SPObjectClass *) SPClipPathClass::static_parent_class)->set(object, key, value);
138 }
139 break;
140 }
141 }
143 void SPClipPath::childAdded(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
144 {
145 /* Invoke SPObjectGroup implementation */
146 ((SPObjectClass *) (SPClipPathClass::static_parent_class))->child_added(object, child, ref);
148 /* Show new object */
149 SPObject *ochild = SP_OBJECT_DOCUMENT(object)->getObjectByRepr(child);
150 if (SP_IS_ITEM(ochild)) {
151 SPClipPath *cp = SP_CLIPPATH(object);
152 for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
153 NRArenaItem *ac = SP_ITEM(ochild)->invoke_show( NR_ARENA_ITEM_ARENA(v->arenaitem),
154 v->key,
155 SP_ITEM_REFERENCE_FLAGS);
156 if (ac) {
157 nr_arena_item_add_child(v->arenaitem, ac, NULL);
158 }
159 }
160 }
161 }
163 void SPClipPath::update(SPObject *object, SPCtx *ctx, guint flags)
164 {
165 if (flags & SP_OBJECT_MODIFIED_FLAG) {
166 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
167 }
169 flags &= SP_OBJECT_MODIFIED_CASCADE;
171 SPObjectGroup *og = SP_OBJECTGROUP(object);
172 GSList *l = NULL;
173 for ( SPObject *child = og->firstChild(); child; child = child->getNext()) {
174 g_object_ref(G_OBJECT(child));
175 l = g_slist_prepend(l, child);
176 }
177 l = g_slist_reverse(l);
178 while (l) {
179 SPObject *child = SP_OBJECT(l->data);
180 l = g_slist_remove(l, child);
181 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
182 child->updateDisplay(ctx, flags);
183 }
184 g_object_unref(G_OBJECT(child));
185 }
187 SPClipPath *cp = SP_CLIPPATH(object);
188 for (SPClipPathView *v = cp->display; v != NULL; v = v->next) {
189 if (cp->clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
190 Geom::Matrix t(Geom::Scale(v->bbox.x1 - v->bbox.x0, v->bbox.y1 - v->bbox.y0));
191 t[4] = v->bbox.x0;
192 t[5] = v->bbox.y0;
193 nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), &t);
194 } else {
195 nr_arena_group_set_child_transform(NR_ARENA_GROUP(v->arenaitem), NULL);
196 }
197 }
198 }
200 void SPClipPath::modified(SPObject *object, guint flags)
201 {
202 if (flags & SP_OBJECT_MODIFIED_FLAG) {
203 flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
204 }
206 flags &= SP_OBJECT_MODIFIED_CASCADE;
208 SPObjectGroup *og = SP_OBJECTGROUP(object);
209 GSList *l = NULL;
210 for (SPObject *child = og->firstChild(); child; child = child->getNext()) {
211 g_object_ref(G_OBJECT(child));
212 l = g_slist_prepend(l, child);
213 }
214 l = g_slist_reverse(l);
215 while (l) {
216 SPObject *child = SP_OBJECT(l->data);
217 l = g_slist_remove(l, child);
218 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
219 child->emitModified(flags);
220 }
221 g_object_unref(G_OBJECT(child));
222 }
223 }
225 Inkscape::XML::Node *SPClipPath::write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
226 {
227 if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
228 repr = xml_doc->createElement("svg:clipPath");
229 }
231 if (((SPObjectClass *) (SPClipPathClass::static_parent_class))->write) {
232 ((SPObjectClass *) (SPClipPathClass::static_parent_class))->write(object, xml_doc, repr, flags);
233 }
235 return repr;
236 }
238 NRArenaItem *SPClipPath::show(NRArena *arena, unsigned int key)
239 {
240 g_return_val_if_fail(arena != NULL, NULL);
241 g_return_val_if_fail(NR_IS_ARENA(arena), NULL);
243 NRArenaItem *ai = NRArenaGroup::create(arena);
244 display = sp_clippath_view_new_prepend(display, key, ai);
246 for ( SPObject *child = firstChild() ; child ; child = child->getNext() ) {
247 if (SP_IS_ITEM(child)) {
248 NRArenaItem *ac = SP_ITEM(child)->invoke_show(arena, key, SP_ITEM_REFERENCE_FLAGS);
249 if (ac) {
250 /* The order is not important in clippath */
251 nr_arena_item_add_child(ai, ac, NULL);
252 }
253 }
254 }
256 if (clipPathUnits == SP_CONTENT_UNITS_OBJECTBOUNDINGBOX) {
257 Geom::Matrix t(Geom::Scale(display->bbox.x1 - display->bbox.x0, display->bbox.y1 - display->bbox.y0));
258 t[4] = display->bbox.x0;
259 t[5] = display->bbox.y0;
260 nr_arena_group_set_child_transform(NR_ARENA_GROUP(ai), &t);
261 }
263 return ai;
264 }
266 void SPClipPath::hide(unsigned int key)
267 {
268 for ( SPObject *child = firstChild() ; child; child = child->getNext() ) {
269 if (SP_IS_ITEM(child)) {
270 SP_ITEM(child)->invoke_hide(key);
271 }
272 }
274 for (SPClipPathView *v = display; v != NULL; v = v->next) {
275 if (v->key == key) {
276 /* We simply unref and let item to manage this in handler */
277 display = sp_clippath_view_list_remove(display, v);
278 return;
279 }
280 }
282 g_assert_not_reached();
283 }
285 void SPClipPath::setBBox(unsigned int key, NRRect *bbox)
286 {
287 for (SPClipPathView *v = display; v != NULL; v = v->next) {
288 if (v->key == key) {
289 if (!NR_DF_TEST_CLOSE(v->bbox.x0, bbox->x0, NR_EPSILON) ||
290 !NR_DF_TEST_CLOSE(v->bbox.y0, bbox->y0, NR_EPSILON) ||
291 !NR_DF_TEST_CLOSE(v->bbox.x1, bbox->x1, NR_EPSILON) ||
292 !NR_DF_TEST_CLOSE(v->bbox.y1, bbox->y1, NR_EPSILON)) {
293 v->bbox = *bbox;
294 }
295 break;
296 }
297 }
298 }
300 void SPClipPath::getBBox(NRRect *bbox, Geom::Matrix const &transform, unsigned const /*flags*/)
301 {
302 SPObject *i = 0;
303 for (i = firstChild(); i && !SP_IS_ITEM(i); i = i->getNext()) {
304 }
305 if (!i) {
306 return;
307 }
309 SP_ITEM(i)->invoke_bbox_full( bbox, Geom::Matrix(SP_ITEM(i)->transform) * transform, SPItem::GEOMETRIC_BBOX, FALSE);
310 SPObject *i_start = i;
312 while (i != NULL) {
313 if (i != i_start) {
314 NRRect i_box;
315 SP_ITEM(i)->invoke_bbox_full( &i_box, Geom::Matrix(SP_ITEM(i)->transform) * transform, SPItem::GEOMETRIC_BBOX, FALSE);
316 nr_rect_d_union (bbox, bbox, &i_box);
317 }
318 i = i->getNext();
319 for (; i && !SP_IS_ITEM(i); i = i->getNext()){};
320 }
321 }
323 /* ClipPath views */
325 SPClipPathView *
326 sp_clippath_view_new_prepend(SPClipPathView *list, unsigned int key, NRArenaItem *arenaitem)
327 {
328 SPClipPathView *new_path_view = g_new(SPClipPathView, 1);
330 new_path_view->next = list;
331 new_path_view->key = key;
332 new_path_view->arenaitem = nr_arena_item_ref(arenaitem);
333 new_path_view->bbox.x0 = new_path_view->bbox.x1 = 0.0;
334 new_path_view->bbox.y0 = new_path_view->bbox.y1 = 0.0;
336 return new_path_view;
337 }
339 SPClipPathView *
340 sp_clippath_view_list_remove(SPClipPathView *list, SPClipPathView *view)
341 {
342 if (view == list) {
343 list = list->next;
344 } else {
345 SPClipPathView *prev;
346 prev = list;
347 while (prev->next != view) prev = prev->next;
348 prev->next = view->next;
349 }
351 nr_arena_item_unref(view->arenaitem);
352 g_free(view);
354 return list;
355 }
357 // Create a mask element (using passed elements), add it to <defs>
358 const gchar *SPClipPath::create (GSList *reprs, SPDocument *document, Geom::Matrix const* applyTransform)
359 {
360 Inkscape::XML::Node *defsrepr = SP_OBJECT_REPR (SP_DOCUMENT_DEFS (document));
362 Inkscape::XML::Document *xml_doc = document->getReprDoc();
363 Inkscape::XML::Node *repr = xml_doc->createElement("svg:clipPath");
364 repr->setAttribute("clipPathUnits", "userSpaceOnUse");
366 defsrepr->appendChild(repr);
367 const gchar *id = repr->attribute("id");
368 SPObject *clip_path_object = document->getObjectById(id);
370 for (GSList *it = reprs; it != NULL; it = it->next) {
371 Inkscape::XML::Node *node = (Inkscape::XML::Node *)(it->data);
372 SPItem *item = SP_ITEM(clip_path_object->appendChildRepr(node));
374 if (NULL != applyTransform) {
375 Geom::Matrix transform (item->transform);
376 transform *= (*applyTransform);
377 item->doWriteTransform(SP_OBJECT_REPR(item), transform);
378 }
379 }
381 Inkscape::GC::release(repr);
382 return id;
383 }
385 /*
386 Local Variables:
387 mode:c++
388 c-file-style:"stroustrup"
389 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
390 indent-tabs-mode:nil
391 fill-column:99
392 End:
393 */
394 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :