+
+
+ // FreeType Interface //////////////////////////////////////////////////////////////////////////////
+
+ // FEATURE: use streams, not memoryfont's
+ // FEATURE: kerning pairs
+ private static final Runtime rt;
+ private static JS loadedStream;
+ private static int loadedStreamAddr;
+
+ static {
+ try {
+ 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);
+ 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);
+ }
+ }
+
+ 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);
+
+ long start = System.currentTimeMillis();
+
+ 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 = 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);
+ }
+ }