foundrytools.app package
foundrytools.app.fix_empty_notdef module
- exception foundrytools.app.fix_empty_notdef.DrawNotdefCFFError[source]
Bases:
DrawNotdefErrorRaised when there is an error drawing the ‘.notdef’ glyph in a CFF font.
- exception foundrytools.app.fix_empty_notdef.DrawNotdefError[source]
Bases:
ExceptionRaised when there is an error drawing the ‘.notdef’ glyph.
- exception foundrytools.app.fix_empty_notdef.DrawNotdefTTError[source]
Bases:
DrawNotdefErrorRaised when there is an error drawing the ‘.notdef’ glyph in a TTF font.
- foundrytools.app.fix_empty_notdef.draw_notdef_cff(font: Font, width: int, height: int, thickness: int, cs_width: int | None = None) T2CharString[source]
Draws a ‘.notdef’ glyph in a CFF font.
- Parameters:
font (Font) – The
Fontobject.width (int) – The width of the ‘.notdef’ glyph.
height (int) – The height of the ‘.notdef’ glyph.
thickness (int) – The thickness of the ‘.notdef’ glyph.
cs_width (Optional[int]) – The width of the ‘.notdef’ glyph to set in the CharString.
- Returns:
The ‘.notdef’ glyph.
- Return type:
T2CharString
- foundrytools.app.fix_empty_notdef.draw_notdef_glyf(font: Font, width: int, height: int, thickness: int) Glyph[source]
Draws a ‘.notdef’ glyph in a TTF font.
- Parameters:
font (Font) – The
Fontobject.width (int) – The width of the ‘.notdef’ glyph.
height (int) – The height of the ‘.notdef’ glyph.
thickness (int) – The thickness of the ‘.notdef’ glyph.
- Returns:
The ‘.notdef’ glyph.
- Return type:
Glyph
foundrytools.app.fix_italic_angle module
- exception foundrytools.app.fix_italic_angle.FixItalicAngleError[source]
Bases:
ExceptionRaised when an error occurs while fixing the italic angle of a font.
- foundrytools.app.fix_italic_angle.run(font: Font, min_slant: float = 2.0, italic: bool = True, oblique: bool = False) dict[str, dict[str, Any]][source]
Fix the italic angle of the font.
This method calculates the italic angle of the font and updates the
postandhheatables with the new values if they differ from the current values. If the font is a PostScript font, theCFFtable is also updated. The method also updates the italic and oblique bits in theOS/2and head tables.- Parameters:
font (Font) – The
Fontto process.min_slant (float) – The minimum slant value to consider the font italic. Defaults to 2.0.
italic (bool) – If
True, set the font to italic when the italic angle is not zero. Defaults toTrue.oblique – If
True, set the font to oblique when the italic angle is not zero. Defaults toFalse.
- Returns:
A dictionary containing the old and new values of the italic angle and the run/rise values, along with a check result indicating whether the values were updated.
- Return type:
dict[str, dict[str, Any]]
foundrytools.app.fix_monospace module
- exception foundrytools.app.fix_monospace.FixMonospaceError[source]
Bases:
ExceptionRaised when an error occurs in the fix_monospace method.
- foundrytools.app.fix_monospace._get_glyph_metrics_stats(font: Font) dict[str, bool | int][source]
Returns a dict containing whether the font seems_monospaced, what’s the maximum glyph width and what’s the most common width.
For a font to be considered monospaced, if at least 80% of ASCII characters have glyphs, then at least 80% of those must have the same width, otherwise all glyphs of printable characters must have one of two widths or be zero-width.
- Parameters:
font (TTFont) – The TTFont object.
- Returns:
A dictionary containing the metrics.
- Return type:
dict[str, Union[bool, int]]
foundrytools.app.otf_autohint module
foundrytools.app.otf_check_outlines module
- exception foundrytools.app.otf_check_outlines.CheckOutlinesError[source]
Bases:
ExceptionRaised when an error occurs while checking the CFF table.
- foundrytools.app.otf_check_outlines.run(font: Font, drop_hinting_data: bool = False) bool[source]
Runs the checkoutlinesufo tool on the font.
- Parameters:
font (Font) – The font to check the outlines of.
drop_hinting_data (bool) – Whether to drop the hinting data when checking the outlines.
- Returns:
Trueif the outlines were checked,Falseotherwise.- Return type:
bool
- Raises:
CheckOutlinesError – If an error occurred while checking the outlines.
foundrytools.app.otf_dehint module
- exception foundrytools.app.otf_dehint.OTFDehintError[source]
Bases:
ExceptionRaised when an error occurs while dehinting a font.
- foundrytools.app.otf_dehint.run(font: Font, drop_hinting_data: bool = False) bool[source]
Dehint a PostScript font.
- Parameters:
font (Font) – The font to dehint.
drop_hinting_data (bool) – If
True, drop hinting data from the CFF table. Defaults toFalse.
- Returns:
A boolean indicating whether the
CFFtable has been modified.- Return type:
bool
foundrytools.app.otf_recalc_stems module
This module provides functionality to recalculate the standard horizontal and vertical stem widths (StdHW and StdVW) and the horizontal and vertical stem snap arrays (StemSnapH and StemSnapV) for OpenType font files.
The module includes the following key functions:
_get_report: Generates a report of horizontal and vertical stems for a given font file. It uses the afdko.otfautohint library to parse the font file and extract stem information. The report can optionally include stems formed by curved line segments.
_group_widths_with_neighbors: Groups report entries based on their width and proximity to neighboring widths. Entries are grouped together if their widths are within a specified maximum distance from each other. This helps in identifying clusters of similar stem widths.
_get_first_n_stems: Extracts a specified number of representative stem values from the grouped stems. It ensures that the selected values maintain a minimum difference of five units to provide optimal results, as per technical recommendations.
_sort_groups_by_count_sum and _sort_groups_by_max_count: Sort the groups of stems based on the sum of counts and the maximum count within each group, respectively. These sorting functions help in prioritizing the most significant stem groups.
run: Orchestrates the entire recalculation process. It takes the path to the font file and various parameters, generates the stem reports, groups the widths, extracts the representative stem values, and returns the new StdHW, StdVW, StemSnapH, and StemSnapV values.
The module relies on the afdko.otfautohint library for font parsing and stem extraction.
- foundrytools.app.otf_recalc_stems.run(file_path: Path, report_all_stems: bool = False, max_distance: int = 1, max_h_stems: int = 2, max_v_stems: int = 2) tuple[int, int, list[int] | None, list[int] | None][source]
Recalculates the StdHW, StdVW, StemSnapH, and StemSnapV values for a font file.
- Parameters:
file_path (Path) – The path to the font file.
report_all_stems (bool) – Include stems formed by curved line segments; by default, includes only stems formed by straight line segments.
max_distance (int) – The maximum distance between widths to consider as part of the same group.
max_h_stems (int) – The number of horizontal stem values to extract.
max_v_stems (int) – The number of vertical stem values to extract.
- Returns:
A tuple containing the new StdHW, StdVW, StemSnapH, and StemSnapV values.
- Return type:
tuple[int, int, list[int], list[int]]
foundrytools.app.otf_recalc_zones module
- foundrytools.app.otf_recalc_zones._calculate_zone(font: Font, glyph_names: list[str], min_or_max: Literal['y_min', 'y_max']) list[float][source]
Calculates the minimum and maximum vertical values for a given zone.
- Parameters:
font (Font) – The Font object.
glyph_names (list[str]) – A list of glyph names to use for calculating the zone.
min_or_max (Literal["y_min", "y_max"]) – Whether to calculate the minimum or maximum value.
- Returns:
A list containing the minimum and maximum values for the zone.
- Return type:
list[float]
- foundrytools.app.otf_recalc_zones._fix_lists_overlaps(lists: list[list[float]]) list[list[float]][source]
Fixes overlaps in a list of lists of floats.
- Parameters:
lists (list[list[float]]) – A list of lists of floats.
- Returns:
The input list with the overlaps fixed.
- Return type:
list[list[float]]
- foundrytools.app.otf_recalc_zones._fix_min_separation_limits(lists: list[list[float]], limit: int) list[list[float]][source]
Fixes the minimum separation between zones.
- Parameters:
lists (list[list[float]]) – A list of lists of floats.
limit (int) – The minimum separation between zones.
- Returns:
The input list with the minimum separation fixed.
- Return type:
list[list[float]]
- foundrytools.app.otf_recalc_zones._get_pair(counter: Counter) list[float][source]
Get the two most common elements from the given counter.
- Parameters:
counter (Counter) – The counter object containing elements and their counts.
- Returns:
List containing the pair of most common elements.
- Return type:
list[float]
- foundrytools.app.otf_recalc_zones._lists_overlaps(lists: list[list[float]]) bool[source]
Check if there are overlapping intervals in a list of lists.
- Parameters:
lists (list[list[float]]) – A list of lists, where each inner list represents an interval.
- Returns:
True if there are overlapping intervals, False otherwise.
- Return type:
bool
- foundrytools.app.otf_recalc_zones.run(font: Font, descender_glyphs: list[str] | None = None, baseline_glyphs: list[str] | None = None, x_height_glyphs: list[str] | None = None, uppercase_glyphs: list[str] | None = None, ascender_glyphs: list[str] | None = None) tuple[list[int], list[int]][source]
Recalculates the zones for a given TTFont object.
- Parameters:
font (Font) – The Font object.
descender_glyphs (Optional[list[str]]) – A list of glyph names to use for calculating the descender zone.
baseline_glyphs (Optional[list[str]]) – A list of glyph names to use for calculating the baseline zone.
x_height_glyphs (Optional[list[str]]) – A list of glyph names to use for calculating the x-height zone.
uppercase_glyphs (Optional[list[str]]) – A list of glyph names to use for calculating the uppercase zone.
ascender_glyphs (Optional[list[str]]) – A list of glyph names to use for calculating the ascender zone.
- Returns:
A tuple containing the recalculated OtherBlues and BlueValues values.
- Return type:
tuple[list[int], list[int]]
foundrytools.app.ttf_autohint module
- exception foundrytools.app.ttf_autohint.TTFAutohintError[source]
Bases:
ExceptionAn error that occurred during TrueType auto-hinting.
- foundrytools.app.ttf_autohint.run(font: Font) bool[source]
Autohint a TrueType font.
- Parameters:
font (Font) – The Font object to autohint.
- Raises:
NotImplementedError – If the font is not a TrueType font.
TTFAutohintError – If an error occurred during autohinting.
foundrytools.app.ttf_dehint module
- exception foundrytools.app.ttf_dehint.TTFDehintError[source]
Bases:
ExceptionAn error occurred while dehinting a TrueType font.
- foundrytools.app.ttf_dehint.run(font: Font) bool[source]
Dehint a TrueType font.
- Parameters:
font (Font) – The Font to dehint.
- Raises:
NotImplementedError – If the font is not a TrueType flavored
TTFDehintError – If an error occurs while dehinting the font.
foundrytools.app.var2static module
- exception foundrytools.app.var2static.BadInstanceError[source]
Bases:
ExceptionRaised if the instance is invalid.
- exception foundrytools.app.var2static.UpdateNameTableError[source]
Bases:
ExceptionRaised if the name table cannot be updated when creating a static instance.
- exception foundrytools.app.var2static.Var2StaticError[source]
Bases:
ExceptionRaised when an error occurs during the conversion of a variable font to a static font.
- foundrytools.app.var2static._remove_unused_names(static_font: Font) None[source]
The method
removeUnusedNamesis very slow. This should be enough for most cases.
- foundrytools.app.var2static.check_instance(var_font: Font, instance: NamedInstance) None[source]
Check if the instance is valid.
- Parameters:
var_font (Font) – The variable font.
instance (NamedInstance) – The named instance.
- Raises:
BadInstanceError – If the instance is invalid.
- foundrytools.app.var2static.check_update_name_table(var_font: Font) None[source]
Check if the name table can be updated when creating a static instance.
This method should be called once by third-party applications before starting the conversion process. Calling it at every iteration is not necessary and slows down the process.
- Parameters:
var_font (Font) – The variable font to check and update.
- Raises:
UpdateNameTableError – If the ‘STAT’ table, Axis Values, or named instances are missing, or if an error occurs during the creation of the static instance.
- foundrytools.app.var2static.cleanup_static_font(static_font: Font) None[source]
Clean up the static font by removing tables left by
InstantiateVariableFontand remapping the name IDs.- Parameters:
static_font (Font) – The static font to clean up.
- foundrytools.app.var2static.create_static_instance(var_font: Font, instance: NamedInstance, update_font_names: bool, overlap: int = 1) Font[source]
Create a static instance from a variable font.
- Parameters:
var_font (Font) – The variable font.
instance (NamedInstance) – A named instance with axis values.
update_font_names (bool) – If
True, update the font names in the static instance.overlap (OverlapMode) – The overlap mode. Defaults to 1 (KEEP_AND_SET_FLAGS).
- Returns:
A static instance of the font.
- Return type:
- foundrytools.app.var2static.get_existing_instance(var_font: Font, instance: NamedInstance) tuple[bool, NamedInstance][source]
Returns a named instance if the instance coordinates are the same, otherwise the custom instance.
- Parameters:
var_font (Font) – The variable font.
instance (NamedInstance) – The named instance.
- Returns:
A tuple with a boolean indicating if the instance is named and the instance object.
- Return type:
tuple[bool, NamedInstance]
- foundrytools.app.var2static.run(var_font: Font, instance: NamedInstance, update_font_names: bool = True, overlap: int = 1) tuple[Font, str][source]
Convert a variable font to a static font.
- Parameters:
var_font (Font) – The variable font to convert.
instance (NamedInstance) – The named instance to use.
update_font_names (bool) – Whether to update the font names in the name table. Defaults to True.
overlap (int) – The overlap mode. Defaults to 1 (KEEP_AND_SET_FLAGS).
- Returns:
The static font and the file stem.
- Return type:
Optional[tuple[Font, str]]