mass rename and rebranding from xwt to ibex - fixed to use ixt files
[org.ibex.core.git] / src / org / xwt / translators / SVG.java
index 9abab1d..3e2e780 100644 (file)
@@ -1,9 +1,278 @@
-// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
+// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
 package org.xwt.translators;
 import java.util.*;
 
+// FIXME: need to support style sheets and the 'style=' attribute
+// FIXME: need to convert markers into subboxes
 public class SVG {
 
+    /*
+    public static void parseNode(String name, String[] keys, Object[] vals, Template t) {
+        Hash h = new Hash();
+        for(int i=0; i<keys.length; i++) if (vals[i] != null) h.put(keys[i], vals[i]);
+
+        Hash props = new Hash();
+        props.put("transform", h.get("transform"));
+        props.put("fill", h.get("fill"));
+        props.put("stroke", h.get("stroke"));
+        if ("visible".equals(h.get("overflow")) || "auto".equals(h.get("overflow")))
+            Log.info(VectorGraphics.class, "warning: overflow={auto|visible} not supported; ignoring");
+        if (h.get("display") != null) props.put("invisible", new Boolean("none".equals(h.get("display"))));
+
+
+        // FIXME: "the automatic transformation that is created due to
+        // a viewBox does not affect the x, y, width and height
+        // attributes".  Also, transform+viewbox together?
+
+        if (h.get("preserveAspectRatio") != null) {
+            StringTokenizer st = new StringTokenizer((String)h.get("preserveAspectRatio"), " ");
+            String align = st.nextToken();
+            if ("defer".equals(align)) align = st.nextToken();
+            if (!align.equals("none")) {
+                // FIXME, need to beef up Ibex's align property
+                align = "";
+                if (align.startsWith("yMin")) align = "top";
+                else if (align.startsWith("yMax")) align = "bottom";
+                if (align.startsWith("xMin")) align += "left";
+                else if (align.startsWith("xMax")) align += "right";
+                props.put("align", align);
+            }
+            // FIXME: need to implement scaling property on boxes, also size-to-viewbox
+            props.put("scaling", "uniform");
+            if (st.hasMoreTokens()) {
+                String meetOrSlice = st.nextToken();
+                if (meetOrSlice.equals("meet")) props.put("scaling", "meet");          // keep within viewport
+                else if (meetOrSlice.equals("slice")) props.put("scaling", "slice");   // expand beyond viewport
+            }
+        }
+
+        // FIXME: insert an extra layer of boxen and put this transform on the inner layer
+        if (h.get("viewBox") != null) {
+            PathTokenizer pt = new PathTokenizer(h.get("viewBox").toString());
+            String transform = (String)props.get("transform");
+            if (transform == null) transform = "";
+            transform = "translate(" + (-1 * pt.parseFloat()) + ", " + (-1 * pt.parseFloat()) + ") " + 
+                "scale(" + pt.parseFloat() + "%, " + pt.parseFloat() + "%) ";
+        }
+        
+        String path = (String)h.get("d");
+        if (name.equals("g")) {
+            path = null;
+
+        } else if (name.equals("font")) {
+            VectorGraphics.Font f = currentFont = new VectorGraphics.Font();
+            if (h.get("horiz-origin-x") != null) f.horiz_origin_x = Float.parseFloat(h.get("horiz-origin-x").toString());
+            if (h.get("horiz-origin-y") != null) f.horiz_origin_y = Float.parseFloat(h.get("horiz-origin-y").toString());
+            if (h.get("horiz-adv-x") != null) f.horiz_adv_x = Float.parseFloat(h.get("horiz-adv-x").toString());
+            if (h.get("vert-origin-x") != null) f.vert_origin_x = Float.parseFloat(h.get("vert-origin-x").toString());
+            if (h.get("vert-origin-y") != null) f.vert_origin_y = Float.parseFloat(h.get("vert-origin_y").toString());
+            if (h.get("vert-adv-y") != null) f.vert_adv_y = Float.parseFloat(h.get("vert-adv-y").toString());
+
+        } else if (name.equals("hkern")) {
+        //FIXME
+
+        } else if (name.equals("vkern")) {
+        //FIXME
+
+        } else if (name.equals("font-face")) {
+        //FIXME
+
+        } else if (name.equals("glyph") || name.equals("missing-glyph")) {
+            String glyphName = name.equals("missing-glyph") ? "missing-glyph" : (String)h.get("glyph-name");
+            VectorGraphics.Font.Glyph g = new VectorGraphics.Font.Glyph(glyphName, (String)h.get("unicode"), t, currentFont);
+            if (h.get("horiz-adv-x") != null) g.horiz_adv_x = Float.parseFloat(h.get("horiz-adv-x").toString());
+            if (h.get("vert-origin-x") != null) g.vert_origin_x = Float.parseFloat(h.get("vert-origin-x").toString());
+            if (h.get("vert-origin-y") != null) g.vert_origin_y = Float.parseFloat(h.get("vert-origin-y").toString());
+            if (h.get("vert-adv-y") != null) g.vert_adv_y = Float.parseFloat(h.get("vert-adv-y").toString());
+            if ("v".equals(h.get("orientation"))) g.isVerticallyOriented = true;
+
+        } else if (name.equals("svg")) {
+            // FIXME: handle percentages
+            // FIXME: what if these aren't provided?
+            // FIXME (in general)
+            float x = Float.parseFloat(h.get("x").toString());
+            float y = Float.parseFloat(h.get("y").toString());
+            float width = Float.parseFloat(h.get("width").toString());
+            float height = Float.parseFloat(h.get("height").toString());
+            h.put("viewBox", x + ", " + y + ", " + (x + width) + ", " + (y + height));
+            path = "";
+            
+        } else if (name.equals("path")) {
+            path = h.get("d").toString();
+           
+        } else if (name.equals("rect")) {
+            float x = Float.parseFloat(h.get("x").toString());
+            float y = Float.parseFloat(h.get("y").toString());
+            float width = Float.parseFloat(h.get("width").toString());
+            float height = Float.parseFloat(h.get("height").toString());
+            float rx = Float.parseFloat(h.get("rx").toString());
+            float ry = Float.parseFloat(h.get("ry").toString());
+            path =
+                "M" + (x + rx) + "," + y +
+                "H" + (x + width - rx) +
+                "A" + rx + "," + rx + ",0,0,1," + (x + width) + "," + (y + ry) +
+                "V" + (y + width - ry) +
+                "A" + rx + "," + rx + ",0,0,1," + (x + width - rx) + "," +
+                (y + height) +
+                "H" + (x + rx) +
+                "A" + rx + "," + rx + ",0,0,1," + x + "," + (y + height - ry) +
+                "V" + (y + ry) +
+                "A" + rx + "," + rx + ",0,0,1," + (x + rx) + "," + (y + ry) +
+                "Z";
+           
+        } else if (name.equals("circle")) {
+            float r = Float.parseFloat(h.get("r").toString());
+            float cx = Float.parseFloat(h.get("cx").toString());
+            float cy = Float.parseFloat(h.get("cy").toString());
+            path = "A " + r + " " + r + " 1 1 " + cx + " " + cy;
+           
+        } else if (name.equals("ellipse")) {
+            float rx = Float.parseFloat(h.get("rx").toString());
+            float ry = Float.parseFloat(h.get("ry").toString());
+            float cx = Float.parseFloat(h.get("cx").toString());
+            float cy = Float.parseFloat(h.get("cy").toString());
+            path = "A " + rx + " " + ry + " 1 1 " + cx + " " + cy;
+           
+        } else if (name.equals("line")) {
+            float x1 = Float.parseFloat(h.get("x1").toString());
+            float y1 = Float.parseFloat(h.get("y1").toString());
+            float x2 = Float.parseFloat(h.get("x2").toString());
+            float y2 = Float.parseFloat(h.get("y2").toString());
+            path = "M " + x1 + " " + y1 + " L " + x2 + " " + y2;
+                
+        } else if (name.equals("polyline") || name.equals("polygon")) {
+            StringTokenizer st = new StringTokenizer(h.get("points").toString(), ", ", false);
+            String s = "M ";
+            while(st.hasMoreTokens()) s += st.nextToken() + " " + st.nextToken() + " ";
+            path = s + (name.equals("polygon") ? "z" : "");
+
+        } else {
+            Log.info(VectorGraphics.class, "unknown element in VectorGraphics namespace: " + name);
+        }
+        props.put("path", path);
+        t.keys = new String[props.size()];
+        System.arraycopy(props.keys(), 0, t.keys, 0, t.keys.length);
+        t.vals = new String[props.size()];
+        for(int i=0; i<t.keys.length; i++) t.vals[i] = props.get(t.keys[i]);
+        
+
+        // FIXME!!!!
+        if (h.get("viewBox") != null) {
+        StringTokenizer st = new StringTokenizer(h.get("viewBox").toString(), ", ", false);
+        if (t.transform == null) t.transform = "";
+        Point p1, p2;
+        VectorGraphics.RasterPath.fromString(path).getBoundingBox(p1, p2);
+        
+        float minx = st.parseFloat();
+        float miny = st.parseFloat();
+        float width = st.parseFloat();
+        float height = st.parseFloat();
+        t.transform += "translate(" + (-1 * p1.x) + ", " + (-1 * p1.y) + ") " +
+        "scale(" + ((p2.x - p1.x) / width) + ", " + ((p2.y - p1.y) / height) + ") " + 
+        "translate(" + minx + ", " + miny + ") ";
+        
+        // FIXME: preserveAspectRatio
+        }
+
+    }
+    */
+
+    /*
+    public static class Font {
+        Font() { }
+        float horiz_origin_x = 0,  horiz_origin_y = 0, horiz_adv_x = 0;
+        float vert_origin_x = 0, vert_origin_y = 0, vert_adv_y = 0;
+
+        // FIXME: avoid using substring() in here ore creating any objects
+        public void render(String text, DoubleBuffer buf, int x, int y, int fillcolor, int strokecolor, int size) {
+            // FIXME: points, not pixels
+            Affine a = buf.a;
+            float scaleFactor = (float)(1.0/1000.0) * (float)size;
+            for(int pos=0; pos<text.length(); pos++) { 
+                Glyph g;
+                for(g = (Glyph)glyphByUnicode.get(text.substring(pos, pos+1));
+                    g != null && !g.unicode.equals(text.substring(pos, pos + g.unicode.length()));
+                    g = g.next);
+                if (g == null) {
+                    g = (Glyph)glyphByName.get("missing-glyph");
+                } else {
+                    pos += g.unicode.length() - 1;
+                }
+                if (g != null) {
+                    System.out.println("  " + g.unicode);
+                    g.render(buf, x, y, fillcolor, strokecolor, scaleFactor);
+                    x += (int)(g.horiz_adv_x * size / 1000.0);
+                } else {
+                    x += (int)(horiz_adv_x * size / 1000.0);
+                }
+            }
+            buf.setTransform(a);
+        }
+
+        / ** all glyphs, keyed by their <tt>name</tt> property * /
+        Hashtable glyphByName = new Hashtable();
+
+        / ** linked list of glyphs, stored by the first character of their <tt>unicode</tt> property * /
+        Hashtable glyphByUnicode = new Hashtable();
+
+        // a Glyph in an VectorGraphics font
+        public static class Glyph {
+
+            // FIXME: lang attribute
+            boolean isVerticallyOriented = false;
+            Template t = null;
+            Box b = null;
+
+            float horiz_adv_x = 0;
+            float vert_origin_x = 0;
+            float vert_origin_y = 0;
+            float vert_adv_y = 0;
+
+            String unicode = null;
+
+            // forms the linked list in glyphByUnicode; glyphs appear in the order specified in the font
+            public Glyph next = null;
+
+            Glyph(String name, String unicode, Template t, VectorGraphics.Font f) {
+                if (unicode != null)
+                    if (f.glyphByUnicode.get(unicode.substring(0, 1)) == null) {
+                        f.glyphByUnicode.put(unicode.substring(0, 1), this);
+                    } else {
+                        Glyph g;
+                        for(g = (Glyph)f.glyphByUnicode.get(unicode.substring(0, 1)); g.next != null; g = g.next);
+                        g.next = this;
+                    }
+                if (name != null) f.glyphByUnicode.put(name, this);
+                this.unicode = unicode;
+                this.t = t;
+                horiz_adv_x = f.horiz_adv_x;
+                vert_origin_x = f.vert_origin_x;
+                vert_origin_y = f.vert_origin_y;
+                vert_adv_y = f.vert_adv_y;
+            }
+            public void render(DoubleBuffer buf, int x, int y, int fillcolor, int strokecolor, float scaleFactor) {
+                // FEATURE: make b double-buffered for increased performance
+                if (b == null) {
+                    b = new Box(t, new org.ibex.util.Vec(), new org.ibex.util.Vec(), null, 0, 0);
+                    b.put("absolute", Boolean.TRUE);
+                    b.prerender();
+                    t = null;
+                }
+                // FIXME
+                b.put("width", new Integer(1000));
+                b.put("height", new Integer(1000));
+                b.fillcolor = fillcolor;
+                b.strokecolor = strokecolor;
+
+                // we toss an extra flip on the ctm so that fonts stick "up" instead of down
+                b.render(0, 0, buf.getWidth(), buf.getHeight(), buf,
+                         Affine.flip(false, true).multiply(Affine.scale(scaleFactor, scaleFactor).multiply(Affine.translate(x, y))).multiply(buf.a));
+            }
+        }
+    }
+    */
+
+
     /** Copied verbatim from the SVG specification */
     public static Hashtable colors = new Hashtable(400);
     static {