3079606ac6f097b762d8461eaeca22f3c260d375
[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
17     private int mem_allocated = 0;
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(fontstream.length);
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
46         try {
47             if (loadedStream != glyph.font.res) loadFontByteStream(glyph.font.res);
48             vm.setUserInfo(2, (int)glyph.c);
49             vm.setUserInfo(3, (int)glyph.c);
50             vm.setUserInfo(4, glyph.font.pointsize);
51             long start = System.currentTimeMillis();
52             vm.execute();
53             glyph.font.max_ascent = vm.getUserInfo(8);
54             glyph.font.max_descent = vm.getUserInfo(9);
55             glyph.baseline = vm.getUserInfo(10);
56             glyph.advance = vm.getUserInfo(11);
57             
58             width = vm.getUserInfo(6);
59             height = vm.getUserInfo(7);
60             
61             data = new byte[width * height];
62             int addr = vm.getUserInfo(5);
63             vm.copyin(addr,data,width*height);
64             
65             if (width == 0 || height == 0) Log.log(Freetype.class, "warning glyph has zero width/height");
66             glyph.p = Platform.createAlphaOnlyPicture(data, width, height);
67         } catch (Exception e) {
68             Log.log(this, e);
69         }
70     }
71 }