Debian Codegraph @

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