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

fontMetrics stringWidth returns 0 for non printable characters

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.3.0
    • client-libs
    • 2d
    • beta
    • generic, x86
    • generic, windows_nt



      Name: boT120536 Date: 12/27/2000


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      I am resubmitting this bug. It is not a duplicate of Bug ID: 4104267. That
      bug is about the bad width of non printable characters. This bug is that
      fontWidth is returning a zero for a character that takes up space when printed.

      Another possibly related bug I have since found is that KeyEvent.CHAR_UNDEFINED
      is not drawn. It should probably be printed with a box like other non
      printable characters.

      Here is my original bug report:
       
      There are two related problems here. First, drawString draws a box that takes
      up space for non printable characters, but FontMetrics.stringWidth does
      not include this space. This makes it impossible to line up two different
      pieces of text.

      The second problem is that the box drawn is bigger than the characters in the
      font, even for a fixed width font.

      On jdk 1.8 all of this works properly, probably because a ? character is drawn
      instead of a box.


      import java.awt.Canvas;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.Frame;
      import java.awt.Font;

      class test3 extends Canvas {
      static String str ="abc\t123\nasdfasdfasdfasdf";
      test3() {
          super();
      }
      private static String fontname = "Courier New";
      private static int fontsize= 30;
      private static int fonttype = Font.PLAIN;
      public void paint(Graphics g) {
          g.drawString(str,50, 50);
          int newx = getFontMetrics(getFont()).stringWidth("\t");
          System.out.println("font = " + getFont());
          System.out.println("stringWidth = " + newx);
          System.out.println("stringWidth t = " +
                     getFontMetrics(getFont()).stringWidth("t"));
          System.out.println("string bounds = " + getFont().getStringBounds(
             "\t",((Graphics2D)g).getFontRenderContext()));
          System.out.println("string bounds = " + getFont().getStringBounds(
             "t",((Graphics2D)g).getFontRenderContext()));
      }

      public static void main (String args[]) {
          Frame fr = new Frame("test3 frame");
          test3 inst = new test3();
          inst.setFont(new Font(fontname,fonttype,fontsize));
          inst.setSize(200,200);
          fr.add(inst);
          fr.pack();
          inst.setVisible(true);
          fr.setVisible(true);
      }
      }
      (Review ID: 114220)
      ======================================================================

            dougfelt Doug Felt (Inactive)
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: