在Kubuntu 14.04中运行Xilinx Vivado FPGA设计工具,它在UI的某些部分使用了抗锯齿字体,这使得它甚至难以使用,更不用说难看了.其他人似乎有相同的issue.
由于它似乎至少部分用Java编写,我尝试了一些谷歌搜索后找到的建议解决方案
export _JAVA_OPTIONS="-Dawt.useSystemAAFontSettings=on -Dswing.aatext=true -Dsun.java2d.xrender=true"
没有任何视觉效果.
下面的一个小测试程序很明显,除非显式设置字体呈现提示(但不是RenderingHints.KEY_TEXT_ANTIALIASING),否则将获得非抗锯齿文本.这在OpenJDK和Oracle JDK中都会发生.
import java.awt.*;
import java.awt.event.*;
// partially taken from https://stackoverflow.com/questions/2018102/java-font-rendering
public class AwtAA extends Panel {
private final Font font = new Font(Font.SANS_SERIF,Font.BOLD,16);
private final int line = 24;
private final int medskip = 16;
AwtAA() {
setBackground(SystemColor.control);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
int py = 0;
py = paintText(g2d,py,null,null);
py += medskip;
py = paintText(g2d,RenderingHints.VALUE_ANTIALIAS_DEFAULT);
py = paintText(g2d,RenderingHints.VALUE_ANTIALIAS_ON);
py += medskip;
py = paintText(g2d,RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT,null);
py = paintText(g2d,RenderingHints.VALUE_TEXT_ANTIALIAS_ON,RenderingHints.VALUE_ANTIALIAS_DEFAULT);
py += medskip;
py = paintText(g2d,RenderingHints.VALUE_ANTIALIAS_ON);
py = paintText(g2d,RenderingHints.VALUE_ANTIALIAS_ON);
}
private int paintText(Graphics2D g2d,int py,Object text,Object aa) {
Graphics2D dgc = g2d;
Image img = null;
String text_aa = "none";
if (text != null) {
if (text.equals(RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT)) text_aa = "VALUE_TEXT_ANTIALIAS_DEFAULT";
if (text.equals(RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)) text_aa = "VALUE_TEXT_ANTIALIAS_OFF";
if (text.equals(RenderingHints.VALUE_TEXT_ANTIALIAS_ON)) text_aa = "VALUE_TEXT_ANTIALIAS_ON";
}
String value_aa = "none";
if (aa != null) {
if (aa.equals(RenderingHints.VALUE_ANTIALIAS_DEFAULT)) value_aa = "VALUE_ANTIALIAS_DEFAULT";
if (aa.equals(RenderingHints.VALUE_ANTIALIAS_OFF)) value_aa = "VALUE_ANTIALIAS_OFF";
if (aa.equals(RenderingHints.VALUE_ANTIALIAS_ON)) value_aa = "VALUE_ANTIALIAS_ON";
}
char[] txt = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ (" + text_aa + "," + value_aa + ")").tocharArray();
GraphicsConfiguration cfg = getGraphicsConfiguration();
img = cfg.createCompatibleImage(getWidth(),line);
dgc = (Graphics2D)img.getGraphics();
dgc.setColor(getBackground());
dgc.fillRect(0,getWidth(),line);
dgc.setColor(g2d.getColor());
if (text != null) dgc.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,text);
if (aa != null) dgc.setRenderingHint(RenderingHints.KEY_ANTIALIASING,aa);
dgc.setFont(font);
dgc.drawChars(txt,txt. length,10,line-5);
g2d.drawImage(img,null);
dgc.dispose();
img.flush();
return py + line;
}
public static 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 AwtAA());
wnd.setSize(new Dimension(850,330));
wnd.setVisible(true);
}
}
所以问题是我如何更改默认值(无提示设置或设置为VALUE_TEXT_ANTIALIAS_DEFAULT)以在字体渲染中使用抗锯齿?
由于我可以找到的所有答案都设置了上面的几个属性,我开始查看源代码……并在类SunGraphics2D构造函数中对openjdk-7进行了一些粗略的“修复”:
apt-get source openjdk-7-jre
cd openjdk-7-7u55-2.4.7
tar zxf jdk.tar.gz
cd jdk-9448fff93286/src/share/classes/sun/java2d
patch -p1 <<"__EOF__"
--- a/SunGraphics2D.java 2014-06-29 23:39:34.183177958 +0300
+++ b/SunGraphics2D.java 2014-06-29 23:39:18.675177667 +0300
@@ -244,7 +244,10 @@
renderHint = SunHints.INTVAL_RENDER_DEFAULT;
antialiasHint = SunHints.INTVAL_ANTIALIAS_OFF;
- textAntialiasHint = SunHints.INTVAL_TEXT_ANTIALIAS_DEFAULT;
+ textAntialiasHint = SunHints.INTVAL_TEXT_ANTIALIAS_ON;
fractionalMetricsHint = SunHints.INTVAL_FRACTIONALMETRICS_OFF;
lcdTextContrast = lcdTextContrastDefaultValue;
interpolationHint = -1;
__EOF__
javac SunGraphics2D.java
cd ../..
jar cvf /tmp/rt.jar sun/java2d/SunGraphics2D.class
然后把它作为
java -Xbootclasspath/p:/tmp/rt.jar AwtAA
这为测试程序以及Vivado工具提供了诀窍(除了Vidado以某种方式以不同方式启动并且似乎不尊重-Xbootclasspath,所以我真的不得不更新系统rt.jar中的SunGraphics2D类).这似乎适用于OpenJDK和Oracle.
最后一个问题……除了猴子修补系统罐之外,还有其他一些强制抗锯齿文本作为默认值的方法吗?
最佳答案