2

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.

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.

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

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.

And it works with ASCII characters.

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

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

Edit3: Same with the newest JDK.

Asyx
  • 141
  • 3
  • 11
  • From my limited testing, the font doesn't generate ANY output. If I use the standard font (Tahoma), it works fine – MadProgrammer May 07 '15 at 01:45
  • What's `image`? What's `string`? Is your file utf-8 encoded? Have you tried using a plain number instead of `g2.getFontMetrics().getAscent()`? (Does `g2.getFontMetrics().getAscent()` match what the Noto Sans ascent value says it should be?). Many questions that need answers. – Mike 'Pomax' Kamermans May 07 '15 at 04:55
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). See also [Setting custom font](http://stackoverflow.com/q/13717481/418556) & [Getting fonts, sizes, bold,…etc](http://stackoverflow.com/q/6965038/418556). – Andrew Thompson May 07 '15 at 06:49
  • @MadProgrammer I only get the empty squares with Tahoma – Asyx May 07 '15 at 11:42
  • @Mike'Pomax'Kamermans I posted a working example now. image is the BufferedImage I save in a file and write to, string is the String that's supposed to be rendered and the Font is unicode encoded. Yes to all your other questions. – Asyx May 07 '15 at 11:45
  • @AndrewThompson I edited my initial post with an example. – Asyx May 07 '15 at 11:46
  • good edit, bonus points for the runnable code, that always helps people quickly see what you're see, too. However, I cannot reproduce your bug: if I run your code, using noto sans regular, I see the correct output: https://i.imgur.com/E0sXCiB.png, even with my file saved in ANSI encoding. Have you tried running just this test file through javac on another computer, and then run the Test.class with notosans in the same directory? – Mike 'Pomax' Kamermans May 07 '15 at 16:36
  • @Mike'Pomax'Kamermans Now it gets weird. If I compile the class with javac on my PC (java 1.8_40) I get [this](http://imgur.com/OgKitl1). If I copy the .class file to my Ubuntu server (Java 1.8_45), I get the same thing. If I copy the .java to my server and compile it there, I get the proper result. Gonna try updating Java now. Maybe that will fix it. – Asyx May 07 '15 at 19:58

1 Answers1

5

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

Asyx
  • 141
  • 3
  • 11
  • I distinctly remember asking you whether your file was encoded as utf8, and you said yes ;) – Mike 'Pomax' Kamermans May 08 '15 at 01:08
  • @Mike'Pomax'Kamermans I thought it would be because when I saved the file I still had the umlauts when I opened it again. It was also quite late so I didn't really think about properly checking :( Do you want to write that as a proper answer so I can accept it? – Asyx May 08 '15 at 09:46
  • Exactly this. And also make sure that your compiler picks up the encoding. When using gradle, one should do ``` tasks.withType( GroovyCompile ) { options.encoding = 'UTF-8' } tasks.withType( JavaCompile ) { options.encoding = 'UTF-8' } ``` [see this post for gradle setting](https://stackoverflow.com/a/34717160/11215281) – Thysce Apr 04 '22 at 15:02