1 package org.xwt.translators;
5 import java.util.zip.*;
7 import org.bouncycastle.util.encoders.Base64;
9 // FEATURE: use streams, not memoryfont's
10 // FEATURE: kerning pairs
11 public class Freetype {
15 private static byte[] image = null;
16 private static final int FONT_RESERVED = 256*1024;
18 private org.xwt.mips.Interpreter vm = null;
20 private Res loadedStream = null;
22 public void loadFontByteStream(Res res) {
24 Log.log(this, "loading font " + res);
26 InputStream is = res.getInputStream();
27 byte[] fontstream = InputStreamToByteArray.convert(is);
28 if (image == null) image = InputStreamToByteArray.convert(Main.builtin.getInputStream("freetype.mips"));
29 vm = new org.xwt.mips.Interpreter(image);
30 int baseAddr = vm.sbrk(FONT_RESERVED);
31 vm.copyout(fontstream, baseAddr, fontstream.length);
32 vm.setUserInfo(0, baseAddr);
33 vm.setUserInfo(1, fontstream.length);
34 vm.start(new String[]{ "freetype.mips" });
36 } catch (Exception e) {
41 public synchronized void renderGlyph(Font.Glyph glyph) throws IOException {
45 File cacheFile = null;
47 String key = glyph.font.res.getCacheKey() + ":" + glyph.c;
48 key = new String(Base64.encode(key.getBytes()));
49 cacheFile = new java.io.File(System.getProperty("user.home") +
50 java.io.File.separatorChar + ".xwt" +
51 java.io.File.separatorChar + "caches" +
52 java.io.File.separatorChar + "glyphs" +
53 java.io.File.separatorChar +
55 new java.io.File(cacheFile.getParent()).mkdirs();
56 } catch (Res.NotCacheableException e) {
57 Log.log(Freetype.class, "note: glyph not cacheable");
60 if (cacheFile != null && cacheFile.exists()) {
61 DataInputStream dis = new DataInputStream(new FileInputStream(cacheFile));
62 width = dis.readInt();
63 height = dis.readInt();
64 glyph.font.max_ascent = dis.readInt();
65 glyph.font.max_descent = dis.readInt();
66 glyph.baseline = dis.readInt();
67 glyph.advance = dis.readInt();
68 data = new byte[width * height];
69 if (width != 0 && height != 0) dis.readFully(data);
72 System.out.println("cache miss!");
73 if (loadedStream != glyph.font.res) loadFontByteStream(glyph.font.res);
74 vm.setUserInfo(2, (int)glyph.c);
75 vm.setUserInfo(3, (int)glyph.c);
76 vm.setUserInfo(4, glyph.font.pointsize);
77 long start = System.currentTimeMillis();
79 glyph.font.max_ascent = vm.getUserInfo(8);
80 glyph.font.max_descent = vm.getUserInfo(9);
81 glyph.baseline = vm.getUserInfo(10);
82 glyph.advance = vm.getUserInfo(11);
84 width = vm.getUserInfo(6);
85 height = vm.getUserInfo(7);
87 data = new byte[width * height];
88 int addr = vm.getUserInfo(5);
89 vm.copyin(addr,data,width*height);
90 File tmp = new File(cacheFile.getCanonicalPath() + ".tmp");
91 DataOutputStream dis = new DataOutputStream(new FileOutputStream(tmp));
94 dis.writeInt(glyph.font.max_ascent);
95 dis.writeInt(glyph.font.max_descent);
96 dis.writeInt(glyph.baseline);
97 dis.writeInt(glyph.advance);
98 if (width != 0 && height != 0) dis.write(data, 0, data.length);
100 tmp.renameTo(cacheFile);
102 } catch (Exception e) {
106 if (width == 0 || height == 0) Log.log(Freetype.class, "warning glyph has zero width/height");
107 glyph.p = Platform.createAlphaOnlyPicture(data, width, height);