JDK-6761856 : OpenJDK: vertical text metrics may be significanly different from those returned by Sun JDK
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 7
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2008-10-21
  • Updated: 2010-04-04
  • Resolved: 2009-01-09
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 7 Other
7 b43Fixed OpenJDK6Fixed
Description
Following test returns:

Exception in thread "main" java.lang.RuntimeException: Not equal:
   <java.awt.geom.Rectangle2D$Float[x=5.78125,y=-47.796875,w=633.71484,h=57.515625]>
   <java.awt.geom.Rectangle2D$Float[x=5.78125,y=0.0,w=633.71484,h=96.34375]>
        at Test.testLineMeasurerBounds(Test.java:36)
        at Test.main(Test.java:41)


While some difference between OpenJDK and Sun JDK is expected due to different font rasterizers used
it should not be that large.

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.text.*;
import java.io.File;

public class OpenJDKFontTest {

  // Copy this ttf file from the Sun JDK
  public static final String PATH_TO_FONT = "./LucidaSansRegular.ttf";
  public static final String TEST_CONTENT = "Beds, carriers, bowls,
leashes, snacks, and more.";

  public static void testLineMeasurerBounds() throws Exception {
    Font font = Font.createFont(Font.TRUETYPE_FONT, new File(PATH_TO_FONT)).deriveFont(62f);
    AttributedString attributedString = new AttributedString(TEST_CONTENT);
    attributedString.addAttribute(TextAttribute.FONT, font);

    AttributedCharacterIterator paragraph = attributedString.getIterator();
    int paragraphEnd = paragraph.getEndIndex();
    FontRenderContext frc = new FontRenderContext(null, true, true);

    LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(paragraph, frc);
    lineMeasurer.setPosition(paragraph.getBeginIndex());
    float desiredWidth = 882f;

    TextLayout layout = lineMeasurer.nextLayout(desiredWidth, paragraphEnd, true);

    Rectangle2D textBox = layout.getBounds();
    Rectangle2D expected = new Rectangle2D.Float(5.78125f, -47.796875f, 633.71484f, 57.515625f);

    if (!expected.equals(textBox)) {
      throw new RuntimeException("Not equal: <" + expected + "> <" + textBox + ">");
    }
  }

  public static void main(String[] args) throws Exception {
    testLineMeasurerBounds();
  }

}

Comments
SUGGESTED FIX http://hg.openjdk.java.net/jdk7/jdk7/rev/9cdababf6179
28-10-2008

EVALUATION Freetype and java uses different orientation of Y axis. Y coordinate was not correctly converted before returning text bounds.
28-10-2008