2003/10/28 10:10:19
[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                     Log.log(Freetype.class, "rendering glyph " + g);
46                     vm.execute();
47                     
48                     Glyph glyph = new Glyph();
49                     glyph.max_ascent = vm.getUserInfo(8);
50                     glyph.max_descent = vm.getUserInfo(9);
51                     glyph.baseline = vm.getUserInfo(10);
52                     glyph.advance = vm.getUserInfo(11);
53                     glyph.c = (char)g;
54                     
55                     int width = vm.getUserInfo(6);
56                     int height = vm.getUserInfo(7);
57                     
58                     if (width == 0 || height == 0) {
59                         Log.log(Freetype.class, "warning glyph " + g + " has zero width/height");
60                         
61                     } else {
62                         byte[] data = new byte[width * height];
63                         int addr = vm.getUserInfo(5);
64                         vm.copyin(addr,data,width*height);
65                         glyph.p = Platform.createAlphaOnlyPicture(data, width, height);
66                     }
67                     
68                     glyphCache.put(res, new Integer((g << 16) | pointsize), glyph);
69                 }
70             }
71         } catch (Exception e) {
72             Log.log(Freetype.class, e);
73         }
74     }
75 }