Code

implement par_indent in flowtext, no ui yet, done via first character's dx
[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);
316 static void
317 sp_flowtext_print(SPItem *item, SPPrintContext *ctx)
319     SPFlowtext *group = SP_FLOWTEXT(item);
321     NRRect pbox;
322     sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
323     NRRect bbox;
324     sp_item_bbox_desktop(item, &bbox);
325     NRRect dbox;
326     dbox.x0 = 0.0;
327     dbox.y0 = 0.0;
328     dbox.x1 = sp_document_width(SP_OBJECT_DOCUMENT(item));
329     dbox.y1 = sp_document_height(SP_OBJECT_DOCUMENT(item));
330     NR::Matrix const ctm = sp_item_i2d_affine(item);
332     group->layout.print(ctx, &pbox, &dbox, &bbox, ctm);
336 static gchar *sp_flowtext_description(SPItem *item)
338     Inkscape::Text::Layout const &layout = SP_FLOWTEXT(item)->layout;
339     int const nChars = layout.iteratorToCharIndex(layout.end());
340     if (SP_FLOWTEXT(item)->has_internal_frame())
341         return g_strdup_printf(_("<b>Flowed text</b> (%d characters)"), nChars);
342     else 
343         return g_strdup_printf(_("<b>Linked flowed text</b> (%d characters)"), nChars);
346 static NRArenaItem *
347 sp_flowtext_show(SPItem *item, NRArena *arena, unsigned/* key*/, unsigned /*flags*/)
349     SPFlowtext *group = (SPFlowtext *) item;
350     NRArenaGroup *flowed = NRArenaGroup::create(arena);
351     nr_arena_group_set_transparent(flowed, FALSE);
353     // pass the bbox of the flowtext object as paintbox (used for paintserver fills)    
354     NRRect paintbox;
355     sp_item_invoke_bbox(item, &paintbox, NR::identity(), TRUE);
356     group->layout.show(flowed, &paintbox);
358     return flowed;
361 static void
362 sp_flowtext_hide(SPItem *item, unsigned int key)
364     if (((SPItemClass *) parent_class)->hide)
365         ((SPItemClass *) parent_class)->hide(item, key);
369 /*
370  *
371  */
373 void SPFlowtext::_buildLayoutInput(SPObject *root, Shape const *exclusion_shape, std::list<Shape> *shapes, SPObject **pending_line_break_object)
375     Inkscape::Text::Layout::OptionalTextTagAttrs pi;
376     bool with_indent = false;
378     if (SP_IS_FLOWPARA(root)) {
379         // emulate par-indent with the first char's kern
380         SPObject *t = root;
381         for ( ; t != NULL && !SP_IS_FLOWTEXT(t); t = SP_OBJECT_PARENT(t));
382         if (SP_IS_FLOWTEXT(t)) {
383             double indent = SP_FLOWTEXT(t)->par_indent;
384             if (indent != 0) {
385                 with_indent = true;
386                 SVGLength sl;
387                 sl.value = sl.computed = indent;
388                 sl._set = true;
389                 pi.dx.push_back(sl);
390             }
391         }
392     }
394     if (*pending_line_break_object) {
395         if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object)) {
396             layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
397         } else {
398             layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
399         }
400         *pending_line_break_object = NULL;
401     }
403     for (SPObject *child = sp_object_first_child(root) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
404         if (SP_IS_STRING(child)) {
405             if (*pending_line_break_object) {
406                 if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object))
407                     layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
408                 else {
409                     layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
410                 }
411                 *pending_line_break_object = NULL;
412             }
413             if (with_indent)
414                 layout.appendText(SP_STRING(child)->string, root->style, child, &pi);
415             else 
416                 layout.appendText(SP_STRING(child)->string, root->style, child);
417         } else if (SP_IS_FLOWREGION(child)) {
418             std::vector<Shape*> const &computed = SP_FLOWREGION(child)->computed;
419             for (std::vector<Shape*>::const_iterator it = computed.begin() ; it != computed.end() ; it++) {
420                 shapes->push_back(Shape());
421                 if (exclusion_shape->hasEdges())
422                     shapes->back().Booleen(*it, const_cast<Shape*>(exclusion_shape), bool_op_diff);
423                 else
424                     shapes->back().Copy(*it);
425                 layout.appendWrapShape(&shapes->back());
426             }
427         }
428         else if (!SP_IS_FLOWREGIONEXCLUDE(child))
429             _buildLayoutInput(child, exclusion_shape, shapes, pending_line_break_object);
430     }
432     if (SP_IS_FLOWDIV(root) || SP_IS_FLOWPARA(root) || SP_IS_FLOWREGIONBREAK(root) || SP_IS_FLOWLINE(root)) {
433         if (!root->hasChildren())
434             layout.appendText("", root->style, root);
435         *pending_line_break_object = root;
436     }
439 Shape* SPFlowtext::_buildExclusionShape() const
441     Shape *shape = new Shape;
442     Shape *shape_temp = new Shape;
444     for (SPObject *child = children ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
445         // RH: is it right that this shouldn't be recursive?
446         if ( SP_IS_FLOWREGIONEXCLUDE(child) ) {
447             SPFlowregionExclude *c_child = SP_FLOWREGIONEXCLUDE(child);
448             if (c_child->computed == NULL || !c_child->computed->hasEdges())
449                 continue;
450             if (shape->hasEdges()) {
451                 shape_temp->Booleen(shape, c_child->computed, bool_op_union);
452                 std::swap(shape, shape_temp);
453             } else
454                 shape->Copy(c_child->computed);
455         }
456     }
457     delete shape_temp;
458     return shape;
461 void SPFlowtext::rebuildLayout()
463     std::list<Shape> shapes;
465     layout.clear();
466     Shape *exclusion_shape = _buildExclusionShape();
467     SPObject *pending_line_break_object = NULL;
468     _buildLayoutInput(this, exclusion_shape, &shapes, &pending_line_break_object);
469     delete exclusion_shape;
470     layout.calculateFlow();
471     //g_print(layout.dumpAsText().c_str());
474 void SPFlowtext::_clearFlow(NRArenaGroup *in_arena)
476     nr_arena_item_request_render(NR_ARENA_ITEM(in_arena));
477     for (NRArenaItem *child = in_arena->children; child != NULL; ) {
478         NRArenaItem *nchild = child->next;
480         nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
481         nr_arena_item_remove_child(NR_ARENA_ITEM(in_arena), child);
483         child = nchild;
484     }
487 void SPFlowtext::convert_to_text()
489     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
490     Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
491     SPItem *item = selection->singleItem();
492     if (!SP_IS_FLOWTEXT(item)) return;
494     SPFlowtext *group = SP_FLOWTEXT(item);
496     if (!group->layout.outputExists()) return;
498     Inkscape::XML::Node *repr = sp_repr_new("svg:text");
499     repr->setAttribute("xml:space", "preserve");
500     repr->setAttribute("style", SP_OBJECT_REPR(group)->attribute("style"));
501     NR::Point anchor_point = group->layout.characterAnchorPoint(group->layout.begin());
502     sp_repr_set_svg_double(repr, "x", anchor_point[NR::X]);
503     sp_repr_set_svg_double(repr, "y", anchor_point[NR::Y]);
505     for (Inkscape::Text::Layout::iterator it = group->layout.begin() ; it != group->layout.end() ; ) {
506         
507             Inkscape::XML::Node *line_tspan = sp_repr_new("svg:tspan");
508         line_tspan->setAttribute("sodipodi:role", "line");
510         Inkscape::Text::Layout::iterator it_line_end = it;
511         it_line_end.nextStartOfLine();
512         while (it != it_line_end) {
514                 Inkscape::XML::Node *span_tspan = sp_repr_new("svg:tspan");
515             NR::Point anchor_point = group->layout.characterAnchorPoint(it);
516             // use kerning to simulate justification and whatnot
517             Inkscape::Text::Layout::iterator it_span_end = it;
518             it_span_end.nextStartOfSpan();
519             Inkscape::Text::Layout::OptionalTextTagAttrs attrs;
520             group->layout.simulateLayoutUsingKerning(it, it_span_end, &attrs);
521             // set x,y attributes only when we need to
522             bool set_x = false;
523             bool set_y = false;
524             if (!item->transform.test_identity()) {
525                 set_x = set_y = true;
526             } else {
527                 Inkscape::Text::Layout::iterator it_chunk_start = it;
528                 it_chunk_start.thisStartOfChunk();
529                 if (it == it_chunk_start) {
530                     set_x = true;
531                     // don't set y so linespacing adjustments and things will still work
532                 }
533                 Inkscape::Text::Layout::iterator it_shape_start = it;
534                 it_shape_start.thisStartOfShape();
535                 if (it == it_shape_start)
536                     set_y = true;
537             }
538             if (set_x && !attrs.dx.empty())
539                 attrs.dx[0] = 0.0;
540             TextTagAttributes(attrs).writeTo(span_tspan);
541             if (set_x)
542                 sp_repr_set_svg_double(span_tspan, "x", anchor_point[NR::X]);  // FIXME: this will pick up the wrong end of counter-directional runs
543             if (set_y)
544                 sp_repr_set_svg_double(span_tspan, "y", anchor_point[NR::Y]);
546             SPObject *source_obj;
547             Glib::ustring::iterator span_text_start_iter;
548             group->layout.getSourceOfCharacter(it, (void**)&source_obj, &span_text_start_iter);
549             gchar *style_text = sp_style_write_difference((SP_IS_STRING(source_obj) ? source_obj->parent : source_obj)->style, group->style);
550             if (style_text && *style_text) {
551                 span_tspan->setAttribute("style", style_text);
552                 g_free(style_text);
553             }
555             if (SP_IS_STRING(source_obj)) {
556                 Glib::ustring *string = &SP_STRING(source_obj)->string;
557                 SPObject *span_end_obj;
558                 Glib::ustring::iterator span_text_end_iter;
559                 group->layout.getSourceOfCharacter(it_span_end, (void**)&span_end_obj, &span_text_end_iter);
560                 if (span_end_obj != source_obj) {
561                     if (it_span_end == group->layout.end()) {
562                         span_text_end_iter = span_text_start_iter;
563                         for (int i = group->layout.iteratorToCharIndex(it_span_end) - group->layout.iteratorToCharIndex(it) ; i ; --i)
564                             ++span_text_end_iter;
565                     } else
566                         span_text_end_iter = string->end();    // spans will never straddle a source boundary
567                 }
569                 if (span_text_start_iter != span_text_end_iter) {
570                     Glib::ustring new_string;
571                     while (span_text_start_iter != span_text_end_iter)
572                         new_string += *span_text_start_iter++;    // grr. no substr() with iterators
573                     Inkscape::XML::Node *new_text = sp_repr_new_text(new_string.c_str());
574                     span_tspan->appendChild(new_text);
575                     Inkscape::GC::release(new_text);
576                 }
577             }
578             it = it_span_end;
580             line_tspan->appendChild(span_tspan);
581                 Inkscape::GC::release(span_tspan);
582         }
583         repr->appendChild(line_tspan);
584             Inkscape::GC::release(line_tspan);
585     }
587     Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
588     parent->appendChild(repr);
589     SPItem *new_item = (SPItem *) SP_DT_DOCUMENT(desktop)->getObjectByRepr(repr);
590     sp_item_write_transform(new_item, repr, item->transform);
591     SP_OBJECT(new_item)->updateRepr();
593     Inkscape::GC::release(repr);
594     selection->set(new_item);
595     item->deleteObject();
597     sp_document_done(SP_DT_DOCUMENT(desktop));
600 SPItem *SPFlowtext::get_frame(SPItem *after)
602     SPObject *ft = SP_OBJECT (this);
603     SPObject *region = NULL;
605     for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
606         if (SP_IS_FLOWREGION(o)) {
607             region = o;
608             break;
609         }
610     }
612     if (!region) return NULL;
614     bool past = false;
615     SPItem *frame = NULL;
617     for (SPObject *o = sp_object_first_child(SP_OBJECT(region)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
618         if (SP_IS_ITEM(o)) {
619             if (after == NULL || past) {
620                 frame = SP_ITEM(o);
621             } else {
622                 if (SP_ITEM(o) == after) {
623                     past = true;
624                 }
625             }
626         }
627     }
629     if (!frame) return NULL;
631     if (SP_IS_USE (frame)) {
632         return sp_use_get_original(SP_USE(frame));
633     } else {
634         return frame;
635     }
638 bool SPFlowtext::has_internal_frame()
640     SPItem *frame = get_frame(NULL);
642     return (frame && SP_OBJECT(this)->isAncestorOf(SP_OBJECT(frame)) && SP_IS_RECT(frame));
646 SPItem *create_flowtext_with_internal_frame (SPDesktop *desktop, NR::Point p0, NR::Point p1)
648     SPDocument *doc = SP_DT_DOCUMENT (desktop);
650     Inkscape::XML::Node *root_repr = sp_repr_new("svg:flowRoot");
651     root_repr->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create
652     SPItem *ft_item = SP_ITEM(desktop->currentLayer()->appendChildRepr(root_repr));
653     SPObject *root_object = doc->getObjectByRepr(root_repr);
654     g_assert(SP_IS_FLOWTEXT(root_object));
656     Inkscape::XML::Node *region_repr = sp_repr_new("svg:flowRegion");
657     root_repr->appendChild(region_repr);
658     SPObject *region_object = doc->getObjectByRepr(region_repr);
659     g_assert(SP_IS_FLOWREGION(region_object));
661     Inkscape::XML::Node *rect_repr = sp_repr_new("svg:rect"); // FIXME: use path!!! after rects are converted to use path
662     region_repr->appendChild(rect_repr);
664     SPObject *rect = doc->getObjectByRepr(rect_repr);
666     p0 = sp_desktop_dt2root_xy_point(desktop, p0);
667     p1 = sp_desktop_dt2root_xy_point(desktop, p1);
668     using NR::X;
669     using NR::Y;
670     NR::Coord const x0 = MIN(p0[X], p1[X]);
671     NR::Coord const y0 = MIN(p0[Y], p1[Y]);
672     NR::Coord const x1 = MAX(p0[X], p1[X]);
673     NR::Coord const y1 = MAX(p0[Y], p1[Y]);
674     NR::Coord const w  = x1 - x0;
675     NR::Coord const h  = y1 - y0;
677     sp_rect_position_set(SP_RECT(rect), x0, y0, w, h);
678     SP_OBJECT(rect)->updateRepr();
680     Inkscape::XML::Node *para_repr = sp_repr_new("svg:flowPara");
681     root_repr->appendChild(para_repr);
682     SPObject *para_object = doc->getObjectByRepr(para_repr);
683     g_assert(SP_IS_FLOWPARA(para_object));
685     Inkscape::XML::Node *text = sp_repr_new_text("");
686     para_repr->appendChild(text);
688     Inkscape::GC::release(root_repr);
689     Inkscape::GC::release(region_repr);
690     Inkscape::GC::release(para_repr);
691     Inkscape::GC::release(rect_repr);
693     return ft_item;
697 /*
698   Local Variables:
699   mode:c++
700   c-file-style:"stroustrup"
701   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
702   indent-tabs-mode:nil
703   fill-column:99
704   End:
705 */
706 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :