| 1 |
/* magstep.c: fix up fixed-point vs. floating-point. |
| 2 |
|
| 3 |
Copyright 1994, 1995, 2008 Karl Berry. |
| 4 |
Copyright 1999, 2005 Olaf Weber. |
| 5 |
|
| 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. |
| 10 |
|
| 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. |
| 15 |
|
| 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/>. */ |
| 18 |
|
| 19 |
#include <kpathsea/config.h> |
| 20 |
|
| 21 |
#include <kpathsea/magstep.h> |
| 22 |
|
| 23 |
/* Return true magstep N, where the lsb of N means ``half'' (see |
| 24 |
magstep.h) for resolution BDPI. From Tom Rokicki's dvips. */ |
| 25 |
|
| 26 |
static int |
| 27 |
magstep (int n, int bdpi) |
| 28 |
{ |
| 29 |
double t; |
| 30 |
int step; |
| 31 |
int neg = 0; |
| 32 |
|
| 33 |
if (n < 0) |
| 34 |
{ |
| 35 |
neg = 1; |
| 36 |
n = -n; |
| 37 |
} |
| 38 |
|
| 39 |
if (n & 1) |
| 40 |
{ |
| 41 |
n &= ~1; |
| 42 |
t = 1.095445115; |
| 43 |
} |
| 44 |
else |
| 45 |
t = 1.0; |
| 46 |
|
| 47 |
while (n > 8) |
| 48 |
{ |
| 49 |
n -= 8; |
| 50 |
t = t * 2.0736; |
| 51 |
} |
| 52 |
|
| 53 |
while (n > 0) |
| 54 |
{ |
| 55 |
n -= 2; |
| 56 |
t = t * 1.2; |
| 57 |
} |
| 58 |
|
| 59 |
/* Unnecessary casts to shut up stupid compilers. */ |
| 60 |
step = (int)(0.5 + (neg ? bdpi / t : bdpi * t)); |
| 61 |
return step; |
| 62 |
} |
| 63 |
|
| 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. */ |
| 68 |
|
| 69 |
|
| 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)) |
| 75 |
|
| 76 |
#define MAGSTEP_MAX 40 |
| 77 |
|
| 78 |
unsigned |
| 79 |
kpathsea_magstep_fix (kpathsea kpse, unsigned dpi, unsigned bdpi, int *m_ret) |
| 80 |
{ |
| 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 */ |
| 86 |
|
| 87 |
for (m = 0; !real_dpi && m < MAGSTEP_MAX; m++) /* don't go forever */ |
| 88 |
{ |
| 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; |
| 94 |
} |
| 95 |
|
| 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); |
| 100 |
|
| 101 |
/* Always return the true dpi found. */ |
| 102 |
return real_dpi ? real_dpi : dpi; |
| 103 |
} |
| 104 |
|
| 105 |
#if defined (KPSE_COMPAT_API) |
| 106 |
unsigned |
| 107 |
kpse_magstep_fix (unsigned dpi, unsigned bdpi, int *m_ret) |
| 108 |
{ |
| 109 |
return kpathsea_magstep_fix(kpse_def, dpi, bdpi, m_ret); |
| 110 |
} |
| 111 |
#endif |
| 112 |
|