1268
|
1 /* magstep.c: fix up fixed-point vs. floating-point. |
|
2 |
|
3 Copyright (C) 1994 Karl Berry. |
|
4 |
|
5 This program is free software; you can redistribute it and/or modify |
|
6 it under the terms of the GNU General Public License as published by |
|
7 the Free Software Foundation; either version 2, or (at your option) |
|
8 any later version. |
|
9 |
|
10 This program is distributed in the hope that it will be useful, |
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 GNU General Public License for more details. |
|
14 |
|
15 You should have received a copy of the GNU General Public License |
|
16 along with this program; if not, write to the Free Software |
|
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
|
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 step = 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 /* Don't bother trying to use fabs or some other ``standard'' routine |
|
70 which can only cause trouble; just roll our own simple-minded |
|
71 absolute-value function that is all we need. */ |
|
72 #undef ABS /* be safe */ |
|
73 #define ABS(expr) ((expr) < 0 ? -(expr) : (expr)) |
|
74 |
|
75 #define MAGSTEP_MAX 40 |
|
76 |
|
77 unsigned |
|
78 kpse_magstep_fix P3C(unsigned, dpi, unsigned, bdpi, int *, m_ret) |
|
79 { |
|
80 int m; |
|
81 int mdpi = -1; |
|
82 unsigned real_dpi = 0; |
|
83 int sign = dpi < bdpi ? -1 : 1; /* negative or positive magsteps? */ |
|
84 |
|
85 for (m = 0; !real_dpi && m < MAGSTEP_MAX; m++) /* don't go forever */ |
|
86 { |
|
87 mdpi = magstep (m * sign, bdpi); |
|
88 if (ABS (mdpi - (int) dpi) <= 1) /* if this magstep matches, quit */ |
|
89 real_dpi = mdpi; |
|
90 else if ((mdpi - (int) dpi) * sign > 0) /* if gone too far, quit */ |
|
91 real_dpi = dpi; |
|
92 } |
|
93 |
|
94 /* If requested, return the encoded magstep (the loop went one too far). */ |
|
95 if (m_ret) |
|
96 *m_ret = real_dpi == mdpi ? (m - 1) * sign : 0; |
|
97 |
|
98 /* Always return the true dpi found. */ |
|
99 return real_dpi ? real_dpi : dpi; |
|
100 } |