proverbs.py

../_images/proverbs.jpg

Test for TrueType write_font_converter.

Displays what I hope are chinese proverbs in simplified chinese to test UTF-8 font support. The fonts were converted from True Type fonts using the write_font_converter.py utility.

#!/bin/sh

../../utils/write_font_converter.py \
	-s "万事起头难。熟能生巧。冰冻三尺,非一日之寒。三个臭皮匠,胜过诸葛亮。今日事,今日毕。师父领进门,修行在个人。一口吃不成胖子。欲速则不达。百闻不如一见。不入虎穴,焉得虎子。" \
	NotoSansSC-Regular.otf 20 >proverbs_20.py

../../utils/write_font_converter.py \
	-s "万事起头难。熟能生巧。冰冻三尺,非一日之寒。三个臭皮匠,胜过诸葛亮。今日事,今日毕。师父领进门,修行在个人。一口吃不成胖子。欲速则不达。百闻不如一见。不入虎穴,焉得虎子。" \
	NotoSansSC-Regular.otf 30 >proverbs_30.py

Note

This example requires the following modules:

  • st7789py

  • tft_config

  • proverbs_20

  • proverbs_30

 1"""
 2proverbs.py
 3===========
 4
 5.. figure:: ../_static/proverbs.jpg
 6    :align: center
 7
 8    Test for TrueType write_font_converter.
 9
10Displays what I hope are chinese proverbs in simplified chinese to test UTF-8 font support.
11The fonts were converted from True Type fonts using the
12:ref:`write_font_converter.py<write_font_converter>` utility.
13
14.. literalinclude:: ../../../examples/proverbs/make_proverbs_fonts.sh
15
16
17.. note:: This example requires the following modules:
18
19  .. hlist::
20    :columns: 3
21
22    - `st7789py`
23    - `tft_config`
24    - `proverbs_20`
25    - `proverbs_30`
26
27"""
28
29import utime
30import st7789py as st7789
31import tft_config
32import proverbs_20 as font20
33import proverbs_30 as font30
34
35
36tft = tft_config.config(tft_config.WIDE)
37
38
39def color_wheel(WheelPos):
40    """returns a 565 color from the given position of the color wheel"""
41    WheelPos = (255 - WheelPos) % 255
42
43    if WheelPos < 85:
44        return st7789.color565(255 - WheelPos * 3, 0, WheelPos * 3)
45
46    if WheelPos < 170:
47        WheelPos -= 85
48        return st7789.color565(0, WheelPos * 3, 255 - WheelPos * 3)
49
50    WheelPos -= 170
51    return st7789.color565(WheelPos * 3, 255 - WheelPos * 3, 0)
52
53
54def main():
55    proverbs = [
56        "万事起头难",
57        "熟能生巧",
58        "冰冻三尺,非一日之寒",
59        "三个臭皮匠,胜过诸葛亮",
60        "今日事,今日毕",
61        "师父领进门,修行在个人",
62        "一口吃不成胖子",
63        "欲速则不达",
64        "百闻不如一见",
65        "不入虎穴,焉得虎子",
66    ]
67
68    font = font20 if tft.width < 239 else font30
69    line_height = font.HEIGHT + 8
70    half_height = tft.height // 2
71    half_width = tft.width // 2
72    wheel = 0
73
74    while True:
75        for proverb in proverbs:
76            proverb_lines = proverb.split(",")
77            half_lines_height = len(proverb_lines) * line_height // 2
78
79            tft.fill(st7789.BLACK)
80
81            for count, proverb_line in enumerate(proverb_lines):
82                half_length = tft.write_width(font, proverb_line) // 2
83
84                tft.write(
85                    font,
86                    proverb_line,
87                    half_width - half_length,
88                    half_height - half_lines_height + count * line_height,
89                    color_wheel(wheel),
90                )
91
92            wheel = (wheel + 5) % 256
93
94            # pause to slow down scrolling
95            utime.sleep(5)
96
97
98main()