0cb1b29c0023bf84851ab94af38b646060f9cada
[org.ibex.core.git] / src / org / xwt / Glyph.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt;
3 import org.xwt.translators.*;
4 import org.xwt.util.*;
5 import org.xwt.js.*;
6
7 public class Glyph {
8     public char c;
9     public int max_ascent;     // same value for every glyph in font; the max ascent of all chars
10     public int max_descent;    // same value for every glyph in font; the max descent of all chars
11     public int baseline;       // within the picture, this is the y-coordinate of the baseline
12     public int advance;        // amount to increment the x-coordinate
13     public Picture p;
14
15     // k1=font.res k2=(c << 16 | pointsize)
16     private static Cache glyphCache = new Cache();
17
18     public static Glyph getCachedGlyph(Res res, int pointsize, char c) {
19         return (Glyph)glyphCache.get(res, new Integer((((int)c) << 16) | pointsize));
20     }
21     public static void renderGlyph(Res res, int pointsize, char c) {
22         if (!ThreadMessage.suspendThread())
23             throw new RuntimeException("attempt to perform background-only operation in a foreground thread");
24         synchronized(res) {
25             // FEATURE: be smarter here
26             if ((Glyph)glyphCache.get(res, new Integer((((int)c) << 16) | pointsize)) != null) return;
27             Log.log(Freetype.class, "rendering glyphs for font " + res.getDescriptiveName());
28             if (c >= 32 && c < 127) Freetype.renderGlyphs(res, pointsize, 32, 126, glyphCache);
29             else Freetype.renderGlyphs(res, pointsize, (int)c, (int)c, glyphCache);
30             if ((Glyph)glyphCache.get(res, new Integer((((int)c) << 16) | pointsize)) == null)
31                 throw new JS.Exn("error rendering glyph " + c + "; glyph is null");
32         }
33         ThreadMessage.resumeThread();
34     }
35 }