1c677edb2d7c014a0b689e557e7be8bf1ef38c5a
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-tref.h"
50 #include "sp-tspan.h"
52 #include "text-editing.h"
54 /*#####################################################
55 # SPTEXT
56 #####################################################*/
58 static void sp_text_class_init (SPTextClass *classname);
59 static void sp_text_init (SPText *text);
60 static void sp_text_release (SPObject *object);
62 static void sp_text_build (SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
63 static void sp_text_set (SPObject *object, unsigned key, gchar const *value);
64 static void sp_text_child_added (SPObject *object, Inkscape::XML::Node *rch, Inkscape::XML::Node *ref);
65 static void sp_text_remove_child (SPObject *object, Inkscape::XML::Node *rch);
66 static void sp_text_update (SPObject *object, SPCtx *ctx, guint flags);
67 static void sp_text_modified (SPObject *object, guint flags);
68 static Inkscape::XML::Node *sp_text_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
70 static void sp_text_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
71 static NRArenaItem *sp_text_show (SPItem *item, NRArena *arena, unsigned key, unsigned flags);
72 static void sp_text_hide (SPItem *item, unsigned key);
73 static char *sp_text_description (SPItem *item);
74 static void sp_text_snappoints(SPItem const *item, SnapPointsIter p);
75 static NR::Matrix sp_text_set_transform(SPItem *item, NR::Matrix const &xform);
76 static void sp_text_print (SPItem *item, SPPrintContext *gpc);
78 static SPItemClass *text_parent_class;
80 GType
81 sp_text_get_type ()
82 {
83 static GType type = 0;
84 if (!type) {
85 GTypeInfo info = {
86 sizeof (SPTextClass),
87 NULL, /* base_init */
88 NULL, /* base_finalize */
89 (GClassInitFunc) sp_text_class_init,
90 NULL, /* class_finalize */
91 NULL, /* class_data */
92 sizeof (SPText),
93 16, /* n_preallocs */
94 (GInstanceInitFunc) sp_text_init,
95 NULL, /* value_table */
96 };
97 type = g_type_register_static (SP_TYPE_ITEM, "SPText", &info, (GTypeFlags)0);
98 }
99 return type;
100 }
102 static void
103 sp_text_class_init (SPTextClass *classname)
104 {
105 SPObjectClass *sp_object_class = (SPObjectClass *) classname;
106 SPItemClass *item_class = (SPItemClass *) classname;
108 text_parent_class = (SPItemClass*)g_type_class_ref (SP_TYPE_ITEM);
110 sp_object_class->release = sp_text_release;
111 sp_object_class->build = sp_text_build;
112 sp_object_class->set = sp_text_set;
113 sp_object_class->child_added = sp_text_child_added;
114 sp_object_class->remove_child = sp_text_remove_child;
115 sp_object_class->update = sp_text_update;
116 sp_object_class->modified = sp_text_modified;
117 sp_object_class->write = sp_text_write;
119 item_class->bbox = sp_text_bbox;
120 item_class->show = sp_text_show;
121 item_class->hide = sp_text_hide;
122 item_class->description = sp_text_description;
123 item_class->snappoints = sp_text_snappoints;
124 item_class->set_transform = sp_text_set_transform;
125 item_class->print = sp_text_print;
126 }
128 static void
129 sp_text_init (SPText *text)
130 {
131 new (&text->layout) Inkscape::Text::Layout;
132 new (&text->attributes) TextTagAttributes;
133 }
135 static void
136 sp_text_release (SPObject *object)
137 {
138 SPText *text = SP_TEXT(object);
139 text->attributes.~TextTagAttributes();
140 text->layout.~Layout();
142 if (((SPObjectClass *) text_parent_class)->release)
143 ((SPObjectClass *) text_parent_class)->release(object);
144 }
146 static void
147 sp_text_build (SPObject *object, SPDocument *doc, Inkscape::XML::Node *repr)
148 {
149 sp_object_read_attr(object, "x");
150 sp_object_read_attr(object, "y");
151 sp_object_read_attr(object, "dx");
152 sp_object_read_attr(object, "dy");
153 sp_object_read_attr(object, "rotate");
155 if (((SPObjectClass *) text_parent_class)->build)
156 ((SPObjectClass *) text_parent_class)->build(object, doc, repr);
158 sp_object_read_attr(object, "sodipodi:linespacing"); // has to happen after the styles are read
159 }
161 static void
162 sp_text_set(SPObject *object, unsigned key, gchar const *value)
163 {
164 SPText *text = SP_TEXT (object);
166 if (text->attributes.readSingleAttribute(key, value)) {
167 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
168 } else {
169 switch (key) {
170 case SP_ATTR_SODIPODI_LINESPACING:
171 // convert deprecated tag to css
172 if (value) {
173 text->style->line_height.set = TRUE;
174 text->style->line_height.inherit = FALSE;
175 text->style->line_height.normal = FALSE;
176 text->style->line_height.unit = SP_CSS_UNIT_PERCENT;
177 text->style->line_height.value = text->style->line_height.computed = sp_svg_read_percentage (value, 1.0);
178 }
179 object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_TEXT_LAYOUT_MODIFIED_FLAG);
180 break;
181 default:
182 if (((SPObjectClass *) text_parent_class)->set)
183 ((SPObjectClass *) text_parent_class)->set (object, key, value);
184 break;
185 }
186 }
187 }
189 static void
190 sp_text_child_added (SPObject *object, Inkscape::XML::Node *rch, Inkscape::XML::Node *ref)
191 {
192 SPText *text = SP_TEXT (object);
194 if (((SPObjectClass *) text_parent_class)->child_added)
195 ((SPObjectClass *) text_parent_class)->child_added (object, rch, ref);
197 text->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_TEXT_CONTENT_MODIFIED_FLAG);
198 }
200 static void
201 sp_text_remove_child (SPObject *object, Inkscape::XML::Node *rch)
202 {
203 SPText *text = SP_TEXT (object);
205 if (((SPObjectClass *) text_parent_class)->remove_child)
206 ((SPObjectClass *) text_parent_class)->remove_child (object, rch);
208 text->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_TEXT_CONTENT_MODIFIED_FLAG | SP_TEXT_LAYOUT_MODIFIED_FLAG);
209 }
211 static void
212 sp_text_update (SPObject *object, SPCtx *ctx, guint flags)
213 {
214 SPText *text = SP_TEXT (object);
216 if (((SPObjectClass *) text_parent_class)->update)
217 ((SPObjectClass *) text_parent_class)->update (object, ctx, flags);
219 guint cflags = (flags & SP_OBJECT_MODIFIED_CASCADE);
220 if (flags & SP_OBJECT_MODIFIED_FLAG) cflags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
223 /* Create temporary list of children */
224 GSList *l = NULL;
225 for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
226 sp_object_ref (SP_OBJECT (child), object);
227 l = g_slist_prepend (l, child);
228 }
229 l = g_slist_reverse (l);
230 while (l) {
231 SPObject *child = SP_OBJECT (l->data);
232 l = g_slist_remove (l, child);
233 if (cflags || (child->uflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
234 /* fixme: Do we need transform? */
235 child->updateDisplay(ctx, cflags);
236 }
237 sp_object_unref (SP_OBJECT (child), object);
238 }
239 if (flags & ( SP_OBJECT_STYLE_MODIFIED_FLAG |
240 SP_OBJECT_CHILD_MODIFIED_FLAG |
241 SP_TEXT_LAYOUT_MODIFIED_FLAG ) )
242 {
243 /* fixme: It is not nice to have it here, but otherwise children content changes does not work */
244 /* fixme: Even now it may not work, as we are delayed */
245 /* fixme: So check modification flag everywhere immediate state is used */
246 text->rebuildLayout();
248 NRRect paintbox;
249 sp_item_invoke_bbox(text, &paintbox, NR::identity(), TRUE);
250 for (SPItemView* v = text->display; v != NULL; v = v->next) {
251 text->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
252 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
253 // pass the bbox of the text object as paintbox (used for paintserver fills)
254 text->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
255 }
256 }
257 }
259 static void
260 sp_text_modified (SPObject *object, guint flags)
261 {
262 if (((SPObjectClass *) text_parent_class)->modified)
263 ((SPObjectClass *) text_parent_class)->modified (object, flags);
265 guint cflags = (flags & SP_OBJECT_MODIFIED_CASCADE);
266 if (flags & SP_OBJECT_MODIFIED_FLAG) cflags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
268 // FIXME: all that we need to do here is nr_arena_glyphs_[group_]set_style, to set the changed
269 // style, but there's no easy way to access the arena glyphs or glyph groups corresponding to a
270 // text object. Therefore we do here the same as in _update, that is, destroy all arena items
271 // and create new ones. This is probably quite wasteful.
272 if (flags & ( SP_OBJECT_STYLE_MODIFIED_FLAG )) {
273 SPText *text = SP_TEXT (object);
274 NRRect paintbox;
275 sp_item_invoke_bbox(text, &paintbox, NR::identity(), TRUE);
276 for (SPItemView* v = text->display; v != NULL; v = v->next) {
277 text->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
278 nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
279 text->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
280 }
281 }
283 /* Create temporary list of children */
284 GSList *l = NULL;
285 SPObject *child;
286 for (child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
287 sp_object_ref (SP_OBJECT (child), object);
288 l = g_slist_prepend (l, child);
289 }
290 l = g_slist_reverse (l);
291 while (l) {
292 child = SP_OBJECT (l->data);
293 l = g_slist_remove (l, child);
294 if (cflags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
295 child->emitModified(cflags);
296 }
297 sp_object_unref (SP_OBJECT (child), object);
298 }
299 }
301 static Inkscape::XML::Node *
302 sp_text_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
303 {
304 SPText *text = SP_TEXT (object);
306 if (flags & SP_OBJECT_WRITE_BUILD) {
307 Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
308 if (!repr)
309 repr = xml_doc->createElement("svg:text");
310 GSList *l = NULL;
311 for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
312 Inkscape::XML::Node *crepr = NULL;
313 if (SP_IS_STRING(child)) {
314 crepr = xml_doc->createTextNode(SP_STRING(child)->string.c_str());
315 } else {
316 crepr = child->updateRepr(NULL, flags);
317 }
318 if (crepr) l = g_slist_prepend (l, crepr);
319 }
320 while (l) {
321 repr->addChild((Inkscape::XML::Node *) l->data, NULL);
322 Inkscape::GC::release((Inkscape::XML::Node *) l->data);
323 l = g_slist_remove (l, l->data);
324 }
325 } else {
326 for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
327 if (SP_IS_STRING(child)) {
328 SP_OBJECT_REPR(child)->setContent(SP_STRING(child)->string.c_str());
329 } else {
330 child->updateRepr(flags);
331 }
332 }
333 }
335 text->attributes.writeTo(repr);
337 // deprecated attribute, but keep it around for backwards compatibility
338 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) {
339 Inkscape::SVGOStringStream os;
340 os << (text->style->line_height.value * 100.0) << "%";
341 SP_OBJECT_REPR(text)->setAttribute("sodipodi:linespacing", os.str().c_str());
342 }
343 else
344 SP_OBJECT_REPR(text)->setAttribute("sodipodi:linespacing", NULL);
346 if (((SPObjectClass *) (text_parent_class))->write)
347 ((SPObjectClass *) (text_parent_class))->write (object, repr, flags);
349 return repr;
350 }
352 static void
353 sp_text_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
354 {
355 SP_TEXT(item)->layout.getBoundingBox(bbox, transform);
357 // Add stroke width
358 SPStyle* style=SP_OBJECT_STYLE (item);
359 if (!style->stroke.isNone()) {
360 double const scale = expansion(transform);
361 if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
362 double const width = MAX(0.125, style->stroke_width.computed * scale);
363 if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
364 bbox->x0-=0.5*width;
365 bbox->x1+=0.5*width;
366 bbox->y0-=0.5*width;
367 bbox->y1+=0.5*width;
368 }
369 }
370 }
371 }
374 static NRArenaItem *
375 sp_text_show(SPItem *item, NRArena *arena, unsigned /* key*/, unsigned /*flags*/)
376 {
377 SPText *group = (SPText *) item;
379 NRArenaGroup *flowed = NRArenaGroup::create(arena);
380 nr_arena_group_set_transparent (flowed, FALSE);
382 nr_arena_group_set_style(flowed, group->style);
384 // pass the bbox of the text object as paintbox (used for paintserver fills)
385 NRRect paintbox;
386 sp_item_invoke_bbox(item, &paintbox, NR::identity(), TRUE);
387 group->layout.show(flowed, &paintbox);
389 return flowed;
390 }
392 static void
393 sp_text_hide(SPItem *item, unsigned key)
394 {
395 if (((SPItemClass *) text_parent_class)->hide)
396 ((SPItemClass *) text_parent_class)->hide (item, key);
397 }
399 static char *
400 sp_text_description(SPItem *item)
401 {
402 SPText *text = (SPText *) item;
403 SPStyle *style = SP_OBJECT_STYLE(text);
405 font_instance *tf = (font_factory::Default())->Face(style->text->font_family.value,
406 font_style_to_pos(*style));
407 char name_buf[256];
408 char *n;
409 if (tf) {
410 tf->Name(name_buf, sizeof(name_buf));
411 n = xml_quote_strdup(name_buf);
412 tf->Unref();
413 } else {
414 /* TRANSLATORS: For description of font with no name. */
415 n = g_strdup(_("<no name found>"));
416 }
418 GString *xs = SP_PX_TO_METRIC_STRING(style->font_size.computed, sp_desktop_namedview(SP_ACTIVE_DESKTOP)->getDefaultMetric());
420 char *ret = ( SP_IS_TEXT_TEXTPATH(item)
421 ? g_strdup_printf(_("<b>Text on path</b> (%s, %s)"), n, xs->str)
422 : g_strdup_printf(_("<b>Text</b> (%s, %s)"), n, xs->str) );
423 g_free(n);
424 return ret;
425 }
427 static void sp_text_snappoints(SPItem const *item, SnapPointsIter p)
428 {
429 // the baseline anchor of the first char
430 Inkscape::Text::Layout const *layout = te_get_layout((SPItem *) item);
431 if(layout != NULL) {
432 *p = layout->characterAnchorPoint(layout->begin()) * sp_item_i2d_affine(item);
433 }
434 }
436 static NR::Matrix
437 sp_text_set_transform (SPItem *item, NR::Matrix const &xform)
438 {
439 SPText *text = SP_TEXT(item);
441 // we cannot optimize textpath because changing its fontsize will break its match to the path
442 if (SP_IS_TEXT_TEXTPATH (text))
443 return xform;
445 /* This function takes care of scaling & translation only, we return whatever parts we can't
446 handle. */
448 // TODO: pjrm tried to use fontsize_expansion(xform) here and it works for text in that font size
449 // is scaled more intuitively when scaling non-uniformly; however this necessitated using
450 // fontsize_expansion instead of expansion in other places too, where it was not appropriate
451 // (e.g. it broke stroke width on copy/pasting of style from horizontally stretched to vertically
452 // stretched shape). Using fontsize_expansion only here broke setting the style via font
453 // dialog. This needs to be investigated further.
454 double const ex = NR::expansion(xform);
455 if (ex == 0) {
456 return xform;
457 }
459 NR::Matrix ret(NR::transform(xform));
460 ret[0] /= ex;
461 ret[1] /= ex;
462 ret[2] /= ex;
463 ret[3] /= ex;
465 // Adjust x/y, dx/dy
466 text->_adjustCoordsRecursive (item, xform * ret.inverse(), ex);
468 // Adjust font size
469 text->_adjustFontsizeRecursive (item, ex);
471 // Adjust stroke width
472 sp_item_adjust_stroke_width_recursive (item, ex);
474 // Adjust pattern fill
475 sp_item_adjust_pattern(item, xform * ret.inverse());
477 // Adjust gradient fill
478 sp_item_adjust_gradient(item, xform * ret.inverse());
480 item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_TEXT_LAYOUT_MODIFIED_FLAG);
482 return ret;
483 }
485 static void
486 sp_text_print (SPItem *item, SPPrintContext *ctx)
487 {
488 NRRect pbox, dbox, bbox;
489 SPText *group = SP_TEXT (item);
491 sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
492 sp_item_bbox_desktop (item, &bbox);
493 dbox.x0 = 0.0;
494 dbox.y0 = 0.0;
495 dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
496 dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
497 NR::Matrix const ctm = sp_item_i2d_affine(item);
499 group->layout.print(ctx,&pbox,&dbox,&bbox,ctm);
500 }
502 /*
503 * Member functions
504 */
506 unsigned SPText::_buildLayoutInput(SPObject *root, Inkscape::Text::Layout::OptionalTextTagAttrs const &parent_optional_attrs, unsigned parent_attrs_offset, bool in_textpath)
507 {
508 unsigned length = 0;
509 int child_attrs_offset = 0;
510 Inkscape::Text::Layout::OptionalTextTagAttrs optional_attrs;
512 if (SP_IS_TEXT(root)) {
513 SP_TEXT(root)->attributes.mergeInto(&optional_attrs, parent_optional_attrs, parent_attrs_offset, true, true);
514 }
515 else if (SP_IS_TSPAN(root)) {
516 SPTSpan *tspan = SP_TSPAN(root);
517 bool use_xy = !in_textpath && (tspan->role == SP_TSPAN_ROLE_UNSPECIFIED || !tspan->attributes.singleXYCoordinates());
518 tspan->attributes.mergeInto(&optional_attrs, parent_optional_attrs, parent_attrs_offset, use_xy, true);
519 }
520 else if (SP_IS_TREF(root)) {
521 SP_TREF(root)->attributes.mergeInto(&optional_attrs, parent_optional_attrs, parent_attrs_offset, true, true);
522 }
523 else if (SP_IS_TEXTPATH(root)) {
524 in_textpath = true;
525 SP_TEXTPATH(root)->attributes.mergeInto(&optional_attrs, parent_optional_attrs, parent_attrs_offset, false, true);
526 optional_attrs.x.clear();
527 optional_attrs.y.clear();
528 }
529 else {
530 optional_attrs = parent_optional_attrs;
531 child_attrs_offset = parent_attrs_offset;
532 }
534 if (SP_IS_TSPAN(root))
535 if (SP_TSPAN(root)->role != SP_TSPAN_ROLE_UNSPECIFIED) {
536 // we need to allow the first line not to have role=line, but still set the source_cookie to the right value
537 SPObject *prev_object = SP_OBJECT_PREV(root);
538 if (prev_object && SP_IS_TSPAN(prev_object)) {
539 if (!layout.inputExists())
540 layout.appendText("", prev_object->style, prev_object, &optional_attrs);
541 layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, prev_object);
542 }
543 if (!root->hasChildren())
544 layout.appendText("", root->style, root, &optional_attrs);
545 length++; // interpreting line breaks as a character for the purposes of x/y/etc attributes
546 // is a liberal interpretation of the svg spec, but a strict reading would mean
547 // that if the first line is empty the second line would take its place at the
548 // start position. Very confusing.
549 child_attrs_offset--;
550 }
552 for (SPObject *child = sp_object_first_child(root) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
553 if (SP_IS_STRING(child)) {
554 Glib::ustring const &string = SP_STRING(child)->string;
555 layout.appendText(string, root->style, child, &optional_attrs, child_attrs_offset + length);
556 length += string.length();
557 } else {
558 length += _buildLayoutInput(child, optional_attrs, child_attrs_offset + length, in_textpath);
559 }
560 }
562 return length;
563 }
565 void SPText::rebuildLayout()
566 {
567 layout.clear();
568 Inkscape::Text::Layout::OptionalTextTagAttrs optional_attrs;
569 _buildLayoutInput(this, optional_attrs, 0, false);
570 layout.calculateFlow();
571 for (SPObject *child = firstChild() ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
572 if (SP_IS_TEXTPATH(child)) {
573 SPTextPath const *textpath = SP_TEXTPATH(child);
574 if (textpath->originalPath != NULL) {
575 //g_print(layout.dumpAsText().c_str());
576 layout.fitToPathAlign(textpath->startOffset, *textpath->originalPath);
577 }
578 }
579 }
580 //g_print(layout.dumpAsText().c_str());
582 // set the x,y attributes on role:line spans
583 for (SPObject *child = firstChild() ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
584 if (!SP_IS_TSPAN(child)) continue;
585 SPTSpan *tspan = SP_TSPAN(child);
586 if (tspan->role == SP_TSPAN_ROLE_UNSPECIFIED) continue;
587 if (!tspan->attributes.singleXYCoordinates()) continue;
588 Inkscape::Text::Layout::iterator iter = layout.sourceToIterator(tspan);
589 NR::Point anchor_point = layout.chunkAnchorPoint(iter);
590 tspan->attributes.setFirstXY(anchor_point);
591 }
592 }
595 void SPText::_adjustFontsizeRecursive(SPItem *item, double ex, bool is_root)
596 {
597 SPStyle *style = SP_OBJECT_STYLE (item);
599 if (style && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
600 if (!style->font_size.set && is_root) {
601 style->font_size.set = 1;
602 style->font_size.type = SP_FONT_SIZE_LENGTH;
603 }
604 style->font_size.computed *= ex;
605 style->letter_spacing.computed *= ex;
606 style->word_spacing.computed *= ex;
607 item->updateRepr();
608 }
610 for (SPObject *o = item->children; o != NULL; o = o->next) {
611 if (SP_IS_ITEM(o))
612 _adjustFontsizeRecursive(SP_ITEM(o), ex, false);
613 }
614 }
616 void SPText::_adjustCoordsRecursive(SPItem *item, NR::Matrix const &m, double ex, bool is_root)
617 {
618 if (SP_IS_TSPAN(item))
619 SP_TSPAN(item)->attributes.transform(m, ex, ex, is_root);
620 // it doesn't matter if we change the x,y for role=line spans because we'll just overwrite them anyway
621 else if (SP_IS_TEXT(item))
622 SP_TEXT(item)->attributes.transform(m, ex, ex, is_root);
623 else if (SP_IS_TEXTPATH(item))
624 SP_TEXTPATH(item)->attributes.transform(m, ex, ex, is_root);
625 else if (SP_IS_TREF(item)) {
626 SP_TREF(item)->attributes.transform(m, ex, ex, is_root);
627 }
629 for (SPObject *o = item->children; o != NULL; o = o->next) {
630 if (SP_IS_ITEM(o))
631 _adjustCoordsRecursive(SP_ITEM(o), m, ex, false);
632 }
633 }
636 void SPText::_clearFlow(NRArenaGroup *in_arena)
637 {
638 nr_arena_item_request_render (in_arena);
639 for (NRArenaItem *child = in_arena->children; child != NULL; ) {
640 NRArenaItem *nchild = child->next;
642 nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
643 nr_arena_item_remove_child (in_arena, child);
645 child=nchild;
646 }
647 }
650 /*
651 * TextTagAttributes implementation
652 */
654 void TextTagAttributes::readFrom(Inkscape::XML::Node const *node)
655 {
656 readSingleAttribute(SP_ATTR_X, node->attribute("x"));
657 readSingleAttribute(SP_ATTR_Y, node->attribute("y"));
658 readSingleAttribute(SP_ATTR_DX, node->attribute("dx"));
659 readSingleAttribute(SP_ATTR_DY, node->attribute("dy"));
660 readSingleAttribute(SP_ATTR_ROTATE, node->attribute("rotate"));
661 }
663 bool TextTagAttributes::readSingleAttribute(unsigned key, gchar const *value)
664 {
665 std::vector<SVGLength> *attr_vector;
666 switch (key) {
667 case SP_ATTR_X: attr_vector = &attributes.x; break;
668 case SP_ATTR_Y: attr_vector = &attributes.y; break;
669 case SP_ATTR_DX: attr_vector = &attributes.dx; break;
670 case SP_ATTR_DY: attr_vector = &attributes.dy; break;
671 case SP_ATTR_ROTATE: attr_vector = &attributes.rotate; break;
672 default: return false;
673 }
675 // FIXME: sp_svg_length_list_read() amalgamates repeated separators. This prevents unset values.
676 *attr_vector = sp_svg_length_list_read(value);
677 return true;
678 }
680 void TextTagAttributes::writeTo(Inkscape::XML::Node *node) const
681 {
682 writeSingleAttribute(node, "x", attributes.x);
683 writeSingleAttribute(node, "y", attributes.y);
684 writeSingleAttribute(node, "dx", attributes.dx);
685 writeSingleAttribute(node, "dy", attributes.dy);
686 writeSingleAttribute(node, "rotate", attributes.rotate);
687 }
689 void TextTagAttributes::writeSingleAttribute(Inkscape::XML::Node *node, gchar const *key, std::vector<SVGLength> const &attr_vector)
690 {
691 if (attr_vector.empty())
692 node->setAttribute(key, NULL);
693 else {
694 Glib::ustring string;
695 gchar single_value_string[32];
697 // FIXME: this has no concept of unset values because sp_svg_length_list_read() can't read them back in
698 for (std::vector<SVGLength>::const_iterator it = attr_vector.begin() ; it != attr_vector.end() ; it++) {
699 g_ascii_formatd(single_value_string, sizeof (single_value_string), "%.8g", it->computed);
700 if (!string.empty()) string += ' ';
701 string += single_value_string;
702 }
703 node->setAttribute(key, string.c_str());
704 }
705 }
707 bool TextTagAttributes::singleXYCoordinates() const
708 {
709 return attributes.x.size() <= 1 && attributes.y.size() <= 1;
710 }
712 bool TextTagAttributes::anyAttributesSet() const
713 {
714 return !attributes.x.empty() || !attributes.y.empty() || !attributes.dx.empty() || !attributes.dy.empty() || !attributes.rotate.empty();
715 }
717 NR::Point TextTagAttributes::firstXY() const
718 {
719 NR::Point point;
720 if (attributes.x.empty()) point[NR::X] = 0.0;
721 else point[NR::X] = attributes.x[0].computed;
722 if (attributes.y.empty()) point[NR::Y] = 0.0;
723 else point[NR::Y] = attributes.y[0].computed;
724 return point;
725 }
727 void TextTagAttributes::setFirstXY(NR::Point &point)
728 {
729 SVGLength zero_length;
730 zero_length = 0.0;
732 if (attributes.x.empty())
733 attributes.x.resize(1, zero_length);
734 if (attributes.y.empty())
735 attributes.y.resize(1, zero_length);
736 attributes.x[0].computed = point[NR::X];
737 attributes.y[0].computed = point[NR::Y];
738 }
740 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
741 {
742 mergeSingleAttribute(&output->x, parent_attrs.x, parent_attrs_offset, copy_xy ? &attributes.x : NULL);
743 mergeSingleAttribute(&output->y, parent_attrs.y, parent_attrs_offset, copy_xy ? &attributes.y : NULL);
744 mergeSingleAttribute(&output->dx, parent_attrs.dx, parent_attrs_offset, copy_dxdyrotate ? &attributes.dx : NULL);
745 mergeSingleAttribute(&output->dy, parent_attrs.dy, parent_attrs_offset, copy_dxdyrotate ? &attributes.dy : NULL);
746 mergeSingleAttribute(&output->rotate, parent_attrs.rotate, parent_attrs_offset, copy_dxdyrotate ? &attributes.rotate : NULL);
747 }
749 void TextTagAttributes::mergeSingleAttribute(std::vector<SVGLength> *output_list, std::vector<SVGLength> const &parent_list, unsigned parent_offset, std::vector<SVGLength> const *overlay_list)
750 {
751 if (overlay_list == NULL) {
752 output_list->resize(std::max(0, (int)parent_list.size() - (int)parent_offset));
753 std::copy(parent_list.begin() + parent_offset, parent_list.end(), output_list->begin());
754 } else {
755 output_list->clear();
756 output_list->reserve(std::max((int)parent_list.size() - (int)parent_offset, (int)overlay_list->size()));
757 unsigned overlay_offset = 0;
758 while (parent_offset < parent_list.size() || overlay_offset < overlay_list->size()) {
759 SVGLength const *this_item;
760 if (overlay_offset < overlay_list->size()) {
761 this_item = &(*overlay_list)[overlay_offset];
762 overlay_offset++;
763 parent_offset++;
764 } else {
765 this_item = &parent_list[parent_offset];
766 parent_offset++;
767 }
768 output_list->push_back(*this_item);
769 }
770 }
771 }
773 void TextTagAttributes::erase(unsigned start_index, unsigned n)
774 {
775 if (n == 0) return;
776 if (!singleXYCoordinates()) {
777 eraseSingleAttribute(&attributes.x, start_index, n);
778 eraseSingleAttribute(&attributes.y, start_index, n);
779 }
780 eraseSingleAttribute(&attributes.dx, start_index, n);
781 eraseSingleAttribute(&attributes.dy, start_index, n);
782 eraseSingleAttribute(&attributes.rotate, start_index, n);
783 }
785 void TextTagAttributes::eraseSingleAttribute(std::vector<SVGLength> *attr_vector, unsigned start_index, unsigned n)
786 {
787 if (attr_vector->size() <= start_index) return;
788 if (attr_vector->size() <= start_index + n)
789 attr_vector->erase(attr_vector->begin() + start_index, attr_vector->end());
790 else
791 attr_vector->erase(attr_vector->begin() + start_index, attr_vector->begin() + start_index + n);
792 }
794 void TextTagAttributes::insert(unsigned start_index, unsigned n)
795 {
796 if (n == 0) return;
797 if (!singleXYCoordinates()) {
798 insertSingleAttribute(&attributes.x, start_index, n, true);
799 insertSingleAttribute(&attributes.y, start_index, n, true);
800 }
801 insertSingleAttribute(&attributes.dx, start_index, n, false);
802 insertSingleAttribute(&attributes.dy, start_index, n, false);
803 insertSingleAttribute(&attributes.rotate, start_index, n, false);
804 }
806 void TextTagAttributes::insertSingleAttribute(std::vector<SVGLength> *attr_vector, unsigned start_index, unsigned n, bool is_xy)
807 {
808 if (attr_vector->size() <= start_index) return;
809 SVGLength zero_length;
810 zero_length = 0.0;
811 attr_vector->insert(attr_vector->begin() + start_index, n, zero_length);
812 if (is_xy) {
813 double begin = start_index == 0 ? (*attr_vector)[start_index + n].computed : (*attr_vector)[start_index - 1].computed;
814 double diff = ((*attr_vector)[start_index + n].computed - begin) / n; // n tested for nonzero in insert()
815 for (unsigned i = 0 ; i < n ; i++)
816 (*attr_vector)[start_index + i] = begin + diff * i;
817 }
818 }
820 void TextTagAttributes::split(unsigned index, TextTagAttributes *second)
821 {
822 if (!singleXYCoordinates()) {
823 splitSingleAttribute(&attributes.x, index, &second->attributes.x, false);
824 splitSingleAttribute(&attributes.y, index, &second->attributes.y, false);
825 }
826 splitSingleAttribute(&attributes.dx, index, &second->attributes.dx, true);
827 splitSingleAttribute(&attributes.dy, index, &second->attributes.dy, true);
828 splitSingleAttribute(&attributes.rotate, index, &second->attributes.rotate, true);
829 }
831 void TextTagAttributes::splitSingleAttribute(std::vector<SVGLength> *first_vector, unsigned index, std::vector<SVGLength> *second_vector, bool trimZeros)
832 {
833 second_vector->clear();
834 if (first_vector->size() <= index) return;
835 second_vector->resize(first_vector->size() - index);
836 std::copy(first_vector->begin() + index, first_vector->end(), second_vector->begin());
837 first_vector->resize(index);
838 if (trimZeros)
839 while (!first_vector->empty() && (!first_vector->back()._set || first_vector->back().value == 0.0))
840 first_vector->resize(first_vector->size() - 1);
841 }
843 void TextTagAttributes::join(TextTagAttributes const &first, TextTagAttributes const &second, unsigned second_index)
844 {
845 if (second.singleXYCoordinates()) {
846 attributes.x = first.attributes.x;
847 attributes.y = first.attributes.y;
848 } else {
849 joinSingleAttribute(&attributes.x, first.attributes.x, second.attributes.x, second_index);
850 joinSingleAttribute(&attributes.y, first.attributes.y, second.attributes.y, second_index);
851 }
852 joinSingleAttribute(&attributes.dx, first.attributes.dx, second.attributes.dx, second_index);
853 joinSingleAttribute(&attributes.dy, first.attributes.dy, second.attributes.dy, second_index);
854 joinSingleAttribute(&attributes.rotate, first.attributes.rotate, second.attributes.rotate, second_index);
855 }
857 void TextTagAttributes::joinSingleAttribute(std::vector<SVGLength> *dest_vector, std::vector<SVGLength> const &first_vector, std::vector<SVGLength> const &second_vector, unsigned second_index)
858 {
859 if (second_vector.empty())
860 *dest_vector = first_vector;
861 else {
862 dest_vector->resize(second_index + second_vector.size());
863 if (first_vector.size() < second_index) {
864 std::copy(first_vector.begin(), first_vector.end(), dest_vector->begin());
865 SVGLength zero_length;
866 zero_length = 0.0;
867 std::fill(dest_vector->begin() + first_vector.size(), dest_vector->begin() + second_index, zero_length);
868 } else
869 std::copy(first_vector.begin(), first_vector.begin() + second_index, dest_vector->begin());
870 std::copy(second_vector.begin(), second_vector.end(), dest_vector->begin() + second_index);
871 }
872 }
874 void TextTagAttributes::transform(NR::Matrix const &matrix, double scale_x, double scale_y, bool extend_zero_length)
875 {
876 SVGLength zero_length;
877 zero_length = 0.0;
879 /* edge testcases for this code:
880 1) moving text elements whose position is done entirely with transform="...", no x,y attributes
881 2) unflowing multi-line flowtext then moving it (it has x but not y)
882 */
883 unsigned points_count = std::max(attributes.x.size(), attributes.y.size());
884 if (extend_zero_length && points_count < 1)
885 points_count = 1;
886 for (unsigned i = 0 ; i < points_count ; i++) {
887 NR::Point point;
888 if (i < attributes.x.size()) point[NR::X] = attributes.x[i].computed;
889 else point[NR::X] = 0.0;
890 if (i < attributes.y.size()) point[NR::Y] = attributes.y[i].computed;
891 else point[NR::Y] = 0.0;
892 point *= matrix;
893 if (i < attributes.x.size())
894 attributes.x[i] = point[NR::X];
895 else if (point[NR::X] != 0.0 && extend_zero_length) {
896 attributes.x.resize(i + 1, zero_length);
897 attributes.x[i] = point[NR::X];
898 }
899 if (i < attributes.y.size())
900 attributes.y[i] = point[NR::Y];
901 else if (point[NR::Y] != 0.0 && extend_zero_length) {
902 attributes.y.resize(i + 1, zero_length);
903 attributes.y[i] = point[NR::Y];
904 }
905 }
906 for (std::vector<SVGLength>::iterator it = attributes.dx.begin() ; it != attributes.dx.end() ; it++)
907 *it = it->computed * scale_x;
908 for (std::vector<SVGLength>::iterator it = attributes.dy.begin() ; it != attributes.dy.end() ; it++)
909 *it = it->computed * scale_y;
910 }
912 void TextTagAttributes::addToDxDy(unsigned index, NR::Point const &adjust)
913 {
914 SVGLength zero_length;
915 zero_length = 0.0;
917 if (adjust[NR::X] != 0.0) {
918 if (attributes.dx.size() < index + 1) attributes.dx.resize(index + 1, zero_length);
919 attributes.dx[index] = attributes.dx[index].computed + adjust[NR::X];
920 }
921 if (adjust[NR::Y] != 0.0) {
922 if (attributes.dy.size() < index + 1) attributes.dy.resize(index + 1, zero_length);
923 attributes.dy[index] = attributes.dy[index].computed + adjust[NR::Y];
924 }
925 }
927 void TextTagAttributes::addToRotate(unsigned index, double delta)
928 {
929 SVGLength zero_length;
930 zero_length = 0.0;
932 if (attributes.rotate.size() < index + 1) {
933 if (attributes.rotate.empty())
934 attributes.rotate.resize(index + 1, zero_length);
935 else
936 attributes.rotate.resize(index + 1, attributes.rotate.back());
937 }
938 attributes.rotate[index] = mod360(attributes.rotate[index].computed + delta);
939 }
942 /*
943 Local Variables:
944 mode:c++
945 c-file-style:"stroustrup"
946 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
947 indent-tabs-mode:nil
948 fill-column:99
949 End:
950 */
951 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :