- added new helper methods _distributeparams and _findnormpathitem to
[PyX/mjg.git] / pyx / style.py
blob5cfa61bc65fde32d0f8b6db120a740faec151407
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2002-2004 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2003-2004 Michael Schindler <m-schindler@users.sourceforge.net>
7 # Copyright (C) 2002-2004 André Wobst <wobsta@users.sourceforge.net>
9 # This file is part of PyX (http://pyx.sourceforge.net/).
11 # PyX is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # PyX is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with PyX; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 import math
26 import attr, unit, base
29 # base classes for stroke and fill styles
32 class strokestyle(base.canvasitem): pass
34 class fillstyle(base.canvasitem): pass
37 # common stroke styles
41 class linecap(attr.exclusiveattr, strokestyle):
43 """linecap of paths"""
45 def __init__(self, value=0):
46 attr.exclusiveattr.__init__(self, linecap)
47 self.value = value
49 def outputPS(self, file):
50 file.write("%d setlinecap\n" % self.value)
52 def outputPDF(self, file):
53 file.write("%d J\n" % self.value)
55 linecap.butt = linecap(0)
56 linecap.round = linecap(1)
57 linecap.square = linecap(2)
58 linecap.clear = attr.clearclass(linecap)
61 class linejoin(attr.exclusiveattr, strokestyle):
63 """linejoin of paths"""
65 def __init__(self, value=0):
66 attr.exclusiveattr.__init__(self, linejoin)
67 self.value = value
69 def outputPS(self, file):
70 file.write("%d setlinejoin\n" % self.value)
72 def outputPDF(self, file):
73 file.write("%d j\n" % self.value)
75 linejoin.miter = linejoin(0)
76 linejoin.round = linejoin(1)
77 linejoin.bevel = linejoin(2)
78 linejoin.clear = attr.clearclass(linejoin)
81 class miterlimit(attr.exclusiveattr, strokestyle):
83 """miterlimit of paths"""
85 def __init__(self, value=10.0):
86 attr.exclusiveattr.__init__(self, miterlimit)
87 self.value = value
89 def outputPS(self, file):
90 file.write("%f setmiterlimit\n" % self.value)
92 def outoutPDF(self, file):
93 file.write("%f M\n" % self.value)
95 miterlimit.lessthan180deg = miterlimit(1/math.sin(math.pi*180/360))
96 miterlimit.lessthan90deg = miterlimit(1/math.sin(math.pi*90/360))
97 miterlimit.lessthan60deg = miterlimit(1/math.sin(math.pi*60/360))
98 miterlimit.lessthan45deg = miterlimit(1/math.sin(math.pi*45/360))
99 miterlimit.lessthan11deg = miterlimit(10) # the default, approximately 11.4783 degress
100 miterlimit.clear = attr.clearclass(miterlimit)
103 class dash(attr.exclusiveattr, strokestyle):
105 """dash of paths"""
107 def __init__(self, pattern=[], offset=0, rellengths=0):
108 """set pattern with offset.
110 If rellengths is True, interpret all dash lengths relative to current linewidth.
112 attr.exclusiveattr.__init__(self, dash)
113 self.pattern = pattern
114 self.offset = offset
115 self.rellengths = rellengths
117 def outputPS(self, file):
118 if self.rellengths:
119 sep = " currentlinewidth mul "
120 else:
121 sep = " "
122 patternstring = sep.join(["%f" % element for element in self.pattern])
123 file.write("[%s] %d setdash\n" % (patternstring, self.offset))
125 def outputPDF(self, file):
126 if self.rellengths:
127 raise RuntimeError("rellengths currently not supported in pdf output")
128 file.write("[%s] %d d\n" % (" ".join(["%f" % element for element in self.pattern]), self.offset))
130 dash.clear = attr.clearclass(dash)
133 class linestyle(attr.exclusiveattr, strokestyle):
135 """linestyle (linecap together with dash) of paths"""
137 def __init__(self, c=linecap.butt, d=dash([])):
138 # XXX better, but at the moment not supported by attr.exlusiveattr would be:
139 # XXX attr.exclusiveattr.__init__(self, [linestyle, linecap, dash])
140 attr.exclusiveattr.__init__(self, linestyle)
141 self.c = c
142 self.d = d
144 def outputPS(self, file):
145 self.c.outputPS(file)
146 self.d.outputPS(file)
148 def outputPDF(self, file):
149 self.c.outputPDF(file)
150 self.d.outputPDF(file)
152 linestyle.solid = linestyle(linecap.butt, dash([]))
153 linestyle.dashed = linestyle(linecap.butt, dash([2]))
154 linestyle.dotted = linestyle(linecap.round, dash([0, 2]))
155 linestyle.dashdotted = linestyle(linecap.round, dash([0, 2, 2, 2]))
156 linestyle.clear = attr.clearclass(linestyle)
159 class linewidth(unit.length, attr.sortbeforeexclusiveattr, strokestyle):
161 """linewidth of paths"""
163 def __init__(self, l):
164 unit.length.__init__(self, 0)
165 self.t = l.t
166 self.u = l.u
167 self.v = l.v
168 self.w = l.w
169 self.x = l.x
170 attr.sortbeforeexclusiveattr.__init__(self, linewidth, [dash, linestyle])
172 def outputPS(self, file):
173 file.write("%f setlinewidth\n" % unit.topt(self))
175 def outputPDF(self, file):
176 file.write("%f w\n" % unit.topt(self))
178 _base = 0.02 * unit.w_cm
180 linewidth.THIN = linewidth(_base/math.sqrt(32))
181 linewidth.THIn = linewidth(_base/math.sqrt(16))
182 linewidth.THin = linewidth(_base/math.sqrt(8))
183 linewidth.Thin = linewidth(_base/math.sqrt(4))
184 linewidth.thin = linewidth(_base/math.sqrt(2))
185 linewidth.normal = linewidth(_base)
186 linewidth.thick = linewidth(_base*math.sqrt(2))
187 linewidth.Thick = linewidth(_base*math.sqrt(4))
188 linewidth.THick = linewidth(_base*math.sqrt(8))
189 linewidth.THIck = linewidth(_base*math.sqrt(16))
190 linewidth.THICk = linewidth(_base*math.sqrt(32))
191 linewidth.THICK = linewidth(_base*math.sqrt(64))
192 linewidth.clear = attr.clearclass(linewidth)