Code

Position of baseline anchor is now dependent of the text alignment
[inkscape.git] / src / sp-text.cpp
index 3bbb3b89263d55e0fcc91c14c4d3eccda0bb7c39..423922b80c991a11207973b3ab800b1185a18ee3 100644 (file)
@@ -25,6 +25,7 @@
 # include "config.h"
 #endif
 
+#include <2geom/matrix.h>
 #include <libnr/nr-matrix-fns.h>
 #include <libnrtype/FontFactory.h>
 #include <libnrtype/font-instance.h>
 #include "xml/quote.h"
 #include "xml/repr.h"
 #include "mod360.h"
+#include "sp-title.h"
+#include "sp-desc.h"
 
 #include "sp-textpath.h"
+#include "sp-tref.h"
 #include "sp-tspan.h"
 
 #include "text-editing.h"
@@ -64,14 +68,14 @@ static void sp_text_child_added (SPObject *object, Inkscape::XML::Node *rch, Ink
 static void sp_text_remove_child (SPObject *object, Inkscape::XML::Node *rch);
 static void sp_text_update (SPObject *object, SPCtx *ctx, guint flags);
 static void sp_text_modified (SPObject *object, guint flags);
-static Inkscape::XML::Node *sp_text_write (SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *sp_text_write (SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 
-static void sp_text_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const flags);
+static void sp_text_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags);
 static NRArenaItem *sp_text_show (SPItem *item, NRArena *arena, unsigned key, unsigned flags);
 static void sp_text_hide (SPItem *item, unsigned key);
 static char *sp_text_description (SPItem *item);
-static void sp_text_snappoints(SPItem const *item, SnapPointsIter p);
-static NR::Matrix sp_text_set_transform(SPItem *item, NR::Matrix const &xform);
+static void sp_text_snappoints(SPItem const *item, bool const target, SnapPointsWithType &p, Inkscape::SnapPreferences const *snapprefs);
+static Geom::Matrix sp_text_set_transform(SPItem *item, Geom::Matrix const &xform);
 static void sp_text_print (SPItem *item, SPPrintContext *gpc);
 
 static SPItemClass *text_parent_class;
@@ -193,7 +197,7 @@ sp_text_child_added (SPObject *object, Inkscape::XML::Node *rch, Inkscape::XML::
     if (((SPObjectClass *) text_parent_class)->child_added)
         ((SPObjectClass *) text_parent_class)->child_added (object, rch, ref);
 
-    text->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_TEXT_CONTENT_MODIFIED_FLAG);
+    text->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_TEXT_CONTENT_MODIFIED_FLAG | SP_TEXT_LAYOUT_MODIFIED_FLAG);
 }
 
 static void
@@ -245,9 +249,10 @@ sp_text_update (SPObject *object, SPCtx *ctx, guint flags)
         text->rebuildLayout();
 
         NRRect paintbox;
-        sp_item_invoke_bbox(text, &paintbox, NR::identity(), TRUE);
+        sp_item_invoke_bbox(text, &paintbox, Geom::identity(), TRUE);
         for (SPItemView* v = text->display; v != NULL; v = v->next) {
             text->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
+            nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
             // pass the bbox of the text object as paintbox (used for paintserver fills)
             text->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
         }
@@ -263,6 +268,21 @@ sp_text_modified (SPObject *object, guint flags)
     guint cflags = (flags & SP_OBJECT_MODIFIED_CASCADE);
     if (flags & SP_OBJECT_MODIFIED_FLAG) cflags |= SP_OBJECT_PARENT_MODIFIED_FLAG;
 
+    // FIXME: all that we need to do here is nr_arena_glyphs_[group_]set_style, to set the changed
+    // style, but there's no easy way to access the arena glyphs or glyph groups corresponding to a
+    // text object. Therefore we do here the same as in _update, that is, destroy all arena items
+    // and create new ones. This is probably quite wasteful.
+    if (flags & ( SP_OBJECT_STYLE_MODIFIED_FLAG )) {
+        SPText *text = SP_TEXT (object);
+        NRRect paintbox;
+        sp_item_invoke_bbox(text, &paintbox, Geom::identity(), TRUE);
+        for (SPItemView* v = text->display; v != NULL; v = v->next) {
+            text->_clearFlow(NR_ARENA_GROUP(v->arenaitem));
+            nr_arena_group_set_style(NR_ARENA_GROUP(v->arenaitem), SP_OBJECT_STYLE(object));
+            text->layout.show(NR_ARENA_GROUP(v->arenaitem), &paintbox);
+        }
+    }
+
     /* Create temporary list of children */
     GSList *l = NULL;
     SPObject *child;
@@ -282,20 +302,21 @@ sp_text_modified (SPObject *object, guint flags)
 }
 
 static Inkscape::XML::Node *
-sp_text_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
+sp_text_write (SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     SPText *text = SP_TEXT (object);
 
     if (flags & SP_OBJECT_WRITE_BUILD) {
         if (!repr)
-            repr = sp_repr_new ("svg:text");
+            repr = xml_doc->createElement("svg:text");
         GSList *l = NULL;
         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
+            if (SP_IS_TITLE(child) || SP_IS_DESC(child)) continue;
             Inkscape::XML::Node *crepr = NULL;
             if (SP_IS_STRING(child)) {
-                crepr = sp_repr_new_text(SP_STRING(child)->string.c_str());
+                crepr = xml_doc->createTextNode(SP_STRING(child)->string.c_str());
             } else {
-                crepr = child->updateRepr(NULL, flags);
+                crepr = child->updateRepr(xml_doc, NULL, flags);
             }
             if (crepr) l = g_slist_prepend (l, crepr);
         }
@@ -306,6 +327,7 @@ sp_text_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
         }
     } else {
         for (SPObject *child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
+            if (SP_IS_TITLE(child) || SP_IS_DESC(child)) continue;
             if (SP_IS_STRING(child)) {
                 SP_OBJECT_REPR(child)->setContent(SP_STRING(child)->string.c_str());
             } else {
@@ -326,20 +348,20 @@ sp_text_write (SPObject *object, Inkscape::XML::Node *repr, guint flags)
         SP_OBJECT_REPR(text)->setAttribute("sodipodi:linespacing", NULL);
 
     if (((SPObjectClass *) (text_parent_class))->write)
-        ((SPObjectClass *) (text_parent_class))->write (object, repr, flags);
+        ((SPObjectClass *) (text_parent_class))->write (object, xml_doc, repr, flags);
 
     return repr;
 }
 
 static void
-sp_text_bbox(SPItem const *item, NRRect *bbox, NR::Matrix const &transform, unsigned const /*flags*/)
+sp_text_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const /*flags*/)
 {
     SP_TEXT(item)->layout.getBoundingBox(bbox, transform);
 
     // Add stroke width
     SPStyle* style=SP_OBJECT_STYLE (item);
-    if (style->stroke.type != SP_PAINT_TYPE_NONE) {
-        double const scale = expansion(transform);
+    if (!style->stroke.isNone()) {
+        double const scale = transform.descrim();
         if ( fabs(style->stroke_width.computed * scale) > 0.01 ) { // sinon c'est 0=oon veut pas de bord
             double const width = MAX(0.125, style->stroke_width.computed * scale);
             if ( fabs(bbox->x1 - bbox->x0) > -0.00001 && fabs(bbox->y1 - bbox->y0) > -0.00001 ) {
@@ -361,9 +383,11 @@ sp_text_show(SPItem *item, NRArena *arena, unsigned /* key*/, unsigned /*flags*/
     NRArenaGroup *flowed = NRArenaGroup::create(arena);
     nr_arena_group_set_transparent (flowed, FALSE);
 
+    nr_arena_group_set_style(flowed, group->style);
+
     // pass the bbox of the text object as paintbox (used for paintserver fills)
     NRRect paintbox;
-    sp_item_invoke_bbox(item, &paintbox, NR::identity(), TRUE);
+    sp_item_invoke_bbox(item, &paintbox, Geom::identity(), TRUE);
     group->layout.show(flowed, &paintbox);
 
     return flowed;
@@ -382,8 +406,8 @@ sp_text_description(SPItem *item)
     SPText *text = (SPText *) item;
     SPStyle *style = SP_OBJECT_STYLE(text);
 
-    font_instance *tf = (font_factory::Default())->Face(style->text->font_family.value,
-                                                        font_style_to_pos(*style));
+    font_instance *tf = font_factory::Default()->FaceFromStyle(style);
+
     char name_buf[256];
     char *n;
     if (tf) {
@@ -395,7 +419,7 @@ sp_text_description(SPItem *item)
         n = g_strdup(_("&lt;no name found&gt;"));
     }
 
-    GString *xs = SP_PX_TO_METRIC_STRING(style->font_size.computed, SP_DT_NAMEDVIEW(SP_ACTIVE_DESKTOP)->getDefaultMetric());
+    GString *xs = SP_PX_TO_METRIC_STRING(style->font_size.computed, sp_desktop_namedview(SP_ACTIVE_DESKTOP)->getDefaultMetric());
 
     char *ret = ( SP_IS_TEXT_TEXTPATH(item)
                   ? g_strdup_printf(_("<b>Text on path</b> (%s, %s)"), n, xs->str)
@@ -404,17 +428,22 @@ sp_text_description(SPItem *item)
     return ret;
 }
 
-static void sp_text_snappoints(SPItem const *item, SnapPointsIter p)
+static void sp_text_snappoints(SPItem const *item, bool const target, SnapPointsWithType &p, Inkscape::SnapPreferences const */*snapprefs*/)
 {
-    // the baseline anchor of the first char
+    // Choose a point on the baseline for snapping from or to, with the horizontal position
+    // of this point depending on the text alignment (left vs. right)
     Inkscape::Text::Layout const *layout = te_get_layout((SPItem *) item);
-    if(layout != NULL) {
-        *p = layout->characterAnchorPoint(layout->begin()) * sp_item_i2d_affine(item);
+    if (layout != NULL && layout->outputExists()) {
+        boost::optional<Geom::Point> pt = layout->baselineAnchorPoint();
+        if (pt) {
+            int type = target ? int(Inkscape::SNAPTARGET_TEXT_BASELINE) : int(Inkscape::SNAPSOURCE_TEXT_BASELINE);
+            p.push_back(std::make_pair((*pt) * sp_item_i2d_affine(item), type));
+        }
     }
 }
 
-static NR::Matrix
-sp_text_set_transform (SPItem *item, NR::Matrix const &xform)
+static Geom::Matrix
+sp_text_set_transform (SPItem *item, Geom::Matrix const &xform)
 {
     SPText *text = SP_TEXT(item);
 
@@ -431,12 +460,12 @@ sp_text_set_transform (SPItem *item, NR::Matrix const &xform)
 // (e.g. it broke stroke width on copy/pasting of style from horizontally stretched to vertically
 // stretched shape). Using fontsize_expansion only here broke setting the style via font
 // dialog. This needs to be investigated further.
-    double const ex = NR::expansion(xform);
+    double const ex = xform.descrim();
     if (ex == 0) {
         return xform;
     }
 
-    NR::Matrix ret(NR::transform(xform));
+    Geom::Matrix ret(Geom::Matrix(xform).without_translation());
     ret[0] /= ex;
     ret[1] /= ex;
     ret[2] /= ex;
@@ -468,13 +497,13 @@ sp_text_print (SPItem *item, SPPrintContext *ctx)
     NRRect     pbox, dbox, bbox;
     SPText *group = SP_TEXT (item);
 
-    sp_item_invoke_bbox(item, &pbox, NR::identity(), TRUE);
+    sp_item_invoke_bbox(item, &pbox, Geom::identity(), TRUE);
     sp_item_bbox_desktop (item, &bbox);
     dbox.x0 = 0.0;
     dbox.y0 = 0.0;
     dbox.x1 = sp_document_width (SP_OBJECT_DOCUMENT (item));
     dbox.y1 = sp_document_height (SP_OBJECT_DOCUMENT (item));
-    NR::Matrix const ctm = sp_item_i2d_affine(item);
+    Geom::Matrix const ctm (sp_item_i2d_affine(item));
 
     group->layout.print(ctx,&pbox,&dbox,&bbox,ctm);
 }
@@ -497,6 +526,9 @@ unsigned SPText::_buildLayoutInput(SPObject *root, Inkscape::Text::Layout::Optio
         bool use_xy = !in_textpath && (tspan->role == SP_TSPAN_ROLE_UNSPECIFIED || !tspan->attributes.singleXYCoordinates());
         tspan->attributes.mergeInto(&optional_attrs, parent_optional_attrs, parent_attrs_offset, use_xy, true);
     }
+    else if (SP_IS_TREF(root)) {
+        SP_TREF(root)->attributes.mergeInto(&optional_attrs, parent_optional_attrs, parent_attrs_offset, true, true);
+    }
     else if (SP_IS_TEXTPATH(root)) {
         in_textpath = true;
         SP_TEXTPATH(root)->attributes.mergeInto(&optional_attrs, parent_optional_attrs, parent_attrs_offset, false, true);
@@ -531,7 +563,7 @@ unsigned SPText::_buildLayoutInput(SPObject *root, Inkscape::Text::Layout::Optio
             Glib::ustring const &string = SP_STRING(child)->string;
             layout.appendText(string, root->style, child, &optional_attrs, child_attrs_offset + length);
             length += string.length();
-        } else {
+        } else if (!sp_repr_is_meta_element(child->repr)) {
             length += _buildLayoutInput(child, optional_attrs, child_attrs_offset + length, in_textpath);
         }
     }
@@ -563,9 +595,8 @@ void SPText::rebuildLayout()
         if (tspan->role == SP_TSPAN_ROLE_UNSPECIFIED) continue;
         if (!tspan->attributes.singleXYCoordinates()) continue;
         Inkscape::Text::Layout::iterator iter = layout.sourceToIterator(tspan);
-        NR::Point anchor_point = layout.chunkAnchorPoint(iter);
-        sp_repr_set_svg_double(SP_OBJECT_REPR(tspan), "x", anchor_point[NR::X]);
-        sp_repr_set_svg_double(SP_OBJECT_REPR(tspan), "y", anchor_point[NR::Y]);
+        Geom::Point anchor_point = layout.chunkAnchorPoint(iter);
+        tspan->attributes.setFirstXY(anchor_point);
     }
 }
 
@@ -577,8 +608,8 @@ void SPText::_adjustFontsizeRecursive(SPItem *item, double ex, bool is_root)
     if (style && !NR_DF_TEST_CLOSE (ex, 1.0, NR_EPSILON)) {
         if (!style->font_size.set && is_root) {
             style->font_size.set = 1;
-            style->font_size.type = SP_FONT_SIZE_LENGTH;
         }
+        style->font_size.type = SP_FONT_SIZE_LENGTH;
         style->font_size.computed *= ex;
         style->letter_spacing.computed *= ex;
         style->word_spacing.computed *= ex;
@@ -591,7 +622,7 @@ void SPText::_adjustFontsizeRecursive(SPItem *item, double ex, bool is_root)
     }
 }
 
-void SPText::_adjustCoordsRecursive(SPItem *item, NR::Matrix const &m, double ex, bool is_root)
+void SPText::_adjustCoordsRecursive(SPItem *item, Geom::Matrix const &m, double ex, bool is_root)
 {
     if (SP_IS_TSPAN(item))
         SP_TSPAN(item)->attributes.transform(m, ex, ex, is_root);
@@ -600,6 +631,9 @@ void SPText::_adjustCoordsRecursive(SPItem *item, NR::Matrix const &m, double ex
         SP_TEXT(item)->attributes.transform(m, ex, ex, is_root);
     else if (SP_IS_TEXTPATH(item))
         SP_TEXTPATH(item)->attributes.transform(m, ex, ex, is_root);
+    else if (SP_IS_TREF(item)) {
+        SP_TREF(item)->attributes.transform(m, ex, ex, is_root);
+    }
 
     for (SPObject *o = item->children; o != NULL; o = o->next) {
         if (SP_IS_ITEM(o))
@@ -689,16 +723,29 @@ bool TextTagAttributes::anyAttributesSet() const
     return !attributes.x.empty() || !attributes.y.empty() || !attributes.dx.empty() || !attributes.dy.empty() || !attributes.rotate.empty();
 }
 
-NR::Point TextTagAttributes::firstXY() const
+Geom::Point TextTagAttributes::firstXY() const
 {
-    NR::Point point;
-    if (attributes.x.empty()) point[NR::X] = 0.0;
-    else point[NR::X] = attributes.x[0].computed;
-    if (attributes.y.empty()) point[NR::Y] = 0.0;
-    else point[NR::Y] = attributes.y[0].computed;
+    Geom::Point point;
+    if (attributes.x.empty()) point[Geom::X] = 0.0;
+    else point[Geom::X] = attributes.x[0].computed;
+    if (attributes.y.empty()) point[Geom::Y] = 0.0;
+    else point[Geom::Y] = attributes.y[0].computed;
     return point;
 }
 
+void TextTagAttributes::setFirstXY(Geom::Point &point)
+{
+    SVGLength zero_length;
+    zero_length = 0.0;
+
+    if (attributes.x.empty())
+        attributes.x.resize(1, zero_length);
+    if (attributes.y.empty())
+        attributes.y.resize(1, zero_length);
+    attributes.x[0].computed = point[Geom::X];
+    attributes.y[0].computed = point[Geom::Y];
+}
+
 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
 {
     mergeSingleAttribute(&output->x,      parent_attrs.x,      parent_attrs_offset, copy_xy ? &attributes.x : NULL);
@@ -710,11 +757,14 @@ void TextTagAttributes::mergeInto(Inkscape::Text::Layout::OptionalTextTagAttrs *
 
 void TextTagAttributes::mergeSingleAttribute(std::vector<SVGLength> *output_list, std::vector<SVGLength> const &parent_list, unsigned parent_offset, std::vector<SVGLength> const *overlay_list)
 {
+    output_list->clear();
     if (overlay_list == NULL) {
-        output_list->resize(std::max(0, (int)parent_list.size() - (int)parent_offset));
-        std::copy(parent_list.begin() + parent_offset, parent_list.end(), output_list->begin());
+        if (parent_list.size() > parent_offset)
+        {
+            output_list->reserve(parent_list.size() - parent_offset);
+            std::copy(parent_list.begin() + parent_offset, parent_list.end(), std::back_inserter(*output_list));
+        }
     } else {
-        output_list->clear();
         output_list->reserve(std::max((int)parent_list.size() - (int)parent_offset, (int)overlay_list->size()));
         unsigned overlay_offset = 0;
         while (parent_offset < parent_list.size() || overlay_offset < overlay_list->size()) {
@@ -833,7 +883,7 @@ void TextTagAttributes::joinSingleAttribute(std::vector<SVGLength> *dest_vector,
     }
 }
 
-void TextTagAttributes::transform(NR::Matrix const &matrix, double scale_x, double scale_y, bool extend_zero_length)
+void TextTagAttributes::transform(Geom::Matrix const &matrix, double scale_x, double scale_y, bool extend_zero_length)
 {
     SVGLength zero_length;
     zero_length = 0.0;
@@ -846,23 +896,23 @@ void TextTagAttributes::transform(NR::Matrix const &matrix, double scale_x, doub
     if (extend_zero_length && points_count < 1)
         points_count = 1;
     for (unsigned i = 0 ; i < points_count ; i++) {
-        NR::Point point;
-        if (i < attributes.x.size()) point[NR::X] = attributes.x[i].computed;
-        else point[NR::X] = 0.0;
-        if (i < attributes.y.size()) point[NR::Y] = attributes.y[i].computed;
-        else point[NR::Y] = 0.0;
+        Geom::Point point;
+        if (i < attributes.x.size()) point[Geom::X] = attributes.x[i].computed;
+        else point[Geom::X] = 0.0;
+        if (i < attributes.y.size()) point[Geom::Y] = attributes.y[i].computed;
+        else point[Geom::Y] = 0.0;
         point *= matrix;
         if (i < attributes.x.size())
-            attributes.x[i] = point[NR::X];
-        else if (point[NR::X] != 0.0 && extend_zero_length) {
+            attributes.x[i] = point[Geom::X];
+        else if (point[Geom::X] != 0.0 && extend_zero_length) {
             attributes.x.resize(i + 1, zero_length);
-            attributes.x[i] = point[NR::X];
+            attributes.x[i] = point[Geom::X];
         }
         if (i < attributes.y.size())
-            attributes.y[i] = point[NR::Y];
-        else if (point[NR::Y] != 0.0 && extend_zero_length) {
+            attributes.y[i] = point[Geom::Y];
+        else if (point[Geom::Y] != 0.0 && extend_zero_length) {
             attributes.y.resize(i + 1, zero_length);
-            attributes.y[i] = point[NR::Y];
+            attributes.y[i] = point[Geom::Y];
         }
     }
     for (std::vector<SVGLength>::iterator it = attributes.dx.begin() ; it != attributes.dx.end() ; it++)
@@ -871,18 +921,18 @@ void TextTagAttributes::transform(NR::Matrix const &matrix, double scale_x, doub
         *it = it->computed * scale_y;
 }
 
-void TextTagAttributes::addToDxDy(unsigned index, NR::Point const &adjust)
+void TextTagAttributes::addToDxDy(unsigned index, Geom::Point const &adjust)
 {
     SVGLength zero_length;
     zero_length = 0.0;
 
-    if (adjust[NR::X] != 0.0) {
+    if (adjust[Geom::X] != 0.0) {
         if (attributes.dx.size() < index + 1) attributes.dx.resize(index + 1, zero_length);
-        attributes.dx[index] = attributes.dx[index].computed + adjust[NR::X];
+        attributes.dx[index] = attributes.dx[index].computed + adjust[Geom::X];
     }
-    if (adjust[NR::Y] != 0.0) {
+    if (adjust[Geom::Y] != 0.0) {
         if (attributes.dy.size() < index + 1) attributes.dy.resize(index + 1, zero_length);
-        attributes.dy[index] = attributes.dy[index].computed + adjust[NR::Y];
+        attributes.dy[index] = attributes.dy[index].computed + adjust[Geom::Y];
     }
 }
 
@@ -891,11 +941,11 @@ void TextTagAttributes::addToRotate(unsigned index, double delta)
     SVGLength zero_length;
     zero_length = 0.0;
 
-    if (attributes.rotate.size() < index + 1) {
+    if (attributes.rotate.size() < index + 2) {
         if (attributes.rotate.empty())
-            attributes.rotate.resize(index + 1, zero_length);
+            attributes.rotate.resize(index + 2, zero_length);
         else
-            attributes.rotate.resize(index + 1, attributes.rotate.back());
+            attributes.rotate.resize(index + 2, attributes.rotate.back());
     }
     attributes.rotate[index] = mod360(attributes.rotate[index].computed + delta);
 }