Java字体渲染

发布于 2024-08-17 05:10:28 字数 3760 浏览 3 评论 0原文

我一直在尝试增强用 Java 编写的 GUI 系统以使用子像素抗锯齿功能,并且已经成功,除了两个剩余的异常情况。这是我的 几周前的其他问题

第一个问题是,将渲染提示 KEY_ANTIALIASING 设置为 VALUE_ANTIALIAS_ON 会导致 KEY_TEXT_ANTIALIASING 在设置为 LCD(子像素)AA 值时被忽略。有人能解释一下吗?目前,我被迫在渲染文本之前VALUE_ANTIALIAS_OFF,并在渲染文本后将其重新打开(以便其他绘画,如圆圈等,被AA)。下面的独立测试程序证明了这个问题。

第二个问题是我找不到办法查询 AA 的底层操作系统设置,所以我必须做一个相当笨拙的解决方法,即创建一个 Swing JLabel,获取它的 FontMetrics,获取它的FontRenderContext,然后从中获取 AA 提示。除了在完全不使用 Swing 的程序中使用 Swing 之外,它无法在运行任何 J2ME JVM 的设备上运行。谁能建议更好的方法来做到这一点?如果需要J5或J6也可以,因为当前的kludge已经需要J6(但只需要J4是最好的)。我已经尝试了所有默认设置并使用 AWT 组件而不是 JLabel


测试程序

该程序验证要使子像素 AA 正常工作,必须首先禁用常规 AA 设置。 (PS:我写入后台缓冲区是因为我的底层 GUI 会这样做,并且我想在等效的上下文中进行测试)。

import java.awt.*;
import java.awt.event.*;

public class AwtTestFrame1b extends Panel {

private final Font                      font=new Font(Font.SANS_SERIF, Font.PLAIN, 16);
private final int                       line=25;

AwtTestFrame1b() {
    setBackground(SystemColor.control);
    }

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D)g;

    int                                 py=0;

    py=paintText(g2d,py,null                                        ,false);
    py=paintText(g2d,py,null                                        ,true );
    py+=line;

    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF     ,false);
    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT ,false);
    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_ON      ,false);
    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_GASP    ,false);
    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB,false);
    py+=line;

    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF     ,true );
    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT ,true );
    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_ON      ,true );
    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_GASP    ,true );
    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB,true );
    py+=line;
    }

private int paintText(Graphics2D g2d, int py, Object val, boolean aa) {
    Graphics2D                          dgc=g2d;
    char[]                              txt=("The quick brown fox jumped over the lazy dog ("+val+", General AA: "+aa+")").toCharArray();
    Image                               img=null;

    GraphicsConfiguration cfg=getGraphicsConfiguration();
    img=cfg.createCompatibleImage(getWidth(),line);
    dgc=(Graphics2D)img.getGraphics();
    dgc.setColor(getBackground());
    dgc.fillRect(0,0,getWidth(),line);
    dgc.setColor(g2d.getColor());

    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.drawChars(txt,0,txt.length,10,line-5);
    g2d.drawImage(img,  0,py,  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 AwtTestFrame1b());
    wnd.setSize(new Dimension(1000, 600));
    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 two remaining anomalies. This is a follow on to my other question from a few weeks ago.

The first 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.

The second problem is that I can find no way to query the underlying O/S setting for AA, so I have to do a rather kludgey workaround, which is to create a Swing JLabel, get it's FontMetrics, get it's FontRenderContext and then get the AA hint from that. Apart from involving Swing in a program that otherwise makes absolutely no use of Swing, it will not work on a device running any J2ME JVM. Can anyone suggest a better way to do this? It's OK if it requires J5 or J6, since the current kludge already requires J6 (but needing only J4 would be best). I have already tried every default setting and using an AWT component instead of JLabel.


Test Program

This program verifies that for subpixel AA to work, the general AA setting must first be disabled. (PS: I write to a back-buffer because my underlying GUI does, and I wanted to test in an equivalent context).

import java.awt.*;
import java.awt.event.*;

public class AwtTestFrame1b extends Panel {

private final Font                      font=new Font(Font.SANS_SERIF, Font.PLAIN, 16);
private final int                       line=25;

AwtTestFrame1b() {
    setBackground(SystemColor.control);
    }

public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D)g;

    int                                 py=0;

    py=paintText(g2d,py,null                                        ,false);
    py=paintText(g2d,py,null                                        ,true );
    py+=line;

    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF     ,false);
    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT ,false);
    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_ON      ,false);
    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_GASP    ,false);
    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB,false);
    py+=line;

    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF     ,true );
    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT ,true );
    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_ON      ,true );
    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_GASP    ,true );
    py=paintText(g2d,py,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB,true );
    py+=line;
    }

private int paintText(Graphics2D g2d, int py, Object val, boolean aa) {
    Graphics2D                          dgc=g2d;
    char[]                              txt=("The quick brown fox jumped over the lazy dog ("+val+", General AA: "+aa+")").toCharArray();
    Image                               img=null;

    GraphicsConfiguration cfg=getGraphicsConfiguration();
    img=cfg.createCompatibleImage(getWidth(),line);
    dgc=(Graphics2D)img.getGraphics();
    dgc.setColor(getBackground());
    dgc.fillRect(0,0,getWidth(),line);
    dgc.setColor(g2d.getColor());

    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.drawChars(txt,0,txt.length,10,line-5);
    g2d.drawImage(img,  0,py,  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 AwtTestFrame1b());
    wnd.setSize(new Dimension(1000, 600));
    wnd.setVisible(true);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

記憶穿過時間隧道 2024-08-24 05:10:28

AWT 桌面属性有什么帮助吗?特别是“awt.font.desktophints” - 这些包含本机组件使用的 AA 提示,但可以应用于您想要的任何 Graphics2D。

最近读完《肮脏的富有客户》中的 AA 部分,这只是一个盲目的尝试。

使用看起来像这样:

String str = "A quick brown fox jumps over the lazy dog";
Toolkit tk = Toolkit.getDefaultToolkit();
Map desktopHints = (Map)(tk.getDesktopProperty("awt.font.desktophints"));
Graphics2D g2d = (Graphics2D)g;

if(desktopHints != null) {
    g2d.addRenderingHints(desktopHints);
}

g2d.drawString(str, someX, someY);

我能够获得与使用这些提示的 LCD HRGB 模式相同的结果(使用您的示例类以及drawChars和drawImage,为了简单起见,只需输入drawString),并且在我的机器上没有其他调用。

我不确定这需要哪个版本的 Java,如果它是您正在寻找的...

Are the AWT Desktop Properties of any help? In particular, "awt.font.desktophints" - these contain the AA hints that the native components use, but can be applied to any Graphics2D you want.

Just a shot in the dark, having recently read through the AA section in Filthy Rich Clients.

Use would look something like this:

String str = "A quick brown fox jumps over the lazy dog";
Toolkit tk = Toolkit.getDefaultToolkit();
Map desktopHints = (Map)(tk.getDesktopProperty("awt.font.desktophints"));
Graphics2D g2d = (Graphics2D)g;

if(desktopHints != null) {
    g2d.addRenderingHints(desktopHints);
}

g2d.drawString(str, someX, someY);

I was able to get the same results (using your example class and drawChars and drawImage, just typed drawString for simplicity) as the LCD HRGB mode using these hints and no other calls on my machine.

I'm not sure what release of Java this requires, if it's what you're looking for...

甜宝宝 2024-08-24 05:10:28

不要忘记:“实现可以自由地忽略 完全提示。”作为参考,以下是我在 24 点处看到的内容:

AwtTestFrame, 24 点

Don't forget: "Implementations are free to ignore the hints completely." For reference, here's what I see at 24 points:

AwtTestFrame, 24 points

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文