Code

Merge and cleanup of GSoC C++-ification project.
[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 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);
150     }
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->firstChild() ; child ; child = child->getNext() ) {
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 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         text->invoke_bbox( &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 = ft->firstChild() ; o ; o = o->getNext() ) {
210         if (SP_IS_FLOWREGION(o)) {
211             region = o;
212             break;
213         }
214     }
216     if (region) {
217         if (flags || (region->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
218             region->emitModified(flags); // pass down to the region only
219         }
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     object->readAttr( "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             //XML Tree being directly used while it shouldn't be.
244             SPCSSAttr *opts = sp_repr_css_attr((SP_OBJECT(group))->getRepr(), "inkscape:layoutOptions");
245             {
246                 gchar const *val = sp_repr_css_property(opts, "justification", NULL);
247                 if (val != NULL && !object->style->text_align.set) {
248                     if ( strcmp(val, "0") == 0 || strcmp(val, "false") == 0 ) {
249                         object->style->text_align.value = SP_CSS_TEXT_ALIGN_LEFT;
250                     } else {
251                         object->style->text_align.value = SP_CSS_TEXT_ALIGN_JUSTIFY;
252                     }
253                     object->style->text_align.set = TRUE;
254                     object->style->text_align.inherit = FALSE;
255                     object->style->text_align.computed = object->style->text_align.value;
256                 }
257             }
258             /* no equivalent css attribute for these two (yet)
259             {
260                 gchar const *val = sp_repr_css_property(opts, "layoutAlgo", NULL);
261                 if ( val == NULL ) {
262                     group->algo = 0;
263                 } else {
264                     if ( strcmp(val, "better") == 0 ) {     // knuth-plass, never worked for general cases
265                         group->algo = 2;
266                     } else if ( strcmp(val, "simple") == 0 ) {   // greedy, but allowed lines to be compressed by up to 20% if it would make them fit
267                         group->algo = 1;
268                     } else if ( strcmp(val, "default") == 0 ) {    // the same one we use, a standard greedy
269                         group->algo = 0;
270                     }
271                 }
272             }
273             */
274             {   // This would probably translate to padding-left, if SPStyle had it.
275                 gchar const *val = sp_repr_css_property(opts, "par-indent", NULL);
276                 if ( val == NULL ) {
277                     group->par_indent = 0.0;
278                 } else {
279                     sp_repr_get_double((Inkscape::XML::Node*)opts, "par-indent", &group->par_indent);
280                 }
281             }
282             sp_repr_css_attr_unref(opts);
283             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
284             break;
285         }
286         default:
287             if (((SPObjectClass *) (parent_class))->set) {
288                 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
289             }
290             break;
291     }
294 static Inkscape::XML::Node *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 ) {
298             repr = xml_doc->createElement("svg:flowRoot");
299         }
300         GSList *l = NULL;
301         for (SPObject *child = object->firstChild() ; child ; child = child->getNext() ) {
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 ) {
307                 l = g_slist_prepend(l, c_repr);
308             }
309         }
310         while ( l ) {
311             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
312             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
313             l = g_slist_remove(l, l->data);
314         }
315     } else {
316         for (SPObject *child = object->firstChild() ; child ; child = child->getNext() ) {
317             if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child) ) {
318                 child->updateRepr(flags);
319             }
320         }
321     }
323     if (((SPObjectClass *) (parent_class))->write) {
324         ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
325     }
327     return repr;
330 static void
331 sp_flowtext_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const /*flags*/)
333     SPFlowtext *group = SP_FLOWTEXT(item);
334     group->layout.getBoundingBox(bbox, transform);
336     // Add stroke width
337     SPStyle* style=SP_OBJECT_STYLE (item);
338     if ( !style->stroke.isNone() ) {
339         double const scale = transform.descrim();
340         if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
341             double const width = MAX(0.125, style->stroke_width.computed * scale);
342             if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
343                 bbox->x0-=0.5*width;
344                 bbox->x1+=0.5*width;
345                 bbox->y0-=0.5*width;
346                 bbox->y1+=0.5*width;
347             }
348         }
349     }
352 static void
353 sp_flowtext_print(SPItem *item, SPPrintContext *ctx)
355     SPFlowtext *group = SP_FLOWTEXT(item);
357     NRRect pbox;
358     item->invoke_bbox( &pbox, Geom::identity(), TRUE);
359     NRRect bbox;
360     Geom::OptRect bbox_maybe = item->getBboxDesktop();
361     if (!bbox_maybe) {
362         return;
363     }
364     bbox = NRRect(from_2geom(*bbox_maybe));
366     NRRect dbox;
367     dbox.x0 = 0.0;
368     dbox.y0 = 0.0;
369     dbox.x1 = SP_OBJECT_DOCUMENT(item)->getWidth();
370     dbox.y1 = SP_OBJECT_DOCUMENT(item)->getHeight();
371     Geom::Matrix const ctm (item->i2d_affine());
373     group->layout.print(ctx, &pbox, &dbox, &bbox, ctm);
377 static gchar *sp_flowtext_description(SPItem *item)
379     Inkscape::Text::Layout const &layout = SP_FLOWTEXT(item)->layout;
380     int const nChars = layout.iteratorToCharIndex(layout.end());
382     char const *trunc = (layout.inputTruncated()) ? _(" [truncated]") : "";
384     if (SP_FLOWTEXT(item)->has_internal_frame()) {
385         return g_strdup_printf(ngettext("<b>Flowed text</b> (%d character%s)", "<b>Flowed text</b> (%d characters%s)", nChars), nChars, trunc);
386     } else {
387         return g_strdup_printf(ngettext("<b>Linked flowed text</b> (%d character%s)", "<b>Linked flowed text</b> (%d characters%s)", nChars), nChars, trunc);
388     }
391 static void sp_flowtext_snappoints(SPItem const *item, std::vector<Inkscape::SnapCandidatePoint> &p, Inkscape::SnapPreferences const */*snapprefs*/)
393     // Choose a point on the baseline for snapping from or to, with the horizontal position
394     // of this point depending on the text alignment (left vs. right)
395     Inkscape::Text::Layout const *layout = te_get_layout((SPItem *) item);
396     if (layout != NULL && layout->outputExists()) {
397         boost::optional<Geom::Point> pt = layout->baselineAnchorPoint();
398         if (pt) {
399             p.push_back(Inkscape::SnapCandidatePoint((*pt) * item->i2d_affine(), Inkscape::SNAPSOURCE_TEXT_BASELINE, Inkscape::SNAPTARGET_TEXT_BASELINE));
400         }
401     }
404 static NRArenaItem *
405 sp_flowtext_show(SPItem *item, NRArena *arena, unsigned/* key*/, unsigned /*flags*/)
407     SPFlowtext *group = (SPFlowtext *) item;
408     NRArenaGroup *flowed = NRArenaGroup::create(arena);
409     nr_arena_group_set_transparent(flowed, FALSE);
411     nr_arena_group_set_style(flowed, group->style);
413     // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
414     NRRect paintbox;
415     item->invoke_bbox( &paintbox, Geom::identity(), TRUE);
416     group->layout.show(flowed, &paintbox);
418     return flowed;
421 static void
422 sp_flowtext_hide(SPItem *item, unsigned int key)
424     if (((SPItemClass *) parent_class)->hide)
425         ((SPItemClass *) parent_class)->hide(item, key);
429 /*
430  *
431  */
433 void SPFlowtext::_buildLayoutInput(SPObject *root, Shape const *exclusion_shape, std::list<Shape> *shapes, SPObject **pending_line_break_object)
435     Inkscape::Text::Layout::OptionalTextTagAttrs pi;
436     bool with_indent = false;
438     if (SP_IS_FLOWPARA(root)) {
439         // emulate par-indent with the first char's kern
440         SPObject *t = root;
441         for ( ; t != NULL && !SP_IS_FLOWTEXT(t); t = SP_OBJECT_PARENT(t)){};
442         if (SP_IS_FLOWTEXT(t)) {
443             double indent = SP_FLOWTEXT(t)->par_indent;
444             if (indent != 0) {
445                 with_indent = true;
446                 SVGLength sl;
447                 sl.value = sl.computed = indent;
448                 sl._set = true;
449                 pi.dx.push_back(sl);
450             }
451         }
452     }
454     if (*pending_line_break_object) {
455         if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object)) {
456             layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
457         } else {
458             layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
459         }
460         *pending_line_break_object = NULL;
461     }
463     for (SPObject *child = root->firstChild() ; child ; child = child->getNext() ) {
464         if (SP_IS_STRING(child)) {
465             if (*pending_line_break_object) {
466                 if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object))
467                     layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
468                 else {
469                     layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
470                 }
471                 *pending_line_break_object = NULL;
472             }
473             if (with_indent) {
474                 layout.appendText(SP_STRING(child)->string, root->style, child, &pi);
475             } else {
476                 layout.appendText(SP_STRING(child)->string, root->style, child);
477             }
478         } else if (SP_IS_FLOWREGION(child)) {
479             std::vector<Shape*> const &computed = SP_FLOWREGION(child)->computed;
480             for (std::vector<Shape*>::const_iterator it = computed.begin() ; it != computed.end() ; it++) {
481                 shapes->push_back(Shape());
482                 if (exclusion_shape->hasEdges()) {
483                     shapes->back().Booleen(*it, const_cast<Shape*>(exclusion_shape), bool_op_diff);
484                 } else {
485                     shapes->back().Copy(*it);
486                 }
487                 layout.appendWrapShape(&shapes->back());
488             }
489         }
490         //XML Tree is being directly used while it shouldn't be.
491         else if (!SP_IS_FLOWREGIONEXCLUDE(child) && !sp_repr_is_meta_element(child->getRepr())) {
492             _buildLayoutInput(child, exclusion_shape, shapes, pending_line_break_object);
493         }
494     }
496     if (SP_IS_FLOWDIV(root) || SP_IS_FLOWPARA(root) || SP_IS_FLOWREGIONBREAK(root) || SP_IS_FLOWLINE(root)) {
497         if (!root->hasChildren())
498             layout.appendText("", root->style, root);
499         *pending_line_break_object = root;
500     }
503 Shape* SPFlowtext::_buildExclusionShape() const
505     Shape *shape = new Shape;
506     Shape *shape_temp = new Shape;
508     for (SPObject *child = children ; child ; child = child->getNext() ) {
509         // RH: is it right that this shouldn't be recursive?
510         if ( SP_IS_FLOWREGIONEXCLUDE(child) ) {
511             SPFlowregionExclude *c_child = SP_FLOWREGIONEXCLUDE(child);
512             if ( c_child->computed && c_child->computed->hasEdges() ) {
513                 if (shape->hasEdges()) {
514                     shape_temp->Booleen(shape, c_child->computed, bool_op_union);
515                     std::swap(shape, shape_temp);
516                 } else {
517                     shape->Copy(c_child->computed);
518                 }
519             }
520         }
521     }
522     delete shape_temp;
523     return shape;
526 void SPFlowtext::rebuildLayout()
528     std::list<Shape> shapes;
530     layout.clear();
531     Shape *exclusion_shape = _buildExclusionShape();
532     SPObject *pending_line_break_object = NULL;
533     _buildLayoutInput(this, exclusion_shape, &shapes, &pending_line_break_object);
534     delete exclusion_shape;
535     layout.calculateFlow();
536     //g_print(layout.dumpAsText().c_str());
539 void SPFlowtext::_clearFlow(NRArenaGroup *in_arena)
541     nr_arena_item_request_render(NR_ARENA_ITEM(in_arena));
542     for (NRArenaItem *child = in_arena->children; child != NULL; ) {
543         NRArenaItem *nchild = child->next;
545         nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
546         nr_arena_item_remove_child(NR_ARENA_ITEM(in_arena), child);
548         child = nchild;
549     }
552 Inkscape::XML::Node *
553 SPFlowtext::getAsText()
555     if (!this->layout.outputExists()) return NULL;
557     SPItem *item = SP_ITEM(this);
559     Inkscape::XML::Document *xml_doc = SP_OBJECT_DOCUMENT(this)->getReprDoc();
560     Inkscape::XML::Node *repr = xml_doc->createElement("svg:text");
561     repr->setAttribute("xml:space", "preserve");
562     repr->setAttribute("style", SP_OBJECT_REPR(this)->attribute("style"));
563     Geom::Point anchor_point = this->layout.characterAnchorPoint(this->layout.begin());
564     sp_repr_set_svg_double(repr, "x", anchor_point[Geom::X]);
565     sp_repr_set_svg_double(repr, "y", anchor_point[Geom::Y]);
567     for (Inkscape::Text::Layout::iterator it = this->layout.begin() ; it != this->layout.end() ; ) {
568         Inkscape::XML::Node *line_tspan = xml_doc->createElement("svg:tspan");
569         line_tspan->setAttribute("sodipodi:role", "line");
571         Inkscape::Text::Layout::iterator it_line_end = it;
572         it_line_end.nextStartOfLine();
574         while (it != it_line_end) {
576             Inkscape::XML::Node *span_tspan = xml_doc->createElement("svg:tspan");
577             Geom::Point anchor_point = this->layout.characterAnchorPoint(it);
578             // use kerning to simulate justification and whatnot
579             Inkscape::Text::Layout::iterator it_span_end = it;
580             it_span_end.nextStartOfSpan();
581             Inkscape::Text::Layout::OptionalTextTagAttrs attrs;
582             this->layout.simulateLayoutUsingKerning(it, it_span_end, &attrs);
583             // set x,y attributes only when we need to
584             bool set_x = false;
585             bool set_y = false;
586             if (!item->transform.isIdentity()) {
587                 set_x = set_y = true;
588             } else {
589                 Inkscape::Text::Layout::iterator it_chunk_start = it;
590                 it_chunk_start.thisStartOfChunk();
591                 if (it == it_chunk_start) {
592                     set_x = true;
593                     // don't set y so linespacing adjustments and things will still work
594                 }
595                 Inkscape::Text::Layout::iterator it_shape_start = it;
596                 it_shape_start.thisStartOfShape();
597                 if (it == it_shape_start)
598                     set_y = true;
599             }
600             if (set_x && !attrs.dx.empty())
601                 attrs.dx[0] = 0.0;
602             TextTagAttributes(attrs).writeTo(span_tspan);
603             if (set_x)
604                 sp_repr_set_svg_double(span_tspan, "x", anchor_point[Geom::X]);  // FIXME: this will pick up the wrong end of counter-directional runs
605             if (set_y)
606                 sp_repr_set_svg_double(span_tspan, "y", anchor_point[Geom::Y]);
607             if (line_tspan->childCount() == 0) {
608                 sp_repr_set_svg_double(line_tspan, "x", anchor_point[Geom::X]);  // FIXME: this will pick up the wrong end of counter-directional runs
609                 sp_repr_set_svg_double(line_tspan, "y", anchor_point[Geom::Y]);
610             }
612             SPObject *source_obj = 0;
613             void *rawptr = 0;
614             Glib::ustring::iterator span_text_start_iter;
615             this->layout.getSourceOfCharacter(it, &rawptr, &span_text_start_iter);
616             source_obj = SP_OBJECT (rawptr);
617             gchar *style_text = sp_style_write_difference((SP_IS_STRING(source_obj) ? source_obj->parent : source_obj)->style, this->style);
618             if (style_text && *style_text) {
619                 span_tspan->setAttribute("style", style_text);
620                 g_free(style_text);
621             }
623             if (SP_IS_STRING(source_obj)) {
624                 Glib::ustring *string = &SP_STRING(source_obj)->string;
625                 SPObject *span_end_obj = 0;
626                 void *rawptr = 0;
627                 Glib::ustring::iterator span_text_end_iter;
628                 this->layout.getSourceOfCharacter(it_span_end, &rawptr, &span_text_end_iter);
629                 span_end_obj = SP_OBJECT(rawptr);
630                 if (span_end_obj != source_obj) {
631                     if (it_span_end == this->layout.end()) {
632                         span_text_end_iter = span_text_start_iter;
633                         for (int i = this->layout.iteratorToCharIndex(it_span_end) - this->layout.iteratorToCharIndex(it) ; i ; --i)
634                             ++span_text_end_iter;
635                     } else
636                         span_text_end_iter = string->end();    // spans will never straddle a source boundary
637                 }
639                 if (span_text_start_iter != span_text_end_iter) {
640                     Glib::ustring new_string;
641                     while (span_text_start_iter != span_text_end_iter)
642                         new_string += *span_text_start_iter++;    // grr. no substr() with iterators
643                     Inkscape::XML::Node *new_text = xml_doc->createTextNode(new_string.c_str());
644                     span_tspan->appendChild(new_text);
645                     Inkscape::GC::release(new_text);
646                 }
647             }
648             it = it_span_end;
650             line_tspan->appendChild(span_tspan);
651             Inkscape::GC::release(span_tspan);
652         }
653         repr->appendChild(line_tspan);
654         Inkscape::GC::release(line_tspan);
655     }
657     return repr;
660 SPItem *SPFlowtext::get_frame(SPItem *after)
662     SPItem *frame = 0;
664     SPObject *region = 0;
665     for (SPObject *o = firstChild() ; o ; o = o->getNext() ) {
666         if (SP_IS_FLOWREGION(o)) {
667             region = o;
668             break;
669         }
670     }
672     if (region) {
673         bool past = false;
675         for (SPObject *o = region->firstChild() ; o ; o = o->getNext() ) {
676             if (SP_IS_ITEM(o)) {
677                 if ( (after == NULL) || past ) {
678                     frame = SP_ITEM(o);
679                 } else {
680                     if (SP_ITEM(o) == after) {
681                         past = true;
682                     }
683                 }
684             }
685         }
687         if ( frame && SP_IS_USE(frame) ) {
688             frame = sp_use_get_original(SP_USE(frame));
689         }
690     }
691     return frame;
694 bool SPFlowtext::has_internal_frame()
696     SPItem *frame = get_frame(NULL);
698     return (frame && SP_OBJECT(this)->isAncestorOf(SP_OBJECT(frame)) && SP_IS_RECT(frame));
702 SPItem *create_flowtext_with_internal_frame (SPDesktop *desktop, Geom::Point p0, Geom::Point p1)
704     SPDocument *doc = sp_desktop_document (desktop);
706     Inkscape::XML::Document *xml_doc = doc->getReprDoc();
707     Inkscape::XML::Node *root_repr = xml_doc->createElement("svg:flowRoot");
708     root_repr->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create
709     SPItem *ft_item = SP_ITEM(desktop->currentLayer()->appendChildRepr(root_repr));
710     SPObject *root_object = doc->getObjectByRepr(root_repr);
711     g_assert(SP_IS_FLOWTEXT(root_object));
713     Inkscape::XML::Node *region_repr = xml_doc->createElement("svg:flowRegion");
714     root_repr->appendChild(region_repr);
715     SPObject *region_object = doc->getObjectByRepr(region_repr);
716     g_assert(SP_IS_FLOWREGION(region_object));
718     Inkscape::XML::Node *rect_repr = xml_doc->createElement("svg:rect"); // FIXME: use path!!! after rects are converted to use path
719     region_repr->appendChild(rect_repr);
721     SPObject *rect = doc->getObjectByRepr(rect_repr);
723     p0 *= desktop->dt2doc();
724     p1 *= desktop->dt2doc();
725     using Geom::X;
726     using Geom::Y;
727     Geom::Coord const x0 = MIN(p0[X], p1[X]);
728     Geom::Coord const y0 = MIN(p0[Y], p1[Y]);
729     Geom::Coord const x1 = MAX(p0[X], p1[X]);
730     Geom::Coord const y1 = MAX(p0[Y], p1[Y]);
731     Geom::Coord const w  = x1 - x0;
732     Geom::Coord const h  = y1 - y0;
734     sp_rect_position_set(SP_RECT(rect), x0, y0, w, h);
735     SP_OBJECT(rect)->updateRepr();
737     Inkscape::XML::Node *para_repr = xml_doc->createElement("svg:flowPara");
738     root_repr->appendChild(para_repr);
739     SPObject *para_object = doc->getObjectByRepr(para_repr);
740     g_assert(SP_IS_FLOWPARA(para_object));
742     Inkscape::XML::Node *text = xml_doc->createTextNode("");
743     para_repr->appendChild(text);
745     Inkscape::GC::release(root_repr);
746     Inkscape::GC::release(region_repr);
747     Inkscape::GC::release(para_repr);
748     Inkscape::GC::release(rect_repr);
750     ft_item->transform = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse();
752     return ft_item;
756 /*
757   Local Variables:
758   mode:c++
759   c-file-style:"stroustrup"
760   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
761   indent-tabs-mode:nil
762   fill-column:99
763   End:
764 */
765 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :