Mac OS X上的Java App无法正确打印字体

我最近发现我写的徽章打印程序在
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);

    }
}

最佳答案 我写了一篇关于这篇文章的博文,名为
Printing is Broken on Mac OS X with Java 7,其中包括我发现的所有内容,包括原因,错误报告,以及我们现在的解决方法.简短的版本是它看起来像苹果的Mac OS或Java 7中的一个错误,并且在撰写本文时它仍然没有修复.

点赞