1 /*
2 * SVG <text> and <tspan> implementation
3 *
4 * Author:
5 * Lauris Kaplinski <lauris@kaplinski.com>
6 * bulia byak <buliabyak@users.sf.net>
7 *
8 * Copyright (C) 1999-2002 Lauris Kaplinski
9 * Copyright (C) 2000-2001 Ximian, Inc.
10 *
11 * Released under GNU GPL, read the file 'COPYING' for more information
12 */
14 /*
15 * fixme:
16 *
17 * These subcomponents should not be items, or alternately
18 * we have to invent set of flags to mark, whether standard
19 * attributes are applicable to given item (I even like this
20 * idea somewhat - Lauris)
21 *
22 */
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <libnr/nr-matrix-fns.h>
29 #include <libnrtype/FontFactory.h>
30 #include <libnrtype/font-instance.h>
31 #include <libnrtype/font-style-to-pos.h>
33 #include <glibmm/i18n.h>
34 #include "svg/svg.h"
35 #include "svg/stringstream.h"
36 #include "display/nr-arena-glyphs.h"
37 #include "attributes.h"
38 #include "document.h"
39 #include "desktop-handles.h"
40 #include "sp-namedview.h"
41 #include "style.h"
42 #include "inkscape.h"
43 #include "sp-metrics.h"
44 #include "xml/quote.h"
45 #include "xml/repr.h"
46 #include "mod360.h"
48 #include "sp-textpath.h"
49 #include "sp-tspan.h"
51 #include "text-editing.h"
53 /*#####################################################
54 # SPTEXT
55 #####################################################*/
57 static void sp_text_class_init (SPTextClass *classname);
58 static void sp_text_init (SPText *text);
59 static void sp_text_release (SPObject *object);
61 static void sp_text_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
62 static void sp_text_set (SPObject *object, unsigned key, gchar const *value);
63 static void sp_text_child_added (SPObject *object, Inkscape::XML::Node *rch, Inkscape::XML::Node *ref);
64 static void sp_text_remove_child (SPObject *object, Inkscape::XML::Node *rch);
65 static void sp_text_update (SPObject *object, SPCtx *ctx, guint flags);
66 static void sp_text_modified (SPObject *object, guint flags);
67 static Inkscape::XML::Node *sp_text_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
69 static void sp_text_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
70 static NRArenaItem *sp_text_show (SPItem *item, NRArena *arena, unsigned key, unsigned flags);
71 static void sp_text_hide (SPItem *item, unsigned key);
72 static char *sp_text_description (SPItem *item);
73 static void sp_text_snappoints(SPItem const *item, SnapPointsIter p);
74 static NR::Matrix sp_text_set_transform(SPItem *item, NR::Matrix const &xform);
75 static void sp_text_print (SPItem *item, SPPrintContext *gpc);
77 static SPItemClass *text_parent_class;
79 GType
80 sp_text_get_type ()
81 {
82 static GType type = 0;
83 if (!type) {
84 GTypeInfo info = {
85 sizeof (SPTextClass),
86 NULL, /* base_init */
87 NULL, /* base_finalize */
88 (GClassInitFunc) sp_text_class_init,
89 NULL, /* class_finalize */
90 NULL, /* class_data */
91 sizeof (SPText),
92 16, /* n_preallocs */
93 (GInstanceInitFunc) sp_text_init,
94 NULL, /* value_table */
95 };
96 type = g_type_register_static (SP_TYPE_ITEM, "SPText", &info, (GTypeFlags)0);
97 }
98 return type;
99 }
101 static void
102 sp_text_class_init (SPTextClass *classname)
103 {
104 SPObjectClass *sp_object_class = (SPObjectClass *) classname;
105 SPItemClass *item_class = (SPItemClass *) classname;
107 text_parent_class = (SPItemClass*)g_type_class_ref (SP_TYPE_ITEM);
109 sp_object_class->release = sp_text_release;
110 sp_object_class->build = sp_text_build;
111 sp_object_class->set = sp_text_set;
112 sp_object_class->child_added = sp_text_child_added;
113 sp_object_class->remove_child = sp_text_remove_child;
114 sp_object_class->update = sp_text_update;
115 sp_object_class->modified = sp_text_modified;
116 sp_object_class->write = sp_text_write;
118 item_class->bbox = sp_text_bbox;
119 item_class->show = sp_text_show;
120 item_class->hide = sp_text_hide;
121 item_class->description = sp_text_description;
122 item_class->snappoints = sp_text_snappoints;
123 item_class->set_transform = sp_text_set_transform;
124 item_class->print = sp_text_print;
125 }
127 static void
128 sp_text_init (SPText *text)
129 {
130 new (&text->layout) Inkscape::Text::Layout;
131 new (&text->attributes) TextTagAttributes;
132 }
134 static void
135 sp_text_release (SPObject *object)
136 {
137 SPText *text = SP_TEXT(object);
138 text->attributes.~TextTagAttributes();
139 text->layout.~Layout();
141 if (((SPObjectClass *) text_parent_class)->release)
142 ((SPObjectClass *) text_parent_class)->release(object);
143 }
145 static void
146 sp_text_build (SPObject *object, SPDocument *doc, Inkscape::XML::Node *repr)
147 {
148 sp_object_read_attr(object, "x");
149 sp_object_read_attr(object, "y");
150 sp_object_read_attr(object, "dx");
151 sp_object_read_attr(object, "dy");
152 sp_object_read_attr(object, "rotate");
154 if (((SPObjectClass *) text_parent_class)->build)
155 ((SPObjectClass *) text_parent_class)->build(object, doc, repr);
157 sp_object_read_attr(object, "sodipodi:linespacing"); // has to happen after the styles are read
158 }
160 static void
161 sp_text_set(SPObject *object, unsigned key, gchar const *value)
162 {
163 SPText *text = SP_TEXT (object);
165 if (text->attributes.readSingleAttribute(key, value)) {
166 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
167 } else {
168 switch (key) {
169 case SP_ATTR_SODIPODI_LINESPACING:
170 // convert deprecated tag to css
171 if (value) {
172 text->style->line_height.set = TRUE;
173 text->style->line_height.inherit = FALSE;
174 text->style->line_height.normal = FALSE;
175 text->style->line_height.unit = SP_CSS_UNIT_PERCENT;
176 text->style->line_height.value = text->style->line_height.computed = sp_svg_read_percentage (value, 1.0);
177 }
178 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_TEXT_LAYOUT_MODIFIED_FLAG);
179 break;
180 default:
181 if (((SPObjectClass *) text_parent_class)->set)
182 ((SPObjectClass *) text_parent_class)->set (object, key, value);
183 break;
184 }
185 }
186 }
188 static void
189 sp_text_child_added (SPObject *object, Inkscape::XML::Node *rch, Inkscape::XML::Node *ref)
190 {
191 SPText *text = SP_TEXT (object);
193 if (((SPObjectClass *) text_parent_class)->child_added)
194 ((SPObjectClass *) text_parent_class)->child_added (object, rch, ref);
196 text->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_TEXT_CONTENT_MODIFIED_FLAG);
197 }
199 static void
200 sp_text_remove_child (SPObject *object, Inkscape::XML::Node *rch)
201 {
202 SPText *text = SP_TEXT (object);
204 if (((SPObjectClass *) text_parent_class)->remove_child)
205 ((SPObjectClass *) text_parent_class)->remove_child (object, rch);
207 text->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_TEXT_CONTENT_MODIFIED_FLAG | SP_TEXT_LAYOUT_MODIFIED_FLAG);
208 }
210 static void
211 sp_text_update (SPObject *object, SPCtx *ctx, guint flags)
212 {
213 SPText *text = SP_TEXT (object);
215 if (((SPObjectClass *) text_parent_class)->update)
216 ((SPObjectClass *) text_parent_class)->update (object, ctx, flags);
218 guint cflags = (flags & SP_OBJECT_MODIFIED_CASCADE);
219 if (flags & SP_OBJECT_MODIFIED_FLAG) cflags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
222 /* Create temporary list of children */
223 GSList *l = NULL;
224 for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
225 sp_object_ref (SP_OBJECT (child), object);
226 l = g_slist_prepend (l, child);
227 }
228 l = g_slist_reverse (l);
229 while (l) {
230 SPObject *child = SP_OBJECT (l->data);
231 l = g_slist_remove (l, child);
232 if (cflags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
233 /* fixme: Do we need transform? */
234 child->updateDisplay(ctx, cflags);
235 }
236 sp_object_unref (SP_OBJECT (child), object);
237 }
238 if (flags & ( SP_OBJECT_STYLE_MODIFIED_FLAG |
239 SP_OBJECT_CHILD_MODIFIED_FLAG |
240 SP_TEXT_LAYOUT_MODIFIED_FLAG ) )
241 {
242 /* fixme: It is not nice to have it here, but otherwise children content changes does not work */
243 /* fixme: Even now it may not work, as we are delayed */
244 /* fixme: So check modification flag everywhere immediate state is used */
245 text->rebuildLayout();
247 NRRect paintbox;
248 sp_item_invoke_bbox(text, &paintbox, NR::identity(), TRUE);
249 for (SPItemView* v = text->display; v != NULL; v = v->next) {
250 text->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
251 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
252 // pass the bbox of the text object as paintbox (used for paintserver fills)
253 text->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
254 }
255 }
256 }
258 static void
259 sp_text_modified (SPObject *object, guint flags)
260 {
261 if (((SPObjectClass *) text_parent_class)->modified)
262 ((SPObjectClass *) text_parent_class)->modified (object, flags);
264 guint cflags = (flags & SP_OBJECT_MODIFIED_CASCADE);
265 if (flags & SP_OBJECT_MODIFIED_FLAG) cflags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
267 // FIXME: all that we need to do here is nr_arena_glyphs_[group_]set_style, to set the changed
268 // style, but there's no easy way to access the arena glyphs or glyph groups corresponding to a
269 // text object. Therefore we do here the same as in _update, that is, destroy all arena items
270 // and create new ones. This is probably quite wasteful.
271 if (flags & ( SP_OBJECT_STYLE_MODIFIED_FLAG )) {
272 SPText *text = SP_TEXT (object);
273 NRRect paintbox;
274 sp_item_invoke_bbox(text, &paintbox, NR::identity(), TRUE);
275 for (SPItemView* v = text->display; v != NULL; v = v->next) {
276 text->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
277 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
278 text->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
279 }
280 }
282 /* Create temporary list of children */
283 GSList *l = NULL;
284 SPObject *child;
285 for (child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
286 sp_object_ref (SP_OBJECT (child), object);
287 l = g_slist_prepend (l, child);
288 }
289 l = g_slist_reverse (l);
290 while (l) {
291 child = SP_OBJECT (l->data);
292 l = g_slist_remove (l, child);
293 if (cflags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
294 child->emitModified(cflags);
295 }
296 sp_object_unref (SP_OBJECT (child), object);
297 }
298 }
300 static Inkscape::XML::Node *
301 sp_text_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
302 {
303 SPText *text = SP_TEXT (object);
305 if (flags & SP_OBJECT_WRITE_BUILD) {
306 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
307 if (!repr)
308 repr = xml_doc->createElement("svg:text");
309 GSList *l = NULL;
310 for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
311 Inkscape::XML::Node *crepr = NULL;
312 if (SP_IS_STRING(child)) {
313 crepr = xml_doc->createTextNode(SP_STRING(child)->string.c_str());
314 } else {
315 crepr = child->updateRepr(NULL, flags);
316 }
317 if (crepr) l = g_slist_prepend (l, crepr);
318 }
319 while (l) {
320 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
321 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
322 l = g_slist_remove (l, l->data);
323 }
324 } else {
325 for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
326 if (SP_IS_STRING(child)) {
327 SP_OBJECT_REPR(child)->setContent(SP_STRING(child)->string.c_str());
328 } else {
329 child->updateRepr(flags);
330 }
331 }
332 }
334 text->attributes.writeTo(repr);
336 // deprecated attribute, but keep it around for backwards compatibility
337 if (text->style->line_height.set && !text->style->line_height.inherit && !text->style->line_height.normal && text->style->line_height.unit == SP_CSS_UNIT_PERCENT) {
338 Inkscape::SVGOStringStream os;
339 os << (text->style->line_height.value * 100.0) << "%";
340 SP_OBJECT_REPR(text)->setAttribute("sodipodi:linespacing", os.str().c_str());
341 }
342 else
343 SP_OBJECT_REPR(text)->setAttribute("sodipodi:linespacing", NULL);
345 if (((SPObjectClass *) (text_parent_class))->write)
346 ((SPObjectClass *) (text_parent_class))->write (object, repr, flags);
348 return repr;
349 }
351 static void
352 sp_text_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
353 {
354 SP_TEXT(item)->layout.getBoundingBox(bbox, transform);
356 // Add stroke width
357 SPStyle* style=SP_OBJECT_STYLE (item);
358 if (style->stroke.type != SP_PAINT_TYPE_NONE) {
359 double const scale = expansion(transform);
360 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
361 double const width = MAX(0.125, style->stroke_width.computed * scale);
362 if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
363 bbox->x0-=0.5*width;
364 bbox->x1+=0.5*width;
365 bbox->y0-=0.5*width;
366 bbox->y1+=0.5*width;
367 }
368 }
369 }
370 }
373 static NRArenaItem *
374 sp_text_show(SPItem *item, NRArena *arena, unsigned /* key*/, unsigned /*flags*/)
375 {
376 SPText *group = (SPText *) item;
378 NRArenaGroup *flowed = NRArenaGroup::create(arena);
379 nr_arena_group_set_transparent (flowed, FALSE);
381 nr_arena_group_set_style(flowed, group->style);
383 // pass the bbox of the text object as paintbox (used for paintserver fills)
384 NRRect paintbox;
385 sp_item_invoke_bbox(item, &paintbox, NR::identity(), TRUE);
386 group->layout.show(flowed, &paintbox);
388 return flowed;
389 }
391 static void
392 sp_text_hide(SPItem *item, unsigned key)
393 {
394 if (((SPItemClass *) text_parent_class)->hide)
395 ((SPItemClass *) text_parent_class)->hide (item, key);
396 }
398 static char *
399 sp_text_description(SPItem *item)
400 {
401 SPText *text = (SPText *) item;
402 SPStyle *style = SP_OBJECT_STYLE(text);
404 font_instance *tf = (font_factory::Default())->Face(style->text->font_family.value,
405 font_style_to_pos(*style));
406 char name_buf[256];
407 char *n;
408 if (tf) {
409 tf->Name(name_buf, sizeof(name_buf));
410 n = xml_quote_strdup(name_buf);
411 tf->Unref();
412 } else {
413 /* TRANSLATORS: For description of font with no name. */
414 n = g_strdup(_("<no name found>"));
415 }
417 GString *xs = SP_PX_TO_METRIC_STRING(style->font_size.computed, sp_desktop_namedview(SP_ACTIVE_DESKTOP)->getDefaultMetric());
419 char *ret = ( SP_IS_TEXT_TEXTPATH(item)
420 ? g_strdup_printf(_("<b>Text on path</b> (%s, %s)"), n, xs->str)
421 : g_strdup_printf(_("<b>Text</b> (%s, %s)"), n, xs->str) );
422 g_free(n);
423 return ret;
424 }
426 static void sp_text_snappoints(SPItem const *item, SnapPointsIter p)
427 {
428 // the baseline anchor of the first char
429 Inkscape::Text::Layout const *layout = te_get_layout((SPItem *) item);
430 if(layout != NULL) {
431 *p = layout->characterAnchorPoint(layout->begin()) * sp_item_i2d_affine(item);
432 }
433 }
435 static NR::Matrix
436 sp_text_set_transform (SPItem *item, NR::Matrix const &xform)
437 {
438 SPText *text = SP_TEXT(item);
440 // we cannot optimize textpath because changing its fontsize will break its match to the path
441 if (SP_IS_TEXT_TEXTPATH (text))
442 return xform;
444 /* This function takes care of scaling & translation only, we return whatever parts we can't
445 handle. */
447 // TODO: pjrm tried to use fontsize_expansion(xform) here and it works for text in that font size
448 // is scaled more intuitively when scaling non-uniformly; however this necessitated using
449 // fontsize_expansion instead of expansion in other places too, where it was not appropriate
450 // (e.g. it broke stroke width on copy/pasting of style from horizontally stretched to vertically
451 // stretched shape). Using fontsize_expansion only here broke setting the style via font
452 // dialog. This needs to be investigated further.
453 double const ex = NR::expansion(xform);
454 if (ex == 0) {
455 return xform;
456 }
458 NR::Matrix ret(NR::transform(xform));
459 ret[0] /= ex;
460 ret[1] /= ex;
461 ret[2] /= ex;
462 ret[3] /= ex;
464 // Adjust x/y, dx/dy
465 text->_adjustCoordsRecursive (item, xform * ret.inverse(), ex);
467 // Adjust font size
468 text->_adjustFontsizeRecursive (item, ex);
470 // Adjust stroke width
471 sp_item_adjust_stroke_width_recursive (item, ex);
473 // Adjust pattern fill
474 sp_item_adjust_pattern(item, xform * ret.inverse());
476 // Adjust gradient fill
477 sp_item_adjust_gradient(item, xform * ret.inverse());
479 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_TEXT_LAYOUT_MODIFIED_FLAG);
481 return ret;
482 }
484 static void
485 sp_text_print (SPItem *item, SPPrintContext *ctx)
486 {
487 NRRect pbox, dbox, bbox;
488 SPText *group = SP_TEXT (item);
490 sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
491 sp_item_bbox_desktop (item, &bbox);
492 dbox.x0 = 0.0;
493 dbox.y0 = 0.0;
494 dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
495 dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
496 NR::Matrix const ctm = sp_item_i2d_affine(item);
498 group->layout.print(ctx,&pbox,&dbox,&bbox,ctm);
499 }
501 /*
502 * Member functions
503 */
505 unsigned SPText::_buildLayoutInput(SPObject *root, Inkscape::Text::Layout::OptionalTextTagAttrs const &parent_optional_attrs, unsigned parent_attrs_offset, bool in_textpath)
506 {
507 unsigned length = 0;
508 int child_attrs_offset = 0;
509 Inkscape::Text::Layout::OptionalTextTagAttrs optional_attrs;
511 if (SP_IS_TEXT(root)) {
512 SP_TEXT(root)->attributes.mergeInto(&optional_attrs, parent_optional_attrs, parent_attrs_offset, true, true);
513 }
514 else if (SP_IS_TSPAN(root)) {
515 SPTSpan *tspan = SP_TSPAN(root);
516 bool use_xy = !in_textpath && (tspan->role == SP_TSPAN_ROLE_UNSPECIFIED || !tspan->attributes.singleXYCoordinates());
517 tspan->attributes.mergeInto(&optional_attrs, parent_optional_attrs, parent_attrs_offset, use_xy, true);
518 }
519 else if (SP_IS_TEXTPATH(root)) {
520 in_textpath = true;
521 SP_TEXTPATH(root)->attributes.mergeInto(&optional_attrs, parent_optional_attrs, parent_attrs_offset, false, true);
522 optional_attrs.x.clear();
523 optional_attrs.y.clear();
524 }
525 else {
526 optional_attrs = parent_optional_attrs;
527 child_attrs_offset = parent_attrs_offset;
528 }
530 if (SP_IS_TSPAN(root))
531 if (SP_TSPAN(root)->role != SP_TSPAN_ROLE_UNSPECIFIED) {
532 // we need to allow the first line not to have role=line, but still set the source_cookie to the right value
533 SPObject *prev_object = SP_OBJECT_PREV(root);
534 if (prev_object && SP_IS_TSPAN(prev_object)) {
535 if (!layout.inputExists())
536 layout.appendText("", prev_object->style, prev_object, &optional_attrs);
537 layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, prev_object);
538 }
539 if (!root->hasChildren())
540 layout.appendText("", root->style, root, &optional_attrs);
541 length++; // interpreting line breaks as a character for the purposes of x/y/etc attributes
542 // is a liberal interpretation of the svg spec, but a strict reading would mean
543 // that if the first line is empty the second line would take its place at the
544 // start position. Very confusing.
545 child_attrs_offset--;
546 }
548 for (SPObject *child = sp_object_first_child(root) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
549 if (SP_IS_STRING(child)) {
550 Glib::ustring const &string = SP_STRING(child)->string;
551 layout.appendText(string, root->style, child, &optional_attrs, child_attrs_offset + length);
552 length += string.length();
553 } else {
554 length += _buildLayoutInput(child, optional_attrs, child_attrs_offset + length, in_textpath);
555 }
556 }
558 return length;
559 }
561 void SPText::rebuildLayout()
562 {
563 layout.clear();
564 Inkscape::Text::Layout::OptionalTextTagAttrs optional_attrs;
565 _buildLayoutInput(this, optional_attrs, 0, false);
566 layout.calculateFlow();
567 for (SPObject *child = firstChild() ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
568 if (SP_IS_TEXTPATH(child)) {
569 SPTextPath const *textpath = SP_TEXTPATH(child);
570 if (textpath->originalPath != NULL) {
571 //g_print(layout.dumpAsText().c_str());
572 layout.fitToPathAlign(textpath->startOffset, *textpath->originalPath);
573 }
574 }
575 }
576 //g_print(layout.dumpAsText().c_str());
578 // set the x,y attributes on role:line spans
579 for (SPObject *child = firstChild() ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
580 if (!SP_IS_TSPAN(child)) continue;
581 SPTSpan *tspan = SP_TSPAN(child);
582 if (tspan->role == SP_TSPAN_ROLE_UNSPECIFIED) continue;
583 if (!tspan->attributes.singleXYCoordinates()) continue;
584 Inkscape::Text::Layout::iterator iter = layout.sourceToIterator(tspan);
585 NR::Point anchor_point = layout.chunkAnchorPoint(iter);
586 sp_repr_set_svg_double(SP_OBJECT_REPR(tspan), "x", anchor_point[NR::X]);
587 sp_repr_set_svg_double(SP_OBJECT_REPR(tspan), "y", anchor_point[NR::Y]);
588 }
589 }
592 void SPText::_adjustFontsizeRecursive(SPItem *item, double ex, bool is_root)
593 {
594 SPStyle *style = SP_OBJECT_STYLE (item);
596 if (style && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
597 if (!style->font_size.set && is_root) {
598 style->font_size.set = 1;
599 style->font_size.type = SP_FONT_SIZE_LENGTH;
600 }
601 style->font_size.computed *= ex;
602 style->letter_spacing.computed *= ex;
603 style->word_spacing.computed *= ex;
604 item->updateRepr();
605 }
607 for (SPObject *o = item->children; o != NULL; o = o->next) {
608 if (SP_IS_ITEM(o))
609 _adjustFontsizeRecursive(SP_ITEM(o), ex, false);
610 }
611 }
613 void SPText::_adjustCoordsRecursive(SPItem *item, NR::Matrix const &m, double ex, bool is_root)
614 {
615 if (SP_IS_TSPAN(item))
616 SP_TSPAN(item)->attributes.transform(m, ex, ex, is_root);
617 // it doesn't matter if we change the x,y for role=line spans because we'll just overwrite them anyway
618 else if (SP_IS_TEXT(item))
619 SP_TEXT(item)->attributes.transform(m, ex, ex, is_root);
620 else if (SP_IS_TEXTPATH(item))
621 SP_TEXTPATH(item)->attributes.transform(m, ex, ex, is_root);
623 for (SPObject *o = item->children; o != NULL; o = o->next) {
624 if (SP_IS_ITEM(o))
625 _adjustCoordsRecursive(SP_ITEM(o), m, ex, false);
626 }
627 }
630 void SPText::_clearFlow(NRArenaGroup *in_arena)
631 {
632 nr_arena_item_request_render (in_arena);
633 for (NRArenaItem *child = in_arena->children; child != NULL; ) {
634 NRArenaItem *nchild = child->next;
636 nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
637 nr_arena_item_remove_child (in_arena, child);
639 child=nchild;
640 }
641 }
644 /*
645 * TextTagAttributes implementation
646 */
648 void TextTagAttributes::readFrom(Inkscape::XML::Node const *node)
649 {
650 readSingleAttribute(SP_ATTR_X, node->attribute("x"));
651 readSingleAttribute(SP_ATTR_Y, node->attribute("y"));
652 readSingleAttribute(SP_ATTR_DX, node->attribute("dx"));
653 readSingleAttribute(SP_ATTR_DY, node->attribute("dy"));
654 readSingleAttribute(SP_ATTR_ROTATE, node->attribute("rotate"));
655 }
657 bool TextTagAttributes::readSingleAttribute(unsigned key, gchar const *value)
658 {
659 std::vector<SVGLength> *attr_vector;
660 switch (key) {
661 case SP_ATTR_X: attr_vector = &attributes.x; break;
662 case SP_ATTR_Y: attr_vector = &attributes.y; break;
663 case SP_ATTR_DX: attr_vector = &attributes.dx; break;
664 case SP_ATTR_DY: attr_vector = &attributes.dy; break;
665 case SP_ATTR_ROTATE: attr_vector = &attributes.rotate; break;
666 default: return false;
667 }
669 // FIXME: sp_svg_length_list_read() amalgamates repeated separators. This prevents unset values.
670 *attr_vector = sp_svg_length_list_read(value);
671 return true;
672 }
674 void TextTagAttributes::writeTo(Inkscape::XML::Node *node) const
675 {
676 writeSingleAttribute(node, "x", attributes.x);
677 writeSingleAttribute(node, "y", attributes.y);
678 writeSingleAttribute(node, "dx", attributes.dx);
679 writeSingleAttribute(node, "dy", attributes.dy);
680 writeSingleAttribute(node, "rotate", attributes.rotate);
681 }
683 void TextTagAttributes::writeSingleAttribute(Inkscape::XML::Node *node, gchar const *key, std::vector<SVGLength> const &attr_vector)
684 {
685 if (attr_vector.empty())
686 node->setAttribute(key, NULL);
687 else {
688 Glib::ustring string;
689 gchar single_value_string[32];
691 // FIXME: this has no concept of unset values because sp_svg_length_list_read() can't read them back in
692 for (std::vector<SVGLength>::const_iterator it = attr_vector.begin() ; it != attr_vector.end() ; it++) {
693 g_ascii_formatd(single_value_string, sizeof (single_value_string), "%.8g", it->computed);
694 if (!string.empty()) string += ' ';
695 string += single_value_string;
696 }
697 node->setAttribute(key, string.c_str());
698 }
699 }
701 bool TextTagAttributes::singleXYCoordinates() const
702 {
703 return attributes.x.size() <= 1 && attributes.y.size() <= 1;
704 }
706 bool TextTagAttributes::anyAttributesSet() const
707 {
708 return !attributes.x.empty() || !attributes.y.empty() || !attributes.dx.empty() || !attributes.dy.empty() || !attributes.rotate.empty();
709 }
711 NR::Point TextTagAttributes::firstXY() const
712 {
713 NR::Point point;
714 if (attributes.x.empty()) point[NR::X] = 0.0;
715 else point[NR::X] = attributes.x[0].computed;
716 if (attributes.y.empty()) point[NR::Y] = 0.0;
717 else point[NR::Y] = attributes.y[0].computed;
718 return point;
719 }
721 void TextTagAttributes::mergeInto(Inkscape::Text::Layout::OptionalTextTagAttrs *output, Inkscape::Text::Layout::OptionalTextTagAttrs const &parent_attrs, unsigned parent_attrs_offset, bool copy_xy, bool copy_dxdyrotate) const
722 {
723 mergeSingleAttribute(&output->x, parent_attrs.x, parent_attrs_offset, copy_xy ? &attributes.x : NULL);
724 mergeSingleAttribute(&output->y, parent_attrs.y, parent_attrs_offset, copy_xy ? &attributes.y : NULL);
725 mergeSingleAttribute(&output->dx, parent_attrs.dx, parent_attrs_offset, copy_dxdyrotate ? &attributes.dx : NULL);
726 mergeSingleAttribute(&output->dy, parent_attrs.dy, parent_attrs_offset, copy_dxdyrotate ? &attributes.dy : NULL);
727 mergeSingleAttribute(&output->rotate, parent_attrs.rotate, parent_attrs_offset, copy_dxdyrotate ? &attributes.rotate : NULL);
728 }
730 void TextTagAttributes::mergeSingleAttribute(std::vector<SVGLength> *output_list, std::vector<SVGLength> const &parent_list, unsigned parent_offset, std::vector<SVGLength> const *overlay_list)
731 {
732 if (overlay_list == NULL) {
733 output_list->resize(std::max(0, (int)parent_list.size() - (int)parent_offset));
734 std::copy(parent_list.begin() + parent_offset, parent_list.end(), output_list->begin());
735 } else {
736 output_list->clear();
737 output_list->reserve(std::max((int)parent_list.size() - (int)parent_offset, (int)overlay_list->size()));
738 unsigned overlay_offset = 0;
739 while (parent_offset < parent_list.size() || overlay_offset < overlay_list->size()) {
740 SVGLength const *this_item;
741 if (overlay_offset < overlay_list->size()) {
742 this_item = &(*overlay_list)[overlay_offset];
743 overlay_offset++;
744 parent_offset++;
745 } else {
746 this_item = &parent_list[parent_offset];
747 parent_offset++;
748 }
749 output_list->push_back(*this_item);
750 }
751 }
752 }
754 void TextTagAttributes::erase(unsigned start_index, unsigned n)
755 {
756 if (n == 0) return;
757 if (!singleXYCoordinates()) {
758 eraseSingleAttribute(&attributes.x, start_index, n);
759 eraseSingleAttribute(&attributes.y, start_index, n);
760 }
761 eraseSingleAttribute(&attributes.dx, start_index, n);
762 eraseSingleAttribute(&attributes.dy, start_index, n);
763 eraseSingleAttribute(&attributes.rotate, start_index, n);
764 }
766 void TextTagAttributes::eraseSingleAttribute(std::vector<SVGLength> *attr_vector, unsigned start_index, unsigned n)
767 {
768 if (attr_vector->size() <= start_index) return;
769 if (attr_vector->size() <= start_index + n)
770 attr_vector->erase(attr_vector->begin() + start_index, attr_vector->end());
771 else
772 attr_vector->erase(attr_vector->begin() + start_index, attr_vector->begin() + start_index + n);
773 }
775 void TextTagAttributes::insert(unsigned start_index, unsigned n)
776 {
777 if (n == 0) return;
778 if (!singleXYCoordinates()) {
779 insertSingleAttribute(&attributes.x, start_index, n, true);
780 insertSingleAttribute(&attributes.y, start_index, n, true);
781 }
782 insertSingleAttribute(&attributes.dx, start_index, n, false);
783 insertSingleAttribute(&attributes.dy, start_index, n, false);
784 insertSingleAttribute(&attributes.rotate, start_index, n, false);
785 }
787 void TextTagAttributes::insertSingleAttribute(std::vector<SVGLength> *attr_vector, unsigned start_index, unsigned n, bool is_xy)
788 {
789 if (attr_vector->size() <= start_index) return;
790 SVGLength zero_length;
791 zero_length = 0.0;
792 attr_vector->insert(attr_vector->begin() + start_index, n, zero_length);
793 if (is_xy) {
794 double begin = start_index == 0 ? (*attr_vector)[start_index + n].computed : (*attr_vector)[start_index - 1].computed;
795 double diff = ((*attr_vector)[start_index + n].computed - begin) / n; // n tested for nonzero in insert()
796 for (unsigned i = 0 ; i < n ; i++)
797 (*attr_vector)[start_index + i] = begin + diff * i;
798 }
799 }
801 void TextTagAttributes::split(unsigned index, TextTagAttributes *second)
802 {
803 if (!singleXYCoordinates()) {
804 splitSingleAttribute(&attributes.x, index, &second->attributes.x, false);
805 splitSingleAttribute(&attributes.y, index, &second->attributes.y, false);
806 }
807 splitSingleAttribute(&attributes.dx, index, &second->attributes.dx, true);
808 splitSingleAttribute(&attributes.dy, index, &second->attributes.dy, true);
809 splitSingleAttribute(&attributes.rotate, index, &second->attributes.rotate, true);
810 }
812 void TextTagAttributes::splitSingleAttribute(std::vector<SVGLength> *first_vector, unsigned index, std::vector<SVGLength> *second_vector, bool trimZeros)
813 {
814 second_vector->clear();
815 if (first_vector->size() <= index) return;
816 second_vector->resize(first_vector->size() - index);
817 std::copy(first_vector->begin() + index, first_vector->end(), second_vector->begin());
818 first_vector->resize(index);
819 if (trimZeros)
820 while (!first_vector->empty() && (!first_vector->back()._set || first_vector->back().value == 0.0))
821 first_vector->resize(first_vector->size() - 1);
822 }
824 void TextTagAttributes::join(TextTagAttributes const &first, TextTagAttributes const &second, unsigned second_index)
825 {
826 if (second.singleXYCoordinates()) {
827 attributes.x = first.attributes.x;
828 attributes.y = first.attributes.y;
829 } else {
830 joinSingleAttribute(&attributes.x, first.attributes.x, second.attributes.x, second_index);
831 joinSingleAttribute(&attributes.y, first.attributes.y, second.attributes.y, second_index);
832 }
833 joinSingleAttribute(&attributes.dx, first.attributes.dx, second.attributes.dx, second_index);
834 joinSingleAttribute(&attributes.dy, first.attributes.dy, second.attributes.dy, second_index);
835 joinSingleAttribute(&attributes.rotate, first.attributes.rotate, second.attributes.rotate, second_index);
836 }
838 void TextTagAttributes::joinSingleAttribute(std::vector<SVGLength> *dest_vector, std::vector<SVGLength> const &first_vector, std::vector<SVGLength> const &second_vector, unsigned second_index)
839 {
840 if (second_vector.empty())
841 *dest_vector = first_vector;
842 else {
843 dest_vector->resize(second_index + second_vector.size());
844 if (first_vector.size() < second_index) {
845 std::copy(first_vector.begin(), first_vector.end(), dest_vector->begin());
846 SVGLength zero_length;
847 zero_length = 0.0;
848 std::fill(dest_vector->begin() + first_vector.size(), dest_vector->begin() + second_index, zero_length);
849 } else
850 std::copy(first_vector.begin(), first_vector.begin() + second_index, dest_vector->begin());
851 std::copy(second_vector.begin(), second_vector.end(), dest_vector->begin() + second_index);
852 }
853 }
855 void TextTagAttributes::transform(NR::Matrix const &matrix, double scale_x, double scale_y, bool extend_zero_length)
856 {
857 SVGLength zero_length;
858 zero_length = 0.0;
860 /* edge testcases for this code:
861 1) moving text elements whose position is done entirely with transform="...", no x,y attributes
862 2) unflowing multi-line flowtext then moving it (it has x but not y)
863 */
864 unsigned points_count = std::max(attributes.x.size(), attributes.y.size());
865 if (extend_zero_length && points_count < 1)
866 points_count = 1;
867 for (unsigned i = 0 ; i < points_count ; i++) {
868 NR::Point point;
869 if (i < attributes.x.size()) point[NR::X] = attributes.x[i].computed;
870 else point[NR::X] = 0.0;
871 if (i < attributes.y.size()) point[NR::Y] = attributes.y[i].computed;
872 else point[NR::Y] = 0.0;
873 point *= matrix;
874 if (i < attributes.x.size())
875 attributes.x[i] = point[NR::X];
876 else if (point[NR::X] != 0.0 && extend_zero_length) {
877 attributes.x.resize(i + 1, zero_length);
878 attributes.x[i] = point[NR::X];
879 }
880 if (i < attributes.y.size())
881 attributes.y[i] = point[NR::Y];
882 else if (point[NR::Y] != 0.0 && extend_zero_length) {
883 attributes.y.resize(i + 1, zero_length);
884 attributes.y[i] = point[NR::Y];
885 }
886 }
887 for (std::vector<SVGLength>::iterator it = attributes.dx.begin() ; it != attributes.dx.end() ; it++)
888 *it = it->computed * scale_x;
889 for (std::vector<SVGLength>::iterator it = attributes.dy.begin() ; it != attributes.dy.end() ; it++)
890 *it = it->computed * scale_y;
891 }
893 void TextTagAttributes::addToDxDy(unsigned index, NR::Point const &adjust)
894 {
895 SVGLength zero_length;
896 zero_length = 0.0;
898 if (adjust[NR::X] != 0.0) {
899 if (attributes.dx.size() < index + 1) attributes.dx.resize(index + 1, zero_length);
900 attributes.dx[index] = attributes.dx[index].computed + adjust[NR::X];
901 }
902 if (adjust[NR::Y] != 0.0) {
903 if (attributes.dy.size() < index + 1) attributes.dy.resize(index + 1, zero_length);
904 attributes.dy[index] = attributes.dy[index].computed + adjust[NR::Y];
905 }
906 }
908 void TextTagAttributes::addToRotate(unsigned index, double delta)
909 {
910 SVGLength zero_length;
911 zero_length = 0.0;
913 if (attributes.rotate.size() < index + 1) {
914 if (attributes.rotate.empty())
915 attributes.rotate.resize(index + 1, zero_length);
916 else
917 attributes.rotate.resize(index + 1, attributes.rotate.back());
918 }
919 attributes.rotate[index] = mod360(attributes.rotate[index].computed + delta);
920 }
923 /*
924 Local Variables:
925 mode:c++
926 c-file-style:"stroustrup"
927 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
928 indent-tabs-mode:nil
929 fill-column:99
930 End:
931 */
932 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :