Skip to main content

Search fonts

The backend of the image processing features is pillow. By default, pillow is blind to the installed font list. We offer a feature allowing users to search fonts that are available to render texts with pillow. The font searcher will maintain a cache of available fonts. Therefore, a global font cache should be maintained to reduce the searching consumption.

1. Search available fonts​

The example in the previous section already showed how to use the FontLocator. This main cache automatically searches all available fonts when it initializes.

fontlist = stet.FontLocator()
# The name can be inexact.
font = fontlist.query_best("OpenSans-Bold")

Users can provide a list of font names. The priority will be ordered from the left to the right. When querying the font, the locator will attempt to find the first font. If it is not available, will attempt to search for the second candidate.

fontlist = stet.FontLocator()
# Any name in the candidate list can be inexact.
font = fontlist.query_best_with_fallbacks(
["Linux Libertine", "Arial", "Times New Roman", "Roboto"]
)

Using the searched font, the text can be rendered by

stet.ImageMultiLayer(width=2400, height=120).add_background().add_text(
font.get_name(),
name="text",
font=font,
font_size="h2",
color="white",
shadow_color="black",
pos_shift=(0, 10),
)
example-text1.png

example-text1.png

2. Specify font files​

Suppose that we have a font at ./fonts/Roboto-Regular.ttf. The file can be used directly to specify the font.

stet.ImageMultiLayer(width=2400, height=120).add_background().add_text(
"Roboto",
name="text",
font="./fonts/Roboto-Regular.ttf",
font_size="h2",
color="white",
shadow_color="black",
pos_shift=(0, 15),
)

It is also feasible to specify the fonts folder as a search path member of the FontLocator.

fontlist = stet.FontLocator("fonts", include_system_dirs=False)
stet.ImageMultiLayer(width=2400, height=120).add_background().add_text(
"Roboto",
name="text",
font=fontlist.query_best("Roboto"),
font_size="h2",
color="white",
shadow_color="black",
pos_shift=(0, 15),
)

These two usages produce the same image:

example-text2.png

example-text2.png

tip

Users can specify multiple paths, i.e.,

fontlist = stet.FontLocator(["path1", "path2", ...])

In this case, the paths are ordered from left to right by priority. The system font paths (if used) will have the lowest priority.

3. Query fonts with different languages​

Some fonts have multiple names under different language environments. For example, the following two queries refer to the same font:

fontlist = stet.FontLocator()
font = fontlist.query_best("LXGW Wenkai GB")

stet.ImageMultiLayer(width=2400, height=240).add_background().add_text(
font.get_name(),
name="title",
font=font,
font_size=80,
color="#ffffff",
stroke_color="black",
pos_shift=(0, -20),
).add_text(
"一川烟草,满城风絮,梅子黄时雨。",
name="comment",
font=font,
font_size=40,
color="#ffffff",
stroke_color="black",
anchor="top",
related_to="title",
rel_anchor="bottom",
pos_shift=(0, -10),
)
example-text3eng.png

example-text3eng.png

Check more details in the following example:

search_font.py