55dbf6886ca6b80c53131b588698af8ab3532461
[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.Runtime 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             vm = new FreetypeVM();
31             int baseAddr = vm.sbrk(fontstream.length);
32             vm.copyout(fontstream, baseAddr, fontstream.length);
33             vm.setUserInfo(0, baseAddr);
34             vm.setUserInfo(1, fontstream.length);
35             vm.start(new String[]{ "freetype.mips" });
36             vm.execute();
37         } catch (Exception e) {
38             Log.log(this, e);
39         }
40     }
41
42     public synchronized void renderGlyph(Font.Glyph glyph) throws IOException {
43         try {
44             if (loadedStream != glyph.font.res) loadFontByteStream(glyph.font.res);
45             vm.setUserInfo(2, (int)glyph.c);
46             vm.setUserInfo(3, (int)glyph.c);
47             vm.setUserInfo(4, glyph.font.pointsize);
48             long start = System.currentTimeMillis();
49             vm.execute();
50             glyph.font.max_ascent = vm.getUserInfo(8);
51             glyph.font.max_descent = vm.getUserInfo(9);
52             glyph.baseline = vm.getUserInfo(10);
53             glyph.advance = vm.getUserInfo(11);
54             
55             glyph.width = vm.getUserInfo(6);
56             glyph.height = vm.getUserInfo(7);
57             
58             glyph.data = new byte[glyph.width * glyph.height];
59             int addr = vm.getUserInfo(5);
60             vm.copyin(addr, glyph.data, glyph.width * glyph.height);
61             
62         } catch (Exception e) {
63             Log.log(this, e);
64         }
65     }
66 }