Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python3
  2. # Uses the Python BDF format bitmap font parser:
  3. # https://github.com/tomchen/bdfparser
  4. #
  5. # And the pillow Python Imaging Library:
  6. # https://github.com/python-pillow/Pillow
  7. #
  8. # ----------------------------------------------------------------------------
  9. # "THE BEER-WARE LICENSE" (Revision 42):
  10. # <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  11. # you can do whatever you want with this stuff. If we meet some day, and you
  12. # think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  13. # ----------------------------------------------------------------------------
  14. from bdfparser import Font
  15. from PIL import Image
  16. import os
  17. class DrawText:
  18. def __init__(self, g, fg = (255, 255, 255), bg = (0, 0, 0), c = (0, 255, 0)):
  19. self.gui = g
  20. self.fg = fg
  21. self.bg = bg
  22. self.color = c
  23. scriptDir = os.path.dirname(os.path.realpath(__file__))
  24. fontDir = os.path.join(scriptDir, "fonts")
  25. self.fonts = {}
  26. for f in os.listdir(os.fsencode(fontDir)):
  27. filename = os.fsdecode(f)
  28. if not filename.lower().endswith(".bdf"):
  29. continue
  30. # TODO hard-coded per-font offsets and spacing adjustment
  31. offset = 0
  32. spacing = 0
  33. if filename == "iv18x16u.bdf":
  34. offset = 6
  35. elif filename == "ib8x8u.bdf":
  36. offset = 10
  37. elif filename == "lemon.bdf":
  38. offset = 8
  39. spacing = -3
  40. elif filename == "antidote.bdf":
  41. offset = 9
  42. spacing = -1
  43. elif filename == "uushi.bdf":
  44. offset = 8
  45. spacing = -2
  46. elif filename == "tom-thumb.bdf":
  47. offset = 12
  48. spacing = 2
  49. # center vertically, in case of
  50. # multiple stacked panels
  51. # TODO hard-coded offsets assume 32x32 panels
  52. if self.gui.height > 32:
  53. offset += 16
  54. font = Font(os.path.join(fontDir, filename))
  55. data = (font, offset, {}, spacing)
  56. self.fonts[filename[:-4]] = data
  57. def getGlyph(self, c, font):
  58. if not isinstance(font, str):
  59. # fall-back to first available font
  60. f, offset, cache, spacing = next(iter(self.fonts))
  61. else:
  62. # users font choice
  63. f, offset, cache, spacing = self.fonts[font]
  64. # only render glyphs once, cache resulting image data
  65. if not c in cache:
  66. g = f.glyph(c).draw()
  67. # invert color
  68. g = g.replace(1, 2).replace(0, 1).replace(2, 0)
  69. # render to pixel data
  70. bytesdict = {
  71. 0: int(self.fg[2] << 16 | self.fg[1] << 8 | self.fg[0]).to_bytes(4, byteorder = "little"),
  72. 1: int(self.bg[2] << 16 | self.bg[1] << 8 | self.bg[0]).to_bytes(4, byteorder = "little"),
  73. 2: int(self.color[2] << 16 | self.color[1] << 8 | self.color[0]).to_bytes(4, byteorder = "little"),
  74. }
  75. img = Image.frombytes('RGBA',
  76. (g.width(), g.height()),
  77. g.tobytes('RGBA', bytesdict))
  78. cache[c] = img
  79. return (cache[c], offset, spacing)
  80. def drawGlyph(self, g, xOff, yOff, spacing):
  81. if xOff >= self.gui.width:
  82. return
  83. for x in range(0, g.width):
  84. for y in range(0, g.height):
  85. xTarget = xOff + x
  86. if (xTarget < 0) or (xTarget >= self.gui.width):
  87. continue
  88. p = g.getpixel((x, y))
  89. self.gui.set_pixel(xTarget, yOff + y, p)
  90. for x in range(0, spacing):
  91. for y in range(0, g.height):
  92. self.gui.set_pixel(xOff + x + g.width, yOff + y, self.bg)
  93. def text(self, s, f, offset = 0, earlyAbort = True, yOff = 0):
  94. w = 0
  95. for c in s:
  96. xOff = -offset + w
  97. if earlyAbort:
  98. if xOff >= self.gui.width:
  99. break
  100. g, y, spacing = self.getGlyph(c, f)
  101. w += g.width + spacing
  102. if xOff >= -16: # some wiggle room so chars dont disappear
  103. self.drawGlyph(g, xOff, y + yOff, spacing)
  104. return w