|
| 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) |
0 commit comments