0726fda42009f22fdba9059b8e72ff00c423d449
[org.ibex.core.git] / src / org / xwt / translators / Freetype.java
1 package org.xwt.translators;
2 import org.xwt.*;
3 import org.xwt.util.*;
4 import java.io.*;
5 import java.util.zip.*;
6 import java.util.*;
7 import org.bouncycastle.util.encoders.Base64;
8
9 // FEATURE: use streams, not memoryfont's
10 // FEATURE: kerning pairs
11 public class Freetype {
12
13     public Freetype() { }
14
15     private static byte[] image = null;
16     private static final int FONT_RESERVED = 256*1024;
17
18     private org.xwt.mips.Interpreter vm = null;
19
20     private Res loadedStream = null;
21
22     public void loadFontByteStream(Res res) {
23         try {
24             Log.log(this, "loading font " + res);
25             loadedStream = 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" });
35             vm.execute();
36         } catch (Exception e) {
37             Log.log(this, e);
38         }
39     }
40
41     public synchronized void renderGlyph(Font.Glyph glyph) throws IOException {
42         int width = 0;
43         int height = 0;
44         byte[] data = null;
45         File cacheFile = null;
46         /*
47         try {
48             String key = glyph.font.res.getCacheKey() + ":" + glyph.c;
49             key = new String(Base64.encode(key.getBytes()));
50             cacheFile = new java.io.File(System.getProperty("user.home") +
51                                          java.io.File.separatorChar + ".xwt" +
52                                          java.io.File.separatorChar + "caches" +
53                                          java.io.File.separatorChar + "glyphs" +
54                                          java.io.File.separatorChar +
55                                          key);
56             new java.io.File(cacheFile.getParent()).mkdirs();
57         } catch (Res.NotCacheableException e) {
58             Log.log(Freetype.class, "note: glyph not cacheable");
59         }
60         */
61         if (cacheFile != null && cacheFile.exists()) {
62             DataInputStream dis = new DataInputStream(new FileInputStream(cacheFile));
63             width = dis.readInt();
64             height = dis.readInt();
65             glyph.font.max_ascent = dis.readInt();
66             glyph.font.max_descent = dis.readInt();
67             glyph.baseline = dis.readInt();
68             glyph.advance = dis.readInt();
69             data = new byte[width * height];
70             if (width != 0 && height != 0) dis.readFully(data);
71             
72         } else try {
73             //System.out.println("cache miss!");
74             if (loadedStream != glyph.font.res) loadFontByteStream(glyph.font.res);
75             vm.setUserInfo(2, (int)glyph.c);
76             vm.setUserInfo(3, (int)glyph.c);
77             vm.setUserInfo(4, glyph.font.pointsize);
78             long start = System.currentTimeMillis();
79             vm.execute();
80             glyph.font.max_ascent = vm.getUserInfo(8);
81             glyph.font.max_descent = vm.getUserInfo(9);
82             glyph.baseline = vm.getUserInfo(10);
83             glyph.advance = vm.getUserInfo(11);
84             
85             width = vm.getUserInfo(6);
86             height = vm.getUserInfo(7);
87
88             data = new byte[width * height];
89             int addr = vm.getUserInfo(5);
90             vm.copyin(addr,data,width*height);
91             if (cacheFile != null) {
92                 File tmp = new File(cacheFile.getCanonicalPath() + ".tmp");
93                 DataOutputStream dis = new DataOutputStream(new FileOutputStream(tmp));
94                 dis.writeInt(width);
95                 dis.writeInt(height);
96                 dis.writeInt(glyph.font.max_ascent);
97                 dis.writeInt(glyph.font.max_descent);
98                 dis.writeInt(glyph.baseline);
99                 dis.writeInt(glyph.advance);
100                 if (width != 0 && height != 0) dis.write(data, 0, data.length);
101                 dis.close();
102                 tmp.renameTo(cacheFile);
103             }
104
105         } catch (Exception e) {
106             Log.log(this, e);
107         }
108
109         if (width == 0 || height == 0) Log.log(Freetype.class, "warning glyph has zero width/height");
110         glyph.p = Platform.createAlphaOnlyPicture(data, width, height);
111     }
112 }