reorganized file layout (part 1: moves and renames)
[org.ibex.core.git] / src / org / ibex / graphics / Font.java
diff --git a/src/org/ibex/graphics/Font.java b/src/org/ibex/graphics/Font.java
new file mode 100644 (file)
index 0000000..451672e
--- /dev/null
@@ -0,0 +1,110 @@
+// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
+package org.ibex;
+import org.ibex.translators.*;
+import org.ibex.util.*;
+import java.io.*;
+import java.util.Hashtable;
+
+import org.ibex.js.JSExn;
+
+// FEATURE: this could be cleaner
+/** encapsulates a single font (a set of Glyphs) */
+public class Font {
+
+    private Font(Stream stream, int pointsize) { this.stream = stream; this.pointsize = pointsize; }
+
+    private static boolean glyphRenderingTaskIsScheduled = false;
+
+    public final int pointsize;                 ///< the size of the font
+    public final Stream stream;                 ///< the stream from which this font was loaded
+    public int max_ascent;                      ///< the maximum ascent, in pixels
+    public int max_descent;                     ///< the maximum descent, in pixels
+    boolean latinCharsPreloaded = false;        ///< true if a request to preload ASCII 32-127 has begun
+    Glyph[] glyphs = new Glyph[65535];          ///< the glyphs that comprise this font
+
+    public abstract static class Glyph {
+        protected Glyph(Font font, char c) { this.font = font; this.c = c; }
+        public final Font font;
+        public final char c;
+        public int baseline;                    ///< within the alphamask, this is the y-coordinate of the baseline
+        public int advance;                     ///< amount to increment the x-coordinate
+        public boolean isLoaded = false;        ///< true iff the glyph is loaded
+        public int width = -1;                  ///< the width of the glyph
+        public int height = -1;                 ///< the height of the glyph
+        public byte[] data = null;              ///< the alpha channel samples for this font
+        void render() {
+            if (!isLoaded) try { freetype.renderGlyph(this); } catch (IOException e) { Log.info(Freetype.class, e); }
+            isLoaded = true;
+        }
+    }
+
+
+    // Statics //////////////////////////////////////////////////////////////////////
+
+    static final Freetype freetype = new Freetype();
+    static final Hashtable glyphsToBeCached = new Hashtable();
+    static final Hashtable glyphsToBeDisplayed = new Hashtable();
+    private static Cache fontCache = new Cache(100);
+    public static Font getFont(Stream stream, int pointsize) {
+        Font ret = (Font)fontCache.get(stream, new Integer(pointsize));
+        if (ret == null) fontCache.put(stream, new Integer(pointsize), ret = new Font(stream, pointsize));
+        return ret;
+    }
+
+
+    // Methods //////////////////////////////////////////////////////////////////////
+
+    /**
+     *  Rasterize the glyphs of <code>text</code>.
+     *  @returns <code>(width&lt;&lt;32)|height</code>
+     */
+    public long rasterizeGlyphs(String text, PixelBuffer pb, int textcolor, int x, int y, int cx1, int cy1, int cx2, int cy2) {
+        int width = 0, height = 0;
+        if (!latinCharsPreloaded) {       // preload the Latin-1 charset with low priority (we'll probably want it)
+            for(int i=48; i<57; i++) if (glyphs[i]==null) glyphsToBeCached.put(glyphs[i]=Platform.createGlyph(this, (char)i),"");
+            for(int i=32; i<47; i++) if (glyphs[i]==null) glyphsToBeCached.put(glyphs[i]=Platform.createGlyph(this, (char)i),"");
+            for(int i=57; i<128; i++) if (glyphs[i]==null) glyphsToBeCached.put(glyphs[i]=Platform.createGlyph(this, (char)i),"");
+            if (!glyphRenderingTaskIsScheduled) {
+                Scheduler.add(glyphRenderingTask);
+                glyphRenderingTaskIsScheduled = true;
+            }
+            latinCharsPreloaded = true;
+        }
+        for(int i=0; i<text.length(); i++) {
+            final char c = text.charAt(i);
+            Glyph g = glyphs[c];
+            if (g == null) glyphs[c] = g = Platform.createGlyph(this, c);
+            g.render();
+            if (pb != null) pb.drawGlyph(g, 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);
+        }
+        return ((((long)width) << 32) | (long)(height & 0xffffffffL));
+    }
+
+    // FEATURE do we really need to be caching sizes?
+    private static Cache sizeCache = new Cache(1000);
+    public int textwidth(String s) { return (int)((textsize(s) >>> 32) & 0xffffffff); }
+    public int textheight(String s) { return (int)(textsize(s) & 0xffffffffL); }
+    public long textsize(String s) {
+        Long l = (Long)sizeCache.get(s);
+        if (l != null) return ((Long)l).longValue();
+        long ret = rasterizeGlyphs(s, null, 0, 0, 0, 0, 0, 0, 0);
+        sizeCache.put(s, new Long(ret));
+        return ret;
+    }
+
+    static final Scheduler.Task glyphRenderingTask = new Scheduler.Task() { public void perform() {
+        // FIXME: this should be a low-priority task
+        glyphRenderingTaskIsScheduled = false;
+        if (glyphsToBeCached.isEmpty()) return;
+        Glyph g = (Glyph)glyphsToBeCached.keys().nextElement();
+        if (g == null) return;
+        glyphsToBeCached.remove(g);
+        Log.debug(Font.class, "glyphRenderingTask rendering " + g.c + " of " + g.font);
+        g.render();
+        Log.debug(Glyph.class, "   done rendering glyph " + g.c);
+        glyphRenderingTaskIsScheduled = true;
+        Scheduler.add(this);
+    } };
+}