即使字体支持unicode字符,也无法使用Graphics2D渲染unicode字符

时间:2021-07-09 23:27:01

I try to render text to a BufferedImage and that works quite great but I can't render any characters that are not ASCII (at least as far as I can see). All I could find was that it's because of the font so I downloaded Google's "Noto" fonts that seem to support literally every script but I still get the boxes.

我尝试将文本呈现给BufferedImage并且工作得非常好,但是我无法渲染任何非ASCII字符(至少就我所见)。我所能找到的只是因为字体,所以我下载了Google的“Noto”字体,似乎支持字面上的每个脚本,但我仍然得到了这些框。

I'm not even trying to render something particularly exotic. Only German umlauts and the sharp s (Ää Öö Üü ß).

我甚至都没有尝试渲染特别异国情调的东西。只有德国的变音符号和尖锐的变音符号(ÄäÖöÜüß)。

I create the font like this

我创建这样的字体

Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("NotoSans-Regular.ttf")).deriveFont(12f);

And render the whole thing like this

并渲染这样的整个事情

Graphics2D g2 = image.createGraphics();
g2.setFont(font);
g2.setColor(Color.white);
g2.drawString(string, 0, g2.getFontMetrics().getAscent());
g2.dispose();

It works with ASCII.

它适用于ASCII。

Google either lead me to very simple tutorials (which is literally the code I've got at the moment) or says that the problem is the font but it isn't since it works perfectly in the editor.

谷歌或者引导我学习非常简单的教程(实际上是我目前得到的代码),或者说问题是字体,但它不是因为它在编辑器中完美运行。

Thanks

Edit1: Here my full code

Edit1:这是我的完整代码

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class Test {
    public static void main(String[] args) throws IOException, FontFormatException {
        String string = "ÄäÖöÜüß";
        BufferedImage image;

        Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("NotoSans-Regular.ttf")).deriveFont(50f);
        Rectangle2D rec = font.getStringBounds(string, new FontRenderContext(null, false, false));
        image = new BufferedImage((int)rec.getWidth(), (int)rec.getHeight(), BufferedImage.TYPE_INT_RGB);

        Graphics2D g2 = image.createGraphics();
        g2.setFont(font);
        g2.setColor(Color.white);
        g2.drawString(string, 0, g2.getFontMetrics().getAscent());
        g2.dispose();

        File f = new File("image.png");
        if (!f.exists()) {
            if (!f.createNewFile()) {
                System.err.println("Can't create image file.");
            }
        }
        ImageIO.write(image, "png", f);
    }
}

The font can be downloaded here from google

该字体可以从谷歌这里下载

And this is my result

这是我的结果

Result

I think I got the quads generally with all other fonts. It compiles and saves the result in a PNG.

我想我通常会使用所有其他字体。它将结果编译并保存在PNG中。

And it works with ASCII characters.

它适用于ASCII字符。

Sorry for not using proper images but I can't do that without at least 10 reputation.

很抱歉没有使用正确的图像,但如果没有至少10个声誉,我就无法做到这一点。

Edit2: It works now but not on my PC. It works on Linux if I recompile, though...

Edit2:它现在可以使用,但不能在我的电脑上运行。如果我重新编译,它适用于Linux,但...

Edit3: Same with the newest JDK.

Edit3:与最新的JDK相同。

1 个解决方案

#1


I'm an idiot... sometimes I wonder how I get through the day without accidentally getting myself killed...

我是个白痴......有时候我想知道如何度过这一天而不会意外地让自己被杀......

If you can't use a unicode string properly and you couldn't find an answer even after fighting with Google for 2 days, check the encoding of your source file... Mine was set to Windows-1252...

如果您无法正确使用unicode字符串,即使在与Google斗争2天后也找不到答案,请检查源文件的编码...我的设置为Windows-1252 ...

#1


I'm an idiot... sometimes I wonder how I get through the day without accidentally getting myself killed...

我是个白痴......有时候我想知道如何度过这一天而不会意外地让自己被杀......

If you can't use a unicode string properly and you couldn't find an answer even after fighting with Google for 2 days, check the encoding of your source file... Mine was set to Windows-1252...

如果您无法正确使用unicode字符串,即使在与Google斗争2天后也找不到答案,请检查源文件的编码...我的设置为Windows-1252 ...