2003/11/03 05:28:31
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:40:50 +0000 (07:40 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:40:50 +0000 (07:40 +0000)
darcs-hash:20040130074050-2ba56-4ab46bf938c5ff2eecda5c9d55d79c71ffefe573.gz

src/org/xwt/Glyph.java [deleted file]
src/org/xwt/Res.java
src/org/xwt/Template.java
src/org/xwt/Trap.java
src/org/xwt/XWT.java

diff --git a/src/org/xwt/Glyph.java b/src/org/xwt/Glyph.java
deleted file mode 100644 (file)
index 71cf19e..0000000
+++ /dev/null
@@ -1,103 +0,0 @@
-// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
-package org.xwt;
-import org.xwt.translators.*;
-import org.xwt.util.*;
-import org.xwt.js.*;
-import java.util.*;
-
-public class Glyph {
-    public char c;
-    public int max_ascent;     // same value for every glyph in font; the max ascent of all chars
-    public int max_descent;    // same value for every glyph in font; the max descent of all chars
-    public int baseline;       // within the picture, this is the y-coordinate of the baseline
-    public int advance;        // amount to increment the x-coordinate
-    public int pointsize;
-    public Picture p;
-    public Res res;
-    public boolean rendered = false;
-
-    // k1=font.res k2=(c << 16 | pointsize)
-    private static Cache glyphCache = new Cache();
-
-    private static Queue glyphsToBeRendered = new Queue(255);
-    private static Freetype freetype = new Freetype();
-
-    private static Scheduler.Task glyphRenderingTask = new Scheduler.Task() {
-            public Object call(Object arg) {
-                Glyph g = (Glyph)glyphsToBeRendered.remove(false);
-                if (g == null) return null;
-                if (g.p != null) return null;
-                Log.log(Glyph.class, "rendering glyph " + g.c);
-                freetype.renderGlyph(g.res, g);
-                g.rendered = true;
-                Scheduler.add(this);
-                return null;
-            }
-        };
-
-    /**
-
-     *  If the glyphs of <code>text</code> are not yet loaded, spawn a
-     *  Task to load them and invoke callback.
-     *
-
-     *  returns the width (in the high-order int) and height (in the
-     *  low-order int) of the string's rasterization, or -1 if some
-     *  glyphs are not loaded.
-
-     */
-    public static long rasterizeGlyphs(final Res res, final int pointsize, final String text, PixelBuffer pb, int textcolor,
-                                  int x, int y, int cx1, int cy1, int cx2, int cy2, final Callback callback) {
-        boolean ret = true;
-        int width = 0;
-        int height = 0;
-        for(int i=0; i<text.length(); i++) {
-            final char c = text.charAt(i);
-            Glyph g = (Glyph)glyphCache.get(res, new Integer((((int)c) << 16) | pointsize));
-            if (g == null) {
-                g = new Glyph();
-                g.c = c;
-                g.pointsize = pointsize;
-                g.res = res; 
-                glyphCache.put(res, new Integer((((int)c) << 16) | pointsize), g);
-                glyphsToBeRendered.prepend(g);
-            }
-            if (g.rendered && ret) {
-                if (pb != null && g.p != null)
-                    pb.drawPictureAlphaOnly(g.p, x, y + g.max_ascent - g.baseline, cx1, cy1, cx2, cy2, textcolor);
-                x += g.advance;
-                width += g.advance;
-                height = java.lang.Math.max(height, g.max_ascent + g.max_descent);
-            } else {
-                glyphsToBeRendered.prepend(g);
-                Scheduler.add(glyphRenderingTask);
-                ret = false;
-            }
-        }
-        if (!ret) Scheduler.add(new Scheduler.Task() { public Object call(Object arg) {
-            for(int i=0; i<text.length(); i++) {
-                Glyph g = (Glyph)glyphCache.get(res, new Integer((((int)text.charAt(i)) << 16) | pointsize));
-                if (g == null || !g.rendered) {
-                    Scheduler.add(this);
-                    return null;
-                }
-            }
-            callback.call(null);
-            return null;
-        }});
-
-        if (ret) return ((long)width << 16) | (long)height;
-        for(int i=32; i<128; i++) {
-            Glyph g = (Glyph)glyphCache.get(res, new Integer((i << 16) | pointsize));
-            if (g == null) {
-                g = new Glyph();
-                g.c = (char)i;
-                g.pointsize = pointsize;
-                g.res = res;
-                glyphCache.put(res, new Integer((i << 16) | pointsize), g);
-                glyphsToBeRendered.append(g);
-            }
-        }
-        return -1;
-    }
-}
index ea503b5..399476b 100644 (file)
@@ -57,7 +57,7 @@ public abstract class Res extends JS {
     public Res addExtension(String extension) { return new Ref(this, extension); }
 
     public Object[] keys() { throw new JS.Exn("cannot enumerate a resource"); } 
-    public void put(Object key, Object val) { throw new JS.Exn("cannot put to a resource"); } 
+    public Object put(Object key, Object val) { throw new JS.Exn("cannot put to a resource"); } 
     public Object get(Object key) {
         if ("".equals(key)) {
             Template t = Template.getTemplate(addExtension(".xwt"));
@@ -223,7 +223,8 @@ public abstract class Res extends JS {
                             JS.Array args = new JS.Array();
                             args.addElement(new Integer(bytesDownloaded));
                             args.addElement(new Integer(is instanceof KnownLength ? ((KnownLength)is).getLength() : 0));
-                            new JS.Thread(callback, null, args).resume();
+                            // FIXME
+                            //new JS.Context(callback, null, args).resume();
                         } });
                         return ret;
                     }
index d91c828..bf88c4f 100644 (file)
@@ -97,7 +97,7 @@ public class Template {
         if (staticscript == null) return staticScope;
         JS.CompiledFunction temp = staticscript;
         staticscript = null;
-        new JS.Thread(temp, staticScope).resume();
+        new JS.Context(temp, staticScope, null).resume(null);
         return staticScope;
     }
     
@@ -122,11 +122,11 @@ public class Template {
             Box kid = new Box();
             ((Template)children.elementAt(i)).apply(kid, callback, xwt, pis);
 
-            // FIXME: can't actually pass a null tailcall here
-            b.put(b.numChildren(), kid, null);
+            // FIXME: tailcall?
+            b.put(b.numChildren(), kid);
         }
 
-        if (script != null) new JS.Thread(script, pis).resume();
+        if (script != null) new JS.Context(script, pis, null).resume(null);
 
         for(int i=0; keys != null && i<keys.length; i++)
             if (vals[i] instanceof String && ((String)vals[i]).charAt(0) == '$') b.put(keys[i], pis.get(vals[i]));
@@ -358,9 +358,9 @@ public class Template {
             if (key.equals("static")) return myStatic;
             return super.get(key);
         }
-        public void put(Object key, Object val) {
-            if (super.has(key)) super.put(key, val);
-            super.put(key, val);
+        public Object put(Object key, Object val) {
+            if (super.has(key)) return super.put(key, val);
+            return super.put(key, val);
         }
     }
 
index f982ffa..ea2ea95 100644 (file)
@@ -23,7 +23,7 @@ public class Trap {
     static {
         try {
             cascadeHelper = JS.parse("cascadeHelper", 1, new StringReader(cascadeHelperText));
-            cascadeHelper = (JS.CompiledFunction)new JS.Thread(cascadeHelper).resume();
+            cascadeHelper = (JS.CompiledFunction)new JS.Context(cascadeHelper, null, null).resume(null);
         } catch (Exception e) {
             Log.log(Trap.class, e);
         }
@@ -114,14 +114,14 @@ public class Trap {
 
     // Write Traps //////////////////////////////////////////////////////////////////////
 
-    public void perform(Object val, JS.TailCall tail) {
-        if (f.getNumFormalArgs() == 0) cascade(val, tail);
-        else tail.set(cascadeHelper, new TrapArgs(this, val));
+    public Object perform(Object val) {
+        if (f.getNumFormalArgs() == 0) return cascade(val);
+        else return new JS.TailCall().set(cascadeHelper, new TrapArgs(this, val));
     }
     
-    public void cascade(Object val, JS.TailCall tail) {
-        if (next != null) next.perform(val, tail);
-        else trapee.put(name, val, tail, true);
+    public Object cascade(Object val) {
+        if (next != null) return next.perform(val);
+        else return trapee.put(name, val, true);
     }
 
     // Args ///////////////////////////////////////////////////////////////////////////
@@ -132,9 +132,9 @@ public class Trap {
         public TrapArgs(Trap t) { this.t = t; }
         public TrapArgs(Trap t, Object value) { this.t = t; addElement(value); }
         
-        public void put(Object key, Object val, JS.TailCall tail) {
-            if (key.equals("cascade")) { cascadeHappened = true; t.cascade(val, tail); }
-            else super.put(key, val, (JS.TailCall)tail);
+        public Object put(Object key, Object val) {
+            if (key.equals("cascade")) { cascadeHappened = true; return t.cascade(val); }
+            else return super.put(key, val);
         }
 
         public Object get(Object key) {
index 99b158f..f51f4e6 100644 (file)
@@ -19,12 +19,8 @@ public final class XWT extends JS.Obj {
     private final JS xwtMath = new XWTMath();
     private final JS xwtString = new XWTString();
 
-    /** each key is a string representing a filename which the user has already given XWT permission to write to */
-    private static Hashtable safeFiles = new Hashtable();
-
     public Object get(Object name) {
         if (name.equals("alt")) return Surface.alt ? Boolean.TRUE : Boolean.FALSE;
-        else if (name.equals("rr")) return rr;
         else if (name.equals("box")) return new Box();
         else if (name.equals("control")) return Surface.control ? Boolean.TRUE : Boolean.FALSE;
         else if (name.equals("shift")) return Surface.shift ? Boolean.TRUE : Boolean.FALSE;
@@ -39,21 +35,19 @@ public final class XWT extends JS.Obj {
         else if (name.equals("tempDir")) return System.getProperty("java.io.tempdir");
         else if (name.equals("math")) return xwtMath;
         else if (name.equals("string")) return xwtString;
-        else if (name.equals("parseInt")) return xwtString.get("parseInt");
-        else if (name.equals("parseFloat")) return xwtString.get("parseFloat");
         else if (name.equals("button")) {
             if (Surface.button1 && !Surface.button2 && !Surface.button3) return new Integer(1);
-            else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(1);
-            else if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(1);
+            else if (!Surface.button1 && Surface.button2 && !Surface.button3) return new Integer(2);
+            else if (!Surface.button1 && !Surface.button2 && Surface.button3) return new Integer(3);
             else return new Integer(0);
         }
         else return rr.get(name);
     }
 
-    public void put(Object name, final Object value) {
+    public Object put(Object name, final Object value) {
         if (name.equals("thread") && value != null && (value instanceof JS.Callable || value instanceof JS.CompiledFunction)) {
             Scheduler.add(new Scheduler.Task() { public void perform() {
-                new JS.Thread((CompiledFunction)value).resume();
+                new JS.Context((CompiledFunction)value, null, null).resume(null);
             } });
         } else if (name.equals("clipboard")) Platform.setClipBoard(value.toString());
         else if (name.equals("frame")) Platform.createSurface((Box)value, true, true);
@@ -61,7 +55,8 @@ public final class XWT extends JS.Obj {
         else if (name.equals("proxyAuthorization")) {
             HTTP.Proxy.Authorization.authorization = value.toString();
             HTTP.Proxy.Authorization.waitingForUser.release();
-        } else super.put(name, value);
+        } else return super.put(name, value);
+        return null;
     }
 
     public Object callMethod(Object method, JS.Array args, boolean checkOnly) {
@@ -95,8 +90,7 @@ public final class XWT extends JS.Obj {
 
         } else if (method.equals("yield")) {
             if (checkOnly) return Boolean.TRUE;
-            sleep(0);
-            return null;
+            return sleep(0);
 
         } else if (method.equals("load")) {
             if (checkOnly) return Boolean.TRUE;
@@ -141,44 +135,7 @@ public final class XWT extends JS.Obj {
             if (checkOnly) return Boolean.TRUE;
             if (args != null && (args.length() != 1 || args.elementAt(0) == null)) return null;
             int i = args == null ? 0 : Box.stoi(args.elementAt(0).toString());
-            sleep(i);
-            return null;
-
-        } else if (method.equals("openFile")) {
-            if (checkOnly) return Boolean.TRUE;
-            if (args.length() != 1) return null;
-            String file = Platform.fileDialog(args.elementAt(0).toString(), false);
-            return file == null ? null : new Res.File(file);
-
-        } else if (method.equals("saveFile") || method.equals("saveFileAs")) {
-            if (checkOnly) return Boolean.TRUE;
-            if (args.length() != 2) return null;
-            if (!(args.elementAt(1) instanceof Res)) return null;
-            String file = args.elementAt(0).toString();
-            if (method.equals("saveFileAs") ||
-                safeFiles.get(Platform.isCaseSensitive() ? file : file.toLowerCase()) == null) {
-                file = Platform.fileDialog(file, true);
-                // FIXME: throw exception here
-                if (file == null) return null;
-                safeFiles.put(Platform.isCaseSensitive() ? file : file.toLowerCase(), new Object());
-            }
-            try {
-                InputStream is = ((Res)args.elementAt(1)).getInputStream();
-                FileOutputStream out = new FileOutputStream(file);
-                byte[] buffer = new byte[1024 * 16];
-                while(true) {
-                    int numread = is.read(buffer, 0, buffer.length);
-                    if (numread == -1) break;
-                    out.write(buffer, 0, numread);
-                }
-                is.close();
-                out.close();
-                return null;
-            } catch (IOException e) {
-                if (Log.on) Log.log(XWT.class, "IO Exception while writing a ByteStream to a file");
-                if (Log.on) Log.log(XWT.class, e);
-                throw new JS.Exn("error while writing a Resource to a file");
-            }
+            return sleep(i);
 
         } else if (method.equals("parseHTML")) {
             if (checkOnly) return Boolean.TRUE;
@@ -234,15 +191,15 @@ public final class XWT extends JS.Obj {
         }
     }
 
-    public static void sleep(final int i) {
-        final JS.Thread jsthread = JS.Thread.current();
+    public static Object sleep(final int i) {
+        final JS.Context jsthread = JS.Context.current();
         final long currentTime = System.currentTimeMillis();
         final Scheduler.Task task = new Scheduler.Task() { public void perform() {
             if (System.currentTimeMillis() - currentTime < i) Scheduler.add(this);
-            else jsthread.resume();
+            else jsthread.resume(null);
         } };
-        jsthread.pause();
         Scheduler.add(task);
+        return JS.Context.pause;
     }
     
     private static class XWTMath extends org.xwt.js.Math {