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:
objectThe
Fontclass is a high-level wrapper around theTTFontclass from the fontTools library, providing a user-friendly interface for working with font files and their data.- _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
BytesIOobject of the font. If the font was loaded from aBytesIOobject, this property will return theBytesIOobject. If the font was loaded from a file or aTTFontobject, this property will returnNone.- Returns:
The
BytesIOobject 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.
- 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
removeOverlapsfunction in thefontToolslibrary 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 toTrue.ignore_errors (bool) – If
True, ignore skia pathops errors during the correction process. Defaults toTrue.remove_unused_subroutines (bool) – If
True, remove unused subroutines from the font. Defaults toTrue.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
BytesIOobject or aTTFontobject, this property will returnNone.- 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
Fontobject. Ifoutput_diris not specified, the output file will be saved in the same directory as the input file. It the output file exists andoverwriteisFalse, file name will be incremented by adding a number preceded by ‘#’ before the extension until a non-existing file name is found. Ifsuffixis 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. IfFalse, 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
sfntVersionattribute of theTTFontobject isOTTO.- Returns:
Trueif the font sfntVersion isOTTO,Falseotherwise.- 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
flavorattribute of theTTFontobject isNone.- Returns:
Trueif the font flavor isNone,Falseotherwise.- 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
TTFontobject does not have afvartable.- Returns:
Trueif the font does not have afvartable,Falseotherwise.- 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
sfntVersionattribute of theTTFontobject is\\\.- Returns:
Trueif the font sfntVersion is\\\,Falseotherwise.- 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
TTFontobject has afvartable.- Returns:
Trueif the font has afvartable,Falseotherwise.- 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
flavorattribute of theTTFontobject iswoff.- Returns:
Trueif the font flavor iswoff,Falseotherwise.- 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
flavorattribute of theTTFontobject iswoff2.- Returns:
Trueif the font flavor iswoff2,Falseotherwise.- Return type:
bool
- rebuild() None[source]
Rebuild the font by saving it as XML 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:
Trueif the glyph was renamed,Falseotherwise.- 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:
Trueif the glyphs were renamed,Falseotherwise.- 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
BytesIOobject 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). IfFalse, retain the original order. IfNone, 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:
Trueif the glyphs were reordered,Falseotherwise.- 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
- 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 toTrue.
- 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 toTrue.
- 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
TTFontobject of the font.- Returns:
The
TTFontobject of the font.- Return type:
TTFont
foundrytools.core.tables package
- foundrytools.core.tables.cff_ module
CFFTableCFFTable._find_replace_in_font_names()CFFTable._find_replace_in_top_dict()CFFTable._restore_hinting_data()CFFTable._set_cff_font_names()CFFTable._set_top_dict_names()CFFTable.correct_contours()CFFTable.del_names()CFFTable.find_replace()CFFTable.get_hinting_data()CFFTable.private_dictCFFTable.remove_hinting()CFFTable.round_coordinates()CFFTable.set_hinting_data()CFFTable.set_names()CFFTable.tableCFFTable.top_dict
- foundrytools.core.tables.cmap module
- foundrytools.core.tables.default module
- foundrytools.core.tables.fvar module
- foundrytools.core.tables.gdef module
- foundrytools.core.tables.glyf module
- foundrytools.core.tables.gsub module
- foundrytools.core.tables.head module
- foundrytools.core.tables.hhea module
HheaTableHheaTable.advance_width_maxHheaTable.ascentHheaTable.calc_caret_slope_rise()HheaTable.calc_caret_slope_run()HheaTable.caret_offsetHheaTable.caret_slope_riseHheaTable.caret_slope_runHheaTable.descentHheaTable.is_modifiedHheaTable.line_gapHheaTable.metric_data_formatHheaTable.min_left_side_bearingHheaTable.min_right_side_bearingHheaTable.number_of_hmetricsHheaTable.run_rise_angleHheaTable.tableHheaTable.x_max_extent
- foundrytools.core.tables.hmtx module
- foundrytools.core.tables.kern module
- foundrytools.core.tables.name module
NameTableNameTable._get_name_ids_for_filter()NameTable.append_prefix_suffix()NameTable.build_full_font_name()NameTable.build_mac_names()NameTable.build_postscript_name()NameTable.build_unique_identifier()NameTable.build_version_string()NameTable.filter_names()NameTable.find_replace()NameTable.get_best_family_name()NameTable.get_best_subfamily_name()NameTable.get_debug_name()NameTable.is_modifiedNameTable.remap_name_ids()NameTable.remove_empty_names()NameTable.remove_mac_names()NameTable.remove_names()NameTable.remove_unused_names()NameTable.set_name()NameTable.strip_names()NameTable.table
- foundrytools.core.tables.os_2 module
FsSelectionInvalidOS2VersionErrorOS2TableOS2Table.bitmap_embed_onlyOS2Table.cap_heightOS2Table.codepage_rangesOS2Table.embed_levelOS2Table.is_modifiedOS2Table.max_contextOS2Table.no_subsettingOS2Table.recalc_avg_char_width()OS2Table.recalc_code_page_ranges()OS2Table.recalc_max_context()OS2Table.recalc_unicode_ranges()OS2Table.tableOS2Table.typo_ascenderOS2Table.typo_descenderOS2Table.typo_line_gapOS2Table.unicode_rangesOS2Table.upgrade()OS2Table.us_break_charOS2Table.us_default_charOS2Table.us_lower_optical_point_sizeOS2Table.us_upper_optical_point_sizeOS2Table.vendor_idOS2Table.versionOS2Table.weight_classOS2Table.width_classOS2Table.win_ascentOS2Table.win_descentOS2Table.x_height
- foundrytools.core.tables.post module