/** 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()));
/** loads the image described by string str, possibly blocking for a network load */
static ImageDecoder getImage(String str, final Function callback) {
- boolean ispng = false;
if (str.indexOf(':') == -1) {
String s = str;
byte[] b = Resources.getResource(Resources.resolve(s + ".png", null));
- if (b == null) return null;
- return PNG.decode(new ByteArrayInputStream(b), str);
+ if (b != null) return PNG.decode(new ByteArrayInputStream(b), str);
+ b = Resources.getResource(Resources.resolve(s + ".jpeg", null));
+ if (b != null) return Platform.decodeJPEG(new ByteArrayInputStream(b), str);
+ return null;
} else {
Thread thread = Thread.currentThread();
mythread.setPriority(Thread.MIN_PRIORITY);
mythread.done.release();
try {
- // FIXME use mime types here, not extensions
- if (str.endsWith(".jpeg") || str.endsWith(".jpg"))
- str = "http://xmlrpc.xwt.org/jpeg2png/" + str.substring(str.indexOf("//") + 2);
-
HTTP http = new HTTP(str);
final HTTP.HTTPInputStream in = http.GET();
final int contentLength = in.getContentLength();
return ret;
}
};
-
+
if (str.endsWith(".gif")) return GIF.decode(is, str);
- else return PNG.decode(is, str);
+ else if (str.endsWith(".jpeg") || str.endsWith(".jpg")) return Platform.decodeJPEG(is, str);
+ else return PNG.decode(is, str);
} catch (IOException e) {
if (Log.on) Log.log(Box.class, "error while trying to load an image from " + str);
if (Log.on) Log.log(Box.class, "unable to load image " + s + " at " +
Context.enter().interpreterSourceFile + ":" + Context.enter().interpreterLine);
return;
- }
+ }
if (sizetoimage) syncSizeToImage();
dirty();
}
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);
}
} else {
Box newnode = (Box)value;
+
+ // check if box being moved is currently target of a redirect
+ for(Box cur = newnode.getParent(); cur != null; cur = cur.getParent())
+ if (cur.redirect == newnode) {
+ if (Log.on) Log.log(this, "attempt to move a box that is the target of a redirect at "+
+ Context.enter().interpreterSourceFile + ":" + Context.enter().interpreterLine);
+ return;
+ }
+
+ // check for recursive ancestor violation
for(Box cur = this; cur != null; cur = cur.getParent())
if (cur == newnode) {
if (Log.on) Log.log(this, "attempt to make a node a parent of its own ancestor at " +
/** 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;