mass rename and rebranding from xwt to ibex - fixed to use ixt files
[org.ibex.core.git] / src / org / xwt / translators / Freetype.java
1 package org.xwt.translators;
2 import org.ibex.*;
3 import org.ibex.util.*;
4 import java.io.*;
5
6 import org.xwt.mips.Runtime;
7
8 // FEATURE: use streams, not memoryfont's
9 // FEATURE: kerning pairs
10 public class Freetype {
11
12     public Freetype() { }
13
14     private int mem_allocated = 0;
15     private Runtime vm = null;
16
17     private Stream loadedStream = null;
18
19     public void loadFontByteStream(Stream res) {
20         try {
21             Log.info(this, "loading font " + res);
22             loadedStream = res;
23             InputStream is = Stream.getInputStream(res);
24             byte[] fontstream = InputStreamToByteArray.convert(is);
25             vm = (Runtime)Class.forName("org.xwt.translators.MIPSApps").newInstance();
26             int baseAddr = vm.sbrk(fontstream.length);
27             vm.copyout(fontstream, baseAddr, fontstream.length);
28             vm.setUserInfo(0, baseAddr);
29             vm.setUserInfo(1, fontstream.length);
30             vm.start(new String[]{ "freetype" });
31             vm.execute();
32             if(vm.getState() == Runtime.DONE) throw new Error("Freetype VM exited: " + vm.exitStatus());
33         } catch (Exception e) {
34             Log.info(this, e);
35         }
36     }
37
38     public synchronized void renderGlyph(Font.Glyph glyph) throws IOException {
39         try {
40             Log.debug(this, "rasterizing glyph " + glyph.c + " of font " + glyph.font);
41             if (loadedStream != glyph.font.stream) loadFontByteStream(glyph.font.stream);
42             vm.setUserInfo(2, (int)glyph.c);
43             vm.setUserInfo(3, (int)glyph.c);
44             vm.setUserInfo(4, glyph.font.pointsize);
45             long start = System.currentTimeMillis();
46             vm.execute();
47             glyph.font.max_ascent = vm.getUserInfo(8);
48             glyph.font.max_descent = vm.getUserInfo(9);
49             glyph.baseline = vm.getUserInfo(10);
50             glyph.advance = vm.getUserInfo(11);
51             
52             glyph.width = vm.getUserInfo(6);
53             glyph.height = vm.getUserInfo(7);
54             
55             glyph.data = new byte[glyph.width * glyph.height];
56             int addr = vm.getUserInfo(5);
57             vm.copyin(addr, glyph.data, glyph.width * glyph.height);
58             glyph.isLoaded = true;
59             
60         } catch (Exception e) {
61             Log.info(this, e);
62         }
63     }
64 }