Debian Codegraph @

texk/dvipsk/finclude.c

raw
1 /*
2 * Code for allowing fonts to be used in PostScript files given via
3 * `psfile=...'.
4 *
5 * Modified to check for font usage (DocumentFonts and DocumentNeededResources)
6 * in all included postscript files. This information is added to the
7 * global list of postscript fonts used (ps_fonts_used). Each font is
8 * also looked up (from psfonts.map) and if a file needs to be downloaded
9 * for that font, it is added (with add_header).
10 * 11/1996 Aaron Sawdey
11 */
12 #include "dvips.h" /* The copyright notice in that file is included too! */
13 #ifdef KPATHSEA
14 #include <kpathsea/c-ctype.h>
15 #else
16 #include <ctype.h>
17 #if !defined(SYSV) && !defined(WIN32)
18 extern char *strtok(); /* some systems don't have this in strings.h */
19 #endif
20 #define ISXGIGIT isxdigit
21 #endif
22 #ifdef VMS
23 #define getname vms_getname
24 #endif
25
26 #ifndef STDC_HEADERS
27 double atof();
28 #endif
29
30 /*
31 * The external declarations:
32 */
33 #include "protos.h"
34
35 /*
36 * Create a font descriptor for a font included in a psfile. There will be
37 * no fontmaptype node for the resulting font descriptor until this font is
38 * encountered by fontdef() (if that ever happens).
39 */
40 static fontdesctype *
41 ifontdef(char *name, char *area,
42 int scsize, int dssize, char *scname)
43 {
44 fontdesctype *fp;
45
46 fp = newfontdesc((integer)0, scsize, dssize, name, area);
47 fp->scalename = scname;
48 fp->next = fonthead;
49 fonthead = fp;
50 return fp;
51 }
52 /*
53 * When a font appears in an included psfile for the first time, this routine
54 * links it into the fonthd[] array.
55 */
56 static void
57 setfamily(fontdesctype *f)
58 {
59 int i;
60
61 fontmem -= DICTITEMCOST;
62 for (i=0; i<nextfonthd; i++)
63 if (strcmp(f->name, fonthd[i]->name)==0
64 && strcmp(f->area, fonthd[i]->area)==0) {
65 f->nextsize = fonthd[i];
66 fonthd[i] = f;
67 return;
68 }
69 if (nextfonthd==MAXFONTHD)
70 error("! Too many fonts in included psfiles");
71 fontmem -= NAMECOST + strlen(f->name) + strlen(f->area);
72 fonthd[nextfonthd++] = f;
73 f->nextsize = NULL;
74 }
75 /*
76 * Convert file name s to a pair of new strings in the string pool.
77 * The first string is the original value of nextstring; the second
78 * string is the return value.
79 */
80 static char*
81 getname(char *s)
82 {
83 char *a, *p, sav;
84
85 a = NULL;
86 for (p=s; *p!=0; p++)
87 if (*p=='/')
88 a = p+1;
89 if (a==NULL) *nextstring++ = 0;
90 else { sav = *a;
91 *a = 0;
92 (void) newstring(s);
93 *a = sav;
94 s = a;
95 }
96 return newstring(s);
97 }
98 /*
99 * Mark character usage in *f based on the hexadecimal bitmap found in
100 * string s. A two-digit offset separated by a colon gives the initial
101 * character code. We have no way of knowing how many times each character
102 * is used or how many strings get created when showing the characters so
103 * we just estimate two usages per character and one string per pair of
104 * usages.
105 */
106 static void
107 includechars(fontdesctype *f, char *s)
108 {
109 int b, c, d;
110 int l = strlen(s);
111
112 while (l > 0 && (s[l-1] == '\n' || s[l-1] == '\r'))
113 s[--l] = 0;
114 if (!ISXDIGIT(s[0]) || !ISXDIGIT(s[1]) || s[2]!=':'
115 || strspn(s+3,"0123456789ABCDEFabcdef") < l-3) {
116 fprintf_str(stderr, "%s\n", s);
117 error("Bad syntax in included font usage table");
118 return;
119 }
120 c = (xdig[(int)(s[0])] << 4) + xdig[(int)(s[1])];
121 s += 2;
122 while (*++s) {
123 d = xdig[(int)*s];
124 for (b=8; b!=0; b>>=1) {
125 if ((d&b)!=0) {
126 pagecost ++;
127 (void) prescanchar(&f->chardesc[c]);
128 }
129 if (++c==256) return;
130 }
131 }
132 }
133 /*
134 * String p should be start after the ":" in a font declaration of the form
135 %*FONT: <tfm-name> <scaled-size> <design-size> <2-hex-digits>:<hex-string>
136 * where the sizes are floating-point numbers in units of PostScript points
137 * (TeX's "bp"). We update the data structures for the included font,
138 * charge fontmem for the VM used, and add to delchar if necessary.
139 * Note that the scaled size and the design size are multiplied by mag/1000.
140 * This is needed for the design size to undo the similar factor in conv since
141 * design sizes are not supposed to be affected by magnification. Applying
142 * the magnification factor to the scaled size selects magnified fonts as is
143 * appropriate in the normal case where the included PostScript is scaled by
144 * mag/1000. The definition of `fshow' in finclude.lpro unscales by `DVImag'
145 * to account for this. We cannot change the font scaled size to account for
146 * options like `hscale=' because then the definition of `fshow' would have
147 * to change.
148 */
149 static void
150 scan1fontcomment(char *p)
151 {
152 char *q, *name, *area;
153 char *scname; /* location in buffer where we got scsize */
154 integer scsize, dssize;
155 fontdesctype *fptr;
156 real DVIperBP;
157
158 DVIperBP = actualdpi/(72.0*conv) * (mag/1000.0);
159 p = strtok(p, " ");
160 if (p==NULL) return;
161 area = nextstring; /* tentatively in the string pool */
162 name = getname(p);
163 q = strtok((char *)0, " ");
164 if (p==NULL || (scsize=(integer)(atof(q)*DVIperBP))==0) {
165 fprintf_str(stderr, "%s\n",p);
166 error("No scaled size for included font");
167 nextstring = area; /* remove from string pool */
168 return;
169 }
170 scname = q;
171 q = strtok((char *)0, " ");
172 if (p==NULL || (dssize=(integer)(atof(q)*DVIperBP))==0) {
173 fprintf_str(stderr, "%s\n",p);
174 error("No design size for included font");
175 nextstring = area;
176 return;
177 }
178 q = strtok((char *)0, " ");
179 fptr = matchfont(name, area, scsize, scname);
180 if (!fptr) {
181 fptr = ifontdef(name, area, scsize, dssize, newstring(scname));
182 (void) preselectfont(fptr);
183 setfamily(fptr);
184 } else {
185 nextstring = area; /* remove from string pool */
186 (void) preselectfont(fptr);
187 if (fptr->scalename==NULL) {
188 fptr->scalename=newstring(scname);
189 setfamily(fptr);
190 }
191 }
192 includesfonts = 1;
193 fptr->psflag |= THISPAGE;
194 includechars(fptr, q);
195 }
196 /*
197 * Parse the arguments to a "%%VMusage" comment. The Adobe Type 1 Font Format
198 * book specifies two arguments. This routine will accept one or two arguments;
199 * if there are two arguments we take the maximum.
200 */
201 static integer
202 scanvm(char *p)
203 {
204 char* q;
205 integer vm, vmmax;
206
207 q = strtok(p, " ");
208 if (q==NULL) {
209 error("Missing data in VMusage comment");
210 return 0;
211 }
212 vmmax = atol(q);
213 q = strtok((char *)0, " ");
214 if (q!=NULL && (vm=atol(q))>vmmax)
215 vmmax = vm;
216 return vmmax;
217 }
218 /*
219 * Scan a list of font names.
220 * Each name is added to the list ps_fonts_used, and if it has
221 * an associated header file (from psfonts.map), the header file
222 * is added with add_header.
223 */
224 static void
225 scan_fontnames(char *str, const char *psfile)
226 {
227 char *p,*pe;
228 struct resfont *re;
229
230 /* Turn all newlines, CRs, and tabs into spaces. */
231 p = str;
232 while(*p) {
233 if(*p == '\r' || *p == '\n' || *p == '\t') *p = ' ';
234 p++;
235 }
236 /* Remove trailing spaces. */
237 p = str+strlen(str)-1;
238 while(p > str && *p == ' ') {
239 *p = '\0';
240 p--;
241 }
242
243 p = str;
244 while(*p == ' ') p++; /* skip leading whitespace */
245
246 while(p && *p) {
247 pe = strchr(p,' ');
248 if(pe != NULL) *pe = '\0';
249
250 add_name(p,&ps_fonts_used);
251
252 if (1) {
253 #ifdef DEBUG
254 if (dd(D_FONTS))
255 fprintf_str(stderr,
256 "Adding font '%s' from included postscript file '%s'.\n",
257 p,psfile);
258 #endif /* DEBUG */
259
260 re = findPSname(p);
261 if(re != NULL) {
262 if (re->sent != 2) {
263 if (re->Fontfile) {
264 add_header(re->Fontfile);
265 } else if (re->downloadheader) {
266 /* this code borrowed from residentfont() in resident.c */
267 char *cp = re->downloadheader;
268 char *q;
269
270 infont = re->PSname;
271 while (1) {
272 q = cp;
273 while (*cp && *cp != ' ')
274 cp++;
275 if (*cp) {
276 *cp = 0;
277 add_header(q);
278 *cp++ = ' ';
279 } else {
280 add_header(q);
281 break;
282 }
283 infont = 0;
284 }
285 }
286 infont = 0;
287 }
288 re->sent = 2;
289 if (unused_top_of_psnames < DOWNLOADEDPSSIZE) {
290 downloadedpsnames[unused_top_of_psnames] = xstrdup (re->PSname);
291 unused_top_of_psnames++;
292 }
293 } else {
294 char eb[1000];
295 snprintf(eb, sizeof(eb),
296 "Font %s used in file %s is not in the mapping file.",
297 p, psfile);
298 error(eb);
299 }
300 }
301
302 p = pe;
303 if(p != NULL) {
304 p++;
305 while(*p == ' ') p++; /* skip leading whitespace */
306 }
307 }
308
309 return;
310 }
311
312 /*
313 * fc_state == 0: normal state, looking for interesting comments
314 * fc_state == 1: looking for %%+ following %%DocumentFonts
315 * fc_state == 2: looking for "%%+ font" following %%DocumentNeededResources
316 */
317 static int fc_state = 0;
318 /*
319 * Do we need to check for information at the end of the postscript file?
320 */
321 static int check_atend = 0;
322
323 static void
324 scanfontusage(char *p, const char *psfile)
325 {
326 if (strncmp(p, "%%DocumentFonts: ",17) == 0) {
327 p += 17;
328 while (*p && *p <= ' ')
329 p++;
330 if(!strncmp(p,"(atend)",7)) {
331 check_atend = 1;
332 } else {
333 scan_fontnames(p,psfile);
334 fc_state = 1;
335 }
336 } else if (strncmp(p, "%%DocumentNeededFonts: ",23)==0) {
337 p += 23;
338 while (*p && *p <= ' ')
339 p++;
340 if(!strncmp(p,"(atend)",7)) {
341 check_atend = 1;
342 } else {
343 scan_fontnames(p,psfile);
344 fc_state = 1;
345 }
346 } else if (fc_state == 1 && strncmp(p,"%%+",3) == 0) {
347 scan_fontnames(p+3,psfile);
348 fc_state = 1;
349 } else if (strncmp(p, "%%DocumentNeededResources: ",27) == 0) {
350 p += 27;
351 while (*p && *p <= ' ')
352 p++;
353 if(!strncmp(p,"(atend)",7)) {
354 check_atend = 1;
355 } else {
356 if(!strncmp(p,"font ",5)) scan_fontnames(p+5,psfile);
357 fc_state = 2;
358 }
359 } else if (fc_state == 2 && strncmp(p,"%%+",3) == 0) {
360 p += 3;
361 while (*p && *p <= ' ')
362 p++;
363 if(!strncmp(p,"font ",5)) scan_fontnames(p+5,psfile);
364 fc_state = 2;
365 } else {
366 fc_state = 0;
367 }
368 return;
369 }
370
371 /*
372 * Scan an initial sequence of comment lines looking for font and memory
373 * usage specifications. This does not handle the "atend" construction.
374 */
375 void
376 scanfontcomments(const char *filename)
377 {
378 char p[500];
379 char *r;
380 FILE *f;
381 integer truecost = pagecost;
382 Boolean trueknown = 0;
383 fontdesctype *oldcf = curfnt;
384
385 #ifdef DEBUG
386 if (dd(D_FONTS))
387 fprintf_str(stderr,
388 "Checking for fonts in '%s'\n",filename);
389 #endif /* DEBUG */
390
391 if (*filename == '`') {
392 /*
393 * Allow scanning of ` commands. Better return same results both times.
394 */
395 f = popen(filename+1, "r");
396 SET_BINARY(fileno(f));
397 to_close = USE_PCLOSE;
398 } else {
399 f = search(figpath, filename, READ);
400 }
401 if (f) {
402 SET_BINARY(fileno(f));
403 fc_state = 0;
404 check_atend = 0;
405 while (fgets(p,500,f) && p[0]=='%' &&
406 (p[1]=='!' || p[1]=='%' || p[1]=='*')) {
407 if (strncmp(p, "%*Font:", 7) == 0) {
408 scan1fontcomment(p+7);
409 } else if (strncmp(p, "%%VMusage:", 9) == 0) {
410 truecost += scanvm(p+10);
411 trueknown = 1;
412 }
413 scanfontusage(p,filename);
414 }
415 if (trueknown)
416 pagecost = truecost;
417
418 if(check_atend) {
419 #ifdef DEBUG
420 if (dd(D_FONTS))
421 fprintf_str(stderr,
422 "Checking for (atend) fonts in '%s'\n",filename);
423 #endif /* DEBUG */
424
425 fc_state = 0;
426
427 fseek(f,-4096,2); /* seek to 4096 bytes before EOF. */
428 fgets(p,500,f); /* throw away a partial line. */
429
430 /* find %%Trailer */
431 while((r=fgets(p,500,f)) && strncmp(p,"%%Trailer",9));
432
433 /* look for specs that were deferred to the trailer. */
434 if(r != NULL) {
435 while(fgets(p,500,f)) {
436 if(p[0]=='%' && p[1]=='%') scanfontusage(p,filename);
437 }
438 }
439 #ifdef DEBUG
440 else { /* r == NULL */
441 if (dd(D_FONTS))
442 fprintf_str(stderr,
443 "Did not find %%%%Trailer in included file '%s'.\n",
444 filename);
445 }
446 #endif /* DEBUG */
447 }
448 close_file(f);
449 }
450 curfnt = oldcf;
451 }
452 /*
453 * Is string s less than 30 characters long with no special characters
454 * that are not allowed in PostScript commands.
455 */
456 static Boolean
457 okascmd(char *ss)
458 {
459 register int c = 0;
460 register char *s = ss;
461
462 while (*s)
463 if (*s<' ' || *s>126 || ++c==30)
464 return(0);
465 return(strcspn(ss,"()<>[]{}%/") == c);
466 }
467 /*
468 * Output font area and font name strings as a literal string
469 */
470 static void
471 nameout(char *area, char *name)
472 {
473 char buf[500];
474 char *s;
475
476 if (*area==0 && okascmd(name)) {
477 snprintf(buf, sizeof(buf), "/%s", name);
478 cmdout(name);
479 } else {
480 for (s=area; *s; s++)
481 scout(*s);
482 for (s=name; *s; s++)
483 scout(*s);
484 stringend();
485 cmdout("cvn");
486 }
487 }
488 /*
489 * Output commands for defining a table of PostScript font identifiers for
490 * fonts used in included psfiles in the current section.
491 */
492 void
493 fonttableout(void)
494 {
495 int i, k;
496 fontdesctype *f;
497
498 for (i=0; i<nextfonthd; i++) {
499 for (f=fonthd[i]; f!=NULL; f=f->nextsize)
500 if (f->psflag==EXISTS) break;
501 if (f!=NULL) {
502 nameout(f->area, f->name);
503 k = 0;
504 do {
505 if (f->psflag==EXISTS) {
506 cmdout(f->scalename);
507 lfontout((int)f->psname);
508 k++;
509 }
510 f = f->nextsize;
511 } while (f!=NULL);
512 numout((integer)k);
513 cmdout("fstore");
514 }
515 }
516 }