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

TRACKING textattribute is not properly handled by LineBreakMeasurer

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 6
    • client-libs
    • None
    • 2d
    • generic
    • generic

      The new 1.6 TExtAttribute TRACKING doesn't appear to play nice with
      LineBreakMeasurer
      least on the version of Java I'm using:

      Image (tracking.png) attached.
      Test case below :

      import java.awt.font.*;
      import java.awt.*;
      import java.awt.geom.*;
      import java.text.*;
      import java.io.*;
      import java.util.*;
      import javax.swing.*;

      public class TrackingTest {
          private final static String text = "Nunc ut ane vel nis amet.";
          private final static Float fontSize = 62.0f;
          private final static int style = 0;
          private static final float desiredWidth = 284.0f;
          private static final FontRenderContext frc = new FontRenderContext(null, false, false);

          public static void main(String[] args) throws FontFormatException, IOException {
      Font font = new Font("serif", style, fontSize.intValue());
      JFrame jf = new JFrame("font tracking test");
      jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      jf.add(new FontTestPanel(font));
      jf.pack();
      jf.setSize(300, 300);
      jf.setVisible(true);
          }

          static class FontTestPanel extends JPanel {
      private final Font font;

      FontTestPanel(Font font) {
      this.font = font;
      }

      public void paintComponent(Graphics g) {
      Graphics2D g2d = (Graphics2D)g;
      Point2D.Float pt = new Point2D.Float(10, 30);

      g2d.setColor(Color.BLUE);
      g2d.drawLine((int)pt.x, 0, (int)pt.x, getHeight());
      g2d.drawLine((int)(pt.x + desiredWidth), 0, (int)(pt.x + desiredWidth), getHeight());

      renderText(g2d, null, pt);
      renderText(g2d ,0f, pt);
      renderText(g2d, -.02f, pt);
      renderText(g2d, 0.1f, pt);
      }

      private void renderText(Graphics2D g2d, Float tracking, Point2D.Float loc) {
      AttributedString attributedString = new AttributedString(text);
      Font usedFont = font;
      if (tracking != null) {
      Map m = new HashMap();
      m.put(TextAttribute.TRACKING, tracking);
      usedFont = font.deriveFont(m);
      }
      attributedString.addAttribute(TextAttribute.FONT, usedFont);
      LineBreakMeasurer lineMeasurer = new LineBreakMeasurer(attributedString.getIterator(), frc);
      TextLayout layout;
      while (null != (layout = lineMeasurer.nextLayout(desiredWidth))) {
      loc.y += layout.getAscent();
      g2d.setColor(Color.RED);
      g2d.drawLine((int)loc.x, (int)loc.y, (int)layout.getAdvance(), (int)loc.y);
      g2d.setColor(Color.BLACK);
      layout.draw(g2d, loc.x, loc.y);
      loc.y += layout.getDescent() + layout.getLeading();
      }
      loc.y += 20;
      }
          }
      }

            srl Steven Loomis
            prr Philip Race
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: