2003/10/31 09:50:10
[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
8 // FEATURE: use streams, not memoryfont's
9 // FEATURE: kerning pairs
10 public class Freetype {
11
12     public Freetype() { }
13
14     private static byte[] image = null;
15     private static final int FONT_RESERVED = 256*1024;
16
17     private org.xwt.mips.Interpreter vm = null;
18
19     private Res loadedStream = null;
20
21     public void loadFontByteStream(Res res) {
22         try {
23             Log.log(this, "loading font " + res.getDescriptiveName());
24             loadedStream = res;
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" });
34             vm.execute();
35         } catch (Exception e) {
36             Log.log(this, e);
37         }
38     }
39
40     public synchronized void renderGlyph(Res res, Glyph glyph) {
41         try {
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();
47             vm.execute();
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);
52             
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");
57                 
58             } else {
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);
63             }
64         } catch (Exception e) {
65             Log.log(this, e);
66         }
67     }
68 }