+++ /dev/null
-// 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;
- }
-}
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"));
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;
}
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;
}
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]));
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);
}
}
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);
}
// 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 ///////////////////////////////////////////////////////////////////////////
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) {
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;
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);
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) {
} 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;
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;
}
}
- 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 {