Code

display/inkscape-cairo.h: Supply missing #includes/declarations so that we don't...
[inkscape.git] / src / text-editing.cpp
index bed837cefabf2ff65903734a16ad15c3e5bd8e56..b103a01087560a4d44ae90a3815986342142da63 100644 (file)
@@ -14,6 +14,8 @@
 # include "config.h"
 #endif
 
+#include <cstring>
+#include <string>
 #include <glibmm/i18n.h>
 
 #include "desktop.h"
@@ -78,7 +80,7 @@ sp_te_input_is_empty (SPObject const *item)
 Inkscape::Text::Layout::iterator
 sp_te_get_position_by_coords (SPItem const *item, NR::Point &i_p)
 {
-    NR::Matrix  im=sp_item_i2d_affine (item);
+    NR::Matrix im = from_2geom(sp_item_i2d_affine (item));
     im = im.inverse();
 
     NR::Point p = i_p * im;
@@ -138,8 +140,9 @@ char * dump_hexy(const gchar * utf8)
 
 Inkscape::Text::Layout::iterator sp_te_replace(SPItem *item, Inkscape::Text::Layout::iterator const &start, Inkscape::Text::Layout::iterator const &end, gchar const *utf8)
 {
-    Inkscape::Text::Layout::iterator new_start = sp_te_delete(item, start, end);
-    return sp_te_insert(item, new_start, utf8);
+    iterator_pair pair;
+    sp_te_delete(item, start, end, pair);
+    return sp_te_insert(item, pair.first, utf8);
 }
 
 
@@ -148,13 +151,22 @@ Inkscape::Text::Layout::iterator sp_te_replace(SPItem *item, Inkscape::Text::Lay
 
 static bool is_line_break_object(SPObject const *object)
 {
-    return    SP_IS_TEXT(object)
-           || (SP_IS_TSPAN(object) && SP_TSPAN(object)->role != SP_TSPAN_ROLE_UNSPECIFIED)
-           || SP_IS_TEXTPATH(object)
-           || SP_IS_FLOWDIV(object)
-           || SP_IS_FLOWPARA(object)
-           || SP_IS_FLOWLINE(object)
-           || SP_IS_FLOWREGIONBREAK(object);
+    bool is_line_break = false;
+    
+    if (object) {
+        if (SP_IS_TEXT(object)
+                || (SP_IS_TSPAN(object) && SP_TSPAN(object)->role != SP_TSPAN_ROLE_UNSPECIFIED)
+                || SP_IS_TEXTPATH(object)
+                || SP_IS_FLOWDIV(object)
+                || SP_IS_FLOWPARA(object)
+                || SP_IS_FLOWLINE(object)
+                || SP_IS_FLOWREGIONBREAK(object)) {
+                    
+            is_line_break = true;
+        }
+    }
+    
+    return is_line_break;
 }
 
 /** returns the attributes for an object, or NULL if it isn't a text,
@@ -258,6 +270,9 @@ static Inkscape::XML::Node* duplicate_node_without_children(Inkscape::XML::Docum
         case Inkscape::XML::COMMENT_NODE:
             return xml_doc->createComment(old_node->content());
 
+        case Inkscape::XML::PI_NODE:
+            return xml_doc->createPI(old_node->name(), old_node->content());
+
         case Inkscape::XML::DOCUMENT_NODE:
             return NULL;   // this had better never happen
     }
@@ -334,7 +349,7 @@ Inkscape::Text::Layout::iterator sp_te_insert_line (SPItem *item, Inkscape::Text
 {
     // Disable newlines in a textpath; TODO: maybe on Enter in a textpath, separate it into two
     // texpaths attached to the same path, with a vertical shift
-    if (SP_IS_TEXT_TEXTPATH (item))
+    if (SP_IS_TEXT_TEXTPATH (item) || SP_IS_TREF(item))
         return position;
         
     SPDesktop *desktop = SP_ACTIVE_DESKTOP; 
@@ -350,6 +365,12 @@ Inkscape::Text::Layout::iterator sp_te_insert_line (SPItem *item, Inkscape::Text
 
     if (split_obj == 0 || is_line_break_object(split_obj)) {
         if (split_obj == 0) split_obj = item->lastChild();
+        
+        if (SP_IS_TREF(split_obj)) {
+               desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, tref_edit_message);
+            return position;
+        }
+        
         if (split_obj) {
             Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(split_obj));
             Inkscape::XML::Node *new_node = duplicate_node_without_children(xml_doc, SP_OBJECT_REPR(split_obj));
@@ -377,7 +398,7 @@ Inkscape::Text::Layout::iterator sp_te_insert_line (SPItem *item, Inkscape::Text
         // TODO
         // I think the only case to put here is arbitrary gaps, which nobody uses yet
     }
-    item->updateRepr(SP_OBJECT_REPR(item),SP_OBJECT_WRITE_EXT);
+    item->updateRepr();
     unsigned char_index = layout->iteratorToCharIndex(position);
     te_update_layout_now(item);
     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
@@ -499,7 +520,7 @@ sp_te_insert(SPItem *item, Inkscape::Text::Layout::iterator const &position, gch
         }
     }
 
-    item->updateRepr(SP_OBJECT_REPR(item),SP_OBJECT_WRITE_EXT);
+    item->updateRepr();
     unsigned char_index = layout->iteratorToCharIndex(position);
     te_update_layout_now(item);
     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
@@ -657,19 +678,21 @@ static void erase_from_spstring(SPString *string_item, Glib::ustring::iterator i
 quite a complicated operation, partly due to the cleanup that is done if all
 the text in a subobject has been deleted, and partly due to the difficulty
 of figuring out what is a line break and how to delete one. Returns the
-lesser of \a start and \a end, because that is where the cursor should be
-put after the deletion is done. */
-Inkscape::Text::Layout::iterator
-sp_te_delete (SPItem *item, Inkscape::Text::Layout::iterator const &start, Inkscape::Text::Layout::iterator const &end)
+real start and ending iterators based on the situation. */
+bool
+sp_te_delete (SPItem *item, Inkscape::Text::Layout::iterator const &start,
+              Inkscape::Text::Layout::iterator const &end, iterator_pair &iter_pair)
 {
-    if (start == end) return start;
-    Inkscape::Text::Layout::iterator first, last;
-    if (start < end) {
-        first = start;
-        last = end;
-    } else {
-        first = end;
-        last = start;
+    bool success = false;
+
+    iter_pair.first = start;
+    iter_pair.second = end;
+    
+    if (start == end) return success;
+    
+    if (start > end) {
+        iter_pair.first = end;
+        iter_pair.second = start;
     }
     
     SPDesktop *desktop = SP_ACTIVE_DESKTOP;
@@ -678,12 +701,12 @@ sp_te_delete (SPItem *item, Inkscape::Text::Layout::iterator const &start, Inksc
     SPObject *start_item = 0, *end_item = 0;
     void *rawptr = 0;
     Glib::ustring::iterator start_text_iter, end_text_iter;
-    layout->getSourceOfCharacter(first, &rawptr, &start_text_iter);
+    layout->getSourceOfCharacter(iter_pair.first, &rawptr, &start_text_iter);
     start_item = SP_OBJECT(rawptr);
-    layout->getSourceOfCharacter(last, &rawptr, &end_text_iter);
+    layout->getSourceOfCharacter(iter_pair.second, &rawptr, &end_text_iter);
     end_item = SP_OBJECT(rawptr);
     if (start_item == 0)
-        return first;   // start is at end of text
+        return success;   // start is at end of text
     if (is_line_break_object(start_item))
         move_to_end_of_paragraph(&start_item, &start_text_iter);
     if (end_item == 0) {
@@ -701,10 +724,10 @@ sp_te_delete (SPItem *item, Inkscape::Text::Layout::iterator const &start, Inksc
             // If the parent is a tref, editing on this particular string is disallowed.
             if (SP_IS_TREF(SP_OBJECT_PARENT(start_item))) {
                 desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, tref_edit_message);
-                return end;
+            } else {
+                erase_from_spstring(SP_STRING(start_item), start_text_iter, end_text_iter);
+                success = true;
             }
-            
-            erase_from_spstring(SP_STRING(start_item), start_text_iter, end_text_iter);
         }
     } else {
         SPObject *sub_item = start_item;
@@ -715,11 +738,12 @@ sp_te_delete (SPItem *item, Inkscape::Text::Layout::iterator const &start, Inksc
                     // If the parent is a tref, editing on this particular string is disallowed.
                     if (SP_IS_TREF(SP_OBJECT_PARENT(sub_item))) {
                         desktop->messageStack()->flash(Inkscape::ERROR_MESSAGE, tref_edit_message);
-                        return end;
+                        break;
                     }
             
                     Glib::ustring *string = &SP_STRING(sub_item)->string;
                     erase_from_spstring(SP_STRING(sub_item), string->begin(), end_text_iter);
+                    success = true;
                 }
                 break;
             }
@@ -729,6 +753,7 @@ sp_te_delete (SPItem *item, Inkscape::Text::Layout::iterator const &start, Inksc
                     erase_from_spstring(string, start_text_iter, string->string.end());
                 else
                     erase_from_spstring(string, string->string.begin(), string->string.end());
+                success = true;
             }
             // walk to the next item in the tree
             if (sub_item->hasChildren())
@@ -757,8 +782,9 @@ sp_te_delete (SPItem *item, Inkscape::Text::Layout::iterator const &start, Inksc
     while (tidy_xml_tree_recursively(common_ancestor));
     te_update_layout_now(item);
     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
-    layout->validateIterator(&first);
-    return first;
+    layout->validateIterator(&iter_pair.first);
+    layout->validateIterator(&iter_pair.second);
+    return success;
 }
 
 
@@ -922,7 +948,7 @@ sp_te_adjust_kerning_screen (SPItem *item, Inkscape::Text::Layout::iterator cons
     // divide increment by zoom
     // divide increment by matrix expansion
     gdouble factor = 1 / desktop->current_zoom();
-    NR::Matrix t = sp_item_i2doc_affine(item);
+    NR::Matrix t = from_2geom(sp_item_i2doc_affine(item));
     factor = factor / NR::expansion(t);
     by = factor * by;
 
@@ -944,7 +970,7 @@ sp_te_adjust_rotation_screen(SPItem *text, Inkscape::Text::Layout::iterator cons
     // divide increment by zoom
     // divide increment by matrix expansion
     gdouble factor = 1 / desktop->current_zoom();
-    NR::Matrix t = sp_item_i2doc_affine(text);
+    NR::Matrix t = from_2geom(sp_item_i2doc_affine(text));
     factor = factor / NR::expansion(t);
     Inkscape::Text::Layout const *layout = te_get_layout(text);
     if (layout == NULL) return;
@@ -959,7 +985,7 @@ sp_te_adjust_rotation_screen(SPItem *text, Inkscape::Text::Layout::iterator cons
 }
 
 void
-sp_te_adjust_rotation(SPItem *text, Inkscape::Text::Layout::iterator const &start, Inkscape::Text::Layout::iterator const &end, SPDesktop *desktop, gdouble degrees)
+sp_te_adjust_rotation(SPItem *text, Inkscape::Text::Layout::iterator const &start, Inkscape::Text::Layout::iterator const &end, SPDesktop */*desktop*/, gdouble degrees)
 {
     unsigned char_index;
     TextTagAttributes *attributes = text_tag_attributes_at_position(text, std::min(start, end), &char_index);
@@ -1029,7 +1055,7 @@ sp_te_adjust_tspan_letterspacing_screen(SPItem *text, Inkscape::Text::Layout::it
     gdouble const zoom = desktop->current_zoom();
     gdouble const zby = (by
                          / (zoom * (nb_let > 1 ? nb_let - 1 : 1))
-                         / NR::expansion(sp_item_i2doc_affine(SP_ITEM(source_obj))));
+                         / NR::expansion(from_2geom(sp_item_i2doc_affine(SP_ITEM(source_obj)))));
     val += zby;
 
     if (start == end) {
@@ -1061,7 +1087,7 @@ sp_te_adjust_tspan_letterspacing_screen(SPItem *text, Inkscape::Text::Layout::it
 }
 
 void
-sp_te_adjust_linespacing_screen (SPItem *text, Inkscape::Text::Layout::iterator const &start, Inkscape::Text::Layout::iterator const &end, SPDesktop *desktop, gdouble by)
+sp_te_adjust_linespacing_screen (SPItem *text, Inkscape::Text::Layout::iterator const &/*start*/, Inkscape::Text::Layout::iterator const &/*end*/, SPDesktop *desktop, gdouble by)
 {
     // TODO: use start and end iterators to delineate the area to be affected
     g_return_if_fail (text != NULL);
@@ -1088,7 +1114,7 @@ sp_te_adjust_linespacing_screen (SPItem *text, Inkscape::Text::Layout::iterator
     gdouble zby = by / (desktop->current_zoom() * (line_count == 0 ? 1 : line_count));
 
     // divide increment by matrix expansion
-    NR::Matrix t = sp_item_i2doc_affine (SP_ITEM(text));
+    NR::Matrix t = from_2geom(sp_item_i2doc_affine (SP_ITEM(text)));
     zby = zby / NR::expansion(t);
 
     switch (style->line_height.unit) {
@@ -1107,27 +1133,27 @@ sp_te_adjust_linespacing_screen (SPItem *text, Inkscape::Text::Layout::iterator
             else style->line_height.value *= (average_line_height + zby) / average_line_height;
             break;
             // absolute-type units
-           case SP_CSS_UNIT_PX:
+        case SP_CSS_UNIT_PX:
             style->line_height.computed += zby;
             style->line_height.value = style->line_height.computed;
             break;
-           case SP_CSS_UNIT_PT:
+        case SP_CSS_UNIT_PT:
             style->line_height.computed += zby * PT_PER_PX;
             style->line_height.value = style->line_height.computed;
             break;
-           case SP_CSS_UNIT_PC:
+        case SP_CSS_UNIT_PC:
             style->line_height.computed += zby * (PT_PER_PX / 12);
             style->line_height.value = style->line_height.computed;
             break;
-           case SP_CSS_UNIT_MM:
+        case SP_CSS_UNIT_MM:
             style->line_height.computed += zby * MM_PER_PX;
             style->line_height.value = style->line_height.computed;
             break;
-           case SP_CSS_UNIT_CM:
+        case SP_CSS_UNIT_CM:
             style->line_height.computed += zby * CM_PER_PX;
             style->line_height.value = style->line_height.computed;
             break;
-           case SP_CSS_UNIT_IN:
+        case SP_CSS_UNIT_IN:
             style->line_height.computed += zby * IN_PER_PX;
             style->line_height.value = style->line_height.computed;
             break;
@@ -1686,7 +1712,7 @@ static bool tidy_xml_tree_recursively(SPObject *root)
     bool changes = false;
 
     for (SPObject *child = root->firstChild() ; child != NULL ; ) {
-        if (SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child)) {
+        if (SP_IS_FLOWREGION(child) || SP_IS_FLOWREGIONEXCLUDE(child) || SP_IS_TREF(child)) {
             child = SP_OBJECT_NEXT(child);
             continue;
         }