Debian Codegraph @

texk/dvipsk/papersiz.c

raw
1 /*
2 * This function calculates approximately (whole + num/den) * sf.
3 * No need for real extreme accuracy; one twenty thousandth of an
4 * inch should be sufficient.
5 *
6 * No `sf' parameter means to use an old one; inches are assumed
7 * originally.
8 *
9 * Assumptions:
10 *
11 * 0 <= num < den <= 20000
12 * 0 <= whole
13 */
14 #include "dvips.h" /* The copyright notice in that file is included too! */
15 /*
16 * The external declarations:
17 */
18 #include "protos.h"
19 static long
20 scale(long whole, long num, long den, long sf)
21 {
22 long v;
23
24 v = whole * sf + num * (sf / den);
25 if (v / sf != whole || v < 0 || v > 0x40000000L)
26 error("! arithmetic overflow in parameter");
27 sf = sf % den;
28 v += (sf * num * 2 + den) / (2 * den);
29 return(v);
30 }
31 /*
32 * Convert a sequence of digits into a long; return -1 if no digits.
33 * Advance the passed pointer as well.
34 */
35 static long
36 myatol(char **s)
37 {
38 register char *p;
39 register long result;
40
41 result = 0;
42 p = *s;
43 while ('0' <= *p && *p <= '9') {
44 if (result > 100000000)
45 error("! arithmetic overflow in parameter");
46 result = 10 * result + *p++ - '0';
47 }
48 if (p == *s && *p == '.') {
49 return 0;
50 } else if (p == *s) {
51 #ifdef KPATHSEA
52 error(concat3 ("expected number in ", *s, ", returning 10"));
53 #else
54 error("expected number! returning 10");
55 #endif
56 return 10;
57 } else {
58 *s = p;
59 return(result);
60 }
61 }
62 /*
63 * Get a dimension, allowing all the various extensions, and
64 * defaults. Returns a value in scaled points.
65 */
66 static long scalevals[] = { 1864680L, 65536L, 786432L, 186468L,
67 1L, 65782L, 70124L, 841489L, 4736286L };
68 static const char *scalenames = "cmptpcmmspbpddccin";
69 static long
70 myatodim(char **s)
71 {
72 register long w, num, den, sc;
73 register const char *q;
74 char *p;
75 int negative = 0, i;
76
77 p = *s;
78 if (**s == '-') {
79 p++;
80 negative = 1;
81 }
82 w = myatol(&p);
83 if (w < 0) {
84 error("number too large; 1000 used");
85 w = 1000;
86 }
87 num = 0;
88 den = 1;
89 if (*p == '.') {
90 p++;
91 while ('0' <= *p && *p <= '9') {
92 if (den <= 1000) {
93 den *= 10;
94 num = num * 10 + *p - '0';
95 } else if (den == 10000) {
96 den *= 2;
97 num = num * 2 + (*p - '0') / 5;
98 }
99 p++;
100 }
101 }
102 while (*p == ' ')
103 p++;
104 for (i=0, q=scalenames;; i++, q += 2)
105 if (*q == 0) {
106 #ifdef KPATHSEA
107 error(concat3 ("expected units in ", *s, ", assuming inches."));
108 #else
109 error("expected units! assuming inches.");
110 #endif
111 sc = scalevals[8];
112 break;
113 } else if (*p == *q && p[1] == q[1]) {
114 sc = scalevals[i];
115 p += 2;
116 break;
117 }
118 w = scale(w, num, den, sc);
119 *s = p;
120 return(negative?-w:w);
121 }
122 /*
123 * The routine where we handle the paper size special. We need to pass in
124 * the string after the `papersize=' specification.
125 */
126 void
127 handlepapersize(char *p, integer *x, integer *y)
128 {
129 while (*p == ' ')
130 p++;
131 *x = myatodim(&p);
132 while (*p == ' ' || *p == ',')
133 p++;
134 *y = myatodim(&p);
135 }