/** If redirect is enabled, this holds the Box redirected to */
Box redirect = this;
- /** the Box's font -- you must call textupdate() after changing this */
- String font = Platform.getDefaultFont();
+ /** the Box's font, null inherits from parent -- you must call textupdate() after changing this */
+ String font = null;
+
+ /** if font == null, this might be a cached copy of the inherited ancestor font */
+ String cachedFont = null;
/** The surface for us to render on; null if none; INVARIANT: surface == getParent().surface */
Surface surface = null;
set(dmin, 1, (image == null ? 0 : image.getHeight()) + (border == null ? 0 : border[0].getHeight()) * 2);
}
+ /** returns the actual font that should be used to render this box */
+ private String font() {
+ if (font != null) return font;
+ if (font == null && cachedFont != null) return cachedFont;
+ if (getParent() != null) return cachedFont = getParent().font();
+ return cachedFont = Platform.getDefaultFont();
+ }
+
+ /** this must be called when a box's font changes */
+ void fontChanged() {
+ textupdate();
+ for(Box b = getChild(0); b != null; b = b.nextSibling())
+ if (b.font == null) {
+ b.cachedFont = font();
+ b.fontChanged();
+ }
+ }
+
/** This must be called when font or text is changed */
void textupdate() {
if (text.equals("")) {
set(textdim, 0, 0);
set(textdim, 1, 0);
} else {
- XWF xwf = XWF.getXWF(font);
+ XWF xwf = XWF.getXWF(font());
if (xwf == null) {
- set(textdim, 0, Platform.stringWidth(font, text));
- set(textdim, 1, (Platform.getMaxAscent(font) + Platform.getMaxDescent(font)));
+ set(textdim, 0, Platform.stringWidth(font(), text));
+ set(textdim, 1, (Platform.getMaxAscent(font()) + Platform.getMaxDescent(font())));
} else {
set(textdim, 0, xwf.stringWidth(text));
set(textdim, 1, (xwf.getMaxAscent() + xwf.getMaxDescent()));
if ((textcolor & 0xFF000000) == 0x00000000) return;
buf.setClip(x, y, w + x, h + y);
- XWF xwf = XWF.getXWF(font);
+ XWF xwf = XWF.getXWF(font());
if (xwf != null) {
xwf.drawString(buf, text,
pos(0) + pad(0),
pos(1) + pad(1) + xwf.getMaxAscent() - 1,
textcolor);
} else {
- buf.drawString(font, text,
+ buf.drawString(font(), text,
pos(0) + pad(0),
- pos(1) + pad(1) + Platform.getMaxAscent(font) - 1,
+ pos(1) + pad(1) + Platform.getMaxAscent(font()) - 1,
textcolor);
}
buf.setClip(0, 0, buf.getWidth(), buf.getHeight());
- int i=0; while(i<font.length() && !Character.isDigit(font.charAt(i))) i++;
+ int i=0; while(i<font().length() && !Character.isDigit(font().charAt(i))) i++;
- if (font.lastIndexOf('d') > i) {
+ if (font().lastIndexOf('d') > i) {
for(int j = pos(0) + pad(0); j < pos(0) + pad(0) + textdim(0); j += 2)
- buf.fillRect(j, pos(1) + pad(1) + (xwf == null ? Platform.getMaxAscent(font) : xwf.getMaxAscent()) + 2,
- j + 1, pos(1) + pad(1) + (xwf == null ? Platform.getMaxAscent(font) : xwf.getMaxAscent()) + 2 + 1,
+ buf.fillRect(j, pos(1) + pad(1) + (xwf == null ? Platform.getMaxAscent(font()) : xwf.getMaxAscent()) + 2,
+ j + 1, pos(1) + pad(1) + (xwf == null ? Platform.getMaxAscent(font()) : xwf.getMaxAscent()) + 2 + 1,
textcolor);
- } else if (font.lastIndexOf('u') > i) {
+ } else if (font().lastIndexOf('u') > i) {
buf.fillRect(pos(0) + pad(0),
- pos(1) + pad(1) + (xwf == null ? Platform.getMaxAscent(font) : xwf.getMaxAscent()) + 2,
+ pos(1) + pad(1) + (xwf == null ? Platform.getMaxAscent(font()) : xwf.getMaxAscent()) + 2,
pos(0) + pad(0) + textdim(0),
- pos(1) + pad(1) + (xwf == null ? Platform.getMaxAscent(font) : xwf.getMaxAscent()) + 2 + 1,
+ pos(1) + pad(1) + (xwf == null ? Platform.getMaxAscent(font()) : xwf.getMaxAscent()) + 2 + 1,
textcolor);
}
/** remove this node from its parent; INVARIANT: whenever the parent of a node is changed, remove() gets called. */
public void remove() {
+ cachedFont = null;
if (parent == null) {
if (surface != null) surface.dispose(true);
return;