From ac49a62b989af76577f5b62e5afd58e7972623b4 Mon Sep 17 00:00:00 2001 From: megacz Date: Fri, 30 Jan 2004 07:04:16 +0000 Subject: [PATCH] 2003/08/10 06:03:02 darcs-hash:20040130070416-2ba56-38177033c3d418639a932566ee9d9a5b5327a34e.gz --- src/org/xwt/PNG.java | 2 +- src/org/xwt/Platform.java | 8 +- src/org/xwt/Proxy.java | 3 +- src/org/xwt/SpecialBoxProperty.java | 716 ----------------------------------- src/org/xwt/Surface.java | 71 ++-- src/org/xwt/Template.java | 6 +- src/org/xwt/Trap.java | 8 +- src/org/xwt/XWF.java | 226 ----------- src/org/xwt/XWT.java | 20 +- src/org/xwt/plat/GCJ.java | 2 +- src/org/xwt/util/Preprocessor.java | 87 +++++ 11 files changed, 151 insertions(+), 998 deletions(-) delete mode 100644 src/org/xwt/SpecialBoxProperty.java delete mode 100644 src/org/xwt/XWF.java create mode 100644 src/org/xwt/util/Preprocessor.java diff --git a/src/org/xwt/PNG.java b/src/org/xwt/PNG.java index 20979a9..4f5640a 100644 --- a/src/org/xwt/PNG.java +++ b/src/org/xwt/PNG.java @@ -21,7 +21,7 @@ import java.util.Enumeration; import java.util.zip.*; /** Converts an InputStream carrying a PNG image into an ARGB int[] */ -public class PNG implements ImageDecoder { +public class PNG extends ImageDecoder { // Public Methods /////////////////////////////////////////////////////////////////////////////// diff --git a/src/org/xwt/Platform.java b/src/org/xwt/Platform.java index 6ee700e..6f7771f 100644 --- a/src/org/xwt/Platform.java +++ b/src/org/xwt/Platform.java @@ -371,20 +371,20 @@ public class Platform { public static Surface createSurface(Box b, boolean framed, boolean refreshable) { Surface ret = platform._createSurface(b, framed); ret.setInvisible(b.invisible); - b.set(Box.size, 0, b.size(0) < Surface.scarPicture.getWidth() ? Surface.scarPicture.getWidth() : b.size(0)); - b.set(Box.size, 1, b.size(1) < Surface.scarPicture.getHeight() ? Surface.scarPicture.getHeight() : b.size(1)); + b.width = b.height < Surface.scarPicture.getWidth() ? Surface.scarPicture.getWidth() : b.width; + b.height = b.height < Surface.scarPicture.getHeight() ? Surface.scarPicture.getHeight() : b.height; Object titlebar = b.get("titlebar", true); if (titlebar != null) ret.setTitleBarText(titlebar.toString()); Object icon = b.get("icon", true); if (icon != null && !"".equals(icon)) { - Picture pic = Box.getPicture(icon.toString()); + Picture pic = ImageDecoder.getPicture(icon.toString()); if (pic != null) ret.setIcon(pic); else if (Log.on) Log.log(Platform.class, "unable to load icon " + icon); } - ret.setLimits(b.dmin(0), b.dmin(1), b.dmax(0), b.dmax(1)); + ret.setLimits(b.minwidth, b.minheight, b.maxwidth, b.maxheight); if (refreshable) { Surface.refreshableSurfaceWasCreated = true; diff --git a/src/org/xwt/Proxy.java b/src/org/xwt/Proxy.java index 876d35c..def26ca 100644 --- a/src/org/xwt/Proxy.java +++ b/src/org/xwt/Proxy.java @@ -155,7 +155,8 @@ public class Proxy { if (Log.on) Log.log(Authorization.class, "displaying proxy authorization dialog"); MessageQueue.add(new Message() { public void perform() { - Box b = new Box("org.xwt.builtin.proxy_authorization", null); + Box b = new Box(); + Template.getTemplate("org.xwt.builtin.proxy_authorization", null).apply(b, null, null, null, 0, 0); b.put("realm", realm); b.put("proxyIP", proxyIP); } diff --git a/src/org/xwt/SpecialBoxProperty.java b/src/org/xwt/SpecialBoxProperty.java deleted file mode 100644 index 56ee209..0000000 --- a/src/org/xwt/SpecialBoxProperty.java +++ /dev/null @@ -1,716 +0,0 @@ -// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL] -package org.xwt; - -import java.util.*; -import java.net.*; -import java.text.*; -import org.xwt.js.*; -import org.xwt.util.*; - -/** - * A helper class for properties of Box which require special - * handling. - * - * To avoid excessive use of String.equals(), the Box.get() and - * Box.put() methods employ a Hash keyed on property names that - * require special handling. The value stored in the Hash is an - * instance of an anonymous subclass of SpecialBoxProperty, which knows - * how to handle get()s and put()s for that property name. There - * should be one anonymous subclass of SpecialBoxProperty for each - * specially-handled property on Box. - */ -class SpecialBoxProperty { - - SpecialBoxProperty() { } - private static final int NUMINTS = Box.NUMINTS; - private static final int dmax = Box.dmax; - private static final int dmin = Box.dmin; - private static final int cmin = Box.cmin; - private static final int abs = Box.abs; - private static final int pos = Box.pos; - private static final int size = Box.size; - private static final int oldpos = Box.oldpos; - private static final int oldsize = Box.oldsize; - private static final int pad = Box.pad; - - /** stores instances of SpecialBoxProperty; keyed on property name */ - static Hash specialBoxProperties = new Hash(200, 3); - - /** this method defines the behavior when the property is get()ed from b */ - Object get(Box b) { return null; } - - /** this method defines the behavior when the property is put() to b */ - void put(Box b, Object value) { } - - /** this method defines the behavior when the property is put() to b, allows a single SpecialBoxProperty to serve multiple properties */ - void put(String name, Box b, Object value) { put(b, value); } - - // 'ye olde IBM CGA colours', as defined in the XWT reference - static final int black = 0xFF000000; - static final int blue = 0xFF000088; - static final int green = 0xFF008800; - static final int cyan = 0xFF008888; - static final int red = 0xFF880000; - static final int magenta = 0xFF880088; - static final int brown = 0xFF888800; - static final int lightGray = 0xFFBDBEBD; - static final int darkGray = 0xFF242424; - static final int brightBlue = 0xFF0000FF; - static final int brightGreen = 0xFF00FF00; - static final int brightCyan = 0xFF00FFFF; - static final int brightRed = 0xFFFF0000; - static final int pink = 0xFFFF00FF; - static final int yellow = 0xFFFFFF00; - static final int white = 0xFFFFFFFF; - - static { - specialBoxProperties.put("color", new SpecialBoxProperty() { - public Object get(Box b) { - if ((b.color & 0xFF000000) == 0) return null; - String red = Integer.toHexString((b.color & 0x00FF0000) >> 16); - String green = Integer.toHexString((b.color & 0x0000FF00) >> 8); - String blue = Integer.toHexString(b.color & 0x000000FF); - if (red.length() < 2) red = "0" + red; - if (blue.length() < 2) blue = "0" + blue; - if (green.length() < 2) green = "0" + green; - return "#" + red + green + blue; - } - public void put(Box b, Object value) { - int newcolor = b.color; - String s = value == null ? null : value.toString(); - if (value == null) newcolor = 0x00000000; - else if (s.length() > 0 && s.charAt(0) == '#') - try { - newcolor = 0xFF000000 | - (Integer.parseInt(s.substring(1, 3), 16) << 16) | - (Integer.parseInt(s.substring(3, 5), 16) << 8) | - Integer.parseInt(s.substring(5, 7), 16); - } catch (NumberFormatException e) { - Log.log(this, "invalid color " + s); - return; - } - else if (s.equals("black")) newcolor = black; - else if (s.equals("blue")) newcolor = blue; - else if (s.equals("green")) newcolor = green; - else if (s.equals("cyan")) newcolor = cyan; - else if (s.equals("red")) newcolor = red; - else if (s.equals("magenta")) newcolor = magenta; - else if (s.equals("brown")) newcolor = brown; - else if (s.equals("lightGray")) newcolor = lightGray; - else if (s.equals("darkGray")) newcolor = darkGray; - else if (s.equals("brightBlue")) newcolor = brightBlue; - else if (s.equals("brightGreen")) newcolor = brightGreen; - else if (s.equals("brightCyan")) newcolor = brightCyan; - else if (s.equals("brightRed")) newcolor = brightRed; - else if (s.equals("pink")) newcolor = pink; - else if (s.equals("yellow")) newcolor = yellow; - else if (s.equals("white")) newcolor = white; - else if (Log.on) Log.logJS(this, "invalid color \"" + s + "\""); - if (newcolor == b.color) return; - b.color = newcolor; - b.dirty(); - } }); - - - specialBoxProperties.put("textcolor", new SpecialBoxProperty() { - public Object get(Box b) { - if ((b.textcolor & 0xFF000000) == 0) return null; - String red = Integer.toHexString((b.textcolor & 0x00FF0000) >> 16); - String green = Integer.toHexString((b.textcolor & 0x0000FF00) >> 8); - String blue = Integer.toHexString(b.textcolor & 0x000000FF); - if (red.length() < 2) red = "0" + red; - if (blue.length() < 2) blue = "0" + blue; - if (green.length() < 2) green = "0" + green; - return "#" + red + green + blue; - } - public void put(Box b, Object value) { - int newtextcolor = b.color; - if (value == null) return; - String s = value.toString(); - if (s.length() > 0 && s.charAt(0) == '#') - newtextcolor = 0xFF000000 | - (Integer.parseInt(s.substring(1, 3), 16) << 16) | - (Integer.parseInt(s.substring(3, 5), 16) << 8) | - Integer.parseInt(s.substring(5, 7), 16); - else if (s.equals("black")) newtextcolor = black; - else if (s.equals("blue")) newtextcolor = blue; - else if (s.equals("green")) newtextcolor = green; - else if (s.equals("cyan")) newtextcolor = cyan; - else if (s.equals("red")) newtextcolor = red; - else if (s.equals("magenta")) newtextcolor = magenta; - else if (s.equals("brown")) newtextcolor = brown; - else if (s.equals("lightGray")) newtextcolor = lightGray; - else if (s.equals("darkGray")) newtextcolor = darkGray; - else if (s.equals("brightBlue")) newtextcolor = brightBlue; - else if (s.equals("brightGreen")) newtextcolor = brightGreen; - else if (s.equals("brightCyan")) newtextcolor = brightCyan; - else if (s.equals("brightRed")) newtextcolor = brightRed; - else if (s.equals("pink")) newtextcolor = pink; - else if (s.equals("yellow")) newtextcolor = yellow; - else if (s.equals("white")) newtextcolor = white; - else if (Log.on) Log.logJS(this, "invalid color \"" + s + "\""); - if (newtextcolor == b.textcolor) return; - b.textcolor = newtextcolor; - b.dirty(); - } }); - - specialBoxProperties.put("text", new SpecialBoxProperty() { - public Object get(Box b) { return b.text; } - public void put(Box b, Object value) { - String t = value == null ? "null" : value.toString(); - if (t.equals(b.text)) return; - - for(int i=0; i root.size(0)) w = root.size(0) - x; - if (y+h > root.size(1)) h = root.size(1) - y; + if (x+w > root.width) w = root.width - x; + if (y+h > root.height) h = root.height - y; if (w <= 0 || h <= 0) continue; // if any part of this region falls within the "bad region", just skip it diff --git a/src/org/xwt/Template.java b/src/org/xwt/Template.java index 2f14040..1bf8b2b 100644 --- a/src/org/xwt/Template.java +++ b/src/org/xwt/Template.java @@ -197,7 +197,9 @@ public class Template { } for (int i=0; children != null && i - * metrics[glyphnum][0] == x position of left edge of glyph
- * metrics[glyphnum][1] == x position of right edge of glyph
- * metrics[glyphnum][2] == y position of top edge of glyph
- * metrics[glyphnum][3] == y position of bottom edge of glyph
- * metrics[glyphnum][4] == advance amount
- * metrics[glyphnum][5] == baseline - */ - int[][] metrics = null; - - /** drop all cached fonts when the theme mapping changes */ - public static void flushXWFs() { xwtfonts.clear(); } - - /** retrieve an XWF instance, creating it if needed */ - public static XWF getXWF(String resourcename) { - XWF ret = (XWF)xwtfonts.get(resourcename); - if (ret != null) return ret; - - String resolved = Resources.resolve(resourcename + ".xwf", null); - byte[] bytes = Resources.getResource(resolved); - if (bytes != null) { - PNG png = PNG.decode(new ByteArrayInputStream(bytes), resourcename); - if (png != null) return new XWF(resourcename, png); - } - return null; - } - - public int getMaxAscent() { return maxascent; } - public int getMaxDescent() { return maxdescent; } - - /** draws text on buf in this font, with color argb */ - public void drawString(DoubleBuffer buf, String text, int x, int y, int argb) { - - Integer color = new Integer(argb | 0xFF000000); - Picture pg = (Picture)pictures.get(color); - if (pg == null) { - int[] data2 = new int[data.length]; - for(int i=0; i?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~".indexOf(text.charAt(i)); - if (c == -1 || metrics[c] == null) { - left += metrics[64] != null ? metrics[64][4] : 10; - continue; - } - buf.drawPicture(pg, - left, - y - (metrics[c][5] - metrics[c][2]), - left + metrics[c][1] - metrics[c][0], - y - (metrics[c][5] - metrics[c][2]) + metrics[c][3] - metrics[c][2], - metrics[c][0], metrics[c][2], metrics[c][1], metrics[c][3]); - - left += metrics[c][4]; - } - } - - /** returns the width of text when rendered in this font */ - public int stringWidth(String text) { - int ret = 0; - for(int i=0; i?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~".indexOf(text.charAt(i)); - if (c == -1 || metrics[c] == null) { ret += metrics[64][4]; continue; } - ret += metrics[c][4]; - } - return ret; - } - - private XWF(String name, PNG png) { - this.name = name; - xwtfonts.put(name, this); - - data = png.getData(); - w = png.getWidth(); - h = png.getHeight(); - - int x, y, y1; - boolean breakout; - - int start_x, start_y, end_x, end_y, advance; - - int[] rows = new int[120]; - - int baseline = 0; - - metrics = new int[96][]; - int numglyphs = 0; - - for(y=0; y> 16) - ((data[x + y1 * w] & 0x0000FF00) >> 8)) > 10 || - Math.abs(((data[x + y1 * w] & 0x0000FF00) >> 8) - ((data[x + y1 * w] & 0x000000FF))) > 10) { - breakout = true; - baseline = y1; - } - - // search for the next column without a non-grayscale pixel in it - for(breakout = false; x> 16) - ((data[x + y1 * w] & 0x0000FF00) >> 8)) > 10 || - Math.abs(((data[x + y1 * w] & 0x0000FF00) >> 8) - ((data[x + y1 * w] & 0x000000FF))) > 10) { - breakout = false; - } - - x--; - start_x = x; - - // search for the next column with a non-grayscale pixel in it - for(breakout = false; x> 16) - ((data[x + y1 * w] & 0x0000FF00) >> 8)) > 10 || - Math.abs(((data[x + y1 * w] & 0x0000FF00) >> 8) - ((data[x + y1 * w] & 0x000000FF))) > 10) { - breakout = true; - baseline = y1; - } - - x--; - advance = x - start_x; - - // search for the next column without a grayscale pixel in it - for(breakout = false; x> 16) - ((data[x + y1 * w] & 0x0000FF00) >> 8)) > 10 || - Math.abs(((data[x + y1 * w] & 0x0000FF00) >> 8) - ((data[x + y1 * w] & 0x000000FF))) > 10) { - breakout = false; - } - - x--; - end_x = x; - - if (start_x == end_x) break; - - metrics[numglyphs] = new int[6]; - metrics[numglyphs][0] = start_x; - metrics[numglyphs][1] = end_x; - metrics[numglyphs][2] = start_y; - metrics[numglyphs][3] = end_y; - metrics[numglyphs][4] = advance; - metrics[numglyphs][5] = baseline; - numglyphs++; - - if (numglyphs >= metrics.length) break; - - } - - if (numglyphs >= metrics.length) break; - } - - for(int i=0; i> 16) - ((data[i] & 0x0000FF00) >> 8)) > 10 || - Math.abs(((data[i] & 0x0000FF00) >> 8) - ((data[i] & 0x000000FF))) > 10) - data[i] = 0x00; - else - data[i] = (0xFF - (data[i] & 0xFF)) << 24; - - for(int i=33; i<=126; i++) - if (metrics[i - 33] != null) - maxascent = Math.max(maxascent, metrics[i - 33][5] - metrics[i - 33][2]); - - for(int i=33; i<=126; i++) - if (metrics[i - 33] != null) - maxdescent = Math.max(maxdescent, metrics[i - 33][3] - metrics[i - 33][5]); - - } - -} diff --git a/src/org/xwt/XWT.java b/src/org/xwt/XWT.java index 6055a2d..33950f1 100644 --- a/src/org/xwt/XWT.java +++ b/src/org/xwt/XWT.java @@ -130,18 +130,14 @@ public final class XWT extends JS.Obj { if (args.elementAt(0) == null || (args.length() == 2 && args.elementAt(1) == null)) return null; String font = args.length() == 1 ? Platform.getDefaultFont() : args.elementAt(0).toString(); String text = args.length() == 1 ? args.elementAt(0).toString() : args.elementAt(1).toString(); - XWF xwf = XWF.getXWF(font); - if (xwf == null) return new Integer(Platform.stringWidth(font, text)); - else return new Integer(xwf.stringWidth(text)); + return new Integer(Platform.stringWidth(font, text)); }}); super.put("textheight", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn { if (args.length() > 1) return null; if (args.length() == 1 && args.elementAt(0) == null) return null; String font = args.length() == 0 || args.elementAt(0) == null ? Platform.getDefaultFont() : args.elementAt(0).toString(); - XWF xwf = XWF.getXWF(font); - if (xwf == null) return new Integer(Platform.getMaxAscent(font) + Platform.getMaxDescent(font)); - else return new Integer(xwf.getMaxAscent() + xwf.getMaxDescent()); + return new Integer(Platform.getMaxAscent(font) + Platform.getMaxDescent(font)); }}); super.put("newBox", new JS.Callable() { public Object call(JS.Array args) throws JS.Exn { @@ -150,8 +146,10 @@ public final class XWT extends JS.Obj { for(int i=1; i 1 && args.elementAt(1) instanceof JS.Callable ? (JS.Callable)args.elementAt(1) : null); + ImageDecoder.getImageDecoder(args.elementAt(0).toString(), + args.length() > 1 && args.elementAt(1) instanceof JS.Callable ? (JS.Callable)args.elementAt(1) : null); return null; }}); } diff --git a/src/org/xwt/plat/GCJ.java b/src/org/xwt/plat/GCJ.java index 5814bbc..aec30f8 100644 --- a/src/org/xwt/plat/GCJ.java +++ b/src/org/xwt/plat/GCJ.java @@ -38,7 +38,7 @@ public abstract class GCJ extends Platform { } /** Converts an InputStream carrying a JPEG image into an ARGB int[] */ - private static class JPEG implements ImageDecoder { + private static class JPEG extends ImageDecoder { int[] data; byte[] buffer; int width, height; diff --git a/src/org/xwt/util/Preprocessor.java b/src/org/xwt/util/Preprocessor.java new file mode 100644 index 0000000..b653c46 --- /dev/null +++ b/src/org/xwt/util/Preprocessor.java @@ -0,0 +1,87 @@ +// Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL] +package org.xwt.util; + +import java.util.*; +import java.io.*; + +/** + * A VERY crude, inefficient Java preprocessor + * + * //#define FOO bar baz -- replace all instances of token FOO with "bar baz" + * //#replace foo/bar baz/bop -- DUPLICATE everything between here and //#end, + * replacing foo with bar and baz with bop in the *second* copy + * + * Replacements are done on a token basis. Tokens are defined as a + * sequence of characters which all belong to a single class. The + * two character classes are: + * + * - [a-zA-Z0-9_] + * - all other non-whitespace characters + */ +public class Preprocessor { + + static Hashtable replace = new Hashtable(); + static Hashtable savereplace = replace; + static Vector sinceLastRepeat = null; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String s = null; + + while((s = br.readLine()) != null) { + if (sinceLastRepeat != null) sinceLastRepeat.addElement(s); + String trimmed = s.trim(); + if (trimmed.startsWith("//#define ")) { + trimmed = trimmed.substring(10).trim(); + String key = trimmed.substring(0, trimmed.indexOf(' ')); + String val = trimmed.substring(trimmed.indexOf(' ')).trim(); + replace.put(key, val); + + } else if (trimmed.startsWith("//#repeat ")) { + StringTokenizer st = new StringTokenizer(trimmed.substring(9), " "); + savereplace = replace; + replace = (Hashtable)replace.clone(); + while (st.hasMoreTokens()) { + String tok = st.nextToken().trim(); + String key = tok.substring(0, tok.indexOf('/')); + String val = tok.substring(tok.indexOf('/') + 1); + replace.put(key, val); + } + sinceLastRepeat = new Vector(); + + } else if (trimmed.startsWith("//#end")) { + replace = savereplace; + System.out.println(); + for(int i=0; i