1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
3 import org.xwt.translators.*;
11 public final int pointsize;
13 public int max_ascent;
14 public int max_descent;
15 public Glyph[] glyphs = new Glyph[65535];
18 private Font(Res res, int pointsize) {
20 this.pointsize = pointsize;
23 private static Cache fontCache = new Cache();
24 public static Font getFont(Res res, int pointsize) {
25 Font ret = (Font)fontCache.get(res, new Integer(pointsize));
26 if (ret == null) fontCache.put(res, new Integer(pointsize), ret = new Font(res, pointsize));
31 * If the glyphs of <code>text</code> are not yet loaded, spawn a
32 * Task to load them and invoke callback.
34 * returns the width (in the high-order int) and height (in the
35 * low-order int) of the string's rasterization, or -1 if some
36 * glyphs are not loaded.
38 public long rasterizeGlyphs(final String text, PixelBuffer pb, int textcolor,
39 int x, int y, int cx1, int cy1, int cx2, int cy2,
40 final Scheduler.Task callback) {
41 boolean encounteredUnrenderedGlyph = false;
44 for(int i=0; i<text.length(); i++) {
45 final char c = text.charAt(i);
47 if (g == null) glyphsToBeRendered.prepend(g = new Glyph(c));
49 glyphsToBeRendered.prepend(g);
50 encounteredUnrenderedGlyph = true;
51 } else if (!encounteredUnrenderedGlyph) {
52 if (pb != null && g.p != null)
53 pb.drawPictureAlphaOnly(g.p, x + width, y + g.font.max_ascent - g.baseline,
54 cx1, cy1, cx2, cy2, textcolor);
56 height = java.lang.Math.max(height, max_ascent + max_descent);
60 // FIXME: be cleaner here
61 if (encounteredUnrenderedGlyph) Scheduler.add(new Scheduler.Task() { public void perform() {
62 for(int i=0; i<text.length(); i++) {
63 Glyph g = glyphs[text.charAt(i)];
64 if (g == null || g.p == null) { Scheduler.add(this); return; }
69 if (!used) for(int i=32; i<128; i++) glyphsToBeRendered.append(glyphs[i] = new Glyph((char)i));
70 if (!used || encounteredUnrenderedGlyph) { System.out.println("foo!"); Scheduler.add(glyphRenderingTask); }
72 return ((long)width << 16) | (long)height;
78 public int baseline; // within the picture, this is the y-coordinate of the baseline
79 public int advance; // amount to increment the x-coordinate
81 public final Font font;
82 public Glyph(char c) { this.c = c; font = Font.this; }
85 private static final Freetype freetype = new Freetype();
86 static final Queue glyphsToBeRendered = new Queue(255);
87 static final Scheduler.Task glyphRenderingTask = new Scheduler.Task() { public void perform() {
88 Glyph g = (Glyph)glyphsToBeRendered.remove(false);
89 if (g == null) return;
90 if (g.p != null) { perform(); return; }
91 Log.log(Glyph.class, "rendering glyph " + g.c);
93 freetype.renderGlyph(g);
94 } catch (IOException e) {
95 Log.log(Freetype.class, e);