fonts now rendered immediately on-demand
authoradam <adam@megacz.com>
Wed, 7 Apr 2004 20:47:51 +0000 (20:47 +0000)
committeradam <adam@megacz.com>
Wed, 7 Apr 2004 20:47:51 +0000 (20:47 +0000)
darcs-hash:20040407204751-5007d-fc265cbebcb47b356cda0701859b0b16f0457e04.gz

src/org/ibex/Box.java
src/org/ibex/Font.java
src/org/ibex/Ibex.java

index bb57d5a..c2f16dc 100644 (file)
@@ -537,8 +537,7 @@ public final class Box extends JSScope implements Scheduler.Task {
             int gap_y = height - font.textheight(text);
             int text_x = globalx + (test(ALIGN_RIGHT) ? gap_x : !test(ALIGN_LEFT) ? gap_x/2 : 0);
             int text_y = globaly + (test(ALIGN_BOTTOM) ? gap_y : !test(ALIGN_TOP) ? gap_y/2 : 0);
-            if (font.rasterizeGlyphs(text, buf, strokecolor, text_x, text_y, cx1, cy1, cx2, cy2, null) == -1)
-                font.rasterizeGlyphs(text, buf, strokecolor, text_x, text_y, cx1, cy1, cx2, cy2, this);
+            font.rasterizeGlyphs(text, buf, strokecolor, text_x, text_y, cx1, cy1, cx2, cy2);
         }
 
         for(Box b = getChild(0); b != null; b = b.nextSibling())
index 48fd0de..bbe0ac2 100644 (file)
@@ -32,12 +32,13 @@ public class Font {
         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); } }
     }
 
 
     // Statics //////////////////////////////////////////////////////////////////////
 
-    private static final Freetype freetype = new Freetype();
+    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);
@@ -52,65 +53,30 @@ public class Font {
 
     /**
      *  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 <code>(width&lt;&lt;32)|height</code> if all glyphs are loaded; else -1
+     *  @returns <code>(width&lt;&lt;32)|height</code>
      */
-    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;
+    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;
-        
-        // preload the Latin-1 charset with low priority (we'll probably want it)
-        if (!latinCharsPreloaded) {
-            for(int i=48; i<57; i++) glyphsToBeCached.put(glyphs[i] = Platform.createGlyph(this, (char)i),"");
-            for(int i=32; i<47; i++) glyphsToBeCached.put(glyphs[i] = Platform.createGlyph(this, (char)i),"");
-            for(int i=57; i<128; i++) glyphsToBeCached.put(glyphs[i] = Platform.createGlyph(this, (char)i),"");            
+        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;
-        
-       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) {
-                       g = Platform.createGlyph(this, c);
-                       glyphs[c] = g;
-               }
-               if (!g.isLoaded) {
-                       if (!glyphsToBeDisplayed.contains(g)) {
-                               glyphsToBeDisplayed.put(g, "");              // move glyph to the display hash
-                               glyphsToBeCached.remove(g);
-                               encounteredUnrenderedGlyph = true;
-                       }
-               } else if (!encounteredUnrenderedGlyph) {
-                       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);
-               }
+            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);
         }
-        
-        if (!encounteredUnrenderedGlyph) return ((((long)width) << 32) | (long)(height & 0xffffffffL));
-        
-        if (callback != null) Scheduler.add(new Scheduler.Task() {
-               public void perform() throws IOException, JSExn {
-                       // 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.isLoaded) { Scheduler.add(this); return; }
-                       }
-                       callback.perform();
-               }});
-        return -1;
+        return ((((long)width) << 32) | (long)(height & 0xffffffffL));
     }
 
     // FEATURE do we really need to be caching sizes?
@@ -120,32 +86,22 @@ public class Font {
     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, null);
-        if (ret != -1) sizeCache.put(s, new Long(ret));
-        return ret == -1 ? 0 : ret;
+        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() {
-        Glyph g = null;
-        if (!glyphsToBeDisplayed.isEmpty()) {
-            g = (Glyph)glyphsToBeDisplayed.keys().nextElement();
-            glyphsToBeDisplayed.remove(g);
-            Log.debug(Font.class, "glyphRenderingTask removed glyph " + g.c + " of font " /*+ g.font*/ + " from glyphsToBeDisplayed");
-        }
-        else if (!glyphsToBeCached.isEmpty()) {
-            g = (Glyph)glyphsToBeCached.keys().nextElement();
-            glyphsToBeCached.remove(g);
-            Log.debug(Font.class, "glyphRenderingTask removed glyph " + g.c + " of font " /*+ g.font*/ + " from glyphsToBeCached");
-        }
-        if (g != null) {
-            Log.debug(Glyph.class, "rendering glyph " + g);
-            try { freetype.renderGlyph(g); } catch (IOException e) { Log.info(Freetype.class, e); }
-            Scheduler.add(this);          // keep ourselves in the queue until there are no glyphs to render
-            glyphRenderingTaskIsScheduled = true;
-        }
-        else {
-            glyphRenderingTaskIsScheduled = false;
-            return;
-        }
+        // 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);
     } };
 }
index d1e3284..d97a9d5 100644 (file)
@@ -201,15 +201,7 @@ public final class Ibex extends JS.Cloneable {
                 case 3:
                     //#switch(name)
                     case "ui.font.height": return N(Font.getFont((Stream)a, JS.toInt(b)).textheight((String)c));
-                    case "ui.font.wait":
-                        try {
-                            JS.UnpauseCallback callback = JS.pause();
-                            Font font = Font.getFont((Stream)a, JS.toInt(b));
-                            if (font.rasterizeGlyphs((String)c, null, 0,0,0,0,0,0,0, callback) != -1) Scheduler.add(callback);
-                        } catch (JS.NotPauseableException npe) {
-                            JS.error("can't wait for a font in a foreground thread");
-                        }
-                        return null;
+                    case "ui.font.wait": throw new Error("FIXME: ibex.ui.font.wait not implemented");
                     case "ui.font.width": return N(Font.getFont((Stream)a, JS.toInt(b)).textwidth((String)c));
                     //#end
                     break;