I'd like to draw strings in pngs. I need letters with a height of up to 600 px.
Therefore I tried using Graphics2D.drawString:
String text = "o";
graphics.setFont(font.deriveFont(Font.PLAIN, fontsize));
graphics.drawString(text, 0, 200);
Text with font sizes up to 100 works fine. Problem: With font size 101 some round characters (like "o") are getting a little angled.
This is reproducable with fonts "Abril_Fatface", "Amatic_SC" and "Cinzel_Decorative" (from google Fonts, e.g. https://fonts.google.com/specimen/Abril+Fatface or https://fonts.google.com/specimen/Amatic+SC ; ttf-file downloadable via "select this font" and then "download this section" within the dialog "1 family selected"). With other fonts the strings are drawn OK.
LibreOffice shows the characters round (as expected).
Here is the full code:
import java.awt.Color;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class CustomFontWriting {
public static void main(String[] args) throws FontFormatException, IOException {
Font font = Font.createFont(Font.TRUETYPE_FONT, new File("Amatic_SC.ttf"));
// Create empty image
BufferedImage image = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = image.createGraphics();
graphics.setColor(Color.BLACK);
// Draw letter "o" with fontsize 101 in an image
graphics.setFont(font.deriveFont(Font.PLAIN, 101));
graphics.drawString("o", 0, 200);
// Write image to disk
File output = new File("output.png");
ImageIO.write(image, "png", output);
}
}
Expected result: round letter "o".
Actual result: angled letter "o".
A solution is to use OpenJDK 11 or a later version. See comments at the question.