Code

port the fix from sp-text.cpp rev 11260 to fix bug 1628173
[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"
30 #include "livarot/Shape.h"
32 #include "display/nr-arena-glyphs.h"
35 static void sp_flowtext_class_init(SPFlowtextClass *klass);
36 static void sp_flowtext_init(SPFlowtext *group);
37 static void sp_flowtext_dispose(GObject *object);
39 static void sp_flowtext_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
40 static void sp_flowtext_remove_child(SPObject *object, Inkscape::XML::Node *child);
41 static void sp_flowtext_update(SPObject *object, SPCtx *ctx, guint flags);
42 static void sp_flowtext_modified(SPObject *object, guint flags);
43 static Inkscape::XML::Node *sp_flowtext_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
44 static void sp_flowtext_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
45 static void sp_flowtext_set(SPObject *object, unsigned key, gchar const *value);
47 static void sp_flowtext_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
48 static void sp_flowtext_print(SPItem *item, SPPrintContext *ctx);
49 static gchar *sp_flowtext_description(SPItem *item);
50 static NRArenaItem *sp_flowtext_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags);
51 static void sp_flowtext_hide(SPItem *item, unsigned key);
53 static SPItemClass *parent_class;
55 GType
56 sp_flowtext_get_type(void)
57 {
58     static GType group_type = 0;
59     if (!group_type) {
60         GTypeInfo group_info = {
61             sizeof(SPFlowtextClass),
62             NULL,   /* base_init */
63             NULL,   /* base_finalize */
64             (GClassInitFunc) sp_flowtext_class_init,
65             NULL,   /* class_finalize */
66             NULL,   /* class_data */
67             sizeof(SPFlowtext),
68             16,     /* n_preallocs */
69             (GInstanceInitFunc) sp_flowtext_init,
70             NULL,   /* value_table */
71         };
72         group_type = g_type_register_static(SP_TYPE_ITEM, "SPFlowtext", &group_info, (GTypeFlags)0);
73     }
74     return group_type;
75 }
77 static void
78 sp_flowtext_class_init(SPFlowtextClass *klass)
79 {
80     GObjectClass *object_class = (GObjectClass *) klass;
81     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
82     SPItemClass *item_class = (SPItemClass *) klass;
84     parent_class = (SPItemClass *)g_type_class_ref(SP_TYPE_ITEM);
86     object_class->dispose = sp_flowtext_dispose;
88     sp_object_class->child_added = sp_flowtext_child_added;
89     sp_object_class->remove_child = sp_flowtext_remove_child;
90     sp_object_class->update = sp_flowtext_update;
91     sp_object_class->modified = sp_flowtext_modified;
92     sp_object_class->write = sp_flowtext_write;
93     sp_object_class->build = sp_flowtext_build;
94     sp_object_class->set = sp_flowtext_set;
96     item_class->bbox = sp_flowtext_bbox;
97     item_class->print = sp_flowtext_print;
98     item_class->description = sp_flowtext_description;
99     item_class->show = sp_flowtext_show;
100     item_class->hide = sp_flowtext_hide;
103 static void
104 sp_flowtext_init(SPFlowtext *group)
106     group->par_indent = 0;
107     new (&group->layout) Inkscape::Text::Layout();
110 static void
111 sp_flowtext_dispose(GObject *object)
113     SPFlowtext *group = (SPFlowtext*)object;
115     group->layout.~Layout();
118 static void
119 sp_flowtext_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
121     if (((SPObjectClass *) (parent_class))->child_added)
122         (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
124     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
127 /* fixme: hide (Lauris) */
129 static void
130 sp_flowtext_remove_child(SPObject *object, Inkscape::XML::Node *child)
132     if (((SPObjectClass *) (parent_class))->remove_child)
133         (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
135     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
138 static void
139 sp_flowtext_update(SPObject *object, SPCtx *ctx, unsigned flags)
141     SPFlowtext *group = SP_FLOWTEXT(object);
142     SPItemCtx *ictx = (SPItemCtx *) ctx;
143     SPItemCtx cctx = *ictx;
145     if (((SPObjectClass *) (parent_class))->update)
146         ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
148     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
149     flags &= SP_OBJECT_MODIFIED_CASCADE;
151     GSList *l = NULL;
152     for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
153         g_object_ref(G_OBJECT(child));
154         l = g_slist_prepend(l, child);
155     }
156     l = g_slist_reverse(l);
157     while (l) {
158         SPObject *child = SP_OBJECT(l->data);
159         l = g_slist_remove(l, child);
160         if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
161             if (SP_IS_ITEM(child)) {
162                 SPItem const &chi = *SP_ITEM(child);
163                 cctx.i2doc = chi.transform * ictx->i2doc;
164                 cctx.i2vp = chi.transform * ictx->i2vp;
165                 child->updateDisplay((SPCtx *)&cctx, flags);
166             } else {
167                 child->updateDisplay(ctx, flags);
168             }
169         }
170         g_object_unref(G_OBJECT(child));
171     }
173     group->rebuildLayout();
175     // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
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         group->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
181     }
184 static void
185 sp_flowtext_modified(SPObject *object, guint flags)
187     SPObject *ft = SP_FLOWTEXT (object);
188     SPObject *region = NULL;
190     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
191     flags &= SP_OBJECT_MODIFIED_CASCADE;
193     // FIXME: the below stanza is copied over from sp_text_modified, consider factoring it out
194     if (flags & ( SP_OBJECT_STYLE_MODIFIED_FLAG )) {
195         SPFlowtext *text = SP_FLOWTEXT(object);
196         NRRect paintbox;
197         sp_item_invoke_bbox(text, &paintbox, NR::identity(), TRUE);
198         for (SPItemView* v = text->display; v != NULL; v = v->next) {
199             text->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
200             nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
201             text->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
202         }
203     }
205     for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
206         if (SP_IS_FLOWREGION(o)) {
207             region = o;
208             break;
209         }
210     }
212     if (!region) return;
214     if (flags || (region->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
215         region->emitModified(flags); // pass down to the region only
216     }
219 static void
220 sp_flowtext_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
222     object->_requireSVGVersion(Inkscape::Version(1, 2));
224     if (((SPObjectClass *) (parent_class))->build) {
225         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
226     }
228     sp_object_read_attr(object, "inkscape:layoutOptions");     // must happen after css has been read
231 static void
232 sp_flowtext_set(SPObject *object, unsigned key, gchar const *value)
234     SPFlowtext *group = (SPFlowtext *) object;
236     switch (key) {
237         case SP_ATTR_LAYOUT_OPTIONS: {
238             // deprecated attribute, read for backward compatibility only
239             SPCSSAttr *opts = sp_repr_css_attr((SP_OBJECT(group))->repr, "inkscape:layoutOptions");
240             {
241                 gchar const *val = sp_repr_css_property(opts, "justification", NULL);
242                 if (val != NULL && !object->style->text_align.set) {
243                     if ( strcmp(val, "0") == 0 || strcmp(val, "false") == 0 ) {
244                         object->style->text_align.value = SP_CSS_TEXT_ALIGN_LEFT;
245                     } else {
246                         object->style->text_align.value = SP_CSS_TEXT_ALIGN_JUSTIFY;
247                     }
248                     object->style->text_align.set = TRUE;
249                     object->style->text_align.inherit = FALSE;
250                     object->style->text_align.computed = object->style->text_align.value;
251                 }
252             }
253             /* no equivalent css attribute for these two (yet)
254             {
255                 gchar const *val = sp_repr_css_property(opts, "layoutAlgo", NULL);
256                 if ( val == NULL ) {
257                     group->algo = 0;
258                 } else {
259                     if ( strcmp(val, "better") == 0 ) {     // knuth-plass, never worked for general cases
260                         group->algo = 2;
261                     } else if ( strcmp(val, "simple") == 0 ) {   // greedy, but allowed lines to be compressed by up to 20% if it would make them fit
262                         group->algo = 1;
263                     } else if ( strcmp(val, "default") == 0 ) {    // the same one we use, a standard greedy
264                         group->algo = 0;
265                     }
266                 }
267             }
268             */
269             {   // This would probably translate to padding-left, if SPStyle had it.
270                 gchar const *val = sp_repr_css_property(opts, "par-indent", NULL);
271                 if ( val == NULL ) {
272                     group->par_indent = 0.0;
273                 } else {
274                     sp_repr_get_double((Inkscape::XML::Node*)opts, "par-indent", &group->par_indent);
275                 }
276             }
277             sp_repr_css_attr_unref(opts);
278             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
279             break;
280         }
281         default:
282             if (((SPObjectClass *) (parent_class))->set) {
283                 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
284             }
285             break;
286     }
289 static Inkscape::XML::Node *
290 sp_flowtext_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
292     if ( flags & SP_OBJECT_WRITE_BUILD ) {
293         if ( repr == NULL ) repr = sp_repr_new("svg:flowRoot");
294         GSList *l = NULL;
295         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
296             Inkscape::XML::Node *c_repr = NULL;
297             if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child)) {
298                 c_repr = child->updateRepr(NULL, flags);
299             }
300             if ( c_repr ) l = g_slist_prepend(l, c_repr);
301         }
302         while ( l ) {
303             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
304             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
305             l = g_slist_remove(l, l->data);
306         }
307     } else {
308         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
309             if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child) ) {
310                 child->updateRepr(flags);
311             }
312         }
313     }
315     if (((SPObjectClass *) (parent_class))->write)
316         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
318     return repr;
321 static void
322 sp_flowtext_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
324     SPFlowtext *group = SP_FLOWTEXT(item);
325     group->layout.getBoundingBox(bbox, transform);
327     // Add stroke width
328     SPStyle* style=SP_OBJECT_STYLE (item);
329     if (style->stroke.type != SP_PAINT_TYPE_NONE) {
330         double const scale = expansion(transform);
331         if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
332             double const width = MAX(0.125, style->stroke_width.computed * scale);
333             if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
334                 bbox->x0-=0.5*width;
335                 bbox->x1+=0.5*width;
336                 bbox->y0-=0.5*width;
337                 bbox->y1+=0.5*width;
338             }
339         }
340     }
343 static void
344 sp_flowtext_print(SPItem *item, SPPrintContext *ctx)
346     SPFlowtext *group = SP_FLOWTEXT(item);
348     NRRect pbox;
349     sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
350     NRRect bbox;
351     sp_item_bbox_desktop(item, &bbox);
352     NRRect dbox;
353     dbox.x0 = 0.0;
354     dbox.y0 = 0.0;
355     dbox.x1 = sp_document_width(SP_OBJECT_DOCUMENT(item));
356     dbox.y1 = sp_document_height(SP_OBJECT_DOCUMENT(item));
357     NR::Matrix const ctm = sp_item_i2d_affine(item);
359     group->layout.print(ctx, &pbox, &dbox, &bbox, ctm);
363 static gchar *sp_flowtext_description(SPItem *item)
365     Inkscape::Text::Layout const &layout = SP_FLOWTEXT(item)->layout;
366     int const nChars = layout.iteratorToCharIndex(layout.end());
367     if (SP_FLOWTEXT(item)->has_internal_frame())
368         return g_strdup_printf(ngettext("<b>Flowed text</b> (%d character)", "<b>Flowed text</b> (%d characters)", nChars), nChars);
369     else
370         return g_strdup_printf(ngettext("<b>Linked flowed text</b> (%d character)", "<b>Linked flowed text</b> (%d characters)", nChars), nChars);
373 static NRArenaItem *
374 sp_flowtext_show(SPItem *item, NRArena *arena, unsigned/* key*/, unsigned /*flags*/)
376     SPFlowtext *group = (SPFlowtext *) item;
377     NRArenaGroup *flowed = NRArenaGroup::create(arena);
378     nr_arena_group_set_transparent(flowed, FALSE);
380     // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
381     NRRect paintbox;
382     sp_item_invoke_bbox(item, &paintbox, NR::identity(), TRUE);
383     group->layout.show(flowed, &paintbox);
385     return flowed;
388 static void
389 sp_flowtext_hide(SPItem *item, unsigned int key)
391     if (((SPItemClass *) parent_class)->hide)
392         ((SPItemClass *) parent_class)->hide(item, key);
396 /*
397  *
398  */
400 void SPFlowtext::_buildLayoutInput(SPObject *root, Shape const *exclusion_shape, std::list<Shape> *shapes, SPObject **pending_line_break_object)
402     Inkscape::Text::Layout::OptionalTextTagAttrs pi;
403     bool with_indent = false;
405     if (SP_IS_FLOWPARA(root)) {
406         // emulate par-indent with the first char's kern
407         SPObject *t = root;
408         for ( ; t != NULL && !SP_IS_FLOWTEXT(t); t = SP_OBJECT_PARENT(t));
409         if (SP_IS_FLOWTEXT(t)) {
410             double indent = SP_FLOWTEXT(t)->par_indent;
411             if (indent != 0) {
412                 with_indent = true;
413                 SVGLength sl;
414                 sl.value = sl.computed = indent;
415                 sl._set = true;
416                 pi.dx.push_back(sl);
417             }
418         }
419     }
421     if (*pending_line_break_object) {
422         if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object)) {
423             layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
424         } else {
425             layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
426         }
427         *pending_line_break_object = NULL;
428     }
430     for (SPObject *child = sp_object_first_child(root) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
431         if (SP_IS_STRING(child)) {
432             if (*pending_line_break_object) {
433                 if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object))
434                     layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
435                 else {
436                     layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
437                 }
438                 *pending_line_break_object = NULL;
439             }
440             if (with_indent)
441                 layout.appendText(SP_STRING(child)->string, root->style, child, &pi);
442             else
443                 layout.appendText(SP_STRING(child)->string, root->style, child);
444         } else if (SP_IS_FLOWREGION(child)) {
445             std::vector<Shape*> const &computed = SP_FLOWREGION(child)->computed;
446             for (std::vector<Shape*>::const_iterator it = computed.begin() ; it != computed.end() ; it++) {
447                 shapes->push_back(Shape());
448                 if (exclusion_shape->hasEdges())
449                     shapes->back().Booleen(*it, const_cast<Shape*>(exclusion_shape), bool_op_diff);
450                 else
451                     shapes->back().Copy(*it);
452                 layout.appendWrapShape(&shapes->back());
453             }
454         }
455         else if (!SP_IS_FLOWREGIONEXCLUDE(child))
456             _buildLayoutInput(child, exclusion_shape, shapes, pending_line_break_object);
457     }
459     if (SP_IS_FLOWDIV(root) || SP_IS_FLOWPARA(root) || SP_IS_FLOWREGIONBREAK(root) || SP_IS_FLOWLINE(root)) {
460         if (!root->hasChildren())
461             layout.appendText("", root->style, root);
462         *pending_line_break_object = root;
463     }
466 Shape* SPFlowtext::_buildExclusionShape() const
468     Shape *shape = new Shape;
469     Shape *shape_temp = new Shape;
471     for (SPObject *child = children ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
472         // RH: is it right that this shouldn't be recursive?
473         if ( SP_IS_FLOWREGIONEXCLUDE(child) ) {
474             SPFlowregionExclude *c_child = SP_FLOWREGIONEXCLUDE(child);
475             if (c_child->computed == NULL || !c_child->computed->hasEdges())
476                 continue;
477             if (shape->hasEdges()) {
478                 shape_temp->Booleen(shape, c_child->computed, bool_op_union);
479                 std::swap(shape, shape_temp);
480             } else
481                 shape->Copy(c_child->computed);
482         }
483     }
484     delete shape_temp;
485     return shape;
488 void SPFlowtext::rebuildLayout()
490     std::list<Shape> shapes;
492     layout.clear();
493     Shape *exclusion_shape = _buildExclusionShape();
494     SPObject *pending_line_break_object = NULL;
495     _buildLayoutInput(this, exclusion_shape, &shapes, &pending_line_break_object);
496     delete exclusion_shape;
497     layout.calculateFlow();
498     //g_print(layout.dumpAsText().c_str());
501 void SPFlowtext::_clearFlow(NRArenaGroup *in_arena)
503     nr_arena_item_request_render(NR_ARENA_ITEM(in_arena));
504     for (NRArenaItem *child = in_arena->children; child != NULL; ) {
505         NRArenaItem *nchild = child->next;
507         nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
508         nr_arena_item_remove_child(NR_ARENA_ITEM(in_arena), child);
510         child = nchild;
511     }
514 void SPFlowtext::convert_to_text()
516     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
517     Inkscape::Selection *selection = sp_desktop_selection(desktop);
518     SPItem *item = selection->singleItem();
519     if (!SP_IS_FLOWTEXT(item)) return;
521     SPFlowtext *group = SP_FLOWTEXT(item);
523     if (!group->layout.outputExists()) return;
525     Inkscape::XML::Node *repr = sp_repr_new("svg:text");
526     repr->setAttribute("xml:space", "preserve");
527     repr->setAttribute("style", SP_OBJECT_REPR(group)->attribute("style"));
528     NR::Point anchor_point = group->layout.characterAnchorPoint(group->layout.begin());
529     sp_repr_set_svg_double(repr, "x", anchor_point[NR::X]);
530     sp_repr_set_svg_double(repr, "y", anchor_point[NR::Y]);
532     for (Inkscape::Text::Layout::iterator it = group->layout.begin() ; it != group->layout.end() ; ) {
534             Inkscape::XML::Node *line_tspan = sp_repr_new("svg:tspan");
535         line_tspan->setAttribute("sodipodi:role", "line");
537         Inkscape::Text::Layout::iterator it_line_end = it;
538         it_line_end.nextStartOfLine();
539         while (it != it_line_end) {
541                 Inkscape::XML::Node *span_tspan = sp_repr_new("svg:tspan");
542             NR::Point anchor_point = group->layout.characterAnchorPoint(it);
543             // use kerning to simulate justification and whatnot
544             Inkscape::Text::Layout::iterator it_span_end = it;
545             it_span_end.nextStartOfSpan();
546             Inkscape::Text::Layout::OptionalTextTagAttrs attrs;
547             group->layout.simulateLayoutUsingKerning(it, it_span_end, &attrs);
548             // set x,y attributes only when we need to
549             bool set_x = false;
550             bool set_y = false;
551             if (!item->transform.test_identity()) {
552                 set_x = set_y = true;
553             } else {
554                 Inkscape::Text::Layout::iterator it_chunk_start = it;
555                 it_chunk_start.thisStartOfChunk();
556                 if (it == it_chunk_start) {
557                     set_x = true;
558                     // don't set y so linespacing adjustments and things will still work
559                 }
560                 Inkscape::Text::Layout::iterator it_shape_start = it;
561                 it_shape_start.thisStartOfShape();
562                 if (it == it_shape_start)
563                     set_y = true;
564             }
565             if (set_x && !attrs.dx.empty())
566                 attrs.dx[0] = 0.0;
567             TextTagAttributes(attrs).writeTo(span_tspan);
568             if (set_x)
569                 sp_repr_set_svg_double(span_tspan, "x", anchor_point[NR::X]);  // FIXME: this will pick up the wrong end of counter-directional runs
570             if (set_y)
571                 sp_repr_set_svg_double(span_tspan, "y", anchor_point[NR::Y]);
573             SPObject *source_obj = 0;
574             void *rawptr = 0;
575             Glib::ustring::iterator span_text_start_iter;
576             group->layout.getSourceOfCharacter(it, &rawptr, &span_text_start_iter);
577             source_obj = SP_OBJECT (rawptr);
578             gchar *style_text = sp_style_write_difference((SP_IS_STRING(source_obj) ? source_obj->parent : source_obj)->style, group->style);
579             if (style_text && *style_text) {
580                 span_tspan->setAttribute("style", style_text);
581                 g_free(style_text);
582             }
584             if (SP_IS_STRING(source_obj)) {
585                 Glib::ustring *string = &SP_STRING(source_obj)->string;
586                 SPObject *span_end_obj = 0;
587                 void *rawptr = 0;
588                 Glib::ustring::iterator span_text_end_iter;
589                 group->layout.getSourceOfCharacter(it_span_end, &rawptr, &span_text_end_iter);
590                 span_end_obj = SP_OBJECT(rawptr);
591                 if (span_end_obj != source_obj) {
592                     if (it_span_end == group->layout.end()) {
593                         span_text_end_iter = span_text_start_iter;
594                         for (int i = group->layout.iteratorToCharIndex(it_span_end) - group->layout.iteratorToCharIndex(it) ; i ; --i)
595                             ++span_text_end_iter;
596                     } else
597                         span_text_end_iter = string->end();    // spans will never straddle a source boundary
598                 }
600                 if (span_text_start_iter != span_text_end_iter) {
601                     Glib::ustring new_string;
602                     while (span_text_start_iter != span_text_end_iter)
603                         new_string += *span_text_start_iter++;    // grr. no substr() with iterators
604                     Inkscape::XML::Node *new_text = sp_repr_new_text(new_string.c_str());
605                     span_tspan->appendChild(new_text);
606                     Inkscape::GC::release(new_text);
607                 }
608             }
609             it = it_span_end;
611             line_tspan->appendChild(span_tspan);
612                 Inkscape::GC::release(span_tspan);
613         }
614         repr->appendChild(line_tspan);
615             Inkscape::GC::release(line_tspan);
616     }
618     Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
619     parent->appendChild(repr);
620     SPItem *new_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
621     sp_item_write_transform(new_item, repr, item->transform);
622     SP_OBJECT(new_item)->updateRepr();
624     Inkscape::GC::release(repr);
625     selection->set(new_item);
626     item->deleteObject();
628     sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, 
629                      _("Convert flowed text to text"));
632 SPItem *SPFlowtext::get_frame(SPItem *after)
634     SPObject *ft = SP_OBJECT (this);
635     SPObject *region = NULL;
637     for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
638         if (SP_IS_FLOWREGION(o)) {
639             region = o;
640             break;
641         }
642     }
644     if (!region) return NULL;
646     bool past = false;
647     SPItem *frame = NULL;
649     for (SPObject *o = sp_object_first_child(SP_OBJECT(region)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
650         if (SP_IS_ITEM(o)) {
651             if (after == NULL || past) {
652                 frame = SP_ITEM(o);
653             } else {
654                 if (SP_ITEM(o) == after) {
655                     past = true;
656                 }
657             }
658         }
659     }
661     if (!frame) return NULL;
663     if (SP_IS_USE (frame)) {
664         return sp_use_get_original(SP_USE(frame));
665     } else {
666         return frame;
667     }
670 bool SPFlowtext::has_internal_frame()
672     SPItem *frame = get_frame(NULL);
674     return (frame && SP_OBJECT(this)->isAncestorOf(SP_OBJECT(frame)) && SP_IS_RECT(frame));
678 SPItem *create_flowtext_with_internal_frame (SPDesktop *desktop, NR::Point p0, NR::Point p1)
680     SPDocument *doc = sp_desktop_document (desktop);
682     Inkscape::XML::Node *root_repr = sp_repr_new("svg:flowRoot");
683     root_repr->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create
684     SPItem *ft_item = SP_ITEM(desktop->currentLayer()->appendChildRepr(root_repr));
685     SPObject *root_object = doc->getObjectByRepr(root_repr);
686     g_assert(SP_IS_FLOWTEXT(root_object));
688     Inkscape::XML::Node *region_repr = sp_repr_new("svg:flowRegion");
689     root_repr->appendChild(region_repr);
690     SPObject *region_object = doc->getObjectByRepr(region_repr);
691     g_assert(SP_IS_FLOWREGION(region_object));
693     Inkscape::XML::Node *rect_repr = sp_repr_new("svg:rect"); // FIXME: use path!!! after rects are converted to use path
694     region_repr->appendChild(rect_repr);
696     SPObject *rect = doc->getObjectByRepr(rect_repr);
698     p0 = sp_desktop_dt2root_xy_point(desktop, p0);
699     p1 = sp_desktop_dt2root_xy_point(desktop, p1);
700     using NR::X;
701     using NR::Y;
702     NR::Coord const x0 = MIN(p0[X], p1[X]);
703     NR::Coord const y0 = MIN(p0[Y], p1[Y]);
704     NR::Coord const x1 = MAX(p0[X], p1[X]);
705     NR::Coord const y1 = MAX(p0[Y], p1[Y]);
706     NR::Coord const w  = x1 - x0;
707     NR::Coord const h  = y1 - y0;
709     sp_rect_position_set(SP_RECT(rect), x0, y0, w, h);
710     SP_OBJECT(rect)->updateRepr();
712     Inkscape::XML::Node *para_repr = sp_repr_new("svg:flowPara");
713     root_repr->appendChild(para_repr);
714     SPObject *para_object = doc->getObjectByRepr(para_repr);
715     g_assert(SP_IS_FLOWPARA(para_object));
717     Inkscape::XML::Node *text = sp_repr_new_text("");
718     para_repr->appendChild(text);
720     Inkscape::GC::release(root_repr);
721     Inkscape::GC::release(region_repr);
722     Inkscape::GC::release(para_repr);
723     Inkscape::GC::release(rect_repr);
725     return ft_item;
729 /*
730   Local Variables:
731   mode:c++
732   c-file-style:"stroustrup"
733   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
734   indent-tabs-mode:nil
735   fill-column:99
736   End:
737 */
738 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :