reorganized file layout (part 2: edits)
[org.ibex.core.git] / src / org / ibex / graphics / Font.java
index 451672e..47ba25b 100644 (file)
@@ -1,11 +1,11 @@
 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
-package org.ibex;
-import org.ibex.translators.*;
+package org.ibex.graphics;
 import org.ibex.util.*;
 import java.io.*;
 import java.util.Hashtable;
-
-import org.ibex.js.JSExn;
+import org.ibex.js.*;
+import org.xwt.mips.*;
+import org.ibex.plat.*;
 
 // FEATURE: this could be cleaner
 /** encapsulates a single font (a set of Glyphs) */
@@ -33,7 +33,7 @@ public class Font {
         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); }
+            if (!isLoaded) try { renderGlyph(this); } catch (IOException e) { Log.info(Font.class, e); }
             isLoaded = true;
         }
     }
@@ -41,7 +41,6 @@ public class Font {
 
     // Statics //////////////////////////////////////////////////////////////////////
 
-    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);
@@ -94,7 +93,7 @@ public class Font {
         return ret;
     }
 
-    static final Scheduler.Task glyphRenderingTask = new Scheduler.Task() { public void perform() {
+    static final Task glyphRenderingTask = new Task() { public void perform() {
         // FIXME: this should be a low-priority task
         glyphRenderingTaskIsScheduled = false;
         if (glyphsToBeCached.isEmpty()) return;
@@ -107,4 +106,59 @@ public class Font {
         glyphRenderingTaskIsScheduled = true;
         Scheduler.add(this);
     } };
+
+
+    // FreeType Interface //////////////////////////////////////////////////////////////////////////////
+
+    // 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;
+
+    public static void loadFontByteStream(Stream res) {
+        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());
+        } catch (Exception e) {
+            Log.info(Font.class, e);
+        }
+    }
+
+    public 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);
+            
+            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;
+            
+        } catch (Exception e) {
+            Log.info(Font.class, e);
+        }
+    }
 }