2003/11/16 09:03:16
[org.ibex.core.git] / src / org / xwt / Font.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 import java.util.*;
7 import java.io.*;
8
9 public class Font {
10
11     private Font(Res res, int pointsize) { this.res = res; this.pointsize = pointsize; }
12
13     public final int pointsize;
14     public final Res res;
15     public int max_ascent;
16     public int max_descent;
17     boolean latinCharsPreloaded = false;        ///< true if a request to preload ASCII 32-127 has begun
18     Glyph[] glyphs = new Glyph[65535];
19
20     public static class Glyph {
21         public Glyph(char c, Font f) { this.c = c; font = f; }
22         public char c;
23         public int baseline;       // within the picture, this is the y-coordinate of the baseline
24         public int advance;        // amount to increment the x-coordinate
25         public Picture p;
26         public final Font font;
27     }
28
29
30     // Statics //////////////////////////////////////////////////////////////////////
31
32     private static final Freetype freetype = new Freetype();
33     private static Cache sizeCache = new Cache(100);
34     static final Queue glyphsToBeRendered = new Queue(255);
35     private static Cache fontCache = new Cache(100);
36     public static Font getFont(Res res, int pointsize) {
37         Font ret = (Font)fontCache.get(res, new Integer(pointsize));
38         if (ret == null) fontCache.put(res, new Integer(pointsize), ret = new Font(res, pointsize));
39         return ret;
40     }
41
42
43     // Methods //////////////////////////////////////////////////////////////////////
44
45     /**
46      *  If the glyphs of <code>text</code> are not yet loaded, spawn a
47      *  Task to load them and invoke callback.
48      *
49      *  returns the width (in the high-order int) and height (in the
50      *  low-order int) of the string's rasterization, or -1 if some
51      *  glyphs are not loaded.
52      */
53     public long rasterizeGlyphs(final String text, PixelBuffer pb, int textcolor,
54                                 int x, int y, int cx1, int cy1, int cx2, int cy2,
55                                 final Scheduler.Task callback) {
56         boolean encounteredUnrenderedGlyph = false;
57         int width = 0;
58         int height = 0;
59         for(int i=0; i<text.length(); i++) {
60             final char c = text.charAt(i);
61             Glyph g = glyphs[c];
62             if (g == null) glyphsToBeRendered.prepend(g = glyphs[c] = new Glyph(c, this));    // prepend so they are high priority
63             if (g.p == null) {
64                 glyphsToBeRendered.prepend(g);
65                 encounteredUnrenderedGlyph = true;
66             } else {
67                 if (pb != null && g.p != null)
68                     pb.drawPictureAlphaOnly(g.p, x + width, y + g.font.max_ascent - g.baseline, cx1, cy1, cx2, cy2, textcolor);
69                 width += g.advance;
70                 height = java.lang.Math.max(height, max_ascent + max_descent);
71             }
72         }
73
74         if (encounteredUnrenderedGlyph && callback != null) Scheduler.add(new Scheduler.Task() { public void perform() {
75             for(int i=0; i<text.length(); i++) {
76                 Glyph g = glyphs[text.charAt(i)];
77                 if (g == null || g.p == null) { Scheduler.add(this); return; }
78             }
79             callback.perform();
80         }});
81         if (!latinCharsPreloaded) for(int i=32; i<128; i++) glyphsToBeRendered.append(glyphs[i] = new Glyph((char)i, this));
82         if (!latinCharsPreloaded || encounteredUnrenderedGlyph) Scheduler.add(glyphRenderingTask);
83         latinCharsPreloaded = true;
84         return ((((long)width) << 32) | (long)(height & 0xffffffffL));
85     }
86
87     public int textwidth(String s) { return (int)((textsize(s) >>> 32) & 0xffffffff); }
88     public int textheight(String s) { return (int)(textsize(s) & 0xffffffffL); }
89     public long textsize(String s) {
90         Long l = (Long)sizeCache.get(s);
91         if (l != null) return ((Long)l).longValue();
92         long ret = rasterizeGlyphs(s, null, 0, 0, 0, 0, 0, 0, 0, null);
93         if (ret != -1) sizeCache.put(s, new Long(ret));
94         return ret == -1 ? 0 : ret;
95     }
96
97     static final Scheduler.Task glyphRenderingTask = new Scheduler.Task() { public void perform() {
98         Glyph g = (Glyph)glyphsToBeRendered.remove(false);
99         if (g == null) return;
100         if (g.p != null) { perform(); return; }
101         Log.log(Glyph.class, "rendering glyph " + g.c);
102         try { freetype.renderGlyph(g); } catch (IOException e) { Log.log(Freetype.class, e); }
103         Scheduler.add(this);
104     } };
105 }