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