Code

fix bug 1672619
[inkscape.git] / src / sp-flowtext.cpp
1 /*
2  */
4 #ifdef HAVE_CONFIG_H
5 # include "config.h"
6 #endif
7 #include <glibmm/i18n.h>
9 #include "attributes.h"
10 #include "xml/repr.h"
11 #include "style.h"
12 #include "inkscape.h"
13 #include "document.h"
14 #include "selection.h"
15 #include "desktop-handles.h"
16 #include "desktop.h"
17 #include "desktop-affine.h"
19 #include "xml/repr.h"
21 #include "sp-flowdiv.h"
22 #include "sp-flowregion.h"
23 #include "sp-flowtext.h"
24 #include "sp-string.h"
25 #include "sp-use.h"
26 #include "sp-rect.h"
27 #include "text-tag-attributes.h"
28 #include "text-chemistry.h"
31 #include "livarot/Shape.h"
33 #include "display/nr-arena-glyphs.h"
36 static void sp_flowtext_class_init(SPFlowtextClass *klass);
37 static void sp_flowtext_init(SPFlowtext *group);
38 static void sp_flowtext_dispose(GObject *object);
40 static void sp_flowtext_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
41 static void sp_flowtext_remove_child(SPObject *object, Inkscape::XML::Node *child);
42 static void sp_flowtext_update(SPObject *object, SPCtx *ctx, guint flags);
43 static void sp_flowtext_modified(SPObject *object, guint flags);
44 static Inkscape::XML::Node *sp_flowtext_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
45 static void sp_flowtext_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
46 static void sp_flowtext_set(SPObject *object, unsigned key, gchar const *value);
48 static void sp_flowtext_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
49 static void sp_flowtext_print(SPItem *item, SPPrintContext *ctx);
50 static gchar *sp_flowtext_description(SPItem *item);
51 static NRArenaItem *sp_flowtext_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags);
52 static void sp_flowtext_hide(SPItem *item, unsigned key);
54 static SPItemClass *parent_class;
56 GType
57 sp_flowtext_get_type(void)
58 {
59     static GType group_type = 0;
60     if (!group_type) {
61         GTypeInfo group_info = {
62             sizeof(SPFlowtextClass),
63             NULL,   /* base_init */
64             NULL,   /* base_finalize */
65             (GClassInitFunc) sp_flowtext_class_init,
66             NULL,   /* class_finalize */
67             NULL,   /* class_data */
68             sizeof(SPFlowtext),
69             16,     /* n_preallocs */
70             (GInstanceInitFunc) sp_flowtext_init,
71             NULL,   /* value_table */
72         };
73         group_type = g_type_register_static(SP_TYPE_ITEM, "SPFlowtext", &group_info, (GTypeFlags)0);
74     }
75     return group_type;
76 }
78 static void
79 sp_flowtext_class_init(SPFlowtextClass *klass)
80 {
81     GObjectClass *object_class = (GObjectClass *) klass;
82     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
83     SPItemClass *item_class = (SPItemClass *) klass;
85     parent_class = (SPItemClass *)g_type_class_ref(SP_TYPE_ITEM);
87     object_class->dispose = sp_flowtext_dispose;
89     sp_object_class->child_added = sp_flowtext_child_added;
90     sp_object_class->remove_child = sp_flowtext_remove_child;
91     sp_object_class->update = sp_flowtext_update;
92     sp_object_class->modified = sp_flowtext_modified;
93     sp_object_class->write = sp_flowtext_write;
94     sp_object_class->build = sp_flowtext_build;
95     sp_object_class->set = sp_flowtext_set;
97     item_class->bbox = sp_flowtext_bbox;
98     item_class->print = sp_flowtext_print;
99     item_class->description = sp_flowtext_description;
100     item_class->show = sp_flowtext_show;
101     item_class->hide = sp_flowtext_hide;
104 static void
105 sp_flowtext_init(SPFlowtext *group)
107     group->par_indent = 0;
108     new (&group->layout) Inkscape::Text::Layout();
111 static void
112 sp_flowtext_dispose(GObject *object)
114     SPFlowtext *group = (SPFlowtext*)object;
116     group->layout.~Layout();
119 static void
120 sp_flowtext_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
122     if (((SPObjectClass *) (parent_class))->child_added)
123         (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
125     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
128 /* fixme: hide (Lauris) */
130 static void
131 sp_flowtext_remove_child(SPObject *object, Inkscape::XML::Node *child)
133     if (((SPObjectClass *) (parent_class))->remove_child)
134         (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
136     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
139 static void
140 sp_flowtext_update(SPObject *object, SPCtx *ctx, unsigned flags)
142     SPFlowtext *group = SP_FLOWTEXT(object);
143     SPItemCtx *ictx = (SPItemCtx *) ctx;
144     SPItemCtx cctx = *ictx;
146     if (((SPObjectClass *) (parent_class))->update)
147         ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
149     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
150     flags &= SP_OBJECT_MODIFIED_CASCADE;
152     GSList *l = NULL;
153     for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
154         g_object_ref(G_OBJECT(child));
155         l = g_slist_prepend(l, child);
156     }
157     l = g_slist_reverse(l);
158     while (l) {
159         SPObject *child = SP_OBJECT(l->data);
160         l = g_slist_remove(l, child);
161         if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
162             if (SP_IS_ITEM(child)) {
163                 SPItem const &chi = *SP_ITEM(child);
164                 cctx.i2doc = chi.transform * ictx->i2doc;
165                 cctx.i2vp = chi.transform * ictx->i2vp;
166                 child->updateDisplay((SPCtx *)&cctx, flags);
167             } else {
168                 child->updateDisplay(ctx, flags);
169             }
170         }
171         g_object_unref(G_OBJECT(child));
172     }
174     group->rebuildLayout();
176     NRRect paintbox;
177     sp_item_invoke_bbox(group, &paintbox, NR::identity(), TRUE);
178     for (SPItemView *v = group->display; v != NULL; v = v->next) {
179         group->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
180         nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
181         // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
182         group->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
183     }
186 static void
187 sp_flowtext_modified(SPObject *object, guint flags)
189     SPObject *ft = SP_FLOWTEXT (object);
190     SPObject *region = NULL;
192     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
193     flags &= SP_OBJECT_MODIFIED_CASCADE;
195     // FIXME: the below stanza is copied over from sp_text_modified, consider factoring it out
196     if (flags & ( SP_OBJECT_STYLE_MODIFIED_FLAG )) {
197         SPFlowtext *text = SP_FLOWTEXT(object);
198         NRRect paintbox;
199         sp_item_invoke_bbox(text, &paintbox, NR::identity(), TRUE);
200         for (SPItemView* v = text->display; v != NULL; v = v->next) {
201             text->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
202             nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
203             text->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
204         }
205     }
207     for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
208         if (SP_IS_FLOWREGION(o)) {
209             region = o;
210             break;
211         }
212     }
214     if (!region) return;
216     if (flags || (region->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
217         region->emitModified(flags); // pass down to the region only
218     }
221 static void
222 sp_flowtext_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
224     object->_requireSVGVersion(Inkscape::Version(1, 2));
226     if (((SPObjectClass *) (parent_class))->build) {
227         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
228     }
230     sp_object_read_attr(object, "inkscape:layoutOptions");     // must happen after css has been read
233 static void
234 sp_flowtext_set(SPObject *object, unsigned key, gchar const *value)
236     SPFlowtext *group = (SPFlowtext *) object;
238     switch (key) {
239         case SP_ATTR_LAYOUT_OPTIONS: {
240             // deprecated attribute, read for backward compatibility only
241             SPCSSAttr *opts = sp_repr_css_attr((SP_OBJECT(group))->repr, "inkscape:layoutOptions");
242             {
243                 gchar const *val = sp_repr_css_property(opts, "justification", NULL);
244                 if (val != NULL && !object->style->text_align.set) {
245                     if ( strcmp(val, "0") == 0 || strcmp(val, "false") == 0 ) {
246                         object->style->text_align.value = SP_CSS_TEXT_ALIGN_LEFT;
247                     } else {
248                         object->style->text_align.value = SP_CSS_TEXT_ALIGN_JUSTIFY;
249                     }
250                     object->style->text_align.set = TRUE;
251                     object->style->text_align.inherit = FALSE;
252                     object->style->text_align.computed = object->style->text_align.value;
253                 }
254             }
255             /* no equivalent css attribute for these two (yet)
256             {
257                 gchar const *val = sp_repr_css_property(opts, "layoutAlgo", NULL);
258                 if ( val == NULL ) {
259                     group->algo = 0;
260                 } else {
261                     if ( strcmp(val, "better") == 0 ) {     // knuth-plass, never worked for general cases
262                         group->algo = 2;
263                     } else if ( strcmp(val, "simple") == 0 ) {   // greedy, but allowed lines to be compressed by up to 20% if it would make them fit
264                         group->algo = 1;
265                     } else if ( strcmp(val, "default") == 0 ) {    // the same one we use, a standard greedy
266                         group->algo = 0;
267                     }
268                 }
269             }
270             */
271             {   // This would probably translate to padding-left, if SPStyle had it.
272                 gchar const *val = sp_repr_css_property(opts, "par-indent", NULL);
273                 if ( val == NULL ) {
274                     group->par_indent = 0.0;
275                 } else {
276                     sp_repr_get_double((Inkscape::XML::Node*)opts, "par-indent", &group->par_indent);
277                 }
278             }
279             sp_repr_css_attr_unref(opts);
280             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
281             break;
282         }
283         default:
284             if (((SPObjectClass *) (parent_class))->set) {
285                 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
286             }
287             break;
288     }
291 static Inkscape::XML::Node *
292 sp_flowtext_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
294     if ( flags & SP_OBJECT_WRITE_BUILD ) {
295         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
296         if ( repr == NULL ) repr = xml_doc->createElement("svg:flowRoot");
297         GSList *l = NULL;
298         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
299             Inkscape::XML::Node *c_repr = NULL;
300             if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child)) {
301                 c_repr = child->updateRepr(NULL, flags);
302             }
303             if ( c_repr ) l = g_slist_prepend(l, c_repr);
304         }
305         while ( l ) {
306             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
307             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
308             l = g_slist_remove(l, l->data);
309         }
310     } else {
311         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
312             if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child) ) {
313                 child->updateRepr(flags);
314             }
315         }
316     }
318     if (((SPObjectClass *) (parent_class))->write)
319         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
321     return repr;
324 static void
325 sp_flowtext_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
327     SPFlowtext *group = SP_FLOWTEXT(item);
328     group->layout.getBoundingBox(bbox, transform);
330     // Add stroke width
331     SPStyle* style=SP_OBJECT_STYLE (item);
332     if (style->stroke.type != SP_PAINT_TYPE_NONE) {
333         double const scale = expansion(transform);
334         if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
335             double const width = MAX(0.125, style->stroke_width.computed * scale);
336             if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
337                 bbox->x0-=0.5*width;
338                 bbox->x1+=0.5*width;
339                 bbox->y0-=0.5*width;
340                 bbox->y1+=0.5*width;
341             }
342         }
343     }
346 static void
347 sp_flowtext_print(SPItem *item, SPPrintContext *ctx)
349     SPFlowtext *group = SP_FLOWTEXT(item);
351     NRRect pbox;
352     sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
353     NRRect bbox;
354     sp_item_bbox_desktop(item, &bbox);
355     NRRect dbox;
356     dbox.x0 = 0.0;
357     dbox.y0 = 0.0;
358     dbox.x1 = sp_document_width(SP_OBJECT_DOCUMENT(item));
359     dbox.y1 = sp_document_height(SP_OBJECT_DOCUMENT(item));
360     NR::Matrix const ctm = sp_item_i2d_affine(item);
362     group->layout.print(ctx, &pbox, &dbox, &bbox, ctm);
366 static gchar *sp_flowtext_description(SPItem *item)
368     Inkscape::Text::Layout const &layout = SP_FLOWTEXT(item)->layout;
369     int const nChars = layout.iteratorToCharIndex(layout.end());
370     if (SP_FLOWTEXT(item)->has_internal_frame())
371         return g_strdup_printf(ngettext("<b>Flowed text</b> (%d character)", "<b>Flowed text</b> (%d characters)", nChars), nChars);
372     else
373         return g_strdup_printf(ngettext("<b>Linked flowed text</b> (%d character)", "<b>Linked flowed text</b> (%d characters)", nChars), nChars);
376 static NRArenaItem *
377 sp_flowtext_show(SPItem *item, NRArena *arena, unsigned/* key*/, unsigned /*flags*/)
379     SPFlowtext *group = (SPFlowtext *) item;
380     NRArenaGroup *flowed = NRArenaGroup::create(arena);
381     nr_arena_group_set_transparent(flowed, FALSE);
383     nr_arena_group_set_style(flowed, group->style);
385     // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
386     NRRect paintbox;
387     sp_item_invoke_bbox(item, &paintbox, NR::identity(), TRUE);
388     group->layout.show(flowed, &paintbox);
390     return flowed;
393 static void
394 sp_flowtext_hide(SPItem *item, unsigned int key)
396     if (((SPItemClass *) parent_class)->hide)
397         ((SPItemClass *) parent_class)->hide(item, key);
401 /*
402  *
403  */
405 void SPFlowtext::_buildLayoutInput(SPObject *root, Shape const *exclusion_shape, std::list<Shape> *shapes, SPObject **pending_line_break_object)
407     Inkscape::Text::Layout::OptionalTextTagAttrs pi;
408     bool with_indent = false;
410     if (SP_IS_FLOWPARA(root)) {
411         // emulate par-indent with the first char's kern
412         SPObject *t = root;
413         for ( ; t != NULL && !SP_IS_FLOWTEXT(t); t = SP_OBJECT_PARENT(t));
414         if (SP_IS_FLOWTEXT(t)) {
415             double indent = SP_FLOWTEXT(t)->par_indent;
416             if (indent != 0) {
417                 with_indent = true;
418                 SVGLength sl;
419                 sl.value = sl.computed = indent;
420                 sl._set = true;
421                 pi.dx.push_back(sl);
422             }
423         }
424     }
426     if (*pending_line_break_object) {
427         if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object)) {
428             layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
429         } else {
430             layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
431         }
432         *pending_line_break_object = NULL;
433     }
435     for (SPObject *child = sp_object_first_child(root) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
436         if (SP_IS_STRING(child)) {
437             if (*pending_line_break_object) {
438                 if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object))
439                     layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
440                 else {
441                     layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
442                 }
443                 *pending_line_break_object = NULL;
444             }
445             if (with_indent)
446                 layout.appendText(SP_STRING(child)->string, root->style, child, &pi);
447             else
448                 layout.appendText(SP_STRING(child)->string, root->style, child);
449         } else if (SP_IS_FLOWREGION(child)) {
450             std::vector<Shape*> const &computed = SP_FLOWREGION(child)->computed;
451             for (std::vector<Shape*>::const_iterator it = computed.begin() ; it != computed.end() ; it++) {
452                 shapes->push_back(Shape());
453                 if (exclusion_shape->hasEdges())
454                     shapes->back().Booleen(*it, const_cast<Shape*>(exclusion_shape), bool_op_diff);
455                 else
456                     shapes->back().Copy(*it);
457                 layout.appendWrapShape(&shapes->back());
458             }
459         }
460         else if (!SP_IS_FLOWREGIONEXCLUDE(child))
461             _buildLayoutInput(child, exclusion_shape, shapes, pending_line_break_object);
462     }
464     if (SP_IS_FLOWDIV(root) || SP_IS_FLOWPARA(root) || SP_IS_FLOWREGIONBREAK(root) || SP_IS_FLOWLINE(root)) {
465         if (!root->hasChildren())
466             layout.appendText("", root->style, root);
467         *pending_line_break_object = root;
468     }
471 Shape* SPFlowtext::_buildExclusionShape() const
473     Shape *shape = new Shape;
474     Shape *shape_temp = new Shape;
476     for (SPObject *child = children ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
477         // RH: is it right that this shouldn't be recursive?
478         if ( SP_IS_FLOWREGIONEXCLUDE(child) ) {
479             SPFlowregionExclude *c_child = SP_FLOWREGIONEXCLUDE(child);
480             if (c_child->computed == NULL || !c_child->computed->hasEdges())
481                 continue;
482             if (shape->hasEdges()) {
483                 shape_temp->Booleen(shape, c_child->computed, bool_op_union);
484                 std::swap(shape, shape_temp);
485             } else
486                 shape->Copy(c_child->computed);
487         }
488     }
489     delete shape_temp;
490     return shape;
493 void SPFlowtext::rebuildLayout()
495     std::list<Shape> shapes;
497     layout.clear();
498     Shape *exclusion_shape = _buildExclusionShape();
499     SPObject *pending_line_break_object = NULL;
500     _buildLayoutInput(this, exclusion_shape, &shapes, &pending_line_break_object);
501     delete exclusion_shape;
502     layout.calculateFlow();
503     //g_print(layout.dumpAsText().c_str());
506 void SPFlowtext::_clearFlow(NRArenaGroup *in_arena)
508     nr_arena_item_request_render(NR_ARENA_ITEM(in_arena));
509     for (NRArenaItem *child = in_arena->children; child != NULL; ) {
510         NRArenaItem *nchild = child->next;
512         nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
513         nr_arena_item_remove_child(NR_ARENA_ITEM(in_arena), child);
515         child = nchild;
516     }
519 Inkscape::XML::Node *
520 SPFlowtext::getAsText()
522     if (!this->layout.outputExists()) return NULL;
524     SPItem *item = SP_ITEM(this);
526     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(this));
527     Inkscape::XML::Node *repr = xml_doc->createElement("svg:text");
528     repr->setAttribute("xml:space", "preserve");
529     repr->setAttribute("style", SP_OBJECT_REPR(this)->attribute("style"));
530     NR::Point anchor_point = this->layout.characterAnchorPoint(this->layout.begin());
531     sp_repr_set_svg_double(repr, "x", anchor_point[NR::X]);
532     sp_repr_set_svg_double(repr, "y", anchor_point[NR::Y]);
534     for (Inkscape::Text::Layout::iterator it = this->layout.begin() ; it != this->layout.end() ; ) {
535         Inkscape::XML::Node *line_tspan = xml_doc->createElement("svg:tspan");
536         line_tspan->setAttribute("sodipodi:role", "line");
538         Inkscape::Text::Layout::iterator it_line_end = it;
539         it_line_end.nextStartOfLine();
541         while (it != it_line_end) {
543             Inkscape::XML::Node *span_tspan = xml_doc->createElement("svg:tspan");
544             NR::Point anchor_point = this->layout.characterAnchorPoint(it);
545             // use kerning to simulate justification and whatnot
546             Inkscape::Text::Layout::iterator it_span_end = it;
547             it_span_end.nextStartOfSpan();
548             Inkscape::Text::Layout::OptionalTextTagAttrs attrs;
549             this->layout.simulateLayoutUsingKerning(it, it_span_end, &attrs);
550             // set x,y attributes only when we need to
551             bool set_x = false;
552             bool set_y = false;
553             if (!item->transform.test_identity()) {
554                 set_x = set_y = true;
555             } else {
556                 Inkscape::Text::Layout::iterator it_chunk_start = it;
557                 it_chunk_start.thisStartOfChunk();
558                 if (it == it_chunk_start) {
559                     set_x = true;
560                     // don't set y so linespacing adjustments and things will still work
561                 }
562                 Inkscape::Text::Layout::iterator it_shape_start = it;
563                 it_shape_start.thisStartOfShape();
564                 if (it == it_shape_start)
565                     set_y = true;
566             }
567             if (set_x && !attrs.dx.empty())
568                 attrs.dx[0] = 0.0;
569             TextTagAttributes(attrs).writeTo(span_tspan);
570             if (set_x)
571                 sp_repr_set_svg_double(span_tspan, "x", anchor_point[NR::X]);  // FIXME: this will pick up the wrong end of counter-directional runs
572             if (set_y)
573                 sp_repr_set_svg_double(span_tspan, "y", anchor_point[NR::Y]);
575             SPObject *source_obj = 0;
576             void *rawptr = 0;
577             Glib::ustring::iterator span_text_start_iter;
578             this->layout.getSourceOfCharacter(it, &rawptr, &span_text_start_iter);
579             source_obj = SP_OBJECT (rawptr);
580             gchar *style_text = sp_style_write_difference((SP_IS_STRING(source_obj) ? source_obj->parent : source_obj)->style, this->style);
581             if (style_text && *style_text) {
582                 span_tspan->setAttribute("style", style_text);
583                 g_free(style_text);
584             }
586             if (SP_IS_STRING(source_obj)) {
587                 Glib::ustring *string = &SP_STRING(source_obj)->string;
588                 SPObject *span_end_obj = 0;
589                 void *rawptr = 0;
590                 Glib::ustring::iterator span_text_end_iter;
591                 this->layout.getSourceOfCharacter(it_span_end, &rawptr, &span_text_end_iter);
592                 span_end_obj = SP_OBJECT(rawptr);
593                 if (span_end_obj != source_obj) {
594                     if (it_span_end == this->layout.end()) {
595                         span_text_end_iter = span_text_start_iter;
596                         for (int i = this->layout.iteratorToCharIndex(it_span_end) - this->layout.iteratorToCharIndex(it) ; i ; --i)
597                             ++span_text_end_iter;
598                     } else
599                         span_text_end_iter = string->end();    // spans will never straddle a source boundary
600                 }
602                 if (span_text_start_iter != span_text_end_iter) {
603                     Glib::ustring new_string;
604                     while (span_text_start_iter != span_text_end_iter)
605                         new_string += *span_text_start_iter++;    // grr. no substr() with iterators
606                     Inkscape::XML::Node *new_text = xml_doc->createTextNode(new_string.c_str());
607                     span_tspan->appendChild(new_text);
608                     Inkscape::GC::release(new_text);
609                 }
610             }
611             it = it_span_end;
613             line_tspan->appendChild(span_tspan);
614             Inkscape::GC::release(span_tspan);
615         }
616         repr->appendChild(line_tspan);
617         Inkscape::GC::release(line_tspan);
618     }
620     return repr;
623 SPItem *SPFlowtext::get_frame(SPItem *after)
625     SPObject *ft = SP_OBJECT (this);
626     SPObject *region = NULL;
628     for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
629         if (SP_IS_FLOWREGION(o)) {
630             region = o;
631             break;
632         }
633     }
635     if (!region) return NULL;
637     bool past = false;
638     SPItem *frame = NULL;
640     for (SPObject *o = sp_object_first_child(SP_OBJECT(region)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
641         if (SP_IS_ITEM(o)) {
642             if (after == NULL || past) {
643                 frame = SP_ITEM(o);
644             } else {
645                 if (SP_ITEM(o) == after) {
646                     past = true;
647                 }
648             }
649         }
650     }
652     if (!frame) return NULL;
654     if (SP_IS_USE (frame)) {
655         return sp_use_get_original(SP_USE(frame));
656     } else {
657         return frame;
658     }
661 bool SPFlowtext::has_internal_frame()
663     SPItem *frame = get_frame(NULL);
665     return (frame && SP_OBJECT(this)->isAncestorOf(SP_OBJECT(frame)) && SP_IS_RECT(frame));
669 SPItem *create_flowtext_with_internal_frame (SPDesktop *desktop, NR::Point p0, NR::Point p1)
671     SPDocument *doc = sp_desktop_document (desktop);
673     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
674     Inkscape::XML::Node *root_repr = xml_doc->createElement("svg:flowRoot");
675     root_repr->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create
676     SPItem *ft_item = SP_ITEM(desktop->currentLayer()->appendChildRepr(root_repr));
677     SPObject *root_object = doc->getObjectByRepr(root_repr);
678     g_assert(SP_IS_FLOWTEXT(root_object));
680     Inkscape::XML::Node *region_repr = xml_doc->createElement("svg:flowRegion");
681     root_repr->appendChild(region_repr);
682     SPObject *region_object = doc->getObjectByRepr(region_repr);
683     g_assert(SP_IS_FLOWREGION(region_object));
685     Inkscape::XML::Node *rect_repr = xml_doc->createElement("svg:rect"); // FIXME: use path!!! after rects are converted to use path
686     region_repr->appendChild(rect_repr);
688     SPObject *rect = doc->getObjectByRepr(rect_repr);
690     p0 = sp_desktop_dt2root_xy_point(desktop, p0);
691     p1 = sp_desktop_dt2root_xy_point(desktop, p1);
692     using NR::X;
693     using NR::Y;
694     NR::Coord const x0 = MIN(p0[X], p1[X]);
695     NR::Coord const y0 = MIN(p0[Y], p1[Y]);
696     NR::Coord const x1 = MAX(p0[X], p1[X]);
697     NR::Coord const y1 = MAX(p0[Y], p1[Y]);
698     NR::Coord const w  = x1 - x0;
699     NR::Coord const h  = y1 - y0;
701     sp_rect_position_set(SP_RECT(rect), x0, y0, w, h);
702     SP_OBJECT(rect)->updateRepr();
704     Inkscape::XML::Node *para_repr = xml_doc->createElement("svg:flowPara");
705     root_repr->appendChild(para_repr);
706     SPObject *para_object = doc->getObjectByRepr(para_repr);
707     g_assert(SP_IS_FLOWPARA(para_object));
709     Inkscape::XML::Node *text = xml_doc->createTextNode("");
710     para_repr->appendChild(text);
712     Inkscape::GC::release(root_repr);
713     Inkscape::GC::release(region_repr);
714     Inkscape::GC::release(para_repr);
715     Inkscape::GC::release(rect_repr);
717     return ft_item;
721 /*
722   Local Variables:
723   mode:c++
724   c-file-style:"stroustrup"
725   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
726   indent-tabs-mode:nil
727   fill-column:99
728   End:
729 */
730 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :