freetype fixes
[org.ibex.core.git] / src / org / ibex / graphics / Font.java
index 47ba25b..6df9899 100644 (file)
@@ -6,6 +6,7 @@ import java.util.Hashtable;
 import org.ibex.js.*;
 import org.xwt.mips.*;
 import org.ibex.plat.*;
+import org.xwt.mips.Runtime;
 
 // FEATURE: this could be cleaner
 /** encapsulates a single font (a set of Glyphs) */
@@ -112,52 +113,68 @@ 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 Stream loadedStream;
+    private static int loadedStreamAddr;
 
-    public static void loadFontByteStream(Stream res) {
+    static {
+        try {
+            rt = (Runtime)Class.forName("org.ibex.util.MIPSApps").newInstance();
+        } catch(Exception e) {
+            throw new Error("Error instansiating freetype");
+        }
+        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(Stream res) {
+        if(loadedStream == res) return;
+        
         try {
             Log.info(Font.class, "loading font " + res);
-            loadedStream = res;
             InputStream is = Stream.getInputStream(res);
             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);
             
+            glyph.isLoaded = true;
         } catch (Exception e) {
+            // FEATURE: Better error reporting (thow an exception?)
             Log.info(Font.class, e);
         }
     }