Skip to content

Commit 12d0e11

Browse files
committed
Update TTF example.
1 parent 0dee160 commit 12d0e11

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

examples/ttf.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
You will need to get this external library from PyPI:
55
66
pip install freetype-py
7-
8-
This script has known issues and may crash when the window is resized.
97
"""
8+
# To the extent possible under law, the libtcod maintainers have waived all
9+
# copyright and related or neighboring rights to this example script.
10+
# https://creativecommons.org/publicdomain/zero/1.0/
1011
from typing import Tuple
1112

1213
import freetype # type: ignore # pip install freetype-py
@@ -17,29 +18,42 @@
1718

1819

1920
def load_ttf(path: str, size: Tuple[int, int]) -> tcod.tileset.Tileset:
20-
"""Load a TTF file as a tcod tileset."""
21+
"""Load a TTF file and return a tcod Tileset.
22+
23+
`path` is the file path to the font, this can be any font supported by the
24+
FreeType library.
25+
26+
`size` is the (width, height) of the Tileset in pixels.
27+
28+
Feel free to use this function in your own code.
29+
"""
2130
ttf = freetype.Face(path)
2231
ttf.set_pixel_sizes(*size)
23-
half_advance = size[0] - (ttf.bbox.xMax - ttf.bbox.xMin) // 64
2432

2533
tileset = tcod.tileset.Tileset(*size)
2634
for codepoint, glyph_index in ttf.get_chars():
35+
# Add every glyph to the Tileset.
2736
ttf.load_glyph(glyph_index)
2837
bitmap = ttf.glyph.bitmap
2938
assert bitmap.pixel_mode == freetype.FT_PIXEL_MODE_GRAY
3039
bitmap_array = np.asarray(bitmap.buffer).reshape(
3140
(bitmap.width, bitmap.rows), order="F"
3241
)
3342
if bitmap_array.size == 0:
34-
continue
43+
continue # Skip blank glyphs.
3544
output_image = np.zeros(size, dtype=np.uint8, order="F")
3645
out_slice = output_image
37-
left = ttf.glyph.bitmap_left + half_advance
38-
top = size[1] - ttf.glyph.bitmap_top + ttf.bbox.yMin // 64
46+
47+
# Adjust the position to center this glyph on the tile.
48+
left = (size[0] - bitmap.width) // 2
49+
top = size[1] - ttf.glyph.bitmap_top + ttf.size.descender // 64
50+
51+
# `max` is used because I was too lazy to properly slice the array.
3952
out_slice = out_slice[max(0, left) :, max(0, top) :]
4053
out_slice[
4154
: bitmap_array.shape[0], : bitmap_array.shape[1]
4255
] = bitmap_array[: out_slice.shape[0], : out_slice.shape[1]]
56+
4357
tileset.set_tile(codepoint, output_image.transpose())
4458
return tileset
4559

@@ -53,8 +67,10 @@ def main() -> None:
5367
) as context:
5468
while True:
5569
console.clear()
70+
# Draw checkerboard.
5671
console.tiles_rgb["bg"][::2, ::2] = 0x20
5772
console.tiles_rgb["bg"][1::2, 1::2] = 0x20
73+
# Print ASCII characters.
5874
console.tiles_rgb["ch"][:16, :6] = np.arange(0x20, 0x80).reshape(
5975
0x10, -1, order="F"
6076
)
@@ -67,6 +83,7 @@ def main() -> None:
6783
isinstance(event, tcod.event.WindowResized)
6884
and event.type == "WINDOWSIZECHANGED"
6985
):
86+
# Resize the Tileset to match the new screen size.
7087
context.change_tileset(
7188
load_ttf(
7289
path=FONT,

0 commit comments

Comments
 (0)