]> git.cworth.org Git - zombocom-ai/blobdiff - interpret-cairo-to-svg.py
Reword Coda's final message
[zombocom-ai] / interpret-cairo-to-svg.py
index 99edf1b1acc59bcc050e90cb2c2c9dc88a493027..9c90702a631ab6a12ee75dffe3ae88863c4c4cdf 100755 (executable)
@@ -4,6 +4,9 @@ import cairo
 import tempfile
 import os
 import sys
+import gi
+gi.require_version('Rsvg', '2.0')
+from gi.repository import Rsvg
 
 import math
 import random as rand
@@ -37,6 +40,7 @@ stderr = sys.stderr
 del tempfile
 del os
 del sys
+del gi
 
 # Note: We'll let the following "safe" imports remain:
 #
@@ -816,7 +820,13 @@ with cairo.SVGSurface(filename, 512, 512) as surface:
     cr = cairo.Context(surface);
     cr.set_font_size(190)
     cr.select_font_face("sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD)
+    # Setup RSVG rendering
+    rsvg = Rsvg.Handle()
+
+    answer_shown = False
+
     del cairo
+    del Rsvg
 
     def random_within(max):
         return max * random()
@@ -873,17 +883,57 @@ with cairo.SVGSurface(filename, 512, 512) as surface:
     def set_line_width(width):
         cr.set_line_width(width)
 
+    def show_answer():
+        global answer_shown
+        answer_shown = True
+        # After the submitted code, draw the solution in white, (so it will
+        # only be clearly visible if they've made their drawing big enough).
+        cr.set_source_rgb(1, 1, 1)
+        cr.move_to(0,250)
+        cr.show_text(hint[user])
+        cr.set_font_size(240)
+        cr.move_to(0,480)
+        cr.show_text(code[user])
+
+    def fingers():
+        show_answer()
+        svg = rsvg.new_from_file("hand.svg")
+        cr.scale(0.2,0.2)
+        for i in range(16):
+            cr.save()
+            cr.translate(i%4 * 500, i/4 * 500)
+            cr.rotate(2 * math.pi * random())
+            svg.render_cairo(cr)
+            cr.restore()
+
+    def mouths():
+        show_answer()
+        svg = rsvg.new_from_file("mouth.svg")
+        cr.scale(0.5,0.5)
+        for i in range(16):
+            cr.save()
+            cr.translate(i%4 * 150, i/4 * 150)
+            cr.rotate(2 * math.pi * random())
+            svg.render_cairo(cr)
+            cr.restore()
+
+    def eyes():
+        show_answer()
+        svg = rsvg.new_from_file("eye.svg")
+        cr.scale(0.2,0.2)
+        for i in range(16):
+            cr.save()
+            cr.translate(i%4 * 600, i/4 * 600)
+            cr.rotate(2 * math.pi * random())
+            svg.render_cairo(cr)
+            cr.restore()
+
     # Run the submitted code
     exec(input)
 
-    # After the submitted code, draw the solution in white, (so it will
-    # only be clearly visible if they've made their drawing big enough).
-    cr.set_source_rgb(1, 1, 1)
-    cr.move_to(0,250)
-    cr.show_text(hint[user])
-    cr.set_font_size(240)
-    cr.move_to(0,480)
-    cr.show_text(code[user])
+    # If the answer hasn't been shown yet, show it now
+    if not answer_shown:
+        show_answer()
 
 web_file = filename.removeprefix(OUTPUT_DIR_PREFIX)