2003/12/13 08:13:32
[org.ibex.core.git] / src / org / xwt / Font.java
index e14727f..6081253 100644 (file)
@@ -6,31 +6,37 @@ import org.xwt.js.*;
 import java.util.*;
 import java.io.*;
 
+/** encapsulates a single font (a set of Glyphs) */
 public class Font {
 
     private Font(Res res, int pointsize) { this.res = res; this.pointsize = pointsize; }
 
-    public final int pointsize;
-    public final Res res;
-    public int max_ascent;
-    public int max_descent;
+    private static boolean glyphRenderingTaskIsScheduled = false;
+
+    public final int pointsize;                 ///< the size of the font
+    public final Res res;                       ///< the resource 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];
-
-    public static class Glyph {
-        public Glyph(char c, Font f) { this.c = c; font = f; }
-        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;
+    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 byte[] alphaChannel = null;
+        public int width = -1;                  ///< the width of the glyph
+        public int height = -1;                 ///< the height of the glyph
+        public byte[] data = null;
     }
 
 
     // Statics //////////////////////////////////////////////////////////////////////
 
     private static final Freetype freetype = new Freetype();
-    private static Cache sizeCache = new Cache(100);
     static final Queue glyphsToBeRendered = new Queue(255);
     private static Cache fontCache = new Cache(100);
     public static Font getFont(Res res, int pointsize) {
@@ -43,53 +49,63 @@ public class Font {
     // Methods //////////////////////////////////////////////////////////////////////
 
     /**
-     *  If the glyphs of <code>text</code> are not yet loaded, spawn a
-     *  Task to load them and invoke callback.
+     *  Rasterize the glyphs of <code>text</code>.
+     *
+     *  If all the glyphs of <code>text</code> are not yet loaded,
+     *  spawn a Task to load them and then invoke callback.  If all
+     *  the glyphs <i>are</i> loaded, rasterize them to the
+     *  PixelBuffer (if non-null).
      *
-     *  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.
+     *  @returns <code>(width&lt;&lt;32)|height</code> if all glyphs are loaded; else -1
      */
     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;
+        int width = 0, 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 = glyphs[c] = new Glyph(c, this));    // prepend so they are high priority
-            if (g.p == null) {
-                glyphsToBeRendered.prepend(g);
+            if (g == null) glyphs[c] = g = Platform.createGlyph(this, c);    // prepend so they are high priority
+            if (!g.isLoaded) {
+                glyphsToBeRendered.prepend(g);              // even if it's already in the queue, boost its priority
                 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);
+                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);
             }
         }
 
-        if (encounteredUnrenderedGlyph && callback != null) Scheduler.add(new Scheduler.Task() {
-                public void perform() throws Exception{
+        if (!encounteredUnrenderedGlyph) return ((((long)width) << 32) | (long)(height & 0xffffffffL));
+
+        if (callback != null) Scheduler.add(new Scheduler.Task() {
+                public void perform() throws Exception {
+                    // FEATURE this isn't terribly efficient... perhaps the task should go on the last glyph?
                     for(int i=0; i<text.length(); i++) {
                         Glyph g = glyphs[text.charAt(i)];
-                        if (g == null || g.p == null) { Scheduler.add(this); return; }
+                        if (g == null || !g.isLoaded) { Scheduler.add(this); return; }
                     }
                     callback.perform();
                 }});
+
+        // preload the Latin-1 charset with low priority (we'll probably want it)
         if (!latinCharsPreloaded) {
-            for(int i=48; i<57; i++) glyphsToBeRendered.append(glyphs[i] = new Glyph((char)i, this));
-            for(int i=32; i<47; i++) glyphsToBeRendered.append(glyphs[i] = new Glyph((char)i, this));
-            for(int i=57; i<128; i++) glyphsToBeRendered.append(glyphs[i] = new Glyph((char)i, this));
+            for(int i=48; i<57; i++) glyphsToBeRendered.append(glyphs[i] = Platform.createGlyph(this, (char)i));
+            for(int i=32; i<47; i++) glyphsToBeRendered.append(glyphs[i] = Platform.createGlyph(this, (char)i));
+            for(int i=57; i<128; i++) glyphsToBeRendered.append(glyphs[i] = Platform.createGlyph(this, (char)i));
+            latinCharsPreloaded = true;
+        }
+        if (!glyphRenderingTaskIsScheduled) {
+            Scheduler.add(glyphRenderingTask);
+            glyphRenderingTaskIsScheduled = true;
         }
-        if (!latinCharsPreloaded || encounteredUnrenderedGlyph) Scheduler.add(glyphRenderingTask);
-        latinCharsPreloaded = true;
-        if (encounteredUnrenderedGlyph) return -1;
-        return ((((long)width) << 32) | (long)(height & 0xffffffffL));
+        return -1;
     }
 
+    // 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) {
@@ -102,10 +118,12 @@ public class Font {
 
     static final Scheduler.Task glyphRenderingTask = new Scheduler.Task() { public void perform() {
         Glyph g = (Glyph)glyphsToBeRendered.remove(false);
-        if (g == null) return;
-        if (g.p != null) { perform(); return; }
+        if (g == null) { glyphRenderingTaskIsScheduled = false; return; }
+        if (g.isLoaded) { perform(); /* tailcall to the next glyph */ return; }
         Log.log(Glyph.class, "rendering glyph " + g.c);
         try { freetype.renderGlyph(g); } catch (IOException e) { Log.log(Freetype.class, e); }
-        Scheduler.add(this);
+        g.isLoaded = true;
+        Scheduler.add(this);          // keep ourselves in the queue until there are no glyphs to render
+        glyphRenderingTaskIsScheduled = true;
     } };
 }