Code

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