2003/09/27 06:42:27
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:38:20 +0000 (07:38 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:38:20 +0000 (07:38 +0000)
darcs-hash:20040130073820-2ba56-ff97c93cd0bcb94e7d077c9c7116cd70f68735a0.gz

src/org/xwt/plat/OpenGL.cc
src/org/xwt/plat/OpenGL.java
src/org/xwt/plat/Win32.java
src/org/xwt/plat/X11.java
src/org/xwt/translators/SVG.java

index c7a1002..8200b16 100644 (file)
@@ -96,17 +96,6 @@ void OpenGL$GLPixelBuffer::fillTrapezoid(jint x1, jint x2, jint y1, jint x3, jin
     glEnd();
 }
 
-void OpenGL$GLPixelBuffer::fillRect(jint x1, jint y1, jint x2, jint y2,jint color) {
-    activateContext();
-    setColor(color);
-    glBegin(GL_QUADS); 
-        glVertex3f(x1,y2,0.0f );
-        glVertex3f(x2,y2,0.0f );
-        glVertex3f(x2,y1,0.0f );
-        glVertex3f(x1,y1,0.0f );
-    glEnd();
-}
-
 void OpenGL$GLPixelBuffer::setClip(jint x1, jint y1, jint x2, jint y2) {
     //fprintf(stderr,"setClip: %d %d %d %d\n",x1,y1,x2,y2);
     if(x1==0 && y1==0 && x2==width && y2==height) {
index 9f6d669..510693a 100644 (file)
@@ -74,7 +74,6 @@ abstract class OpenGL {
         
         public native void setClip(int x, int y, int x2, int y2);
         public native void resetClip();
-        public native void fillRect(int x, int y, int x2, int y2, int color);
         public native void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color);
             
         public void drawString(String font, String text, int x, int y, int color) {
index b57109c..f006190 100644 (file)
@@ -269,6 +269,19 @@ public class Win32 extends GCJ {
             drawPicture(source, x, y, x + source.getWidth(), y + source.getHeight(), 0, 0, source.getWidth(), source.getHeight());
         }
 
+        // FIXME: try to use os acceleration
+        public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) {
+            if (x1 == x3 && x2 == x4) {
+                fillRect(x1, y1, x4, y2, argb);
+            } else for(int y=y1; y<y2; y++) {
+                int _x1 = (int)Math.floor((y - y1) * (x3 - x1) / (y2 - y1) + x1);
+                int _y1 = (int)Math.floor(y);
+                int _x2 = (int)Math.ceil((y - y1) * (x4 - x2) / (y2 - y1) + x2);
+                int _y2 = (int)Math.floor(y) + 1;
+                if (_x1 > _x2) { int _x0 = _x1; _x1 = _x2; _x2 = _x0; }
+                fillRect(_x1, _y1, _x2, _y2, argb);
+            }
+        }
     }
 
 }
index 5f579e4..a8d948c 100644 (file)
@@ -125,11 +125,10 @@ public class X11 extends POSIX {
             if (doublebuf != null) return;
             // no point in using a shared pixmap since we'll only write to this image once
             X11PixelBuffer b = new X11PixelBuffer(width, height, false);
-            b.drawPicture(this, 0, 0);
+            b.drawPicture(this, 0, 0, width, height, 0, 0, width, height);
             if (needsStipple) b.createStipple(this);
             doublebuf = b;
         }
-
     }
 
     /**
@@ -176,10 +175,6 @@ public class X11 extends POSIX {
             cliph = y2 - y; if (cliph < 0) cliph = 0;
         }
         
-        public void drawPicture(Picture source, int x, int y) {
-            drawPicture(source, x, y, x + source.getWidth(), y + source.getHeight(), 0, 0, source.getWidth(), source.getHeight());
-        }
-
         public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2) {
             if (!(dx2 - dx1 != sx2 - sx1 || dy2 - dy1 != sy2 - sy1) && ((X11Picture)source).doublebuf != null)
                 fastDrawPicture(source, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2);
@@ -199,6 +194,19 @@ public class X11 extends POSIX {
         public native void fillRect(int x, int y, int x2, int y2, int color);
         public native void finalize();
 
+        // FIXME: try to use os acceleration
+        public void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int argb) {
+            if (x1 == x3 && x2 == x4) {
+                fillRect(x1, y1, x4, y2, argb);
+            } else for(int y=y1; y<y2; y++) {
+                int _x1 = (int)Math.floor((y - y1) * (x3 - x1) / (y2 - y1) + x1);
+                int _y1 = (int)Math.floor(y);
+                int _x2 = (int)Math.ceil((y - y1) * (x4 - x2) / (y2 - y1) + x2);
+                int _y2 = (int)Math.floor(y) + 1;
+                if (_x1 > _x2) { int _x0 = _x1; _x1 = _x2; _x2 = _x0; }
+                fillRect(_x1, _y1, _x2, _y2, argb);
+            }
+        }
     }
 
 }
index 9abab1d..8c22dd0 100644 (file)
@@ -2,8 +2,277 @@
 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.log(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 XWT'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.log(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.xwt.util.Vec(), new org.xwt.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 {