dvitomp fix from Akira
[mplib] / src / texk / kpathsea / magstep.c
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
24 /* Return true magstep N, where the lsb of N means ``half'' (see
25    magstep.h) for resolution BDPI.  From Tom Rokicki's dvips.  */
26
27 static int
28 magstep P2C(int, n,  int, bdpi)
29 {
30    double t;
31    int step;
32    int neg = 0;
33
34    if (n < 0)
35      {
36        neg = 1;
37        n = -n;
38      }
39
40    if (n & 1)
41      {
42        n &= ~1;
43        t = 1.095445115;
44      }
45     else
46       t = 1.0;
47
48    while (n > 8)
49      {
50        n -= 8;
51        t = t * 2.0736;
52      }
53
54    while (n > 0)
55      {
56        n -= 2;
57        t = t * 1.2;
58      }
59
60    /* Unnecessary casts to shut up stupid compilers. */
61    step = (int)(0.5 + (neg ? bdpi / t : bdpi * t));
62    return step;
63 }
64 \f
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.  */
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 kpse_magstep_fix P3C(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   
86   for (m = 0; !real_dpi && m < MAGSTEP_MAX; m++) /* don't go forever */
87     {
88       mdpi = magstep (m * sign, bdpi);
89       if (ABS (mdpi - (int) dpi) <= 1) /* if this magstep matches, quit */
90         real_dpi = mdpi;
91       else if ((mdpi - (int) dpi) * sign > 0) /* if gone too far, quit */
92         real_dpi = dpi;
93     }
94   
95   /* If requested, return the encoded magstep (the loop went one too far).  */
96   /* More unnecessary casts. */
97   if (m_ret)
98     *m_ret = real_dpi == (unsigned)(mdpi ? (m - 1) * sign : 0);
99
100   /* Always return the true dpi found.  */
101   return real_dpi ? real_dpi : dpi;
102 }