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