updates for python3
[yourule] / svgruler.py
1 #!/usr/bin/env python
2
3 # YouRule: Onscreen Ruler Generator
4 #
5 # Copyright (C) 2007 Benjamin Mako Hill <mako@atdot.cc>
6 #
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.
11 #
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.
16 #
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>.
20
21 # define the length of things
22
23 import SVGdraw
24
25 class SVGRuler:
26     def __init__(self, scale=None, units=None,
27                  ruler_height=None, ruler_length=None):
28
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
34
35         font_height = 22
36
37         self.drawing = SVGdraw.drawing()
38         self.svg = SVGdraw.svg()
39
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)
45
46         # add a units label
47         units = SVGdraw.text((self.scale / 2),
48                              (self.ruler_height - self.scale / 5),
49                              self.units, font_height)
50         self.svg.addElement(units)
51
52         self.point = 0.0
53
54
55         # find the appropriate subunit marker
56         self.subunit_marks = 1
57
58         while self.scale / self.subunit_marks >= font_height * 3:
59             if self.subunit_marks == 1:
60                 self.subunit_marks += 3
61             else:
62                 self.subunit_marks = self.subunit_marks * 2
63
64         # for every unit 
65         for i in range(self.ruler_length):
66
67             # place subunit marks
68             for i2 in range(self.subunit_marks):
69                 self.__drawline(0.3)
70             
71             self.__drawline(0.6)
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))
75     
76
77         self.drawing.setSVG(self.svg)
78
79     def getxml(self):
80         import io
81         xml = io.StringIO()
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")      
84         self.svg.toXml(0,xml)
85
86         return(xml.getvalue())
87
88     def write_svg_to_file(self, filename='output.svg'):
89         self.drawing.toXml(filename)
90
91     def __drawline(self, size):
92         
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)
97
98

Benjamin Mako Hill || Want to submit a patch?