added Font.getGlyph(char)
[org.ibex.core.git] / src / org / ibex / graphics / Font.java
index 47ba25b..1f8f35c 100644 (file)
@@ -1,26 +1,31 @@
-// 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.core.*;
+import org.ibex.nestedvm.*;
 import org.ibex.plat.*;
+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
-    Glyph[] glyphs = new Glyph[65535];          ///< the glyphs that comprise this font
+    public 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; }
@@ -32,6 +37,7 @@ 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
+        public String path = null; // FIXME this should be shared across point sizes
         void render() {
             if (!isLoaded) try { renderGlyph(this); } catch (IOException e) { Log.info(Font.class, e); }
             isLoaded = true;
@@ -43,10 +49,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.HashMap();
+    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.HashMap());
+        if (ret == null) m.put(new Integer(pointsize), ret = new Font(stream, pointsize));
         return ret;
     }
 
@@ -82,7 +92,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) {
@@ -93,18 +103,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);
+        return null;
     } };
 
 
@@ -112,52 +130,73 @@ public class Font {
 
     // FEATURE: use streams, not memoryfont's
     // FEATURE: kerning pairs
-    private static int mem_allocated = 0;
-    private static org.xwt.mips.Runtime vm = null;
-    private static Stream loadedStream = null;
+    private static final Runtime rt;
+    private static JS loadedStream;
+    private static int loadedStreamAddr;
 
-    public static void loadFontByteStream(Stream res) {
+    static {
         try {
-            Log.info(Font.class, "loading font " + res);
-            loadedStream = res;
-            InputStream is = Stream.getInputStream(res);
+            rt = (Runtime)Class.forName("org.ibex.util.MIPSApps").newInstance();
+        } catch(Exception e) {
+            throw new Error("Error instantiating freetype: " + e);
+        }
+        rt.start(new String[]{"freetype"});
+        rt.execute();
+        rtCheck();
+    }
+    
+    private static void rtCheck() { if(rt.getState() != Runtime.PAUSED) throw new Error("Freetype exited " + rt.exitStatus()); }
+    
+    private static void loadFontByteStream(JS res) {
+        if(loadedStream == res) return;
+        
+        try {
+            Log.info(Font.class, "loading font " + JSU.str(res));
+            InputStream is = res.getInputStream();
             byte[] fontstream = InputStreamToByteArray.convert(is);
-            vm = (org.xwt.mips.Runtime)Class.forName("org.ibex.util.MIPSApps").newInstance();
-            int baseAddr = vm.sbrk(fontstream.length);
-            vm.copyout(fontstream, baseAddr, fontstream.length);
-            vm.setUserInfo(0, baseAddr);
-            vm.setUserInfo(1, fontstream.length);
-            vm.start(new String[]{ "freetype" });
-            vm.execute();
-            if(vm.getState() == org.xwt.mips.Runtime.DONE) throw new Error("Freetype VM exited: " + vm.exitStatus());
+            rt.free(loadedStreamAddr);
+            loadedStreamAddr = rt.xmalloc(fontstream.length);
+            rt.copyout(fontstream, loadedStreamAddr, fontstream.length);
+            if(rt.call("load_font", loadedStreamAddr, fontstream.length) != 0)
+                throw new RuntimeException("load_font failed"); // FEATURE: better error
+            rtCheck();
+            loadedStream = res;
         } catch (Exception e) {
+            // FEATURE: Better error reporting (thow an exception?)
             Log.info(Font.class, e);
         }
     }
 
-    public static synchronized void renderGlyph(Font.Glyph glyph) throws IOException {
+    private static synchronized void renderGlyph(Font.Glyph glyph) throws IOException {
         try {
             Log.debug(Font.class, "rasterizing glyph " + glyph.c + " of font " + glyph.font);
             if (loadedStream != glyph.font.stream) loadFontByteStream(glyph.font.stream);
-            vm.setUserInfo(2, (int)glyph.c);
-            vm.setUserInfo(3, (int)glyph.c);
-            vm.setUserInfo(4, glyph.font.pointsize);
+            
             long start = System.currentTimeMillis();
-            vm.execute();
-            glyph.font.max_ascent = vm.getUserInfo(8);
-            glyph.font.max_descent = vm.getUserInfo(9);
-            glyph.baseline = vm.getUserInfo(10);
-            glyph.advance = vm.getUserInfo(11);
             
-            glyph.width = vm.getUserInfo(6);
-            glyph.height = vm.getUserInfo(7);
+            rt.call("render",glyph.c,glyph.font.pointsize);
+            rtCheck();
             
+            glyph.font.max_ascent = rt.getUserInfo(3);
+            glyph.font.max_descent = rt.getUserInfo(4);
+            glyph.baseline = rt.getUserInfo(5);
+            glyph.advance = rt.getUserInfo(6);
+            
+            glyph.width = rt.getUserInfo(1);
+            glyph.height = rt.getUserInfo(2);
+
             glyph.data = new byte[glyph.width * glyph.height];
-            int addr = vm.getUserInfo(5);
-            vm.copyin(addr, glyph.data, glyph.width * glyph.height);
-            glyph.isLoaded = true;
+            int addr = rt.getUserInfo(0);
+            rt.copyin(addr, glyph.data, glyph.width * glyph.height);
             
+            byte[] path = new byte[rt.getUserInfo(8)];
+            rt.copyin(rt.getUserInfo(7), path, rt.getUserInfo(8));
+            glyph.path = new String(path);
+            glyph.path += " m " + (64 * glyph.advance) + " 0 "; 
+
+            glyph.isLoaded = true;
         } catch (Exception e) {
+            // FEATURE: Better error reporting (thow an exception?)
             Log.info(Font.class, e);
         }
     }