2003/09/20 05:03:47
[org.ibex.core.git] / src / org / xwt / translators / Font.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 Font {
10
11     Font() { }
12
13     private static org.xwt.mips.Interpreter vm = null;
14
15     public static synchronized void renderGlyphs(Res res, int pointsize, int firstGlyph, int lastGlyph, Cache glyphCache) {
16         try {
17            
18             if (vm == null) {
19
20                 InputStream bis = Platform.getBuiltinInputStream();
21                 ZipInputStream zis = new ZipInputStream(bis);
22                 for(ZipEntry ze = zis.getNextEntry(); ze != null && !ze.getName().equals("freetype.mips"); ze = zis.getNextEntry());
23                 byte[] image = Resources.isToByteArray(zis);
24                 vm = new org.xwt.mips.Interpreter(image);
25                 vm.start(new String[]{ "freetype.mips" });
26                 vm.execute();
27             }
28
29             int FONT_RESERVED = 256*1024;
30             int baseAddr = vm.sbrk(FONT_RESERVED);
31             
32             byte[] fontstream = Resources.isToByteArray(res.getInputStream());
33             vm.copyout(fontstream, baseAddr, fontstream.length);
34             vm.setUserInfo(0, baseAddr);
35             vm.setUserInfo(1, fontstream.length);
36             vm.setUserInfo(2, firstGlyph);
37             vm.setUserInfo(3, lastGlyph);
38             vm.setUserInfo(4, pointsize);
39             
40             long start = System.currentTimeMillis();
41             
42             for(int g = firstGlyph; g <= lastGlyph; g++) {
43                 vm.execute();
44                 
45                 Glyph glyph = new Glyph();
46                 glyph.max_ascent = vm.getUserInfo(8);
47                 glyph.max_descent = vm.getUserInfo(9) - glyph.max_ascent;
48                 glyph.baseline = vm.getUserInfo(10);
49                 glyph.advance = vm.getUserInfo(11);
50                 glyph.c = (char)g;
51                 
52                 gid.width = vm.getUserInfo(6);
53                 gid.height = vm.getUserInfo(7);
54                 if (gid.data == null || gid.data.length < gid.width * gid.height)
55                     gid.data = new int[gid.width * gid.height];
56                 int addr = vm.getUserInfo(5);
57
58                 for(int i=0; i<gid.width * gid.height; i += 4) {
59                     int val = vm.memRead(addr + i);
60                     for (int k = 3; k >= 0; k--) {
61                         if (i + k < gid.width * gid.height)
62                             gid.data[i + k] = (val & 0xff) << 24;
63                         val >>>= 8;
64                     }
65                 }
66                 
67                 glyph.p = Platform.createPicture(gid);
68                 glyphCache.put(res, new Integer((g << 16) | pointsize), glyph);
69             }
70         } catch (Exception e) {
71             Log.log(Font.class, e);
72         }
73     }
74
75     private static GlyphImageDecoder gid = new GlyphImageDecoder();
76     private static class GlyphImageDecoder extends ImageDecoder {
77         int[] data = null;
78         int width, height;
79         public int getWidth() { return width; }
80         public int getHeight() { return height; }
81         public int[] getData() { return data; }
82     }
83 }