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