Java 应用在 Mac OS X 上无法正确打印字体

5

我最近发现,我写的一个徽章打印程序在Windows上运行良好,但在MAC OS X上无法正确打印。我正在使用Arial TrueType字体。该程序似乎显示一种基本字体,没有正确的缩放。我正在使用Intellij和jdk1.7.0_15。该字体在屏幕上显示正确,但在打印到打印机或PDF时不正确。当我在控制台上列出程序可用的字体时,使用GraphicsEnvironment.getAvailableFontFamilyNames,Arial被列为其中之一。当我直接从字体文件中加载字体时,它可以正常工作。我已经在这个问题上纠结了一天,希望有任何建议。以下是演示我的问题的代码...

/**
 *  DisplayPage.java
 */
import java.awt.*;
import java.awt.print.*;

public class DisplayPage extends ApplicationFrame {

    public static void main(String[] args) {
        // Create app and display draw page
        DisplayPage f = new DisplayPage();
        f.setTitle("PaintingAndStroking v1.0");
        f.setSize(850, 1100);
        f.center();
        f.setVisible(true);

        // Generate print job to print page
        PrinterJob pj = PrinterJob.getPrinterJob();
        pj.setPrintable(new PrintPage());
        boolean doPrint = pj.printDialog();
        if (doPrint) {
            try {
                pj.print();
            } catch (PrinterException e) {
                System.out.println(e);
            }
        }
    }

    /**
     * This is called by the windows event processor
     * @param g java.awt.Graphics context for display
     */
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D)g;
        DrawPage.draw(g2d);
    }

}

/**
 *  ApplicationFrame.java
 */
import java.awt.*;
import java.awt.event.*;

public class ApplicationFrame extends Frame {

    public ApplicationFrame() { this("ApplicationFrame v1.0"); }

    public ApplicationFrame(String title) {
        super(title);
        setSize(850, 1100);
        center();

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                dispose();
                System.exit(0);
            }
        });
    }

    public void center() {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = getSize();
        int x = (screenSize.width - frameSize.width) / 2;
        int y = (screenSize.height - frameSize.height) / 2;
        setLocation(x, y);
    }

}

/**
 * PrintPage.java
 */
import java.awt.*;
import java.awt.print.*;

public class PrintPage
        implements Printable {

    public int print(Graphics g, PageFormat pf, int page)
            throws PrinterException {

        // We have only one page
        if (page > 0) {
            return NO_SUCH_PAGE;
        }

        // Create Graphics2D context
        Graphics2D g2d = (Graphics2D)g;

        // Draw page
        DrawPage.draw(g2d);

        // Verify page exists
        return PAGE_EXISTS;
    }

}

/**
 * DrawPage.java
 */
import java.awt.*;

public class DrawPage {

    static public void draw(Graphics2D g2d) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Font[] fonts = ge.getAllFonts();
        String[] fontFamilies = ge.getAvailableFontFamilyNames();
        for (int i=0; i<fontFamilies.length; i++) {
            System.out.println(fontFamilies[i]);
        }

        double x = 15, y = 50, w = 70, h = 70;
        GradientPaint gp = new GradientPaint(75, 75, Color.white,
                95, 95, Color.gray, true);

        // Fill with a gradient
        g2d.setPaint(gp);

        // Stroke with a solid color.
        g2d.setPaint(Color.black);
        g2d.setStroke(new BasicStroke(8));

        // Stroke with a gradient.
        g2d.setPaint(gp);

        // Draw text string
        String text = new String("This is a test...");
        g2d.setPaint(Color.black);
        g2d.setStroke(new BasicStroke(8));
        Font font = new Font("Arial", Font.PLAIN, 36);
        g2d.setFont(font);
        g2d.drawString("This is a test of Arial 36", 50, 100);

        font = new Font("Arial", Font.PLAIN, 72);
        g2d.setFont(font);
        g2d.drawString("This is a test of Arial 72", 50, 200);

    }
}
3个回答

2
我写了一篇关于这个问题的博客文章,名为“使用Java 7在Mac OS X上打印存在问题”,包括我发现的一切内容,包括原因、错误报告以及我们目前的解决方法。简而言之,看起来这是苹果的Mac OS或Java 7中的一个错误,截至目前仍未被修复。请点击此处阅读我的博客文章。

1
这个问题有一个解决方法。字体渲染问题是由于Graphics.drawString()实现的bug引起的。
避免这个问题的两种方法:
1. 您可以用以下方式替换对Graphics.drawString(...)的调用
Graphics.drawGlyphVector(Graphics.getFont().createGlyphVector(Graphics.getFontRenderingContext(), String.toCharArray()))
2. 您可以提供自己的Graphics2D实现,覆盖drawString()方法并执行上述转换并委托给drawGlyphVector()
使用drawGlyphVector()将导致文本由Java的AWT代码呈现,而不是本地操作系统的呈现,因此可能会出现一些视觉伪影和性能降低。但它比当前情况要好得多。

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接