1 # -*- encoding: utf-8 -*-
4 # Copyright (C) 2002-2004 Jörg Lehmann <joergl@users.sourceforge.net>
5 # Copyright (C) 2002-2004 André Wobst <wobsta@users.sourceforge.net>
7 # This file is part of PyX (http://pyx.sourceforge.net/).
9 # PyX is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # PyX is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with PyX; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 scale
= dict(u
=1, v
=1, w
=1, x
=1)
37 def set(uscale
=None, vscale
=None, wscale
=None, xscale
=None, defaultunit
=None):
38 if uscale
is not None:
40 if vscale
is not None:
42 if wscale
is not None:
44 if xscale
is not None:
46 if defaultunit
is not None:
48 _default_unit
= defaultunit
51 def _convert_to(l
, dest_unit
="m"):
52 if isinstance(l
, length
):
53 return (l
.t
+ l
.u
*scale
["u"] + l
.v
*scale
["v"] + l
.w
*scale
["w"] + l
.x
*scale
["x"]) / _m
[dest_unit
]
55 return l
* _m
[_default_unit
] * scale
["u"] / _m
[dest_unit
]
58 return _convert_to(l
, "m")
61 return _convert_to(l
, "cm")
64 return _convert_to(l
, "mm")
67 return _convert_to(l
, "inch")
70 return _convert_to(l
, "pt")
72 ################################################################################
73 # class for generic length
74 ################################################################################
79 PyX lengths are composed of five components (t=true, u=user, v=visual,
80 w=width, and x=TeX) which can be scaled separately (except for the true
81 component, which is always unscaled). Lengths can be constructed in units
82 of "pt", "mm", "cm", "m" and "inch". When no unit is given, a module
83 default is used, which can be changed with the help of the module level function
87 def __init__(self
, f
=0, type="u", unit
=None):
88 """ create a length instance of the given type with a length f
89 in the given unit. If unit is not set, the currently set default unit is used.
91 self
.t
= self
.u
= self
.v
= self
.w
= self
.x
= 0
92 l
= float(f
) * _m
[unit
or _default_unit
]
104 def __cmp__(self
, other
):
105 # we try to convert self and other into meters and
106 # if this fails, we give other a chance to do the comparison
108 return cmp(tom(self
), tom(other
))
110 # why does -cmp(other, self) not work?
111 return -other
.__cmp
__(self
)
113 def __mul__(self
, factor
):
115 result
.t
= factor
* self
.t
116 result
.u
= factor
* self
.u
117 result
.v
= factor
* self
.v
118 result
.w
= factor
* self
.w
119 result
.x
= factor
* self
.x
124 def __div__(self
, divisor
):
125 if isinstance(divisor
, length
):
126 return tom(self
) / tom(divisor
)
128 result
.t
= self
.t
/ divisor
129 result
.u
= self
.u
/ divisor
130 result
.v
= self
.v
/ divisor
131 result
.w
= self
.w
/ divisor
132 result
.x
= self
.x
/ divisor
135 __truediv__
= __div__
137 def __add__(self
, other
):
138 # convert to length if necessary
139 if not isinstance(other
, length
):
140 # if other is not a length, we try to convert it into a length and
141 # if this fails, we give other a chance to do the addition
143 other
= length(other
)
147 result
.t
= self
.t
+ other
.t
148 result
.u
= self
.u
+ other
.u
149 result
.v
= self
.v
+ other
.v
150 result
.w
= self
.w
+ other
.w
151 result
.x
= self
.x
+ other
.x
156 def __sub__(self
, other
):
157 # convert to length if necessary
158 if not isinstance(other
, length
):
159 # if other is not a length, we try to convert it into a length and
160 # if this fails, we give other a chance to do the subtraction
162 other
= length(other
)
166 result
.t
= self
.t
- other
.t
167 result
.u
= self
.u
- other
.u
168 result
.v
= self
.v
- other
.v
169 result
.w
= self
.w
- other
.w
170 result
.x
= self
.x
- other
.x
173 def __rsub__(self
, other
):
174 # convert to length if necessary
175 if not isinstance(other
, length
):
176 other
= length(other
)
178 result
.t
= other
.t
- self
.t
179 result
.u
= other
.u
- self
.u
180 result
.v
= other
.v
- self
.v
181 result
.w
= other
.w
- self
.w
182 result
.x
= other
.x
- self
.x
195 return "(%(t)f t + %(u)f u + %(v)f v + %(w)f w + %(x)f x) m" % self
.__dict
__
198 ################################################################################
199 # predefined instances which can be used as length units
200 ################################################################################
202 # user lengths and unqualified length which are also user length
203 u_pt
= pt
= length(1, type="u", unit
="pt")
204 u_m
= m
= length(1, type="u", unit
="m")
205 u_mm
= mm
= length(1, type="u", unit
="mm")
206 u_cm
= cm
= length(1, type="u", unit
="cm")
207 u_inch
= inch
= length(1, type="u", unit
="inch")
210 t_pt
= length(1, type="t", unit
="pt")
211 t_m
= length(1, type="t", unit
="m")
212 t_mm
= length(1, type="t", unit
="mm")
213 t_cm
= length(1, type="t", unit
="cm")
214 t_inch
= length(1, type="t", unit
="inch")
217 v_pt
= length(1, type="v", unit
="pt")
218 v_m
= length(1, type="v", unit
="m")
219 v_mm
= length(1, type="v", unit
="mm")
220 v_cm
= length(1, type="v", unit
="cm")
221 v_inch
= length(1, type="v", unit
="inch")
224 w_pt
= length(1, type="w", unit
="pt")
225 w_m
= length(1, type="w", unit
="m")
226 w_mm
= length(1, type="w", unit
="mm")
227 w_cm
= length(1, type="w", unit
="cm")
228 w_inch
= length(1, type="w", unit
="inch")
231 x_pt
= length(1, type="x", unit
="pt")
232 x_m
= length(1, type="x", unit
="m")
233 x_mm
= length(1, type="x", unit
="mm")
234 x_cm
= length(1, type="x", unit
="cm")
235 x_inch
= length(1, type="x", unit
="inch")