summaryrefslogtreecommitdiff
path: root/src/gf/gflib.c
blob: 323190d616868b2c3ad239475fda8a1dfbfeaa5e (plain)
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/****************************************************************************
 *
 * gflib.c
 *
 *   FreeType font driver for METAFONT GF FONT files
 *
 * Copyright 1996-2018 by
 * David Turner, Robert Wilhelm, and Werner Lemberg.
 *
 * This file is part of the FreeType project, and may only be used,
 * modified, and distributed under the terms of the FreeType project
 * license, LICENSE.TXT.  By continuing to use, modify, or distribute
 * this file you indicate that you have read the license and
 * understand and accept it fully.
 *
 */

#include <ft2build.h>

#include FT_FREETYPE_H
#include FT_INTERNAL_DEBUG_H
#include FT_INTERNAL_STREAM_H
#include FT_INTERNAL_OBJECTS_H
#include FT_SYSTEM_H
#include FT_CONFIG_CONFIG_H
#include FT_ERRORS_H
#include FT_TYPES_H

#include "gf.h"
#include "gfdrivr.h"
#include "gferror.h"


  /**************************************************************************
   *
   * The macro FT_COMPONENT is used in trace mode.  It is an implicit
   * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
   * messages during execution.
   */
#undef  FT_COMPONENT
#define FT_COMPONENT  trace_gflib

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

  /**************************************************************************
   *
   * GF font utility functions.
   *
   */

  FT_Long     gf_read_intn(FT_Stream, FT_Int);
  FT_ULong    gf_read_uintn(FT_Stream, FT_Int);

#define READ_UINT1( stream )    (FT_Byte)gf_read_uintn( stream, 1)
#define READ_UINT2( stream )    (FT_Byte)gf_read_uintn( stream, 2)
#define READ_UINT3( stream )    (FT_Byte)gf_read_uintn( stream, 3)
#define READ_UINT4( stream )    (FT_Byte)gf_read_uintn( stream, 4)
#define READ_UINTN( stream,n)   (FT_ULong)gf_read_uintn( stream, n)
#define READ_INT1( stream )     (FT_String)gf_read_intn( stream, 1)
#define READ_INT4( stream )     (FT_Long)gf_read_intn( stream, 4)

/*
 * Reading a Number from file
 */
  FT_ULong
  gf_read_uintn(FT_Stream stream, FT_Int size)
  {
    FT_ULong  v,k;
    FT_Error  error;
    FT_Byte   tp;

    v = 0L;

    while (size >= 1)
    {
      if ( FT_READ_BYTE(tp) )
        return 0;
      k = (FT_ULong)tp;
      v = v*256L + k;
      --size;
    }
    return v;
  }

  FT_Long
  gf_read_intn(FT_Stream stream, FT_Int size)
  {
    FT_Long   v;
    FT_Byte   tp;
    FT_Error  error;
    FT_ULong  z;

    if ( FT_READ_BYTE(tp) )
        return 0;
    z = (FT_ULong)tp;
    v = (FT_Long)z & 0xffL;

    if (v & 0x80L)
      v = v - 256L;
    --size;

    while (size >= 1)
    {
      if ( FT_READ_BYTE(tp) )
        return 0;
      z = (FT_ULong)tp;
      v = v*256L + z;
      --size;
		}
    return v;
  }

  /**************************************************************************
   *
   * API.
   *
   */

  FT_LOCAL_DEF( FT_Error )
  gf_read_glyph( FT_Stream    stream,
                 GF_Bitmap    bm,
                 FT_Memory    memory )
  {
    FT_Long        m, n;
    FT_Int         paint_sw;
    FT_Int         instr,inst;
    FT_Long        min_m, max_m, min_n, max_n, del_m, del_n;
    FT_Long        w, h, d;
    FT_Int         m_b, k;
    FT_Byte        *ptr;
    FT_Error       error  = FT_Err_Ok;

    for (  ;  ;  )
    {
      inst = READ_UINT1( stream );
      switch ((FT_Int)inst)
      {
      case GF_BOC:
        if ( FT_STREAM_SKIP( 4 ) )
          return -1;
        if ( FT_STREAM_SKIP( 4 ) )
          return -1;
        min_m = READ_INT4( stream );
        max_m = READ_INT4( stream );
        min_n = READ_INT4( stream );
        max_n = READ_INT4( stream );
        goto BOC;
        break;
      case GF_BOC1:
        if ( FT_STREAM_SKIP( 1 ) )
          return -1;
        del_m = (FT_Long)READ_UINT1( stream );
        max_m = (FT_Long)READ_UINT1( stream );
        del_n = (FT_Long)READ_UINT1( stream );
        max_n = (FT_Long)READ_UINT1( stream );
        min_m = max_m - del_m;
        min_n = max_n - del_n;
        goto BOC;
        break;
      case GF_XXX1:
        k = (FT_ULong)READ_UINT1( stream );
        if ( FT_STREAM_SKIP( k ) )
          return -1;
        break;
      case GF_XXX2:
        k = (FT_ULong)READ_UINT2( stream );
        if ( FT_STREAM_SKIP( k ) )
          return -1;
        break;
      case GF_XXX3:
        k = (FT_ULong)READ_UINT3( stream );
        if ( FT_STREAM_SKIP( k ) )
          return -1;
        break;
      case GF_XXX4:
        k = (FT_ULong)READ_UINT4( stream );
        if ( FT_STREAM_SKIP( k ) )
          return -1;
        break;
      case GF_YYY :
        if ( FT_STREAM_SKIP( 4 ) )
          return -1;
        break;
      case GF_NO_OP:
        break;
      default:
        return -1;
      }
    }

    return 0;

    BOC:
      if(error != FT_Err_Ok)
        return -1;
      w = max_m - min_m + 1;
      h = max_n - min_n + 1;
      if ((w < 0) || (h < 0))
      {
        FT_ERROR(( "gf_read_glyph: invalid w and h values\n" ));
        error = FT_THROW( Invalid_File_Format );
        return -1;
      }

      /* allocate and build bitmap */
      if ((bm->bitmap = (FT_Byte*)malloc(h*((w+7)/8))) == NULL)
      {
        error = FT_THROW( Invalid_File_Format );
        return -1;
      }

      memset(bm->bitmap, 0, h*((w+7)/8));
      bm->raster     = (FT_UInt)(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 = (FT_Int)READ_UINT1( stream )) != 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 ((FT_Int)instr)
          {
          case GF_PAINT1:
          case GF_PAINT2:
          case GF_PAINT3:
            d = (FT_ULong)READ_UINTN( stream, (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 - (FT_ULong)READ_UINTN( stream, (instr - GF_SKIP1 + 1)) - 1;
            paint_sw = 0;
            break;
          case GF_XXX1:
          case GF_XXX2:
          case GF_XXX3:
          case GF_XXX4:
            k = READ_UINTN( stream, instr - GF_XXX1 + 1);
            if ( FT_STREAM_SKIP( k ) )
              return -1;
            break;
          case GF_YYY:
            if ( FT_STREAM_SKIP( 4 ) )
              return -1;
            break;
          case GF_NO_OP:
            break;
          default:
            FT_FREE(bm->bitmap);
            bm->bitmap = NULL;
            error = FT_THROW( Invalid_File_Format );
            return -1;
           }
        }
      }
    return 0;
  }

  FT_LOCAL_DEF( FT_Error )
  gf_load_font(  FT_Stream    stream,
                 FT_Memory    extmemory,
                 GF_Glyph     *goptr  )
  {
    GF_Glyph        go;
    GF_Bitmap       bm;
    FT_Byte         instr, d, pre, id, k, code;
    FT_ULong        ds, check_sum, hppp, vppp;
    FT_Long         min_m, max_m, min_n, max_n, w;
    FT_UInt         dx, dy;
    FT_Long         ptr_post, ptr_p, ptr, optr;
    FT_Int          bc, ec, nchars, i;
    FT_Error        error  = FT_Err_Ok;
    FT_Memory       memory = extmemory; /* needed for FT_NEW */

    go = NULL;
    nchars = -1;

    if( FT_STREAM_SEEK( 0 ) )
      goto Exit;

    pre = READ_UINT1( stream );
    if (pre != GF_PRE)
    {
      FT_ERROR(( "gf_load_font: missing GF_PRE(247) field\n" ));
      error = FT_THROW( Unknown_File_Format );
      goto Exit;
    }

    id = READ_UINT1( stream );
    if (id != GF_ID)
    {
      FT_ERROR(( "gf_load_font: missing GF_ID(131) field\n" ));
      error = FT_THROW( Unknown_File_Format );
      goto Exit;
    }

    k = READ_UINT1( stream );
    if ( FT_STREAM_SKIP( k ) )
      goto Exit;

    /* seek to post_post instr. */
    if( FT_STREAM_SEEK( stream->size - 1 ) )
      goto Exit;
    if( FT_STREAM_SEEK( stream->size - 1 ) )
      goto Exit;

    while ( READ_UINT1( stream ) == 223)
    {
      if( FT_STREAM_SEEK( stream->pos -2 ) )
        goto Exit;
    }


    if( FT_STREAM_SEEK( stream->pos -1 ) )
        goto Exit;
    d= READ_UINT1( stream );

    if (d != GF_ID)
    {
      FT_ERROR(( "gf_load_font: missing GF_ID(131) field\n" ));
      error = FT_THROW( Unknown_File_Format );
      goto Exit;
    }

    FT_TRACE2(( "gf_load_font: GF_ID(131) found\n" ));

    if(FT_STREAM_SEEK( stream->pos -6 ))
      goto Exit;

    /* check if the code is post_post */
    if (READ_UINT1( stream ) != GF_POST_POST)
    {
      FT_ERROR(( "gf_load_font: missing GF_POST_POST(249) field\n" ));
      error = FT_THROW( Unknown_File_Format );
      goto Exit;
    }

    FT_TRACE2(( "gf_load_font: GF_POST_POST(249) found\n" ));

    /* read pointer to post instr. */
    if(FT_READ_ULONG( ptr_post ))
      goto Exit;

    if (ptr_post == -1)
    {
      FT_ERROR(( "gf_load_font: invalid postamble pointer\n" ));
      error = FT_THROW( Unknown_File_Format );
      goto Exit;
    }

    /* goto post instr. and read it */
    if(FT_STREAM_SEEK( ptr_post ))
      goto Exit;

    if (READ_UINT1( stream ) != GF_POST)
    {
      FT_ERROR(( "gf_load_font: missing GF_POST(248) field\n" ));
      error = FT_THROW( Unknown_File_Format );
      goto Exit;
    }
    FT_TRACE2(( "gf_load_font: GF Postamble found\n" ));

    if(FT_READ_ULONG( ptr_p ))
      goto Exit;
    if(FT_READ_ULONG( ds ))
      goto Exit;
    if(FT_READ_ULONG( check_sum ))
      goto Exit;
    if(FT_READ_ULONG( hppp ))
      goto Exit;
    if(FT_READ_ULONG( vppp ))
      goto Exit;
    min_m     = READ_INT4( stream );
    max_m     = READ_INT4( stream );
    min_n     = READ_INT4( stream );
    max_n     = READ_INT4( stream );

    if( ptr_p < 0 )     /* Defined to use ptr_p */
    {
      FT_ERROR(( "gf_load_font: invalid pointer in postamble\n" ));
      goto Exit;
    }

    if( check_sum < 0 ) /* Defined to use check_sum */
    {
      FT_ERROR(( "gf_load_font: invalid check sum value\n" ));
      goto Exit;
    }
    #if 0
      gptr = ftell(fp);
    #endif

    #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
        {
          error = FT_THROW( Invalid_File_Format );
          goto Exit;
        }
        if (code < bc)
          bc = code;
        if (code > ec)
          ec = code;
      }
    #else
      bc = 0;
      ec = 255;
    #endif

    nchars = ec - bc + 1;

    if( FT_ALLOC(go, sizeof(GF_GlyphRec)) )
      goto Exit;

    if( FT_ALLOC_MULT(go->bm_table, sizeof(GF_BitmapRec), nchars) )
      goto Exit;

    FT_TRACE2(( "gf_load_font: Allocated bitmap table\n" ));

    for (i = 0; i < nchars; i++)
      go->bm_table[i].bitmap = NULL;

    go->ds   = (FT_UInt)ds/(1<<20);
    go->hppp = (FT_UInt)hppp/(1<<16);
    go->vppp = (FT_UInt)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( stream )) == GF_POST_POST)
        break;
      switch ((FT_Int)instr)
      {
      case GF_CHAR_LOC:
        code = READ_UINT1( stream );
        dx   = (FT_UInt)READ_INT4( stream )/(FT_UInt)(1<<16);
        dy   = (FT_UInt)READ_INT4( stream )/(FT_UInt)(1<<16);
        w    = READ_INT4( stream );
        ptr  = READ_INT4( stream );
        break;
      case GF_CHAR_LOC0:
        code = READ_UINT1( stream );
        dx   = (FT_UInt)READ_INT1( stream );
        dy   = (FT_UInt)0;
        w    = READ_INT4( stream );
        ptr  = READ_INT4( stream );
        break;
      default:
        FT_ERROR(( "gf_load_font: missing character locators in postamble\n" ));
        error = FT_THROW( Unknown_File_Format );
        goto Exit;
      }

      /*
      if( w > max_m)   Defined to use w
      {
        FT_ERROR(( "gf_load_font: invalid width in charloc\n" ));
        goto Exit;
      }
      */

      optr = stream->pos;
      if( FT_STREAM_SEEK( ptr ) )
        goto Exit;

      bm = &go->bm_table[code - bc];
      if (gf_read_glyph( stream, bm, memory ) < 0)
        goto Exit;

      bm->mv_x = dx;
      bm->mv_y = dy;
      if(FT_STREAM_SEEK( optr ))
        goto Exit;
    }
    *goptr          = go;
    return error;

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


  FT_LOCAL_DEF( void )
  gf_free_font( GF_Face face )
  {
    FT_Memory  memory = FT_FACE( face )->memory;
    GF_Glyph   go     = face->gf_glyph;
    FT_UInt    nchars = FT_FACE( face )->num_glyphs,i;

    if ( !go )
      return;

    if( go->bm_table )
    {
      for (i = 0; i < nchars; i++)
	      FT_FREE(go->bm_table[i].bitmap);
    }
    FT_FREE(go->bm_table);
    FT_FREE(go);
  }


/* END */