Back to index

tetex-bin  3.0
papersiz.c
Go to the documentation of this file.
00001 /*
00002  *   This function calculates approximately (whole + num/den) * sf.
00003  *   No need for real extreme accuracy; one twenty thousandth of an
00004  *   inch should be sufficient.
00005  *
00006  *   No `sf' parameter means to use an old one; inches are assumed
00007  *   originally.
00008  *
00009  *   Assumptions:
00010  *
00011  *      0 <= num < den <= 20000
00012  *      0 <= whole
00013  */
00014 #include "dvips.h"
00015 #include "protos.h"
00016 static long scale P4C(long, whole, long, num, long, den, long, sf)
00017 {
00018    long v ;
00019 
00020    v = whole * sf + num * (sf / den) ;
00021    if (v / sf != whole || v < 0 || v > 0x40000000L)
00022       error("! arithmetic overflow in parameter") ;
00023    sf = sf % den ;
00024    v += (sf * num * 2 + den) / (2 * den) ;
00025    return(v) ;
00026 }
00027 /*
00028  *   Convert a sequence of digits into a long; return -1 if no digits.
00029  *   Advance the passed pointer as well.
00030  */
00031 static long myatol P1C(char **, s)
00032 {
00033    register char *p ;
00034    register long result ;
00035 
00036    result = 0 ;
00037    p = *s ;
00038    while ('0' <= *p && *p <= '9') {
00039       if (result > 100000000)
00040          error("! arithmetic overflow in parameter") ;
00041       result = 10 * result + *p++ - '0' ;
00042    }
00043    if (p == *s) {
00044 #ifdef KPATHSEA
00045       error(concat3 ("expected number in ", *s, ", returning 10")) ;
00046 #else
00047       error("expected number!  returning 10") ;
00048 #endif
00049       return 10 ;
00050    } else {
00051       *s = p ;
00052       return(result) ;
00053    }
00054 }
00055 /*
00056  *   Get a dimension, allowing all the various extensions, and
00057  *   defaults.  Returns a value in scaled points.
00058  */
00059 static long scalevals[] = { 1864680L, 65536L, 786432L, 186468L,
00060                             1L, 65782L, 70124L, 841489L, 4736286L } ;
00061 static char *scalenames = "cmptpcmmspbpddccin" ;
00062 long myatodim P1C(char **, s)
00063 {
00064    register long w, num, den, sc ;
00065    register char *q ;
00066    char *p ;
00067    int negative = 0, i ;
00068 
00069    p = *s ;
00070    if (**s == '-') {
00071       p++ ;
00072       negative = 1 ;
00073    }
00074    w = myatol(&p) ;
00075    if (w < 0) {
00076       error("number too large; 1000 used") ;
00077       w = 1000 ;
00078    }
00079    num = 0 ;
00080    den = 1 ;
00081    if (*p == '.') {
00082       p++ ;
00083       while ('0' <= *p && *p <= '9') {
00084          if (den <= 1000) {
00085             den *= 10 ;
00086             num = num * 10 + *p - '0' ;
00087          } else if (den == 10000) {
00088             den *= 2 ;
00089             num = num * 2 + (*p - '0') / 5 ;
00090          }
00091          p++ ;
00092       }
00093    }
00094    while (*p == ' ')
00095       p++ ;
00096    for (i=0, q=scalenames; ; i++, q += 2)
00097       if (*q == 0) {
00098 #ifdef KPATHSEA
00099          error(concat3 ("expected units in ", *s, ", assuming inches.")) ;
00100 #else
00101          error("expected units!  assuming inches.") ;
00102 #endif
00103          sc = scalevals[8] ;
00104          break ;
00105       } else if (*p == *q && p[1] == q[1]) {
00106          sc = scalevals[i] ;
00107          p += 2 ;
00108          break ;
00109       }
00110    w = scale(w, num, den, sc) ;
00111    *s = p ;
00112    return(negative?-w:w) ;
00113 }
00114 /*
00115  *   The routine where we handle the paper size special.  We need to pass in
00116  *   the string after the `papersize=' specification.
00117  */
00118 void handlepapersize P3C(char *, p, integer *, x, integer *, y)
00119 { 
00120    while (*p == ' ')
00121       p++ ;
00122    *x = myatodim(&p) ;
00123    while (*p == ' ' || *p == ',')
00124       p++ ;
00125    *y = myatodim(&p) ;
00126 }