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

Java2D: text display and measurement inconsistencies.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.3.0
    • client-libs
    • None
    • 2d
    • beta
    • x86
    • windows_nt

      The test case below demonstrates some oddities observed when measuring
      and displaying text on Windows NT 4.0 SP4 using JDK 1.2, 1.2.2, and 1.3

      Problem (2) below was also observed on Solaris 7 on these releases.

      The test case measures the length of a String of 30 "A" characters at
      point sizes 1->29.

      The following problems can be observed.

      1) Using the "serif" font with fractional metrics off, the char width
      at a point size of 14 is less than that at a point size of 13.

      2) When fractional metrics is turned on, there ought to be a smooth
      curve as the length of the string of "A"s grows. In fact there is a
      stepping effect. This is NOT borne out by the lengths we calculate
      for the string. The calculated lengths simply don't match the displayed
      lengths.

      When Switching from serif to SansSerif, almost identical problems can be
      observed.

      To use Fractional Metrics pass in anything for a command line argument.

      import java.awt.*;

      public class TextLen extends Component {
      Object fm = RenderingHints.VALUE_FRACTIONALMETRICS_OFF;

      public TextLen(boolean fmon) {
      if (fmon) {
      fm = RenderingHints.VALUE_FRACTIONALMETRICS_ON;
      }
      }

      public void paint(Graphics g) {
                  String astr = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
      Graphics2D g2d = (Graphics2D)g;
      g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fm);
      g.drawString("Java Version is " + System.getProperty("java.version"), 20, 20);
                  g.drawString("Fractional Metrics is "+
                                g2d.getRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS), 20, 40);
      int ypos=0;
      for (int sz=1; sz<30;sz++) {
      Font f = new Font("serif", Font.PLAIN, sz);

      g.setFont(f);
      FontMetrics fm = g.getFontMetrics();
      int len = fm.stringWidth(astr);
      int cw = fm.charWidth('A');
      String msg = "font size="+sz + " strlen="+len+ " charwidth of 'A'="+cw;
      System.out.println(msg);
      ypos+=fm.getHeight();
      g.drawString(msg, 20, 60+ypos);
      g.drawString(astr,520, 60+ypos);

      }

      }

      public Dimension getPreferredSize() {
      return new Dimension(1200,800);
      }

      public static void main(String[] args) {
      boolean fm = false;
      if (args.length > 0) {
      fm = true;
      }
      TextLen test = new TextLen (fm);
      Frame f = new Frame();
      f.add("Center", test);
      f.pack();
      f.show();
      }
      }

            pkejriwasunw Parry Kejriwal (Inactive)
            prr Philip Race
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: