2003/11/03 07:36:40
[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     public final int pointsize;
12     public final Res res;
13     public int max_ascent;
14     public int max_descent;
15     public Glyph[] glyphs = new Glyph[65535];
16     boolean used = false;
17
18     private Font(Res res, int pointsize) {
19         this.res = res;
20         this.pointsize = pointsize;
21     }
22
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));
27         return ret;
28     }
29
30     /**
31      *  If the glyphs of <code>text</code> are not yet loaded, spawn a
32      *  Task to load them and invoke callback.
33      *
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.
37      */
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;
42         int width = 0;
43         int height = 0;
44         for(int i=0; i<text.length(); i++) {
45             final char c = text.charAt(i);
46             Glyph g = glyphs[c];
47             if (g == null) glyphsToBeRendered.prepend(g = new Glyph(c));
48             if (g.p == null) {
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);
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         if (!used || encounteredUnrenderedGlyph) { System.out.println("foo!"); Scheduler.add(glyphRenderingTask); }
71         used = true;
72         return ((long)width << 16) | (long)height;
73     }
74
75
76     public class Glyph {
77         public char c;
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
80         public Picture p;
81         public final Font font;
82         public Glyph(char c) { this.c = c; font = Font.this; }
83     }
84
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);
92         try {
93             freetype.renderGlyph(g);
94         } catch (IOException e) {
95             Log.log(Freetype.class, e);
96         }
97         Scheduler.add(this);
98     } };
99 }