File: pk.c

package info (click to toggle)
vflib3 3.6.14.dfsg-3+nmu3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 11,536 kB
  • ctags: 3,710
  • sloc: ansic: 35,811; sh: 10,357; asm: 3,290; makefile: 962; lisp: 123; perl: 109; awk: 43
file content (407 lines) | stat: -rw-r--r-- 10,534 bytes parent folder | download | duplicates (9)
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
/*
 * pk.c - TeX PK format font fonts loader.
 *
 * 28 Sep 1996  First version.
 * 30 Jan 1998  VFlib 3.4  Changed API.
 * 16 Sep 1999  Changed not to use TFM
 */
/*
 * Copyright (C) 1996-1999  Hirotsugu Kakugawa. 
 * All rights reserved.
 *
 * This file is part of the VFlib Library.  This library is free
 * software; you can redistribute it and/or modify it under the terms of
 * the GNU Library General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  This library 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 Library General Public License for more details.
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */


Private PK_GLYPH  pk_loader(VF_CACHE,FILE*);
Private int       pk_read_14(FILE*,int,int,UINT4,VF_BITMAP,long);
Private int       pk_read_n14(FILE*,int,int,UINT4,VF_BITMAP,long);
Private long      pk_read_packed_number(long*,FILE*,int);
Private void      pk_read_nyble_init(int);
Private int       pk_read_nyble(FILE*);

Private unsigned char   bit_table[] = {
  0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };



Private PK_GLYPH
PK_CacheLoader(VF_CACHE c, char *font_path, int l)
{
  FILE      *fp;
  UINT1      pre, id;

  if (font_path == NULL)
    return NULL;
  if ((fp = vf_fm_OpenBinaryFileStream(font_path)) == NULL)
    return NULL;

  pre = READ_UINT1(fp);
  if (pre != PK_PRE)
    return NULL;

  id = READ_UINT1(fp);
  if (id != PK_ID)
    return NULL;

  return pk_loader(c, fp);
}

Private void
PK_CacheDisposer(PK_GLYPH go)
{
  int   nc, i;

  if (go != NULL){
    if (go->bm_table != NULL){
      nc = go->code_max - go->code_min + 1;
      for (i = 0; i < nc; i++)
	vf_free(go->bm_table[i].bitmap);
      vf_free(go->bm_table);
    }
    vf_free(go);
  }
}


Private PK_GLYPH
pk_loader(VF_CACHE c, FILE* fp)
{
  PK_GLYPH      go;
  UINT1         instr;
  UINT4         ds, check_sum, hppp, vppp, k;
  unsigned int  flag, dny_f, bw, ess, size;
  UINT4         cc, tfm, dx, dy, dm, w, h, rs; 
  INT4          hoff, voff, mv_x, mv_y;
  long          gptr;
  int           bc, ec, nchars, index, i;

  k         = READ_UINT1(fp);
  SKIP_N(fp, k);
  ds        = READ_UINT4(fp);
  check_sum = READ_UINT4(fp);
  hppp      = READ_UINT4(fp);
  vppp      = READ_UINT4(fp);

  gptr = ftell(fp);

#if 0
  /* read min & max char code */
  bc = 256;
  ec = -1;
  for (;;){
    instr = READ_UINT1(fp);
    if (instr == PK_POST)
      break; 
    switch ((int) instr){
    case PK_XXX1:  k = (UINT4)READ_UINT1(fp); SKIP_N(fp, k); break;
    case PK_XXX2:  k = (UINT4)READ_UINT2(fp); SKIP_N(fp, k); break;
    case PK_XXX3:  k = (UINT4)READ_UINT3(fp); SKIP_N(fp, k); break;
    case PK_XXX4:  k = (UINT4)READ_UINT4(fp); SKIP_N(fp, k); break;
    case PK_YYY:   SKIP_N(fp, 4); break;
    case PK_NO_OP: break;
    default:
      size  = instr & 0x3; instr >>= 2;
      ess   = instr & 0x1; 
      if (ess == 0){                          /* short */
	rs = (UINT4)(size*256) + (UINT4)READ_UINT1(fp);
	cc   = (UINT4)READ_UINT1(fp);  
      } else if ((ess == 1) && (size != 3)){  /* extended short */
	rs = (UINT4)(size*65536) + (UINT4)READ_UINT2(fp);
	cc   = (UINT4)READ_UINT1(fp);  
      } else {                                /* standard */
	rs   = READ_UINT4(fp);
	cc   = (UINT4)READ_UINT4(fp);  
      }
      SKIP_N(fp, rs);
      if (cc < bc)
	bc = cc;
      if (cc > ec)
	ec = cc;
      break;
    }
  }

#else
  bc = 0;
  ec = 255;
#endif

  nchars = ec - bc + 1;
  ALLOC_IF_ERR(go, struct s_pk_glyph)
    return NULL;
  ALLOCN_IF_ERR(go->bm_table, struct vf_s_bitmap, nchars){
    vf_free(go);
    return NULL;
  }
  for (i = 0; i < nchars; i++)
    go->bm_table[i].bitmap = NULL;
  go->ds   = (double)ds/(1<<20);
  go->hppp = (double)hppp/(1<<16);
  go->vppp = (double)vppp/(1<<16);
  go->font_bbx_w = 0;
  go->font_bbx_h = 0;
  go->font_bbx_xoff = 0;
  go->font_bbx_yoff = 0;
  go->code_min = bc;
  go->code_max = ec;

  /* read glyphs */
  fseek(fp, gptr, SEEK_SET);
  for (;;){
    if ((instr = READ_UINT1(fp)) == PK_POST)
      break;
    switch ((int)instr){
    case PK_XXX1:  k = (UINT4)READ_UINT1(fp); SKIP_N(fp, k); break;
    case PK_XXX2:  k = (UINT4)READ_UINT2(fp); SKIP_N(fp, k); break;
    case PK_XXX3:  k = (UINT4)READ_UINT3(fp); SKIP_N(fp, k); break;
    case PK_XXX4:  k = (UINT4)READ_UINT4(fp); SKIP_N(fp, k); break;
    case PK_YYY:   SKIP_N(fp, 4); break;
    case PK_NO_OP: break;
    default:
      flag = instr;
      size  = flag % 0x04;  flag = flag >> 2;
      ess   = flag % 0x02;  flag = flag >> 1;
      bw    = flag % 0x02;  flag = flag >> 1;
      dny_f = flag % 0x10;
      if (ess == 0){                          /* short */
	rs = (UINT4)(size*256) + (UINT4)READ_UINT1(fp) - (UINT4)8; 
	cc   = (UINT4)READ_UINT1(fp);   tfm  = (UINT4)READ_UINT3(fp);
	dm   = (UINT4)READ_UINT1(fp);
	w    = (UINT4)READ_UINT1(fp);   h    = (UINT4)READ_UINT1(fp);
	hoff = (INT4)READ_INT1(fp);     voff = (INT4)READ_INT1(fp);
	mv_x = dm;
	mv_y = 0;
      } else if ((ess == 1) && (size != 3)){  /* extended short */
	rs = (UINT4)(size*65536) + (UINT4)READ_UINT2(fp) - (UINT4)13; 
	cc   = (UINT4)READ_UINT1(fp);   tfm  = (UINT4)READ_UINT3(fp);
	dm   = (UINT4)READ_UINT2(fp);
	w    = (UINT4)READ_UINT2(fp);   h    = (UINT4)READ_UINT2(fp);
	hoff = (INT4)READ_INT2(fp);     voff = (INT4)READ_INT2(fp);
	mv_x = dm;
	mv_y = 0;
      } else {                                /* standard */
	rs   = READ_UINT4(fp) - (UINT4)28;
	cc   = READ_UINT4(fp);          tfm  = READ_UINT4(fp);
	dx   = READ_UINT4(fp);	        dy   = READ_UINT4(fp);
	w    = READ_UINT4(fp);	        h    = READ_UINT4(fp);
	hoff = READ_INT4(fp);	        voff = READ_INT4(fp);
	mv_x = (double)dx/(double)(1<<16);
	mv_y = (double)dy/(double)(1<<16);
      }
      if ((cc < go->code_min) || (go->code_max < cc)){
	vf_error = VF_ERR_ILL_CODE_POINT;
	goto Error;
      }
      index = cc - go->code_min;
      go->bm_table[index].bbx_width  = w;
      go->bm_table[index].bbx_height = h;
      go->bm_table[index].raster = (w+7)/8;
      go->bm_table[index].off_x  = -hoff;
      go->bm_table[index].off_y  = voff;
      go->bm_table[index].mv_x   = mv_x;
      go->bm_table[index].mv_y   = mv_y;
      go->bm_table[index].bitmap = (unsigned char*)malloc(h*((w+7)/8));
      if (go->bm_table[index].bitmap == NULL){
	vf_error = VF_ERR_NO_MEMORY;
	goto Error;
      }
      memclr(go->bm_table[index].bitmap, h*((w+7)/8));
      if (dny_f == 14){
	if (pk_read_14(fp, dny_f, bw, rs, &(go->bm_table[index]), cc) < 0){
	  vf_error = VF_ERR_ILL_FONT_FILE;
	  goto Error;
	}
      } else {
	if (pk_read_n14(fp, dny_f, bw, rs, &(go->bm_table[index]), cc) < 0){
	  vf_error = VF_ERR_ILL_FONT_FILE;
	  goto Error;
	}
      }
      if (go->font_bbx_w < w)
	go->font_bbx_w = w;
      if (go->font_bbx_h < h)
	go->font_bbx_h = h;
      if (go->font_bbx_xoff > -hoff)
	go->font_bbx_xoff = -hoff;
      if (go->font_bbx_yoff > (voff - h))
	go->font_bbx_yoff = (voff - h);
    }
  }
  return go;

Error:
  for (i = 0; i < nchars; i++){
    if (go->bm_table[i].bitmap != NULL)
      vf_free(go->bm_table[i].bitmap);
  }
  vf_free(go->bm_table);
  vf_free(go);
  return NULL;
}

Private int
pk_read_14(FILE* fp, int dyn_f, int bw, UINT4 rs, VF_BITMAP bm, long cc)
{
  long           x, y, x8, xm;
  unsigned char  *bm_ptr;
  unsigned long  bit16_buff;
  int            rest_bit16_buff;
  static unsigned int mask_table[] = 
    { 0xdead,   0x80,   0xc0,   0xe0,   0xf0,   0xf8,   0xfc,   0xfe, 0xdead };
  
  if (rs == 0)
    return 0;

  x8 = bm->bbx_width / 8;
  xm = bm->bbx_width % 8;
  bm_ptr = bm->bitmap;

  bit16_buff = READ_UINT1(fp) << 8; 
  rest_bit16_buff = 8;
  --rs;

  for(y = 0; y < bm->bbx_height; y++){
    for(x = 0; x < x8; x++){
      *(bm_ptr++) = bit16_buff >> 8;
      rest_bit16_buff -= 8;
      bit16_buff = (bit16_buff << 8) & 0xffff;
      if (rs > 0){
	bit16_buff |= (READ_UINT1(fp) << (8 - rest_bit16_buff));
	rest_bit16_buff += 8;
	--rs;
      }
    }
    if (xm != 0){
      *(bm_ptr++) = (bit16_buff >> 8) & mask_table[xm];
      rest_bit16_buff -= xm;
      bit16_buff = (bit16_buff << xm) & 0xffff;
      if (rest_bit16_buff < 8){
	if (rs > 0){
	  bit16_buff |= (READ_UINT1(fp) << (8 - rest_bit16_buff));
	  rest_bit16_buff += 8;
	  --rs;
	}
      }
    }
  }

  return 0;
}

Private int
pk_read_n14(FILE* fp, int dyn_f, int bw, UINT4 rs, VF_BITMAP bm, long cc)
{
  long           x, y, xx, yy, repeat;
  int            bits, b_p;
  unsigned char  *p, *p0, *p1;

  pk_read_nyble_init(rs);
  p    = bm->bitmap;
  bw   = 1-bw;
  bits = 0;
  for (y = 0; y < bm->bbx_height; ){
    b_p    = 0;
    repeat = 0;
    p0     = p;
    for (x = 0; x < bm->bbx_width; x++){
      if (bits == 0){
	bw   = 1-bw;
	if ((bits = pk_read_packed_number(&repeat, fp, dyn_f)) < 0)
	  return -1;
      }
      if (bw == 1)
	*p = *p | bit_table[b_p];
      --bits;
      if (++b_p >= 8){
	b_p = 0;
	p++;
      }
    }
    if (b_p != 0)
      p++;
    y++;
    for (yy = 0; yy < repeat; yy++){
      p1 = p0;
      for (xx = 0; xx < bm->raster; xx++)
	*(p++) = *(p1++);
      y++;
    }
  }
  return 0;
}

Private long
pk_read_packed_number(long* repeat, FILE* fp, int dyn_f)
{
  int   d, n;
  long  di;
  
entry:
  d = pk_read_nyble(fp);
  if (d == 0){
    n = 0; 
    do {
      di = pk_read_nyble(fp);
      n++;
    } while (di == 0);
    for ( ; n > 0; n--)
      di = di*16 + pk_read_nyble(fp); 
    return di - 15 + (13 - dyn_f)*16 + dyn_f;
  } 
  if (d <= dyn_f)
    return d;
  if (d <= 13)
    return (d - dyn_f - 1)*16 + pk_read_nyble(fp) + dyn_f + 1;
  *repeat = 1;
  if (d == 14)
    *repeat = pk_read_packed_number(repeat, fp, dyn_f);
  goto entry;
}

Private int  pk_read_nyble_rest_cnt; 
Private int  pk_read_nyble_max_bytes; 

Private void
pk_read_nyble_init(int max)
{
  pk_read_nyble_rest_cnt  = 0;
  pk_read_nyble_max_bytes = max;
}

Private int
pk_read_nyble(FILE* fp)
{
  static UINT1  d; 
  int           v;

  switch (pk_read_nyble_rest_cnt){
  case 0:
    d = READ_UINT1(fp);
    if (--pk_read_nyble_max_bytes < 0)
      return -1L;
    v = d / 0x10;
    d = d % 0x10;
    pk_read_nyble_rest_cnt = 1;    
    break;
  case 1:
  default:
    v = d;
    pk_read_nyble_rest_cnt = 0;
    break;
  }

  return v;
}

/*EOF*/