2003/11/03 05:28:30
[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
8 public class Font {
9
10     public final int pointsize;
11     public final Res res;
12     public int max_ascent;
13     public int max_descent;
14     public Glyph[] glyphs = new Glyph[65535];
15     boolean used = false;
16
17     private Font(Res res, int pointsize) {
18         this.res = res;
19         this.pointsize = pointsize;
20     }
21
22     private static Cache fontCache = new Cache();
23     public static Font getFont(Res res, int pointsize) {
24         Font ret = (Font)fontCache.get(res, new Integer(pointsize));
25         if (ret == null) fontCache.put(res, new Integer(pointsize), ret = new Font(res, pointsize));
26         return ret;
27     }
28
29     /**
30      *  If the glyphs of <code>text</code> are not yet loaded, spawn a
31      *  Task to load them and invoke callback.
32      *
33      *  returns the width (in the high-order int) and height (in the
34      *  low-order int) of the string's rasterization, or -1 if some
35      *  glyphs are not loaded.
36      */
37     public long rasterizeGlyphs(final String text, PixelBuffer pb, int textcolor,
38                                 int x, int y, int cx1, int cy1, int cx2, int cy2,
39                                 final Scheduler.Task callback) {
40         boolean encounteredUnrenderedGlyph = false;
41         int width = 0;
42         int height = 0;
43         for(int i=0; i<text.length(); i++) {
44             final char c = text.charAt(i);
45             Glyph g = glyphs[c];
46             if (g == null) glyphsToBeRendered.prepend(g = new Glyph(c));
47             if (g.p == null) {
48                 glyphsToBeRendered.prepend(g);
49                 Scheduler.add(glyphRenderingTask);
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);
55                 width += g.advance;
56                 height = java.lang.Math.max(height, max_ascent + max_descent);
57             }
58         }
59         
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; }
65             }
66             callback.perform();
67         }});
68     
69         if (!used) for(int i=32; i<128; i++) glyphsToBeRendered.append(glyphs[i] = new Glyph((char)i));
70         used = true;
71         return ((long)width << 16) | (long)height;
72     }
73
74
75     public class Glyph {
76         public char c;
77         public int baseline;       // within the picture, this is the y-coordinate of the baseline
78         public int advance;        // amount to increment the x-coordinate
79         public Picture p;
80         public final Font font;
81         public Glyph(char c) { this.c = c; font = Font.this; }
82     }
83
84     private static final Freetype freetype = new Freetype();
85     static final Queue glyphsToBeRendered = new Queue(255);
86     static final Scheduler.Task glyphRenderingTask = new Scheduler.Task() { public void perform() {
87         Glyph g = (Glyph)glyphsToBeRendered.remove(false);
88         if (g == null || g.p != null) return;
89         Log.log(Glyph.class, "rendering glyph " + g.c);
90         freetype.renderGlyph(g);
91         Scheduler.add(this);
92     } };
93 }