Code

b589bb64d6dc298255e35351470bfd009faee566
[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         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
295         if ( repr == NULL ) repr = xml_doc->createElement("svg:flowRoot");
296         GSList *l = NULL;
297         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
298             Inkscape::XML::Node *c_repr = NULL;
299             if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child)) {
300                 c_repr = child->updateRepr(NULL, flags);
301             }
302             if ( c_repr ) l = g_slist_prepend(l, c_repr);
303         }
304         while ( l ) {
305             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
306             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
307             l = g_slist_remove(l, l->data);
308         }
309     } else {
310         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
311             if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child) ) {
312                 child->updateRepr(flags);
313             }
314         }
315     }
317     if (((SPObjectClass *) (parent_class))->write)
318         ((SPObjectClass *) (parent_class))->write(object, repr, flags);
320     return repr;
323 static void
324 sp_flowtext_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
326     SPFlowtext *group = SP_FLOWTEXT(item);
327     group->layout.getBoundingBox(bbox, transform);
329     // Add stroke width
330     SPStyle* style=SP_OBJECT_STYLE (item);
331     if (style->stroke.type != SP_PAINT_TYPE_NONE) {
332         double const scale = expansion(transform);
333         if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
334             double const width = MAX(0.125, style->stroke_width.computed * scale);
335             if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
336                 bbox->x0-=0.5*width;
337                 bbox->x1+=0.5*width;
338                 bbox->y0-=0.5*width;
339                 bbox->y1+=0.5*width;
340             }
341         }
342     }
345 static void
346 sp_flowtext_print(SPItem *item, SPPrintContext *ctx)
348     SPFlowtext *group = SP_FLOWTEXT(item);
350     NRRect pbox;
351     sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
352     NRRect bbox;
353     sp_item_bbox_desktop(item, &bbox);
354     NRRect dbox;
355     dbox.x0 = 0.0;
356     dbox.y0 = 0.0;
357     dbox.x1 = sp_document_width(SP_OBJECT_DOCUMENT(item));
358     dbox.y1 = sp_document_height(SP_OBJECT_DOCUMENT(item));
359     NR::Matrix const ctm = sp_item_i2d_affine(item);
361     group->layout.print(ctx, &pbox, &dbox, &bbox, ctm);
365 static gchar *sp_flowtext_description(SPItem *item)
367     Inkscape::Text::Layout const &layout = SP_FLOWTEXT(item)->layout;
368     int const nChars = layout.iteratorToCharIndex(layout.end());
369     if (SP_FLOWTEXT(item)->has_internal_frame())
370         return g_strdup_printf(ngettext("<b>Flowed text</b> (%d character)", "<b>Flowed text</b> (%d characters)", nChars), nChars);
371     else
372         return g_strdup_printf(ngettext("<b>Linked flowed text</b> (%d character)", "<b>Linked flowed text</b> (%d characters)", nChars), nChars);
375 static NRArenaItem *
376 sp_flowtext_show(SPItem *item, NRArena *arena, unsigned/* key*/, unsigned /*flags*/)
378     SPFlowtext *group = (SPFlowtext *) item;
379     NRArenaGroup *flowed = NRArenaGroup::create(arena);
380     nr_arena_group_set_transparent(flowed, FALSE);
382     // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
383     NRRect paintbox;
384     sp_item_invoke_bbox(item, &paintbox, NR::identity(), TRUE);
385     group->layout.show(flowed, &paintbox);
387     return flowed;
390 static void
391 sp_flowtext_hide(SPItem *item, unsigned int key)
393     if (((SPItemClass *) parent_class)->hide)
394         ((SPItemClass *) parent_class)->hide(item, key);
398 /*
399  *
400  */
402 void SPFlowtext::_buildLayoutInput(SPObject *root, Shape const *exclusion_shape, std::list<Shape> *shapes, SPObject **pending_line_break_object)
404     Inkscape::Text::Layout::OptionalTextTagAttrs pi;
405     bool with_indent = false;
407     if (SP_IS_FLOWPARA(root)) {
408         // emulate par-indent with the first char's kern
409         SPObject *t = root;
410         for ( ; t != NULL && !SP_IS_FLOWTEXT(t); t = SP_OBJECT_PARENT(t));
411         if (SP_IS_FLOWTEXT(t)) {
412             double indent = SP_FLOWTEXT(t)->par_indent;
413             if (indent != 0) {
414                 with_indent = true;
415                 SVGLength sl;
416                 sl.value = sl.computed = indent;
417                 sl._set = true;
418                 pi.dx.push_back(sl);
419             }
420         }
421     }
423     if (*pending_line_break_object) {
424         if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object)) {
425             layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
426         } else {
427             layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
428         }
429         *pending_line_break_object = NULL;
430     }
432     for (SPObject *child = sp_object_first_child(root) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
433         if (SP_IS_STRING(child)) {
434             if (*pending_line_break_object) {
435                 if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object))
436                     layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
437                 else {
438                     layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
439                 }
440                 *pending_line_break_object = NULL;
441             }
442             if (with_indent)
443                 layout.appendText(SP_STRING(child)->string, root->style, child, &pi);
444             else
445                 layout.appendText(SP_STRING(child)->string, root->style, child);
446         } else if (SP_IS_FLOWREGION(child)) {
447             std::vector<Shape*> const &computed = SP_FLOWREGION(child)->computed;
448             for (std::vector<Shape*>::const_iterator it = computed.begin() ; it != computed.end() ; it++) {
449                 shapes->push_back(Shape());
450                 if (exclusion_shape->hasEdges())
451                     shapes->back().Booleen(*it, const_cast<Shape*>(exclusion_shape), bool_op_diff);
452                 else
453                     shapes->back().Copy(*it);
454                 layout.appendWrapShape(&shapes->back());
455             }
456         }
457         else if (!SP_IS_FLOWREGIONEXCLUDE(child))
458             _buildLayoutInput(child, exclusion_shape, shapes, pending_line_break_object);
459     }
461     if (SP_IS_FLOWDIV(root) || SP_IS_FLOWPARA(root) || SP_IS_FLOWREGIONBREAK(root) || SP_IS_FLOWLINE(root)) {
462         if (!root->hasChildren())
463             layout.appendText("", root->style, root);
464         *pending_line_break_object = root;
465     }
468 Shape* SPFlowtext::_buildExclusionShape() const
470     Shape *shape = new Shape;
471     Shape *shape_temp = new Shape;
473     for (SPObject *child = children ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
474         // RH: is it right that this shouldn't be recursive?
475         if ( SP_IS_FLOWREGIONEXCLUDE(child) ) {
476             SPFlowregionExclude *c_child = SP_FLOWREGIONEXCLUDE(child);
477             if (c_child->computed == NULL || !c_child->computed->hasEdges())
478                 continue;
479             if (shape->hasEdges()) {
480                 shape_temp->Booleen(shape, c_child->computed, bool_op_union);
481                 std::swap(shape, shape_temp);
482             } else
483                 shape->Copy(c_child->computed);
484         }
485     }
486     delete shape_temp;
487     return shape;
490 void SPFlowtext::rebuildLayout()
492     std::list<Shape> shapes;
494     layout.clear();
495     Shape *exclusion_shape = _buildExclusionShape();
496     SPObject *pending_line_break_object = NULL;
497     _buildLayoutInput(this, exclusion_shape, &shapes, &pending_line_break_object);
498     delete exclusion_shape;
499     layout.calculateFlow();
500     //g_print(layout.dumpAsText().c_str());
503 void SPFlowtext::_clearFlow(NRArenaGroup *in_arena)
505     nr_arena_item_request_render(NR_ARENA_ITEM(in_arena));
506     for (NRArenaItem *child = in_arena->children; child != NULL; ) {
507         NRArenaItem *nchild = child->next;
509         nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
510         nr_arena_item_remove_child(NR_ARENA_ITEM(in_arena), child);
512         child = nchild;
513     }
516 Inkscape::XML::Node *
517 SPFlowtext::getAsText()
519     if (!this->layout.outputExists()) return NULL;
521     SPItem *item = SP_ITEM(this);
523     Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(this));
524     Inkscape::XML::Node *repr = xml_doc->createElement("svg:text");
525     repr->setAttribute("xml:space", "preserve");
526     repr->setAttribute("style", SP_OBJECT_REPR(this)->attribute("style"));
527     NR::Point anchor_point = this->layout.characterAnchorPoint(this->layout.begin());
528     sp_repr_set_svg_double(repr, "x", anchor_point[NR::X]);
529     sp_repr_set_svg_double(repr, "y", anchor_point[NR::Y]);
531     for (Inkscape::Text::Layout::iterator it = this->layout.begin() ; it != this->layout.end() ; ) {
532         Inkscape::XML::Node *line_tspan = xml_doc->createElement("svg:tspan");
533         line_tspan->setAttribute("sodipodi:role", "line");
535         Inkscape::Text::Layout::iterator it_line_end = it;
536         it_line_end.nextStartOfLine();
538         while (it != it_line_end) {
540             Inkscape::XML::Node *span_tspan = xml_doc->createElement("svg:tspan");
541             NR::Point anchor_point = this->layout.characterAnchorPoint(it);
542             // use kerning to simulate justification and whatnot
543             Inkscape::Text::Layout::iterator it_span_end = it;
544             it_span_end.nextStartOfSpan();
545             Inkscape::Text::Layout::OptionalTextTagAttrs attrs;
546             this->layout.simulateLayoutUsingKerning(it, it_span_end, &attrs);
547             // set x,y attributes only when we need to
548             bool set_x = false;
549             bool set_y = false;
550             if (!item->transform.test_identity()) {
551                 set_x = set_y = true;
552             } else {
553                 Inkscape::Text::Layout::iterator it_chunk_start = it;
554                 it_chunk_start.thisStartOfChunk();
555                 if (it == it_chunk_start) {
556                     set_x = true;
557                     // don't set y so linespacing adjustments and things will still work
558                 }
559                 Inkscape::Text::Layout::iterator it_shape_start = it;
560                 it_shape_start.thisStartOfShape();
561                 if (it == it_shape_start)
562                     set_y = true;
563             }
564             if (set_x && !attrs.dx.empty())
565                 attrs.dx[0] = 0.0;
566             TextTagAttributes(attrs).writeTo(span_tspan);
567             if (set_x)
568                 sp_repr_set_svg_double(span_tspan, "x", anchor_point[NR::X]);  // FIXME: this will pick up the wrong end of counter-directional runs
569             if (set_y)
570                 sp_repr_set_svg_double(span_tspan, "y", anchor_point[NR::Y]);
572             SPObject *source_obj = 0;
573             void *rawptr = 0;
574             Glib::ustring::iterator span_text_start_iter;
575             this->layout.getSourceOfCharacter(it, &rawptr, &span_text_start_iter);
576             source_obj = SP_OBJECT (rawptr);
577             gchar *style_text = sp_style_write_difference((SP_IS_STRING(source_obj) ? source_obj->parent : source_obj)->style, this->style);
578             if (style_text && *style_text) {
579                 span_tspan->setAttribute("style", style_text);
580                 g_free(style_text);
581             }
583             if (SP_IS_STRING(source_obj)) {
584                 Glib::ustring *string = &SP_STRING(source_obj)->string;
585                 SPObject *span_end_obj = 0;
586                 void *rawptr = 0;
587                 Glib::ustring::iterator span_text_end_iter;
588                 this->layout.getSourceOfCharacter(it_span_end, &rawptr, &span_text_end_iter);
589                 span_end_obj = SP_OBJECT(rawptr);
590                 if (span_end_obj != source_obj) {
591                     if (it_span_end == this->layout.end()) {
592                         span_text_end_iter = span_text_start_iter;
593                         for (int i = this->layout.iteratorToCharIndex(it_span_end) - this->layout.iteratorToCharIndex(it) ; i ; --i)
594                             ++span_text_end_iter;
595                     } else
596                         span_text_end_iter = string->end();    // spans will never straddle a source boundary
597                 }
599                 if (span_text_start_iter != span_text_end_iter) {
600                     Glib::ustring new_string;
601                     while (span_text_start_iter != span_text_end_iter)
602                         new_string += *span_text_start_iter++;    // grr. no substr() with iterators
603                     Inkscape::XML::Node *new_text = xml_doc->createTextNode(new_string.c_str());
604                     span_tspan->appendChild(new_text);
605                     Inkscape::GC::release(new_text);
606                 }
607             }
608             it = it_span_end;
610             line_tspan->appendChild(span_tspan);
611             Inkscape::GC::release(span_tspan);
612         }
613         repr->appendChild(line_tspan);
614         Inkscape::GC::release(line_tspan);
615     }
617     return repr;
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::Document *xml_doc = sp_document_repr_doc(doc);
671     Inkscape::XML::Node *root_repr = xml_doc->createElement("svg:flowRoot");
672     root_repr->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create
673     SPItem *ft_item = SP_ITEM(desktop->currentLayer()->appendChildRepr(root_repr));
674     SPObject *root_object = doc->getObjectByRepr(root_repr);
675     g_assert(SP_IS_FLOWTEXT(root_object));
677     Inkscape::XML::Node *region_repr = xml_doc->createElement("svg:flowRegion");
678     root_repr->appendChild(region_repr);
679     SPObject *region_object = doc->getObjectByRepr(region_repr);
680     g_assert(SP_IS_FLOWREGION(region_object));
682     Inkscape::XML::Node *rect_repr = xml_doc->createElement("svg:rect"); // FIXME: use path!!! after rects are converted to use path
683     region_repr->appendChild(rect_repr);
685     SPObject *rect = doc->getObjectByRepr(rect_repr);
687     p0 = sp_desktop_dt2root_xy_point(desktop, p0);
688     p1 = sp_desktop_dt2root_xy_point(desktop, p1);
689     using NR::X;
690     using NR::Y;
691     NR::Coord const x0 = MIN(p0[X], p1[X]);
692     NR::Coord const y0 = MIN(p0[Y], p1[Y]);
693     NR::Coord const x1 = MAX(p0[X], p1[X]);
694     NR::Coord const y1 = MAX(p0[Y], p1[Y]);
695     NR::Coord const w  = x1 - x0;
696     NR::Coord const h  = y1 - y0;
698     sp_rect_position_set(SP_RECT(rect), x0, y0, w, h);
699     SP_OBJECT(rect)->updateRepr();
701     Inkscape::XML::Node *para_repr = xml_doc->createElement("svg:flowPara");
702     root_repr->appendChild(para_repr);
703     SPObject *para_object = doc->getObjectByRepr(para_repr);
704     g_assert(SP_IS_FLOWPARA(para_object));
706     Inkscape::XML::Node *text = xml_doc->createTextNode("");
707     para_repr->appendChild(text);
709     Inkscape::GC::release(root_repr);
710     Inkscape::GC::release(region_repr);
711     Inkscape::GC::release(para_repr);
712     Inkscape::GC::release(rect_repr);
714     return ft_item;
718 /*
719   Local Variables:
720   mode:c++
721   c-file-style:"stroustrup"
722   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
723   indent-tabs-mode:nil
724   fill-column:99
725   End:
726 */
727 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :