Created
August 11, 2009 11:52
-
-
Save fdb/165768 to your computer and use it in GitHub Desktop.
Demonstrates a text rendering bug on the OS X Java implementation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import javax.swing.event.DocumentEvent; | |
import javax.swing.event.DocumentListener; | |
import java.awt.*; | |
import java.awt.font.FontRenderContext; | |
import java.awt.font.LineBreakMeasurer; | |
import java.awt.font.TextAttribute; | |
import java.awt.font.TextLayout; | |
import java.awt.geom.AffineTransform; | |
import java.text.AttributedString; | |
public class TextRenderTest extends JFrame { | |
private String text = "3sa"; | |
private JTextField field; | |
public TextRenderTest() throws HeadlessException { | |
super("Text Render Test"); | |
JPanel p = new JPanel(new BorderLayout()); | |
field = new JTextField(text); | |
field.getDocument().addDocumentListener(new DocumentListener() { | |
public void insertUpdate(DocumentEvent e) { | |
fieldChanged(); | |
} | |
public void removeUpdate(DocumentEvent e) { | |
fieldChanged(); | |
} | |
public void changedUpdate(DocumentEvent e) { | |
fieldChanged(); | |
} | |
}); | |
p.add(field, BorderLayout.NORTH); | |
p.add(new TextView(), BorderLayout.CENTER); | |
p.setSize(400, 400); | |
setContentPane(p); | |
setLocationByPlatform(true); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setSize(800, 800); | |
setVisible(true); | |
} | |
public static void main(String[] args) { | |
new TextRenderTest(); | |
} | |
public void fieldChanged() { | |
text = field.getText(); | |
repaint(); | |
} | |
public class TextView extends JComponent { | |
public TextView() { | |
} | |
@Override | |
protected void paintComponent(Graphics g) { | |
Graphics2D g2 = (Graphics2D) g; | |
g2.setColor(Color.WHITE); | |
g2.fillRect(0, 0, TextView.this.getWidth(), TextView.this.getHeight()); | |
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | |
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); | |
if (text.length() == 0) { | |
text = " "; | |
} | |
AttributedString attrString = new AttributedString(text); | |
Font font = new Font("Verdana", Font.PLAIN, 400); | |
font = font.deriveFont(font.getStyle(), font.getSize2D()); | |
attrString.addAttribute(TextAttribute.FONT, font); | |
attrString.addAttribute(TextAttribute.FOREGROUND, Color.BLACK); | |
LineBreakMeasurer measurer = new LineBreakMeasurer(attrString.getIterator(), new FontRenderContext(null, false, false)); | |
TextLayout layout = measurer.nextLayout(10000); | |
AffineTransform trans = new AffineTransform(); | |
trans.translate(10, 350); | |
Shape s = layout.getOutline(trans); | |
g2.setColor(Color.RED); | |
g2.draw(s); | |
g2.setColor(Color.BLACK); | |
g2.setFont(font); | |
g2.drawString(text, 10, 700); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment