Code

r10815@tres: ted | 2006-01-29 14:46:19 -0800
[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     new (&group->layout) Inkscape::Text::Layout();
109 static void
110 sp_flowtext_dispose(GObject *object)
112     SPFlowtext *group = (SPFlowtext*)object;
114     group->layout.~Layout();
117 static void
118 sp_flowtext_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
120     if (((SPObjectClass *) (parent_class))->child_added)
121         (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
123     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
126 /* fixme: hide (Lauris) */
128 static void
129 sp_flowtext_remove_child(SPObject *object, Inkscape::XML::Node *child)
131     if (((SPObjectClass *) (parent_class))->remove_child)
132         (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
134     object->requestModified(SP_OBJECT_MODIFIED_FLAG);
137 static void
138 sp_flowtext_update(SPObject *object, SPCtx *ctx, unsigned flags)
140     SPFlowtext *group = SP_FLOWTEXT(object);
141     SPItemCtx *ictx = (SPItemCtx *) ctx;
142     SPItemCtx cctx = *ictx;
144     if (((SPObjectClass *) (parent_class))->update)
145         ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
147     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
148     flags &= SP_OBJECT_MODIFIED_CASCADE;
150     GSList *l = NULL;
151     for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
152         g_object_ref(G_OBJECT(child));
153         l = g_slist_prepend(l, child);
154     }
155     l = g_slist_reverse(l);
156     while (l) {
157         SPObject *child = SP_OBJECT(l->data);
158         l = g_slist_remove(l, child);
159         if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
160             if (SP_IS_ITEM(child)) {
161                 SPItem const &chi = *SP_ITEM(child);
162                 cctx.i2doc = chi.transform * ictx->i2doc;
163                 cctx.i2vp = chi.transform * ictx->i2vp;
164                 child->updateDisplay((SPCtx *)&cctx, flags);
165             } else {
166                 child->updateDisplay(ctx, flags);
167             }
168         }
169         g_object_unref(G_OBJECT(child));
170     }
172     group->rebuildLayout();
174     // pass the bbox of the flowtext object as paintbox (used for paintserver fills)    
175     NRRect paintbox;
176     sp_item_invoke_bbox(group, &paintbox, NR::identity(), TRUE);
177     for (SPItemView *v = group->display; v != NULL; v = v->next) {
178         group->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
179         group->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
180     }
183 static void
184 sp_flowtext_modified(SPObject *object, guint flags)
186     SPObject *ft = SP_FLOWTEXT (object);
187     SPObject *region = NULL;
189     if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
190     flags &= SP_OBJECT_MODIFIED_CASCADE;
192     for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
193         if (SP_IS_FLOWREGION(o)) {
194             region = o;
195             break;
196         }
197     }
199     if (!region) return;
201     if (flags || (region->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
202         region->emitModified(flags); // pass down to the region only
203     }
206 static void
207 sp_flowtext_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
209     object->_requireSVGVersion(Inkscape::Version(1, 2));
211     if (((SPObjectClass *) (parent_class))->build) {
212         (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
213     }
215     sp_object_read_attr(object, "inkscape:layoutOptions");     // must happen after css has been read
218 static void
219 sp_flowtext_set(SPObject *object, unsigned key, gchar const *value)
221     SPFlowtext *group = (SPFlowtext *) object;
223     switch (key) {
224         case SP_ATTR_LAYOUT_OPTIONS: {
225             // deprecated attribute, read for backward compatibility only
226             SPCSSAttr *opts = sp_repr_css_attr((SP_OBJECT(group))->repr, "inkscape:layoutOptions");
227             {
228                 gchar const *val = sp_repr_css_property(opts, "justification", NULL);
229                 if (val != NULL && !object->style->text_align.set) {
230                     if ( strcmp(val, "0") == 0 || strcmp(val, "false") == 0 ) {
231                         object->style->text_align.value = SP_CSS_TEXT_ALIGN_LEFT;
232                     } else {
233                         object->style->text_align.value = SP_CSS_TEXT_ALIGN_JUSTIFY;
234                     }
235                     object->style->text_align.set = TRUE;
236                     object->style->text_align.inherit = FALSE;
237                     object->style->text_align.computed = object->style->text_align.value;
238                 }
239             }
240             /* no equivalent css attribute for these two (yet)
241             {
242                 gchar const *val = sp_repr_css_property(opts, "layoutAlgo", NULL);
243                 if ( val == NULL ) {
244                     group->algo = 0;
245                 } else {
246                     if ( strcmp(val, "better") == 0 ) {     // knuth-plass, never worked for general cases
247                         group->algo = 2;
248                     } else if ( strcmp(val, "simple") == 0 ) {   // greedy, but allowed lines to be compressed by up to 20% if it would make them fit
249                         group->algo = 1;
250                     } else if ( strcmp(val, "default") == 0 ) {    // the same one we use, a standard greedy
251                         group->algo = 0;
252                     }
253                 }
254             }
255             {   // This would probably translate to padding-left, if SPStyle had it.
256                 gchar const *val = sp_repr_css_property(opts, "par-indent", NULL);
257                 if ( val == NULL ) {
258                     group->par_indent = 0.0;
259                 } else {
260                     sp_repr_get_double((Inkscape::XML::Node*)opts, "par-indent", &group->par_indent);
261                 }
262             }
263             */
264             sp_repr_css_attr_unref(opts);
265             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
266             break;
267         }
268         default:
269             if (((SPObjectClass *) (parent_class))->set) {
270                 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
271             }
272             break;
273     }
276 static Inkscape::XML::Node *
277 sp_flowtext_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
279     if ( flags & SP_OBJECT_WRITE_BUILD ) {
280         if ( repr == NULL ) repr = sp_repr_new("svg:flowRoot");
281         GSList *l = NULL;
282         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
283             Inkscape::XML::Node *c_repr = NULL;
284             if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child)) {
285                 c_repr = child->updateRepr(NULL, flags);
286             } 
287             if ( c_repr ) l = g_slist_prepend(l, c_repr);
288         }
289         while ( l ) {
290             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
291             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
292             l = g_slist_remove(l, l->data);
293         }
294     } else {
295         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
296             if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child) ) {
297                 child->updateRepr(flags);
298             }
299         }
300     }
302     if (((SPObjectClass *) (parent_class))->write)
303         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
305     return repr;
308 static void
309 sp_flowtext_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
311     SPFlowtext *group = SP_FLOWTEXT(item);
312     group->layout.getBoundingBox(bbox, transform);
315 static void
316 sp_flowtext_print(SPItem *item, SPPrintContext *ctx)
318     SPFlowtext *group = SP_FLOWTEXT(item);
320     NRRect pbox;
321     sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
322     NRRect bbox;
323     sp_item_bbox_desktop(item, &bbox);
324     NRRect dbox;
325     dbox.x0 = 0.0;
326     dbox.y0 = 0.0;
327     dbox.x1 = sp_document_width(SP_OBJECT_DOCUMENT(item));
328     dbox.y1 = sp_document_height(SP_OBJECT_DOCUMENT(item));
329     NR::Matrix const ctm = sp_item_i2d_affine(item);
331     group->layout.print(ctx, &pbox, &dbox, &bbox, ctm);
335 static gchar *sp_flowtext_description(SPItem *item)
337     Inkscape::Text::Layout const &layout = SP_FLOWTEXT(item)->layout;
338     int const nChars = layout.iteratorToCharIndex(layout.end());
339     if (SP_FLOWTEXT(item)->has_internal_frame())
340         return g_strdup_printf(_("<b>Flowed text</b> (%d characters)"), nChars);
341     else 
342         return g_strdup_printf(_("<b>Linked flowed text</b> (%d characters)"), nChars);
345 static NRArenaItem *
346 sp_flowtext_show(SPItem *item, NRArena *arena, unsigned/* key*/, unsigned /*flags*/)
348     SPFlowtext *group = (SPFlowtext *) item;
349     NRArenaGroup *flowed = NRArenaGroup::create(arena);
350     nr_arena_group_set_transparent(flowed, FALSE);
352     // pass the bbox of the flowtext object as paintbox (used for paintserver fills)    
353     NRRect paintbox;
354     sp_item_invoke_bbox(item, &paintbox, NR::identity(), TRUE);
355     group->layout.show(flowed, &paintbox);
357     return flowed;
360 static void
361 sp_flowtext_hide(SPItem *item, unsigned int key)
363     if (((SPItemClass *) parent_class)->hide)
364         ((SPItemClass *) parent_class)->hide(item, key);
368 /*
369  *
370  */
372 void SPFlowtext::_buildLayoutInput(SPObject *root, Shape const *exclusion_shape, std::list<Shape> *shapes, SPObject **pending_line_break_object)
374     if (*pending_line_break_object) {
375         if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object))
376             layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
377         else
378             layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
379         *pending_line_break_object = NULL;
380     }
382     for (SPObject *child = sp_object_first_child(root) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
383         if (SP_IS_STRING(child)) {
384             if (*pending_line_break_object) {
385                 if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object))
386                     layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
387                 else
388                     layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
389                 *pending_line_break_object = NULL;
390             }
391             layout.appendText(SP_STRING(child)->string, root->style, child);
392         } else if (SP_IS_FLOWREGION(child)) {
393             std::vector<Shape*> const &computed = SP_FLOWREGION(child)->computed;
394             for (std::vector<Shape*>::const_iterator it = computed.begin() ; it != computed.end() ; it++) {
395                 shapes->push_back(Shape());
396                 if (exclusion_shape->hasEdges())
397                     shapes->back().Booleen(*it, const_cast<Shape*>(exclusion_shape), bool_op_diff);
398                 else
399                     shapes->back().Copy(*it);
400                 layout.appendWrapShape(&shapes->back());
401             }
402         }
403         else if (!SP_IS_FLOWREGIONEXCLUDE(child))
404             _buildLayoutInput(child, exclusion_shape, shapes, pending_line_break_object);
405     }
407     if (SP_IS_FLOWDIV(root) || SP_IS_FLOWPARA(root) || SP_IS_FLOWREGIONBREAK(root) || SP_IS_FLOWLINE(root)) {
408         if (!root->hasChildren())
409             layout.appendText("", root->style, root);
410         *pending_line_break_object = root;
411     }
414 Shape* SPFlowtext::_buildExclusionShape() const
416     Shape *shape = new Shape;
417     Shape *shape_temp = new Shape;
419     for (SPObject *child = children ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
420         // RH: is it right that this shouldn't be recursive?
421         if ( SP_IS_FLOWREGIONEXCLUDE(child) ) {
422             SPFlowregionExclude *c_child = SP_FLOWREGIONEXCLUDE(child);
423             if (c_child->computed == NULL || !c_child->computed->hasEdges())
424                 continue;
425             if (shape->hasEdges()) {
426                 shape_temp->Booleen(shape, c_child->computed, bool_op_union);
427                 std::swap(shape, shape_temp);
428             } else
429                 shape->Copy(c_child->computed);
430         }
431     }
432     delete shape_temp;
433     return shape;
436 void SPFlowtext::rebuildLayout()
438     std::list<Shape> shapes;
440     layout.clear();
441     Shape *exclusion_shape = _buildExclusionShape();
442     SPObject *pending_line_break_object = NULL;
443     _buildLayoutInput(this, exclusion_shape, &shapes, &pending_line_break_object);
444     delete exclusion_shape;
445     layout.calculateFlow();
446     //g_print(layout.dumpAsText().c_str());
449 void SPFlowtext::_clearFlow(NRArenaGroup *in_arena)
451     nr_arena_item_request_render(NR_ARENA_ITEM(in_arena));
452     for (NRArenaItem *child = in_arena->children; child != NULL; ) {
453         NRArenaItem *nchild = child->next;
455         nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
456         nr_arena_item_remove_child(NR_ARENA_ITEM(in_arena), child);
458         child = nchild;
459     }
462 void SPFlowtext::convert_to_text()
464     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
465     Inkscape::Selection *selection = SP_DT_SELECTION(desktop);
466     SPItem *item = selection->singleItem();
467     if (!SP_IS_FLOWTEXT(item)) return;
469     SPFlowtext *group = SP_FLOWTEXT(item);
471     if (!group->layout.outputExists()) return;
473     Inkscape::XML::Node *repr = sp_repr_new("svg:text");
474     repr->setAttribute("xml:space", "preserve");
475     repr->setAttribute("style", SP_OBJECT_REPR(group)->attribute("style"));
476     NR::Point anchor_point = group->layout.characterAnchorPoint(group->layout.begin());
477     sp_repr_set_svg_double(repr, "x", anchor_point[NR::X]);
478     sp_repr_set_svg_double(repr, "y", anchor_point[NR::Y]);
480     for (Inkscape::Text::Layout::iterator it = group->layout.begin() ; it != group->layout.end() ; ) {
481         
482             Inkscape::XML::Node *line_tspan = sp_repr_new("svg:tspan");
483         line_tspan->setAttribute("sodipodi:role", "line");
485         Inkscape::Text::Layout::iterator it_line_end = it;
486         it_line_end.nextStartOfLine();
487         while (it != it_line_end) {
489                 Inkscape::XML::Node *span_tspan = sp_repr_new("svg:tspan");
490             NR::Point anchor_point = group->layout.characterAnchorPoint(it);
491             // use kerning to simulate justification and whatnot
492             Inkscape::Text::Layout::iterator it_span_end = it;
493             it_span_end.nextStartOfSpan();
494             Inkscape::Text::Layout::OptionalTextTagAttrs attrs;
495             group->layout.simulateLayoutUsingKerning(it, it_span_end, &attrs);
496             // set x,y attributes only when we need to
497             bool set_x = false;
498             bool set_y = false;
499             if (!item->transform.test_identity()) {
500                 set_x = set_y = true;
501             } else {
502                 Inkscape::Text::Layout::iterator it_chunk_start = it;
503                 it_chunk_start.thisStartOfChunk();
504                 if (it == it_chunk_start) {
505                     set_x = true;
506                     // don't set y so linespacing adjustments and things will still work
507                 }
508                 Inkscape::Text::Layout::iterator it_shape_start = it;
509                 it_shape_start.thisStartOfShape();
510                 if (it == it_shape_start)
511                     set_y = true;
512             }
513             if (set_x && !attrs.dx.empty())
514                 attrs.dx[0] = 0.0;
515             TextTagAttributes(attrs).writeTo(span_tspan);
516             if (set_x)
517                 sp_repr_set_svg_double(span_tspan, "x", anchor_point[NR::X]);  // FIXME: this will pick up the wrong end of counter-directional runs
518             if (set_y)
519                 sp_repr_set_svg_double(span_tspan, "y", anchor_point[NR::Y]);
521             SPObject *source_obj;
522             Glib::ustring::iterator span_text_start_iter;
523             group->layout.getSourceOfCharacter(it, (void**)&source_obj, &span_text_start_iter);
524             gchar *style_text = sp_style_write_difference((SP_IS_STRING(source_obj) ? source_obj->parent : source_obj)->style, group->style);
525             if (style_text && *style_text) {
526                 span_tspan->setAttribute("style", style_text);
527                 g_free(style_text);
528             }
530             if (SP_IS_STRING(source_obj)) {
531                 Glib::ustring *string = &SP_STRING(source_obj)->string;
532                 SPObject *span_end_obj;
533                 Glib::ustring::iterator span_text_end_iter;
534                 group->layout.getSourceOfCharacter(it_span_end, (void**)&span_end_obj, &span_text_end_iter);
535                 if (span_end_obj != source_obj) {
536                     if (it_span_end == group->layout.end()) {
537                         span_text_end_iter = span_text_start_iter;
538                         for (int i = group->layout.iteratorToCharIndex(it_span_end) - group->layout.iteratorToCharIndex(it) ; i ; --i)
539                             ++span_text_end_iter;
540                     } else
541                         span_text_end_iter = string->end();    // spans will never straddle a source boundary
542                 }
544                 if (span_text_start_iter != span_text_end_iter) {
545                     Glib::ustring new_string;
546                     while (span_text_start_iter != span_text_end_iter)
547                         new_string += *span_text_start_iter++;    // grr. no substr() with iterators
548                     Inkscape::XML::Node *new_text = sp_repr_new_text(new_string.c_str());
549                     span_tspan->appendChild(new_text);
550                     Inkscape::GC::release(new_text);
551                 }
552             }
553             it = it_span_end;
555             line_tspan->appendChild(span_tspan);
556                 Inkscape::GC::release(span_tspan);
557         }
558         repr->appendChild(line_tspan);
559             Inkscape::GC::release(line_tspan);
560     }
562     Inkscape::XML::Node *parent = SP_OBJECT_REPR(item)->parent();
563     parent->appendChild(repr);
564     SPItem *new_item = (SPItem *) SP_DT_DOCUMENT(desktop)->getObjectByRepr(repr);
565     sp_item_write_transform(new_item, repr, item->transform);
566     SP_OBJECT(new_item)->updateRepr();
568     Inkscape::GC::release(repr);
569     selection->set(new_item);
570     item->deleteObject();
572     sp_document_done(SP_DT_DOCUMENT(desktop));
575 SPItem *SPFlowtext::get_frame(SPItem *after)
577     SPObject *ft = SP_OBJECT (this);
578     SPObject *region = NULL;
580     for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
581         if (SP_IS_FLOWREGION(o)) {
582             region = o;
583             break;
584         }
585     }
587     if (!region) return NULL;
589     bool past = false;
590     SPItem *frame = NULL;
592     for (SPObject *o = sp_object_first_child(SP_OBJECT(region)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
593         if (SP_IS_ITEM(o)) {
594             if (after == NULL || past) {
595                 frame = SP_ITEM(o);
596             } else {
597                 if (SP_ITEM(o) == after) {
598                     past = true;
599                 }
600             }
601         }
602     }
604     if (!frame) return NULL;
606     if (SP_IS_USE (frame)) {
607         return sp_use_get_original(SP_USE(frame));
608     } else {
609         return frame;
610     }
613 bool SPFlowtext::has_internal_frame()
615     SPItem *frame = get_frame(NULL);
617     return (frame && SP_OBJECT(this)->isAncestorOf(SP_OBJECT(frame)) && SP_IS_RECT(frame));
621 SPItem *create_flowtext_with_internal_frame (SPDesktop *desktop, NR::Point p0, NR::Point p1)
623     SPDocument *doc = SP_DT_DOCUMENT (desktop);
625     Inkscape::XML::Node *root_repr = sp_repr_new("svg:flowRoot");
626     root_repr->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create
627     SPItem *ft_item = SP_ITEM(desktop->currentLayer()->appendChildRepr(root_repr));
628     SPObject *root_object = doc->getObjectByRepr(root_repr);
629     g_assert(SP_IS_FLOWTEXT(root_object));
631     Inkscape::XML::Node *region_repr = sp_repr_new("svg:flowRegion");
632     root_repr->appendChild(region_repr);
633     SPObject *region_object = doc->getObjectByRepr(region_repr);
634     g_assert(SP_IS_FLOWREGION(region_object));
636     Inkscape::XML::Node *rect_repr = sp_repr_new("svg:rect"); // FIXME: use path!!! after rects are converted to use path
637     region_repr->appendChild(rect_repr);
639     SPObject *rect = doc->getObjectByRepr(rect_repr);
641     p0 = sp_desktop_dt2root_xy_point(desktop, p0);
642     p1 = sp_desktop_dt2root_xy_point(desktop, p1);
643     using NR::X;
644     using NR::Y;
645     NR::Coord const x0 = MIN(p0[X], p1[X]);
646     NR::Coord const y0 = MIN(p0[Y], p1[Y]);
647     NR::Coord const x1 = MAX(p0[X], p1[X]);
648     NR::Coord const y1 = MAX(p0[Y], p1[Y]);
649     NR::Coord const w  = x1 - x0;
650     NR::Coord const h  = y1 - y0;
652     sp_rect_position_set(SP_RECT(rect), x0, y0, w, h);
653     SP_OBJECT(rect)->updateRepr();
655     Inkscape::XML::Node *para_repr = sp_repr_new("svg:flowPara");
656     root_repr->appendChild(para_repr);
657     SPObject *para_object = doc->getObjectByRepr(para_repr);
658     g_assert(SP_IS_FLOWPARA(para_object));
660     Inkscape::XML::Node *text = sp_repr_new_text("");
661     para_repr->appendChild(text);
663     Inkscape::GC::release(root_repr);
664     Inkscape::GC::release(region_repr);
665     Inkscape::GC::release(para_repr);
666     Inkscape::GC::release(rect_repr);
668     return ft_item;
672 /*
673   Local Variables:
674   mode:c++
675   c-file-style:"stroustrup"
676   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
677   indent-tabs-mode:nil
678   fill-column:99
679   End:
680 */
681 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :