pyhanko.pdf_utils.font.opentype module
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: pyhanko.pdf_utils.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)
Bases:
pyhanko.pdf_utils.font.api.FontEngine
Utility to collect & measure glyphs from OpenType/TrueType fonts.
- Parameters
writer – A PDF writer.
font_handle – File-like object
font_size –
Font size in pt units.
Note
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.
- marked_content_property_list(txt) pyhanko.pdf_utils.generic.DictionaryObject
- shape(txt: str, with_actual_text: bool = True) pyhanko.pdf_utils.font.api.ShapeResult
Render a string to a format suitable for inclusion in a content stream and measure its total cursor advancement vector in em units.
- Parameters
txt – String to shape.
- Returns
A shaping result.
- prepare_write()
This implementation of
prepare_write
will 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 viafeed_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.
- as_resource() pyhanko.pdf_utils.generic.IndirectObject
Convert a
FontEngine
to a PDF object suitable for embedding inside a resource dictionary.Note
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
prepare_write()
).- Returns
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)
Bases:
pyhanko.pdf_utils.font.api.FontEngineFactory
Stateless callable helper class to instantiate
GlyphAccumulator
objects.- font_file: str
Path to the OTF/TTF font to load.
- font_size: int = 10
Font size.
- 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.
- create_font_engine(writer: pyhanko.pdf_utils.writer.BasePdfFileWriter, obj_stream=None) pyhanko.pdf_utils.font.opentype.GlyphAccumulator