Code

Remove some old snap code.
[inkscape.git] / src / text-editing.cpp
index 91182b37f47be707454ec1b28c4fe83923ba5ac2..f0cb4bd9da12967f29a32a30863485bc1154a367 100644 (file)
@@ -182,6 +182,43 @@ unsigned sp_text_get_length(SPObject const *item)
     return length;
 }
 
+/** Recursively gets the length of all the SPStrings at or below the given
+\a item, before and not including \a upto. Also adds 1 for each line break encountered. */
+unsigned sp_text_get_length_upto(SPObject const *item, SPObject const *upto)
+{
+    unsigned length = 0;
+
+    if (SP_IS_STRING(item)) {
+        return SP_STRING(item)->string.length();
+    }
+    if (is_line_break_object(item) && !SP_IS_TEXT(item)) {
+        if (item != SP_OBJECT_PARENT(item)->firstChild()) {
+            // add 1 for each newline
+            length++;
+        }
+    }
+    for (SPObject const *child = item->firstChild() ; child ; child = SP_OBJECT_NEXT(child)) {
+        if (upto && child == upto) {
+            // hit upto, return immediately
+            return length;
+        }
+        if (SP_IS_STRING(child)) {
+            length += SP_STRING(child)->string.length();
+        }
+        else {
+            if (upto && child->isAncestorOf(upto)) {
+                // upto is below us, recurse and break loop
+                length += sp_text_get_length_upto(child, upto);
+                return length;
+            } else {
+                // recurse and go to the next sibling
+                length += sp_text_get_length_upto(child, upto);
+            }
+        }
+    }
+    return length;
+}
+
 static Inkscape::XML::Node* duplicate_node_without_children(Inkscape::XML::Node const *old_node)
 {
     switch (old_node->type()) {
@@ -879,10 +916,13 @@ sp_te_adjust_tspan_letterspacing_screen(SPItem *text, Inkscape::Text::Layout::it
     SPObject *source_obj;
     unsigned nb_let;
     layout->getSourceOfCharacter(std::min(start, end), (void**)&source_obj);
-    if (source_obj == NULL)    // end of text
+
+    if (source_obj == NULL) {   // end of text
         source_obj = text->lastChild();
-    else if (SP_IS_STRING(source_obj))
+    }
+    if (SP_IS_STRING(source_obj)) {
         source_obj = source_obj->parent;
+    }
 
     SPStyle *style = SP_OBJECT_STYLE (source_obj);