2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2002-2004 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2002-2004 André Wobst <wobsta@users.sourceforge.net>
8 # This file is part of PyX (http://pyx.sourceforge.net/).
10 # PyX is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # PyX is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with PyX; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 scale
= { 't':1, 'u':1, 'v':1, 'w':1, 'x':1 }
32 unit_pattern
= re
.compile(r
"""^\s*([+-]?\d*((\d\.?)|(\.?\d))\d*(E[+-]?\d+)?)
34 (\s+(([a-z][a-z]+)|[^t-x]))?\s*$""",
35 re
.IGNORECASE | re
.VERBOSE
)
46 def set(uscale
=None, vscale
=None, wscale
=None, xscale
=None, defaultunit
=None):
47 if uscale
is not None:
49 if vscale
is not None:
51 if wscale
is not None:
53 if xscale
is not None:
55 if defaultunit
is not None:
57 _default_unit
= defaultunit
60 def _convert_to(l
, dest_unit
="m"):
61 if type(l
) in (types
.IntType
, types
.LongType
, types
.FloatType
):
62 return l
*_m
[_default_unit
]*scale
['u']/_m
[dest_unit
]
63 elif not isinstance(l
, length
):
64 l
=length(l
) # convert to length instance if necessary
66 return ( l
.length
['t'] +
67 l
.length
['u']*scale
['u'] +
68 l
.length
['v']*scale
['v'] +
69 l
.length
['w']*scale
['w'] +
70 l
.length
['x']*scale
['x'] ) / _m
[dest_unit
]
73 return _convert_to(l
, "m")
76 return _convert_to(l
, "cm")
79 return _convert_to(l
, "mm")
82 return _convert_to(l
, "inch")
85 return _convert_to(l
, "pt")
87 ################################################################################
88 # class for generic length
89 ################################################################################
94 Lengths can either be a initialized with a number or a string:
96 - a length specified as a number corresponds to the default values of
97 unit_type and unit_name
98 - a string has to consist of a maximum of three parts:
99 -quantifier: integer/float value
100 -unit_type: "t", "u", "v", "w", or "x".
101 Optional, defaults to "u"
102 -unit_name: "m", "cm", "mm", "inch", "pt".
103 Optional, defaults to _default_unit
105 Internally all length are stored in units of m as a quadruple of the four
110 def __init__(self
, l
=1, default_type
="u", dunit
=None):
111 self
.length
= { 't': 0 , 'u': 0, 'v': 0, 'w': 0, 'x': 0 }
113 if isinstance(l
, length
):
114 self
.length
= l
.length
115 elif helper
.isnumber(l
):
116 self
.length
[default_type
] = l
*_m
[dunit
or _default_unit
]
117 elif helper
.isstring(l
):
118 unit_match
= re
.match(unit_pattern
, l
)
119 if unit_match
is None:
120 raise ValueError("expecting number or string of the form 'number [u|v|w|x] unit'")
122 self
.prefactor
= float(unit_match
.group(1))
123 self
.unit_type
= unit_match
.group(7) or default_type
124 self
.unit_name
= unit_match
.group(9) or dunit
or _default_unit
126 self
.length
[self
.unit_type
] = self
.prefactor
*_m
[self
.unit_name
]
128 raise NotImplementedError("cannot convert given argument to length type")
130 def __cmp__(self
, other
):
131 return cmp(tom(self
), tom(other
))
133 def __mul__(self
, factor
):
134 newlength
= self
.__class
__()
135 for unit_type
in newlength
.length
.keys():
136 newlength
.length
[unit_type
] = self
.length
[unit_type
]*factor
141 def __div__(self
, factor
):
142 newlength
= self
.__class
__()
143 for unit_type
in newlength
.length
.keys():
144 newlength
.length
[unit_type
] = self
.length
[unit_type
]/factor
147 def __add__(self
, l
):
148 # convert to length if necessary
150 newlength
= self
.__class
__()
151 for unit_type
in newlength
.length
.keys():
152 newlength
.length
[unit_type
] = self
.length
[unit_type
] + ll
.length
[unit_type
]
157 def __sub__(self
, l
):
158 # convert to length if necessary
160 newlength
= self
.__class
__()
161 for unit_type
in newlength
.length
.keys():
162 newlength
.length
[unit_type
] = self
.length
[unit_type
] - ll
.length
[unit_type
]
165 def __rsub__(self
, l
):
166 # convert to length if necessary
168 newlength
= self
.__class
__()
169 for unit_type
in newlength
.length
.keys():
170 newlength
.length
[unit_type
] = ll
.length
[unit_type
] - self
.length
[unit_type
]
174 newlength
= self
.__class
__()
175 for unit_type
in newlength
.length
.keys():
176 newlength
.length
[unit_type
] = -self
.length
[unit_type
]
180 return "(%(t)f t + %(u)f u + %(v)f v + %(w)f w + %(x)f x) m" % self
.length
183 ################################################################################
184 # class for more specialized lengths
185 ################################################################################
187 # lengths with user units as default
190 def __init__(self
, l
=1, default_type
="u"):
191 length
.__init
__(self
, l
, default_type
=default_type
, dunit
="pt")
195 def __init__(self
, l
=1, default_type
="u"):
196 length
.__init
__(self
, l
, default_type
=default_type
, dunit
="m")
200 def __init__(self
, l
=1, default_type
="u"):
201 length
.__init
__(self
, l
, default_type
=default_type
, dunit
="mm")
205 def __init__(self
, l
=1, default_type
="u"):
206 length
.__init
__(self
, l
, default_type
=default_type
, dunit
="cm")
209 class u_inch(length
):
210 def __init__(self
, l
=1, default_type
="u"):
211 length
.__init
__(self
, l
, default_type
=default_type
, dunit
="inch")
213 # without further specification, length are user length. Hence we
214 # define the following aliases
225 def __init__(self
, l
=1):
226 length
.__init
__(self
, l
, default_type
="t", dunit
="pt")
230 def __init__(self
, l
=1):
231 length
.__init
__(self
, l
, default_type
="t", dunit
="m")
235 def __init__(self
, l
=1):
236 length
.__init
__(self
, l
, default_type
="t", dunit
="cm")
240 def __init__(self
, l
=1):
241 length
.__init
__(self
, l
, default_type
="t", dunit
="mm")
244 class t_inch(length
):
245 def __init__(self
, l
=1):
246 length
.__init
__(self
, l
, default_type
="t", dunit
="inch")
251 def __init__(self
, l
=1):
252 length
.__init
__(self
, l
, default_type
="v", dunit
="pt")
256 def __init__(self
, l
=1):
257 length
.__init
__(self
, l
, default_type
="v", dunit
="m")
261 def __init__(self
, l
=1):
262 length
.__init
__(self
, l
, default_type
="v", dunit
="cm")
266 def __init__(self
, l
=1):
267 length
.__init
__(self
, l
, default_type
="v", dunit
="mm")
270 class v_inch(length
):
271 def __init__(self
, l
=1):
272 length
.__init
__(self
, l
, default_type
="v", dunit
="inch")
277 def __init__(self
, l
=1):
278 length
.__init
__(self
, l
, default_type
="w", dunit
="pt")
282 def __init__(self
, l
=1):
283 length
.__init
__(self
, l
, default_type
="w", dunit
="m")
287 def __init__(self
, l
=1):
288 length
.__init
__(self
, l
, default_type
="w", dunit
="cm")
292 def __init__(self
, l
=1):
293 length
.__init
__(self
, l
, default_type
="w", dunit
="mm")
296 class w_inch(length
):
297 def __init__(self
, l
=1):
298 length
.__init
__(self
, l
, default_type
="w", dunit
="inch")
303 def __init__(self
, l
=1):
304 length
.__init
__(self
, l
, default_type
="x", dunit
="pt")
308 def __init__(self
, l
=1):
309 length
.__init
__(self
, l
, default_type
="x", dunit
="m")
313 def __init__(self
, l
=1):
314 length
.__init
__(self
, l
, default_type
="x", dunit
="cm")
318 def __init__(self
, l
=1):
319 length
.__init
__(self
, l
, default_type
="x", dunit
="mm")
322 class x_inch(length
):
323 def __init__(self
, l
=1):
324 length
.__init
__(self
, l
, default_type
="x", dunit
="inch")