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