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, bool const target, SnapPointsWithType &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, bool const target, SnapPointsWithType &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 int type = target ? int(Inkscape::SNAPTARGET_TEXT_BASELINE) : int(Inkscape::SNAPSOURCE_TEXT_BASELINE);
396 p.push_back(std::make_pair((*pt) * sp_item_i2d_affine(item), type));
397 }
398 }
399 }
401 static NRArenaItem *
402 sp_flowtext_show(SPItem *item, NRArena *arena, unsigned/* key*/, unsigned /*flags*/)
403 {
404 SPFlowtext *group = (SPFlowtext *) item;
405 NRArenaGroup *flowed = NRArenaGroup::create(arena);
406 nr_arena_group_set_transparent(flowed, FALSE);
408 nr_arena_group_set_style(flowed, group->style);
410 // pass the bbox of the flowtext object as paintbox (used for paintserver fills)
411 NRRect paintbox;
412 sp_item_invoke_bbox(item, &paintbox, Geom::identity(), TRUE);
413 group->layout.show(flowed, &paintbox);
415 return flowed;
416 }
418 static void
419 sp_flowtext_hide(SPItem *item, unsigned int key)
420 {
421 if (((SPItemClass *) parent_class)->hide)
422 ((SPItemClass *) parent_class)->hide(item, key);
423 }
426 /*
427 *
428 */
430 void SPFlowtext::_buildLayoutInput(SPObject *root, Shape const *exclusion_shape, std::list<Shape> *shapes, SPObject **pending_line_break_object)
431 {
432 Inkscape::Text::Layout::OptionalTextTagAttrs pi;
433 bool with_indent = false;
435 if (SP_IS_FLOWPARA(root)) {
436 // emulate par-indent with the first char's kern
437 SPObject *t = root;
438 for ( ; t != NULL && !SP_IS_FLOWTEXT(t); t = SP_OBJECT_PARENT(t)){};
439 if (SP_IS_FLOWTEXT(t)) {
440 double indent = SP_FLOWTEXT(t)->par_indent;
441 if (indent != 0) {
442 with_indent = true;
443 SVGLength sl;
444 sl.value = sl.computed = indent;
445 sl._set = true;
446 pi.dx.push_back(sl);
447 }
448 }
449 }
451 if (*pending_line_break_object) {
452 if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object)) {
453 layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
454 } else {
455 layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
456 }
457 *pending_line_break_object = NULL;
458 }
460 for (SPObject *child = sp_object_first_child(root) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
461 if (SP_IS_STRING(child)) {
462 if (*pending_line_break_object) {
463 if (SP_IS_FLOWREGIONBREAK(*pending_line_break_object))
464 layout.appendControlCode(Inkscape::Text::Layout::SHAPE_BREAK, *pending_line_break_object);
465 else {
466 layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, *pending_line_break_object);
467 }
468 *pending_line_break_object = NULL;
469 }
470 if (with_indent)
471 layout.appendText(SP_STRING(child)->string, root->style, child, &pi);
472 else
473 layout.appendText(SP_STRING(child)->string, root->style, child);
474 } else if (SP_IS_FLOWREGION(child)) {
475 std::vector<Shape*> const &computed = SP_FLOWREGION(child)->computed;
476 for (std::vector<Shape*>::const_iterator it = computed.begin() ; it != computed.end() ; it++) {
477 shapes->push_back(Shape());
478 if (exclusion_shape->hasEdges())
479 shapes->back().Booleen(*it, const_cast<Shape*>(exclusion_shape), bool_op_diff);
480 else
481 shapes->back().Copy(*it);
482 layout.appendWrapShape(&shapes->back());
483 }
484 }
485 else if (!SP_IS_FLOWREGIONEXCLUDE(child) && !sp_repr_is_meta_element(child->repr))
486 _buildLayoutInput(child, exclusion_shape, shapes, pending_line_break_object);
487 }
489 if (SP_IS_FLOWDIV(root) || SP_IS_FLOWPARA(root) || SP_IS_FLOWREGIONBREAK(root) || SP_IS_FLOWLINE(root)) {
490 if (!root->hasChildren())
491 layout.appendText("", root->style, root);
492 *pending_line_break_object = root;
493 }
494 }
496 Shape* SPFlowtext::_buildExclusionShape() const
497 {
498 Shape *shape = new Shape;
499 Shape *shape_temp = new Shape;
501 for (SPObject *child = children ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
502 // RH: is it right that this shouldn't be recursive?
503 if ( SP_IS_FLOWREGIONEXCLUDE(child) ) {
504 SPFlowregionExclude *c_child = SP_FLOWREGIONEXCLUDE(child);
505 if (c_child->computed == NULL || !c_child->computed->hasEdges())
506 continue;
507 if (shape->hasEdges()) {
508 shape_temp->Booleen(shape, c_child->computed, bool_op_union);
509 std::swap(shape, shape_temp);
510 } else
511 shape->Copy(c_child->computed);
512 }
513 }
514 delete shape_temp;
515 return shape;
516 }
518 void SPFlowtext::rebuildLayout()
519 {
520 std::list<Shape> shapes;
522 layout.clear();
523 Shape *exclusion_shape = _buildExclusionShape();
524 SPObject *pending_line_break_object = NULL;
525 _buildLayoutInput(this, exclusion_shape, &shapes, &pending_line_break_object);
526 delete exclusion_shape;
527 layout.calculateFlow();
528 //g_print(layout.dumpAsText().c_str());
529 }
531 void SPFlowtext::_clearFlow(NRArenaGroup *in_arena)
532 {
533 nr_arena_item_request_render(NR_ARENA_ITEM(in_arena));
534 for (NRArenaItem *child = in_arena->children; child != NULL; ) {
535 NRArenaItem *nchild = child->next;
537 nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
538 nr_arena_item_remove_child(NR_ARENA_ITEM(in_arena), child);
540 child = nchild;
541 }
542 }
544 Inkscape::XML::Node *
545 SPFlowtext::getAsText()
546 {
547 if (!this->layout.outputExists()) return NULL;
549 SPItem *item = SP_ITEM(this);
551 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(this));
552 Inkscape::XML::Node *repr = xml_doc->createElement("svg:text");
553 repr->setAttribute("xml:space", "preserve");
554 repr->setAttribute("style", SP_OBJECT_REPR(this)->attribute("style"));
555 Geom::Point anchor_point = this->layout.characterAnchorPoint(this->layout.begin());
556 sp_repr_set_svg_double(repr, "x", anchor_point[Geom::X]);
557 sp_repr_set_svg_double(repr, "y", anchor_point[Geom::Y]);
559 for (Inkscape::Text::Layout::iterator it = this->layout.begin() ; it != this->layout.end() ; ) {
560 Inkscape::XML::Node *line_tspan = xml_doc->createElement("svg:tspan");
561 line_tspan->setAttribute("sodipodi:role", "line");
563 Inkscape::Text::Layout::iterator it_line_end = it;
564 it_line_end.nextStartOfLine();
566 while (it != it_line_end) {
568 Inkscape::XML::Node *span_tspan = xml_doc->createElement("svg:tspan");
569 Geom::Point anchor_point = this->layout.characterAnchorPoint(it);
570 // use kerning to simulate justification and whatnot
571 Inkscape::Text::Layout::iterator it_span_end = it;
572 it_span_end.nextStartOfSpan();
573 Inkscape::Text::Layout::OptionalTextTagAttrs attrs;
574 this->layout.simulateLayoutUsingKerning(it, it_span_end, &attrs);
575 // set x,y attributes only when we need to
576 bool set_x = false;
577 bool set_y = false;
578 if (!item->transform.isIdentity()) {
579 set_x = set_y = true;
580 } else {
581 Inkscape::Text::Layout::iterator it_chunk_start = it;
582 it_chunk_start.thisStartOfChunk();
583 if (it == it_chunk_start) {
584 set_x = true;
585 // don't set y so linespacing adjustments and things will still work
586 }
587 Inkscape::Text::Layout::iterator it_shape_start = it;
588 it_shape_start.thisStartOfShape();
589 if (it == it_shape_start)
590 set_y = true;
591 }
592 if (set_x && !attrs.dx.empty())
593 attrs.dx[0] = 0.0;
594 TextTagAttributes(attrs).writeTo(span_tspan);
595 if (set_x)
596 sp_repr_set_svg_double(span_tspan, "x", anchor_point[Geom::X]); // FIXME: this will pick up the wrong end of counter-directional runs
597 if (set_y)
598 sp_repr_set_svg_double(span_tspan, "y", anchor_point[Geom::Y]);
599 if (line_tspan->childCount() == 0) {
600 sp_repr_set_svg_double(line_tspan, "x", anchor_point[Geom::X]); // FIXME: this will pick up the wrong end of counter-directional runs
601 sp_repr_set_svg_double(line_tspan, "y", anchor_point[Geom::Y]);
602 }
604 SPObject *source_obj = 0;
605 void *rawptr = 0;
606 Glib::ustring::iterator span_text_start_iter;
607 this->layout.getSourceOfCharacter(it, &rawptr, &span_text_start_iter);
608 source_obj = SP_OBJECT (rawptr);
609 gchar *style_text = sp_style_write_difference((SP_IS_STRING(source_obj) ? source_obj->parent : source_obj)->style, this->style);
610 if (style_text && *style_text) {
611 span_tspan->setAttribute("style", style_text);
612 g_free(style_text);
613 }
615 if (SP_IS_STRING(source_obj)) {
616 Glib::ustring *string = &SP_STRING(source_obj)->string;
617 SPObject *span_end_obj = 0;
618 void *rawptr = 0;
619 Glib::ustring::iterator span_text_end_iter;
620 this->layout.getSourceOfCharacter(it_span_end, &rawptr, &span_text_end_iter);
621 span_end_obj = SP_OBJECT(rawptr);
622 if (span_end_obj != source_obj) {
623 if (it_span_end == this->layout.end()) {
624 span_text_end_iter = span_text_start_iter;
625 for (int i = this->layout.iteratorToCharIndex(it_span_end) - this->layout.iteratorToCharIndex(it) ; i ; --i)
626 ++span_text_end_iter;
627 } else
628 span_text_end_iter = string->end(); // spans will never straddle a source boundary
629 }
631 if (span_text_start_iter != span_text_end_iter) {
632 Glib::ustring new_string;
633 while (span_text_start_iter != span_text_end_iter)
634 new_string += *span_text_start_iter++; // grr. no substr() with iterators
635 Inkscape::XML::Node *new_text = xml_doc->createTextNode(new_string.c_str());
636 span_tspan->appendChild(new_text);
637 Inkscape::GC::release(new_text);
638 }
639 }
640 it = it_span_end;
642 line_tspan->appendChild(span_tspan);
643 Inkscape::GC::release(span_tspan);
644 }
645 repr->appendChild(line_tspan);
646 Inkscape::GC::release(line_tspan);
647 }
649 return repr;
650 }
652 SPItem *SPFlowtext::get_frame(SPItem *after)
653 {
654 SPObject *ft = SP_OBJECT (this);
655 SPObject *region = NULL;
657 for (SPObject *o = sp_object_first_child(SP_OBJECT(ft)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
658 if (SP_IS_FLOWREGION(o)) {
659 region = o;
660 break;
661 }
662 }
664 if (!region) return NULL;
666 bool past = false;
667 SPItem *frame = NULL;
669 for (SPObject *o = sp_object_first_child(SP_OBJECT(region)) ; o != NULL ; o = SP_OBJECT_NEXT(o) ) {
670 if (SP_IS_ITEM(o)) {
671 if (after == NULL || past) {
672 frame = SP_ITEM(o);
673 } else {
674 if (SP_ITEM(o) == after) {
675 past = true;
676 }
677 }
678 }
679 }
681 if (!frame) return NULL;
683 if (SP_IS_USE (frame)) {
684 return sp_use_get_original(SP_USE(frame));
685 } else {
686 return frame;
687 }
688 }
690 bool SPFlowtext::has_internal_frame()
691 {
692 SPItem *frame = get_frame(NULL);
694 return (frame && SP_OBJECT(this)->isAncestorOf(SP_OBJECT(frame)) && SP_IS_RECT(frame));
695 }
698 SPItem *create_flowtext_with_internal_frame (SPDesktop *desktop, Geom::Point p0, Geom::Point p1)
699 {
700 SPDocument *doc = sp_desktop_document (desktop);
702 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
703 Inkscape::XML::Node *root_repr = xml_doc->createElement("svg:flowRoot");
704 root_repr->setAttribute("xml:space", "preserve"); // we preserve spaces in the text objects we create
705 SPItem *ft_item = SP_ITEM(desktop->currentLayer()->appendChildRepr(root_repr));
706 SPObject *root_object = doc->getObjectByRepr(root_repr);
707 g_assert(SP_IS_FLOWTEXT(root_object));
709 Inkscape::XML::Node *region_repr = xml_doc->createElement("svg:flowRegion");
710 root_repr->appendChild(region_repr);
711 SPObject *region_object = doc->getObjectByRepr(region_repr);
712 g_assert(SP_IS_FLOWREGION(region_object));
714 Inkscape::XML::Node *rect_repr = xml_doc->createElement("svg:rect"); // FIXME: use path!!! after rects are converted to use path
715 region_repr->appendChild(rect_repr);
717 SPObject *rect = doc->getObjectByRepr(rect_repr);
719 p0 *= desktop->dt2doc();
720 p1 *= desktop->dt2doc();
721 using Geom::X;
722 using Geom::Y;
723 Geom::Coord const x0 = MIN(p0[X], p1[X]);
724 Geom::Coord const y0 = MIN(p0[Y], p1[Y]);
725 Geom::Coord const x1 = MAX(p0[X], p1[X]);
726 Geom::Coord const y1 = MAX(p0[Y], p1[Y]);
727 Geom::Coord const w = x1 - x0;
728 Geom::Coord const h = y1 - y0;
730 sp_rect_position_set(SP_RECT(rect), x0, y0, w, h);
731 SP_OBJECT(rect)->updateRepr();
733 Inkscape::XML::Node *para_repr = xml_doc->createElement("svg:flowPara");
734 root_repr->appendChild(para_repr);
735 SPObject *para_object = doc->getObjectByRepr(para_repr);
736 g_assert(SP_IS_FLOWPARA(para_object));
738 Inkscape::XML::Node *text = xml_doc->createTextNode("");
739 para_repr->appendChild(text);
741 Inkscape::GC::release(root_repr);
742 Inkscape::GC::release(region_repr);
743 Inkscape::GC::release(para_repr);
744 Inkscape::GC::release(rect_repr);
746 ft_item->transform = sp_item_i2doc_affine(SP_ITEM(desktop->currentLayer())).inverse();
748 return ft_item;
749 }
752 /*
753 Local Variables:
754 mode:c++
755 c-file-style:"stroustrup"
756 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
757 indent-tabs-mode:nil
758 fill-column:99
759 End:
760 */
761 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :