Basic support for OpenType/TrueType font handling & subsetting.
This module relies on fontTools for
OTF parsing and subsetting, and on HarfBuzz (via
uharfbuzz) for shaping.
- class pyhanko.pdf_utils.font.opentype.GlyphAccumulator(writer: BasePdfFileWriter, font_handle, font_size, features=None, ot_language_tag=None, ot_script_tag=None, writing_direction=None, bcp47_lang_code=None, obj_stream=None)
Utility to collect & measure glyphs from OpenType/TrueType fonts.
writer – A PDF writer.
font_handle – File-like object
Font size in pt units.
This is only relevant for some positioning intricacies (or hacks, depending on your perspective) that may not matter for your use case.
features – Features to use. If
None, use HarfBuzz defaults.
ot_script_tag – OpenType script tag to use. Will be guessed by HarfBuzz if not specified.
ot_language_tag – OpenType language tag to use. Defaults to the default language system for the current script.
writing_direction – Writing direction, one of ‘ltr’, ‘rtl’, ‘ttb’ or ‘btt’. Will be guessed by HarfBuzz if not specified.
bcp47_lang_code – BCP 47 language code. Used to mark the text’s language in the PDF content stream, if specified.
obj_stream – Try to put font-related objects into a particular object stream, if specified.
- shape(txt: str, with_actual_text: bool = True) ShapeResult
Render a string to a format suitable for inclusion in a content stream and measure its total cursor advancement vector in em units.
txt – String to shape.
A shaping result.
This implementation of
prepare_writewill embed a subset of this glyph accumulator’s font into the PDF writer it belongs to. Said subset will include all glyphs necessary to render the strings provided to the accumulator via
Due to the way
fontToolshandles subsetting, this is a destructive operation. The in-memory representation of the original font will be overwritten by the generated subset.
- as_resource() IndirectObject
FontEngineto a PDF object suitable for embedding inside a resource dictionary.
If the PDF object is an indirect reference, the caller must not attempt to dereference it. In other words, implementations can use preallocated references to delay subsetting until the last possible moment (this is even encouraged, see
A PDF dictionary.
- class pyhanko.pdf_utils.font.opentype.GlyphAccumulatorFactory(font_file: str, font_size: int = 10, ot_script_tag: Optional[str] = None, ot_language_tag: Optional[str] = None, writing_direction: Optional[str] = None, create_objstream_if_needed: bool = True)
Stateless callable helper class to instantiate
- font_file: str
Path to the OTF/TTF font to load.
- font_size: int = 10
- ot_script_tag: str = None
OpenType script tag to use. Will be guessed by HarfBuzz if not specified.
- ot_language_tag: str = None
OpenType language tag to use. Defaults to the default language system for the current script.
- writing_direction: str = None
Writing direction, one of ‘ltr’, ‘rtl’, ‘ttb’ or ‘btt’. Will be guessed by HarfBuzz if not specified.
- create_objstream_if_needed: bool = True
Create an object stream to hold this glyph accumulator’s assets if no object stream is passed in, and the writer supports object streams.