noto_fonts.py

../_images/noto_fonts.png

Writes the names of three Noto fonts centered on the display using the font. The fonts were converted from True Type fonts using the write_font_converter.py utility.

 1"""
 2noto_fonts.py
 3=============
 4
 5.. figure:: /_static/noto_fonts.png
 6  :align: center
 7
 8
 9Writes the names of three Noto fonts centered on the display using the font. The fonts were
10converted from True Type fonts using the write_font_converter.py utility.
11"""
12
13from machine import SPI, Pin
14import gc9a01
15import tft_config
16
17import NotoSans_32 as noto_sans
18import NotoSerif_32 as noto_serif
19import NotoSansMono_32 as noto_mono
20
21
22def main():
23    def center(font, s, row, color=gc9a01.WHITE):
24        screen = tft.width()  # get screen width
25        width = tft.write_len(font, s)  # get the width of the string
26        col = tft.width() // 2 - width // 2 if width and width < screen else 0
27        tft.write(font, s, col, row, color)  # and write the string
28
29    tft = tft_config.config(tft_config.TALL)
30
31    tft.init()
32
33    # enable display and clear screen
34    tft.init()
35    tft.fill(gc9a01.BLACK)
36
37    # center the name of the first font, using the font
38    row = 16
39    center(noto_sans, "NotoSans", row, gc9a01.RED)
40    row += noto_sans.HEIGHT
41
42    # center the name of the second font, using the font
43    center(noto_serif, "NotoSerif", row, gc9a01.GREEN)
44    row += noto_serif.HEIGHT
45
46    # center the name of the third font, using the font
47    center(noto_mono, "NotoSansMono", row, gc9a01.BLUE)
48    row += noto_mono.HEIGHT
49
50
51main()