| 1 |
/* |
| 2 |
* This is the main routine for the first (prescanning) pass. |
| 3 |
*/ |
| 4 |
#include "dvips.h" /* The copyright notice in that file is included too! */ |
| 5 |
|
| 6 |
/* |
| 7 |
* The external declarations: |
| 8 |
*/ |
| 9 |
#include "protos.h" |
| 10 |
|
| 11 |
/* |
| 12 |
* This routine handles the processing of the preamble in the dvi file. |
| 13 |
*/ |
| 14 |
void |
| 15 |
readpreamble(void) |
| 16 |
{ |
| 17 |
register int i; |
| 18 |
char *p; |
| 19 |
|
| 20 |
if (dvibyte()!=247) error("! Bad DVI file: first byte not preamble"); |
| 21 |
if (dvibyte()!=2) error("! Bad DVI file: id byte not 2"); |
| 22 |
num = signedquad(); |
| 23 |
den = signedquad(); |
| 24 |
if (overridemag > 0) signedquad(); |
| 25 |
else if (overridemag < 0) mag = (mag * signedquad()) / 1000.0; |
| 26 |
else mag = signedquad(); |
| 27 |
conv = (real) num * DPI * mag / ( den * 254000000.0 ); |
| 28 |
vconv = (real) num * VDPI * mag / ( den * 254000000.0 ); |
| 29 |
alpha = (((real)den / 7227.0) / 0x100000) * (25400000.0 / (real) num); |
| 30 |
fsizetol = 1 + (integer)(DPI/(72270.0 * conv)); |
| 31 |
if (!pprescan) { |
| 32 |
for (i=dvibyte(),p=preamblecomment;i>0;i--,p++) *p=dvibyte(); |
| 33 |
*p='\0'; |
| 34 |
if (!quiet) { |
| 35 |
fprintf(stderr, "'"); |
| 36 |
#ifdef VMCMS /* IBM: VM/CMS */ |
| 37 |
for(p=preamblecomment;*p;p++) putc(ascii2ebcdic[*p], stderr); |
| 38 |
#else |
| 39 |
#ifdef MVSXA /* IBM: MVS/XA */ |
| 40 |
for(p=preamblecomment;*p;p++) putc(ascii2ebcdic[*p], stderr); |
| 41 |
#else |
| 42 |
for(p=preamblecomment;*p;p++) putc_str(*p, stderr); |
| 43 |
#endif /* IBM: VM/CMS */ |
| 44 |
#endif |
| 45 |
fprintf_str(stderr, "' -> %s\n", oname); |
| 46 |
} |
| 47 |
} else |
| 48 |
skipover(dvibyte()); |
| 49 |
} |
| 50 |
|
| 51 |
/* |
| 52 |
* Finally, here's our main prescan routine. |
| 53 |
*/ |
| 54 |
static integer firstmatch = -1, lastmatch = -1; |
| 55 |
void |
| 56 |
prescanpages(void) |
| 57 |
{ |
| 58 |
register int cmd; |
| 59 |
short ret = 0; |
| 60 |
register integer thispageloc, thissecloc; |
| 61 |
register fontdesctype *f; |
| 62 |
register shalfword c; |
| 63 |
register long thissectionmem = 0; |
| 64 |
integer mpagenum; |
| 65 |
integer pageseq = 0; |
| 66 |
int ntfirst = notfirst; |
| 67 |
|
| 68 |
readpreamble(); |
| 69 |
/* |
| 70 |
* Now we look for the first page to process. If we get to the end of |
| 71 |
* the file before the page, we complain (fatally). |
| 72 |
* Incidentally, we don't use the DVI file's bop backpointer to skip |
| 73 |
* over pages at high speed, because we want to look to for special |
| 74 |
* header that might be in skipped pages. |
| 75 |
*/ |
| 76 |
while (1) { |
| 77 |
cmd = skipnop(); |
| 78 |
if (cmd==248) |
| 79 |
error("! End of document before first specified page"); |
| 80 |
if (cmd!=139) |
| 81 |
error("! Bad DVI file: expected bop"); |
| 82 |
thispageloc = ftell(dvifile); /* the location FOLLOWING the bop */ |
| 83 |
#ifdef DEBUG |
| 84 |
if (dd(D_PAGE)) |
| 85 |
#ifdef SHORTINT |
| 86 |
fprintf(stderr,"bop at %ld\n", thispageloc); |
| 87 |
#else /* ~SHORTINT */ |
| 88 |
fprintf(stderr,"bop at %d\n", (int)thispageloc); |
| 89 |
#endif /* ~SHORTINT */ |
| 90 |
#endif /* DEBUG */ |
| 91 |
pagenum = signedquad(); |
| 92 |
pageseq++; |
| 93 |
mpagenum = abspage ? pageseq : pagenum; |
| 94 |
if (mpagenum == firstpage && ntfirst) |
| 95 |
firstmatch++; |
| 96 |
if (mpagenum == lastpage && notlast) |
| 97 |
lastmatch++; |
| 98 |
if (ntfirst && mpagenum == firstpage && firstmatch == firstseq) |
| 99 |
ntfirst = 0; |
| 100 |
if (ntfirst || |
| 101 |
((evenpages && (pagenum & 1)) || (oddpages && (pagenum & 1)==0) || |
| 102 |
(pagelist && !InPageList(pagenum)))) { |
| 103 |
skipover(40); |
| 104 |
skippage(); |
| 105 |
} else { |
| 106 |
if (notlast && mpagenum == lastpage) |
| 107 |
lastmatch--; |
| 108 |
break; |
| 109 |
} |
| 110 |
} |
| 111 |
/* |
| 112 |
* Here we scan for each of the sections. First we initialize some of |
| 113 |
* the variables we need. |
| 114 |
*/ |
| 115 |
while (maxpages > 0 && cmd != 248) { |
| 116 |
for (f=fonthead; f; f=f->next) { |
| 117 |
f->psname = 0; |
| 118 |
if (f->loaded==1) |
| 119 |
for (c=255; c>=0; c--) |
| 120 |
f->chardesc[c].flags &= (STATUSFLAGS); |
| 121 |
} |
| 122 |
fontmem = swmem - OVERCOST; |
| 123 |
if (fontmem <= 1000) |
| 124 |
error("! Too little VM in printer"); |
| 125 |
|
| 126 |
/* The section begins at the bop command just before thispageloc (which may |
| 127 |
* be a page that was aborted because the previous section overflowed memory). |
| 128 |
*/ |
| 129 |
pagecount = 0; |
| 130 |
fseek(dvifile, (long)thispageloc, 0); |
| 131 |
pagenum = signedquad(); |
| 132 |
skipover(40); |
| 133 |
thissecloc = thispageloc; |
| 134 |
/* |
| 135 |
* Now we have the loop that actually scans the pages. The scanpage routine |
| 136 |
* returns 1 if the page scans okay; it returns 2 if the memory ran out |
| 137 |
* before any pages were completed (in which case we'll try to carry on |
| 138 |
* and hope for the best); it returns 0 if a page was aborted for lack |
| 139 |
* of memory. After each page, we mark the characters seen on that page |
| 140 |
* as seen for this section so that they will be downloaded. |
| 141 |
*/ |
| 142 |
ret = 0; |
| 143 |
while (maxpages>0) { |
| 144 |
if (!(evenpages && (pagenum & 1)) && |
| 145 |
!(oddpages && (pagenum & 1)==0) && |
| 146 |
!(pagelist && !InPageList(pagenum))) { |
| 147 |
ret = scanpage(); |
| 148 |
if (ret == 0) |
| 149 |
break; |
| 150 |
pagecount++; |
| 151 |
maxpages--; |
| 152 |
} else |
| 153 |
skippage(); |
| 154 |
thissectionmem = swmem - fontmem - OVERCOST; |
| 155 |
mpagenum = abspage ? pageseq : pagenum; |
| 156 |
pageseq++; |
| 157 |
if (mpagenum == lastpage && notlast) |
| 158 |
lastmatch++; |
| 159 |
if (notlast && mpagenum == lastpage && lastmatch == lastseq) |
| 160 |
maxpages = -1; /* we are done after this page. */ |
| 161 |
if (reverse) |
| 162 |
thissecloc = thispageloc; |
| 163 |
for (f=fonthead; f; f=f->next) |
| 164 |
if (f->loaded==1) { |
| 165 |
if (f->psflag & THISPAGE) |
| 166 |
f->psflag = PREVPAGE; |
| 167 |
for (c=255; c>=0; c--) |
| 168 |
if (f->chardesc[c].flags & THISPAGE) |
| 169 |
f->chardesc[c].flags = PREVPAGE | |
| 170 |
(f->chardesc[c].flags & (STATUSFLAGS)); |
| 171 |
} |
| 172 |
cmd=skipnop(); |
| 173 |
if (cmd==248) break; |
| 174 |
if (cmd!=139) |
| 175 |
error("! Bad DVI file: expected bop"); |
| 176 |
thispageloc = ftell(dvifile); |
| 177 |
#ifdef DEBUG |
| 178 |
if (dd(D_PAGE)) |
| 179 |
#ifdef SHORTINT |
| 180 |
fprintf(stderr,"bop at %ld\n", thispageloc); |
| 181 |
#else /* ~SHORTINT */ |
| 182 |
fprintf(stderr,"bop at %d\n", (int)thispageloc); |
| 183 |
#endif /* ~SHORTINT */ |
| 184 |
#endif /* DEBUG */ |
| 185 |
pagenum = signedquad(); |
| 186 |
skipover(40); |
| 187 |
if (ret==2 || (maxsecsize && pagecount >= maxsecsize)) |
| 188 |
break; |
| 189 |
} |
| 190 |
/* |
| 191 |
* Now we have reached the end of a section for some reason. |
| 192 |
* If there are any pages, we save the pagecount, section location, |
| 193 |
* and continue. |
| 194 |
*/ |
| 195 |
if (pagecount>0) { |
| 196 |
register int fc = 0; |
| 197 |
register sectiontype *sp; |
| 198 |
register charusetype *cp; |
| 199 |
|
| 200 |
totalpages += pagecount; |
| 201 |
for (f=fonthead; f; f=f->next) |
| 202 |
if (f->loaded==1 && f->psname) |
| 203 |
fc++; |
| 204 |
sp = (sectiontype *)mymalloc((integer)(sizeof(sectiontype) + |
| 205 |
fc * sizeof(charusetype) + sizeof(fontdesctype *))); |
| 206 |
sp->bos = thissecloc; |
| 207 |
if (reverse) { |
| 208 |
sp->next = sections; |
| 209 |
sections = sp; |
| 210 |
} else { |
| 211 |
register sectiontype *p; |
| 212 |
|
| 213 |
sp->next = NULL; |
| 214 |
if (sections == NULL) |
| 215 |
sections = sp; |
| 216 |
else { |
| 217 |
for (p=sections; p->next != NULL; p = p->next); |
| 218 |
p->next = sp; |
| 219 |
} |
| 220 |
} |
| 221 |
sp->numpages = pagecount; |
| 222 |
#ifdef DEBUG |
| 223 |
if (dd(D_PAGE)) |
| 224 |
#ifdef SHORTINT |
| 225 |
fprintf(stderr,"Have a section: %ld pages at %ld fontmem %ld\n", |
| 226 |
#else /* ~SHORTINT */ |
| 227 |
fprintf(stderr,"Have a section: %d pages at %d fontmem %d\n", |
| 228 |
#endif /* ~SHORTINT */ |
| 229 |
(integer)pagecount, (integer)thissecloc, (integer)thissectionmem); |
| 230 |
#endif /* DEBUG */ |
| 231 |
cp = (charusetype *) (sp + 1); |
| 232 |
fc = 0; |
| 233 |
for (f=fonthead; f; f=f->next) |
| 234 |
if (f->loaded==1 && f->psname) { |
| 235 |
register halfword b, bit; |
| 236 |
|
| 237 |
cp->psfused = (f->psflag & PREVPAGE); |
| 238 |
f->psflag = 0; |
| 239 |
cp->fd = f; |
| 240 |
c = 0; |
| 241 |
for (b=0; b<16; b++) { |
| 242 |
cp->bitmap[b] = 0; |
| 243 |
for (bit=32768; bit!=0; bit>>=1) { |
| 244 |
if (f->chardesc[c].flags & PREVPAGE) |
| 245 |
cp->bitmap[b] |= bit; |
| 246 |
c++; |
| 247 |
} |
| 248 |
} |
| 249 |
cp++; |
| 250 |
} |
| 251 |
cp->fd = NULL; |
| 252 |
} |
| 253 |
} |
| 254 |
} |