United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-6478336 Exception measuring zero length string when font has layout attributes.
JDK-6478336 : Exception measuring zero length string when font has layout attributes.

Details
Type:
Bug
Submit Date:
2006-10-04
Status:
Closed
Updated Date:
2011-03-08
Project Name:
JDK
Resolved Date:
2011-03-08
Component:
client-libs
OS:
generic,windows_xp
Sub-Component:
2d
CPU:
x86,generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports
Backport:
Duplicate:
Duplicate:
Relates:

Sub Tasks

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
                                     
2006-10-04



Hardware and Software, Engineered to Work Together