Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8245159

Font.getStringBounds() throws IAE for empty string if the Font has layout attributes.

    Details

    • Subcomponent:
      2d
    • Resolved In Build:
      b08
    • CPU:
      x86
    • OS:
      os_x

      Description

      A DESCRIPTION OF THE PROBLEM :
      Normally, Font.getStringBounds() methods can be called with empty text and the result is a rectangle of font height and zero width. If the font uses kerning, ligatures, or a baseline transformation though, an IllegalArgumentException will be thrown.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run code

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expect similar output for test1 and test2.
      ACTUAL -
      test1 works as expected, test2 throws IllegalArgumentException

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import javax.swing.*;
      import java.awt.font.*;
      import java.text.AttributedCharacterIterator;

      public class FontMetricsBug {
         public static void main(String[] args) {
            SwingUtilities.invokeLater(()->go());
         }
         public static void go() {
            JFrame f =
               new JFrame() {
                  public void paint(Graphics g) {
                     super.paint(g);
                     FontRenderContext frc = ((Graphics2D) g).getFontRenderContext();
                     Font f = new Font("Monospaced", 0, 10);
                     System.out.println("test1: " + f.getStringBounds("", frc));
                     java.util.Map<AttributedCharacterIterator.Attribute, Object> attrs = new java.util.HashMap<>();
                     attrs.put(java.awt.font.TextAttribute.KERNING, java.awt.font.TextAttribute.KERNING_ON);
                     Font f2 = f.deriveFont(attrs);
                     System.out.println("test2: " + f2.getStringBounds("", frc));
                  }
               };
            f.setVisible(true);
         }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Create a work-around utility method to be used in cases where the text may be empty when calling Font.getStringBounds(), FontMetrics.getStringBounds(), and anything else that might indirectly call Font.getStringBounds().

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              prr Philip Race
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: