2016-03-26 22:46:18 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import freetype
|
|
|
|
|
2016-03-27 11:36:27 +00:00
|
|
|
MIN_GLYPH = ord(' ')
|
|
|
|
MAX_GLYPH = ord('~')
|
2016-03-26 22:46:18 +00:00
|
|
|
|
|
|
|
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('// 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')
|
|
|
|
for i in range(MIN_GLYPH, MAX_GLYPH + 1):
|
|
|
|
c = chr(i)
|
|
|
|
face.load_char(c, freetype.FT_LOAD_RENDER | freetype.FT_LOAD_TARGET_NORMAL)
|
|
|
|
bitmap = face.glyph.bitmap
|
|
|
|
metrics = face.glyph.metrics
|
|
|
|
assert(metrics.width // 64 == bitmap.width)
|
|
|
|
assert(metrics.height // 64 == bitmap.rows)
|
|
|
|
assert(metrics.horiAdvance % 64 == 0)
|
|
|
|
assert(metrics.horiBearingX % 64 == 0)
|
|
|
|
assert(metrics.horiBearingY % 64 == 0)
|
2016-03-27 11:36:27 +00:00
|
|
|
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)))
|
2016-03-26 22:46:18 +00:00
|
|
|
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:
|
|
|
|
buf.append(0)
|
|
|
|
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')
|
2016-03-27 11:36:27 +00:00
|
|
|
f.write('\nconst uint8_t * const Font_%s_%s_%s[%d + 1 - %d] = {\n' % (name, style, size, MAX_GLYPH, MIN_GLYPH))
|
2016-03-26 22:46:18 +00:00
|
|
|
for i in range(MIN_GLYPH, MAX_GLYPH + 1):
|
|
|
|
f.write(' glyph_%d,\n' % i)
|
|
|
|
f.write('};\n')
|
|
|
|
|
|
|
|
process_face('Roboto', 'Regular', 18)
|