summaryrefslogtreecommitdiff
path: root/dviware/quicspool/src/pk.c
blob: 09d1b2086692b127da6d2720329ba281601564d9 (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
#ifndef lint
static char	*rcs = "$Header: pk.c,v 1.1 88/01/15 13:04:52 simpson Rel $";
#endif
/*
$Log:	pk.c,v $
 * Revision 1.1  88/01/15  13:04:52  simpson
 * initial release
 * 
 * Revision 0.1  87/12/11  18:31:09  simpson
 * beta test
 * 
*/
#include <stdio.h>
#include <assert.h>
#include <local/standard.h>
#include "constants.h"
#include "fontinfo.h"
#include "pk.h"

/* Routines pertaining to PK files. */

static int  BitNumber;	/* Count of bits read in a byte */
static int  Byte;	/* Byte currently reading bits or nybbles from */
static int  RepeatCount; /* Count found during unpacking of packed number */

/* Resets the bit count so bit and nybble reading start on a byte boundary. */
static void resetbitcount()
{
    BitNumber = 0;
}

/* Returns the next nybble from the file.  This routine uses the cgetc()
 * function so an appropriate error routine will need to be set.
 */
static int getnyb(f)
FILE	*f;
{
    if (BitNumber == 0) {
	Byte = cgetc(f);
	BitNumber = 8;
    }
    if (BitNumber == 8) {
	BitNumber -= 4;
	return Byte >> 4 & 0xF;
    } else {
	BitNumber -= 4;
	return Byte & 0xF;
    }
}

/* Returns true if the next bit is on, false otherwise.  This routine uses the
 * cgetc() function so an appropriate error routine will need to be set.
 */
static Boolean getbit(f)
FILE	*f;
{
    if (BitNumber == 0) {
	Byte = cgetc(f);
	BitNumber = 8;
    }
    return Byte & 1 << --BitNumber ? TRUE : FALSE;
}

/* Returns the next packed number from the file f.  The global variable
 * RepeatCount is set appropriately if a repeat count is found.  This routine
 * uses the cgetc() function so an appropriate error routine will need to be
 * set.
 */
static int pkpackednum(f, dynf)
register FILE	*f;
register int	dynf;
{
    register int    i, j;

    i = getnyb(f);
    if (i == 0) {
	do {
	    j = getnyb(f);
	    i++;
	} while (j == 0);
	while (i-- > 0)
	    j = (j << 4) + getnyb(f);
	return j - 15 + ((13 - dynf) << 4) + dynf;
    } else if (i <= dynf)
	return i;
    else if (i < 14)
	return ((i - dynf - 1) << 4) + getnyb(f) + dynf + 1;
    else {
	if (i == 14)
	    RepeatCount = pkpackednum(f, dynf);
	else
	    RepeatCount = 1;
	return pkpackednum(f, dynf);
    }
    /* NOTREACHED */
}

/* Returns information about a PK file or NULL if it couldn't open the file
 * or the file is an invalid PK file.  The information returned is static and
 * is overwritten with each call.  Downloadtype should be 'X' or 'Y' (see the
 * QMS Programming Manual).  This function uses the cgetc() routine so an
 * appropriate error recovery function should be set.
 */
struct FontInfo *getfontinfo(filename, downloadtype)
char *filename;
int  downloadtype;
{
    unsigned long	    uinteger(), li, lk;
    long		    integer(), pl, cc;
    static struct FontInfo  fi;
    FILE		    *f;
    int			    wordsneeded = 0, i, j, command, flag;
    int			    heightstorage[256][2]; /* 0=h,1=voff f/each char*/
    int			    largestvoff;    /* Largest voff of all chars */

    if (!(f = fopen(filename, "r")))
	return NULL;
    if (cgetc(f) != PKPRE || cgetc(f) != PKID) {
	(void)fclose(f);
	return NULL;
    }
    bzero((char *)&fi, sizeof(struct FontInfo));
    bzero((char *)heightstorage, sizeof(heightstorage));
    for (i = 0, j = cgetc(f); i < j; i++)
	fi.comment[i] = cgetc(f);
    fi.comment[i] = '\0';
    fi.ds = integer(f, 4);
    fi.cs = integer(f, 4);
    fi.hppp = integer(f, 4);
    fi.vppp = integer(f, 4);
    fi.numchars = fi.qmsheight = largestvoff = 0;
    while ((command = cgetc(f)) != PKPOST)
	if (0 <= command && command <= 239) {
	    flag = command;
	    if ((flag & 0x7) == 0x7) {	/* Long format preamble */
		pl = integer(f, 4);
		cc = integer(f, 4);
		fi.chararray[cc].tfm = integer(f, 4);
		fi.chararray[cc].dx = integer(f, 4);
		fi.chararray[cc].dy = integer(f, 4);
		fi.chararray[cc].w = integer(f, 4);
		fi.chararray[cc].h = integer(f, 4);
		fi.chararray[cc].hoff = integer(f, 4);
		fi.chararray[cc].voff = integer(f, 4);
		(void)fseek(f, pl - 28, 1);
	    } else if (0 <= (flag & 0x7) && (flag & 0x7) <= 3) { /* Short */
		pl = cgetc(f) | (flag & 0x3) << 8;
		cc = cgetc(f);
		fi.chararray[cc].tfm = uinteger(f, 3);
		fi.chararray[cc].dx = fi.chararray[cc].dy = cgetc(f) * SPOINT;
		fi.chararray[cc].w = cgetc(f);
		fi.chararray[cc].h = cgetc(f);
		fi.chararray[cc].hoff = integer(f, 1);
		fi.chararray[cc].voff = integer(f, 1);
		(void)fseek(f, pl - 8, 1);
	    } else {			/* Extended short form */
		pl = uinteger(f, 2) | (flag & 0x3) << 16;
		cc = cgetc(f);
		fi.chararray[cc].tfm = uinteger(f, 3);
		fi.chararray[cc].dx = fi.chararray[cc].dy = uinteger(f, 2)
		* SPOINT;
		fi.chararray[cc].w = uinteger(f, 2);
		fi.chararray[cc].h = uinteger(f, 2);
		fi.chararray[cc].hoff = integer(f, 2);
		fi.chararray[cc].voff = integer(f, 2);
		(void)fseek(f, pl - 13, 1);
	    }
	    assert(0 <= cc && cc <=255);
	    fi.numchars++;
	    fi.qmsheight = MAX(fi.qmsheight, fi.chararray[cc].h);
	    largestvoff = MAX(largestvoff, fi.chararray[cc].voff);
	    heightstorage[cc][0] = fi.chararray[cc].h;
	    heightstorage[cc][1] = fi.chararray[cc].voff;
	    if (downloadtype == 'Y')
		wordsneeded += (int)((fi.chararray[cc].h + 15.0) / 16.0) *
		fi.chararray[cc].w;
	    else
		wordsneeded += (int)((fi.chararray[cc].w + 15.0) / 16.0) *
		fi.chararray[cc].h;
	} else
	    switch (command) {
	    case PKXXX1:
	    case PKXXX2:
	    case PKXXX3:
	    case PKXXX4:
		for (li = 0, lk = uinteger(f, command - PKXXX1 + 1); li <
		lk; li++)
		    (void)cgetc(f);
		break;
	    case PKYYY:
		(void)uinteger(f, 4);
		break;
	    case PKNOOP:
		break;
	    default:
		(void)fclose(f);
		return NULL;
	    }
    wordsneeded += 800.0 / 2.0 + (fi.numchars * (14.0 + 12.0)) / 2.0;
    fi.blocksize = CEILING((wordsneeded * 2.0) / 1024.0);
    fi.qmsbaseline = largestvoff;
    for (li = 0; li < 256; li++)
	fi.qmsheight = MAX(fi.qmsheight, largestvoff + (heightstorage[li][0]
	- heightstorage[li][1]));
    (void)fclose(f);
    return &fi;
}

/* This routine takes a PK font name and extracts the information
 * contained in its naming convention.  PK fonts are named like
 * <font name><optional design size>.<magnification>pk.  The first
 * parameter is the font name.  This is the only input parameter.  The
 * following output parameters are the name of the font (including the
 * optional design size), the name of the font (without the design size),
 * the design size and the magnification.  For example, parameter
 * cmr10.200pk would return the values
 *	fontname	"cmr10"
 *	shortfontname	"cmr"
 *	designsize	10
 *	magnification	200
 * The function returns TRUE if it was successful parsing the name, FALSE
 * otherwise.  The designsize returned will be -1 if it is left out.
 */
Boolean extractinfo(fullfontname, fontname, shortfontname, designsize,
magnification)
char	*fullfontname;
char	*fontname;
char	*shortfontname;
int	*designsize;
int	*magnification;
{
    char	*p, *strcpy();

    if (sscanf(fullfontname, "%[^0123456789.]", shortfontname) != 1)
	return FALSE;
    p = &fullfontname[strlen(shortfontname)];
    if ('0' <= *p && *p <= '9') {
	if (sscanf(p, "%d", designsize) != 1)
	    return FALSE;
	while ('0' <= *p && *p <= '9')
	    p++;
    } else
	*designsize = -1;
    if (*designsize != -1)
	(void)sprintf(fontname, "%s%d", shortfontname, *designsize);
    else
	(void)strcpy(fontname, shortfontname);
    if (*p != '.')
	return FALSE;
    else
	p++;
    if (sscanf(p, "%d", magnification) != 1)
	return FALSE;
    while ('0' <= *p && *p <= '9')
	p++;
    if (!EQ(p, "pk"))
	return FALSE;
    return TRUE;
}

static FILE		*PkFilePtr;	/* File pointer used in resetpkfile()
					 * and getnextcharinfo() */
static struct CharInfo	CInfo;		/* Structure returned to the user */

/* Resets the getnextcharinfo() function so it reopens the PK file */
void resetpkfile()
{
    if (PkFilePtr) {
	(void)fclose(PkFilePtr), PkFilePtr = NULL;
	if (CInfo.bitmap)
	    free(CInfo.bitmap), CInfo.bitmap = NULL;
    }
}

/* This function gets information on the next character in a font.  If the
 * font file is not already opened, it will open the font file. The 
 * downloadtype should be 'X' or 'Y'.  The routine allocates and deallocates
 * memory for the character bitmap itself, so you needn't worry about it.
 * The filename parameter is only consulted when the routine is first called
 * to get the information on the first character.  If you wish to read from a 
 * new PK file, you must use the resetpkfile() function.  The routine returns
 * 0 on success, 1 if there are no more characters, 2 if the PK file could not
 * be opened, 3 if the file is not a PK file and 4 if the PK version is 
 * incorrect.  This routine calls the cgetc() function so the error routine
 * should be set appropriately.
 */
int getnextcharinfo(filename, downloadtype, charinfo)
char		*filename;
int		downloadtype;
struct CharInfo **charinfo;
{
    unsigned long	uinteger();
    char	    	*malloc();
    int		    	command, flag, dynf, packednum;
    long                pl, li, lj, integer();
    register Boolean	black;
    register char	*bitmap, *rowmap, *putbyte;
    register int	i, j, k, currentw, currenth, mask;
    register long	w, h;

    if (!PkFilePtr) {
	if (!(PkFilePtr = fopen(filename, "r")))
	    return 2;
	if (cgetc(PkFilePtr) != PKPRE) {
	    (void)fclose(PkFilePtr), PkFilePtr = NULL;
	    return 3;
	}
	if (cgetc(PkFilePtr) != PKID) {
	    (void)fclose(PkFilePtr), PkFilePtr = NULL;
	    return 4;
	}
	for (i = 0, j = cgetc(PkFilePtr); i < j; i++)	/* Comment */
	    (void)cgetc(PkFilePtr);
	(void)integer(PkFilePtr, 4);		/* Design size */
	(void)integer(PkFilePtr, 4);		/* Checksum */
	(void)integer(PkFilePtr, 4);		/* hppp */
	(void)integer(PkFilePtr, 4);		/* vppp */
    } else if (CInfo.bitmap)
	free(CInfo.bitmap), CInfo.bitmap = NULL;
tryagain:
    if ((command = cgetc(PkFilePtr)) == PKPOST) {	/* No more chars */
	(void)fseek(PkFilePtr, -1L, 1);	/* Back up by 1 byte */
	return 1;
    }
    if (0 <= command && command <= 239) {
	flag = command;
	dynf = (flag & DYNF) >> 4;
	if ((flag & 0x7) == 0x7) {	/* Long format preamble */
	    CInfo.preambletype = plong;
	    pl = integer(PkFilePtr, 4);
	    CInfo.cc = integer(PkFilePtr, 4);
	    CInfo.tfm = integer(PkFilePtr, 4);
	    CInfo.dxordm = integer(PkFilePtr, 4);
	    CInfo.dy = integer(PkFilePtr, 4);
	    CInfo.w = integer(PkFilePtr, 4);
	    CInfo.h = integer(PkFilePtr, 4);
	    CInfo.hoff = integer(PkFilePtr, 4);
	    CInfo.voff = integer(PkFilePtr, 4);
	    if (CInfo.h == 0 || CInfo.w == 0)
	    	(void)fseek(PkFilePtr, pl - 28L, 1);
	} else if (0 <= (flag & 0x7) && (flag & 0x7) <= 3) { /* Short */
	    CInfo.preambletype = pshort;
	    pl = cgetc(PkFilePtr) | (flag & 0x3) << 8;
	    CInfo.cc = cgetc(PkFilePtr);
	    CInfo.tfm = uinteger(PkFilePtr, 3);
	    CInfo.dxordm = cgetc(PkFilePtr);
	    CInfo.w = cgetc(PkFilePtr);
	    CInfo.h = cgetc(PkFilePtr);
	    CInfo.hoff = integer(PkFilePtr, 1); /* integer() returns */
	    CInfo.voff = integer(PkFilePtr, 1); /* signed values */
	    if (CInfo.h == 0 || CInfo.w == 0)
	    	(void)fseek(PkFilePtr, pl - 8L, 1);
	} else {			/* Extended short form */
	    CInfo.preambletype = pexshort;
	    pl = uinteger(PkFilePtr, 2) | (flag & 0x3) << 16;
	    CInfo.cc = cgetc(PkFilePtr);
	    CInfo.tfm = uinteger(PkFilePtr, 3);
	    CInfo.dxordm = uinteger(PkFilePtr, 2);
	    CInfo.w = uinteger(PkFilePtr, 2);
	    CInfo.h = uinteger(PkFilePtr, 2);
	    CInfo.hoff = integer(PkFilePtr, 2);
	    CInfo.voff = integer(PkFilePtr, 2);
	    if (CInfo.h == 0 || CInfo.w == 0)
	    	(void)fseek(PkFilePtr, pl - 13L, 1);
	}
	assert(pl > 0);
	assert(CInfo.h >= 0 && CInfo.w >= 0);
	assert(0 <= CInfo.cc && CInfo.cc <= 255);
	*charinfo = &CInfo;
	h = CInfo.h, w = CInfo.w;
	if (h == 0 || w == 0)
	    return 0;
	if (downloadtype == 'X') {
	    /* Shifting right 3 divides by 8 faster than division */
	    bitmap = CInfo.bitmap = malloc((unsigned)((w+7>>3)*h));
	    if (dynf == 14) {
		bzero(bitmap, (int)((w+7>>3)*h));
		for (i = 0; i < h; i++)
		    for (j = 0; j < w; j++)
		    	if (getbit(PkFilePtr))
			    *(bitmap + i*(w+7>>3) + (j>>3)) |= 1<<7-j%8;
	    } else {			/* Get normally packed raster */
		black = flag & BRUNCOUNT;
		rowmap = malloc((unsigned)(w+7>>3));
		bzero(rowmap,(int)(w+7>>3));
		currenth = currentw = RepeatCount = 0;
		while (currenth < h && currentw < w) {
		    packednum = pkpackednum(PkFilePtr, dynf);
		    for (i = 0; i < packednum; i++) {
			if (black)
			    *(rowmap + (currentw>>3)) |= 1<<7-currentw%8;
			if (++currentw == w) {
			    for (j = 0; j < RepeatCount + 1; j++)
			    	bcopy(rowmap, bitmap + currenth++ * (w+7>>3),
				(w+7>>3));
			    bzero(rowmap, (int)(w+7>>3));
			    currentw = RepeatCount = 0;
			}
		    }
		    black = !black;
		}
		free(rowmap);
	    }
	} else {			/* Y orientation */
	    bitmap = CInfo.bitmap = malloc((unsigned)((h+7>>3)*w));
	    bzero(bitmap, (int)((h+7>>3)*w));
	    if (dynf == 14) {			/* Get raster by bits */
		for (i = 0; i < h; i++)
		    for (j = 0; j < w; j++)
		    	if (getbit(PkFilePtr)) {
			    putbyte = bitmap + j*(h+7>>3) + (h-(i+1)>>3);
			    *putbyte |= 1<<7-((h+7)-i)%8;
			}
	    } else {			/* Get normally packed raster */
		black = flag & BRUNCOUNT;
		rowmap = malloc((unsigned)(w+7>>3));
		bzero(rowmap, (int)(w+7>>3));
		currenth = currentw = RepeatCount = 0;
		while (currenth < h && currentw < w) {
		    packednum = pkpackednum(PkFilePtr, dynf);
		    for (i = 0; i < packednum; i++) {
			if (black) {
			    *(rowmap + (currentw>>3)) |= 1<<7-currentw%8;
			    putbyte = bitmap + currentw*(h+7>>3)+
			    (h-(currenth+1)>>3);
			    *putbyte |= 1<<7-((h+7)-currenth)%8;
			}
			if (++currentw == w) {
			    currenth++;
			    for (j = 0; j < RepeatCount; j++) {
				for (k = 0; k < w; k++) {
				    if (k % 8 == 0)
				    	mask = 0x80;
				    else
				    	mask >>= 1;
				    if (*(rowmap + (k>>3)) & mask) {
					putbyte = bitmap + k*(h+7>>3) +
					(h-(currenth+1)>>3);
					*putbyte |= 1<<7-((h+7)-currenth)%8;
				    }
				}
				currenth++;
			    }
			    bzero(rowmap, (int)(w+7>>3));
			    currentw = RepeatCount = 0;
			}
		    }
		    black = !black;
		}
		free(rowmap);
	    }			/* End get normally packed raster */
	}			/* End Y orientation */
	resetbitcount();
    } else	    /* End of if (0 <= command && command <= 239) { */
	switch (command) {
	case PKXXX1:
	case PKXXX2:
	case PKXXX3:
	case PKXXX4:
	    for (li = 0, lj = uinteger(PkFilePtr, command - PKXXX1 + 1);
	    li < lj; li++)
		(void)cgetc(PkFilePtr);
	    goto tryagain;
	case PKYYY:
	    (void)uinteger(PkFilePtr, 4);
	    goto tryagain;
	default:
#ifndef lint
	    assert(FALSE);
#else
	    ;
#endif
	}
    return 0;
}