pyhanko.pdf_utils.font module¶
Basic support for font handling & subsetting.
This module relies on fontTools for OTF parsing and subsetting.
Warning
If/when support is added for more advanced typographical features, the
general FontEngine
interface might change.
- class pyhanko.pdf_utils.font.FontEngine¶
Bases:
object
General interface for glyph lookups and font metrics.
- measure(txt: str) → float¶
Measure the length of a string in em units.
- Parameters
txt – String to measure.
- Returns
A length in em units.
- render(txt: str)¶
Render a string to a format suitable for inclusion in a content stream.
- Parameters
txt – String to render.
- Returns
A string.
- as_resource() → pyhanko.pdf_utils.generic.DictionaryObject¶
Convert a
FontEngine
to a PDF object suitable for embedding inside a resource dictionary.- Returns
A PDF dictionary.
- class pyhanko.pdf_utils.font.SimpleFontEngine(name, avg_width)¶
Bases:
pyhanko.pdf_utils.font.FontEngine
Simplistic font engine that only works with PDF standard fonts, and does not care about font metrics. Best used with monospaced fonts such as Courier.
- static default_engine()¶
- Returns
A
FontEngine
instance representing the Courier standard font.
- render(txt)¶
Render a string to a format suitable for inclusion in a content stream.
- Parameters
txt – String to render.
- Returns
A string.
- measure(txt)¶
Measure the length of a string in em units.
- Parameters
txt – String to measure.
- Returns
A length in em units.
- as_resource()¶
Convert a
FontEngine
to a PDF object suitable for embedding inside a resource dictionary.- Returns
A PDF dictionary.
- class pyhanko.pdf_utils.font.GlyphAccumulator(tt: fontTools.ttLib.ttFont.TTFont)¶
Bases:
pyhanko.pdf_utils.font.FontEngine
Utility to collect & measure glyphs from TrueType fonts.
Warning
This utility class ignores all positioning & substition information in the font file, other than glyph width/height. In particular, features such as kerning, ligatures, complex script support and regional substitution will not work out of the box.
Warning
This functionality was only really tested with CID-keyed fonts that have a CFF table. This is good enough to offer basic support for CJK scripts, but as I am not an OTF expert, more testing is necessary.
- feed_string(txt)¶
Feed a string to this glyph accumulator.
- Parameters
txt – String to encode/measure. The glyphs used to render the string are marked for inclusion in the font subset associated with this glyph accumulator.
- Returns
Returns the CID-encoded version of the string passed in, and an estimate of the width in em units. The width computation ignores kerning, but takes the width of all characters into account.
- render(txt)¶
Render a string to a format suitable for inclusion in a content stream.
- Parameters
txt – String to render.
- Returns
A string.
- measure(txt)¶
Measure the length of a string in em units.
- Parameters
txt – String to measure.
- Returns
A length in em units.
- embed_subset(writer: pyhanko.pdf_utils.writer.BasePdfFileWriter, obj_stream=None)¶
Embed a subset of this glyph accumulator’s font into the provided PDF writer. Said subset will include all glyphs necessary to render the strings provided to the accumulator via
feed_string()
.Danger
Due to the way
fontTools
handles subsetting, this is a destructive operation. The in-memory representation of the original font will be overwritten by the generated subset.- Parameters
writer – A PDF writer.
obj_stream – If provided, write all relevant objects to the provided obj_stream. If
None
(the default), they will simply be written to the file as top-level objects.
- Returns
A reference to the embedded
/Font
object.
- as_resource()¶
Convert a
FontEngine
to a PDF object suitable for embedding inside a resource dictionary.- Returns
A PDF dictionary.
- class pyhanko.pdf_utils.font.GlyphAccumulatorFactory(font_file: str)¶
Bases:
object
Stateless callable helper class to instantiate
GlyphAccumulator
objects.- font_file: str¶
Path to the OTF/TTF font to load.