1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
|
static char rcsid[] = "$Header: /usr/jjc/dvitops/RCS/font.c,v 1.6 90/03/12 18:47:02 jjc Exp $";
#include "dvitops.h"
#define MAXFONTSIZE (20*1024) /*maximum printer VM that a bitmap font can use*/
#ifdef PROTO
static void load_tfm_file(int f);
static void scale_font(int i, FILE *fp);
static void find_font(char *name, FILE *fp);
#if 0
static void choose_ps_prolog(void);
#endif
static void read_f_directory(void);
static int compare(char *p1, char *p2);
static void choose_mags(void);
static int order_mag(char *p1, char *p2);
static void emit_encodings(FILE *psfp);
#else
static void load_tfm_file();
static void scale_font();
static void find_font();
#if 0
static void choose_ps_prolog();
#endif
static void read_f_directory();
static int compare();
static void choose_mags();
static int order_mag();
static void emit_encodings();
#endif
/* int nprolog_fonts = 2; */
#define MAGS_MAX 50
static struct postscript_f_list *postscript_fonts = NULL;
static struct f_info {
int no;
enum { BLANK, BITMAP, POSTSCRIPT, BITMAP_PROLOG } type;
int mag;
int same_font; /* index of next font that is the same */
long doc_chars; /* sum over each page of the number of distinct
characters from the font occurring on that page */
integer at_size;
integer checksum;
long usage_index; /* the higher this is, the better it is to download
it in the prologue rather than on each page */
int npages; /* how many pages is it used on */
char *texname;
char *psname; /* the postscript name */
char *encoding; /* name of the encoding vector */
struct postscript_f_list *ps;
char used[MAXCHARS]; /* which characters are used in the document */
integer width[MAXCHARS];
integer rounded_width[MAXCHARS];
char *filename;
} *font[MAXFONTS];
static int target_mag;
/* returns -1, 0, or 1 according as *p1 is better, equally good,
or worse as a choice of magnification when the desired
magnification is target_mag
*/
static int order_mag(p1, p2)
char *p1, *p2;
{
int n1 = *(int *)p1;
int n2 = *(int *)p2;
int d1 = abs(target_mag - n1);
int d2 = abs(target_mag - n2);
if (d2 < d1 && d2 <= 2)
return 1;
if (d1 < d2 && d1 <= 2)
return -1;
if (d1 == d2)
return 0;
if (n1 > n2)
return -1;
return 1;
}
static void choose_mags()
{
int i;
int nmags;
char *p;
static int mag[MAGS_MAX] = {TEXMAGS,0};
for (i = 0; i < MAGS_MAX && mag[i] != 0; i++)
;
nmags = i;
if ((p = getenv("TEXMAGS")) != NULL) {
static char sep_string[] = { AREA_LIST_SEP, ',', ':', ' ', '\t', '\0' }; /* be tolerant */
for (i = 0,p = strtok(p, sep_string); p != NULL && i < MAGS_MAX; p = strtok((char *)NULL, sep_string))
if ((mag[i] = atoi(p)) > 0)
++i;
nmags = i;
}
for (i = 0; i < MAXFONTS; i++)
if (font[i] != NULL && font[i]->type != POSTSCRIPT) {
int j;
target_mag = font[i]->mag;
qsort((char *)mag, nmags, sizeof(int), order_mag);
for (j = 0; j < nmags; j++) {
char *p = find_pk_file(font[i]->texname, mag[j]);
if (p != NULL) {
char buf[FILENAME_MAX + 20];
if (abs(mag[j] - font[i]->mag) > 2)
message(WARNING,
"no exact font for %s at %d dpi",
font[i]->texname, font[i]->mag);
font[i]->mag = mag[j];
font[i]->type = BITMAP;
sprintf(buf,"%s-%d", font[i]->texname, font[i]->mag);
font[i]->psname = strsave(buf);
font[i]->filename = strsave(p);
break;
}
}
}
}
static void read_f_directory()
{
char buf[512];
FILE *fp;
int i, j;
if ((fp = xfopen("dvitops.fnt", FALSE, texinputs, (char *)NULL)) == NULL)
message(FATAL_ERROR, "can't find dvitops.fnt");
while (fgets(buf, 512, fp) != NULL) {
char *ptr, *name, *psname;
char *encoding = NULL;
if ((ptr = strchr(buf, '%')) != NULL)
*ptr = '\0';
ptr = buf;
while (isspace(*ptr))
ptr++;
if (*ptr == '\0')
continue;
if ((name = strtok(ptr, " \t\n\r")) == NULL)
continue;
if ((psname = strtok((char *)NULL, " \t\n\r")) == NULL)
psname = name;
else
encoding = strtok((char *)NULL, "\t\n\r");
for (i = 0; i < MAXFONTS; i++)
if (font[i] != NULL && strcmp(font[i]->texname, name) == 0) {
font[i]->psname = strsave(psname);
font[i]->ps = add_postscript_font(psname);
font[i]->encoding = strsave(encoding);
font[i]->type = POSTSCRIPT;
}
}
choose_mags();
for (i = 0; i < MAXFONTS; i++)
if (font[i] != NULL && font[i]->type == BLANK)
message(WARNING,
"no font directory entry for %s: using blank space instead\n",
font[i]->texname);
fclose(fp);
for (i = 0; i < MAXFONTS; i++)
if (font[i] != NULL && font[i]->type == POSTSCRIPT)
for (j = i + 1; j < MAXFONTS; j++)
if (font[j] != NULL && font[j]->type == POSTSCRIPT
&& strcmp(font[i]->psname, font[j]->psname) == 0) {
font[i]->same_font = j;
break;
}
}
static int compare(p1, p2)
char *p1;
char *p2;
{
return (int)((*(struct f_info **)p2)->usage_index
- (*(struct f_info **)p1)->usage_index);
}
void f_prologue(fp)
FILE *fp;
{
struct f_info *p[MAXFONTS];
int i, j;
integer n;
struct postscript_f_list *q = postscript_fonts;
#if 0
choose_ps_prolog();
#endif
emit_encodings(fp);
for (q = postscript_fonts; q != NULL; q = q->next)
if (q->file != NULL && q->in_prolog) {
emit_ps_font(q->s, fp);
find_font(q->s, fp);
}
/* determine the most used bitmapped fonts */
for (i = j = 0; i < MAXFONTS; i++) {
if (font[i] != NULL && font[i]->type == BITMAP)
p[j++] = font[i];
if (font[i] != NULL)
font[i]->no = (unsigned char)i;
}
for (i = 0; i < j; i++) {
char *ptr = p[i]->used;
int k;
int nused = 0;
for (k = 0; k < MAXCHARS; k++)
if (ptr[k])
++nused;
p[i]->usage_index = p[i]->doc_chars - nused;
}
qsort((char *)p, j, sizeof(struct f_info *), compare);
n = 0;
/* download and scale the most used bitmapped fonts */
for (i = 0; i < j && p[i]->usage_index > 0
&& n + MAXFONTSIZE < f_memory; i++) {
int k = p[i]->no;
if (p[i]->type != BITMAP)
continue;
if (!load_pk_font(k, p[i]->filename,p[i]->used,p[i]->at_size,
p[i]->checksum, p[i]->width, p[i]->rounded_width)) {
p[i]->type = BLANK;
continue;
}
n += emit_pk_font(k, p[i]->psname, p[i]->used, fp);
free_pk_font(k);
p[i]->type = BITMAP_PROLOG;
find_font(p[i]->psname, fp);
scale_font(k, fp);
}
/* Load the pk files for the bitmapped fonts
not downloaded in the prologue;
load the tfm files for the bitmapped fonts for which no pk file
could be found, and for the PostScript files */
for (i = 0; i < MAXFONTS; i++) {
if (font[i] == NULL)
continue;
if (font[i]->type == BITMAP) {
if (!load_pk_font(i, font[i]->filename,
font[i]->used, font[i]->at_size,
font[i]->checksum, font[i]->width, font[i]->rounded_width))
font[i]->type = BLANK;
}
if (font[i]->type == BLANK || font[i]->type == POSTSCRIPT) {
load_tfm_file(i);
memcpy((char *)font[i]->rounded_width, (char *)font[i]->width,
MAXCHARS*sizeof(integer));
}
}
for (i = 0; i < MAXFONTS; i++)
if (font[i] != NULL && font[i]->type == POSTSCRIPT
&& font[i]->ps->in_prolog == TRUE)
scale_font(i, fp);
}
static void scale_font(i, fp)
int i;
FILE *fp;
{
fprintf(fp, "/F%d _%s", i, font[i]->psname);
if (font[i]->encoding != NULL)
fprintf(fp, "_%s", font[i]->encoding);
putc(' ', fp);
put_dim((long)(font[i]->at_size), fp);
fputs(" SF\n", fp);
}
static void find_font(name, fp)
char *name;
FILE *fp;
{
int i;
fprintf(fp, "/_%s /%s FF\n", name, name);
/* reencode all fonts based on it */
for (i = 0; i < MAXFONTS; i++)
if (font[i] != 0 && font[i]->encoding != 0
&& strcmp(font[i]->psname, name) == 0) {
int j;
for (j = 0; j < i; j++)
if (font[j] != 0 && font[j]->encoding != 0
&& strcmp(font[i]->encoding, font[j]->encoding) == 0
&& strcmp(font[j]->psname, name) == 0)
break;
if (j == i)
/* newdictname newfontname encodingvector basefontdict */
fprintf(fp, "/_%s_%s /%s_%s %s_encoding _%s RE\n",
name, font[i]->encoding,
name, font[i]->encoding,
font[i]->encoding,
name);
}
}
/* we might use the area argument in the future */
void f_def(fontno, name, area, checksum, at_size, design_size, magnification)
integer fontno;
char *name, *area;
integer checksum, at_size, design_size;
int magnification;
{
int i;
if (fontno < 0 || fontno > MAXFONTS)
message(FATAL_ERROR, "too many fonts");
i = (int)fontno;
if (font[i] != NULL)
cant_happen();
if ((font[i] = (struct f_info *)
calloc(1, sizeof(struct f_info))) == NULL)
out_of_memory();
font[i]->texname = strsave(name);
font[i]->checksum = checksum;
font[i]->at_size = at_size;
font[i]->mag = (int)(.5 +
((double)magnification*(double)at_size*(double)dpi)
/(design_size*1000.0));
font[i]->same_font = EOF;
font[i]->npages = 0;
if ((page_used[i] = (char *)calloc(MAXCHARS, 1)) == NULL)
out_of_memory();
}
void store_page_usage()
{
int i, j;
for (i = 0; i < MAXFONTS; i++) {
char *ptr = page_used[i], *q = font[i]->used;
int used = FALSE;
if (ptr != NULL)
for (j = 0; j < MAXCHARS; j++)
if (ptr[j]) {
q[j] = TRUE;
font[i]->doc_chars++;
ptr[j] = FALSE;
used = TRUE;
}
if (used)
(font[i]->npages)++;
}
}
char *strsave(s)
char *s;
{
char *p;
if (s == NULL)
return NULL;
if ((p = (char *)malloc(strlen(s) + 1)) == NULL)
out_of_memory();
strcpy(p, s);
return p;
}
/* this code is borrowed from sections 571, 572 of TeX the Program */
integer scale(x, z)
integer x, z;
{
integer sw;
integer a, b, c, d;
integer alpha, beta;
alpha = 16*z; beta = 16;
while (z >= 040000000L) {
z /= 2; beta /= 2;
}
d = x & 255;
c = (x >> 8) & 255;
b = (x >> 16) & 255;
a = (x >> 24) & 255;
sw = (((((d * z) / 0400) + (c * z)) / 0400) + (b * z)) / beta;
if ( a == 255)
sw -= alpha;
else if (a != 0)
cant_happen();
return sw;
}
static void load_tfm_file(f)
int f;
{
int i;
int lh, bc, ec, nw;
integer ds, cs;
FILE *tfmfp;
unsigned char width_index[MAXCHARS];
integer width_table[MAXCHARS];
if ((tfmfp = xfopen(font[f]->texname, TRUE, texfonts, ".tfm")) == NULL)
message(FATAL_ERROR, "couldn't find tfm file for %s",
font[f]->texname);
(void)sread2(tfmfp);
lh = sread2(tfmfp);
bc = sread2(tfmfp);
ec = sread2(tfmfp);
nw = sread2(tfmfp);
fseek(tfmfp, 14L, 1);
cs = sread4(tfmfp);
if (cs != font[f]->checksum)
message(WARNING, "font %s: checksum in tfm file doesn't match",
font[f]->texname);
ds = sread4(tfmfp); /* not used at the moment */
lh -= 2;
fseek(tfmfp, (long)lh*4, 1);
for (i = 0; i < ec + 1 - bc; ++i) {
width_index[i] = uread1(tfmfp);
fseek(tfmfp, 3L, 1);
}
for (i = 0; i < nw; ++i)
width_table[i] = scale(sread4(tfmfp), font[f]->at_size);
fclose(tfmfp);
/* Microsoft C 5.0's optimizer messes up this loop */
for (i = bc; i <= ec; i++)
font[f]->width[i] = width_table[width_index[i - bc]];
}
struct postscript_f_list *add_postscript_font(font)
char *font;
{
struct postscript_f_list *p;
for (p = postscript_fonts;p != NULL; p = p->next)
if (strcmp(font, p->s) == 0)
return p;
if ((p = (struct postscript_f_list *)malloc(
sizeof(struct postscript_f_list)+strlen(font))
) == NULL)
out_of_memory();
strcpy(p->s, font);
p->next = postscript_fonts;
p->file = NULL;
p->in_prolog = FALSE;
p->nuses = 0;
postscript_fonts = p;
return p;
}
void f_comment(psfp)
FILE *psfp;
{
int i;
int cols;
struct postscript_f_list *p;
read_f_directory();
cols = fprintf(psfp, "%%%%DocumentFonts:");
for (i = 0; i < MAXFONTS; i++)
if (font[i] != NULL && font[i]->doc_chars > 0
&& (font[i]->type == BITMAP || font[i]->type == BITMAP_PROLOG)) {
if (cols + strlen(font[i]->psname) > 70)
cols = fprintf(psfp, "\n%%%%+ %s", font[i]->psname);
else
cols += fprintf(psfp, " %s", font[i]->psname);
}
for (p = postscript_fonts; p != NULL; p = p->next) {
if (cols + strlen(p->s) > 70)
cols = fprintf(psfp, "\n%%%%+ %s",p->s) - 1;
else
cols += fprintf(psfp, " %s", p->s);
}
putc('\n', psfp);
cols = fprintf(psfp, "%%%%DocumentSuppliedFonts:");
for (i = 0; i < MAXFONTS; i++)
if (font[i] != NULL && font[i]->doc_chars > 0
&& (font[i]->type == BITMAP || font[i]->type == BITMAP_PROLOG)) {
if (cols + strlen(font[i]->psname) > 70)
cols = fprintf(psfp, "\n%%%%+ %s", font[i]->psname);
else
cols += fprintf(psfp, " %s", font[i]->psname);
}
putc('\n', psfp);
cols = fprintf(psfp, "%%%%DocumentNeededFonts:");
for (p = postscript_fonts; p != NULL; p = p->next) {
if (cols + strlen(p->s) > 70)
cols = fprintf(psfp, "\n%%%%+ %s",p->s) - 1;
else
cols += fprintf(psfp, " %s", p->s);
}
putc('\n', psfp);
}
void f_emit(i, fp)
int i;
FILE *fp;
{
if (font[i]->type == BITMAP) {
emit_pk_font(i, font[i]->psname, page_used[i], fp);
find_font(font[i]->psname, fp);
}
else if (font[i]->type == POSTSCRIPT && !font[i]->ps->in_prolog) {
emit_ps_font(font[i]->psname, fp);
find_font(font[i]->psname, fp);
}
}
void f_set(i, fp)
int i;
FILE *fp;
{
if (font[i]->type == BITMAP ||
(font[i]->type == POSTSCRIPT && !font[i]->ps->in_prolog))
scale_font(i, fp);
memset(page_used[i], '\0', MAXCHARS);
fprintf(fp, "F%d F\n", i);
}
void emit_ps_font(font, psfp)
char *font;
FILE *psfp;
{
fprintf(psfp, "%%%%IncludeFont: %s\n", font);
}
int f_is_blank(f)
int f;
{
return (font[f]->type == BLANK);
}
integer f_space(f)
int f;
{
return (font[f]->at_size/6);
}
integer f_width(f, c)
int f;
unsigned char c;
{
return font[f]->width[c];
}
integer f_rounded_width(f, c)
int f;
unsigned char c;
{
return font[f]->rounded_width[c];
}
int same_font(f)
int f;
{
return font[f] == NULL ? EOF : font[f]->same_font;
}
#if 0
static void choose_ps_prolog()
{
int i;
struct postscript_f_list *p, *s;
for (i = 0; i < MAXFONTS; ++i)
if (font[i] != NULL && font[i]->type == POSTSCRIPT)
font[i]->ps->nuses += font[i]->npages;
/* do a list insertion sort */
s = NULL;
p = postscript_fonts;
while (p != NULL) {
struct postscript_f_list *ss = s, *ssp = NULL, *temp;
while (ss != NULL && p->nuses < ss->nuses) {
ssp = ss;
ss = ss->next;
}
temp = p->next;
if (ssp == NULL) {
p->next = s;
s = p;
}
else {
ssp->next = p;
p->next = ss;
}
p = temp;
}
postscript_fonts = s;
for (p = postscript_fonts, i = 0; p != NULL && i < nprolog_fonts;
p = p->next, i++)
p->in_prolog = TRUE;
}
#endif
static void emit_encodings(psfp)
FILE *psfp;
{
int i;
for (i = 0; i < MAXFONTS; i++)
if (font[i] != 0 && font[i]->encoding != NULL) {
int j;
for (j = 0; j < i; j++)
if (font[j] != 0 && font[j]->encoding != 0
&& strcmp(font[j]->encoding, font[i]->encoding) == 0)
break;
if (j == i) {
char name[256];
FILE *fp = xfopen(font[i]->encoding, FALSE, texfonts, ".enc");
if (fp == NULL) {
message(FATAL_ERROR, "%s: no encoding file",
font[i]->encoding);
}
fprintf(psfp, "/%s_encoding [\n",
font[i]->encoding);
for (j = 0; j < 256 && fscanf(fp, "%s", name) == 1; j++)
fprintf(psfp, "/%s\n", name);
for (; j < 256; j++)
fputs("/.notdef\n", psfp);
fputs("] def\n", psfp);
}
}
}
/*
Local Variables:
tab-width: 4
c-indent-level: 4
c-continued-statement-offset: 4
c-brace-offset: -4
c-argdecl-indent: 0
c-label-offset: -4
End:
*/
|