update for HEAD-2003050101
[reactos.git] / lib / freetype / src / type1 / t1load.c
1 /***************************************************************************/
2 /*                                                                         */
3 /*  t1load.c                                                               */
4 /*                                                                         */
5 /*    Type 1 font loader (body).                                           */
6 /*                                                                         */
7 /*  Copyright 1996-2001, 2002 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   /*************************************************************************/
20   /*                                                                       */
21   /* This is the new and improved Type 1 data loader for FreeType 2.  The  */
22   /* old loader has several problems: it is slow, complex, difficult to    */
23   /* maintain, and contains incredible hacks to make it accept some        */
24   /* ill-formed Type 1 fonts without hiccup-ing.  Moreover, about 5% of    */
25   /* the Type 1 fonts on my machine still aren't loaded correctly by it.   */
26   /*                                                                       */
27   /* This version is much simpler, much faster and also easier to read and */
28   /* maintain by a great order of magnitude.  The idea behind it is to     */
29   /* _not_ try to read the Type 1 token stream with a state machine (i.e.  */
30   /* a Postscript-like interpreter) but rather to perform simple pattern   */
31   /* matching.                                                             */
32   /*                                                                       */
33   /* Indeed, nearly all data definitions follow a simple pattern like      */
34   /*                                                                       */
35   /*  ... /Field <data> ...                                                */
36   /*                                                                       */
37   /* where <data> can be a number, a boolean, a string, or an array of     */
38   /* numbers.  There are a few exceptions, namely the encoding, font name, */
39   /* charstrings, and subrs; they are handled with a special pattern       */
40   /* matching routine.                                                     */
41   /*                                                                       */
42   /* All other common cases are handled very simply.  The matching rules   */
43   /* are defined in the file `t1tokens.h' through the use of several       */
44   /* macros calls PARSE_XXX.                                               */
45   /*                                                                       */
46   /* This file is included twice here; the first time to generate parsing  */
47   /* callback functions, the second to generate a table of keywords (with  */
48   /* pointers to the associated callback).                                 */
49   /*                                                                       */
50   /* The function `parse_dict' simply scans *linearly* a given dictionary  */
51   /* (either the top-level or private one) and calls the appropriate       */
52   /* callback when it encounters an immediate keyword.                     */
53   /*                                                                       */
54   /* This is by far the fastest way one can find to parse and read all     */
55   /* data.                                                                 */
56   /*                                                                       */
57   /* This led to tremendous code size reduction.  Note that later, the     */
58   /* glyph loader will also be _greatly_ simplified, and the automatic     */
59   /* hinter will replace the clumsy `t1hinter'.                            */
60   /*                                                                       */
61   /*************************************************************************/
62
63
64 #include <ft2build.h>
65 #include FT_INTERNAL_DEBUG_H
66 #include FT_CONFIG_CONFIG_H
67 #include FT_MULTIPLE_MASTERS_H
68 #include FT_INTERNAL_TYPE1_TYPES_H
69
70 #include "t1load.h"
71 #include "t1errors.h"
72
73
74
75   /*************************************************************************/
76   /*                                                                       */
77   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
78   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
79   /* messages during execution.                                            */
80   /*                                                                       */
81 #undef  FT_COMPONENT
82 #define FT_COMPONENT  trace_t1load
83
84
85 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
86
87
88   /*************************************************************************/
89   /*************************************************************************/
90   /*****                                                               *****/
91   /*****                    MULTIPLE MASTERS SUPPORT                   *****/
92   /*****                                                               *****/
93   /*************************************************************************/
94   /*************************************************************************/
95
96   static FT_Error
97   t1_allocate_blend( T1_Face  face,
98                      FT_UInt  num_designs,
99                      FT_UInt  num_axis )
100   {
101     PS_Blend   blend;
102     FT_Memory  memory = face->root.memory;
103     FT_Error   error  = 0;
104
105
106     blend = face->blend;
107     if ( !blend )
108     {
109       if ( FT_NEW( blend ) )
110         goto Exit;
111
112       face->blend = blend;
113     }
114
115     /* allocate design data if needed */
116     if ( num_designs > 0 )
117     {
118       if ( blend->num_designs == 0 )
119       {
120         FT_UInt  nn;
121
122
123         /* allocate the blend `private' and `font_info' dictionaries */
124         if ( FT_NEW_ARRAY( blend->font_infos[1], num_designs     ) ||
125              FT_NEW_ARRAY( blend->privates[1], num_designs       ) ||
126              FT_NEW_ARRAY( blend->bboxes[1], num_designs         ) ||
127              FT_NEW_ARRAY( blend->weight_vector, num_designs * 2 ) )
128           goto Exit;
129
130         blend->default_weight_vector = blend->weight_vector + num_designs;
131
132         blend->font_infos[0] = &face->type1.font_info;
133         blend->privates  [0] = &face->type1.private_dict;
134         blend->bboxes    [0] = &face->type1.font_bbox;
135
136         for ( nn = 2; nn <= num_designs; nn++ )
137         {
138           blend->privates[nn]   = blend->privates  [nn - 1] + 1;
139           blend->font_infos[nn] = blend->font_infos[nn - 1] + 1;
140           blend->bboxes[nn]     = blend->bboxes    [nn - 1] + 1;
141         }
142
143         blend->num_designs   = num_designs;
144       }
145       else if ( blend->num_designs != num_designs )
146         goto Fail;
147     }
148
149     /* allocate axis data if needed */
150     if ( num_axis > 0 )
151     {
152       if ( blend->num_axis != 0 && blend->num_axis != num_axis )
153         goto Fail;
154
155       blend->num_axis = num_axis;
156     }
157
158     /* allocate the blend design pos table if needed */
159     num_designs = blend->num_designs;
160     num_axis    = blend->num_axis;
161     if ( num_designs && num_axis && blend->design_pos[0] == 0 )
162     {
163       FT_UInt  n;
164
165
166       if ( FT_NEW_ARRAY( blend->design_pos[0], num_designs * num_axis ) )
167         goto Exit;
168
169       for ( n = 1; n < num_designs; n++ )
170         blend->design_pos[n] = blend->design_pos[0] + num_axis * n;
171     }
172
173   Exit:
174     return error;
175
176   Fail:
177     error = -1;
178     goto Exit;
179   }
180
181
182   FT_LOCAL_DEF( FT_Error )
183   T1_Get_Multi_Master( T1_Face           face,
184                        FT_Multi_Master*  master )
185   {
186     PS_Blend  blend = face->blend;
187     FT_UInt   n;
188     FT_Error  error;
189
190
191     error = T1_Err_Invalid_Argument;
192
193     if ( blend )
194     {
195       master->num_axis    = blend->num_axis;
196       master->num_designs = blend->num_designs;
197
198       for ( n = 0; n < blend->num_axis; n++ )
199       {
200         FT_MM_Axis*   axis = master->axis + n;
201         PS_DesignMap  map = blend->design_map + n;
202
203
204         axis->name    = blend->axis_names[n];
205         axis->minimum = map->design_points[0];
206         axis->maximum = map->design_points[map->num_points - 1];
207       }
208       error = 0;
209     }
210     return error;
211   }
212
213
214   FT_LOCAL_DEF( FT_Error )
215   T1_Set_MM_Blend( T1_Face    face,
216                    FT_UInt    num_coords,
217                    FT_Fixed*  coords )
218   {
219     PS_Blend  blend = face->blend;
220     FT_Error  error;
221     FT_UInt   n, m;
222
223
224     error = T1_Err_Invalid_Argument;
225
226     if ( blend && blend->num_axis == num_coords )
227     {
228       /* recompute the weight vector from the blend coordinates */
229       error = T1_Err_Ok;
230
231       for ( n = 0; n < blend->num_designs; n++ )
232       {
233         FT_Fixed  result = 0x10000L;  /* 1.0 fixed */
234
235
236         for ( m = 0; m < blend->num_axis; m++ )
237         {
238           FT_Fixed  factor;
239
240
241           /* get current blend axis position */
242           factor = coords[m];
243           if ( factor < 0 )        factor = 0;
244           if ( factor > 0x10000L ) factor = 0x10000L;
245
246           if ( ( n & ( 1 << m ) ) == 0 )
247             factor = 0x10000L - factor;
248
249           result = FT_MulFix( result, factor );
250         }
251         blend->weight_vector[n] = result;
252       }
253
254       error = T1_Err_Ok;
255     }
256     return error;
257   }
258
259
260   FT_LOCAL_DEF( FT_Error )
261   T1_Set_MM_Design( T1_Face   face,
262                     FT_UInt   num_coords,
263                     FT_Long*  coords )
264   {
265     PS_Blend  blend = face->blend;
266     FT_Error  error;
267     FT_UInt   n, p;
268
269
270     error = T1_Err_Invalid_Argument;
271     if ( blend && blend->num_axis == num_coords )
272     {
273       /* compute the blend coordinates through the blend design map */
274       FT_Fixed  final_blends[T1_MAX_MM_DESIGNS];
275
276
277       for ( n = 0; n < blend->num_axis; n++ )
278       {
279         FT_Long       design  = coords[n];
280         FT_Fixed      the_blend;
281         PS_DesignMap  map     = blend->design_map + n;
282         FT_Fixed*     designs = map->design_points;
283         FT_Fixed*     blends  = map->blend_points;
284         FT_Int        before  = -1, after = -1;
285
286
287         for ( p = 0; p < (FT_UInt)map->num_points; p++ )
288         {
289           FT_Fixed  p_design = designs[p];
290
291
292           /* exact match ? */
293           if ( design == p_design )
294           {
295             the_blend = blends[p];
296             goto Found;
297           }
298
299           if ( design < p_design )
300           {
301             after = p;
302             break;
303           }
304
305           before = p;
306         }
307
308         /* now, interpolate if needed */
309         if ( before < 0 )
310           the_blend = blends[0];
311
312         else if ( after < 0 )
313           the_blend = blends[map->num_points - 1];
314
315         else
316           the_blend = FT_MulDiv( design         - designs[before],
317                                  blends [after] - blends [before],
318                                  designs[after] - designs[before] );
319
320       Found:
321         final_blends[n] = the_blend;
322       }
323
324       error = T1_Set_MM_Blend( face, num_coords, final_blends );
325     }
326
327     return error;
328   }
329
330
331   FT_LOCAL_DEF( void )
332   T1_Done_Blend( T1_Face  face )
333   {
334     FT_Memory  memory = face->root.memory;
335     PS_Blend   blend  = face->blend;
336
337
338     if ( blend )
339     {
340       FT_UInt  num_designs = blend->num_designs;
341       FT_UInt  num_axis    = blend->num_axis;
342       FT_UInt  n;
343
344
345       /* release design pos table */
346       FT_FREE( blend->design_pos[0] );
347       for ( n = 1; n < num_designs; n++ )
348         blend->design_pos[n] = 0;
349
350       /* release blend `private' and `font info' dictionaries */
351       FT_FREE( blend->privates[1] );
352       FT_FREE( blend->font_infos[1] );
353       FT_FREE( blend->bboxes[1] );
354
355       for ( n = 0; n < num_designs; n++ )
356       {
357         blend->privates  [n] = 0;
358         blend->font_infos[n] = 0;
359         blend->bboxes    [n] = 0;
360       }
361
362       /* release weight vectors */
363       FT_FREE( blend->weight_vector );
364       blend->default_weight_vector = 0;
365
366       /* release axis names */
367       for ( n = 0; n < num_axis; n++ )
368         FT_FREE( blend->axis_names[n] );
369
370       /* release design map */
371       for ( n = 0; n < num_axis; n++ )
372       {
373         PS_DesignMap  dmap = blend->design_map + n;
374
375
376         FT_FREE( dmap->design_points );
377         dmap->num_points = 0;
378       }
379
380       FT_FREE( face->blend );
381     }
382   }
383
384
385   static void
386   parse_blend_axis_types( T1_Face    face,
387                           T1_Loader  loader )
388   {
389     T1_TokenRec  axis_tokens[ T1_MAX_MM_AXIS ];
390     FT_Int       n, num_axis;
391     FT_Error     error = 0;
392     PS_Blend     blend;
393     FT_Memory    memory;
394
395
396     /* take an array of objects */
397     T1_ToTokenArray( &loader->parser, axis_tokens,
398                      T1_MAX_MM_AXIS, &num_axis );
399     if ( num_axis <= 0 || num_axis > T1_MAX_MM_AXIS )
400     {
401       FT_ERROR(( "parse_blend_axis_types: incorrect number of axes: %d\n",
402                  num_axis ));
403       error = T1_Err_Invalid_File_Format;
404       goto Exit;
405     }
406
407     /* allocate blend if necessary */
408     error = t1_allocate_blend( face, 0, (FT_UInt)num_axis );
409     if ( error )
410       goto Exit;
411
412     blend  = face->blend;
413     memory = face->root.memory;
414
415     /* each token is an immediate containing the name of the axis */
416     for ( n = 0; n < num_axis; n++ )
417     {
418       T1_Token    token = axis_tokens + n;
419       FT_Byte*    name;
420       FT_PtrDist  len;
421
422
423       /* skip first slash, if any */
424       if ( token->start[0] == '/' )
425         token->start++;
426
427       len = token->limit - token->start;
428       if ( len <= 0 )
429       {
430         error = T1_Err_Invalid_File_Format;
431         goto Exit;
432       }
433
434       if ( FT_ALLOC( blend->axis_names[n], len + 1 ) )
435         goto Exit;
436
437       name = (FT_Byte*)blend->axis_names[n];
438       FT_MEM_COPY( name, token->start, len );
439       name[len] = 0;
440     }
441
442   Exit:
443     loader->parser.root.error = error;
444   }
445
446
447   static void
448   parse_blend_design_positions( T1_Face    face,
449                                 T1_Loader  loader )
450   {
451     T1_TokenRec  design_tokens[ T1_MAX_MM_DESIGNS ];
452     FT_Int       num_designs;
453     FT_Int       num_axis;
454     T1_Parser    parser = &loader->parser;
455
456     FT_Error     error = 0;
457     PS_Blend     blend;
458
459
460     /* get the array of design tokens - compute number of designs */
461     T1_ToTokenArray( parser, design_tokens, T1_MAX_MM_DESIGNS, &num_designs );
462     if ( num_designs <= 0 || num_designs > T1_MAX_MM_DESIGNS )
463     {
464       FT_ERROR(( "parse_blend_design_positions:" ));
465       FT_ERROR(( " incorrect number of designs: %d\n",
466                  num_designs ));
467       error = T1_Err_Invalid_File_Format;
468       goto Exit;
469     }
470
471     {
472       FT_Byte*  old_cursor = parser->root.cursor;
473       FT_Byte*  old_limit  = parser->root.limit;
474       FT_UInt   n;
475
476
477       blend    = face->blend;
478       num_axis = 0;  /* make compiler happy */
479
480       for ( n = 0; n < (FT_UInt)num_designs; n++ )
481       {
482         T1_TokenRec  axis_tokens[ T1_MAX_MM_DESIGNS ];
483         T1_Token     token;
484         FT_Int       axis, n_axis;
485
486
487         /* read axis/coordinates tokens */
488         token = design_tokens + n;
489         parser->root.cursor = token->start - 1;
490         parser->root.limit  = token->limit + 1;
491         T1_ToTokenArray( parser, axis_tokens, T1_MAX_MM_AXIS, &n_axis );
492
493         if ( n == 0 )
494         {
495           num_axis = n_axis;
496           error = t1_allocate_blend( face, num_designs, num_axis );
497           if ( error )
498             goto Exit;
499           blend = face->blend;
500         }
501         else if ( n_axis != num_axis )
502         {
503           FT_ERROR(( "parse_blend_design_positions: incorrect table\n" ));
504           error = T1_Err_Invalid_File_Format;
505           goto Exit;
506         }
507
508         /* now, read each axis token into the design position */
509         for ( axis = 0; axis < n_axis; axis++ )
510         {
511           T1_Token  token2 = axis_tokens + axis;
512
513
514           parser->root.cursor = token2->start;
515           parser->root.limit  = token2->limit;
516           blend->design_pos[n][axis] = T1_ToFixed( parser, 0 );
517         }
518       }
519
520       loader->parser.root.cursor = old_cursor;
521       loader->parser.root.limit  = old_limit;
522     }
523
524   Exit:
525     loader->parser.root.error = error;
526   }
527
528
529   static void
530   parse_blend_design_map( T1_Face    face,
531                           T1_Loader  loader )
532   {
533     FT_Error     error  = 0;
534     T1_Parser    parser = &loader->parser;
535     PS_Blend     blend;
536     T1_TokenRec  axis_tokens[T1_MAX_MM_AXIS];
537     FT_Int       n, num_axis;
538     FT_Byte*     old_cursor;
539     FT_Byte*     old_limit;
540     FT_Memory    memory = face->root.memory;
541
542
543     T1_ToTokenArray( parser, axis_tokens, T1_MAX_MM_AXIS, &num_axis );
544     if ( num_axis <= 0 || num_axis > T1_MAX_MM_AXIS )
545     {
546       FT_ERROR(( "parse_blend_design_map: incorrect number of axes: %d\n",
547                  num_axis ));
548       error = T1_Err_Invalid_File_Format;
549       goto Exit;
550     }
551     old_cursor = parser->root.cursor;
552     old_limit  = parser->root.limit;
553
554     error = t1_allocate_blend( face, 0, num_axis );
555     if ( error )
556       goto Exit;
557     blend = face->blend;
558
559     /* now, read each axis design map */
560     for ( n = 0; n < num_axis; n++ )
561     {
562       PS_DesignMap  map = blend->design_map + n;
563       T1_Token      token;
564       FT_Int        p, num_points;
565
566
567       token = axis_tokens + n;
568       parser->root.cursor = token->start;
569       parser->root.limit  = token->limit;
570
571       /* count the number of map points */
572       {
573         FT_Byte*  ptr   = token->start;
574         FT_Byte*  limit = token->limit;
575
576
577         num_points = 0;
578         for ( ; ptr < limit; ptr++ )
579           if ( ptr[0] == '[' )
580             num_points++;
581       }
582       if ( num_points <= 0 || num_points > T1_MAX_MM_MAP_POINTS )
583       {
584         FT_ERROR(( "parse_blend_design_map: incorrect table\n" ));
585         error = T1_Err_Invalid_File_Format;
586         goto Exit;
587       }
588
589       /* allocate design map data */
590       if ( FT_NEW_ARRAY( map->design_points, num_points * 2 ) )
591         goto Exit;
592       map->blend_points = map->design_points + num_points;
593       map->num_points   = (FT_Byte)num_points;
594
595       for ( p = 0; p < num_points; p++ )
596       {
597         map->design_points[p] = T1_ToInt( parser );
598         map->blend_points [p] = T1_ToFixed( parser, 0 );
599       }
600     }
601
602     parser->root.cursor = old_cursor;
603     parser->root.limit  = old_limit;
604
605   Exit:
606     parser->root.error = error;
607   }
608
609
610   static void
611   parse_weight_vector( T1_Face    face,
612                        T1_Loader  loader )
613   {
614     FT_Error     error  = 0;
615     T1_Parser    parser = &loader->parser;
616     PS_Blend     blend  = face->blend;
617     T1_TokenRec  master;
618     FT_UInt      n;
619     FT_Byte*     old_cursor;
620     FT_Byte*     old_limit;
621
622
623     if ( !blend || blend->num_designs == 0 )
624     {
625       FT_ERROR(( "parse_weight_vector: too early!\n" ));
626       error = T1_Err_Invalid_File_Format;
627       goto Exit;
628     }
629
630     T1_ToToken( parser, &master );
631     if ( master.type != T1_TOKEN_TYPE_ARRAY )
632     {
633       FT_ERROR(( "parse_weight_vector: incorrect format!\n" ));
634       error = T1_Err_Invalid_File_Format;
635       goto Exit;
636     }
637
638     old_cursor = parser->root.cursor;
639     old_limit  = parser->root.limit;
640
641     parser->root.cursor = master.start;
642     parser->root.limit  = master.limit;
643
644     for ( n = 0; n < blend->num_designs; n++ )
645     {
646       blend->default_weight_vector[n] =
647       blend->weight_vector[n]         = T1_ToFixed( parser, 0 );
648     }
649
650     parser->root.cursor = old_cursor;
651     parser->root.limit  = old_limit;
652
653   Exit:
654     parser->root.error = error;
655   }
656
657
658   /* the keyword `/shareddict' appears in some multiple master fonts   */
659   /* with a lot of Postscript garbage behind it (that's completely out */
660   /* of spec!); we detect it and terminate the parsing                 */
661   /*                                                                   */
662   static void
663   parse_shared_dict( T1_Face    face,
664                      T1_Loader  loader )
665   {
666     T1_Parser  parser = &loader->parser;
667
668     FT_UNUSED( face );
669
670
671     parser->root.cursor = parser->root.limit;
672     parser->root.error  = 0;
673   }
674
675 #endif /* T1_CONFIG_OPTION_NO_MM_SUPPORT */
676
677
678   /*************************************************************************/
679   /*************************************************************************/
680   /*****                                                               *****/
681   /*****                      TYPE 1 SYMBOL PARSING                    *****/
682   /*****                                                               *****/
683   /*************************************************************************/
684   /*************************************************************************/
685
686
687   /*************************************************************************/
688   /*                                                                       */
689   /* First of all, define the token field static variables.  This is a set */
690   /* of T1_FieldRec variables used later.                                  */
691   /*                                                                       */
692   /*************************************************************************/
693
694
695   static FT_Error
696   t1_load_keyword( T1_Face    face,
697                    T1_Loader  loader,
698                    T1_Field   field )
699   {
700     FT_Error  error;
701     void*     dummy_object;
702     void**    objects;
703     FT_UInt   max_objects;
704     PS_Blend  blend = face->blend;
705
706
707     /* if the keyword has a dedicated callback, call it */
708     if ( field->type == T1_FIELD_TYPE_CALLBACK )
709     {
710       field->reader( (FT_Face)face, loader );
711       error = loader->parser.root.error;
712       goto Exit;
713     }
714
715     /* now, the keyword is either a simple field, or a table of fields; */
716     /* we are now going to take care of it                              */
717     switch ( field->location )
718     {
719     case T1_FIELD_LOCATION_FONT_INFO:
720       dummy_object = &face->type1.font_info;
721       objects      = &dummy_object;
722       max_objects  = 0;
723
724       if ( blend )
725       {
726         objects     = (void**)blend->font_infos;
727         max_objects = blend->num_designs;
728       }
729       break;
730
731     case T1_FIELD_LOCATION_PRIVATE:
732       dummy_object = &face->type1.private_dict;
733       objects      = &dummy_object;
734       max_objects  = 0;
735
736       if ( blend )
737       {
738         objects     = (void**)blend->privates;
739         max_objects = blend->num_designs;
740       }
741       break;
742
743     case T1_FIELD_LOCATION_BBOX:
744       dummy_object = &face->type1.font_bbox;
745       objects      = &dummy_object;
746       max_objects  = 0;
747
748       if ( blend )
749       {
750         objects     = (void**)blend->bboxes;
751         max_objects = blend->num_designs;
752       }
753       break;
754
755     default:
756       dummy_object = &face->type1;
757       objects      = &dummy_object;
758       max_objects  = 0;
759     }
760
761     if ( field->type == T1_FIELD_TYPE_INTEGER_ARRAY ||
762          field->type == T1_FIELD_TYPE_FIXED_ARRAY   )
763       error = T1_Load_Field_Table( &loader->parser, field,
764                                    objects, max_objects, 0 );
765     else
766       error = T1_Load_Field( &loader->parser, field,
767                              objects, max_objects, 0 );
768
769   Exit:
770     return error;
771   }
772
773
774   static int
775   is_space( FT_Byte  c )
776   {
777     return ( c == ' ' || c == '\t' || c == '\r' || c == '\n' );
778   }
779
780
781   static int
782   is_name_char( FT_Byte  c )
783   {
784     /* Note: PostScript allows any non-delimiting, non-whitespace      */
785     /*       in a name (PS Ref Manual, 3rd Ed, p31)                    */
786     /*       PostScript delimiters include (,),<,>,[,],{,},/ and %     */
787
788     return ( c != '(' &&
789              c != ')' &&
790              c != '<' &&
791              c != '>' &&
792              c != '[' &&
793              c != ']' &&
794              c != '{' &&
795              c != '}' &&
796              c != '/' &&
797              c != '%' &&
798              ! is_space( c ) 
799              );
800  }
801
802
803   static int
804   read_binary_data( T1_Parser  parser,
805                     FT_Long*   size,
806                     FT_Byte**  base )
807   {
808     FT_Byte*  cur;
809     FT_Byte*  limit = parser->root.limit;
810
811
812     /* the binary data has the following format */
813     /*                                          */
814     /* `size' [white*] RD white ....... ND      */
815     /*                                          */
816
817     T1_Skip_Spaces( parser );
818     cur = parser->root.cursor;
819
820     if ( cur < limit && (FT_Byte)( *cur - '0' ) < 10 )
821     {
822       *size = T1_ToInt( parser );
823
824       T1_Skip_Spaces( parser );
825       T1_Skip_Alpha ( parser );  /* `RD' or `-|' or something else */
826
827       /* there is only one whitespace char after the */
828       /* `RD' or `-|' token                          */
829       *base = parser->root.cursor + 1;
830
831       parser->root.cursor += *size + 1;
832       return 1;
833     }
834
835     FT_ERROR(( "read_binary_data: invalid size field\n" ));
836     parser->root.error = T1_Err_Invalid_File_Format;
837     return 0;
838   }
839
840
841   /* we will now define the routines used to handle */
842   /* the `/Encoding', `/Subrs', and `/CharStrings'  */
843   /* dictionaries                                   */
844
845   static void
846   parse_font_name( T1_Face    face,
847                    T1_Loader  loader )
848   {
849     T1_Parser   parser = &loader->parser;
850     FT_Error    error;
851     FT_Memory   memory = parser->root.memory;
852     FT_PtrDist  len;
853     FT_Byte*    cur;
854     FT_Byte*    cur2;
855     FT_Byte*    limit;
856
857
858     if ( face->type1.font_name )
859       /*  with synthetic fonts, it's possible we get here twice  */
860       return;
861
862     T1_Skip_Spaces( parser );
863
864     cur   = parser->root.cursor;
865     limit = parser->root.limit;
866
867     if ( cur >= limit - 1 || *cur != '/' )
868       return;
869
870     cur++;
871     cur2 = cur;
872     while ( cur2 < limit && is_name_char( *cur2 ) )
873       cur2++;
874
875     len = cur2 - cur;
876     if ( len > 0 )
877     {
878       if ( FT_ALLOC( face->type1.font_name, len + 1 ) )
879       {
880         parser->root.error = error;
881         return;
882       }
883
884       FT_MEM_COPY( face->type1.font_name, cur, len );
885       face->type1.font_name[len] = '\0';
886     }
887     parser->root.cursor = cur2;
888   }
889
890
891 #if 0
892   static void
893   parse_font_bbox( T1_Face    face,
894                    T1_Loader  loader )
895   {
896     T1_Parser  parser = &loader->parser;
897     FT_Fixed   temp[4];
898     FT_BBox*   bbox   = &face->type1.font_bbox;
899
900
901     (void)T1_ToFixedArray( parser, 4, temp, 0 );
902     bbox->xMin = FT_RoundFix( temp[0] );
903     bbox->yMin = FT_RoundFix( temp[1] );
904     bbox->xMax = FT_RoundFix( temp[2] );
905     bbox->yMax = FT_RoundFix( temp[3] );
906   }
907 #endif
908
909
910   static void
911   parse_font_matrix( T1_Face    face,
912                      T1_Loader  loader )
913   {
914     T1_Parser   parser = &loader->parser;
915     FT_Matrix*  matrix = &face->type1.font_matrix;
916     FT_Vector*  offset = &face->type1.font_offset;
917     FT_Face     root   = (FT_Face)&face->root;
918     FT_Fixed    temp[6];
919     FT_Fixed    temp_scale;
920
921
922     if ( matrix->xx || matrix->yx )
923       /* with synthetic fonts, it's possible we get here twice  */
924       return;
925
926     (void)T1_ToFixedArray( parser, 6, temp, 3 );
927
928     temp_scale = ABS( temp[3] );
929
930     /* Set Units per EM based on FontMatrix values.  We set the value to */
931     /* 1000 / temp_scale, because temp_scale was already multiplied by   */
932     /* 1000 (in t1_tofixed, from psobjs.c).                              */
933
934     root->units_per_EM = (FT_UShort)( FT_DivFix( 1000 * 0x10000L,
935                                                  temp_scale ) >> 16 );
936
937     /* we need to scale the values by 1.0/temp_scale */
938     if ( temp_scale != 0x10000L )
939     {
940       temp[0] = FT_DivFix( temp[0], temp_scale );
941       temp[1] = FT_DivFix( temp[1], temp_scale );
942       temp[2] = FT_DivFix( temp[2], temp_scale );
943       temp[4] = FT_DivFix( temp[4], temp_scale );
944       temp[5] = FT_DivFix( temp[5], temp_scale );
945       temp[3] = 0x10000L;
946     }
947
948     matrix->xx = temp[0];
949     matrix->yx = temp[1];
950     matrix->xy = temp[2];
951     matrix->yy = temp[3];
952
953     /* note that the offsets must be expressed in integer font units */
954     offset->x  = temp[4] >> 16;
955     offset->y  = temp[5] >> 16;
956   }
957
958
959   static void
960   parse_encoding( T1_Face    face,
961                   T1_Loader  loader )
962   {
963     T1_Parser      parser = &loader->parser;
964     FT_Byte*       cur    = parser->root.cursor;
965     FT_Byte*       limit  = parser->root.limit;
966
967     PSAux_Service  psaux  = (PSAux_Service)face->psaux;
968
969
970     /* skip whitespace */
971     while ( is_space( *cur ) )
972     {
973       cur++;
974       if ( cur >= limit )
975       {
976         FT_ERROR(( "parse_encoding: out of bounds!\n" ));
977         parser->root.error = T1_Err_Invalid_File_Format;
978         return;
979       }
980     }
981
982     /* if we have a number, then the encoding is an array, */
983     /* and we must load it now                             */
984     if ( (FT_Byte)( *cur - '0' ) < 10 )
985     {
986       T1_Encoding  encode     = &face->type1.encoding;
987       FT_Int       count, n;
988       PS_Table     char_table = &loader->encoding_table;
989       FT_Memory    memory     = parser->root.memory;
990       FT_Error     error;
991
992
993       if ( encode->char_index )
994         /*  with synthetic fonts, it's possible we get here twice  */
995         return;
996
997       /* read the number of entries in the encoding, should be 256 */
998       count = (FT_Int)T1_ToInt( parser );
999       if ( parser->root.error )
1000         return;
1001
1002       /* we use a T1_Table to store our charnames */
1003       loader->num_chars = encode->num_chars = count;
1004       if ( FT_NEW_ARRAY( encode->char_index, count ) ||
1005            FT_NEW_ARRAY( encode->char_name,  count ) ||
1006            FT_SET_ERROR( psaux->ps_table_funcs->init(
1007                            char_table, count, memory ) ) )
1008       {
1009         parser->root.error = error;
1010         return;
1011       }
1012
1013       /* We need to `zero' out encoding_table.elements */
1014       for ( n = 0; n < count; n++ )
1015       {
1016         char*  notdef = (char *)".notdef";
1017
1018
1019         T1_Add_Table( char_table, n, notdef, 8 );
1020       }
1021
1022       /* Now, we will need to read a record of the form         */
1023       /* ... charcode /charname ... for each entry in our table */
1024       /*                                                        */
1025       /* We simply look for a number followed by an immediate   */
1026       /* name.  Note that this ignores correctly the sequence   */
1027       /* that is often seen in type1 fonts:                     */
1028       /*                                                        */
1029       /*   0 1 255 { 1 index exch /.notdef put } for dup        */
1030       /*                                                        */
1031       /* used to clean the encoding array before anything else. */
1032       /*                                                        */
1033       /* We stop when we encounter a `def'.                     */
1034
1035       cur   = parser->root.cursor;
1036       limit = parser->root.limit;
1037       n     = 0;
1038
1039       for ( ; cur < limit; )
1040       {
1041         FT_Byte  c;
1042
1043
1044         c = *cur;
1045
1046         /* we stop when we encounter a `def' */
1047         if ( c == 'd' && cur + 3 < limit )
1048         {
1049           if ( cur[1] == 'e'       &&
1050                cur[2] == 'f'       &&
1051                is_space( cur[-1] ) &&
1052                is_space( cur[3] )  )
1053           {
1054             FT_TRACE6(( "encoding end\n" ));
1055             break;
1056           }
1057         }
1058
1059         /* otherwise, we must find a number before anything else */
1060         if ( (FT_Byte)( c - '0' ) < 10 )
1061         {
1062           FT_Int  charcode;
1063
1064
1065           parser->root.cursor = cur;
1066           charcode = (FT_Int)T1_ToInt( parser );
1067           cur      = parser->root.cursor;
1068
1069           /* skip whitespace */
1070           while ( cur < limit && is_space( *cur ) )
1071             cur++;
1072
1073           if ( cur < limit && *cur == '/' )
1074           {
1075             /* bingo, we have an immediate name -- it must be a */
1076             /* character name                                   */
1077             FT_Byte*    cur2 = cur + 1;
1078             FT_PtrDist  len;
1079
1080
1081             while ( cur2 < limit && is_name_char( *cur2 ) )
1082               cur2++;
1083
1084             len = cur2 - cur - 1;
1085
1086             parser->root.error = T1_Add_Table( char_table, charcode,
1087                                                cur + 1, len + 1 );
1088             char_table->elements[charcode][len] = '\0';
1089             if ( parser->root.error )
1090               return;
1091
1092             cur = cur2;
1093           }
1094         }
1095         else
1096           cur++;
1097       }
1098
1099       face->type1.encoding_type = T1_ENCODING_TYPE_ARRAY;
1100       parser->root.cursor       = cur;
1101     }
1102     /* Otherwise, we should have either `StandardEncoding', */
1103     /* `ExpertEncoding', or `ISOLatin1Encoding'             */
1104     else
1105     {
1106       if ( cur + 17 < limit                                            &&
1107            ft_strncmp( (const char*)cur, "StandardEncoding", 16 ) == 0 )
1108         face->type1.encoding_type = T1_ENCODING_TYPE_STANDARD;
1109
1110       else if ( cur + 15 < limit                                          &&
1111                 ft_strncmp( (const char*)cur, "ExpertEncoding", 14 ) == 0 )
1112         face->type1.encoding_type = T1_ENCODING_TYPE_EXPERT;
1113
1114       else if ( cur + 18 < limit                                             &&
1115                 ft_strncmp( (const char*)cur, "ISOLatin1Encoding", 17 ) == 0 )
1116         face->type1.encoding_type = T1_ENCODING_TYPE_ISOLATIN1;
1117
1118       else
1119       {
1120         FT_ERROR(( "parse_encoding: invalid token!\n" ));
1121         parser->root.error = T1_Err_Invalid_File_Format;
1122       }
1123     }
1124   }
1125
1126
1127   static void
1128   parse_subrs( T1_Face    face,
1129                T1_Loader  loader )
1130   {
1131     T1_Parser      parser = &loader->parser;
1132     PS_Table       table  = &loader->subrs;
1133     FT_Memory      memory = parser->root.memory;
1134     FT_Error       error;
1135     FT_Int         n;
1136
1137     PSAux_Service  psaux  = (PSAux_Service)face->psaux;
1138
1139
1140     if ( loader->num_subrs )
1141       /*  with synthetic fonts, it's possible we get here twice  */
1142       return;
1143
1144     if ( parser->root.cursor + 2 > parser->root.limit &&
1145          parser->root.cursor[0] == '['                &&
1146          parser->root.cursor[1] == ']'                )
1147     {
1148       /* empty array */
1149       return;
1150     }
1151
1152     loader->num_subrs = (FT_Int)T1_ToInt( parser );
1153     if ( parser->root.error )
1154       return;
1155
1156     /* position the parser right before the `dup' of the first subr */
1157     T1_Skip_Spaces( parser );
1158     T1_Skip_Alpha( parser );      /* `array' */
1159     T1_Skip_Spaces( parser );
1160
1161     /* initialize subrs array */
1162     error = psaux->ps_table_funcs->init( table, loader->num_subrs, memory );
1163     if ( error )
1164       goto Fail;
1165
1166     /* the format is simple:                                 */
1167     /*                                                       */
1168     /*   `index' + binary data                               */
1169     /*                                                       */
1170     for ( n = 0; n < loader->num_subrs; n++ )
1171     {
1172       FT_Long   idx, size;
1173       FT_Byte*  base;
1174
1175
1176       /* If the next token isn't `dup', we are also done.  This */
1177       /* happens when there are `holes' in the Subrs array.     */
1178       if ( ft_strncmp( (char*)parser->root.cursor, "dup", 3 ) != 0 )
1179         break;
1180
1181       idx = T1_ToInt( parser );
1182
1183       if ( !read_binary_data( parser, &size, &base ) )
1184         return;
1185
1186       /* The binary string is followed by one token, e.g. `NP' */
1187       /* (bound to `noaccess put') or by two separate tokens:  */
1188       /* `noaccess' & `put'.  We position the parser right     */
1189       /* before the next `dup', if any.                        */
1190       T1_Skip_Spaces( parser );
1191       T1_Skip_Alpha( parser );    /* `NP' or `I' or `noaccess' */
1192       T1_Skip_Spaces( parser );
1193
1194       if ( ft_strncmp( (char*)parser->root.cursor, "put", 3 ) == 0 )
1195       {
1196         T1_Skip_Alpha( parser );  /* skip `put' */
1197         T1_Skip_Spaces( parser );
1198       }
1199
1200       /* some fonts use a value of -1 for lenIV to indicate that */
1201       /* the charstrings are unencoded                           */
1202       /*                                                         */
1203       /* thanks to Tom Kacvinsky for pointing this out           */
1204       /*                                                         */
1205       if ( face->type1.private_dict.lenIV >= 0 )
1206       {
1207         FT_Byte*  temp;
1208
1209
1210         /* t1_decrypt() shouldn't write to base -- make temporary copy */
1211         if ( FT_ALLOC( temp, size ) )
1212           goto Fail;
1213         FT_MEM_COPY( temp, base, size );
1214         psaux->t1_decrypt( temp, size, 4330 );
1215         size -= face->type1.private_dict.lenIV;
1216         error = T1_Add_Table( table, idx,
1217                               temp + face->type1.private_dict.lenIV, size );
1218         FT_FREE( temp );
1219       }
1220       else
1221         error = T1_Add_Table( table, idx, base, size );
1222       if ( error )
1223         goto Fail;
1224     }
1225     return;
1226
1227   Fail:
1228     parser->root.error = error;
1229   }
1230
1231
1232   static void
1233   parse_charstrings( T1_Face    face,
1234                      T1_Loader  loader )
1235   {
1236     T1_Parser      parser       = &loader->parser;
1237     PS_Table       code_table   = &loader->charstrings;
1238     PS_Table       name_table   = &loader->glyph_names;
1239     PS_Table       swap_table   = &loader->swap_table;
1240     FT_Memory      memory       = parser->root.memory;
1241     FT_Error       error;
1242
1243     PSAux_Service  psaux        = (PSAux_Service)face->psaux;
1244
1245     FT_Byte*       cur;
1246     FT_Byte*       limit        = parser->root.limit;
1247     FT_Int         n;
1248     FT_UInt        notdef_index = 0;
1249     FT_Byte        notdef_found = 0;
1250
1251
1252     if ( loader->num_glyphs )
1253       /*  with synthetic fonts, it's possible we get here twice  */
1254       return;
1255
1256     loader->num_glyphs = (FT_Int)T1_ToInt( parser );
1257     if ( parser->root.error )
1258       return;
1259
1260     /* initialize tables (leaving room for addition of .notdef, */
1261     /* if necessary).                                           */
1262
1263     error = psaux->ps_table_funcs->init( code_table,
1264                                          loader->num_glyphs + 1,
1265                                          memory );
1266     if ( error )
1267       goto Fail;
1268
1269     error = psaux->ps_table_funcs->init( name_table,
1270                                          loader->num_glyphs + 1,
1271                                          memory );
1272     if ( error )
1273       goto Fail;
1274
1275     /* Initialize table for swapping index notdef_index and */
1276     /* index 0 names and codes (if necessary).              */
1277
1278     error = psaux->ps_table_funcs->init( swap_table, 4, memory );
1279
1280     if ( error )
1281       goto Fail;
1282
1283     n = 0;
1284
1285     for (;;)
1286     {
1287       FT_Long   size;
1288       FT_Byte*  base;
1289
1290
1291       /* the format is simple:                    */
1292       /*   `/glyphname' + binary data             */
1293       /*                                          */
1294       /* note that we stop when we find a `def'   */
1295       /*                                          */
1296       T1_Skip_Spaces( parser );
1297
1298       cur = parser->root.cursor;
1299       if ( cur >= limit )
1300         break;
1301
1302       /* we stop when we find a `def' or `end' keyword */
1303       if ( *cur   == 'd'   &&
1304            cur + 3 < limit &&
1305            cur[1] == 'e'   &&
1306            cur[2] == 'f'   )
1307         break;
1308
1309       if ( *cur   == 'e'   &&
1310            cur + 3 < limit &&
1311            cur[1] == 'n'   &&
1312            cur[2] == 'd'   )
1313         break;
1314
1315       if ( *cur != '/' )
1316         T1_Skip_Alpha( parser );
1317       else
1318       {
1319         FT_Byte*    cur2 = cur + 1;
1320         FT_PtrDist  len;
1321
1322
1323         while ( cur2 < limit && is_name_char( *cur2 ) )
1324           cur2++;
1325         len = cur2 - cur - 1;
1326
1327         error = T1_Add_Table( name_table, n, cur + 1, len + 1 );
1328         if ( error )
1329           goto Fail;
1330
1331         /* add a trailing zero to the name table */
1332         name_table->elements[n][len] = '\0';
1333
1334         /* record index of /.notdef              */
1335         if ( ft_strcmp( (const char*)".notdef",
1336                         (const char*)(name_table->elements[n]) ) == 0 )
1337         {
1338           notdef_index = n;
1339           notdef_found = 1;
1340         }
1341
1342         parser->root.cursor = cur2;
1343         if ( !read_binary_data( parser, &size, &base ) )
1344           return;
1345
1346         if ( face->type1.private_dict.lenIV >= 0 )
1347         {
1348           FT_Byte*  temp;
1349
1350
1351           /* t1_decrypt() shouldn't write to base -- make temporary copy */
1352           if ( FT_ALLOC( temp, size ) )
1353             goto Fail;
1354           FT_MEM_COPY( temp, base, size );
1355           psaux->t1_decrypt( temp, size, 4330 );
1356           size -= face->type1.private_dict.lenIV;
1357           error = T1_Add_Table( code_table, n,
1358                                 temp + face->type1.private_dict.lenIV, size );
1359           FT_FREE( temp );
1360         }
1361         else
1362           error = T1_Add_Table( code_table, n, base, size );
1363         if ( error )
1364           goto Fail;
1365
1366         n++;
1367         if ( n >= loader->num_glyphs )
1368           break;
1369       }
1370     }
1371
1372     loader->num_glyphs = n;
1373
1374     /* if /.notdef is found but does not occupy index 0, do our magic.      */
1375     if ( ft_strcmp( (const char*)".notdef",
1376                     (const char*)name_table->elements[0] ) &&
1377          notdef_found                                      )
1378     {
1379       /* Swap glyph in index 0 with /.notdef glyph.  First, add index 0    */
1380       /* name and code entries to swap_table. Then place notdef_index name */
1381       /* and code entries into swap_table.  Then swap name and code        */
1382       /* entries at indices notdef_index and 0 using values stored in      */
1383       /* swap_table.                                                       */
1384
1385       /* Index 0 name */
1386       error = T1_Add_Table( swap_table, 0,
1387                             name_table->elements[0],
1388                             name_table->lengths [0] );
1389       if ( error )
1390         goto Fail;
1391
1392       /* Index 0 code */
1393       error = T1_Add_Table( swap_table, 1,
1394                             code_table->elements[0],
1395                             code_table->lengths [0] );
1396       if ( error )
1397         goto Fail;
1398
1399       /* Index notdef_index name */
1400       error = T1_Add_Table( swap_table, 2,
1401                             name_table->elements[notdef_index],
1402                             name_table->lengths [notdef_index] );
1403       if ( error )
1404         goto Fail;
1405
1406       /* Index notdef_index code */
1407       error = T1_Add_Table( swap_table, 3,
1408                             code_table->elements[notdef_index],
1409                             code_table->lengths [notdef_index] );
1410       if ( error )
1411         goto Fail;
1412
1413       error = T1_Add_Table( name_table, notdef_index,
1414                             swap_table->elements[0],
1415                             swap_table->lengths [0] );
1416       if ( error )
1417         goto Fail;
1418
1419       error = T1_Add_Table( code_table, notdef_index,
1420                             swap_table->elements[1],
1421                             swap_table->lengths [1] );
1422       if ( error )
1423         goto Fail;
1424
1425       error = T1_Add_Table( name_table, 0,
1426                             swap_table->elements[2],
1427                             swap_table->lengths [2] );
1428       if ( error )
1429         goto Fail;
1430
1431       error = T1_Add_Table( code_table, 0,
1432                             swap_table->elements[3],
1433                             swap_table->lengths [3] );
1434       if ( error )
1435         goto Fail;
1436
1437     }
1438     else if ( !notdef_found )
1439     {
1440       /* notdef_index is already 0, or /.notdef is undefined in   */
1441       /* charstrings dictionary.  Worry about /.notdef undefined. */
1442       /* We take index 0 and add it to the end of the table(s)    */
1443       /* and add our own /.notdef glyph to index 0.               */
1444
1445       /* 0 333 hsbw endchar                                      */
1446       FT_Byte  notdef_glyph[] = {0x8B, 0xF7, 0xE1, 0x0D, 0x0E};
1447       char*    notdef_name    = (char *)".notdef";
1448
1449
1450       error = T1_Add_Table( swap_table, 0,
1451                             name_table->elements[0],
1452                             name_table->lengths [0] );
1453       if ( error )
1454         goto Fail;
1455
1456       error = T1_Add_Table( swap_table, 1,
1457                             code_table->elements[0],
1458                             code_table->lengths [0] );
1459       if ( error )
1460         goto Fail;
1461
1462       error = T1_Add_Table( name_table, 0, notdef_name, 8 );
1463       if ( error )
1464         goto Fail;
1465
1466       error = T1_Add_Table( code_table, 0, notdef_glyph, 5 );
1467
1468       if ( error )
1469         goto Fail;
1470
1471       error = T1_Add_Table( name_table, n,
1472                             swap_table->elements[0],
1473                             swap_table->lengths [0] );
1474       if ( error )
1475         goto Fail;
1476
1477       error = T1_Add_Table( code_table, n,
1478                             swap_table->elements[1],
1479                             swap_table->lengths [1] );
1480       if ( error )
1481         goto Fail;
1482
1483       /* we added a glyph. */
1484       loader->num_glyphs = n + 1;
1485     }
1486
1487     return;
1488
1489   Fail:
1490     parser->root.error = error;
1491   }
1492
1493
1494   static
1495   const T1_FieldRec  t1_keywords[] =
1496   {
1497
1498 #include "t1tokens.h"
1499
1500     /* now add the special functions... */
1501     T1_FIELD_CALLBACK( "FontName", parse_font_name )
1502 #if 0
1503     T1_FIELD_CALLBACK( "FontBBox", parse_font_bbox )
1504 #endif
1505     T1_FIELD_CALLBACK( "FontMatrix", parse_font_matrix )
1506     T1_FIELD_CALLBACK( "Encoding", parse_encoding )
1507     T1_FIELD_CALLBACK( "Subrs", parse_subrs )
1508     T1_FIELD_CALLBACK( "CharStrings", parse_charstrings )
1509
1510 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
1511     T1_FIELD_CALLBACK( "BlendDesignPositions", parse_blend_design_positions )
1512     T1_FIELD_CALLBACK( "BlendDesignMap", parse_blend_design_map )
1513     T1_FIELD_CALLBACK( "BlendAxisTypes", parse_blend_axis_types )
1514     T1_FIELD_CALLBACK( "WeightVector", parse_weight_vector )
1515     T1_FIELD_CALLBACK( "shareddict", parse_shared_dict )
1516 #endif
1517
1518     { 0, T1_FIELD_LOCATION_CID_INFO, T1_FIELD_TYPE_NONE, 0, 0, 0, 0, 0 }
1519   };
1520
1521
1522   static FT_Error
1523   parse_dict( T1_Face    face,
1524               T1_Loader  loader,
1525               FT_Byte*   base,
1526               FT_Long    size )
1527   {
1528     T1_Parser  parser = &loader->parser;
1529
1530
1531     parser->root.cursor = base;
1532     parser->root.limit  = base + size;
1533     parser->root.error  = 0;
1534
1535     {
1536       FT_Byte*  cur   = base;
1537       FT_Byte*  limit = cur + size;
1538
1539
1540       for ( ; cur < limit; cur++ )
1541       {
1542         /* look for `FontDirectory', which causes problems on some fonts */
1543         if ( *cur == 'F' && cur + 25 < limit                    &&
1544              ft_strncmp( (char*)cur, "FontDirectory", 13 ) == 0 )
1545         {
1546           FT_Byte*  cur2;
1547
1548
1549           /* skip the `FontDirectory' keyword */
1550           cur += 13;
1551           cur2 = cur;
1552
1553           /* lookup the `known' keyword */
1554           while ( cur < limit && *cur != 'k'           &&
1555                   ft_strncmp( (char*)cur, "known", 5 ) )
1556             cur++;
1557
1558           if ( cur < limit )
1559           {
1560             T1_TokenRec  token;
1561
1562
1563             /* skip the `known' keyword and the token following it */
1564             cur += 5;
1565             loader->parser.root.cursor = cur;
1566             T1_ToToken( &loader->parser, &token );
1567
1568             /* if the last token was an array, skip it! */
1569             if ( token.type == T1_TOKEN_TYPE_ARRAY )
1570               cur2 = parser->root.cursor;
1571           }
1572           cur = cur2;
1573         }
1574         /* look for immediates */
1575         else if ( *cur == '/' && cur + 2 < limit )
1576         {
1577           FT_Byte*    cur2;
1578           FT_PtrDist  len;
1579
1580
1581           cur++;
1582           cur2 = cur;
1583           while ( cur2 < limit && is_name_char( *cur2 ) )
1584             cur2++;
1585
1586           len = cur2 - cur;
1587           if ( len > 0 && len < 22 )
1588           {
1589             {
1590               /* now, compare the immediate name to the keyword table */
1591               T1_Field  keyword = (T1_Field)t1_keywords;
1592
1593
1594               for (;;)
1595               {
1596                 FT_Byte*  name;
1597
1598
1599                 name = (FT_Byte*)keyword->ident;
1600                 if ( !name )
1601                   break;
1602
1603                 if ( cur[0] == name[0]                     &&
1604                      len == ft_strlen( (const char*)name ) )
1605                 {
1606                   FT_PtrDist  n;
1607
1608
1609                   for ( n = 1; n < len; n++ )
1610                     if ( cur[n] != name[n] )
1611                       break;
1612
1613                   if ( n >= len )
1614                   {
1615                     /* we found it -- run the parsing callback! */
1616                     parser->root.cursor = cur2;
1617                     T1_Skip_Spaces( parser );
1618                     parser->root.error = t1_load_keyword( face,
1619                                                           loader,
1620                                                           keyword );
1621                     if ( parser->root.error )
1622                       return parser->root.error;
1623
1624                     cur = parser->root.cursor;
1625                     break;
1626                   }
1627                 }
1628                 keyword++;
1629               }
1630             }
1631           }
1632         }
1633       }
1634     }
1635     return parser->root.error;
1636   }
1637
1638
1639   static void
1640   t1_init_loader( T1_Loader  loader,
1641                   T1_Face    face )
1642   {
1643     FT_UNUSED( face );
1644
1645     FT_MEM_ZERO( loader, sizeof ( *loader ) );
1646     loader->num_glyphs = 0;
1647     loader->num_chars  = 0;
1648
1649     /* initialize the tables -- simply set their `init' field to 0 */
1650     loader->encoding_table.init = 0;
1651     loader->charstrings.init    = 0;
1652     loader->glyph_names.init    = 0;
1653     loader->subrs.init          = 0;
1654     loader->swap_table.init     = 0;
1655     loader->fontdata            = 0;
1656   }
1657
1658
1659   static void
1660   t1_done_loader( T1_Loader  loader )
1661   {
1662     T1_Parser  parser = &loader->parser;
1663
1664
1665     /* finalize tables */
1666     T1_Release_Table( &loader->encoding_table );
1667     T1_Release_Table( &loader->charstrings );
1668     T1_Release_Table( &loader->glyph_names );
1669     T1_Release_Table( &loader->swap_table );
1670     T1_Release_Table( &loader->subrs );
1671
1672     /* finalize parser */
1673     T1_Finalize_Parser( parser );
1674   }
1675
1676
1677   FT_LOCAL_DEF( FT_Error )
1678   T1_Open_Face( T1_Face  face )
1679   {
1680     T1_LoaderRec   loader;
1681     T1_Parser      parser;
1682     T1_Font        type1 = &face->type1;
1683     FT_Error       error;
1684
1685     PSAux_Service  psaux = (PSAux_Service)face->psaux;
1686
1687
1688     t1_init_loader( &loader, face );
1689
1690     /* default lenIV */
1691     type1->private_dict.lenIV = 4;
1692
1693     /* default blue fuzz, we put it there since 0 is a valid value */
1694     type1->private_dict.blue_fuzz = 1;
1695
1696     parser = &loader.parser;
1697     error  = T1_New_Parser( parser,
1698                             face->root.stream,
1699                             face->root.memory,
1700                             psaux );
1701     if ( error )
1702       goto Exit;
1703
1704     error = parse_dict( face, &loader, parser->base_dict, parser->base_len );
1705     if ( error )
1706       goto Exit;
1707
1708     error = T1_Get_Private_Dict( parser, psaux );
1709     if ( error )
1710       goto Exit;
1711
1712     error = parse_dict( face, &loader, parser->private_dict,
1713                         parser->private_len );
1714     if ( error )
1715       goto Exit;
1716
1717     /* now, propagate the subrs, charstrings, and glyphnames tables */
1718     /* to the Type1 data                                            */
1719     type1->num_glyphs = loader.num_glyphs;
1720
1721     if ( loader.subrs.init )
1722     {
1723       loader.subrs.init  = 0;
1724       type1->num_subrs   = loader.num_subrs;
1725       type1->subrs_block = loader.subrs.block;
1726       type1->subrs       = loader.subrs.elements;
1727       type1->subrs_len   = loader.subrs.lengths;
1728     }
1729
1730 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1731     if ( !face->root.internal->incremental_interface )
1732 #endif
1733       if ( !loader.charstrings.init )
1734       {
1735         FT_ERROR(( "T1_Open_Face: no charstrings array in face!\n" ));
1736         error = T1_Err_Invalid_File_Format;
1737       }
1738
1739     loader.charstrings.init  = 0;
1740     type1->charstrings_block = loader.charstrings.block;
1741     type1->charstrings       = loader.charstrings.elements;
1742     type1->charstrings_len   = loader.charstrings.lengths;
1743
1744     /* we copy the glyph names `block' and `elements' fields; */
1745     /* the `lengths' field must be released later             */
1746     type1->glyph_names_block    = loader.glyph_names.block;
1747     type1->glyph_names          = (FT_String**)loader.glyph_names.elements;
1748     loader.glyph_names.block    = 0;
1749     loader.glyph_names.elements = 0;
1750
1751     /* we must now build type1.encoding when we have a custom array */
1752     if ( type1->encoding_type == T1_ENCODING_TYPE_ARRAY )
1753     {
1754       FT_Int    charcode, idx, min_char, max_char;
1755       FT_Byte*  char_name;
1756       FT_Byte*  glyph_name;
1757
1758
1759       /* OK, we do the following: for each element in the encoding  */
1760       /* table, look up the index of the glyph having the same name */
1761       /* the index is then stored in type1.encoding.char_index, and */
1762       /* a the name to type1.encoding.char_name                     */
1763
1764       min_char = +32000;
1765       max_char = -32000;
1766
1767       charcode = 0;
1768       for ( ; charcode < loader.encoding_table.max_elems; charcode++ )
1769       {
1770         type1->encoding.char_index[charcode] = 0;
1771         type1->encoding.char_name [charcode] = (char *)".notdef";
1772
1773         char_name = loader.encoding_table.elements[charcode];
1774         if ( char_name )
1775           for ( idx = 0; idx < type1->num_glyphs; idx++ )
1776           {
1777             glyph_name = (FT_Byte*)type1->glyph_names[idx];
1778             if ( ft_strcmp( (const char*)char_name,
1779                             (const char*)glyph_name ) == 0 )
1780             {
1781               type1->encoding.char_index[charcode] = (FT_UShort)idx;
1782               type1->encoding.char_name [charcode] = (char*)glyph_name;
1783
1784               /* Change min/max encoded char only if glyph name is */
1785               /* not /.notdef                                      */
1786               if ( ft_strcmp( (const char*)".notdef",
1787                               (const char*)glyph_name ) != 0 )
1788               {
1789                 if ( charcode < min_char ) min_char = charcode;
1790                 if ( charcode > max_char ) max_char = charcode;
1791               }
1792               break;
1793             }
1794           }
1795       }
1796       type1->encoding.code_first = min_char;
1797       type1->encoding.code_last  = max_char;
1798       type1->encoding.num_chars  = loader.num_chars;
1799     }
1800
1801   Exit:
1802     t1_done_loader( &loader );
1803     return error;
1804   }
1805
1806
1807 /* END */