Code

- try to use more forward declarations for less dependencies on display/curve.h
[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::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, NR::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, NR::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, NR::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::Node *repr, guint flags)
296     if ( flags & SP_OBJECT_WRITE_BUILD ) {
297         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
298         if ( repr == NULL ) repr = xml_doc->createElement("svg:flowRoot");
299         GSList *l = NULL;
300         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
301             Inkscape::XML::Node *c_repr = NULL;
302             if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child)) {
303                 c_repr = child->updateRepr(NULL, flags);
304             }
305             if ( c_repr ) l = g_slist_prepend(l, c_repr);
306         }
307         while ( l ) {
308             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
309             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
310             l = g_slist_remove(l, l->data);
311         }
312     } else {
313         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
314             if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child) ) {
315                 child->updateRepr(flags);
316             }
317         }
318     }
320     if (((SPObjectClass *) (parent_class))->write)
321         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
323     return repr;
326 static void
327 sp_flowtext_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
329     SPFlowtext *group = SP_FLOWTEXT(item);
330     group->layout.getBoundingBox(bbox, transform);
332     // Add stroke width
333     SPStyle* style=SP_OBJECT_STYLE (item);
334     if ( !style->stroke.isNone() ) {
335         double const scale = expansion(transform);
336         if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
337             double const width = MAX(0.125, style->stroke_width.computed * scale);
338             if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
339                 bbox->x0-=0.5*width;
340                 bbox->x1+=0.5*width;
341                 bbox->y0-=0.5*width;
342                 bbox->y1+=0.5*width;
343             }
344         }
345     }
348 static void
349 sp_flowtext_print(SPItem *item, SPPrintContext *ctx)
351     SPFlowtext *group = SP_FLOWTEXT(item);
353     NRRect pbox;
354     sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
355     NRRect bbox;
356     NR::Maybe<NR::Rect> bbox_maybe = sp_item_bbox_desktop(item);
357     if (!bbox_maybe) {
358         return;
359     }
360     bbox = NRRect(*bbox_maybe);
362     NRRect dbox;
363     dbox.x0 = 0.0;
364     dbox.y0 = 0.0;
365     dbox.x1 = sp_document_width(SP_OBJECT_DOCUMENT(item));
366     dbox.y1 = sp_document_height(SP_OBJECT_DOCUMENT(item));
367     NR::Matrix const ctm = sp_item_i2d_affine(item);
369     group->layout.print(ctx, &pbox, &dbox, &bbox, ctm);
373 static gchar *sp_flowtext_description(SPItem *item)
375     Inkscape::Text::Layout const &layout = SP_FLOWTEXT(item)->layout;
376     int const nChars = layout.iteratorToCharIndex(layout.end());
377     if (SP_FLOWTEXT(item)->has_internal_frame())
378         return g_strdup_printf(ngettext("<b>Flowed text</b> (%d character)", "<b>Flowed text</b> (%d characters)", nChars), nChars);
379     else
380         return g_strdup_printf(ngettext("<b>Linked flowed text</b> (%d character)", "<b>Linked flowed text</b> (%d characters)", nChars), nChars);
383 static NRArenaItem *
384 sp_flowtext_show(SPItem *item, NRArena *arena, unsigned/* key*/, unsigned /*flags*/)
386     SPFlowtext *group = (SPFlowtext *) item;
387     NRArenaGroup *flowed = NRArenaGroup::create(arena);
388     nr_arena_group_set_transparent(flowed, FALSE);
390     nr_arena_group_set_style(flowed, group->style);
392     // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
393     NRRect paintbox;
394     sp_item_invoke_bbox(item, &paintbox, NR::identity(), TRUE);
395     group->layout.show(flowed, &paintbox);
397     return flowed;
400 static void
401 sp_flowtext_hide(SPItem *item, unsigned int key)
403     if (((SPItemClass *) parent_class)->hide)
404         ((SPItemClass *) parent_class)->hide(item, key);
408 /*
409  *
410  */
412 void SPFlowtext::_buildLayoutInput(SPObject *root, Shape const *exclusion_shape, std::list<Shape> *shapes, SPObject **pending_line_break_object)
414     Inkscape::Text::Layout::OptionalTextTagAttrs pi;
415     bool with_indent = false;
417     if (SP_IS_FLOWPARA(root)) {
418         // emulate par-indent with the first char's kern
419         SPObject *t = root;
420         for ( ; t != NULL && !SP_IS_FLOWTEXT(t); t = SP_OBJECT_PARENT(t));
421         if (SP_IS_FLOWTEXT(t)) {
422             double indent = SP_FLOWTEXT(t)->par_indent;
423             if (indent != 0) {
424                 with_indent = true;
425                 SVGLength sl;
426                 sl.value = sl.computed = indent;
427                 sl._set = true;
428                 pi.dx.push_back(sl);
429             }
430         }
431     }
433     if (*pending_line_break_object) {
434         if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object)) {
435             layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
436         } else {
437             layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
438         }
439         *pending_line_break_object = NULL;
440     }
442     for (SPObject *child = sp_object_first_child(root) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
443         if (SP_IS_STRING(child)) {
444             if (*pending_line_break_object) {
445                 if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object))
446                     layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
447                 else {
448                     layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
449                 }
450                 *pending_line_break_object = NULL;
451             }
452             if (with_indent)
453                 layout.appendText(SP_STRING(child)->string, root->style, child, &pi);
454             else
455                 layout.appendText(SP_STRING(child)->string, root->style, child);
456         } else if (SP_IS_FLOWREGION(child)) {
457             std::vector<Shape*> const &computed = SP_FLOWREGION(child)->computed;
458             for (std::vector<Shape*>::const_iterator it = computed.begin() ; it != computed.end() ; it++) {
459                 shapes->push_back(Shape());
460                 if (exclusion_shape->hasEdges())
461                     shapes->back().Booleen(*it, const_cast<Shape*>(exclusion_shape), bool_op_diff);
462                 else
463                     shapes->back().Copy(*it);
464                 layout.appendWrapShape(&shapes->back());
465             }
466         }
467         else if (!SP_IS_FLOWREGIONEXCLUDE(child))
468             _buildLayoutInput(child, exclusion_shape, shapes, pending_line_break_object);
469     }
471     if (SP_IS_FLOWDIV(root) || SP_IS_FLOWPARA(root) || SP_IS_FLOWREGIONBREAK(root) || SP_IS_FLOWLINE(root)) {
472         if (!root->hasChildren())
473             layout.appendText("", root->style, root);
474         *pending_line_break_object = root;
475     }
478 Shape* SPFlowtext::_buildExclusionShape() const
480     Shape *shape = new Shape;
481     Shape *shape_temp = new Shape;
483     for (SPObject *child = children ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
484         // RH: is it right that this shouldn't be recursive?
485         if ( SP_IS_FLOWREGIONEXCLUDE(child) ) {
486             SPFlowregionExclude *c_child = SP_FLOWREGIONEXCLUDE(child);
487             if (c_child->computed == NULL || !c_child->computed->hasEdges())
488                 continue;
489             if (shape->hasEdges()) {
490                 shape_temp->Booleen(shape, c_child->computed, bool_op_union);
491                 std::swap(shape, shape_temp);
492             } else
493                 shape->Copy(c_child->computed);
494         }
495     }
496     delete shape_temp;
497     return shape;
500 void SPFlowtext::rebuildLayout()
502     std::list<Shape> shapes;
504     layout.clear();
505     Shape *exclusion_shape = _buildExclusionShape();
506     SPObject *pending_line_break_object = NULL;
507     _buildLayoutInput(this, exclusion_shape, &shapes, &pending_line_break_object);
508     delete exclusion_shape;
509     layout.calculateFlow();
510     //g_print(layout.dumpAsText().c_str());
513 void SPFlowtext::_clearFlow(NRArenaGroup *in_arena)
515     nr_arena_item_request_render(NR_ARENA_ITEM(in_arena));
516     for (NRArenaItem *child = in_arena->children; child != NULL; ) {
517         NRArenaItem *nchild = child->next;
519         nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
520         nr_arena_item_remove_child(NR_ARENA_ITEM(in_arena), child);
522         child = nchild;
523     }
526 Inkscape::XML::Node *
527 SPFlowtext::getAsText()
529     if (!this->layout.outputExists()) return NULL;
531     SPItem *item = SP_ITEM(this);
533     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(this));
534     Inkscape::XML::Node *repr = xml_doc->createElement("svg:text");
535     repr->setAttribute("xml:space", "preserve");
536     repr->setAttribute("style", SP_OBJECT_REPR(this)->attribute("style"));
537     NR::Point anchor_point = this->layout.characterAnchorPoint(this->layout.begin());
538     sp_repr_set_svg_double(repr, "x", anchor_point[NR::X]);
539     sp_repr_set_svg_double(repr, "y", anchor_point[NR::Y]);
541     for (Inkscape::Text::Layout::iterator it = this->layout.begin() ; it != this->layout.end() ; ) {
542         Inkscape::XML::Node *line_tspan = xml_doc->createElement("svg:tspan");
543         line_tspan->setAttribute("sodipodi:role", "line");
545         Inkscape::Text::Layout::iterator it_line_end = it;
546         it_line_end.nextStartOfLine();
548         while (it != it_line_end) {
550             Inkscape::XML::Node *span_tspan = xml_doc->createElement("svg:tspan");
551             NR::Point anchor_point = this->layout.characterAnchorPoint(it);
552             // use kerning to simulate justification and whatnot
553             Inkscape::Text::Layout::iterator it_span_end = it;
554             it_span_end.nextStartOfSpan();
555             Inkscape::Text::Layout::OptionalTextTagAttrs attrs;
556             this->layout.simulateLayoutUsingKerning(it, it_span_end, &attrs);
557             // set x,y attributes only when we need to
558             bool set_x = false;
559             bool set_y = false;
560             if (!item->transform.test_identity()) {
561                 set_x = set_y = true;
562             } else {
563                 Inkscape::Text::Layout::iterator it_chunk_start = it;
564                 it_chunk_start.thisStartOfChunk();
565                 if (it == it_chunk_start) {
566                     set_x = true;
567                     // don't set y so linespacing adjustments and things will still work
568                 }
569                 Inkscape::Text::Layout::iterator it_shape_start = it;
570                 it_shape_start.thisStartOfShape();
571                 if (it == it_shape_start)
572                     set_y = true;
573             }
574             if (set_x && !attrs.dx.empty())
575                 attrs.dx[0] = 0.0;
576             TextTagAttributes(attrs).writeTo(span_tspan);
577             if (set_x)
578                 sp_repr_set_svg_double(span_tspan, "x", anchor_point[NR::X]);  // FIXME: this will pick up the wrong end of counter-directional runs
579             if (set_y)
580                 sp_repr_set_svg_double(span_tspan, "y", anchor_point[NR::Y]);
582             SPObject *source_obj = 0;
583             void *rawptr = 0;
584             Glib::ustring::iterator span_text_start_iter;
585             this->layout.getSourceOfCharacter(it, &rawptr, &span_text_start_iter);
586             source_obj = SP_OBJECT (rawptr);
587             gchar *style_text = sp_style_write_difference((SP_IS_STRING(source_obj) ? source_obj->parent : source_obj)->style, this->style);
588             if (style_text && *style_text) {
589                 span_tspan->setAttribute("style", style_text);
590                 g_free(style_text);
591             }
593             if (SP_IS_STRING(source_obj)) {
594                 Glib::ustring *string = &SP_STRING(source_obj)->string;
595                 SPObject *span_end_obj = 0;
596                 void *rawptr = 0;
597                 Glib::ustring::iterator span_text_end_iter;
598                 this->layout.getSourceOfCharacter(it_span_end, &rawptr, &span_text_end_iter);
599                 span_end_obj = SP_OBJECT(rawptr);
600                 if (span_end_obj != source_obj) {
601                     if (it_span_end == this->layout.end()) {
602                         span_text_end_iter = span_text_start_iter;
603                         for (int i = this->layout.iteratorToCharIndex(it_span_end) - this->layout.iteratorToCharIndex(it) ; i ; --i)
604                             ++span_text_end_iter;
605                     } else
606                         span_text_end_iter = string->end();    // spans will never straddle a source boundary
607                 }
609                 if (span_text_start_iter != span_text_end_iter) {
610                     Glib::ustring new_string;
611                     while (span_text_start_iter != span_text_end_iter)
612                         new_string += *span_text_start_iter++;    // grr. no substr() with iterators
613                     Inkscape::XML::Node *new_text = xml_doc->createTextNode(new_string.c_str());
614                     span_tspan->appendChild(new_text);
615                     Inkscape::GC::release(new_text);
616                 }
617             }
618             it = it_span_end;
620             line_tspan->appendChild(span_tspan);
621             Inkscape::GC::release(span_tspan);
622         }
623         repr->appendChild(line_tspan);
624         Inkscape::GC::release(line_tspan);
625     }
627     return repr;
630 SPItem *SPFlowtext::get_frame(SPItem *after)
632     SPObject *ft = SP_OBJECT (this);
633     SPObject *region = NULL;
635     for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
636         if (SP_IS_FLOWREGION(o)) {
637             region = o;
638             break;
639         }
640     }
642     if (!region) return NULL;
644     bool past = false;
645     SPItem *frame = NULL;
647     for (SPObject *o = sp_object_first_child(SP_OBJECT(region)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
648         if (SP_IS_ITEM(o)) {
649             if (after == NULL || past) {
650                 frame = SP_ITEM(o);
651             } else {
652                 if (SP_ITEM(o) == after) {
653                     past = true;
654                 }
655             }
656         }
657     }
659     if (!frame) return NULL;
661     if (SP_IS_USE (frame)) {
662         return sp_use_get_original(SP_USE(frame));
663     } else {
664         return frame;
665     }
668 bool SPFlowtext::has_internal_frame()
670     SPItem *frame = get_frame(NULL);
672     return (frame && SP_OBJECT(this)->isAncestorOf(SP_OBJECT(frame)) && SP_IS_RECT(frame));
676 SPItem *create_flowtext_with_internal_frame (SPDesktop *desktop, NR::Point p0, NR::Point p1)
678     SPDocument *doc = sp_desktop_document (desktop);
680     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
681     Inkscape::XML::Node *root_repr = xml_doc->createElement("svg:flowRoot");
682     root_repr->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create
683     SPItem *ft_item = SP_ITEM(desktop->currentLayer()->appendChildRepr(root_repr));
684     SPObject *root_object = doc->getObjectByRepr(root_repr);
685     g_assert(SP_IS_FLOWTEXT(root_object));
687     Inkscape::XML::Node *region_repr = xml_doc->createElement("svg:flowRegion");
688     root_repr->appendChild(region_repr);
689     SPObject *region_object = doc->getObjectByRepr(region_repr);
690     g_assert(SP_IS_FLOWREGION(region_object));
692     Inkscape::XML::Node *rect_repr = xml_doc->createElement("svg:rect"); // FIXME: use path!!! after rects are converted to use path
693     region_repr->appendChild(rect_repr);
695     SPObject *rect = doc->getObjectByRepr(rect_repr);
697     p0 = sp_desktop_dt2root_xy_point(desktop, p0);
698     p1 = sp_desktop_dt2root_xy_point(desktop, p1);
699     using NR::X;
700     using NR::Y;
701     NR::Coord const x0 = MIN(p0[X], p1[X]);
702     NR::Coord const y0 = MIN(p0[Y], p1[Y]);
703     NR::Coord const x1 = MAX(p0[X], p1[X]);
704     NR::Coord const y1 = MAX(p0[Y], p1[Y]);
705     NR::Coord const w  = x1 - x0;
706     NR::Coord const h  = y1 - y0;
708     sp_rect_position_set(SP_RECT(rect), x0, y0, w, h);
709     SP_OBJECT(rect)->updateRepr();
711     Inkscape::XML::Node *para_repr = xml_doc->createElement("svg:flowPara");
712     root_repr->appendChild(para_repr);
713     SPObject *para_object = doc->getObjectByRepr(para_repr);
714     g_assert(SP_IS_FLOWPARA(para_object));
716     Inkscape::XML::Node *text = xml_doc->createTextNode("");
717     para_repr->appendChild(text);
719     Inkscape::GC::release(root_repr);
720     Inkscape::GC::release(region_repr);
721     Inkscape::GC::release(para_repr);
722     Inkscape::GC::release(rect_repr);
724     return ft_item;
728 /*
729   Local Variables:
730   mode:c++
731   c-file-style:"stroustrup"
732   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
733   indent-tabs-mode:nil
734   fill-column:99
735   End:
736 */
737 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :