Code

patch from Gustav Broberg: undo annotations and history dialog
[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"
30 #include "livarot/Shape.h"
32 #include "display/nr-arena-glyphs.h"
35 static void sp_flowtext_class_init(SPFlowtextClass *klass);
36 static void sp_flowtext_init(SPFlowtext *group);
37 static void sp_flowtext_dispose(GObject *object);
39 static void sp_flowtext_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
40 static void sp_flowtext_remove_child(SPObject *object, Inkscape::XML::Node *child);
41 static void sp_flowtext_update(SPObject *object, SPCtx *ctx, guint flags);
42 static void sp_flowtext_modified(SPObject *object, guint flags);
43 static Inkscape::XML::Node *sp_flowtext_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
44 static void sp_flowtext_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
45 static void sp_flowtext_set(SPObject *object, unsigned key, gchar const *value);
47 static void sp_flowtext_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
48 static void sp_flowtext_print(SPItem *item, SPPrintContext *ctx);
49 static gchar *sp_flowtext_description(SPItem *item);
50 static NRArenaItem *sp_flowtext_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags);
51 static void sp_flowtext_hide(SPItem *item, unsigned key);
53 static SPItemClass *parent_class;
55 GType
56 sp_flowtext_get_type(void)
57 {
58     static GType group_type = 0;
59     if (!group_type) {
60         GTypeInfo group_info = {
61             sizeof(SPFlowtextClass),
62             NULL,   /* base_init */
63             NULL,   /* base_finalize */
64             (GClassInitFunc) sp_flowtext_class_init,
65             NULL,   /* class_finalize */
66             NULL,   /* class_data */
67             sizeof(SPFlowtext),
68             16,     /* n_preallocs */
69             (GInstanceInitFunc) sp_flowtext_init,
70             NULL,   /* value_table */
71         };
72         group_type = g_type_register_static(SP_TYPE_ITEM, "SPFlowtext", &group_info, (GTypeFlags)0);
73     }
74     return group_type;
75 }
77 static void
78 sp_flowtext_class_init(SPFlowtextClass *klass)
79 {
80     GObjectClass *object_class = (GObjectClass *) klass;
81     SPObjectClass *sp_object_class = (SPObjectClass *) klass;
82     SPItemClass *item_class = (SPItemClass *) klass;
84     parent_class = (SPItemClass *)g_type_class_ref(SP_TYPE_ITEM);
86     object_class->dispose = sp_flowtext_dispose;
88     sp_object_class->child_added = sp_flowtext_child_added;
89     sp_object_class->remove_child = sp_flowtext_remove_child;
90     sp_object_class->update = sp_flowtext_update;
91     sp_object_class->modified = sp_flowtext_modified;
92     sp_object_class->write = sp_flowtext_write;
93     sp_object_class->build = sp_flowtext_build;
94     sp_object_class->set = sp_flowtext_set;
96     item_class->bbox = sp_flowtext_bbox;
97     item_class->print = sp_flowtext_print;
98     item_class->description = sp_flowtext_description;
99     item_class->show = sp_flowtext_show;
100     item_class->hide = sp_flowtext_hide;
103 static void
104 sp_flowtext_init(SPFlowtext *group)
106     group->par_indent = 0;
107     new (&group->layout) Inkscape::Text::Layout();
110 static void
111 sp_flowtext_dispose(GObject *object)
113     SPFlowtext *group = (SPFlowtext*)object;
115     group->layout.~Layout();
118 static void
119 sp_flowtext_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
121     if (((SPObjectClass *) (parent_class))->child_added)
122         (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
124     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
127 /* fixme: hide (Lauris) */
129 static void
130 sp_flowtext_remove_child(SPObject *object, Inkscape::XML::Node *child)
132     if (((SPObjectClass *) (parent_class))->remove_child)
133         (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
135     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
138 static void
139 sp_flowtext_update(SPObject *object, SPCtx *ctx, unsigned flags)
141     SPFlowtext *group = SP_FLOWTEXT(object);
142     SPItemCtx *ictx = (SPItemCtx *) ctx;
143     SPItemCtx cctx = *ictx;
145     if (((SPObjectClass *) (parent_class))->update)
146         ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
148     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
149     flags &= SP_OBJECT_MODIFIED_CASCADE;
151     GSList *l = NULL;
152     for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
153         g_object_ref(G_OBJECT(child));
154         l = g_slist_prepend(l, child);
155     }
156     l = g_slist_reverse(l);
157     while (l) {
158         SPObject *child = SP_OBJECT(l->data);
159         l = g_slist_remove(l, child);
160         if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
161             if (SP_IS_ITEM(child)) {
162                 SPItem const &chi = *SP_ITEM(child);
163                 cctx.i2doc = chi.transform * ictx->i2doc;
164                 cctx.i2vp = chi.transform * ictx->i2vp;
165                 child->updateDisplay((SPCtx *)&cctx, flags);
166             } else {
167                 child->updateDisplay(ctx, flags);
168             }
169         }
170         g_object_unref(G_OBJECT(child));
171     }
173     group->rebuildLayout();
175     // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
176     NRRect paintbox;
177     sp_item_invoke_bbox(group, &paintbox, NR::identity(), TRUE);
178     for (SPItemView *v = group->display; v != NULL; v = v->next) {
179         group->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
180         group->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
181     }
184 static void
185 sp_flowtext_modified(SPObject *object, guint flags)
187     SPObject *ft = SP_FLOWTEXT (object);
188     SPObject *region = NULL;
190     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
191     flags &= SP_OBJECT_MODIFIED_CASCADE;
193     for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
194         if (SP_IS_FLOWREGION(o)) {
195             region = o;
196             break;
197         }
198     }
200     if (!region) return;
202     if (flags || (region->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
203         region->emitModified(flags); // pass down to the region only
204     }
207 static void
208 sp_flowtext_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
210     object->_requireSVGVersion(Inkscape::Version(1, 2));
212     if (((SPObjectClass *) (parent_class))->build) {
213         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
214     }
216     sp_object_read_attr(object, "inkscape:layoutOptions");     // must happen after css has been read
219 static void
220 sp_flowtext_set(SPObject *object, unsigned key, gchar const *value)
222     SPFlowtext *group = (SPFlowtext *) object;
224     switch (key) {
225         case SP_ATTR_LAYOUT_OPTIONS: {
226             // deprecated attribute, read for backward compatibility only
227             SPCSSAttr *opts = sp_repr_css_attr((SP_OBJECT(group))->repr, "inkscape:layoutOptions");
228             {
229                 gchar const *val = sp_repr_css_property(opts, "justification", NULL);
230                 if (val != NULL && !object->style->text_align.set) {
231                     if ( strcmp(val, "0") == 0 || strcmp(val, "false") == 0 ) {
232                         object->style->text_align.value = SP_CSS_TEXT_ALIGN_LEFT;
233                     } else {
234                         object->style->text_align.value = SP_CSS_TEXT_ALIGN_JUSTIFY;
235                     }
236                     object->style->text_align.set = TRUE;
237                     object->style->text_align.inherit = FALSE;
238                     object->style->text_align.computed = object->style->text_align.value;
239                 }
240             }
241             /* no equivalent css attribute for these two (yet)
242             {
243                 gchar const *val = sp_repr_css_property(opts, "layoutAlgo", NULL);
244                 if ( val == NULL ) {
245                     group->algo = 0;
246                 } else {
247                     if ( strcmp(val, "better") == 0 ) {     // knuth-plass, never worked for general cases
248                         group->algo = 2;
249                     } else if ( strcmp(val, "simple") == 0 ) {   // greedy, but allowed lines to be compressed by up to 20% if it would make them fit
250                         group->algo = 1;
251                     } else if ( strcmp(val, "default") == 0 ) {    // the same one we use, a standard greedy
252                         group->algo = 0;
253                     }
254                 }
255             }
256             */
257             {   // This would probably translate to padding-left, if SPStyle had it.
258                 gchar const *val = sp_repr_css_property(opts, "par-indent", NULL);
259                 if ( val == NULL ) {
260                     group->par_indent = 0.0;
261                 } else {
262                     sp_repr_get_double((Inkscape::XML::Node*)opts, "par-indent", &group->par_indent);
263                 }
264             }
265             sp_repr_css_attr_unref(opts);
266             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
267             break;
268         }
269         default:
270             if (((SPObjectClass *) (parent_class))->set) {
271                 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
272             }
273             break;
274     }
277 static Inkscape::XML::Node *
278 sp_flowtext_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
280     if ( flags & SP_OBJECT_WRITE_BUILD ) {
281         if ( repr == NULL ) repr = sp_repr_new("svg:flowRoot");
282         GSList *l = NULL;
283         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
284             Inkscape::XML::Node *c_repr = NULL;
285             if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child)) {
286                 c_repr = child->updateRepr(NULL, flags);
287             }
288             if ( c_repr ) l = g_slist_prepend(l, c_repr);
289         }
290         while ( l ) {
291             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
292             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
293             l = g_slist_remove(l, l->data);
294         }
295     } else {
296         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
297             if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child) ) {
298                 child->updateRepr(flags);
299             }
300         }
301     }
303     if (((SPObjectClass *) (parent_class))->write)
304         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
306     return repr;
309 static void
310 sp_flowtext_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
312     SPFlowtext *group = SP_FLOWTEXT(item);
313     group->layout.getBoundingBox(bbox, transform);
315     // Add stroke width
316     SPStyle* style=SP_OBJECT_STYLE (item);
317     if (style->stroke.type != SP_PAINT_TYPE_NONE) {
318         double const scale = expansion(transform);
319         if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
320             double const width = MAX(0.125, style->stroke_width.computed * scale);
321             if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
322                 bbox->x0-=0.5*width;
323                 bbox->x1+=0.5*width;
324                 bbox->y0-=0.5*width;
325                 bbox->y1+=0.5*width;
326             }
327         }
328     }
331 static void
332 sp_flowtext_print(SPItem *item, SPPrintContext *ctx)
334     SPFlowtext *group = SP_FLOWTEXT(item);
336     NRRect pbox;
337     sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
338     NRRect bbox;
339     sp_item_bbox_desktop(item, &bbox);
340     NRRect dbox;
341     dbox.x0 = 0.0;
342     dbox.y0 = 0.0;
343     dbox.x1 = sp_document_width(SP_OBJECT_DOCUMENT(item));
344     dbox.y1 = sp_document_height(SP_OBJECT_DOCUMENT(item));
345     NR::Matrix const ctm = sp_item_i2d_affine(item);
347     group->layout.print(ctx, &pbox, &dbox, &bbox, ctm);
351 static gchar *sp_flowtext_description(SPItem *item)
353     Inkscape::Text::Layout const &layout = SP_FLOWTEXT(item)->layout;
354     int const nChars = layout.iteratorToCharIndex(layout.end());
355     if (SP_FLOWTEXT(item)->has_internal_frame())
356         return g_strdup_printf(ngettext("<b>Flowed text</b> (%d character)", "<b>Flowed text</b> (%d characters)", nChars), nChars);
357     else
358         return g_strdup_printf(ngettext("<b>Linked flowed text</b> (%d character)", "<b>Linked flowed text</b> (%d characters)", nChars), nChars);
361 static NRArenaItem *
362 sp_flowtext_show(SPItem *item, NRArena *arena, unsigned/* key*/, unsigned /*flags*/)
364     SPFlowtext *group = (SPFlowtext *) item;
365     NRArenaGroup *flowed = NRArenaGroup::create(arena);
366     nr_arena_group_set_transparent(flowed, FALSE);
368     // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
369     NRRect paintbox;
370     sp_item_invoke_bbox(item, &paintbox, NR::identity(), TRUE);
371     group->layout.show(flowed, &paintbox);
373     return flowed;
376 static void
377 sp_flowtext_hide(SPItem *item, unsigned int key)
379     if (((SPItemClass *) parent_class)->hide)
380         ((SPItemClass *) parent_class)->hide(item, key);
384 /*
385  *
386  */
388 void SPFlowtext::_buildLayoutInput(SPObject *root, Shape const *exclusion_shape, std::list<Shape> *shapes, SPObject **pending_line_break_object)
390     Inkscape::Text::Layout::OptionalTextTagAttrs pi;
391     bool with_indent = false;
393     if (SP_IS_FLOWPARA(root)) {
394         // emulate par-indent with the first char's kern
395         SPObject *t = root;
396         for ( ; t != NULL && !SP_IS_FLOWTEXT(t); t = SP_OBJECT_PARENT(t));
397         if (SP_IS_FLOWTEXT(t)) {
398             double indent = SP_FLOWTEXT(t)->par_indent;
399             if (indent != 0) {
400                 with_indent = true;
401                 SVGLength sl;
402                 sl.value = sl.computed = indent;
403                 sl._set = true;
404                 pi.dx.push_back(sl);
405             }
406         }
407     }
409     if (*pending_line_break_object) {
410         if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object)) {
411             layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
412         } else {
413             layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
414         }
415         *pending_line_break_object = NULL;
416     }
418     for (SPObject *child = sp_object_first_child(root) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
419         if (SP_IS_STRING(child)) {
420             if (*pending_line_break_object) {
421                 if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object))
422                     layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
423                 else {
424                     layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
425                 }
426                 *pending_line_break_object = NULL;
427             }
428             if (with_indent)
429                 layout.appendText(SP_STRING(child)->string, root->style, child, &pi);
430             else
431                 layout.appendText(SP_STRING(child)->string, root->style, child);
432         } else if (SP_IS_FLOWREGION(child)) {
433             std::vector<Shape*> const &computed = SP_FLOWREGION(child)->computed;
434             for (std::vector<Shape*>::const_iterator it = computed.begin() ; it != computed.end() ; it++) {
435                 shapes->push_back(Shape());
436                 if (exclusion_shape->hasEdges())
437                     shapes->back().Booleen(*it, const_cast<Shape*>(exclusion_shape), bool_op_diff);
438                 else
439                     shapes->back().Copy(*it);
440                 layout.appendWrapShape(&shapes->back());
441             }
442         }
443         else if (!SP_IS_FLOWREGIONEXCLUDE(child))
444             _buildLayoutInput(child, exclusion_shape, shapes, pending_line_break_object);
445     }
447     if (SP_IS_FLOWDIV(root) || SP_IS_FLOWPARA(root) || SP_IS_FLOWREGIONBREAK(root) || SP_IS_FLOWLINE(root)) {
448         if (!root->hasChildren())
449             layout.appendText("", root->style, root);
450         *pending_line_break_object = root;
451     }
454 Shape* SPFlowtext::_buildExclusionShape() const
456     Shape *shape = new Shape;
457     Shape *shape_temp = new Shape;
459     for (SPObject *child = children ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
460         // RH: is it right that this shouldn't be recursive?
461         if ( SP_IS_FLOWREGIONEXCLUDE(child) ) {
462             SPFlowregionExclude *c_child = SP_FLOWREGIONEXCLUDE(child);
463             if (c_child->computed == NULL || !c_child->computed->hasEdges())
464                 continue;
465             if (shape->hasEdges()) {
466                 shape_temp->Booleen(shape, c_child->computed, bool_op_union);
467                 std::swap(shape, shape_temp);
468             } else
469                 shape->Copy(c_child->computed);
470         }
471     }
472     delete shape_temp;
473     return shape;
476 void SPFlowtext::rebuildLayout()
478     std::list<Shape> shapes;
480     layout.clear();
481     Shape *exclusion_shape = _buildExclusionShape();
482     SPObject *pending_line_break_object = NULL;
483     _buildLayoutInput(this, exclusion_shape, &shapes, &pending_line_break_object);
484     delete exclusion_shape;
485     layout.calculateFlow();
486     //g_print(layout.dumpAsText().c_str());
489 void SPFlowtext::_clearFlow(NRArenaGroup *in_arena)
491     nr_arena_item_request_render(NR_ARENA_ITEM(in_arena));
492     for (NRArenaItem *child = in_arena->children; child != NULL; ) {
493         NRArenaItem *nchild = child->next;
495         nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
496         nr_arena_item_remove_child(NR_ARENA_ITEM(in_arena), child);
498         child = nchild;
499     }
502 void SPFlowtext::convert_to_text()
504     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
505     Inkscape::Selection *selection = sp_desktop_selection(desktop);
506     SPItem *item = selection->singleItem();
507     if (!SP_IS_FLOWTEXT(item)) return;
509     SPFlowtext *group = SP_FLOWTEXT(item);
511     if (!group->layout.outputExists()) return;
513     Inkscape::XML::Node *repr = sp_repr_new("svg:text");
514     repr->setAttribute("xml:space", "preserve");
515     repr->setAttribute("style", SP_OBJECT_REPR(group)->attribute("style"));
516     NR::Point anchor_point = group->layout.characterAnchorPoint(group->layout.begin());
517     sp_repr_set_svg_double(repr, "x", anchor_point[NR::X]);
518     sp_repr_set_svg_double(repr, "y", anchor_point[NR::Y]);
520     for (Inkscape::Text::Layout::iterator it = group->layout.begin() ; it != group->layout.end() ; ) {
522             Inkscape::XML::Node *line_tspan = sp_repr_new("svg:tspan");
523         line_tspan->setAttribute("sodipodi:role", "line");
525         Inkscape::Text::Layout::iterator it_line_end = it;
526         it_line_end.nextStartOfLine();
527         while (it != it_line_end) {
529                 Inkscape::XML::Node *span_tspan = sp_repr_new("svg:tspan");
530             NR::Point anchor_point = group->layout.characterAnchorPoint(it);
531             // use kerning to simulate justification and whatnot
532             Inkscape::Text::Layout::iterator it_span_end = it;
533             it_span_end.nextStartOfSpan();
534             Inkscape::Text::Layout::OptionalTextTagAttrs attrs;
535             group->layout.simulateLayoutUsingKerning(it, it_span_end, &attrs);
536             // set x,y attributes only when we need to
537             bool set_x = false;
538             bool set_y = false;
539             if (!item->transform.test_identity()) {
540                 set_x = set_y = true;
541             } else {
542                 Inkscape::Text::Layout::iterator it_chunk_start = it;
543                 it_chunk_start.thisStartOfChunk();
544                 if (it == it_chunk_start) {
545                     set_x = true;
546                     // don't set y so linespacing adjustments and things will still work
547                 }
548                 Inkscape::Text::Layout::iterator it_shape_start = it;
549                 it_shape_start.thisStartOfShape();
550                 if (it == it_shape_start)
551                     set_y = true;
552             }
553             if (set_x && !attrs.dx.empty())
554                 attrs.dx[0] = 0.0;
555             TextTagAttributes(attrs).writeTo(span_tspan);
556             if (set_x)
557                 sp_repr_set_svg_double(span_tspan, "x", anchor_point[NR::X]);  // FIXME: this will pick up the wrong end of counter-directional runs
558             if (set_y)
559                 sp_repr_set_svg_double(span_tspan, "y", anchor_point[NR::Y]);
561             SPObject *source_obj = 0;
562             void *rawptr = 0;
563             Glib::ustring::iterator span_text_start_iter;
564             group->layout.getSourceOfCharacter(it, &rawptr, &span_text_start_iter);
565             source_obj = SP_OBJECT (rawptr);
566             gchar *style_text = sp_style_write_difference((SP_IS_STRING(source_obj) ? source_obj->parent : source_obj)->style, group->style);
567             if (style_text && *style_text) {
568                 span_tspan->setAttribute("style", style_text);
569                 g_free(style_text);
570             }
572             if (SP_IS_STRING(source_obj)) {
573                 Glib::ustring *string = &SP_STRING(source_obj)->string;
574                 SPObject *span_end_obj = 0;
575                 void *rawptr = 0;
576                 Glib::ustring::iterator span_text_end_iter;
577                 group->layout.getSourceOfCharacter(it_span_end, &rawptr, &span_text_end_iter);
578                 span_end_obj = SP_OBJECT(rawptr);
579                 if (span_end_obj != source_obj) {
580                     if (it_span_end == group->layout.end()) {
581                         span_text_end_iter = span_text_start_iter;
582                         for (int i = group->layout.iteratorToCharIndex(it_span_end) - group->layout.iteratorToCharIndex(it) ; i ; --i)
583                             ++span_text_end_iter;
584                     } else
585                         span_text_end_iter = string->end();    // spans will never straddle a source boundary
586                 }
588                 if (span_text_start_iter != span_text_end_iter) {
589                     Glib::ustring new_string;
590                     while (span_text_start_iter != span_text_end_iter)
591                         new_string += *span_text_start_iter++;    // grr. no substr() with iterators
592                     Inkscape::XML::Node *new_text = sp_repr_new_text(new_string.c_str());
593                     span_tspan->appendChild(new_text);
594                     Inkscape::GC::release(new_text);
595                 }
596             }
597             it = it_span_end;
599             line_tspan->appendChild(span_tspan);
600                 Inkscape::GC::release(span_tspan);
601         }
602         repr->appendChild(line_tspan);
603             Inkscape::GC::release(line_tspan);
604     }
606     Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
607     parent->appendChild(repr);
608     SPItem *new_item = (SPItem *) sp_desktop_document(desktop)->getObjectByRepr(repr);
609     sp_item_write_transform(new_item, repr, item->transform);
610     SP_OBJECT(new_item)->updateRepr();
612     Inkscape::GC::release(repr);
613     selection->set(new_item);
614     item->deleteObject();
616     sp_document_done(sp_desktop_document(desktop), SP_VERB_NONE, 
617                      /* TODO: annotate */ "sp-flowtext.cpp:617");
620 SPItem *SPFlowtext::get_frame(SPItem *after)
622     SPObject *ft = SP_OBJECT (this);
623     SPObject *region = NULL;
625     for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
626         if (SP_IS_FLOWREGION(o)) {
627             region = o;
628             break;
629         }
630     }
632     if (!region) return NULL;
634     bool past = false;
635     SPItem *frame = NULL;
637     for (SPObject *o = sp_object_first_child(SP_OBJECT(region)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
638         if (SP_IS_ITEM(o)) {
639             if (after == NULL || past) {
640                 frame = SP_ITEM(o);
641             } else {
642                 if (SP_ITEM(o) == after) {
643                     past = true;
644                 }
645             }
646         }
647     }
649     if (!frame) return NULL;
651     if (SP_IS_USE (frame)) {
652         return sp_use_get_original(SP_USE(frame));
653     } else {
654         return frame;
655     }
658 bool SPFlowtext::has_internal_frame()
660     SPItem *frame = get_frame(NULL);
662     return (frame && SP_OBJECT(this)->isAncestorOf(SP_OBJECT(frame)) && SP_IS_RECT(frame));
666 SPItem *create_flowtext_with_internal_frame (SPDesktop *desktop, NR::Point p0, NR::Point p1)
668     SPDocument *doc = sp_desktop_document (desktop);
670     Inkscape::XML::Node *root_repr = sp_repr_new("svg:flowRoot");
671     root_repr->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create
672     SPItem *ft_item = SP_ITEM(desktop->currentLayer()->appendChildRepr(root_repr));
673     SPObject *root_object = doc->getObjectByRepr(root_repr);
674     g_assert(SP_IS_FLOWTEXT(root_object));
676     Inkscape::XML::Node *region_repr = sp_repr_new("svg:flowRegion");
677     root_repr->appendChild(region_repr);
678     SPObject *region_object = doc->getObjectByRepr(region_repr);
679     g_assert(SP_IS_FLOWREGION(region_object));
681     Inkscape::XML::Node *rect_repr = sp_repr_new("svg:rect"); // FIXME: use path!!! after rects are converted to use path
682     region_repr->appendChild(rect_repr);
684     SPObject *rect = doc->getObjectByRepr(rect_repr);
686     p0 = sp_desktop_dt2root_xy_point(desktop, p0);
687     p1 = sp_desktop_dt2root_xy_point(desktop, p1);
688     using NR::X;
689     using NR::Y;
690     NR::Coord const x0 = MIN(p0[X], p1[X]);
691     NR::Coord const y0 = MIN(p0[Y], p1[Y]);
692     NR::Coord const x1 = MAX(p0[X], p1[X]);
693     NR::Coord const y1 = MAX(p0[Y], p1[Y]);
694     NR::Coord const w  = x1 - x0;
695     NR::Coord const h  = y1 - y0;
697     sp_rect_position_set(SP_RECT(rect), x0, y0, w, h);
698     SP_OBJECT(rect)->updateRepr();
700     Inkscape::XML::Node *para_repr = sp_repr_new("svg:flowPara");
701     root_repr->appendChild(para_repr);
702     SPObject *para_object = doc->getObjectByRepr(para_repr);
703     g_assert(SP_IS_FLOWPARA(para_object));
705     Inkscape::XML::Node *text = sp_repr_new_text("");
706     para_repr->appendChild(text);
708     Inkscape::GC::release(root_repr);
709     Inkscape::GC::release(region_repr);
710     Inkscape::GC::release(para_repr);
711     Inkscape::GC::release(rect_repr);
713     return ft_item;
717 /*
718   Local Variables:
719   mode:c++
720   c-file-style:"stroustrup"
721   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
722   indent-tabs-mode:nil
723   fill-column:99
724   End:
725 */
726 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :