1 // Copyright 2003 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);
69 if (g == null) glyphs[c] = g = Platform.createGlyph(this, c); // prepend so they are high priority
71 glyphsToBeRendered.prepend(g); // even if it's already in the queue, boost its priority
72 encounteredUnrenderedGlyph = true;
73 } else if (!encounteredUnrenderedGlyph) {
75 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);
81 if (!encounteredUnrenderedGlyph) return ((((long)width) << 32) | (long)(height & 0xffffffffL));
83 if (callback != null) Scheduler.add(new Scheduler.Task() {
84 public void perform() throws Exception {
85 // FEATURE this isn't terribly efficient... perhaps the task should go on the last glyph?
86 for(int i=0; i<text.length(); i++) {
87 Glyph g = glyphs[text.charAt(i)];
88 if (g == null || !g.isLoaded) { Scheduler.add(this); return; }
93 // preload the Latin-1 charset with low priority (we'll probably want it)
94 if (!latinCharsPreloaded) {
95 for(int i=48; i<57; i++) glyphsToBeRendered.append(glyphs[i] = Platform.createGlyph(this, (char)i));
96 for(int i=32; i<47; i++) glyphsToBeRendered.append(glyphs[i] = Platform.createGlyph(this, (char)i));
97 for(int i=57; i<128; i++) glyphsToBeRendered.append(glyphs[i] = Platform.createGlyph(this, (char)i));
98 latinCharsPreloaded = true;
100 if (!glyphRenderingTaskIsScheduled) {
101 Scheduler.add(glyphRenderingTask);
102 glyphRenderingTaskIsScheduled = true;
107 // FEATURE do we really need to be caching sizes?
108 private static Cache sizeCache = new Cache(1000);
109 public int textwidth(String s) { return (int)((textsize(s) >>> 32) & 0xffffffff); }
110 public int textheight(String s) { return (int)(textsize(s) & 0xffffffffL); }
111 public long textsize(String s) {
112 Long l = (Long)sizeCache.get(s);
113 if (l != null) return ((Long)l).longValue();
114 long ret = rasterizeGlyphs(s, null, 0, 0, 0, 0, 0, 0, 0, null);
115 if (ret != -1) sizeCache.put(s, new Long(ret));
116 return ret == -1 ? 0 : ret;
119 static final Scheduler.Task glyphRenderingTask = new Scheduler.Task() { public void perform() {
120 Glyph g = (Glyph)glyphsToBeRendered.remove(false);
121 if (g == null) { glyphRenderingTaskIsScheduled = false; return; }
122 if (g.isLoaded) { perform(); /* tailcall to the next glyph */ return; }
123 Log.info(Glyph.class, "rendering glyph " + g.c);
124 try { freetype.renderGlyph(g); } catch (IOException e) { Log.info(Freetype.class, e); }
126 Scheduler.add(this); // keep ourselves in the queue until there are no glyphs to render
127 glyphRenderingTaskIsScheduled = true;