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 if (!repr)
307 repr = sp_repr_new ("svg:text");
308 GSList *l = NULL;
309 for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
310 Inkscape::XML::Node *crepr = NULL;
311 if (SP_IS_STRING(child)) {
312 crepr = sp_repr_new_text(SP_STRING(child)->string.c_str());
313 } else {
314 crepr = child->updateRepr(NULL, flags);
315 }
316 if (crepr) l = g_slist_prepend (l, crepr);
317 }
318 while (l) {
319 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
320 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
321 l = g_slist_remove (l, l->data);
322 }
323 } else {
324 for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
325 if (SP_IS_STRING(child)) {
326 SP_OBJECT_REPR(child)->setContent(SP_STRING(child)->string.c_str());
327 } else {
328 child->updateRepr(flags);
329 }
330 }
331 }
333 text->attributes.writeTo(repr);
335 // deprecated attribute, but keep it around for backwards compatibility
336 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) {
337 Inkscape::SVGOStringStream os;
338 os << (text->style->line_height.value * 100.0) << "%";
339 SP_OBJECT_REPR(text)->setAttribute("sodipodi:linespacing", os.str().c_str());
340 }
341 else
342 SP_OBJECT_REPR(text)->setAttribute("sodipodi:linespacing", NULL);
344 if (((SPObjectClass *) (text_parent_class))->write)
345 ((SPObjectClass *) (text_parent_class))->write (object, repr, flags);
347 return repr;
348 }
350 static void
351 sp_text_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
352 {
353 SP_TEXT(item)->layout.getBoundingBox(bbox, transform);
355 // Add stroke width
356 SPStyle* style=SP_OBJECT_STYLE (item);
357 if (style->stroke.type != SP_PAINT_TYPE_NONE) {
358 double const scale = expansion(transform);
359 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
360 double const width = MAX(0.125, style->stroke_width.computed * scale);
361 if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
362 bbox->x0-=0.5*width;
363 bbox->x1+=0.5*width;
364 bbox->y0-=0.5*width;
365 bbox->y1+=0.5*width;
366 }
367 }
368 }
369 }
372 static NRArenaItem *
373 sp_text_show(SPItem *item, NRArena *arena, unsigned /* key*/, unsigned /*flags*/)
374 {
375 SPText *group = (SPText *) item;
377 NRArenaGroup *flowed = NRArenaGroup::create(arena);
378 nr_arena_group_set_transparent (flowed, FALSE);
380 nr_arena_group_set_style(flowed, group->style);
382 // pass the bbox of the text object as paintbox (used for paintserver fills)
383 NRRect paintbox;
384 sp_item_invoke_bbox(item, &paintbox, NR::identity(), TRUE);
385 group->layout.show(flowed, &paintbox);
387 return flowed;
388 }
390 static void
391 sp_text_hide(SPItem *item, unsigned key)
392 {
393 if (((SPItemClass *) text_parent_class)->hide)
394 ((SPItemClass *) text_parent_class)->hide (item, key);
395 }
397 static char *
398 sp_text_description(SPItem *item)
399 {
400 SPText *text = (SPText *) item;
401 SPStyle *style = SP_OBJECT_STYLE(text);
403 font_instance *tf = (font_factory::Default())->Face(style->text->font_family.value,
404 font_style_to_pos(*style));
405 char name_buf[256];
406 char *n;
407 if (tf) {
408 tf->Name(name_buf, sizeof(name_buf));
409 n = xml_quote_strdup(name_buf);
410 tf->Unref();
411 } else {
412 /* TRANSLATORS: For description of font with no name. */
413 n = g_strdup(_("<no name found>"));
414 }
416 GString *xs = SP_PX_TO_METRIC_STRING(style->font_size.computed, sp_desktop_namedview(SP_ACTIVE_DESKTOP)->getDefaultMetric());
418 char *ret = ( SP_IS_TEXT_TEXTPATH(item)
419 ? g_strdup_printf(_("<b>Text on path</b> (%s, %s)"), n, xs->str)
420 : g_strdup_printf(_("<b>Text</b> (%s, %s)"), n, xs->str) );
421 g_free(n);
422 return ret;
423 }
425 static void sp_text_snappoints(SPItem const *item, SnapPointsIter p)
426 {
427 // the baseline anchor of the first char
428 Inkscape::Text::Layout const *layout = te_get_layout((SPItem *) item);
429 if(layout != NULL) {
430 *p = layout->characterAnchorPoint(layout->begin()) * sp_item_i2d_affine(item);
431 }
432 }
434 static NR::Matrix
435 sp_text_set_transform (SPItem *item, NR::Matrix const &xform)
436 {
437 SPText *text = SP_TEXT(item);
439 // we cannot optimize textpath because changing its fontsize will break its match to the path
440 if (SP_IS_TEXT_TEXTPATH (text))
441 return xform;
443 /* This function takes care of scaling & translation only, we return whatever parts we can't
444 handle. */
446 // TODO: pjrm tried to use fontsize_expansion(xform) here and it works for text in that font size
447 // is scaled more intuitively when scaling non-uniformly; however this necessitated using
448 // fontsize_expansion instead of expansion in other places too, where it was not appropriate
449 // (e.g. it broke stroke width on copy/pasting of style from horizontally stretched to vertically
450 // stretched shape). Using fontsize_expansion only here broke setting the style via font
451 // dialog. This needs to be investigated further.
452 double const ex = NR::expansion(xform);
453 if (ex == 0) {
454 return xform;
455 }
457 NR::Matrix ret(NR::transform(xform));
458 ret[0] /= ex;
459 ret[1] /= ex;
460 ret[2] /= ex;
461 ret[3] /= ex;
463 // Adjust x/y, dx/dy
464 text->_adjustCoordsRecursive (item, xform * ret.inverse(), ex);
466 // Adjust font size
467 text->_adjustFontsizeRecursive (item, ex);
469 // Adjust stroke width
470 sp_item_adjust_stroke_width_recursive (item, ex);
472 // Adjust pattern fill
473 sp_item_adjust_pattern(item, xform * ret.inverse());
475 // Adjust gradient fill
476 sp_item_adjust_gradient(item, xform * ret.inverse());
478 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_TEXT_LAYOUT_MODIFIED_FLAG);
480 return ret;
481 }
483 static void
484 sp_text_print (SPItem *item, SPPrintContext *ctx)
485 {
486 NRRect pbox, dbox, bbox;
487 SPText *group = SP_TEXT (item);
489 sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
490 sp_item_bbox_desktop (item, &bbox);
491 dbox.x0 = 0.0;
492 dbox.y0 = 0.0;
493 dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
494 dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
495 NR::Matrix const ctm = sp_item_i2d_affine(item);
497 group->layout.print(ctx,&pbox,&dbox,&bbox,ctm);
498 }
500 /*
501 * Member functions
502 */
504 unsigned SPText::_buildLayoutInput(SPObject *root, Inkscape::Text::Layout::OptionalTextTagAttrs const &parent_optional_attrs, unsigned parent_attrs_offset, bool in_textpath)
505 {
506 unsigned length = 0;
507 int child_attrs_offset = 0;
508 Inkscape::Text::Layout::OptionalTextTagAttrs optional_attrs;
510 if (SP_IS_TEXT(root)) {
511 SP_TEXT(root)->attributes.mergeInto(&optional_attrs, parent_optional_attrs, parent_attrs_offset, true, true);
512 }
513 else if (SP_IS_TSPAN(root)) {
514 SPTSpan *tspan = SP_TSPAN(root);
515 bool use_xy = !in_textpath && (tspan->role == SP_TSPAN_ROLE_UNSPECIFIED || !tspan->attributes.singleXYCoordinates());
516 tspan->attributes.mergeInto(&optional_attrs, parent_optional_attrs, parent_attrs_offset, use_xy, true);
517 }
518 else if (SP_IS_TEXTPATH(root)) {
519 in_textpath = true;
520 SP_TEXTPATH(root)->attributes.mergeInto(&optional_attrs, parent_optional_attrs, parent_attrs_offset, false, true);
521 optional_attrs.x.clear();
522 optional_attrs.y.clear();
523 }
524 else {
525 optional_attrs = parent_optional_attrs;
526 child_attrs_offset = parent_attrs_offset;
527 }
529 if (SP_IS_TSPAN(root))
530 if (SP_TSPAN(root)->role != SP_TSPAN_ROLE_UNSPECIFIED) {
531 // we need to allow the first line not to have role=line, but still set the source_cookie to the right value
532 SPObject *prev_object = SP_OBJECT_PREV(root);
533 if (prev_object && SP_IS_TSPAN(prev_object)) {
534 if (!layout.inputExists())
535 layout.appendText("", prev_object->style, prev_object, &optional_attrs);
536 layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, prev_object);
537 }
538 if (!root->hasChildren())
539 layout.appendText("", root->style, root, &optional_attrs);
540 length++; // interpreting line breaks as a character for the purposes of x/y/etc attributes
541 // is a liberal interpretation of the svg spec, but a strict reading would mean
542 // that if the first line is empty the second line would take its place at the
543 // start position. Very confusing.
544 child_attrs_offset--;
545 }
547 for (SPObject *child = sp_object_first_child(root) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
548 if (SP_IS_STRING(child)) {
549 Glib::ustring const &string = SP_STRING(child)->string;
550 layout.appendText(string, root->style, child, &optional_attrs, child_attrs_offset + length);
551 length += string.length();
552 } else {
553 length += _buildLayoutInput(child, optional_attrs, child_attrs_offset + length, in_textpath);
554 }
555 }
557 return length;
558 }
560 void SPText::rebuildLayout()
561 {
562 layout.clear();
563 Inkscape::Text::Layout::OptionalTextTagAttrs optional_attrs;
564 _buildLayoutInput(this, optional_attrs, 0, false);
565 layout.calculateFlow();
566 for (SPObject *child = firstChild() ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
567 if (SP_IS_TEXTPATH(child)) {
568 SPTextPath const *textpath = SP_TEXTPATH(child);
569 if (textpath->originalPath != NULL) {
570 //g_print(layout.dumpAsText().c_str());
571 layout.fitToPathAlign(textpath->startOffset, *textpath->originalPath);
572 }
573 }
574 }
575 //g_print(layout.dumpAsText().c_str());
577 // set the x,y attributes on role:line spans
578 for (SPObject *child = firstChild() ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
579 if (!SP_IS_TSPAN(child)) continue;
580 SPTSpan *tspan = SP_TSPAN(child);
581 if (tspan->role == SP_TSPAN_ROLE_UNSPECIFIED) continue;
582 if (!tspan->attributes.singleXYCoordinates()) continue;
583 Inkscape::Text::Layout::iterator iter = layout.sourceToIterator(tspan);
584 NR::Point anchor_point = layout.chunkAnchorPoint(iter);
585 sp_repr_set_svg_double(SP_OBJECT_REPR(tspan), "x", anchor_point[NR::X]);
586 sp_repr_set_svg_double(SP_OBJECT_REPR(tspan), "y", anchor_point[NR::Y]);
587 }
588 }
591 void SPText::_adjustFontsizeRecursive(SPItem *item, double ex, bool is_root)
592 {
593 SPStyle *style = SP_OBJECT_STYLE (item);
595 if (style && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
596 if (!style->font_size.set && is_root) {
597 style->font_size.set = 1;
598 style->font_size.type = SP_FONT_SIZE_LENGTH;
599 }
600 style->font_size.computed *= ex;
601 style->letter_spacing.computed *= ex;
602 style->word_spacing.computed *= ex;
603 item->updateRepr();
604 }
606 for (SPObject *o = item->children; o != NULL; o = o->next) {
607 if (SP_IS_ITEM(o))
608 _adjustFontsizeRecursive(SP_ITEM(o), ex, false);
609 }
610 }
612 void SPText::_adjustCoordsRecursive(SPItem *item, NR::Matrix const &m, double ex, bool is_root)
613 {
614 if (SP_IS_TSPAN(item))
615 SP_TSPAN(item)->attributes.transform(m, ex, ex, is_root);
616 // it doesn't matter if we change the x,y for role=line spans because we'll just overwrite them anyway
617 else if (SP_IS_TEXT(item))
618 SP_TEXT(item)->attributes.transform(m, ex, ex, is_root);
619 else if (SP_IS_TEXTPATH(item))
620 SP_TEXTPATH(item)->attributes.transform(m, ex, ex, is_root);
622 for (SPObject *o = item->children; o != NULL; o = o->next) {
623 if (SP_IS_ITEM(o))
624 _adjustCoordsRecursive(SP_ITEM(o), m, ex, false);
625 }
626 }
629 void SPText::_clearFlow(NRArenaGroup *in_arena)
630 {
631 nr_arena_item_request_render (in_arena);
632 for (NRArenaItem *child = in_arena->children; child != NULL; ) {
633 NRArenaItem *nchild = child->next;
635 nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
636 nr_arena_item_remove_child (in_arena, child);
638 child=nchild;
639 }
640 }
643 /*
644 * TextTagAttributes implementation
645 */
647 void TextTagAttributes::readFrom(Inkscape::XML::Node const *node)
648 {
649 readSingleAttribute(SP_ATTR_X, node->attribute("x"));
650 readSingleAttribute(SP_ATTR_Y, node->attribute("y"));
651 readSingleAttribute(SP_ATTR_DX, node->attribute("dx"));
652 readSingleAttribute(SP_ATTR_DY, node->attribute("dy"));
653 readSingleAttribute(SP_ATTR_ROTATE, node->attribute("rotate"));
654 }
656 bool TextTagAttributes::readSingleAttribute(unsigned key, gchar const *value)
657 {
658 std::vector<SVGLength> *attr_vector;
659 switch (key) {
660 case SP_ATTR_X: attr_vector = &attributes.x; break;
661 case SP_ATTR_Y: attr_vector = &attributes.y; break;
662 case SP_ATTR_DX: attr_vector = &attributes.dx; break;
663 case SP_ATTR_DY: attr_vector = &attributes.dy; break;
664 case SP_ATTR_ROTATE: attr_vector = &attributes.rotate; break;
665 default: return false;
666 }
668 // FIXME: sp_svg_length_list_read() amalgamates repeated separators. This prevents unset values.
669 *attr_vector = sp_svg_length_list_read(value);
670 return true;
671 }
673 void TextTagAttributes::writeTo(Inkscape::XML::Node *node) const
674 {
675 writeSingleAttribute(node, "x", attributes.x);
676 writeSingleAttribute(node, "y", attributes.y);
677 writeSingleAttribute(node, "dx", attributes.dx);
678 writeSingleAttribute(node, "dy", attributes.dy);
679 writeSingleAttribute(node, "rotate", attributes.rotate);
680 }
682 void TextTagAttributes::writeSingleAttribute(Inkscape::XML::Node *node, gchar const *key, std::vector<SVGLength> const &attr_vector)
683 {
684 if (attr_vector.empty())
685 node->setAttribute(key, NULL);
686 else {
687 Glib::ustring string;
688 gchar single_value_string[32];
690 // FIXME: this has no concept of unset values because sp_svg_length_list_read() can't read them back in
691 for (std::vector<SVGLength>::const_iterator it = attr_vector.begin() ; it != attr_vector.end() ; it++) {
692 g_ascii_formatd(single_value_string, sizeof (single_value_string), "%.8g", it->computed);
693 if (!string.empty()) string += ' ';
694 string += single_value_string;
695 }
696 node->setAttribute(key, string.c_str());
697 }
698 }
700 bool TextTagAttributes::singleXYCoordinates() const
701 {
702 return attributes.x.size() <= 1 && attributes.y.size() <= 1;
703 }
705 bool TextTagAttributes::anyAttributesSet() const
706 {
707 return !attributes.x.empty() || !attributes.y.empty() || !attributes.dx.empty() || !attributes.dy.empty() || !attributes.rotate.empty();
708 }
710 NR::Point TextTagAttributes::firstXY() const
711 {
712 NR::Point point;
713 if (attributes.x.empty()) point[NR::X] = 0.0;
714 else point[NR::X] = attributes.x[0].computed;
715 if (attributes.y.empty()) point[NR::Y] = 0.0;
716 else point[NR::Y] = attributes.y[0].computed;
717 return point;
718 }
720 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
721 {
722 mergeSingleAttribute(&output->x, parent_attrs.x, parent_attrs_offset, copy_xy ? &attributes.x : NULL);
723 mergeSingleAttribute(&output->y, parent_attrs.y, parent_attrs_offset, copy_xy ? &attributes.y : NULL);
724 mergeSingleAttribute(&output->dx, parent_attrs.dx, parent_attrs_offset, copy_dxdyrotate ? &attributes.dx : NULL);
725 mergeSingleAttribute(&output->dy, parent_attrs.dy, parent_attrs_offset, copy_dxdyrotate ? &attributes.dy : NULL);
726 mergeSingleAttribute(&output->rotate, parent_attrs.rotate, parent_attrs_offset, copy_dxdyrotate ? &attributes.rotate : NULL);
727 }
729 void TextTagAttributes::mergeSingleAttribute(std::vector<SVGLength> *output_list, std::vector<SVGLength> const &parent_list, unsigned parent_offset, std::vector<SVGLength> const *overlay_list)
730 {
731 if (overlay_list == NULL) {
732 output_list->resize(std::max(0, (int)parent_list.size() - (int)parent_offset));
733 std::copy(parent_list.begin() + parent_offset, parent_list.end(), output_list->begin());
734 } else {
735 output_list->clear();
736 output_list->reserve(std::max((int)parent_list.size() - (int)parent_offset, (int)overlay_list->size()));
737 unsigned overlay_offset = 0;
738 while (parent_offset < parent_list.size() || overlay_offset < overlay_list->size()) {
739 SVGLength const *this_item;
740 if (overlay_offset < overlay_list->size()) {
741 this_item = &(*overlay_list)[overlay_offset];
742 overlay_offset++;
743 parent_offset++;
744 } else {
745 this_item = &parent_list[parent_offset];
746 parent_offset++;
747 }
748 output_list->push_back(*this_item);
749 }
750 }
751 }
753 void TextTagAttributes::erase(unsigned start_index, unsigned n)
754 {
755 if (n == 0) return;
756 if (!singleXYCoordinates()) {
757 eraseSingleAttribute(&attributes.x, start_index, n);
758 eraseSingleAttribute(&attributes.y, start_index, n);
759 }
760 eraseSingleAttribute(&attributes.dx, start_index, n);
761 eraseSingleAttribute(&attributes.dy, start_index, n);
762 eraseSingleAttribute(&attributes.rotate, start_index, n);
763 }
765 void TextTagAttributes::eraseSingleAttribute(std::vector<SVGLength> *attr_vector, unsigned start_index, unsigned n)
766 {
767 if (attr_vector->size() <= start_index) return;
768 if (attr_vector->size() <= start_index + n)
769 attr_vector->erase(attr_vector->begin() + start_index, attr_vector->end());
770 else
771 attr_vector->erase(attr_vector->begin() + start_index, attr_vector->begin() + start_index + n);
772 }
774 void TextTagAttributes::insert(unsigned start_index, unsigned n)
775 {
776 if (n == 0) return;
777 if (!singleXYCoordinates()) {
778 insertSingleAttribute(&attributes.x, start_index, n, true);
779 insertSingleAttribute(&attributes.y, start_index, n, true);
780 }
781 insertSingleAttribute(&attributes.dx, start_index, n, false);
782 insertSingleAttribute(&attributes.dy, start_index, n, false);
783 insertSingleAttribute(&attributes.rotate, start_index, n, false);
784 }
786 void TextTagAttributes::insertSingleAttribute(std::vector<SVGLength> *attr_vector, unsigned start_index, unsigned n, bool is_xy)
787 {
788 if (attr_vector->size() <= start_index) return;
789 SVGLength zero_length;
790 zero_length = 0.0;
791 attr_vector->insert(attr_vector->begin() + start_index, n, zero_length);
792 if (is_xy) {
793 double begin = start_index == 0 ? (*attr_vector)[start_index + n].computed : (*attr_vector)[start_index - 1].computed;
794 double diff = ((*attr_vector)[start_index + n].computed - begin) / n; // n tested for nonzero in insert()
795 for (unsigned i = 0 ; i < n ; i++)
796 (*attr_vector)[start_index + i] = begin + diff * i;
797 }
798 }
800 void TextTagAttributes::split(unsigned index, TextTagAttributes *second)
801 {
802 if (!singleXYCoordinates()) {
803 splitSingleAttribute(&attributes.x, index, &second->attributes.x, false);
804 splitSingleAttribute(&attributes.y, index, &second->attributes.y, false);
805 }
806 splitSingleAttribute(&attributes.dx, index, &second->attributes.dx, true);
807 splitSingleAttribute(&attributes.dy, index, &second->attributes.dy, true);
808 splitSingleAttribute(&attributes.rotate, index, &second->attributes.rotate, true);
809 }
811 void TextTagAttributes::splitSingleAttribute(std::vector<SVGLength> *first_vector, unsigned index, std::vector<SVGLength> *second_vector, bool trimZeros)
812 {
813 second_vector->clear();
814 if (first_vector->size() <= index) return;
815 second_vector->resize(first_vector->size() - index);
816 std::copy(first_vector->begin() + index, first_vector->end(), second_vector->begin());
817 first_vector->resize(index);
818 if (trimZeros)
819 while (!first_vector->empty() && (!first_vector->back()._set || first_vector->back().value == 0.0))
820 first_vector->resize(first_vector->size() - 1);
821 }
823 void TextTagAttributes::join(TextTagAttributes const &first, TextTagAttributes const &second, unsigned second_index)
824 {
825 if (second.singleXYCoordinates()) {
826 attributes.x = first.attributes.x;
827 attributes.y = first.attributes.y;
828 } else {
829 joinSingleAttribute(&attributes.x, first.attributes.x, second.attributes.x, second_index);
830 joinSingleAttribute(&attributes.y, first.attributes.y, second.attributes.y, second_index);
831 }
832 joinSingleAttribute(&attributes.dx, first.attributes.dx, second.attributes.dx, second_index);
833 joinSingleAttribute(&attributes.dy, first.attributes.dy, second.attributes.dy, second_index);
834 joinSingleAttribute(&attributes.rotate, first.attributes.rotate, second.attributes.rotate, second_index);
835 }
837 void TextTagAttributes::joinSingleAttribute(std::vector<SVGLength> *dest_vector, std::vector<SVGLength> const &first_vector, std::vector<SVGLength> const &second_vector, unsigned second_index)
838 {
839 if (second_vector.empty())
840 *dest_vector = first_vector;
841 else {
842 dest_vector->resize(second_index + second_vector.size());
843 if (first_vector.size() < second_index) {
844 std::copy(first_vector.begin(), first_vector.end(), dest_vector->begin());
845 SVGLength zero_length;
846 zero_length = 0.0;
847 std::fill(dest_vector->begin() + first_vector.size(), dest_vector->begin() + second_index, zero_length);
848 } else
849 std::copy(first_vector.begin(), first_vector.begin() + second_index, dest_vector->begin());
850 std::copy(second_vector.begin(), second_vector.end(), dest_vector->begin() + second_index);
851 }
852 }
854 void TextTagAttributes::transform(NR::Matrix const &matrix, double scale_x, double scale_y, bool extend_zero_length)
855 {
856 SVGLength zero_length;
857 zero_length = 0.0;
859 /* edge testcases for this code:
860 1) moving text elements whose position is done entirely with transform="...", no x,y attributes
861 2) unflowing multi-line flowtext then moving it (it has x but not y)
862 */
863 unsigned points_count = std::max(attributes.x.size(), attributes.y.size());
864 if (extend_zero_length && points_count < 1)
865 points_count = 1;
866 for (unsigned i = 0 ; i < points_count ; i++) {
867 NR::Point point;
868 if (i < attributes.x.size()) point[NR::X] = attributes.x[i].computed;
869 else point[NR::X] = 0.0;
870 if (i < attributes.y.size()) point[NR::Y] = attributes.y[i].computed;
871 else point[NR::Y] = 0.0;
872 point *= matrix;
873 if (i < attributes.x.size())
874 attributes.x[i] = point[NR::X];
875 else if (point[NR::X] != 0.0 && extend_zero_length) {
876 attributes.x.resize(i + 1, zero_length);
877 attributes.x[i] = point[NR::X];
878 }
879 if (i < attributes.y.size())
880 attributes.y[i] = point[NR::Y];
881 else if (point[NR::Y] != 0.0 && extend_zero_length) {
882 attributes.y.resize(i + 1, zero_length);
883 attributes.y[i] = point[NR::Y];
884 }
885 }
886 for (std::vector<SVGLength>::iterator it = attributes.dx.begin() ; it != attributes.dx.end() ; it++)
887 *it = it->computed * scale_x;
888 for (std::vector<SVGLength>::iterator it = attributes.dy.begin() ; it != attributes.dy.end() ; it++)
889 *it = it->computed * scale_y;
890 }
892 void TextTagAttributes::addToDxDy(unsigned index, NR::Point const &adjust)
893 {
894 SVGLength zero_length;
895 zero_length = 0.0;
897 if (adjust[NR::X] != 0.0) {
898 if (attributes.dx.size() < index + 1) attributes.dx.resize(index + 1, zero_length);
899 attributes.dx[index] = attributes.dx[index].computed + adjust[NR::X];
900 }
901 if (adjust[NR::Y] != 0.0) {
902 if (attributes.dy.size() < index + 1) attributes.dy.resize(index + 1, zero_length);
903 attributes.dy[index] = attributes.dy[index].computed + adjust[NR::Y];
904 }
905 }
907 void TextTagAttributes::addToRotate(unsigned index, double delta)
908 {
909 SVGLength zero_length;
910 zero_length = 0.0;
912 if (attributes.rotate.size() < index + 1) {
913 if (attributes.rotate.empty())
914 attributes.rotate.resize(index + 1, zero_length);
915 else
916 attributes.rotate.resize(index + 1, attributes.rotate.back());
917 }
918 attributes.rotate[index] = mod360(attributes.rotate[index].computed + delta);
919 }
922 /*
923 Local Variables:
924 mode:c++
925 c-file-style:"stroustrup"
926 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
927 indent-tabs-mode:nil
928 fill-column:99
929 End:
930 */
931 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :