1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
5 package org.ibex.graphics;
6 import org.ibex.util.*;
8 import java.util.Hashtable;
10 import org.ibex.core.*;
11 import org.ibex.nestedvm.*;
12 import org.ibex.plat.*;
13 import org.ibex.nestedvm.Runtime;
15 // FEATURE: this could be cleaner
16 /** encapsulates a single font (a set of Glyphs) */
19 private Font(JS stream, int pointsize) { this.stream = stream; this.pointsize = pointsize; }
21 private static boolean glyphRenderingTaskIsScheduled = false;
23 public final int pointsize; ///< the size of the font
24 public final JS stream; ///< the stream from which this font was loaded
25 public int max_ascent; ///< the maximum ascent, in pixels
26 public int max_descent; ///< the maximum descent, in pixels
27 boolean latinCharsPreloaded = false; ///< true if a request to preload ASCII 32-127 has begun
28 public Glyph[] glyphs = new Glyph[65535]; ///< the glyphs that comprise this font
30 public abstract static class Glyph {
31 protected Glyph(Font font, char c) { this.font = font; this.c = c; }
32 public final Font font;
34 public int baseline; ///< within the alphamask, this is the y-coordinate of the baseline
35 public int advance; ///< amount to increment the x-coordinate
36 public boolean isLoaded = false; ///< true iff the glyph is loaded
37 public int width = -1; ///< the width of the glyph
38 public int height = -1; ///< the height of the glyph
39 public byte[] data = null; ///< the alpha channel samples for this font
40 public String path = null; // FIXME this should be shared across point sizes
42 if (!isLoaded) try { renderGlyph(this); } catch (IOException e) { Log.info(Font.class, e); }
48 // Statics //////////////////////////////////////////////////////////////////////
50 static final Hashtable glyphsToBeCached = new Hashtable();
51 static final Hashtable glyphsToBeDisplayed = new Hashtable();
52 private static Cache fontCache = new Cache(100);
53 public static Font getFont(JS stream, int pointsize) {
54 Font ret = (Font)fontCache.get(stream, new Integer(pointsize));
55 if (ret == null) fontCache.put(stream, new Integer(pointsize), ret = new Font(stream, pointsize));
60 // Methods //////////////////////////////////////////////////////////////////////
63 * Rasterize the glyphs of <code>text</code>.
64 * @returns <code>(width<<32)|height</code>
66 public long rasterizeGlyphs(String text, PixelBuffer pb, int textcolor, int x, int y, int cx1, int cy1, int cx2, int cy2) {
67 int width = 0, height = 0;
68 if (!latinCharsPreloaded) { // preload the Latin-1 charset with low priority (we'll probably want it)
69 for(int i=48; i<57; i++) if (glyphs[i]==null) glyphsToBeCached.put(glyphs[i]=Platform.createGlyph(this, (char)i),"");
70 for(int i=32; i<47; i++) if (glyphs[i]==null) glyphsToBeCached.put(glyphs[i]=Platform.createGlyph(this, (char)i),"");
71 for(int i=57; i<128; i++) if (glyphs[i]==null) glyphsToBeCached.put(glyphs[i]=Platform.createGlyph(this, (char)i),"");
72 if (!glyphRenderingTaskIsScheduled) {
73 Scheduler.add(glyphRenderingTask);
74 glyphRenderingTaskIsScheduled = true;
76 latinCharsPreloaded = true;
78 for(int i=0; i<text.length(); i++) {
79 final char c = text.charAt(i);
81 if (g == null) glyphs[c] = g = Platform.createGlyph(this, c);
83 if (pb != null) pb.drawGlyph(g, x + width, y + g.font.max_ascent - g.baseline, cx1, cy1, cx2, cy2, textcolor);
85 height = java.lang.Math.max(height, max_ascent + max_descent);
87 return ((((long)width) << 32) | (long)(height & 0xffffffffL));
90 // FEATURE do we really need to be caching sizes?
91 private static Cache sizeCache = new Cache(1000);
92 public int textwidth(String s) { return (int)((textsize(s) >>> 32) & 0xffffffff); }
93 public int textheight(String s) { return (int)(textsize(s) & 0xffffffffL); }
94 public long textsize(String s) {
95 Long l = (Long)sizeCache.get(s);
96 if (l != null) return ((Long)l).longValue();
97 long ret = rasterizeGlyphs(s, null, 0, 0, 0, 0, 0, 0, 0);
98 sizeCache.put(s, new Long(ret));
102 static final Task glyphRenderingTask = new Task() { public void perform() {
103 // FIXME: this should be a low-priority task
104 glyphRenderingTaskIsScheduled = false;
105 if (glyphsToBeCached.isEmpty()) return;
106 Glyph g = (Glyph)glyphsToBeCached.keys().nextElement();
107 if (g == null) return;
108 glyphsToBeCached.remove(g);
109 Log.debug(Font.class, "glyphRenderingTask rendering " + g.c + " of " + g.font);
111 Log.debug(Glyph.class, " done rendering glyph " + g.c);
112 glyphRenderingTaskIsScheduled = true;
117 // FreeType Interface //////////////////////////////////////////////////////////////////////////////
119 // FEATURE: use streams, not memoryfont's
120 // FEATURE: kerning pairs
121 private static final Runtime rt;
122 private static JS loadedStream;
123 private static int loadedStreamAddr;
127 rt = (Runtime)Class.forName("org.ibex.util.MIPSApps").newInstance();
128 } catch(Exception e) {
129 throw new Error("Error instantiating freetype: " + e);
131 rt.start(new String[]{"freetype"});
136 private static void rtCheck() { if(rt.getState() != Runtime.PAUSED) throw new Error("Freetype exited " + rt.exitStatus()); }
138 private static void loadFontByteStream(JS res) {
139 if(loadedStream == res) return;
142 Log.info(Font.class, "loading font " + JS.debugToString(res));
143 InputStream is = res.getInputStream();
144 byte[] fontstream = InputStreamToByteArray.convert(is);
145 rt.free(loadedStreamAddr);
146 loadedStreamAddr = rt.xmalloc(fontstream.length);
147 rt.copyout(fontstream, loadedStreamAddr, fontstream.length);
148 if(rt.call("load_font", loadedStreamAddr, fontstream.length) != 0)
149 throw new RuntimeException("load_font failed"); // FEATURE: better error
152 } catch (Exception e) {
153 // FEATURE: Better error reporting (thow an exception?)
154 Log.info(Font.class, e);
158 private static synchronized void renderGlyph(Font.Glyph glyph) throws IOException {
160 Log.debug(Font.class, "rasterizing glyph " + glyph.c + " of font " + glyph.font);
161 if (loadedStream != glyph.font.stream) loadFontByteStream(glyph.font.stream);
163 long start = System.currentTimeMillis();
165 rt.call("render",glyph.c,glyph.font.pointsize);
168 glyph.font.max_ascent = rt.getUserInfo(3);
169 glyph.font.max_descent = rt.getUserInfo(4);
170 glyph.baseline = rt.getUserInfo(5);
171 glyph.advance = rt.getUserInfo(6);
173 glyph.width = rt.getUserInfo(1);
174 glyph.height = rt.getUserInfo(2);
176 glyph.data = new byte[glyph.width * glyph.height];
177 int addr = rt.getUserInfo(0);
178 rt.copyin(addr, glyph.data, glyph.width * glyph.height);
180 byte[] path = new byte[rt.getUserInfo(8)];
181 rt.copyin(rt.getUserInfo(7), path, rt.getUserInfo(8));
182 glyph.path = new String(path);
183 glyph.path += " m " + (64 * glyph.advance) + " 0 ";
185 glyph.isLoaded = true;
186 } catch (Exception e) {
187 // FEATURE: Better error reporting (thow an exception?)
188 Log.info(Font.class, e);