diff options
-rw-r--r-- | gnu/javax/swing/text/html/css/CSSParser.java | 2 | ||||
-rw-r--r-- | gnu/javax/swing/text/html/css/FontSize.java | 114 | ||||
-rw-r--r-- | javax/swing/text/html/CSS.java | 2 | ||||
-rw-r--r-- | javax/swing/text/html/StyleSheet.java | 47 |
4 files changed, 154 insertions, 11 deletions
diff --git a/gnu/javax/swing/text/html/css/CSSParser.java b/gnu/javax/swing/text/html/css/CSSParser.java index 753782378..5647e2506 100644 --- a/gnu/javax/swing/text/html/css/CSSParser.java +++ b/gnu/javax/swing/text/html/css/CSSParser.java @@ -269,7 +269,7 @@ public class CSSParser StringBuilder value = new StringBuilder(); if (parseValue(value)) { - callback.declaration(property, value.toString()); + callback.declaration(property, value.toString().trim()); } else { diff --git a/gnu/javax/swing/text/html/css/FontSize.java b/gnu/javax/swing/text/html/css/FontSize.java index 9b7c2b667..7dc8d46c6 100644 --- a/gnu/javax/swing/text/html/css/FontSize.java +++ b/gnu/javax/swing/text/html/css/FontSize.java @@ -52,6 +52,24 @@ public class FontSize private String value; /** + * The actual font size. + */ + private int size; + + /** + * The index of one of the standard sizes that this font size maps to. + * This is -1 if this fontsize doesn't map to one of the standard sizes. + * + * @see #SCALE + */ + private int sizeIndex; + + /** + * True when this font size is relative. + */ + private boolean isRelative; + + /** * The default size for 'medium' absolute size. The other absolute sizes * are calculated from this. */ @@ -70,6 +88,27 @@ public class FontSize public FontSize(String val) { value = val; + sizeIndex = -1; + isRelative = false; + size = mapValue(); + } + + /** + * Returns the font size value. + * + * @return the font size value + */ + public int getValue(int p) + { + if (isRelative) + mapRelative(p); + return size; + } + + public int getValue() + { + assert ! isRelative; + return size; } /** @@ -77,17 +116,21 @@ public class FontSize * * @return the converted real value in point */ - public int getValue() + private int mapValue() { int intVal; if (value.contains("pt")) intVal = mapPoints(); else if (value.contains("px")) intVal = mapPixels(); + else if (value.contains("em") || value.contains("%") + || value.contains("larger") || value.contains("smaller")) + { + intVal = -1; + isRelative = true; + } else intVal = mapAbsolute(); - // FIXME: Allow relative font values, ('larger' and 'smaller'). This - // requires knowledge about the parent element's font size. return intVal; } @@ -126,6 +169,52 @@ public class FontSize } } + private int mapPercent(int par) + { + int end = value.indexOf("%"); + if (end == -1) + end = value.length(); + String number = value.substring(0, end); + try + { + int intVal = Integer.parseInt(number); + return intVal * par / 100; + } + catch (NumberFormatException ex) + { + System.err.println("couldn't map value: '" + value + "'"); + return DEFAULT_FONT_SIZE; + } + } + + private int mapEM(int par) + { + int end = value.indexOf("em"); + if (end == -1) + end = value.length(); + String number = value.substring(0, end); + try + { + float factor = Float.parseFloat(number); + // FIXME: Should be relative to the parent element's size. + return (int) (factor * par); + } + catch (NumberFormatException ex) + { + return DEFAULT_FONT_SIZE; + } + } + + private int mapSmaller(int par) + { + return (int) (par * 0.9); + } + + private int mapLarger(int par) + { + return (int) (par * 0.9); + } + /** * Maps absolute font-size values. * @@ -152,6 +241,7 @@ public class FontSize // FIXME: Scale the real medium size of the document, rather than the // constant here. int intVal = (int) (scale * DEFAULT_FONT_SIZE); + sizeIndex = index; return intVal; } @@ -162,4 +252,22 @@ public class FontSize { return value; } + + private int mapRelative(int par) + { + if (value.contains("%")) + size = mapPercent(par); + else if (value.contains("em")) + size = mapEM(par); + else if (value.contains("larger")) + size = mapLarger(par); + else if (value.contains("smaller")) + size = mapSmaller(par); + return size; + } + + public boolean isRelative() + { + return isRelative; + } } diff --git a/javax/swing/text/html/CSS.java b/javax/swing/text/html/CSS.java index 2068076e2..c82b6c537 100644 --- a/javax/swing/text/html/CSS.java +++ b/javax/swing/text/html/CSS.java @@ -548,7 +548,7 @@ public class CSS implements Serializable String token = tokens.nextToken(); if (CSSColor.isValidColor(token)) atts.addAttribute(Attribute.BACKGROUND_COLOR, - getValue(Attribute.BACKGROUND_COLOR, token)); + new CSSColor(token)); } } } diff --git a/javax/swing/text/html/StyleSheet.java b/javax/swing/text/html/StyleSheet.java index 150b27426..aa2590464 100644 --- a/javax/swing/text/html/StyleSheet.java +++ b/javax/swing/text/html/StyleSheet.java @@ -484,6 +484,9 @@ public class StyleSheet extends StyleContext // Shouldn't happen. And if, then we System.err.println("IOException while parsing stylesheet: " + ex.getMessage()); } + // Clean up resolved styles cache so that the new styles are recognized + // on next stylesheet request. + resolvedStyles.clear(); } /** @@ -704,13 +707,13 @@ public class StyleSheet extends StyleContext o = htmlAttrSet.getAttribute(HTML.Attribute.WIDTH); if (o != null) cssAttr = addAttribute(cssAttr, CSS.Attribute.WIDTH, - CSS.getValue(CSS.Attribute.WIDTH, o.toString())); + new Length(o.toString())); // The HTML height attribute maps directly to CSS height. o = htmlAttrSet.getAttribute(HTML.Attribute.HEIGHT); if (o != null) cssAttr = addAttribute(cssAttr, CSS.Attribute.HEIGHT, - CSS.getValue(CSS.Attribute.HEIGHT, o.toString())); + new Length(o.toString())); o = htmlAttrSet.getAttribute(HTML.Attribute.NOWRAP); if (o != null) @@ -852,10 +855,7 @@ public class StyleSheet extends StyleContext */ public Font getFont(AttributeSet a) { - FontSize size = (FontSize) a.getAttribute(CSS.Attribute.FONT_SIZE); - int realSize = 12; - if (size != null) - realSize = size.getValue(); + int realSize = getFontSize(a); // Decrement size for subscript and superscript. Object valign = a.getAttribute(CSS.Attribute.VERTICAL_ALIGN); @@ -880,6 +880,41 @@ public class StyleSheet extends StyleContext } /** + * Resolves the fontsize for a given set of attributes. + * + * @param atts the attributes + * + * @return the resolved font size + */ + private int getFontSize(AttributeSet atts) + { + int size = 12; + if (atts.isDefined(CSS.Attribute.FONT_SIZE)) + { + FontSize fs = (FontSize) atts.getAttribute(CSS.Attribute.FONT_SIZE); + if (fs.isRelative()) + { + int parSize = 12; + AttributeSet resolver = atts.getResolveParent(); + if (resolver != null) { + parSize = getFontSize(resolver);System.err.println("parent size: " + parSize); } + size = fs.getValue(parSize); + } + else + { + size = fs.getValue(); + } + } + else + { + AttributeSet resolver = atts.getResolveParent(); + if (resolver != null) + size = getFontSize(resolver); + } + return size; + } + + /** * Takes a set of attributes and turns it into a foreground * color specification. This is used to specify things like, brigher, more hue * etc. |