aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/postscript/common/bbox.c
blob: b6807a4d102cd78628bf2fa62aa0faa4c0abd121 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 *
 * Boundingbox code for PostScript translators. The boundingbox for each page
 * is accumulated in bbox - the one for the whole document goes in docbbox. A
 * call to writebbox() puts out an appropriate comment, updates docbbox, and
 * resets bbox for the next page. The assumption made at the end of writebbox()
 * is that we're really printing the current page only if output is now going
 * to stdout - a valid assumption for all supplied translators. Needs the math
 * library.
 *
 */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <fcntl.h>
#include <math.h>

#include "comments.h"			/* PostScript file structuring comments */
#include "gen.h"			/* a few general purpose definitions */
#include "ext.h"			/* external variable declarations */

typedef struct bbox {
	int	set;
	double	llx, lly;
	double	urx, ury;
} Bbox;

Bbox	bbox = {FALSE, 0.0, 0.0, 0.0, 0.0};
Bbox	docbbox = {FALSE, 0.0, 0.0, 0.0, 0.0};

double	ctm[6] = {1.0, 0.0, 0.0, 1.0, 0.0, 0.0};
double	matrix1[6], matrix2[6];

/*****************************************************************************/

void
cover(x, y)

    double	x, y;

{

/*
 *
 * Adds point (x, y) to bbox. Coordinates are in user space - the transformation
 * to default coordinates happens in writebbox().
 *
 */

    if ( bbox.set == FALSE ) {
	bbox.llx = bbox.urx = x;
	bbox.lly = bbox.ury = y;
	bbox.set = TRUE;
    } else {
	if ( x < bbox.llx )
	    bbox.llx = x;
	if ( y < bbox.lly )
	    bbox.lly = y;
	if ( x > bbox.urx )
	    bbox.urx = x;
	if ( y > bbox.ury )
	    bbox.ury = y;
    }	/* End else */

}   /* End of cover */

/*****************************************************************************/
void	resetbbox(int);

void
writebbox(fp, keyword, slop)

    FILE	*fp;			/* the comment is written here */
    char	*keyword;		/* the boundingbox comment string */
    int		slop;			/* expand (or contract?) the box a bit */

{

    Bbox	ubbox;			/* user space bounding box */
    double	x, y;

/*
 *
 * Transforms the numbers in the bbox[] using ctm[], adjusts the corners a bit
 * (depending on slop) and then writes comment. If *keyword is BoundingBox use
 * whatever's been saved in docbbox, otherwise assume the comment is just for
 * the current page.
 *
 */

    if ( strcmp(keyword, BOUNDINGBOX) == 0 )
	bbox = docbbox;

    if ( bbox.set == TRUE ) {
	ubbox = bbox;
	bbox.set = FALSE;		/* so cover() works properly */
	x = ctm[0] * ubbox.llx + ctm[2] * ubbox.lly + ctm[4];
	y = ctm[1] * ubbox.llx + ctm[3] * ubbox.lly + ctm[5];
	cover(x, y);
	x = ctm[0] * ubbox.llx + ctm[2] * ubbox.ury + ctm[4];
	y = ctm[1] * ubbox.llx + ctm[3] * ubbox.ury + ctm[5];
	cover(x, y);
	x = ctm[0] * ubbox.urx + ctm[2] * ubbox.ury + ctm[4];
	y = ctm[1] * ubbox.urx + ctm[3] * ubbox.ury + ctm[5];
	cover(x, y);
	x = ctm[0] * ubbox.urx + ctm[2] * ubbox.lly + ctm[4];
	y = ctm[1] * ubbox.urx + ctm[3] * ubbox.lly + ctm[5];
	cover(x, y);
	bbox.llx -= slop + 0.5;
	bbox.lly -= slop + 0.5;
	bbox.urx += slop + 0.5;
	bbox.ury += slop + 0.5;
	fprintf(fp, "%s %d %d %d %d\n", keyword, (int)bbox.llx, (int)bbox.lly,(int)bbox.urx, (int)bbox.ury);
	bbox = ubbox;
    }	/* End if */

    resetbbox((fp == stdout) ? TRUE : FALSE);

}   /* End of writebbox */

/*****************************************************************************/
void
resetbbox(output)

    int		output;

{

/*
 *
 * Adds bbox to docbbox and resets bbox for the next page. Only update docbbox
 * if we really did output on the last page.
 *
 */

    if ( docbbox.set == TRUE ) {
	cover(docbbox.llx, docbbox.lly);
	cover(docbbox.urx, docbbox.ury);
    }	/* End if */

    if ( output == TRUE ) {
	docbbox = bbox;
	docbbox.set = TRUE;
    }	/* End if */

    bbox.set = FALSE;

}   /* End of resetbbox */

/*****************************************************************************/
void
scale(sx, sy)

    double	sx, sy;

{

/*
 *
 * Scales the default matrix.
 *
 */

    matrix1[0] = sx;
    matrix1[1] = 0;
    matrix1[2] = 0;
    matrix1[3] = sy;
    matrix1[4] = 0;
    matrix1[5] = 0;

    concat(matrix1);

}   /* End of scale */

/*****************************************************************************/
void
translate(tx, ty)

    double	tx, ty;

{

/*
 *
 * Translates the default matrix.
 *
 */

    matrix1[0] = 1.0;
    matrix1[1] = 0.0;
    matrix1[2] = 0.0;
    matrix1[3] = 1.0;
    matrix1[4] = tx;
    matrix1[5] = ty;

    concat(matrix1);

}   /* End of translate */

/*****************************************************************************/
void
rotate(angle)

    double	angle;

{

/*
 *
 * Rotates by angle degrees.
 *
 */

    angle *= 3.1416 / 180;

    matrix1[0] = matrix1[3] = cos(angle);
    matrix1[1] = sin(angle);
    matrix1[2] = -matrix1[1];
    matrix1[4] = 0.0;
    matrix1[5] = 0.0;

    concat(matrix1);

}   /* End of rotate */

/*****************************************************************************/

void
concat(m1)

    double	m1[];

{

    double	m2[6];

/*
 *
 * Replaces the ctm[] by the result of the matrix multiplication m1[] x ctm[].
 *
 */

    m2[0] = ctm[0];
    m2[1] = ctm[1];
    m2[2] = ctm[2];
    m2[3] = ctm[3];
    m2[4] = ctm[4];
    m2[5] = ctm[5];

    ctm[0] = m1[0] * m2[0] + m1[1] * m2[2];
    ctm[1] = m1[0] * m2[1] + m1[1] * m2[3];
    ctm[2] = m1[2] * m2[0] + m1[3] * m2[2];
    ctm[3] = m1[2] * m2[1] + m1[3] * m2[3];
    ctm[4] = m1[4] * m2[0] + m1[5] * m2[2] + m2[4];
    ctm[5] = m1[4] * m2[1] + m1[5] * m2[3] + m2[5];

}   /* End of concat */

/*****************************************************************************/