Skip to content

Commit 904bb4d

Browse files
committed
Add fonts preview generator
1 parent 97da5c4 commit 904bb4d

File tree

73 files changed

+79
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+79
-0
lines changed

res/fonts/font-preview.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python
2+
# turing-smart-screen-python - a Python system monitor and library for USB-C displays like Turing Smart Screen or XuanFang
3+
# https://github.com/mathoudebine/turing-smart-screen-python/
4+
5+
# Copyright (C) 2021-2023 Matthieu Houdebine (mathoudebine)
6+
#
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This program is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
20+
# This file generate PNG previews for available fonts
21+
22+
import os
23+
import sys
24+
25+
MIN_PYTHON = (3, 9)
26+
if sys.version_info < MIN_PYTHON:
27+
print("[ERROR] Python %s.%s or later is required." % MIN_PYTHON)
28+
try:
29+
sys.exit(0)
30+
except:
31+
os._exit(0)
32+
33+
from PIL import Image, ImageDraw, ImageFont
34+
import math
35+
from pathlib import Path
36+
37+
FONTS_DIR = str(Path(__file__).parent.resolve()) + "/"
38+
39+
40+
def generate_preview(font_dir: str, font_filename: str, text: str, font_size: int = 40,
41+
font_color: tuple[int, int, int] = (0, 0, 0),
42+
background_color: tuple[int, int, int] = (255, 255, 255)):
43+
font = os.path.join(font_dir, font_filename)
44+
45+
ttfont = ImageFont.truetype(font, font_size)
46+
text_image = Image.new(
47+
'RGB',
48+
(1000, 1000),
49+
background_color
50+
)
51+
d = ImageDraw.Draw(text_image)
52+
left, top, right, bottom = d.textbbox((0, 0), text, font=ttfont)
53+
54+
# textbbox may return float values, which is not good for the bitmap operations below.
55+
# Let's extend the bounding box to the next whole pixel in all directions
56+
left, top = math.floor(left), math.floor(top)
57+
right, bottom = math.ceil(right), math.ceil(bottom)
58+
59+
d.text((0, 0), text, font=ttfont, fill=font_color)
60+
61+
text_image = text_image.crop(box=(left, top, right, bottom))
62+
63+
text_image.save(os.path.join(font_dir, font_filename[:-4] + "_preview.png"), "PNG")
64+
65+
66+
TEXT = ("50% 9876M Core i5-4321\n"
67+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"
68+
"abcdefghijklmnopqrstuvwxyz\n"
69+
"0123456789 %;,:?!/-_()[]{}<>=+*#$")
70+
71+
fonts = []
72+
for dir in os.listdir(FONTS_DIR):
73+
font_dir = os.path.join(FONTS_DIR, dir)
74+
if os.path.isdir(font_dir):
75+
for font in os.listdir(font_dir):
76+
font_file = os.path.join(font_dir, font)
77+
if os.path.isfile(font_file) and font_file.endswith(".ttf"):
78+
print(f"Found font {dir}/{font}")
79+
generate_preview(font_dir, font, "40%", font_size=60)
2.09 KB
2.05 KB
1.21 KB
1.22 KB
1.21 KB
3.19 KB
2.48 KB
3.18 KB
2.57 KB

0 commit comments

Comments
 (0)