import org.xwt.util.*;
import java.io.*;
import java.util.zip.*;
+import java.util.*;
+import org.bouncycastle.util.encoders.Base64;
// FEATURE: use streams, not memoryfont's
// FEATURE: kerning pairs
public class Freetype {
- Freetype() { }
+ public Freetype() { }
- private static org.xwt.mips.Interpreter vm = null;
+ private static byte[] image = null;
+ private static final int FONT_RESERVED = 256*1024;
- public static synchronized void renderGlyphs(Res res, int pointsize, int firstGlyph, int lastGlyph, Cache glyphCache) {
- try {
-
- if (vm == null) {
+ private org.xwt.mips.Interpreter vm = null;
- InputStream bis = Platform.getBuiltinInputStream();
- ZipInputStream zis = new ZipInputStream(bis);
- for(ZipEntry ze = zis.getNextEntry(); ze != null && !ze.getName().equals("freetype.mips"); ze = zis.getNextEntry()) { }
- byte[] image = InputStreamToByteArray.convert(zis);
- vm = new org.xwt.mips.Interpreter(image);
- vm.start(new String[]{ "freetype.mips" });
- vm.execute();
- }
+ private Res loadedStream = null;
- int FONT_RESERVED = 256*1024;
+ public void loadFontByteStream(Res res) {
+ try {
+ Log.log(this, "loading font " + res);
+ loadedStream = res;
+ InputStream is = res.getInputStream();
+ byte[] fontstream = InputStreamToByteArray.convert(is);
+ if (image == null) image = InputStreamToByteArray.convert(Main.builtin.getInputStream("freetype.mips"));
+ vm = new org.xwt.mips.Interpreter(image);
int baseAddr = vm.sbrk(FONT_RESERVED);
-
- byte[] fontstream = InputStreamToByteArray.convert(res.getInputStream());
vm.copyout(fontstream, baseAddr, fontstream.length);
vm.setUserInfo(0, baseAddr);
vm.setUserInfo(1, fontstream.length);
- vm.setUserInfo(2, firstGlyph);
- vm.setUserInfo(3, lastGlyph);
- vm.setUserInfo(4, pointsize);
+ vm.start(new String[]{ "freetype.mips" });
+ vm.execute();
+ } catch (Exception e) {
+ Log.log(this, e);
+ }
+ }
+
+ public synchronized void renderGlyph(Font.Glyph glyph) throws IOException {
+ int width = 0;
+ int height = 0;
+ byte[] data = null;
+ File cacheFile = null;
+ /*
+ try {
+ String key = glyph.font.res.getCacheKey() + ":" + glyph.c;
+ key = new String(Base64.encode(key.getBytes()));
+ cacheFile = new java.io.File(System.getProperty("user.home") +
+ java.io.File.separatorChar + ".xwt" +
+ java.io.File.separatorChar + "caches" +
+ java.io.File.separatorChar + "glyphs" +
+ java.io.File.separatorChar +
+ key);
+ new java.io.File(cacheFile.getParent()).mkdirs();
+ } catch (Res.NotCacheableException e) {
+ Log.log(Freetype.class, "note: glyph not cacheable");
+ }
+ */
+ if (cacheFile != null && cacheFile.exists()) {
+ DataInputStream dis = new DataInputStream(new FileInputStream(cacheFile));
+ width = dis.readInt();
+ height = dis.readInt();
+ glyph.font.max_ascent = dis.readInt();
+ glyph.font.max_descent = dis.readInt();
+ glyph.baseline = dis.readInt();
+ glyph.advance = dis.readInt();
+ data = new byte[width * height];
+ if (width != 0 && height != 0) dis.readFully(data);
+ } else try {
+ //System.out.println("cache miss!");
+ if (loadedStream != glyph.font.res) loadFontByteStream(glyph.font.res);
+ 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);
- for(int g = firstGlyph; g <= lastGlyph; g++) {
- vm.execute();
-
- Glyph glyph = new Glyph();
- glyph.max_ascent = vm.getUserInfo(8);
- glyph.max_descent = vm.getUserInfo(9) - glyph.max_ascent;
- glyph.baseline = vm.getUserInfo(10);
- glyph.advance = vm.getUserInfo(11);
- glyph.c = (char)g;
-
- int width = vm.getUserInfo(6);
- int height = vm.getUserInfo(7);
- byte[] data = new byte[width * height];
- int addr = vm.getUserInfo(5);
+ width = vm.getUserInfo(6);
+ height = vm.getUserInfo(7);
- vm.copyin(addr,data,width*height);
-
- /*for(int i=0; i<width * height; i += 4) {
- int val = vm.memRead(addr + i);
- for (int k = 3; k >= 0; k--) {
- if (i + k < width * height)
- data[i + k] = (val & 0xff) << 24;
- val >>>= 8;
- }
- }*/
-
- glyph.p = Platform.createAlphaOnlyPicture(data, width, height);
- glyphCache.put(res, new Integer((g << 16) | pointsize), glyph);
+ data = new byte[width * height];
+ int addr = vm.getUserInfo(5);
+ vm.copyin(addr,data,width*height);
+ if (cacheFile != null) {
+ File tmp = new File(cacheFile.getCanonicalPath() + ".tmp");
+ DataOutputStream dis = new DataOutputStream(new FileOutputStream(tmp));
+ dis.writeInt(width);
+ dis.writeInt(height);
+ dis.writeInt(glyph.font.max_ascent);
+ dis.writeInt(glyph.font.max_descent);
+ dis.writeInt(glyph.baseline);
+ dis.writeInt(glyph.advance);
+ if (width != 0 && height != 0) dis.write(data, 0, data.length);
+ dis.close();
+ tmp.renameTo(cacheFile);
}
+
} catch (Exception e) {
- Log.log(Freetype.class, e);
+ Log.log(this, e);
}
+
+ if (width == 0 || height == 0) Log.log(Freetype.class, "warning glyph has zero width/height");
+ glyph.p = Platform.createAlphaOnlyPicture(data, width, height);
}
}