Java:.ttf字体为空,宽度< = 0,系统字体工作

时间:2014-07-03 19:37:56

标签: java fonts awt true-type-fonts fontmetrics

我正在尝试生成给定文本大小的bufferedImage。 使用系统字体时,没有问题。 我试图检查了位置,所以这不应该是我的错误。 如果需要,我可以在某处上传字体。

font = Font.createFont(Font.TRUETYPE_FONT, ttfStream);

当使用.ttf文件时,我收到错误,表明那里没有数据。

font = Font.createFont(Font.TRUETYPE_FONT, ttfStream);

错误说:

Exception in thread "main" java.lang.IllegalArgumentException: Width (0) and height (1) cannot be <= 0
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1016)
at java.awt.image.BufferedImage.<init>(BufferedImage.java:340)
at ErrorExample.stringToBufferedImage(Untitled.java:64)
at ErrorExample.main(Untitled.java:35)

示例代码:

class ErrorExample {
    static boolean dontwork = true;

    public static void main(String[] args) throws IOException, FontFormatException{
        InputStream ttfStream = new BufferedInputStream(new FileInputStream("/test/monofont.ttf"));
         Font font;
        if(dontwork == true){ //here the fun seems to be.
            font = Font.createFont(Font.TRUETYPE_FONT, ttfStream);
        }else{
            font = new Font( "Verdana", Font.BOLD, 20 );
        }
        BufferedImage img = stringToBufferedImage(font, "sdf");

    System.out.println("Done.");
    }


    /**
         * Modiefied from http://stackoverflow.com/a/17301696/3423324
         * @param font 
         */
        public static BufferedImage stringToBufferedImage(Font f, String s) {
            //First, we have to calculate the string's width and height

            BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics g = img.getGraphics();

            //Set the font to be used when drawing the string
            //f = new Font("Tahoma", Font.PLAIN, 48);
            g.setFont(f);

            //Get the string visual bounds
            FontRenderContext frc = g.getFontMetrics().getFontRenderContext();
            Rectangle2D rect = f.getStringBounds(s, frc);
            //Release resources
            g.dispose();

            //Then, we have to draw the string on the final image

            //Create a new image where to print the character
            img = new BufferedImage((int) Math.ceil(rect.getWidth()), (int) Math.ceil(rect.getHeight()), BufferedImage.TYPE_INT_ARGB);
            //Graphics2D g2d = img.;
            //g2d.setColor(Color.black); // Otherwise the text would be white

            g = img.getGraphics();
            Graphics2D g2d = (Graphics2D) g;
            g.setColor(Color.black); //Otherwise the text would be white
            g2d.setColor(Color.black); //Otherwise the text would be white
            g2d.setFont(f);

            //Calculate x and y for that string
            FontMetrics fm = g.getFontMetrics();
            int x = 0;
            int y = fm.getAscent(); //getAscent() = baseline
            g2d.drawString(s, x, y);

            //Release resources
            g.dispose();

            //Return the image
            return img;
        }

}

1 个答案:

答案 0 :(得分:2)

问题是:新加载的字体没有嵌入大小信息。 来自Javadoc:

  

这些字体将返回为大小为1 的字体对象,标识转换和默认字体功能。然后,可以使用这些基本字体通过此类中的 deriveFont 方法派生具有不同大小,样式,变换和字体功能的新Font对象。

http://docs.oracle.com/javase/7/docs/api/java/awt/Font.html

使用系统字体时,已在给定参数中设置大小。

然而,使用ttf不是这种情况,必须手动设置大小:

font = font.deriveFont( 20f );

另请注意,它是一个浮点值,因为函数deriveFont重载了int值,这将设置样式,而不是大小。

相关问题