fixed up the database and some other minor bugs
[yourule] / svgruler.py
1 # define the length of things
2 from __future__ import division
3 import SVGdraw
4
5 class SVGRuler:
6     def __init__(self, scale=None, units=None,
7                  ruler_height=None, ruler_length=None):
8
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
14
15         font_height = 22
16
17         self.drawing = SVGdraw.drawing()
18         self.svg = SVGdraw.svg()
19
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)
25
26         # add a units label
27         units = SVGdraw.text((self.scale / 2),
28                              (self.ruler_height - self.scale / 5),
29                              self.units, font_height)
30         self.svg.addElement(units)
31
32         self.point = 0.0
33
34
35         # find the appropriate subunit marker
36         self.subunit_marks = 1
37
38         while self.scale / self.subunit_marks >= font_height * 3:
39             if self.subunit_marks == 1:
40                 self.subunit_marks += 3
41             else:
42                 self.subunit_marks = self.subunit_marks * 2
43
44         # for every unit 
45         for i in range(self.ruler_length):
46
47             # place subunit marks
48             for i2 in range(self.subunit_marks):
49                 self.__drawline(0.3)
50             
51             self.__drawline(0.6)
52             self.svg.addElement(SVGdraw.text((self.point - font_height * 1.15),
53                            (self.ruler_height * 0.6 - 5),
54                             str(i + 1), font_height))
55     
56
57         self.drawing.setSVG(self.svg)
58
59     def getxml(self):
60         import cStringIO
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")      
64         self.svg.toXml(0,xml)
65
66         return(xml.getvalue())
67
68     def write_svg_to_file(self, filename='output.svg'):
69         self.drawing.toXml(filename)
70
71     def __drawline(self, size):
72         
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)
77
78

Benjamin Mako Hill || Want to submit a patch?