Class FontInfo


  • public class FontInfo
    extends java.lang.Object
    The FontInfo holds font information for the layout and rendering of a fo document. This stores the list of available fonts that are setup by the renderer. The font name can be retrieved for the family style and weight.
    Currently font supported font-variant small-caps is not implemented.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private FontEventListener eventListener
      Event listener for font events
      private java.util.Map<FontTriplet,​java.util.Map<java.lang.Integer,​Font>> fontInstanceCache
      Cache for Font instances.
      private java.util.Map<java.lang.String,​Typeface> fonts
      look up a font-name to get a font (that implements FontMetrics at least)
      protected static org.apache.commons.logging.Log log
      logging instance
      private java.util.Map<FontTriplet,​java.lang.Integer> tripletPriorities
      look up a font-triplet to find its priority (only used inside addFontProperties())
      private java.util.Map<FontTriplet,​java.lang.String> triplets
      look up a font-triplet to find a font-name
      private java.util.Map<java.lang.String,​Typeface> usedFonts
      Map containing fonts that have been used
    • Constructor Summary

      Constructors 
      Constructor Description
      FontInfo()
      Main constructor
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addFontProperties​(java.lang.String name, java.lang.String[] families, java.lang.String style, int weight)
      Adds a series of new font triplets given an array of font family names.
      void addFontProperties​(java.lang.String name, java.lang.String family, java.lang.String style, int weight)
      Adds a new font triplet.
      void addFontProperties​(java.lang.String internalFontKey, FontTriplet triplet)
      Adds a new font triplet.
      void addMetrics​(java.lang.String internalFontKey, FontMetrics metrics)
      Adds font metrics for a specific font.
      static FontTriplet createFontKey​(java.lang.String family, java.lang.String style, int weight)
      Creates a key from the given strings.
      void dumpAllTripletsToSystemOut()
      Diagnostic method for logging all registered fonts to System.out.
      FontTriplet findAdjustWeight​(java.lang.String family, java.lang.String style, int weight)
      Find a font with a given family and style by trying different font weights according to the spec.
      FontTriplet[] fontLookup​(java.lang.String[] families, java.lang.String style, int weight)
      Looks up a set of fonts.
      private java.util.List<FontTriplet> fontLookup​(java.lang.String[] families, java.lang.String style, int weight, boolean substitutable)  
      FontTriplet fontLookup​(java.lang.String family, java.lang.String style, int weight)
      Lookup a font.
      private FontTriplet fontLookup​(java.lang.String family, java.lang.String style, int weight, boolean substitutable)
      Lookup a font.
      private FontTriplet fuzzyFontLookup​(java.lang.String family, java.lang.String style, int weight, FontTriplet startKey, boolean substitutable)  
      Font getFontInstance​(FontTriplet triplet, int fontSize)
      Retrieves a (possibly cached) Font instance based on a FontTriplet and a font size.
      private java.util.Map<FontTriplet,​java.util.Map<java.lang.Integer,​Font>> getFontInstanceCache()  
      Font getFontInstanceForAWTFont​(java.awt.Font awtFont)
      Returns a suitable internal font given an AWT Font instance.
      java.util.Map<java.lang.String,​Typeface> getFonts()
      Gets a Map of all registered fonts.
      java.lang.String getFontStyleFor​(java.lang.String fontName)
      Returns the font style for a particular font.
      java.util.Map<FontTriplet,​java.lang.String> getFontTriplets()
      Gets a Map of all registered font triplets.
      int getFontWeightFor​(java.lang.String fontName)
      Returns the font weight for a particular font.
      java.lang.String getInternalFontKey​(FontTriplet triplet)
      Returns the internal font key (F1, F2, F3 etc.) for a given triplet.
      FontMetrics getMetricsFor​(java.lang.String fontName)
      Returns the FontMetrics for a particular font
      FontTriplet getTripletFor​(java.lang.String fontName)
      Returns the first triplet matching the given font name.
      java.util.List<FontTriplet> getTripletsFor​(java.lang.String fontName)
      Returns all font triplet matching the given font name.
      private java.util.List<FontTriplet> getTripletsForName​(java.lang.String fontName)  
      java.util.Map<java.lang.String,​Typeface> getUsedFonts()
      This is used by the renderers to retrieve all the fonts used in the document.
      boolean hasFont​(java.lang.String family, java.lang.String style, int weight)
      Determines if a particular font is available.
      boolean isSetupValid()
      Checks if the font setup is valid (At least the ultimate fallback font must be registered.)
      private void logDuplicateFont​(FontTriplet triplet, boolean replacing, java.lang.String oldKey, int oldPriority, java.lang.String newKey, int newPriority)
      Log warning about duplicate font triplets.
      private void notifyFontReplacement​(FontTriplet replacedKey, FontTriplet newKey)  
      void notifyStrokingSVGTextAsShapes​(java.lang.String fontFamily)
      Notify listeners that the SVG text for the given font will be stroked as shapes.
      void setEventListener​(FontEventListener listener)
      Sets the font event listener that can be used to receive events about particular events in this class.
      void useFont​(java.lang.String internalName)
      Tells this class that the font with the given internal name has been used.
      • Methods inherited from class java.lang.Object

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

      • log

        protected static final org.apache.commons.logging.Log log
        logging instance
      • usedFonts

        private java.util.Map<java.lang.String,​Typeface> usedFonts
        Map containing fonts that have been used
      • triplets

        private java.util.Map<FontTriplet,​java.lang.String> triplets
        look up a font-triplet to find a font-name
      • tripletPriorities

        private java.util.Map<FontTriplet,​java.lang.Integer> tripletPriorities
        look up a font-triplet to find its priority (only used inside addFontProperties())
      • fonts

        private java.util.Map<java.lang.String,​Typeface> fonts
        look up a font-name to get a font (that implements FontMetrics at least)
      • fontInstanceCache

        private java.util.Map<FontTriplet,​java.util.Map<java.lang.Integer,​Font>> fontInstanceCache
        Cache for Font instances.
      • eventListener

        private FontEventListener eventListener
        Event listener for font events
    • Constructor Detail

      • FontInfo

        public FontInfo()
        Main constructor
    • Method Detail

      • setEventListener

        public void setEventListener​(FontEventListener listener)
        Sets the font event listener that can be used to receive events about particular events in this class.
        Parameters:
        listener - the font event listener
      • isSetupValid

        public boolean isSetupValid()
        Checks if the font setup is valid (At least the ultimate fallback font must be registered.)
        Returns:
        True if valid
      • addFontProperties

        public void addFontProperties​(java.lang.String name,
                                      java.lang.String family,
                                      java.lang.String style,
                                      int weight)
        Adds a new font triplet.
        Parameters:
        name - internal key
        family - font family name
        style - font style (normal, italic, oblique...)
        weight - font weight
      • addFontProperties

        public void addFontProperties​(java.lang.String name,
                                      java.lang.String[] families,
                                      java.lang.String style,
                                      int weight)
        Adds a series of new font triplets given an array of font family names.
        Parameters:
        name - internal key
        families - an array of font family names
        style - font style (normal, italic, oblique...)
        weight - font weight
      • addFontProperties

        public void addFontProperties​(java.lang.String internalFontKey,
                                      FontTriplet triplet)
        Adds a new font triplet.
        Parameters:
        internalFontKey - internal font key
        triplet - the font triplet to associate with the internal key
      • logDuplicateFont

        private void logDuplicateFont​(FontTriplet triplet,
                                      boolean replacing,
                                      java.lang.String oldKey,
                                      int oldPriority,
                                      java.lang.String newKey,
                                      int newPriority)
        Log warning about duplicate font triplets.
        Parameters:
        triplet - the duplicate font triplet
        replacing - true iff the new font will replace the old one
        oldKey - the old internal font name
        oldPriority - the priority of the existing font mapping
        newKey - the new internal font name
        newPriority - the priority of the duplicate font mapping
      • addMetrics

        public void addMetrics​(java.lang.String internalFontKey,
                               FontMetrics metrics)
        Adds font metrics for a specific font.
        Parameters:
        internalFontKey - internal key
        metrics - metrics to register
      • fontLookup

        private FontTriplet fontLookup​(java.lang.String family,
                                       java.lang.String style,
                                       int weight,
                                       boolean substitutable)
        Lookup a font.
        Locate the font name for a given family, style and weight. The font name can then be used as a key as it is unique for the associated document. This also adds the font to the list of used fonts.
        Parameters:
        family - font family
        style - font style
        weight - font weight
        substitutable - true if the font may be substituted with the default font if not found
        Returns:
        internal font triplet key
      • fuzzyFontLookup

        private FontTriplet fuzzyFontLookup​(java.lang.String family,
                                            java.lang.String style,
                                            int weight,
                                            FontTriplet startKey,
                                            boolean substitutable)
      • useFont

        public void useFont​(java.lang.String internalName)
        Tells this class that the font with the given internal name has been used.
        Parameters:
        internalName - the internal font name (F1, F2 etc.)
      • getFontInstanceCache

        private java.util.Map<FontTriplet,​java.util.Map<java.lang.Integer,​Font>> getFontInstanceCache()
      • getFontInstance

        public Font getFontInstance​(FontTriplet triplet,
                                    int fontSize)
        Retrieves a (possibly cached) Font instance based on a FontTriplet and a font size.
        Parameters:
        triplet - the font triplet designating the requested font
        fontSize - the font size
        Returns:
        the requested Font instance
      • getTripletsForName

        private java.util.List<FontTriplet> getTripletsForName​(java.lang.String fontName)
      • getFontInstanceForAWTFont

        public Font getFontInstanceForAWTFont​(java.awt.Font awtFont)
        Returns a suitable internal font given an AWT Font instance.
        Parameters:
        awtFont - the AWT font
        Returns:
        a best matching internal Font
      • fontLookup

        public FontTriplet fontLookup​(java.lang.String family,
                                      java.lang.String style,
                                      int weight)
        Lookup a font.
        Locate the font name for a given family, style and weight. The font name can then be used as a key as it is unique for the associated document. This also adds the font to the list of used fonts.
        Parameters:
        family - font family
        style - font style
        weight - font weight
        Returns:
        the font triplet of the font chosen
      • fontLookup

        private java.util.List<FontTriplet> fontLookup​(java.lang.String[] families,
                                                       java.lang.String style,
                                                       int weight,
                                                       boolean substitutable)
      • fontLookup

        public FontTriplet[] fontLookup​(java.lang.String[] families,
                                        java.lang.String style,
                                        int weight)
        Looks up a set of fonts.
        Locate the font name(s) for the given families, style and weight. The font name(s) can then be used as a key as they are unique for the associated document. This also adds the fonts to the list of used fonts.
        Parameters:
        families - font families (priority list)
        style - font style
        weight - font weight
        Returns:
        the set of font triplets of all supported and chosen font-families in the specified style and weight.
      • notifyFontReplacement

        private void notifyFontReplacement​(FontTriplet replacedKey,
                                           FontTriplet newKey)
      • notifyStrokingSVGTextAsShapes

        public void notifyStrokingSVGTextAsShapes​(java.lang.String fontFamily)
        Notify listeners that the SVG text for the given font will be stroked as shapes.
        Parameters:
        fontFamily - a SVG font family
      • findAdjustWeight

        public FontTriplet findAdjustWeight​(java.lang.String family,
                                            java.lang.String style,
                                            int weight)
        Find a font with a given family and style by trying different font weights according to the spec.
        Parameters:
        family - font family
        style - font style
        weight - font weight
        Returns:
        internal key
      • hasFont

        public boolean hasFont​(java.lang.String family,
                               java.lang.String style,
                               int weight)
        Determines if a particular font is available.
        Parameters:
        family - font family
        style - font style
        weight - font weight
        Returns:
        True if available
      • getInternalFontKey

        public java.lang.String getInternalFontKey​(FontTriplet triplet)
        Returns the internal font key (F1, F2, F3 etc.) for a given triplet.
        Parameters:
        triplet - the font triplet
        Returns:
        the associated internal key or null, if not found
      • createFontKey

        public static FontTriplet createFontKey​(java.lang.String family,
                                                java.lang.String style,
                                                int weight)
        Creates a key from the given strings.
        Parameters:
        family - font family
        style - font style
        weight - font weight
        Returns:
        internal key
      • getFonts

        public java.util.Map<java.lang.String,​Typeface> getFonts()
        Gets a Map of all registered fonts.
        Returns:
        a read-only Map with font key/FontMetrics pairs
      • getFontTriplets

        public java.util.Map<FontTriplet,​java.lang.String> getFontTriplets()
        Gets a Map of all registered font triplets.
        Returns:
        a Map with FontTriplet/font key pairs
      • getUsedFonts

        public java.util.Map<java.lang.String,​Typeface> getUsedFonts()
        This is used by the renderers to retrieve all the fonts used in the document. This is for embedded font or creating a list of used fonts.
        Returns:
        a read-only Map with font key/FontMetrics pairs
      • getMetricsFor

        public FontMetrics getMetricsFor​(java.lang.String fontName)
        Returns the FontMetrics for a particular font
        Parameters:
        fontName - internal key
        Returns:
        font metrics
      • getTripletsFor

        public java.util.List<FontTriplet> getTripletsFor​(java.lang.String fontName)
        Returns all font triplet matching the given font name.
        Parameters:
        fontName - The font name we are looking for
        Returns:
        A list of matching font triplets
      • getTripletFor

        public FontTriplet getTripletFor​(java.lang.String fontName)
        Returns the first triplet matching the given font name. As there may be multiple triplets matching the font name the result set is sorted first to guarantee consistent results.
        Parameters:
        fontName - The font name we are looking for
        Returns:
        The first triplet for the given font name
      • getFontStyleFor

        public java.lang.String getFontStyleFor​(java.lang.String fontName)
        Returns the font style for a particular font. There may be multiple font styles matching this font. Only the first found is returned. Searching is done on a sorted list to guarantee consistent results.
        Parameters:
        fontName - internal key
        Returns:
        font style
      • getFontWeightFor

        public int getFontWeightFor​(java.lang.String fontName)
        Returns the font weight for a particular font. There may be multiple font weights matching this font. Only the first found is returned. Searching is done on a sorted list to guarantee consistent results.
        Parameters:
        fontName - internal key
        Returns:
        font weight
      • dumpAllTripletsToSystemOut

        public void dumpAllTripletsToSystemOut()
        Diagnostic method for logging all registered fonts to System.out.