Java 字体渲染:对于子像素 AA 真的必须关闭常规 AA 吗?
我一直在尝试增强用 Java 编写的 GUI 系统以使用子像素抗锯齿功能,并且已经成功,除了一个剩余的异常情况。这是我昨天提出的其他问题的后续问题。
剩下的问题是,将渲染提示 KEY_ANTIALIASING 设置为 VALUE_ANTIALIAS_ON 会导致 KEY_TEXT_ANTIALIASING 在设置为 LCD(子像素)AA 值时被忽略。有人能解释一下吗?目前,我被迫在渲染文本之前将其设置为 VALUE_ANTIALIAS_OFF,并在渲染文本后将其重新打开(以便其他绘画,如圆圈等,被 AA'd)。
下面的独立测试程序证明了这个问题。如果运行它,您会看到,当字体不是时,圆圈会被绘制为 AA,反之亦然。如果能够预先配置 AA 以适用于所有绘画,那就太好了。
独立测试程序
import java.awt.*;
import java.awt.event.*;
public class AwtTestFrame1c extends Panel {
AwtTestFrame1c() {
setBackground(SystemColor.control);
}
public void paint(Graphics gc) {
Graphics2D g2d = (Graphics2D)gc;
int py=0;
py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB,true );
py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB,false);
}
private int paintText(Graphics2D dgc, int py, Object val, boolean aa) {
char[] txt=("The quick brown fox jumped over the lazy dog ("+val+", General AA: "+aa+")").toCharArray();
if(aa ) { dgc.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON ); }
else { dgc.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF); }
if(val !=null) { dgc.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,val); }
dgc.setFont(font);
dgc.drawOval(5,py+5,15,15);
dgc.drawChars(txt,0,txt.length,30,py+line-5);
return (py+line);
}
static private final Font font=new Font("SansSerif",Font.PLAIN,16);
static private final int line=25;
static public void main(String[] args) {
Frame wnd=new Frame("AWT Antialiased Text Sample");
wnd.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
wnd.add(new AwtTestFrame1c());
wnd.setSize(new Dimension(1000, 300));
wnd.setVisible(true);
}
}
I have been attempting to enhance my GUI system written in Java to use subpixel antialiasing and have been successful, except for one remaining anomaly. This is a follow on to my other question from yesterday.
The remaining problem is that setting rendering hints KEY_ANTIALIASING to VALUE_ANTIALIAS_ON causes KEY_TEXT_ANTIALIASING to be ignored when it is set to an LCD (subpixel) AA value. Can anyone shed some light on this? Currently I am forced to VALUE_ANTIALIAS_OFF before rendering text and turn it back on after rendering text (so that other painting, like circles, etc, is AA'd).
This problem is proven by the self-contained test program below. As you can see if you run it, the circle is painted with AA when the font isn't, and vice versa. It would be nice to have AA preconfigured to work for all painting.
Self Contained Test Program
import java.awt.*;
import java.awt.event.*;
public class AwtTestFrame1c extends Panel {
AwtTestFrame1c() {
setBackground(SystemColor.control);
}
public void paint(Graphics gc) {
Graphics2D g2d = (Graphics2D)gc;
int py=0;
py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB,true );
py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB,false);
}
private int paintText(Graphics2D dgc, int py, Object val, boolean aa) {
char[] txt=("The quick brown fox jumped over the lazy dog ("+val+", General AA: "+aa+")").toCharArray();
if(aa ) { dgc.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON ); }
else { dgc.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF); }
if(val !=null) { dgc.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,val); }
dgc.setFont(font);
dgc.drawOval(5,py+5,15,15);
dgc.drawChars(txt,0,txt.length,30,py+line-5);
return (py+line);
}
static private final Font font=new Font("SansSerif",Font.PLAIN,16);
static private final int line=25;
static public void main(String[] args) {
Frame wnd=new Frame("AWT Antialiased Text Sample");
wnd.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
wnd.add(new AwtTestFrame1c());
wnd.setSize(new Dimension(1000, 300));
wnd.setVisible(true);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在更新 VirtualBox,所以我拍了照片。我可能只是看到主机的渲染,但我怀疑它也依赖于实现。
Ubuntu 9.10
Mac OS X 10.5
Windows 7
I was updating VirtualBox, so I took pictures. I may just be seeing the host's rendering, but I suspect it is also implementation dependent.
Ubuntu 9.10
Mac OS X 10.5
Windows 7
稍微研究一下发现了这个:bug 6263951。
貌似胸围是固定在b17的?我不太确定如何解释错误报告。
在 1.6.0_17-b04 (MacOS X 10.5) 上绝对被破坏。
A bit of poking around found this: bug 6263951.
Apparently the bustage is fixed in b17? I'm not exactly sure how to interpret the bug report.
Definitely broken here on 1.6.0_17-b04 (MacOS X 10.5).