Skip to main content

FontLocator

ClassSource

FontLocator(dirs: str | PathLike[str], include_system_dirs: bool = True)
FontLocator(
dirs: Sequence[str | PathLike[str]], include_system_dirs: bool = True
)
FontLocator(dirs: None = None, include_system_dirs: bool = True)

Cross-platform font locator with fuzzy name search.

Scans system, user, and customized font directories on initialization.

Inspired by Pillow's truetype method: https://pillow.readthedocs.io/en/stable/_modules/PIL/ImageFont.html#truetype

But this locator provides more features:

  1. Fetch by name: The query of this locator is with respect to the true name of the font, not the file name. We also allow users to search names in a specific language.
  2. More efficient: The fonts are preloaded. Users do not need to iterate font files every time when querying a font.
  3. Flexibility: Provide fuzzy search and a fallback chain mechanism.
  4. More accurate: For Windows users, the user font folder is included. This folder is not considered by Pillow until now (ver 12.1.0).
  5. Customizable: Users can provide their own font folders to override system font folders.

Arguments​

Initialization.

ver. 1​

NameTypeRequired
Description
dirsstr | PathLike[str]The path to the directory defining the scope of searching the fonts.
include_system_dirsboolA Flag. Whether to search the system font directories.

ver. 2​

NameTypeRequired
Description
dirsSequence[str | PathLike[str]]Several paths to directories defining the scope of searching the fonts. The order of the given path is the priority. The former path will have higher priority than the next paths.
include_system_dirsboolA Flag. Whether to search the system font directories.

ver. 3​

NameTypeRequired
Description
dirsNoneThe scope used for searching fonts. When it is not provided, will not search any user-specified folder.
include_system_dirsboolA Flag. Whether to search the system font directories.

Methods​

from_file​

v_1: Self = FontLocator.from_file(fobj: str | PathLike[str] | IO[str])

Load the cached data as this font index list.

Requires​

NameTypeRequired
Description
fobjstr | PathLike[str] | IO[str]The path to the output file or a file-like object used for saving the file.

Returns​

NameType
Description
v_1SelfThe font locator initialized from the cached data.

query​

v_1: list[FontInfo] = self.query(
name: str, lang: str | FontLanguage = FontLanguage.en, threshold: float = 70.0
)

Query the font.

Requires​

NameTypeRequired
Description
namestrThe name used for querying the font. Usually, the name is related to the language.
langstr | FontLanguage

The language of the font name. It will limit the searching scope of the fonts. The name argument should be consistent with this specified language.

If this value is str, will be treated as the label of FontLanguage.

thresholdfloatThe threshold for the matching rate. Any matched result with a score lower than this threshold will be filtered out.

Returns​

NameType
Description
v_1list[FontInfo]

A list of queried fonts. The font will be sorted by the matching rate. If no font can be found, will return an empty list.

Will not return more than five candidates.


query_best​

v_1: FontInfo | None = self.query_best(
name: str, lang: str | FontLanguage = FontLanguage.en, threshold: float = 70.0
)

Query the best font.

Requires​

NameTypeRequired
Description
namestrThe name used for querying the font. Usually, the name is related to the language.
langstr | FontLanguage

The language of the font name. It will limit the searching scope of the fonts. The name argument should be consistent with this specified language.

If this value is str, will be treated as the label of FontLanguage.

thresholdfloatThe threshold for the matching rate. If the best matched font has a score lower than this value, will return None.

Returns​

NameType
Description
v_1FontInfo | NoneThe best matched font. If no font can be found, will return None.

query_best_with_fallbacks​

v_1: FontInfo | None = self.query_best_with_fallbacks(
name_chain: str | Sequence[str],
lang: str | FontLanguage = FontLanguage.en,
threshold: float = 70.0,
stop_threshold: float = 90.0,
)

Use a chain to query the best font.

The mechanism is similar to the fallback name list of the css font selector.

Given the number of candidates in name_chain as N, the efficiency of this method should be O(N).

Requires​

NameTypeRequired
Description
name_chainstr | Sequence[str]A chain of name fallbacks used for querying the font. This method will start from the first name in this chain, attempt to get the font, and stop until the best matched font compared to a name is found.
langstr | FontLanguage

The language of the font name. It will limit the searching scope of the fonts. The name argument should be consistent with this specified language.

If this value is str, will be treated as the label of FontLanguage.

thresholdfloatThe threshold for the matching rate. If the best matched font has a score lower than this value, will return None.
stop_thresholdfloatThe threshold for early stopping the search. During the iteration of the chain, if a searched font provides a score higher than this value, will return it and stop the search immediately.

Returns​

NameType
Description
v_1FontInfo | NoneThe result will be equivalent to running self.query for each name, and selecting the font with the highest score.

to_file​

self.to_file(fobj: str | PathLike[str] | IO[str])

Serialize this locator as a file.

Requires​

NameTypeRequired
Description
fobjstr | PathLike[str] | IO[str]The path to the output file or a file-like object used for saving the file.

Properties​

font_dirs​

font_dirs: tuple[str, ...] = self.font_dirs

The read-only searching scope of the font directories.

All folders are guaranteed to exist.

The folders are sorted by the priority. The folder appearing at first will have higher priority.


fonts​

fonts: list[FontInfo] = self.fonts

List of all indexed fonts.


lang_stats​

lang_stats: dict[str, int] = self.lang_stats

Get the count of fonts for each language.


langs​

langs: list[str] = self.langs

Get the supported language short names.


n_fonts​

n_fonts: int = self.n_fonts

Number of fonts.