1 # define the length of things
2 from __future__ import division
6 def __init__(self, scale=None, units=None,
7 ruler_height=None, ruler_length=None):
9 # default values come from OLPC
10 self.scale = scale or 78.740
11 self.units = units or 'centimeters'
12 self.ruler_height = ruler_height or 200
13 self.ruler_length = ruler_length or 15
17 self.drawing = SVGdraw.drawing()
18 self.svg = SVGdraw.svg()
20 # create the body of the ruler
21 body = SVGdraw.rect(0, 0,
22 (self.scale * self.ruler_length),
23 self.ruler_height, "white", "black", 2)
24 self.svg.addElement(body)
27 units = SVGdraw.text((self.scale / 2),
28 (self.ruler_height - self.scale / 5),
29 self.units, font_height)
30 self.svg.addElement(units)
35 # find the appropriate subunit marker
36 self.subunit_marks = 1
38 while self.scale / self.subunit_marks >= font_height * 3:
39 if self.subunit_marks == 1:
40 self.subunit_marks += 3
42 self.subunit_marks = self.subunit_marks * 2
45 for i in range(self.ruler_length):
48 for i2 in range(self.subunit_marks):
52 self.svg.addElement(SVGdraw.text((self.point - font_height * 1.4),
53 (self.ruler_height * 0.6 - 5),
54 str(i + 1), font_height))
57 self.drawing.setSVG(self.svg)
61 xml = cStringIO.StringIO()
62 xml.write("<?xml version='1.0' encoding='UTF-8'?>\n")
63 xml.write("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \"http://www .w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd \">\n")
66 return(xml.getvalue())
68 def write_svg_to_file(self, filename='output.svg'):
69 self.drawing.toXml(filename)
71 def __drawline(self, size):
73 self.point = self.point + (self.scale / (1 + self.subunit_marks))
74 line = SVGdraw.line(self.point, 0, self.point,
75 (self.ruler_height * size), "black", 2)
76 self.svg.addElement(line)