1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL]
3 import org.xwt.translators.*;
9 /** encapsulates a single font (a set of Glyphs) */
12 private Font(Stream res, int pointsize) { this.res = res; this.pointsize = pointsize; }
14 private static boolean glyphRenderingTaskIsScheduled = false;
16 public final int pointsize; ///< the size of the font
17 public final Stream res; ///< the resource from which this font was loaded
18 public int max_ascent; ///< the maximum ascent, in pixels
19 public int max_descent; ///< the maximum descent, in pixels
20 boolean latinCharsPreloaded = false; ///< true if a request to preload ASCII 32-127 has begun
21 Glyph[] glyphs = new Glyph[65535]; ///< the glyphs that comprise this font
23 public abstract static class Glyph {
24 protected Glyph(Font font, char c) { this.font = font; this.c = c; }
25 public final Font font;
27 public int baseline; ///< within the alphamask, this is the y-coordinate of the baseline
28 public int advance; ///< amount to increment the x-coordinate
29 public boolean isLoaded = false; ///< true iff the glyph is loaded
30 public byte[] alphaChannel = null;
31 public int width = -1; ///< the width of the glyph
32 public int height = -1; ///< the height of the glyph
33 public byte[] data = null;
37 // Statics //////////////////////////////////////////////////////////////////////
39 private static final Freetype freetype = new Freetype();
40 static final Queue glyphsToBeRendered = new Queue(255);
41 private static Cache fontCache = new Cache(100);
42 public static Font getFont(Stream res, int pointsize) {
43 Font ret = (Font)fontCache.get(res, new Integer(pointsize));
44 if (ret == null) fontCache.put(res, new Integer(pointsize), ret = new Font(res, pointsize));
49 // Methods //////////////////////////////////////////////////////////////////////
52 * Rasterize the glyphs of <code>text</code>.
54 * If all the glyphs of <code>text</code> are not yet loaded,
55 * spawn a Task to load them and then invoke callback. If all
56 * the glyphs <i>are</i> loaded, rasterize them to the
57 * PixelBuffer (if non-null).
59 * @returns <code>(width<<32)|height</code> if all glyphs are loaded; else -1
61 public long rasterizeGlyphs(final String text, PixelBuffer pb, int textcolor,
62 int x, int y, int cx1, int cy1, int cx2, int cy2,
63 final Scheduler.Task callback) {
64 boolean encounteredUnrenderedGlyph = false;
65 int width = 0, height = 0;
66 for(int i=0; i<text.length(); i++) {
67 final char c = text.charAt(i);
70 g = Platform.createGlyph(this, c); // prepend so they are high priority
74 glyphsToBeRendered.prepend(g); // even if it's already in the queue, boost its priority
75 encounteredUnrenderedGlyph = true;
76 } else if (!encounteredUnrenderedGlyph) {
78 pb.drawGlyph(g, x + width, y + g.font.max_ascent - g.baseline, cx1, cy1, cx2, cy2, textcolor);
80 height = java.lang.Math.max(height, max_ascent + max_descent);
84 if (!encounteredUnrenderedGlyph) return ((((long)width) << 32) | (long)(height & 0xffffffffL));
86 if (callback != null) Scheduler.add(new Scheduler.Task() {
87 public void perform() throws Exception {
88 // FEATURE this isn't terribly efficient... perhaps the task should go on the last glyph?
89 for(int i=0; i<text.length(); i++) {
90 Glyph g = glyphs[text.charAt(i)];
91 if (g == null || !g.isLoaded) { Scheduler.add(this); return; }
96 // preload the Latin-1 charset with low priority (we'll probably want it)
97 if (!latinCharsPreloaded) {
98 for(int i=48; i<57; i++) glyphsToBeRendered.append(glyphs[i] = Platform.createGlyph(this, (char)i));
99 for(int i=32; i<47; i++) glyphsToBeRendered.append(glyphs[i] = Platform.createGlyph(this, (char)i));
100 for(int i=57; i<128; i++) glyphsToBeRendered.append(glyphs[i] = Platform.createGlyph(this, (char)i));
101 latinCharsPreloaded = true;
103 if (!glyphRenderingTaskIsScheduled) {
104 Scheduler.add(glyphRenderingTask);
105 glyphRenderingTaskIsScheduled = true;
110 // FEATURE do we really need to be caching sizes?
111 private static Cache sizeCache = new Cache(1000);
112 public int textwidth(String s) { return (int)((textsize(s) >>> 32) & 0xffffffff); }
113 public int textheight(String s) { return (int)(textsize(s) & 0xffffffffL); }
114 public long textsize(String s) {
115 Long l = (Long)sizeCache.get(s);
116 if (l != null) return ((Long)l).longValue();
117 long ret = rasterizeGlyphs(s, null, 0, 0, 0, 0, 0, 0, 0, null);
118 if (ret != -1) sizeCache.put(s, new Long(ret));
119 return ret == -1 ? 0 : ret;
122 static final Scheduler.Task glyphRenderingTask = new Scheduler.Task() { public void perform() {
123 Glyph g = (Glyph)glyphsToBeRendered.remove(false);
124 if (g == null) { glyphRenderingTaskIsScheduled = false; return; }
125 if (g.isLoaded) { perform(); /* tailcall to the next glyph */ return; }
126 //Log.info(Glyph.class, "rendering glyph " + g.c);
127 try { freetype.renderGlyph(g); } catch (IOException e) { Log.info(Freetype.class, e); }
128 Scheduler.add(this); // keep ourselves in the queue until there are no glyphs to render
129 glyphRenderingTaskIsScheduled = true;