JDK-6478336 : Exception measuring zero length string when font has layout attributes.
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: generic,windows_xp
  • CPU: generic,x86
  • Submitted: 2006-10-04
  • Updated: 2011-03-08
  • Resolved: 2011-03-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6 JDK 7
6u2Fixed 7 b03Fixed
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Description
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)

Comments
EVALUATION It is possible, but unlikely, that existing programs will encounter this bug. The attributes that trigger 'layout' were not supported on Font in 1.5. More likely new programs which use this API will be affected. Also measuring zero-width strings isn't commmon. Still this probably should be fixed in an early update release. The FontMetrics implementation class should be updated to test for zero length before constructing the TextLayout. Also its non-optimal (but not terribly so) that attributes such as FOREGROUND and BACKGROUND which require layout for rendering but not for measurement, need to construct a TextLayout. We could possibly dig into which attributes are specified and if its only 'safe' ones skip creating the TextLayoy
04-10-2006