~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/3rdparty/freetype/src/psaux/psobjs.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************/
 
2
/*                                                                         */
 
3
/*  psobjs.c                                                               */
 
4
/*                                                                         */
 
5
/*    Auxiliary functions for PostScript fonts (body).                     */
 
6
/*                                                                         */
 
7
/*  Copyright 1996-2001, 2002, 2003, 2004 by                               */
 
8
/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
 
9
/*                                                                         */
 
10
/*  This file is part of the FreeType project, and may only be used,       */
 
11
/*  modified, and distributed under the terms of the FreeType project      */
 
12
/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
 
13
/*  this file you indicate that you have read the license and              */
 
14
/*  understand and accept it fully.                                        */
 
15
/*                                                                         */
 
16
/***************************************************************************/
 
17
 
 
18
 
 
19
#include <ft2build.h>
 
20
#include FT_INTERNAL_POSTSCRIPT_AUX_H
 
21
#include FT_INTERNAL_DEBUG_H
 
22
 
 
23
#include "psobjs.h"
 
24
 
 
25
#include "psauxerr.h"
 
26
 
 
27
 
 
28
  /*************************************************************************/
 
29
  /*************************************************************************/
 
30
  /*****                                                               *****/
 
31
  /*****                             PS_TABLE                          *****/
 
32
  /*****                                                               *****/
 
33
  /*************************************************************************/
 
34
  /*************************************************************************/
 
35
 
 
36
  /*************************************************************************/
 
37
  /*                                                                       */
 
38
  /* <Function>                                                            */
 
39
  /*    ps_table_new                                                       */
 
40
  /*                                                                       */
 
41
  /* <Description>                                                         */
 
42
  /*    Initializes a PS_Table.                                            */
 
43
  /*                                                                       */
 
44
  /* <InOut>                                                               */
 
45
  /*    table  :: The address of the target table.                         */
 
46
  /*                                                                       */
 
47
  /* <Input>                                                               */
 
48
  /*    count  :: The table size = the maximum number of elements.         */
 
49
  /*                                                                       */
 
50
  /*    memory :: The memory object to use for all subsequent              */
 
51
  /*              reallocations.                                           */
 
52
  /*                                                                       */
 
53
  /* <Return>                                                              */
 
54
  /*    FreeType error code.  0 means success.                             */
 
55
  /*                                                                       */
 
56
  FT_LOCAL_DEF( FT_Error )
 
57
  ps_table_new( PS_Table   table,
 
58
                FT_Int     count,
 
59
                FT_Memory  memory )
 
60
  {
 
61
    FT_Error  error;
 
62
 
 
63
 
 
64
    table->memory = memory;
 
65
    if ( FT_NEW_ARRAY( table->elements, count ) ||
 
66
         FT_NEW_ARRAY( table->lengths,  count ) )
 
67
      goto Exit;
 
68
 
 
69
    table->max_elems = count;
 
70
    table->init      = 0xDEADBEEFUL;
 
71
    table->num_elems = 0;
 
72
    table->block     = 0;
 
73
    table->capacity  = 0;
 
74
    table->cursor    = 0;
 
75
 
 
76
    *(PS_Table_FuncsRec*)&table->funcs = ps_table_funcs;
 
77
 
 
78
  Exit:
 
79
    if ( error )
 
80
      FT_FREE( table->elements );
 
81
 
 
82
    return error;
 
83
  }
 
84
 
 
85
 
 
86
  static void
 
87
  shift_elements( PS_Table  table,
 
88
                  FT_Byte*  old_base )
 
89
  {
 
90
    FT_Long    delta  = (FT_Long)( table->block - old_base );
 
91
    FT_Byte**  offset = table->elements;
 
92
    FT_Byte**  limit  = offset + table->max_elems;
 
93
 
 
94
 
 
95
    for ( ; offset < limit; offset++ )
 
96
    {
 
97
      if ( offset[0] )
 
98
        offset[0] += delta;
 
99
    }
 
100
  }
 
101
 
 
102
 
 
103
  static FT_Error
 
104
  reallocate_t1_table( PS_Table  table,
 
105
                       FT_Long   new_size )
 
106
  {
 
107
    FT_Memory  memory   = table->memory;
 
108
    FT_Byte*   old_base = table->block;
 
109
    FT_Error   error;
 
110
 
 
111
 
 
112
    /* allocate new base block */
 
113
    if ( FT_ALLOC( table->block, new_size ) )
 
114
    {
 
115
      table->block = old_base;
 
116
      return error;
 
117
    }
 
118
 
 
119
    /* copy elements and shift offsets */
 
120
    if (old_base )
 
121
    {
 
122
      FT_MEM_COPY( table->block, old_base, table->capacity );
 
123
      shift_elements( table, old_base );
 
124
      FT_FREE( old_base );
 
125
    }
 
126
 
 
127
    table->capacity = new_size;
 
128
 
 
129
    return PSaux_Err_Ok;
 
130
  }
 
131
 
 
132
 
 
133
  /*************************************************************************/
 
134
  /*                                                                       */
 
135
  /* <Function>                                                            */
 
136
  /*    ps_table_add                                                       */
 
137
  /*                                                                       */
 
138
  /* <Description>                                                         */
 
139
  /*    Adds an object to a PS_Table, possibly growing its memory block.   */
 
140
  /*                                                                       */
 
141
  /* <InOut>                                                               */
 
142
  /*    table  :: The target table.                                        */
 
143
  /*                                                                       */
 
144
  /* <Input>                                                               */
 
145
  /*    idx    :: The index of the object in the table.                    */
 
146
  /*                                                                       */
 
147
  /*    object :: The address of the object to copy in memory.             */
 
148
  /*                                                                       */
 
149
  /*    length :: The length in bytes of the source object.                */
 
150
  /*                                                                       */
 
151
  /* <Return>                                                              */
 
152
  /*    FreeType error code.  0 means success.  An error is returned if a  */
 
153
  /*    reallocation fails.                                                */
 
154
  /*                                                                       */
 
155
  FT_LOCAL_DEF( FT_Error )
 
156
  ps_table_add( PS_Table    table,
 
157
                FT_Int      idx,
 
158
                void*       object,
 
159
                FT_PtrDist  length )
 
160
  {
 
161
    if ( idx < 0 || idx > table->max_elems )
 
162
    {
 
163
      FT_ERROR(( "ps_table_add: invalid index\n" ));
 
164
      return PSaux_Err_Invalid_Argument;
 
165
    }
 
166
 
 
167
    /* grow the base block if needed */
 
168
    if ( table->cursor + length > table->capacity )
 
169
    {
 
170
      FT_Error   error;
 
171
      FT_Offset  new_size  = table->capacity;
 
172
      FT_Long    in_offset;
 
173
 
 
174
 
 
175
      in_offset = (FT_Long)((FT_Byte*)object - table->block);
 
176
      if ( (FT_ULong)in_offset >= table->capacity )
 
177
        in_offset = -1;
 
178
 
 
179
      while ( new_size < table->cursor + length )
 
180
      {
 
181
        /* increase size by 25% and round up to the nearest multiple
 
182
           of 1024 */
 
183
        new_size += ( new_size >> 2 ) + 1;
 
184
        new_size  = FT_PAD_CEIL( new_size, 1024 );
 
185
      }
 
186
 
 
187
      error = reallocate_t1_table( table, new_size );
 
188
      if ( error )
 
189
        return error;
 
190
 
 
191
      if ( in_offset >= 0 )
 
192
        object = table->block + in_offset;
 
193
    }
 
194
 
 
195
    /* add the object to the base block and adjust offset */
 
196
    table->elements[idx] = table->block + table->cursor;
 
197
    table->lengths [idx] = length;
 
198
    FT_MEM_COPY( table->block + table->cursor, object, length );
 
199
 
 
200
    table->cursor += length;
 
201
    return PSaux_Err_Ok;
 
202
  }
 
203
 
 
204
 
 
205
  /*************************************************************************/
 
206
  /*                                                                       */
 
207
  /* <Function>                                                            */
 
208
  /*    ps_table_done                                                      */
 
209
  /*                                                                       */
 
210
  /* <Description>                                                         */
 
211
  /*    Finalizes a PS_TableRec (i.e., reallocate it to its current        */
 
212
  /*    cursor).                                                           */
 
213
  /*                                                                       */
 
214
  /* <InOut>                                                               */
 
215
  /*    table :: The target table.                                         */
 
216
  /*                                                                       */
 
217
  /* <Note>                                                                */
 
218
  /*    This function does NOT release the heap's memory block.  It is up  */
 
219
  /*    to the caller to clean it, or reference it in its own structures.  */
 
220
  /*                                                                       */
 
221
  FT_LOCAL_DEF( void )
 
222
  ps_table_done( PS_Table  table )
 
223
  {
 
224
    FT_Memory  memory = table->memory;
 
225
    FT_Error   error;
 
226
    FT_Byte*   old_base = table->block;
 
227
 
 
228
 
 
229
    /* should never fail, because rec.cursor <= rec.size */
 
230
    if ( !old_base )
 
231
      return;
 
232
 
 
233
    if ( FT_ALLOC( table->block, table->cursor ) )
 
234
      return;
 
235
    FT_MEM_COPY( table->block, old_base, table->cursor );
 
236
    shift_elements( table, old_base );
 
237
 
 
238
    table->capacity = table->cursor;
 
239
    FT_FREE( old_base );
 
240
 
 
241
    FT_UNUSED( error );
 
242
  }
 
243
 
 
244
 
 
245
  FT_LOCAL_DEF( void )
 
246
  ps_table_release( PS_Table  table )
 
247
  {
 
248
    FT_Memory  memory = table->memory;
 
249
 
 
250
 
 
251
    if ( (FT_ULong)table->init == 0xDEADBEEFUL )
 
252
    {
 
253
      FT_FREE( table->block );
 
254
      FT_FREE( table->elements );
 
255
      FT_FREE( table->lengths );
 
256
      table->init = 0;
 
257
    }
 
258
  }
 
259
 
 
260
 
 
261
  /*************************************************************************/
 
262
  /*************************************************************************/
 
263
  /*****                                                               *****/
 
264
  /*****                            T1 PARSER                          *****/
 
265
  /*****                                                               *****/
 
266
  /*************************************************************************/
 
267
  /*************************************************************************/
 
268
 
 
269
  /* In the PostScript Language Reference Manual (PLRM) the following */
 
270
  /* characters are called `whitespace characters'.                   */
 
271
#define IS_T1_WHITESPACE( c )  ( (c) == ' '  || (c) == '\t' )
 
272
#define IS_T1_LINESPACE( c )   ( (c) == '\r' || (c) == '\n' || (c) == '\f' )
 
273
#define IS_T1_NULLSPACE( c )   ( (c) == '\0' )
 
274
 
 
275
  /* According to the PLRM all whitespace characters are equivalent, */
 
276
  /* except in comments and strings.                                 */
 
277
#define IS_T1_SPACE( c )  ( IS_T1_WHITESPACE( c ) || \
 
278
                            IS_T1_LINESPACE( c )  || \
 
279
                            IS_T1_NULLSPACE( c )  )
 
280
 
 
281
 
 
282
  /* The following array is used by various functions to quickly convert */
 
283
  /* digits (both decimal and non-decimal) into numbers.                 */
 
284
 
 
285
#if 'A' == 65
 
286
  /* ASCII */
 
287
 
 
288
  static const char  ft_char_table[128] =
 
289
  {
 
290
    /* 0x00 */
 
291
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 
292
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 
293
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 
294
     0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
 
295
    -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
 
296
    25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
 
297
    -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
 
298
    25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1,
 
299
  };
 
300
 
 
301
  /* no character >= 0x80 can represent a valid number */
 
302
#define OP  >=
 
303
 
 
304
#endif /* 'A' == 65 */
 
305
 
 
306
#if 'A' == 193
 
307
  /* EBCDIC */
 
308
 
 
309
  static const char  ft_char_table[128] =
 
310
  {
 
311
    /* 0x80 */
 
312
    -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, -1, -1, -1, -1, -1, -1,
 
313
    -1, 19, 20, 21, 22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1,
 
314
    -1, -1, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1,
 
315
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 
316
    -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, -1, -1, -1, -1, -1, -1,
 
317
    -1, 19, 20, 21, 22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1,
 
318
    -1, -1, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1,
 
319
     0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
 
320
  }
 
321
 
 
322
  /* no character < 0x80 can represent a valid number */
 
323
#define OP  <
 
324
 
 
325
#endif /* 'A' == 193 */
 
326
 
 
327
 
 
328
  /* first character must be already part of the comment */
 
329
 
 
330
  static void
 
331
  skip_comment( FT_Byte*  *acur,
 
332
                FT_Byte*   limit )
 
333
  {
 
334
    FT_Byte*  cur = *acur;
 
335
 
 
336
 
 
337
    while ( cur < limit )
 
338
    {
 
339
      if ( IS_T1_LINESPACE( *cur ) )
 
340
        break;
 
341
      cur++;
 
342
    }
 
343
 
 
344
    *acur = cur;
 
345
  }
 
346
 
 
347
 
 
348
  static void
 
349
  skip_spaces( FT_Byte*  *acur,
 
350
               FT_Byte*   limit )
 
351
  {
 
352
    FT_Byte*  cur = *acur;
 
353
 
 
354
 
 
355
    while ( cur < limit )
 
356
    {
 
357
      if ( !IS_T1_SPACE( *cur ) )
 
358
      {
 
359
        if ( *cur == '%' )
 
360
          /* According to the PLRM, a comment is equal to a space. */
 
361
          skip_comment( &cur, limit );
 
362
        else
 
363
          break;
 
364
      }
 
365
      cur++;
 
366
    }
 
367
 
 
368
    *acur = cur;
 
369
  }
 
370
 
 
371
 
 
372
  /* first character must be `(' */
 
373
 
 
374
  static void
 
375
  skip_literal_string( FT_Byte*  *acur,
 
376
                       FT_Byte*   limit )
 
377
  {
 
378
    FT_Byte*  cur   = *acur;
 
379
    FT_Int    embed = 0;
 
380
 
 
381
 
 
382
    while ( cur < limit )
 
383
    {
 
384
      if ( *cur == '\\' )
 
385
        cur++;
 
386
      else if ( *cur == '(' )
 
387
        embed++;
 
388
      else if ( *cur == ')' )
 
389
      {
 
390
        embed--;
 
391
        if ( embed == 0 )
 
392
        {
 
393
          cur++;
 
394
          break;
 
395
        }
 
396
      }
 
397
      cur++;
 
398
    }
 
399
 
 
400
    *acur = cur;
 
401
  }
 
402
 
 
403
 
 
404
  /* first character must be `<' */
 
405
 
 
406
  static void
 
407
  skip_string( PS_Parser  parser )
 
408
  {
 
409
    FT_Byte*  cur   = parser->cursor;
 
410
    FT_Byte*  limit = parser->limit;
 
411
 
 
412
 
 
413
    while ( ++cur < limit )
 
414
    {
 
415
      int  d;
 
416
 
 
417
 
 
418
      /* All whitespace characters are ignored. */
 
419
      skip_spaces( &cur, limit );
 
420
      if ( cur >= limit )
 
421
        break;
 
422
 
 
423
      if ( *cur OP 0x80 )
 
424
        break;
 
425
 
 
426
      d = ft_char_table[*cur & 0x7F];
 
427
      if ( d < 0 || d >= 16 )
 
428
        break;
 
429
    }
 
430
 
 
431
    if ( cur < limit && *cur != '>' )
 
432
      parser->error = PSaux_Err_Invalid_File_Format;
 
433
    else
 
434
      cur++;
 
435
 
 
436
    parser->cursor = cur;
 
437
  }
 
438
 
 
439
 
 
440
  /***********************************************************************/
 
441
  /*                                                                     */
 
442
  /* All exported parsing routines handle leading whitespace and stop at */
 
443
  /* the first character which isn't part of the just handled token.     */
 
444
  /*                                                                     */
 
445
  /***********************************************************************/
 
446
 
 
447
 
 
448
  FT_LOCAL_DEF( void )
 
449
  ps_parser_skip_PS_token( PS_Parser  parser )
 
450
  {
 
451
    /* Note: PostScript allows any non-delimiting, non-whitespace        */
 
452
    /*       character in a name (PS Ref Manual, 3rd ed, p31).           */
 
453
    /*       PostScript delimiters are (, ), <, >, [, ], {, }, /, and %. */
 
454
 
 
455
    FT_Byte*  cur   = parser->cursor;
 
456
    FT_Byte*  limit = parser->limit;
 
457
 
 
458
 
 
459
    skip_spaces( &cur, limit );             /* this also skips comments */
 
460
    if ( cur >= limit )
 
461
      goto Exit;
 
462
 
 
463
    /* self-delimiting, single-character tokens */
 
464
    if ( *cur == '[' || *cur == ']' ||
 
465
         *cur == '{' || *cur == '}' )
 
466
    {
 
467
      cur++;
 
468
      goto Exit;
 
469
    }
 
470
 
 
471
    if ( *cur == '(' )                              /* (...) */
 
472
    {
 
473
      skip_literal_string( &cur, limit );
 
474
      goto Exit;
 
475
    }
 
476
 
 
477
    if ( *cur == '<' )                              /* <...> */
 
478
    {
 
479
      if ( cur + 1 < limit && *(cur + 1) == '<' )   /* << */
 
480
      {
 
481
        cur++;
 
482
        cur++;
 
483
        goto Exit;
 
484
      }
 
485
      parser->cursor = cur;
 
486
      skip_string( parser );
 
487
      return;
 
488
    }
 
489
 
 
490
    if ( *cur == '>' )
 
491
    {
 
492
      cur++;
 
493
      if ( cur >= limit || *cur != '>' )             /* >> */
 
494
      {
 
495
        parser->error = PSaux_Err_Invalid_File_Format;
 
496
        goto Exit;
 
497
      }
 
498
      cur++;
 
499
      goto Exit;
 
500
    }
 
501
 
 
502
    if ( *cur == '/' )
 
503
      cur++;
 
504
 
 
505
    /* anything else */
 
506
    while ( cur < limit )
 
507
    {
 
508
      if ( IS_T1_SPACE( *cur )        ||
 
509
           *cur == '('                ||
 
510
           *cur == '/'                ||
 
511
           *cur == '%'                ||
 
512
           *cur == '[' || *cur == ']' ||
 
513
           *cur == '{' || *cur == '}' ||
 
514
           *cur == '<' || *cur == '>' )
 
515
        break;
 
516
 
 
517
      if ( *cur == ')' )
 
518
      {
 
519
        parser->error = PSaux_Err_Invalid_File_Format;
 
520
        goto Exit;
 
521
      }
 
522
 
 
523
      cur++;
 
524
    }
 
525
 
 
526
  Exit:
 
527
    parser->cursor = cur;
 
528
  }
 
529
 
 
530
 
 
531
  FT_LOCAL_DEF( void )
 
532
  ps_parser_skip_spaces( PS_Parser  parser )
 
533
  {
 
534
    skip_spaces( &parser->cursor, parser->limit );
 
535
  }
 
536
 
 
537
 
 
538
  /* `token' here means either something between balanced delimiters */
 
539
  /* or the next token; the delimiters are not removed.              */
 
540
 
 
541
  FT_LOCAL_DEF( void )
 
542
  ps_parser_to_token( PS_Parser  parser,
 
543
                      T1_Token   token )
 
544
  {
 
545
    FT_Byte*  cur;
 
546
    FT_Byte*  limit;
 
547
    FT_Byte   starter, ender;
 
548
    FT_Int    embed;
 
549
 
 
550
 
 
551
    token->type  = T1_TOKEN_TYPE_NONE;
 
552
    token->start = 0;
 
553
    token->limit = 0;
 
554
 
 
555
    /* first of all, skip leading whitespace */
 
556
    ps_parser_skip_spaces( parser );
 
557
 
 
558
    cur   = parser->cursor;
 
559
    limit = parser->limit;
 
560
 
 
561
    if ( cur >= limit )
 
562
      return;
 
563
 
 
564
    switch ( *cur )
 
565
    {
 
566
      /************* check for literal string *****************/
 
567
    case '(':
 
568
      token->type  = T1_TOKEN_TYPE_STRING;
 
569
      token->start = cur;
 
570
      skip_literal_string( &cur, limit );
 
571
      if ( cur < limit )
 
572
        token->limit = cur;
 
573
      break;
 
574
 
 
575
      /************* check for programs/array *****************/
 
576
    case '{':
 
577
      token->type = T1_TOKEN_TYPE_ARRAY;
 
578
      ender = '}';
 
579
      goto Lookup_Ender;
 
580
 
 
581
      /************* check for table/array ********************/
 
582
    case '[':
 
583
      token->type = T1_TOKEN_TYPE_ARRAY;
 
584
      ender = ']';
 
585
      /* fall through */
 
586
 
 
587
    Lookup_Ender:
 
588
      embed        = 1;
 
589
      starter      = *cur;
 
590
      token->start = cur++;
 
591
 
 
592
      /* we need this to catch `[ ]' */
 
593
      parser->cursor = cur;
 
594
      ps_parser_skip_spaces( parser );
 
595
      cur = parser->cursor;
 
596
 
 
597
      while ( cur < limit && !parser->error )
 
598
      {
 
599
        if ( *cur == starter )
 
600
          embed++;
 
601
        else if ( *cur == ender )
 
602
        {
 
603
          embed--;
 
604
          if ( embed <= 0 )
 
605
          {
 
606
            token->limit = ++cur;
 
607
            break;
 
608
          }
 
609
        }
 
610
 
 
611
        parser->cursor = cur;
 
612
        ps_parser_skip_PS_token( parser );
 
613
        /* we need this to catch `[XXX ]' */
 
614
        ps_parser_skip_spaces  ( parser );
 
615
        cur = parser->cursor;
 
616
      }
 
617
      break;
 
618
 
 
619
      /* ************ otherwise, it is any token **************/
 
620
    default:
 
621
      token->start = cur;
 
622
      token->type  = T1_TOKEN_TYPE_ANY;
 
623
      ps_parser_skip_PS_token( parser );
 
624
      cur = parser->cursor;
 
625
      if ( !parser->error )
 
626
        token->limit = cur;
 
627
    }
 
628
 
 
629
    if ( !token->limit )
 
630
    {
 
631
      token->start = 0;
 
632
      token->type  = T1_TOKEN_TYPE_NONE;
 
633
    }
 
634
 
 
635
    parser->cursor = cur;
 
636
  }
 
637
 
 
638
 
 
639
  FT_LOCAL_DEF( void )
 
640
  ps_parser_to_token_array( PS_Parser  parser,
 
641
                            T1_Token   tokens,
 
642
                            FT_UInt    max_tokens,
 
643
                            FT_Int*    pnum_tokens )
 
644
  {
 
645
    T1_TokenRec  master;
 
646
 
 
647
 
 
648
    *pnum_tokens = -1;
 
649
 
 
650
    /* this also handles leading whitespace */
 
651
    ps_parser_to_token( parser, &master );
 
652
 
 
653
    if ( master.type == T1_TOKEN_TYPE_ARRAY )
 
654
    {
 
655
      FT_Byte*  old_cursor = parser->cursor;
 
656
      FT_Byte*  old_limit  = parser->limit;
 
657
      T1_Token  cur        = tokens;
 
658
      T1_Token  limit      = cur + max_tokens;
 
659
 
 
660
 
 
661
      /* don't include outermost delimiters */
 
662
      parser->cursor = master.start + 1;
 
663
      parser->limit  = master.limit - 1;
 
664
 
 
665
      while ( parser->cursor < parser->limit )
 
666
      {
 
667
        T1_TokenRec  token;
 
668
 
 
669
 
 
670
        ps_parser_to_token( parser, &token );
 
671
        if ( !token.type )
 
672
          break;
 
673
 
 
674
        if ( cur < limit )
 
675
          *cur = token;
 
676
 
 
677
        cur++;
 
678
      }
 
679
 
 
680
      *pnum_tokens = (FT_Int)( cur - tokens );
 
681
 
 
682
      parser->cursor = old_cursor;
 
683
      parser->limit  = old_limit;
 
684
    }
 
685
  }
 
686
 
 
687
 
 
688
  /* first character must be already part of the number */
 
689
 
 
690
  static FT_Long
 
691
  ps_radix( FT_Long    radixBase,
 
692
            FT_Byte*  *acur,
 
693
            FT_Byte*   limit )
 
694
  {
 
695
    FT_Long   result = 0;
 
696
    FT_Byte*  cur    = *acur;
 
697
 
 
698
 
 
699
    if ( radixBase < 2 || radixBase > 36 )
 
700
      return 0;
 
701
 
 
702
    while ( cur < limit )
 
703
    {
 
704
      int  d;
 
705
 
 
706
 
 
707
      if ( *cur OP 0x80 )
 
708
        break;
 
709
 
 
710
      d = ft_char_table[*cur & 0x7F];
 
711
      if ( d < 0 || d >= radixBase )
 
712
        break;
 
713
 
 
714
      result = result * radixBase + d;
 
715
 
 
716
      cur++;
 
717
    }
 
718
 
 
719
    *acur = cur;
 
720
 
 
721
    return result;
 
722
  }
 
723
 
 
724
 
 
725
  /* first character must be already part of the number */
 
726
 
 
727
  static FT_Long
 
728
  ps_toint( FT_Byte*  *acur,
 
729
            FT_Byte*   limit )
 
730
  {
 
731
    FT_Long   result = 0;
 
732
    FT_Byte*  cur    = *acur;
 
733
    FT_Byte   c;
 
734
 
 
735
 
 
736
    if ( cur >= limit )
 
737
      goto Exit;
 
738
 
 
739
    c = *cur;
 
740
    if ( c == '-' )
 
741
      cur++;
 
742
 
 
743
    while ( cur < limit )
 
744
    {
 
745
      int  d;
 
746
 
 
747
 
 
748
      if ( *cur == '#' )
 
749
      {
 
750
        cur++;
 
751
        result = ps_radix( result, &cur, limit );
 
752
        break;
 
753
      }
 
754
 
 
755
      if ( *cur OP 0x80 )
 
756
        break;
 
757
 
 
758
      d = ft_char_table[*cur & 0x7F];
 
759
      if ( d < 0 || d >= 10 )
 
760
        break;
 
761
      result = result * 10 + d;
 
762
 
 
763
      cur++;
 
764
    };
 
765
 
 
766
    if ( c == '-' )
 
767
      result = -result;
 
768
 
 
769
  Exit:
 
770
    *acur = cur;
 
771
    return result;
 
772
  }
 
773
 
 
774
 
 
775
  /* first character must be `<' if `delimiters' is non-zero */
 
776
 
 
777
  static FT_Error
 
778
  ps_tobytes( FT_Byte*  *acur,
 
779
              FT_Byte*   limit,
 
780
              FT_Long    max_bytes,
 
781
              FT_Byte*   bytes,
 
782
              FT_Long*   pnum_bytes,
 
783
              FT_Bool    delimiters )
 
784
  {
 
785
    FT_Error  error = PSaux_Err_Ok;
 
786
 
 
787
    FT_Byte*  cur = *acur;
 
788
    FT_Long   n   = 0;
 
789
 
 
790
 
 
791
    if ( cur >= limit )
 
792
      goto Exit;
 
793
 
 
794
    if ( delimiters )
 
795
    {
 
796
      if ( *cur != '<' )
 
797
      {
 
798
        error = PSaux_Err_Invalid_File_Format;
 
799
        goto Exit;
 
800
      }
 
801
 
 
802
      cur++;
 
803
    }
 
804
 
 
805
    max_bytes = max_bytes * 2;
 
806
 
 
807
    for ( n = 0; cur < limit; n++, cur++ )
 
808
    {
 
809
      int  d;
 
810
 
 
811
 
 
812
      if ( n >= max_bytes )
 
813
        /* buffer is full */
 
814
        goto Exit;
 
815
 
 
816
      /* All whitespace characters are ignored. */
 
817
      skip_spaces( &cur, limit );
 
818
      if ( cur >= limit )
 
819
        break;
 
820
 
 
821
      if ( *cur OP 0x80 )
 
822
        break;
 
823
 
 
824
      d = ft_char_table[*cur & 0x7F];
 
825
      if ( d < 0 || d >= 16 )
 
826
        break;
 
827
 
 
828
      /* <f> == <f0> != <0f> */
 
829
      bytes[n / 2] = (FT_Byte)( ( n % 2 ) ? bytes[n / 2] + d
 
830
                                          : d * 16 );
 
831
    }
 
832
 
 
833
    if ( delimiters )
 
834
    {
 
835
      if ( cur < limit && *cur != '>' )
 
836
      {
 
837
        error = PSaux_Err_Invalid_File_Format;
 
838
        goto Exit;
 
839
      }
 
840
 
 
841
      cur++;
 
842
    }
 
843
 
 
844
    *acur = cur;
 
845
 
 
846
  Exit:
 
847
    *pnum_bytes = ( n + 1 ) / 2;
 
848
 
 
849
    return error;
 
850
  }
 
851
 
 
852
 
 
853
  /* first character must be already part of the number */
 
854
 
 
855
  static FT_Long
 
856
  ps_tofixed( FT_Byte*  *acur,
 
857
              FT_Byte*   limit,
 
858
              FT_Long    power_ten )
 
859
  {
 
860
    FT_Byte*  cur  = *acur;
 
861
    FT_Long   num, divider, result;
 
862
    FT_Int    sign = 0;
 
863
 
 
864
 
 
865
    if ( cur >= limit )
 
866
      return 0;
 
867
 
 
868
    /* first of all, check the sign */
 
869
    if ( *cur == '-' && cur + 1 < limit )
 
870
    {
 
871
      sign = 1;
 
872
      cur++;
 
873
    }
 
874
 
 
875
    /* then, read the integer part, if any */
 
876
    if ( *cur != '.' )
 
877
      result = ps_toint( &cur, limit ) << 16;
 
878
    else
 
879
      result = 0;
 
880
 
 
881
    num     = 0;
 
882
    divider = 1;
 
883
 
 
884
    if ( cur >= limit )
 
885
      goto Exit;
 
886
 
 
887
    /* read decimal part, if any */
 
888
    if ( *cur == '.' && cur + 1 < limit )
 
889
    {
 
890
      cur++;
 
891
 
 
892
      for (;;)
 
893
      {
 
894
        int  d;
 
895
 
 
896
 
 
897
        if ( *cur OP 0x80 )
 
898
          break;
 
899
 
 
900
        d = ft_char_table[*cur & 0x7F];
 
901
        if ( d < 0 || d >= 10 )
 
902
          break;
 
903
 
 
904
        if ( divider < 10000000L )
 
905
        {
 
906
          num      = num * 10 + d;
 
907
          divider *= 10;
 
908
        }
 
909
 
 
910
        cur++;
 
911
        if ( cur >= limit )
 
912
          break;
 
913
      }
 
914
    }
 
915
 
 
916
    /* read exponent, if any */
 
917
    if ( cur + 1 < limit && ( *cur == 'e' || *cur == 'E' ) )
 
918
    {
 
919
      cur++;
 
920
      power_ten += ps_toint( &cur, limit );
 
921
    }
 
922
 
 
923
  Exit:
 
924
    /* raise to power of ten if needed */
 
925
    while ( power_ten > 0 )
 
926
    {
 
927
      result = result * 10;
 
928
      num    = num * 10;
 
929
      power_ten--;
 
930
    }
 
931
 
 
932
    while ( power_ten < 0 )
 
933
    {
 
934
      result  = result / 10;
 
935
      divider = divider * 10;
 
936
      power_ten++;
 
937
    }
 
938
 
 
939
    if ( num )
 
940
      result += FT_DivFix( num, divider );
 
941
 
 
942
    if ( sign )
 
943
      result = -result;
 
944
 
 
945
    *acur = cur;
 
946
    return result;
 
947
  }
 
948
 
 
949
 
 
950
  /* first character must be a delimiter or a part of a number */
 
951
 
 
952
  static FT_Int
 
953
  ps_tocoordarray( FT_Byte*  *acur,
 
954
                   FT_Byte*   limit,
 
955
                   FT_Int     max_coords,
 
956
                   FT_Short*  coords )
 
957
  {
 
958
    FT_Byte*  cur   = *acur;
 
959
    FT_Int    count = 0;
 
960
    FT_Byte   c, ender;
 
961
 
 
962
 
 
963
    if ( cur >= limit )
 
964
      goto Exit;
 
965
 
 
966
    /* check for the beginning of an array; otherwise, only one number */
 
967
    /* will be read                                                    */
 
968
    c     = *cur;
 
969
    ender = 0;
 
970
 
 
971
    if ( c == '[' )
 
972
      ender = ']';
 
973
 
 
974
    if ( c == '{' )
 
975
      ender = '}';
 
976
 
 
977
    if ( ender )
 
978
      cur++;
 
979
 
 
980
    /* now, read the coordinates */
 
981
    while ( cur < limit )
 
982
    {
 
983
      /* skip whitespace in front of data */
 
984
      skip_spaces( &cur, limit );
 
985
      if ( cur >= limit )
 
986
        goto Exit;
 
987
 
 
988
      if ( count >= max_coords )
 
989
        break;
 
990
 
 
991
      if ( c == ender )
 
992
      {
 
993
        cur++;
 
994
        break;
 
995
      }
 
996
 
 
997
      coords[count] = (FT_Short)( ps_tofixed( &cur, limit, 0 ) >> 16 );
 
998
      count++;
 
999
 
 
1000
      if ( !ender )
 
1001
        break;
 
1002
    }
 
1003
 
 
1004
  Exit:
 
1005
    *acur = cur;
 
1006
    return count;
 
1007
  }
 
1008
 
 
1009
 
 
1010
  /* first character must be a delimiter or a part of a number */
 
1011
 
 
1012
  static FT_Int
 
1013
  ps_tofixedarray( FT_Byte*  *acur,
 
1014
                   FT_Byte*   limit,
 
1015
                   FT_Int     max_values,
 
1016
                   FT_Fixed*  values,
 
1017
                   FT_Int     power_ten )
 
1018
  {
 
1019
    FT_Byte*  cur   = *acur;
 
1020
    FT_Int    count = 0;
 
1021
    FT_Byte   c, ender;
 
1022
 
 
1023
 
 
1024
    if ( cur >= limit )
 
1025
      goto Exit;
 
1026
 
 
1027
    /* Check for the beginning of an array.  Otherwise, only one number */
 
1028
    /* will be read.                                                    */
 
1029
    c     = *cur;
 
1030
    ender = 0;
 
1031
 
 
1032
    if ( c == '[' )
 
1033
      ender = ']';
 
1034
 
 
1035
    if ( c == '{' )
 
1036
      ender = '}';
 
1037
 
 
1038
    if ( ender )
 
1039
      cur++;
 
1040
 
 
1041
    /* now, read the values */
 
1042
    while ( cur < limit )
 
1043
    {
 
1044
      /* skip whitespace in front of data */
 
1045
      skip_spaces( &cur, limit );
 
1046
      if ( cur >= limit )
 
1047
        goto Exit;
 
1048
 
 
1049
      if ( count >= max_values )
 
1050
        break;
 
1051
 
 
1052
      if ( c == ender )
 
1053
      {
 
1054
        cur++;
 
1055
        break;
 
1056
      }
 
1057
 
 
1058
      values[count] = ps_tofixed( &cur, limit, power_ten );
 
1059
      count++;
 
1060
 
 
1061
      if ( !ender )
 
1062
        break;
 
1063
    }
 
1064
 
 
1065
  Exit:
 
1066
    *acur = cur;
 
1067
    return count;
 
1068
  }
 
1069
 
 
1070
 
 
1071
#if 0
 
1072
 
 
1073
  static FT_String*
 
1074
  ps_tostring( FT_Byte**  cursor,
 
1075
               FT_Byte*   limit,
 
1076
               FT_Memory  memory )
 
1077
  {
 
1078
    FT_Byte*    cur = *cursor;
 
1079
    FT_PtrDist  len = 0;
 
1080
    FT_Int      count;
 
1081
    FT_String*  result;
 
1082
    FT_Error    error;
 
1083
 
 
1084
 
 
1085
    /* XXX: some stupid fonts have a `Notice' or `Copyright' string     */
 
1086
    /*      that simply doesn't begin with an opening parenthesis, even */
 
1087
    /*      though they have a closing one!  E.g. "amuncial.pfb"        */
 
1088
    /*                                                                  */
 
1089
    /*      We must deal with these ill-fated cases there.  Note that   */
 
1090
    /*      these fonts didn't work with the old Type 1 driver as the   */
 
1091
    /*      notice/copyright was not recognized as a valid string token */
 
1092
    /*      and made the old token parser commit errors.                */
 
1093
 
 
1094
    while ( cur < limit && ( *cur == ' ' || *cur == '\t' ) )
 
1095
      cur++;
 
1096
    if ( cur + 1 >= limit )
 
1097
      return 0;
 
1098
 
 
1099
    if ( *cur == '(' )
 
1100
      cur++;  /* skip the opening parenthesis, if there is one */
 
1101
 
 
1102
    *cursor = cur;
 
1103
    count   = 0;
 
1104
 
 
1105
    /* then, count its length */
 
1106
    for ( ; cur < limit; cur++ )
 
1107
    {
 
1108
      if ( *cur == '(' )
 
1109
        count++;
 
1110
 
 
1111
      else if ( *cur == ')' )
 
1112
      {
 
1113
        count--;
 
1114
        if ( count < 0 )
 
1115
          break;
 
1116
      }
 
1117
    }
 
1118
 
 
1119
    len = cur - *cursor;
 
1120
    if ( cur >= limit || FT_ALLOC( result, len + 1 ) )
 
1121
      return 0;
 
1122
 
 
1123
    /* now copy the string */
 
1124
    FT_MEM_COPY( result, *cursor, len );
 
1125
    result[len] = '\0';
 
1126
    *cursor = cur;
 
1127
    return result;
 
1128
  }
 
1129
 
 
1130
#endif /* 0 */
 
1131
 
 
1132
 
 
1133
  static int
 
1134
  ps_tobool( FT_Byte*  *acur,
 
1135
             FT_Byte*   limit )
 
1136
  {
 
1137
    FT_Byte*  cur    = *acur;
 
1138
    FT_Bool   result = 0;
 
1139
 
 
1140
 
 
1141
    /* return 1 if we find `true', 0 otherwise */
 
1142
    if ( cur + 3 < limit &&
 
1143
         cur[0] == 't'   &&
 
1144
         cur[1] == 'r'   &&
 
1145
         cur[2] == 'u'   &&
 
1146
         cur[3] == 'e'   )
 
1147
    {
 
1148
      result = 1;
 
1149
      cur   += 5;
 
1150
    }
 
1151
    else if ( cur + 4 < limit &&
 
1152
              cur[0] == 'f'   &&
 
1153
              cur[1] == 'a'   &&
 
1154
              cur[2] == 'l'   &&
 
1155
              cur[3] == 's'   &&
 
1156
              cur[4] == 'e'   )
 
1157
    {
 
1158
      result = 0;
 
1159
      cur   += 6;
 
1160
    }
 
1161
 
 
1162
    *acur = cur;
 
1163
    return result;
 
1164
  }
 
1165
 
 
1166
 
 
1167
  /* load a simple field (i.e. non-table) into the current list of objects */
 
1168
 
 
1169
  FT_LOCAL_DEF( FT_Error )
 
1170
  ps_parser_load_field( PS_Parser       parser,
 
1171
                        const T1_Field  field,
 
1172
                        void**          objects,
 
1173
                        FT_UInt         max_objects,
 
1174
                        FT_ULong*       pflags )
 
1175
  {
 
1176
    T1_TokenRec  token;
 
1177
    FT_Byte*     cur;
 
1178
    FT_Byte*     limit;
 
1179
    FT_UInt      count;
 
1180
    FT_UInt      idx;
 
1181
    FT_Error     error;
 
1182
 
 
1183
 
 
1184
    /* this also skips leading whitespace */
 
1185
    ps_parser_to_token( parser, &token );
 
1186
    if ( !token.type )
 
1187
      goto Fail;
 
1188
 
 
1189
    count = 1;
 
1190
    idx   = 0;
 
1191
    cur   = token.start;
 
1192
    limit = token.limit;
 
1193
 
 
1194
    /* we must detect arrays in /FontBBox */
 
1195
    if ( field->type == T1_FIELD_TYPE_BBOX )
 
1196
    {
 
1197
      T1_TokenRec  token2;
 
1198
      FT_Byte*     old_cur   = parser->cursor;
 
1199
      FT_Byte*     old_limit = parser->limit;
 
1200
 
 
1201
 
 
1202
      /* don't include delimiters */
 
1203
      parser->cursor = token.start + 1;
 
1204
      parser->limit  = token.limit - 1;
 
1205
 
 
1206
      ps_parser_to_token( parser, &token2 );
 
1207
      parser->cursor = old_cur;
 
1208
      parser->limit  = old_limit;
 
1209
 
 
1210
      if ( token2.type == T1_TOKEN_TYPE_ARRAY )
 
1211
        goto FieldArray;
 
1212
    }
 
1213
    else if ( token.type == T1_TOKEN_TYPE_ARRAY )
 
1214
    {
 
1215
    FieldArray:
 
1216
      /* if this is an array and we have no blend, an error occurs */
 
1217
      if ( max_objects == 0 )
 
1218
        goto Fail;
 
1219
 
 
1220
      count = max_objects;
 
1221
      idx   = 1;
 
1222
 
 
1223
      /* don't include delimiters */
 
1224
      cur++;
 
1225
      limit--;
 
1226
    }
 
1227
 
 
1228
    for ( ; count > 0; count--, idx++ )
 
1229
    {
 
1230
      FT_Byte*    q = (FT_Byte*)objects[idx] + field->offset;
 
1231
      FT_Long     val;
 
1232
      FT_String*  string;
 
1233
 
 
1234
 
 
1235
      skip_spaces( &cur, limit );
 
1236
 
 
1237
      switch ( field->type )
 
1238
      {
 
1239
      case T1_FIELD_TYPE_BOOL:
 
1240
        val = ps_tobool( &cur, limit );
 
1241
        goto Store_Integer;
 
1242
 
 
1243
      case T1_FIELD_TYPE_FIXED:
 
1244
        val = ps_tofixed( &cur, limit, 0 );
 
1245
        goto Store_Integer;
 
1246
 
 
1247
      case T1_FIELD_TYPE_FIXED_1000:
 
1248
        val = ps_tofixed( &cur, limit, 3 );
 
1249
        goto Store_Integer;
 
1250
 
 
1251
      case T1_FIELD_TYPE_INTEGER:
 
1252
        val = ps_toint( &cur, limit );
 
1253
        /* fall through */
 
1254
 
 
1255
      Store_Integer:
 
1256
        switch ( field->size )
 
1257
        {
 
1258
        case (8 / FT_CHAR_BIT):
 
1259
          *(FT_Byte*)q = (FT_Byte)val;
 
1260
          break;
 
1261
 
 
1262
        case (16 / FT_CHAR_BIT):
 
1263
          *(FT_UShort*)q = (FT_UShort)val;
 
1264
          break;
 
1265
 
 
1266
        case (32 / FT_CHAR_BIT):
 
1267
          *(FT_UInt32*)q = (FT_UInt32)val;
 
1268
          break;
 
1269
 
 
1270
        default:                /* for 64-bit systems */
 
1271
          *(FT_Long*)q = val;
 
1272
        }
 
1273
        break;
 
1274
 
 
1275
      case T1_FIELD_TYPE_STRING:
 
1276
      case T1_FIELD_TYPE_KEY:
 
1277
        {
 
1278
          FT_Memory  memory = parser->memory;
 
1279
          FT_UInt    len    = (FT_UInt)( limit - cur );
 
1280
 
 
1281
 
 
1282
          if ( cur >= limit )
 
1283
            break;
 
1284
 
 
1285
          if ( field->type == T1_FIELD_TYPE_KEY )
 
1286
          {
 
1287
            /* don't include leading `/' */
 
1288
            len--;
 
1289
            cur++;
 
1290
          }
 
1291
          else
 
1292
          {
 
1293
            /* don't include delimiting parentheses */
 
1294
            cur++;
 
1295
            len -= 2;
 
1296
          }
 
1297
 
 
1298
          if ( FT_ALLOC( string, len + 1 ) )
 
1299
            goto Exit;
 
1300
 
 
1301
          FT_MEM_COPY( string, cur, len );
 
1302
          string[len] = 0;
 
1303
 
 
1304
          *(FT_String**)q = string;
 
1305
        }
 
1306
        break;
 
1307
 
 
1308
      case T1_FIELD_TYPE_BBOX:
 
1309
        {
 
1310
          FT_Fixed  temp[4];
 
1311
          FT_BBox*  bbox = (FT_BBox*)q;
 
1312
 
 
1313
 
 
1314
          (void)ps_tofixedarray( &token.start, token.limit, 4, temp, 0 );
 
1315
 
 
1316
          bbox->xMin = FT_RoundFix( temp[0] );
 
1317
          bbox->yMin = FT_RoundFix( temp[1] );
 
1318
          bbox->xMax = FT_RoundFix( temp[2] );
 
1319
          bbox->yMax = FT_RoundFix( temp[3] );
 
1320
        }
 
1321
        break;
 
1322
 
 
1323
      default:
 
1324
        /* an error occured */
 
1325
        goto Fail;
 
1326
      }
 
1327
    }
 
1328
 
 
1329
#if 0  /* obsolete -- keep for reference */
 
1330
    if ( pflags )
 
1331
      *pflags |= 1L << field->flag_bit;
 
1332
#else
 
1333
    FT_UNUSED( pflags );
 
1334
#endif
 
1335
 
 
1336
    error = PSaux_Err_Ok;
 
1337
 
 
1338
  Exit:
 
1339
    return error;
 
1340
 
 
1341
  Fail:
 
1342
    error = PSaux_Err_Invalid_File_Format;
 
1343
    goto Exit;
 
1344
  }
 
1345
 
 
1346
 
 
1347
#define T1_MAX_TABLE_ELEMENTS  32
 
1348
 
 
1349
 
 
1350
  FT_LOCAL_DEF( FT_Error )
 
1351
  ps_parser_load_field_table( PS_Parser       parser,
 
1352
                              const T1_Field  field,
 
1353
                              void**          objects,
 
1354
                              FT_UInt         max_objects,
 
1355
                              FT_ULong*       pflags )
 
1356
  {
 
1357
    T1_TokenRec  elements[T1_MAX_TABLE_ELEMENTS];
 
1358
    T1_Token     token;
 
1359
    FT_Int       num_elements;
 
1360
    FT_Error     error = PSaux_Err_Ok;
 
1361
    FT_Byte*     old_cursor;
 
1362
    FT_Byte*     old_limit;
 
1363
    T1_FieldRec  fieldrec = *(T1_Field)field;
 
1364
 
 
1365
 
 
1366
#if 1
 
1367
    fieldrec.type = T1_FIELD_TYPE_INTEGER;
 
1368
    if ( field->type == T1_FIELD_TYPE_FIXED_ARRAY )
 
1369
      fieldrec.type = T1_FIELD_TYPE_FIXED;
 
1370
#endif
 
1371
 
 
1372
    ps_parser_to_token_array( parser, elements,
 
1373
                              T1_MAX_TABLE_ELEMENTS, &num_elements );
 
1374
    if ( num_elements < 0 )
 
1375
    {
 
1376
      error = PSaux_Err_Ignore;
 
1377
      goto Exit;
 
1378
    }
 
1379
    if ( num_elements > T1_MAX_TABLE_ELEMENTS )
 
1380
      num_elements = T1_MAX_TABLE_ELEMENTS;
 
1381
 
 
1382
    old_cursor = parser->cursor;
 
1383
    old_limit  = parser->limit;
 
1384
 
 
1385
    /* we store the elements count */
 
1386
    *(FT_Byte*)( (FT_Byte*)objects[0] + field->count_offset ) =
 
1387
      (FT_Byte)num_elements;
 
1388
 
 
1389
    /* we now load each element, adjusting the field.offset on each one */
 
1390
    token = elements;
 
1391
    for ( ; num_elements > 0; num_elements--, token++ )
 
1392
    {
 
1393
      parser->cursor = token->start;
 
1394
      parser->limit  = token->limit;
 
1395
      ps_parser_load_field( parser, &fieldrec, objects, max_objects, 0 );
 
1396
      fieldrec.offset += fieldrec.size;
 
1397
    }
 
1398
 
 
1399
#if 0  /* obsolete -- keep for reference */
 
1400
    if ( pflags )
 
1401
      *pflags |= 1L << field->flag_bit;
 
1402
#else
 
1403
    FT_UNUSED( pflags );
 
1404
#endif
 
1405
 
 
1406
    parser->cursor = old_cursor;
 
1407
    parser->limit  = old_limit;
 
1408
 
 
1409
  Exit:
 
1410
    return error;
 
1411
  }
 
1412
 
 
1413
 
 
1414
  FT_LOCAL_DEF( FT_Long )
 
1415
  ps_parser_to_int( PS_Parser  parser )
 
1416
  {
 
1417
    ps_parser_skip_spaces( parser );
 
1418
    return ps_toint( &parser->cursor, parser->limit );
 
1419
  }
 
1420
 
 
1421
 
 
1422
  FT_LOCAL_DEF( FT_Error )
 
1423
  ps_parser_to_bytes( PS_Parser  parser,
 
1424
                      FT_Byte*   bytes,
 
1425
                      FT_Long    max_bytes,
 
1426
                      FT_Long*   pnum_bytes,
 
1427
                      FT_Bool    delimiters )
 
1428
  {
 
1429
    ps_parser_skip_spaces( parser );
 
1430
    return ps_tobytes( &parser->cursor,
 
1431
                       parser->limit,
 
1432
                       max_bytes,
 
1433
                       bytes,
 
1434
                       pnum_bytes,
 
1435
                       delimiters );
 
1436
  }
 
1437
 
 
1438
 
 
1439
  FT_LOCAL_DEF( FT_Fixed )
 
1440
  ps_parser_to_fixed( PS_Parser  parser,
 
1441
                      FT_Int     power_ten )
 
1442
  {
 
1443
    ps_parser_skip_spaces( parser );
 
1444
    return ps_tofixed( &parser->cursor, parser->limit, power_ten );
 
1445
  }
 
1446
 
 
1447
 
 
1448
  FT_LOCAL_DEF( FT_Int )
 
1449
  ps_parser_to_coord_array( PS_Parser  parser,
 
1450
                            FT_Int     max_coords,
 
1451
                            FT_Short*  coords )
 
1452
  {
 
1453
    ps_parser_skip_spaces( parser );
 
1454
    return ps_tocoordarray( &parser->cursor, parser->limit,
 
1455
                            max_coords, coords );
 
1456
  }
 
1457
 
 
1458
 
 
1459
  FT_LOCAL_DEF( FT_Int )
 
1460
  ps_parser_to_fixed_array( PS_Parser  parser,
 
1461
                            FT_Int     max_values,
 
1462
                            FT_Fixed*  values,
 
1463
                            FT_Int     power_ten )
 
1464
  {
 
1465
    ps_parser_skip_spaces( parser );
 
1466
    return ps_tofixedarray( &parser->cursor, parser->limit,
 
1467
                            max_values, values, power_ten );
 
1468
  }
 
1469
 
 
1470
 
 
1471
#if 0
 
1472
 
 
1473
  FT_LOCAL_DEF( FT_String* )
 
1474
  T1_ToString( PS_Parser  parser )
 
1475
  {
 
1476
    return ps_tostring( &parser->cursor, parser->limit, parser->memory );
 
1477
  }
 
1478
 
 
1479
 
 
1480
  FT_LOCAL_DEF( FT_Bool )
 
1481
  T1_ToBool( PS_Parser  parser )
 
1482
  {
 
1483
    return ps_tobool( &parser->cursor, parser->limit );
 
1484
  }
 
1485
 
 
1486
#endif /* 0 */
 
1487
 
 
1488
 
 
1489
  FT_LOCAL_DEF( void )
 
1490
  ps_parser_init( PS_Parser  parser,
 
1491
                  FT_Byte*   base,
 
1492
                  FT_Byte*   limit,
 
1493
                  FT_Memory  memory )
 
1494
  {
 
1495
    parser->error  = PSaux_Err_Ok;
 
1496
    parser->base   = base;
 
1497
    parser->limit  = limit;
 
1498
    parser->cursor = base;
 
1499
    parser->memory = memory;
 
1500
    parser->funcs  = ps_parser_funcs;
 
1501
  }
 
1502
 
 
1503
 
 
1504
  FT_LOCAL_DEF( void )
 
1505
  ps_parser_done( PS_Parser  parser )
 
1506
  {
 
1507
    FT_UNUSED( parser );
 
1508
  }
 
1509
 
 
1510
 
 
1511
  /*************************************************************************/
 
1512
  /*************************************************************************/
 
1513
  /*****                                                               *****/
 
1514
  /*****                            T1 BUILDER                         *****/
 
1515
  /*****                                                               *****/
 
1516
  /*************************************************************************/
 
1517
  /*************************************************************************/
 
1518
 
 
1519
  /*************************************************************************/
 
1520
  /*                                                                       */
 
1521
  /* <Function>                                                            */
 
1522
  /*    t1_builder_init                                                    */
 
1523
  /*                                                                       */
 
1524
  /* <Description>                                                         */
 
1525
  /*    Initializes a given glyph builder.                                 */
 
1526
  /*                                                                       */
 
1527
  /* <InOut>                                                               */
 
1528
  /*    builder :: A pointer to the glyph builder to initialize.           */
 
1529
  /*                                                                       */
 
1530
  /* <Input>                                                               */
 
1531
  /*    face    :: The current face object.                                */
 
1532
  /*                                                                       */
 
1533
  /*    size    :: The current size object.                                */
 
1534
  /*                                                                       */
 
1535
  /*    glyph   :: The current glyph object.                               */
 
1536
  /*                                                                       */
 
1537
  /*    hinting :: Whether hinting should be applied.                      */
 
1538
  /*                                                                       */
 
1539
  FT_LOCAL_DEF( void )
 
1540
  t1_builder_init( T1_Builder    builder,
 
1541
                   FT_Face       face,
 
1542
                   FT_Size       size,
 
1543
                   FT_GlyphSlot  glyph,
 
1544
                   FT_Bool       hinting )
 
1545
  {
 
1546
    builder->parse_state = T1_Parse_Start;
 
1547
    builder->load_points = 1;
 
1548
 
 
1549
    builder->face   = face;
 
1550
    builder->glyph  = glyph;
 
1551
    builder->memory = face->memory;
 
1552
 
 
1553
    if ( glyph )
 
1554
    {
 
1555
      FT_GlyphLoader  loader = glyph->internal->loader;
 
1556
 
 
1557
 
 
1558
      builder->loader  = loader;
 
1559
      builder->base    = &loader->base.outline;
 
1560
      builder->current = &loader->current.outline;
 
1561
      FT_GlyphLoader_Rewind( loader );
 
1562
 
 
1563
      builder->hints_globals = size->internal;
 
1564
      builder->hints_funcs   = 0;
 
1565
 
 
1566
      if ( hinting )
 
1567
        builder->hints_funcs = glyph->internal->glyph_hints;
 
1568
    }
 
1569
 
 
1570
    if ( size )
 
1571
    {
 
1572
      builder->scale_x = size->metrics.x_scale;
 
1573
      builder->scale_y = size->metrics.y_scale;
 
1574
    }
 
1575
 
 
1576
    builder->pos_x = 0;
 
1577
    builder->pos_y = 0;
 
1578
 
 
1579
    builder->left_bearing.x = 0;
 
1580
    builder->left_bearing.y = 0;
 
1581
    builder->advance.x      = 0;
 
1582
    builder->advance.y      = 0;
 
1583
 
 
1584
    builder->funcs = t1_builder_funcs;
 
1585
  }
 
1586
 
 
1587
 
 
1588
  /*************************************************************************/
 
1589
  /*                                                                       */
 
1590
  /* <Function>                                                            */
 
1591
  /*    t1_builder_done                                                    */
 
1592
  /*                                                                       */
 
1593
  /* <Description>                                                         */
 
1594
  /*    Finalizes a given glyph builder.  Its contents can still be used   */
 
1595
  /*    after the call, but the function saves important information       */
 
1596
  /*    within the corresponding glyph slot.                               */
 
1597
  /*                                                                       */
 
1598
  /* <Input>                                                               */
 
1599
  /*    builder :: A pointer to the glyph builder to finalize.             */
 
1600
  /*                                                                       */
 
1601
  FT_LOCAL_DEF( void )
 
1602
  t1_builder_done( T1_Builder  builder )
 
1603
  {
 
1604
    FT_GlyphSlot  glyph = builder->glyph;
 
1605
 
 
1606
 
 
1607
    if ( glyph )
 
1608
      glyph->outline = *builder->base;
 
1609
  }
 
1610
 
 
1611
 
 
1612
  /* check that there is enough space for `count' more points */
 
1613
  FT_LOCAL_DEF( FT_Error )
 
1614
  t1_builder_check_points( T1_Builder  builder,
 
1615
                           FT_Int      count )
 
1616
  {
 
1617
    return FT_GlyphLoader_CheckPoints( builder->loader, count, 0 );
 
1618
  }
 
1619
 
 
1620
 
 
1621
  /* add a new point, do not check space */
 
1622
  FT_LOCAL_DEF( void )
 
1623
  t1_builder_add_point( T1_Builder  builder,
 
1624
                        FT_Pos      x,
 
1625
                        FT_Pos      y,
 
1626
                        FT_Byte     flag )
 
1627
  {
 
1628
    FT_Outline*  outline = builder->current;
 
1629
 
 
1630
 
 
1631
    if ( builder->load_points )
 
1632
    {
 
1633
      FT_Vector*  point   = outline->points + outline->n_points;
 
1634
      FT_Byte*    control = (FT_Byte*)outline->tags + outline->n_points;
 
1635
 
 
1636
 
 
1637
      if ( builder->shift )
 
1638
      {
 
1639
        x >>= 16;
 
1640
        y >>= 16;
 
1641
      }
 
1642
      point->x = x;
 
1643
      point->y = y;
 
1644
      *control = (FT_Byte)( flag ? FT_CURVE_TAG_ON : FT_CURVE_TAG_CUBIC );
 
1645
 
 
1646
      builder->last = *point;
 
1647
    }
 
1648
    outline->n_points++;
 
1649
  }
 
1650
 
 
1651
 
 
1652
  /* check space for a new on-curve point, then add it */
 
1653
  FT_LOCAL_DEF( FT_Error )
 
1654
  t1_builder_add_point1( T1_Builder  builder,
 
1655
                         FT_Pos      x,
 
1656
                         FT_Pos      y )
 
1657
  {
 
1658
    FT_Error  error;
 
1659
 
 
1660
 
 
1661
    error = t1_builder_check_points( builder, 1 );
 
1662
    if ( !error )
 
1663
      t1_builder_add_point( builder, x, y, 1 );
 
1664
 
 
1665
    return error;
 
1666
  }
 
1667
 
 
1668
 
 
1669
  /* check space for a new contour, then add it */
 
1670
  FT_LOCAL_DEF( FT_Error )
 
1671
  t1_builder_add_contour( T1_Builder  builder )
 
1672
  {
 
1673
    FT_Outline*  outline = builder->current;
 
1674
    FT_Error     error;
 
1675
 
 
1676
 
 
1677
    if ( !builder->load_points )
 
1678
    {
 
1679
      outline->n_contours++;
 
1680
      return PSaux_Err_Ok;
 
1681
    }
 
1682
 
 
1683
    error = FT_GlyphLoader_CheckPoints( builder->loader, 0, 1 );
 
1684
    if ( !error )
 
1685
    {
 
1686
      if ( outline->n_contours > 0 )
 
1687
        outline->contours[outline->n_contours - 1] =
 
1688
          (short)( outline->n_points - 1 );
 
1689
 
 
1690
      outline->n_contours++;
 
1691
    }
 
1692
 
 
1693
    return error;
 
1694
  }
 
1695
 
 
1696
 
 
1697
  /* if a path was begun, add its first on-curve point */
 
1698
  FT_LOCAL_DEF( FT_Error )
 
1699
  t1_builder_start_point( T1_Builder  builder,
 
1700
                          FT_Pos      x,
 
1701
                          FT_Pos      y )
 
1702
  {
 
1703
    FT_Error  error = PSaux_Err_Invalid_File_Format;
 
1704
 
 
1705
 
 
1706
    /* test whether we are building a new contour */
 
1707
 
 
1708
    if ( builder->parse_state == T1_Parse_Have_Path )
 
1709
      error = PSaux_Err_Ok;
 
1710
    else if ( builder->parse_state == T1_Parse_Have_Moveto )
 
1711
    {
 
1712
      builder->parse_state = T1_Parse_Have_Path;
 
1713
      error = t1_builder_add_contour( builder );
 
1714
      if ( !error )
 
1715
        error = t1_builder_add_point1( builder, x, y );
 
1716
    }
 
1717
 
 
1718
    return error;
 
1719
  }
 
1720
 
 
1721
 
 
1722
  /* close the current contour */
 
1723
  FT_LOCAL_DEF( void )
 
1724
  t1_builder_close_contour( T1_Builder  builder )
 
1725
  {
 
1726
    FT_Outline*  outline = builder->current;
 
1727
 
 
1728
 
 
1729
    /* XXXX: We must not include the last point in the path if it */
 
1730
    /*       is located on the first point.                       */
 
1731
    if ( outline->n_points > 1 )
 
1732
    {
 
1733
      FT_Int      first   = 0;
 
1734
      FT_Vector*  p1      = outline->points + first;
 
1735
      FT_Vector*  p2      = outline->points + outline->n_points - 1;
 
1736
      FT_Byte*    control = (FT_Byte*)outline->tags + outline->n_points - 1;
 
1737
 
 
1738
 
 
1739
      if ( outline->n_contours > 1 )
 
1740
      {
 
1741
        first = outline->contours[outline->n_contours - 2] + 1;
 
1742
        p1    = outline->points + first;
 
1743
      }
 
1744
 
 
1745
      /* `delete' last point only if it coincides with the first */
 
1746
      /* point and it is not a control point (which can happen). */
 
1747
      if ( p1->x == p2->x && p1->y == p2->y )
 
1748
        if ( *control == FT_CURVE_TAG_ON )
 
1749
          outline->n_points--;
 
1750
    }
 
1751
 
 
1752
    if ( outline->n_contours > 0 )
 
1753
      outline->contours[outline->n_contours - 1] =
 
1754
        (short)( outline->n_points - 1 );
 
1755
  }
 
1756
 
 
1757
 
 
1758
  /*************************************************************************/
 
1759
  /*************************************************************************/
 
1760
  /*****                                                               *****/
 
1761
  /*****                            OTHER                              *****/
 
1762
  /*****                                                               *****/
 
1763
  /*************************************************************************/
 
1764
  /*************************************************************************/
 
1765
 
 
1766
  FT_LOCAL_DEF( void )
 
1767
  t1_decrypt( FT_Byte*   buffer,
 
1768
              FT_Offset  length,
 
1769
              FT_UShort  seed )
 
1770
  {
 
1771
    while ( length > 0 )
 
1772
    {
 
1773
      FT_Byte  plain;
 
1774
 
 
1775
 
 
1776
      plain     = (FT_Byte)( *buffer ^ ( seed >> 8 ) );
 
1777
      seed      = (FT_UShort)( ( *buffer + seed ) * 52845U + 22719 );
 
1778
      *buffer++ = plain;
 
1779
      length--;
 
1780
    }
 
1781
  }
 
1782
 
 
1783
 
 
1784
/* END */