initial checkin
[org.ibex.nanogoat.git] / src / org / ibex / translators / Freetype.java
1 package org.ibex.translators;
2 import org.ibex.*;
3 import org.ibex.util.*;
4 import java.io.*;
5 import java.util.zip.*;
6 import java.util.*;
7 import org.bouncycastle.util.encoders.Base64;
8
9 import org.xwt.mips.Runtime;
10
11 // FEATURE: use streams, not memoryfont's
12 // FEATURE: kerning pairs
13 public class Freetype {
14
15     public Freetype() { }
16
17     private int mem_allocated = 0;
18     private Runtime vm = null;
19
20     private Stream loadedStream = null;
21
22     public void loadFontByteStream(Stream res) {
23         try {
24             Log.info(this, "loading font " + res);
25             loadedStream = res;
26             InputStream is = Stream.getInputStream(res);
27             byte[] fontstream = InputStreamToByteArray.convert(is);
28             vm = (Runtime)Class.forName("org.ibex.translators.MIPSApps").newInstance();
29             int baseAddr = vm.sbrk(fontstream.length);
30             vm.copyout(fontstream, baseAddr, fontstream.length);
31             vm.setUserInfo(0, baseAddr);
32             vm.setUserInfo(1, fontstream.length);
33             vm.start(new String[]{ "freetype" });
34             vm.execute();
35             if(vm.getState() == Runtime.DONE) throw new Error("Freetype VM exited: " + vm.exitStatus());
36         } catch (Exception e) {
37             Log.info(this, e);
38         }
39     }
40
41     public synchronized void renderGlyph(Font.Glyph glyph) throws IOException {
42         try {
43             Log.debug(this, "rasterizing glyph " + glyph.c + " of font " + glyph.font);
44             if (loadedStream != glyph.font.stream) loadFontByteStream(glyph.font.stream);
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             glyph.isLoaded = true;
62             
63         } catch (Exception e) {
64             Log.info(this, e);
65         }
66     }
67 }