X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fgraphics%2FFont.java;h=2b8b13cdca0f92feb663aa29e999f9b74e87feb6;hp=451672e4a3ad254ffdb6c3af31657b56e78e8f6c;hb=c8598d51fe71ce77d466c02b6fa264617be9335b;hpb=4daeeb4119b901d53b44913c86f8af3ce67db925 diff --git a/src/org/ibex/graphics/Font.java b/src/org/ibex/graphics/Font.java index 451672e..2b8b13c 100644 --- a/src/org/ibex/graphics/Font.java +++ b/src/org/ibex/graphics/Font.java @@ -1,11 +1,12 @@ // 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.*; +import org.xwt.mips.Runtime; // FEATURE: this could be cleaner /** encapsulates a single font (a set of Glyphs) */ @@ -33,7 +34,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 +42,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 +94,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 +107,75 @@ public class Font { glyphRenderingTaskIsScheduled = true; Scheduler.add(this); } }; + + + // FreeType Interface ////////////////////////////////////////////////////////////////////////////// + + // FEATURE: use streams, not memoryfont's + // FEATURE: kerning pairs + private static final Runtime rt; + private static Stream 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(Stream res) { + if(loadedStream == res) return; + + try { + Log.info(Font.class, "loading font " + res); + InputStream is = Stream.getInputStream(res); + 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); + + glyph.isLoaded = true; + } catch (Exception e) { + // FEATURE: Better error reporting (thow an exception?) + Log.info(Font.class, e); + } + } }