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