further adjust some copyright dates
[PyX/mjg.git] / pyx / style.py
blob2ba48b70a89a8874b944323d359e9186f6dec4bb
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2002-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 import math
26 import attr, unit, canvas
29 # base classes for stroke and fill styles
32 class strokestyle(canvas.canvasitem): pass
34 class fillstyle(canvas.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, writer, context):
50 file.write("%d setlinecap\n" % self.value)
52 def outputPDF(self, file, writer, context):
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, writer, context):
70 file.write("%d setlinejoin\n" % self.value)
72 def outputPDF(self, file, writer, context):
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, writer, context):
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, writer, context):
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, writer, context):
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, writer, context):
145 self.c.outputPS(file, writer, context)
146 self.d.outputPS(file, writer, context)
148 def outputPDF(self, file, writer, context):
149 self.c.outputPDF(file, writer, context)
150 self.d.outputPDF(file, writer, context)
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(attr.sortbeforeexclusiveattr, strokestyle):
161 """linewidth of paths"""
163 def __init__(self, width):
164 attr.sortbeforeexclusiveattr.__init__(self, linewidth, [dash, linestyle])
165 self.width = width
167 def outputPS(self, file, writer, context):
168 file.write("%f setlinewidth\n" % unit.topt(self.width))
169 context.linewidth_pt = unit.topt(self.width)
171 def outputPDF(self, file, writer, context):
172 file.write("%f w\n" % unit.topt(self.width))
173 context.linewidth_pt = unit.topt(self.width)
175 _base = 0.02 * unit.w_cm
177 linewidth.THIN = linewidth(_base/math.sqrt(32))
178 linewidth.THIn = linewidth(_base/math.sqrt(16))
179 linewidth.THin = linewidth(_base/math.sqrt(8))
180 linewidth.Thin = linewidth(_base/math.sqrt(4))
181 linewidth.thin = linewidth(_base/math.sqrt(2))
182 linewidth.normal = linewidth(_base)
183 linewidth.thick = linewidth(_base*math.sqrt(2))
184 linewidth.Thick = linewidth(_base*math.sqrt(4))
185 linewidth.THick = linewidth(_base*math.sqrt(8))
186 linewidth.THIck = linewidth(_base*math.sqrt(16))
187 linewidth.THICk = linewidth(_base*math.sqrt(32))
188 linewidth.THICK = linewidth(_base*math.sqrt(64))
189 linewidth.clear = attr.clearclass(linewidth)