1 package org.xwt.translators;
5 import java.util.zip.*;
8 // FEATURE: use streams, not memoryfont's
9 // FEATURE: kerning pairs
10 public class Freetype {
14 private static byte[] image = null;
15 private static final int FONT_RESERVED = 256*1024;
17 private org.xwt.mips.Interpreter vm = null;
19 private Res loadedStream = null;
21 public void loadFontByteStream(Res res) {
23 Log.log(this, "loading font " + res.getDescriptiveName());
25 InputStream is = res.getInputStream();
26 byte[] fontstream = InputStreamToByteArray.convert(is);
27 if (image == null) image = InputStreamToByteArray.convert(Main.builtin.getInputStream("freetype.mips"));
28 vm = new org.xwt.mips.Interpreter(image);
29 int baseAddr = vm.sbrk(FONT_RESERVED);
30 vm.copyout(fontstream, baseAddr, fontstream.length);
31 vm.setUserInfo(0, baseAddr);
32 vm.setUserInfo(1, fontstream.length);
33 vm.start(new String[]{ "freetype.mips" });
35 } catch (Exception e) {
40 public synchronized void renderGlyph(Res res, Glyph glyph) {
42 if (loadedStream != res) loadFontByteStream(res);
43 vm.setUserInfo(2, (int)glyph.c);
44 vm.setUserInfo(3, (int)glyph.c);
45 vm.setUserInfo(4, glyph.pointsize);
46 long start = System.currentTimeMillis();
48 glyph.max_ascent = vm.getUserInfo(8);
49 glyph.max_descent = vm.getUserInfo(9);
50 glyph.baseline = vm.getUserInfo(10);
51 glyph.advance = vm.getUserInfo(11);
53 int width = vm.getUserInfo(6);
54 int height = vm.getUserInfo(7);
55 if (width == 0 || height == 0) {
56 Log.log(Freetype.class, "warning glyph has zero width/height");
59 byte[] data = new byte[width * height];
60 int addr = vm.getUserInfo(5);
61 vm.copyin(addr,data,width*height);
62 glyph.p = Platform.createAlphaOnlyPicture(data, width, height);
64 } catch (Exception e) {