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

When printing, stringWidth returns an incorrect result for long strings

    XMLWordPrintable

Details

    • 2d
    • 1.4
    • x86
    • windows_95, windows_nt

    Description

      Name: rm29839 Date: 12/08/97


      When printing, characters are not an exact number of pixels across.
      For example, Monospaced 12 pt characters are about 7.03 pixels across.
      This renders stringWidth useless for longer strings as it reports the
      number of characters * the width of a single character for a monofont.

      Either stringWidth must be fixed to report the actual number of pixels
      across for a string, or an additional method must be added to the
      FontMetric class to return the fractional width of a character.

      The code to demonstrate the bug is:

      ... page is the printers graphics context ...
      Font f = new Font ("Monospaced", Font.PLAIN, 12);
      page.setFont (f);
      FontMetrics fm = page.getFontMetrics ();
      String s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
      page.drawString (s, 0, 100);
      // Notice the lines gradually go out-of-sync
      // from the characters in the string.
      for (int cnt = 0 ; cnt <= s.length () ; cnt++)
      {
          int x = fm.stringWidth (s.substring (0, cnt));
          page.drawLine (x, 100, x, 150);
      }
      (Review ID: 21397)
      ======================================================================

      Attachments

        Activity

          People

            prr Philip Race
            rmandelsunw Ronan Mandel (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: