Module genpdf::fonts [−][src]
Expand description
Fonts, font families and a font cache.
Before you can use a font in a PDF document, you have to load the FontData
for it, either
from a file (FontData::load
) or from bytes (FontData::new
). See the rusttype
crate for the supported data formats. Use the from_files
function to load a font family
from a set of files following the default naming conventions.
The FontCache
caches all loaded fonts. A Font
is a reference to a cached font in
the FontCache
. A FontFamily
is a collection of a regular, a bold, an italic and a
bold italic font (raw data or cached).
Add fonts to a document’s font cache by calling Document::add_font_family
. This method
returns a reference to the cached data that you then can use with the Style
struct to
change the font family of an element.
There are two methods for using fonts in a PDF font: You can either embed the font data into
the PDF file. Or you can use one of the three built-in font families (Builtin
) that PDF
viewers are expected to support. You can choose between the two methods when loading the font
(from_files
, FontData::load
, FontData::new
).
If you choose a built-in font family, you still have to provide the font data so that genpdf
has access to its glyph metrics. Note that it is sufficient to use a font that is metrically
identical to the built-in font. For example, you can use the Liberation fonts instad of the
proprietary Helvetica, Times and Courier fonts.
Built-in fonts can only be used with characters that are supported by the Windows-1252 encoding.
Note: The Font
and FontFamily<Font>
structs are only valid for the
FontCache
they have been created with. If you dont use the low-level render
module
directly, only use the Document::add_font_family
method to add fonts!
Internals
There are two types of font data: A FontData
instance stores information about the glyph
metrics that is used to calculate the text size. It can be loaded at any time using the
FontData::load
and FontData::new
methods. Once the PDF document is rendered, a
printpdf::IndirectFontRef
is used to draw text in the PDF document. Before a font can be
used in a PDF document, it has to be embedded using the FontCache::load_pdf_fonts
method.
If you use the high-level interface provided by Document
to generate a PDF document, these
steps are done automatically. You only have to manually populate the font cache if you use the
low-level interface in the render
module.
Structs
Font | A reference to a font cached by a |
FontCache | Stores font data that can be referenced by a |
FontData | The data for a font that is cached by a |
FontFamily | A collection of fonts with different styles. |
Enums
Builtin | A built-in font family. |
Functions
from_files | Loads the font family at the given path with the given name. |