2
Copyright (c) 1996-2002 Han The Thanh, <thanh@pdftex.org>
4
This file is part of pdfTeX.
6
pdfTeX is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
11
pdfTeX is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with pdfTeX; if not, write to the Free Software
18
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
$Id: //depot/Build/source.development/TeX/texk/web2c/pdftexdir/pkin.c#4 $
25
* pkin.c - implementation of readchar()
27
* This implementation of readchar() uses parts of the program dvips
28
* written by Tomas Rokicki--the inventor of the pkformat--(loadfont.c,
29
* download.c and unpack.c). Dvips in turn is derived from pktype.
30
* Pktype(TeX) is described in debt in ``The PKtype processor'',
31
* which is available as pktype.weave as part of the METAFONTware.
32
* What was needed to implement readchar() is rearranged in pkfile.c to
33
* get more modularity in the style of MODULA2.
35
* May 1997: Eric Delaunay <delaunay@lix.polytechnique.fr> reports a
36
* problem with huge fonts (greater than 1008 DPI). The code for
37
* handling PK characters in `extended format' was wrongly derived
38
* from dvips. Made some minor improvements regarding error handling.
43
* Modified for use with pdftex by Han The Thanh <thanh@fi.muni.cz>.
49
* Now we have some routines to get stuff from the pk file. pkbyte returns
50
* the next byte from the pk file.
59
#define pkfile t3_file
61
static shalfword pkbyte(void)
65
if ((i = xgetc(pkfile)) == EOF)
66
pdftex_fail("unexpected eof in pk file");
70
static integer pkduo(void)
77
i = i * 256 + pkbyte();
81
static integer pktrio(void)
88
i = i * 256 + pkbyte();
89
i = i * 256 + pkbyte();
93
static integer pkquad(void)
100
i = i * 256 + pkbyte();
101
i = i * 256 + pkbyte();
102
i = i * 256 + pkbyte();
107
* The next part is devoted to unpacking the character data.
111
* We need procedures to get a nybble, bit, and packed word from the
112
* packed data structure.
115
static halfword inputbyte, flagbyte;
116
static halfword bitweight;
117
static halfword dynf;
118
static halfword repeatcount;
120
static shalfword getnyb(void)
123
if (bitweight == 0) {
125
inputbyte = pkbyte();
126
temp = inputbyte >> 4;
129
temp = inputbyte & 15;
134
static boolean getbit(void)
137
if (bitweight == 0) {
138
inputbyte = pkbyte();
141
return (inputbyte & bitweight);
144
static halfword(*realfunc) (void);
146
static halfword handlehuge(halfword i, halfword k);
148
static halfword pkpackednum(void)
150
register halfword i, j;
159
* Damn, we got a huge count! We *fake* it by giving an artificially
160
* large repeat count.
162
return (handlehuge(i, j));
165
j = j * 16 + getnyb();
168
return (j - 15 + (13 - dynf) * 16 + dynf);
170
} else if (i <= dynf)
173
return ((i - dynf - 1) * 16 + getnyb() + dynf + 1);
176
repeatcount = pkpackednum();
180
printf("[%d]", (int) repeatcount);
182
return ((*realfunc) ());
186
static halfword rest(void)
190
if (pk_remainder < 0) {
191
pk_remainder = -pk_remainder;
193
} else if (pk_remainder > 0) {
194
if (pk_remainder > 4000) {
195
pk_remainder = 4000 - pk_remainder;
200
realfunc = pkpackednum;
204
pdftex_fail("shouldn't happen");
209
static halfword handlehuge(halfword i, halfword k)
214
j = (j << 4L) + getnyb();
217
pk_remainder = j - 15 + (13 - dynf) * 16 + dynf;
223
* And now we have our unpacking routine.
226
static halfword gpower[17] = { 0, 1, 3, 7, 15, 31, 63, 127,
227
255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535
230
static void unpack(chardesc * cd)
232
register integer i, j;
233
register halfword word, wordweight;
241
wordwidth = (cd->cwidth + 15) / 16;
242
i = 2 * cd->cheight * (long) wordwidth;
245
if (i > cd->rastersize) {
248
cd->raster = xtalloc(cd->rastersize, halfword);
251
realfunc = pkpackednum;
252
dynf = flagbyte / 16;
253
turnon = flagbyte & 8;
256
for (i = 1; i <= cd->cheight; i++) {
259
for (j = 1; j <= cd->cwidth; j++) {
263
if (wordweight == 0) {
269
if (wordweight != 32768)
273
rowsleft = cd->cheight;
279
while (rowsleft > 0) {
280
count = (*realfunc) ();
283
printf("(%d) ", (int) count);
285
printf("%d ", (int) count);
288
if ((count < wordweight) && (count < hbit)) {
290
word += gpower[wordweight] - gpower[wordweight - count];
294
} else if ((count >= hbit) && (hbit <= wordweight)) {
296
word += gpower[wordweight] - gpower[wordweight - hbit];
298
for (i = 1; i <= repeatcount; i++) {
299
for (j = 1; j <= wordwidth; j++) {
300
*raster = *(raster - wordwidth);
304
rowsleft -= repeatcount + 1;
312
word += gpower[wordweight];
322
if ((rowsleft != 0) || ((integer) hbit != cd->cwidth))
323
pdftex_fail("error while unpacking; more bits than required");
328
* readchar(): the main routine
329
* Reads the character definition of character `c' into `cd' if available,
330
* return FALSE (0) otherwise.
334
* readchar(): the main routine
335
* check pk preamble if necessary,
336
* read the next character definition into `cd',
337
* return EOF if no character definition is available
340
int readchar(boolean check_preamble, chardesc * cd)
342
register shalfword i;
344
register integer length = 0;
347
* Check the preamble of the pkfile
349
if (check_preamble) {
351
pdftex_fail("bad pk file, expected pre");
353
pdftex_fail("bad version of pk file");
354
for (i = pkbyte(); i > 0; i--) /* creator of pkfile */
356
(void) pkquad(); /* design size */
357
k = pkquad(); /* checksum */
358
k = pkquad(); /* hppp */
359
k = pkquad(); /* vppp */
362
* Now we skip to the desired character definition
364
while ((flagbyte = pkbyte()) != 245) {
365
if (flagbyte < 240) {
366
switch (flagbyte & 7) {
371
length = (flagbyte & 7) * 256 + pkbyte() - 3;
372
cd->charcode = pkbyte();
373
(void) pktrio(); /* TFMwidth */
374
cd->xescape = pkbyte(); /* pixel width */
375
cd->cwidth = pkbyte();
376
cd->cheight = pkbyte();
387
length = (flagbyte & 3) * 65536L + pkbyte() * 256L;
388
length = length + pkbyte() - 4L;
389
cd->charcode = pkbyte();
390
(void) pktrio(); /* TFMwidth */
391
cd->xescape = pkduo(); /* pixelwidth */
392
cd->cwidth = pkduo();
393
cd->cheight = pkduo();
398
length = pkquad() - 9L;
399
cd->charcode = pkquad();
400
(void) pkquad(); /* TFMwidth */
401
cd->xescape = pkquad(); /* pixelwidth */
403
cd->cwidth = pkquad();
404
cd->cheight = pkquad();
409
pdftex_fail("packet length (%i) too small", (int) length);
420
k = k * 256 + pkbyte();
422
k = k * 256 + pkbyte();
424
k = k * 256 + pkbyte();
434
pdftex_fail("unexpected command (%i)", (int) flagbyte);
438
return 0; /* character not found */