Mesh replaces Polygon
[org.ibex.core.git] / src / org / ibex / graphics / Path.java
index 690924d..2b5be3c 100644 (file)
@@ -2,7 +2,6 @@
 // Licensed under the GNU General Public License version 2 ("the License").
 // You may not use this file except in compliance with the License.
 
-// FIXME
 package org.ibex.graphics;
 import java.util.*;
 import org.ibex.util.*;
@@ -14,38 +13,165 @@ public class Path {
     public static final float INCHES_PER_CM = (float)0.3937;
     public static final float INCHES_PER_MM = INCHES_PER_CM / 10;
     private static final int DEFAULT_PATHLEN = 1000;
+    private static final int NUMSTEPS = 10;
     private static final float PI = (float)Math.PI;
 
-    // the number of vertices on this path
-    int numvertices = 0;
+    boolean closed = false;
+    Curve head = null;
+    Curve tail = null;
+    protected void add(Curve c) {
+        if (head==null) { tail=head=c; return; }
+        c.prev = tail;
+        tail.next = c;
+        tail = c;
+    }
 
-    // the vertices of the path
-    float[] x = new float[DEFAULT_PATHLEN];
-    float[] y = new float[DEFAULT_PATHLEN];
+    public void addTo(Mesh m, boolean evenOdd) {
+        for(Curve c = head; c != null; c = c.next) c.addTo(m);
+        m.setIn(evenOdd);
+    }
 
-    // the type of each edge; type[i] is the type of the edge from x[i],y[i] to x[i+1],y[i+1]
-    byte[] type = new byte[DEFAULT_PATHLEN];
+    abstract class Curve {
+        Curve next, prev;
+        float x, y;
+        float c1x, c1y, c2x, c2y;
+        public Curve() { }
+        public abstract void addTo(Mesh ret);
+    }
 
-    // bezier control points
-    float[] c1x = new float[DEFAULT_PATHLEN];  // or rx (arcto)
-    float[] c1y = new float[DEFAULT_PATHLEN];  // or ry (arcto)
-    float[] c2x = new float[DEFAULT_PATHLEN];  // or x-axis-rotation (arcto)
-    float[] c2y = new float[DEFAULT_PATHLEN];  // or large-arc << 1 | sweep (arcto)
+    class Line extends Curve {
+        public void addTo(Mesh ret) {
+            float rx = next.x;
+            float ry = next.y;
+            ret.add(rx,ry);
+        }
+    }
 
-    boolean closed = false;
+    class Move extends Curve {
+        public void addTo(Mesh ret) {
+            ret.newcontour();
+            if (next==null) return;
+            float rx = next.x;
+            float ry = next.y;
+            ret.add(rx, ry);
+        }
+    }
 
-    static final byte TYPE_MOVETO = 0;
-    static final byte TYPE_LINETO = 1;
-    static final byte TYPE_ARCTO = 2;
-    static final byte TYPE_CUBIC = 3;
-    static final byte TYPE_QUADRADIC = 4;
+    class Arc extends Curve {
+        public void addTo(Mesh ret) {
+            System.out.println("ARC!");
+            float rx = c1x;
+            float ry = c1y;
+            float phi = c2x;
+            float fa = ((int)c2y) >> 1;
+            float fs = ((int)c2y) & 1;
+            float x1 = x;
+            float y1 = y;
+            float x2 = next.x;
+            float y2 = next.y;
+
+            // F.6.5: given x1,y1,x2,y2,fa,fs, compute cx,cy,theta1,dtheta
+            float x1_ = (float)Math.cos(phi) * (x1 - x2) / 2 + (float)Math.sin(phi) * (y1 - y2) / 2;
+            float y1_ = -1 * (float)Math.sin(phi) * (x1 - x2) / 2 + (float)Math.cos(phi) * (y1 - y2) / 2;
+            float tmp = (float)Math.sqrt((rx * rx * ry * ry - rx * rx * y1_ * y1_ - ry * ry * x1_ * x1_) /
+                                         (rx * rx * y1_ * y1_ + ry * ry * x1_ * x1_));
+            float cx_ = (fa == fs ? -1 : 1) * tmp * (rx * y1_ / ry);
+            float cy_ = (fa == fs ? -1 : 1) * -1 * tmp * (ry * x1_ / rx);
+            float cx = (float)Math.cos(phi) * cx_ - (float)Math.sin(phi) * cy_ + (x1 + x2) / 2;
+            float cy = (float)Math.sin(phi) * cx_ + (float)Math.cos(phi) * cy_ + (y1 + y2) / 2;
+            
+            // F.6.4 Conversion from center to endpoint parameterization
+            float ux = 1, uy = 0, vx = (x1_ - cx_) / rx, vy = (y1_ - cy_) / ry;
+            float det = ux * vy - uy * vx;
+            float theta1 = (det < 0 ? -1 : 1) *
+                (float)Math.acos((ux * vx + uy * vy) / 
+                                 ((float)Math.sqrt(ux * ux + uy * uy) * (float)Math.sqrt(vx * vx + vy * vy)));
+            ux = (x1_ - cx_) / rx; uy = (y1_ - cy_) / ry;
+            vx = (-1 * x1_ - cx_) / rx; vy = (-1 * y1_ - cy_) / ry;
+            det = ux * vy - uy * vx;
+            float dtheta = (det < 0 ? -1 : 1) *
+                (float)Math.acos((ux * vx + uy * vy) / 
+                                 ((float)Math.sqrt(ux * ux + uy * uy) * (float)Math.sqrt(vx * vx + vy * vy)));
+            dtheta = dtheta % (float)(2 * Math.PI);
+            
+            if (fs == 0 && dtheta > 0) theta1 -= 2 * PI; 
+            if (fs == 1 && dtheta < 0) theta1 += 2 * PI;
+            
+            if (fa == 1 && dtheta < 0) dtheta = 2 * PI + dtheta;
+            else if (fa == 1 && dtheta > 0) dtheta = -1 * (2 * PI - dtheta);
+
+            // FIXME: integrate F.6.6
+            // FIXME: isn't quite ending where it should...
+
+            // F.6.3: Parameterization alternatives
+            float theta = theta1;
+            for(int j=0; j<NUMSTEPS; j++) {
+                float rasterx = rx * (float)Math.cos(theta) * (float)Math.cos(phi) -
+                    ry * (float)Math.sin(theta) * (float)Math.sin(phi) + cx;
+                float rastery = rx * (float)Math.cos(theta) * (float)Math.sin(phi) +
+                    ry * (float)Math.cos(phi) * (float)Math.sin(theta) + cy;
+                ret.add(rasterx, rastery);
+                theta += dtheta / NUMSTEPS;
+            }
+        }
+    }
 
-    // FIXME: hack
-    private String toString;
-    public String toString() { return toString; }
+    class Bezier extends Curve {
+        float cx, cy;
+        public void addTo(Mesh ret) {
+            float ax = next.x - 3 * c2x + 3 * c1x - x;
+            float bx = 3 * c2x - 6 * c1x + 3 * x;
+            float cx = 3 * c1x - 3 * x;
+            float dx = x;
+            float ay = next.y - 3 * c2y + 3 * c1y - y;
+            float by = 3 * c2y - 6 * c1y + 3 * y;
+            float cy = 3 * c1y - 3 * y;
+            float dy = y;
+            
+            float x0 = x;
+            float y0 = y;
+            float x1 = next.x;
+            float y1 = next.y;
+            float steps = (float)Math.sqrt( (x1-x0) * (x1-x0) + (y1-y0) * (y1-y0) );
+            
+            //for(float t=0; t<1; t += 0.5) {
+            for(float t=0; t<1; t += 1/(steps/20)) {
+                float rx = ax * t * t * t + bx * t * t + cx * t + dx;
+                float ry = ay * t * t * t + by * t * t + cy * t + dy;
+                ret.add(rx,ry);
+            }
+        }
+    }
+
+    class QuadBezier extends Curve {
+        float cx, cy, cx2, cy2;
+        public void addTo(Mesh ret) {
+            throw new Error("doesn't work yet");
+            /*
+            float bx = next.x - 2 * c1x + x;
+            float cx = 2 * c1x - 2 * x;
+            float dx = x;
+            float by = next.y - 2 * c1y + y;
+            float cy = 2 * c1y - 2 * y;
+            float dy = y;
+                       
+            float x0 = a.multiply_px(x, y);
+            float y0 = a.multiply_py(x, y);
+            float x1 = a.multiply_px(next.x, next.y);
+            float y1 = a.multiply_py(next.x, next.y);
+            float steps = (float)Math.sqrt( (x1-x0) * (x1-x0) + (y1-y0) * (y1-y0) );
+
+            for(float t=0; t<1; t += 1 / (steps/20)) {
+                float rx = bx * t * t + cx * t + dx;
+                float ry = by * t * t + cy * t + dy;
+                ret.add(a.multiply_px(rx, ry), a.multiply_py(rx, ry));
+            }
+            */
+        }
+    }
 
     public Path(String s) {
-        this.toString = s;
+        //this.toString = s;
         Tokenizer t = new Tokenizer(s);
         char last_command = 'M';
         boolean first = true;
@@ -66,9 +192,9 @@ public class Path {
         // FIXME wrong
         float min = Float.MAX_VALUE;
         float max = Float.MIN_VALUE;
-        for(int i=0; i<numvertices; i++) {
-            min = Math.min(min, a.multiply_px(x[i], y[i]));
-            max = Math.max(max, a.multiply_px(x[i], y[i]));
+        for(Curve c = head; c != null; c = c.next) {
+            min = Math.min(min, a.multiply_px(c.x, c.y));
+            max = Math.max(max, a.multiply_px(c.x, c.y));
         }
         return Encode.twoFloatsToLong(max, min);
     }
@@ -78,18 +204,18 @@ public class Path {
     public long transform(Affine a, boolean forReal, boolean widthheight) {
         float minx = Integer.MAX_VALUE; float miny = Integer.MAX_VALUE;
         float maxx = Integer.MIN_VALUE; float maxy = Integer.MIN_VALUE;
-        for(int i=0; i<numvertices; i++) {
-            if (type[i] == TYPE_ARCTO) { /* FIXME!!! WRONG!!!! */ continue; }
-            float x   = a.multiply_px(this.x[i],   this.y[i]);    if (x>maxx) maxx = x; if (x<minx) minx = x;
-            float y   = a.multiply_py(this.x[i],   this.y[i]);    if (y>maxy) maxy = y; if (y<miny) miny = y;
-            float c1x = a.multiply_px(this.c1x[i], this.c1y[i]);  if (c1x>maxx) maxx = c1x; if (c1x<minx) minx = c1x;
-            float c1y = a.multiply_py(this.c1x[i], this.c1y[i]);  if (c1y>maxy) maxy = c1y; if (c1y<miny) miny = c1y;
-            float c2x = a.multiply_px(this.c2x[i], this.c2y[i]);  if (c2x>maxx) maxx = c2x; if (c2x<minx) minx = c2x;
-            float c2y = a.multiply_py(this.c2x[i], this.c2y[i]);  if (c2y>maxy) maxy = c2y; if (c2y<miny) miny = c2y;
+        for(Curve c = head; c != null; c = c.next) {
+            if (c instanceof Arc) { /* FIXME!!! WRONG!!!! */ continue; }
+            float x   = a.multiply_px(c.x,   c.y);    if (x>maxx) maxx = x; if (x<minx) minx = x;
+            float y   = a.multiply_py(c.x,   c.y);    if (y>maxy) maxy = y; if (y<miny) miny = y;
+            float c1x = a.multiply_px(c.c1x, c.c1y);  if (c1x>maxx) maxx = c1x; if (c1x<minx) minx = c1x;
+            float c1y = a.multiply_py(c.c1x, c.c1y);  if (c1y>maxy) maxy = c1y; if (c1y<miny) miny = c1y;
+            float c2x = a.multiply_px(c.c2x, c.c2y);  if (c2x>maxx) maxx = c2x; if (c2x<minx) minx = c2x;
+            float c2y = a.multiply_py(c.c2x, c.c2y);  if (c2y>maxy) maxy = c2y; if (c2y<miny) miny = c2y;
             if (forReal) {
-                this.x[i] = x; this.y[i] = y;
-                this.c1x[i] = c1x; this.c1y[i] = c1y;
-                this.c2x[i] = c2x; this.c2y[i] = c2y;
+                c.x = x; c.y = y;
+                c.c1x = c1x; c.c1y = c1y;
+                c.c2x = c2x; c.c2y = c2y;
             }
         }
         if (widthheight) return ((((long)Float.floatToIntBits(maxx - minx)) << 32) | ((long)Float.floatToIntBits(maxy - miny)));
@@ -98,15 +224,14 @@ public class Path {
 
     public void alignToOrigin() {
         float minx = Integer.MAX_VALUE; float miny = Integer.MAX_VALUE;
-        for(int i=0; i<numvertices; i++) { if (x[i] < minx) minx = x[i]; if (y[i] < miny) miny = y[i]; }
-        for(int i=0; i<numvertices; i++) {
-            x[i] -= minx; y[i] -= miny;
-            if (type[i] == TYPE_ARCTO) continue;
-            c1x[i] -= minx; c2x[i] -= minx; c1y[i] -= miny; c2y[i] -= miny;
+        for(Curve c = head; c != null; c = c.next) { if (c.x < minx) minx = c.x; if (c.y < miny) miny = c.y; }
+        for(Curve c = head; c != null; c = c.next) {
+            c.x -= minx; c.y -= miny;
+            if (c instanceof Arc) continue;
+            c.c1x -= minx; c.c2x -= minx; c.c1y -= miny; c.c2y -= miny;
         }
     }
 
-
     public static class Tokenizer {
         // FIXME: check array bounds exception for improperly terminated string
         String s;
@@ -118,6 +243,7 @@ public class Path {
             if (s == null) return null;
             Tokenizer t = new Tokenizer(s);
             Path ret = new Path(s);
+            return ret;/*
             char last_command = 'M';
             boolean first = true;
             while(t.hasMoreTokens()) {
@@ -129,9 +255,8 @@ public class Path {
                 ret.parseSingleCommandAndArguments(t, command, relative);
                 last_command = command;
             }
-            return ret;
+            return ret;*/
         }
-
         private void consumeWhitespace() {
             while(i < s.length() && (Character.isWhitespace(s.charAt(i)))) i++;
             if (i < s.length() && s.charAt(i) == ',') i++;
@@ -177,263 +302,98 @@ public class Path {
         }
     }
 
-    /** Creates a concrete vector path transformed through the given matrix. */
-    public void addTo(Polygon ret, Affine a) {
-        float NUMSTEPS = 5;  // FIXME
-        ret.x[0] = a.multiply_px(x[0], y[0]);
-        ret.y[0] = a.multiply_py(x[0], y[0]);
-
-        for(int i=0; i<numvertices; i++) {
-            if (type[i] == TYPE_LINETO) {
-                float rx = x[i+1];
-                float ry = y[i+1];
-                ret.add(a.multiply_px(rx, ry), a.multiply_py(rx, ry));
-
-            } else if (type[i] == TYPE_MOVETO) {
-                float rx = x[i+1];
-                float ry = y[i+1];
-                ret.newcontour();
-                ret.add(a.multiply_px(rx, ry), a.multiply_py(rx, ry));
-
-            } else if (type[i] == TYPE_ARCTO) {
-                float rx = c1x[i];
-                float ry = c1y[i];
-                float phi = c2x[i];
-                float fa = ((int)c2y[i]) >> 1;
-                float fs = ((int)c2y[i]) & 1;
-                float x1 = x[i];
-                float y1 = y[i];
-                float x2 = x[i+1];
-                float y2 = y[i+1];
-
-                // F.6.5: given x1,y1,x2,y2,fa,fs, compute cx,cy,theta1,dtheta
-                float x1_ = (float)Math.cos(phi) * (x1 - x2) / 2 + (float)Math.sin(phi) * (y1 - y2) / 2;
-                float y1_ = -1 * (float)Math.sin(phi) * (x1 - x2) / 2 + (float)Math.cos(phi) * (y1 - y2) / 2;
-                float tmp = (float)Math.sqrt((rx * rx * ry * ry - rx * rx * y1_ * y1_ - ry * ry * x1_ * x1_) /
-                                             (rx * rx * y1_ * y1_ + ry * ry * x1_ * x1_));
-                float cx_ = (fa == fs ? -1 : 1) * tmp * (rx * y1_ / ry);
-                float cy_ = (fa == fs ? -1 : 1) * -1 * tmp * (ry * x1_ / rx);
-                float cx = (float)Math.cos(phi) * cx_ - (float)Math.sin(phi) * cy_ + (x1 + x2) / 2;
-                float cy = (float)Math.sin(phi) * cx_ + (float)Math.cos(phi) * cy_ + (y1 + y2) / 2;
-
-                // F.6.4 Conversion from center to endpoint parameterization
-                float ux = 1, uy = 0, vx = (x1_ - cx_) / rx, vy = (y1_ - cy_) / ry;
-                float det = ux * vy - uy * vx;
-                float theta1 = (det < 0 ? -1 : 1) *
-                    (float)Math.acos((ux * vx + uy * vy) / 
-                                     ((float)Math.sqrt(ux * ux + uy * uy) * (float)Math.sqrt(vx * vx + vy * vy)));
-                ux = (x1_ - cx_) / rx; uy = (y1_ - cy_) / ry;
-                vx = (-1 * x1_ - cx_) / rx; vy = (-1 * y1_ - cy_) / ry;
-                det = ux * vy - uy * vx;
-                float dtheta = (det < 0 ? -1 : 1) *
-                    (float)Math.acos((ux * vx + uy * vy) / 
-                                     ((float)Math.sqrt(ux * ux + uy * uy) * (float)Math.sqrt(vx * vx + vy * vy)));
-                dtheta = dtheta % (float)(2 * Math.PI);
-
-                if (fs == 0 && dtheta > 0) theta1 -= 2 * PI; 
-                if (fs == 1 && dtheta < 0) theta1 += 2 * PI;
-
-                if (fa == 1 && dtheta < 0) dtheta = 2 * PI + dtheta;
-                else if (fa == 1 && dtheta > 0) dtheta = -1 * (2 * PI - dtheta);
-
-                // FIXME: integrate F.6.6
-                // FIXME: isn't quite ending where it should...
-
-                // F.6.3: Parameterization alternatives
-                float theta = theta1;
-                for(int j=0; j<NUMSTEPS; j++) {
-                    float rasterx = rx * (float)Math.cos(theta) * (float)Math.cos(phi) -
-                        ry * (float)Math.sin(theta) * (float)Math.sin(phi) + cx;
-                    float rastery = rx * (float)Math.cos(theta) * (float)Math.sin(phi) +
-                        ry * (float)Math.cos(phi) * (float)Math.sin(theta) + cy;
-                    ret.add(a.multiply_px(rasterx, rastery), a.multiply_py(rasterx, rastery));
-                    theta += dtheta / NUMSTEPS;
-                }
-
-            } else if (type[i] == TYPE_CUBIC) {
-
-                float ax = x[i+1] - 3 * c2x[i] + 3 * c1x[i] - x[i];
-                float bx = 3 * c2x[i] - 6 * c1x[i] + 3 * x[i];
-                float cx = 3 * c1x[i] - 3 * x[i];
-                float dx = x[i];
-                float ay = y[i+1] - 3 * c2y[i] + 3 * c1y[i] - y[i];
-                float by = 3 * c2y[i] - 6 * c1y[i] + 3 * y[i];
-                float cy = 3 * c1y[i] - 3 * y[i];
-                float dy = y[i];
-                   
-                float x0 = a.multiply_px(x[i], y[i]);
-                float y0 = a.multiply_py(x[i], y[i]);
-                float x1 = a.multiply_px(x[i+1], y[i+1]);
-                float y1 = a.multiply_py(x[i+1], y[i+1]);
-                float steps = (float)Math.sqrt( (x1-x0) * (x1-x0) + (y1-y0) * (y1-y0) );
-
-                for(float t=0; t<1; t += 1 / (steps/20)) {
-                    float rx = ax * t * t * t + bx * t * t + cx * t + dx;
-                    float ry = ay * t * t * t + by * t * t + cy * t + dy;
-                    ret.add(a.multiply_px(rx, ry), a.multiply_py(rx, ry));
-                }
-
-
-            } else if (type[i] == TYPE_QUADRADIC) {
-
-                float bx = x[i+1] - 2 * c1x[i] + x[i];
-                float cx = 2 * c1x[i] - 2 * x[i];
-                float dx = x[i];
-                float by = y[i+1] - 2 * c1y[i] + y[i];
-                float cy = 2 * c1y[i] - 2 * y[i];
-                float dy = y[i];
-                       
-                float x0 = a.multiply_px(x[i], y[i]);
-                float y0 = a.multiply_py(x[i], y[i]);
-                float x1 = a.multiply_px(x[i+1], y[i+1]);
-                float y1 = a.multiply_py(x[i+1], y[i+1]);
-                float steps = (float)Math.sqrt( (x1-x0) * (x1-x0) + (y1-y0) * (y1-y0) );
-
-                for(float t=0; t<1; t += 1 / (steps/20)) {
-                    float rx = bx * t * t + cx * t + dx;
-                    float ry = by * t * t + cy * t + dy;
-                    ret.add(a.multiply_px(rx, ry), a.multiply_py(rx, ry));
-                }
-
-            }
-        }
-    }
-
     protected void parseSingleCommandAndArguments(Tokenizer t, char command, boolean relative) {
-        if (numvertices == 0 && command != 'm')
-            throw new RuntimeException("first command MUST be an 'm', not a " + command);
-        if (numvertices > x.length - 2) {
-            float[] new_x = new float[x.length * 2]; System.arraycopy(x, 0, new_x, 0, x.length); x = new_x;
-            float[] new_y = new float[y.length * 2]; System.arraycopy(y, 0, new_y, 0, y.length); y = new_y;
-        }
+        if (tail==null && command!='m') throw new RuntimeException("first command MUST be an 'm', not a " + command);
         switch(command) {
             case 'z': {
-               int where;
-                type[numvertices-1] = TYPE_LINETO;
-               for(where = numvertices-2; where >= 0 && type[where] != TYPE_MOVETO; where--);
-                x[numvertices] = x[where+1];
-                y[numvertices] = y[where+1];
-                numvertices++;
+                Curve c;
+                for(c = tail.prev; c != null && !(c instanceof Move); c = c.prev);
+                Line ret = new Line();
+                ret.x = c.x;
+                ret.y = c.y;
+                add(ret);
+                Move mov = new Move();
+                mov.x = ret.x;
+                mov.y = ret.y;
+                add(mov);
                 closed = true;
                 // FIXME: actually, we should search back to the last 'z' or 'm', not just 'm'
                 break;
             }
 
             case 'm': {
-                if (numvertices > 0) type[numvertices-1] = TYPE_MOVETO;
-               x[numvertices] = t.parseFloat() + (relative ? x[numvertices - 1] : 0);
-               y[numvertices] = t.parseFloat() + (relative ? y[numvertices - 1] : 0);
-                if (numvertices > 2 && type[numvertices-2] == TYPE_MOVETO) {
-                    x[numvertices-1] = x[numvertices];
-                    y[numvertices-1] = y[numvertices];
-                } else {
-                    numvertices++;
-                }
+                // feature: collapse consecutive movetos
+                Move ret = new Move();
+                ret.x = t.parseFloat() + (relative ? tail.y : 0);
+                ret.y = t.parseFloat() + (relative ? tail.y : 0);
+                add(ret);
                 break;
             }
 
             case 'l': case 'h': case 'v': {
-                type[numvertices-1] = TYPE_LINETO;
                 float first = t.parseFloat(), second;
-                if (command == 'h') {
-                    second = relative ? 0 : y[numvertices - 1];
-                } else if (command == 'v') {
-                    second = first; first = relative ? 0 : x[numvertices - 1];
-                } else {
-                    second = t.parseFloat();
-                }
-                x[numvertices] = first + (relative ? x[numvertices - 1] : 0);
-                y[numvertices] = second + (relative ? y[numvertices - 1] : 0);
-                numvertices++;
+                if (command == 'h')      second = relative ? 0 : tail.y;
+                else if (command == 'v') { second = first; first = relative ? 0 : tail.x; }
+                else                     second = t.parseFloat();
+                Line ret = new Line();
+                ret.x = first + (relative ? tail.x : 0);
+                ret.y = second + (relative ? tail.y : 0);
+                add(ret);
                 break;
             }
             
             case 'a': {
-               type[numvertices-1] = TYPE_ARCTO;
-               c1x[numvertices-1] = t.parseFloat() + (relative ? x[numvertices - 1] : 0);
-               c1y[numvertices-1] = t.parseFloat() + (relative ? y[numvertices - 1] : 0);
-               c2x[numvertices-1] = (t.parseFloat() / 360) * 2 * PI;
-               c2y[numvertices-1] = (((int)t.parseFloat()) << 1) | (int)t.parseFloat();
-               x[numvertices] = t.parseFloat() + (relative ? x[numvertices - 1] : 0);
-               y[numvertices] = t.parseFloat() + (relative ? y[numvertices - 1] : 0);
-                numvertices++;
+                Arc ret = new Arc();
+                ret.c1x = t.parseFloat() + (relative ? tail.x : 0);
+                ret.c1y = t.parseFloat() + (relative ? tail.y : 0);
+                ret.c2x = (t.parseFloat() / 360) * 2 * PI;
+                ret.c2y = t.parseFloat();
+                ret.x = t.parseFloat() + (relative ? tail.x : 0);
+                ret.y = t.parseFloat() + (relative ? tail.y : 0);
+                add(ret);
                 break;
             }
 
             case 's': case 'c': {
-                type[numvertices-1] = TYPE_CUBIC;
+                Bezier ret = new Bezier();
                 if (command == 'c') {
-                    c1x[numvertices-1] = t.parseFloat() + (relative ? x[numvertices - 1] : 0);
-                    c1y[numvertices-1] = t.parseFloat() + (relative ? y[numvertices - 1] : 0);
-                } else if (numvertices > 1 && type[numvertices-2] == TYPE_CUBIC) {
-                    c1x[numvertices-1] = 2 * x[numvertices - 1] - c2x[numvertices-2];
-                    c1y[numvertices-1] = 2 * y[numvertices - 1] - c2y[numvertices-2];
+                    tail.c1x = t.parseFloat() + (relative ? tail.x : 0);
+                    tail.c1y = t.parseFloat() + (relative ? tail.y : 0);
+                } else if (head != null && tail instanceof Bezier) {
+                    tail.c1x = 2 * tail.x-((Bezier)tail).c2x;
+                    tail.c1y = 2 * tail.y-((Bezier)tail).c2x;
                 } else {
-                    c1x[numvertices-1] = x[numvertices-1];
-                    c1y[numvertices-1] = y[numvertices-1];
+                    tail.c1x = tail.x;
+                    tail.c1y = tail.y;
                 }
-                c2x[numvertices-1] = t.parseFloat() + (relative ? x[numvertices - 1] : 0);
-                c2y[numvertices-1] = t.parseFloat() + (relative ? y[numvertices - 1] : 0);
-               x[numvertices] = t.parseFloat() + (relative ? x[numvertices - 1] : 0);
-               y[numvertices] = t.parseFloat() + (relative ? y[numvertices - 1] : 0);
-                numvertices++;
+                tail.c2x = t.parseFloat() + (relative ? tail.x : 0);
+                tail.c2y = t.parseFloat() + (relative ? tail.y : 0);
+               ret.x = t.parseFloat() + (relative ? tail.x : 0);
+               ret.y = t.parseFloat() + (relative ? tail.y : 0);
+                add(ret);
                 break;
             }
 
             case 't': case 'q': {
-                type[numvertices-1] = TYPE_QUADRADIC;
+                QuadBezier ret = new QuadBezier();
                 if (command == 'q') {
-                    c1x[numvertices-1] = t.parseFloat() + (relative ? x[numvertices - 1] : 0);
-                    c1y[numvertices-1] = t.parseFloat() + (relative ? y[numvertices - 1] : 0);
-                } else if (numvertices > 1 && type[numvertices-2] == TYPE_QUADRADIC) {
-                    c1x[numvertices-1] = 2 * x[numvertices - 1] - c1x[numvertices-2];
-                    c1y[numvertices-1] = 2 * y[numvertices - 1] - c1y[numvertices-2];
+                    tail.c1x = t.parseFloat() + (relative ? tail.x : 0);
+                    tail.c1y = t.parseFloat() + (relative ? tail.y : 0);
+                } else if (head != null && tail instanceof QuadBezier) {
+                    tail.c1x = 2 * tail.x-((QuadBezier)tail).c1x;
+                    tail.c1y = 2 * tail.y-((QuadBezier)tail).c1y;
                 } else {
-                    c1x[numvertices-1] = x[numvertices-1];
-                    c1y[numvertices-1] = y[numvertices-1];
+                    tail.c1x = tail.x;
+                    tail.c1y = tail.y;
                 }
-                x[numvertices] = t.parseFloat() + (relative ? x[numvertices - 1] : 0);
-                y[numvertices] = t.parseFloat() + (relative ? y[numvertices - 1] : 0);
-                numvertices++;
+                ret.x = t.parseFloat() + (relative ? tail.x : 0);
+                ret.y = t.parseFloat() + (relative ? tail.y : 0);
+                add(ret);
                 break;
             }
 
             default:
-                // FIXME
-        }
-
-        /*
-        // invariant: after this loop, no two lines intersect other than at a vertex
-        // FIXME: cleanup
-        int index = numvertices - 2;
-        for(int i=0; i<Math.min(numvertices - 3, index); i++) {
-        for(int j = index; j < numvertices - 1; j++) {
-
-        // I'm not sure how to deal with vertical lines...
-        if (x[i+1] == x[i] || x[j+1] == x[j]) continue;
-                       
-        float islope = (y[i+1] - y[i]) / (x[i+1] - x[i]);
-        float jslope = (y[j+1] - y[j]) / (x[j+1] - x[j]);
-        if (islope == jslope) continue;   // parallel lines can't intersect
-                       
-        float _x = (islope * x[i] - jslope * x[j] + y[j] - y[i]) / (islope - jslope);
-        float _y = islope * (_x - x[i]) + y[i];
-                       
-        if (_x > Math.min(x[i+1], x[i]) && _x < Math.max(x[i+1], x[i]) &&
-        _x > Math.min(x[j+1], x[j]) && _x < Math.max(x[j+1], x[j])) {
-        // FIXME: something's not right in here.  See if we can do without fracturing line 'i'.
-        for(int k = ++numvertices; k>i; k--) { x[k] = x[k - 1]; y[k] = y[k - 1]; }
-        x[i+1] = _x;
-        y[i+1] = _y;
-        x[numvertices] = x[numvertices - 1];  x[numvertices - 1] = _x;
-        y[numvertices] = y[numvertices - 1];  y[numvertices - 1] = _y;
-        edges[numedges++] = numvertices - 1; numvertices++;
-        index++;
-        break;  // actually 'continue' the outermost loop
-        }
-        }
         }
-        */
 
     }