Diagram.py: make aligning to the baseline optional in draw_centered_text()
[experiments/RadialSymmetry.git] / Diagram.py
index 0bc3925..8d958f9 100755 (executable)
@@ -139,8 +139,8 @@ class Diagram(object):
         cr = self.cr
 
         cr.save()
-        cr.set_source_rgba(fill_color[0], fill_color[1], fill_color[2],
-                           fill_color[3])
+        r, g, b, a = self.color_to_rgba(fill_color)
+        cr.set_source_rgba(r, g, b, a)
         cr.arc(cx, cy, size, 0, 2 * pi)
         cr.fill()
         cr.restore()
@@ -157,8 +157,8 @@ class Diagram(object):
         cr.stroke()
 
     def draw_rect_from_center(self, cx, cy, width, height, theta=0,
-                              fill=True, fill_color=[1, 1, 1],
-                              stroke=False, stroke_color=[0, 0, 0]):
+                              fill=True, fill_color=[1, 1, 1, 0.8],
+                              stroke=False, stroke_color=[0, 0, 0, 0.5]):
         cr = self.cr
 
         mx = width / 2.0
@@ -173,19 +173,21 @@ class Diagram(object):
 
         if fill:
             cr.rectangle(0, 0, width, height)
-            cr.set_source_rgba(fill_color[0], fill_color[1], fill_color[2], 0.8)
+            r, g, b, a = self.color_to_rgba(fill_color)
+            cr.set_source_rgba(r, g, b, a)
             cr.fill()
 
         if stroke:
             cr.rectangle(0, 0, width, height)
-            cr.set_source_rgba(stroke_color[0], stroke_color[1], stroke_color[2], 0.5)
+            r, g, b, a = self.color_to_rgba(stroke_color)
+            cr.set_source_rgba(r, g, b, a)
             cr.stroke()
-            self.draw_dot(0, 0, 3.0, list(stroke_color) + [0.5])
+            self.draw_dot(0, 0, 3.0, stroke_color)
 
         cr.restore()
 
-    def draw_rect(self, x, y, width, height, fill=True, fill_color=[1, 1, 1],
-                  stroke=False, stroke_color=[0, 0, 0]):
+    def draw_rect(self, x, y, width, height, fill=True, fill_color=[1, 1, 1, 0.8],
+                  stroke=False, stroke_color=[0, 0, 0, 0.5]):
         cr = self.cr
 
         cr.save()
@@ -193,27 +195,41 @@ class Diagram(object):
 
         if fill:
             cr.rectangle(0, 0, width, height)
-            cr.set_source_rgba(fill_color[0], fill_color[1], fill_color[2], 0.8)
+            r, g, b, a = self.color_to_rgba(fill_color)
+            cr.set_source_rgba(r, g, b, a)
             cr.fill()
 
         if stroke:
             cr.rectangle(0, 0, width, height)
-            cr.set_source_rgba(stroke_color[0], stroke_color[1], stroke_color[2], 0.5)
+            r, g, b, a = self.color_to_rgba(stroke_color)
+            cr.set_source_rgba(r, g, b, a)
             cr.stroke()
-            self.draw_dot(0, 0, 3.0, list(stroke_color) + [0.5])
+            self.draw_dot(0, 0, 3.0, stroke_color)
 
         cr.restore()
 
-    def draw_centered_text(self, cx, cy, text, theta=0, color=[0, 0, 0], bounding_box=False):
+    def draw_centered_text(self, cx, cy, text, theta=0,
+                           color=[0, 0, 0],
+                           align_baseline=False,
+                           bb_fill=True, bb_fill_color=[1, 1, 1, 0.8],
+                           bb_stroke=False, bb_stroke_color=[0, 0, 0, 0.5]):
         cr = self.cr
 
         x_bearing, y_bearing, width, height, x_advance = cr.text_extents(text)[:5]
         ascent, descent = cr.font_extents()[:2]
 
         # The offset of the lower-left corner of the text.
-        # NOTE: y is kept on the baseline
         tx = width / 2.0 + x_bearing
-        ty = 0
+
+        if align_baseline:
+            # When aligning to the  baseline it is convenient the make the
+            # bounding box depend on the font vertical extent and not from the
+            # text content.
+            ty = 0
+            bb = [0, -descent, width, ascent]
+        else:
+            ty = height / 2.0 + y_bearing
+            bb = [0, y_bearing, width, height]
 
         # Angles are intended clockwise by the caller, but the trigonometric
         # functions below consider angles counter-clockwise
@@ -226,8 +242,12 @@ class Diagram(object):
         cr.save()
         cr.translate(rx, ry)
         cr.rotate(theta)
-        self.draw_rect(0, -descent, width, ascent, fill_color=[1, 1, 1, 0.1], stroke=bounding_box)
-        cr.set_source_rgba(color[0], color[1], color[2], 0.8)
+
+        if bb_fill or bb_stroke:
+            self.draw_rect(bb[0], bb[1], bb[2], bb[3], bb_fill, bb_fill_color, bb_stroke, bb_stroke_color)
+
+        r, g, b, a = self.color_to_rgba(color)
+        cr.set_source_rgba(r, g, b, a)
         cr.move_to(0, 0)
         cr.show_text(text)
         cr.fill()
@@ -248,28 +268,28 @@ if __name__ == "__main__":
 
     theta = 0
 
-    advance = diagram.draw_centered_text(x_offset, y, "Ciao", theta, bounding_box=True)
+    advance = diagram.draw_centered_text(x_offset, y, "Ciao", theta, align_baseline=True, bb_stroke=True)
     x_offset += advance
 
-    advance = diagram.draw_centered_text(x_offset, y, "____", theta + pi / 4, bounding_box=True)
+    advance = diagram.draw_centered_text(x_offset, y, "____", theta + pi / 4, align_baseline=True, bb_stroke=True)
     x_offset += advance
 
-    advance = diagram.draw_centered_text(x_offset, y, "jxpqdlf", theta + pi / 2, bounding_box=True)
+    advance = diagram.draw_centered_text(x_offset, y, "jxpqdlf", theta + pi / 2, align_baseline=True, bb_stroke=True)
     x_offset += advance
 
-    advance = diagram.draw_centered_text(x_offset, y, "pppp", theta + 3 * pi / 4, bounding_box=True)
+    advance = diagram.draw_centered_text(x_offset, y, "pppp", theta + 3 * pi / 4, align_baseline=True, bb_stroke=True)
     x_offset += advance
 
-    advance = diagram.draw_centered_text(x_offset, y, "dddd", theta + pi, bounding_box=True)
+    advance = diagram.draw_centered_text(x_offset, y, "dddd", theta + pi, align_baseline=True, bb_stroke=True)
     x_offset += advance
 
-    advance = diagram.draw_centered_text(x_offset, y, "Jjjj", theta + 5 * pi / 4, bounding_box=True)
+    advance = diagram.draw_centered_text(x_offset, y, "Jjjj", theta + 5 * pi / 4, align_baseline=True, bb_stroke=True)
     x_offset += advance
 
-    advance = diagram.draw_centered_text(x_offset, y, "1369", theta + 3 * pi / 2, bounding_box=True)
+    advance = diagram.draw_centered_text(x_offset, y, "1369", theta + 3 * pi / 2, align_baseline=True, bb_stroke=True)
     x_offset += advance
 
-    advance = diagram.draw_centered_text(x_offset, y, "qqqq", theta + 7 * pi / 4, bounding_box=True)
+    advance = diagram.draw_centered_text(x_offset, y, "qqqq", theta + 7 * pi / 4, align_baseline=True, bb_stroke=True)
     x_offset += advance
 
     diagram.draw_line(0, y, 400, y, [0, 0, 1, 0.2])