Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

draw.py 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/env python
  2. from bdfparser import Font
  3. from PIL import Image
  4. import os
  5. import time
  6. class DrawText:
  7. def __init__(self, g):
  8. self.gui = g
  9. scriptDir = os.path.dirname(os.path.realpath(__file__))
  10. fontDir = os.path.join(scriptDir, "fonts")
  11. self.fonts = []
  12. for f in os.listdir(os.fsencode(fontDir)):
  13. filename = os.fsdecode(f)
  14. if not filename.lower().endswith(".bdf"):
  15. continue
  16. font = Font(os.path.join(fontDir, filename))
  17. print(f"{filename} global size is "
  18. f"{font.headers['fbbx']} x {font.headers['fbby']} (pixel), "
  19. f"it contains {len(font)} glyphs.")
  20. # TODO hard-coded per-font offsets
  21. offset = 0
  22. if filename == "iv18x16u.bdf":
  23. offset = 6
  24. data = (font, offset)
  25. self.fonts.append(data)
  26. def getGlyph(self, c):
  27. f, o = self.fonts[0] # TODO selection of fonts
  28. g = f.glyph(c).draw()
  29. # invert color
  30. g = g.replace(1, 2).replace(0, 1).replace(2, 0)
  31. # render to pixel data
  32. img = Image.frombytes('RGBA',
  33. (g.width(), g.height()),
  34. g.tobytes('RGBA'))
  35. return (img, o)
  36. def drawGlyph(self, g, xOff, yOff):
  37. for x in range(0, g.width):
  38. for y in range(0, g.height):
  39. p = g.getpixel((x, y))
  40. self.gui.set_pixel(xOff + x, yOff + y, p)
  41. def text(self, s, offset = 0):
  42. w = 0
  43. for c in s:
  44. g, y = self.getGlyph(c)
  45. self.drawGlyph(g, -offset + w, y)
  46. w += g.width
  47. return w
  48. class ScrollText:
  49. def __init__(self, g, t, i = 1, s = 100):
  50. self.gui = g
  51. self.drawer = DrawText(self.gui)
  52. self.text = t
  53. self.iterations = i
  54. self.speed = 1.0 / s
  55. self.width = self.drawer.text(self.text)
  56. self.offset = -self.gui.width
  57. self.last = time.time()
  58. self.count = 0
  59. def restart(self):
  60. self.offset = -self.gui.width
  61. self.last = time.time()
  62. self.count = 0
  63. def finished(self):
  64. return (self.count >= self.iterations)
  65. def draw(self):
  66. if (time.time() - self.last) > self.speed:
  67. self.offset = (self.offset + 1)
  68. if self.offset >= self.width:
  69. self.offset = -self.gui.width
  70. self.count += 1
  71. self.last = time.time()
  72. self.drawer.text(self.text, self.offset)
  73. if __name__ == "__main__":
  74. from test import TestGUI
  75. t = TestGUI(32, 32)
  76. d = ScrollText(t, "Hello, World!")
  77. t.debug_loop(d.draw)