Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font glyph not rendering with Graphics2D drawString since Java 7u13

I have a weird problem when drawing strings with some specific true type fonts in Java 32bit on Windows 10.

Since Java 7u13, whenever a character/glyph of the font is wider than 4 times it's height, it is not rendered at all using Graphics2D.drawString (e.g. glyph 4001em wide with basic font size of 1000em):

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    g2.setFont(new Font("myFontWithWideGlyphForX", Font.PLAIN, 12));
    g2.drawString("XXXX", 10, 10);
}

However, the font renders correctly on a JLabel so after some investigation of the underlying Swing code I have realised that setting the rendering hint KEY_TEXT_ANTIALIASING to VALUE_TEXT_ANTIALIAS_LCD_HRGB makes the text render correctly:

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D)g;
    g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
    g2.setFont(new Font("myFontWithWideGlyphForX", Font.PLAIN, 12));
    g2.drawString("XXXX", 10, 10);
}

It seems that the rendering algorithm works differently after setting the subpixel atialiasig and renders the font correctly.

If I switch to Java 7u11 or earlier the text renders without any problems and without setting the VALUE_TEXT_ANTIALIAS_LCD_HRGB.

The same happens for any other font with such wide glyphs - for example this random font has such wide glyph for character "J": http://www.fontspace.com/digital-magic/hdgems5 - it renders ok with Java 7u11 but doesn't render at all with anythig newer than that.

Setting the subpixel antialiasing VALUE_TEXT_ANTIALIAS_LCD_HRGB just to render the font seems hacky, ugly and is not always possible (e.g. when usuing third party libs). Does anyone know what is the reason behind awt not rendering such characters since 7u13? Are such font's just not supported? Or maybe this is a bug?

like image 913
rince Avatar asked Jun 27 '17 13:06

rince


1 Answers

I was not able to get an answer regarding this problem, however I did find a workaround.

None of the .ttf fonts that I have found rendered Glyphs wider that 4 times the bounding box/glyph height, when using Java 7u13 or newer. This problem persisted no matter the tool used to generate the font.

However .otf fonts seem to work ok, even with such wide glyphs, so as a workaround the font was just converted to otf.

like image 175
rince Avatar answered Oct 06 '22 00:10

rince