Add boxes.text() and use it in drillbox.py

This commit is contained in:
Florian Festi 2014-01-21 22:44:22 +01:00
parent 82ebbf09ed
commit 13d46195b7
2 changed files with 25 additions and 5 deletions

View File

@ -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 = {

View File

@ -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