Skip to main content

FontIndexList

ClassSource

FontIndexList()

The internal font list maintained by the FontLocator.

This list is designed for fast query.

Arguments​

No argument is needed.

Methods​

add_font​

self.add_font(font: FontInfo, names_for_query: Mapping[FontLanguage, Sequence[str]])

Add a font to this index list.

Requires​

NameTypeRequired
Description
fontFontInfoThe font information that can be used for retrieving the font.
names_for_queryMapping[FontLanguage, Sequence[str]]

All names that can be used for querying this font.

Some fonts may have more than one name for a specific language, that's because such font may have a different name compared to the full name.


from_file​

v_1: Self = FontIndexList.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 index list loaded from the file.

list_fonts_of_lang​

v_1: list[FontInfo] = self.list_fonts_of_lang(lang: str | FontLanguage)

List all fonts supporting a specific language.

Requires​

NameTypeRequired
Description
langstr | FontLanguage

The language used for searching the font name.

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

Returns​

NameType
Description
v_1list[FontInfo]A list of all fonts supporting this language.

query​

v_1: list[tuple[FontInfo, str, float]] = 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[tuple[FontInfo, str, float]]

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

Each item include:

  1. The searched font.
  2. The name that the font is used for searching.
  3. The matching rate of the searched font.

Will not return more than five candidates.


query_best​

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

Query only one 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_1tuple[FontInfo, str, float] | None

The best matched font. If no font can be found, will return None.

The returned values are

  1. The searched font.
  2. The name that the font is used for searching.
  3. The matching rate of the searched font.

to_file​

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

Serialize this index list 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​

fonts​

fonts: list[FontInfo] = self.fonts

List of all indexed fonts.


lang_stats​

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

Get the count of fonts for each language.


langs​

langs: list[FontLanguage] = self.langs

Get the supported languages.


n_fonts​

n_fonts: int = self.n_fonts

Number of fonts.