Java Font

In Java, Font is a class that belongs to the java.awt package. It implements the Serializable interface. FontUIResource is the direct known subclass of the Java Font class.

It represents the font that are used to render the text. In Java, there are two technical terms that are used to represent font are characters and Glyphs.

Types of Fonts in Java

There are two types of fonts in Java:

  • Physical Fonts
  • Logical Fonts

Physical Fonts

Physical fonts are actual Java font library. It contains tables that maps character sequence to glyph sequences by using the font technology such as TrueType Fonts (TTF) and PostScript Type 1 Font. Note that all implementation of Java must support TTF. Using other font technologies is implementation dependent. Physical font includes the name such as Helvetica, Palatino, HonMincho, other font names.

The property of the physical font is that it uses the limited set of writing systems such as Latin characters or only Japanese and Basic Latin characters. It may vary as to configuration changes. If any application requires a specific font, user can bundle and instantiate that font by using the createFont() method of the Java Font class.

Logical Fonts

Java defines five logical font families that are Serif, SansSerif, Monospaced, Dialog, and DialogInput. It must be supported by the JRE. Note that JRE maps the logical font names to physical font because these are not the actual font libraries. Usually, mapping implementation is locale dependent. Each logical font name map to several physical fonts in order to cover a large range of characters.

For example, AWT components such as Label and TextField uses logical fonts only.

Font Faces and Names

A font may have many faces such as heavy, regular, medium, oblique, gothic, etc. All font faces have the similar typograph design.

A Font object have three different names that are:

  • Logical font name: It is the name that is used to construct the font.
  • Font face name: It is the name of particular font face. For example, Helvetica Bold.
  • Family name: It is the name of the font family. It determines the typograph design among several faces.

The Java Font class represents an instance of a font face from a collection of font faces that are present in the system resources of the host system. Example of font faces are Arial Bold, Courier Bold Italic, etc. A font face (each differing in size, style, transform and font feature) may associate with several Font objects.

Font Class Fields

ModifierFieldDescription
static intBOLDThe bold style constant.
static intCENTER_BASELINEThe baseline used in ideographic scripts like Chinese, Japanese, and Korean when laying out text.
static StringDIALOGA String constant for the canonical family name of the logical font "Dialog".
static StringDIALOG_INPUTA String constant for the canonical family name of the logical font "DialogInput".
static intHANGING_BASELINEThe baseline used in Devanigiri and similar scripts when laying out text.
static intITALICThe italicized style constant.
static intLAYOUT_LEFT_TO_RIGHTA flag to layoutGlyphVector indicating that text is left-to-right as determined by Bidi analysis.
static intLAYOUT_NO_LIMIT_CONTEXTA flag to layoutGlyphVector indicating that text in the char array after the indicated limit should not be examined.
static intLAYOUT_NO_START_CONTEXTA flag to layoutGlyphVector indicating that text in the char array before the indicated start should not be examined.
static intLAYOUT_RIGHT_TO_LEFTA flag to layoutGlyphVector indicating that text is right-to-left as determined by Bidi analysis.
static StringMONOSPACEDA String constant for the canonical family name of the logical font "Monospaced".
static StringnameThe logical name of this Font, as passed to the constructor.
static intPLAINThe plain style constant.
static floatpointSizeThe point size of this Font in float.
static intROMAN_BASELINEThe baseline used in most Roman scripts when laying out text.
static StringSANS_SERIFA String constant for the canonical family name of the logical font "SansSerif".
static StringSERIFA String constant for the canonical family name of the logical font "Serif".
protected intsizeThe point size of this Font, rounded to integer.
protected intstyleThe style of this Font, as passed to the constructor.
static intTRUETYPE_FONTIdentify a font resource of type TRUETYPE.
static intTYPE1_FONTIdentify a font resource of type TYPE1.

Font Class Constructors

ConstructorDescription
Font(Font font)Creates a new Font from the specified font.
Font(Map<?extends AttributedCharacterIterator.Attribute,?> attributes)Creates a new Font with the specified attributes.
Font(String name, int style, int size)Creates a new Font from the specified name, style and point size.

Font Class Methods

MethodsDescription
canDisplay(char c)It checks if this Font has a glyph for the specified character.
canDisplay(int codePoint)It checks if this Font has a glyph for the specified character.
canDisplayUpTo(char[] text, int start, int limit)It indicates whether or not this Font can display the characters in the specified text starting at start and ending at limit.
canDisplayUpTo(CharacterIterator iter, int start, int limit)It indicates whether or not this Font can display the text specified by the iter starting at start and ending at limit.
canDisplayUpTo(String str)It indicates whether or not this Font can display a specified String.
createFont(int fontFormat, File fontFile)It returns a new Font using the specified font type and the specified font file.
createFont(int fontFormat, InputStream fontStream)It returns a new Font using the specified font type and input data.
createGlyphVector(FontRenderContext frc, char[] chars)It creates a GlyphVector by mapping characters to glyphs one-to-one based on the Unicode cmap in this Font.
createGlyphVector(FontRenderContext frc, CharacterIterator ci)It creates a GlyphVector by mapping the specified characters to glyphs one-to-one based on the Unicode cmap in this Font.
createGlyphVector(FontRenderContext frc, int[] glyphCodes)It creates a GlyphVector by mapping characters to glyphs one-to-one based on the Unicode cmap in this Font.
createGlyphVector(FontRenderContext frc, String str)It creates a GlyphVector by mapping characters to glyphs one-to-one based on the Unicode cmap in this Font.
decode(String str)It returns the Font that the str argument describes.
deriveFont(AffineTransform trans)It creates a new Font object by replicating the current Font object and applying a new transform to it.
deriveFont(float size)It creates a new Font object by replicating the current Font object and applying a new size to it.
deriveFont(int style)It creates a new Font object by replicating the current Font object and applying a new style to it.
deriveFont(int style, AffineTransform trans)It creates a new Font object by replicating this Font object and applying a new style and transform.
deriveFont(int style, float size)It creates a new Font object by replicating this Font object and applying a new style and size.
deriveFont(Map<? extends AttributedCharacterIterator.Attribute,?> attributes)It creates a new Font object by replicating the current Font object and applying a new set of font attributes to it.
equals(Object obj)It compares this Font object to the specified Object.
getAttributes()It returns a map of font attributes available in this Font.
getAvailableAttributes()It returns the keys of all the attributes supported by this Font.
getBaselineFor(char c)It returns the baseline appropriate for displaying this character.
getFamily()It returns the family name of this Font.
getFamily(Locale l)It returns the family name of this Font, localized for the specified locale.
getFont(Map<?extends AttributedCharacterIterator.Attribute,?> attributes)It returns a Font appropriate to the attributes.
getFont(String nm)It returns a Font object from the system properties list.
getFont(String nm, Font font)Gets the specified Font from the system properties list.
getFontName()It returns the font face name of this Font.
getFontName(Locale l)It returns the font face name of the Font, localized for the specified locale.
getItalicAngle()It returns the italic angle of this Font.
getLineMetrics(char[] chars, int beginIndex, int limit, FontRenderContext frc)It returns a LineMetrics object created with the specified arguments.
getLineMetrics(CharacterIterator ci, int beginIndex, int limit, FontRenderContext frc)It returns a LineMetrics object created with the specified arguments.
getLineMetrics(String str, FontRenderContext frc)It returns a LineMetrics object created with the specified String and FontRenderContext.
getLineMetrics(String str, int beginIndex, int limit, FontRenderContext frc)It returns a LineMetrics object created with the specified arguments.
getMaxCharBounds(FontRenderContext frc)It returns the bounds for the character with the maximum bounds as defined in the specified FontRenderContext.
getMissingGlyphCode()It returns the glyphCode which is used when this Font does not have a glyph for a specified unicode code point.
getName()It returns the logical name of this Font.
getNumGlyphs()It returns the number of glyphs in this Font.
getPeer()Font rendering is now platform independent.
getPSName()It returns the postscript name of this Font.
getSize()It returns the point size of this Font, rounded to an integer.
getSize2D()It returns the point size of this Font in float value.
getStringBounds(char[] chars, int beginIndex, int limit, FontRenderContext frc)It returns the logical bounds of the specified array of characters in the specified FontRenderContext.
getStringBounds(CharacterIterator ci, int beginIndex, int limit, FontRenderContext frc)It returns the logical bounds of the characters indexed in the specified CharacterIterator in the specified FontRenderContext.
getStringBounds(String str, FontRenderContext frc)It returns the logical bounds of the specified String in the specified FontRenderContext.
getStringBounds(String str, int beginIndex, int limit, FontRenderContext frc)It returns the logical bounds of the specified String in the specified FontRenderContext.
getStyle()It returns the style of this Font.
getTransform()It returns a copy of the transform associated with this Font.
hashCode()It returns a hashcode for this Font.
hasLayoutAttributes()It returns true if this Font contains attributes that require extra layout processing.
hasUniformLineMetrics()It checks whether or not this Font has uniform line metrics.
isBold()It indicates whether or not this Font object's style is BOLD.
isItalic()It indicates whether or not this Font object's style is ITALIC.
isPlain()It indicates whether or not this Font object's style is PLAIN.
isTransformed()It indicates whether or not this Font object has a transform that affects its size in addition to the Size attribute.
layoutGlyphVector(FontRenderContext frc, char[] text, int start, int limit, int flags)It returns a new GlyphVector object, performing full layout of the text if possible.
toString()It converts this Font object to a String representation.

Let' see an example of Java Font.

JavaFontExample.java

To execute the above program, follow the steps given below:

i. Copy and paste the above code and save the file with the name JavaFontExample.java.

ii. Create an HTM file and write the following code.

JavaFontExample.html

iii. Open the Command Prompt and execute the following commands:

Output 1:

Java Font

To get the following output, make the changes in the following statement:

Output 2:

Java Font