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