1 #define __SP_FLOWREGION_C__
3 /*
4 */
6 #ifdef HAVE_CONFIG_H
7 # include "config.h"
8 #endif
9 #include <glibmm/i18n.h>
11 #include <xml/repr.h>
12 #include "display/curve.h"
13 #include "sp-shape.h"
14 #include "sp-text.h"
15 #include "sp-use.h"
16 #include "style.h"
17 #include "document.h"
18 #include "sp-title.h"
19 #include "sp-desc.h"
21 #include "sp-flowregion.h"
23 #include "display/canvas-bpath.h"
26 #include "livarot/Path.h"
27 #include "livarot/Shape.h"
29 static void sp_flowregion_class_init (SPFlowregionClass *klass);
30 static void sp_flowregion_init (SPFlowregion *group);
31 static void sp_flowregion_dispose (GObject *object);
33 static void sp_flowregion_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
34 static void sp_flowregion_remove_child (SPObject * object, Inkscape::XML::Node * child);
35 static void sp_flowregion_update (SPObject *object, SPCtx *ctx, guint flags);
36 static void sp_flowregion_modified (SPObject *object, guint flags);
37 static Inkscape::XML::Node *sp_flowregion_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
39 static gchar * sp_flowregion_description (SPItem * item);
41 static SPItemClass * flowregion_parent_class;
43 static void sp_flowregionexclude_class_init (SPFlowregionExcludeClass *klass);
44 static void sp_flowregionexclude_init (SPFlowregionExclude *group);
45 static void sp_flowregionexclude_dispose (GObject *object);
47 static void sp_flowregionexclude_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
48 static void sp_flowregionexclude_remove_child (SPObject * object, Inkscape::XML::Node * child);
49 static void sp_flowregionexclude_update (SPObject *object, SPCtx *ctx, guint flags);
50 static void sp_flowregionexclude_modified (SPObject *object, guint flags);
51 static Inkscape::XML::Node *sp_flowregionexclude_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
53 static gchar * sp_flowregionexclude_description (SPItem * item);
55 static SPItemClass * flowregionexclude_parent_class;
58 static void GetDest(SPObject* child,Shape **computed);
60 GType
61 sp_flowregion_get_type (void)
62 {
63 static GType group_type = 0;
64 if (!group_type) {
65 GTypeInfo group_info = {
66 sizeof (SPFlowregionClass),
67 NULL, /* base_init */
68 NULL, /* base_finalize */
69 (GClassInitFunc) sp_flowregion_class_init,
70 NULL, /* class_finalize */
71 NULL, /* class_data */
72 sizeof (SPFlowregion),
73 16, /* n_preallocs */
74 (GInstanceInitFunc) sp_flowregion_init,
75 NULL, /* value_table */
76 };
77 group_type = g_type_register_static (SP_TYPE_ITEM, "SPFlowregion", &group_info, (GTypeFlags)0);
78 }
79 return group_type;
80 }
82 static void
83 sp_flowregion_class_init (SPFlowregionClass *klass)
84 {
85 GObjectClass * object_class;
86 SPObjectClass * sp_object_class;
87 SPItemClass * item_class;
89 object_class = (GObjectClass *) klass;
90 sp_object_class = (SPObjectClass *) klass;
91 item_class = (SPItemClass *) klass;
93 flowregion_parent_class = (SPItemClass *)g_type_class_ref (SP_TYPE_ITEM);
95 object_class->dispose = sp_flowregion_dispose;
97 sp_object_class->child_added = sp_flowregion_child_added;
98 sp_object_class->remove_child = sp_flowregion_remove_child;
99 sp_object_class->update = sp_flowregion_update;
100 sp_object_class->modified = sp_flowregion_modified;
101 sp_object_class->write = sp_flowregion_write;
103 item_class->description = sp_flowregion_description;
104 }
106 static void
107 sp_flowregion_init (SPFlowregion *group)
108 {
109 new (&group->computed) std::vector<Shape*>;
110 }
112 static void
113 sp_flowregion_dispose(GObject *object)
114 {
115 SPFlowregion *group=(SPFlowregion *)object;
116 for (std::vector<Shape*>::iterator it = group->computed.begin() ; it != group->computed.end() ; it++)
117 delete *it;
118 group->computed.~vector<Shape*>();
119 }
121 static void
122 sp_flowregion_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
123 {
124 SPItem *item;
126 item = SP_ITEM (object);
128 if (((SPObjectClass *) (flowregion_parent_class))->child_added)
129 (* ((SPObjectClass *) (flowregion_parent_class))->child_added) (object, child, ref);
131 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
132 }
134 /* fixme: hide (Lauris) */
136 static void
137 sp_flowregion_remove_child (SPObject * object, Inkscape::XML::Node * child)
138 {
139 if (((SPObjectClass *) (flowregion_parent_class))->remove_child)
140 (* ((SPObjectClass *) (flowregion_parent_class))->remove_child) (object, child);
142 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
143 }
146 static void
147 sp_flowregion_update (SPObject *object, SPCtx *ctx, unsigned int flags)
148 {
149 SPFlowregion *group;
150 SPObject *child;
151 SPItemCtx *ictx, cctx;
152 GSList *l;
154 group = SP_FLOWREGION (object);
155 ictx = (SPItemCtx *) ctx;
156 cctx = *ictx;
158 if (((SPObjectClass *) (flowregion_parent_class))->update)
159 ((SPObjectClass *) (flowregion_parent_class))->update (object, ctx, flags);
161 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
162 flags &= SP_OBJECT_MODIFIED_CASCADE;
164 l = NULL;
165 for (child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
166 g_object_ref (G_OBJECT (child));
167 l = g_slist_prepend (l, child);
168 }
169 l = g_slist_reverse (l);
170 while (l) {
171 child = SP_OBJECT (l->data);
172 l = g_slist_remove (l, child);
173 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
174 if (SP_IS_ITEM (child)) {
175 SPItem const &chi = *SP_ITEM(child);
176 cctx.i2doc = chi.transform * ictx->i2doc;
177 cctx.i2vp = chi.transform * ictx->i2vp;
178 child->updateDisplay((SPCtx *)&cctx, flags);
179 } else {
180 child->updateDisplay(ctx, flags);
181 }
182 }
183 g_object_unref (G_OBJECT (child));
184 }
186 group->UpdateComputed();
187 }
189 void SPFlowregion::UpdateComputed(void)
190 {
191 SPObject* object=SP_OBJECT(this);
193 for (std::vector<Shape*>::iterator it = computed.begin() ; it != computed.end() ; it++)
194 delete *it;
195 computed.clear();
197 for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
198 Shape *shape = NULL;
199 GetDest(child,&shape);
200 computed.push_back(shape);
201 }
202 }
204 static void
205 sp_flowregion_modified (SPObject *object, guint flags)
206 {
207 SPFlowregion *group;
208 SPObject *child;
209 GSList *l;
211 group = SP_FLOWREGION (object);
213 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
214 flags &= SP_OBJECT_MODIFIED_CASCADE;
216 l = NULL;
217 for (child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
218 g_object_ref (G_OBJECT (child));
219 l = g_slist_prepend (l, child);
220 }
221 l = g_slist_reverse (l);
222 while (l) {
223 child = SP_OBJECT (l->data);
224 l = g_slist_remove (l, child);
225 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
226 child->emitModified(flags);
227 }
228 g_object_unref (G_OBJECT (child));
229 }
230 }
232 static Inkscape::XML::Node *
233 sp_flowregion_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
234 {
235 if (flags & SP_OBJECT_WRITE_BUILD) {
236 if ( repr == NULL ) {
237 repr = xml_doc->createElement("svg:flowRegion");
238 }
240 GSList *l = NULL;
241 for ( SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
242 if (SP_IS_TITLE(child) || SP_IS_DESC(child)) continue;
243 Inkscape::XML::Node *crepr = child->updateRepr(xml_doc, NULL, flags);
244 if (crepr) l = g_slist_prepend(l, crepr);
245 }
247 while (l) {
248 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
249 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
250 l = g_slist_remove(l, l->data);
251 }
253 } else {
254 for ( SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
255 if (SP_IS_TITLE(child) || SP_IS_DESC(child)) continue;
256 child->updateRepr(flags);
257 }
258 }
260 if (((SPObjectClass *) (flowregion_parent_class))->write)
261 ((SPObjectClass *) (flowregion_parent_class))->write (object, xml_doc, repr, flags);
263 return repr;
264 }
267 static gchar *sp_flowregion_description(SPItem */*item*/)
268 {
269 // TRANSLATORS: "Flow region" is an area where text is allowed to flow
270 return g_strdup_printf(_("Flow region"));
271 }
273 /*
274 *
275 */
277 GType
278 sp_flowregionexclude_get_type (void)
279 {
280 static GType group_type = 0;
281 if (!group_type) {
282 GTypeInfo group_info = {
283 sizeof (SPFlowregionExcludeClass),
284 NULL, /* base_init */
285 NULL, /* base_finalize */
286 (GClassInitFunc) sp_flowregionexclude_class_init,
287 NULL, /* class_finalize */
288 NULL, /* class_data */
289 sizeof (SPFlowregionExclude),
290 16, /* n_preallocs */
291 (GInstanceInitFunc) sp_flowregionexclude_init,
292 NULL, /* value_table */
293 };
294 group_type = g_type_register_static (SP_TYPE_ITEM, "SPFlowregionExclude", &group_info, (GTypeFlags)0);
295 }
296 return group_type;
297 }
299 static void
300 sp_flowregionexclude_class_init (SPFlowregionExcludeClass *klass)
301 {
302 GObjectClass * object_class;
303 SPObjectClass * sp_object_class;
304 SPItemClass * item_class;
306 object_class = (GObjectClass *) klass;
307 sp_object_class = (SPObjectClass *) klass;
308 item_class = (SPItemClass *) klass;
310 flowregionexclude_parent_class = (SPItemClass *)g_type_class_ref (SP_TYPE_ITEM);
312 object_class->dispose = sp_flowregionexclude_dispose;
314 sp_object_class->child_added = sp_flowregionexclude_child_added;
315 sp_object_class->remove_child = sp_flowregionexclude_remove_child;
316 sp_object_class->update = sp_flowregionexclude_update;
317 sp_object_class->modified = sp_flowregionexclude_modified;
318 sp_object_class->write = sp_flowregionexclude_write;
320 item_class->description = sp_flowregionexclude_description;
321 }
323 static void
324 sp_flowregionexclude_init (SPFlowregionExclude *group)
325 {
326 group->computed = NULL;
327 }
329 static void
330 sp_flowregionexclude_dispose(GObject *object)
331 {
332 SPFlowregionExclude *group=(SPFlowregionExclude *)object;
333 if (group->computed) {
334 delete group->computed;
335 group->computed = NULL;
336 }
337 }
339 static void
340 sp_flowregionexclude_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
341 {
342 SPItem *item;
344 item = SP_ITEM (object);
346 if (((SPObjectClass *) (flowregionexclude_parent_class))->child_added)
347 (* ((SPObjectClass *) (flowregionexclude_parent_class))->child_added) (object, child, ref);
349 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
350 }
352 /* fixme: hide (Lauris) */
354 static void
355 sp_flowregionexclude_remove_child (SPObject * object, Inkscape::XML::Node * child)
356 {
357 if (((SPObjectClass *) (flowregionexclude_parent_class))->remove_child)
358 (* ((SPObjectClass *) (flowregionexclude_parent_class))->remove_child) (object, child);
360 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
361 }
364 static void
365 sp_flowregionexclude_update (SPObject *object, SPCtx *ctx, unsigned int flags)
366 {
367 SPFlowregionExclude *group;
368 SPObject *child;
369 SPItemCtx *ictx, cctx;
370 GSList *l;
372 group = SP_FLOWREGIONEXCLUDE (object);
373 ictx = (SPItemCtx *) ctx;
374 cctx = *ictx;
376 if (((SPObjectClass *) (flowregionexclude_parent_class))->update)
377 ((SPObjectClass *) (flowregionexclude_parent_class))->update (object, ctx, flags);
379 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
380 flags &= SP_OBJECT_MODIFIED_CASCADE;
382 l = NULL;
383 for (child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
384 g_object_ref (G_OBJECT (child));
385 l = g_slist_prepend (l, child);
386 }
387 l = g_slist_reverse (l);
388 while (l) {
389 child = SP_OBJECT (l->data);
390 l = g_slist_remove (l, child);
391 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
392 if (SP_IS_ITEM (child)) {
393 SPItem const &chi = *SP_ITEM(child);
394 cctx.i2doc = chi.transform * ictx->i2doc;
395 cctx.i2vp = chi.transform * ictx->i2vp;
396 child->updateDisplay((SPCtx *)&cctx, flags);
397 } else {
398 child->updateDisplay(ctx, flags);
399 }
400 }
401 g_object_unref (G_OBJECT (child));
402 }
404 group->UpdateComputed();
405 }
406 void SPFlowregionExclude::UpdateComputed(void)
407 {
408 SPObject* object=SP_OBJECT(this);
410 if (computed) {
411 delete computed;
412 computed = NULL;
413 }
415 for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
416 GetDest(child,&computed);
417 }
418 }
420 static void
421 sp_flowregionexclude_modified (SPObject *object, guint flags)
422 {
423 SPFlowregionExclude *group;
424 SPObject *child;
425 GSList *l;
427 group = SP_FLOWREGIONEXCLUDE (object);
429 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
430 flags &= SP_OBJECT_MODIFIED_CASCADE;
432 l = NULL;
433 for (child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
434 g_object_ref (G_OBJECT (child));
435 l = g_slist_prepend (l, child);
436 }
437 l = g_slist_reverse (l);
438 while (l) {
439 child = SP_OBJECT (l->data);
440 l = g_slist_remove (l, child);
441 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
442 child->emitModified(flags);
443 }
444 g_object_unref (G_OBJECT (child));
445 }
446 }
448 static Inkscape::XML::Node *
449 sp_flowregionexclude_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
450 {
451 if (flags & SP_OBJECT_WRITE_BUILD) {
452 if ( repr == NULL ) {
453 repr = xml_doc->createElement("svg:flowRegionExclude");
454 }
456 GSList *l = NULL;
457 for ( SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
458 Inkscape::XML::Node *crepr = child->updateRepr(xml_doc, NULL, flags);
459 if (crepr) l = g_slist_prepend(l, crepr);
460 }
462 while (l) {
463 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
464 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
465 l = g_slist_remove(l, l->data);
466 }
468 } else {
469 for ( SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
470 child->updateRepr(flags);
471 }
472 }
474 if (((SPObjectClass *) (flowregionexclude_parent_class))->write)
475 ((SPObjectClass *) (flowregionexclude_parent_class))->write (object, xml_doc, repr, flags);
477 return repr;
478 }
481 static gchar *sp_flowregionexclude_description(SPItem */*item*/)
482 {
483 /* TRANSLATORS: A region "cut out of" a flow region; text is not allowed to flow inside the
484 * flow excluded region. flowRegionExclude in SVG 1.2: see
485 * http://www.w3.org/TR/2004/WD-SVG12-20041027/flow.html#flowRegion-elem and
486 * http://www.w3.org/TR/2004/WD-SVG12-20041027/flow.html#flowRegionExclude-elem. */
487 return g_strdup_printf(_("Flow excluded region"));
488 }
490 /*
491 *
492 */
494 static void UnionShape(Shape **base_shape, Shape const *add_shape)
495 {
496 if (*base_shape == NULL)
497 *base_shape = new Shape;
498 if ( (*base_shape)->hasEdges() == false ) {
499 (*base_shape)->Copy(const_cast<Shape*>(add_shape));
500 } else if ( add_shape->hasEdges() ) {
501 Shape* temp=new Shape;
502 temp->Booleen(const_cast<Shape*>(add_shape), *base_shape, bool_op_union);
503 delete *base_shape;
504 *base_shape = temp;
505 }
506 }
508 static void GetDest(SPObject* child,Shape **computed)
509 {
510 if ( child == NULL ) return;
512 SPCurve *curve=NULL;
513 Geom::Matrix tr_mat;
515 SPObject* u_child=child;
516 if ( SP_IS_USE(u_child) ) {
517 u_child=SP_USE(u_child)->child;
518 tr_mat = SP_ITEM(u_child)->getRelativeTransform(SP_OBJECT_PARENT(child));
519 } else {
520 tr_mat = SP_ITEM(u_child)->transform;
521 }
522 if ( SP_IS_SHAPE (u_child) ) {
523 curve = sp_shape_get_curve (SP_SHAPE (u_child));
524 } else if ( SP_IS_TEXT (u_child) ) {
525 curve = SP_TEXT (u_child)->getNormalizedBpath ();
526 }
528 if ( curve ) {
529 Path* temp=new Path;
530 temp->LoadPathVector(curve->get_pathvector(), tr_mat, true);
531 Shape* n_shp=new Shape;
532 temp->Convert(0.25);
533 temp->Fill(n_shp,0);
534 Shape* uncross=new Shape;
535 SPStyle* style=SP_OBJECT_STYLE(u_child);
536 if ( style && style->fill_rule.computed == SP_WIND_RULE_EVENODD ) {
537 uncross->ConvertToShape(n_shp,fill_oddEven);
538 } else {
539 uncross->ConvertToShape(n_shp,fill_nonZero);
540 }
541 UnionShape(computed, uncross);
542 delete uncross;
543 delete n_shp;
544 delete temp;
545 curve->unref();
546 } else {
547 // printf("no curve\n");
548 }
549 }