2003/11/13 05:04:22
[org.ibex.core.git] / src / org / xwt / VectorGraphics.java
index 55c0930..212052e 100644 (file)
@@ -40,6 +40,7 @@ public final class VectorGraphics {
     // Public entry points /////////////////////////////////////////////////////////////////
 
     public static VectorPath parseVectorPath(String s) {
+        if (s == null) return null;
         PathTokenizer t = new PathTokenizer(s);
         VectorPath ret = new VectorPath();
         char last_command = 'M';
@@ -119,6 +120,72 @@ public final class VectorGraphics {
 
     // PathTokenizer //////////////////////////////////////////////////////////////////////////////
 
+    public static Affine parseTransform(String t) {
+        if (t == null) return null;
+        t = t.trim();
+        Affine ret = VectorGraphics.Affine.identity();
+        while (t.length() > 0) {
+            if (t.startsWith("skewX(")) {
+                // FIXME
+                
+            } else if (t.startsWith("shear(")) {
+                // FIXME: nonstandard; remove this
+                ret.multiply(VectorGraphics.Affine.shear(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
+                
+            } else if (t.startsWith("skewY(")) {
+                // FIXME
+                
+            } else if (t.startsWith("rotate(")) {
+                String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
+                if (sub.indexOf(',') != -1) {
+                    float angle = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
+                    sub = sub.substring(sub.indexOf(',') + 1);
+                    float cx = Float.parseFloat(sub.substring(0, sub.indexOf(',')));
+                    sub = sub.substring(sub.indexOf(',') + 1);
+                    float cy = Float.parseFloat(sub);
+                    ret.multiply(VectorGraphics.Affine.translate(cx, cy));
+                    ret.multiply(VectorGraphics.Affine.rotate(angle));
+                    ret.multiply(VectorGraphics.Affine.translate(-1 * cx, -1 * cy));
+                } else {
+                    ret.multiply(VectorGraphics.Affine.rotate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(')')))));
+                }
+                
+            } else if (t.startsWith("translate(")) {
+                String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
+                if (sub.indexOf(',') > -1) {
+                    ret.multiply(VectorGraphics.Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
+                                                                 Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
+                } else {
+                    ret.multiply(VectorGraphics.Affine.translate(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))), 0));
+                }
+                
+            } else if (t.startsWith("flip(")) {
+                String which = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
+                ret.multiply(VectorGraphics.Affine.flip(which.equals("horizontal"), which.equals("vertical")));
+                
+            } else if (t.startsWith("scale(")) {
+                String sub = t.substring(t.indexOf('(') + 1, t.indexOf(')'));
+                if (sub.indexOf(',') > -1) {
+                    ret.multiply(VectorGraphics.Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
+                                                             Float.parseFloat(t.substring(t.indexOf(',') + 1, t.indexOf(')')))));
+                } else {
+                    ret.multiply(VectorGraphics.Affine.scale(Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(','))),
+                                                             Float.parseFloat(t.substring(t.indexOf('(') + 1, t.indexOf(',')))));
+                }
+                
+            } else if (t.startsWith("matrix(")) {
+                // FIXME: is this mapped right?
+                float d[] = new float[6];
+                StringTokenizer st = new StringTokenizer(t, ",", false);
+                for(int i=0; i<6; i++)
+                    d[i] = Float.parseFloat(st.nextToken());
+                ret.multiply(new VectorGraphics.Affine(d[0], d[1], d[2], d[3], d[4], d[5]));
+            }
+            t = t.substring(t.indexOf(')') + 1).trim();
+        }
+        return ret;
+    }
+    
     public static final float PX_PER_INCH = 72;
     public static final float INCHES_PER_CM = (float)0.3937;
     public static final float INCHES_PER_MM = INCHES_PER_CM / 10;
@@ -553,7 +620,7 @@ public final class VectorGraphics {
                     if (leftSegment == rightSegment || rightSegment == Integer.MAX_VALUE) break;
                     if (leftSegment != -1)
                         if ((useEvenOdd && count % 2 != 0) || (!useEvenOdd && count != 0))
-                            paint.fillTrapezoid(intercept(edges[leftSegment], y0, true, true),
+                            paint.fillJSTrapezoid(intercept(edges[leftSegment], y0, true, true),
                                                 intercept(edges[rightSegment], y0, true, true), y0,
                                                 intercept(edges[leftSegment], y1, true, true),
                                                 intercept(edges[rightSegment], y1, true, true), y1,
@@ -642,14 +709,14 @@ public final class VectorGraphics {
 
     public static interface Paint {
        public abstract void
-            fillTrapezoid(int tx1, int tx2, int ty1, int tx3, int tx4, int ty2, PixelBuffer buf);
+            fillJSTrapezoid(int tx1, int tx2, int ty1, int tx3, int tx4, int ty2, PixelBuffer buf);
     }
 
     public static class SingleColorPaint implements Paint {
         int color;
         public SingleColorPaint(int color) { this.color = color; }
-        public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, PixelBuffer buf) {
-            buf.fillTrapezoid(x1, x2, y1, x3, x4, y2, color);
+        public void fillJSTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, PixelBuffer buf) {
+            buf.fillJSTrapezoid(x1, x2, y1, x3, x4, y2, color);
         }
     }
 
@@ -682,7 +749,7 @@ public final class VectorGraphics {
        int[] stop_colors;
        float[] stop_offsets;
 
-       public void fillTrapezoid(float tx1, float tx2, float ty1, float tx3, float tx4, float ty2, PixelBuffer buf) {
+       public void fillJSTrapezoid(float tx1, float tx2, float ty1, float tx3, float tx4, float ty2, PixelBuffer buf) {
             Affine a = buf.a;
            Affine inverse = a.copy().invert();
            float slope1 = (tx3 - tx1) / (ty2 - ty1);