Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8144703

ClassCastException: sun.font.CompositeFont cannot be cast to PhysicalFont

    Details

    • Subcomponent:
      2d
    • Resolved In Build:
      b127
    • OS:
      linux

      Backports

        Description


        Java app gives the following exception message:

        Exception in thread "main" java.lang.ClassCastException:
        sun.font.CompositeFont cannot be cast to sun.font.PhysicalFont

        1. CanDisplayTest.java
          0.2 kB
          Dmitry Markov
        2. CanDisplayTest2.java
          0.2 kB
          Dmitry Markov
        3. Helvetica.ttf
          397 kB
          Dmitry Markov

          Issue Links

            Activity

            Hide
            dmarkov Dmitry Markov added a comment -
            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.
            Show
            dmarkov Dmitry Markov added a comment - 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.
            Hide
            dmarkov Dmitry Markov added a comment -
            Problem description:
            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.

            Fix:
            Surround invocation of SunFontManager.findFont2D() inside CompositeFont.doDeferredInitialisation() with 'try-catch' to avoid propagation of ClassCastException.
            Show
            dmarkov Dmitry Markov added a comment - Problem description: 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. Fix: Surround invocation of SunFontManager.findFont2D() inside CompositeFont.doDeferredInitialisation() with 'try-catch' to avoid propagation of ClassCastException.
            Hide
            dmarkov Dmitry Markov added a comment -
            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.
            Show
            dmarkov Dmitry Markov added a comment - 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.
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk9/client/jdk/rev/745d16c6c67d
            User: dmarkov
            Date: 2016-06-29 08:39:41 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/client/jdk/rev/745d16c6c67d User: dmarkov Date: 2016-06-29 08:39:41 +0000
            Hide
            hgupdate HG Updates added a comment -
            URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/745d16c6c67d
            User: amurillo
            Date: 2016-07-13 03:50:56 +0000
            Show
            hgupdate HG Updates added a comment - URL: http://hg.openjdk.java.net/jdk9/jdk9/jdk/rev/745d16c6c67d User: amurillo Date: 2016-07-13 03:50:56 +0000

              People

              • Assignee:
                dmarkov Dmitry Markov
                Reporter:
                shadowbug Shadow Bug
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: