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:
- 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.
- More efficient: The fonts are preloaded. Users do not need to iterate font files every time when querying a font.
- Flexibility: Provide fuzzy search and a fallback chain mechanism.
- More accurate: For Windows users, the user font folder is included. This folder is not considered by Pillow until now (ver
12.1.0). - Customizable: Users can provide their own font folders to override system font folders.
Arguments
Initialization.
ver. 1
| Name | Type | Required | |
|---|---|---|---|
dirs | str | PathLike[str] | The path to the directory defining the scope of searching the fonts. | |
include_system_dirs | bool | A Flag. Whether to search the system font directories. |
ver. 2
| Name | Type | Required | |
|---|---|---|---|
dirs | Sequence[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_dirs | bool | A Flag. Whether to search the system font directories. |
ver. 3
| Name | Type | Required | |
|---|---|---|---|
dirs | None | The scope used for searching fonts. When it is not provided, will not search any user-specified folder. | |
include_system_dirs | bool | A 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
| Name | Type | Required | |
|---|---|---|---|
fobj | str | PathLike[str] | IO[str] | The path to the output file or a file-like object used for saving the file. |
Returns
| Name | Type | |
|---|---|---|
v_1 | Self | The 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
| Name | Type | Required | |
|---|---|---|---|
name | str | The name used for querying the font. Usually, the name is related to the language. | |
lang | str | FontLanguage | The language of the font name. It will limit the searching scope of the fonts. The If this value is | |
threshold | float | The threshold for the matching rate. Any matched result with a score lower than this threshold will be filtered out. |
Returns
| Name | Type | |
|---|---|---|
v_1 | list[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
| Name | Type | Required | |
|---|---|---|---|
name | str | The name used for querying the font. Usually, the name is related to the language. | |
lang | str | FontLanguage | The language of the font name. It will limit the searching scope of the fonts. The If this value is | |
threshold | float | The threshold for the matching rate. If the best matched font has a score lower than this value, will return None. |
Returns
| Name | Type | |
|---|---|---|
v_1 | FontInfo | None | The 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
| Name | Type | Required | |
|---|---|---|---|
name_chain | str | 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. | |
lang | str | FontLanguage | The language of the font name. It will limit the searching scope of the fonts. The If this value is | |
threshold | float | The threshold for the matching rate. If the best matched font has a score lower than this value, will return None. | |
stop_threshold | float | The 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
| Name | Type | |
|---|---|---|
v_1 | FontInfo | None | The 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
| Name | Type | Required | |
|---|---|---|---|
fobj | str | 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.