Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 6478336
Votes 0
Synopsis Exception measuring zero length string when font has layout attributes.
Category java:classes_2d
Reported Against
Release Fixed 7(b03), 6u2(b01) (Bug ID:2145706)
State 10-Fix Delivered, bug
Priority: 4-Low
Related Bugs 6513875 , 6561485 , 6699843
Submit Date 04-OCT-2006
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)
Posted Date : 2006-10-04 20:01:52.0
Work Around
N/A
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
Posted Date : 2006-10-04 20:01:52.0
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang