Code

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