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