From 13d46195b7b2632b306424a0d95e0d4aa7424e79 Mon Sep 17 00:00:00 2001 From: Florian Festi Date: Tue, 21 Jan 2014 22:44:22 +0100 Subject: [PATCH] Add boxes.text() and use it in drillbox.py --- boxes.py | 22 ++++++++++++++++++++++ drillbox.py | 8 +++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/boxes.py b/boxes.py index a7d9d85..e95b72a 100755 --- a/boxes.py +++ b/boxes.py @@ -705,6 +705,28 @@ class Boxes: self.corner(-90, r) self.edge(d) + @restore + def text(self, text, x=0, y=0, angle=0, align=""): + self.moveTo(x, y, angle) + (tx, ty, width, height, dx, dy) = self.ctx.text_extents(text) + align = align.split() + moves = { + "top" : (0, -height), + "middle" : (0, -0.5*height), + "bottom" : (0, 0), + "left" : (0, 0), + "center" : (-0.5*width, 0), + "right" : (-width, 0), + } + for a in align: + if a in moves: + self.moveTo(*moves[a]) + else: + raise ValueError("Unknown alignment: %s" % align) + + self.ctx.scale(1, -1) + self.ctx.show_text(text) + @restore def NEMA(self, size, x=0, y=0, angle=0): nema = { diff --git a/drillbox.py b/drillbox.py index febae24..eab6df1 100755 --- a/drillbox.py +++ b/drillbox.py @@ -29,12 +29,10 @@ class Box(Boxes): for i in range(4): for j in range(6): self.rectangularHole(i*60+30, 20*j+10, 58, 14+1*j) - self.ctx.save() - self.ctx.move_to(i*60+14, 19*j+6) d = 2.5-0.5*i+2*j - self.ctx.scale(1,-1) - self.ctx.show_text("%.1f" % d) - self.ctx.restore() + self.text("%.1f" % d, i*60+20, 19*j+6, + align="center") + def render(self): x, y, h = self.x, self.y, self.h