added missing patches to move to fillTriangle() and Mesh
[org.ibex.core.git] / src / org / ibex / graphics / Font.java
index 99c9bdc..b2c30ca 100644 (file)
@@ -17,8 +17,6 @@ public class Font {
 
     private Font(JS 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 JS stream;                 ///< the stream from which this font was loaded
     public int max_ascent;                      ///< the maximum ascent, in pixels
@@ -46,9 +44,8 @@ public class Font {
 
     // Statics //////////////////////////////////////////////////////////////////////
 
-    static final Hashtable glyphsToBeCached = new Hashtable();
-    static final Hashtable glyphsToBeDisplayed = new Hashtable();
-    // HACK: replace with Cache<JS, int>
+    static final Queue toBeRasterized = new Queue(300);
+    static final Queue toBeDisplayed = new Queue(300);
     private static Basket.Map fonts = new Basket.Hash();
     public static Font getFont(JS stream, int pointsize) {
         Basket.Map m = (Basket.Map)fonts.get(stream);
@@ -66,35 +63,36 @@ public class Font {
      *  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) {
-        return rasterizeGlyphs(text, pb, textcolor, x, y, cx1, cy1, cx2, cy2, 0); }
-    public long rasterizeGlyphs(String text, PixelBuffer pb, int textcolor, int x, int y,
-                                int cx1, int cy1, int cx2, int cy2, int bg) {
+    public String rasterizeGlyphs(String text) {
+        StringBuffer path = new StringBuffer(text.length() * 50);
+        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();
+            path.append(g.path);
+        }
+        return path.toString();
+    }
+    public long rasterizeGlyphs(String text, PixelBuffer pb, Affine a, Mesh h, int fg, int bg) {
         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) {
-                Platform.Scheduler.add(glyphRenderingTask);
-                glyphRenderingTaskIsScheduled = true;
-            }
+            for(int i=48; i<57; i++) if(glyphs[i]==null) toBeRasterized.append(glyphs[i]=Platform.createGlyph(this, (char)i));
+            for(int i=32; i<47; i++) if(glyphs[i]==null) toBeRasterized.append(glyphs[i]=Platform.createGlyph(this, (char)i));
+            for(int i=57; i<128; i++) if(glyphs[i]==null) toBeRasterized.append(glyphs[i]=Platform.createGlyph(this, (char)i));
             latinCharsPreloaded = true;
         }
+        a = a==null ? null : a.copy();
         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,
-                                         x+width,
-                                         cy1,
-                                         x+width+g.advance,
-                                         cy2,
-                                         textcolor, bg);
+            if (a!=null) a.premultiply(Affine.translate(0, g.font.max_ascent - g.baseline));
+            if (pb != null) pb.drawGlyph(g, a, h, fg, bg);
+            if (a!=null) a.premultiply(Affine.translate(0, -1 * (g.font.max_ascent - g.baseline)));
             width += g.advance;
+            if (a!=null) a.premultiply(Affine.translate(g.advance, 0));
             height = java.lang.Math.max(height, max_ascent + max_descent);
         }
         return ((((long)width) << 32) | (long)(height & 0xffffffffL));
@@ -107,32 +105,27 @@ 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);
+        long ret = rasterizeGlyphs(s, null, null, null, 0, 0);
         sizeCache.put(s, new Long(ret));
         return ret;
     }
 
     public Glyph getGlyph(char c) {
-        rasterizeGlyphs(c+"", null, 0, 0, 0, 0, 0, 0, 0);
+        rasterizeGlyphs(c+"", null, null, null, 0, 0);
         Glyph g = glyphs[c];
         g.render();
         return g;
     }
 
-    static final Callable glyphRenderingTask = new Callable() { public Object run(Object o) {
-        // FIXME: this should be a low-priority task
-        glyphRenderingTaskIsScheduled = false;
-        if (glyphsToBeCached.isEmpty()) return null;
-        Glyph g = (Glyph)glyphsToBeCached.keys().nextElement();
-        if (g == null) return null;
-        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;
-        Platform.Scheduler.add(this);
-        return null;
-    } };
+    static { new Thread() { public void run() {
+        while(true) {
+            Glyph g = (Glyph)toBeRasterized.remove(true);
+            if (g == null) continue;
+            Log.debug(Font.class, "glyphRenderingTask rendering " + g.c + " of " + g.font);
+            g.render();
+            Log.debug(Glyph.class, "   done rendering glyph " + g.c);
+        }
+    } }.start(); }
 
 
     // FreeType Interface //////////////////////////////////////////////////////////////////////////////