Font Finder

This module assists you in locating a font for languages that cannot be displayed using Kivy’s default font, Roboto, from the system. You no longer need to include fonts in your app, saving valuable space.

Usage

Usage is pretty straightforward. Enumerate pre-installed fonts, then check if they are capable of rendering these languages one by one.

from kivy_garden.i18n.fontfinder import enum_pre_installed_fonts, can_render_lang

for font in enum_pre_installed_fonts():
    if can_render_lang(font, 'ja'):
        print("Found a Japanese font:", font.name)
    if can_render_lang(font, 'ko'):
        print("Found a Korean font:", font.name)

The can_render_lang() API does not offer support for all languages by default. If a language is not supported, you will need to register it first:

from kivy_garden.i18n.fontfinder import register_lang

register_lang('th', "ราชอAB")  # Thai language

for font in enum_pre_installed_fonts():
    if can_render_lang(font, 'th'):
        print("Found a Thai font:", font.name)

You may have noticed the AB in the code above. If you exclude ASCII characters, you might choose a font that cannot render them, such as a fallback font, which is probably not what you want.

API Reference

can_render_lang(font: str | Path, lang: str) bool

Whether a specified font is capable of rendering a specified lang.

from kivy_garden.i18n.fontfinder import can_render_lang as f

assert not f("Roboto", "zh")
assert not f("Roboto", "ko")
assert not f("Roboto", "ja")

assert f("NotoSerifCJK-Regular.ttc", "zh")
assert f("NotoSerifCJK-Regular.ttc", "ko")
assert f("NotoSerifCJK-Regular.ttc", "ja")

# Font that lacks ASCII characters is considered unable to render any language.
assert not f("DroidSansFallbackFull.ttf", "zh")
assert not f("DroidSansFallbackFull.ttf", "ko")
assert not f("DroidSansFallbackFull.ttf", "ja")
Raises:

UnsupportedLanguageError – if the given lang is not supported.

can_render_text(font: str | Path, text: str) bool

Whether a specified font is capable of rendering a specified text.

Parameters:

text – must consist of more than two characters without repetition.

from kivy_garden.i18n.fontfinder import can_render_text as f

# Roboto, the default font, lacks CJK characters.
assert f("Roboto", "ABC")
assert not f("Roboto", "漢字한글ひら")
assert not f("Roboto", "漢字한글ひらABC")

assert f("NotoSerifCJK-Regular.ttc", "ABC")
assert f("NotoSerifCJK-Regular.ttc", "漢字한글ひら")
assert f("NotoSerifCJK-Regular.ttc", "漢字한글ひらABC")

# Fallback-fonts may lack ASCII characters.
assert not f("DroidSansFallbackFull.ttf", "ABC")
assert f("DroidSansFallbackFull.ttf", "漢字한글ひら")
assert not f("DroidSansFallbackFull.ttf", "漢字한글ひらABC")

Note

The longer the text is, the more accurate the result will be, but the more time it would take.

default_filter(font: PurePath, suffixes=('.ttf', '.otf', '.ttc')) bool

The default filter of enum_pre_installed_fonts(). If the suffix of the font is one of .ttf, .otf and .ttc as well as its filename doesn’t contain 'fallback', this function returns True.

enum_langs() Iterator[str]

Available languages for can_render_lang().

enum_pre_installed_fonts(*, filter: ~collections.abc.Callable[[~pathlib.Path], bool] = <function default_filter>) Iterator[Path]
register_lang(lang: str, discriminant: str)

Enable a language in the can_render_lang().

register_lang('th', "ราชอAB")  # Thai language