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>
24 /* Return true magstep N, where the lsb of N means ``half'' (see
25 magstep.h) for resolution BDPI. From Tom Rokicki's dvips. */
28 magstep P2C(int, n, int, bdpi)
60 /* Unnecessary casts to shut up stupid compilers. */
61 step = (int)(0.5 + (neg ? bdpi / t : bdpi * t));
65 /* This is adapted from code written by Tom Rokicki for dvips. It's
66 part of Kpathsea now so all the drivers can use it. The idea is to
67 return the true dpi corresponding to DPI with a base resolution of
68 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
79 kpse_magstep_fix P3C(unsigned, dpi, unsigned, bdpi, int *, m_ret)
83 unsigned real_dpi = 0;
84 int sign = dpi < bdpi ? -1 : 1; /* negative or positive magsteps? */
86 for (m = 0; !real_dpi && m < MAGSTEP_MAX; m++) /* don't go forever */
88 mdpi = magstep (m * sign, bdpi);
89 if (ABS (mdpi - (int) dpi) <= 1) /* if this magstep matches, quit */
91 else if ((mdpi - (int) dpi) * sign > 0) /* if gone too far, quit */
95 /* If requested, return the encoded magstep (the loop went one too far). */
96 /* More unnecessary casts. */
98 *m_ret = real_dpi == (unsigned)(mdpi ? (m - 1) * sign : 0);
100 /* Always return the true dpi found. */
101 return real_dpi ? real_dpi : dpi;