Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / sp-glyph.cpp
index 0c6d3f1e0dd9541e61b142ff669880c4cd2a671c..6f72381335e323a7a681f1dd4e711b3b7bec4f9c 100644 (file)
@@ -1,10 +1,16 @@
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#ifdef ENABLE_SVG_FONTS
 #define __SP_GLYPH_C__
 
 /*
  * SVG <glyph> element implementation
  *
  * Author:
- *   Felipe C. da S. Sanches <felipe.sanches@gmail.com>
+ *   Felipe C. da S. Sanches <juca@members.fsf.org>
+ *   Abhishek Sharma
  *
  * Copyright (C) 2008, Felipe C. da S. Sanches
  *
@@ -15,7 +21,6 @@
 #include "attributes.h"
 #include "sp-glyph.h"
 #include "document.h"
-#include "helper-fns.h"
 
 static void sp_glyph_class_init(SPGlyphClass *gc);
 static void sp_glyph_init(SPGlyph *glyph);
@@ -23,7 +28,7 @@ static void sp_glyph_init(SPGlyph *glyph);
 static void sp_glyph_build(SPObject *object, SPDocument *document, Inkscape::XML::Node *repr);
 static void sp_glyph_release(SPObject *object);
 static void sp_glyph_set(SPObject *object, unsigned int key, const gchar *value);
-static Inkscape::XML::Node *sp_glyph_write(SPObject *object, Inkscape::XML::Node *repr, guint flags);
+static Inkscape::XML::Node *sp_glyph_write(SPObject *object, Inkscape::XML::Document *doc, Inkscape::XML::Node *repr, guint flags);
 static void sp_glyph_update(SPObject *object, SPCtx *ctx, guint flags);
 
 static SPObjectClass *parent_class;
@@ -35,15 +40,15 @@ GType sp_glyph_get_type(void)
     if (!type) {
         GTypeInfo info = {
             sizeof(SPGlyphClass),
-            NULL,      /* base_init */
-            NULL,      /* base_finalize */
+            NULL,       /* base_init */
+            NULL,       /* base_finalize */
             (GClassInitFunc) sp_glyph_class_init,
-            NULL,      /* class_finalize */
-            NULL,      /* class_data */
+            NULL,       /* class_finalize */
+            NULL,       /* class_data */
             sizeof(SPGlyph),
-            16,        /* n_preallocs */
+            16, /* n_preallocs */
             (GInstanceInitFunc) sp_glyph_init,
-            NULL,      /* value_table */
+            NULL,       /* value_table */
         };
         type = g_type_register_static(SP_TYPE_OBJECT, "SPGlyph", &info, (GTypeFlags) 0);
     }
@@ -67,8 +72,9 @@ static void sp_glyph_class_init(SPGlyphClass *gc)
 static void sp_glyph_init(SPGlyph *glyph)
 {
 //TODO: correct these values:
-    glyph->unicode = NULL;
-    glyph->glyph_name = NULL;
+
+    new (&glyph->unicode) Glib::ustring();
+    new (&glyph->glyph_name) Glib::ustring();
     glyph->d = NULL;
     glyph->orientation = GLYPH_ORIENTATION_BOTH;
     glyph->arabic_form = GLYPH_ARABIC_FORM_INITIAL;
@@ -85,16 +91,16 @@ static void sp_glyph_build(SPObject *object, SPDocument *document, Inkscape::XML
         ((SPObjectClass *) (parent_class))->build(object, document, repr);
     }
 
-    sp_object_read_attr(object, "unicode");
-    sp_object_read_attr(object, "glyph-name");
-    sp_object_read_attr(object, "d");
-    sp_object_read_attr(object, "orientation");
-    sp_object_read_attr(object, "arabic-form");
-    sp_object_read_attr(object, "lang");
-    sp_object_read_attr(object, "horiz-adv-x");
-    sp_object_read_attr(object, "vert-origin-x");
-    sp_object_read_attr(object, "vert-origin-y");
-    sp_object_read_attr(object, "vert-adv-y");
+    object->readAttr( "unicode" );
+    object->readAttr( "glyph-name" );
+    object->readAttr( "d" );
+    object->readAttr( "orientation" );
+    object->readAttr( "arabic-form" );
+    object->readAttr( "lang" );
+    object->readAttr( "horiz-adv-x" );
+    object->readAttr( "vert-origin-x" );
+    object->readAttr( "vert-origin-y" );
+    object->readAttr( "vert-adv-y" );
 }
 
 static void sp_glyph_release(SPObject *object)
@@ -140,88 +146,97 @@ static glyphOrientation sp_glyph_read_orientation(gchar const *value){
 static void sp_glyph_set(SPObject *object, unsigned int key, const gchar *value)
 {
     SPGlyph *glyph = SP_GLYPH(object);
-    double number;
-    glyphOrientation orient;
-    glyphArabicForm form;
 
     switch (key) {
         case SP_ATTR_UNICODE:
-            if (glyph->unicode) g_free(glyph->unicode);
-            glyph->unicode = g_strdup(value);
+        {
+            glyph->unicode.clear();
+            if (value) glyph->unicode.append(value);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
-g_warning("<glyph>: SP_ATTR_UNICODE: %s", value);
-             break;
+            break;
+        }
         case SP_ATTR_GLYPH_NAME:
-            if (glyph->glyph_name) g_free(glyph->glyph_name);
-            glyph->glyph_name = g_strdup(value);
+        {
+            glyph->glyph_name.clear();
+            if (value) glyph->glyph_name.append(value);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
-g_warning("<glyph>: SP_ATTR_GLYPH_NAME: %s", value);
-             break;
+            break;
+        }
         case SP_ATTR_D:
+        {
             if (glyph->d) g_free(glyph->d);
             glyph->d = g_strdup(value);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
-g_warning("<glyph>: SP_ATTR_D: %s", value);
             break;
+        }
         case SP_ATTR_ORIENTATION:
-            orient = sp_glyph_read_orientation(value);
+        {
+            glyphOrientation orient = sp_glyph_read_orientation(value);
             if (glyph->orientation != orient){
                 glyph->orientation = orient;
                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             }
-g_warning("<glyph>: SP_ATTR_ORIENTATION: %d", orient);
             break;
+        }
         case SP_ATTR_ARABIC_FORM:
-            form = sp_glyph_read_arabic_form(value);
+        {
+            glyphArabicForm form = sp_glyph_read_arabic_form(value);
             if (glyph->arabic_form != form){
                 glyph->arabic_form = form;
                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             }
-g_warning("<glyph>: SP_ATTR_ARABIC_FORM: %d", glyph->arabic_form);
             break;
+        }
         case SP_ATTR_LANG:
+        {
             if (glyph->lang) g_free(glyph->lang);
             glyph->lang = g_strdup(value);
-g_warning("<glyph>: SP_ATTR_LANG: %s", value);
             object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
-       case SP_ATTR_HORIZ_ADV_X:
-            number = helperfns_read_number(value);
+        }
+        case SP_ATTR_HORIZ_ADV_X:
+        {
+            double number = value ? g_ascii_strtod(value, 0) : 0;
             if (number != glyph->horiz_adv_x){
                 glyph->horiz_adv_x = number;
-g_warning("<glyph>: SP_ATTR_HORIZ_ADV_X: %f", number);
                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             }
             break;
-       case SP_ATTR_VERT_ORIGIN_X:
-            number = helperfns_read_number(value);
+        }
+        case SP_ATTR_VERT_ORIGIN_X:
+        {
+            double number = value ? g_ascii_strtod(value, 0) : 0;
             if (number != glyph->vert_origin_x){
                 glyph->vert_origin_x = number;
-g_warning("<glyph>: SP_ATTR_VERT_ORIGIN_X: %f", number);
                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             }
             break;
-       case SP_ATTR_VERT_ORIGIN_Y:
-            number = helperfns_read_number(value);
+        }
+        case SP_ATTR_VERT_ORIGIN_Y:
+        {
+            double number = value ? g_ascii_strtod(value, 0) : 0;
             if (number != glyph->vert_origin_y){
                 glyph->vert_origin_y = number;
-g_warning("<glyph>: SP_ATTR_VERT_ORIGIN_Y: %f", number);
                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             }
             break;
-       case SP_ATTR_VERT_ADV_Y:
-            number = helperfns_read_number(value);
+        }
+        case SP_ATTR_VERT_ADV_Y:
+        {
+            double number = value ? g_ascii_strtod(value, 0) : 0;
             if (number != glyph->vert_adv_y){
                 glyph->vert_adv_y = number;
-g_warning("<glyph>: SP_ATTR_VERT_ADV_Y: %f", number);
                 object->requestModified(SP_OBJECT_MODIFIED_FLAG);
             }
             break;
-       default:
+        }
+        default:
+        {
             if (((SPObjectClass *) (parent_class))->set) {
                 ((SPObjectClass *) (parent_class))->set(object, key, value);
             }
             break;
+        }
     }
 }
 
@@ -236,16 +251,16 @@ sp_glyph_update(SPObject *object, SPCtx *ctx, guint flags)
 
     if (flags & SP_OBJECT_MODIFIED_FLAG) {
         /* do something to trigger redisplay, updates? */
-            sp_object_read_attr(object, "unicode");
-            sp_object_read_attr(object, "glyph-name");
-            sp_object_read_attr(object, "d");
-            sp_object_read_attr(object, "orientation");
-            sp_object_read_attr(object, "arabic-form");
-            sp_object_read_attr(object, "lang");
-            sp_object_read_attr(object, "horiz-adv-x");
-            sp_object_read_attr(object, "vert-origin-x");
-            sp_object_read_attr(object, "vert-origin-y");
-            sp_object_read_attr(object, "vert-adv-y");
+            object->readAttr( "unicode" );
+            object->readAttr( "glyph-name" );
+            object->readAttr( "d" );
+            object->readAttr( "orientation" );
+            object->readAttr( "arabic-form" );
+            object->readAttr( "lang" );
+            object->readAttr( "horiz-adv-x" );
+            object->readAttr( "vert-origin-x" );
+            object->readAttr( "vert-origin-y" );
+            object->readAttr( "vert-adv-y" );
     }
 
     if (((SPObjectClass *) parent_class)->update) {
@@ -255,12 +270,11 @@ sp_glyph_update(SPObject *object, SPCtx *ctx, guint flags)
 
 #define COPY_ATTR(rd,rs,key) (rd)->setAttribute((key), rs->attribute(key));
 
-static Inkscape::XML::Node *sp_glyph_write(SPObject *object, Inkscape::XML::Node *repr, guint flags)
+static Inkscape::XML::Node *sp_glyph_write(SPObject *object, Inkscape::XML::Document *xml_doc, Inkscape::XML::Node *repr, guint flags)
 {
 //    SPGlyph *glyph = SP_GLYPH(object);
 
     if ((flags & SP_OBJECT_WRITE_BUILD) && !repr) {
-        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_OBJECT_DOCUMENT(object));
         repr = xml_doc->createElement("svg:glyph");
     }
 
@@ -277,25 +291,27 @@ static Inkscape::XML::Node *sp_glyph_write(SPObject *object, Inkscape::XML::Node
     sp_repr_set_svg_double(repr, "vert-adv-y", glyph->vert_adv_y);
 */
     if (repr != SP_OBJECT_REPR(object)) {
-        COPY_ATTR(repr, object->repr, "unicode");
-        COPY_ATTR(repr, object->repr, "glyph-name");
-        COPY_ATTR(repr, object->repr, "d");
-        COPY_ATTR(repr, object->repr, "orientation");
-        COPY_ATTR(repr, object->repr, "arabic-form");
-        COPY_ATTR(repr, object->repr, "lang");
-        COPY_ATTR(repr, object->repr, "horiz-adv-x");
-        COPY_ATTR(repr, object->repr, "vert-origin-x");
-        COPY_ATTR(repr, object->repr, "vert-origin-y");
-        COPY_ATTR(repr, object->repr, "vert-adv-y");
+        // All the COPY_ATTR functions below use
+        //   XML Tree directly while they shouldn't.
+        COPY_ATTR(repr, object->getRepr(), "unicode");
+        COPY_ATTR(repr, object->getRepr(), "glyph-name");
+        COPY_ATTR(repr, object->getRepr(), "d");
+        COPY_ATTR(repr, object->getRepr(), "orientation");
+        COPY_ATTR(repr, object->getRepr(), "arabic-form");
+        COPY_ATTR(repr, object->getRepr(), "lang");
+        COPY_ATTR(repr, object->getRepr(), "horiz-adv-x");
+        COPY_ATTR(repr, object->getRepr(), "vert-origin-x");
+        COPY_ATTR(repr, object->getRepr(), "vert-origin-y");
+        COPY_ATTR(repr, object->getRepr(), "vert-adv-y");
     }
 
     if (((SPObjectClass *) (parent_class))->write) {
-        ((SPObjectClass *) (parent_class))->write(object, repr, flags);
+        ((SPObjectClass *) (parent_class))->write(object, xml_doc, repr, flags);
     }
 
     return repr;
 }
-
+#endif //#ifdef ENABLE_SVG_FONTS
 /*
   Local Variables:
   mode:c++