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

TextLayout.draw displays incorrectly with scaled and rotated Graphics

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.3.0
    • 1.2.0, 1.2.1, 1.3.0
    • client-libs
    • 2d
    • kestrel
    • generic, x86, sparc
    • generic, solaris_2.6, windows_nt



      Name: skT88420 Date: 09/08/99


      The program below illustrates a bug in TextLayout.draw. The
      program comment describes the problem. Information is lost when
      the characters run off the edge of the display area or overlap.

      Ray Tomlinson

      package com.bbn.tomlinson;

      import java.awt.*;
      import java.awt.event.*;
      import java.awt.font.*;
      import java.awt.print.*;
      import java.awt.geom.*;
      import java.text.*;
      import java.util.*;

      /**
       * Draw a small window displaying the same text string six different
       * ways: as a TextLayout or ordinary String and with three different x
       * scale factors. The expectation is that each set of three strings
       * would appear the same although offset in y to avoid
       * overlap. Instead, the y positions of the individual characters of
       * the TextLayout are wrong.
       **/
      public class Bug2 extends Panel {

        static Map hints = new HashMap();

        static {
          hints.put(RenderingHints.KEY_ANTIALIASING,
      RenderingHints.VALUE_ANTIALIAS_ON);
          hints.put(RenderingHints.KEY_TEXT_ANTIALIASING,
      RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
          hints.put(RenderingHints.KEY_FRACTIONALMETRICS,
      RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        }
        public Dimension getPreferredSize() {
          return new Dimension(400, 400);
        }

        static String test = "This is only a test";

        static double angle = Math.PI / 6.0; // Rotate 30 degrees

        private void draw(Graphics2D g2d, TextLayout layout,
      float x, float y, float scalex)
        {
          AffineTransform saveTransform = g2d.getTransform();
          g2d.translate(x, y);
          g2d.rotate(angle);
          g2d.scale(scalex, 1f);
          layout.draw(g2d, 0f, 0f);
          g2d.setTransform(saveTransform);
        }

        private void draw(Graphics2D g2d, String string,
      float x, float y, float scalex) {
          AffineTransform saveTransform = g2d.getTransform();
          g2d.translate(x, y);
          g2d.rotate(angle);
          g2d.scale(scalex, 1f);
          g2d.drawString(string, 0f, 0f);
          g2d.setTransform(saveTransform);
        }

        public void paint(Graphics g) {
          Graphics2D g2d = (Graphics2D) g;
          g2d.scale(1.481f, 1.481); // Convert to 108 dpi
          g2d.addRenderingHints(hints);
          FontRenderContext frc = g2d.getFontRenderContext();
          TextLayout tl1 = new TextLayout(new AttributedString(test).getIterator(), frc);
          draw(g2d, tl1, 10f, 12f, 3.0f);
          draw(g2d, tl1, 10f, 24f, 1.0f);
          draw(g2d, tl1, 10f, 36f, 0.33f);
          draw(g2d, test, 10f, 84f, 3.0f);
          draw(g2d, test, 10f, 96f, 1.0f);
          draw(g2d, test, 10f, 108f, .33f);
        }

        public static void main(String[] args) {
          Frame frame = new Frame("Bug2");
          Bug2 bug2 = new Bug2();
          frame.add(bug2);
          frame.pack();
          frame.show();
          frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
      System.exit(0);
            }
          });
        }
      }
      (Review ID: 94995)
      ======================================================================

      This is actually a problem with GlyphVector (which TextLayout uses for drawing). Attached is a modified version of the test which uses GlyphVector instead of TextLayout. The output is the same.
       
      john.raley@eng 1999-09-27

            dougfelt Doug Felt
            skonchad Sandeep Konchady
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: