Debian Codegraph @

texk/web2c/pdftexdir/pkin.c

raw
1 /*
2 Copyright 1996-2014 Han The Thanh, <[email protected]>
3
4 This file is part of pdfTeX.
5
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.
10
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.
15
16 You should have received a copy of the GNU General Public License along
17 with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*
21 * NAME
22 * pkin.c - implementation of readchar()
23 * DESCRIPTION
24 * This implementation of readchar() uses parts of the program dvips
25 * written by Tomas Rokicki--the inventor of the pkformat--(loadfont.c,
26 * download.c and unpack.c). Dvips in turn is derived from pktype.
27 * Pktype(TeX) is described in debt in ``The PKtype processor'',
28 * which is available as pktype.weave as part of the METAFONTware.
29 * What was needed to implement readchar() is rearranged in pkfile.c to
30 * get more modularity in the style of MODULA2.
31 * BUGFIXES
32 * May 1997: Eric Delaunay <[email protected]> reports a
33 * problem with huge fonts (greater than 1008 DPI). The code for
34 * handling PK characters in `extended format' was wrongly derived
35 * from dvips. Made some minor improvements regarding error handling.
36 * REDESIGN
37 * Piet Tutelaers
38 * [email protected]
39 *
40 * Modified for use with pdftex by Han The Thanh <[email protected]>.
41 */
42
43 #include "ptexlib.h"
44
45 /*
46 * Now we have some routines to get stuff from the pk file. pkbyte returns
47 * the next byte from the pk file.
48 */
49
50
51 /*
52 static FILE *pkfile ;
53 */
54 #define pkfile t3_file
55
56 static shalfword pkbyte(void)
57 {
58 register shalfword i;
59
60 if ((i = xgetc(pkfile)) == EOF)
61 pdftex_fail("unexpected eof in pk file");
62 return (i);
63 }
64
65 static integer pkduo(void)
66 {
67 register integer i;
68
69 i = pkbyte();
70 if (i > 127)
71 i -= 256;
72 i = i * 256 + pkbyte();
73 return (i);
74 }
75
76 static integer pktrio(void)
77 {
78 register integer i;
79
80 i = pkbyte();
81 if (i > 127)
82 i -= 256;
83 i = i * 256 + pkbyte();
84 i = i * 256 + pkbyte();
85 return (i);
86 }
87
88 static integer pkquad(void)
89 {
90 register integer i;
91
92 i = pkbyte();
93 if (i > 127)
94 i -= 256;
95 i = i * 256 + pkbyte();
96 i = i * 256 + pkbyte();
97 i = i * 256 + pkbyte();
98 return (i);
99 }
100
101 /*
102 * The next part is devoted to unpacking the character data.
103 */
104
105 /*
106 * We need procedures to get a nybble, bit, and packed word from the
107 * packed data structure.
108 */
109
110 static halfword inputbyte, flagbyte;
111 static halfword bitweight;
112 static halfword dynf;
113 static halfword repeatcount;
114
115 static shalfword getnyb(void)
116 {
117 halfword temp;
118 if (bitweight == 0) {
119 bitweight = 16;
120 inputbyte = pkbyte();
121 temp = inputbyte >> 4;
122 } else {
123 bitweight = 0;
124 temp = inputbyte & 15;
125 }
126 return (temp);
127 }
128
129 static boolean getbit(void)
130 {
131 bitweight >>= 1;
132 if (bitweight == 0) {
133 inputbyte = pkbyte();
134 bitweight = 128;
135 }
136 return (inputbyte & bitweight);
137 }
138
139 static halfword(*realfunc) (void);
140 long pk_remainder;
141 static halfword handlehuge(halfword i, halfword k);
142
143 static halfword pkpackednum(void)
144 {
145 register halfword i, j;
146 i = getnyb();
147 if (i == 0) {
148 do {
149 j = getnyb();
150 i++;
151 } while (!(j != 0));
152 if (i > 3) {
153 /*
154 * Damn, we got a huge count! We *fake* it by giving an artificially
155 * large repeat count.
156 */
157 return (handlehuge(i, j));
158 } else {
159 while (i > 0) {
160 j = j * 16 + getnyb();
161 i--;
162 }
163 return (j - 15 + (13 - dynf) * 16 + dynf);
164 }
165 } else if (i <= dynf)
166 return (i);
167 else if (i < 14)
168 return ((i - dynf - 1) * 16 + getnyb() + dynf + 1);
169 else {
170 if (i == 14)
171 repeatcount = pkpackednum();
172 else
173 repeatcount = 1;
174 return ((*realfunc) ());
175 }
176 }
177
178 static halfword rest(void)
179 {
180 halfword i;
181
182 if (pk_remainder < 0) {
183 pk_remainder = -pk_remainder;
184 return (0);
185 } else if (pk_remainder > 0) {
186 if (pk_remainder > 4000) {
187 pk_remainder = 4000 - pk_remainder;
188 return (4000);
189 } else {
190 i = pk_remainder;
191 pk_remainder = 0;
192 realfunc = pkpackednum;
193 return (i);
194 }
195 } else {
196 pdftex_fail("shouldn't happen");
197 return 0;
198 /*NOTREACHED*/}
199 }
200
201 static halfword handlehuge(halfword i, halfword k)
202 {
203 register long j = k;
204
205 while (i) {
206 j = (j << 4L) + getnyb();
207 i--;
208 }
209 pk_remainder = j - 15 + (13 - dynf) * 16 + dynf;
210 realfunc = rest;
211 return (rest());
212 }
213
214 /*
215 * And now we have our unpacking routine.
216 */
217
218 static halfword gpower[17] = { 0, 1, 3, 7, 15, 31, 63, 127,
219 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535
220 };
221
222 static void unpack(chardesc * cd)
223 {
224 register integer i, j;
225 register halfword word, wordweight;
226 halfword *raster;
227 shalfword rowsleft;
228 boolean turnon;
229 shalfword hbit;
230 halfword count;
231 shalfword wordwidth;
232
233 wordwidth = (cd->cwidth + 15) / 16;
234 i = 2 * cd->cheight * (long) wordwidth;
235 if (i <= 0)
236 i = 2;
237 if (i > cd->rastersize) {
238 xfree(cd->raster);
239 cd->rastersize = i;
240 cd->raster = xtalloc(cd->rastersize, halfword);
241 }
242 raster = cd->raster;
243 realfunc = pkpackednum;
244 dynf = flagbyte / 16;
245 turnon = flagbyte & 8;
246 if (dynf == 14) {
247 bitweight = 0;
248 for (i = 1; i <= cd->cheight; i++) {
249 word = 0;
250 wordweight = 32768;
251 for (j = 1; j <= cd->cwidth; j++) {
252 if (getbit())
253 word += wordweight;
254 wordweight >>= 1;
255 if (wordweight == 0) {
256 *raster++ = word;
257 word = 0;
258 wordweight = 32768;
259 }
260 }
261 if (wordweight != 32768)
262 *raster++ = word;
263 }
264 } else {
265 rowsleft = cd->cheight;
266 hbit = cd->cwidth;
267 repeatcount = 0;
268 wordweight = 16;
269 word = 0;
270 bitweight = 0;
271 while (rowsleft > 0) {
272 count = (*realfunc) ();
273 while (count != 0) {
274 if ((count < wordweight) && (count < hbit)) {
275 if (turnon)
276 word += gpower[wordweight] - gpower[wordweight - count];
277 hbit -= count;
278 wordweight -= count;
279 count = 0;
280 } else if ((count >= hbit) && (hbit <= wordweight)) {
281 if (turnon)
282 word += gpower[wordweight] - gpower[wordweight - hbit];
283 *raster++ = word;
284 for (i = 1; i <= repeatcount; i++) {
285 for (j = 1; j <= wordwidth; j++) {
286 *raster = *(raster - wordwidth);
287 raster++;
288 }
289 }
290 rowsleft -= repeatcount + 1;
291 repeatcount = 0;
292 word = 0;
293 wordweight = 16;
294 count -= hbit;
295 hbit = cd->cwidth;
296 } else {
297 if (turnon)
298 word += gpower[wordweight];
299 *raster++ = word;
300 word = 0;
301 count -= wordweight;
302 hbit -= wordweight;
303 wordweight = 16;
304 }
305 }
306 turnon = !turnon;
307 }
308 if ((rowsleft != 0) || ((integer) hbit != cd->cwidth))
309 pdftex_fail("error while unpacking; more bits than required");
310 }
311 }
312
313 /*
314 * readchar(): the main routine
315 * Reads the character definition of character `c' into `cd' if available,
316 * return FALSE (0) otherwise.
317 */
318
319 /*
320 * readchar(): the main routine
321 * check pk preamble if necessary,
322 * read the next character definition into `cd',
323 * return EOF if no character definition is available
324 */
325
326 int readchar(boolean check_preamble, chardesc * cd)
327 {
328 register shalfword i;
329 register integer k;
330 register integer length = 0;
331
332 /*
333 * Check the preamble of the pkfile
334 */
335 if (check_preamble) {
336 if (pkbyte() != 247)
337 pdftex_fail("bad pk file, expected pre");
338 if (pkbyte() != 89)
339 pdftex_fail("bad version of pk file");
340 for (i = pkbyte(); i > 0; i--) /* creator of pkfile */
341 (void) pkbyte();
342 (void) pkquad(); /* design size */
343 k = pkquad(); /* checksum */
344 k = pkquad(); /* hppp */
345 k = pkquad(); /* vppp */
346 }
347 /*
348 * Now we skip to the desired character definition
349 */
350 while ((flagbyte = pkbyte()) != 245) {
351 if (flagbyte < 240) {
352 switch (flagbyte & 7) {
353 case 0:
354 case 1:
355 case 2:
356 case 3:
357 length = (flagbyte & 7) * 256 + pkbyte() - 3;
358 cd->charcode = pkbyte();
359 (void) pktrio(); /* TFMwidth */
360 cd->xescape = pkbyte(); /* pixel width */
361 cd->cwidth = pkbyte();
362 cd->cheight = pkbyte();
363 cd->xoff = pkbyte();
364 cd->yoff = pkbyte();
365 if (cd->xoff > 127)
366 cd->xoff -= 256;
367 if (cd->yoff > 127)
368 cd->yoff -= 256;
369 break;
370 case 4:
371 case 5:
372 case 6:
373 length = (flagbyte & 3) * 65536L + pkbyte() * 256L;
374 length = length + pkbyte() - 4L;
375 cd->charcode = pkbyte();
376 (void) pktrio(); /* TFMwidth */
377 cd->xescape = pkduo(); /* pixelwidth */
378 cd->cwidth = pkduo();
379 cd->cheight = pkduo();
380 cd->xoff = pkduo();
381 cd->yoff = pkduo();
382 break;
383 case 7:
384 length = pkquad() - 9L;
385 cd->charcode = pkquad();
386 (void) pkquad(); /* TFMwidth */
387 cd->xescape = pkquad(); /* pixelwidth */
388 k = pkquad();
389 cd->cwidth = pkquad();
390 cd->cheight = pkquad();
391 cd->xoff = pkquad();
392 cd->yoff = pkquad();
393 }
394 if (length <= 0)
395 pdftex_fail("packet length (%i) too small", (int) length);
396 unpack(cd);
397 return 1;
398 } else {
399 k = 0;
400 switch (flagbyte) {
401 case 243:
402 k = pkbyte();
403 if (k > 127)
404 k -= 256;
405 case 242:
406 k = k * 256 + pkbyte();
407 case 241:
408 k = k * 256 + pkbyte();
409 case 240:
410 k = k * 256 + pkbyte();
411 while (k-- > 0)
412 i = pkbyte();
413 break;
414 case 244:
415 k = pkquad();
416 break;
417 case 246:
418 break;
419 default:
420 pdftex_fail("unexpected command (%i)", (int) flagbyte);
421 }
422 }
423 }
424 return 0; /* character not found */
425 }