- Java get all font name
- Characters and Glyphs
- Physical and Logical Fonts
- Font Faces and Names
- Font and TextAttribute
- Java: How to list of all the available Java/Swing fonts
- Print / list all fonts & font family using java (example)
- Program – get or list all system fonts & font’s family using java (example)
- Output – Print all fonts & font’s family using java (example)
Java get all font name
The Font class represents fonts, which are used to render text in a visible way. A font provides the information needed to map sequences of characters to sequences of glyphs and to render sequences of glyphs on Graphics and Component objects.
Characters and Glyphs
A character is a symbol that represents an item such as a letter, a digit, or punctuation in an abstract way. For example, ‘g’ , LATIN SMALL LETTER G, is a character. A glyph is a shape used to render a character or a sequence of characters. In simple writing systems, such as Latin, typically one glyph represents one character. In general, however, characters and glyphs do not have one-to-one correspondence. For example, the character ‘á’ LATIN SMALL LETTER A WITH ACUTE, can be represented by two glyphs: one for ‘a’ and one for ‘´’. On the other hand, the two-character string «fi» can be represented by a single glyph, an «fi» ligature. In complex writing systems, such as Arabic or the South and South-East Asian writing systems, the relationship between characters and glyphs can be more complicated and involve context-dependent selection of glyphs as well as glyph reordering. A font encapsulates the collection of glyphs needed to render a selected set of characters as well as the tables needed to map sequences of characters to corresponding sequences of glyphs.
Physical and Logical Fonts
The Java Platform distinguishes between two kinds of fonts: physical fonts and logical fonts. Physical fonts are the actual font libraries containing glyph data and tables to map from character sequences to glyph sequences, using a font technology such as TrueType or PostScript Type 1. All implementations of the Java Platform must support TrueType fonts; support for other font technologies is implementation dependent. Physical fonts may use names such as Helvetica, Palatino, HonMincho, or any number of other font names. Typically, each physical font supports only a limited set of writing systems, for example, only Latin characters or only Japanese and Basic Latin. The set of available physical fonts varies between configurations. Applications that require specific fonts can bundle them and instantiate them using the createFont method. Logical fonts are the five font families defined by the Java platform which must be supported by any Java runtime environment: Serif, SansSerif, Monospaced, Dialog, and DialogInput. These logical fonts are not actual font libraries. Instead, the logical font names are mapped to physical fonts by the Java runtime environment. The mapping is implementation and usually locale dependent, so the look and the metrics provided by them vary. Typically, each logical font name maps to several physical fonts in order to cover a large range of characters. Peered AWT components, such as Label and TextField , can only use logical fonts. For a discussion of the relative advantages and disadvantages of using physical or logical fonts, see the Physical and Logical Fonts in The Java Tutorials document.
Font Faces and Names
A Font can have many faces, such as heavy, medium, oblique, gothic and regular. All of these faces have similar typographic design. There are three different names that you can get from a Font object. The logical font name is simply the name that was used to construct the font. The font face name, or just font name for short, is the name of a particular font face, like Helvetica Bold. The family name is the name of the font family that determines the typographic design across several faces, like Helvetica. The 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. As examples, Arial Bold and Courier Bold Italic are font faces. There can be several Font objects associated with a font face, each differing in size, style, transform and font features. Glyphs may not always be rendered with the requested properties (e.g, font and style) due to platform limitations such as the absence of suitable platform fonts to implement a logical font. The getAllFonts method of the GraphicsEnvironment class returns an array of all font faces available in the system. These font faces are returned as Font objects with a size of 1, identity transform and default font features. These base fonts can then be used to derive new Font objects with varying sizes, styles, transforms and font features via the deriveFont methods in this class.
Font and TextAttribute
- FOREGROUND and BACKGROUND use Paint values. The subclass Color is serializable, while GradientPaint and TexturePaint are not.
- CHAR_REPLACEMENT uses GraphicAttribute values. The subclasses ShapeGraphicAttribute and ImageGraphicAttribute are not serializable.
- INPUT_METHOD_HIGHLIGHT uses InputMethodHighlight values, which are not serializable. See InputMethodHighlight .
Clients who create custom subclasses of Paint and GraphicAttribute can make them serializable and avoid this problem. Clients who use input method highlights can convert these to the platform-specific attributes for that highlight on the current platform and set them on the Font as a workaround.
The Map -based constructor and deriveFont APIs ignore the FONT attribute, and it is not retained by the Font; the static getFont(java.util.Map) method should be used if the FONT attribute might be present. See TextAttribute.FONT for more information.
Several attributes will cause additional rendering overhead and potentially invoke layout. If a Font has such attributes, the hasLayoutAttributes() method will return true.
Note: Font rotations can cause text baselines to be rotated. In order to account for this (rare) possibility, font APIs are specified to return metrics and take parameters ‘in baseline-relative coordinates’. This maps the ‘x’ coordinate to the advance along the baseline, (positive x is forward along the baseline), and the ‘y’ coordinate to a distance along the perpendicular to the baseline at ‘x’ (positive y is 90 degrees clockwise from the baseline vector). APIs for which this is especially important are called out as having ‘baseline-relative coordinates.’
Java: How to list of all the available Java/Swing fonts
Java Fonts FAQ: How do I create a list of all the fonts available on the current platform (MacOS, Linux, Windows)?
Answer: To list all the fonts available to you in a Java application (a Java Swing application), use the GraphicsEnvironment.getLocalGraphicsEnvironment().
getAvailableFontFamilyNames() method of the GraphicsEnvironment class, which technically returns an array of all the font family names it finds on the local system.
Here’s a small but complete sample Java program that prints out all the fonts (font family names):
package com.alvinalexander.javasamples; import java.awt.GraphicsEnvironment; public class ListJavaFonts < public static void main(String[] args) < String fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); for (int i = 0; i < fonts.length; i++) < System.out.println(fonts[i]); >> >
On my macOS version 10.4.10 system, this Java fonts program results in the following output:
#GungSeo #HeadLineA #PCMyungjo #PilGi Abadi MT Condensed Extra Bold Abadi MT Condensed Light Academy Engraved LET Al Bayan American Typewriter Andale Mono Apple Casual Apple Chancery Apple LiGothic Apple LiSung Apple Symbols AppleGothic AppleMyungjo Arial Arial Black Arial Hebrew Arial Narrow Arial Rounded MT Bold Ayuthaya Baghdad Bank Gothic Baskerville Baskerville Old Face Batang Bauhaus 93 Bell MT Bernard MT Condensed BiauKai Big Caslon Bitstream Vera Sans Bitstream Vera Sans Mono Bitstream Vera Serif Blackmoor LET BlairMdITC TT Bodoni Ornaments ITC TT Bodoni SvtyTwo ITC TT Bodoni SvtyTwo OS ITC TT Bodoni SvtyTwo SC ITC TT Book Antiqua Bookman Old Style Bordeaux Roman Bold LET Bradley Hand ITC TT Braggadocio Britannic Bold Brush Script MT Calisto MT Century Century Gothic Century Schoolbook Chalkboard Charcoal CY Cochin Colonna MT Comic Sans MS Cooper Black Copperplate Copperplate Gothic Bold Copperplate Gothic Light Corsiva Hebrew Courier Courier New Cracked Curlz MT DecoType Naskh Desdemona Devanagari MT Dialog DialogInput Didot Edwardian Script ITC Engravers MT Euphemia UCAS Eurostile Footlight MT Light Futura Garamond GB18030 Bitmap Geeza Pro Geneva Geneva CY Georgia Gill Sans Gill Sans Ultra Bold Gloucester MT Extra Condensed Goudy Old Style Gujarati MT Gulim Gurmukhi MT Haettenschweiler Handwriting - Dakota Harrington Hei Helvetica Helvetica CY Helvetica Neue Herculanum Hiragino Kaku Gothic Pro Hiragino Kaku Gothic Std Hiragino Maru Gothic Pro Hiragino Mincho Pro Hoefler Text Impact Imprint MT Shadow InaiMathi Jazz LET Kai Kino MT Krungthep KufiStandardGK LiHei Pro LiSong Pro Lucida Blackletter Lucida Bright Lucida Calligraphy Lucida Fax Lucida Grande Lucida Handwriting Lucida Sans Lucida Sans Typewriter Marker Felt Matura MT Script Capitals Mistral Modern No. 20 Mona Lisa Solid ITC TT Monaco Monospaced Monotype Corsiva Monotype Sorts MS Gothic MS Mincho MS PGothic MS PMincho Mshtakan MT Extra Nadeem New Peninim MT News Gothic MT Onyx OpenSymbol Optima Osaka Palatino Papyrus Party LET Perpetua Titling MT Plantagenet Cherokee Playbill PMingLiU PortagoITC TT Princetown LET Raanana Rockwell Rockwell Extra Bold SansSerif Santa Fe LET Sathu Savoye LET SchoolHouse Cursive B SchoolHouse Printed A Serif Silom SimSun Skia Snell Roundhand Stencil STFangsong STHeiti STKaiti Stone Sans ITC TT Stone Sans Sem ITC TT STSong Symbol Synchro LET Tahoma Thonburi Times Times New Roman Trebuchet MS Type Embellishments One LET Verdana Webdings Wide Latin Wingdings Wingdings 2 Wingdings 3 Zapf Dingbats Zapfino
Print / list all fonts & font family using java (example)
Program – get or list all system fonts & font’s family using java (example)
package org.numbers; import java.awt.*; import java.util.Arrays; import java.util.concurrent.atomic.AtomicInteger; public class AvailableFonts < public static void main(String[] args) < GraphicsEnvironment gEnvironment = GraphicsEnvironment .getLocalGraphicsEnvironment(); Font[] fonts = gEnvironment.getAllFonts(); AtomicInteger atomicInteger = new AtomicInteger(1); Arrays.stream(fonts) .forEach(font ->< System.out.printf( "%d. Font Family=%s, Font Name=%s \n", atomicInteger.getAndIncrement(), font.getFamily(), font.getFontName()); >); > >
Output – Print all fonts & font’s family using java (example)
1 Font Family=Agency FB, Font Name=Agency FB 2 Font Family=Agency FB, Font Name=Agency FB Bold 3 Font Family=Algerian, Font Name=Algerian 4 Font Family=Arial, Font Name=Arial 5 Font Family=Arial Black, Font Name=Arial Black 6 Font Family=Arial, Font Name=Arial Bold 7 Font Family=Arial, Font Name=Arial Bold Italic 8 Font Family=Arial, Font Name=Arial Italic 9 Font Family=Arial Narrow, Font Name=Arial Narrow 10 Font Family=Arial Narrow, Font Name=Arial Narrow Bold 11 Font Family=Arial Narrow, Font Name=Arial Narrow Bold Italic 12 Font Family=Arial Narrow, Font Name=Arial Narrow Italic 13 Font Family=Arial Rounded MT Bold, Font Name=Arial Rounded MT Bold 14 Font Family=Baskerville Old Face, Font Name=Baskerville Old Face 15 Font Family=Bauhaus 93, Font Name=Bauhaus 93 16 Font Family=Bell MT, Font Name=Bell MT 17 Font Family=Bell MT, Font Name=Bell MT Bold 18 Font Family=Bell MT, Font Name=Bell MT Italic 19 Font Family=Berlin Sans FB, Font Name=Berlin Sans FB 20 Font Family=Berlin Sans FB, Font Name=Berlin Sans FB Bold 21 Font Family=Berlin Sans FB Demi, Font Name=Berlin Sans FB Demi Bold 22 Font Family=Bernard MT Condensed, Font Name=Bernard MT Condensed 23 Font Family=Blackadder ITC, Font Name=Blackadder ITC 24 Font Family=Bodoni MT, Font Name=Bodoni MT 25 Font Family=Bodoni MT Black, Font Name=Bodoni MT Black 26 Font Family=Bodoni MT Black, Font Name=Bodoni MT Black Italic 27 Font Family=Bodoni MT, Font Name=Bodoni MT Bold 28 Font Family=Bodoni MT, Font Name=Bodoni MT Bold Italic 29 Font Family=Bodoni MT Condensed, Font Name=Bodoni MT Condensed 30 Font Family=Bodoni MT Condensed, Font Name=Bodoni MT Condensed Bold 31 Font Family=Bodoni MT Condensed, Font Name=Bodoni MT Condensed Bold Italic 32 Font Family=Bodoni MT Condensed, Font Name=Bodoni MT Condensed Italic 33 Font Family=Bodoni MT, Font Name=Bodoni MT Italic 34 Font Family=Bodoni MT Poster Compressed, Font Name=Bodoni MT Poster Compressed 35 Font Family=Book Antiqua, Font Name=Book Antiqua 36 Font Family=Book Antiqua, Font Name=Book Antiqua Bold 37 Font Family=Book Antiqua, Font Name=Book Antiqua Bold Italic 38 Font Family=Book Antiqua, Font Name=Book Antiqua Italic 39 Font Family=Bookman Old Style, Font Name=Bookman Old Style 40 Font Family=Bookman Old Style, Font Name=Bookman Old Style Bold 41 Font Family=Bookman Old Style, Font Name=Bookman Old Style Bold Italic 42 Font Family=Bookman Old Style, Font Name=Bookman Old Style Italic 43 Font Family=Bookshelf Symbol 7, Font Name=Bookshelf Symbol 7 44 Font Family=Bradley Hand ITC, Font Name=Bradley Hand ITC 45 Font Family=Britannic Bold, Font Name=Britannic Bold 46 Font Family=Broadway, Font Name=Broadway 47 Font Family=Brush Script MT, Font Name=Brush Script MT Italic 48 Font Family=Calibri, Font Name=Calibri 49 Font Family=Calibri, Font Name=Calibri Bold 50 Font Family=Calibri, Font Name=Calibri Bold Italic ------Rest of fonts.