File: pkfont.c

package info (click to toggle)
dvi2ps 3.0a1-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,392 kB
  • ctags: 1,750
  • sloc: ansic: 11,699; sh: 277; makefile: 235
file content (338 lines) | stat: -rw-r--r-- 7,731 bytes parent folder | download | duplicates (2)
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
/*
 *	This file is based on `texpr/dvi2lpb/pkfont.c'.
 *	The copyright of the original is:
 *		Copyright (c) Japan TeX Users Group, 1986 1987
 */

#include	"defs.h"
#include	"global.h"
#include	"rastfont.h"
#include	"pkfont.h"
#ifdef KPATHSEA
#include	<kpathsea/pathsearch.h>
#include	<kpathsea/tex-file.h>
#include	<kpathsea/tex-glyph.h>
#endif

int pktype_access();
void init_pk_fontinfo();
struct fontop pkop = {
    "pk",
    pathtype_init,
    pktype_access,
    init_pk_fontinfo,
};

static FILE *fntfp;
void loadpkchar();

pktype_access(proto, fe, acca)
char *proto;
struct font_entry *fe;
struct accarg *acca;
{
    BOOLEAN ok;
#ifdef KPATHSEA
    char *path, *base, *filename;
    const_string savepath;
    kpse_format_info_type *spec = &kpse_format_info[kpse_pk_format];
    kpse_glyph_file_type font_file;
#endif

    rast_mag(fe, acca, 1);
#ifdef KPATHSEA
    pavek(fe->name, &path, &base, proto, acca);
    if (path != NULL) {
	savepath = spec->path;
	spec->path = NULL;
	spec->override_path = path;
    }
    if (spec->path == NULL)
	kpse_init_format(kpse_pk_format);
    if (acca->acc_mode == ACC_GEN)
	spec->program_enabled_p = true;
    if (base == NULL)
	filename = kpse_find_pk(fe->n, acca->pv_mag, &font_file);
    else if (path == NULL)
	filename = kpse_find_pk(base, acca->pv_mag, &font_file);
    else
	filename = kpse_path_search(spec->path, base, false);
    if (path != NULL)
	spec->path = savepath;
    if (acca->acc_mode == ACC_GEN)
	spec->program_enabled_p = false;
    if (ok = (filename != NULL))
	strcpy(fe->name, filename);
#else
    pave(fe->name, proto, acca);
    ok = access(fe->name, R_OK) == 0;
#endif
#ifdef DEBUG
    rast_debug_report(fe, acca, ok);
#endif
    return ok;
}

void
init_pk_fontinfo(fe)
register struct font_entry *fe;
{
    void read_pk_fontinfo();

    fe->fnt_readfontinfo = read_pk_fontinfo;
    init_rast_fontinfo(fe);
}

void
read_pk_fontinfo(fe)
register struct font_entry *fe;
{
    register int cmd, i, t;
    struct rastinitfontinfo *rii;
    struct rastfntinfo *rfi;
    int ds;

    openfontfile(fe);
    fntfp = fe->openfile;
    i = 0;
    do {
	if (fseek(fntfp, (long)--i, 2) == -1)
	    Fatal("can't seek to the end of pk file %s\n", fe->name);
    } while ((t=getuint(fntfp, 1)) == PK_NO_OP);
    (void)fseek(fntfp, 0L, 0);
    if (t != PK_POST ||
	getuint(fntfp,1) != PK_PRE || getuint(fntfp,1) != PK_ID)
	Fatal("bad pk file %s\n", fe->name);
    (void)fseek(fntfp, (long)getuint(fntfp, 1), 1);	/* comment */
    ds = getuint(fntfp, 4);	/* design size */
    if ((t = getuint(fntfp, 4)) && fe->c && t != fe->c)
	Warning("font = \"%s\",\n-->font checksum = %d,\n-->dvi checksum = %d",
		fe->name, fe->c, t);
    (void)fseek(fntfp, 8L, 1);			/* hppp,vppp */

    rii = rastinifinfo(fe);
    rastfinfo(fe) = rfi = alloc_rastfinfo(rii->maxc+1, TRUE, rii);
#ifdef DEBUG
    if (Debug) {
	for (i = 0; i <= rii->maxc; i++)
	    rastfinfo(fe)->ch[i].where.pixptr = NULL;
    }
#endif
    dev_rast_initfe(fe);
    /* rfi->designsize = ds; */
    rfi->nfntchars = rii->maxc + 1;
    for (;;) {
	switch (cmd = getuint(fntfp, 1)) {

	case PK_XXX1: case PK_XXX2: case PK_XXX3: case PK_XXX4:
	    (void)fseek(fntfp, (long)getuint(fntfp, cmd-PK_XXX1+1), 1);
	    continue;

	case PK_YYY:
	    skipbytes(fntfp, 4);
	    continue;

	case PK_NO_OP:
	    continue;

	case PK_POST:
	    break;

	default:
	    if (cmd <= PK_FLAG)
		readpkchar(cmd, fe, rii);
	    else
		Fatal("illegal pk command %d in %s\n", cmd, fe->name);
	    continue;
	}
	break;
    }

#ifdef DEBUG
    if (Debug) {
	for (i = 0; i <= rii->maxc; i++)
	    if (rii->mark[i] && rastfinfo(fe)->ch[i].where.pixptr == NULL)
		Warning("The glyf of char %x in %s missing\n", i, fe->name);
    }
#endif
    free((char *)rii);
}

readpkchar(flag, fe, rii)
int flag;
register struct font_entry *fe;
struct rastinitfontinfo *rii;
{
    long pl;
    unsigned int cc;
    register int n;
    register struct rastchar_entry *ce;	/* temporary char_entry pointer  */

    if ((n = flag&7) == 7) {	/* long form */
	pl = getuint(fntfp, 4) - 8;	/* without tfmw,dx */
	if ((cc = getuint(fntfp, 4)) > MAXMARKCHAR) {
	    Warning("cc %d in pk file %s (skipped)", cc, fe->name);
	    (void)fseek(fntfp, pl+8, 1);
	    return;
	}
	if (rii->mark[cc] == FALSE) {
	    (void)fseek(fntfp, pl+8, 1);
	    return;
	}
	ce = &rastfinfo(fe)->ch[cc];
	ce->tfmw = scale(getuint(fntfp, 4), fe->s);
	skipbytes(fntfp, 4); 	/* dx */
	n = 4;
    } else if (n >= 4) {		/* extended short form */
	pl = (n-4)*65536 + getuint(fntfp, 2) - 3;	/* without tfmw */
	ce = &rastfinfo(fe)->ch[cc = getuint(fntfp, 1)];
	if (rii->mark[cc] == FALSE) {
	    (void)fseek(fntfp, pl+3, 1);
	    return;
	}
	ce->tfmw = scale(getuint(fntfp, 3), fe->s);
	n = 2;
    } else {			/* short form */
	pl = n*256 + getuint(fntfp, 1) - 3;	/* without tfmw */
	ce = &rastfinfo(fe)->ch[cc = getuint(fntfp, 1)];
	if (rii->mark[cc] == FALSE) {
	    (void)fseek(fntfp, pl+3, 1);
	    return;
	}
	ce->tfmw = scale(getuint(fntfp, 3), fe->s);
	n = 1;
    }
    skipbytes(fntfp, n); 		/* dy or dm */
    ce->width = getuint(fntfp, n);
    ce->height = getuint(fntfp, n);
    ce->xoffset = getint(fntfp, n);
    ce->yoffset = getint(fntfp, n);
    ce->nbpl = (unsigned short)(ce->width + 7) >> 3;

    loadpkchar(fe, ce, flag, (long)(pl-5*n));
    dev_rast_initfontdict(fe, cc);
}

static int	width, height, dyn_f;
static int	b_width, black;
static unsigned char *pixel;
static unsigned char pat[8] = {0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01};

/*	character shape definition	*/
/* ARGSUSED */
void
loadpkchar(fe, ce, flag, pl)
struct font_entry *fe;
register struct rastchar_entry *ce;
int flag;
long pl;
{
    dyn_f = flag/16;
    black = (flag&8) >> 3;
    width = ce->width;
    height = ce->height;
    b_width = ce->nbpl;
    if ((pixel = (unsigned char *)calloc((unsigned)height, (unsigned)b_width)) == NULL)
	Fatal("Unable to allocate memory for char\n");
    if (dyn_f == 14)
	unpack_raster();
    else
	unpack_run(pl);
    ce->where.pixptr = (char *)pixel;
}

/*	get nybble	*/

#define get_nyb()  ((nyb_f^=1)? (nyb_w=getuint(fntfp,1))/16: nyb_w&15)

/*	get run_count/repeat_count	*/

#define get_run()\
	for (;;) {\
		if ((i=get_nyb()) >= 14) {\
			rep = i*2-29;\
			continue;\
		} else if (i > dyn_f)\
			run = (i-dyn_f-1)*16 + get_nyb() + dyn_f + 1;\
		else if (i > 0)\
			run = i;\
		else /* i==0 */ {\
			do	++i;\
			while ((run = get_nyb()) == 0);\
			while (--i >= 0)\
				run = run*16 + get_nyb();\
			run += (13-dyn_f)*16 + dyn_f - 15;\
		}\
		if (rep >= 0)\
			break;\
		rep = run;\
	}\

#define	min(x,y)	((x)<=(y) ? (x) : (y))

/*	unpack run_encoded packet	*/

unpack_run(pl)
long pl;
{
    register unsigned char *p, *q;
    register i, run, rep, h, h1, v;
    int	nyb_f, nyb_w;
    long off;

    nyb_f = 0;
#ifdef lint
    nyb_w = 0;
#endif
    p = pixel;
    black ^= 1;
    run = 0;
    off = ftell(fntfp);
    for (v=0; v < height; v += rep+1) {
	rep = 0;
	for (h=0; h < width; h=h1) {
	    if (run == 0) {
		get_run();
		black ^= 1;
	    }
	    h1 = min(h+run, width);
	    if (black) {
		h1 = min(h1, (h+8)&~7);
		p[h/8] |= pat[h&7];
		if(h1 & 7)
		    p[h1/8] &= ~pat[h1&7];
	    }
	    run -= h1-h;
	}
	q = p;	p += b_width;
	for (h=rep*b_width; --h >= 0; )
	    *p++ = *q++;
    }
    (void)fseek(fntfp, off+pl, 0);
}

/*	unpack raster packet	*/

unpack_raster()
{
    register unsigned char *p;
    register n, m, t, h, v;

    p = pixel;
    n = 0;
    for (v=0; v < height; ++v) {
	for (h=0; h < width; h+=m) {
	    if (n == 0) {
		t = getuint(fntfp, 1);
		n = 8;
	    }
	    m = min(n, 8-(h&7));
	    m = min(m, width-h);
	    p[h/8] |= t >> (h&7);
	    t <<= m; n -= m;
	}
	if (width & 7)
	    p[width/8] &= ~pat[width&7];
	p += b_width;
    }
}