summaryrefslogtreecommitdiff
path: root/cut-n-paste-code/freetype/t1parse.c
blob: dfb3a0f090ba648b8bbbaa36f87b943f346df983 (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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
/***************************************************************************/
/*                                                                         */
/*  t1parse.c                                                              */
/*                                                                         */
/*    Type 1 parser (body).                                                */
/*                                                                         */
/*  Copyright 1996-2000 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.                                        */
/*                                                                         */
/***************************************************************************/


#ifdef FT_FLAT_COMPILE

#include "ftdebug.h"
#include "t1types.h"


#else

#include <freetype/internal/ftdebug.h>
#include <freetype/internal/t1types.h>


#endif

#ifdef FT_FLAT_COMPILE

#include "t1parse.h"

#else

#ifdef FT_FLAT_COMPILE

#include "t1parse.h"

#else

#include <type1/t1parse.h>

#endif

#endif


#include <stdio.h>  /* for sscanf()  */
#include <string.h> /* for strncpy() */


  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    T1_New_Table                                                       */
  /*                                                                       */
  /* <Description>                                                         */
  /*    Initializes a T1_Table structure.                                  */
  /*                                                                       */
  /* <InOut>                                                               */
  /*    table  :: The address of the target table.                         */
  /*                                                                       */
  /* <Input>                                                               */
  /*    count  :: The table size (i.e. maximum number of elements).        */
  /*    memory :: The memory object to use for all subsequent              */
  /*              reallocations.                                           */
  /*                                                                       */
  /* <Return>                                                              */
  /*    FreeType error code.  0 means success.                             */
  /*                                                                       */
  LOCAL_FUNC
  FT_Error  T1_New_Table( T1_Table*  table,
                          FT_Int     count,
                          FT_Memory  memory )
  {
	 FT_Error  error;


	 table->memory = memory;

	 if ( ALLOC_ARRAY( table->elements, count, FT_Byte* ) )
		 return error;

	 if ( ALLOC_ARRAY( table->lengths, count, FT_Byte* ) )
	 {
	   FREE( table->elements );
	   return error;
	}

	table->max_elems = count;
	table->num_elems = 0;

	table->block        = 0;
	table->capacity     = 0;
	table->cursor       = 0;

	return error;
  }


  static
  FT_Error  reallocate_t1_table( T1_Table*  table,
                                 FT_Int     new_size )
  {
    FT_Memory  memory   = table->memory;
    FT_Byte*   old_base = table->block;
    FT_Error   error;


    /* reallocate the base block */
    if ( REALLOC( table->block, table->capacity, new_size ) )
      return error;
    table->capacity = new_size;

    /* shift all offsets if necessary */
    if ( old_base )
    {
      FT_Long    delta  = table->block - old_base;
      FT_Byte**  offset = table->elements;
      FT_Byte**  limit  = offset + table->max_elems;


      if ( delta )
        for ( ; offset < limit; offset ++ )
          if (offset[0])
            offset[0] += delta;
    }

    return T1_Err_Ok;
  }


  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    T1_Add_Table                                                       */
  /*                                                                       */
  /* <Description>                                                         */
  /*    Adds an object to a T1_Table, possibly growing its memory block.   */
  /*                                                                       */
  /* <InOut>                                                               */
  /*    table  :: The target table.                                        */
  /*                                                                       */
  /* <Input>                                                               */
  /*    index  :: The index of the object in the table.                    */
  /*                                                                       */
  /*    object :: The address of the object to copy in memory.             */
  /*                                                                       */
  /*    length :: The length in bytes of the source object.                */
  /*                                                                       */
  /* <Return>                                                              */
  /*    FreeType error code.  0 means success.  An error is returned if a  */
  /*    reallocation failed.                                               */
  /*                                                                       */
  LOCAL_FUNC
  FT_Error  T1_Add_Table( T1_Table*  table,
                          FT_Int     index,
                          void*      object,
                          FT_Int     length )
  {
	if ( index < 0 || index > table->max_elems )
    {
	  FT_ERROR(( "T1_Add_Table: invalid index\n" ));
	  return T1_Err_Syntax_Error;
    }

    /* grow the base block if needed */
    if ( table->cursor + length > table->capacity )
    {
      FT_Error  error;
      FT_Int    new_size = table->capacity;


      while ( new_size < table->cursor + length )
        new_size += 1024;

      error = reallocate_t1_table( table, new_size );
      if ( error )
        return error;
    }

    /* add the object to the base block and adjust offset */
    table->elements[index] = table->block + table->cursor;
    table->lengths [index] = length;
    MEM_Copy( table->block + table->cursor, object, length );

    table->cursor += length;

    return T1_Err_Ok;
  }


  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    T1_Done_Table                                                      */
  /*                                                                       */
  /* <Description>                                                         */
  /*    Finalize a T1_Table (reallocate it to its current cursor).         */
  /*                                                                       */
  /* <Input>                                                               */
  /*    table :: The target table.                                         */
  /*                                                                       */
  /* <Note>                                                                */
  /*    This function does NOT release the heap's memory block.  It is up  */
  /*    to the caller to clean it, or reference it in its own structures.  */
  /*                                                                       */
  LOCAL_FUNC
  void  T1_Done_Table( T1_Table*  table )
  {
    FT_Memory  memory = table->memory;
    FT_Error   error;
    FT_Byte*   old_base;


    /* should never fail, as rec.cursor <= rec.size */
    old_base = table->block;
    if ( !old_base )
      return;

    (void)REALLOC( table->block, table->capacity, table->cursor );
    table->capacity = table->cursor;

    if ( old_base != table->block )
    {
      FT_Long    delta   = table->block - old_base;
      FT_Byte**  element = table->elements;
      FT_Byte**  limit   = element + table->max_elems;


      for ( ; element < limit; element++ )
        if ( element[0] )
          element[0] += delta;
    }
  }


  LOCAL_FUNC
  FT_String*  CopyString( T1_Parser*  parser )
  {
    FT_String*  string = NULL;
    T1_Token*   token  = parser->args++;
    FT_Memory   memory = parser->tokenizer->memory;
    FT_Error    error;


    if ( token->kind == tok_string )
    {
      FT_Int  len = token->len - 2;


      if ( ALLOC( string, len + 1 ) )
      {
        parser->error = error;
        return 0;
      }

      MEM_Copy( string, parser->tokenizer->base + token->start + 1, len );
      string[len] = '\0';

      parser->error = T1_Err_Ok;
    }
    else
    {
      FT_ERROR(( "T1_CopyString: syntax error, string token expected!\n" ));
      parser->error = T1_Err_Syntax_Error;
    }

    return string;
  }


  static
  FT_Error  parse_int( FT_Byte*  base,
                       FT_Byte*  limit,
                       FT_Long*  result )
  {
    FT_Bool  sign = 0;
    FT_Long  sum  = 0;


    if ( base >= limit )
      goto Fail;

    /* check sign */
    if ( *base == '+' )
      base++;

    else if ( *base == '-' )
    {
      sign++;
      base++;
    }

    /* parse digits */
    if ( base >= limit )
      goto Fail;

    do
    {
      sum = ( 10 * sum + ( *base++ - '0' ) );

    } while ( base < limit );

    if ( sign )
      sum = -sum;

    *result = sum;
    return T1_Err_Ok;

  Fail:
    FT_ERROR(( "parse_int: integer expected\n" ));
    *result = 0;
    return T1_Err_Syntax_Error;
  }


  static
  FT_Error  parse_float( FT_Byte*  base,
                         FT_Byte*  limit,
                         FT_Long   scale,
                         FT_Long*  result )
  {
#if 1

    /* XXX: We are simply much too lazy to code this function   */
    /*      properly for now.  We will do that when the rest of */
    /*      the driver works properly.                          */
    char    temp[32];
    int     len = limit - base;
    double  value;


    if ( len > 31 )
      goto Fail;

    strncpy( temp, (char*)base, len );
    temp[len] = '\0';
    if ( sscanf( temp, "%lf", &value ) != 1 )
      goto Fail;

    *result = (FT_Long)( scale * value );
    return 0;

#else

    FT_Byte*  cur;
    FT_Bool   sign        = 0;  /* sign                        */
    FT_Long   number_int  = 0;  /* integer part                */
    FT_Long   number_frac = 0;  /* fractional part             */
    FT_Long   exponent    = 0;  /* exponent value              */
    FT_Int    num_frac    = 0;  /* number of fractional digits */


    /* check sign */
    if ( *base == '+' )
      base++;

    else if ( *base == '-' )
    {
      sign++;
      base++;
    }

    /* find integer part */
    cur = base;
    while ( cur < limit )
    {
      FT_Byte  c = *cur;


      if ( c == '.' || c == 'e' || c == 'E' )
        break;

      cur++;
    }

    if ( cur > base )
    {
      error = parse_integer( base, cur, &number_int );
      if ( error )
        goto Fail;
    }

    /* read fractional part, if any */
    if ( *cur == '.' )
    {
      cur++;
      base = cur;
      while ( cur < limit )
      {
        FT_Byte  c = *cur;


        if ( c == 'e' || c == 'E' )
          break;
        cur++;
      }

      num_frac = cur - base;

      if ( cur > base )
      {
        error = parse_integer( base, cur, &number_frac );
        if ( error )
          goto Fail;
        base = cur;
      }
   }

    /* read exponent, if any */
    if ( *cur == 'e' || *cur == 'E' )
    {
      cur++;
      base = cur;
      error = parse_integer( base, limit, &exponent );
      if ( error )
        goto Fail;

      /* now check that exponent is within `correct bounds' */
      /* i.e. between -6 and 6                              */
      if ( exponent < -6 || exponent > 6 )
        goto Fail;
    }

    /* now adjust integer value and exponent for fractional part */
    while ( num_frac > 0 )
    {
      number_int *= 10;
      exponent--;
      num_frac--;
    }

    number_int += num_frac;

    /* skip point if any, read fractional part */
    if ( cur + 1 < limit )
    {
      if (*cur..
    }

    /* now compute scaled float value */
    /* XXX: incomplete!               */

#endif /* 1 */

  Fail:
    FT_ERROR(( "parse_float: syntax error!\n" ));
    return T1_Err_Syntax_Error;
  }


  static
  FT_Error  parse_integer( FT_Byte*  base,
                           FT_Byte*  limit,
                           FT_Long*  result )
  {
    FT_Byte*  cur;


    /* the lexical analyser accepts floats as well as integers */
    /* now; check that we really have an int in this token     */
    cur = base;
    while ( cur < limit )
    {
      FT_Byte  c = *cur++;


      if ( c == '.' || c == 'e' || c == 'E' )
        goto Float_Number;
    }

    /* now read the number's value */
    return parse_int( base, limit, result );

  Float_Number:
    /* we really have a float there; simply call parse_float in this */
    /* case with a scale of `10' to perform round                    */
    {
      FT_Error error;


      error = parse_float( base, limit, 10, result );
      if ( !error )
      {
        if ( *result >= 0 )
          *result = ( *result + 5 ) / 10;      /* round value */
        else
          *result = -( ( 5 - *result ) / 10 );
      }
      return error;
    }
  }


  LOCAL_FUNC
  FT_Long  CopyInteger( T1_Parser*  parser )
  {
    FT_Long    sum   = 0;
    T1_Token*  token = parser->args++;


    if ( token->kind == tok_number )
    {
      FT_Byte*  base  = parser->tokenizer->base + token->start;
      FT_Byte*  limit = base + token->len;


      /* now read the number's value */
      parser->error = parse_integer( base, limit, &sum );
      return sum;
    }

    FT_ERROR(( "CopyInteger: number expected\n" ));
    parser->args--;
    parser->error = T1_Err_Syntax_Error;
    return 0;
  }


  LOCAL_FUNC
  FT_Bool   CopyBoolean( T1_Parser*  parser )
  {
    FT_Error   error  = T1_Err_Ok;
    FT_Bool    result = 0;
    T1_Token*  token  = parser->args++;


    if ( token->kind == tok_keyword )
    {
      if ( token->kind2 == key_false )
        result = 0;

      else if ( token->kind2 == key_true )
        result = !0;

      else
        goto Fail;
    }
    else
    {
      Fail:
        FT_ERROR(( "CopyBoolean:" ));
        FT_ERROR(( " syntax error; `false' or `true' expected\n" ));
        error = T1_Err_Syntax_Error;
    }
    parser->error = error;
    return result;
  }


  LOCAL_FUNC
  FT_Long   CopyFloat( T1_Parser*  parser,
                       FT_Int      scale )
  {
    FT_Error   error;
    FT_Long    sum = 0;
    T1_Token*  token = parser->args++;


    if ( token->kind == tok_number )
    {
      FT_Byte*  base  = parser->tokenizer->base + token->start;
      FT_Byte*  limit = base + token->len;


      error = parser->error = parse_float( base, limit, scale, &sum );
      if ( error )
        goto Fail;

      return sum;
    }

  Fail:
    FT_ERROR(( "CopyFloat: syntax error!\n" ));
    parser->error = T1_Err_Syntax_Error;
    return 0;
  }


  LOCAL_FUNC
  void  CopyBBox( T1_Parser*  parser,
                  FT_BBox*    bbox )
  {
    T1_Token*  token = parser->args++;
    FT_Int     n;
    FT_Error   error;


    if ( token->kind == tok_program ||
         token->kind == tok_array   )
    {
      /* get rid of `['/`]', or `{'/`}' */
      FT_Byte*  base  = parser->tokenizer->base + token->start + 1;
      FT_Byte*  limit = base + token->len - 2;
      FT_Byte*  cur;
      FT_Byte*  start;


      /* read each parameter independently */
      cur = base;
      for ( n = 0; n < 4; n++ )
      {
        FT_Long*  result;


        /* skip whitespace */
        while ( cur < limit && *cur == ' ' )
          cur++;

        /* skip numbers */
        start = cur;
        while ( cur < limit && *cur != ' ' )
          cur++;

        /* compute result address */
        switch ( n )
        {
        case 0:
          result = &bbox->xMin;
          break;
        case 1:
          result = &bbox->yMin;
          break;
        case 2:
          result = &bbox->xMax;
          break;
        default:
          result = &bbox->yMax;
        }

        error = parse_integer( start, cur, result );
        if ( error )
          goto Fail;
      }
      parser->error = 0;
      return;
    }

  Fail:
    FT_ERROR(( "CopyBBox: syntax error!\n" ));
    parser->error = T1_Err_Syntax_Error;
  }


  LOCAL_FUNC
  void  CopyMatrix( T1_Parser*  parser,
                    FT_Matrix*  matrix )
  {
    T1_Token* token = parser->args++;
    FT_Error  error;


    if ( token->kind == tok_array )
    {
      /* get rid of `[' and `]' */
      FT_Byte*  base  = parser->tokenizer->base + token->start + 1;
      FT_Byte*  limit = base + token->len - 2;
      FT_Byte*  cur;
      FT_Byte*  start;
      FT_Int    n;


      /* read each parameter independently */
      cur = base;
      for ( n = 0; n < 4; n++ )
      {
        FT_Long*  result;


        /* skip whitespace */
        while ( cur < limit && *cur == ' ' )
          cur++;

        /* skip numbers */
        start = cur;
        while ( cur < limit && *cur != ' ')
          cur++;

        /* compute result address */
        switch ( n )
        {
        case 0:
          result = &matrix->xx;
          break;
        case 1:
          result = &matrix->yx;
          break;
        case 2:
          result = &matrix->xy;
          break;
        default:
          result = &matrix->yy;
        }

        error = parse_float( start, cur, 65536000L, result );
        if ( error )
          goto Fail;
      }
      parser->error = 0;
      return;
    }

  Fail:
    FT_ERROR(( "CopyMatrix: syntax error!\n" ));
    parser->error = T1_Err_Syntax_Error;
  }


  LOCAL_FUNC
  void  CopyArray( T1_Parser*  parser,
                   FT_Byte*    num_elements,
                   FT_Short*   elements,
                   FT_Int      max_elements )
  {
    T1_Token* token = parser->args++;
    FT_Error  error;


    if ( token->kind == tok_array   ||
         token->kind == tok_program )   /* in the case of MinFeature */
    {
      /* get rid of `['/`]', or `{'/`}' */
      FT_Byte*  base  = parser->tokenizer->base + token->start + 1;
      FT_Byte*  limit = base + token->len - 2;
      FT_Byte*  cur;
      FT_Byte*  start;
      FT_Int    n;


      /* read each parameter independently */
      cur = base;
      for ( n = 0; n < max_elements; n++ )
      {
        FT_Long  result;


        /* test end of string */
        if ( cur >= limit )
          break;

        /* skip whitespace */
        while ( cur < limit && *cur == ' ' )
          cur++;

        /* end of list? */
        if ( cur >= limit )
          break;

        /* skip numbers */
        start = cur;
        while ( cur < limit && *cur != ' ' )
          cur++;

        error = parse_integer( start, cur, &result );
        if ( error )
          goto Fail;

        *elements++ = (FT_Short)result;
      }

      if ( num_elements )
        *num_elements = (FT_Byte)n;

      parser->error = 0;
      return;
    }

  Fail:
    FT_ERROR(( "CopyArray: syntax error!\n" ));
    parser->error = T1_Err_Syntax_Error;
  }


/* END */