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"
19 #include "sp-flowregion.h"
21 #include "display/canvas-bpath.h"
24 #include "livarot/Path.h"
25 #include "livarot/Shape.h"
27 static void sp_flowregion_class_init (SPFlowregionClass *klass);
28 static void sp_flowregion_init (SPFlowregion *group);
29 static void sp_flowregion_dispose (GObject *object);
31 static void sp_flowregion_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
32 static void sp_flowregion_remove_child (SPObject * object, Inkscape::XML::Node * child);
33 static void sp_flowregion_update (SPObject *object, SPCtx *ctx, guint flags);
34 static void sp_flowregion_modified (SPObject *object, guint flags);
35 static Inkscape::XML::Node *sp_flowregion_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
37 static gchar * sp_flowregion_description (SPItem * item);
39 static SPItemClass * flowregion_parent_class;
41 static void sp_flowregionexclude_class_init (SPFlowregionExcludeClass *klass);
42 static void sp_flowregionexclude_init (SPFlowregionExclude *group);
43 static void sp_flowregionexclude_dispose (GObject *object);
45 static void sp_flowregionexclude_child_added (SPObject * object, Inkscape::XML::Node * child, Inkscape::XML::Node * ref);
46 static void sp_flowregionexclude_remove_child (SPObject * object, Inkscape::XML::Node * child);
47 static void sp_flowregionexclude_update (SPObject *object, SPCtx *ctx, guint flags);
48 static void sp_flowregionexclude_modified (SPObject *object, guint flags);
49 static Inkscape::XML::Node *sp_flowregionexclude_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
51 static gchar * sp_flowregionexclude_description (SPItem * item);
53 static SPItemClass * flowregionexclude_parent_class;
56 static void GetDest(SPObject* child,Shape **computed,NR::Matrix itr_mat);
58 GType
59 sp_flowregion_get_type (void)
60 {
61 static GType group_type = 0;
62 if (!group_type) {
63 GTypeInfo group_info = {
64 sizeof (SPFlowregionClass),
65 NULL, /* base_init */
66 NULL, /* base_finalize */
67 (GClassInitFunc) sp_flowregion_class_init,
68 NULL, /* class_finalize */
69 NULL, /* class_data */
70 sizeof (SPFlowregion),
71 16, /* n_preallocs */
72 (GInstanceInitFunc) sp_flowregion_init,
73 NULL, /* value_table */
74 };
75 group_type = g_type_register_static (SP_TYPE_ITEM, "SPFlowregion", &group_info, (GTypeFlags)0);
76 }
77 return group_type;
78 }
80 static void
81 sp_flowregion_class_init (SPFlowregionClass *klass)
82 {
83 GObjectClass * object_class;
84 SPObjectClass * sp_object_class;
85 SPItemClass * item_class;
87 object_class = (GObjectClass *) klass;
88 sp_object_class = (SPObjectClass *) klass;
89 item_class = (SPItemClass *) klass;
91 flowregion_parent_class = (SPItemClass *)g_type_class_ref (SP_TYPE_ITEM);
93 object_class->dispose = sp_flowregion_dispose;
95 sp_object_class->child_added = sp_flowregion_child_added;
96 sp_object_class->remove_child = sp_flowregion_remove_child;
97 sp_object_class->update = sp_flowregion_update;
98 sp_object_class->modified = sp_flowregion_modified;
99 sp_object_class->write = sp_flowregion_write;
101 item_class->description = sp_flowregion_description;
102 }
104 static void
105 sp_flowregion_init (SPFlowregion *group)
106 {
107 new (&group->computed) std::vector<Shape*>;
108 }
110 static void
111 sp_flowregion_dispose(GObject *object)
112 {
113 SPFlowregion *group=(SPFlowregion *)object;
114 for (std::vector<Shape*>::iterator it = group->computed.begin() ; it != group->computed.end() ; it++)
115 delete *it;
116 group->computed.~vector<Shape*>();
117 }
119 static void
120 sp_flowregion_child_added (SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
121 {
122 SPItem *item;
124 item = SP_ITEM (object);
126 if (((SPObjectClass *) (flowregion_parent_class))->child_added)
127 (* ((SPObjectClass *) (flowregion_parent_class))->child_added) (object, child, ref);
129 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
130 }
132 /* fixme: hide (Lauris) */
134 static void
135 sp_flowregion_remove_child (SPObject * object, Inkscape::XML::Node * child)
136 {
137 if (((SPObjectClass *) (flowregion_parent_class))->remove_child)
138 (* ((SPObjectClass *) (flowregion_parent_class))->remove_child) (object, child);
140 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
141 }
144 static void
145 sp_flowregion_update (SPObject *object, SPCtx *ctx, unsigned int flags)
146 {
147 SPFlowregion *group;
148 SPObject *child;
149 SPItemCtx *ictx, cctx;
150 GSList *l;
152 group = SP_FLOWREGION (object);
153 ictx = (SPItemCtx *) ctx;
154 cctx = *ictx;
156 if (((SPObjectClass *) (flowregion_parent_class))->update)
157 ((SPObjectClass *) (flowregion_parent_class))->update (object, ctx, flags);
159 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
160 flags &= SP_OBJECT_MODIFIED_CASCADE;
162 l = NULL;
163 for (child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
164 g_object_ref (G_OBJECT (child));
165 l = g_slist_prepend (l, child);
166 }
167 l = g_slist_reverse (l);
168 while (l) {
169 child = SP_OBJECT (l->data);
170 l = g_slist_remove (l, child);
171 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
172 if (SP_IS_ITEM (child)) {
173 SPItem const &chi = *SP_ITEM(child);
174 cctx.i2doc = chi.transform * ictx->i2doc;
175 cctx.i2vp = chi.transform * ictx->i2vp;
176 child->updateDisplay((SPCtx *)&cctx, flags);
177 } else {
178 child->updateDisplay(ctx, flags);
179 }
180 }
181 g_object_unref (G_OBJECT (child));
182 }
184 group->UpdateComputed();
185 }
187 void SPFlowregion::UpdateComputed(void)
188 {
189 SPObject* object=SP_OBJECT(this);
191 NR::Matrix itr_mat=sp_item_i2root_affine (SP_ITEM(object));
192 itr_mat=itr_mat.inverse();
194 for (std::vector<Shape*>::iterator it = computed.begin() ; it != computed.end() ; it++)
195 delete *it;
196 computed.clear();
198 for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
199 Shape *shape = NULL;
200 GetDest(child,&shape,itr_mat);
201 computed.push_back(shape);
202 }
203 }
205 static void
206 sp_flowregion_modified (SPObject *object, guint flags)
207 {
208 SPFlowregion *group;
209 SPObject *child;
210 GSList *l;
212 group = SP_FLOWREGION (object);
214 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
215 flags &= SP_OBJECT_MODIFIED_CASCADE;
217 l = NULL;
218 for (child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
219 g_object_ref (G_OBJECT (child));
220 l = g_slist_prepend (l, child);
221 }
222 l = g_slist_reverse (l);
223 while (l) {
224 child = SP_OBJECT (l->data);
225 l = g_slist_remove (l, child);
226 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
227 child->emitModified(flags);
228 }
229 g_object_unref (G_OBJECT (child));
230 }
231 }
233 static Inkscape::XML::Node *
234 sp_flowregion_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
235 {
236 if (flags & SP_OBJECT_WRITE_BUILD) {
237 if ( repr == NULL ) {
238 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
239 repr = xml_doc->createElement("svg:flowRegion");
240 }
242 GSList *l = NULL;
243 for ( SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
244 Inkscape::XML::Node *crepr = child->updateRepr(NULL, flags);
245 if (crepr) l = g_slist_prepend(l, crepr);
246 }
248 while (l) {
249 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
250 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
251 l = g_slist_remove(l, l->data);
252 }
254 } else {
255 for ( SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
256 child->updateRepr(flags);
257 }
258 }
260 if (((SPObjectClass *) (flowregion_parent_class))->write)
261 ((SPObjectClass *) (flowregion_parent_class))->write (object, 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 }
414 NR::Matrix itr_mat=sp_item_i2root_affine (SP_ITEM(object));
415 itr_mat=itr_mat.inverse();
417 for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
418 GetDest(child,&computed,itr_mat);
419 }
420 }
422 static void
423 sp_flowregionexclude_modified (SPObject *object, guint flags)
424 {
425 SPFlowregionExclude *group;
426 SPObject *child;
427 GSList *l;
429 group = SP_FLOWREGIONEXCLUDE (object);
431 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
432 flags &= SP_OBJECT_MODIFIED_CASCADE;
434 l = NULL;
435 for (child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
436 g_object_ref (G_OBJECT (child));
437 l = g_slist_prepend (l, child);
438 }
439 l = g_slist_reverse (l);
440 while (l) {
441 child = SP_OBJECT (l->data);
442 l = g_slist_remove (l, child);
443 if (flags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
444 child->emitModified(flags);
445 }
446 g_object_unref (G_OBJECT (child));
447 }
448 }
450 static Inkscape::XML::Node *
451 sp_flowregionexclude_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
452 {
453 if (flags & SP_OBJECT_WRITE_BUILD) {
454 if ( repr == NULL ) {
455 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
456 repr = xml_doc->createElement("svg:flowRegionExclude");
457 }
459 GSList *l = NULL;
460 for ( SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
461 Inkscape::XML::Node *crepr = child->updateRepr(NULL, flags);
462 if (crepr) l = g_slist_prepend(l, crepr);
463 }
465 while (l) {
466 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
467 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
468 l = g_slist_remove(l, l->data);
469 }
471 } else {
472 for ( SPObject *child = sp_object_first_child(object) ; child != NULL; child = SP_OBJECT_NEXT(child) ) {
473 child->updateRepr(flags);
474 }
475 }
477 if (((SPObjectClass *) (flowregionexclude_parent_class))->write)
478 ((SPObjectClass *) (flowregionexclude_parent_class))->write (object, repr, flags);
480 return repr;
481 }
484 static gchar *sp_flowregionexclude_description(SPItem *item)
485 {
486 /* TRANSLATORS: A region "cut out of" a flow region; text is not allowed to flow inside the
487 * flow excluded region. flowRegionExclude in SVG 1.2: see
488 * http://www.w3.org/TR/2004/WD-SVG12-20041027/flow.html#flowRegion-elem and
489 * http://www.w3.org/TR/2004/WD-SVG12-20041027/flow.html#flowRegionExclude-elem. */
490 return g_strdup_printf(_("Flow excluded region"));
491 }
493 /*
494 *
495 */
497 static void UnionShape(Shape **base_shape, Shape const *add_shape)
498 {
499 if (*base_shape == NULL)
500 *base_shape = new Shape;
501 if ( (*base_shape)->hasEdges() == false ) {
502 (*base_shape)->Copy(const_cast<Shape*>(add_shape));
503 } else if ( add_shape->hasEdges() ) {
504 Shape* temp=new Shape;
505 temp->Booleen(const_cast<Shape*>(add_shape), *base_shape, bool_op_union);
506 delete *base_shape;
507 *base_shape = temp;
508 }
509 }
511 static void GetDest(SPObject* child,Shape **computed,NR::Matrix itr_mat)
512 {
513 if ( child == NULL ) return;
515 SPCurve *curve=NULL;
517 SPObject* u_child=child;
518 if ( SP_IS_USE(u_child) ) {
519 u_child=SP_USE(u_child)->child;
520 }
521 if ( SP_IS_SHAPE (u_child) ) {
522 curve = sp_shape_get_curve (SP_SHAPE (u_child));
523 } else if ( SP_IS_TEXT (u_child) ) {
524 curve = SP_TEXT (u_child)->getNormalizedBpath ();
525 }
527 if ( curve ) {
528 Path* temp=new Path;
529 NR::Matrix tr_mat=sp_item_i2root_affine (SP_ITEM(u_child));
530 tr_mat=itr_mat*tr_mat;
531 temp->LoadArtBPath(SP_CURVE_BPATH(curve),tr_mat,true);
532 Shape* n_shp=new Shape;
533 temp->Convert(0.25);
534 temp->Fill(n_shp,0);
535 Shape* uncross=new Shape;
536 SPStyle* style=SP_OBJECT_STYLE(u_child);
537 if ( style && style->fill_rule.computed == SP_WIND_RULE_EVENODD ) {
538 uncross->ConvertToShape(n_shp,fill_oddEven);
539 } else {
540 uncross->ConvertToShape(n_shp,fill_nonZero);
541 }
542 UnionShape(computed, uncross);
543 delete uncross;
544 delete n_shp;
545 delete temp;
546 sp_curve_unref(curve);
547 } else {
548 // printf("no curve\n");
549 }
550 }