File: zeit.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 (472 lines) | stat: -rw-r--r-- 11,663 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * zeit.c - 'Syotai Kurabu' font interface
 * by Hirotsugu Kakugawa
 *
 *  3 Dec 1996  First version.
 * 10 Dec 1996  Changed for VFlib version 3.1
 * 12 Dec 1996  Eliminated "do" capability.
 * 26 Feb 1997  Added 'query_font_type'.
 *  7 Aug 1997  VFlib 3.3  Changed API.
 * 27 Jan 1998  VFlib 3.4  Changed API.
 * 18 Oct 2001    Fixed memory leaks.
 *
 */
/*
 * Copyright (C) 1993-2001 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.
 */

#include  "zeit.h"

static  VF_TABLE  zeit_table    = NULL;



Private int
ZEIT_Init(void)
{
  ZEIT_GetZEIT(-1);

  if ((zeit_table = vf_table_create()) == NULL){
    vf_error = VF_ERR_NO_MEMORY;
    return -1;
  }

  return 0;
}


Private void      zeit_release(ZEIT zeit);
Private int       zeit_make_header(char*,int,char**,long**,long**,int);


Private int
ZEIT_Open(char *font_name)
{
  ZEIT  zeit;
  int   zeit_id;

  if ((zeit_id = (zeit_table->get_id_by_key)(zeit_table, font_name, 
					     strlen(font_name)+1)) >= 0){
    (zeit_table->link_by_id)(zeit_table, zeit_id);
    return zeit_id;
  }

  ALLOC_IF_ERR(zeit, struct s_zeit){
    vf_error = VF_ERR_NO_MEMORY;
    return -1;
  }
  zeit->path_name1 = NULL;
  zeit->ol_size1   = NULL;
  zeit->ol_offset1 = NULL;
  zeit->path_name2 = NULL;
  zeit->ol_size2   = NULL;
  zeit->ol_offset2 = NULL;

  if (zeit_make_header(font_name, 1, &zeit->path_name1,
		       &zeit->ol_size1, &zeit->ol_offset1, 1) < 0)
    goto Error;
  if (zeit_make_header(font_name, 2, &zeit->path_name2,
		       &zeit->ol_size2, &zeit->ol_offset2, 2) < 0)
    goto Error;
  
  if ((zeit_id = (zeit_table->put)(zeit_table, zeit, 
				   font_name, strlen(font_name)+1)) < 0){
    vf_error = VF_ERR_NO_MEMORY;
    goto Error;
  }

  ZEIT_SetZEIT(zeit_id, zeit);

  return zeit_id;

Error:
  zeit_release(zeit);
  return -1;
}


Private void
ZEIT_Close(int zeit_id)
{
  ZEIT  zeit;

  if ((zeit = ZEIT_GetZEIT(zeit_id)) == NULL){
    fprintf(stderr, "VFlib internal error: ZEIT_Close()\n");
    vf_error = VF_ERR_INTERNAL;
    return;
  }
  if ((zeit_table->unlink_by_id)(zeit_table, zeit_id) > 0)
    return;
  zeit_release(zeit);
}


Private void
zeit_release(ZEIT zeit)
{
  if (zeit != NULL){
    vf_free(zeit->path_name1);
    vf_free(zeit->path_name2);
    vf_free(zeit->ol_offset1);
    vf_free(zeit->ol_offset2);
    vf_free(zeit->ol_size1);
    vf_free(zeit->ol_size2);
    vf_free(zeit);
  }
}


Private int   zeit_read_header(long**,long**,char*);

Private int            zeit_correct_size(long*,long*,FILE*,int);
Private int            zeit_code2c(int);
Private int            zeit_read_1byte(FILE*);
Private unsigned long  zeit_read_4bytes(FILE*);
Private void           zeit_init_bit_stream(FILE*);
Private int            zeit_read_10bits(void);
Private void           zeit_seek(FILE*,long);
Private long           zeit_current_pos(FILE*);


Private int
zeit_make_header(char *font_name, int ext, char **path_namep,
		 long **ol_sizep, long **ol_offsetp, int type)
{
  char    *e;
  char    fname[MAXPATHLEN];
  int     i;
  SEXP    s;

  *path_namep = NULL;
  *ol_offsetp = NULL;
  *ol_sizep   = NULL;

  for (s = default_extensions; vf_sexp_consp(s); s = vf_sexp_cdr(s)){
    e = vf_sexp_get_cstring(vf_sexp_car(s));
    sprintf(fname, "%s%s%d", font_name, e, ext);
    if ((*path_namep
	 = vf_search_file(fname, -1, NULL, FALSE, -1,
			  default_fontdirs, NULL, NULL)) != NULL)
      break;
  }

  if (*path_namep == NULL){
    if (type == 1){
      vf_error = VF_ERR_NO_FONT_FILE;
      return -1;
    }
    return 0;
  }

  if (debug_on('f'))
    printf("VFlib ZEIT: font path: %s\n", *path_namep);

  ALLOCN_IF_ERR(*ol_offsetp, long, ZEIT_NCHARS){
    vf_error = VF_ERR_NO_MEMORY;
    goto Error;
  }
  ALLOCN_IF_ERR(*ol_sizep, long, ZEIT_NCHARS){
    vf_error = VF_ERR_NO_MEMORY;
    goto Error;
  }

  for (i = 0; i < ZEIT_NCHARS; i++){
    (*ol_offsetp)[i] = -1;
    (*ol_sizep)[i]   = 0;
  }

  if (zeit_read_header(ol_sizep, ol_offsetp, *path_namep) < 0){
    goto Error;
  }

  return 0;

Error:
  vf_free(*path_namep);  *path_namep = NULL;
  vf_free(*ol_offsetp);  *ol_offsetp = NULL;
  vf_free(*ol_sizep);    *ol_sizep   = NULL;
  return -1;
}


Private int 
zeit_read_header(long **ol_sizep, long **ol_offsetp, char *font_path)
{
  int    i, j;
  FILE   *fp;

  if ((fp = vf_fm_OpenBinaryFileStream(font_path)) == NULL){
    fprintf(stderr, "VFlib Error. File not found: %s\n", font_path);
    return -1;
  }

  (void) zeit_read_1byte(fp);
  (void) zeit_read_1byte(fp);

  for (i = 0; i < ZEIT_NCHARS; i++){
    (*ol_offsetp)[i] = zeit_read_4bytes(fp);
    if (debug_on('i'))
      printf("VFlib ZEIT: Header  %04d: %08lx\n", i, (*ol_offsetp)[i]);
  }

  for (i = 0; i < ZEIT_NCHARS-1; i++){
    if ((*ol_offsetp)[i] == 0xffffffff){
      (*ol_sizep)[i] = 0;
      continue;
    }
    for (j = i+1; ; j++){
      if (j >= ZEIT_NCHARS){
	(*ol_sizep)[i] = -(THRESHOLD_SIZE+1);
	break;
      }
      if ((*ol_offsetp)[j] != 0xffffffff){
	(*ol_sizep)[i] = -((*ol_offsetp)[j] - (*ol_offsetp)[i]); /* A MAGIC */
	break;
      }
    }
    if (-((*ol_sizep)[i]) > THRESHOLD_SIZE)  /* Large... check size. */
      zeit_correct_size(&((*ol_sizep)[i]), &((*ol_offsetp)[i]), fp, i); 
  }
  if ((*ol_offsetp)[ZEIT_NCHARS-1] == 0xffffffff){
    (*ol_sizep)[ZEIT_NCHARS-1] = 0;
  } else {
    (*ol_sizep)[ZEIT_NCHARS-1] = -(THRESHOLD_SIZE+1);           /* A MAGIC */
    zeit_correct_size(&((*ol_sizep)[i]), &((*ol_offsetp)[i]), 
		      fp, ZEIT_NCHARS-1);
  }

  return 0;
}

Private int
zeit_correct_size(long *sizep, long *offsetp, FILE *fp, int i)
{
  int   x, y;

  if (debug_on('i'))
    printf("VFlib ZEIT: Correct Size  %04x:  size %d, offs %ld\n", 
	   i, (int)-(*sizep), *offsetp);

  zeit_seek(fp, *offsetp);
  zeit_init_bit_stream(fp);
  for (;;){
    x = zeit_read_10bits(); 
    y = zeit_read_10bits();
    if ((x == 1023) && (y == 1023))
      break;
    for (;;){
      x = zeit_read_10bits();
      y = zeit_read_10bits();
      if ((x == 1023) && (y == 1023))
	break;
    }
  }
  *sizep = -(zeit_current_pos(fp) - *offsetp);

  if (debug_on('i'))
    printf("VFlib ZEIT:   ==>  %04x\n", (int)-(*sizep));

  return *sizep;
}

Private VF_OUTLINE
ZEIT_ReadOutline(int zeit_id, int code_point, 
		 double mag_x, double mag_y)
{
  FILE       *fp;
  ZEIT       zeit;
  int        idx, x, y, xx, yy, max_code, space2121;
  char       *font_file;
  VF_OUTLINE      outline;
  VF_OUTLINE_ELEM *sizep;
  long          size, offs;
  unsigned int  scode;
  double        mx, my;

  mx = mag_x;
  my = mag_y;
  if (mx > 1){
    mx = 1.0;
    my = 1.0 / mx;
  }

  if ((zeit = ZEIT_GetZEIT(zeit_id)) == NULL){
    fprintf(stderr, "VFlib internal error: ZEIT_ReadOutline()\n");
    vf_error = VF_ERR_INTERNAL;
    return NULL;
  }
  
  /* Assume JISX0208-1990 & KUTEN encoding */
  max_code = 0x7426;
  space2121 = 1;

  if ((code_point < 0x2121) || (max_code < code_point)
      || (code_point%256 < 0x21) || (0x7e < code_point%256)){
    vf_error = VF_ERR_ILL_CODE_POINT;
    return NULL;
  }

  if ((space2121 == 1) && (code_point == 0x2121)){
    size = VF_OL_OUTLINE_HEADER_SIZE_TYPE0 + 1;
    ALLOCN_IF_ERR(outline, VF_OUTLINE_ELEM, size)
      return NULL;
    outline[VF_OL_HEADER_INDEX_DATA_SIZE] = size;
    return outline;
  }

  scode = zeit_code2c(code_point);
  if (code_point < 0x5000){
    font_file = zeit->path_name1;
    offs  = zeit->ol_offset1[scode];
    sizep = &zeit->ol_size1[scode];
  } else {
    font_file = zeit->path_name2;
    offs  = zeit->ol_offset2[scode];
    sizep = &zeit->ol_size2[scode];
  }
  if (*sizep == 0)
    return NULL;
  if (*sizep < 0)                 /* A MAGIC */
    size = 2*(-*sizep) + 1; 
  else
    size = *sizep + 1;

  ALLOCN_IF_ERR(outline, VF_OUTLINE_ELEM, 
		VF_OL_OUTLINE_HEADER_SIZE_TYPE0 + size)
    return NULL;
  
  if ((fp = vf_fm_OpenBinaryFileStream(font_file)) == NULL){
    fprintf(stderr, "VFlib Error. File not found: %s\n", font_file);
    return NULL;
  }

  zeit_seek(fp, offs);
  zeit_init_bit_stream(fp);
  for (idx = 0; ; ){
    x = zeit_read_10bits();
    y = zeit_read_10bits();
    if ((x == 1023) && (y == 1023))
      break;
    xx = VF_OL_COORD_OFFSET + mx * (x*VF_OL_COORD_RANGE)/ZEIT_MAX_VALUE;
    yy = VF_OL_COORD_OFFSET + my * (y*VF_OL_COORD_RANGE)/ZEIT_MAX_VALUE;
    outline[VF_OL_OUTLINE_HEADER_SIZE_TYPE0 + idx++] 
      = VF_OL_INSTR_TOKEN | VF_OL_INSTR_CWCURV | VF_OL_INSTR_LINE;
    outline[VF_OL_OUTLINE_HEADER_SIZE_TYPE0 + idx++] 
      = VF_OL_MAKE_XY(xx, yy);
    for (;;){
      x = zeit_read_10bits();
      y = zeit_read_10bits();
      if ((x == 1023) && (y == 1023))
	break;
      xx = VF_OL_COORD_OFFSET + mx * (x*VF_OL_COORD_RANGE)/ZEIT_MAX_VALUE;
      yy = VF_OL_COORD_OFFSET + my * (y*VF_OL_COORD_RANGE)/ZEIT_MAX_VALUE;
      outline[VF_OL_OUTLINE_HEADER_SIZE_TYPE0 + idx++] = VF_OL_MAKE_XY(xx, yy);
    }
  }
  outline[VF_OL_OUTLINE_HEADER_SIZE_TYPE0 + idx++] = 0;

  if (outline[VF_OL_OUTLINE_HEADER_SIZE_TYPE0] != 0)
    outline[VF_OL_OUTLINE_HEADER_SIZE_TYPE0] |= VF_OL_INSTR_CHAR;

  if (*sizep < 0)
    *sizep = (long)idx;

  if (debug_on('i'))
    printf("VFlib ZEIT:      SIZE %5ld\n", *sizep);

  outline[VF_OL_HEADER_INDEX_DATA_SIZE] 
    = *sizep + VF_OL_OUTLINE_HEADER_SIZE_TYPE0;

  return outline;
}


Private int
zeit_code2c(int code)
{
  if (code < 0x5000)
    return ((code/0x100) - 0x21)*0x5e + (code%0x100) - 0x21;

  return ((code/0x100) - 0x50)*0x5e + (code%0x100) - 0x21;
}



Private int
zeit_read_1byte(FILE *fp)
{
  return getc(fp);
}

Private unsigned long
zeit_read_4bytes(FILE *fp)
{
  unsigned long i1, i2, i3, i4;
  
  i1 = (unsigned long)zeit_read_1byte(fp);
  i2 = (unsigned long)zeit_read_1byte(fp);
  i3 = (unsigned long)zeit_read_1byte(fp);
  i4 = (unsigned long)zeit_read_1byte(fp);

  return i1 + i2*0x100 + i3*0x10000 + i4*0x1000000;
}


Private FILE           *zeit_bitstream_fp = NULL;
Private unsigned int   zeit_left_bits  = 0;
Private unsigned long  zeit_bit_stream = 0;
Private unsigned long  zeit_power2tbl[] = {
  0x000001,0x000002,0x000004,0x000008,0x000010,0x000020,0x000040,0x000080,
  0x000100,0x000200,0x000400,0x000800,0x001000,0x002000,0x004000,0x008000,
  0x010000,0x020000,0x040000,0x080000,0x100000,0x200000,0x400000,0x800000
}; 

Private void
zeit_init_bit_stream(FILE *fp)
{
  zeit_bitstream_fp = fp;
  zeit_bit_stream   = 0;
  zeit_left_bits    = 0;
}

Private int 
zeit_read_10bits(void)
{
  if (zeit_left_bits < 10){
    zeit_bit_stream  *= 0x10000;
    zeit_bit_stream  += (unsigned int)zeit_read_1byte(zeit_bitstream_fp);
    zeit_bit_stream  += (unsigned int)zeit_read_1byte(zeit_bitstream_fp)*0x100;
    zeit_left_bits   += 16;
  }
  zeit_left_bits -= 10;

  return (zeit_bit_stream/zeit_power2tbl[zeit_left_bits]) % ZEIT_MAX_VALUE;
}

Private void
zeit_seek(FILE *fp, long pos)
{
  fseek(fp, pos + ZEIT_HEADER_SIZE, SEEK_SET);
}

Private long
zeit_current_pos(FILE *fp)
{
  return ftell(fp) - ZEIT_HEADER_SIZE;
}


/*EOF*/