Class Font
- java.lang.Object
-
- com.day.image.font.AbstractFont
-
- com.day.image.Font
-
public final class Font extends AbstractFont
TheFont
class is a wrapper for thecom.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) thecom.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 systemcom.day.cq.contentbus.Ticket
are not available.- Since:
- degu
-
-
Field Summary
-
Fields inherited from class com.day.image.font.AbstractFont
ALIGN_BASE, ALIGN_BOTTOM, ALIGN_CENTER, ALIGN_FULL, ALIGN_HBASE, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_TOP, ALIGN_VBASE, BOLD, DRAW_OUTLINE, DRAW_STRIKEOUT, DRAW_UNDERLINE, DRAWBASE, ITALIC, PLAIN, ROT180, ROT270, ROT90, ROTBASE, ROTODD, STRIKEOUT, TTANTIALIASED, TTBASE, TTFONTERLINESPACING, TTFONTERSCALE, TTHINTED, TTOVERSAMPLING, TTUNHINTED, UNDERLINE
-
-
Constructor Summary
Constructors Constructor Description Font(java.lang.String faceName, int size)
Creates a font wrapper for the given font.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.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
canDisplay(char c)
Checks if this Font has a glyph for the specified character.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.int
canDisplayUpTo(java.lang.String str)
Indicates whether or not this Font can display a specified String.int
canDisplayUpTo(java.text.CharacterIterator iter, int start, int limit)
Indicates whether or not this Font can display the specified String.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 theLayer
using the attributes given.double
getAscent()
Returns the ascent of thisFont
object, which is the maximal value any glyph of this font ascends above the base line.java.awt.Font
getAwtFont()
double
getDescent()
Returns the descent of thisFont
object, which is the maximal value any glyph of this font descends below the base line.static java.util.List<FontListEntry>
getFontList()
Returns the list of available fonts known to all registered font providers and thecom.day.image.Font
class.double
getHeight()
Returns the calculated font height in pixels.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.java.lang.String
toString()
Returns aString
representation of this object containing the font name, the size and style flags.-
Methods inherited from class com.day.image.font.AbstractFont
createFontFileName, stringToStyle, styleToDescription, styleToString
-
-
-
-
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 openstyle
- 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 callingFont(String, int, int)
with a style ofFont.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 thecom.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 classAbstractFont
- Parameters:
x
- left edge of the text box, requiredy
- top edge of the text box, requiredwidth
- maximum width of the textbox. If 0 (or negative) the width of the bounding box is dependent of the rendering attributesheight
- maximum height of the textbox. If 0 (or negative) the width of the bounding box is dependent of the rendering attributestext
- the text string to calculate the bounding box foralign
- alignment, rotation and TrueType attributes for the text. UseAbstractFont.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 theLayer
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 classAbstractFont
- Parameters:
layer
- the layer to draw the text intox
- left edge of the text box, requiredy
- top edge of the text box, requiredwidth
- maximum width of the textbox. If 0 (or negative) the width of the bounding box is dependent of the rendering attributesheight
- maximum height of the textbox. If 0 (or negative) the width of the bounding box is dependent of the rendering attributestext
- the text string to calculate the bounding box forpaint
- ThePaint
to use for the text drawing.stroke
- TheStroke
to use for the text drawing.align
- alignment, rotation and TrueType attributes for the text. UseAbstractFont.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 classAbstractFont
- Returns:
- the calculated font height in pixels.
-
getAscent
public double getAscent()
Returns the ascent of thisFont
object, which is the maximal value any glyph of this font ascends above the base line.- Specified by:
getAscent
in classAbstractFont
- Returns:
- The ascent of this
Font
.
-
getDescent
public double getDescent()
Returns the descent of thisFont
object, which is the maximal value any glyph of this font descends below the base line.- Specified by:
getDescent
in classAbstractFont
- 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 classAbstractFont
- 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 classAbstractFont
- Parameters:
iter
- a CharacterIterator objectstart
- the specified starting offset into the specified array of characterslimit
- 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 classAbstractFont
- 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 classAbstractFont
- Parameters:
text
- the specified array of charactersstart
- the specified starting offset into the specified array of characterslimit
- 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 aString
representation of this object containing the font name, the size and style flags.- Specified by:
toString
in classAbstractFont
- Returns:
- A
String
representation of this object.
-
getAwtFont
public java.awt.Font getAwtFont()
- Specified by:
getAwtFont
in classAbstractFont
-
-