Home » Java Font

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

Modifier Field Description
static int BOLD The bold style constant.
static int CENTER_BASELINE The baseline used in ideographic scripts like Chinese, Japanese, and Korean when laying out text.
static String DIALOG A String constant for the canonical family name of the logical font “Dialog”.
static String DIALOG_INPUT A String constant for the canonical family name of the logical font “DialogInput”.
static int HANGING_BASELINE The baseline used in Devanigiri and similar scripts when laying out text.
static int ITALIC The italicized style constant.
static int LAYOUT_LEFT_TO_RIGHT A flag to layoutGlyphVector indicating that text is left-to-right as determined by Bidi analysis.
static int LAYOUT_NO_LIMIT_CONTEXT A flag to layoutGlyphVector indicating that text in the char array after the indicated limit should not be examined.
static int LAYOUT_NO_START_CONTEXT A flag to layoutGlyphVector indicating that text in the char array before the indicated start should not be examined.
static int LAYOUT_RIGHT_TO_LEFT A flag to layoutGlyphVector indicating that text is right-to-left as determined by Bidi analysis.
static String MONOSPACED A String constant for the canonical family name of the logical font “Monospaced”.
static String name The logical name of this Font, as passed to the constructor.
static int PLAIN The plain style constant.
static float pointSize The point size of this Font in float.
static int ROMAN_BASELINE The baseline used in most Roman scripts when laying out text.
static String SANS_SERIF A String constant for the canonical family name of the logical font “SansSerif”.
static String SERIF A String constant for the canonical family name of the logical font “Serif”.
protected int size The point size of this Font, rounded to integer.
protected int style The style of this Font, as passed to the constructor.
static int TRUETYPE_FONT Identify a font resource of type TRUETYPE.
static int TYPE1_FONT Identify a font resource of type TYPE1.

Font Class Constructors

Constructor Description
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

Methods Description
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


You may also like