mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-13 19:18:56 +00:00
update ttf2c generator
This commit is contained in:
parent
8041bc7a8e
commit
711adbeea4
13
tools/ttf2c
13
tools/ttf2c
@ -1,16 +1,13 @@
|
||||
#!/usr/bin/env python3
|
||||
import freetype
|
||||
|
||||
MIN_GLYPH = 33
|
||||
MAX_GLYPH = 126
|
||||
MIN_GLYPH = ord(' ')
|
||||
MAX_GLYPH = ord('~')
|
||||
|
||||
def process_face(name, style, size):
|
||||
face = freetype.Face('/usr/share/fonts/truetype/%s-%s.ttf' % (name, style))
|
||||
face.set_pixel_sizes(0, size)
|
||||
with open('%s-%s-%s.c' % (name, style, size), 'wt') as f:
|
||||
f.write('#include <stdint.h>\n\n')
|
||||
f.write('#define MIN_GLYPH %d\n' % MIN_GLYPH)
|
||||
f.write('#define MAX_GLYPH %d\n\n' % MAX_GLYPH)
|
||||
f.write('// first two bytes are width and height of the glyph\n')
|
||||
f.write('// third, fourth and fifth bytes are advance, bearingX and bearingY of the horizontal metrics of the glyph\n')
|
||||
f.write('// rest is packed 4-bit glyph data\n\n')
|
||||
@ -24,7 +21,9 @@ def process_face(name, style, size):
|
||||
assert(metrics.horiAdvance % 64 == 0)
|
||||
assert(metrics.horiBearingX % 64 == 0)
|
||||
assert(metrics.horiBearingY % 64 == 0)
|
||||
print('Loaded glyph "%c" ... %d x %d @ %d grays' % (c, bitmap.width, bitmap.rows, bitmap.num_grays))
|
||||
assert(bitmap.width == bitmap.pitch)
|
||||
assert(len(bitmap.buffer) == bitmap.pitch * bitmap.rows)
|
||||
print('Loaded glyph "%c" ... %d x %d @ %d grays (%d bytes)' % (c, bitmap.width, bitmap.rows, bitmap.num_grays, len(bitmap.buffer)))
|
||||
f.write('/* %c */ static const uint8_t glyph_%d[] = { %d, %d, %d, %d, %d, ' % (c, i, bitmap.width, bitmap.rows, metrics.horiAdvance // 64, metrics.horiBearingX // 64, metrics.horiBearingY // 64))
|
||||
buf = list(bitmap.buffer)
|
||||
if len(buf) % 2 > 0:
|
||||
@ -32,7 +31,7 @@ def process_face(name, style, size):
|
||||
buf = [ ( (a & 0xF0) | (b >> 4) ) for a, b in [buf[i:i + 2] for i in range(0, len(buf), 2)] ]
|
||||
f.write(', '.join(['%d' % x for x in buf]))
|
||||
f.write(' };\n')
|
||||
f.write('\nconst uint8_t * const Font_%s_%s_%s[MAX_GLYPH + 1 - MIN_GLYPH] = {\n' % (name, style, size))
|
||||
f.write('\nconst uint8_t * const Font_%s_%s_%s[%d + 1 - %d] = {\n' % (name, style, size, MAX_GLYPH, MIN_GLYPH))
|
||||
for i in range(MIN_GLYPH, MAX_GLYPH + 1):
|
||||
f.write(' glyph_%d,\n' % i)
|
||||
f.write('};\n')
|
||||
|
Loading…
Reference in New Issue
Block a user