buildhzk.py 2.1 KB

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