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
|
//
// Copyright 2007 Johannes Hofmann <Johannes.Hofmann@gmx.de>
//
// This software may be used and distributed according to the terms
// of the GNU General Public License, incorporated herein by reference.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "PostscriptDSC.H"
PostscriptDSC::PostscriptDSC() {
bb_x = 0;
bb_y = 0;
bb_w = 594; // A4
bb_h = 841; //
setup_len = 0;
pages = 0;
page_off = NULL;
page_len = NULL;
}
PostscriptDSC::~PostscriptDSC() {
if (page_off) {
free(page_off);
}
if (page_len) {
free(page_len);
}
}
int
PostscriptDSC::parse(int fd) {
FILE *fp;
char linebuf[1024];
int x, y, w, h;
int p1 = 0, ps;
int i = 0;
int bb_read = 0;
int max_pages = 128;
bb_x = 0;
bb_y = 0;
bb_w = 594; // A4
bb_h = 841; //
setup_len = 0;
pages = 0;
if (page_off) {
free(page_off);
page_off = NULL;
}
if(page_len) {
free(page_len);
page_len = NULL;
}
fp = fdopen(fd, "r");
if (!fp) {
perror("fdopen");
return 1;
}
page_off = (size_t*) malloc(sizeof(size_t) * max_pages);
memset(page_off, 0, sizeof(size_t) * max_pages);
page_len = (size_t*) malloc(sizeof(size_t) * max_pages);
memset(page_len, 0, sizeof(size_t) * max_pages);
while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
if (!bb_read &&
sscanf(linebuf, "%%%%BoundingBox: %d %d %d %d", &x, &y, &w, &h) == 4) {
bb_x = x;
bb_y = y;
bb_w = w;
bb_h = h;
bb_read++;
} else if (setup_len == 0 &&
strncmp(linebuf, "%%EndSetup", strlen("%%EndSetup")) == 0) {
setup_len = ftell(fp);
} else if (strncmp(linebuf, "%%Page: ", strlen("%%Page: ")) == 0) {
char *p_str = &linebuf[strlen(linebuf)];
// implicitely end setup section
if (!setup_len) {
setup_len = ftell(fp);
}
// move p_str back to beginning of last number in linebuf
while (p_str > linebuf && !isdigit(*p_str)) {
p_str--;
}
while (p_str > linebuf && isdigit(*p_str)) {
p_str--;
}
p1 = atoi(p_str);
if (p1 < 1) {
fprintf(stderr, "Page %d out of range\n", p1);
return 1;
}
if (p1 > max_pages) {
page_off = (size_t*) realloc(page_off, 2 * max_pages * sizeof(size_t));
memset(page_off + max_pages, 0, sizeof(size_t) * max_pages);
page_len = (size_t*) realloc(page_len, 2 * max_pages * sizeof(size_t));
memset(page_len + max_pages, 0, sizeof(size_t) * max_pages);
max_pages = max_pages * 2;
}
if (page_off[p1 - 1] != 0) {
fprintf(stderr, "Page %d already defined\n", p1);
return 1;
}
if (p1 > 1 && page_off[p1 - 2] == 0) {
fprintf(stderr, "Page %d not yet defined\n", p1 - 1);
return 1;
}
page_off[p1 - 1] = ftell(fp);
if (p1 > 1) {
page_len[p1 - 2] = page_off[p1 - 1] - page_off[p1 - 2];
}
if (p1 > pages) {
pages = p1;
}
}
}
// Now do some checks
if (!setup_len) {
fprintf(stderr, "PageSetup end not found\n");
return 1;
}
if (page_len && page_off && p1 > 0 && p1 <= pages) {
page_len[p1 - 1] = ftell(fp) - page_off[p1 - 1];
for (int i=0; i<pages; i++) {
if (page_off[i] == 0 || page_len[i] == 0) {
fprintf(stderr, "Page %d not defined\n", i);
return 1;
}
}
} else {
return 1;
}
return 0;
}
void
PostscriptDSC::get_bounding_box(int *x, int *y, int *w, int *h) {
*x = bb_x;
*y = bb_y;
*w = bb_w;
*h = bb_h;
}
int
PostscriptDSC::get_pages() {
return pages;
}
size_t
PostscriptDSC::get_setup_len() {
return setup_len;
}
size_t
PostscriptDSC::get_page_off(int p) {
if (p < 1 || p > pages) {
return 0;
} else {
return page_off[p - 1];
}
}
size_t
PostscriptDSC::get_page_len(int p) {
if (p < 1 || p > pages) {
return 0;
} else {
return page_len[p - 1];
}
}
void
PostscriptDSC::print() {
int i;
printf("x %d, y %d, w %d, h %d\n", bb_x, bb_y, bb_w, bb_h);
printf("setup_len %d\n", setup_len);
for (i=0; i<pages; i++) {
printf("p %d, off %d, len %d\n", i, page_off[i], page_len[i]);
}
}
|