Skip to main content

Bug in rendering text under JDK 6.0

8 replies [Last post]
kirillcool
Offline
Joined: 2004-11-17
Points: 0

I'm running the following code under JDK 6.0 on Vista. The second line of text is painted all garbled. The interesting thing here is that all three lines marked with the comments must be there:

1. Desktop hints must be installed
2. Another text must be painted under the default composite
3. A custom translucent SRC_OVER composite must be installed afterwards

<br />
public class TextRenderingBug extends JPanel {<br />
	@Override<br />
	protected void paintComponent(Graphics g) {<br />
		Graphics2D graphics = (Graphics2D) g.create();</p>
<p>		// get the desktop hints and install them<br />
		Toolkit toolkit = Toolkit.getDefaultToolkit();<br />
		Map desktopHints = (Map) toolkit<br />
				.getDesktopProperty("awt.font.desktophints");<br />
		if (desktopHints != null) {<br />
			// if this line is removed, everything is OK<br />
			graphics.addRenderingHints(desktopHints);<br />
		}</p>
<p>		graphics.setColor(Color.black);</p>
<p>		// if this line is removed, everything is OK<br />
		graphics.drawString("test", 10, 50);</p>
<p>		// if this line is removed, everything is OK<br />
		graphics.setComposite(AlphaComposite.getInstance(<br />
				AlphaComposite.SRC_OVER, 0.6f));<br />
		// this line will be garbled.<br />
		graphics.drawString("test", 10, 80);</p>
<p>		graphics.dispose();<br />
	}</p>
<p>	public static void main(String[] args) throws Exception {<br />
		SwingUtilities.invokeLater(new Runnable() {<br />
			public void run() {<br />
				JFrame fr = new JFrame("cool");<br />
				fr.setLayout(new BorderLayout());<br />
				fr.setSize(400, 300);<br />
				fr.add(new TextRenderingBug(), BorderLayout.CENTER);<br />
				fr.setLocationRelativeTo(null);<br />
				fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br />
				fr.setVisible(true);<br />
			}<br />
		});<br />
	}<br />
}<br />

Any insight on this particular scenario?

Thanks
Kirill

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kirillcool
Offline
Joined: 2004-11-17
Points: 0

Thanks for the suggested workaround. Works good and the text is no longer garbled.

Kirill

Dmitri Trembovetski

And Phil already fixed the bug in the jdk7 workspace (6576507).
It will take some time for it to appear in a promoted jdk7
build, though.

Thanks,
Dmitri

java2d@javadesktop.org wrote:
> Thanks for the suggested workaround. Works good and the text is no longer garbled.
>
> Kirill
> [Message sent by forum member 'kirillcool' (kirillcool)]
>
> http://forums.java.net/jive/thread.jspa?messageID=225149
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

kirillcool
Offline
Joined: 2004-11-17
Points: 0

Screenshot of "garbled" text rendering is available at http://www.pushing-pixels.org/?p=46

Thanks
Kirill

tarbo
Offline
Joined: 2006-12-18
Points: 0

Okay, now that's just weird. :| Garbling happens under WinXP as well.

It seems to be related to LCD text settings. For as far as my limited testing took me, enabling ClearType will garble the second line. As soon as it is enabled/disabled (no need to reload/recompile), the second line changes.

Did some further testing: could it be that the wrong pixels are taken for AlphaComposite? I have the impression that the pixels drawn onto the screen are really just "test" but with each pixel repeated three times:

[code]
+++++++
+ x +
+xxxxx+
+ x +
+ x +
+ x +
+ xx+
+++++++

==>

+++++++
+ +
+ xxx +
+ +
+xxxxx+
+xxxxx+
+xxxxx+
+ +
+++++++
[/code]

Changing the LCD orientation (horizontal to vertical) also changes the pixel's positions, but they seem to have correct horizontal spacing, hence. In the horizontal case, if we could "just" take each third pixel, we'd get "test" back, I believe.

Phil Race

It is completely cross-platform.
Looks like a case where I forgot to ask that the text pipes be re-validated.
I think it may be as little as a one line fix for setComposite to set a
flag that some font rendering settings need to be re-validated.
I see Igor filed bug 6576507 for your test.

Its probable that there's a workaround of setting some other hint
at the same time that will tickle the re-validation.

-phil

java2d@javadesktop.org wrote:
> Okay, now that's just weird. :| Garbling happens under WinXP as well.
>
> It seems to be related to LCD text settings. For as far as my limited testing took me, enabling ClearType will garble the second line. As soon as it is enabled/disabled (no need to reload/recompile), the second line changes.
>
> Did some further testing: could it be that the wrong pixels are taking for AlphaComposite? I have the impression that the pixels drawn onto the screen are really just "test" but with each pixel repeated three times:
>
> [code]
> +++++++
> + x +
> +xxxxx+
> + x +
> + x +
> + x +
> + xx+
> +++++++
>
> ==>
>
> +++++++
> + +
> + xxx +
> + +
> +xxxxx+
> +xxxxx+
> +xxxxx+
> + +
> +++++++
> [/code]
>
> Changing the LCD orientation (horizontal to vertical) also changes the pixel's positions, but they seem to have correct horizontal spacing, hence. In the horizontal case, if we could "just" take each third pixel, we'd get "test" back, I believe.
> [Message sent by forum member 'tarbo' (tarbo)]
>
> http://forums.java.net/jive/thread.jspa?messageID=225069
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

kirillcool
Offline
Joined: 2004-11-17
Points: 0

> Its probable that there's a workaround of setting
> some other hint
> at the same time that will tickle the re-validation.

I'd be very interested in a workaround. I was thinking about rendering the second text into a temporary image. Should work (if that would be the first text rendered there), but i'm worried about the performance hit - creating an image every time i need to paint the text of a button / toggle button / radio button / checkbox / ...

Thanks
Kirill

Phil Race

> I'd be very interested in a workaround.

There's probably several things that will work, but
something as simple as this does the trick for me, and
will be fairly cheap.

// if this line is removed, everything is OK
graphics.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.6f));
Font font = graphics.getFont();
font = font.deriveFont(font.getStyle(), font.getSize2D());
graphics.setFont(font);
graphics.drawString("test", 10, 80);

it works because even though the font is the same as before,
the implementation treats it as simpler to simply set the new
font (since its not == the old one), and re-validate.
Since you need to revalidate anyway here, nothing is lost there.

-phil

java2d@javadesktop.org wrote:
>> Its probable that there's a workaround of setting
>> some other hint
>> at the same time that will tickle the re-validation.
>
> I'd be very interested in a workaround. I was thinking about rendering the second text into a temporary image. Should work (if that would be the first text rendered there), but i'm worried about the performance hit - creating an image every time i need to paint the text of a button / toggle button / radio button / checkbox / ...
>
> Thanks
> Kirill
> [Message sent by forum member 'kirillcool' (kirillcool)]
>
> http://forums.java.net/jive/thread.jspa?messageID=225089
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

kirillcool
Offline
Joined: 2004-11-17
Points: 0

> Font font = graphics.getFont();
> font = font.deriveFont(font.getStyle(),
> font.getSize2D());
> graphics.setFont(font);

Thanks. I'll try it when i get home.

Kirill