- added new helper methods _distributeparams and _findnormpathitem to
[PyX/mjg.git] / pyx / unit.py
blob8dcc5ab575cdac3a410ebe365dfc0fba72e3fee5
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) 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
24 import copy
25 import types
26 import helper
28 scale = { 't':1, 'u':1, 'v':1, 'w':1, 'x':1 }
30 _default_unit = "cm"
32 _m = {
33 'm' : 1,
34 'cm': 0.01,
35 'mm': 0.001,
36 'inch': 0.01*2.54,
37 'pt': 0.01*2.54/72,
40 def set(uscale=None, vscale=None, wscale=None, xscale=None, defaultunit=None):
41 if uscale is not None:
42 scale['u'] = uscale
43 if vscale is not None:
44 scale['v'] = vscale
45 if wscale is not None:
46 scale['w'] = wscale
47 if xscale is not None:
48 scale['x'] = xscale
49 if defaultunit is not None:
50 global _default_unit
51 _default_unit = defaultunit
54 def _convert_to(l, dest_unit="m"):
55 if type(l) in (types.IntType, types.LongType, types.FloatType):
56 return l * _m[_default_unit] * scale['u'] / _m[dest_unit]
57 elif not isinstance(l, length):
58 l = length(l) # convert to length instance if necessary
60 return (l.t + l.u*scale['u'] + l.v*scale['v'] + l.w*scale['w'] + l.x*scale['x']) / _m[dest_unit]
62 def tom(l):
63 return _convert_to(l, "m")
65 def tocm(l):
66 return _convert_to(l, "cm")
68 def tomm(l):
69 return _convert_to(l, "mm")
71 def toinch(l):
72 return _convert_to(l, "inch")
74 def topt(l):
75 return _convert_to(l, "pt")
77 ################################################################################
78 # class for generic length
79 ################################################################################
81 class length:
82 """ PyX lengths
84 PyX lengths are composed of five components (t=true, u=user, v=visual,
85 w=width, and x=TeX) which can be scaled separately (except for the true
86 component, which is always unscaled). Lengths can be constructed in units
87 of "pt", "mm", "cm", "m" and "inch". When no unit is given, a module
88 default is used, which can be changed with the help of the set method of
89 the present module.
90 """
92 def __init__(self, f, type="u", unit=None):
93 """ create a length instance of the given type with a length f
94 in the given unit. If unit is not set, the currently set default unit is used.
95 """
96 self.t = self.u = self.v = self.w = self.x = 0
97 l = f * _m[unit or _default_unit]
98 if type == "t":
99 self.t = l
100 elif type == "u":
101 self.u = l
102 elif type == "v":
103 self.v = l
104 elif type == "w":
105 self.w = l
106 elif type == "x":
107 self.x = l
109 def __cmp__(self, other):
110 return cmp(tom(self), tom(other))
112 def __mul__(self, factor):
113 result = copy.copy(self)
114 result.t *= factor
115 result.u *= factor
116 result.v *= factor
117 result.w *= factor
118 result.x *= factor
119 return result
121 __rmul__=__mul__
123 def __div__(self, factor):
124 result = copy.copy(self)
125 result.t /= factor
126 result.u /= factor
127 result.v /= factor
128 result.w /= factor
129 result.x /= factor
130 return result
132 def __add__(self, other):
133 # convert to length if necessary
134 if not isinstance(other, length):
135 other = length(other)
136 result = copy.copy(self)
137 result.t += other.t
138 result.u += other.u
139 result.v += other.v
140 result.w += other.w
141 result.x += other.x
142 return result
144 __radd__=__add__
146 def __sub__(self, other):
147 # convert to length if necessary
148 if not isinstance(other, length):
149 other = length(other)
150 result = copy.copy(self)
151 result.t -= other.t
152 result.u -= other.u
153 result.v -= other.v
154 result.w -= other.w
155 result.x -= other.x
156 return result
158 def __rsub__(self, other):
159 # convert to length if necessary
160 if not isinstance(other, length):
161 other = length(other)
162 result = copy.copy(self)
163 result.t = other.t - self.t
164 result.u = other.u - self.u
165 result.v = other.v - self.v
166 result.w = other.w - self.w
167 result.x = other.x - self.x
168 return result
170 def __neg__(self):
171 result = copy.copy(self)
172 result.t *= -1
173 result.u *= -1
174 result.v *= -1
175 result.w *= -1
176 result.x *= -1
177 return result
179 def __str__(self):
180 return "(%(t)f t + %(u)f u + %(v)f v + %(w)f w + %(x)f x) m" % self.__dict__
183 ################################################################################
184 # predefined instances which can be used as length units
185 ################################################################################
187 # user lengths and unqualified length which are also user length
188 u_pt = pt = length(1, type="u", unit="pt")
189 u_m = m = length(1, type="u", unit="m")
190 u_mm = mm = length(1, type="u", unit="mm")
191 u_cm = cm = length(1, type="u", unit="cm")
192 u_inch = inch = length(1, type="u", unit="inch")
194 # true lengths
195 t_pt = length(1, type="t", unit="pt")
196 t_m = length(1, type="t", unit="m")
197 t_mm = length(1, type="t", unit="mm")
198 t_cm = length(1, type="t", unit="cm")
199 t_inch = length(1, type="t", unit="inch")
201 # visual lengths
202 v_pt = length(1, type="v", unit="pt")
203 v_m = length(1, type="v", unit="m")
204 v_mm = length(1, type="v", unit="mm")
205 v_cm = length(1, type="v", unit="cm")
206 v_inch = length(1, type="v", unit="inch")
208 # width lengths
209 w_pt = length(1, type="w", unit="pt")
210 w_m = length(1, type="w", unit="m")
211 w_mm = length(1, type="w", unit="mm")
212 w_cm = length(1, type="w", unit="cm")
213 w_inch = length(1, type="w", unit="inch")
215 # TeX lengths
216 x_pt = length(1, type="x", unit="pt")
217 x_m = length(1, type="x", unit="m")
218 x_mm = length(1, type="x", unit="mm")
219 x_cm = length(1, type="x", unit="cm")
220 x_inch = length(1, type="x", unit="inch")