Package com.day.image

Class Font


  • public final class Font
    extends AbstractFont
    The Font class is a wrapper for the com.day.image.Font class which provides the functionality to seamlessly support multiple font formats and sources. All API methods are delegated to an instance of (an extension of) the com.day.image.Font class which is setup in the constructor.

    The constructor tries a list of configured font providers to get the delegatee and falls back to the com.day.image.Font class if none of the configured font providers can provide the desired font.

    While the list of font providers and their order is configurable, the fallback to the base class is hard coded and cannot be switched of. This way it is guaranteed, that instances of this class always contain a valid delegatee.

    This class cannot be initialized if the FontHelper and/or the system com.day.cq.contentbus.Ticket are not available.

    Since:
    degu
    • Constructor Detail

      • Font

        public Font​(java.lang.String faceName,
                    int size,
                    int style)
        Creates an instance of the font wrapper with a delegatee provided by one of the font providers or the base class.

        The font providers are asked in configuration order for the given font. The first provider not returning null is taken. If none of the providers may provide the desired font, the constructor falls back to asking the base class for the font instance.

        Parameters:
        faceName - Name of the font as known to the user. This generally is not the same as the name of the font file. In fact this MUST be the name of the Font family. That is "Times New Roman" and not "Times New Roman Bold" is expected.
        size - Size in points for the font to open
        style - Style flags for the new font
        See Also:
        "The java.awt.Font javadoc page for details of this call."
      • Font

        public Font​(java.lang.String faceName,
                    int size)
        Creates a font wrapper for the given font. This constructor is the same as calling Font(String, int, int) with a style of Font.PLAIN.
        Parameters:
        faceName - Name of the font as known to the user. This generally is not the same as the name of the font file.
        size - Size in points for the font to open
        See Also:
        "The java.awt.Font javadoc page for details of this call.", Font(String, int, int)
    • Method Detail

      • getFontList

        public static java.util.List<FontListEntry> getFontList()
        Returns the list of available fonts known to all registered font providers and the com.day.image.Font class.
        Returns:
        List of fonts on the platform
      • getTextExtent

        public java.awt.geom.Rectangle2D getTextExtent​(int x,
                                                       int y,
                                                       int width,
                                                       int height,
                                                       java.lang.String text,
                                                       int align,
                                                       double cs,
                                                       int ls)
        Calculate the bounding box of the text, if it would be rendered with the rendering attributes given. The calculation will be done as if the text would be rendered in the current font object.

        If the width of the text box is set to some value other than zero, the text is broken to fit lines of the given length. The line breaking algorithm breaking algorithm breaks on whitespace (blank, tab), carriage return and linefeed (CR/LF) in any combination as well as on other characters such as hyphens.

        If the text contains carriage return and/or linefeed characters, the text is broken into several lines, regardless of whether the text would be broken because of the text box width setting.

        Specified by:
        getTextExtent in class AbstractFont
        Parameters:
        x - left edge of the text box, required
        y - top edge of the text box, required
        width - maximum width of the textbox. If 0 (or negative) the width of the bounding box is dependent of the rendering attributes
        height - maximum height of the textbox. If 0 (or negative) the width of the bounding box is dependent of the rendering attributes
        text - the text string to calculate the bounding box for
        align - alignment, rotation and TrueType attributes for the text. Use AbstractFont.ALIGN_LEFT as default value.
        cs - extra intercharacter spacing. Use 0 as default value to not add additional space.
        ls - extra line spacing. Use 0 as default value to not add additional space.
        Returns:
        returns a rectangle representing the bounding box, if the text would be rendered in this font using the given additional rendering attributes.
      • drawText

        public int drawText​(Layer layer,
                            int x,
                            int y,
                            int width,
                            int height,
                            java.lang.String text,
                            java.awt.Paint paint,
                            java.awt.Stroke stroke,
                            int align,
                            double cs,
                            int ls)
        Render the given text string in the given font to the Layer using the attributes given. Use the default values given for unspecified values.

        If the width of the text box is set to some value other than zero, the text is broken to fit lines of the given length. The line breaking algorithm breaking algorithm breaks on whitespace (blank, tab), carriage return and linefeed (CR/LF) in any combination as well as on other characters such as hyphens.

        If the text contains carriage return and/or linefeed characters, the text is broken into several lines, regardless of whether the text would be broken because of the text box width setting.

        Specified by:
        drawText in class AbstractFont
        Parameters:
        layer - the layer to draw the text into
        x - left edge of the text box, required
        y - top edge of the text box, required
        width - maximum width of the textbox. If 0 (or negative) the width of the bounding box is dependent of the rendering attributes
        height - maximum height of the textbox. If 0 (or negative) the width of the bounding box is dependent of the rendering attributes
        text - the text string to calculate the bounding box for
        paint - The Paint to use for the text drawing.
        stroke - The Stroke to use for the text drawing.
        align - alignment, rotation and TrueType attributes for the text. Use AbstractFont.ALIGN_LEFT as default value.
        cs - extra intercharacter spacing. Use 0 as default value to not add additional space.
        ls - extra line spacing. Use 0 as default value to not add additional space.
        Returns:
        the number of text lines drawn to the layer
      • getHeight

        public double getHeight()
        Returns the calculated font height in pixels.
        Specified by:
        getHeight in class AbstractFont
        Returns:
        the calculated font height in pixels.
      • getAscent

        public double getAscent()
        Returns the ascent of this Font object, which is the maximal value any glyph of this font ascends above the base line.
        Specified by:
        getAscent in class AbstractFont
        Returns:
        The ascent of this Font.
      • getDescent

        public double getDescent()
        Returns the descent of this Font object, which is the maximal value any glyph of this font descends below the base line.
        Specified by:
        getDescent in class AbstractFont
        Returns:
        The ascent of this Font.
      • canDisplay

        public boolean canDisplay​(char c)
        Checks if this Font has a glyph for the specified character.
        Specified by:
        canDisplay in class AbstractFont
        Parameters:
        c - a unicode character code
        Returns:
        true if this Font can display the character; false otherwise.
      • canDisplayUpTo

        public int canDisplayUpTo​(java.text.CharacterIterator iter,
                                  int start,
                                  int limit)
        Indicates whether or not this Font can display the specified String. For strings with Unicode encoding, it is important to know if a particular font can display the string. This method returns an offset into the String str which is the first character this Font cannot display without using the missing glyph code . If this Font can display all characters, -1 is returned.
        Specified by:
        canDisplayUpTo in class AbstractFont
        Parameters:
        iter - a CharacterIterator object
        start - the specified starting offset into the specified array of characters
        limit - the specified ending offset into the specified array of characters
        Returns:
        an offset into the String object that can be displayed by this Font.
      • canDisplayUpTo

        public int canDisplayUpTo​(java.lang.String str)
        Indicates whether or not this Font can display a specified String. For strings with Unicode encoding, it is important to know if a particular font can display the string. This method returns an offset into the String str which is the first character this Font cannot display without using the missing glyph code. If the Font can display all characters, -1 is returned.
        Specified by:
        canDisplayUpTo in class AbstractFont
        Parameters:
        str - a String object
        Returns:
        an offset into str that points to the first character in str that this Font cannot display; or -1 if this Font can display all characters in str.
      • canDisplayUpTo

        public int canDisplayUpTo​(char[] text,
                                  int start,
                                  int limit)
        Indicates whether or not this Font can display the characters in the specified text starting at start and ending at limit. This method is a convenience overload.
        Specified by:
        canDisplayUpTo in class AbstractFont
        Parameters:
        text - the specified array of characters
        start - the specified starting offset into the specified array of characters
        limit - the specified ending offset into the specified array of characters
        Returns:
        an offset into text that points to the first character in text that this Font cannot display; or -1 if this Font can display all characters in text.
      • toString

        public java.lang.String toString()
        Returns a String representation of this object containing the font name, the size and style flags.
        Specified by:
        toString in class AbstractFont
        Returns:
        A String representation of this object.