Code

excise never-used code and stale comments
[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     /* Create temporary list of children */
267     GSList *l = NULL;
268     SPObject *child;
269     for (child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
270         sp_object_ref (SP_OBJECT (child), object);
271         l = g_slist_prepend (l, child);
272     }
273     l = g_slist_reverse (l);
274     while (l) {
275         child = SP_OBJECT (l->data);
276         l = g_slist_remove (l, child);
277         if (cflags || (child->mflags & (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_CHILD_MODIFIED_FLAG))) {
278             child->emitModified(cflags);
279         }
280         sp_object_unref (SP_OBJECT (child), object);
281     }
284 static Inkscape::XML::Node *
285 sp_text_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
287     SPText *text = SP_TEXT (object);
289     if (flags & SP_OBJECT_WRITE_BUILD) {
290         if (!repr)
291             repr = sp_repr_new ("svg:text");
292         GSList *l = NULL;
293         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
294             Inkscape::XML::Node *crepr = NULL;
295             if (SP_IS_STRING(child)) {
296                 crepr = sp_repr_new_text(SP_STRING(child)->string.c_str());
297             } else {
298                 crepr = child->updateRepr(NULL, flags);
299             }
300             if (crepr) l = g_slist_prepend (l, crepr);
301         }
302         while (l) {
303             repr->addChild((Inkscape::XML::Node *) l->data, NULL);
304             Inkscape::GC::release((Inkscape::XML::Node *) l->data);
305             l = g_slist_remove (l, l->data);
306         }
307     } else {
308         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
309             if (SP_IS_STRING(child)) {
310                 SP_OBJECT_REPR(child)->setContent(SP_STRING(child)->string.c_str());
311             } else {
312                 child->updateRepr(flags);
313             }
314         }
315     }
317     text->attributes.writeTo(repr);
319     // deprecated attribute, but keep it around for backwards compatibility
320     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) {
321         Inkscape::SVGOStringStream os;
322         os << (text->style->line_height.value * 100.0) << "%";
323         SP_OBJECT_REPR(text)->setAttribute("sodipodi:linespacing", os.str().c_str());
324     }
325     else
326         SP_OBJECT_REPR(text)->setAttribute("sodipodi:linespacing", NULL);
328     if (((SPObjectClass *) (text_parent_class))->write)
329         ((SPObjectClass *) (text_parent_class))->write (object, repr, flags);
331     return repr;
334 static void
335 sp_text_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
337     SP_TEXT(item)->layout.getBoundingBox(bbox, transform);
339     // Add stroke width
340     SPStyle* style=SP_OBJECT_STYLE (item);
341     if (style->stroke.type != SP_PAINT_TYPE_NONE) {
342         double const scale = expansion(transform);
343         if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
344             double const width = MAX(0.125, style->stroke_width.computed * scale);
345             if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
346                 bbox->x0-=0.5*width;
347                 bbox->x1+=0.5*width;
348                 bbox->y0-=0.5*width;
349                 bbox->y1+=0.5*width;
350             }
351         }
352     }
356 static NRArenaItem *
357 sp_text_show(SPItem *item, NRArena *arena, unsigned /* key*/, unsigned /*flags*/)
359     SPText *group = (SPText *) item;
361     NRArenaGroup *flowed = NRArenaGroup::create(arena);
362     nr_arena_group_set_transparent (flowed, FALSE);
364     // pass the bbox of the text object as paintbox (used for paintserver fills)
365     NRRect paintbox;
366     sp_item_invoke_bbox(item, &paintbox, NR::identity(), TRUE);
367     group->layout.show(flowed, &paintbox);
369     return flowed;
372 static void
373 sp_text_hide(SPItem *item, unsigned key)
375     if (((SPItemClass *) text_parent_class)->hide)
376         ((SPItemClass *) text_parent_class)->hide (item, key);
379 static char *
380 sp_text_description(SPItem *item)
382     SPText *text = (SPText *) item;
383     SPStyle *style = SP_OBJECT_STYLE(text);
385     font_instance *tf = (font_factory::Default())->Face(style->text->font_family.value,
386                                                         font_style_to_pos(*style));
387     char name_buf[256];
388     char *n;
389     if (tf) {
390         tf->Name(name_buf, sizeof(name_buf));
391         n = xml_quote_strdup(name_buf);
392         tf->Unref();
393     } else {
394         /* TRANSLATORS: For description of font with no name. */
395         n = g_strdup(_("&lt;no name found&gt;"));
396     }
398     GString *xs = SP_PX_TO_METRIC_STRING(style->font_size.computed, SP_DT_NAMEDVIEW(SP_ACTIVE_DESKTOP)->getDefaultMetric());
400     char *ret = ( SP_IS_TEXT_TEXTPATH(item)
401                   ? g_strdup_printf(_("<b>Text on path</b> (%s, %s)"), n, xs->str)
402                   : g_strdup_printf(_("<b>Text</b> (%s, %s)"), n, xs->str) );
403     g_free(n);
404     return ret;
407 static void sp_text_snappoints(SPItem const *item, SnapPointsIter p)
409     // the baseline anchor of the first char
410     Inkscape::Text::Layout const *layout = te_get_layout((SPItem *) item);
411     if(layout != NULL) {
412         *p = layout->characterAnchorPoint(layout->begin()) * sp_item_i2d_affine(item);
413     }
416 static NR::Matrix
417 sp_text_set_transform (SPItem *item, NR::Matrix const &xform)
419     SPText *text = SP_TEXT(item);
421     // we cannot optimize textpath because changing its fontsize will break its match to the path
422     if (SP_IS_TEXT_TEXTPATH (text))
423         return xform;
425     /* This function takes care of scaling & translation only, we return whatever parts we can't
426        handle. */
428 // TODO: pjrm tried to use fontsize_expansion(xform) here and it works for text in that font size
429 // is scaled more intuitively when scaling non-uniformly; however this necessitated using
430 // fontsize_expansion instead of expansion in other places too, where it was not appropriate
431 // (e.g. it broke stroke width on copy/pasting of style from horizontally stretched to vertically
432 // stretched shape). Using fontsize_expansion only here broke setting the style via font
433 // dialog. This needs to be investigated further.
434     double const ex = NR::expansion(xform);
435     if (ex == 0) {
436         return xform;
437     }
439     NR::Matrix ret(NR::transform(xform));
440     ret[0] /= ex;
441     ret[1] /= ex;
442     ret[2] /= ex;
443     ret[3] /= ex;
445     // Adjust x/y, dx/dy
446     text->_adjustCoordsRecursive (item, xform * ret.inverse(), ex);
448     // Adjust font size
449     text->_adjustFontsizeRecursive (item, ex);
451     // Adjust stroke width
452     sp_item_adjust_stroke_width_recursive (item, ex);
454     // Adjust pattern fill
455     sp_item_adjust_pattern(item, xform * ret.inverse());
457     // Adjust gradient fill
458     sp_item_adjust_gradient(item, xform * ret.inverse());
460     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_TEXT_LAYOUT_MODIFIED_FLAG);
462     return ret;
465 static void
466 sp_text_print (SPItem *item, SPPrintContext *ctx)
468     NRRect     pbox, dbox, bbox;
469     SPText *group = SP_TEXT (item);
471     sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
472     sp_item_bbox_desktop (item, &bbox);
473     dbox.x0 = 0.0;
474     dbox.y0 = 0.0;
475     dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
476     dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
477     NR::Matrix const ctm = sp_item_i2d_affine(item);
479     group->layout.print(ctx,&pbox,&dbox,&bbox,ctm);
482 /*
483  * Member functions
484  */
486 unsigned SPText::_buildLayoutInput(SPObject *root, Inkscape::Text::Layout::OptionalTextTagAttrs const &parent_optional_attrs, unsigned parent_attrs_offset, bool in_textpath)
488     unsigned length = 0;
489     int child_attrs_offset = 0;
490     Inkscape::Text::Layout::OptionalTextTagAttrs optional_attrs;
492     if (SP_IS_TEXT(root)) {
493         SP_TEXT(root)->attributes.mergeInto(&optional_attrs, parent_optional_attrs, parent_attrs_offset, true, true);
494     }
495     else if (SP_IS_TSPAN(root)) {
496         SPTSpan *tspan = SP_TSPAN(root);
497         bool use_xy = !in_textpath && (tspan->role == SP_TSPAN_ROLE_UNSPECIFIED || !tspan->attributes.singleXYCoordinates());
498         tspan->attributes.mergeInto(&optional_attrs, parent_optional_attrs, parent_attrs_offset, use_xy, true);
499     }
500     else if (SP_IS_TEXTPATH(root)) {
501         in_textpath = true;
502         SP_TEXTPATH(root)->attributes.mergeInto(&optional_attrs, parent_optional_attrs, parent_attrs_offset, false, true);
503         optional_attrs.x.clear();
504         optional_attrs.y.clear();
505     }
506     else {
507         optional_attrs = parent_optional_attrs;
508         child_attrs_offset = parent_attrs_offset;
509     }
511     if (SP_IS_TSPAN(root))
512         if (SP_TSPAN(root)->role != SP_TSPAN_ROLE_UNSPECIFIED) {
513             // we need to allow the first line not to have role=line, but still set the source_cookie to the right value
514             SPObject *prev_object = SP_OBJECT_PREV(root);
515             if (prev_object && SP_IS_TSPAN(prev_object)) {
516                 if (!layout.inputExists())
517                     layout.appendText("", prev_object->style, prev_object, &optional_attrs);
518                 layout.appendControlCode(Inkscape::Text::Layout::PARAGRAPH_BREAK, prev_object);
519             }
520             if (!root->hasChildren())
521                 layout.appendText("", root->style, root, &optional_attrs);
522             length++;     // interpreting line breaks as a character for the purposes of x/y/etc attributes
523                           // is a liberal interpretation of the svg spec, but a strict reading would mean
524                           // that if the first line is empty the second line would take its place at the
525                           // start position. Very confusing.
526             child_attrs_offset--;
527         }
529     for (SPObject *child = sp_object_first_child(root) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
530         if (SP_IS_STRING(child)) {
531             Glib::ustring const &string = SP_STRING(child)->string;
532             layout.appendText(string, root->style, child, &optional_attrs, child_attrs_offset + length);
533             length += string.length();
534         } else {
535             length += _buildLayoutInput(child, optional_attrs, child_attrs_offset + length, in_textpath);
536         }
537     }
539     return length;
542 void SPText::rebuildLayout()
544     layout.clear();
545     Inkscape::Text::Layout::OptionalTextTagAttrs optional_attrs;
546     _buildLayoutInput(this, optional_attrs, 0, false);
547     layout.calculateFlow();
548     for (SPObject *child = firstChild() ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
549         if (SP_IS_TEXTPATH(child)) {
550             SPTextPath const *textpath = SP_TEXTPATH(child);
551             if (textpath->originalPath != NULL) {
552                 //g_print(layout.dumpAsText().c_str());
553                 layout.fitToPathAlign(textpath->startOffset, *textpath->originalPath);
554             }
555         }
556     }
557     //g_print(layout.dumpAsText().c_str());
559     // set the x,y attributes on role:line spans
560     for (SPObject *child = firstChild() ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
561         if (!SP_IS_TSPAN(child)) continue;
562         SPTSpan *tspan = SP_TSPAN(child);
563         if (tspan->role == SP_TSPAN_ROLE_UNSPECIFIED) continue;
564         if (!tspan->attributes.singleXYCoordinates()) continue;
565         Inkscape::Text::Layout::iterator iter = layout.sourceToIterator(tspan);
566         NR::Point anchor_point = layout.chunkAnchorPoint(iter);
567         sp_repr_set_svg_double(SP_OBJECT_REPR(tspan), "x", anchor_point[NR::X]);
568         sp_repr_set_svg_double(SP_OBJECT_REPR(tspan), "y", anchor_point[NR::Y]);
569     }
573 void SPText::_adjustFontsizeRecursive(SPItem *item, double ex, bool is_root)
575     SPStyle *style = SP_OBJECT_STYLE (item);
577     if (style && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
578         if (!style->font_size.set && is_root) {
579             style->font_size.set = 1;
580             style->font_size.type = SP_FONT_SIZE_LENGTH;
581         }
582         style->font_size.computed *= ex;
583         style->letter_spacing.computed *= ex;
584         style->word_spacing.computed *= ex;
585         item->updateRepr();
586     }
588     for (SPObject *o = item->children; o != NULL; o = o->next) {
589         if (SP_IS_ITEM(o))
590             _adjustFontsizeRecursive(SP_ITEM(o), ex, false);
591     }
594 void SPText::_adjustCoordsRecursive(SPItem *item, NR::Matrix const &m, double ex, bool is_root)
596     if (SP_IS_TSPAN(item))
597         SP_TSPAN(item)->attributes.transform(m, ex, ex, is_root);
598               // it doesn't matter if we change the x,y for role=line spans because we'll just overwrite them anyway
599     else if (SP_IS_TEXT(item))
600         SP_TEXT(item)->attributes.transform(m, ex, ex, is_root);
601     else if (SP_IS_TEXTPATH(item))
602         SP_TEXTPATH(item)->attributes.transform(m, ex, ex, is_root);
604     for (SPObject *o = item->children; o != NULL; o = o->next) {
605         if (SP_IS_ITEM(o))
606             _adjustCoordsRecursive(SP_ITEM(o), m, ex, false);
607     }
611 void SPText::_clearFlow(NRArenaGroup *in_arena)
613     nr_arena_item_request_render (in_arena);
614     for (NRArenaItem *child = in_arena->children; child != NULL; ) {
615         NRArenaItem *nchild = child->next;
617         nr_arena_glyphs_group_clear(NR_ARENA_GLYPHS_GROUP(child));
618         nr_arena_item_remove_child (in_arena, child);
620         child=nchild;
621     }
625 /*
626  * TextTagAttributes implementation
627  */
629 void TextTagAttributes::readFrom(Inkscape::XML::Node const *node)
631     readSingleAttribute(SP_ATTR_X, node->attribute("x"));
632     readSingleAttribute(SP_ATTR_Y, node->attribute("y"));
633     readSingleAttribute(SP_ATTR_DX, node->attribute("dx"));
634     readSingleAttribute(SP_ATTR_DY, node->attribute("dy"));
635     readSingleAttribute(SP_ATTR_ROTATE, node->attribute("rotate"));
638 bool TextTagAttributes::readSingleAttribute(unsigned key, gchar const *value)
640     std::vector<SVGLength> *attr_vector;
641     switch (key) {
642         case SP_ATTR_X:      attr_vector = &attributes.x; break;
643         case SP_ATTR_Y:      attr_vector = &attributes.y; break;
644         case SP_ATTR_DX:     attr_vector = &attributes.dx; break;
645         case SP_ATTR_DY:     attr_vector = &attributes.dy; break;
646         case SP_ATTR_ROTATE: attr_vector = &attributes.rotate; break;
647         default: return false;
648     }
650     // FIXME: sp_svg_length_list_read() amalgamates repeated separators. This prevents unset values.
651     *attr_vector = sp_svg_length_list_read(value);
652     return true;
655 void TextTagAttributes::writeTo(Inkscape::XML::Node *node) const
657     writeSingleAttribute(node, "x", attributes.x);
658     writeSingleAttribute(node, "y", attributes.y);
659     writeSingleAttribute(node, "dx", attributes.dx);
660     writeSingleAttribute(node, "dy", attributes.dy);
661     writeSingleAttribute(node, "rotate", attributes.rotate);
664 void TextTagAttributes::writeSingleAttribute(Inkscape::XML::Node *node, gchar const *key, std::vector<SVGLength> const &attr_vector)
666     if (attr_vector.empty())
667         node->setAttribute(key, NULL);
668     else {
669         Glib::ustring string;
670         gchar single_value_string[32];
672         // FIXME: this has no concept of unset values because sp_svg_length_list_read() can't read them back in
673         for (std::vector<SVGLength>::const_iterator it = attr_vector.begin() ; it != attr_vector.end() ; it++) {
674             g_ascii_formatd(single_value_string, sizeof (single_value_string), "%.8g", it->computed);
675             if (!string.empty()) string += ' ';
676             string += single_value_string;
677         }
678         node->setAttribute(key, string.c_str());
679     }
682 bool TextTagAttributes::singleXYCoordinates() const
684     return attributes.x.size() <= 1 && attributes.y.size() <= 1;
687 bool TextTagAttributes::anyAttributesSet() const
689     return !attributes.x.empty() || !attributes.y.empty() || !attributes.dx.empty() || !attributes.dy.empty() || !attributes.rotate.empty();
692 NR::Point TextTagAttributes::firstXY() const
694     NR::Point point;
695     if (attributes.x.empty()) point[NR::X] = 0.0;
696     else point[NR::X] = attributes.x[0].computed;
697     if (attributes.y.empty()) point[NR::Y] = 0.0;
698     else point[NR::Y] = attributes.y[0].computed;
699     return point;
702 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
704     mergeSingleAttribute(&output->x,      parent_attrs.x,      parent_attrs_offset, copy_xy ? &attributes.x : NULL);
705     mergeSingleAttribute(&output->y,      parent_attrs.y,      parent_attrs_offset, copy_xy ? &attributes.y : NULL);
706     mergeSingleAttribute(&output->dx,     parent_attrs.dx,     parent_attrs_offset, copy_dxdyrotate ? &attributes.dx : NULL);
707     mergeSingleAttribute(&output->dy,     parent_attrs.dy,     parent_attrs_offset, copy_dxdyrotate ? &attributes.dy : NULL);
708     mergeSingleAttribute(&output->rotate, parent_attrs.rotate, parent_attrs_offset, copy_dxdyrotate ? &attributes.rotate : NULL);
711 void TextTagAttributes::mergeSingleAttribute(std::vector<SVGLength> *output_list, std::vector<SVGLength> const &parent_list, unsigned parent_offset, std::vector<SVGLength> const *overlay_list)
713     if (overlay_list == NULL) {
714         output_list->resize(std::max(0, (int)parent_list.size() - (int)parent_offset));
715         std::copy(parent_list.begin() + parent_offset, parent_list.end(), output_list->begin());
716     } else {
717         output_list->clear();
718         output_list->reserve(std::max((int)parent_list.size() - (int)parent_offset, (int)overlay_list->size()));
719         unsigned overlay_offset = 0;
720         while (parent_offset < parent_list.size() || overlay_offset < overlay_list->size()) {
721             SVGLength const *this_item;
722             if (overlay_offset < overlay_list->size()) {
723                 this_item = &(*overlay_list)[overlay_offset];
724                 overlay_offset++;
725                 parent_offset++;
726             } else {
727                 this_item = &parent_list[parent_offset];
728                 parent_offset++;
729             }
730             output_list->push_back(*this_item);
731         }
732     }
735 void TextTagAttributes::erase(unsigned start_index, unsigned n)
737     if (n == 0) return;
738     if (!singleXYCoordinates()) {
739         eraseSingleAttribute(&attributes.x, start_index, n);
740         eraseSingleAttribute(&attributes.y, start_index, n);
741     }
742     eraseSingleAttribute(&attributes.dx, start_index, n);
743     eraseSingleAttribute(&attributes.dy, start_index, n);
744     eraseSingleAttribute(&attributes.rotate, start_index, n);
747 void TextTagAttributes::eraseSingleAttribute(std::vector<SVGLength> *attr_vector, unsigned start_index, unsigned n)
749     if (attr_vector->size() <= start_index) return;
750     if (attr_vector->size() <= start_index + n)
751         attr_vector->erase(attr_vector->begin() + start_index, attr_vector->end());
752     else
753         attr_vector->erase(attr_vector->begin() + start_index, attr_vector->begin() + start_index + n);
756 void TextTagAttributes::insert(unsigned start_index, unsigned n)
758     if (n == 0) return;
759     if (!singleXYCoordinates()) {
760         insertSingleAttribute(&attributes.x, start_index, n, true);
761         insertSingleAttribute(&attributes.y, start_index, n, true);
762     }
763     insertSingleAttribute(&attributes.dx, start_index, n, false);
764     insertSingleAttribute(&attributes.dy, start_index, n, false);
765     insertSingleAttribute(&attributes.rotate, start_index, n, false);
768 void TextTagAttributes::insertSingleAttribute(std::vector<SVGLength> *attr_vector, unsigned start_index, unsigned n, bool is_xy)
770     if (attr_vector->size() <= start_index) return;
771     SVGLength zero_length;
772     zero_length = 0.0;
773     attr_vector->insert(attr_vector->begin() + start_index, n, zero_length);
774     if (is_xy) {
775         double begin = start_index == 0 ? (*attr_vector)[start_index + n].computed : (*attr_vector)[start_index - 1].computed;
776         double diff = ((*attr_vector)[start_index + n].computed - begin) / n;   // n tested for nonzero in insert()
777         for (unsigned i = 0 ; i < n ; i++)
778             (*attr_vector)[start_index + i] = begin + diff * i;
779     }
782 void TextTagAttributes::split(unsigned index, TextTagAttributes *second)
784     if (!singleXYCoordinates()) {
785         splitSingleAttribute(&attributes.x, index, &second->attributes.x, false);
786         splitSingleAttribute(&attributes.y, index, &second->attributes.y, false);
787     }
788     splitSingleAttribute(&attributes.dx, index, &second->attributes.dx, true);
789     splitSingleAttribute(&attributes.dy, index, &second->attributes.dy, true);
790     splitSingleAttribute(&attributes.rotate, index, &second->attributes.rotate, true);
793 void TextTagAttributes::splitSingleAttribute(std::vector<SVGLength> *first_vector, unsigned index, std::vector<SVGLength> *second_vector, bool trimZeros)
795     second_vector->clear();
796     if (first_vector->size() <= index) return;
797     second_vector->resize(first_vector->size() - index);
798     std::copy(first_vector->begin() + index, first_vector->end(), second_vector->begin());
799     first_vector->resize(index);
800     if (trimZeros)
801         while (!first_vector->empty() && (!first_vector->back()._set || first_vector->back().value == 0.0))
802             first_vector->resize(first_vector->size() - 1);
805 void TextTagAttributes::join(TextTagAttributes const &first, TextTagAttributes const &second, unsigned second_index)
807     if (second.singleXYCoordinates()) {
808         attributes.x = first.attributes.x;
809         attributes.y = first.attributes.y;
810     } else {
811         joinSingleAttribute(&attributes.x, first.attributes.x, second.attributes.x, second_index);
812         joinSingleAttribute(&attributes.y, first.attributes.y, second.attributes.y, second_index);
813     }
814     joinSingleAttribute(&attributes.dx, first.attributes.dx, second.attributes.dx, second_index);
815     joinSingleAttribute(&attributes.dy, first.attributes.dy, second.attributes.dy, second_index);
816     joinSingleAttribute(&attributes.rotate, first.attributes.rotate, second.attributes.rotate, second_index);
819 void TextTagAttributes::joinSingleAttribute(std::vector<SVGLength> *dest_vector, std::vector<SVGLength> const &first_vector, std::vector<SVGLength> const &second_vector, unsigned second_index)
821     if (second_vector.empty())
822         *dest_vector = first_vector;
823     else {
824         dest_vector->resize(second_index + second_vector.size());
825         if (first_vector.size() < second_index) {
826             std::copy(first_vector.begin(), first_vector.end(), dest_vector->begin());
827             SVGLength zero_length;
828             zero_length = 0.0;
829             std::fill(dest_vector->begin() + first_vector.size(), dest_vector->begin() + second_index, zero_length);
830         } else
831             std::copy(first_vector.begin(), first_vector.begin() + second_index, dest_vector->begin());
832         std::copy(second_vector.begin(), second_vector.end(), dest_vector->begin() + second_index);
833     }
836 void TextTagAttributes::transform(NR::Matrix const &matrix, double scale_x, double scale_y, bool extend_zero_length)
838     SVGLength zero_length;
839     zero_length = 0.0;
841     /* edge testcases for this code:
842        1) moving text elements whose position is done entirely with transform="...", no x,y attributes
843        2) unflowing multi-line flowtext then moving it (it has x but not y)
844     */
845     unsigned points_count = std::max(attributes.x.size(), attributes.y.size());
846     if (extend_zero_length && points_count < 1)
847         points_count = 1;
848     for (unsigned i = 0 ; i < points_count ; i++) {
849         NR::Point point;
850         if (i < attributes.x.size()) point[NR::X] = attributes.x[i].computed;
851         else point[NR::X] = 0.0;
852         if (i < attributes.y.size()) point[NR::Y] = attributes.y[i].computed;
853         else point[NR::Y] = 0.0;
854         point *= matrix;
855         if (i < attributes.x.size())
856             attributes.x[i] = point[NR::X];
857         else if (point[NR::X] != 0.0 && extend_zero_length) {
858             attributes.x.resize(i + 1, zero_length);
859             attributes.x[i] = point[NR::X];
860         }
861         if (i < attributes.y.size())
862             attributes.y[i] = point[NR::Y];
863         else if (point[NR::Y] != 0.0 && extend_zero_length) {
864             attributes.y.resize(i + 1, zero_length);
865             attributes.y[i] = point[NR::Y];
866         }
867     }
868     for (std::vector<SVGLength>::iterator it = attributes.dx.begin() ; it != attributes.dx.end() ; it++)
869         *it = it->computed * scale_x;
870     for (std::vector<SVGLength>::iterator it = attributes.dy.begin() ; it != attributes.dy.end() ; it++)
871         *it = it->computed * scale_y;
874 void TextTagAttributes::addToDxDy(unsigned index, NR::Point const &adjust)
876     SVGLength zero_length;
877     zero_length = 0.0;
879     if (adjust[NR::X] != 0.0) {
880         if (attributes.dx.size() < index + 1) attributes.dx.resize(index + 1, zero_length);
881         attributes.dx[index] = attributes.dx[index].computed + adjust[NR::X];
882     }
883     if (adjust[NR::Y] != 0.0) {
884         if (attributes.dy.size() < index + 1) attributes.dy.resize(index + 1, zero_length);
885         attributes.dy[index] = attributes.dy[index].computed + adjust[NR::Y];
886     }
889 void TextTagAttributes::addToRotate(unsigned index, double delta)
891     SVGLength zero_length;
892     zero_length = 0.0;
894     if (attributes.rotate.size() < index + 1) {
895         if (attributes.rotate.empty())
896             attributes.rotate.resize(index + 1, zero_length);
897         else
898             attributes.rotate.resize(index + 1, attributes.rotate.back());
899     }
900     attributes.rotate[index] = mod360(attributes.rotate[index].computed + delta);
904 /*
905   Local Variables:
906   mode:c++
907   c-file-style:"stroustrup"
908   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
909   indent-tabs-mode:nil
910   fill-column:99
911   End:
912 */
913 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :