Code

A simple layout document as to what, why and how is cppification.
[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"
20 #include "xml/repr.h"
22 #include "sp-flowdiv.h"
23 #include "sp-flowregion.h"
24 #include "sp-flowtext.h"
25 #include "sp-string.h"
26 #include "sp-use.h"
27 #include "sp-rect.h"
28 #include "text-tag-attributes.h"
29 #include "text-chemistry.h"
30 #include "text-editing.h"
32 #include "livarot/Shape.h"
34 #include "display/nr-arena-glyphs.h"
37 static void sp_flowtext_class_init(SPFlowtextClass *klass);
38 static void sp_flowtext_init(SPFlowtext *group);
39 static void sp_flowtext_dispose(GObject *object);
41 static void sp_flowtext_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
42 static void sp_flowtext_remove_child(SPObject *object, Inkscape::XML::Node *child);
43 static void sp_flowtext_update(SPObject *object, SPCtx *ctx, guint flags);
44 static void sp_flowtext_modified(SPObject *object, guint flags);
45 static Inkscape::XML::Node *sp_flowtext_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
46 static void sp_flowtext_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
47 static void sp_flowtext_set(SPObject *object, unsigned key, gchar const *value);
49 static void sp_flowtext_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags);
50 static void sp_flowtext_print(SPItem *item, SPPrintContext *ctx);
51 static gchar *sp_flowtext_description(SPItem *item);
52 static void sp_flowtext_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const *snapprefs);
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->snappoints = sp_flowtext_snappoints;
103     item_class->show = sp_flowtext_show;
104     item_class->hide = sp_flowtext_hide;
107 static void
108 sp_flowtext_init(SPFlowtext *group)
110     group->par_indent = 0;
111     new (&group->layout) Inkscape::Text::Layout();
114 static void
115 sp_flowtext_dispose(GObject *object)
117     SPFlowtext *group = (SPFlowtext*)object;
119     group->layout.~Layout();
122 static void
123 sp_flowtext_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
125     if (((SPObjectClass *) (parent_class))->child_added)
126         (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
128     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
131 /* fixme: hide (Lauris) */
133 static void
134 sp_flowtext_remove_child(SPObject *object, Inkscape::XML::Node *child)
136     if (((SPObjectClass *) (parent_class))->remove_child)
137         (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
139     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
142 static void
143 sp_flowtext_update(SPObject *object, SPCtx *ctx, unsigned flags)
145     SPFlowtext *group = SP_FLOWTEXT(object);
146     SPItemCtx *ictx = (SPItemCtx *) ctx;
147     SPItemCtx cctx = *ictx;
149     if (((SPObjectClass *) (parent_class))->update)
150         ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
152     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
153     flags &= SP_OBJECT_MODIFIED_CASCADE;
155     GSList *l = NULL;
156     for (SPObject *child = object->first_child() ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
157         g_object_ref(G_OBJECT(child));
158         l = g_slist_prepend(l, child);
159     }
160     l = g_slist_reverse(l);
161     while (l) {
162         SPObject *child = SP_OBJECT(l->data);
163         l = g_slist_remove(l, child);
164         if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
165             if (SP_IS_ITEM(child)) {
166                 SPItem const &chi = *SP_ITEM(child);
167                 cctx.i2doc = chi.transform * ictx->i2doc;
168                 cctx.i2vp = chi.transform * ictx->i2vp;
169                 child->updateDisplay((SPCtx *)&cctx, flags);
170             } else {
171                 child->updateDisplay(ctx, flags);
172             }
173         }
174         g_object_unref(G_OBJECT(child));
175     }
177     group->rebuildLayout();
179     NRRect paintbox;
180     group->invoke_bbox( &paintbox, Geom::identity(), TRUE);
181     for (SPItemView *v = group->display; v != NULL; v = v->next) {
182         group->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
183         nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
184         // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
185         group->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
186     }
189 static void
190 sp_flowtext_modified(SPObject *object, guint flags)
192     SPObject *ft = SP_FLOWTEXT (object);
193     SPObject *region = NULL;
195     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
196     flags &= SP_OBJECT_MODIFIED_CASCADE;
198     // FIXME: the below stanza is copied over from sp_text_modified, consider factoring it out
199     if (flags & ( SP_OBJECT_STYLE_MODIFIED_FLAG )) {
200         SPFlowtext *text = SP_FLOWTEXT(object);
201         NRRect paintbox;
202         text->invoke_bbox( &paintbox, Geom::identity(), TRUE);
203         for (SPItemView* v = text->display; v != NULL; v = v->next) {
204             text->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
205             nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
206             text->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
207         }
208     }
210     for (SPObject *o = SP_OBJECT(ft)->first_child() ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
211         if (SP_IS_FLOWREGION(o)) {
212             region = o;
213             break;
214         }
215     }
217     if (!region) return;
219     if (flags || (region->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
220         region->emitModified(flags); // pass down to the region only
221     }
224 static void
225 sp_flowtext_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
227     object->_requireSVGVersion(Inkscape::Version(1, 2));
229     if (((SPObjectClass *) (parent_class))->build) {
230         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
231     }
233     object->readAttr( "inkscape:layoutOptions");     // must happen after css has been read
236 static void
237 sp_flowtext_set(SPObject *object, unsigned key, gchar const *value)
239     SPFlowtext *group = (SPFlowtext *) object;
241     switch (key) {
242         case SP_ATTR_LAYOUT_OPTIONS: {
243             // deprecated attribute, read for backward compatibility only
244                         //XML Tree being directly used while it shouldn't be.                                                    
245             SPCSSAttr *opts = sp_repr_css_attr((SP_OBJECT(group))->getRepr(), "inkscape:layoutOptions");
246             {
247                 gchar const *val = sp_repr_css_property(opts, "justification", NULL);
248                 if (val != NULL && !object->style->text_align.set) {
249                     if ( strcmp(val, "0") == 0 || strcmp(val, "false") == 0 ) {
250                         object->style->text_align.value = SP_CSS_TEXT_ALIGN_LEFT;
251                     } else {
252                         object->style->text_align.value = SP_CSS_TEXT_ALIGN_JUSTIFY;
253                     }
254                     object->style->text_align.set = TRUE;
255                     object->style->text_align.inherit = FALSE;
256                     object->style->text_align.computed = object->style->text_align.value;
257                 }
258             }
259             /* no equivalent css attribute for these two (yet)
260             {
261                 gchar const *val = sp_repr_css_property(opts, "layoutAlgo", NULL);
262                 if ( val == NULL ) {
263                     group->algo = 0;
264                 } else {
265                     if ( strcmp(val, "better") == 0 ) {     // knuth-plass, never worked for general cases
266                         group->algo = 2;
267                     } else if ( strcmp(val, "simple") == 0 ) {   // greedy, but allowed lines to be compressed by up to 20% if it would make them fit
268                         group->algo = 1;
269                     } else if ( strcmp(val, "default") == 0 ) {    // the same one we use, a standard greedy
270                         group->algo = 0;
271                     }
272                 }
273             }
274             */
275             {   // This would probably translate to padding-left, if SPStyle had it.
276                 gchar const *val = sp_repr_css_property(opts, "par-indent", NULL);
277                 if ( val == NULL ) {
278                     group->par_indent = 0.0;
279                 } else {
280                     sp_repr_get_double((Inkscape::XML::Node*)opts, "par-indent", &group->par_indent);
281                 }
282             }
283             sp_repr_css_attr_unref(opts);
284             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
285             break;
286         }
287         default:
288             if (((SPObjectClass *) (parent_class))->set) {
289                 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
290             }
291             break;
292     }
295 static Inkscape::XML::Node *
296 sp_flowtext_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
298     if ( flags & SP_OBJECT_WRITE_BUILD ) {
299         if ( repr == NULL ) repr = xml_doc->createElement("svg:flowRoot");
300         GSList *l = NULL;
301         for (SPObject *child = object->first_child() ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
302             Inkscape::XML::Node *c_repr = NULL;
303             if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child)) {
304                 c_repr = child->updateRepr(xml_doc, NULL, flags);
305             }
306             if ( c_repr ) l = g_slist_prepend(l, c_repr);
307         }
308         while ( l ) {
309             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
310             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
311             l = g_slist_remove(l, l->data);
312         }
313     } else {
314         for (SPObject *child = object->first_child() ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
315             if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child) ) {
316                 child->updateRepr(flags);
317             }
318         }
319     }
321     if (((SPObjectClass *) (parent_class))->write)
322         ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
324     return repr;
327 static void
328 sp_flowtext_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const /*flags*/)
330     SPFlowtext *group = SP_FLOWTEXT(item);
331     group->layout.getBoundingBox(bbox, transform);
333     // Add stroke width
334     SPStyle* style=SP_OBJECT_STYLE (item);
335     if ( !style->stroke.isNone() ) {
336         double const scale = transform.descrim();
337         if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
338             double const width = MAX(0.125, style->stroke_width.computed * scale);
339             if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
340                 bbox->x0-=0.5*width;
341                 bbox->x1+=0.5*width;
342                 bbox->y0-=0.5*width;
343                 bbox->y1+=0.5*width;
344             }
345         }
346     }
349 static void
350 sp_flowtext_print(SPItem *item, SPPrintContext *ctx)
352     SPFlowtext *group = SP_FLOWTEXT(item);
354     NRRect pbox;
355     item->invoke_bbox( &pbox, Geom::identity(), TRUE);
356     NRRect bbox;
357     Geom::OptRect bbox_maybe = item->getBboxDesktop();
358     if (!bbox_maybe) {
359         return;
360     }
361     bbox = NRRect(from_2geom(*bbox_maybe));
363     NRRect dbox;
364     dbox.x0 = 0.0;
365     dbox.y0 = 0.0;
366     dbox.x1 = SP_OBJECT_DOCUMENT(item)->getWidth();
367     dbox.y1 = SP_OBJECT_DOCUMENT(item)->getHeight();
368     Geom::Matrix const ctm (item->i2d_affine());
370     group->layout.print(ctx, &pbox, &dbox, &bbox, ctm);
374 static gchar *sp_flowtext_description(SPItem *item)
376     Inkscape::Text::Layout const &layout = SP_FLOWTEXT(item)->layout;
377     int const nChars = layout.iteratorToCharIndex(layout.end());
379     char const *trunc = (layout.inputTruncated()) ? _(" [truncated]") : "";
381     if (SP_FLOWTEXT(item)->has_internal_frame()) {
382         return g_strdup_printf(ngettext("<b>Flowed text</b> (%d character%s)", "<b>Flowed text</b> (%d characters%s)", nChars), nChars, trunc);
383     } else {
384         return g_strdup_printf(ngettext("<b>Linked flowed text</b> (%d character%s)", "<b>Linked flowed text</b> (%d characters%s)", nChars), nChars, trunc);
385     }
388 static void sp_flowtext_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const */*snapprefs*/)
390     // Choose a point on the baseline for snapping from or to, with the horizontal position
391     // of this point depending on the text alignment (left vs. right)
392     Inkscape::Text::Layout const *layout = te_get_layout((SPItem *) item);
393     if (layout != NULL && layout->outputExists()) {
394         boost::optional<Geom::Point> pt = layout->baselineAnchorPoint();
395         if (pt) {
396             p.push_back(Inkscape::SnapCandidatePoint((*pt) * item->i2d_affine(), Inkscape::SNAPSOURCE_TEXT_BASELINE, Inkscape::SNAPTARGET_TEXT_BASELINE));
397         }
398     }
401 static NRArenaItem *
402 sp_flowtext_show(SPItem *item, NRArena *arena, unsigned/* key*/, unsigned /*flags*/)
404     SPFlowtext *group = (SPFlowtext *) item;
405     NRArenaGroup *flowed = NRArenaGroup::create(arena);
406     nr_arena_group_set_transparent(flowed, FALSE);
408     nr_arena_group_set_style(flowed, group->style);
410     // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
411     NRRect paintbox;
412     item->invoke_bbox( &paintbox, Geom::identity(), TRUE);
413     group->layout.show(flowed, &paintbox);
415     return flowed;
418 static void
419 sp_flowtext_hide(SPItem *item, unsigned int key)
421     if (((SPItemClass *) parent_class)->hide)
422         ((SPItemClass *) parent_class)->hide(item, key);
426 /*
427  *
428  */
430 void SPFlowtext::_buildLayoutInput(SPObject *root, Shape const *exclusion_shape, std::list<Shape> *shapes, SPObject **pending_line_break_object)
432     Inkscape::Text::Layout::OptionalTextTagAttrs pi;
433     bool with_indent = false;
435     if (SP_IS_FLOWPARA(root)) {
436         // emulate par-indent with the first char's kern
437         SPObject *t = root;
438         for ( ; t != NULL && !SP_IS_FLOWTEXT(t); t = SP_OBJECT_PARENT(t)){};
439         if (SP_IS_FLOWTEXT(t)) {
440             double indent = SP_FLOWTEXT(t)->par_indent;
441             if (indent != 0) {
442                 with_indent = true;
443                 SVGLength sl;
444                 sl.value = sl.computed = indent;
445                 sl._set = true;
446                 pi.dx.push_back(sl);
447             }
448         }
449     }
451     if (*pending_line_break_object) {
452         if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object)) {
453             layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
454         } else {
455             layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
456         }
457         *pending_line_break_object = NULL;
458     }
460     for (SPObject *child = root->first_child() ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
461         if (SP_IS_STRING(child)) {
462             if (*pending_line_break_object) {
463                 if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object))
464                     layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
465                 else {
466                     layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
467                 }
468                 *pending_line_break_object = NULL;
469             }
470             if (with_indent)
471                 layout.appendText(SP_STRING(child)->string, root->style, child, &pi);
472             else
473                 layout.appendText(SP_STRING(child)->string, root->style, child);
474         } else if (SP_IS_FLOWREGION(child)) {
475             std::vector<Shape*> const &computed = SP_FLOWREGION(child)->computed;
476             for (std::vector<Shape*>::const_iterator it = computed.begin() ; it != computed.end() ; it++) {
477                 shapes->push_back(Shape());
478                 if (exclusion_shape->hasEdges())
479                     shapes->back().Booleen(*it, const_cast<Shape*>(exclusion_shape), bool_op_diff);
480                 else
481                     shapes->back().Copy(*it);
482                 layout.appendWrapShape(&shapes->back());
483             }
484         }
485                 //XML Tree is being directly used while it shouldn't be.
486         else if (!SP_IS_FLOWREGIONEXCLUDE(child) && !sp_repr_is_meta_element(child->getRepr()))
487             _buildLayoutInput(child, exclusion_shape, shapes, pending_line_break_object);
488     }
490     if (SP_IS_FLOWDIV(root) || SP_IS_FLOWPARA(root) || SP_IS_FLOWREGIONBREAK(root) || SP_IS_FLOWLINE(root)) {
491         if (!root->hasChildren())
492             layout.appendText("", root->style, root);
493         *pending_line_break_object = root;
494     }
497 Shape* SPFlowtext::_buildExclusionShape() const
499     Shape *shape = new Shape;
500     Shape *shape_temp = new Shape;
502     for (SPObject *child = children ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
503         // RH: is it right that this shouldn't be recursive?
504         if ( SP_IS_FLOWREGIONEXCLUDE(child) ) {
505             SPFlowregionExclude *c_child = SP_FLOWREGIONEXCLUDE(child);
506             if (c_child->computed == NULL || !c_child->computed->hasEdges())
507                 continue;
508             if (shape->hasEdges()) {
509                 shape_temp->Booleen(shape, c_child->computed, bool_op_union);
510                 std::swap(shape, shape_temp);
511             } else
512                 shape->Copy(c_child->computed);
513         }
514     }
515     delete shape_temp;
516     return shape;
519 void SPFlowtext::rebuildLayout()
521     std::list<Shape> shapes;
523     layout.clear();
524     Shape *exclusion_shape = _buildExclusionShape();
525     SPObject *pending_line_break_object = NULL;
526     _buildLayoutInput(this, exclusion_shape, &shapes, &pending_line_break_object);
527     delete exclusion_shape;
528     layout.calculateFlow();
529     //g_print(layout.dumpAsText().c_str());
532 void SPFlowtext::_clearFlow(NRArenaGroup *in_arena)
534     nr_arena_item_request_render(NR_ARENA_ITEM(in_arena));
535     for (NRArenaItem *child = in_arena->children; child != NULL; ) {
536         NRArenaItem *nchild = child->next;
538         nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
539         nr_arena_item_remove_child(NR_ARENA_ITEM(in_arena), child);
541         child = nchild;
542     }
545 Inkscape::XML::Node *
546 SPFlowtext::getAsText()
548     if (!this->layout.outputExists()) return NULL;
550     SPItem *item = SP_ITEM(this);
552     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(this));
553     Inkscape::XML::Node *repr = xml_doc->createElement("svg:text");
554     repr->setAttribute("xml:space", "preserve");
555     repr->setAttribute("style", SP_OBJECT_REPR(this)->attribute("style"));
556     Geom::Point anchor_point = this->layout.characterAnchorPoint(this->layout.begin());
557     sp_repr_set_svg_double(repr, "x", anchor_point[Geom::X]);
558     sp_repr_set_svg_double(repr, "y", anchor_point[Geom::Y]);
560     for (Inkscape::Text::Layout::iterator it = this->layout.begin() ; it != this->layout.end() ; ) {
561         Inkscape::XML::Node *line_tspan = xml_doc->createElement("svg:tspan");
562         line_tspan->setAttribute("sodipodi:role", "line");
564         Inkscape::Text::Layout::iterator it_line_end = it;
565         it_line_end.nextStartOfLine();
567         while (it != it_line_end) {
569             Inkscape::XML::Node *span_tspan = xml_doc->createElement("svg:tspan");
570             Geom::Point anchor_point = this->layout.characterAnchorPoint(it);
571             // use kerning to simulate justification and whatnot
572             Inkscape::Text::Layout::iterator it_span_end = it;
573             it_span_end.nextStartOfSpan();
574             Inkscape::Text::Layout::OptionalTextTagAttrs attrs;
575             this->layout.simulateLayoutUsingKerning(it, it_span_end, &attrs);
576             // set x,y attributes only when we need to
577             bool set_x = false;
578             bool set_y = false;
579             if (!item->transform.isIdentity()) {
580                 set_x = set_y = true;
581             } else {
582                 Inkscape::Text::Layout::iterator it_chunk_start = it;
583                 it_chunk_start.thisStartOfChunk();
584                 if (it == it_chunk_start) {
585                     set_x = true;
586                     // don't set y so linespacing adjustments and things will still work
587                 }
588                 Inkscape::Text::Layout::iterator it_shape_start = it;
589                 it_shape_start.thisStartOfShape();
590                 if (it == it_shape_start)
591                     set_y = true;
592             }
593             if (set_x && !attrs.dx.empty())
594                 attrs.dx[0] = 0.0;
595             TextTagAttributes(attrs).writeTo(span_tspan);
596             if (set_x)
597                 sp_repr_set_svg_double(span_tspan, "x", anchor_point[Geom::X]);  // FIXME: this will pick up the wrong end of counter-directional runs
598             if (set_y)
599                 sp_repr_set_svg_double(span_tspan, "y", anchor_point[Geom::Y]);
600             if (line_tspan->childCount() == 0) {
601                 sp_repr_set_svg_double(line_tspan, "x", anchor_point[Geom::X]);  // FIXME: this will pick up the wrong end of counter-directional runs
602                 sp_repr_set_svg_double(line_tspan, "y", anchor_point[Geom::Y]);
603             }
605             SPObject *source_obj = 0;
606             void *rawptr = 0;
607             Glib::ustring::iterator span_text_start_iter;
608             this->layout.getSourceOfCharacter(it, &rawptr, &span_text_start_iter);
609             source_obj = SP_OBJECT (rawptr);
610             gchar *style_text = sp_style_write_difference((SP_IS_STRING(source_obj) ? source_obj->parent : source_obj)->style, this->style);
611             if (style_text && *style_text) {
612                 span_tspan->setAttribute("style", style_text);
613                 g_free(style_text);
614             }
616             if (SP_IS_STRING(source_obj)) {
617                 Glib::ustring *string = &SP_STRING(source_obj)->string;
618                 SPObject *span_end_obj = 0;
619                 void *rawptr = 0;
620                 Glib::ustring::iterator span_text_end_iter;
621                 this->layout.getSourceOfCharacter(it_span_end, &rawptr, &span_text_end_iter);
622                 span_end_obj = SP_OBJECT(rawptr);
623                 if (span_end_obj != source_obj) {
624                     if (it_span_end == this->layout.end()) {
625                         span_text_end_iter = span_text_start_iter;
626                         for (int i = this->layout.iteratorToCharIndex(it_span_end) - this->layout.iteratorToCharIndex(it) ; i ; --i)
627                             ++span_text_end_iter;
628                     } else
629                         span_text_end_iter = string->end();    // spans will never straddle a source boundary
630                 }
632                 if (span_text_start_iter != span_text_end_iter) {
633                     Glib::ustring new_string;
634                     while (span_text_start_iter != span_text_end_iter)
635                         new_string += *span_text_start_iter++;    // grr. no substr() with iterators
636                     Inkscape::XML::Node *new_text = xml_doc->createTextNode(new_string.c_str());
637                     span_tspan->appendChild(new_text);
638                     Inkscape::GC::release(new_text);
639                 }
640             }
641             it = it_span_end;
643             line_tspan->appendChild(span_tspan);
644             Inkscape::GC::release(span_tspan);
645         }
646         repr->appendChild(line_tspan);
647         Inkscape::GC::release(line_tspan);
648     }
650     return repr;
653 SPItem *SPFlowtext::get_frame(SPItem *after)
655     SPObject *ft = SP_OBJECT (this);
656     SPObject *region = NULL;
658     for (SPObject *o = SP_OBJECT(ft)->first_child() ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
659         if (SP_IS_FLOWREGION(o)) {
660             region = o;
661             break;
662         }
663     }
665     if (!region) return NULL;
667     bool past = false;
668     SPItem *frame = NULL;
670     for (SPObject *o = SP_OBJECT(region)->first_child() ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
671         if (SP_IS_ITEM(o)) {
672             if (after == NULL || past) {
673                 frame = SP_ITEM(o);
674             } else {
675                 if (SP_ITEM(o) == after) {
676                     past = true;
677                 }
678             }
679         }
680     }
682     if (!frame) return NULL;
684     if (SP_IS_USE (frame)) {
685         return sp_use_get_original(SP_USE(frame));
686     } else {
687         return frame;
688     }
691 bool SPFlowtext::has_internal_frame()
693     SPItem *frame = get_frame(NULL);
695     return (frame && SP_OBJECT(this)->isAncestorOf(SP_OBJECT(frame)) && SP_IS_RECT(frame));
699 SPItem *create_flowtext_with_internal_frame (SPDesktop *desktop, Geom::Point p0, Geom::Point p1)
701     SPDocument *doc = sp_desktop_document (desktop);
703     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
704     Inkscape::XML::Node *root_repr = xml_doc->createElement("svg:flowRoot");
705     root_repr->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create
706     SPItem *ft_item = SP_ITEM(desktop->currentLayer()->appendChildRepr(root_repr));
707     SPObject *root_object = doc->getObjectByRepr(root_repr);
708     g_assert(SP_IS_FLOWTEXT(root_object));
710     Inkscape::XML::Node *region_repr = xml_doc->createElement("svg:flowRegion");
711     root_repr->appendChild(region_repr);
712     SPObject *region_object = doc->getObjectByRepr(region_repr);
713     g_assert(SP_IS_FLOWREGION(region_object));
715     Inkscape::XML::Node *rect_repr = xml_doc->createElement("svg:rect"); // FIXME: use path!!! after rects are converted to use path
716     region_repr->appendChild(rect_repr);
718     SPObject *rect = doc->getObjectByRepr(rect_repr);
720     p0 *= desktop->dt2doc();
721     p1 *= desktop->dt2doc();
722     using Geom::X;
723     using Geom::Y;
724     Geom::Coord const x0 = MIN(p0[X], p1[X]);
725     Geom::Coord const y0 = MIN(p0[Y], p1[Y]);
726     Geom::Coord const x1 = MAX(p0[X], p1[X]);
727     Geom::Coord const y1 = MAX(p0[Y], p1[Y]);
728     Geom::Coord const w  = x1 - x0;
729     Geom::Coord const h  = y1 - y0;
731     sp_rect_position_set(SP_RECT(rect), x0, y0, w, h);
732     SP_OBJECT(rect)->updateRepr();
734     Inkscape::XML::Node *para_repr = xml_doc->createElement("svg:flowPara");
735     root_repr->appendChild(para_repr);
736     SPObject *para_object = doc->getObjectByRepr(para_repr);
737     g_assert(SP_IS_FLOWPARA(para_object));
739     Inkscape::XML::Node *text = xml_doc->createTextNode("");
740     para_repr->appendChild(text);
742     Inkscape::GC::release(root_repr);
743     Inkscape::GC::release(region_repr);
744     Inkscape::GC::release(para_repr);
745     Inkscape::GC::release(rect_repr);
747     ft_item->transform = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse();
749     return ft_item;
753 /*
754   Local Variables:
755   mode:c++
756   c-file-style:"stroustrup"
757   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
758   indent-tabs-mode:nil
759   fill-column:99
760   End:
761 */
762 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :