Code

bug 1243190: add tref element support; limited editing support thus far (patch by...
[inkscape.git] / src / sp-text.cpp
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;
102 static void
103 sp_text_class_init (SPTextClass *classname)
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;
128 static void
129 sp_text_init (SPText *text)
131     new (&text->layout) Inkscape::Text::Layout;
132     new (&text->attributes) TextTagAttributes;
135 static void
136 sp_text_release (SPObject *object)
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);
146 static void
147 sp_text_build (SPObject *object, SPDocument *doc, Inkscape::XML::Node *repr)
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
161 static void
162 sp_text_set(SPObject *object, unsigned key, gchar const *value)
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     }
189 static void
190 sp_text_child_added (SPObject *object, Inkscape::XML::Node *rch, Inkscape::XML::Node *ref)
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);
200 static void
201 sp_text_remove_child (SPObject *object, Inkscape::XML::Node *rch)
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);
211 static void
212 sp_text_update (SPObject *object, SPCtx *ctx, guint flags)
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     }
259 static void
260 sp_text_modified (SPObject *object, guint flags)
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     }
301 static Inkscape::XML::Node *
302 sp_text_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
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;
352 static void
353 sp_text_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
355     SP_TEXT(item)->layout.getBoundingBox(bbox, transform);
357     // Add stroke width
358     SPStyle* style=SP_OBJECT_STYLE (item);
359     if (style->stroke.type != SP_PAINT_TYPE_NONE) {
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     }
374 static NRArenaItem *
375 sp_text_show(SPItem *item, NRArena *arena, unsigned /* key*/, unsigned /*flags*/)
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;
392 static void
393 sp_text_hide(SPItem *item, unsigned key)
395     if (((SPItemClass *) text_parent_class)->hide)
396         ((SPItemClass *) text_parent_class)->hide (item, key);
399 static char *
400 sp_text_description(SPItem *item)
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(_("&lt;no name found&gt;"));
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;
427 static void sp_text_snappoints(SPItem const *item, SnapPointsIter p)
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     }
436 static NR::Matrix
437 sp_text_set_transform (SPItem *item, NR::Matrix const &xform)
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;
485 static void
486 sp_text_print (SPItem *item, SPPrintContext *ctx)
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);
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)
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;
565 void SPText::rebuildLayout()
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         sp_repr_set_svg_double(SP_OBJECT_REPR(tspan), "x", anchor_point[NR::X]);
591         sp_repr_set_svg_double(SP_OBJECT_REPR(tspan), "y", anchor_point[NR::Y]);
592     }
596 void SPText::_adjustFontsizeRecursive(SPItem *item, double ex, bool is_root)
598     SPStyle *style = SP_OBJECT_STYLE (item);
600     if (style && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
601         if (!style->font_size.set && is_root) {
602             style->font_size.set = 1;
603             style->font_size.type = SP_FONT_SIZE_LENGTH;
604         }
605         style->font_size.computed *= ex;
606         style->letter_spacing.computed *= ex;
607         style->word_spacing.computed *= ex;
608         item->updateRepr();
609     }
611     for (SPObject *o = item->children; o != NULL; o = o->next) {
612         if (SP_IS_ITEM(o))
613             _adjustFontsizeRecursive(SP_ITEM(o), ex, false);
614     }
617 void SPText::_adjustCoordsRecursive(SPItem *item, NR::Matrix const &m, double ex, bool is_root)
619     if (SP_IS_TSPAN(item))
620         SP_TSPAN(item)->attributes.transform(m, ex, ex, is_root);
621               // it doesn't matter if we change the x,y for role=line spans because we'll just overwrite them anyway
622     else if (SP_IS_TEXT(item))
623         SP_TEXT(item)->attributes.transform(m, ex, ex, is_root);
624     else if (SP_IS_TEXTPATH(item))
625         SP_TEXTPATH(item)->attributes.transform(m, ex, ex, is_root);
626     else if (SP_IS_TREF(item)) {
627         SP_TREF(item)->attributes.transform(m, ex, ex, is_root);
628     }
630     for (SPObject *o = item->children; o != NULL; o = o->next) {
631         if (SP_IS_ITEM(o))
632             _adjustCoordsRecursive(SP_ITEM(o), m, ex, false);
633     }
637 void SPText::_clearFlow(NRArenaGroup *in_arena)
639     nr_arena_item_request_render (in_arena);
640     for (NRArenaItem *child = in_arena->children; child != NULL; ) {
641         NRArenaItem *nchild = child->next;
643         nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
644         nr_arena_item_remove_child (in_arena, child);
646         child=nchild;
647     }
651 /*
652  * TextTagAttributes implementation
653  */
655 void TextTagAttributes::readFrom(Inkscape::XML::Node const *node)
657     readSingleAttribute(SP_ATTR_X, node->attribute("x"));
658     readSingleAttribute(SP_ATTR_Y, node->attribute("y"));
659     readSingleAttribute(SP_ATTR_DX, node->attribute("dx"));
660     readSingleAttribute(SP_ATTR_DY, node->attribute("dy"));
661     readSingleAttribute(SP_ATTR_ROTATE, node->attribute("rotate"));
664 bool TextTagAttributes::readSingleAttribute(unsigned key, gchar const *value)
666     std::vector<SVGLength> *attr_vector;
667     switch (key) {
668         case SP_ATTR_X:      attr_vector = &attributes.x; break;
669         case SP_ATTR_Y:      attr_vector = &attributes.y; break;
670         case SP_ATTR_DX:     attr_vector = &attributes.dx; break;
671         case SP_ATTR_DY:     attr_vector = &attributes.dy; break;
672         case SP_ATTR_ROTATE: attr_vector = &attributes.rotate; break;
673         default: return false;
674     }
676     // FIXME: sp_svg_length_list_read() amalgamates repeated separators. This prevents unset values.
677     *attr_vector = sp_svg_length_list_read(value);
678     return true;
681 void TextTagAttributes::writeTo(Inkscape::XML::Node *node) const
683     writeSingleAttribute(node, "x", attributes.x);
684     writeSingleAttribute(node, "y", attributes.y);
685     writeSingleAttribute(node, "dx", attributes.dx);
686     writeSingleAttribute(node, "dy", attributes.dy);
687     writeSingleAttribute(node, "rotate", attributes.rotate);
690 void TextTagAttributes::writeSingleAttribute(Inkscape::XML::Node *node, gchar const *key, std::vector<SVGLength> const &attr_vector)
692     if (attr_vector.empty())
693         node->setAttribute(key, NULL);
694     else {
695         Glib::ustring string;
696         gchar single_value_string[32];
698         // FIXME: this has no concept of unset values because sp_svg_length_list_read() can't read them back in
699         for (std::vector<SVGLength>::const_iterator it = attr_vector.begin() ; it != attr_vector.end() ; it++) {
700             g_ascii_formatd(single_value_string, sizeof (single_value_string), "%.8g", it->computed);
701             if (!string.empty()) string += ' ';
702             string += single_value_string;
703         }
704         node->setAttribute(key, string.c_str());
705     }
708 bool TextTagAttributes::singleXYCoordinates() const
710     return attributes.x.size() <= 1 && attributes.y.size() <= 1;
713 bool TextTagAttributes::anyAttributesSet() const
715     return !attributes.x.empty() || !attributes.y.empty() || !attributes.dx.empty() || !attributes.dy.empty() || !attributes.rotate.empty();
718 NR::Point TextTagAttributes::firstXY() const
720     NR::Point point;
721     if (attributes.x.empty()) point[NR::X] = 0.0;
722     else point[NR::X] = attributes.x[0].computed;
723     if (attributes.y.empty()) point[NR::Y] = 0.0;
724     else point[NR::Y] = attributes.y[0].computed;
725     return point;
728 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
730     mergeSingleAttribute(&output->x,      parent_attrs.x,      parent_attrs_offset, copy_xy ? &attributes.x : NULL);
731     mergeSingleAttribute(&output->y,      parent_attrs.y,      parent_attrs_offset, copy_xy ? &attributes.y : NULL);
732     mergeSingleAttribute(&output->dx,     parent_attrs.dx,     parent_attrs_offset, copy_dxdyrotate ? &attributes.dx : NULL);
733     mergeSingleAttribute(&output->dy,     parent_attrs.dy,     parent_attrs_offset, copy_dxdyrotate ? &attributes.dy : NULL);
734     mergeSingleAttribute(&output->rotate, parent_attrs.rotate, parent_attrs_offset, copy_dxdyrotate ? &attributes.rotate : NULL);
737 void TextTagAttributes::mergeSingleAttribute(std::vector<SVGLength> *output_list, std::vector<SVGLength> const &parent_list, unsigned parent_offset, std::vector<SVGLength> const *overlay_list)
739     if (overlay_list == NULL) {
740         output_list->resize(std::max(0, (int)parent_list.size() - (int)parent_offset));
741         std::copy(parent_list.begin() + parent_offset, parent_list.end(), output_list->begin());
742     } else {
743         output_list->clear();
744         output_list->reserve(std::max((int)parent_list.size() - (int)parent_offset, (int)overlay_list->size()));
745         unsigned overlay_offset = 0;
746         while (parent_offset < parent_list.size() || overlay_offset < overlay_list->size()) {
747             SVGLength const *this_item;
748             if (overlay_offset < overlay_list->size()) {
749                 this_item = &(*overlay_list)[overlay_offset];
750                 overlay_offset++;
751                 parent_offset++;
752             } else {
753                 this_item = &parent_list[parent_offset];
754                 parent_offset++;
755             }
756             output_list->push_back(*this_item);
757         }
758     }
761 void TextTagAttributes::erase(unsigned start_index, unsigned n)
763     if (n == 0) return;
764     if (!singleXYCoordinates()) {
765         eraseSingleAttribute(&attributes.x, start_index, n);
766         eraseSingleAttribute(&attributes.y, start_index, n);
767     }
768     eraseSingleAttribute(&attributes.dx, start_index, n);
769     eraseSingleAttribute(&attributes.dy, start_index, n);
770     eraseSingleAttribute(&attributes.rotate, start_index, n);
773 void TextTagAttributes::eraseSingleAttribute(std::vector<SVGLength> *attr_vector, unsigned start_index, unsigned n)
775     if (attr_vector->size() <= start_index) return;
776     if (attr_vector->size() <= start_index + n)
777         attr_vector->erase(attr_vector->begin() + start_index, attr_vector->end());
778     else
779         attr_vector->erase(attr_vector->begin() + start_index, attr_vector->begin() + start_index + n);
782 void TextTagAttributes::insert(unsigned start_index, unsigned n)
784     if (n == 0) return;
785     if (!singleXYCoordinates()) {
786         insertSingleAttribute(&attributes.x, start_index, n, true);
787         insertSingleAttribute(&attributes.y, start_index, n, true);
788     }
789     insertSingleAttribute(&attributes.dx, start_index, n, false);
790     insertSingleAttribute(&attributes.dy, start_index, n, false);
791     insertSingleAttribute(&attributes.rotate, start_index, n, false);
794 void TextTagAttributes::insertSingleAttribute(std::vector<SVGLength> *attr_vector, unsigned start_index, unsigned n, bool is_xy)
796     if (attr_vector->size() <= start_index) return;
797     SVGLength zero_length;
798     zero_length = 0.0;
799     attr_vector->insert(attr_vector->begin() + start_index, n, zero_length);
800     if (is_xy) {
801         double begin = start_index == 0 ? (*attr_vector)[start_index + n].computed : (*attr_vector)[start_index - 1].computed;
802         double diff = ((*attr_vector)[start_index + n].computed - begin) / n;   // n tested for nonzero in insert()
803         for (unsigned i = 0 ; i < n ; i++)
804             (*attr_vector)[start_index + i] = begin + diff * i;
805     }
808 void TextTagAttributes::split(unsigned index, TextTagAttributes *second)
810     if (!singleXYCoordinates()) {
811         splitSingleAttribute(&attributes.x, index, &second->attributes.x, false);
812         splitSingleAttribute(&attributes.y, index, &second->attributes.y, false);
813     }
814     splitSingleAttribute(&attributes.dx, index, &second->attributes.dx, true);
815     splitSingleAttribute(&attributes.dy, index, &second->attributes.dy, true);
816     splitSingleAttribute(&attributes.rotate, index, &second->attributes.rotate, true);
819 void TextTagAttributes::splitSingleAttribute(std::vector<SVGLength> *first_vector, unsigned index, std::vector<SVGLength> *second_vector, bool trimZeros)
821     second_vector->clear();
822     if (first_vector->size() <= index) return;
823     second_vector->resize(first_vector->size() - index);
824     std::copy(first_vector->begin() + index, first_vector->end(), second_vector->begin());
825     first_vector->resize(index);
826     if (trimZeros)
827         while (!first_vector->empty() && (!first_vector->back()._set || first_vector->back().value == 0.0))
828             first_vector->resize(first_vector->size() - 1);
831 void TextTagAttributes::join(TextTagAttributes const &first, TextTagAttributes const &second, unsigned second_index)
833     if (second.singleXYCoordinates()) {
834         attributes.x = first.attributes.x;
835         attributes.y = first.attributes.y;
836     } else {
837         joinSingleAttribute(&attributes.x, first.attributes.x, second.attributes.x, second_index);
838         joinSingleAttribute(&attributes.y, first.attributes.y, second.attributes.y, second_index);
839     }
840     joinSingleAttribute(&attributes.dx, first.attributes.dx, second.attributes.dx, second_index);
841     joinSingleAttribute(&attributes.dy, first.attributes.dy, second.attributes.dy, second_index);
842     joinSingleAttribute(&attributes.rotate, first.attributes.rotate, second.attributes.rotate, second_index);
845 void TextTagAttributes::joinSingleAttribute(std::vector<SVGLength> *dest_vector, std::vector<SVGLength> const &first_vector, std::vector<SVGLength> const &second_vector, unsigned second_index)
847     if (second_vector.empty())
848         *dest_vector = first_vector;
849     else {
850         dest_vector->resize(second_index + second_vector.size());
851         if (first_vector.size() < second_index) {
852             std::copy(first_vector.begin(), first_vector.end(), dest_vector->begin());
853             SVGLength zero_length;
854             zero_length = 0.0;
855             std::fill(dest_vector->begin() + first_vector.size(), dest_vector->begin() + second_index, zero_length);
856         } else
857             std::copy(first_vector.begin(), first_vector.begin() + second_index, dest_vector->begin());
858         std::copy(second_vector.begin(), second_vector.end(), dest_vector->begin() + second_index);
859     }
862 void TextTagAttributes::transform(NR::Matrix const &matrix, double scale_x, double scale_y, bool extend_zero_length)
864     SVGLength zero_length;
865     zero_length = 0.0;
867     /* edge testcases for this code:
868        1) moving text elements whose position is done entirely with transform="...", no x,y attributes
869        2) unflowing multi-line flowtext then moving it (it has x but not y)
870     */
871     unsigned points_count = std::max(attributes.x.size(), attributes.y.size());
872     if (extend_zero_length && points_count < 1)
873         points_count = 1;
874     for (unsigned i = 0 ; i < points_count ; i++) {
875         NR::Point point;
876         if (i < attributes.x.size()) point[NR::X] = attributes.x[i].computed;
877         else point[NR::X] = 0.0;
878         if (i < attributes.y.size()) point[NR::Y] = attributes.y[i].computed;
879         else point[NR::Y] = 0.0;
880         point *= matrix;
881         if (i < attributes.x.size())
882             attributes.x[i] = point[NR::X];
883         else if (point[NR::X] != 0.0 && extend_zero_length) {
884             attributes.x.resize(i + 1, zero_length);
885             attributes.x[i] = point[NR::X];
886         }
887         if (i < attributes.y.size())
888             attributes.y[i] = point[NR::Y];
889         else if (point[NR::Y] != 0.0 && extend_zero_length) {
890             attributes.y.resize(i + 1, zero_length);
891             attributes.y[i] = point[NR::Y];
892         }
893     }
894     for (std::vector<SVGLength>::iterator it = attributes.dx.begin() ; it != attributes.dx.end() ; it++)
895         *it = it->computed * scale_x;
896     for (std::vector<SVGLength>::iterator it = attributes.dy.begin() ; it != attributes.dy.end() ; it++)
897         *it = it->computed * scale_y;
900 void TextTagAttributes::addToDxDy(unsigned index, NR::Point const &adjust)
902     SVGLength zero_length;
903     zero_length = 0.0;
905     if (adjust[NR::X] != 0.0) {
906         if (attributes.dx.size() < index + 1) attributes.dx.resize(index + 1, zero_length);
907         attributes.dx[index] = attributes.dx[index].computed + adjust[NR::X];
908     }
909     if (adjust[NR::Y] != 0.0) {
910         if (attributes.dy.size() < index + 1) attributes.dy.resize(index + 1, zero_length);
911         attributes.dy[index] = attributes.dy[index].computed + adjust[NR::Y];
912     }
915 void TextTagAttributes::addToRotate(unsigned index, double delta)
917     SVGLength zero_length;
918     zero_length = 0.0;
920     if (attributes.rotate.size() < index + 1) {
921         if (attributes.rotate.empty())
922             attributes.rotate.resize(index + 1, zero_length);
923         else
924             attributes.rotate.resize(index + 1, attributes.rotate.back());
925     }
926     attributes.rotate[index] = mod360(attributes.rotate[index].computed + delta);
930 /*
931   Local Variables:
932   mode:c++
933   c-file-style:"stroustrup"
934   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
935   indent-tabs-mode:nil
936   fill-column:99
937   End:
938 */
939 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :