2003/11/03 05:28:30
[org.ibex.core.git] / src / org / xwt / Font.java
diff --git a/src/org/xwt/Font.java b/src/org/xwt/Font.java
new file mode 100644 (file)
index 0000000..b1d9230
--- /dev/null
@@ -0,0 +1,93 @@
+// 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 Font {
+
+    public final int pointsize;
+    public final Res res;
+    public int max_ascent;
+    public int max_descent;
+    public Glyph[] glyphs = new Glyph[65535];
+    boolean used = false;
+
+    private Font(Res res, int pointsize) {
+        this.res = res;
+        this.pointsize = pointsize;
+    }
+
+    private static Cache fontCache = new Cache();
+    public static Font getFont(Res res, int pointsize) {
+        Font ret = (Font)fontCache.get(res, new Integer(pointsize));
+        if (ret == null) fontCache.put(res, new Integer(pointsize), ret = new Font(res, pointsize));
+        return ret;
+    }
+
+    /**
+     *  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 long rasterizeGlyphs(final String text, PixelBuffer pb, int textcolor,
+                                int x, int y, int cx1, int cy1, int cx2, int cy2,
+                                final Scheduler.Task callback) {
+        boolean encounteredUnrenderedGlyph = false;
+        int width = 0;
+        int height = 0;
+        for(int i=0; i<text.length(); i++) {
+            final char c = text.charAt(i);
+            Glyph g = glyphs[c];
+            if (g == null) glyphsToBeRendered.prepend(g = new Glyph(c));
+            if (g.p == null) {
+                glyphsToBeRendered.prepend(g);
+                Scheduler.add(glyphRenderingTask);
+                encounteredUnrenderedGlyph = true;
+            } else if (!encounteredUnrenderedGlyph) {
+                if (pb != null && g.p != null)
+                    pb.drawPictureAlphaOnly(g.p, x + width, y + g.font.max_ascent - g.baseline,
+                                            cx1, cy1, cx2, cy2, textcolor);
+                width += g.advance;
+                height = java.lang.Math.max(height, max_ascent + max_descent);
+            }
+        }
+        
+        // FIXME: be cleaner here
+        if (encounteredUnrenderedGlyph) Scheduler.add(new Scheduler.Task() { public void perform() {
+            for(int i=0; i<text.length(); i++) {
+                Glyph g = glyphs[text.charAt(i)];
+                if (g == null || g.p == null) { Scheduler.add(this); return; }
+            }
+            callback.perform();
+        }});
+    
+        if (!used) for(int i=32; i<128; i++) glyphsToBeRendered.append(glyphs[i] = new Glyph((char)i));
+        used = true;
+        return ((long)width << 16) | (long)height;
+    }
+
+
+    public class Glyph {
+        public char c;
+        public int baseline;       // within the picture, this is the y-coordinate of the baseline
+        public int advance;        // amount to increment the x-coordinate
+        public Picture p;
+        public final Font font;
+        public Glyph(char c) { this.c = c; font = Font.this; }
+    }
+
+    private static final Freetype freetype = new Freetype();
+    static final Queue glyphsToBeRendered = new Queue(255);
+    static final Scheduler.Task glyphRenderingTask = new Scheduler.Task() { public void perform() {
+        Glyph g = (Glyph)glyphsToBeRendered.remove(false);
+        if (g == null || g.p != null) return;
+        Log.log(Glyph.class, "rendering glyph " + g.c);
+        freetype.renderGlyph(g);
+        Scheduler.add(this);
+    } };
+}