Code

Position of baseline anchor is now dependent of the text alignment
[inkscape.git] / src / sp-text.cpp
index dd57428ef914877edc58da707e64634a3a420f9e..423922b80c991a11207973b3ab800b1185a18ee3 100644 (file)
@@ -74,7 +74,7 @@ static void sp_text_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &t
 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 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);
 
@@ -249,7 +249,7 @@ 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));
@@ -275,7 +275,7 @@ sp_text_modified (SPObject *object, guint flags)
     if (flags & ( SP_OBJECT_STYLE_MODIFIED_FLAG )) {
         SPText *text = SP_TEXT (object);
         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));
@@ -387,7 +387,7 @@ sp_text_show(SPItem *item, NRArena *arena, unsigned /* key*/, unsigned /*flags*/
 
     // 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;
@@ -407,7 +407,7 @@ sp_text_description(SPItem *item)
     SPStyle *style = SP_OBJECT_STYLE(text);
 
     font_instance *tf = font_factory::Default()->FaceFromStyle(style);
-    
+
     char name_buf[256];
     char *n;
     if (tf) {
@@ -428,12 +428,17 @@ 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));
+        }
     }
 }
 
@@ -492,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);
 }
@@ -590,7 +595,7 @@ 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);
+        Geom::Point anchor_point = layout.chunkAnchorPoint(iter);
         tspan->attributes.setFirstXY(anchor_point);
     }
 }
@@ -603,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;
@@ -617,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);
@@ -718,17 +723,17 @@ 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(NR::Point &point)
+void TextTagAttributes::setFirstXY(Geom::Point &point)
 {
     SVGLength zero_length;
     zero_length = 0.0;
@@ -737,8 +742,8 @@ void TextTagAttributes::setFirstXY(NR::Point &point)
         attributes.x.resize(1, zero_length);
     if (attributes.y.empty())
         attributes.y.resize(1, zero_length);
-    attributes.x[0].computed = point[NR::X];
-    attributes.y[0].computed = point[NR::Y];
+    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
@@ -878,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;
@@ -891,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++)
@@ -916,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];
     }
 }
 
@@ -936,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);
 }