File: gf.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 (361 lines) | stat: -rw-r--r-- 8,154 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
/*
 * gf.c - TeX GF 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 GF_GLYPH  gf_loader(VF_CACHE,FILE*);
Private int       gf_read_glyph(FILE*,VF_BITMAP);

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


Private GF_GLYPH
GF_CacheLoader(VF_CACHE c, char *font_path, int l)
{
  FILE      *fp;
  UINT1      pre, id;
  UINT4      k;

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

  pre = READ_UINT1(fp);
  if (pre != GF_PRE)
    return NULL;
  id = READ_UINT1(fp);
  if (id != GF_ID)
    return NULL;
  k = READ_UINT1(fp);
  SKIP_N(fp, k);

  return gf_loader(c, fp);
}

Private void
GF_CacheDisposer(GF_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 GF_GLYPH
gf_loader(VF_CACHE c, FILE* fp)
{
  GF_GLYPH    go;
  VF_BITMAP   bm;
  UINT1       instr, d;
  UINT4       ds, check_sum, hppp, vppp;
  INT4        min_m, max_m, min_n, max_n;
  INT4        w;
  UINT4       code;
  double      dx, dy;
  long        ptr_post, ptr_p, ptr, optr, gptr;
  int         bc, ec, nchars, i;

  go = NULL;
  nchars = -1;

  /* seek to post_post instr. */
  fseek(fp, -1, SEEK_END);
  while ((d = READ_UINT1(fp)) == 223)
    fseek(fp, -2, SEEK_CUR);
  if (d != GF_ID){
    vf_error = VF_ERR_ILL_FONT_FILE;
    goto ErrExit;
  }
  fseek(fp, -6, SEEK_CUR);

  /* check if the code is post_post */
  if (READ_UINT1(fp) != GF_POST_POST){
    vf_error = VF_ERR_ILL_FONT_FILE;
    goto ErrExit;
  }

  /* read pointer to post instr. */
  if ((ptr_post = READ_UINT4(fp)) == -1){
    vf_error = VF_ERR_ILL_FONT_FILE;
    goto ErrExit;
  }

  /* goto post instr. and read it */
  fseek(fp, ptr_post, SEEK_SET);
  if (READ_UINT1(fp) != GF_POST){
    vf_error = VF_ERR_ILL_FONT_FILE;
    goto ErrExit;
  }
  ptr_p     = READ_UINT4(fp);
  ds        = READ_UINT4(fp);
  check_sum = READ_UINT4(fp);
  hppp      = READ_UINT4(fp);
  vppp      = READ_UINT4(fp);
  min_m     = READ_INT4(fp);
  max_m     = READ_INT4(fp);
  min_n     = READ_INT4(fp);
  max_n     = READ_INT4(fp);

  gptr = ftell(fp);

#if 0
  /* read min & max char code */
  bc = 256;
  ec = -1;
  for (;;){
    instr = READ_UINT1(fp);
    if (instr == GF_POST_POST){
      break;
    } else if (instr == GF_CHAR_LOC){
      code = READ_UINT1(fp);
      (void)SKIP_N(fp, 16);
    } else if (instr == GF_CHAR_LOC0){
      code = READ_UINT1(fp);
      (void)SKIP_N(fp, 9);
    } else {
      vf_error = VF_ERR_ILL_FONT_FILE;
      goto ErrExit;
    }
    if (code < bc)
      bc = code;
    if (code > ec)
      ec = code;
  }
#else
  bc = 0;
  ec = 255;
#endif

  nchars = ec - bc + 1;
  ALLOC_IF_ERR(go, struct s_gf_glyph){
    vf_error = VF_ERR_NO_MEMORY;
    goto ErrExit;
  }
  ALLOCN_IF_ERR(go->bm_table, struct vf_s_bitmap, nchars){
    vf_error = VF_ERR_NO_MEMORY;
    goto ErrExit;
  }

  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 = max_m - min_m;
  go->font_bbx_h = max_n - min_n;
  go->font_bbx_xoff = min_m;
  go->font_bbx_yoff = min_n;
  go->code_min = bc;
  go->code_max = ec;

  /* read glyph */
#if 0
  fseek(fp, gptr, SEEK_SET);
#endif
  for (;;){
    if ((instr = READ_UINT1(fp)) == GF_POST_POST)
      break;
    switch ((int)instr){
    case GF_CHAR_LOC:
      code = READ_UINT1(fp);
      dx   = (double)READ_INT4(fp)/(double)(1<<16);
      dy   = (double)READ_INT4(fp)/(double)(1<<16);
      w    = READ_INT4(fp);
      ptr  = READ_INT4(fp);
      break;
    case GF_CHAR_LOC0:
      code = READ_UINT1(fp);
      dx   = (double)READ_INT1(fp);
      dy   = (double)0;
      w    = READ_INT4(fp);
      ptr  = READ_INT4(fp);
      break;
    default:
      vf_error = VF_ERR_ILL_FONT_FILE;
      goto ErrExit;
    }
    optr = ftell(fp);
    fseek(fp, ptr, SEEK_SET);
    bm = &go->bm_table[code - bc];
    if (gf_read_glyph(fp, bm) < 0)
      goto ErrExit;
    bm->mv_x = dx;
    bm->mv_y = dy;
    fseek(fp, optr, SEEK_SET);
  }
  return go;
  
ErrExit:
printf("*ERROR\n");
  if (go != NULL){
    if (go->bm_table != NULL){
      for (i = 0; i < nchars; i++)
	vf_free(go->bm_table[i].bitmap);
    }
    vf_free(go->bm_table);
  }
  vf_free(go);
  return NULL;
}

Private int
gf_read_glyph(FILE* fp, VF_BITMAP bm)
{
  long           m, n;
  int            paint_sw;
  int            instr;
  INT4           min_m, max_m, min_n, max_n, del_m, del_n;
  long           w, h, d;
  int            m_b, k;
  unsigned char  *ptr;

  switch (READ_UINT1(fp)){
  case GF_BOC:
    SKIP_N(fp, 4);
    SKIP_N(fp, 4);
    min_m = READ_INT4(fp);
    max_m = READ_INT4(fp);
    min_n = READ_INT4(fp);
    max_n = READ_INT4(fp);
    break;
  case GF_BOC1:
    SKIP_N(fp, 1);
    del_m = (INT4)READ_UINT1(fp);
    max_m = (INT4)READ_UINT1(fp);
    del_n = (INT4)READ_UINT1(fp);
    max_n = (INT4)READ_UINT1(fp);
    min_m = max_m - del_m;
    min_n = max_n - del_n;
    break;
  default:
    return -1;
  }

  w = max_m - min_m + 1;
  h = max_n - min_n + 1;
  if ((w < 0) || (h < 0)){
    vf_error = VF_ERR_ILL_FONT_FILE;
    return -1;
  }

  if ((bm->bitmap = (unsigned char*)malloc(h*((w+7)/8))) == NULL){
    vf_error = VF_ERR_NO_MEMORY;
    return -1;
  }
  memclr(bm->bitmap, h*((w+7)/8));
  bm->raster     = (w+7)/8;
  bm->bbx_width  = w;
  bm->bbx_height = h;
  bm->off_x      = -min_m;
  bm->off_y      = max_n;
#if 0
  bm->mv_x       = -min_m;
  bm->mv_y       = max_n;
#endif

  m        = min_m;
  n        = max_n;
  paint_sw = 0;
  while ((instr = (int)READ_UINT1(fp)) != GF_EOC){
    if (instr == GF_PAINT_0){
      paint_sw = 1 - paint_sw;
    } else if ((GF_NEW_ROW_0 <= instr) && (instr <= GF_NEW_ROW_164)){
      m        = min_m + (instr - GF_NEW_ROW_0);
      n        = n - 1;
      paint_sw = 1;
    } else if ((GF_PAINT_1 <= instr) && (instr <= GF_PAINT_63)){
      d = (instr - GF_PAINT_1 + 1);
      goto Paint;
    } else {
      switch ((int)instr){
      case GF_PAINT1:
      case GF_PAINT2:
      case GF_PAINT3:
	d = (UINT4)READ_UINTN(fp, (instr - GF_PAINT1 + 1));
Paint:
	if (paint_sw == 0){
	  m = m + d;
	} else {
	  ptr = &bm->bitmap[(max_n - n) * bm->raster + (m - min_m)/8];
	  m_b = (m - min_m) % 8;
	  while (d > 0){
	    *ptr |= bit_table[m_b];
	    m++;
	    if (++m_b >= 8){
	      m_b = 0;
	      ++ptr;
	    }
	    d--;
	  }
	}
	paint_sw = 1 - paint_sw;
	break;
      case GF_SKIP0:
	m = min_m;
	n = n - 1;
	paint_sw = 0;
	break;
      case GF_SKIP1:
      case GF_SKIP2:
      case GF_SKIP3:
	m = min_m;
	n = n - (UINT4)READ_UINTN(fp, (instr - GF_SKIP1 + 1)) - 1;
	paint_sw = 0;
	break;
      case GF_XXX1:
      case GF_XXX2:
      case GF_XXX3:
      case GF_XXX4:
	k = READ_UINTN(fp, instr - GF_XXX1 + 1);
	SKIP_N(fp, k);
	break;
      case GF_YYY:
	SKIP_N(fp, 4);
	break;
      case GF_NO_OP:
	break;
      default:
	vf_free(bm->bitmap);
	bm->bitmap = NULL;
	vf_error = VF_ERR_ILL_FONT_FILE;
	return -1;
      }
    }
  }

  return 0;
}


/*EOF*/