Duplicate :
|
|
Duplicate :
|
|
Relates :
|
In JDK 6, for ease of use the Font class supports all TextAttributes as font attributes. Previously there was a hardcoded subset. This makes it easy to (say) specify a foreground colour for text in that font without using AttributedString. Attributes which might require such behaviours are treated as 'layout' attributes as they need to invoke code which can properly render using all these attributes. When measuring text which requires layout, the implementation internally constructs a TextLayout. But suppose the application passes an empty string, or zero-length character array to be measured? TextLayout (as described in bug 4138921) throws an exception on being constructed with a zero-length string. There was a plan to fix this in JDK 6 but it did not happen. As a consequence measuring a zero with string of text in a font which has layout attributes throws that exception. The following simple program demonstrates this : import java.util.*; import java.awt.*; import java.awt.font.*; import java.awt.image.*; public class zws { public static void main(String args[]) { BufferedImage bi=new BufferedImage(1,1,BufferedImage.TYPE_INT_RGB); Graphics g = bi.getGraphics(); Font f = new Font("Dialog", Font.PLAIN, 12); Map map = new HashMap(); map.put(TextAttribute.BACKGROUND, Color.BLUE); f = f.deriveFont(map); g.setFont(f); FontMetrics fm = g.getFontMetrics(); fm.stringWidth(""); } } /java/re/jdk/1.6.0/latest/binaries/solaris-sparc/bin/java zws Exception in thread "main" java.lang.IllegalArgumentException: Zero length string passed to TextLayout constructor. at java.awt.font.TextLayout.<init>(TextLayout.java:364) at sun.font.FontDesignMetrics.stringWidth(FontDesignMetrics.java:463) at zws.main(zws.java:18)
|