Code

Remove custom color wheel widget in favor of the (relatively) recent GtkHSV. Fixes...
[inkscape.git] / src / text-editing.cpp
index dd054f06309fb0df3112169dde2bad075e8c1656..5bad33d290b28510900ec2dc98cd4b5ef779c2d6 100644 (file)
@@ -14,6 +14,8 @@
 # include "config.h"
 #endif
 
+#include <cstring>
+#include <string>
 #include <glibmm/i18n.h>
 
 #include "desktop.h"
@@ -55,6 +57,7 @@ static void te_update_layout_now (SPItem *item)
         SP_TEXT(item)->rebuildLayout();
     else if (SP_IS_FLOWTEXT (item))
         SP_FLOWTEXT(item)->rebuildLayout();
+    item->updateRepr();
 }
 
 /** Returns true if there are no visible characters on the canvas */
@@ -76,37 +79,45 @@ 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)
+sp_te_get_position_by_coords (SPItem const *item, Geom::Point const &i_p)
 {
-    NR::Matrix  im=sp_item_i2d_affine (item);
+    Geom::Matrix im (sp_item_i2d_affine (item));
     im = im.inverse();
 
-    NR::Point p = i_p * im;
+    Geom::Point p = i_p * im;
     Inkscape::Text::Layout const *layout = te_get_layout(item);
     return layout->getNearestCursorPositionTo(p);
 }
 
-std::vector<NR::Point> sp_te_create_selection_quads(SPItem const *item, Inkscape::Text::Layout::iterator const &start, Inkscape::Text::Layout::iterator const &end, NR::Matrix const &transform)
+std::vector<Geom::Point> sp_te_create_selection_quads(SPItem const *item, Inkscape::Text::Layout::iterator const &start, Inkscape::Text::Layout::iterator const &end, Geom::Matrix const &transform)
 {
     if (start == end)
-        return std::vector<NR::Point>();
+        return std::vector<Geom::Point>();
     Inkscape::Text::Layout const *layout = te_get_layout(item);
     if (layout == NULL)
-        return std::vector<NR::Point>();
+        return std::vector<Geom::Point>();
 
     return layout->createSelectionShape(start, end, transform);
 }
 
 void
-sp_te_get_cursor_coords (SPItem const *item, Inkscape::Text::Layout::iterator const &position, NR::Point &p0, NR::Point &p1)
+sp_te_get_cursor_coords (SPItem const *item, Inkscape::Text::Layout::iterator const &position, Geom::Point &p0, Geom::Point &p1)
 {
     Inkscape::Text::Layout const *layout = te_get_layout(item);
     double height, rotation;
-    layout->queryCursorShape(position, &p0, &height, &rotation);
-    p1 = NR::Point(p0[NR::X] + height * sin(rotation), p0[NR::Y] - height * cos(rotation));
+    layout->queryCursorShape(position, p0, height, rotation);
+    p1 = Geom::Point(p0[Geom::X] + height * sin(rotation), p0[Geom::Y] - height * cos(rotation));
 }
 
 SPStyle const * sp_te_style_at_position(SPItem const *text, Inkscape::Text::Layout::iterator const &position)
+{
+    SPObject const *pos_obj = sp_te_object_at_position(text, position);
+    if (pos_obj)
+        return SP_OBJECT_STYLE(pos_obj);
+    return NULL;
+}
+
+SPObject const * sp_te_object_at_position(SPItem const *text, Inkscape::Text::Layout::iterator const &position)
 {
     Inkscape::Text::Layout const *layout = te_get_layout(text);
     if (layout == NULL)
@@ -117,8 +128,8 @@ SPStyle const * sp_te_style_at_position(SPItem const *text, Inkscape::Text::Layo
     pos_obj = SP_OBJECT(rawptr);
     if (pos_obj == 0) pos_obj = text;
     while (SP_OBJECT_STYLE(pos_obj) == NULL)
-        pos_obj = SP_OBJECT_PARENT(pos_obj);   // SPStrings don't have style
-    return SP_OBJECT_STYLE(pos_obj);
+        pos_obj = SP_OBJECT_PARENT(pos_obj);   // not interested in SPStrings 
+    return pos_obj;
 }
 
 /*
@@ -268,6 +279,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
     }
@@ -393,7 +407,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);
@@ -515,7 +529,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);
@@ -774,7 +788,7 @@ sp_te_delete (SPItem *item, Inkscape::Text::Layout::iterator const &start,
         }
     }
 
-    while (tidy_xml_tree_recursively(common_ancestor));
+    while (tidy_xml_tree_recursively(common_ancestor)){};
     te_update_layout_now(item);
     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
     layout->validateIterator(&iter_pair.first);
@@ -915,7 +929,7 @@ sp_te_set_repr_text_multiline(SPItem *text, gchar const *str)
 
 /** Returns the attributes block and the character index within that block
 which represents the iterator \a position. */
-static TextTagAttributes*
+TextTagAttributes*
 text_tag_attributes_at_position(SPItem *item, Inkscape::Text::Layout::iterator const &position, unsigned *char_index)
 {
     if (item == NULL || char_index == NULL || !SP_IS_TEXT(item))
@@ -938,13 +952,13 @@ text_tag_attributes_at_position(SPItem *item, Inkscape::Text::Layout::iterator c
 }
 
 void
-sp_te_adjust_kerning_screen (SPItem *item, Inkscape::Text::Layout::iterator const &start, Inkscape::Text::Layout::iterator const &end, SPDesktop *desktop, NR::Point by)
+sp_te_adjust_kerning_screen (SPItem *item, Inkscape::Text::Layout::iterator const &start, Inkscape::Text::Layout::iterator const &end, SPDesktop *desktop, Geom::Point by)
 {
     // divide increment by zoom
     // divide increment by matrix expansion
     gdouble factor = 1 / desktop->current_zoom();
-    NR::Matrix t = sp_item_i2doc_affine(item);
-    factor = factor / NR::expansion(t);
+    Geom::Matrix t (sp_item_i2doc_affine(item));
+    factor = factor / t.descrim();
     by = factor * by;
 
     unsigned char_index;
@@ -959,14 +973,50 @@ sp_te_adjust_kerning_screen (SPItem *item, Inkscape::Text::Layout::iterator cons
     item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
 }
 
+void sp_te_adjust_dx(SPItem *item, Inkscape::Text::Layout::iterator const &start, Inkscape::Text::Layout::iterator const &end, SPDesktop * /*desktop*/, double delta)
+{
+    unsigned char_index = 0;
+    TextTagAttributes *attributes = text_tag_attributes_at_position(item, std::min(start, end), &char_index);
+    if (attributes) {
+        attributes->addToDx(char_index, delta);
+    }
+    if (start != end) {
+        attributes = text_tag_attributes_at_position(item, std::max(start, end), &char_index);
+        if (attributes) {
+            attributes->addToDx(char_index, -delta);
+        }
+    }
+
+    item->updateRepr();
+    item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
+}
+
+void sp_te_adjust_dy(SPItem *item, Inkscape::Text::Layout::iterator const &start, Inkscape::Text::Layout::iterator const &end, SPDesktop * /*desktop*/, double delta)
+{
+    unsigned char_index = 0;
+    TextTagAttributes *attributes = text_tag_attributes_at_position(item, std::min(start, end), &char_index);
+    if (attributes) {
+        attributes->addToDy(char_index, delta);
+    }
+    if (start != end) {
+        attributes = text_tag_attributes_at_position(item, std::max(start, end), &char_index);
+        if (attributes) {
+            attributes->addToDy(char_index, -delta);
+        }
+    }
+
+    item->updateRepr();
+    item->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
+}
+
 void
 sp_te_adjust_rotation_screen(SPItem *text, Inkscape::Text::Layout::iterator const &start, Inkscape::Text::Layout::iterator const &end, SPDesktop *desktop, gdouble pixels)
 {
     // divide increment by zoom
     // divide increment by matrix expansion
     gdouble factor = 1 / desktop->current_zoom();
-    NR::Matrix t = sp_item_i2doc_affine(text);
-    factor = factor / NR::expansion(t);
+    Geom::Matrix t (sp_item_i2doc_affine(text));
+    factor = factor / t.descrim();
     Inkscape::Text::Layout const *layout = te_get_layout(text);
     if (layout == NULL) return;
     SPObject *source_item = 0;
@@ -998,6 +1048,27 @@ sp_te_adjust_rotation(SPItem *text, Inkscape::Text::Layout::iterator const &star
     text->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
 }
 
+void sp_te_set_rotation(SPItem *text, Inkscape::Text::Layout::iterator const &start, Inkscape::Text::Layout::iterator const &end, SPDesktop */*desktop*/, gdouble degrees)
+{
+    unsigned char_index = 0;
+    TextTagAttributes *attributes = text_tag_attributes_at_position(text, std::min(start, end), &char_index);
+    if (attributes != NULL) {
+        if (start != end) {
+            for (Inkscape::Text::Layout::iterator it = std::min(start, end) ; it != std::max(start, end) ; it.nextCharacter()) {
+                attributes = text_tag_attributes_at_position(text, it, &char_index);
+                if (attributes) {
+                    attributes->setRotate(char_index, degrees);
+                }
+            }
+        } else {
+            attributes->setRotate(char_index, degrees);
+        }
+
+        text->updateRepr();
+        text->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
+    }
+}
+
 void
 sp_te_adjust_tspan_letterspacing_screen(SPItem *text, Inkscape::Text::Layout::iterator const &start, Inkscape::Text::Layout::iterator const &end, SPDesktop *desktop, gdouble by)
 {
@@ -1050,7 +1121,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))));
+                         / to_2geom(sp_item_i2doc_affine(SP_ITEM(source_obj))).descrim());
     val += zby;
 
     if (start == end) {
@@ -1081,6 +1152,19 @@ sp_te_adjust_tspan_letterspacing_screen(SPItem *text, Inkscape::Text::Layout::it
     text->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_TEXT_LAYOUT_MODIFIED_FLAG);
 }
 
+double
+sp_te_get_average_linespacing (SPItem *text)
+{
+    Inkscape::Text::Layout const *layout = te_get_layout(text);
+    if (!layout)
+        return 0;
+
+    unsigned line_count = layout->lineIndex(layout->end());
+    double all_lines_height = layout->characterAnchorPoint(layout->end())[Geom::Y] - layout->characterAnchorPoint(layout->begin())[Geom::Y];
+    double average_line_height = all_lines_height / (line_count == 0 ? 1 : line_count);
+    return average_line_height;
+}
+
 void
 sp_te_adjust_linespacing_screen (SPItem *text, Inkscape::Text::Layout::iterator const &/*start*/, Inkscape::Text::Layout::iterator const &/*end*/, SPDesktop *desktop, gdouble by)
 {
@@ -1100,7 +1184,7 @@ sp_te_adjust_linespacing_screen (SPItem *text, Inkscape::Text::Layout::iterator
     }
 
     unsigned line_count = layout->lineIndex(layout->end());
-    double all_lines_height = layout->characterAnchorPoint(layout->end())[NR::Y] - layout->characterAnchorPoint(layout->begin())[NR::Y];
+    double all_lines_height = layout->characterAnchorPoint(layout->end())[Geom::Y] - layout->characterAnchorPoint(layout->begin())[Geom::Y];
     double average_line_height = all_lines_height / (line_count == 0 ? 1 : line_count);
     if (fabs(average_line_height) < 0.001) average_line_height = 0.001;
 
@@ -1109,8 +1193,8 @@ 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));
-    zby = zby / NR::expansion(t);
+    Geom::Matrix t (sp_item_i2doc_affine (SP_ITEM(text)));
+    zby = zby / t.descrim();
 
     switch (style->line_height.unit) {
         case SP_CSS_UNIT_NONE:
@@ -1208,16 +1292,17 @@ static bool objects_have_equal_style(SPObject const *parent, SPObject const *chi
     parent_style = sp_style_write_string(parent_spstyle, SP_STYLE_FLAG_ALWAYS);
     sp_style_unref(parent_spstyle);
 
-    Glib::ustring child_style_construction(parent_style);
+    Glib::ustring child_style_construction;
     while (child != parent) {
         // FIXME: this assumes that child's style is only in style= whereas it can also be in css attributes!
         char const *style_text = SP_OBJECT_REPR(child)->attribute("style");
         if (style_text && *style_text) {
-            child_style_construction += ';';
-            child_style_construction += style_text;
+            child_style_construction.insert(0, style_text);
+            child_style_construction.insert(0, 1, ';');
         }
         child = SP_OBJECT_PARENT(child);
     }
+    child_style_construction.insert(0, parent_style);
     SPStyle *child_spstyle = sp_style_new(SP_OBJECT_DOCUMENT(parent));
     sp_style_merge_from_style_string(child_spstyle, child_style_construction.c_str());
     gchar *child_style = sp_style_write_string(child_spstyle, SP_STYLE_FLAG_ALWAYS);
@@ -1422,6 +1507,7 @@ static bool tidy_operator_empty_spans(SPObject **item)
 the repeated strings will be merged by another operator. */
 static bool tidy_operator_inexplicable_spans(SPObject **item)
 {
+    if (*item && sp_repr_is_meta_element((*item)->repr)) return false;
     if (SP_IS_STRING(*item)) return false;
     if (is_line_break_object(*item)) return false;
     TextTagAttributes *attrs = attributes_for_object(*item);
@@ -1520,8 +1606,9 @@ static bool redundant_double_nesting_processor(SPObject **item, SPObject *child,
     if (!objects_have_equal_style(SP_OBJECT_PARENT(*item), child)) return false;
 
     Inkscape::XML::Node *insert_after_repr;
-    if (prepend) insert_after_repr = SP_OBJECT_REPR(SP_OBJECT_PREV(*item));
-    else insert_after_repr = SP_OBJECT_REPR(*item);
+    if (!prepend) insert_after_repr = SP_OBJECT_REPR(*item);
+    else if (SP_OBJECT_PREV(*item)) insert_after_repr = SP_OBJECT_REPR(SP_OBJECT_PREV(*item));
+    else insert_after_repr = NULL;
     while (SP_OBJECT_REPR(child)->childCount()) {
         Inkscape::XML::Node *move_repr = SP_OBJECT_REPR(child)->firstChild();
         Inkscape::GC::anchor(move_repr);
@@ -1569,15 +1656,15 @@ static bool redundant_semi_nesting_processor(SPObject **item, SPObject *child, b
 
     SPCSSAttr *css_child_and_item = sp_repr_css_attr_new();
     SPCSSAttr *css_child_only = sp_repr_css_attr_new();
+    gchar const *item_style = SP_OBJECT_REPR(*item)->attribute("style");
+    if (item_style && *item_style) {
+        sp_repr_css_attr_add_from_string(css_child_and_item, item_style);
+    }
     gchar const *child_style = SP_OBJECT_REPR(child)->attribute("style");
     if (child_style && *child_style) {
         sp_repr_css_attr_add_from_string(css_child_and_item, child_style);
         sp_repr_css_attr_add_from_string(css_child_only, child_style);
     }
-    gchar const *item_style = SP_OBJECT_REPR(*item)->attribute("style");
-    if (item_style && *item_style) {
-        sp_repr_css_attr_add_from_string(css_child_and_item, item_style);
-    }
     bool equal = css_attrs_are_equal(css_child_only, css_child_and_item);
     sp_repr_css_attr_unref(css_child_and_item);
     sp_repr_css_attr_unref(css_child_only);
@@ -1650,6 +1737,7 @@ static bool tidy_operator_styled_whitespace(SPObject **item)
         for ( ; ; ) {   // go up one item in the xml
             test_item = SP_OBJECT_PARENT(test_item);
             if (is_line_break_object(test_item)) break;
+            if (SP_IS_FLOWTEXT(test_item)) return false;
             SPObject *next = SP_OBJECT_NEXT(test_item);
             if (next) {
                 test_item = next;
@@ -1777,9 +1865,24 @@ void sp_te_apply_style(SPItem *text, Inkscape::Text::Layout::iterator const &sta
     The recursion may involve creating new spans.
     */
     SPObject *common_ancestor = get_common_ancestor(text, start_item, end_item);
+
+    // bug #168370 (consider parent transform and viewBox)
+    // snipplet copied from desktop-style.cpp sp_desktop_apply_css_recursive(...)
+    SPCSSAttr *css_set = sp_repr_css_attr_new();
+    sp_repr_css_merge(css_set, (SPCSSAttr*) css);
+    {
+        Geom::Matrix const local(sp_item_i2doc_affine(SP_ITEM(common_ancestor)));
+        double const ex(local.descrim());
+        if ( ( ex != 0. )
+             && ( ex != 1. ) ) {
+            sp_css_attr_scale(css_set, 1/ex);
+        }
+    }
+
     start_item = ascend_while_first(start_item, start_text_iter, common_ancestor);
     end_item = ascend_while_first(end_item, end_text_iter, common_ancestor);
-    recursively_apply_style(common_ancestor, css, start_item, start_text_iter, end_item, end_text_iter, span_name_for_text_object(text));
+    recursively_apply_style(common_ancestor, css_set, start_item, start_text_iter, end_item, end_text_iter, span_name_for_text_object(text));
+    sp_repr_css_attr_unref(css_set);
 
     /* stage 2: cleanup the xml tree (of which there are multiple passes) */
     /* discussion: this stage requires a certain level of inventiveness because
@@ -1793,12 +1896,43 @@ void sp_te_apply_style(SPItem *text, Inkscape::Text::Layout::iterator const &sta
     and neither option can be made to work, a fallback could be to reduce
     everything to a single level of nesting and drop all pretence of
     roundtrippability. */
-    while (tidy_xml_tree_recursively(common_ancestor));
+    while (tidy_xml_tree_recursively(common_ancestor)){};
 
     // if we only modified subobjects this won't have been automatically sent
     text->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG);
 }
 
+bool is_part_of_text_subtree (SPObject *obj)
+{
+    return (SP_IS_TSPAN(obj) 
+            || SP_IS_TEXT(obj) 
+            || SP_IS_FLOWTEXT(obj)
+            || SP_IS_FLOWTSPAN(obj)
+            || SP_IS_FLOWDIV(obj)
+            || SP_IS_FLOWPARA(obj)
+            || SP_IS_FLOWLINE(obj)
+            || SP_IS_FLOWREGIONBREAK(obj));
+}
+
+bool is_top_level_text_object (SPObject *obj)
+{
+    return (SP_IS_TEXT(obj) 
+            || SP_IS_FLOWTEXT(obj));
+}
+
+bool has_visible_text (SPObject *obj)
+{
+    if (SP_IS_STRING(obj) && !SP_STRING(obj)->string.empty()) 
+        return true; // maybe we should also check that it's not all whitespace?
+
+    for (SPObject const *child = obj->firstChild() ; child ; child = SP_OBJECT_NEXT(child)) {
+        if (has_visible_text((SPObject *) child))
+            return true;
+    }
+
+    return false;
+}
+
 /*
   Local Variables:
   mode:c++