Class FontProgramFactory


  • public final class FontProgramFactory
    extends java.lang.Object
    Provides methods for creating various types of fonts.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private FontProgramFactory()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void clearRegisteredFontFamilies()
      Clears registered font cache
      static void clearRegisteredFonts()  
      static FontProgram createFont()
      Creates a new standard Helvetica font program file.
      static FontProgram createFont​(byte[] fontProgram)
      Creates a new font program.
      static FontProgram createFont​(byte[] fontProgram, boolean cached)
      Creates a new font program.
      static FontProgram createFont​(byte[] ttc, int ttcIndex, boolean cached)
      Creates a new TrueType font program from ttc (TrueType Collection) file bytes.
      static FontProgram createFont​(java.lang.String fontProgram)
      Creates a new font program.
      static FontProgram createFont​(java.lang.String fontProgram, boolean cached)
      Creates a new font program.
      static FontProgram createFont​(java.lang.String ttc, int ttcIndex, boolean cached)
      Creates a new TrueType font program from ttc (TrueType Collection) file.
      static FontProgram createFont​(java.lang.String fontProgram, java.lang.String cmap, boolean cached)
      Creates a new font program.
      private static FontProgram createFont​(java.lang.String name, java.lang.String cmap, byte[] fontProgram, boolean cached)  
      private static FontCacheKey createFontCacheKey​(java.lang.String name, byte[] fontProgram)  
      static FontProgram createRegisteredFont​(java.lang.String fontName)
      Creates a FontProgram from the font file that has been previously registered.
      static FontProgram createRegisteredFont​(java.lang.String fontName, int style)
      Creates a FontProgram from the font file that has been previously registered.
      static FontProgram createRegisteredFont​(java.lang.String fontName, int style, boolean cached)
      Creates a FontProgram from the font file that has been previously registered.
      static FontProgram createType1Font​(byte[] afm, byte[] pfb)
      Creates a new Type 1 font by the byte contents of the corresponding AFM/PFM and PFB files
      static FontProgram createType1Font​(byte[] afm, byte[] pfb, boolean cached)
      Creates a new Type 1 font by the byte contents of the corresponding AFM/PFM and PFB files
      static FontProgram createType1Font​(java.lang.String metricsPath, java.lang.String binaryPath)
      Creates a new Type 1 font by the corresponding AFM/PFM and PFB files
      static FontProgram createType1Font​(java.lang.String metricsPath, java.lang.String binaryPath, boolean cached)
      Creates a new Type 1 font by the corresponding AFM/PFM and PFB files
      private static FontProgram createType1Font​(java.lang.String metricsPath, java.lang.String binaryPath, byte[] afm, byte[] pfb, boolean cached)  
      static java.util.Set<java.lang.String> getRegisteredFontFamilies()
      Gets a set of registered font names.
      static java.util.Set<java.lang.String> getRegisteredFonts()
      Gets a set of registered font names.
      static boolean isRegisteredFont​(java.lang.String fontName)
      Checks if a certain font is registered.
      (package private) static byte[] readFontBytesFromPath​(java.lang.String path)  
      static void registerFont​(java.lang.String path)
      Registers a .ttf, .otf, .afm, .pfm, or a .ttc font file.
      static void registerFont​(java.lang.String path, java.lang.String alias)
      Register a font file and use an alias for the font contained in it.
      static int registerFontDirectory​(java.lang.String dir)
      Register all the fonts in a directory.
      static void registerFontFamily​(java.lang.String familyName, java.lang.String fullName, java.lang.String path)
      Register a font by giving explicitly the font family and name.
      static int registerSystemFontDirectories()
      Register fonts in some probable directories.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_CACHED

        private static boolean DEFAULT_CACHED
        This is the default value of the cached variable.
    • Constructor Detail

      • FontProgramFactory

        private FontProgramFactory()
    • Method Detail

      • createFont

        public static FontProgram createFont()
                                      throws java.io.IOException
        Creates a new standard Helvetica font program file.
        Returns:
        a FontProgram object with Helvetica font description
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createFont

        public static FontProgram createFont​(java.lang.String fontProgram)
                                      throws java.io.IOException
        Creates a new font program. This font program can be one of the 14 built in fonts, a Type1 font referred to by an AFM or PFM file, a TrueType font or a CJK font from the Adobe Asian Font Pack. Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1". This would get the second font (indexes start at 0), in this case "MS PGothic".

        The fonts are cached and if they already exist they are extracted from the cache, not parsed again.

        Parameters:
        fontProgram - the name of the font or its location on file
        Returns:
        returns a new FontProgram. This font program may come from the cache
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createFont

        public static FontProgram createFont​(java.lang.String fontProgram,
                                             boolean cached)
                                      throws java.io.IOException
        Creates a new font program. This font program can be one of the 14 built in fonts, a Type1 font referred to by an AFM or PFM file, a TrueType font or a CJK font from the Adobe Asian Font Pack. Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1". This would get the second font (indexes start at 0), in this case "MS PGothic".

        The fonts are cached and if they already exist they are extracted from the cache, not parsed again.

        Parameters:
        fontProgram - the name of the font or its location on file
        cached - whether to to cache this font program after it has been loaded
        Returns:
        returns a new FontProgram. This font program may come from the cache
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createFont

        public static FontProgram createFont​(java.lang.String fontProgram,
                                             java.lang.String cmap,
                                             boolean cached)
                                      throws java.io.IOException
        Creates a new font program. This font program can be one of the 14 built in fonts, a Type1 font referred to by an AFM or PFM file, a TrueType font or a CJK font from the Adobe Asian Font Pack. Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1". This would get the second font (indexes start at 0), in this case "MS PGothic".

        The fonts are cached and if they already exist they are extracted from the cache, not parsed again.

        Parameters:
        fontProgram - the name of the font or its location on file
        cmap - CMap to convert Unicode value to CID if CJK font is used
        cached - whether to cache this font program after it has been loaded
        Returns:
        returns a new FontProgram. This font program may come from the cache
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createFont

        public static FontProgram createFont​(byte[] fontProgram)
                                      throws java.io.IOException
        Creates a new font program. This font program can be one of the 14 built in fonts, a Type1 font referred to by an AFM or PFM file, a TrueType font or a CJK font from the Adobe Asian Font Pack. Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1". This would get the second font (indexes start at 0), in this case "MS PGothic".

        The fonts are cached and if they already exist they are extracted from the cache, not parsed again.

        Parameters:
        fontProgram - the byte contents of the font program
        Returns:
        returns a new FontProgram. This font program may come from the cache
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createFont

        public static FontProgram createFont​(byte[] fontProgram,
                                             boolean cached)
                                      throws java.io.IOException
        Creates a new font program. This font program can be one of the 14 built in fonts, a Type 1 font referred to by an AFM or PFM file, a TrueType font or a CJK font from the Adobe Asian Font Pack. Fonts in TrueType Collections are addressed by index such as "msgothic.ttc,1". This would get the second font (indexes start at 0), in this case "MS PGothic".

        The fonts are cached and if they already exist they are extracted from the cache, not parsed again.

        Parameters:
        fontProgram - the byte contents of the font program
        cached - whether to to cache this font program
        Returns:
        returns a new FontProgram. This font program may come from the cache
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createFont

        private static FontProgram createFont​(java.lang.String name,
                                              java.lang.String cmap,
                                              byte[] fontProgram,
                                              boolean cached)
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • createType1Font

        public static FontProgram createType1Font​(byte[] afm,
                                                  byte[] pfb)
                                           throws java.io.IOException
        Creates a new Type 1 font by the byte contents of the corresponding AFM/PFM and PFB files
        Parameters:
        afm - the contents of the AFM or PFM metrics file
        pfb - the contents of the PFB file
        Returns:
        created FontProgram instance
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createType1Font

        public static FontProgram createType1Font​(byte[] afm,
                                                  byte[] pfb,
                                                  boolean cached)
                                           throws java.io.IOException
        Creates a new Type 1 font by the byte contents of the corresponding AFM/PFM and PFB files
        Parameters:
        afm - the contents of the AFM or PFM metrics file
        pfb - the contents of the PFB file
        cached - specifies whether to cache the created FontProgram or not
        Returns:
        created FontProgram instance
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createType1Font

        public static FontProgram createType1Font​(java.lang.String metricsPath,
                                                  java.lang.String binaryPath)
                                           throws java.io.IOException
        Creates a new Type 1 font by the corresponding AFM/PFM and PFB files
        Parameters:
        metricsPath - path to the AFM or PFM metrics file
        binaryPath - path to the contents of the PFB file
        Returns:
        created FontProgram instance
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createType1Font

        public static FontProgram createType1Font​(java.lang.String metricsPath,
                                                  java.lang.String binaryPath,
                                                  boolean cached)
                                           throws java.io.IOException
        Creates a new Type 1 font by the corresponding AFM/PFM and PFB files
        Parameters:
        metricsPath - path to the AFM or PFM metrics file
        binaryPath - path to the contents of the PFB file
        cached - specifies whether to cache the created FontProgram or not
        Returns:
        created FontProgram instance
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createFont

        public static FontProgram createFont​(java.lang.String ttc,
                                             int ttcIndex,
                                             boolean cached)
                                      throws java.io.IOException
        Creates a new TrueType font program from ttc (TrueType Collection) file.
        Parameters:
        ttc - location of TrueType Collection file (*.ttc)
        ttcIndex - the index of the font file from the collection to be read
        cached - true if the font comes from the cache or is added to the cache if new, false if the font is always created new
        Returns:
        returns a new FontProgram instance. This font may come from the cache but only if cached is true, otherwise it will always be created new
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createFont

        public static FontProgram createFont​(byte[] ttc,
                                             int ttcIndex,
                                             boolean cached)
                                      throws java.io.IOException
        Creates a new TrueType font program from ttc (TrueType Collection) file bytes.
        Parameters:
        ttc - the content of a TrueType Collection file (*.ttc)
        ttcIndex - the index of the font file from the collection to be read
        cached - true if the font comes from the cache or is added to the cache if new, false if the font is always created new
        Returns:
        returns a new FontProgram instance. This font may come from the cache but only if cached is true, otherwise it will always be created new
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createRegisteredFont

        public static FontProgram createRegisteredFont​(java.lang.String fontName,
                                                       int style,
                                                       boolean cached)
                                                throws java.io.IOException
        Creates a FontProgram from the font file that has been previously registered.
        Parameters:
        fontName - either a font alias, if the font file has been registered with an alias, or just a font name otherwise
        style - the style of the font to look for. Possible values are listed in FontStyles. See FontStyles.BOLD, FontStyles.ITALIC, FontStyles.NORMAL, FontStyles.BOLDITALIC, FontStyles.UNDEFINED
        cached - whether to try to get the font program from cache
        Returns:
        created FontProgram
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createRegisteredFont

        public static FontProgram createRegisteredFont​(java.lang.String fontName,
                                                       int style)
                                                throws java.io.IOException
        Creates a FontProgram from the font file that has been previously registered.
        Parameters:
        fontName - either a font alias, if the font file has been registered with an alias, or just a font name otherwise
        style - the style of the font to look for. Possible values are listed in FontStyles. See FontStyles.BOLD, FontStyles.ITALIC, FontStyles.NORMAL, FontStyles.BOLDITALIC, FontStyles.UNDEFINED
        Returns:
        created FontProgram
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file
      • createRegisteredFont

        public static FontProgram createRegisteredFont​(java.lang.String fontName)
                                                throws java.io.IOException
        Creates a FontProgram from the font file that has been previously registered.
        Parameters:
        fontName - either a font alias, if the font file has been registered with an alias, or just a font name otherwise
        Returns:
        created FontProgram
        Throws:
        java.io.IOException - exception is thrown in case an I/O error occurs when reading the file2
      • registerFontFamily

        public static void registerFontFamily​(java.lang.String familyName,
                                              java.lang.String fullName,
                                              java.lang.String path)
        Register a font by giving explicitly the font family and name.
        Parameters:
        familyName - the font family
        fullName - the font name
        path - the font path
      • registerFont

        public static void registerFont​(java.lang.String path)
        Registers a .ttf, .otf, .afm, .pfm, or a .ttc font file. In case if TrueType Collection (.ttc), an additional parameter may be specified defining the index of the font to be registered, e.g. "path/to/font/collection.ttc,0". The index is zero-based.
        Parameters:
        path - the path to a font file
      • registerFont

        public static void registerFont​(java.lang.String path,
                                        java.lang.String alias)
        Register a font file and use an alias for the font contained in it.
        Parameters:
        path - the path to a font file
        alias - the alias you want to use for the font
      • registerFontDirectory

        public static int registerFontDirectory​(java.lang.String dir)
        Register all the fonts in a directory.
        Parameters:
        dir - the directory
        Returns:
        the number of fonts registered
      • registerSystemFontDirectories

        public static int registerSystemFontDirectories()
        Register fonts in some probable directories. It usually works in Windows, Linux and Solaris.
        Returns:
        the number of fonts registered
      • getRegisteredFonts

        public static java.util.Set<java.lang.String> getRegisteredFonts()
        Gets a set of registered font names.
        Returns:
        a set of registered fonts
      • getRegisteredFontFamilies

        public static java.util.Set<java.lang.String> getRegisteredFontFamilies()
        Gets a set of registered font names.
        Returns:
        a set of registered font families
      • isRegisteredFont

        public static boolean isRegisteredFont​(java.lang.String fontName)
        Checks if a certain font is registered.
        Parameters:
        fontName - the name of the font that has to be checked.
        Returns:
        true if the font is found
      • createType1Font

        private static FontProgram createType1Font​(java.lang.String metricsPath,
                                                   java.lang.String binaryPath,
                                                   byte[] afm,
                                                   byte[] pfb,
                                                   boolean cached)
                                            throws java.io.IOException
        Throws:
        java.io.IOException
      • createFontCacheKey

        private static FontCacheKey createFontCacheKey​(java.lang.String name,
                                                       byte[] fontProgram)
      • clearRegisteredFonts

        public static void clearRegisteredFonts()
      • clearRegisteredFontFamilies

        public static void clearRegisteredFontFamilies()
        Clears registered font cache
      • readFontBytesFromPath

        static byte[] readFontBytesFromPath​(java.lang.String path)
                                     throws java.io.IOException
        Throws:
        java.io.IOException