summaryrefslogtreecommitdiff
path: root/dviware/dvipage/utils.c
blob: 757813ece73ca45f788bc5a2ad88d0b0726029ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * dvipage: DVI Previewer Program for Suns
 *
 * Neil Hunt (hunt@spar.slb.com)
 *
 * This program is based, in part, upon the program dvisun,
 * distributed by the UnixTeX group, extensively modified by
 * Neil Hunt at the Schlumberger Palo Alto Research Laboratories
 * of Schlumberger Technologies, Inc.
 *
 * Copyright (c) 1988 Schlumberger Technologies, Inc 1988.
 * Anyone can use this software in any manner they choose,
 * including modification and redistribution, provided they make
 * no charge for it, and these conditions remain unchanged.
 *
 * This program is distributed as is, with all faults (if any), and
 * without any warranty. No author or distributor accepts responsibility
 * to anyone for the consequences of using it, or for whether it serves any
 * particular purpose at all, or any other reason.
 *
 * $Log:	utils.c,v $
 * Revision 1.1  88/11/28  18:41:33  hunt
 * Initial revision
 * 
 * Stripped from dvipage.c 1.4.
 */

#include <stdio.h>
#include <fcntl.h>
#include <sys/param.h>		/* For MAXPATHLEN */
#include <suntool/sunview.h>
#include "dvipage.h"

/*
 * Utility Functions.
 * =================
 */

/*
 * get_unsigned:
 *
 */

unsigned int
get_unsigned(fp, n)
register FILE *fp;
register int n;
{
	register int x;

	x = 0;
	while (n--)
	{
		x <<= 8;
		x |= getc(fp);
	}

	return(x);
}

/*
 * get_signed:
 *
 */

int
get_signed(fp, n)
register FILE *fp;
register int n;
{
	int n1;
	register int x;

	x = getc(fp);   /* get first (high-order) byte */
	n1 = n--;
	while (n--)
	{
		x <<= 8;
		x |= getc(fp);
	}

	/* NOTE: This code assumes that the right-shift is an arithmetic, rather
	than logical, shift which will propagate the sign bit right.   According
	to Kernighan and Ritchie, this is compiler dependent! */

	x<<=32-8*n1;
	x>>=32-8*n1;  /* sign extend */

	return(x);
}

/*
 * actual_factor:
 *	compute the actual size factor given the approximation.
 */

double
actual_factor(unmodsize)
int unmodsize;  /* actually factor * 1000 */
{
	float realsize;	/* the actual magnification factor */

	realsize = (float)unmodsize / 1000.0;
	/* a real hack to correct for rounding in some cases--rkf */
	if(unmodsize==1095) realsize = 1.095445;	/*stephalf*/
	else if(unmodsize==1315) realsize=1.314534;	/*stepihalf*/
	else if(unmodsize==2074) realsize=2.0736;	/*stepiv*/
	else if(unmodsize==2488) realsize=2.48832;  /*stepv*/
	else if(unmodsize==2986) realsize=2.985984;	/*stepiv*/
	/* the remaining magnification steps are represented with sufficient
	   accuracy already */
	return(realsize);
}


/*
 * do_convert
 */

int
do_convert(num, den, convResolution)
int num;
int den;
int convResolution;
{
	register float conv;
	conv = ((float)num/(float)den) * 
#ifdef USEGLOBALMAG
/*	actual_factor(mag) * why was this in as Actual Factor?  jls */
	((float) mag/1000.0) *
#endif
	((float)convResolution/254000.0);
	return((int) (1.0 / conv + 0.5));
}