Class FontLibrary


  • public class FontLibrary
    extends java.lang.Object
    Wraps anything Font related. Should be used for getting a Font everywhere it is needed.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  FontLibrary.FontType
      FontType is used for choosing the typeface of the Font.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static float DEFAULT_UNSCALED_MAIN_FONT_SIZE  
      private static java.util.Map<java.lang.String,​java.awt.Font> fontCache
      The font cache.
      private static java.util.Map<FontLibrary.FontType,​java.lang.String> keyMap
      Conversion map for getFontKey.
      private static java.util.logging.Logger logger  
      private static java.awt.Font mainFont
      Cache for the (optional) custom main Font.
      private static float mainFontSize
      Default size, used for the main-font.
    • Constructor Summary

      Constructors 
      Constructor Description
      FontLibrary()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.awt.Font createMainFont​(java.lang.String fontName)
      Create a default Font set on initialization of the GUI.
      private static boolean displayTest​(java.awt.Font font, java.lang.String text)
      Is a given font non-null and is it able to display some optional text?
      private static java.lang.String getFontKey​(FontLibrary.FontType fontType)
      Convert a FontType to a resource key.
      static float getFontScaling()
      Gets the scaling factor for the main font.
      static java.awt.Font getMainFont()  
      static java.awt.Font getScaledFont​(java.lang.String spec)
      Get a scaled font with a simple text specification.
      static java.awt.Font getScaledFont​(java.lang.String spec, float scale, java.lang.String text)
      Get a scaled font with a simple text specification.
      static java.awt.Font getScaledFont​(java.lang.String spec, java.lang.String text)
      Get a scaled font with a simple text specification.
      private static float getScaledSize​(Size fontSize, float scaleFactor)
      Convert a font size and scale factor to float.
      static java.awt.Font getUnscaledFont​(java.lang.String spec)
      Get an unscaled font with a simple text specification.
      static java.awt.Font getUnscaledFont​(java.lang.String spec, java.lang.String text)
      Get an unscaled font with a simple text specification and optional test string.
      static void setMainFontSize​(float newMainFontSize)  
      private static java.awt.Font tryFont​(FontLibrary.FontType type, java.lang.String text)
      Try to find a basic font that of a given type that can display some text.
      • Methods inherited from class java.lang.Object

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

      • logger

        private static final java.util.logging.Logger logger
      • DEFAULT_UNSCALED_MAIN_FONT_SIZE

        public static final float DEFAULT_UNSCALED_MAIN_FONT_SIZE
        See Also:
        Constant Field Values
      • mainFontSize

        private static float mainFontSize
        Default size, used for the main-font.
      • keyMap

        private static final java.util.Map<FontLibrary.FontType,​java.lang.String> keyMap
        Conversion map for getFontKey.
      • mainFont

        private static java.awt.Font mainFont
        Cache for the (optional) custom main Font.
      • fontCache

        private static final java.util.Map<java.lang.String,​java.awt.Font> fontCache
        The font cache.
    • Constructor Detail

      • FontLibrary

        public FontLibrary()
    • Method Detail

      • getFontKey

        private static java.lang.String getFontKey​(FontLibrary.FontType fontType)
        Convert a FontType to a resource key.
        Parameters:
        fontType - The FontType to convert.
        Returns:
        The resource key, or null if the main font should be used.
      • setMainFontSize

        public static void setMainFontSize​(float newMainFontSize)
      • getScaledSize

        private static float getScaledSize​(Size fontSize,
                                           float scaleFactor)
        Convert a font size and scale factor to float.
        Parameters:
        fontSize - The font size expressed as a Size.
        scaleFactor - A secondary scaling.
        Returns:
        The conversion result.
      • createMainFont

        public static java.awt.Font createMainFont​(java.lang.String fontName)
        Create a default Font set on initialization of the GUI.
        Parameters:
        fontName - Can be used to choose a different font from a user-provided name.
        Returns:
        The new Font.
      • getMainFont

        public static java.awt.Font getMainFont()
      • displayTest

        private static boolean displayTest​(java.awt.Font font,
                                           java.lang.String text)
        Is a given font non-null and is it able to display some optional text?
        Parameters:
        font - The Font to check.
        text - Optional text to that the font must be able to represent.
        Returns:
        True if the tests pass.
      • tryFont

        private static java.awt.Font tryFont​(FontLibrary.FontType type,
                                             java.lang.String text)
        Try to find a basic font that of a given type that can display some text.
        Parameters:
        type - The FontType to try.
        text - Optional text that the font must be able to represent.
        Returns:
        The Font found.
      • getUnscaledFont

        public static java.awt.Font getUnscaledFont​(java.lang.String spec)
        Get an unscaled font with a simple text specification.
        Parameters:
        spec - The font specification.
        Returns:
        The Font found.
      • getUnscaledFont

        public static java.awt.Font getUnscaledFont​(java.lang.String spec,
                                                    java.lang.String text)
        Get an unscaled font with a simple text specification and optional test string. The spec is a '-' delimited string with three parts. 1. The type, a case-independent value of the FontType enum. 2. The style, '+' delimited strings in ["plain", "bold", "italic"] 3. The (absolute) size, a case-independent name of a Size enum. This routine *should* always return a font, but it is allowed to throw exceptions if the font spec is bad. It should not take too long to find bad font specs. AFAICT the only way there can be a null result is if Font.deriveFont were to fail, which it is not documented to do. Synchronized so as to be able to use a cache.
        Parameters:
        spec - The font specification.
        text - Optional text that the font must be able to represent.
        Returns:
        The Font found.
      • getScaledFont

        public static java.awt.Font getScaledFont​(java.lang.String spec)
        Get a scaled font with a simple text specification. Beware the null return here. Callers need to handle potential failure.
        Parameters:
        spec - The font specification.
        Returns:
        The Font found, or null if scaling fails.
      • getScaledFont

        public static java.awt.Font getScaledFont​(java.lang.String spec,
                                                  java.lang.String text)
        Get a scaled font with a simple text specification. Beware the null return here. Callers need to handle potential failure.
        Parameters:
        spec - The font specification.
        text - Optional text that the font must be able to represent.
        Returns:
        The Font found, or null if scaling fails.
      • getFontScaling

        public static float getFontScaling()
        Gets the scaling factor for the main font.
        Returns:
        A scaling factor that can be used when scaling resources that should scale together with the font.
      • getScaledFont

        public static java.awt.Font getScaledFont​(java.lang.String spec,
                                                  float scale,
                                                  java.lang.String text)
        Get a scaled font with a simple text specification. Beware the null return here. Callers need to handle potential failure.
        Parameters:
        spec - The font specification.
        scale - The font scale (in addition to that in the specification).
        text - Optional text that the font must be able to represent.
        Returns:
        The Font found, or null if scaling fails.