summaryrefslogtreecommitdiff
path: root/tests/render.py
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2019-10-11 17:53:28 +0200
committerLaurenz <laurmaedje@gmail.com>2019-10-11 17:53:28 +0200
commitc0e4fd55e6fa738cfc5dcc851d0fc3ee2d0f2cd2 (patch)
treee5531e605d0ab9e06dc950b4cd9b7a8caa116d34 /tests/render.py
parent8f788f9a4f5e970bbe6147987b711470d57aca8d (diff)
Create test runner which renders layouts to images 🗺
Diffstat (limited to 'tests/render.py')
-rw-r--r--tests/render.py73
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/render.py b/tests/render.py
new file mode 100644
index 00000000..02c2693f
--- /dev/null
+++ b/tests/render.py
@@ -0,0 +1,73 @@
+import sys
+import os
+import pathlib
+from PIL import Image, ImageDraw, ImageFont
+
+
+BASE = os.path.dirname(__file__)
+CACHE_DIR = os.path.join(BASE, "../test-cache/");
+
+
+def main():
+ assert len(sys.argv) == 2, "usage: python render.py <name>"
+ name = sys.argv[1]
+
+ filename = os.path.join(CACHE_DIR, f"serialized/{name}.box")
+ with open(filename, encoding="utf-8") as file:
+ lines = [line[:-1] for line in file.readlines()]
+
+ fonts = {}
+ font_count = int(lines[0])
+ for i in range(font_count):
+ parts = lines[1 + i].split(' ', 1)
+ index = int(parts[0])
+ path = parts[1]
+ fonts[index] = os.path.join(BASE, "../fonts", path)
+
+ width, height = (float(s) for s in lines[font_count + 1].split())
+
+ renderer = Renderer(fonts, width, height)
+ for command in lines[font_count + 2:]:
+ renderer.execute(command)
+
+ pathlib.Path(os.path.join(CACHE_DIR, "rendered")).mkdir(parents=True, exist_ok=True)
+ renderer.export(name)
+
+
+class Renderer:
+ def __init__(self, fonts, width, height):
+ self.fonts = fonts
+ self.img = Image.new("RGBA", (pix(width), pix(height)), (255, 255, 255))
+ self.draw = ImageDraw.Draw(self.img)
+ self.cursor = (0, 0)
+
+ def execute(self, command):
+ cmd = command[0]
+ parts = command.split()[1:]
+
+ if cmd == 'm':
+ x, y = (pix(float(s)) for s in parts)
+ self.cursor = (x, y)
+
+ elif cmd == 'f':
+ index = int(parts[0])
+ size = pix(float(parts[1]))
+ self.font = ImageFont.truetype(self.fonts[index], size)
+
+ elif cmd == 'w':
+ text = command[2:]
+ self.draw.text(self.cursor, text, (0, 0, 0), font=self.font)
+
+ else:
+ raise Exception("invalid command")
+
+ def export(self, name):
+ self.img.save(CACHE_DIR + "rendered/" + name + ".png")
+
+
+def pix(points):
+ return int(2 * points)
+
+
+if __name__ == "__main__":
+ main()