JDK-8139838 : Bug in PrismFontFactory generates NullPointerException when loading .ttc font
  • Type: Enhancement
  • Component: javafx
  • Sub-Component: graphics
  • Affected Version: 8u60
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: os_x
  • CPU: x86
  • Submitted: 2015-10-08
  • Updated: 2018-08-13
  • Resolved: 2016-05-20
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9Fixed
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

A DESCRIPTION OF THE PROBLEM :
in method createFontResource line 333:

>  if (index == 0 || name.equals(fontname)) 

name is the parameter to the method.index can be != 0 when loading .ttc file so the check for equality is performed. 

createFontResource is called from loadEmbeddedFont which is called from e.g. PrismFontLoader.loadFont with null name parameter. 



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Toolkit.getToolkit().getFontLoader().loadFont("PTSans.ttc", 16);
returns null. The following css fragment prints exception to the console: 

@font-face { 
 src: url("PTSans.ttc");

}

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Font does not load.

ACTUAL -
Font should load


ERROR MESSAGES/STACK TRACES THAT OCCUR :
Caused by: java.lang.NullPointerException
	at com.sun.javafx.font.PrismFontFactory.createFontResource(PrismFontFactory.java:333)
	at com.sun.javafx.font.PrismFontFactory.loadEmbeddedFont(PrismFontFactory.java:1607)
	at com.sun.javafx.font.PrismFontFactory.loadEmbeddedFont(PrismFontFactory.java:1547)
	at com.sun.javafx.font.PrismFontLoader.loadFont(PrismFontLoader.java:99)
	at javafx.scene.text.Font.loadFont(Font.java:400)
	at com.sun.javafx.css.StyleManager.loadStylesheetUnPrivileged(StyleManager.java:1109)
	at com.sun.javafx.css.StyleManager.loadStylesheet(StyleManager.java:917)
	at com.sun.javafx.css.StyleManager.processStylesheets(StyleManager.java:1538)
	at com.sun.javafx.css.StyleManager.gatherParentStylesheets(StyleManager.java:1579)
	at com.sun.javafx.css.StyleManager.findMatchingStyles(StyleManager.java:1641)
	at javafx.scene.CssStyleHelper.createStyleHelper(CssStyleHelper.java:111)
	at javafx.scene.Node.reapplyCss(Node.java:8983)
	at javafx.scene.Node.impl_reapplyCSS(Node.java:8946)
	at javafx.scene.Node.invalidatedScenes(Node.java:854)
	at javafx.scene.Node.setScenes(Node.java:919)
	at javafx.scene.Parent.scenesChanged(Parent.java:659)
	at javafx.scene.Node.invalidatedScenes(Node.java:853)
	at javafx.scene.Node.setScenes(Node.java:919)
	at javafx.scene.Scene$9.invalidated(Scene.java:1119)
	at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
	at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
	at javafx.scene.Scene.setRoot(Scene.java:1072)
	at javafx.scene.Scene.<init>(Scene.java:347)
	at javafx.scene.Scene.<init>(Scene.java:223)
	at sample.Main.start(Main.java:18)
	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
	at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
	at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
	at java.security.AccessController.doPrivileged(Native Method)
	at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
	at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)

REPRODUCIBILITY :
This bug can be reproduced always.


Comments
Changeset: d758b6552e67 Author: prr Date: 2016-05-20 12:04 -0700 URL: http://hg.openjdk.java.net/openjfx/9-dev/rt/rev/d758b6552e67 8139838: Bug in PrismFontFactory generates NullPointerException when loading .ttc font Reviewed-by: kcr ! modules/graphics/src/main/java/com/sun/javafx/font/FontFactory.java ! modules/graphics/src/main/java/com/sun/javafx/font/PrismFontFactory.java ! modules/graphics/src/main/java/com/sun/javafx/font/PrismFontFile.java ! modules/graphics/src/main/java/com/sun/javafx/font/PrismFontLoader.java ! modules/graphics/src/main/java/com/sun/javafx/tk/FontLoader.java ! modules/graphics/src/main/java/com/sun/prism/j2d/J2DFontFactory.java ! modules/graphics/src/main/java/javafx/scene/text/Font.java ! modules/graphics/src/test/java/test/com/sun/javafx/pgstub/StubFontLoader.java ! modules/web/src/main/java/com/sun/javafx/webkit/prism/WCFontCustomPlatformDataImpl.java + tests/manual/text/LoadFonts.java
20-05-2016

It looks fine now other than one minor thing in two places: * font format then an array of <code>Font</code> object will be returned. Should be "array of ... objects" (plural). No need for a new webrev. +1
20-05-2016

http://cr.openjdk.java.net/~prr/8139838.1/ fixes the javadoc (which I created and reviewed) and white space. gradle checkrepo was run too. The suggesting logging does not have access to the file in the refcounter as the class is static. Also I know this logic already needs some cleaning up which will happen in a different bug. The shutdown hook enumerates two lists - embedded and temp fonts and these may not be mutually exclusive.
20-05-2016

The API looks good and is approved for JDK 9. Issues: 1. The following javadoc in Font.java is not right. You have a sentence about loading a TTC font collection as well as the "@since 9" annotation on the existing loadFont(InputStream,int) method rather than on the new loadFonts method: 512 * The use case for this method is for loading all fonts 513 * from a TrueType Collection (TTC). 514 * <p> ... 539 * @since 9 540 */ 541 public static Font loadFont(InputStream in, double size) { 2. Very minor: slight spacing issue in FontFactory.java -- the second line of arguments in the loadEmbeddedFont methods no longer align with the first line. Similar issue in PrismFontFactory.java (call to createFontResource on line 313). Question: In PrismFontFile.java might it be worth checking and logging a warning of the ref count is decremented when already 0? 181 synchronized int decrement() { 182 return (refCnt == 0) ? 0 : --refCnt; 183 }
20-05-2016

webrev : http://cr.openjdk.java.net/~prr/8139838/ Adds loadFonts() equivalents of the existing loadFont(). The existing loadFont() will now not NPE and will just return the first font in the collection. I have tested this on Mac, Linux and Windows using the included manual LoadFonts.java test and supplying fonts that are not platform fonts. I also verified the temp files are cleaned up.
19-05-2016

See also https://bugs.openjdk.java.net/browse/JDK-8055463 which requests the same in Java 2D. We should do both if we do either although even if we do not add a capability to enumerate the list of fonts we can fix the NPE and return the first font.
12-01-2016

.ttc (TrueType collection) is not a supported format for this API. We would have to enhance it to accept an index to specify which font in the collection you want. Or, just always accept the first font in the collection, even if it is not the one you want .. If you want a particular font from a collection you would need prior knowledge of its internal list, or would need to iterate until you find the one you want.
30-10-2015