1
2 package org.xwt;
3 import org.xwt.translators.*;
4 import org.xwt.util.*;
5 import org.xwt.js.*;
6 import java.util.*;
7 import java.io.*;
8
9
10
11 public class Font {
12
13 private Font(Stream stream, int pointsize) { this.stream = stream; this.pointsize = pointsize; }
14
15 private static boolean glyphRenderingTaskIsScheduled = false;
16
17 public final int pointsize;
18 public final Stream stream;
19 public int max_ascent;
20 public int max_descent;
21 boolean latinCharsPreloaded = false;
22 Glyph[] glyphs = new Glyph[65535];
23
24 public abstract static class Glyph {
25 protected Glyph(Font font, char c) { this.font = font; this.c = c; }
26 public final Font font;
27 public final char c;
28 public int baseline;
29 public int advance;
30 public boolean isLoaded = false;
31 public int width = -1;
32 public int height = -1;
33 public byte[] data = null;
34 }
35
36
37
38
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 stream, int pointsize) {
43 Font ret = (Font)fontCache.get(stream, new Integer(pointsize));
44 if (ret == null) fontCache.put(stream, new Integer(pointsize), ret = new Font(stream, pointsize));
45 return ret;
46 }
47
48
49
50
51
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 intintdth = 0, height = 0;
66 for(int i=0; i<text.length(); i++) {
67 final char c = text.charAt(i);
68 Glyph g = glyphs[c];
69 if (g == null) {
70 g = Platform.createGlyph(this, c);
71 glyphs[c] = g;
72 }
73 if (!g.isLoaded) {
74 glyphsToBeRendered.prepend(g);
75 encounteredUnrenderedGlyph = true;
76 } else if (!encounteredUnrenderedGlyph) {
77 if (pb != null) pb.drawGlyph(g, x + width, y + g.font.max_ascent - g.baseline, cx1, cy1, cx2, cy2, textcolor);
78 width += g.advance;
79 height = java.lang.Math.max(height, max_ascent + max_descent);
80 }
81 }
82
83 if (!encounteredUnrenderedGlyph) return ((((long)width) << 32) | (long)(height & 0xffffffffL));
84
85 if (callback != null) Scheduler.add(new Scheduler.Task() {
86 public void perform() throws Exception {
87
88 for(int i=0; i<text.length(); i++) {
89 Glyph g = glyphs[text.charAt(i)];
90 if (g == null || !g.isLoaded) { Scheduler.add(this); return; }
91 }
92 callback.perform();
93 }});
94
95
96 if (!latinCharsPreloaded) {
97 for(int i=48; i<57; i++) glyphsToBeRendered.append(glyphs[i] = Platform.createGlyph(this, (char)i));
98 for(int i=32; i<47; i++) glyphsToBeRendered.append(glyphs[i] = Platform.createGlyph(this, (char)i));
99 for(int i=57; i<128; i++) glyphsToBeRendered.append(glyphs[i] = Platform.createGlyph(this, (char)i));
100 latinCharsPreloaded = true;
101 }
102 if (!glyphRenderingTaskIsScheduled) {
103 Scheduler.add(glyphRenderingTask);
104 glyphRenderingTaskIsScheduled = true;
105 }
106 return -1;
107 }
108
109
110 private static Cache sizeCache = new Cache(1000);
111 public int textwidth(String s) { return (int)((textsize(s) >>> 32) & 0xffffffff); }
112 public int textheight(String s) { return (int)(textsize(s) & 0xffffffffL); }
113 public long textsize(String s) {
114 Long l = (Long)sizeCache.get(s);
115 if (l != null) return ((Long)l).longValue();
116 long ret = rasterizeGlyphs(s, null, 0, 0, 0, 0, 0, 0, 0, null);
117 if (ret != -1) sizeCache.put(s, new Long(ret));
118 return ret == -1 ? 0 : ret;
119 }
120
121 static final Scheduler.Task glyphRenderingTask = new Scheduler.Task() { public void perform() {
122 Glyph g = (Glyph)glyphsToBeRendered.remove(false);
123 if (g == null) { glyphRenderingTaskIsScheduled = false; return; }
124 if (g.isLoaded) { perform(); return; }
125 Log.debug(Glyph.class, "rendering glyph " + g.c);
126 try { freetype.renderGlyph(g); } catch (IOException e) { Log.info(Freetype.class, e); }
127 Scheduler.add(this);
128 glyphRenderingTaskIsScheduled = true;
129 } };
130 }
131