[jdk8u-dev] RFR: 8301246: NPE in FcFontManager.getDefaultPlatformFont() on Linux without installed fontconfig
Sergey Bylokhov
serb at openjdk.org
Tue Jan 31 20:36:56 UTC 2023
On Fri, 27 Jan 2023 14:33:36 GMT, Alexander Scherbatiy <alexsch at openjdk.org> wrote:
> The issues is reproduced only with OpenJDK. Oracle 1.8.0_351 does not throw the NPE.
>
> OpenJDK 8u built from sources with custom fontconfig.properties file throws NPE
> `at sun.awt.FcFontManager.getDefaultPlatformFont(FcFontManager.java:76)`
> on Linux where fontconfig is not installed.
>
> The fix allows to return a default font info from FcFontManager.getDefaultPlatformFont() method even FontConfigManager.getFontConfigFonts() returns null.
>
> The issue has been already fixed in JDK 11 as part of the fix `8191522: Remove Bigelow&Holmes Lucida fonts from JDK sources` https://github.com/openjdk/jdk/commit/9a9dad8b63e6234829132d5557fbd412d295bb26
>
> This is a backport of only small part of the fix `8191522` related to NPE in FcFontManager.getDefaultPlatformFont() method.
>
> Steps to reproduce:
> - Build jdk8u from sources: https://github.com/openjdk/jdk8u-dev/
> - Copy custom [fontconfig.properties](https://bugs.openjdk.org/secure/attachment/102432/fontconfig.properties) file to build/linux-x86_64-normal-server-release/images/j2sdk-image/jre/lib directory
> - Run docker with ubuntu 20.04, install dejavu fonts, and freetype (do not install fontconfig)
>
> docker run -it ubuntu:20.04 bash
> apt update
> apt install -y fonts-dejavu
> apt install -y libfreetype6
>
> - Run HelloImage java sample in the docker with the built jdk
>
> import javax.imageio.ImageIO;
> import java.awt.*;
> import java.awt.image.BufferedImage;
> import java.io.File;
>
> public class HelloImage {
>
> public static void main(String[] args) throws Exception {
>
> BufferedImage buff = new BufferedImage(300, 200, BufferedImage.TYPE_INT_RGB);
> Graphics2D g = buff.createGraphics();
> g.setColor(Color.WHITE);
> g.fillRect(0, 0, buff.getWidth(), buff.getHeight());
>
> g.setColor(Color.BLUE);
> g.setFont(g.getFont().deriveFont(32f));
> g.drawString("Hello, Image!", 50, 50);
> g.dispose();
>
> File file = new File("hello-image.png");
> ImageIO.write(buff, "png", file);
> }
> }
>
>
>
> build/linux-x86_64-normal-server-release/images/j2sdk-image/bin/javac HelloImage.java
> build/linux-x86_64-normal-server-release/images/j2sdk-image/bin/java HelloImage
> Exception in thread "main" java.lang.NullPointerException
> at sun.awt.FcFontManager.getDefaultPlatformFont(FcFontManager.java:76)
> at sun.font.SunFontManager$2.run(SunFontManager.java:443)
> at java.security.AccessController.doPrivileged(Native Method)
> at sun.font.SunFontManager.<init>(SunFontManager.java:386)
> at sun.awt.FcFontManager.<init>(FcFontManager.java:35)
> at sun.awt.X11FontManager.<init>(X11FontManager.java:57)
> at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
> at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
> at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
> at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
> at java.lang.Class.newInstance(Class.java:442)
> at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:83)
> at java.security.AccessController.doPrivileged(Native Method)
> at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
> at java.awt.Font.getFont2D(Font.java:491)
> at java.awt.Font.access$000(Font.java:224)
> at java.awt.Font$FontAccessImpl.getFont2D(Font.java:228)
> at sun.font.FontUtilities.getFont2D(FontUtilities.java:200)
> at sun.java2d.SunGraphics2D.checkFontInfo(SunGraphics2D.java:669)
> at sun.java2d.SunGraphics2D.getFontInfo(SunGraphics2D.java:835)
> at sun.java2d.pipe.GlyphListPipe.drawString(GlyphListPipe.java:50)
> at sun.java2d.SunGraphics2D.drawString(SunGraphics2D.java:2933)
> at HelloImage.main(HelloImage.java:17)
Marked as reviewed by serb (Reviewer).
I can reproduce the bug described above using the steps to reproduce and jdk8u. The change fixed the problem.
-------------
PR: https://git.openjdk.org/jdk8u-dev/pull/246
More information about the jdk8u-dev
mailing list