Code

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