Code

Avoid crash by uninitialized perspectives.
[inkscape.git] / src / sp-tspan.cpp
index 8ec7af1500c2e4d4e40194399c7356e581b94719..89a86218efb37d0318ce3a0d35505ad8a4ab9ab4 100644 (file)
 # include "config.h"
 #endif
 
+#include <cstring>
+#include <string>
+#include <glibmm/i18n.h>
+
 #include <livarot/Path.h>
 #include "svg/stringstream.h"
 #include "attributes.h"
 #include "sp-use-reference.h"
 #include "sp-tspan.h"
+#include "sp-tref.h"
 #include "sp-textpath.h"
+#include "text-editing.h"
+#include "style.h"
+#include "libnr/nr-matrix-fns.h"
 #include "xml/repr.h"
+#include "document.h"
 
 
 /*#####################################################
@@ -48,7 +57,9 @@ static void sp_tspan_release(SPObject *object);
 static void sp_tspan_set(SPObject *object, unsigned key, gchar const *value);
 static void sp_tspan_update(SPObject *object, SPCtx *ctx, guint flags);
 static void sp_tspan_modified(SPObject *object, unsigned flags);
-static Inkscape::XML::Node *sp_tspan_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static void sp_tspan_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const flags);
+static Inkscape::XML::Node *sp_tspan_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
+static char *sp_tspan_description (SPItem *item);
 
 static SPItemClass *tspan_parent_class;
 
@@ -94,6 +105,9 @@ sp_tspan_class_init(SPTSpanClass *classname)
     sp_object_class->update = sp_tspan_update;
     sp_object_class->modified = sp_tspan_modified;
     sp_object_class->write = sp_tspan_write;
+
+    item_class->bbox = sp_tspan_bbox;
+    item_class->description = sp_tspan_description;
 }
 
 static void
@@ -136,8 +150,7 @@ sp_tspan_set(SPObject *object, unsigned key, gchar const *value)
     SPTSpan *tspan = SP_TSPAN(object);
        
     if (tspan->attributes.readSingleAttribute(key, value)) {
-        if (tspan->role != SP_TSPAN_ROLE_LINE)
-            object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
+        object->requestDisplayUpdate(SP_OBJECT_MODIFIED_FLAG);
     } else {
         switch (key) {
             case SP_ATTR_SODIPODI_ROLE:
@@ -190,13 +203,39 @@ sp_tspan_modified(SPObject *object, unsigned flags)
     }
 }
 
+static void sp_tspan_bbox(SPItem const *item, NRRect *bbox, Geom::Matrix const &transform, unsigned const /*flags*/)
+{
+    // find out the ancestor text which holds our layout
+    SPObject *parent_text = SP_OBJECT(item);
+    for (; parent_text != NULL && !SP_IS_TEXT(parent_text); parent_text = SP_OBJECT_PARENT (parent_text)){};
+    if (parent_text == NULL) return;
+
+    // get the bbox of our portion of the layout
+    SP_TEXT(parent_text)->layout.getBoundingBox(bbox, transform, sp_text_get_length_upto(parent_text, item), sp_text_get_length_upto(item, NULL) - 1);
+
+    // Add stroke width
+    SPStyle* style=SP_OBJECT_STYLE (item);
+    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 ) {
+                bbox->x0-=0.5*width;
+                bbox->x1+=0.5*width;
+                bbox->y0-=0.5*width;
+                bbox->y1+=0.5*width;
+            }
+        }
+    }
+}
+
 static Inkscape::XML::Node *
-sp_tspan_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+sp_tspan_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     SPTSpan *tspan = SP_TSPAN(object);
-       
+
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        repr = sp_repr_new("svg:tspan");
+        repr = xml_doc->createElement("svg:tspan");
     }
        
     tspan->attributes.writeTo(repr);
@@ -205,12 +244,12 @@ sp_tspan_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
         GSList *l = NULL;
         for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
             Inkscape::XML::Node* c_repr=NULL;
-            if ( SP_IS_TSPAN(child) ) {
-                c_repr = child->updateRepr(NULL, flags);
+            if ( SP_IS_TSPAN(child) || SP_IS_TREF(child) ) {
+                c_repr = child->updateRepr(xml_doc, NULL, flags);
             } else if ( SP_IS_TEXTPATH(child) ) {
-                //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
+                //c_repr = child->updateRepr(xml_doc, NULL, flags); // shouldn't happen
             } else if ( SP_IS_STRING(child) ) {
-                c_repr = sp_repr_new_text(SP_STRING(child)->string.c_str());
+                c_repr = xml_doc->createTextNode(SP_STRING(child)->string.c_str());
             }
             if ( c_repr ) l = g_slist_prepend(l, c_repr);
         }
@@ -221,10 +260,10 @@ sp_tspan_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_TSPAN(child) ) {
+            if ( SP_IS_TSPAN(child) || SP_IS_TREF(child) ) {
                 child->updateRepr(flags);
             } else if ( SP_IS_TEXTPATH(child) ) {
-                //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
+                //c_repr = child->updateRepr(xml_doc, NULL, flags); // shouldn't happen
             } else if ( SP_IS_STRING(child) ) {
                 SP_OBJECT_REPR(child)->setContent(SP_STRING(child)->string.c_str());
             }
@@ -232,11 +271,20 @@ sp_tspan_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
     }
        
     if (((SPObjectClass *) tspan_parent_class)->write)
-        ((SPObjectClass *) tspan_parent_class)->write(object, repr, flags);
+        ((SPObjectClass *) tspan_parent_class)->write(object, xml_doc, repr, flags);
        
     return repr;
 }
 
+static char *
+sp_tspan_description(SPItem *item)
+{
+    g_return_val_if_fail(SP_IS_TSPAN(item), NULL);
+
+    return g_strdup(_("<b>Text span</b>"));
+}
+
+
 /*#####################################################
 #  SPTEXTPATH
 #####################################################*/
@@ -250,7 +298,7 @@ static void sp_textpath_release(SPObject *object);
 static void sp_textpath_set(SPObject *object, unsigned key, gchar const *value);
 static void sp_textpath_update(SPObject *object, SPCtx *ctx, guint flags);
 static void sp_textpath_modified(SPObject *object, unsigned flags);
-static Inkscape::XML::Node *sp_textpath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *sp_textpath_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 
 static SPItemClass *textpath_parent_class;
 
@@ -358,7 +406,8 @@ sp_textpath_build(SPObject *object, SPDocument *doc, Inkscape::XML::Node *repr)
     }
        
     if ( no_content ) {
-        Inkscape::XML::Node* rch = sp_repr_new_text("");
+        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(doc);
+        Inkscape::XML::Node* rch = xml_doc->createTextNode("");
         repr->addChild(rch, NULL);
     }
        
@@ -421,7 +470,7 @@ void   refresh_textpath_source(SPTextPath* tp)
     tp->sourcePath->sourceDirty=false;
        
     // finalisons
-    if ( tp->sourcePath->originalPath ) { 
+    if ( tp->sourcePath->originalPath ) {
         if (tp->originalPath) {
             delete tp->originalPath;
         }
@@ -452,12 +501,12 @@ sp_textpath_modified(SPObject *object, unsigned flags)
     }
 }
 static Inkscape::XML::Node *
-sp_textpath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+sp_textpath_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
     SPTextPath *textpath = SP_TEXTPATH(object);
-       
+
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        repr = sp_repr_new("svg:textPath");
+        repr = xml_doc->createElement("svg:textPath");
     }
        
     textpath->attributes.writeTo(repr);
@@ -479,12 +528,12 @@ sp_textpath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
         GSList *l = NULL;
         for (SPObject* child = sp_object_first_child(object) ; child != NULL ; child = SP_OBJECT_NEXT(child) ) {
             Inkscape::XML::Node* c_repr=NULL;
-            if ( SP_IS_TSPAN(child) ) {
-                c_repr = child->updateRepr(NULL, flags);
+            if ( SP_IS_TSPAN(child) || SP_IS_TREF(child) ) {
+                c_repr = child->updateRepr(xml_doc, NULL, flags);
             } else if ( SP_IS_TEXTPATH(child) ) {
-                //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
+                //c_repr = child->updateRepr(xml_doc, NULL, flags); // shouldn't happen
             } else if ( SP_IS_STRING(child) ) {
-                c_repr = sp_repr_new_text(SP_STRING(child)->string.c_str());
+                c_repr = xml_doc->createTextNode(SP_STRING(child)->string.c_str());
             }
             if ( c_repr ) l = g_slist_prepend(l, c_repr);
         }
@@ -495,10 +544,10 @@ sp_textpath_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_TSPAN(child) ) {
+            if ( SP_IS_TSPAN(child) || SP_IS_TREF(child) ) {
                 child->updateRepr(flags);
             } else if ( SP_IS_TEXTPATH(child) ) {
-                //c_repr = child->updateRepr(NULL, flags); // shouldn't happen
+                //c_repr = child->updateRepr(xml_doc, NULL, flags); // shouldn't happen
             } else if ( SP_IS_STRING(child) ) {
                 SP_OBJECT_REPR(child)->setContent(SP_STRING(child)->string.c_str());
             }
@@ -506,7 +555,7 @@ sp_textpath_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
     }
        
     if (((SPObjectClass *) textpath_parent_class)->write)
-        ((SPObjectClass *) textpath_parent_class)->write(object, repr, flags);
+        ((SPObjectClass *) textpath_parent_class)->write(object, xml_doc, repr, flags);
        
     return repr;
 }
@@ -530,7 +579,7 @@ sp_textpath_to_text(SPObject *tp)
 
     NRRect bbox;
     sp_item_invoke_bbox(SP_ITEM(text), &bbox, sp_item_i2doc_affine(SP_ITEM(text)), TRUE);
-    NR::Point xy(bbox.x0, bbox.y0);
+    Geom::Point xy(bbox.x0, bbox.y0);
 
     // make a list of textpath children
     GSList *tp_reprs = NULL;
@@ -540,10 +589,10 @@ sp_textpath_to_text(SPObject *tp)
 
     for ( GSList *i = tp_reprs ; i ; i = i->next ) {
         // make a copy of each textpath child
-        Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) i->data)->duplicate();
+        Inkscape::XML::Node *copy = ((Inkscape::XML::Node *) i->data)->duplicate(SP_OBJECT_REPR(text)->document());
         // remove the old repr from under textpath
-        SP_OBJECT_REPR(tp)->removeChild((Inkscape::XML::Node *) i->data); 
-        // put its copy into under textPath
+        SP_OBJECT_REPR(tp)->removeChild((Inkscape::XML::Node *) i->data);
+        // put its copy under text
         SP_OBJECT_REPR(text)->addChild(copy, NULL); // fixme: copy id
     }
 
@@ -551,11 +600,11 @@ sp_textpath_to_text(SPObject *tp)
     tp->deleteObject();
     g_slist_free(tp_reprs);
 
-    // set x/y on text 
+    // set x/y on text
     /* fixme: Yuck, is this really the right test? */
-    if (xy[NR::X] != 1e18 && xy[NR::Y] != 1e18) {
-        sp_repr_set_svg_double(SP_OBJECT_REPR(text), "x", xy[NR::X]);
-        sp_repr_set_svg_double(SP_OBJECT_REPR(text), "y", xy[NR::Y]);
+    if (xy[Geom::X] != 1e18 && xy[Geom::Y] != 1e18) {
+        sp_repr_set_svg_double(SP_OBJECT_REPR(text), "x", xy[Geom::X]);
+        sp_repr_set_svg_double(SP_OBJECT_REPR(text), "y", xy[Geom::Y]);
     }
 }