1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
3 import org.ibex.translators.*;
4 import org.ibex.util.*;
6 import java.util.Hashtable;
8 import org.ibex.js.JSExn;
10 // FEATURE: this could be cleaner
11 /** encapsulates a single font (a set of Glyphs) */
14 private Font(Stream stream, int pointsize) { this.stream = stream; this.pointsize = pointsize; }
16 private static boolean glyphRenderingTaskIsScheduled = false;
18 public final int pointsize; ///< the size of the font
19 public final Stream stream; ///< the stream from which this font was loaded
20 public int max_ascent; ///< the maximum ascent, in pixels
21 public int max_descent; ///< the maximum descent, in pixels
22 boolean latinCharsPreloaded = false; ///< true if a request to preload ASCII 32-127 has begun
23 Glyph[] glyphs = new Glyph[65535]; ///< the glyphs that comprise this font
25 public abstract static class Glyph {
26 protected Glyph(Font font, char c) { this.font = font; this.c = c; }
27 public final Font font;
29 public int baseline; ///< within the alphamask, this is the y-coordinate of the baseline
30 public int advance; ///< amount to increment the x-coordinate
31 public boolean isLoaded = false; ///< true iff the glyph is loaded
32 public int width = -1; ///< the width of the glyph
33 public int height = -1; ///< the height of the glyph
34 public byte[] data = null; ///< the alpha channel samples for this font
35 void render() { if (!isLoaded) try { freetype.renderGlyph(this); } catch (IOException e) { Log.info(Freetype.class, e); } }
39 // Statics //////////////////////////////////////////////////////////////////////
41 static final Freetype freetype = new Freetype();
42 static final Hashtable glyphsToBeCached = new Hashtable();
43 static final Hashtable glyphsToBeDisplayed = new Hashtable();
44 private static Cache fontCache = new Cache(100);
45 public static Font getFont(Stream stream, int pointsize) {
46 Font ret = (Font)fontCache.get(stream, new Integer(pointsize));
47 if (ret == null) fontCache.put(stream, new Integer(pointsize), ret = new Font(stream, pointsize));
52 // Methods //////////////////////////////////////////////////////////////////////
55 * Rasterize the glyphs of <code>text</code>.
56 * @returns <code>(width<<32)|height</code>
58 public long rasterizeGlyphs(String text, PixelBuffer pb, int textcolor, int x, int y, int cx1, int cy1, int cx2, int cy2) {
59 int width = 0, height = 0;
60 if (!latinCharsPreloaded) { // preload the Latin-1 charset with low priority (we'll probably want it)
61 for(int i=48; i<57; i++) if (glyphs[i]==null) glyphsToBeCached.put(glyphs[i]=Platform.createGlyph(this, (char)i),"");
62 for(int i=32; i<47; i++) if (glyphs[i]==null) glyphsToBeCached.put(glyphs[i]=Platform.createGlyph(this, (char)i),"");
63 for(int i=57; i<128; i++) if (glyphs[i]==null) glyphsToBeCached.put(glyphs[i]=Platform.createGlyph(this, (char)i),"");
64 if (!glyphRenderingTaskIsScheduled) {
65 Scheduler.add(glyphRenderingTask);
66 glyphRenderingTaskIsScheduled = true;
68 latinCharsPreloaded = true;
70 for(int i=0; i<text.length(); i++) {
71 final char c = text.charAt(i);
73 if (g == null) glyphs[c] = g = Platform.createGlyph(this, c);
75 if (pb != null) pb.drawGlyph(g, x + width, y + g.font.max_ascent - g.baseline, cx1, cy1, cx2, cy2, textcolor);
77 height = java.lang.Math.max(height, max_ascent + max_descent);
79 return ((((long)width) << 32) | (long)(height & 0xffffffffL));
82 // FEATURE do we really need to be caching sizes?
83 private static Cache sizeCache = new Cache(1000);
84 public int textwidth(String s) { return (int)((textsize(s) >>> 32) & 0xffffffff); }
85 public int textheight(String s) { return (int)(textsize(s) & 0xffffffffL); }
86 public long textsize(String s) {
87 Long l = (Long)sizeCache.get(s);
88 if (l != null) return ((Long)l).longValue();
89 long ret = rasterizeGlyphs(s, null, 0, 0, 0, 0, 0, 0, 0);
90 sizeCache.put(s, new Long(ret));
94 static final Scheduler.Task glyphRenderingTask = new Scheduler.Task() { public void perform() {
95 // FIXME: this should be a low-priority task
96 glyphRenderingTaskIsScheduled = false;
97 if (glyphsToBeCached.isEmpty()) return;
98 Glyph g = (Glyph)glyphsToBeCached.keys().nextElement();
99 if (g == null) return;
100 glyphsToBeCached.remove(g);
101 Log.debug(Font.class, "glyphRenderingTask rendering " + g.c + " of " + g.font);
103 Log.debug(Glyph.class, " done rendering glyph " + g.c);
104 glyphRenderingTaskIsScheduled = true;