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