foundrytools.core package

foundrytools.core.font module

class foundrytools.core.font.Font(font_source: str | Path | BytesIO | TTFont, lazy: bool | None = None, recalc_bboxes: bool = True, recalc_timestamp: bool = False)[source]

Bases: object

The Font class is a high-level wrapper around the TTFont class from the fontTools library, providing a user-friendly interface for working with font files and their data.

_get_table(table_tag: str)[source]
_init_font(font_source: str | Path | BytesIO | TTFont, lazy: bool | None, recalc_bboxes: bool, recalc_timestamp: bool) None[source]
_init_from_bytesio(bytesio: BytesIO, lazy: bool | None, recalc_bboxes: bool, recalc_timestamp: bool) None[source]
_init_from_file(path: str | Path, lazy: bool | None, recalc_bboxes: bool, recalc_timestamp: bool) None[source]
_init_from_ttfont(ttfont: TTFont, lazy: bool | None, recalc_bboxes: bool, recalc_timestamp: bool) None[source]
_init_tables() None[source]

Initialize all font table attributes to None. This method sets up the initial state for each table in the font, ensuring that they are ready to be loaded when accessed.

property bytesio: BytesIO | None

A property with both getter and setter methods for the BytesIO object of the font. If the font was loaded from a BytesIO object, this property will return the BytesIO object. If the font was loaded from a file or a TTFont object, this property will return None.

Returns:

The BytesIO object of the font, if any.

Return type:

Optional[BytesIO]

calc_italic_angle(min_slant: float = 2.0) float[source]

Calculates the italic angle of a font by measuring the slant of the glyph ‘H’ or ‘uni0048’.

Parameters:

min_slant (float) – The minimum slant value to consider the font italic. Defaults to 2.0.

Returns:

The italic angle of the font.

Return type:

float

Raises:

FontError – If the font does not contain the glyph ‘H’ or ‘uni0048’ or if an error occurs while calculating the italic angle.

close() None[source]

Close the font and delete the temporary file.

correct_contours(remove_hinting: bool = True, ignore_errors: bool = True, remove_unused_subroutines: bool = True, min_area: int = 25) set[str][source]

Correct the contours of a font by removing overlaps and tiny paths and correcting the direction of contours.

This tool is an implementation of the removeOverlaps function in the fontTools library to add support for correcting contours winding and removing tiny paths.

If one or more contours are modified, the glyf or CFF table will be rebuilt. If no contours are modified, the font will remain unchanged and the method will return an empty list.

The minimum area default value, 25, is the same as afdko.checkoutlinesufo. All subpaths with a bounding box less than this area will be deleted. To prevent the deletion of small subpaths, set this value to 0.

Parameters:
  • remove_hinting (bool) – If True, remove hinting instructions from the font if one or more contours are modified. Defaults to True.

  • ignore_errors (bool) – If True, ignore skia pathops errors during the correction process. Defaults to True.

  • remove_unused_subroutines (bool) – If True, remove unused subroutines from the font. Defaults to True.

  • min_area (int) – The minimum area expressed in square units. Subpaths with a bounding box less than this area will be deleted. Defaults to 25.

Returns:

A set of glyph names that have been modified.

Return type:

set[str]

del_table(table_tag: str) bool[source]

Delete a table from the font.

Parameters:

table_tag (str) – The table tag.

desubroutinize() bool[source]

Desubroutinize the CFF table of a font.

As with the subroutinize method, a context manager is used to allow desubroutinization of WOFF and WOFF2 fonts.

Returns:

True if the font was desubroutinized successfully.

Return type:

bool

property file: Path | None

A property with both getter and setter methods for the file path of the font. If the font was loaded from a file, this property will return the file path. If the font was loaded from a BytesIO object or a TTFont object, this property will return None.

Returns:

The file path of the font, if any.

Return type:

Optional[Path]

get_file_ext() str[source]

Get the real extension of the font (e.g., .otf, .ttf, .woff, .woff2).

Returns:

The real extension of the font.

Return type:

str

get_file_path(file: Path | None = None, output_dir: Path | None = None, overwrite: bool = True, extension: str | None = None, suffix: str = '') Path[source]

Get the output file for a Font object. If output_dir is not specified, the output file will be saved in the same directory as the input file. It the output file exists and overwrite is False, file name will be incremented by adding a number preceded by ‘#’ before the extension until a non-existing file name is found. If suffix is specified, it will be appended to the file name. If the suffix is already present, it will be removed before adding it again.

Parameters:
  • file (Optional[Path]) – The file name to use for the output file.

  • output_dir (Optional[Path]) – The output directory.

  • overwrite (bool) – If True, overwrite the output file if it exists. If False, increment the file name until a non-existing file name is found.

  • extension (Optional[str]) – The extension of the output file. If not specified, the extension of the input file will be used.

  • suffix (str) – The suffix to add to the file name.

Returns:

The output file.

Return type:

Path

get_glyph_bounds(glyph_name: str) GlyphBounds[source]

Get the bounding box of a glyph.

Parameters:

glyph_name (str) – The glyph name.

Returns:

The bounding box of the glyph.

Return type:

dict[str, float]

get_glyph_bounds_many(glyph_names: set[str]) dict[str, GlyphBounds][source]

Get the bounding box of multiple glyphs.

Parameters:

glyph_names (set[str]) – A set of glyph names.

Returns:

A dictionary of glyph names and their bounding boxes.

Return type:

dict[str, GlyphBounds]

property is_ps: bool

A read-only property for checking if the font has PostScript outlines. The font has PostScript outlines if the sfntVersion attribute of the TTFont object is OTTO.

Returns:

True if the font sfntVersion is OTTO, False otherwise.

Return type:

bool

property is_sfnt: bool

A read-only property for checking if the font is an SFNT font. The font is an SFNT font if the flavor attribute of the TTFont object is None.

Returns:

True if the font flavor is None, False otherwise.

Return type:

bool

property is_static: bool

A read-only property for checking if the font is a static font. The font is a static font if the TTFont object does not have a fvar table.

Returns:

True if the font does not have a fvar table, False otherwise.

Return type:

bool

property is_tt: bool

A read-only property for checking if the font has TrueType outlines. The font has TrueType outlines if the sfntVersion attribute of the TTFont object is \\\.

Returns:

True if the font sfntVersion is \\\, False otherwise.

Return type:

bool

property is_variable: bool

A read-only property for checking if the font is a variable font. The font is a variable font if the TTFont object has a fvar table.

Returns:

True if the font has a fvar table, False otherwise.

Return type:

bool

property is_woff: bool

A read-only property for checking if the font is a WOFF font. The font is a WOFF font if the flavor attribute of the TTFont object is woff.

Returns:

True if the font flavor is woff, False otherwise.

Return type:

bool

property is_woff2: bool

A read-only property for checking if the font is a WOFF2 font. The font is a WOFF2 font if the flavor attribute of the TTFont object is woff2.

Returns:

True if the font flavor is woff2, False otherwise.

Return type:

bool

rebuild() None[source]

Rebuild the font by saving it as XML to a temporary stream and then loading it back.

reload() None[source]

Reload the font by saving it to a temporary stream and then loading it back.

remove_glyphs(glyph_names_to_remove: set[str] | None = None, glyph_ids_to_remove: set[int] | None = None) set[str][source]

Removes glyphs from the font using the fontTools subsetter.

Parameters:
  • glyph_names_to_remove (Optional[set[str]]) – A set of glyph names to remove.

  • glyph_ids_to_remove (Optional[set[int]]) – A set of glyph IDs to remove.

Returns:

A set of glyph names that were removed.

Return type:

set[str]

remove_unused_glyphs() set[str][source]

Remove glyphs that are not reachable by Unicode values or by substitution rules in the font.

Returns:

A set of glyph names that were removed.

Return type:

set[str]

rename_glyph(old_name: str, new_name: str) bool[source]

Rename a single glyph in the font.

Parameters:
  • old_name (str) – The old glyph name.

  • new_name (str) – The new glyph name.

Returns:

True if the glyph was renamed, False otherwise.

Return type:

bool

rename_glyphs(new_glyph_order: list[str]) bool[source]

Rename the glyphs in the font based on the new glyph order.

Parameters:

new_glyph_order (List[str]) – The new glyph order.

Returns:

True if the glyphs were renamed, False otherwise.

Return type:

bool

save(file: str | Path | BytesIO, reorder_tables: bool | None = True) None[source]

Save the font to a file.

Parameters:
  • file (Union[str, Path, BytesIO]) – The file path or BytesIO object to save the font to.

  • reorder_tables (Optional[bool]) – If True (the default), reorder the tables, sorting them by tag (recommended by the OpenType specification). If False, retain the original order. If None, reorder by table dependency (fastest).

scale_upm(target_upm: int) None[source]

Scale the font to the specified Units Per Em (UPM) value.

Parameters:

target_upm (int) – The target UPM value. Must be in the range 16 to 16384.

set_production_names() list[tuple[str, str]][source]

Set the production names for the glyphs in the font.

The method iterates through each glyph in the old glyph order and determines its production name based on its assigned or calculated Unicode value. If the production name is already assigned, the glyph is skipped. If the production name is different from the original glyph name and is not yet assigned, the glyph is renamed and added to the new glyph order list. Finally, the font is updated with the new glyph order, the cmap table is rebuilt, and the list of renamed glyphs is returned.

Returns:

A list of tuples containing the old and new glyph names.

Return type:

List[Tuple[str, str]]

Raises:

SetProdNamesError – If an error occurs during the process.

sort_glyphs(sort_by: Literal['unicode', 'alphabetical', 'cannedDesign'] = 'unicode') bool[source]

Reorder the glyphs based on the Unicode values, alphabetical order, or canned design order.

Parameters:

sort_by (Literal['unicode', 'alphabetical', 'cannedDesign']) – The sorting method. Can be one of the following values: ‘unicode’, ‘alphabetical’, or ‘cannedDesign’. Defaults to ‘unicode’.

Returns:

True if the glyphs were reordered, False otherwise.

Return type:

bool

subroutinize() bool[source]

Subroutinize the CFF table of a font.

A context manager is used to allow subroutinization of WOFF and WOFF2 fonts. The context manager temporarily sets the flavor of the font to ‘None’ before subroutinizing the font. Then restores the original flavor after the subroutinization process.

Returns:

True if the subroutinization process was successful.

Return type:

bool

property t_cff_: CFFTable

The CFF table wrapper.

Returns:

The loaded CFFTable.

Return type:

CFFTable

property t_cmap: CmapTable

The cmap table wrapper.

Returns:

The loaded CmapTable.

Return type:

CmapTable

property t_fvar: FvarTable

The fvar table wrapper.

Returns:

The loaded FvarTable.

Return type:

FvarTable

property t_gdef: GdefTable

The GDEF table wrapper.

Returns:

The loaded GdefTable.

Return type:

GdefTable

property t_glyf: GlyfTable

The glyf table wrapper.

Returns:

The loaded GlyfTable.

Return type:

GlyfTable

property t_gsub: GsubTable

The GSUB table wrapper.

Returns:

The loaded GsubTable.

Return type:

GsubTable

property t_head: HeadTable

The head table wrapper.

Returns:

The loaded HeadTable.

Return type:

HeadTable

property t_hhea: HheaTable

The hhea table wrapper.

Returns:

The loaded HheaTable.

Return type:

HheaTable

property t_hmtx: HmtxTable

The hmtx table wrapper.

Returns:

The loaded HmtxTable.

Return type:

HmtxTable

property t_kern: KernTable

The kern table wrapper.

Returns:

The loaded KernTable.

Return type:

KernTable

property t_name: NameTable

The name table wrapper.

Returns:

The loaded NameTable.

Return type:

NameTable

property t_os_2: OS2Table

The OS/2 table wrapper.

Returns:

The loaded OS2Table.

Return type:

OS2Table

property t_post: PostTable

The post table wrapper.

Returns:

The loaded PostTable.

Return type:

PostTable

to_otf(tolerance: float = 1.0, correct_contours: bool = True) None[source]

Converts a TrueType font to PostScript.

Parameters:
  • tolerance (float) – The tolerance value used to convert quadratic curves to cubic curves. Defaults to 1.0.

  • correct_contours (bool) – If True, correct the contours of the font by removing overlaps and tiny paths and correcting the direction of contours. Defaults to True.

Raises:

FontConversionError – If the font is already a PostScript font or if the font is a variable font.

to_sfnt() None[source]

Convert a font to SFNT.

Raises:

FontConversionError – If the font is already a SFNT font.

to_ttf(max_err: float = 1.0, reverse_direction: bool = True) None[source]

Converts a PostScript font to TrueType.

Parameters:
  • max_err (float) – The maximum error allowed when converting the font to TrueType. Defaults to 1.0.

  • reverse_direction (bool) – If True, reverse the direction of the contours. Defaults to True.

Raises:

FontConversionError – If the font is already a TrueType font or if the font is a variable font.

to_woff() None[source]

Convert a font to WOFF.

Raises:

FontConversionError – If the font is already a WOFF font.

to_woff2() None[source]

Convert a font to WOFF2.

Raises:

FontConversionError – If the font is already a WOFF2 font.

property ttfont: TTFont

A property with both getter and setter methods for the underlying TTFont object of the font.

Returns:

The TTFont object of the font.

Return type:

TTFont

exception foundrytools.core.font.FontConversionError[source]

Bases: Exception

The FontConversionError class is a custom exception class for font conversion errors.

exception foundrytools.core.font.FontError[source]

Bases: Exception

The FontError class is a custom exception class for font-related errors.

foundrytools.core.tables package