concept BaseFont in category itext

appears as: BaseFont, BaseFont
iText in Action, Second Edition

This is an excerpt from Manning's book iText in Action, Second Edition.

We’ve covered a lot of ground in this chapter. You learned about Chunk objects and several—not all—of a Chunk’s attributes; you’ll discover more attributes as you read on in part 1 of this book. You’ve worked with Phrases and Paragraphs, and you’ve been introduced to the Font and BaseFont classes. You’ve made Lists containing ListItems, and you’ve discovered different ways to use separator Chunks.

We discussed two font classes in chapter 2: Font and BaseFont. You used the Font class in section 2.2.1 for the 14 standard Type 1 fonts. These fonts are supposed to be “known” by every PDF viewer and are therefore never embedded by iText. In section 2.2.2, you used the Font class in combination with BaseFont to embed fonts. You created a BaseFont object by telling iText where to find a font program; for instance, a .ttf file. In chapter 3, you needed the BaseFont class to set the font when writing text to the direct content. You’ve also learned how to measure Strings in a certain font, how to change the render mode, and so on.

In the first column of table 11.3, you can find the most important font classes in iText. You already know Font and BaseFont, but more classes are used under the hood, most of which are subclasses of BaseFont (see the subclass column).

Table 11.3. iText Font classes

iText class

Subclass

Description

Font NA This is the most simple Font class. There are different constructors to create one of the standard Type 1 font types (not embedded). You can also create a Font instance using a BaseFont object and a font size as a parameter.
BaseFont NA This is the abstract superclass; the createFont() method returns an instance of one of its subclasses, depending on the font file used.
Type1Font Yes You’ll get a Type1Font instance if you create a standard Type 1 font, or if you pass an .afm or .pfm file. Standard Type 1 fonts are never embedded; for other Type 1 fonts, it depends on the value of the embedded parameter and the presence of a .pfb file.
Type3Font Yes Type 3 fonts are special: they don’t come in files. You need to create them using PDF syntax. Type 3 fonts are always embedded.
TrueTypeFont Yes In spite of its name, this class isn’t only used for TrueType fonts (.ttf), but also for OpenType fonts with TrueType (.ttf) or Type 1 (.otf) outlines. This class will create a TrueType or a Type 1 font subtype in a PDF document.
TrueTypeFontUnicode Yes Files with extension .ttf or .otf can also result in this subclass of TrueTypeFont if you use them as composite fonts. So will files with extension .ttc. Inside the PDF, you’ll find the subtype /Type0 along with /CIDFontType2 (.ttf and .ttc files) or /CIDFontType0 (.otf files). Contrary to its superclass, TrueTypeFontUnicode ignores the embedded parameter. iText will always embed a subset of the font.
CFFFont No OpenType fonts with extension .otf use the Compact Font Format (CFF). Although creating a font using an .otf file results in an instance of TrueTypeFont, it’s the CFFFont class that does the work.
CJKFont Yes This is a special class for Chinese, Japanese, and Korean fonts for which the metrics files are shipped in a separate JAR. Using a CJK font results in a Type 0 font; the font is never embedded.

Table 11.3 covers all the file types listed in table 11.1, as well as all the font subtypes listed in table 11.2. You don’t need to address classes such as Type1Font and TrueTypeFont directly; just as you used the Image class to select the correct image type in the previous chapter, you can let the BaseFont class decide which font class applies.