~ubuntu-branches/ubuntu/trusty/pyx/trusty

« back to all changes in this revision

Viewing changes to pyx/unit.py

  • Committer: Bazaar Package Importer
  • Author(s): Graham Wilson
  • Date: 2004-12-25 06:42:57 UTC
  • Revision ID: james.westby@ubuntu.com-20041225064257-31469ij5uysqq302
Tags: upstream-0.7.1
Import upstream version 0.7.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- coding: ISO-8859-1 -*-
 
3
#
 
4
#
 
5
# Copyright (C) 2002-2004 J�rg Lehmann <joergl@users.sourceforge.net>
 
6
# Copyright (C) 2002-2004 Andr� Wobst <wobsta@users.sourceforge.net>
 
7
#
 
8
# This file is part of PyX (http://pyx.sourceforge.net/).
 
9
#
 
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.
 
14
#
 
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.
 
19
#
 
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
 
23
 
 
24
import types
 
25
import helper
 
26
 
 
27
scale = { 't':1, 'u':1, 'v':1, 'w':1, 'x':1 }
 
28
 
 
29
_default_unit = "cm"
 
30
 
 
31
_m = { 
 
32
      'm' :   1,
 
33
      'cm':   0.01,
 
34
      'mm':   0.001,
 
35
      'inch': 0.01*2.54,
 
36
      'pt':   0.01*2.54/72,
 
37
    }
 
38
 
 
39
def set(uscale=None, vscale=None, wscale=None, xscale=None, defaultunit=None):
 
40
    if uscale is not None:
 
41
        scale['u'] = uscale
 
42
    if vscale is not None:
 
43
        scale['v'] = vscale
 
44
    if wscale is not None:
 
45
        scale['w'] = wscale
 
46
    if xscale is not None:
 
47
        scale['x'] = xscale
 
48
    if defaultunit is not None:
 
49
        global _default_unit
 
50
        _default_unit = defaultunit
 
51
 
 
52
 
 
53
def _convert_to(l, dest_unit="m"):
 
54
    if type(l) in (types.IntType, types.LongType, types.FloatType):
 
55
        return l * _m[_default_unit] * scale['u'] / _m[dest_unit]
 
56
    elif not isinstance(l, length): 
 
57
        l = length(l)       # convert to length instance if necessary
 
58
 
 
59
    return (l.t + l.u*scale['u'] + l.v*scale['v'] + l.w*scale['w'] + l.x*scale['x']) / _m[dest_unit]
 
60
 
 
61
def tom(l):
 
62
    return _convert_to(l, "m")
 
63
 
 
64
def tocm(l):
 
65
    return _convert_to(l, "cm")
 
66
 
 
67
def tomm(l):
 
68
    return _convert_to(l, "mm")
 
69
 
 
70
def toinch(l):
 
71
    return _convert_to(l, "inch")
 
72
 
 
73
def topt(l):
 
74
    return _convert_to(l, "pt")
 
75
 
 
76
################################################################################
 
77
# class for generic length
 
78
################################################################################
 
79
 
 
80
class length:
 
81
    """ PyX lengths
 
82
 
 
83
    PyX lengths are composed of five components (t=true, u=user, v=visual,
 
84
    w=width, and x=TeX) which can be scaled separately (except for the true
 
85
    component, which is always unscaled). Lengths can be constructed in units
 
86
    of "pt", "mm", "cm", "m" and "inch". When no unit is given, a module
 
87
    default is used, which can be changed with the help of the set method of
 
88
    the present module.
 
89
    """
 
90
 
 
91
    def __init__(self, f=0, type="u", unit=None):
 
92
        """ create a length instance of the given type with a length f
 
93
        in the given unit. If unit is not set, the currently set default unit is used.
 
94
        """
 
95
        self.t = self.u = self.v = self.w = self.x = 0
 
96
        l = f * _m[unit or _default_unit]
 
97
        if type == "t":
 
98
            self.t = l
 
99
        elif type == "u":
 
100
            self.u = l
 
101
        elif type == "v":
 
102
            self.v = l
 
103
        elif type == "w":
 
104
            self.w = l
 
105
        elif type == "x":
 
106
            self.x = l
 
107
 
 
108
    def __cmp__(self, other):
 
109
        return cmp(tom(self), tom(other))
 
110
 
 
111
    def __mul__(self, factor):
 
112
        result = length()
 
113
        result.t = factor * self.t
 
114
        result.u = factor * self.u
 
115
        result.v = factor * self.v
 
116
        result.w = factor * self.w
 
117
        result.x = factor * self.x
 
118
        return result
 
119
 
 
120
    __rmul__=__mul__
 
121
 
 
122
    def __div__(self, factor):
 
123
        if isinstance(factor, length):
 
124
            return tom(self) / tom(factor)
 
125
        result = length()
 
126
        result.t = self.t / factor
 
127
        result.u = self.u / factor
 
128
        result.v = self.v / factor
 
129
        result.w = self.w / factor
 
130
        result.x = self.x / factor
 
131
        return result
 
132
 
 
133
    def __add__(self, other):
 
134
        # convert to length if necessary
 
135
        if not isinstance(other, length):
 
136
            other = length(other)
 
137
        result = length()
 
138
        result.t = self.t + other.t
 
139
        result.u = self.u + other.u
 
140
        result.v = self.v + other.v
 
141
        result.w = self.w + other.w
 
142
        result.x = self.x + other.x
 
143
        return result
 
144
 
 
145
    __radd__=__add__
 
146
 
 
147
    def __sub__(self, other):
 
148
        # convert to length if necessary
 
149
        if not isinstance(other, length):
 
150
            other = length(other)
 
151
        result = length()
 
152
        result.t = self.t - other.t
 
153
        result.u = self.u - other.u
 
154
        result.v = self.v - other.v
 
155
        result.w = self.w - other.w
 
156
        result.x = self.x - other.x
 
157
        return result
 
158
 
 
159
    def __rsub__(self, other):
 
160
        # convert to length if necessary
 
161
        if not isinstance(other, length):
 
162
            other = length(other)
 
163
        result = length()
 
164
        result.t = other.t - self.t
 
165
        result.u = other.u - self.u
 
166
        result.v = other.v - self.v
 
167
        result.w = other.w - self.w
 
168
        result.x = other.x - self.x
 
169
        return result
 
170
 
 
171
    def __neg__(self):
 
172
        result = length()
 
173
        result.t = -self.t
 
174
        result.u = -self.u
 
175
        result.v = -self.v
 
176
        result.w = -self.w
 
177
        result.x = -self.x
 
178
        return result
 
179
 
 
180
    def __str__(self):
 
181
        return "(%(t)f t + %(u)f u + %(v)f v + %(w)f w + %(x)f x) m" % self.__dict__
 
182
 
 
183
 
 
184
################################################################################
 
185
# predefined instances which can be used as length units
 
186
################################################################################
 
187
 
 
188
# user lengths and unqualified length which are also user length
 
189
u_pt   = pt   = length(1, type="u", unit="pt")
 
190
u_m    = m    = length(1, type="u", unit="m")
 
191
u_mm   = mm   = length(1, type="u", unit="mm")
 
192
u_cm   = cm   = length(1, type="u", unit="cm")
 
193
u_inch = inch = length(1, type="u", unit="inch")
 
194
 
 
195
# true lengths
 
196
t_pt   = length(1, type="t", unit="pt")
 
197
t_m    = length(1, type="t", unit="m")
 
198
t_mm   = length(1, type="t", unit="mm")
 
199
t_cm   = length(1, type="t", unit="cm")
 
200
t_inch = length(1, type="t", unit="inch")
 
201
 
 
202
# visual lengths
 
203
v_pt   = length(1, type="v", unit="pt")
 
204
v_m    = length(1, type="v", unit="m")
 
205
v_mm   = length(1, type="v", unit="mm")
 
206
v_cm   = length(1, type="v", unit="cm")
 
207
v_inch = length(1, type="v", unit="inch")
 
208
 
 
209
# width lengths
 
210
w_pt   = length(1, type="w", unit="pt")
 
211
w_m    = length(1, type="w", unit="m")
 
212
w_mm   = length(1, type="w", unit="mm")
 
213
w_cm   = length(1, type="w", unit="cm")
 
214
w_inch = length(1, type="w", unit="inch")
 
215
 
 
216
# TeX lengths
 
217
x_pt   = length(1, type="x", unit="pt")
 
218
x_m    = length(1, type="x", unit="m")
 
219
x_mm   = length(1, type="x", unit="mm")
 
220
x_cm   = length(1, type="x", unit="cm")
 
221
x_inch = length(1, type="x", unit="inch")