foundrytools.lib package
foundrytools.lib.font_finder module
- exception foundrytools.lib.font_finder.FinderError[source]
Bases:
ExceptionAn exception raised by the FontFinder class.
- class foundrytools.lib.font_finder.FinderFilter(filter_out_tt: bool = False, filter_out_ps: bool = False, filter_out_woff: bool = False, filter_out_woff2: bool = False, filter_out_sfnt: bool = False, filter_out_static: bool = False, filter_out_variable: bool = False)[source]
Bases:
objectA class that specifies which fonts to filter out when searching for fonts.
- filter_out_ps: bool = False
- filter_out_sfnt: bool = False
- filter_out_static: bool = False
- filter_out_tt: bool = False
- filter_out_variable: bool = False
- filter_out_woff: bool = False
- filter_out_woff2: bool = False
- class foundrytools.lib.font_finder.FinderOptions(recursive: bool = False, lazy: bool | None = None, recalc_bboxes: bool = True, recalc_timestamp: bool = False)[source]
Bases:
objectA class that specifies the options to pass to the FontFinder class.
- lazy: bool | None = None
- recalc_bboxes: bool = True
- recalc_timestamp: bool = False
- recursive: bool = False
- class foundrytools.lib.font_finder.FontFinder(input_path: str | Path, options: FinderOptions | None = None, filter_: FinderFilter | None = None)[source]
Bases:
objectA class that finds fonts in a given path. It can search for fonts in a directory and its subdirectories, and can also handle a single font file.
The class allows for filtering based on various criteria such as outline format (TrueType or PostScript), font variations (static or variable), and font flavor (‘woff’, ‘woff2’ or
None).The class returns a list or a generator of Font objects that meet the specified criteria.
- static _generate_filter_conditions(filter_: FinderFilter) list[tuple[bool, Callable]][source]
- find_collections() list[TTCollection][source]
Finds TTCollections in the given input path.
- Returns:
A list of
TTCollectionobjects generated by the generate_collections method.- Return type:
list[TTCollection]
- find_fonts() list[Font][source]
Finds fonts in the given input path.
- Returns:
A list of
Fontobjects generated by the generate_fonts method.- Return type:
list[Font]
- generate_collections() Generator[TTCollection, None, None][source]
Generates a sequence of
TTCollectionobjects from the files generated by the_generate_filesmethod.- Returns:
A generator yielding TTCollection objects
- Return type:
Generator[TTCollection, None, None]
- generate_fonts() Generator[Font, None, None][source]
Generates a sequence of
Fontobjects from the files generated by the_generate_filesmethod. Filters out fonts based on specified conditions in_filter_conditions.- Returns:
A generator yielding Font objects
- Return type:
Generator[Font, None, None]
foundrytools.lib.otf_builder module
- foundrytools.lib.otf_builder._get_hmtx_values(font: TTFont, charstrings: dict[str, T2CharString]) dict[str, tuple[int, int]][source]
- foundrytools.lib.otf_builder.build_otf(font: TTFont, charstrings_dict: dict[str, T2CharString], ps_name: str | None = None, font_info: dict[str, Any] | None = None, private_dict: dict[str, Any] | None = None) None[source]
Builds an OpenType-PS font with
fontTools.fontBuilder.FontBuilder.- Parameters:
font (TTFont) – The
TTFontobject.charstrings_dict (dict) – The charstrings dictionary.
ps_name (str, optional) – The PostScript name of the font. If None, it will be extracted from the font metadata.
font_info – The font info dictionary. If None, it will be extracted from the font metadata.
private_dict (dict, optional) – The private dict dictionary. If None, it will be extracted from the font metadata.
foundrytools.lib.pathops module
- foundrytools.lib.pathops.correct_cff_contours(font: TTFont, remove_hinting: bool, ignore_errors: bool, remove_unused_subroutines: bool = True, min_area: int = 25) set[str][source]
Corrects the contours of the given CFF font by removing overlaps, correcting the direction of the contours, and removing tiny paths. The function returns the list of modified glyphs.
- Parameters:
font (TTFont) – The
TTFontobject.remove_hinting (bool) – Whether to remove hinting instructions.
ignore_errors (bool) – Whether to ignore errors.
remove_unused_subroutines (bool) – Whether to remove unused subroutines. Defaults to True.
min_area (int) – The minimum area of a contour to be considered. Defaults to 25.
- Raises:
CorrectContoursError – If an error occurs while correcting the contours.
- Returns:
The list of modified glyphs.
- Return type:
set[str]
- foundrytools.lib.pathops.correct_glyf_contours(font: TTFont, remove_hinting: bool, ignore_errors: bool, min_area: int = 25) set[str][source]
Corrects the contours of the given TrueType font by removing overlaps, correcting the direction of the contours, and removing tiny paths. The function returns the list of modified glyphs.
- Parameters:
font (TTFont) – The
TTFontobject.remove_hinting (bool) – Whether to remove hinting instructions.
ignore_errors (bool) – Whether to ignore errors.
min_area (int) – The minimum area of a contour to be considered. Defaults to 25.
- Raises:
CorrectContoursError – If an error occurs while correcting the contours.
- Returns:
The list of modified glyphs.
- Return type:
set[str]
- foundrytools.lib.pathops.simplify_path(path: Path, glyph_name: str, clockwise: bool) Path[source]
Simplifies a
pathops.Pathby removing overlaps and correcting the direction of the contours.- Parameters:
path (pathops.Path) – The
pathops.Pathto simplify.glyph_name (str) – The name of the glyph.
clockwise (bool) – Whether the contours should be clockwise.
- Raises:
CorrectContoursError – If an error occurs while simplifying the path.
- Returns:
The simplified path.
- Return type:
pathops.Path
foundrytools.lib.qu2cu module
- foundrytools.lib.qu2cu.quadratics_to_cubics(font: TTFont, tolerance: float = 1.0, correct_contours: bool = True) dict[str, T2CharString][source]
Converts quadratic Bézier splines to cubic curves with a specified tolerance using the
Qu2CuPen.Optionally corrects the winding direction of the contours.
- Parameters:
font (TTFont) – The TTFont object representing the font to process.
tolerance (float) – The maximum acceptable deviation in font units when approximating the quadratic Bézier curves. Default is 1.0.
correct_contours (bool) – Whether to correct contours for overlapping. Default is True.
- Returns:
A dictionary mapping glyph names to their corresponding T2CharString objects after conversion.
- Return type:
dict[str, T2CharString]
- foundrytools.lib.qu2cu.quadratics_to_cubics_2(font: TTFont) dict[str, T2CharString][source]
Converts quadratic Bézier splines to cubic curves using the
T2CharStringPen.- Parameters:
font (TTFont) – The
TTFontobject representing the font to process.- Returns:
A dictionary mapping glyph names to their corresponding T2CharString objects after conversion.
- Return type:
dict[str, T2CharString]
foundrytools.lib.ttf_builder module
- foundrytools.lib.ttf_builder.build_ttf(font: TTFont, max_err: float = 1.0, reverse_direction: bool = True, post_format: float = 2.0) None[source]
Builds an OpenType-TT font with quadratic outlines.
- Parameters:
font (TTFont) – The input
TTFontobject.max_err (float, optional) – The maximum approximation error, measured in UPEM.
reverse_direction (bool, optional) – Whether to reverse the direction of the contours.
post_format (float, optional) – The format of the
posttable.
foundrytools.lib.unicode module
- class foundrytools.lib.unicode.UnicodeBlock(bit_number: int, first_codepoint: int, last_codepoint: int, name: str, min_os2_version: int, sub_blocks: list[UnicodeBlock] | None = None)[source]
Bases:
objectA class representing a Unicode block.
- Parameters:
bit_number (int) – The bit number of the block.
first_codepoint (int) – The first codepoint of the block.
last_codepoint (int) – The last codepoint of the block.
name (str) – The name of the block.
min_os2_version (int) – The minimum OS/2 version required for the block.
sub_blocks (Optional[list["UnicodeBlock"]]) – A list of sub-blocks.
- property size: int
Returns the size of the block.
- foundrytools.lib.unicode._cmap_from_reversed_cmap(reversed_cmap: dict[str, set[int]]) dict[int, str][source]
Rebuild the cmap from the reversed cmap. Alternative to getBestCmap.
- Parameters:
reversed_cmap (dict[str, set[int]]) – The reversed character map.
- Returns:
The character map.
- Return type:
_CharacterMap
- foundrytools.lib.unicode._get_multi_mapped_glyphs(reversed_cmap: dict[str, set[int]]) list[tuple[str, list[int]]][source]
Get the glyphs that are mapped to multiple Unicode values.
- Parameters:
reversed_cmap (dict[str, set[int]]) – The reversed character map.
- Returns:
The multi-mapped glyphs.
- Return type:
list[tuple[str, list[int]]]
- foundrytools.lib.unicode._glyph_name_from_uni_str(uni_str: str) str | None[source]
Guess the name of a glyph from its Unicode value.
- Example:
>>> _glyph_name_from_uni_str("0x0041") 'uni0041' >>> _glyph_name_from_uni_str("0x10FFFF") 'u10FFFF' >>> _glyph_name_from_uni_str("0x1F600") None
- Parameters:
uni_str (str) – The Unicode value of the glyph.
- Returns:
The name of the glyph.
- Return type:
Optional[str]
- foundrytools.lib.unicode._uni_str_from_glyph_name(glyph_name: str) str | None[source]
Guess the Unicode value of a glyph from its name. If the glyph name is not in the expected format (e.g. “uniXXXX” or “uXXXXXX”), it will return None.
- Example:
>>> _uni_str_from_glyph_name("uni0041") '0x0041' >>> _uni_str_from_glyph_name("u10FFFF") '0x10FFFF' >>> _uni_str_from_glyph_name("A") None
- Parameters:
glyph_name (str) – The name of the glyph.
- Returns:
The Unicode string of the glyph.
- Return type:
Optional[str]
- foundrytools.lib.unicode._uni_str_from_int(codepoint: int) str | None[source]
Get a Unicode string from an integer.
- Example:
>>> _uni_str_from_int(65) '0x0041' >>> _uni_str_from_int(0x10FFFF) '0x10FFFF'
- Parameters:
codepoint (int) – The codepoint to convert.
- Returns:
The Unicode string of the codepoint.
- Return type:
Optional[str]
- foundrytools.lib.unicode._uni_str_from_reversed_cmap(glyph_name: str, reversed_cmap: dict[str, set[int]]) str | None[source]
Get the Unicode value of a glyph from the reversed cmap.
- Example:
>>> _uni_str_from_reversed_cmap("A", {"A": {65}}) '0x0041'
- Parameters:
glyph_name (str) – The name of the glyph.
reversed_cmap (_ReversedCmap) – The reversed character map.
- Returns:
The Unicode string of the glyph.
- Return type:
Optional[str]
- foundrytools.lib.unicode.check_block_support(block: UnicodeBlock, found_codepoints: int, min_codepoints: int, os2_version: int, has_cmap_32: bool) bool[source]
Check if a Unicode block is supported based on the following criteria:
The number of codepoints found in the block is greater than or equal to the minimum required.
The minimum OS/2 version required for the block is satisfied.
The block is not block 57 and the font has a cmap format 32 subtable.
- Parameters:
block (UnicodeBlock) – The Unicode block to check.
found_codepoints (int) – The number of codepoints found in the block.
min_codepoints (int) – The minimum number of codepoints required for the block.
os2_version (int) – The current OS/2 version.
has_cmap_32 (bool) – Whether the font has a cmap format 32 subtable.
- Returns:
Whether the block is supported.
- Return type:
bool
- foundrytools.lib.unicode.cmap_from_glyph_names(glyphs_list: list[str]) dict[int, str][source]
Get the Unicode values for the given list of glyph names.
- Parameters:
glyphs_list (list[str]) – The list of glyph names.
- Returns:
The character map.
- Return type:
_CharacterMap
- foundrytools.lib.unicode.count_block_codepoints(block: UnicodeBlock, unicodes: set[int]) int[source]
Count the number of codepoints in a given UnicodeBlock that are present in the provided set of unicodes.
This function iterates over the range of codepoints defined by the input UnicodeBlock and counts how many of them are included in the provided set of unicodes.
- Parameters:
block (UnicodeBlock) – The UnicodeBlock object that defines the range of codepoints.
unicodes (set[int]) – A set of integers representing Unicode codepoints.
- Returns:
The number of codepoints in the UnicodeBlock that are present in the set of unicodes.
- Return type:
int
- foundrytools.lib.unicode.create_cmap_tables(subtable_format: int, platform_id: int, plat_enc_id: int, cmap: dict[int, str]) CmapSubtable[source]
Create a cmap subtable with the given parameters.
- Parameters:
subtable_format (int) – The subtable format.
platform_id (int) – The platform ID.
plat_enc_id (int) – The platform encoding ID.
cmap (_CharacterMap) – The character map.
- Returns:
The cmap subtable.
- Return type:
CmapSubtable
- foundrytools.lib.unicode.friendly_name_from_uni_str(uni_str: str) str | None[source]
Get the friendly name of a glyph from its Unicode value.
- Parameters:
uni_str (str) – The Unicode value of the glyph.
- Returns:
The friendly name of the glyph.
- Return type:
Optional[str]
- foundrytools.lib.unicode.prod_name_from_glyph_name(glyph_name: str) str | None[source]
Get the production name of a glyph from its name.
- Example:
>>> prod_name_from_glyph_name("uni0041") 'A' >>> prod_name_from_glyph_name("u10FFFF") 'u10FFFF'
- Parameters:
glyph_name (str) – The name of the glyph.
- Returns:
The production name of the glyph.
- Return type:
Optional[str]
- foundrytools.lib.unicode.production_name_from_unicode(uni_str: str) str | None[source]
Get the production name of a glyph from its Unicode value.
- Example:
>>> production_name_from_unicode("0x0041") 'A' >>> production_name_from_unicode("0x10FFFF") 'u10FFFF' >>> production_name_from_unicode("0x1F600") None
- Parameters:
uni_str (str) – The Unicode value of the glyph.
- Returns:
The production name of the glyph.
- Return type:
Optional[str]
- foundrytools.lib.unicode.setup_character_map(ttfont: TTFont, mapping: dict[int, str]) None[source]
Set up the character map for the given
TTFontobject.- Parameters:
ttfont (TTFont) – The
TTFontobject.mapping (_CharacterMap) – The character map.
- foundrytools.lib.unicode.unicode_from_glyph_name(glyph_name: str, reversed_cmap: dict[str, set[int]]) str | None[source]
Attempt to retrieve a Unicode string, using various fallback mechanisms.
- It will try to:
check if the glyph is present in the reversed cmap;
check if the Unicode value is present in the JSON file;
calculate the Unicode value from the glyph name;
- Parameters:
glyph_name (str) – The name of the glyph.
reversed_cmap (_ReversedCmap) – The reversed character map.
- Returns:
The Unicode string of the glyph.
- Return type:
Optional[str]
- foundrytools.lib.unicode.update_character_map(source_cmap: dict[int, str], target_cmap: dict[int, str]) tuple[dict[int, str], list[tuple[int, str]], list[tuple[int, str, str]]][source]
Update the target character map with the source character map.
- Parameters:
source_cmap (_CharacterMap) – The source character map.
target_cmap (_CharacterMap) – The target character map.
- Returns:
A tuple containing the updated character map, remapped and duplicate glyphs.
- Return type:
tuple[_CharacterMap, list[tuple[int, str]], list[tuple[int, str, str]]]