2003/10/29 00:53:08
[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
7 // FEATURE: use streams, not memoryfont's
8 // FEATURE: kerning pairs
9 public class Freetype {
10
11     Freetype() { }
12
13     private static org.xwt.mips.Interpreter vm = null;
14
15     public static void renderGlyphs(Res res, int pointsize, int firstGlyph, int lastGlyph, Cache glyphCache) {
16
17         try {
18             InputStream is = res.getInputStream();
19             byte[] fontstream = InputStreamToByteArray.convert(is);
20
21             synchronized(Freetype.class) {
22                 if (vm == null) {
23                     InputStream bis = Platform.getBuiltinInputStream();
24                     ZipInputStream zis = new ZipInputStream(bis);
25                     for(ZipEntry ze = zis.getNextEntry(); ze != null && !ze.getName().equals("freetype.mips"); ze = zis.getNextEntry()) { }
26                     byte[] image = InputStreamToByteArray.convert(zis);
27                     vm = new org.xwt.mips.Interpreter(image);
28                     vm.start(new String[]{ "freetype.mips" });
29                     vm.execute();
30                 }
31                 
32                 int FONT_RESERVED = 256*1024;
33                 int baseAddr = vm.sbrk(FONT_RESERVED);
34                 
35                 vm.copyout(fontstream, baseAddr, fontstream.length);
36                 vm.setUserInfo(0, baseAddr);
37                 vm.setUserInfo(1, fontstream.length);
38                 vm.setUserInfo(2, firstGlyph);
39                 vm.setUserInfo(3, lastGlyph);
40                 vm.setUserInfo(4, pointsize);
41                 
42                 long start = System.currentTimeMillis();
43                 
44                 for(int g = firstGlyph; g <= lastGlyph; g++) {
45                     vm.execute();
46                     
47                     Glyph glyph = new Glyph();
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                     glyph.c = (char)g;
53                     
54                     int width = vm.getUserInfo(6);
55                     int height = vm.getUserInfo(7);
56                     
57                     if (width == 0 || height == 0) {
58                         Log.log(Freetype.class, "warning glyph " + g + " has zero width/height");
59                         
60                     } else {
61                         byte[] data = new byte[width * height];
62                         int addr = vm.getUserInfo(5);
63                         vm.copyin(addr,data,width*height);
64                         glyph.p = Platform.createAlphaOnlyPicture(data, width, height);
65                     }
66                     
67                     glyphCache.put(res, new Integer((g << 16) | pointsize), glyph);
68                 }
69             }
70         } catch (Exception e) {
71             Log.log(Freetype.class, e);
72         }
73     }
74 }