summaryrefslogtreecommitdiff
path: root/backend/dvi/mdvi-lib/pk.c
blob: 5e8da5bb806a49a6f9d20c291a89f8467fca8f70 (plain)
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

/* Copyright (C) 2000, Matias Atria
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/*
 * History:
 *
 * 11/3/2000:
 *    - First working version
 * 11/4/2000:
 *    - FIXED: entirely white/black rows were missed.
 * 11/8/2000:
 *    - TESTED: Glyphs are rendered correctly in different byte orders.
 *    - Made bitmap code much more efficient and compact.
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>

#include "mdvi.h"
#include "private.h"

#define PK_ID      89
#define PK_CMD_START 240
#define PK_X1     240
#define PK_X2     241
#define PK_X3     242
#define PK_X4     243
#define PK_Y      244
#define PK_POST   245
#define PK_NOOP   246
#define PK_PRE    247

#define PK_DYN_F(x)	(((x) >> 4) & 0xf)
#define PK_PACKED(x)	(PK_DYN_F(x) != 14)

static int pk_load_font __PROTO((DviParams *, DviFont *));
static int pk_font_get_glyph __PROTO((DviParams *, DviFont *, int));

static int pk_auto_generate = 1; /* this is ON by default */

typedef struct {
	char	currbyte;
	char	nybpos;
	int	dyn_f;
} pkread;

static char *pk_lookup __PROTO((const char *, Ushort *, Ushort *));
static char *pk_lookupn __PROTO((const char *, Ushort *, Ushort *));

/* only symbols exported by this file */
DviFontInfo pk_font_info = {
	"PK",
	0, /* scaling not supported natively */
	pk_load_font,
	pk_font_get_glyph,
	mdvi_shrink_glyph,
	mdvi_shrink_glyph_grey,
	NULL,	/* free */
	NULL,	/* reset */
	pk_lookup,	/* lookup */
	kpse_pk_format,
	NULL
};

DviFontInfo pkn_font_info = {
	"PKN",
	0, /* scaling not supported natively */
	pk_load_font,
	pk_font_get_glyph,
	mdvi_shrink_glyph,
	mdvi_shrink_glyph_grey,
	NULL,	/* free */
	NULL,	/* reset */
	pk_lookupn,	/* lookup */
	kpse_pk_format,
	NULL
};

static char *pk_lookup(const char *name, Ushort *hdpi, Ushort *vdpi)
{
	kpse_glyph_file_type type;
	char *filename;

	if(pk_auto_generate == 0) {
		kpse_set_program_enabled(kpse_pk_format, 1, kpse_src_cmdline);
		pk_auto_generate = 1;
	}
	filename = kpse_find_glyph(name, Max(*hdpi, *vdpi),
		kpse_pk_format, &type);
	if(filename && type.source == kpse_glyph_source_fallback) {
		mdvi_free(filename);
		filename = NULL;
	} else if(filename) {
		*hdpi = *vdpi = type.dpi;
	}
	return filename;
}

static char *pk_lookupn(const char *name, Ushort *hdpi, Ushort *vdpi)
{
	kpse_glyph_file_type type;
	char *filename;

	if(pk_auto_generate) {
		kpse_set_program_enabled(kpse_pk_format, 0, kpse_src_cmdline);
		pk_auto_generate = 0;
	}
	filename = kpse_find_glyph(name, Max(*hdpi, *vdpi),
		kpse_pk_format, &type);
	if(filename && type.source == kpse_glyph_source_fallback) {
		mdvi_free(filename);
		filename = NULL;
	} else if(filename) {
		*hdpi = *vdpi = type.dpi;
	}
	return filename;
}

static inline int pk_get_nyb(FILE *p, pkread *pk)
{
	unsigned t;
	int	nb;
	char	c;

	t = c = pk->currbyte;
	nb = pk->nybpos;

	switch(nb) {
	case 0:
		c = pk->currbyte = fuget1(p);
		t = (c >> 4);
		break;
	case 1:
		t = c;
		break;
	}
	pk->nybpos = !nb;
	return (t & 0xf);
}

/*
 * this is a bit cumbersome because we have to pass around
 * the `pkread' data...
 */
static int pk_packed_num(FILE *p, pkread *pkr, int *repeat)
{
	int	i, j;
	int	dyn_f = pkr->dyn_f;

	i = pk_get_nyb(p, pkr);
	if(i == 0) {
		do {
			j = pk_get_nyb(p, pkr);
			i++;
		} while(j == 0);
		while(i-- > 0)
			j = (j << 4) + pk_get_nyb(p, pkr);
		return (j - 15 + ((13 - dyn_f) << 4) +
			dyn_f);
	} else if(i <= dyn_f)
		return i;
	else if(i < 14)
		return ((i - dyn_f - 1) << 4) +
			pk_get_nyb(p, pkr) + dyn_f + 1;
	else {
		*repeat = 1;
		if(i == 14)
			*repeat = pk_packed_num(p, pkr, repeat);
		return pk_packed_num(p, pkr, repeat);
	}
}

#define ROUND(x,y)	(((x) + (y) - 1) / (y))

static BITMAP *get_bitmap(FILE *p, int w, int h, int flags)
{
	int	i, j;
	BmUnit	*ptr;
	BITMAP	*bm;
	int	bitpos;
	int	currch;

	flags = 0; /* shut up that compiler */
	bitpos = -1;
	if((bm = bitmap_alloc(w, h)) == NULL)
		return NULL;
	DEBUG((DBG_BITMAPS, "get_bitmap(%d,%d,%d): reading raw bitmap\n",
		w, h, flags));
	ptr = bm->data;
	currch = 0;
	for(i = 0; i < h; i++) {
		BmUnit	mask;

		mask = FIRSTMASK;
		for(j = 0; j < w; j++) {
			if(bitpos < 0) {
				currch = fuget1(p);
				bitpos = 7;
			}
			if(currch & (1 << bitpos))
				*ptr |= mask;
			bitpos--;
			if(mask == LASTMASK) {
				ptr++;
				mask = FIRSTMASK;
			} else
				NEXTMASK(mask);
		}
		ptr = bm_offset(ptr, bm->stride);
	}
	return bm;
}

static BITMAP *get_packed(FILE *p, int w, int h, int flags)
{
	int	inrow, count;
	int	row;
	BITMAP	*bm;
	int	repeat_count;
	int	paint;
	pkread	pkr;

	pkr.nybpos = 0;
	pkr.currbyte = 0;
	pkr.dyn_f = PK_DYN_F(flags);
	paint = !!(flags & 0x8);

	repeat_count = 0;
	row = 0;
	inrow = w;
	if((bm = bitmap_alloc(w, h)) == NULL)
		return NULL;
	DEBUG((DBG_BITMAPS, "get_packed(%d,%d,%d): reading packed glyph\n",
		w, h, flags));
	while(row < h) {
		int	i = 0;

		count = pk_packed_num(p, &pkr, &i);
		if(i > 0) {
			if(repeat_count)
				fprintf(stderr, "second repeat count for this row (had %d and got %d)\n",
					repeat_count, i);
			repeat_count = i;
		}

		if(count >= inrow) {
			Uchar	*r, *t;
			BmUnit	*a, mask;

			/* first finish current row */
			if(paint)
				bitmap_set_row(bm, row, w - inrow, inrow, paint);
			/* now copy it as many times as required */
			r = (Uchar *)bm->data + row * bm->stride;
			while(repeat_count-- > 0) {
				t = r + bm->stride;
				/* copy entire lines */
				memcpy(t, r, bm->stride);
				r = t;
				row++;
			}
			repeat_count = 0;
			/* count first row we drew */
			row++;
			/* update run count */
			count -= inrow;
			/* now r points to the beginning of the last row we finished */
			if(paint)
				mask = ~((BmUnit)0);
			else
				mask = 0;
			/* goto next row */
			a = (BmUnit *)(r + bm->stride);
			/* deal with entirely with/black rows */
			while(count >= w) {
				/* count number of atoms in a row */
				i = ROUND(w, BITMAP_BITS);
				while(i-- > 0)
					*a++ = mask;
				count -= w;
				row++;
			}
			inrow = w;
		}
		if(count > 0)
			bitmap_set_row(bm, row, w - inrow, count, paint);
		inrow -= count;
		paint = !paint;
	}
	if(row != h || inrow != w) {
		mdvi_error(_("Bad PK file: More bits than required\n"));
		bitmap_destroy(bm);
		return NULL;
	}
	return bm;
}

static BITMAP *get_char(FILE *p, int w, int h, int flags)
{
	/* check if dyn_f == 14 */
	if(((flags >> 4) & 0xf) == 14)
		return get_bitmap(p, w, h, flags);
	else
		return get_packed(p, w, h, flags);
}

/* supports any number of characters in a font */
static int pk_load_font(DviParams *unused, DviFont *font)
{
	int	i;
	int	flag_byte;
	int	hic, maxch;
	Int32	checksum;
	FILE	*p;
#ifndef NODEBUG
	char	s[256];
#endif
	long	alpha, beta, z;
	unsigned int loc;

	font->chars = xnalloc(DviFontChar, 256);
	p = font->in;
	memzero(font->chars, 256 * sizeof(DviFontChar));
	for(i = 0; i < 256; i++)
		font->chars[i].offset = 0;

	/* check the preamble */
	loc = fuget1(p); hic = fuget1(p);
	if(loc != PK_PRE || hic != PK_ID)
		goto badpk;
	i = fuget1(p);
#ifndef NODEBUG
	for(loc = 0; loc < i; loc++)
		s[loc] = fuget1(p);
	s[loc] = 0;
	DEBUG((DBG_FONTS, "(pk) %s: %s\n", font->fontname, s));
#else
	fseek(in, (long)i, SEEK_CUR);
#endif
	/* get the design size */
	font->design = fuget4(p);
	/* get the checksum */
	checksum = fuget4(p);
	if(checksum && font->checksum && font->checksum != checksum) {
		mdvi_warning(_("%s: checksum mismatch (expected %u, got %u)\n"),
			     font->fontname, font->checksum, checksum);
	} else if(!font->checksum)
		font->checksum = checksum;
	/* skip pixel per point ratios */
	fuget4(p);
	fuget4(p);
	if(feof(p))
		goto badpk;

	/* now start reading the font */
	loc = 256; hic = -1; maxch = 256;

	/* initialize alpha and beta for TFM width computation */
	TFMPREPARE(font->scale, z, alpha, beta);

	while((flag_byte = fuget1(p)) != PK_POST) {
		if(feof(p))
			break;
		if(flag_byte >= PK_CMD_START) {
			switch(flag_byte) {
			case PK_X1:
			case PK_X2:
			case PK_X3:
			case PK_X4: {
#ifndef NODEBUG
				char	*t;
				int	n;

				i = fugetn(p, flag_byte - PK_X1 + 1);
				if(i < 256)
					t = &s[0];
				else
					t = mdvi_malloc(i + 1);
				for(n = 0; n < i; n++)
					t[n] = fuget1(p);
				t[n] = 0;
				DEBUG((DBG_SPECIAL, "(pk) %s: Special \"%s\"\n",
					font->fontname, t));
				if(t != &s[0])
					mdvi_free(t);
#else
				i = fugetn(p, flag_byte - PK_X1 + 1);
				while(i-- > 0)
					fuget1(p);
#endif
				break;
			}
			case PK_Y:
				i = fuget4(p);
				DEBUG((DBG_SPECIAL, "(pk) %s: MF special %u\n",
					font->fontname, (unsigned)i));
				break;
			case PK_POST:
			case PK_NOOP:
				break;
			case PK_PRE:
				mdvi_error(_("%s: unexpected preamble\n"), font->fontname);
				goto error;
			}
		} else {
			int	pl;
			int	cc;
			int	w, h;
			int	x, y;
			int	offset;
			long	tfm;

			switch(flag_byte & 0x7) {
			case 7:
				pl = fuget4(p);
				cc = fuget4(p);
				offset = ftell(p) + pl;
				tfm = fuget4(p);
				fsget4(p); /* skip dx */
				fsget4(p); /* skip dy */
				w  = fuget4(p);
				h  = fuget4(p);
				x  = fsget4(p);
				y  = fsget4(p);
				break;
			case 4:
			case 5:
			case 6:
				pl = (flag_byte % 4) * 65536 + fuget2(p);
				cc = fuget1(p);
				offset = ftell(p) + pl;
				tfm = fuget3(p);
				fsget2(p); /* skip dx */
				           /* dy assumed 0 */
				w = fuget2(p);
				h = fuget2(p);
				x = fsget2(p);
				y = fsget2(p);
				break;
			default:
				pl = (flag_byte % 4) * 256 + fuget1(p);
				cc = fuget1(p);
				offset = ftell(p) + pl;
				tfm = fuget3(p);
				fsget1(p); /* skip dx */
				           /* dy assumed 0 */
				w = fuget1(p);
				h = fuget1(p);
				x = fsget1(p);
				y = fsget1(p);
			}
			if(feof(p))
				break;

			/* Although the PK format support bigger char codes,
                         * XeTeX and other extended TeX engines support charcodes up to
                         * 65536, while normal TeX engine supports only charcode up to 255.*/
			if (cc < 0 || cc > 65536) {
				mdvi_error (_("%s: unexpected charcode (%d)\n"),
					    font->fontname,cc);
				goto error;
			}
			if(cc < loc)
				loc = cc;
			if(cc > hic)
				hic = cc;
			if(cc > maxch) {
				font->chars = xresize(font->chars,
					DviFontChar, cc + 16);
				for(i = maxch; i < cc + 16; i++)
					font->chars[i].offset = 0;
				maxch = cc + 16;
			}
			font->chars[cc].code = cc;
			font->chars[cc].flags = flag_byte;
			font->chars[cc].offset = ftell(p);
			font->chars[cc].width = w;
			font->chars[cc].height = h;
			font->chars[cc].glyph.data = NULL;
			font->chars[cc].x = x;
			font->chars[cc].y = y;
			font->chars[cc].glyph.x = x;
			font->chars[cc].glyph.y = y;
			font->chars[cc].glyph.w = w;
			font->chars[cc].glyph.h = h;
			font->chars[cc].grey.data = NULL;
			font->chars[cc].shrunk.data = NULL;
			font->chars[cc].tfmwidth = TFMSCALE(z, tfm, alpha, beta);
			font->chars[cc].loaded = 0;
			fseek(p, (long)offset, SEEK_SET);
		}
	}
	if(flag_byte != PK_POST) {
		mdvi_error(_("%s: unexpected end of file (no postamble)\n"),
			   font->fontname);
		goto error;
	}
	while((flag_byte = fuget1(p)) != EOF) {
		if(flag_byte != PK_NOOP) {
			mdvi_error(_("invalid PK file! (junk in postamble)\n"));
			goto error;
		}
	}

	/* resize font char data */
	if(loc > 0 || hic < maxch-1) {
		memmove(font->chars, font->chars + loc,
			(hic - loc + 1) * sizeof(DviFontChar));
		font->chars = xresize(font->chars,
			DviFontChar, hic - loc + 1);
	}
	font->loc = loc;
	font->hic = hic;
	return 0;

badpk:
	mdvi_error(_("%s: File corrupted, or not a PK file\n"), font->fontname);
error:
	mdvi_free(font->chars);
	font->chars = NULL;
	font->loc = font->hic = 0;
	return -1;
}

static int pk_font_get_glyph(DviParams *params, DviFont *font, int code)
{
	DviFontChar	*ch;

	if((ch = FONTCHAR(font, code)) == NULL)
		return -1;

	if(ch->offset == 0)
		return -1;
	DEBUG((DBG_GLYPHS, "(pk) loading glyph for character %d (%dx%d) in font `%s'\n",
		code, ch->width, ch->height, font->fontname));
	if(font->in == NULL && font_reopen(font) < 0)
		return -1;
	if(!ch->width || !ch->height) {
		/* this happens for ` ' (ASCII 32) in some fonts */
		ch->glyph.x = ch->x;
		ch->glyph.y = ch->y;
		ch->glyph.w = ch->width;
		ch->glyph.h = ch->height;
		ch->glyph.data = NULL;
		return 0;
	}
	if(fseek(font->in, ch->offset, SEEK_SET) == -1)
		return -1;
	ch->glyph.data = get_char(font->in,
		ch->width, ch->height, ch->flags);
	if(ch->glyph.data) {
		/* restore original settings */
		ch->glyph.x = ch->x;
		ch->glyph.y = ch->y;
		ch->glyph.w = ch->width;
		ch->glyph.h = ch->height;
	} else
		return -1;
	ch->loaded = 1;
	return 0;
}