beta-0.89.2
[luatex.git] / source / texk / kpathsea / magstep.c
blob95a16ba5d7cda6822b5a245ae7816819ba703795
1 /* magstep.c: fix up fixed-point vs. floating-point.
3 Copyright 1994, 1995, 2008 Karl Berry.
4 Copyright 1999, 2005 Olaf Weber.
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with this library; if not, see <http://www.gnu.org/licenses/>. */
19 #include <kpathsea/config.h>
21 #include <kpathsea/magstep.h>
23 /* Return true magstep N, where the lsb of N means ``half'' (see
24 magstep.h) for resolution BDPI. From Tom Rokicki's dvips. */
26 static int
27 magstep (int n, int bdpi)
29 double t;
30 int step;
31 int neg = 0;
33 if (n < 0)
35 neg = 1;
36 n = -n;
39 if (n & 1)
41 n &= ~1;
42 t = 1.095445115;
44 else
45 t = 1.0;
47 while (n > 8)
49 n -= 8;
50 t = t * 2.0736;
53 while (n > 0)
55 n -= 2;
56 t = t * 1.2;
59 /* Unnecessary casts to shut up stupid compilers. */
60 step = (int)(0.5 + (neg ? bdpi / t : bdpi * t));
61 return step;
64 /* This is adapted from code written by Tom Rokicki for dvips. It's
65 part of Kpathsea now so all the drivers can use it. The idea is to
66 return the true dpi corresponding to DPI with a base resolution of
67 BDPI. If M_RET is non-null, we also set that to the mag value. */
70 /* Don't bother trying to use fabs or some other ``standard'' routine
71 which can only cause trouble; just roll our own simple-minded
72 absolute-value function that is all we need. */
73 #undef ABS /* be safe */
74 #define ABS(expr) ((expr) < 0 ? -(expr) : (expr))
76 #define MAGSTEP_MAX 40
78 unsigned
79 kpathsea_magstep_fix (kpathsea kpse, unsigned dpi, unsigned bdpi, int *m_ret)
81 int m;
82 int mdpi = -1;
83 unsigned real_dpi = 0;
84 int sign = dpi < bdpi ? -1 : 1; /* negative or positive magsteps? */
85 (void)kpse; /* currenty not used */
87 for (m = 0; !real_dpi && m < MAGSTEP_MAX; m++) /* don't go forever */
89 mdpi = magstep (m * sign, bdpi);
90 if (ABS (mdpi - (int) dpi) <= 1) /* if this magstep matches, quit */
91 real_dpi = mdpi;
92 else if ((mdpi - (int) dpi) * sign > 0) /* if gone too far, quit */
93 real_dpi = dpi;
96 /* If requested, return the encoded magstep (the loop went one too far). */
97 /* More unnecessary casts. */
98 if (m_ret)
99 *m_ret = real_dpi == (unsigned)(mdpi ? (m - 1) * sign : 0);
101 /* Always return the true dpi found. */
102 return real_dpi ? real_dpi : dpi;
105 #if defined (KPSE_COMPAT_API)
106 unsigned
107 kpse_magstep_fix (unsigned dpi, unsigned bdpi, int *m_ret)
109 return kpathsea_magstep_fix(kpse_def, dpi, bdpi, m_ret);
111 #endif