mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-15 20:19:23 +00:00
core/tools: add some typing/small improvements
This commit is contained in:
parent
af685c9e14
commit
7b77888a60
@ -2,16 +2,22 @@
|
||||
|
||||
# script used to generate /embed/extmod/modtrezorui/font_*_*.c
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import freetype
|
||||
|
||||
HERE = Path(__file__).parent
|
||||
FONTS_DIR = HERE / "fonts"
|
||||
|
||||
MIN_GLYPH = ord(" ")
|
||||
MAX_GLYPH = ord("~")
|
||||
|
||||
# metrics explanation: https://www.freetype.org/freetype2/docs/glyphs/metrics.png
|
||||
|
||||
|
||||
def process_bitmap_buffer(buf, bpp):
|
||||
def process_bitmap_buffer(buf: list[int], bpp: int) -> list[int]:
|
||||
res = buf[:]
|
||||
if bpp == 1:
|
||||
for _ in range(8 - len(res) % 8):
|
||||
@ -52,17 +58,25 @@ def process_bitmap_buffer(buf, bpp):
|
||||
return res
|
||||
|
||||
|
||||
def drop_left_columns(buf, width, drop):
|
||||
res = []
|
||||
def drop_left_columns(buf: list[int], width: int, drop: int) -> list[int]:
|
||||
res: list[int] = []
|
||||
for i in range(len(buf)):
|
||||
if i % width >= drop:
|
||||
res.append(buf[i])
|
||||
return res
|
||||
|
||||
|
||||
def process_face(name, style, size, bpp=4, shaveX=0, ext="ttf"):
|
||||
def process_face(
|
||||
name: str,
|
||||
style: str,
|
||||
size: int,
|
||||
bpp: int = 4,
|
||||
shaveX: int = 0,
|
||||
ext: str = "ttf",
|
||||
) -> None:
|
||||
print("Processing ... %s %s %s" % (name, style, size))
|
||||
face = freetype.Face("fonts/%s-%s.%s" % (name, style, ext))
|
||||
file_name = FONTS_DIR / f"{name}-{style}.{ext}"
|
||||
face = freetype.Face(str(file_name))
|
||||
face.set_pixel_sizes(0, size)
|
||||
fontname = "%s_%s_%d" % (name.lower(), style.lower(), size)
|
||||
font_ymin = 0
|
||||
@ -72,7 +86,9 @@ def process_face(name, style, size, bpp=4, shaveX=0, ext="ttf"):
|
||||
f.write("#include <stdint.h>\n\n")
|
||||
f.write("// clang-format off\n\n")
|
||||
f.write("// - the first two bytes are width and height of the glyph\n")
|
||||
f.write("// - the third, fourth and fifth bytes are advance, bearingX and bearingY of the horizontal metrics of the glyph\n")
|
||||
f.write(
|
||||
"// - the third, fourth and fifth bytes are advance, bearingX and bearingY of the horizontal metrics of the glyph\n"
|
||||
)
|
||||
f.write("// - the rest is packed %d-bit glyph data\n\n" % bpp)
|
||||
for i in range(MIN_GLYPH, MAX_GLYPH + 1):
|
||||
c = chr(i)
|
||||
@ -111,7 +127,7 @@ def process_face(name, style, size, bpp=4, shaveX=0, ext="ttf"):
|
||||
bearingY = metrics.horiBearingY // 64
|
||||
assert advance >= 0 and advance <= 255
|
||||
assert bearingX >= 0 and bearingX <= 255
|
||||
if bearingY < 0: # HACK
|
||||
if bearingY < 0: # HACK
|
||||
print("normalizing bearingY %d for '%s'" % (bearingY, c))
|
||||
bearingY = 0
|
||||
assert bearingY >= 0 and bearingY <= 255
|
||||
@ -124,7 +140,10 @@ def process_face(name, style, size, bpp=4, shaveX=0, ext="ttf"):
|
||||
width -= remove_left
|
||||
assert advance > remove_left
|
||||
advance -= remove_left
|
||||
print('Glyph "%c": removed %d pixel columns from the left' % (c, remove_left))
|
||||
print(
|
||||
'Glyph "%c": removed %d pixel columns from the left'
|
||||
% (c, remove_left)
|
||||
)
|
||||
print(
|
||||
'Loaded glyph "%c" ... %d x %d @ %d grays (%d bytes, metrics: %d, %d, %d)'
|
||||
% (
|
||||
@ -159,7 +178,6 @@ def process_face(name, style, size, bpp=4, shaveX=0, ext="ttf"):
|
||||
)
|
||||
nonprintable += " };\n"
|
||||
|
||||
|
||||
yMin = bearingY - rows
|
||||
yMax = yMin + rows
|
||||
font_ymin = min(font_ymin, yMin)
|
||||
@ -182,7 +200,10 @@ def process_face(name, style, size, bpp=4, shaveX=0, ext="ttf"):
|
||||
f.write("#endif\n")
|
||||
|
||||
f.write("#define Font_%s_%s_%d_HEIGHT %d\n" % (name, style, size, size))
|
||||
f.write("#define Font_%s_%s_%d_MAX_HEIGHT %d\n" % (name, style, size, font_ymax - font_ymin))
|
||||
f.write(
|
||||
"#define Font_%s_%s_%d_MAX_HEIGHT %d\n"
|
||||
% (name, style, size, font_ymax - font_ymin)
|
||||
)
|
||||
f.write("#define Font_%s_%s_%d_BASELINE %d\n" % (name, style, size, -font_ymin))
|
||||
f.write(
|
||||
"extern const uint8_t* const Font_%s_%s_%d[%d + 1 - %d];\n"
|
||||
|
@ -1,9 +1,16 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import BinaryIO, TextIO
|
||||
|
||||
from trezorlib import toif
|
||||
|
||||
HERE = Path(__file__).parent
|
||||
CORE_DIR = HERE.parent.parent
|
||||
|
||||
def process_line(infile, outfile):
|
||||
|
||||
def process_line(infile: TextIO, outfile: BinaryIO) -> None:
|
||||
line = infile.readline()
|
||||
data = [x.strip().lower() for x in line.split(',')]
|
||||
for c in data:
|
||||
@ -11,7 +18,7 @@ def process_line(infile, outfile):
|
||||
outfile.write(bytes((int(c, 16),)))
|
||||
|
||||
|
||||
def header_to_toif(path) -> bool:
|
||||
def header_to_toif(path: str | Path) -> str:
|
||||
with open(path, "r") as infile, open('tmp.toif', "wb") as outfile:
|
||||
infile.readline()
|
||||
name_line = infile.readline()
|
||||
@ -44,7 +51,7 @@ def header_to_toif(path) -> bool:
|
||||
return name
|
||||
|
||||
|
||||
def toif_to_header(path, name):
|
||||
def toif_to_header(path: str | Path, name: str) -> None:
|
||||
with open('tmp_c.toif', "rb") as infile, open(path, "w") as outfile:
|
||||
b = infile.read(4)
|
||||
outfile.write("// clang-format off\n")
|
||||
@ -61,7 +68,6 @@ def toif_to_header(path, name):
|
||||
else:
|
||||
raise Exception("Unknown format")
|
||||
|
||||
|
||||
outfile.write(" // width (16-bit), height (16-bit)\n",)
|
||||
outfile.write(" ")
|
||||
for i in range(4):
|
||||
@ -92,10 +98,10 @@ def toif_to_header(path, name):
|
||||
hex_data = infile.read(1).hex()
|
||||
outfile.write("\n};\n")
|
||||
|
||||
byte = infile.read(1)
|
||||
_byte = infile.read(1)
|
||||
|
||||
|
||||
def reformat_c_icon(path):
|
||||
def reformat_c_icon(path: str | Path) -> None:
|
||||
name = header_to_toif(path)
|
||||
with open("tmp.toif", "rb") as f:
|
||||
toi = toif.from_bytes(f.read())
|
||||
@ -109,14 +115,14 @@ def reformat_c_icon(path):
|
||||
os.remove("tmp_c.toif")
|
||||
|
||||
|
||||
def reformat_c_icons(p):
|
||||
def reformat_c_icons(p: str | Path) -> None:
|
||||
files = os.listdir(p)
|
||||
for file in files:
|
||||
if file.startswith("icon_") and file.endswith(".h"):
|
||||
reformat_c_icon(os.path.join(p, file))
|
||||
|
||||
|
||||
def reformat_toif_icon(p):
|
||||
def reformat_toif_icon(p: str | Path) -> None:
|
||||
with open(p, "rb") as f:
|
||||
toi = toif.from_bytes(f.read())
|
||||
im = toi.to_image()
|
||||
@ -125,7 +131,7 @@ def reformat_toif_icon(p):
|
||||
f.write(toi.to_bytes())
|
||||
|
||||
|
||||
def reformat_toif_icons(p):
|
||||
def reformat_toif_icons(p: str | Path) -> None:
|
||||
files = os.listdir(p)
|
||||
for file in files:
|
||||
if file.endswith(".toif"):
|
||||
@ -134,27 +140,27 @@ def reformat_toif_icons(p):
|
||||
|
||||
def change_icon_format():
|
||||
# bootloader icons
|
||||
reformat_c_icons("../../embed/bootloader")
|
||||
reformat_c_icons(CORE_DIR / "embed/bootloader")
|
||||
|
||||
# bootloader_ci icons
|
||||
reformat_c_icons("../../embed/bootloader_ci")
|
||||
reformat_c_icons(CORE_DIR / "embed/bootloader_ci")
|
||||
|
||||
# rust icons
|
||||
reformat_toif_icons("../../embed/rust/src/ui/model_tr/res")
|
||||
reformat_toif_icons("../../embed/rust/src/ui/model_tt/res")
|
||||
reformat_toif_icons(CORE_DIR / "embed/rust/src/ui/model_tr/res")
|
||||
reformat_toif_icons(CORE_DIR / "embed/rust/src/ui/model_tt/res")
|
||||
|
||||
# python icons
|
||||
reformat_toif_icons("../../src/trezor/res")
|
||||
reformat_toif_icons("../../src/trezor/res/header_icons")
|
||||
reformat_toif_icons(CORE_DIR / "src/trezor/res")
|
||||
reformat_toif_icons(CORE_DIR / "src/trezor/res/header_icons")
|
||||
|
||||
# vendor header icons
|
||||
reformat_toif_icon("../../embed/vendorheader/vendor_satoshilabs.toif")
|
||||
reformat_toif_icon("../../embed/vendorheader/vendor_unsafe.toif")
|
||||
reformat_toif_icon(CORE_DIR / "embed/vendorheader/vendor_satoshilabs.toif")
|
||||
reformat_toif_icon(CORE_DIR / "embed/vendorheader/vendor_unsafe.toif")
|
||||
|
||||
# additional python icons
|
||||
# reformat_toif_icon("../src/apps/homescreen/res/bg.toif") - unchanged - using as avatar
|
||||
reformat_toif_icon("../../src/apps/management/res/small-arrow.toif")
|
||||
reformat_toif_icon("../../src/apps/webauthn/res/icon_webauthn.toif")
|
||||
# reformat_toif_icon(CORE_DIR / "src/apps/homescreen/res/bg.toif") - unchanged - using as avatar
|
||||
reformat_toif_icon(CORE_DIR / "src/apps/management/res/small-arrow.toif")
|
||||
reformat_toif_icon(CORE_DIR / "src/apps/webauthn/res/icon_webauthn.toif")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Loading…
Reference in New Issue
Block a user