ソースを参照

Basic support for selection between multiple fonts. Attempt to add heading to QR code, not yet finished.

Thomas Buck 11ヶ月前
コミット
3e0c60902b
6個のファイルの変更10439行の追加22行の削除
  1. 2
    2
      camp_small.py
  2. 16
    12
      draw.py
  3. 10405
    0
      fonts/ib8x8u.bdf
  4. 4
    4
      manager.py
  5. 2
    2
      net.py
  6. 10
    2
      qr.py

+ 2
- 2
camp_small.py ファイルの表示

@@ -30,12 +30,12 @@ if __name__ == "__main__":
30 30
     success = Manager(t)
31 31
     success.add(ImageScreen(t, "drinka.gif", 0.2, 2, 20.0))
32 32
     success.add(Solid(t, 1.0))
33
-    success.add(QRScreen(t, url, 30.0))
33
+    success.add(QRScreen(t, url, 30.0, "Order:"))
34 34
     success.add(Solid(t, 1.0))
35 35
 
36 36
     fail = Manager(t)
37 37
     fail.add(ImageScreen(t, "attention.gif", 0.2, 2, 20.0, (0, 0, 0)))
38
-    fail.add(ScrollText(t, "The UbaBot Cocktail machine is currently closed. Please come back later for more drinks!", 2))
38
+    fail.add(ScrollText(t, "The UbaBot Cocktail machine is currently closed. Please come back later for more drinks!", "ib8x8u", 2))
39 39
     fail.add(Solid(t, 2.0))
40 40
     fail.add(GameOfLife(t, 20, (0, 255, 0), (0, 0, 0), None, 2.0))
41 41
     fail.add(Solid(t, 2.0))

+ 16
- 12
draw.py ファイルの表示

@@ -18,7 +18,7 @@ class DrawText:
18 18
 
19 19
         scriptDir = os.path.dirname(os.path.realpath(__file__))
20 20
         fontDir = os.path.join(scriptDir, "fonts")
21
-        self.fonts = []
21
+        self.fonts = {}
22 22
         for f in os.listdir(os.fsencode(fontDir)):
23 23
             filename = os.fsdecode(f)
24 24
 
@@ -34,12 +34,14 @@ class DrawText:
34 34
             offset = 0
35 35
             if filename == "iv18x16u.bdf":
36 36
                 offset = 6
37
+            elif filename == "ib8x8u.bdf":
38
+                offset = 10
37 39
 
38 40
             data = (font, offset, {})
39
-            self.fonts.append(data)
41
+            self.fonts[filename[:-4]] = data
40 42
 
41
-    def getGlyph(self, c):
42
-        f, o, cache = self.fonts[0] # TODO selection of fonts
43
+    def getGlyph(self, c, font):
44
+        f, o, cache = self.fonts[font]
43 45
 
44 46
         # only render glyphs once, cache resulting image data
45 47
         if not c in cache:
@@ -70,7 +72,7 @@ class DrawText:
70 72
                 p = g.getpixel((x, y))
71 73
                 self.gui.set_pixel(xTarget, yOff + y, p)
72 74
 
73
-    def text(self, s, offset = 0, earlyAbort = True):
75
+    def text(self, s, f, offset = 0, earlyAbort = True, yOff = 0):
74 76
         w = 0
75 77
         for c in s:
76 78
             xOff = -offset + w
@@ -78,22 +80,23 @@ class DrawText:
78 80
                 if xOff >= self.gui.width:
79 81
                     break
80 82
 
81
-            g, y = self.getGlyph(c)
83
+            g, y = self.getGlyph(c, f)
82 84
             w += g.width
83 85
 
84 86
             if xOff >= -10: # some wiggle room so chars dont disappear
85
-                self.drawGlyph(g, xOff, y)
87
+                self.drawGlyph(g, xOff, y + yOff)
86 88
         return w
87
-import sys
89
+
88 90
 class ScrollText:
89
-    def __init__(self, g, t, i = 1, s = 75):
91
+    def __init__(self, g, t, f, i = 1, s = 75):
90 92
         self.gui = g
91 93
         self.drawer = DrawText(self.gui)
92 94
         self.text = t
95
+        self.font = f
93 96
         self.iterations = i
94 97
         self.speed = 1.0 / s
95 98
 
96
-        self.width = self.drawer.text(self.text, 0, False)
99
+        self.width = self.drawer.text(self.text, self.font, 0, False)
97 100
         self.restart()
98 101
 
99 102
     def restart(self):
@@ -113,11 +116,12 @@ class ScrollText:
113 116
                 self.offset = -self.gui.width
114 117
                 self.count += 1
115 118
 
116
-        self.drawer.text(self.text, self.offset, True)
119
+        self.drawer.text(self.text, self.font, self.offset, True)
117 120
 
118 121
 if __name__ == "__main__":
119 122
     import util
120 123
     t = util.getTarget()
121 124
 
122
-    d = ScrollText(t, "This is a long scrolling text. Is it too fast or maybe too slow?")
125
+    #d = ScrollText(t, "This is a long scrolling text. Is it too fast or maybe too slow?", "iv18x16u")
126
+    d = ScrollText(t, "This is a long scrolling text. Is it too fast or maybe too slow?", "ib8x8u")
123 127
     t.debug_loop(d.draw)

+ 10405
- 0
fonts/ib8x8u.bdf
ファイル差分が大きすぎるため省略します
ファイルの表示


+ 4
- 4
manager.py ファイルの表示

@@ -63,19 +63,19 @@ if __name__ == "__main__":
63 63
 
64 64
     m = Manager(t)
65 65
 
66
-    m.add(ScrollText(t, "This appears once"))
66
+    m.add(ScrollText(t, "This appears once", "ib8x8u"))
67 67
     m.add(Solid(t, 1.0))
68 68
 
69
-    m.add(ScrollText(t, "And this twice...", 2))
69
+    m.add(ScrollText(t, "And this twice...", "ib8x8u", 2))
70 70
     m.add(Solid(t, 1.0))
71 71
 
72 72
     m.add(GameOfLife(t, 20, (0, 255, 0), (0, 0, 0), 20.0, True))
73 73
     m.add(Solid(t, 1.0))
74 74
 
75 75
     sub = Manager(t)
76
-    sub.add(ScrollText(t, "Hello"))
76
+    sub.add(ScrollText(t, "Hello", "ib8x8u"))
77 77
     sub.add(Solid(t, 1.0, (0, 255, 0)))
78
-    sub.add(ScrollText(t, "World"))
78
+    sub.add(ScrollText(t, "World", "ib8x8u"))
79 79
     sub.add(Solid(t, 1.0, (0, 0, 255)))
80 80
     m.add(sub)
81 81
     m.add(Solid(t, 1.0))

+ 2
- 2
net.py ファイルの表示

@@ -63,7 +63,7 @@ if __name__ == "__main__":
63 63
     t = util.getTarget()
64 64
 
65 65
     d = CheckHTTP("http://xythobuz.de")
66
-    d.success(ScrollText(t, "Success"))
67
-    d.fail(ScrollText(t, "Failure"))
66
+    d.success(ScrollText(t, "Success", "ib8x8u"))
67
+    d.fail(ScrollText(t, "Failure", "ib8x8u"))
68 68
 
69 69
     t.debug_loop(d.draw)

+ 10
- 2
qr.py ファイルの表示

@@ -10,11 +10,13 @@
10 10
 import time
11 11
 import qrcode
12 12
 import util
13
+from draw import DrawText
13 14
 
14 15
 class QRScreen:
15
-    def __init__(self, g, d, t = 10.0, c1 = (0, 0, 0), c2 = (255, 255, 255)):
16
+    def __init__(self, g, d, t = 10.0, h = None, c1 = (0, 0, 0), c2 = (255, 255, 255)):
16 17
         self.gui = g
17 18
         self.time = t
19
+        self.heading = h
18 20
         self.c1 = c1
19 21
         self.c2 = c2
20 22
 
@@ -33,6 +35,9 @@ class QRScreen:
33 35
         else:
34 36
             self.image = qr.make_image(fill_color = self.c1, back_color = self.c2)
35 37
 
38
+        if self.heading != None:
39
+            self.text = DrawText(self.gui)
40
+
36 41
         self.xOff = int((self.gui.width - self.image.width) / 2)
37 42
         self.yOff = int((self.gui.height - self.image.height) / 2)
38 43
 
@@ -65,9 +70,12 @@ class QRScreen:
65 70
                     v = (v, v, v)
66 71
                 self.gui.set_pixel(x + self.xOff, y + self.yOff, v)
67 72
 
73
+        if self.heading != None:
74
+            self.text.text(self.heading, "ib8x8u", 0, True, -10)
75
+
68 76
 if __name__ == "__main__":
69 77
     import util
70 78
     t = util.getTarget()
71 79
 
72
-    d = QRScreen(t, "Hello World")
80
+    d = QRScreen(t, "Hello World", 10.0, "Test")
73 81
     t.debug_loop(d.draw)

読み込み中…
キャンセル
保存