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

FontMetrics getStringBounds unicode height too short problem

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 6
    • 1.4.2
    • client-libs
    • 2d
    • beta
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      1.4.2

      A DESCRIPTION OF THE PROBLEM :
      When I do a print out of the getStringBounds() Height I get a wrong value (too short). What is wrong ? If I try to get value of a normal string (nonunicode) height is fine but with unicode values height is too small.
      However when I do fm.getHeight() I get a good height result.
      The problem also occurs with Hebrew unicode and even if I change the Font still the main difference occurs.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See test case

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Using the unicode :
      DEBUG currH 14 15

      Using normal "hello" :
      DEBUG currH 14 15
      ACTUAL -
      Using the unicode :
      DEBUG currH 3 15 << TOO SMALL

      Using normal "hello" :
      DEBUG currH 14 15

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.font.FontRenderContext;
      import java.awt.font.TextLayout;
      import java.awt.geom.Rectangle2D;

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;

      // FontMetrics getStringBounds Unicode Height Problem

      public class ProgressTest extends JPanel {
          
          String titleString = "\u062e\u0644\u0639";
      // String titleString = "hello";

          public ProgressTest() {
              super(new BorderLayout());

              JLabel lbl = new JLabel(){
                      public void paint(Graphics g) {
                           Graphics2D g2d = (Graphics2D)g;

                           g2d.setFont(new Font("Lucida Sans Regular", Font.PLAIN, 12));

                           FontMetrics fm = g2d.getFontMetrics();
                           Rectangle2D bounds = fm.getStringBounds(titleString, g2d);
                           int currH = (int) Math.ceil(bounds.getHeight());
                           
                           System.out.println("DEBUG currH" + " " + currH + " " + fm.getHeight());

                           super.paint(g);
                      }
                  };

              lbl.setText(titleString);
              add(lbl, BorderLayout.PAGE_START);
          }

          public static void createAndShowGUI() {
              //Create and set up the window.
              JFrame frame = new JFrame("Unicode error");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              //Create and set up the content pane.
              JComponent newContentPane = new ProgressTest();
              newContentPane.setOpaque(true); //content panes must be opaque
              frame.setContentPane(newContentPane);

              //Display the window.
              frame.setSize(new Dimension(100, 500));
              //frame.pack();
              frame.setVisible(true);
          }

          public static void main(String[] args) {
              //Schedule a job for the event-dispatching thread:
              //creating and showing this application's GUI.
              javax.swing.SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      createAndShowGUI();
                  }
              });
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      None that I know of (maybe to use getHeight() instead of getStringBounds().getHeight())
      ###@###.### 10/22/04 20:28 GMT

            dougfelt Doug Felt (Inactive)
            prr Philip Race
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: