Debian Codegraph @

texk/dvipsk/loadfont.c

raw
1 /*
2 * Here's the code to load a PK file into memory.
3 * Individual bitmaps won't be unpacked until they prove to be needed.
4 */
5 #include "dvips.h" /* The copyright notice in that file is included too! */
6 #ifdef OS2
7 #include <stdlib.h>
8 #endif
9 #ifdef KPATHSEA
10 #include <kpathsea/c-pathmx.h>
11 #include <kpathsea/concatn.h>
12 #include <kpathsea/tex-glyph.h>
13 #include <kpathsea/tex-make.h>
14 #include <kpathsea/lib.h>
15 #ifndef MAXPATHLEN
16 #define MAXPATHLEN (256)
17 #endif
18 #else
19 #if defined(SYSV) || defined(VMS) || (defined(MSDOS) && !defined(__DJGPP__)) || defined(OS2) || defined(ATARIST)
20 #define MAXPATHLEN (256)
21 #else
22 #include <sys/param.h> /* for MAXPATHLEN */
23 #endif
24 #endif
25 /*
26 * The external declarations:
27 */
28 #include "protos_add.h"
29
30 char errbuf[1500];
31 int lastresortsizes[40];
32 int bitmapfontseen = 0 ;
33 /*
34 * Now we have some routines to get stuff from the PK file.
35 * Subroutine pkbyte returns the next byte.
36 */
37 FILE *pkfile;
38 char name[MAXPATHLEN];
39 void
40 badpk(const char *s)
41 {
42 char *msg = concatn ("! Bad PK file ", name, ": ", s, NULL);
43 error(msg);
44 }
45
46 shalfword
47 pkbyte(void)
48 {
49 register shalfword i;
50
51 if ((i=getc(pkfile))==EOF)
52 badpk("unexpected eof");
53 return(i);
54 }
55
56 integer
57 pkquad(void)
58 {
59 register integer i;
60
61 i = pkbyte();
62 if (i > 127)
63 i -= 256;
64 i = i * 256 + pkbyte();
65 i = i * 256 + pkbyte();
66 i = i * 256 + pkbyte();
67 return(i);
68 }
69
70 integer
71 pktrio(void)
72 {
73 register integer i;
74
75 i = pkbyte();
76 i = i * 256 + pkbyte();
77 i = i * 256 + pkbyte();
78 return(i);
79 }
80
81
82 /*
83 * pkopen opens the pk file. This is system dependent. We work really
84 * hard to open some sort of PK file.
85 */
86 #ifdef VMCMS /* IBM: VM/CMS - we let DVIPS EXEC handle this after
87 the DVIPS MODULE has finished to avoid
88 complications with system calls. */
89 int dontmakefont = 0;
90 #else
91 #ifdef MVSXA /* IBM: MVS/XA - we let system administrator handle this on
92 MVS/XA since some printers can't get to user
93 fonts anyway */
94 int dontmakefont = 1;
95 #else
96 int dontmakefont = 0; /* if makefont fails once we won't try again */
97 #endif /* IBM: VM/CMS */
98 #endif
99
100 static void
101 lectureuser(void) {
102 static int userwarned = 0;
103
104 if (! userwarned) {
105 error("Such scaling will generate extremely poor output.");
106 userwarned = 1;
107 }
108 }
109 static Boolean
110 pkopen(register fontdesctype *fd)
111 {
112 register char *d, *n;
113 const char *name_ret;
114 #ifdef KPATHSEA
115 int dpi_ret;
116 #else
117 int vdpi;
118 #endif
119 d = fd->area;
120 n = fd->name;
121 #ifndef KPATHSEA
122 if (*d==0)
123 d = pkpath;
124 #endif
125 #ifdef FONTLIB
126 if (*(fd->area) == 0) {
127 int del;
128 for (del=0; del<=RES_TOLERANCE(fd->dpi); del=del>0?-del:-del+1) {
129 if ((pkfile=flisearch(n, fd->dpi + del)) != (FILE *)NULL )
130 return(1);
131 }
132 }
133 #endif
134 {
135 #ifdef KPATHSEA
136 char *this_name = concat (d, n);
137
138 pkfile = pksearch(this_name, READBIN, fd->dpi, &name_ret, &dpi_ret);
139
140 if (!pkfile || !FILESTRCASEEQ (this_name, name_ret)) {
141 char *msg = concatn ("PK font ", fd->area, n, " not found; ", NULL);
142 /* wasting some memory */
143 if (!pkfile)
144 msg = concat (msg, "characters will be left blank.");
145 else
146 msg = concat3 (msg, "using ", name_ret);
147 dontmakefont = 1;
148 error (msg);
149
150 } else if (!kpse_bitmap_tolerance ((double) dpi_ret, (double) fd->dpi))
151 {
152 fd->loadeddpi = dpi_ret;
153 fd->alreadyscaled = 0;
154 sprintf(errbuf,
155 "Font %s at %d not found; scaling %d instead.",
156 n, fd->dpi, dpi_ret);
157 error(errbuf);
158 lectureuser();
159 }
160
161 if (this_name != name_ret)
162 free (this_name);
163
164 return pkfile != NULL;
165 }
166 #else
167 int del;
168 for (del=0; del<=RES_TOLERANCE(fd->dpi); del=del>0?-del:-del+1) {
169 if (actualdpi == vactualdpi) {
170 vdpi = 0;
171 } else {
172 vdpi = (2 * ((long)vactualdpi) * (fd->dpi + del) + actualdpi)
173 / (2 * actualdpi);
174 }
175 #ifdef MVSXA
176 sprintf(name, "pk%d(%s)", fd->dpi + del, n);
177 #else
178 sprintf(name, "%s.%dpk", n, fd->dpi + del);
179 #endif
180 if (0 != (pkfile=pksearch(d, name, READBIN, n, fd->dpi + del, vdpi)))
181 return(1);
182 }
183 }
184 if (d == pkpath) {
185 if (actualdpi == vactualdpi) {
186 vdpi = 0;
187 } else {
188 vdpi = (2 * ((long)vactualdpi) * fd->dpi + actualdpi)
189 / (2 * actualdpi);
190 }
191 #ifdef MVSXA
192 sprintf(name, "pk%d(%s)", fd->dpi, n);
193 #else
194 sprintf(name, "%s.%dpk", n, fd->dpi);
195 #endif
196 makefont(n, (int)fd->dpi, DPI);
197 if (dontmakefont == 0 &&
198 (pkfile = pksearch(d, name, READBIN, n, fd->dpi, vdpi)))
199 return(1);
200 #ifndef MSDOS
201 #ifdef OS2
202 if (_osmode == OS2_MODE)
203 dontmakefont = 1; /* failed so don't try again under OS/2,
204 but do try for MSDOS */
205 #else
206 #ifndef ATARIST
207 dontmakefont = 1;
208 #endif
209 #endif
210 #endif
211 }
212 /*
213 * If nothing above worked, then we get desparate. We attempt to
214 * open the stupid font at one of a small set of predefined sizes,
215 * and then use PostScript scaling to generate the correct size.
216 *
217 * We much prefer scaling up to scaling down, since scaling down
218 * can omit character features, so we try the larger sizes first,
219 * and then work down.
220 */
221 {
222 int i, j;
223
224 if (lastresortsizes[0] && fd->dpi < 30000) {
225 for (i=0; lastresortsizes[i] < fd->dpi; i++);
226 for (j = i-1; j >= 0; j--) {
227 if (actualdpi == vactualdpi) {
228 vdpi = 0;
229 } else {
230 vdpi = (2 * ((long)vactualdpi) * lastresortsizes[j] + actualdpi)
231 / (2 * actualdpi);
232 }
233 #ifdef MVSXA
234 sprintf(name, "pk%d(%s)", lastresortsizes[j], n);
235 #else
236 sprintf(name, "%s.%dpk", n, lastresortsizes[j]);
237 #endif
238 #ifdef FONTLIB
239 if (0 != (pkfile=flisearch(n,(halfword)lastresortsizes[j]))
240 || 0 != (pkfile=pksearch(d, name, READBIN, n,
241 (halfword)lastresortsizes[j], vdpi))) {
242 #else
243 if (0 != (pkfile=pksearch(d, name, READBIN, n,
244 (halfword)lastresortsizes[j], vdpi))) {
245 #endif
246 fd->loadeddpi = lastresortsizes[j];
247 fd->alreadyscaled = 0;
248 sprintf(errbuf,
249 "Font %s at %d dpi not found; scaling %d instead.",
250 n, fd->dpi, lastresortsizes[j]);
251 error(errbuf);
252 lectureuser();
253 return 1;
254 }
255 }
256 for (j = i; lastresortsizes[j] < 30000; j++) {
257 if (actualdpi == vactualdpi) {
258 vdpi = 0;
259 } else {
260 vdpi = (2 * ((long)vactualdpi) * lastresortsizes[j] + actualdpi)
261 / (2 * actualdpi);
262 }
263 sprintf(name, "%s.%dpk", n, lastresortsizes[j]);
264 #ifdef FONTLIB
265 if (0 != (pkfile=flisearch(n, (halfword)lastresortsizes[j]))
266 || 0 != (pkfile=pksearch(d, name, READBIN, n,
267 (halfword)lastresortsizes[j], vdpi))) {
268 #else
269 if (0 != (pkfile=pksearch(d, name, READBIN, n,
270 (halfword)lastresortsizes[j], vdpi))) {
271 #endif
272 fd->loadeddpi = lastresortsizes[j];
273 fd->alreadyscaled = 0;
274 sprintf(errbuf,
275 "Font %s at %d dpi not found; scaling %d instead.",
276 name, fd->dpi, lastresortsizes[j]);
277 error(errbuf);
278 lectureuser();
279 return 1;
280 }
281 }
282 }
283 }
284 #ifdef MVSXA
285 sprintf(name, "%s.pk%d", n, fd->dpi);
286 #else
287 sprintf(name, "%s.%dpk", n, fd->dpi);
288 #endif
289 sprintf(errbuf,
290 "Font %s%s not found, characters will be left blank.",
291 fd->area, name);
292 error(errbuf);
293 return(0);
294 #endif /* KPATHSEA */
295 }
296
297 /*
298 * Now our loadfont routine. We return an integer indicating the
299 * highest character code in the font, so we know how much space
300 * to reserve for the character. (It's returned in the font
301 * structure, along with everything else.)
302 */
303 void
304 loadfont(register fontdesctype *curfnt)
305 {
306 register integer i;
307 register shalfword cmd;
308 register integer k;
309 register integer length = 0;
310 register shalfword cc = 0;
311 register integer scaledsize = curfnt->scaledsize;
312 register quarterword *tempr;
313 register chardesctype *cd = 0;
314 int maxcc = 0;
315 int munged = 0;
316 /*
317 * We clear out some pointers:
318 */
319 if (curfnt->loaded == 3) {
320 for (i=0; i<256; i++) {
321 curfnt->chardesc[i].TFMwidth = 0;
322 curfnt->chardesc[i].packptr = NULL;
323 curfnt->chardesc[i].pixelwidth = 0;
324 curfnt->chardesc[i].flags &= EXISTS;
325 curfnt->chardesc[i].flags2 = 0;
326 }
327 } else {
328 for (i=0; i<256; i++) {
329 curfnt->chardesc[i].TFMwidth = 0;
330 curfnt->chardesc[i].packptr = NULL;
331 curfnt->chardesc[i].pixelwidth = 0;
332 curfnt->chardesc[i].flags = 0;
333 curfnt->chardesc[i].flags2 = 0;
334 }
335 }
336 curfnt->maxchars = 256; /* just in case we return before the end */
337 curfnt->llx = 0 ;
338 curfnt->lly = 0 ;
339 curfnt->urx = 0 ;
340 curfnt->ury = 0 ;
341 if (!pkopen(curfnt)) {
342 tfmload(curfnt);
343 return;
344 }
345 bitmapfontseen = 1 ;
346 curfnt->dir = 0;
347 if (!quiet) {
348 if (strlen(realnameoffile) + prettycolumn > STDOUTSIZE) {
349 fprintf(stderr, "\n");
350 prettycolumn = 0;
351 }
352 fprintf_str(stderr, "<%s>", realnameoffile);
353 prettycolumn += strlen(realnameoffile) + 2;
354 }
355 #ifdef DEBUG
356 if (dd(D_FONTS))
357 fprintf_str(stderr,"Loading pk font %s at %.1fpt\n",
358 curfnt->name, (real)scaledsize/(alpha*0x100000));
359 #endif /* DEBUG */
360 if (pkbyte()!=247)
361 badpk("expected pre");
362 if (pkbyte()!=89)
363 badpk("wrong id byte");
364 for(i=pkbyte(); i>0; i--)
365 pkbyte();
366 k = (integer)(alpha * (real)pkquad());
367 if (k > curfnt->designsize + fsizetol
368 || k < curfnt->designsize - fsizetol) {
369 char *msg = concat ("Design size mismatch in font ", curfnt->name);
370 error (msg);
371 free (msg);
372 }
373 k = pkquad();
374 if (k && curfnt->checksum)
375 if (k!=curfnt->checksum) {
376 char *msg = concat ("Checksum mismatch in font ", curfnt->name);
377 error (msg);
378 free (msg);
379 }
380 k = pkquad(); /* assume that hppp is correct in the PK file */
381 k = pkquad(); /* assume that vppp is correct in the PK file */
382 /*
383 * Now we get down to the serious business of reading character definitions.
384 */
385 while ((cmd=pkbyte())!=245) {
386 if (cmd < 240) {
387 switch (cmd & 7) {
388 case 0: case 1: case 2: case 3:
389 length = (cmd & 7) * 256 + pkbyte() - 3;
390 cc = pkbyte();
391 cd = curfnt->chardesc+cc;
392 if (nosmallchars || curfnt->dpi != curfnt->loadeddpi)
393 cd->flags |= BIGCHAR;
394 cd->TFMwidth = scalewidth(pktrio(), scaledsize);
395 cd->pixelwidth = pkbyte();
396 break;
397 case 4: case 5: case 6:
398 length = (cmd & 3) * 65536L + pkbyte() * 256L;
399 length = length + pkbyte() - 4L;
400 cc = pkbyte();
401 cd = curfnt->chardesc+cc;
402 cd->TFMwidth = scalewidth(pktrio(), scaledsize);
403 cd->flags |= BIGCHAR;
404 i = pkbyte();
405 cd->pixelwidth = i * 256 + pkbyte();
406 break;
407 case 7:
408 length = pkquad() - 11;
409 cc = pkquad();
410 if (cc<0 || cc>255) badpk("character code out of range");
411 cd = curfnt->chardesc + cc;
412 cd->flags |= BIGCHAR;
413 cd->TFMwidth = scalewidth(pkquad(), scaledsize);
414 cd->pixelwidth = (pkquad() + 32768) >> 16;
415 k = pkquad();
416 }
417 if (cd->pixelwidth == 0 && cd->TFMwidth != 0) {
418 if (cd->TFMwidth > 0)
419 k = (integer)(cd->TFMwidth * conv + 0.5);
420 else
421 k = -(integer)(-cd->TFMwidth * conv + 0.5);
422 if (k != 0) {
423 cd->pixelwidth = k;
424 munged++;
425 }
426 }
427 if (length <= 0)
428 badpk("packet length too small");
429 if (dopprescan && ((cd->flags & EXISTS) == 0)) {
430 for (length--; length>0; length--)
431 pkbyte();
432 } else {
433 if (cc > maxcc)
434 maxcc = cc;
435 if (bytesleft < length || (length > MINCHUNK && compressed)) {
436 #ifdef DEBUG
437 if (dd(D_MEM))
438 fprintf(stderr,
439 #ifdef SHORTINT
440 "Allocating new raster memory (%ld req, %ld left)\n",
441 length, bytesleft);
442 #else
443 "Allocating new raster memory (%d req, %d left)\n",
444 (int)length, (int)bytesleft);
445 #endif
446 #endif /* DEBUG */
447 if (length > MINCHUNK) {
448 tempr = (quarterword *)mymalloc((integer)length);
449 } else {
450 raster = (quarterword *)mymalloc((integer)RASTERCHUNK);
451 tempr = raster;
452 bytesleft = RASTERCHUNK - length;
453 raster += length;
454 }
455 } else {
456 tempr = raster;
457 bytesleft -= length;
458 raster += length;
459 }
460 cd->packptr = tempr;
461 *tempr++ = cmd;
462 for (length--; length>0; length--)
463 *tempr++ = pkbyte();
464 {
465 // update the global font bounding box
466 // this is only used to set font sizes for type 3 bitmap
467 // encoding.
468 integer cwidth, cheight, xoff, yoff ;
469 unpack_bb(cd, &cwidth, &cheight, &xoff, &yoff) ;
470 if (-xoff < curfnt->llx)
471 curfnt->llx = -xoff ;
472 if (cwidth - xoff > curfnt->urx)
473 curfnt->urx = cwidth - xoff ;
474 if (yoff - cheight < curfnt->lly)
475 curfnt->lly = yoff - cheight ;
476 if (yoff > curfnt->ury)
477 curfnt->ury = yoff ;
478 }
479 }
480 cd->flags2 |= EXISTS;
481 } else {
482 k = 0;
483 switch (cmd) {
484 case 243:
485 k = pkbyte();
486 if (k > 127)
487 k -= 256;
488 case 242:
489 k = k * 256 + pkbyte();
490 case 241:
491 k = k * 256 + pkbyte();
492 case 240:
493 k = k * 256 + pkbyte();
494 while (k-- > 0)
495 i = pkbyte();
496 break;
497 case 244:
498 k = pkquad();
499 break;
500 case 246:
501 break;
502 default:
503 badpk("! unexpected command");
504 }
505 }
506 }
507 #ifdef FONTLIB
508 if (flib)
509 flib = 0;
510 else
511 #endif
512 fclose(pkfile);
513 curfnt->loaded = 1;
514 curfnt->maxchars = maxcc + 1;
515 if (munged > 0) {
516 static int seen = 0;
517 sprintf(errbuf,
518 "Font %s at %d dpi has most likely been made improperly;",
519 curfnt->name, curfnt->dpi);
520 error(errbuf);
521 if (seen)
522 return;
523 seen = 1;
524 sprintf(errbuf,
525 "%d characters have 0 escapements but non-trivial TFM widths.", munged);
526 error(errbuf);
527 error(
528 "I'm stumbling along as best I can, but I recommend regenerating");
529 error(
530 "these fonts; the problem is probably that they are non-CM fonts");
531 error(
532 "(such as circle10 or line10) created with a MF with the CM base");
533 error("preloaded .");
534 }
535 }