buildhzk.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Generate a 'HZK' font file for the T5UIC1 DWIN LCD
  2. # from multiple bdf font files.
  3. # Note: the 16x16 glyphs are not produced
  4. # Author: Taylor Talkington
  5. # License: GPL
  6. import bdflib.reader, math
  7. def glyph_bits(size_x, size_y, font, glyph_ord):
  8. asc = font[b'FONT_ASCENT']
  9. desc = font[b'FONT_DESCENT']
  10. bits = [0 for y in range(size_y)]
  11. glyph_bytes = math.ceil(size_x / 8)
  12. try:
  13. glyph = font[glyph_ord]
  14. for y, row in enumerate(glyph.data):
  15. v = row
  16. rpad = size_x - glyph.bbW
  17. if rpad < 0: rpad = 0
  18. if glyph.bbW > size_x: v = v >> (glyph.bbW - size_x) # some glyphs are actually too wide to fit!
  19. v = v << (glyph_bytes * 8) - size_x + rpad
  20. v = v >> glyph.bbX
  21. bits[y + desc + glyph.bbY] |= v
  22. except KeyError:
  23. pass
  24. bits.reverse()
  25. return bits
  26. def marlin_font_hzk():
  27. fonts = [
  28. [6,12,'marlin-6x12-3.bdf'],
  29. [8,16,'marlin-8x16.bdf'],
  30. [10,20,'marlin-10x20.bdf'],
  31. [12,24,'marlin-12x24.bdf'],
  32. [14,28,'marlin-14x28.bdf'],
  33. [16,32,'marlin-16x32.bdf'],
  34. [20,40,'marlin-20x40.bdf'],
  35. [24,48,'marlin-24x48.bdf'],
  36. [28,56,'marlin-28x56.bdf'],
  37. [32,64,'marlin-32x64.bdf']
  38. ]
  39. with open('marlin_fixed.hzk','wb') as output:
  40. for f in fonts:
  41. with open(f[2], 'rb') as file:
  42. print(f'{f[0]}x{f[1]}')
  43. font = bdflib.reader.read_bdf(file)
  44. for glyph in range(128):
  45. bits = glyph_bits(f[0], f[1], font, glyph)
  46. glyph_bytes = math.ceil(f[0]/8)
  47. for b in bits:
  48. try:
  49. z = b.to_bytes(glyph_bytes, 'big')
  50. output.write(z)
  51. except OverflowError:
  52. print('Overflow')
  53. print(f'{glyph}')
  54. print(font[glyph])
  55. for b in bits: print(f'{b:0{f[0]}b}')
  56. return