JDK-8144703 : ClassCastException: sun.font.CompositeFont cannot be cast to PhysicalFont
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.
Java app gives the following exception message:
Exception in thread "main" java.lang.ClassCastException:
sun.font.CompositeFont cannot be cast to sun.font.PhysicalFont
There is no possibility to create an auto test since the problem is reproducible only with specific font file. As far as I understand we avoid creating manual regression tests.
If an incorrect font file is used, we unable to initialise the font during CompositeFont.doDeferredInitialisation() invocation and assign default physical font to the corresponding components entry. Later at the same method, (i.e. doDeferredInitialisation()) we try to update this entry one more time, since the name retrieved from the entry and the name provide by componentNames array are different. The method SunFontManager.findFont2D() may return a composite font for such case. As a result we get ClassCastException trying to assign CompositeFont object to the instance of PhysicalFont.
Surround invocation of SunFontManager.findFont2D() inside CompositeFont.doDeferredInitialisation() with 'try-catch' to avoid propagation of ClassCastException.
It seems the font file provided by the submitter has an incorrect format or something like that. Windows font viewer cannot open Helvetica.ttf and reports that it is not a valid font file.
Java is unable to retrieve 'font name' and 'family name' during initialisation which take place at CompositeFont.doDeferredInitialisation() and assigns default physical font (Lucida Sans Regular) to the corresponding components entry. Later at the same method, (i.e. doDeferredInitialisation()) we try to update this entry one more time, since the name retrieved from the entry and the name provide by componentNames array are different. The method SunFontManager.findFont2D() returns a composite font for the name 'Helvetica', since this font is only present as a part of composite. As a result we get ClassCastException trying to assign CompositeFont object to the instance of PhysicalFont.
So the exception is caused by incorrect font file format.
Note: I converted provided font file (Helvetica.ttf) to TrueTypeFont using on-line font converter at http://www.files-conversion.com/font-converter.php
I was not able to reproduce the problem using the converted font file.
Anyway I think we should NOT fail with ClassCastException even if a font file with incorrect format is used.
The problem is reproducible on jdk8u92 and jdk9
The fix for JDK-8132850 didn't resolve the problem. It just changed the conditions under which the issue can be observed.
ClassCastException is still reproducible on Linux with a slightly changed test case (CanDisplayTest2.java). The steps to reproduce are the same.
The problem is reproducible on Linux.
Steps to reproduce:
1. Install font, (i.e. 'cp Helvetica.ttf /usr/share/fonts' and then 'sudo fc-cache -f')
2. Run the test, (i.e. 'java CanDisplayTest')
The following exception is thrown
Exception in thread "main" java.lang.ClassCastException: sun.font.CompositeFont cannot be cast to sun.font.PhysicalFont
The test should print out 'false'.
Tests and font file attached
Most likely the problem is already fixed by JDK-8132850