Debian Codegraph @

texk/web2c/luatexdir/font/pkin.c

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