Stream->Fountain, move Scheduler to Platform, HashMap->Hash
[org.ibex.core.git] / src / org / ibex / graphics / Font.java
index 9870aa6..99c9bdc 100644 (file)
@@ -1,23 +1,26 @@
-// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the GNU General Public License version 2 ("the License").
+// You may not use this file except in compliance with the License.
+
 package org.ibex.graphics;
 import org.ibex.util.*;
 import java.io.*;
 import java.util.Hashtable;
 import org.ibex.js.*;
-import org.xwt.mips.*;
+import org.ibex.nestedvm.*;
 import org.ibex.plat.*;
-import org.xwt.mips.Runtime;
+import org.ibex.nestedvm.Runtime;
 
 // 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 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 Stream stream;                 ///< the stream from which this font was loaded
+    public final JS 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
@@ -45,10 +48,14 @@ public class Font {
 
     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));
+    // HACK: replace with Cache<JS, int>
+    private static Basket.Map fonts = new Basket.Hash();
+    public static Font getFont(JS stream, int pointsize) {
+        Basket.Map m = (Basket.Map)fonts.get(stream);
+        Font ret = null;
+        if (m != null) ret = (Font)m.get(new Integer(pointsize));
+        else fonts.put(stream, m = new Basket.Hash());
+        if (ret == null) m.put(new Integer(pointsize), ret = new Font(stream, pointsize));
         return ret;
     }
 
@@ -60,13 +67,16 @@ public class Font {
      *  @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) {
         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);
+                Platform.Scheduler.add(glyphRenderingTask);
                 glyphRenderingTaskIsScheduled = true;
             }
             latinCharsPreloaded = true;
@@ -76,7 +86,14 @@ public class Font {
             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);
+            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);
             width += g.advance;
             height = java.lang.Math.max(height, max_ascent + max_descent);
         }
@@ -84,7 +101,7 @@ public class Font {
     }
 
     // FEATURE do we really need to be caching sizes?
-    private static Cache sizeCache = new Cache(1000);
+    private static Cache sizeCache = new Cache(1000, true);
     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) {
@@ -95,18 +112,26 @@ public class Font {
         return ret;
     }
 
-    static final Task glyphRenderingTask = new Task() { public void perform() {
+    public Glyph getGlyph(char c) {
+        rasterizeGlyphs(c+"", null, 0, 0, 0, 0, 0, 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;
+        if (glyphsToBeCached.isEmpty()) return null;
         Glyph g = (Glyph)glyphsToBeCached.keys().nextElement();
-        if (g == null) return;
+        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;
-        Scheduler.add(this);
+        Platform.Scheduler.add(this);
+        return null;
     } };
 
 
@@ -115,7 +140,7 @@ public class Font {
     // FEATURE: use streams, not memoryfont's
     // FEATURE: kerning pairs
     private static final Runtime rt;
-    private static Stream loadedStream;
+    private static JS loadedStream;
     private static int loadedStreamAddr;
 
     static {
@@ -131,12 +156,12 @@ public class Font {
     
     private static void rtCheck() { if(rt.getState() != Runtime.PAUSED) throw new Error("Freetype exited " + rt.exitStatus()); }
     
-    private static void loadFontByteStream(Stream res) {
+    private static void loadFontByteStream(JS res) {
         if(loadedStream == res) return;
         
         try {
-            Log.info(Font.class, "loading font " + res);
-            InputStream is = Stream.getInputStream(res);
+            Log.info(Font.class, "loading font " + JSU.str(res));
+            InputStream is = JSU.getInputStream(res);
             byte[] fontstream = InputStreamToByteArray.convert(is);
             rt.free(loadedStreamAddr);
             loadedStreamAddr = rt.xmalloc(fontstream.length);