Code

Merging from trunk
[inkscape.git] / src / sp-flowtext.cpp
1 /*
2  */
4 #ifdef HAVE_CONFIG_H
5 # include "config.h"
6 #endif
7 #include <glibmm/i18n.h>
8 #include <cstring>
9 #include <string>
11 #include "attributes.h"
12 #include "xml/repr.h"
13 #include "style.h"
14 #include "inkscape.h"
15 #include "document.h"
16 #include "selection.h"
17 #include "desktop-handles.h"
18 #include "desktop.h"
19 #include "desktop-affine.h"
21 #include "xml/repr.h"
23 #include "sp-flowdiv.h"
24 #include "sp-flowregion.h"
25 #include "sp-flowtext.h"
26 #include "sp-string.h"
27 #include "sp-use.h"
28 #include "sp-rect.h"
29 #include "text-tag-attributes.h"
30 #include "text-chemistry.h"
33 #include "livarot/Shape.h"
35 #include "display/nr-arena-glyphs.h"
38 static void sp_flowtext_class_init(SPFlowtextClass *klass);
39 static void sp_flowtext_init(SPFlowtext *group);
40 static void sp_flowtext_dispose(GObject *object);
42 static void sp_flowtext_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
43 static void sp_flowtext_remove_child(SPObject *object, Inkscape::XML::Node *child);
44 static void sp_flowtext_update(SPObject *object, SPCtx *ctx, guint flags);
45 static void sp_flowtext_modified(SPObject *object, guint flags);
46 static Inkscape::XML::Node *sp_flowtext_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
47 static void sp_flowtext_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
48 static void sp_flowtext_set(SPObject *object, unsigned key, gchar const *value);
50 static void sp_flowtext_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags);
51 static void sp_flowtext_print(SPItem *item, SPPrintContext *ctx);
52 static gchar *sp_flowtext_description(SPItem *item);
53 static NRArenaItem *sp_flowtext_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags);
54 static void sp_flowtext_hide(SPItem *item, unsigned key);
56 static SPItemClass *parent_class;
58 GType
59 sp_flowtext_get_type(void)
60 {
61     static GType group_type = 0;
62     if (!group_type) {
63         GTypeInfo group_info = {
64             sizeof(SPFlowtextClass),
65             NULL,   /* base_init */
66             NULL,   /* base_finalize */
67             (GClassInitFunc) sp_flowtext_class_init,
68             NULL,   /* class_finalize */
69             NULL,   /* class_data */
70             sizeof(SPFlowtext),
71             16,     /* n_preallocs */
72             (GInstanceInitFunc) sp_flowtext_init,
73             NULL,   /* value_table */
74         };
75         group_type = g_type_register_static(SP_TYPE_ITEM, "SPFlowtext", &group_info, (GTypeFlags)0);
76     }
77     return group_type;
78 }
80 static void
81 sp_flowtext_class_init(SPFlowtextClass *klass)
82 {
83     GObjectClass *object_class = (GObjectClass *) klass;
84     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
85     SPItemClass *item_class = (SPItemClass *) klass;
87     parent_class = (SPItemClass *)g_type_class_ref(SP_TYPE_ITEM);
89     object_class->dispose = sp_flowtext_dispose;
91     sp_object_class->child_added = sp_flowtext_child_added;
92     sp_object_class->remove_child = sp_flowtext_remove_child;
93     sp_object_class->update = sp_flowtext_update;
94     sp_object_class->modified = sp_flowtext_modified;
95     sp_object_class->write = sp_flowtext_write;
96     sp_object_class->build = sp_flowtext_build;
97     sp_object_class->set = sp_flowtext_set;
99     item_class->bbox = sp_flowtext_bbox;
100     item_class->print = sp_flowtext_print;
101     item_class->description = sp_flowtext_description;
102     item_class->show = sp_flowtext_show;
103     item_class->hide = sp_flowtext_hide;
106 static void
107 sp_flowtext_init(SPFlowtext *group)
109     group->par_indent = 0;
110     new (&group->layout) Inkscape::Text::Layout();
113 static void
114 sp_flowtext_dispose(GObject *object)
116     SPFlowtext *group = (SPFlowtext*)object;
118     group->layout.~Layout();
121 static void
122 sp_flowtext_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
124     if (((SPObjectClass *) (parent_class))->child_added)
125         (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
127     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
130 /* fixme: hide (Lauris) */
132 static void
133 sp_flowtext_remove_child(SPObject *object, Inkscape::XML::Node *child)
135     if (((SPObjectClass *) (parent_class))->remove_child)
136         (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
138     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
141 static void
142 sp_flowtext_update(SPObject *object, SPCtx *ctx, unsigned flags)
144     SPFlowtext *group = SP_FLOWTEXT(object);
145     SPItemCtx *ictx = (SPItemCtx *) ctx;
146     SPItemCtx cctx = *ictx;
148     if (((SPObjectClass *) (parent_class))->update)
149         ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
151     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
152     flags &= SP_OBJECT_MODIFIED_CASCADE;
154     GSList *l = NULL;
155     for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
156         g_object_ref(G_OBJECT(child));
157         l = g_slist_prepend(l, child);
158     }
159     l = g_slist_reverse(l);
160     while (l) {
161         SPObject *child = SP_OBJECT(l->data);
162         l = g_slist_remove(l, child);
163         if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
164             if (SP_IS_ITEM(child)) {
165                 SPItem const &chi = *SP_ITEM(child);
166                 cctx.i2doc = chi.transform * ictx->i2doc;
167                 cctx.i2vp = chi.transform * ictx->i2vp;
168                 child->updateDisplay((SPCtx *)&cctx, flags);
169             } else {
170                 child->updateDisplay(ctx, flags);
171             }
172         }
173         g_object_unref(G_OBJECT(child));
174     }
176     group->rebuildLayout();
178     NRRect paintbox;
179     sp_item_invoke_bbox(group, &paintbox, Geom::identity(), TRUE);
180     for (SPItemView *v = group->display; v != NULL; v = v->next) {
181         group->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
182         nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
183         // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
184         group->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
185     }
188 static void
189 sp_flowtext_modified(SPObject *object, guint flags)
191     SPObject *ft = SP_FLOWTEXT (object);
192     SPObject *region = NULL;
194     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
195     flags &= SP_OBJECT_MODIFIED_CASCADE;
197     // FIXME: the below stanza is copied over from sp_text_modified, consider factoring it out
198     if (flags & ( SP_OBJECT_STYLE_MODIFIED_FLAG )) {
199         SPFlowtext *text = SP_FLOWTEXT(object);
200         NRRect paintbox;
201         sp_item_invoke_bbox(text, &paintbox, Geom::identity(), TRUE);
202         for (SPItemView* v = text->display; v != NULL; v = v->next) {
203             text->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
204             nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
205             text->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
206         }
207     }
209     for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
210         if (SP_IS_FLOWREGION(o)) {
211             region = o;
212             break;
213         }
214     }
216     if (!region) return;
218     if (flags || (region->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
219         region->emitModified(flags); // pass down to the region only
220     }
223 static void
224 sp_flowtext_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
226     object->_requireSVGVersion(Inkscape::Version(1, 2));
228     if (((SPObjectClass *) (parent_class))->build) {
229         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
230     }
232     sp_object_read_attr(object, "inkscape:layoutOptions");     // must happen after css has been read
235 static void
236 sp_flowtext_set(SPObject *object, unsigned key, gchar const *value)
238     SPFlowtext *group = (SPFlowtext *) object;
240     switch (key) {
241         case SP_ATTR_LAYOUT_OPTIONS: {
242             // deprecated attribute, read for backward compatibility only
243             SPCSSAttr *opts = sp_repr_css_attr((SP_OBJECT(group))->repr, "inkscape:layoutOptions");
244             {
245                 gchar const *val = sp_repr_css_property(opts, "justification", NULL);
246                 if (val != NULL && !object->style->text_align.set) {
247                     if ( strcmp(val, "0") == 0 || strcmp(val, "false") == 0 ) {
248                         object->style->text_align.value = SP_CSS_TEXT_ALIGN_LEFT;
249                     } else {
250                         object->style->text_align.value = SP_CSS_TEXT_ALIGN_JUSTIFY;
251                     }
252                     object->style->text_align.set = TRUE;
253                     object->style->text_align.inherit = FALSE;
254                     object->style->text_align.computed = object->style->text_align.value;
255                 }
256             }
257             /* no equivalent css attribute for these two (yet)
258             {
259                 gchar const *val = sp_repr_css_property(opts, "layoutAlgo", NULL);
260                 if ( val == NULL ) {
261                     group->algo = 0;
262                 } else {
263                     if ( strcmp(val, "better") == 0 ) {     // knuth-plass, never worked for general cases
264                         group->algo = 2;
265                     } else if ( strcmp(val, "simple") == 0 ) {   // greedy, but allowed lines to be compressed by up to 20% if it would make them fit
266                         group->algo = 1;
267                     } else if ( strcmp(val, "default") == 0 ) {    // the same one we use, a standard greedy
268                         group->algo = 0;
269                     }
270                 }
271             }
272             */
273             {   // This would probably translate to padding-left, if SPStyle had it.
274                 gchar const *val = sp_repr_css_property(opts, "par-indent", NULL);
275                 if ( val == NULL ) {
276                     group->par_indent = 0.0;
277                 } else {
278                     sp_repr_get_double((Inkscape::XML::Node*)opts, "par-indent", &group->par_indent);
279                 }
280             }
281             sp_repr_css_attr_unref(opts);
282             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
283             break;
284         }
285         default:
286             if (((SPObjectClass *) (parent_class))->set) {
287                 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
288             }
289             break;
290     }
293 static Inkscape::XML::Node *
294 sp_flowtext_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
296     if ( flags & SP_OBJECT_WRITE_BUILD ) {
297         if ( repr == NULL ) repr = xml_doc->createElement("svg:flowRoot");
298         GSList *l = NULL;
299         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
300             Inkscape::XML::Node *c_repr = NULL;
301             if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child)) {
302                 c_repr = child->updateRepr(xml_doc, NULL, flags);
303             }
304             if ( c_repr ) l = g_slist_prepend(l, c_repr);
305         }
306         while ( l ) {
307             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
308             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
309             l = g_slist_remove(l, l->data);
310         }
311     } else {
312         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
313             if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child) ) {
314                 child->updateRepr(flags);
315             }
316         }
317     }
319     if (((SPObjectClass *) (parent_class))->write)
320         ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
322     return repr;
325 static void
326 sp_flowtext_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const /*flags*/)
328     SPFlowtext *group = SP_FLOWTEXT(item);
329     group->layout.getBoundingBox(bbox, transform);
331     // Add stroke width
332     SPStyle* style=SP_OBJECT_STYLE (item);
333     if ( !style->stroke.isNone() ) {
334         double const scale = transform.descrim();
335         if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
336             double const width = MAX(0.125, style->stroke_width.computed * scale);
337             if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
338                 bbox->x0-=0.5*width;
339                 bbox->x1+=0.5*width;
340                 bbox->y0-=0.5*width;
341                 bbox->y1+=0.5*width;
342             }
343         }
344     }
347 static void
348 sp_flowtext_print(SPItem *item, SPPrintContext *ctx)
350     SPFlowtext *group = SP_FLOWTEXT(item);
352     NRRect pbox;
353     sp_item_invoke_bbox(item, &pbox, Geom::identity(), TRUE);
354     NRRect bbox;
355     boost::optional<Geom::Rect> bbox_maybe = sp_item_bbox_desktop(item);
356     if (!bbox_maybe) {
357         return;
358     }
359     bbox = NRRect(from_2geom(*bbox_maybe));
361     NRRect dbox;
362     dbox.x0 = 0.0;
363     dbox.y0 = 0.0;
364     dbox.x1 = sp_document_width(SP_OBJECT_DOCUMENT(item));
365     dbox.y1 = sp_document_height(SP_OBJECT_DOCUMENT(item));
366     NR::Matrix const ctm (sp_item_i2d_affine(item));
368     group->layout.print(ctx, &pbox, &dbox, &bbox, ctm);
372 static gchar *sp_flowtext_description(SPItem *item)
374     Inkscape::Text::Layout const &layout = SP_FLOWTEXT(item)->layout;
375     int const nChars = layout.iteratorToCharIndex(layout.end());
376     if (SP_FLOWTEXT(item)->has_internal_frame())
377         return g_strdup_printf(ngettext("<b>Flowed text</b> (%d character)", "<b>Flowed text</b> (%d characters)", nChars), nChars);
378     else
379         return g_strdup_printf(ngettext("<b>Linked flowed text</b> (%d character)", "<b>Linked flowed text</b> (%d characters)", nChars), nChars);
382 static NRArenaItem *
383 sp_flowtext_show(SPItem *item, NRArena *arena, unsigned/* key*/, unsigned /*flags*/)
385     SPFlowtext *group = (SPFlowtext *) item;
386     NRArenaGroup *flowed = NRArenaGroup::create(arena);
387     nr_arena_group_set_transparent(flowed, FALSE);
389     nr_arena_group_set_style(flowed, group->style);
391     // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
392     NRRect paintbox;
393     sp_item_invoke_bbox(item, &paintbox, Geom::identity(), TRUE);
394     group->layout.show(flowed, &paintbox);
396     return flowed;
399 static void
400 sp_flowtext_hide(SPItem *item, unsigned int key)
402     if (((SPItemClass *) parent_class)->hide)
403         ((SPItemClass *) parent_class)->hide(item, key);
407 /*
408  *
409  */
411 void SPFlowtext::_buildLayoutInput(SPObject *root, Shape const *exclusion_shape, std::list<Shape> *shapes, SPObject **pending_line_break_object)
413     Inkscape::Text::Layout::OptionalTextTagAttrs pi;
414     bool with_indent = false;
416     if (SP_IS_FLOWPARA(root)) {
417         // emulate par-indent with the first char's kern
418         SPObject *t = root;
419         for ( ; t != NULL && !SP_IS_FLOWTEXT(t); t = SP_OBJECT_PARENT(t));
420         if (SP_IS_FLOWTEXT(t)) {
421             double indent = SP_FLOWTEXT(t)->par_indent;
422             if (indent != 0) {
423                 with_indent = true;
424                 SVGLength sl;
425                 sl.value = sl.computed = indent;
426                 sl._set = true;
427                 pi.dx.push_back(sl);
428             }
429         }
430     }
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     }
441     for (SPObject *child = sp_object_first_child(root) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
442         if (SP_IS_STRING(child)) {
443             if (*pending_line_break_object) {
444                 if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object))
445                     layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
446                 else {
447                     layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
448                 }
449                 *pending_line_break_object = NULL;
450             }
451             if (with_indent)
452                 layout.appendText(SP_STRING(child)->string, root->style, child, &pi);
453             else
454                 layout.appendText(SP_STRING(child)->string, root->style, child);
455         } else if (SP_IS_FLOWREGION(child)) {
456             std::vector<Shape*> const &computed = SP_FLOWREGION(child)->computed;
457             for (std::vector<Shape*>::const_iterator it = computed.begin() ; it != computed.end() ; it++) {
458                 shapes->push_back(Shape());
459                 if (exclusion_shape->hasEdges())
460                     shapes->back().Booleen(*it, const_cast<Shape*>(exclusion_shape), bool_op_diff);
461                 else
462                     shapes->back().Copy(*it);
463                 layout.appendWrapShape(&shapes->back());
464             }
465         }
466         else if (!SP_IS_FLOWREGIONEXCLUDE(child) && !sp_repr_is_meta_element(child->repr))
467             _buildLayoutInput(child, exclusion_shape, shapes, pending_line_break_object);
468     }
470     if (SP_IS_FLOWDIV(root) || SP_IS_FLOWPARA(root) || SP_IS_FLOWREGIONBREAK(root) || SP_IS_FLOWLINE(root)) {
471         if (!root->hasChildren())
472             layout.appendText("", root->style, root);
473         *pending_line_break_object = root;
474     }
477 Shape* SPFlowtext::_buildExclusionShape() const
479     Shape *shape = new Shape;
480     Shape *shape_temp = new Shape;
482     for (SPObject *child = children ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
483         // RH: is it right that this shouldn't be recursive?
484         if ( SP_IS_FLOWREGIONEXCLUDE(child) ) {
485             SPFlowregionExclude *c_child = SP_FLOWREGIONEXCLUDE(child);
486             if (c_child->computed == NULL || !c_child->computed->hasEdges())
487                 continue;
488             if (shape->hasEdges()) {
489                 shape_temp->Booleen(shape, c_child->computed, bool_op_union);
490                 std::swap(shape, shape_temp);
491             } else
492                 shape->Copy(c_child->computed);
493         }
494     }
495     delete shape_temp;
496     return shape;
499 void SPFlowtext::rebuildLayout()
501     std::list<Shape> shapes;
503     layout.clear();
504     Shape *exclusion_shape = _buildExclusionShape();
505     SPObject *pending_line_break_object = NULL;
506     _buildLayoutInput(this, exclusion_shape, &shapes, &pending_line_break_object);
507     delete exclusion_shape;
508     layout.calculateFlow();
509     //g_print(layout.dumpAsText().c_str());
512 void SPFlowtext::_clearFlow(NRArenaGroup *in_arena)
514     nr_arena_item_request_render(NR_ARENA_ITEM(in_arena));
515     for (NRArenaItem *child = in_arena->children; child != NULL; ) {
516         NRArenaItem *nchild = child->next;
518         nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
519         nr_arena_item_remove_child(NR_ARENA_ITEM(in_arena), child);
521         child = nchild;
522     }
525 Inkscape::XML::Node *
526 SPFlowtext::getAsText()
528     if (!this->layout.outputExists()) return NULL;
530     SPItem *item = SP_ITEM(this);
532     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(this));
533     Inkscape::XML::Node *repr = xml_doc->createElement("svg:text");
534     repr->setAttribute("xml:space", "preserve");
535     repr->setAttribute("style", SP_OBJECT_REPR(this)->attribute("style"));
536     NR::Point anchor_point = this->layout.characterAnchorPoint(this->layout.begin());
537     sp_repr_set_svg_double(repr, "x", anchor_point[NR::X]);
538     sp_repr_set_svg_double(repr, "y", anchor_point[NR::Y]);
540     for (Inkscape::Text::Layout::iterator it = this->layout.begin() ; it != this->layout.end() ; ) {
541         Inkscape::XML::Node *line_tspan = xml_doc->createElement("svg:tspan");
542         line_tspan->setAttribute("sodipodi:role", "line");
544         Inkscape::Text::Layout::iterator it_line_end = it;
545         it_line_end.nextStartOfLine();
547         while (it != it_line_end) {
549             Inkscape::XML::Node *span_tspan = xml_doc->createElement("svg:tspan");
550             NR::Point anchor_point = this->layout.characterAnchorPoint(it);
551             // use kerning to simulate justification and whatnot
552             Inkscape::Text::Layout::iterator it_span_end = it;
553             it_span_end.nextStartOfSpan();
554             Inkscape::Text::Layout::OptionalTextTagAttrs attrs;
555             this->layout.simulateLayoutUsingKerning(it, it_span_end, &attrs);
556             // set x,y attributes only when we need to
557             bool set_x = false;
558             bool set_y = false;
559             if (!item->transform.isIdentity()) {
560                 set_x = set_y = true;
561             } else {
562                 Inkscape::Text::Layout::iterator it_chunk_start = it;
563                 it_chunk_start.thisStartOfChunk();
564                 if (it == it_chunk_start) {
565                     set_x = true;
566                     // don't set y so linespacing adjustments and things will still work
567                 }
568                 Inkscape::Text::Layout::iterator it_shape_start = it;
569                 it_shape_start.thisStartOfShape();
570                 if (it == it_shape_start)
571                     set_y = true;
572             }
573             if (set_x && !attrs.dx.empty())
574                 attrs.dx[0] = 0.0;
575             TextTagAttributes(attrs).writeTo(span_tspan);
576             if (set_x)
577                 sp_repr_set_svg_double(span_tspan, "x", anchor_point[NR::X]);  // FIXME: this will pick up the wrong end of counter-directional runs
578             if (set_y)
579                 sp_repr_set_svg_double(span_tspan, "y", anchor_point[NR::Y]);
581             SPObject *source_obj = 0;
582             void *rawptr = 0;
583             Glib::ustring::iterator span_text_start_iter;
584             this->layout.getSourceOfCharacter(it, &rawptr, &span_text_start_iter);
585             source_obj = SP_OBJECT (rawptr);
586             gchar *style_text = sp_style_write_difference((SP_IS_STRING(source_obj) ? source_obj->parent : source_obj)->style, this->style);
587             if (style_text && *style_text) {
588                 span_tspan->setAttribute("style", style_text);
589                 g_free(style_text);
590             }
592             if (SP_IS_STRING(source_obj)) {
593                 Glib::ustring *string = &SP_STRING(source_obj)->string;
594                 SPObject *span_end_obj = 0;
595                 void *rawptr = 0;
596                 Glib::ustring::iterator span_text_end_iter;
597                 this->layout.getSourceOfCharacter(it_span_end, &rawptr, &span_text_end_iter);
598                 span_end_obj = SP_OBJECT(rawptr);
599                 if (span_end_obj != source_obj) {
600                     if (it_span_end == this->layout.end()) {
601                         span_text_end_iter = span_text_start_iter;
602                         for (int i = this->layout.iteratorToCharIndex(it_span_end) - this->layout.iteratorToCharIndex(it) ; i ; --i)
603                             ++span_text_end_iter;
604                     } else
605                         span_text_end_iter = string->end();    // spans will never straddle a source boundary
606                 }
608                 if (span_text_start_iter != span_text_end_iter) {
609                     Glib::ustring new_string;
610                     while (span_text_start_iter != span_text_end_iter)
611                         new_string += *span_text_start_iter++;    // grr. no substr() with iterators
612                     Inkscape::XML::Node *new_text = xml_doc->createTextNode(new_string.c_str());
613                     span_tspan->appendChild(new_text);
614                     Inkscape::GC::release(new_text);
615                 }
616             }
617             it = it_span_end;
619             line_tspan->appendChild(span_tspan);
620             Inkscape::GC::release(span_tspan);
621         }
622         repr->appendChild(line_tspan);
623         Inkscape::GC::release(line_tspan);
624     }
626     return repr;
629 SPItem *SPFlowtext::get_frame(SPItem *after)
631     SPObject *ft = SP_OBJECT (this);
632     SPObject *region = NULL;
634     for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
635         if (SP_IS_FLOWREGION(o)) {
636             region = o;
637             break;
638         }
639     }
641     if (!region) return NULL;
643     bool past = false;
644     SPItem *frame = NULL;
646     for (SPObject *o = sp_object_first_child(SP_OBJECT(region)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
647         if (SP_IS_ITEM(o)) {
648             if (after == NULL || past) {
649                 frame = SP_ITEM(o);
650             } else {
651                 if (SP_ITEM(o) == after) {
652                     past = true;
653                 }
654             }
655         }
656     }
658     if (!frame) return NULL;
660     if (SP_IS_USE (frame)) {
661         return sp_use_get_original(SP_USE(frame));
662     } else {
663         return frame;
664     }
667 bool SPFlowtext::has_internal_frame()
669     SPItem *frame = get_frame(NULL);
671     return (frame && SP_OBJECT(this)->isAncestorOf(SP_OBJECT(frame)) && SP_IS_RECT(frame));
675 SPItem *create_flowtext_with_internal_frame (SPDesktop *desktop, NR::Point p0, NR::Point p1)
677     SPDocument *doc = sp_desktop_document (desktop);
679     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
680     Inkscape::XML::Node *root_repr = xml_doc->createElement("svg:flowRoot");
681     root_repr->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create
682     SPItem *ft_item = SP_ITEM(desktop->currentLayer()->appendChildRepr(root_repr));
683     SPObject *root_object = doc->getObjectByRepr(root_repr);
684     g_assert(SP_IS_FLOWTEXT(root_object));
686     Inkscape::XML::Node *region_repr = xml_doc->createElement("svg:flowRegion");
687     root_repr->appendChild(region_repr);
688     SPObject *region_object = doc->getObjectByRepr(region_repr);
689     g_assert(SP_IS_FLOWREGION(region_object));
691     Inkscape::XML::Node *rect_repr = xml_doc->createElement("svg:rect"); // FIXME: use path!!! after rects are converted to use path
692     region_repr->appendChild(rect_repr);
694     SPObject *rect = doc->getObjectByRepr(rect_repr);
696     p0 = sp_desktop_dt2root_xy_point(desktop, p0);
697     p1 = sp_desktop_dt2root_xy_point(desktop, p1);
698     using NR::X;
699     using NR::Y;
700     NR::Coord const x0 = MIN(p0[X], p1[X]);
701     NR::Coord const y0 = MIN(p0[Y], p1[Y]);
702     NR::Coord const x1 = MAX(p0[X], p1[X]);
703     NR::Coord const y1 = MAX(p0[Y], p1[Y]);
704     NR::Coord const w  = x1 - x0;
705     NR::Coord const h  = y1 - y0;
707     sp_rect_position_set(SP_RECT(rect), x0, y0, w, h);
708     SP_OBJECT(rect)->updateRepr();
710     Inkscape::XML::Node *para_repr = xml_doc->createElement("svg:flowPara");
711     root_repr->appendChild(para_repr);
712     SPObject *para_object = doc->getObjectByRepr(para_repr);
713     g_assert(SP_IS_FLOWPARA(para_object));
715     Inkscape::XML::Node *text = xml_doc->createTextNode("");
716     para_repr->appendChild(text);
718     Inkscape::GC::release(root_repr);
719     Inkscape::GC::release(region_repr);
720     Inkscape::GC::release(para_repr);
721     Inkscape::GC::release(rect_repr);
723     return ft_item;
727 /*
728   Local Variables:
729   mode:c++
730   c-file-style:"stroustrup"
731   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
732   indent-tabs-mode:nil
733   fill-column:99
734   End:
735 */
736 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :