Code

clean up tabs and DOS-ishness
[inkscape.git] / src / svg / svg-path.cpp
index b37814164eb1c2bca3f2557eabfda04386bc7de9..0239d2ae0cd843d32c916982ff9170fdee0d5007 100644 (file)
@@ -1,39 +1,42 @@
 #define __SP_SVG_PARSE_C__
-/* 
+/*
    svg-path.c: Parse SVG path element data into bezier path.
+
    Copyright (C) 2000 Eazel, Inc.
    Copyright (C) 2000 Lauris Kaplinski
    Copyright (C) 2001 Ximian, Inc.
-  
+
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.
-  
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.
-  
+
    You should have received a copy of the GNU General Public
    License along with this program; if not, write to the
    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
-  
+
    Authors:
      Raph Levien <raph@artofcode.com>
      Lauris Kaplinski <lauris@ximian.com>
 */
 
+#include <cstring>
+#include <string>
 #include <cassert>
 #include <glib/gmem.h>
 #include <glib/gmessages.h>
 #include <glib/gstrfuncs.h>
+#include <glib.h> // g_assert()
 
 #include "libnr/n-art-bpath.h"
 #include "gnome-canvas-bpath-util.h"
-#include "stringstream.h"
+#include "svg/path-string.h"
 
 
 /* This module parses an SVG path element into an RsvgBpathDef.
@@ -63,7 +66,7 @@ struct RSVGParsePathCtx {
     double spx, spy;  /* beginning of current subpath point */
     char cmd;         /* current command (lowercase) */
     int param;        /* parameter number */
-    bool rel;     /* true if relative coords */
+    bool rel;         /* true if relative coords */
     double params[7]; /* parameters that have been parsed */
 };
 
@@ -238,8 +241,7 @@ static void rsvg_parse_path_do_cmd(RSVGParsePathCtx *ctx, bool final)
     switch (ctx->cmd) {
     case 'm':
         /* moveto */
-        if (ctx->param == 2
-            || final)
+        if (ctx->param == 2 || final)
         {
             rsvg_parse_path_default_xy (ctx, 2);
 #ifdef VERBOSE
@@ -256,8 +258,7 @@ static void rsvg_parse_path_do_cmd(RSVGParsePathCtx *ctx, bool final)
         break;
     case 'l':
         /* lineto */
-        if (ctx->param == 2
-            || final)
+        if (ctx->param == 2 || final)
         {
             rsvg_parse_path_default_xy (ctx, 2);
 #ifdef VERBOSE
@@ -273,8 +274,7 @@ static void rsvg_parse_path_do_cmd(RSVGParsePathCtx *ctx, bool final)
         break;
     case 'c':
         /* curveto */
-        if ( ( ctx->param == 6 )
-             || final )
+        if (ctx->param == 6 || final )
         {
             rsvg_parse_path_default_xy (ctx, 6);
             x1 = ctx->params[0];
@@ -298,8 +298,7 @@ static void rsvg_parse_path_do_cmd(RSVGParsePathCtx *ctx, bool final)
         break;
     case 's':
         /* smooth curveto */
-        if ( ( ctx->param == 4 )
-             || final )
+        if (ctx->param == 4 || final)
         {
             rsvg_parse_path_default_xy (ctx, 4);
             x1 = 2 * ctx->cpx - ctx->rpx;
@@ -455,10 +454,10 @@ static void rsvg_parse_path_data(RSVGParsePathCtx *ctx, const char *data)
     int i = 0;
     double val = 0;
     char c = 0;
-    bool in_num = FALSE;
-    bool in_frac = FALSE;
-    bool in_exp = FALSE;
-    bool exp_wait_sign = FALSE;
+    bool in_num = false;
+    bool in_frac = false;
+    bool in_exp = false;
+    bool exp_wait_sign = false;
     int sign = 0;
     int exp = 0;
     int exp_sign = 0;
@@ -479,7 +478,7 @@ static void rsvg_parse_path_data(RSVGParsePathCtx *ctx, const char *data)
                 if (in_exp)
                 {
                     exp = (exp * 10) + c - '0';
-                    exp_wait_sign = FALSE;
+                    exp_wait_sign = false;
                 }
                 else if (in_frac)
                     val += (frac *= 0.1) * (c - '0');
@@ -488,11 +487,11 @@ static void rsvg_parse_path_data(RSVGParsePathCtx *ctx, const char *data)
             }
             else
             {
-                in_num = TRUE;
+                in_num = true;
                 assert(!in_frac && !in_exp);
                 exp = 0;
                 exp_sign = 1;
-                exp_wait_sign = FALSE;
+                exp_wait_sign = false;
                 val = c - '0';
                 sign = 1;
             }
@@ -501,23 +500,23 @@ static void rsvg_parse_path_data(RSVGParsePathCtx *ctx, const char *data)
         {
             if (!in_num)
             {
-                in_num = TRUE;
+                in_num = true;
                 assert(!in_exp);
                 exp = 0;
                 exp_sign = 1;
-                exp_wait_sign = FALSE;
+                exp_wait_sign = false;
                 val = 0;
                 sign = 1;
             }
-            in_frac = TRUE;
+            in_frac = true;
             frac = 1;
         }
         else if ((c == 'E' || c == 'e') && in_num)
         {
             /* fixme: Should we add `&& !in_exp' to the above condition?
              * It looks like the current code will parse `1e3e4' (as 1e4). */
-            in_exp = TRUE;
-            exp_wait_sign = TRUE;
+            in_exp = true;
+            exp_wait_sign = true;
             exp = 0;
             exp_sign = 1;
         }
@@ -568,18 +567,18 @@ static void rsvg_parse_path_data(RSVGParsePathCtx *ctx, const char *data)
                 }
             }
             ctx->params[ctx->param++] = val;
-            rsvg_parse_path_do_cmd (ctx, FALSE);
+            rsvg_parse_path_do_cmd (ctx, false);
             if (c=='.') {
-                in_num = TRUE;
+                in_num = true;
                 val = 0;
-                in_frac = TRUE;
-                in_exp = FALSE;
+                in_frac = true;
+                in_exp = false;
                 frac = 1;
             }
             else {
-                in_num = FALSE;
-                in_frac = FALSE;
-                in_exp = FALSE;
+                in_num = false;
+                in_frac = false;
+                in_exp = false;
             }
         }
 
@@ -589,17 +588,17 @@ static void rsvg_parse_path_data(RSVGParsePathCtx *ctx, const char *data)
         {
             sign = c == '+' ? 1 : -1;;
             val = 0;
-            in_num = TRUE;
-            in_frac = FALSE;
-            in_exp = FALSE;
+            in_num = true;
+            in_frac = false;
+            in_exp = false;
             exp = 0;
             exp_sign = 1;
-            exp_wait_sign = FALSE;
+            exp_wait_sign = false;
         }
         else if (c == 'z' || c == 'Z')
         {
             if (ctx->param)
-                rsvg_parse_path_do_cmd (ctx, TRUE);
+                rsvg_parse_path_do_cmd (ctx, true);
             rsvg_bpath_def_closepath (ctx->bpath);
 
             ctx->cmd = 'm';
@@ -610,16 +609,16 @@ static void rsvg_parse_path_data(RSVGParsePathCtx *ctx, const char *data)
         else if (c >= 'A' && c <= 'Z' && c != 'E')
         {
             if (ctx->param)
-                rsvg_parse_path_do_cmd (ctx, TRUE);
+                rsvg_parse_path_do_cmd (ctx, true);
             ctx->cmd = c + 'a' - 'A';
-            ctx->rel = FALSE;
+            ctx->rel = false;
         }
         else if (c >= 'a' && c <= 'z' && c != 'e')
         {
             if (ctx->param)
-                rsvg_parse_path_do_cmd (ctx, TRUE);
+                rsvg_parse_path_do_cmd (ctx, true);
             ctx->cmd = c;
-            ctx->rel = TRUE;
+            ctx->rel = true;
         }
         /* else c _should_ be whitespace or , */
     }
@@ -660,42 +659,38 @@ gchar *sp_svg_write_path(NArtBpath const *bpath)
     
     g_return_val_if_fail (bpath != NULL, NULL);
 
+    Inkscape::SVG::PathString str;
+
     for (int i = 0; bpath[i].code != NR_END; i++){
-        if (i) {
-            os << " ";
-        }
         switch (bpath [i].code){
         case NR_LINETO:
-            os << "L " << bpath[i].x3 << "," << bpath[i].y3;
+            str.lineTo(bpath[i].x3, bpath[i].y3);
             break;
 
         case NR_CURVETO:
-            os << "C " << bpath[i].x1 << "," << bpath[i].y1
-               << " "  << bpath[i].x2 << "," << bpath[i].y2
-               << " "  << bpath[i].x3 << "," << bpath[i].y3;
+            str.curveTo(bpath[i].x1, bpath[i].y1,
+                        bpath[i].x2, bpath[i].y2,
+                        bpath[i].x3, bpath[i].y3);
             break;
 
         case NR_MOVETO_OPEN:
         case NR_MOVETO:
             if (closed) {
-                os << "z ";
+                str.closePath();
             }
             closed = ( bpath[i].code == NR_MOVETO );
-            os << "M " << bpath[i].x3 << "," << bpath[i].y3;
+            str.moveTo(bpath[i].x3, bpath[i].y3);
             break;
+
         default:
             g_assert_not_reached ();
         }
     }
     if (closed) {
-        os << " z ";
+        str.closePath();
     }
 
-//    std::string s = os.str();
-//    gchar *ret = g_strdup(s.c_str());
-//    delete (s);
-//    return ret;
-    return g_strdup (os.str().c_str());
+    return g_strdup(str.c_str());
 }
 
 /*