3 # YouRule: Onscreen Ruler Generator
5 # Copyright (C) 2007 Benjamin Mako Hill <mako@atdot.cc>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the Affero General Public License as published
9 # by the Free Software Foundation, either version 1 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the Affero General Public License
18 # along with this program. If not, see
19 # <http://http://www.affero.org/oagpl.html>.
21 # define the length of things
26 def __init__(self, scale=None, units=None,
27 ruler_height=None, ruler_length=None):
29 # default values come from OLPC
30 self.scale = scale or 78.740
31 self.units = units or 'centimeters'
32 self.ruler_height = ruler_height or 200
33 self.ruler_length = ruler_length or 15
37 self.drawing = SVGdraw.drawing()
38 self.svg = SVGdraw.svg()
40 # create the body of the ruler
41 body = SVGdraw.rect(0, 0,
42 (self.scale * self.ruler_length),
43 self.ruler_height, "white", "black", 2)
44 self.svg.addElement(body)
47 units = SVGdraw.text((self.scale / 2),
48 (self.ruler_height - self.scale / 5),
49 self.units, font_height)
50 self.svg.addElement(units)
55 # find the appropriate subunit marker
56 self.subunit_marks = 1
58 while self.scale / self.subunit_marks >= font_height * 3:
59 if self.subunit_marks == 1:
60 self.subunit_marks += 3
62 self.subunit_marks = self.subunit_marks * 2
65 for i in range(self.ruler_length):
68 for i2 in range(self.subunit_marks):
72 self.svg.addElement(SVGdraw.text((self.point - font_height * 1.15),
73 (self.ruler_height * 0.6 - 5),
74 str(i + 1), font_height))
77 self.drawing.setSVG(self.svg)
82 xml.write("<?xml version='1.0' encoding='UTF-8'?>\n")
83 xml.write("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \"http://www .w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd \">\n")
86 return(xml.getvalue())
88 def write_svg_to_file(self, filename='output.svg'):
89 self.drawing.toXml(filename)
91 def __drawline(self, size):
93 self.point = self.point + (self.scale / (1 + self.subunit_marks))
94 line = SVGdraw.line(self.point, 0, self.point,
95 (self.ruler_height * size), "black", 2)
96 self.svg.addElement(line)