switch to utf-8 file encoding, add some missing copyrights
[PyX/mjg.git] / pyx / graph / axis / texter.py
blobba1231e36550039c7e65f5d9907e2ed410ac9bec
1 # -*- encoding: utf-8 -*-
4 # Copyright (C) 2002-2004 Jörg Lehmann <joergl@users.sourceforge.net>
5 # Copyright (C) 2003-2004 Michael Schindler <m-schindler@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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 from pyx import text
26 from pyx.graph.axis import tick
29 class _Itexter:
31 def labels(self, ticks):
32 """fill the label attribute of ticks
33 - ticks is a list of instances of tick
34 - for each element of ticks the value of the attribute label is set to
35 a string appropriate to the attributes num and denom of that tick
36 instance
37 - label attributes of the tick instances are just kept, whenever they
38 are not equal to None
39 - the method might modify the labelattrs attribute of the ticks; be sure
40 to not modify it in-place!"""
43 class decimal:
44 "a texter creating decimal labels (e.g. '1.234' or even '0.\overline{3}')"
46 __implements__ = _Itexter
48 def __init__(self, prefix="", infix="", suffix="", equalprecision=0,
49 decimalsep=".", thousandsep="", thousandthpartsep="",
50 plus="", minus="-", period=r"\overline{%s}",
51 labelattrs=[text.mathmode]):
52 r"""initializes the instance
53 - prefix, infix, and suffix (strings) are added at the begin,
54 immediately after the minus, and at the end of the label,
55 respectively
56 - decimalsep, thousandsep, and thousandthpartsep (strings)
57 are used as separators
58 - plus or minus (string) is inserted for non-negative or negative numbers
59 - period (string) is taken as a format string generating a period;
60 it has to contain exactly one string insert operators "%s" for the
61 period; usually it should be r"\overline{%s}"
62 - labelattrs is a list of attributes to be added to the label attributes
63 given in the painter"""
64 self.prefix = prefix
65 self.infix = infix
66 self.suffix = suffix
67 self.equalprecision = equalprecision
68 self.decimalsep = decimalsep
69 self.thousandsep = thousandsep
70 self.thousandthpartsep = thousandthpartsep
71 self.plus = plus
72 self.minus = minus
73 self.period = period
74 self.labelattrs = labelattrs
76 def labels(self, ticks):
77 labeledticks = []
78 maxdecprecision = 0
79 for tick in ticks:
80 if tick.label is None and tick.labellevel is not None:
81 labeledticks.append(tick)
82 m, n = tick.num, tick.denom
83 if m < 0: m = -m
84 if n < 0: n = -n
85 quotient, remainder = divmod(m, n)
86 quotient = str(quotient)
87 if len(self.thousandsep):
88 l = len(quotient)
89 tick.label = ""
90 for i in range(l):
91 tick.label += quotient[i]
92 if not ((l-i-1) % 3) and l > i+1:
93 tick.label += self.thousandsep
94 else:
95 tick.label = quotient
96 if remainder:
97 tick.label += self.decimalsep
98 oldremainders = []
99 tick.temp_decprecision = 0
100 while (remainder):
101 tick.temp_decprecision += 1
102 if remainder in oldremainders:
103 tick.temp_decprecision = None
104 periodstart = len(tick.label) - (len(oldremainders) - oldremainders.index(remainder))
105 tick.label = tick.label[:periodstart] + self.period % tick.label[periodstart:]
106 break
107 oldremainders += [remainder]
108 remainder *= 10
109 quotient, remainder = divmod(remainder, n)
110 if not ((tick.temp_decprecision - 1) % 3) and tick.temp_decprecision > 1:
111 tick.label += self.thousandthpartsep
112 tick.label += str(quotient)
113 if maxdecprecision < tick.temp_decprecision:
114 maxdecprecision = tick.temp_decprecision
115 if self.equalprecision:
116 for tick in labeledticks:
117 if tick.temp_decprecision is not None:
118 if tick.temp_decprecision == 0 and maxdecprecision > 0:
119 tick.label += self.decimalsep
120 for i in range(tick.temp_decprecision, maxdecprecision):
121 if not ((i - 1) % 3) and i > 1:
122 tick.label += self.thousandthpartsep
123 tick.label += "0"
124 for tick in labeledticks:
125 if tick.num * tick.denom < 0:
126 plusminus = self.minus
127 else:
128 plusminus = self.plus
129 tick.label = "%s%s%s%s%s" % (self.prefix, plusminus, self.infix, tick.label, self.suffix)
130 tick.labelattrs = tick.labelattrs + self.labelattrs
132 # del tick.temp_decprecision # we've inserted this temporary variable ... and do not care any longer about it
135 class exponential:
136 "a texter creating labels with exponentials (e.g. '2\cdot10^5')"
138 __implements__ = _Itexter
140 def __init__(self, plus="", minus="-",
141 mantissaexp=r"{{%s}\cdot10^{%s}}",
142 skipexp0=r"{%s}",
143 skipexp1=None,
144 nomantissaexp=r"{10^{%s}}",
145 minusnomantissaexp=r"{-10^{%s}}",
146 mantissamin=tick.rational((1, 1)), mantissamax=tick.rational((10L, 1)),
147 skipmantissa1=0, skipallmantissa1=1,
148 mantissatexter=decimal()):
149 r"""initializes the instance
150 - plus or minus (string) is inserted for non-negative or negative exponents
151 - mantissaexp (string) is taken as a format string generating the exponent;
152 it has to contain exactly two string insert operators "%s" --
153 the first for the mantissa and the second for the exponent;
154 examples are r"{{%s}\cdot10^{%s}}" and r"{{%s}{\rm e}{%s}}"
155 - skipexp0 (string) is taken as a format string used for exponent 0;
156 exactly one string insert operators "%s" for the mantissa;
157 None turns off the special handling of exponent 0;
158 an example is r"{%s}"
159 - skipexp1 (string) is taken as a format string used for exponent 1;
160 exactly one string insert operators "%s" for the mantissa;
161 None turns off the special handling of exponent 1;
162 an example is r"{{%s}\cdot10}"
163 - nomantissaexp (string) is taken as a format string generating the exponent
164 when the mantissa is one and should be skipped; it has to contain
165 exactly one string insert operators "%s" for the exponent;
166 an examples is r"{10^{%s}}"
167 - minusnomantissaexp (string) is taken as a format string generating the exponent
168 when the mantissa is minus one and should be skipped; it has to contain
169 exactly one string insert operators "%s" for the exponent;
170 None turns off the special handling of mantissa -1;
171 an examples is r"{-10^{%s}}"
172 - mantissamin and mantissamax are the minimum and maximum of the mantissa;
173 they are rational instances greater than zero and mantissamin < mantissamax;
174 the sign of the tick is ignored here
175 - skipmantissa1 (boolean) turns on skipping of any mantissa equals one
176 (and minus when minusnomantissaexp is set)
177 - skipallmantissa1 (boolean) as above, but all mantissas must be 1 (or -1)
178 - mantissatexter is the texter for the mantissa
179 - the skipping of a mantissa is stronger than the skipping of an exponent"""
180 self.plus = plus
181 self.minus = minus
182 self.mantissaexp = mantissaexp
183 self.skipexp0 = skipexp0
184 self.skipexp1 = skipexp1
185 self.nomantissaexp = nomantissaexp
186 self.minusnomantissaexp = minusnomantissaexp
187 self.mantissamin = mantissamin
188 self.mantissamax = mantissamax
189 self.mantissamindivmax = self.mantissamin / self.mantissamax
190 self.mantissamaxdivmin = self.mantissamax / self.mantissamin
191 self.skipmantissa1 = skipmantissa1
192 self.skipallmantissa1 = skipallmantissa1
193 self.mantissatexter = mantissatexter
195 def labels(self, ticks):
196 labeledticks = []
197 for tick in ticks:
198 if tick.label is None and tick.labellevel is not None:
199 tick.temp_orgnum, tick.temp_orgdenom = tick.num, tick.denom
200 labeledticks.append(tick)
201 tick.temp_exp = 0
202 if tick.num:
203 while abs(tick) >= self.mantissamax:
204 tick.temp_exp += 1
205 x = tick * self.mantissamindivmax
206 tick.num, tick.denom = x.num, x.denom
207 while abs(tick) < self.mantissamin:
208 tick.temp_exp -= 1
209 x = tick * self.mantissamaxdivmin
210 tick.num, tick.denom = x.num, x.denom
211 if tick.temp_exp < 0:
212 tick.temp_exp = "%s%i" % (self.minus, -tick.temp_exp)
213 else:
214 tick.temp_exp = "%s%i" % (self.plus, tick.temp_exp)
215 self.mantissatexter.labels(labeledticks)
216 if self.minusnomantissaexp is not None:
217 allmantissa1 = len(labeledticks) == len([tick for tick in labeledticks if abs(tick.num) == abs(tick.denom)])
218 else:
219 allmantissa1 = len(labeledticks) == len([tick for tick in labeledticks if tick.num == tick.denom])
220 for tick in labeledticks:
221 if (self.skipallmantissa1 and allmantissa1 or
222 (self.skipmantissa1 and (tick.num == tick.denom or
223 (tick.num == -tick.denom and self.minusnomantissaexp is not None)))):
224 if tick.num == tick.denom:
225 tick.label = self.nomantissaexp % tick.temp_exp
226 else:
227 tick.label = self.minusnomantissaexp % tick.temp_exp
228 else:
229 if tick.temp_exp == "0" and self.skipexp0 is not None:
230 tick.label = self.skipexp0 % tick.label
231 elif tick.temp_exp == "1" and self.skipexp1 is not None:
232 tick.label = self.skipexp1 % tick.label
233 else:
234 tick.label = self.mantissaexp % (tick.label, tick.temp_exp)
235 tick.num, tick.denom = tick.temp_orgnum, tick.temp_orgdenom
237 # del tick.temp_orgnum # we've inserted those temporary variables ... and do not care any longer about them
238 # del tick.temp_orgdenom
239 # del tick.temp_exp
242 class mixed:
243 "a texter creating decimal or exponential labels"
245 __implements__ = _Itexter
247 def __init__(self, smallestdecimal=tick.rational((1, 1000)),
248 biggestdecimal=tick.rational((9999, 1)),
249 equaldecision=1,
250 decimal=decimal(),
251 exponential=exponential()):
252 """initializes the instance
253 - smallestdecimal and biggestdecimal are the smallest and
254 biggest decimal values, where the decimal texter should be used;
255 they are rational instances; the sign of the tick is ignored here;
256 a tick at zero is considered for the decimal texter as well
257 - equaldecision (boolean) uses decimal texter or exponential texter
258 globaly (set) or for each tick separately (unset)
259 - decimal and exponential are texters to be used"""
260 self.smallestdecimal = smallestdecimal
261 self.biggestdecimal = biggestdecimal
262 self.equaldecision = equaldecision
263 self.decimal = decimal
264 self.exponential = exponential
266 def labels(self, ticks):
267 decticks = []
268 expticks = []
269 for tick in ticks:
270 if tick.label is None and tick.labellevel is not None:
271 if not tick.num or (abs(tick) >= self.smallestdecimal and abs(tick) <= self.biggestdecimal):
272 decticks.append(tick)
273 else:
274 expticks.append(tick)
275 if self.equaldecision:
276 if len(expticks):
277 self.exponential.labels(ticks)
278 else:
279 self.decimal.labels(ticks)
280 else:
281 for tick in decticks:
282 self.decimal.labels([tick])
283 for tick in expticks:
284 self.exponential.labels([tick])
287 class rational:
288 "a texter creating rational labels (e.g. 'a/b' or even 'a \over b')"
289 # XXX: we use divmod here to be more expicit
291 __implements__ = _Itexter
293 def __init__(self, prefix="", infix="", suffix="",
294 numprefix="", numinfix="", numsuffix="",
295 denomprefix="", denominfix="", denomsuffix="",
296 plus="", minus="-", minuspos=0, over=r"{{%s}\over{%s}}",
297 equaldenom=0, skip1=1, skipnum0=1, skipnum1=1, skipdenom1=1,
298 labelattrs=[text.mathmode]):
299 r"""initializes the instance
300 - prefix, infix, and suffix (strings) are added at the begin,
301 immediately after the minus, and at the end of the label,
302 respectively
303 - prefixnum, infixnum, and suffixnum (strings) are added
304 to the labels numerator correspondingly
305 - prefixdenom, infixdenom, and suffixdenom (strings) are added
306 to the labels denominator correspondingly
307 - plus or minus (string) is inserted for non-negative or negative numbers
308 - minuspos is an integer, which determines the position, where the
309 plus or minus sign has to be placed; the following values are allowed:
310 1 - writes the plus or minus in front of the numerator
311 0 - writes the plus or minus in front of the hole fraction
312 -1 - writes the plus or minus in front of the denominator
313 - over (string) is taken as a format string generating the
314 fraction bar; it has to contain exactly two string insert
315 operators "%s" -- the first for the numerator and the second
316 for the denominator; by far the most common examples are
317 r"{{%s}\over{%s}}" and "{{%s}/{%s}}"
318 - usually the numerator and denominator are canceled; however,
319 when equaldenom is set, the least common multiple of all
320 denominators is used
321 - skip1 (boolean) just prints the prefix, the plus or minus,
322 the infix and the suffix, when the value is plus or minus one
323 and at least one of prefix, infix and the suffix is present
324 - skipnum0 (boolean) just prints a zero instead of
325 the hole fraction, when the numerator is zero;
326 no prefixes, infixes, and suffixes are taken into account
327 - skipnum1 (boolean) just prints the numprefix, the plus or minus,
328 the numinfix and the numsuffix, when the num value is plus or minus one
329 and at least one of numprefix, numinfix and the numsuffix is present
330 - skipdenom1 (boolean) just prints the numerator instead of
331 the hole fraction, when the denominator is one and none of the parameters
332 denomprefix, denominfix and denomsuffix are set and minuspos is not -1 or the
333 fraction is positive
334 - labelattrs is a list of attributes for a texrunners text method;
335 None is considered as an empty list; labelattrs might be changed
336 in the painter as well"""
337 self.prefix = prefix
338 self.infix = infix
339 self.suffix = suffix
340 self.numprefix = numprefix
341 self.numinfix = numinfix
342 self.numsuffix = numsuffix
343 self.denomprefix = denomprefix
344 self.denominfix = denominfix
345 self.denomsuffix = denomsuffix
346 self.plus = plus
347 self.minus = minus
348 self.minuspos = minuspos
349 self.over = over
350 self.equaldenom = equaldenom
351 self.skip1 = skip1
352 self.skipnum0 = skipnum0
353 self.skipnum1 = skipnum1
354 self.skipdenom1 = skipdenom1
355 self.labelattrs = labelattrs
357 def gcd(self, *n):
358 """returns the greates common divisor of all elements in n
359 - the elements of n must be non-negative integers
360 - return None if the number of elements is zero
361 - the greates common divisor is not affected when some
362 of the elements are zero, but it becomes zero when
363 all elements are zero"""
364 if len(n) == 2:
365 i, j = n
366 if i < j:
367 i, j = j, i
368 while j > 0:
369 i, (dummy, j) = j, divmod(i, j)
370 return i
371 if len(n):
372 res = n[0]
373 for i in n[1:]:
374 res = self.gcd(res, i)
375 return res
377 def lcm(self, *n):
378 """returns the least common multiple of all elements in n
379 - the elements of n must be non-negative integers
380 - return None if the number of elements is zero
381 - the least common multiple is zero when some of the
382 elements are zero"""
383 if len(n):
384 res = n[0]
385 for i in n[1:]:
386 res = divmod(res * i, self.gcd(res, i))[0]
387 return res
389 def labels(self, ticks):
390 labeledticks = []
391 for tick in ticks:
392 if tick.label is None and tick.labellevel is not None:
393 labeledticks.append(tick)
394 tick.temp_rationalnum = tick.num
395 tick.temp_rationaldenom = tick.denom
396 tick.temp_rationalminus = 1
397 if tick.temp_rationalnum < 0:
398 tick.temp_rationalminus = -tick.temp_rationalminus
399 tick.temp_rationalnum = -tick.temp_rationalnum
400 if tick.temp_rationaldenom < 0:
401 tick.temp_rationalminus = -tick.temp_rationalminus
402 tick.temp_rationaldenom = -tick.temp_rationaldenom
403 gcd = self.gcd(tick.temp_rationalnum, tick.temp_rationaldenom)
404 (tick.temp_rationalnum, dummy1), (tick.temp_rationaldenom, dummy2) = divmod(tick.temp_rationalnum, gcd), divmod(tick.temp_rationaldenom, gcd)
405 if self.equaldenom:
406 equaldenom = self.lcm(*[tick.temp_rationaldenom for tick in ticks if tick.label is None])
407 if equaldenom is not None:
408 for tick in labeledticks:
409 factor, dummy = divmod(equaldenom, tick.temp_rationaldenom)
410 tick.temp_rationalnum, tick.temp_rationaldenom = factor * tick.temp_rationalnum, factor * tick.temp_rationaldenom
411 for tick in labeledticks:
412 rationalminus = rationalnumminus = rationaldenomminus = ""
413 if tick.temp_rationalminus == -1:
414 plusminus = self.minus
415 else:
416 plusminus = self.plus
417 if self.minuspos == 0:
418 rationalminus = plusminus
419 elif self.minuspos == 1:
420 rationalnumminus = plusminus
421 elif self.minuspos == -1:
422 rationaldenomminus = plusminus
423 else:
424 raise RuntimeError("invalid minuspos")
425 if self.skipnum0 and tick.temp_rationalnum == 0:
426 tick.label = "0"
427 elif (self.skip1 and self.skipdenom1 and tick.temp_rationalnum == 1 and tick.temp_rationaldenom == 1 and
428 (len(self.prefix) or len(self.infix) or len(self.suffix)) and
429 not len(rationalnumminus) and not len(self.numprefix) and not len(self.numinfix) and not len(self.numsuffix) and
430 not len(rationaldenomminus) and not len(self.denomprefix) and not len(self.denominfix) and not len(self.denomsuffix)):
431 tick.label = "%s%s%s%s" % (self.prefix, rationalminus, self.infix, self.suffix)
432 else:
433 if self.skipnum1 and tick.temp_rationalnum == 1 and (len(self.numprefix) or len(self.numinfix) or len(self.numsuffix)):
434 tick.temp_rationalnum = "%s%s%s%s" % (self.numprefix, rationalnumminus, self.numinfix, self.numsuffix)
435 else:
436 tick.temp_rationalnum = "%s%s%s%i%s" % (self.numprefix, rationalnumminus, self.numinfix, tick.temp_rationalnum, self.numsuffix)
437 if self.skipdenom1 and tick.temp_rationaldenom == 1 and not len(rationaldenomminus) and not len(self.denomprefix) and not len(self.denominfix) and not len(self.denomsuffix):
438 rational = tick.temp_rationalnum
439 else:
440 tick.temp_rationaldenom = "%s%s%s%i%s" % (self.denomprefix, rationaldenomminus, self.denominfix, tick.temp_rationaldenom, self.denomsuffix)
441 rational = self.over % (tick.temp_rationalnum, tick.temp_rationaldenom)
442 tick.label = "%s%s%s%s%s" % (self.prefix, rationalminus, self.infix, rational, self.suffix)
443 tick.labelattrs = tick.labelattrs + self.labelattrs
445 # del tick.temp_rationalnum # we've inserted those temporary variables ... and do not care any longer about them
446 # del tick.temp_rationaldenom
447 # del tick.temp_rationalminus