Debian Codegraph @

texk/xdvipsk/prescan.c

raw
1 /*
2 * This is the main routine for the first (prescanning) pass.
3 */
4 #ifndef XDVIPSK
5 #include "dvips.h" /* The copyright notice in that file is included too! */
6 #else
7 #include "xdvips.h" /* The copyright notice in that file is included too! */
8 #endif /* XDVIPSK */
9
10 /*
11 * The external declarations:
12 */
13 #include "protos.h"
14 #ifdef XDVIPSK
15 #include "uthash.h"
16
17 charusetype_ref* charused_hash[RESHASHPRIME];
18 #endif /* XDVIPSK */
19
20 /*
21 * This routine handles the processing of the preamble in the dvi file.
22 */
23 void
24 readpreamble(void)
25 {
26 register int i;
27 char *p;
28
29 if (dvibyte()!=247) error("! Bad DVI file: first byte not preamble");
30 if (dvibyte()!=2) error("! Bad DVI file: id byte not 2");
31 num = signedquad();
32 den = signedquad();
33 if (overridemag > 0) signedquad();
34 else if (overridemag < 0) mag = (mag * signedquad()) / 1000.0;
35 else mag = signedquad();
36 conv = (real) num * DPI * mag / ( den * 254000000.0 );
37 vconv = (real) num * VDPI * mag / ( den * 254000000.0 );
38 alpha = (((real)den / 7227.0) / 0x100000) * (25400000.0 / (real) num);
39 fsizetol = 1 + (integer)(DPI/(72270.0 * conv));
40 if (!pprescan) {
41 for (i=dvibyte(),p=preamblecomment;i>0;i--,p++) *p=dvibyte();
42 *p='\0';
43 if (!quiet) {
44 fprintf(stderr, "'");
45 #ifdef VMCMS /* IBM: VM/CMS */
46 for(p=preamblecomment;*p;p++) putc(ascii2ebcdic[*p], stderr);
47 #else
48 #ifdef MVSXA /* IBM: MVS/XA */
49 for(p=preamblecomment;*p;p++) putc(ascii2ebcdic[*p], stderr);
50 #else
51 for(p=preamblecomment;*p;p++) putc_str(*p, stderr);
52 #endif /* IBM: VM/CMS */
53 #endif
54 fprintf_str(stderr, "' -> %s\n", oname);
55 }
56 } else
57 skipover(dvibyte());
58 }
59
60 #ifdef XDVIPSK
61 charusetype_ref *lookup_charused(const char* ps_name, quarterword /* otftype_enum */ otftype)
62 {
63 charusetype_ref *ptr;
64
65 assert(ps_name);
66
67 for (ptr = charused_hash[hash(ps_name)]; ptr != NULL; ptr = ptr->next)
68 if (ptr->head && ptr->head->charused_ptr && ptr->head->charused_ptr->fd && ptr->head->charused_ptr->fd->resfont && ptr->head->charused_ptr->fd->resfont->PSname &&
69 (strcmp(ptr->head->charused_ptr->fd->resfont->PSname, ps_name) == 0) && otftype_conforms(ptr->head->charused_ptr->fd->resfont->otftype, otftype))
70 break;
71
72 return(ptr);
73 }
74 #endif /* XDVIPSK */
75
76 /*
77 * Finally, here's our main prescan routine.
78 */
79 static integer firstmatch = -1, lastmatch = -1;
80 void
81 #ifdef XDVIPSK
82 prescanpages(boolean parse_maps)
83 #else
84 prescanpages(void)
85 #endif /* XDVIPSK */
86 {
87 register int cmd;
88 short ret = 0;
89 register integer thispageloc, thissecloc;
90 register fontdesctype *f;
91 register shalfword c;
92 register long thissectionmem = 0;
93 integer mpagenum;
94 integer pageseq = 0;
95 int ntfirst = notfirst;
96 #ifdef XDVIPSK
97 chardesctype *current, *tmp;
98 #endif /* XDVIPSK */
99
100 #ifdef XDVIPSK
101 firstmatch = -1;
102 lastmatch = -1;
103 pprescan = parse_maps;
104 #endif /* XDVIPSK */
105 readpreamble();
106 /*
107 * Now we look for the first page to process. If we get to the end of
108 * the file before the page, we complain (fatally).
109 * Incidentally, we don't use the DVI file's bop backpointer to skip
110 * over pages at high speed, because we want to look to for special
111 * header that might be in skipped pages.
112 */
113 while (1) {
114 cmd = skipnop();
115 if (cmd==248)
116 error("! End of document before first specified page");
117 if (cmd!=139)
118 error("! Bad DVI file: expected bop");
119 thispageloc = ftell(dvifile); /* the location FOLLOWING the bop */
120 #ifdef DEBUG
121 if (dd(D_PAGE))
122 #ifdef SHORTINT
123 fprintf(stderr,"bop at %ld\n", thispageloc);
124 #else /* ~SHORTINT */
125 fprintf(stderr,"bop at %d\n", (int)thispageloc);
126 #endif /* ~SHORTINT */
127 #endif /* DEBUG */
128 pagenum = signedquad();
129 pageseq++;
130 mpagenum = abspage ? pageseq : pagenum;
131 if (mpagenum == firstpage && ntfirst)
132 firstmatch++;
133 if (mpagenum == lastpage && notlast)
134 lastmatch++;
135 if (ntfirst && mpagenum == firstpage && firstmatch == firstseq)
136 ntfirst = 0;
137 if (ntfirst ||
138 ((evenpages && (pagenum & 1)) || (oddpages && (pagenum & 1)==0) ||
139 (pagelist && !InPageList(pagenum)))) {
140 skipover(40);
141 skippage();
142 } else {
143 if (notlast && mpagenum == lastpage)
144 lastmatch--;
145 break;
146 }
147 }
148 /*
149 * Here we scan for each of the sections. First we initialize some of
150 * the variables we need.
151 */
152 while (maxpages > 0 && cmd != 248) {
153 for (f=fonthead; f; f=f->next) {
154 f->psname = 0;
155 #ifndef XDVIPSK
156 if (f->loaded==1)
157 for (c=255; c>=0; c--)
158 f->chardesc[c].flags &= (STATUSFLAGS);
159 #else
160 if (f->loaded == 1) {
161 HASH_ITER(hh, f->chardesc_hh, current, tmp) {
162 current->flags &= (STATUSFLAGS);
163 }
164 }
165 #endif /* XDVIPSK */
166 }
167 fontmem = swmem - OVERCOST;
168 if (fontmem <= 1000)
169 error("! Too little VM in printer");
170
171 /* The section begins at the bop command just before thispageloc (which may
172 * be a page that was aborted because the previous section overflowed memory).
173 */
174 pagecount = 0;
175 fseek(dvifile, (long)thispageloc, 0);
176 pagenum = signedquad();
177 skipover(40);
178 thissecloc = thispageloc;
179 /*
180 * Now we have the loop that actually scans the pages. The scanpage routine
181 * returns 1 if the page scans okay; it returns 2 if the memory ran out
182 * before any pages were completed (in which case we'll try to carry on
183 * and hope for the best); it returns 0 if a page was aborted for lack
184 * of memory. After each page, we mark the characters seen on that page
185 * as seen for this section so that they will be downloaded.
186 */
187 ret = 0;
188 while (maxpages>0) {
189 if (!(evenpages && (pagenum & 1)) &&
190 !(oddpages && (pagenum & 1)==0) &&
191 !(pagelist && !InPageList(pagenum))) {
192 #ifdef XDVIPSK
193 ret = scanpage(parse_maps);
194 #else
195 ret = scanpage();
196 #endif /* XDVIPSK */
197 if (ret == 0)
198 break;
199 pagecount++;
200 maxpages--;
201 } else
202 skippage();
203 thissectionmem = swmem - fontmem - OVERCOST;
204 mpagenum = abspage ? pageseq : pagenum;
205 pageseq++;
206 if (mpagenum == lastpage && notlast)
207 lastmatch++;
208 if (notlast && mpagenum == lastpage && lastmatch == lastseq)
209 maxpages = -1; /* we are done after this page. */
210 if (reverse)
211 thissecloc = thispageloc;
212 for (f=fonthead; f; f=f->next)
213 if (f->loaded==1) {
214 if (f->psflag & THISPAGE)
215 f->psflag = PREVPAGE;
216 #ifndef XDVIPSK
217 for (c=255; c>=0; c--)
218 if (f->chardesc[c].flags & THISPAGE)
219 f->chardesc[c].flags = PREVPAGE |
220 (f->chardesc[c].flags & (STATUSFLAGS));
221 #else
222 HASH_ITER(hh, f->chardesc_hh, current, tmp) {
223 if (current->flags & THISPAGE)
224 current->flags = PREVPAGE |
225 (current->flags & (STATUSFLAGS));
226 }
227 #endif /* XDVIPSK */
228 }
229 cmd=skipnop();
230 if (cmd==248) break;
231 if (cmd!=139)
232 error("! Bad DVI file: expected bop");
233 thispageloc = ftell(dvifile);
234 #ifdef DEBUG
235 if (dd(D_PAGE))
236 #ifdef SHORTINT
237 fprintf(stderr,"bop at %ld\n", thispageloc);
238 #else /* ~SHORTINT */
239 fprintf(stderr,"bop at %d\n", (int)thispageloc);
240 #endif /* ~SHORTINT */
241 #endif /* DEBUG */
242 pagenum = signedquad();
243 skipover(40);
244 if (ret==2 || (maxsecsize && pagecount >= maxsecsize))
245 break;
246 }
247 /*
248 * Now we have reached the end of a section for some reason.
249 * If there are any pages, we save the pagecount, section location,
250 * and continue.
251 */
252 #ifdef XDVIPSK
253 if (!parse_maps) {
254 #endif /* XDVIPSK */
255 if (pagecount>0) {
256 register int fc = 0;
257 register sectiontype *sp;
258 register charusetype *cp;
259
260 totalpages += pagecount;
261 for (f=fonthead; f; f=f->next)
262 if (f->loaded==1 && f->psname)
263 fc++;
264 sp = (sectiontype *)mymalloc((integer)(sizeof(sectiontype) +
265 fc * sizeof(charusetype) + sizeof(fontdesctype *)));
266 sp->bos = thissecloc;
267 if (reverse) {
268 sp->next = sections;
269 sections = sp;
270 } else {
271 register sectiontype *p;
272
273 sp->next = NULL;
274 if (sections == NULL)
275 sections = sp;
276 else {
277 for (p=sections; p->next != NULL; p = p->next);
278 p->next = sp;
279 }
280 }
281 sp->numpages = pagecount;
282 #ifdef DEBUG
283 if (dd(D_PAGE))
284 #ifdef SHORTINT
285 fprintf(stderr,"Have a section: %ld pages at %ld fontmem %ld\n",
286 #else /* ~SHORTINT */
287 fprintf(stderr,"Have a section: %d pages at %d fontmem %d\n",
288 #endif /* ~SHORTINT */
289 (integer)pagecount, (integer)thissecloc, (integer)thissectionmem);
290 #endif /* DEBUG */
291 cp = (charusetype *) (sp + 1);
292 fc = 0;
293 for (f=fonthead; f; f=f->next) {
294 if (f->loaded==1 && f->psname) {
295 register halfword b, bit;
296
297 cp->psfused = (f->psflag & PREVPAGE);
298 f->psflag = 0;
299 cp->fd = f;
300 #ifdef XDVIPSK
301 if (f->resfont && f->resfont->PSname)
302 {
303 charusetype_ref *cu_ref;
304 charusetype_entry *cu_entry;
305 int hh;
306 cu_ref = lookup_charused(f->resfont->PSname, f->resfont->otftype);
307 if (cu_ref == NULL)
308 {
309 cu_ref = (charusetype_ref *)mymalloc((integer)sizeof(charusetype_ref));
310 assert(cu_ref);
311 memset(cu_ref, 0, sizeof(charusetype_ref));
312 hh = hash(f->resfont->PSname);
313 cu_ref->next = charused_hash[hh];
314 charused_hash[hh] = cu_ref;
315 }
316 cu_entry = (charusetype_entry *)mymalloc((integer)sizeof(charusetype_entry));
317 assert(cu_entry);
318 memset(cu_entry, 0, sizeof(charusetype_entry));
319 cu_entry->next = cu_ref->head;
320 cu_ref->head = cu_entry;
321 cu_entry->charused_ptr = cp;
322 }
323
324 memset(cp->bitmap, 0, sizeof(cp->bitmap));
325 if (f->resfont && f->resfont->otftype) {
326 HASH_ITER(hh, f->chardesc_hh, current, tmp) {
327 if (current->charcode == 120601) {
328 int xxx = 0;
329 }
330 if ((current->cid > 0) && (current->flags & PREVPAGE))
331 ADD_TO_USED_CHARS(cp->bitmap, current->cid);
332 }
333 }
334 else {
335 #endif /* XDVIPSK */
336 c = 0;
337 for (b=0; b<16; b++) {
338 cp->bitmap[b] = 0;
339 for (bit=32768; bit!=0; bit>>=1) {
340 #ifdef XDVIPSK
341 current = find_chardesc(f, c);
342 if (!current)
343 {
344 PRINTF_PR("Error: character %d not found in the font %s\n", c, f->name);
345 }
346 else
347 if (current->flags & PREVPAGE)
348 #else
349 if (f->chardesc[c].flags & PREVPAGE)
350 #endif /* XDVIPSK */
351 cp->bitmap[b] |= bit;
352 c++;
353 }
354 #ifdef XDVIPSK
355 }
356 #endif /* XDVIPSK */
357 }
358 cp++;
359 }
360 }
361 cp->fd = NULL;
362 #ifdef XDVIPSK
363 }
364 #endif /* XDVIPSK */
365 }
366 }
367 }