1 /*
2 */
4 #ifdef HAVE_CONFIG_H
5 # include "config.h"
6 #endif
7 #include <glibmm/i18n.h>
8 #include <cstring>
9 #include <string>
11 #include "attributes.h"
12 #include "xml/repr.h"
13 #include "style.h"
14 #include "inkscape.h"
15 #include "document.h"
16 #include "selection.h"
17 #include "desktop-handles.h"
18 #include "desktop.h"
20 #include "xml/repr.h"
22 #include "sp-flowdiv.h"
23 #include "sp-flowregion.h"
24 #include "sp-flowtext.h"
25 #include "sp-string.h"
26 #include "sp-use.h"
27 #include "sp-rect.h"
28 #include "text-tag-attributes.h"
29 #include "text-chemistry.h"
32 #include "livarot/Shape.h"
34 #include "display/nr-arena-glyphs.h"
37 static void sp_flowtext_class_init(SPFlowtextClass *klass);
38 static void sp_flowtext_init(SPFlowtext *group);
39 static void sp_flowtext_dispose(GObject *object);
41 static void sp_flowtext_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref);
42 static void sp_flowtext_remove_child(SPObject *object, Inkscape::XML::Node *child);
43 static void sp_flowtext_update(SPObject *object, SPCtx *ctx, guint flags);
44 static void sp_flowtext_modified(SPObject *object, guint flags);
45 static Inkscape::XML::Node *sp_flowtext_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
46 static void sp_flowtext_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
47 static void sp_flowtext_set(SPObject *object, unsigned key, gchar const *value);
49 static void sp_flowtext_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags);
50 static void sp_flowtext_print(SPItem *item, SPPrintContext *ctx);
51 static gchar *sp_flowtext_description(SPItem *item);
52 static NRArenaItem *sp_flowtext_show(SPItem *item, NRArena *arena, unsigned key, unsigned flags);
53 static void sp_flowtext_hide(SPItem *item, unsigned key);
55 static SPItemClass *parent_class;
57 GType
58 sp_flowtext_get_type(void)
59 {
60 static GType group_type = 0;
61 if (!group_type) {
62 GTypeInfo group_info = {
63 sizeof(SPFlowtextClass),
64 NULL, /* base_init */
65 NULL, /* base_finalize */
66 (GClassInitFunc) sp_flowtext_class_init,
67 NULL, /* class_finalize */
68 NULL, /* class_data */
69 sizeof(SPFlowtext),
70 16, /* n_preallocs */
71 (GInstanceInitFunc) sp_flowtext_init,
72 NULL, /* value_table */
73 };
74 group_type = g_type_register_static(SP_TYPE_ITEM, "SPFlowtext", &group_info, (GTypeFlags)0);
75 }
76 return group_type;
77 }
79 static void
80 sp_flowtext_class_init(SPFlowtextClass *klass)
81 {
82 GObjectClass *object_class = (GObjectClass *) klass;
83 SPObjectClass *sp_object_class = (SPObjectClass *) klass;
84 SPItemClass *item_class = (SPItemClass *) klass;
86 parent_class = (SPItemClass *)g_type_class_ref(SP_TYPE_ITEM);
88 object_class->dispose = sp_flowtext_dispose;
90 sp_object_class->child_added = sp_flowtext_child_added;
91 sp_object_class->remove_child = sp_flowtext_remove_child;
92 sp_object_class->update = sp_flowtext_update;
93 sp_object_class->modified = sp_flowtext_modified;
94 sp_object_class->write = sp_flowtext_write;
95 sp_object_class->build = sp_flowtext_build;
96 sp_object_class->set = sp_flowtext_set;
98 item_class->bbox = sp_flowtext_bbox;
99 item_class->print = sp_flowtext_print;
100 item_class->description = sp_flowtext_description;
101 item_class->show = sp_flowtext_show;
102 item_class->hide = sp_flowtext_hide;
103 }
105 static void
106 sp_flowtext_init(SPFlowtext *group)
107 {
108 group->par_indent = 0;
109 new (&group->layout) Inkscape::Text::Layout();
110 }
112 static void
113 sp_flowtext_dispose(GObject *object)
114 {
115 SPFlowtext *group = (SPFlowtext*)object;
117 group->layout.~Layout();
118 }
120 static void
121 sp_flowtext_child_added(SPObject *object, Inkscape::XML::Node *child, Inkscape::XML::Node *ref)
122 {
123 if (((SPObjectClass *) (parent_class))->child_added)
124 (* ((SPObjectClass *) (parent_class))->child_added)(object, child, ref);
126 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
127 }
129 /* fixme: hide (Lauris) */
131 static void
132 sp_flowtext_remove_child(SPObject *object, Inkscape::XML::Node *child)
133 {
134 if (((SPObjectClass *) (parent_class))->remove_child)
135 (* ((SPObjectClass *) (parent_class))->remove_child)(object, child);
137 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
138 }
140 static void
141 sp_flowtext_update(SPObject *object, SPCtx *ctx, unsigned flags)
142 {
143 SPFlowtext *group = SP_FLOWTEXT(object);
144 SPItemCtx *ictx = (SPItemCtx *) ctx;
145 SPItemCtx cctx = *ictx;
147 if (((SPObjectClass *) (parent_class))->update)
148 ((SPObjectClass *) (parent_class))->update(object, ctx, flags);
150 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
151 flags &= SP_OBJECT_MODIFIED_CASCADE;
153 GSList *l = NULL;
154 for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
155 g_object_ref(G_OBJECT(child));
156 l = g_slist_prepend(l, child);
157 }
158 l = g_slist_reverse(l);
159 while (l) {
160 SPObject *child = SP_OBJECT(l->data);
161 l = g_slist_remove(l, child);
162 if (flags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
163 if (SP_IS_ITEM(child)) {
164 SPItem const &chi = *SP_ITEM(child);
165 cctx.i2doc = chi.transform * ictx->i2doc;
166 cctx.i2vp = chi.transform * ictx->i2vp;
167 child->updateDisplay((SPCtx *)&cctx, flags);
168 } else {
169 child->updateDisplay(ctx, flags);
170 }
171 }
172 g_object_unref(G_OBJECT(child));
173 }
175 group->rebuildLayout();
177 NRRect paintbox;
178 sp_item_invoke_bbox(group, &paintbox, Geom::identity(), TRUE);
179 for (SPItemView *v = group->display; v != NULL; v = v->next) {
180 group->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
181 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
182 // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
183 group->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
184 }
185 }
187 static void
188 sp_flowtext_modified(SPObject *object, guint flags)
189 {
190 SPObject *ft = SP_FLOWTEXT (object);
191 SPObject *region = NULL;
193 if (flags & SP_OBJECT_MODIFIED_FLAG) flags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
194 flags &= SP_OBJECT_MODIFIED_CASCADE;
196 // FIXME: the below stanza is copied over from sp_text_modified, consider factoring it out
197 if (flags & ( SP_OBJECT_STYLE_MODIFIED_FLAG )) {
198 SPFlowtext *text = SP_FLOWTEXT(object);
199 NRRect paintbox;
200 sp_item_invoke_bbox(text, &paintbox, Geom::identity(), TRUE);
201 for (SPItemView* v = text->display; v != NULL; v = v->next) {
202 text->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
203 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
204 text->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
205 }
206 }
208 for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
209 if (SP_IS_FLOWREGION(o)) {
210 region = o;
211 break;
212 }
213 }
215 if (!region) return;
217 if (flags || (region->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
218 region->emitModified(flags); // pass down to the region only
219 }
220 }
222 static void
223 sp_flowtext_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr)
224 {
225 object->_requireSVGVersion(Inkscape::Version(1, 2));
227 if (((SPObjectClass *) (parent_class))->build) {
228 (* ((SPObjectClass *) (parent_class))->build)(object, document, repr);
229 }
231 sp_object_read_attr(object, "inkscape:layoutOptions"); // must happen after css has been read
232 }
234 static void
235 sp_flowtext_set(SPObject *object, unsigned key, gchar const *value)
236 {
237 SPFlowtext *group = (SPFlowtext *) object;
239 switch (key) {
240 case SP_ATTR_LAYOUT_OPTIONS: {
241 // deprecated attribute, read for backward compatibility only
242 SPCSSAttr *opts = sp_repr_css_attr((SP_OBJECT(group))->repr, "inkscape:layoutOptions");
243 {
244 gchar const *val = sp_repr_css_property(opts, "justification", NULL);
245 if (val != NULL && !object->style->text_align.set) {
246 if ( strcmp(val, "0") == 0 || strcmp(val, "false") == 0 ) {
247 object->style->text_align.value = SP_CSS_TEXT_ALIGN_LEFT;
248 } else {
249 object->style->text_align.value = SP_CSS_TEXT_ALIGN_JUSTIFY;
250 }
251 object->style->text_align.set = TRUE;
252 object->style->text_align.inherit = FALSE;
253 object->style->text_align.computed = object->style->text_align.value;
254 }
255 }
256 /* no equivalent css attribute for these two (yet)
257 {
258 gchar const *val = sp_repr_css_property(opts, "layoutAlgo", NULL);
259 if ( val == NULL ) {
260 group->algo = 0;
261 } else {
262 if ( strcmp(val, "better") == 0 ) { // knuth-plass, never worked for general cases
263 group->algo = 2;
264 } else if ( strcmp(val, "simple") == 0 ) { // greedy, but allowed lines to be compressed by up to 20% if it would make them fit
265 group->algo = 1;
266 } else if ( strcmp(val, "default") == 0 ) { // the same one we use, a standard greedy
267 group->algo = 0;
268 }
269 }
270 }
271 */
272 { // This would probably translate to padding-left, if SPStyle had it.
273 gchar const *val = sp_repr_css_property(opts, "par-indent", NULL);
274 if ( val == NULL ) {
275 group->par_indent = 0.0;
276 } else {
277 sp_repr_get_double((Inkscape::XML::Node*)opts, "par-indent", &group->par_indent);
278 }
279 }
280 sp_repr_css_attr_unref(opts);
281 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
282 break;
283 }
284 default:
285 if (((SPObjectClass *) (parent_class))->set) {
286 (* ((SPObjectClass *) (parent_class))->set)(object, key, value);
287 }
288 break;
289 }
290 }
292 static Inkscape::XML::Node *
293 sp_flowtext_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
294 {
295 if ( flags & SP_OBJECT_WRITE_BUILD ) {
296 if ( repr == NULL ) repr = xml_doc->createElement("svg:flowRoot");
297 GSList *l = NULL;
298 for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
299 Inkscape::XML::Node *c_repr = NULL;
300 if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child)) {
301 c_repr = child->updateRepr(xml_doc, NULL, flags);
302 }
303 if ( c_repr ) l = g_slist_prepend(l, c_repr);
304 }
305 while ( l ) {
306 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
307 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
308 l = g_slist_remove(l, l->data);
309 }
310 } else {
311 for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
312 if ( SP_IS_FLOWDIV(child) || SP_IS_FLOWPARA(child) || SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child) ) {
313 child->updateRepr(flags);
314 }
315 }
316 }
318 if (((SPObjectClass *) (parent_class))->write)
319 ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
321 return repr;
322 }
324 static void
325 sp_flowtext_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const /*flags*/)
326 {
327 SPFlowtext *group = SP_FLOWTEXT(item);
328 group->layout.getBoundingBox(bbox, transform);
330 // Add stroke width
331 SPStyle* style=SP_OBJECT_STYLE (item);
332 if ( !style->stroke.isNone() ) {
333 double const scale = transform.descrim();
334 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
335 double const width = MAX(0.125, style->stroke_width.computed * scale);
336 if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
337 bbox->x0-=0.5*width;
338 bbox->x1+=0.5*width;
339 bbox->y0-=0.5*width;
340 bbox->y1+=0.5*width;
341 }
342 }
343 }
344 }
346 static void
347 sp_flowtext_print(SPItem *item, SPPrintContext *ctx)
348 {
349 SPFlowtext *group = SP_FLOWTEXT(item);
351 NRRect pbox;
352 sp_item_invoke_bbox(item, &pbox, Geom::identity(), TRUE);
353 NRRect bbox;
354 Geom::OptRect bbox_maybe = sp_item_bbox_desktop(item);
355 if (!bbox_maybe) {
356 return;
357 }
358 bbox = NRRect(from_2geom(*bbox_maybe));
360 NRRect dbox;
361 dbox.x0 = 0.0;
362 dbox.y0 = 0.0;
363 dbox.x1 = sp_document_width(SP_OBJECT_DOCUMENT(item));
364 dbox.y1 = sp_document_height(SP_OBJECT_DOCUMENT(item));
365 Geom::Matrix const ctm (sp_item_i2d_affine(item));
367 group->layout.print(ctx, &pbox, &dbox, &bbox, ctm);
368 }
371 static gchar *sp_flowtext_description(SPItem *item)
372 {
373 Inkscape::Text::Layout const &layout = SP_FLOWTEXT(item)->layout;
374 int const nChars = layout.iteratorToCharIndex(layout.end());
376 char *trunc = "";
377 if (layout.inputTruncated()) {
378 trunc = _(" [truncated]");
379 }
381 if (SP_FLOWTEXT(item)->has_internal_frame())
382 return g_strdup_printf(ngettext("<b>Flowed text</b> (%d character%s)", "<b>Flowed text</b> (%d characters%s)", nChars), nChars, trunc);
383 else
384 return g_strdup_printf(ngettext("<b>Linked flowed text</b> (%d character%s)", "<b>Linked flowed text</b> (%d characters%s)", nChars), nChars, trunc);
385 }
387 static NRArenaItem *
388 sp_flowtext_show(SPItem *item, NRArena *arena, unsigned/* key*/, unsigned /*flags*/)
389 {
390 SPFlowtext *group = (SPFlowtext *) item;
391 NRArenaGroup *flowed = NRArenaGroup::create(arena);
392 nr_arena_group_set_transparent(flowed, FALSE);
394 nr_arena_group_set_style(flowed, group->style);
396 // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
397 NRRect paintbox;
398 sp_item_invoke_bbox(item, &paintbox, Geom::identity(), TRUE);
399 group->layout.show(flowed, &paintbox);
401 return flowed;
402 }
404 static void
405 sp_flowtext_hide(SPItem *item, unsigned int key)
406 {
407 if (((SPItemClass *) parent_class)->hide)
408 ((SPItemClass *) parent_class)->hide(item, key);
409 }
412 /*
413 *
414 */
416 void SPFlowtext::_buildLayoutInput(SPObject *root, Shape const *exclusion_shape, std::list<Shape> *shapes, SPObject **pending_line_break_object)
417 {
418 Inkscape::Text::Layout::OptionalTextTagAttrs pi;
419 bool with_indent = false;
421 if (SP_IS_FLOWPARA(root)) {
422 // emulate par-indent with the first char's kern
423 SPObject *t = root;
424 for ( ; t != NULL && !SP_IS_FLOWTEXT(t); t = SP_OBJECT_PARENT(t)){};
425 if (SP_IS_FLOWTEXT(t)) {
426 double indent = SP_FLOWTEXT(t)->par_indent;
427 if (indent != 0) {
428 with_indent = true;
429 SVGLength sl;
430 sl.value = sl.computed = indent;
431 sl._set = true;
432 pi.dx.push_back(sl);
433 }
434 }
435 }
437 if (*pending_line_break_object) {
438 if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object)) {
439 layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
440 } else {
441 layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
442 }
443 *pending_line_break_object = NULL;
444 }
446 for (SPObject *child = sp_object_first_child(root) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
447 if (SP_IS_STRING(child)) {
448 if (*pending_line_break_object) {
449 if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object))
450 layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
451 else {
452 layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
453 }
454 *pending_line_break_object = NULL;
455 }
456 if (with_indent)
457 layout.appendText(SP_STRING(child)->string, root->style, child, &pi);
458 else
459 layout.appendText(SP_STRING(child)->string, root->style, child);
460 } else if (SP_IS_FLOWREGION(child)) {
461 std::vector<Shape*> const &computed = SP_FLOWREGION(child)->computed;
462 for (std::vector<Shape*>::const_iterator it = computed.begin() ; it != computed.end() ; it++) {
463 shapes->push_back(Shape());
464 if (exclusion_shape->hasEdges())
465 shapes->back().Booleen(*it, const_cast<Shape*>(exclusion_shape), bool_op_diff);
466 else
467 shapes->back().Copy(*it);
468 layout.appendWrapShape(&shapes->back());
469 }
470 }
471 else if (!SP_IS_FLOWREGIONEXCLUDE(child) && !sp_repr_is_meta_element(child->repr))
472 _buildLayoutInput(child, exclusion_shape, shapes, pending_line_break_object);
473 }
475 if (SP_IS_FLOWDIV(root) || SP_IS_FLOWPARA(root) || SP_IS_FLOWREGIONBREAK(root) || SP_IS_FLOWLINE(root)) {
476 if (!root->hasChildren())
477 layout.appendText("", root->style, root);
478 *pending_line_break_object = root;
479 }
480 }
482 Shape* SPFlowtext::_buildExclusionShape() const
483 {
484 Shape *shape = new Shape;
485 Shape *shape_temp = new Shape;
487 for (SPObject *child = children ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
488 // RH: is it right that this shouldn't be recursive?
489 if ( SP_IS_FLOWREGIONEXCLUDE(child) ) {
490 SPFlowregionExclude *c_child = SP_FLOWREGIONEXCLUDE(child);
491 if (c_child->computed == NULL || !c_child->computed->hasEdges())
492 continue;
493 if (shape->hasEdges()) {
494 shape_temp->Booleen(shape, c_child->computed, bool_op_union);
495 std::swap(shape, shape_temp);
496 } else
497 shape->Copy(c_child->computed);
498 }
499 }
500 delete shape_temp;
501 return shape;
502 }
504 void SPFlowtext::rebuildLayout()
505 {
506 std::list<Shape> shapes;
508 layout.clear();
509 Shape *exclusion_shape = _buildExclusionShape();
510 SPObject *pending_line_break_object = NULL;
511 _buildLayoutInput(this, exclusion_shape, &shapes, &pending_line_break_object);
512 delete exclusion_shape;
513 layout.calculateFlow();
514 //g_print(layout.dumpAsText().c_str());
515 }
517 void SPFlowtext::_clearFlow(NRArenaGroup *in_arena)
518 {
519 nr_arena_item_request_render(NR_ARENA_ITEM(in_arena));
520 for (NRArenaItem *child = in_arena->children; child != NULL; ) {
521 NRArenaItem *nchild = child->next;
523 nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
524 nr_arena_item_remove_child(NR_ARENA_ITEM(in_arena), child);
526 child = nchild;
527 }
528 }
530 Inkscape::XML::Node *
531 SPFlowtext::getAsText()
532 {
533 if (!this->layout.outputExists()) return NULL;
535 SPItem *item = SP_ITEM(this);
537 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(this));
538 Inkscape::XML::Node *repr = xml_doc->createElement("svg:text");
539 repr->setAttribute("xml:space", "preserve");
540 repr->setAttribute("style", SP_OBJECT_REPR(this)->attribute("style"));
541 Geom::Point anchor_point = this->layout.characterAnchorPoint(this->layout.begin());
542 sp_repr_set_svg_double(repr, "x", anchor_point[Geom::X]);
543 sp_repr_set_svg_double(repr, "y", anchor_point[Geom::Y]);
545 for (Inkscape::Text::Layout::iterator it = this->layout.begin() ; it != this->layout.end() ; ) {
546 Inkscape::XML::Node *line_tspan = xml_doc->createElement("svg:tspan");
547 line_tspan->setAttribute("sodipodi:role", "line");
549 Inkscape::Text::Layout::iterator it_line_end = it;
550 it_line_end.nextStartOfLine();
552 while (it != it_line_end) {
554 Inkscape::XML::Node *span_tspan = xml_doc->createElement("svg:tspan");
555 Geom::Point anchor_point = this->layout.characterAnchorPoint(it);
556 // use kerning to simulate justification and whatnot
557 Inkscape::Text::Layout::iterator it_span_end = it;
558 it_span_end.nextStartOfSpan();
559 Inkscape::Text::Layout::OptionalTextTagAttrs attrs;
560 this->layout.simulateLayoutUsingKerning(it, it_span_end, &attrs);
561 // set x,y attributes only when we need to
562 bool set_x = false;
563 bool set_y = false;
564 if (!item->transform.isIdentity()) {
565 set_x = set_y = true;
566 } else {
567 Inkscape::Text::Layout::iterator it_chunk_start = it;
568 it_chunk_start.thisStartOfChunk();
569 if (it == it_chunk_start) {
570 set_x = true;
571 // don't set y so linespacing adjustments and things will still work
572 }
573 Inkscape::Text::Layout::iterator it_shape_start = it;
574 it_shape_start.thisStartOfShape();
575 if (it == it_shape_start)
576 set_y = true;
577 }
578 if (set_x && !attrs.dx.empty())
579 attrs.dx[0] = 0.0;
580 TextTagAttributes(attrs).writeTo(span_tspan);
581 if (set_x)
582 sp_repr_set_svg_double(span_tspan, "x", anchor_point[Geom::X]); // FIXME: this will pick up the wrong end of counter-directional runs
583 if (set_y)
584 sp_repr_set_svg_double(span_tspan, "y", anchor_point[Geom::Y]);
585 if (line_tspan->childCount() == 0) {
586 sp_repr_set_svg_double(line_tspan, "x", anchor_point[Geom::X]); // FIXME: this will pick up the wrong end of counter-directional runs
587 sp_repr_set_svg_double(line_tspan, "y", anchor_point[Geom::Y]);
588 }
590 SPObject *source_obj = 0;
591 void *rawptr = 0;
592 Glib::ustring::iterator span_text_start_iter;
593 this->layout.getSourceOfCharacter(it, &rawptr, &span_text_start_iter);
594 source_obj = SP_OBJECT (rawptr);
595 gchar *style_text = sp_style_write_difference((SP_IS_STRING(source_obj) ? source_obj->parent : source_obj)->style, this->style);
596 if (style_text && *style_text) {
597 span_tspan->setAttribute("style", style_text);
598 g_free(style_text);
599 }
601 if (SP_IS_STRING(source_obj)) {
602 Glib::ustring *string = &SP_STRING(source_obj)->string;
603 SPObject *span_end_obj = 0;
604 void *rawptr = 0;
605 Glib::ustring::iterator span_text_end_iter;
606 this->layout.getSourceOfCharacter(it_span_end, &rawptr, &span_text_end_iter);
607 span_end_obj = SP_OBJECT(rawptr);
608 if (span_end_obj != source_obj) {
609 if (it_span_end == this->layout.end()) {
610 span_text_end_iter = span_text_start_iter;
611 for (int i = this->layout.iteratorToCharIndex(it_span_end) - this->layout.iteratorToCharIndex(it) ; i ; --i)
612 ++span_text_end_iter;
613 } else
614 span_text_end_iter = string->end(); // spans will never straddle a source boundary
615 }
617 if (span_text_start_iter != span_text_end_iter) {
618 Glib::ustring new_string;
619 while (span_text_start_iter != span_text_end_iter)
620 new_string += *span_text_start_iter++; // grr. no substr() with iterators
621 Inkscape::XML::Node *new_text = xml_doc->createTextNode(new_string.c_str());
622 span_tspan->appendChild(new_text);
623 Inkscape::GC::release(new_text);
624 }
625 }
626 it = it_span_end;
628 line_tspan->appendChild(span_tspan);
629 Inkscape::GC::release(span_tspan);
630 }
631 repr->appendChild(line_tspan);
632 Inkscape::GC::release(line_tspan);
633 }
635 return repr;
636 }
638 SPItem *SPFlowtext::get_frame(SPItem *after)
639 {
640 SPObject *ft = SP_OBJECT (this);
641 SPObject *region = NULL;
643 for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
644 if (SP_IS_FLOWREGION(o)) {
645 region = o;
646 break;
647 }
648 }
650 if (!region) return NULL;
652 bool past = false;
653 SPItem *frame = NULL;
655 for (SPObject *o = sp_object_first_child(SP_OBJECT(region)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
656 if (SP_IS_ITEM(o)) {
657 if (after == NULL || past) {
658 frame = SP_ITEM(o);
659 } else {
660 if (SP_ITEM(o) == after) {
661 past = true;
662 }
663 }
664 }
665 }
667 if (!frame) return NULL;
669 if (SP_IS_USE (frame)) {
670 return sp_use_get_original(SP_USE(frame));
671 } else {
672 return frame;
673 }
674 }
676 bool SPFlowtext::has_internal_frame()
677 {
678 SPItem *frame = get_frame(NULL);
680 return (frame && SP_OBJECT(this)->isAncestorOf(SP_OBJECT(frame)) && SP_IS_RECT(frame));
681 }
684 SPItem *create_flowtext_with_internal_frame (SPDesktop *desktop, Geom::Point p0, Geom::Point p1)
685 {
686 SPDocument *doc = sp_desktop_document (desktop);
688 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
689 Inkscape::XML::Node *root_repr = xml_doc->createElement("svg:flowRoot");
690 root_repr->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create
691 SPItem *ft_item = SP_ITEM(desktop->currentLayer()->appendChildRepr(root_repr));
692 SPObject *root_object = doc->getObjectByRepr(root_repr);
693 g_assert(SP_IS_FLOWTEXT(root_object));
695 Inkscape::XML::Node *region_repr = xml_doc->createElement("svg:flowRegion");
696 root_repr->appendChild(region_repr);
697 SPObject *region_object = doc->getObjectByRepr(region_repr);
698 g_assert(SP_IS_FLOWREGION(region_object));
700 Inkscape::XML::Node *rect_repr = xml_doc->createElement("svg:rect"); // FIXME: use path!!! after rects are converted to use path
701 region_repr->appendChild(rect_repr);
703 SPObject *rect = doc->getObjectByRepr(rect_repr);
705 p0 *= desktop->dt2doc();
706 p1 *= desktop->dt2doc();
707 using Geom::X;
708 using Geom::Y;
709 Geom::Coord const x0 = MIN(p0[X], p1[X]);
710 Geom::Coord const y0 = MIN(p0[Y], p1[Y]);
711 Geom::Coord const x1 = MAX(p0[X], p1[X]);
712 Geom::Coord const y1 = MAX(p0[Y], p1[Y]);
713 Geom::Coord const w = x1 - x0;
714 Geom::Coord const h = y1 - y0;
716 sp_rect_position_set(SP_RECT(rect), x0, y0, w, h);
717 SP_OBJECT(rect)->updateRepr();
719 Inkscape::XML::Node *para_repr = xml_doc->createElement("svg:flowPara");
720 root_repr->appendChild(para_repr);
721 SPObject *para_object = doc->getObjectByRepr(para_repr);
722 g_assert(SP_IS_FLOWPARA(para_object));
724 Inkscape::XML::Node *text = xml_doc->createTextNode("");
725 para_repr->appendChild(text);
727 Inkscape::GC::release(root_repr);
728 Inkscape::GC::release(region_repr);
729 Inkscape::GC::release(para_repr);
730 Inkscape::GC::release(rect_repr);
732 ft_item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
734 return ft_item;
735 }
738 /*
739 Local Variables:
740 mode:c++
741 c-file-style:"stroustrup"
742 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
743 indent-tabs-mode:nil
744 fill-column:99
745 End:
746 */
747 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :