File: jg.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 (574 lines) | stat: -rw-r--r-- 13,592 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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
 * jg.c - JG 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  "jg.h"


Private  VF_TABLE  jg_table    = NULL;


Private int
JG_Init(void)
{
  JG_GetJG(-1);

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

  return 0;
}


Private void  jg_release(JG jg);
Private int   jg_make_header(char*,JG,int);
Private int   jg_correct_size(int seq, FILE *fp, JG_HEADER header);

Private int
JG_Open(char *font_name)
{
  JG   jg;
  int  jg_id, i;

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

  ALLOC_IF_ERR(jg, struct s_jg){
    vf_error = VF_ERR_NO_MEMORY;
    return -1;
  }

  jg->nfiles = 3;

  ALLOCN_IF_ERR(jg->headers, JG_HEADER, jg->nfiles){
    vf_error = VF_ERR_NO_MEMORY;
    goto Error;
  }
  for (i = 0; i < jg->nfiles; i++)
    jg->headers[i] = NULL;

  for (i = 0; i < jg->nfiles; i++){
    ALLOC_IF_ERR(jg->headers[i], struct s_jg_header){
      vf_error = VF_ERR_NO_MEMORY;
      goto Error;
    }
  }
  jg->headers[0]->nchars = JG_CODE_SIZE0;
  jg->headers[1]->nchars = JG_CODE_SIZE1;
  jg->headers[2]->nchars = JG_CODE_SIZE2;

  for (i = 0; i < jg->nfiles; i++){
    if (jg_make_header(font_name, jg, i) < 0)
      goto Error;
  }    

  if ((jg_id = (jg_table->put)(jg_table, jg, 
			       font_name, strlen(font_name)+1)) < 0){
    vf_error = VF_ERR_NO_MEMORY;
    goto Error;
  }

  JG_SetJG(jg_id, jg);

  return jg_id;

Error:
  jg_release(jg);
  return -1;
}


Private void
JG_Close(int jg_id)
{
  JG   jg;

  if ((jg = JG_GetJG(jg_id)) == NULL){
    fprintf(stderr, "VFlib internal error: JG_Close()\n");
    vf_error = VF_ERR_INTERNAL;
    return;
  }
  if ((jg_table->unlink_by_id)(jg_table, jg_id) > 0)
    return;

  jg_release(jg);
}

Private void
jg_release(JG jg)
{
  int  i;

  if (jg != NULL){ 
    for (i = 0; i < jg->nfiles; i++){
      if (jg->headers[i] != NULL){
	vf_free(jg->headers[i]->font_path);
	vf_free(jg->headers[i]->ol_offset);
	vf_free(jg->headers[i]->ol_size);
	vf_free(jg->headers[i]);
      }
    }
    vf_free(jg->headers);
    vf_free(jg);
  }
}


Private int   jg_read_header(char*,JG_HEADER);

Private int            jg_read_1byte(FILE*);
Private unsigned long  jg_read_4bytes(FILE*);
Private void           jg_init_bit_stream(FILE*);
Private unsigned int   jg_read_12bits(void);
Private int            jg_read_xy(int*,int*);


Private int
jg_make_header(char *font_name, JG jg, int fseq)
{
  char   *font_path, *e;
  char   fname[MAXPATHLEN];
  int    i;
  SEXP   s;

  font_path = 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, fseq);
    if ((font_path
	 = vf_search_file(fname, -1, NULL, FALSE, -1,
			  default_fontdirs, NULL, NULL)) != NULL)
      break;
  }
  
  if (font_path == NULL){
    if (fseq == 0){
      vf_error = VF_ERR_NO_FONT_FILE;
      return -1;
    } else {
      jg->headers[fseq]->font_path = NULL;
      jg->headers[fseq]->ol_offset = NULL;
      jg->headers[fseq]->ol_size   = NULL;
    }
    return 0;
  }

  if (debug_on('f'))
    printf("VFlib JG: font path: %s\n", font_path);

  jg->headers[fseq]->font_path = font_path;
  ALLOCN_IF_ERR(jg->headers[fseq]->ol_offset, long, jg->headers[fseq]->nchars)
    goto Error;
  ALLOCN_IF_ERR(jg->headers[fseq]->ol_size, long, jg->headers[fseq]->nchars)
    goto Error;

  for (i = 0; i < jg->headers[fseq]->nchars; i++){
    jg->headers[fseq]->ol_offset[i] = -1;
    jg->headers[fseq]->ol_size[i]   = 0;
  }

  if (jg_read_header(font_path, jg->headers[fseq]) < 0)
    goto Error;

  return 0;

Error:
  vf_error = VF_ERR_NO_MEMORY;
  vf_free(jg->headers[fseq]->font_path); jg->headers[fseq]->font_path = NULL;
  vf_free(jg->headers[fseq]->ol_offset); jg->headers[fseq]->ol_offset = NULL;
  vf_free(jg->headers[fseq]->ol_size);   jg->headers[fseq]->ol_size   = NULL;
  return -1;
}

Private int 
jg_read_header(char *font_path, JG_HEADER header)
{
  int   prefix, 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;
  }

  fseek(fp, 8L, SEEK_SET);
  prefix  = (unsigned int) jg_read_1byte(fp);
  prefix += (unsigned int) jg_read_1byte(fp) * 0x100;
  header->base = 10 + prefix + 4*header->nchars;

  if (debug_on('i'))
    printf("VFlib JG: PREFIX %04x \n", prefix); 

  fseek(fp, (long)(prefix+0x0a), SEEK_SET);
  for (i = 0; i < header->nchars; i++){
    header->ol_offset[i] = jg_read_4bytes(fp);
    if (debug_on('i'))
      printf("VFlib JG: Index: %04x,  offset: %04lx\n",
	     i, header->ol_offset[i]);
    if (header->ol_offset[i] != EMPTY_PTR)
      header->ol_offset[i] += header->base;
  }

  for (i = 0; i < header->nchars-1; i++){
    if (header->ol_offset[i] == EMPTY_PTR){
      header->ol_size[i] = 0;
    } else {
      for (j = i+1; ; j++){
	if (j >= header->nchars){
	  header->ol_size[i] = -(THRESHOLD_SIZE+1);
	  break;
	}
	if (header->ol_offset[j] != EMPTY_PTR){
	  header->ol_size[i] = -(header->ol_offset[j] - header->ol_offset[i]);
	  break;
	}
      }
    }
    if (debug_on('i'))
      printf("VFlib JG:  index: %04x,  ol size: %04lx\n",
	     i, -header->ol_size[i]);
    if (-(header->ol_size[i]) > THRESHOLD_SIZE)
      header->ol_size[i] = EMPTY_PTR;
  }

  /* when i == header->nchars-1: */
  if (header->ol_offset[header->nchars-1] == EMPTY_PTR){
    header->ol_size[header->nchars-1] = 0;
  } else {
    header->ol_size[header->nchars-1] = -(THRESHOLD_SIZE+1);
    jg_correct_size(header->nchars-1, fp, header);
  }

  return 0;
}

/* correct the size of font data */
Private int
jg_correct_size(int seq, FILE *fp, JG_HEADER header) 
{
  int   x, y;

  if (debug_on('i'))
    printf("VFlib JG:  Too Large for %04x\n   Size:%04lx ",
	   seq, -header->ol_size[seq]); 

  fseek(fp, header->ol_offset[seq], SEEK_SET);
  jg_init_bit_stream(fp);
  for (;;){
    if (jg_read_xy(&x, &y) == -1)
      break;
    while (jg_read_xy(&x, &y) != -1)
      ;
  }
  header->ol_size[seq] = -(ftell(fp) - header->ol_offset[seq]);

  if (debug_on('i'))
    printf("VFlib JG:  ==>  %04lx\n", -header->ol_size[seq]);

  return header->ol_size[seq];
}


Private int   jg_correct_size(int,FILE*,JG_HEADER);
Private int   jg_charcode2c(int);

Private VF_OUTLINE
JG_ReadOutline(int jg_id, int code_point, 
	       double mag_x, double mag_y)
{
  FILE       *fp;
  JG         jg;
  int        fseq, max_code, space2121;
  int        token_idx, idx, cnt, cmd, x, y, cmdp, xp, yp, cmd0, x0, y0;
  long          size, offs;
  unsigned int  scode;
  VF_OUTLINE        outline;
  VF_OUTLINE_ELEM   *sizep, chd;
  double        mx, my;
#define CONV_COORD_X(p)  \
   ((unsigned int)(VF_OL_COORD_OFFSET + ((p)*mx*VF_OL_COORD_RANGE)/JG_MAX_XY))
#define CONV_COORD_Y(p)  \
   ((unsigned int)(VF_OL_COORD_OFFSET + ((p)*my*VF_OL_COORD_RANGE)/JG_MAX_XY))

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

  if ((jg = JG_GetJG(jg_id)) == NULL){
    fprintf(stderr, "VFlib internal error: JG_ReadOutline()\n");
    vf_error = VF_ERR_INTERNAL;
    return NULL;
  }
  
  /* Assume JISX0208-1990 & JIS 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)){
    ALLOCN_IF_ERR(outline, VF_OUTLINE_ELEM, 
		  VF_OL_OUTLINE_HEADER_SIZE_TYPE0 + 1)
      return NULL;
    outline[VF_OL_OUTLINE_HEADER_SIZE_TYPE0] = 0L;
    return outline;
  }

  if (code_point < 0x3000){
    fseq = 0;
  } else if (code_point < 0x5000){
    fseq = 1;
  } else {
    fseq = 2;
  }
  scode = jg_charcode2c((int)code_point);

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

  offs  = jg->headers[fseq]->ol_offset[scode];
  sizep = &jg->headers[fseq]->ol_size[scode];
  if (*sizep == EMPTY_PTR)
    jg_correct_size(scode, fp, jg->headers[fseq]);
  
  if (*sizep == 0)
    return NULL;
  if (*sizep < 0)
    size = -3*(*sizep) + 1;
  else
    size = *sizep;

  ALLOCN_IF_ERR(outline, VF_OUTLINE_ELEM, 
		VF_OL_OUTLINE_HEADER_SIZE_TYPE0 + size)
    return NULL;
  
  fseek(fp, offs, SEEK_SET);
  jg_init_bit_stream(fp);

  cnt = 0;
  idx = VF_OL_OUTLINE_HEADER_SIZE_TYPE0;
  chd = VF_OL_INSTR_CHAR;
  for (;;){
NEXT_CIRCLE:
    if ((cmd = jg_read_xy(&x, &y)) == -1)
      break;
    cmd0 = cmd; x0 = x; y0 = y;
    cmdp = cmd; xp = x; yp = y; 
    if (debug_on('o'))
      printf("VFlib JG:  CMD0=%d, X0=%d, Y0=%d\n", cmd0, x0, y0);
    token_idx = idx;
    outline[idx++] = (chd | VF_OL_INSTR_CWCURV | VF_OL_INSTR_TOKEN);
    cnt++;
    chd = 0L;

    for (;;){ 
      cmd = jg_read_xy(&x, &y);
      switch (cmd){
      case -1:
      END_OF_CCURV:
	if (cmdp != cmd0){
	  outline[idx++] 
	    = ((cmd0==0) ? VF_OL_INSTR_LINE : VF_OL_INSTR_BEZ) 
	      | VF_OL_INSTR_TOKEN;
	  cnt++;
	}
	outline[idx++] = VF_OL_MAKE_XY(CONV_COORD_X(xp), CONV_COORD_Y(yp));
	cnt++;
	goto NEXT_CIRCLE;
      case 0:
      LINE:
	outline[token_idx] |= (VF_OL_INSTR_LINE | VF_OL_INSTR_TOKEN);
	outline[idx++] = VF_OL_MAKE_XY(CONV_COORD_X(xp), CONV_COORD_Y(yp));
	cnt++;
	cmdp = cmd;  xp = x; yp = y;
	cmd = jg_read_xy(&x, &y);
	while (cmd == 0){
	  outline[idx++] = VF_OL_MAKE_XY(CONV_COORD_X(xp), CONV_COORD_Y(yp));
	  cnt++;
	  cmdp = cmd;  xp = x; yp = y; 
	  cmd = jg_read_xy(&x, &y);
	}
	if (cmd == -1)
	  goto END_OF_CCURV; 
	token_idx = idx;
	outline[idx++] = 0L;
	cnt++;
	goto BEZ;
      case 1:
      BEZ: 
	outline[token_idx] |= (VF_OL_INSTR_BEZ | VF_OL_INSTR_TOKEN);
	outline[idx++] = VF_OL_MAKE_XY(CONV_COORD_X(xp), CONV_COORD_Y(yp));
	cnt++;
	cmdp = cmd;  xp = x; yp = y; 
	cmd = jg_read_xy(&x, &y);
	while (cmd == 1){
	  outline[idx++] = VF_OL_MAKE_XY(CONV_COORD_X(xp), CONV_COORD_Y(yp));
	  cnt++;
	  cmdp = cmd;  xp = x; yp = y; 
	  cmd = jg_read_xy(&x, &y);
	}
	if (cmd == -1)
	  goto END_OF_CCURV; 
	token_idx = idx;
	outline[idx++] = 0L;
	cnt++;
	goto LINE;
      }
    }
  }
  outline[idx++] = 0L;
  cnt++;

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

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

  return outline;
}


Private int
jg_charcode2c(int code_point)
{
  int  jgcode;
  
  if (code_point < 0x3000)
    jgcode = (code_point/256 - 0x21)*0x5e + (code_point%256) - 0x21;
  else if (code_point < 0x5000)
    jgcode = (code_point/256 - 0x30)*0x5e + (code_point%256) - 0x21;
  else 
    jgcode = (code_point/256 - 0x50)*0x5e + (code_point%256) - 0x21;

  return jgcode;
}



Private int
jg_read_1byte(FILE *fp)
{
  return fgetc(fp);
}

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

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


Private FILE           *jg_bitstream_fp = NULL;
Private unsigned int    jg_left_bits  = 0;
Private unsigned long   jg_bit_stream = 0;

Private void
jg_init_bit_stream(FILE *fp)
{
  jg_bitstream_fp = fp;
  jg_bit_stream   = 0;
  jg_left_bits    = 0;
}

Private unsigned int 
jg_read_12bits(void)
{
  if (jg_left_bits < 12){
    jg_bit_stream  = jg_bit_stream * 0x10000L;
    jg_bit_stream  += (unsigned int) jg_read_1byte(jg_bitstream_fp);
    jg_bit_stream  += (unsigned int) jg_read_1byte(jg_bitstream_fp) * 0x0100L;
    jg_left_bits   += 16;
  }
  jg_left_bits -= 12;

  return (jg_bit_stream >> jg_left_bits) & JG_MAX_VALUE;
}

Private int
jg_read_xy(int *xp, int *yp)
{
  int   x, y, fx, fy;

  x = jg_read_12bits();
  y = jg_read_12bits();
  if ((x == JG_MAX_VALUE) && (y == JG_MAX_VALUE))
    return -1;

  *xp  = x & JG_XY_MASK;
  *yp  = y & JG_XY_MASK;
  fx   = x & JG_CMD_MASK;
  fy   = y & JG_CMD_MASK;

  if (*xp > (JG_MAX_XY+1)/2)
    *xp = (JG_MAX_XY+1) - *xp;
  else 
    *xp += (JG_MAX_XY+1)/2;
  if (*yp < (JG_MAX_XY+1)/2)
    *yp  = (JG_MAX_XY+1)/2 - *yp;

  *xp -= (JG_MAX_XY+1)/4;
  *xp = (*xp < 0) ? 0: *xp * 2;
  *yp -= (JG_MAX_XY+1)*5/16;
  *yp = (*yp < 0) ? 0: *yp * 2;
  if (fx != 0)
    return 1;

  return 0;
}



/*EOF*/