1 /****************************************************************************
2  *
3  * psaux.h
4  *
5  *   Auxiliary functions and data structures related to PostScript fonts
6  *   (specification).
7  *
8  * Copyright (C) 1996-2020 by
9  * David Turner, Robert Wilhelm, and Werner Lemberg.
10  *
11  * This file is part of the FreeType project, and may only be used,
12  * modified, and distributed under the terms of the FreeType project
13  * license, LICENSE.TXT.  By continuing to use, modify, or distribute
14  * this file you indicate that you have read the license and
15  * understand and accept it fully.
16  *
17  */
18 
19 
20 #ifndef PSAUX_H_
21 #define PSAUX_H_
22 
23 
24 #include <freetype/internal/ftobjs.h>
25 #include <freetype/internal/t1types.h>
26 #include <freetype/internal/fthash.h>
27 #include <freetype/internal/tttypes.h>
28 #include <freetype/internal/services/svpscmap.h>
29 #include <freetype/internal/cfftypes.h>
30 #include <freetype/internal/cffotypes.h>
31 
32 
33 
34 FT_BEGIN_HEADER
35 
36 
37   /**************************************************************************
38    *
39    * PostScript modules driver class.
40    */
41   typedef struct  PS_DriverRec_
42   {
43     FT_DriverRec  root;
44 
45     FT_UInt   hinting_engine;
46     FT_Bool   no_stem_darkening;
47     FT_Int    darken_params[8];
48     FT_Int32  random_seed;
49 
50   } PS_DriverRec, *PS_Driver;
51 
52 
53   /*************************************************************************/
54   /*************************************************************************/
55   /*****                                                               *****/
56   /*****                             T1_TABLE                          *****/
57   /*****                                                               *****/
58   /*************************************************************************/
59   /*************************************************************************/
60 
61 
62   typedef struct PS_TableRec_*              PS_Table;
63   typedef const struct PS_Table_FuncsRec_*  PS_Table_Funcs;
64 
65 
66   /**************************************************************************
67    *
68    * @struct:
69    *   PS_Table_FuncsRec
70    *
71    * @description:
72    *   A set of function pointers to manage PS_Table objects.
73    *
74    * @fields:
75    *   table_init ::
76    *     Used to initialize a table.
77    *
78    *   table_done ::
79    *     Finalizes resp. destroy a given table.
80    *
81    *   table_add ::
82    *     Adds a new object to a table.
83    *
84    *   table_release ::
85    *     Releases table data, then finalizes it.
86    */
87   typedef struct  PS_Table_FuncsRec_
88   {
89     FT_Error
90     (*init)( PS_Table   table,
91              FT_Int     count,
92              FT_Memory  memory );
93 
94     void
95     (*done)( PS_Table  table );
96 
97     FT_Error
98     (*add)( PS_Table     table,
99             FT_Int       idx,
100             const void*  object,
101             FT_UInt      length );
102 
103     void
104     (*release)( PS_Table  table );
105 
106   } PS_Table_FuncsRec;
107 
108 
109   /**************************************************************************
110    *
111    * @struct:
112    *   PS_TableRec
113    *
114    * @description:
115    *   A PS_Table is a simple object used to store an array of objects in a
116    *   single memory block.
117    *
118    * @fields:
119    *   block ::
120    *     The address in memory of the growheap's block.  This can change
121    *     between two object adds, due to reallocation.
122    *
123    *   cursor ::
124    *     The current top of the grow heap within its block.
125    *
126    *   capacity ::
127    *     The current size of the heap block.  Increments by 1kByte chunks.
128    *
129    *   init ::
130    *     Set to 0xDEADBEEF if 'elements' and 'lengths' have been allocated.
131    *
132    *   max_elems ::
133    *     The maximum number of elements in table.
134    *
135    *   num_elems ::
136    *     The current number of elements in table.
137    *
138    *   elements ::
139    *     A table of element addresses within the block.
140    *
141    *   lengths ::
142    *     A table of element sizes within the block.
143    *
144    *   memory ::
145    *     The object used for memory operations (alloc/realloc).
146    *
147    *   funcs ::
148    *     A table of method pointers for this object.
149    */
150   typedef struct  PS_TableRec_
151   {
152     FT_Byte*           block;          /* current memory block           */
153     FT_Offset          cursor;         /* current cursor in memory block */
154     FT_Offset          capacity;       /* current size of memory block   */
155     FT_ULong           init;
156 
157     FT_Int             max_elems;
158     FT_Int             num_elems;
159     FT_Byte**          elements;       /* addresses of table elements */
160     FT_UInt*           lengths;        /* lengths of table elements   */
161 
162     FT_Memory          memory;
163     PS_Table_FuncsRec  funcs;
164 
165   } PS_TableRec;
166 
167 
168   /*************************************************************************/
169   /*************************************************************************/
170   /*****                                                               *****/
171   /*****                       T1 FIELDS & TOKENS                      *****/
172   /*****                                                               *****/
173   /*************************************************************************/
174   /*************************************************************************/
175 
176   typedef struct PS_ParserRec_*  PS_Parser;
177 
178   typedef struct T1_TokenRec_*   T1_Token;
179 
180   typedef struct T1_FieldRec_*   T1_Field;
181 
182 
183   /* simple enumeration type used to identify token types */
184   typedef enum  T1_TokenType_
185   {
186     T1_TOKEN_TYPE_NONE = 0,
187     T1_TOKEN_TYPE_ANY,
188     T1_TOKEN_TYPE_STRING,
189     T1_TOKEN_TYPE_ARRAY,
190     T1_TOKEN_TYPE_KEY, /* aka `name' */
191 
192     /* do not remove */
193     T1_TOKEN_TYPE_MAX
194 
195   } T1_TokenType;
196 
197 
198   /* a simple structure used to identify tokens */
199   typedef struct  T1_TokenRec_
200   {
201     FT_Byte*      start;   /* first character of token in input stream */
202     FT_Byte*      limit;   /* first character after the token          */
203     T1_TokenType  type;    /* type of token                            */
204 
205   } T1_TokenRec;
206 
207 
208   /* enumeration type used to identify object fields */
209   typedef enum  T1_FieldType_
210   {
211     T1_FIELD_TYPE_NONE = 0,
212     T1_FIELD_TYPE_BOOL,
213     T1_FIELD_TYPE_INTEGER,
214     T1_FIELD_TYPE_FIXED,
215     T1_FIELD_TYPE_FIXED_1000,
216     T1_FIELD_TYPE_STRING,
217     T1_FIELD_TYPE_KEY,
218     T1_FIELD_TYPE_BBOX,
219     T1_FIELD_TYPE_MM_BBOX,
220     T1_FIELD_TYPE_INTEGER_ARRAY,
221     T1_FIELD_TYPE_FIXED_ARRAY,
222     T1_FIELD_TYPE_CALLBACK,
223 
224     /* do not remove */
225     T1_FIELD_TYPE_MAX
226 
227   } T1_FieldType;
228 
229 
230   typedef enum  T1_FieldLocation_
231   {
232     T1_FIELD_LOCATION_CID_INFO,
233     T1_FIELD_LOCATION_FONT_DICT,
234     T1_FIELD_LOCATION_FONT_EXTRA,
235     T1_FIELD_LOCATION_FONT_INFO,
236     T1_FIELD_LOCATION_PRIVATE,
237     T1_FIELD_LOCATION_BBOX,
238     T1_FIELD_LOCATION_LOADER,
239     T1_FIELD_LOCATION_FACE,
240     T1_FIELD_LOCATION_BLEND,
241 
242     /* do not remove */
243     T1_FIELD_LOCATION_MAX
244 
245   } T1_FieldLocation;
246 
247 
248   typedef void
249   (*T1_Field_ParseFunc)( FT_Face     face,
250                          FT_Pointer  parser );
251 
252 
253   /* structure type used to model object fields */
254   typedef struct  T1_FieldRec_
255   {
256     const char*         ident;        /* field identifier               */
257     T1_FieldLocation    location;
258     T1_FieldType        type;         /* type of field                  */
259     T1_Field_ParseFunc  reader;
260     FT_UInt             offset;       /* offset of field in object      */
261     FT_Byte             size;         /* size of field in bytes         */
262     FT_UInt             array_max;    /* maximum number of elements for */
263                                       /* array                          */
264     FT_UInt             count_offset; /* offset of element count for    */
265                                       /* arrays; must not be zero if in */
266                                       /* use -- in other words, a       */
267                                       /* `num_FOO' element must not     */
268                                       /* start the used structure if we */
269                                       /* parse a `FOO' array            */
270     FT_UInt             dict;         /* where we expect it             */
271   } T1_FieldRec;
272 
273 #define T1_FIELD_DICT_FONTDICT ( 1 << 0 ) /* also FontInfo and FDArray */
274 #define T1_FIELD_DICT_PRIVATE  ( 1 << 1 )
275 
276 
277 
278 #define T1_NEW_SIMPLE_FIELD( _ident, _type, _fname, _dict ) \
279           {                                                 \
280             _ident, T1CODE, _type,                          \
281             0,                                              \
282             FT_FIELD_OFFSET( _fname ),                      \
283             FT_FIELD_SIZE( _fname ),                        \
284             0, 0,                                           \
285             _dict                                           \
286           },
287 
288 #define T1_NEW_CALLBACK_FIELD( _ident, _reader, _dict ) \
289           {                                             \
290             _ident, T1CODE, T1_FIELD_TYPE_CALLBACK,     \
291             (T1_Field_ParseFunc)_reader,                \
292             0, 0,                                       \
293             0, 0,                                       \
294             _dict                                       \
295           },
296 
297 #define T1_NEW_TABLE_FIELD( _ident, _type, _fname, _max, _dict ) \
298           {                                                      \
299             _ident, T1CODE, _type,                               \
300             0,                                                   \
301             FT_FIELD_OFFSET( _fname ),                           \
302             FT_FIELD_SIZE_DELTA( _fname ),                       \
303             _max,                                                \
304             FT_FIELD_OFFSET( num_ ## _fname ),                   \
305             _dict                                                \
306           },
307 
308 #define T1_NEW_TABLE_FIELD2( _ident, _type, _fname, _max, _dict ) \
309           {                                                       \
310             _ident, T1CODE, _type,                                \
311             0,                                                    \
312             FT_FIELD_OFFSET( _fname ),                            \
313             FT_FIELD_SIZE_DELTA( _fname ),                        \
314             _max, 0,                                              \
315             _dict                                                 \
316           },
317 
318 
319 #define T1_FIELD_BOOL( _ident, _fname, _dict )                             \
320           T1_NEW_SIMPLE_FIELD( _ident, T1_FIELD_TYPE_BOOL, _fname, _dict )
321 
322 #define T1_FIELD_NUM( _ident, _fname, _dict )                                 \
323           T1_NEW_SIMPLE_FIELD( _ident, T1_FIELD_TYPE_INTEGER, _fname, _dict )
324 
325 #define T1_FIELD_FIXED( _ident, _fname, _dict )                             \
326           T1_NEW_SIMPLE_FIELD( _ident, T1_FIELD_TYPE_FIXED, _fname, _dict )
327 
328 #define T1_FIELD_FIXED_1000( _ident, _fname, _dict )                     \
329           T1_NEW_SIMPLE_FIELD( _ident, T1_FIELD_TYPE_FIXED_1000, _fname, \
330                                _dict )
331 
332 #define T1_FIELD_STRING( _ident, _fname, _dict )                             \
333           T1_NEW_SIMPLE_FIELD( _ident, T1_FIELD_TYPE_STRING, _fname, _dict )
334 
335 #define T1_FIELD_KEY( _ident, _fname, _dict )                             \
336           T1_NEW_SIMPLE_FIELD( _ident, T1_FIELD_TYPE_KEY, _fname, _dict )
337 
338 #define T1_FIELD_BBOX( _ident, _fname, _dict )                             \
339           T1_NEW_SIMPLE_FIELD( _ident, T1_FIELD_TYPE_BBOX, _fname, _dict )
340 
341 
342 #define T1_FIELD_NUM_TABLE( _ident, _fname, _fmax, _dict )         \
343           T1_NEW_TABLE_FIELD( _ident, T1_FIELD_TYPE_INTEGER_ARRAY, \
344                               _fname, _fmax, _dict )
345 
346 #define T1_FIELD_FIXED_TABLE( _ident, _fname, _fmax, _dict )     \
347           T1_NEW_TABLE_FIELD( _ident, T1_FIELD_TYPE_FIXED_ARRAY, \
348                               _fname, _fmax, _dict )
349 
350 #define T1_FIELD_NUM_TABLE2( _ident, _fname, _fmax, _dict )         \
351           T1_NEW_TABLE_FIELD2( _ident, T1_FIELD_TYPE_INTEGER_ARRAY, \
352                                _fname, _fmax, _dict )
353 
354 #define T1_FIELD_FIXED_TABLE2( _ident, _fname, _fmax, _dict )     \
355           T1_NEW_TABLE_FIELD2( _ident, T1_FIELD_TYPE_FIXED_ARRAY, \
356                                _fname, _fmax, _dict )
357 
358 #define T1_FIELD_CALLBACK( _ident, _name, _dict )       \
359           T1_NEW_CALLBACK_FIELD( _ident, _name, _dict )
360 
361 
362   /*************************************************************************/
363   /*************************************************************************/
364   /*****                                                               *****/
365   /*****                            T1 PARSER                          *****/
366   /*****                                                               *****/
367   /*************************************************************************/
368   /*************************************************************************/
369 
370   typedef const struct PS_Parser_FuncsRec_*  PS_Parser_Funcs;
371 
372   typedef struct  PS_Parser_FuncsRec_
373   {
374     void
375     (*init)( PS_Parser  parser,
376              FT_Byte*   base,
377              FT_Byte*   limit,
378              FT_Memory  memory );
379 
380     void
381     (*done)( PS_Parser  parser );
382 
383     void
384     (*skip_spaces)( PS_Parser  parser );
385     void
386     (*skip_PS_token)( PS_Parser  parser );
387 
388     FT_Long
389     (*to_int)( PS_Parser  parser );
390     FT_Fixed
391     (*to_fixed)( PS_Parser  parser,
392                  FT_Int     power_ten );
393 
394     FT_Error
395     (*to_bytes)( PS_Parser  parser,
396                  FT_Byte*   bytes,
397                  FT_Offset  max_bytes,
398                  FT_ULong*  pnum_bytes,
399                  FT_Bool    delimiters );
400 
401     FT_Int
402     (*to_coord_array)( PS_Parser  parser,
403                        FT_Int     max_coords,
404                        FT_Short*  coords );
405     FT_Int
406     (*to_fixed_array)( PS_Parser  parser,
407                        FT_Int     max_values,
408                        FT_Fixed*  values,
409                        FT_Int     power_ten );
410 
411     void
412     (*to_token)( PS_Parser  parser,
413                  T1_Token   token );
414     void
415     (*to_token_array)( PS_Parser  parser,
416                        T1_Token   tokens,
417                        FT_UInt    max_tokens,
418                        FT_Int*    pnum_tokens );
419 
420     FT_Error
421     (*load_field)( PS_Parser       parser,
422                    const T1_Field  field,
423                    void**          objects,
424                    FT_UInt         max_objects,
425                    FT_ULong*       pflags );
426 
427     FT_Error
428     (*load_field_table)( PS_Parser       parser,
429                          const T1_Field  field,
430                          void**          objects,
431                          FT_UInt         max_objects,
432                          FT_ULong*       pflags );
433 
434   } PS_Parser_FuncsRec;
435 
436 
437   /**************************************************************************
438    *
439    * @struct:
440    *   PS_ParserRec
441    *
442    * @description:
443    *   A PS_Parser is an object used to parse a Type 1 font very quickly.
444    *
445    * @fields:
446    *   cursor ::
447    *     The current position in the text.
448    *
449    *   base ::
450    *     Start of the processed text.
451    *
452    *   limit ::
453    *     End of the processed text.
454    *
455    *   error ::
456    *     The last error returned.
457    *
458    *   memory ::
459    *     The object used for memory operations (alloc/realloc).
460    *
461    *   funcs ::
462    *     A table of functions for the parser.
463    */
464   typedef struct  PS_ParserRec_
465   {
466     FT_Byte*   cursor;
467     FT_Byte*   base;
468     FT_Byte*   limit;
469     FT_Error   error;
470     FT_Memory  memory;
471 
472     PS_Parser_FuncsRec  funcs;
473 
474   } PS_ParserRec;
475 
476 
477   /*************************************************************************/
478   /*************************************************************************/
479   /*****                                                               *****/
480   /*****                         PS BUILDER                            *****/
481   /*****                                                               *****/
482   /*************************************************************************/
483   /*************************************************************************/
484 
485 
486   typedef struct PS_Builder_  PS_Builder;
487   typedef const struct PS_Builder_FuncsRec_*  PS_Builder_Funcs;
488 
489   typedef struct  PS_Builder_FuncsRec_
490   {
491     void
492     (*init)( PS_Builder*  ps_builder,
493              void*        builder,
494              FT_Bool      is_t1 );
495 
496     void
497     (*done)( PS_Builder*  builder );
498 
499   } PS_Builder_FuncsRec;
500 
501 
502   /**************************************************************************
503    *
504    * @struct:
505    *   PS_Builder
506    *
507    * @description:
508    *    A structure used during glyph loading to store its outline.
509    *
510    * @fields:
511    *   memory ::
512    *     The current memory object.
513    *
514    *   face ::
515    *     The current face object.
516    *
517    *   glyph ::
518    *     The current glyph slot.
519    *
520    *   loader ::
521    *     XXX
522    *
523    *   base ::
524    *     The base glyph outline.
525    *
526    *   current ::
527    *     The current glyph outline.
528    *
529    *   pos_x ::
530    *     The horizontal translation (if composite glyph).
531    *
532    *   pos_y ::
533    *     The vertical translation (if composite glyph).
534    *
535    *   left_bearing ::
536    *     The left side bearing point.
537    *
538    *   advance ::
539    *     The horizontal advance vector.
540    *
541    *   bbox ::
542    *     Unused.
543    *
544    *   path_begun ::
545    *     A flag which indicates that a new path has begun.
546    *
547    *   load_points ::
548    *     If this flag is not set, no points are loaded.
549    *
550    *   no_recurse ::
551    *     Set but not used.
552    *
553    *   metrics_only ::
554    *     A boolean indicating that we only want to compute the metrics of a
555    *     given glyph, not load all of its points.
556    *
557    *   is_t1 ::
558    *     Set if current font type is Type 1.
559    *
560    *   funcs ::
561    *     An array of function pointers for the builder.
562    */
563   struct  PS_Builder_
564   {
565     FT_Memory       memory;
566     FT_Face         face;
567     CFF_GlyphSlot   glyph;
568     FT_GlyphLoader  loader;
569     FT_Outline*     base;
570     FT_Outline*     current;
571 
572     FT_Pos*  pos_x;
573     FT_Pos*  pos_y;
574 
575     FT_Vector*  left_bearing;
576     FT_Vector*  advance;
577 
578     FT_BBox*  bbox;          /* bounding box */
579     FT_Bool   path_begun;
580     FT_Bool   load_points;
581     FT_Bool   no_recurse;
582 
583     FT_Bool  metrics_only;
584     FT_Bool  is_t1;
585 
586     PS_Builder_FuncsRec  funcs;
587 
588   };
589 
590 
591   /*************************************************************************/
592   /*************************************************************************/
593   /*****                                                               *****/
594   /*****                            PS DECODER                         *****/
595   /*****                                                               *****/
596   /*************************************************************************/
597   /*************************************************************************/
598 
599 #define PS_MAX_OPERANDS        48
600 #define PS_MAX_SUBRS_CALLS     16   /* maximum subroutine nesting;         */
601                                     /* only 10 are allowed but there exist */
602                                     /* fonts like `HiraKakuProN-W3.ttf'    */
603                                     /* (Hiragino Kaku Gothic ProN W3;      */
604                                     /* 8.2d6e1; 2014-12-19) that exceed    */
605                                     /* this limit                          */
606 
607   /* execution context charstring zone */
608 
609   typedef struct  PS_Decoder_Zone_
610   {
611     FT_Byte*  base;
612     FT_Byte*  limit;
613     FT_Byte*  cursor;
614 
615   } PS_Decoder_Zone;
616 
617 
618   typedef FT_Error
619   (*CFF_Decoder_Get_Glyph_Callback)( TT_Face    face,
620                                      FT_UInt    glyph_index,
621                                      FT_Byte**  pointer,
622                                      FT_ULong*  length );
623 
624   typedef void
625   (*CFF_Decoder_Free_Glyph_Callback)( TT_Face    face,
626                                       FT_Byte**  pointer,
627                                       FT_ULong   length );
628 
629 
630   typedef struct  PS_Decoder_
631   {
632     PS_Builder  builder;
633 
634     FT_Fixed   stack[PS_MAX_OPERANDS + 1];
635     FT_Fixed*  top;
636 
637     PS_Decoder_Zone   zones[PS_MAX_SUBRS_CALLS + 1];
638     PS_Decoder_Zone*  zone;
639 
640     FT_Int     flex_state;
641     FT_Int     num_flex_vectors;
642     FT_Vector  flex_vectors[7];
643 
644     CFF_Font     cff;
645     CFF_SubFont  current_subfont; /* for current glyph_index */
646     FT_Generic*  cf2_instance;
647 
648     FT_Pos*  glyph_width;
649     FT_Bool  width_only;
650     FT_Int   num_hints;
651 
652     FT_UInt  num_locals;
653     FT_UInt  num_globals;
654 
655     FT_Int  locals_bias;
656     FT_Int  globals_bias;
657 
658     FT_Byte**  locals;
659     FT_Byte**  globals;
660 
661     FT_Byte**  glyph_names;   /* for pure CFF fonts only  */
662     FT_UInt    num_glyphs;    /* number of glyphs in font */
663 
664     FT_Render_Mode  hint_mode;
665 
666     FT_Bool  seac;
667 
668     CFF_Decoder_Get_Glyph_Callback   get_glyph_callback;
669     CFF_Decoder_Free_Glyph_Callback  free_glyph_callback;
670 
671     /* Type 1 stuff */
672     FT_Service_PsCMaps  psnames;      /* for seac */
673 
674     FT_Int    lenIV;         /* internal for sub routine calls   */
675     FT_UInt*  locals_len;    /* array of subrs length (optional) */
676     FT_Hash   locals_hash;   /* used if `num_subrs' was massaged */
677 
678     FT_Matrix  font_matrix;
679     FT_Vector  font_offset;
680 
681     PS_Blend  blend;         /* for multiple master support */
682 
683     FT_Long*  buildchar;
684     FT_UInt   len_buildchar;
685 
686   } PS_Decoder;
687 
688 
689   /*************************************************************************/
690   /*************************************************************************/
691   /*****                                                               *****/
692   /*****                         T1 BUILDER                            *****/
693   /*****                                                               *****/
694   /*************************************************************************/
695   /*************************************************************************/
696 
697 
698   typedef struct T1_BuilderRec_*  T1_Builder;
699 
700 
701   typedef FT_Error
702   (*T1_Builder_Check_Points_Func)( T1_Builder  builder,
703                                    FT_Int      count );
704 
705   typedef void
706   (*T1_Builder_Add_Point_Func)( T1_Builder  builder,
707                                 FT_Pos      x,
708                                 FT_Pos      y,
709                                 FT_Byte     flag );
710 
711   typedef FT_Error
712   (*T1_Builder_Add_Point1_Func)( T1_Builder  builder,
713                                  FT_Pos      x,
714                                  FT_Pos      y );
715 
716   typedef FT_Error
717   (*T1_Builder_Add_Contour_Func)( T1_Builder  builder );
718 
719   typedef FT_Error
720   (*T1_Builder_Start_Point_Func)( T1_Builder  builder,
721                                   FT_Pos      x,
722                                   FT_Pos      y );
723 
724   typedef void
725   (*T1_Builder_Close_Contour_Func)( T1_Builder  builder );
726 
727 
728   typedef const struct T1_Builder_FuncsRec_*  T1_Builder_Funcs;
729 
730   typedef struct  T1_Builder_FuncsRec_
731   {
732     void
733     (*init)( T1_Builder    builder,
734              FT_Face       face,
735              FT_Size       size,
736              FT_GlyphSlot  slot,
737              FT_Bool       hinting );
738 
739     void
740     (*done)( T1_Builder   builder );
741 
742     T1_Builder_Check_Points_Func   check_points;
743     T1_Builder_Add_Point_Func      add_point;
744     T1_Builder_Add_Point1_Func     add_point1;
745     T1_Builder_Add_Contour_Func    add_contour;
746     T1_Builder_Start_Point_Func    start_point;
747     T1_Builder_Close_Contour_Func  close_contour;
748 
749   } T1_Builder_FuncsRec;
750 
751 
752   /* an enumeration type to handle charstring parsing states */
753   typedef enum  T1_ParseState_
754   {
755     T1_Parse_Start,
756     T1_Parse_Have_Width,
757     T1_Parse_Have_Moveto,
758     T1_Parse_Have_Path
759 
760   } T1_ParseState;
761 
762 
763   /**************************************************************************
764    *
765    * @struct:
766    *   T1_BuilderRec
767    *
768    * @description:
769    *    A structure used during glyph loading to store its outline.
770    *
771    * @fields:
772    *   memory ::
773    *     The current memory object.
774    *
775    *   face ::
776    *     The current face object.
777    *
778    *   glyph ::
779    *     The current glyph slot.
780    *
781    *   loader ::
782    *     XXX
783    *
784    *   base ::
785    *     The base glyph outline.
786    *
787    *   current ::
788    *     The current glyph outline.
789    *
790    *   max_points ::
791    *     maximum points in builder outline
792    *
793    *   max_contours ::
794    *     Maximum number of contours in builder outline.
795    *
796    *   pos_x ::
797    *     The horizontal translation (if composite glyph).
798    *
799    *   pos_y ::
800    *     The vertical translation (if composite glyph).
801    *
802    *   left_bearing ::
803    *     The left side bearing point.
804    *
805    *   advance ::
806    *     The horizontal advance vector.
807    *
808    *   bbox ::
809    *     Unused.
810    *
811    *   parse_state ::
812    *     An enumeration which controls the charstring parsing state.
813    *
814    *   load_points ::
815    *     If this flag is not set, no points are loaded.
816    *
817    *   no_recurse ::
818    *     Set but not used.
819    *
820    *   metrics_only ::
821    *     A boolean indicating that we only want to compute the metrics of a
822    *     given glyph, not load all of its points.
823    *
824    *   funcs ::
825    *     An array of function pointers for the builder.
826    */
827   typedef struct  T1_BuilderRec_
828   {
829     FT_Memory       memory;
830     FT_Face         face;
831     FT_GlyphSlot    glyph;
832     FT_GlyphLoader  loader;
833     FT_Outline*     base;
834     FT_Outline*     current;
835 
836     FT_Pos          pos_x;
837     FT_Pos          pos_y;
838 
839     FT_Vector       left_bearing;
840     FT_Vector       advance;
841 
842     FT_BBox         bbox;          /* bounding box */
843     T1_ParseState   parse_state;
844     FT_Bool         load_points;
845     FT_Bool         no_recurse;
846 
847     FT_Bool         metrics_only;
848 
849     void*           hints_funcs;    /* hinter-specific */
850     void*           hints_globals;  /* hinter-specific */
851 
852     T1_Builder_FuncsRec  funcs;
853 
854   } T1_BuilderRec;
855 
856 
857   /*************************************************************************/
858   /*************************************************************************/
859   /*****                                                               *****/
860   /*****                         T1 DECODER                            *****/
861   /*****                                                               *****/
862   /*************************************************************************/
863   /*************************************************************************/
864 
865 #if 0
866 
867   /**************************************************************************
868    *
869    * T1_MAX_SUBRS_CALLS details the maximum number of nested sub-routine
870    * calls during glyph loading.
871    */
872 #define T1_MAX_SUBRS_CALLS  8
873 
874 
875   /**************************************************************************
876    *
877    * T1_MAX_CHARSTRING_OPERANDS is the charstring stack's capacity.  A
878    * minimum of 16 is required.
879    */
880 #define T1_MAX_CHARSTRINGS_OPERANDS  32
881 
882 #endif /* 0 */
883 
884 
885   typedef struct  T1_Decoder_ZoneRec_
886   {
887     FT_Byte*  cursor;
888     FT_Byte*  base;
889     FT_Byte*  limit;
890 
891   } T1_Decoder_ZoneRec, *T1_Decoder_Zone;
892 
893 
894   typedef struct T1_DecoderRec_*              T1_Decoder;
895   typedef const struct T1_Decoder_FuncsRec_*  T1_Decoder_Funcs;
896 
897 
898   typedef FT_Error
899   (*T1_Decoder_Callback)( T1_Decoder  decoder,
900                           FT_UInt     glyph_index );
901 
902 
903   typedef struct  T1_Decoder_FuncsRec_
904   {
905     FT_Error
906     (*init)( T1_Decoder           decoder,
907              FT_Face              face,
908              FT_Size              size,
909              FT_GlyphSlot         slot,
910              FT_Byte**            glyph_names,
911              PS_Blend             blend,
912              FT_Bool              hinting,
913              FT_Render_Mode       hint_mode,
914              T1_Decoder_Callback  callback );
915 
916     void
917     (*done)( T1_Decoder  decoder );
918 
919 #ifdef T1_CONFIG_OPTION_OLD_ENGINE
920     FT_Error
921     (*parse_charstrings_old)( T1_Decoder  decoder,
922                               FT_Byte*    base,
923                               FT_UInt     len );
924 #else
925     FT_Error
926     (*parse_metrics)( T1_Decoder  decoder,
927                       FT_Byte*    base,
928                       FT_UInt     len );
929 #endif
930 
931     FT_Error
932     (*parse_charstrings)( PS_Decoder*  decoder,
933                           FT_Byte*     charstring_base,
934                           FT_ULong     charstring_len );
935 
936 
937   } T1_Decoder_FuncsRec;
938 
939 
940   typedef struct  T1_DecoderRec_
941   {
942     T1_BuilderRec        builder;
943 
944     FT_Long              stack[T1_MAX_CHARSTRINGS_OPERANDS];
945     FT_Long*             top;
946 
947     T1_Decoder_ZoneRec   zones[T1_MAX_SUBRS_CALLS + 1];
948     T1_Decoder_Zone      zone;
949 
950     FT_Service_PsCMaps   psnames;      /* for seac */
951     FT_UInt              num_glyphs;
952     FT_Byte**            glyph_names;
953 
954     FT_Int               lenIV;        /* internal for sub routine calls */
955     FT_Int               num_subrs;
956     FT_Byte**            subrs;
957     FT_UInt*             subrs_len;    /* array of subrs length (optional) */
958     FT_Hash              subrs_hash;   /* used if `num_subrs' was massaged */
959 
960     FT_Matrix            font_matrix;
961     FT_Vector            font_offset;
962 
963     FT_Int               flex_state;
964     FT_Int               num_flex_vectors;
965     FT_Vector            flex_vectors[7];
966 
967     PS_Blend             blend;       /* for multiple master support */
968 
969     FT_Render_Mode       hint_mode;
970 
971     T1_Decoder_Callback  parse_callback;
972     T1_Decoder_FuncsRec  funcs;
973 
974     FT_Long*             buildchar;
975     FT_UInt              len_buildchar;
976 
977     FT_Bool              seac;
978 
979     FT_Generic           cf2_instance;
980 
981   } T1_DecoderRec;
982 
983 
984   /*************************************************************************/
985   /*************************************************************************/
986   /*****                                                               *****/
987   /*****                        CFF BUILDER                            *****/
988   /*****                                                               *****/
989   /*************************************************************************/
990   /*************************************************************************/
991 
992 
993   typedef struct CFF_Builder_  CFF_Builder;
994 
995 
996   typedef FT_Error
997   (*CFF_Builder_Check_Points_Func)( CFF_Builder*  builder,
998                                     FT_Int        count );
999 
1000   typedef void
1001   (*CFF_Builder_Add_Point_Func)( CFF_Builder*  builder,
1002                                  FT_Pos        x,
1003                                  FT_Pos        y,
1004                                  FT_Byte       flag );
1005   typedef FT_Error
1006   (*CFF_Builder_Add_Point1_Func)( CFF_Builder*  builder,
1007                                   FT_Pos        x,
1008                                   FT_Pos        y );
1009   typedef FT_Error
1010   (*CFF_Builder_Start_Point_Func)( CFF_Builder*  builder,
1011                                    FT_Pos        x,
1012                                    FT_Pos        y );
1013   typedef void
1014   (*CFF_Builder_Close_Contour_Func)( CFF_Builder*  builder );
1015 
1016   typedef FT_Error
1017   (*CFF_Builder_Add_Contour_Func)( CFF_Builder*  builder );
1018 
1019   typedef const struct CFF_Builder_FuncsRec_*  CFF_Builder_Funcs;
1020 
1021   typedef struct  CFF_Builder_FuncsRec_
1022   {
1023     void
1024     (*init)( CFF_Builder*   builder,
1025              TT_Face        face,
1026              CFF_Size       size,
1027              CFF_GlyphSlot  glyph,
1028              FT_Bool        hinting );
1029 
1030     void
1031     (*done)( CFF_Builder*  builder );
1032 
1033     CFF_Builder_Check_Points_Func   check_points;
1034     CFF_Builder_Add_Point_Func      add_point;
1035     CFF_Builder_Add_Point1_Func     add_point1;
1036     CFF_Builder_Add_Contour_Func    add_contour;
1037     CFF_Builder_Start_Point_Func    start_point;
1038     CFF_Builder_Close_Contour_Func  close_contour;
1039 
1040   } CFF_Builder_FuncsRec;
1041 
1042 
1043   /**************************************************************************
1044    *
1045    * @struct:
1046    *   CFF_Builder
1047    *
1048    * @description:
1049    *    A structure used during glyph loading to store its outline.
1050    *
1051    * @fields:
1052    *   memory ::
1053    *     The current memory object.
1054    *
1055    *   face ::
1056    *     The current face object.
1057    *
1058    *   glyph ::
1059    *     The current glyph slot.
1060    *
1061    *   loader ::
1062    *     The current glyph loader.
1063    *
1064    *   base ::
1065    *     The base glyph outline.
1066    *
1067    *   current ::
1068    *     The current glyph outline.
1069    *
1070    *   pos_x ::
1071    *     The horizontal translation (if composite glyph).
1072    *
1073    *   pos_y ::
1074    *     The vertical translation (if composite glyph).
1075    *
1076    *   left_bearing ::
1077    *     The left side bearing point.
1078    *
1079    *   advance ::
1080    *     The horizontal advance vector.
1081    *
1082    *   bbox ::
1083    *     Unused.
1084    *
1085    *   path_begun ::
1086    *     A flag which indicates that a new path has begun.
1087    *
1088    *   load_points ::
1089    *     If this flag is not set, no points are loaded.
1090    *
1091    *   no_recurse ::
1092    *     Set but not used.
1093    *
1094    *   metrics_only ::
1095    *     A boolean indicating that we only want to compute the metrics of a
1096    *     given glyph, not load all of its points.
1097    *
1098    *   hints_funcs ::
1099    *     Auxiliary pointer for hinting.
1100    *
1101    *   hints_globals ::
1102    *     Auxiliary pointer for hinting.
1103    *
1104    *   funcs ::
1105    *     A table of method pointers for this object.
1106    */
1107   struct  CFF_Builder_
1108   {
1109     FT_Memory       memory;
1110     TT_Face         face;
1111     CFF_GlyphSlot   glyph;
1112     FT_GlyphLoader  loader;
1113     FT_Outline*     base;
1114     FT_Outline*     current;
1115 
1116     FT_Pos  pos_x;
1117     FT_Pos  pos_y;
1118 
1119     FT_Vector  left_bearing;
1120     FT_Vector  advance;
1121 
1122     FT_BBox  bbox;          /* bounding box */
1123 
1124     FT_Bool  path_begun;
1125     FT_Bool  load_points;
1126     FT_Bool  no_recurse;
1127 
1128     FT_Bool  metrics_only;
1129 
1130     void*  hints_funcs;     /* hinter-specific */
1131     void*  hints_globals;   /* hinter-specific */
1132 
1133     CFF_Builder_FuncsRec  funcs;
1134   };
1135 
1136 
1137   /*************************************************************************/
1138   /*************************************************************************/
1139   /*****                                                               *****/
1140   /*****                        CFF DECODER                            *****/
1141   /*****                                                               *****/
1142   /*************************************************************************/
1143   /*************************************************************************/
1144 
1145 
1146 #define CFF_MAX_OPERANDS        48
1147 #define CFF_MAX_SUBRS_CALLS     16  /* maximum subroutine nesting;         */
1148                                     /* only 10 are allowed but there exist */
1149                                     /* fonts like `HiraKakuProN-W3.ttf'    */
1150                                     /* (Hiragino Kaku Gothic ProN W3;      */
1151                                     /* 8.2d6e1; 2014-12-19) that exceed    */
1152                                     /* this limit                          */
1153 #define CFF_MAX_TRANS_ELEMENTS  32
1154 
1155   /* execution context charstring zone */
1156 
1157   typedef struct  CFF_Decoder_Zone_
1158   {
1159     FT_Byte*  base;
1160     FT_Byte*  limit;
1161     FT_Byte*  cursor;
1162 
1163   } CFF_Decoder_Zone;
1164 
1165 
1166   typedef struct  CFF_Decoder_
1167   {
1168     CFF_Builder  builder;
1169     CFF_Font     cff;
1170 
1171     FT_Fixed   stack[CFF_MAX_OPERANDS + 1];
1172     FT_Fixed*  top;
1173 
1174     CFF_Decoder_Zone   zones[CFF_MAX_SUBRS_CALLS + 1];
1175     CFF_Decoder_Zone*  zone;
1176 
1177     FT_Int     flex_state;
1178     FT_Int     num_flex_vectors;
1179     FT_Vector  flex_vectors[7];
1180 
1181     FT_Pos  glyph_width;
1182     FT_Pos  nominal_width;
1183 
1184     FT_Bool   read_width;
1185     FT_Bool   width_only;
1186     FT_Int    num_hints;
1187     FT_Fixed  buildchar[CFF_MAX_TRANS_ELEMENTS];
1188 
1189     FT_UInt  num_locals;
1190     FT_UInt  num_globals;
1191 
1192     FT_Int  locals_bias;
1193     FT_Int  globals_bias;
1194 
1195     FT_Byte**  locals;
1196     FT_Byte**  globals;
1197 
1198     FT_Byte**  glyph_names;   /* for pure CFF fonts only  */
1199     FT_UInt    num_glyphs;    /* number of glyphs in font */
1200 
1201     FT_Render_Mode  hint_mode;
1202 
1203     FT_Bool  seac;
1204 
1205     CFF_SubFont  current_subfont; /* for current glyph_index */
1206 
1207     CFF_Decoder_Get_Glyph_Callback   get_glyph_callback;
1208     CFF_Decoder_Free_Glyph_Callback  free_glyph_callback;
1209 
1210   } CFF_Decoder;
1211 
1212 
1213   typedef const struct CFF_Decoder_FuncsRec_*  CFF_Decoder_Funcs;
1214 
1215   typedef struct  CFF_Decoder_FuncsRec_
1216   {
1217     void
1218     (*init)( CFF_Decoder*                     decoder,
1219              TT_Face                          face,
1220              CFF_Size                         size,
1221              CFF_GlyphSlot                    slot,
1222              FT_Bool                          hinting,
1223              FT_Render_Mode                   hint_mode,
1224              CFF_Decoder_Get_Glyph_Callback   get_callback,
1225              CFF_Decoder_Free_Glyph_Callback  free_callback );
1226 
1227     FT_Error
1228     (*prepare)( CFF_Decoder*  decoder,
1229                 CFF_Size      size,
1230                 FT_UInt       glyph_index );
1231 
1232 #ifdef CFF_CONFIG_OPTION_OLD_ENGINE
1233     FT_Error
1234     (*parse_charstrings_old)( CFF_Decoder*  decoder,
1235                               FT_Byte*      charstring_base,
1236                               FT_ULong      charstring_len,
1237                               FT_Bool       in_dict );
1238 #endif
1239 
1240     FT_Error
1241     (*parse_charstrings)( PS_Decoder*  decoder,
1242                           FT_Byte*     charstring_base,
1243                           FT_ULong     charstring_len );
1244 
1245   } CFF_Decoder_FuncsRec;
1246 
1247 
1248   /*************************************************************************/
1249   /*************************************************************************/
1250   /*****                                                               *****/
1251   /*****                            AFM PARSER                         *****/
1252   /*****                                                               *****/
1253   /*************************************************************************/
1254   /*************************************************************************/
1255 
1256   typedef struct AFM_ParserRec_*  AFM_Parser;
1257 
1258   typedef struct  AFM_Parser_FuncsRec_
1259   {
1260     FT_Error
1261     (*init)( AFM_Parser  parser,
1262              FT_Memory   memory,
1263              FT_Byte*    base,
1264              FT_Byte*    limit );
1265 
1266     void
1267     (*done)( AFM_Parser  parser );
1268 
1269     FT_Error
1270     (*parse)( AFM_Parser  parser );
1271 
1272   } AFM_Parser_FuncsRec;
1273 
1274 
1275   typedef struct AFM_StreamRec_*  AFM_Stream;
1276 
1277 
1278   /**************************************************************************
1279    *
1280    * @struct:
1281    *   AFM_ParserRec
1282    *
1283    * @description:
1284    *   An AFM_Parser is a parser for the AFM files.
1285    *
1286    * @fields:
1287    *   memory ::
1288    *     The object used for memory operations (alloc and realloc).
1289    *
1290    *   stream ::
1291    *     This is an opaque object.
1292    *
1293    *   FontInfo ::
1294    *     The result will be stored here.
1295    *
1296    *   get_index ::
1297    *     A user provided function to get a glyph index by its name.
1298    */
1299   typedef struct  AFM_ParserRec_
1300   {
1301     FT_Memory     memory;
1302     AFM_Stream    stream;
1303 
1304     AFM_FontInfo  FontInfo;
1305 
1306     FT_Int
1307     (*get_index)( const char*  name,
1308                   FT_Offset    len,
1309                   void*        user_data );
1310 
1311     void*         user_data;
1312 
1313   } AFM_ParserRec;
1314 
1315 
1316   /*************************************************************************/
1317   /*************************************************************************/
1318   /*****                                                               *****/
1319   /*****                     TYPE1 CHARMAPS                            *****/
1320   /*****                                                               *****/
1321   /*************************************************************************/
1322   /*************************************************************************/
1323 
1324   typedef const struct T1_CMap_ClassesRec_*  T1_CMap_Classes;
1325 
1326   typedef struct T1_CMap_ClassesRec_
1327   {
1328     FT_CMap_Class  standard;
1329     FT_CMap_Class  expert;
1330     FT_CMap_Class  custom;
1331     FT_CMap_Class  unicode;
1332 
1333   } T1_CMap_ClassesRec;
1334 
1335 
1336   /*************************************************************************/
1337   /*************************************************************************/
1338   /*****                                                               *****/
1339   /*****                        PSAux Module Interface                 *****/
1340   /*****                                                               *****/
1341   /*************************************************************************/
1342   /*************************************************************************/
1343 
1344   typedef struct  PSAux_ServiceRec_
1345   {
1346     /* don't use `PS_Table_Funcs' and friends to avoid compiler warnings */
1347     const PS_Table_FuncsRec*    ps_table_funcs;
1348     const PS_Parser_FuncsRec*   ps_parser_funcs;
1349     const T1_Builder_FuncsRec*  t1_builder_funcs;
1350     const T1_Decoder_FuncsRec*  t1_decoder_funcs;
1351 
1352     void
1353     (*t1_decrypt)( FT_Byte*   buffer,
1354                    FT_Offset  length,
1355                    FT_UShort  seed );
1356 
1357     FT_UInt32
1358     (*cff_random)( FT_UInt32  r );
1359 
1360     void
1361     (*ps_decoder_init)( PS_Decoder*  ps_decoder,
1362                         void*        decoder,
1363                         FT_Bool      is_t1 );
1364 
1365     void
1366     (*t1_make_subfont)( FT_Face      face,
1367                         PS_Private   priv,
1368                         CFF_SubFont  subfont );
1369 
1370     T1_CMap_Classes  t1_cmap_classes;
1371 
1372     /* fields after this comment line were added after version 2.1.10 */
1373     const AFM_Parser_FuncsRec*  afm_parser_funcs;
1374 
1375     const CFF_Decoder_FuncsRec*  cff_decoder_funcs;
1376 
1377   } PSAux_ServiceRec, *PSAux_Service;
1378 
1379   /* backward compatible type definition */
1380   typedef PSAux_ServiceRec   PSAux_Interface;
1381 
1382 
1383   /*************************************************************************/
1384   /*************************************************************************/
1385   /*****                                                               *****/
1386   /*****                 Some convenience functions                    *****/
1387   /*****                                                               *****/
1388   /*************************************************************************/
1389   /*************************************************************************/
1390 
1391 #define IS_PS_NEWLINE( ch ) \
1392   ( (ch) == '\r' ||         \
1393     (ch) == '\n' )
1394 
1395 #define IS_PS_SPACE( ch )  \
1396   ( (ch) == ' '         || \
1397     IS_PS_NEWLINE( ch ) || \
1398     (ch) == '\t'        || \
1399     (ch) == '\f'        || \
1400     (ch) == '\0' )
1401 
1402 #define IS_PS_SPECIAL( ch )       \
1403   ( (ch) == '/'                || \
1404     (ch) == '(' || (ch) == ')' || \
1405     (ch) == '<' || (ch) == '>' || \
1406     (ch) == '[' || (ch) == ']' || \
1407     (ch) == '{' || (ch) == '}' || \
1408     (ch) == '%'                )
1409 
1410 #define IS_PS_DELIM( ch )  \
1411   ( IS_PS_SPACE( ch )   || \
1412     IS_PS_SPECIAL( ch ) )
1413 
1414 #define IS_PS_DIGIT( ch )        \
1415   ( (ch) >= '0' && (ch) <= '9' )
1416 
1417 #define IS_PS_XDIGIT( ch )            \
1418   ( IS_PS_DIGIT( ch )              || \
1419     ( (ch) >= 'A' && (ch) <= 'F' ) || \
1420     ( (ch) >= 'a' && (ch) <= 'f' ) )
1421 
1422 #define IS_PS_BASE85( ch )       \
1423   ( (ch) >= '!' && (ch) <= 'u' )
1424 
1425 #define IS_PS_TOKEN( cur, limit, token )                                \
1426   ( (char)(cur)[0] == (token)[0]                                     && \
1427     ( (cur) + sizeof ( (token) ) == (limit) ||                          \
1428       ( (cur) + sizeof( (token) ) < (limit)          &&                 \
1429         IS_PS_DELIM( (cur)[sizeof ( (token) ) - 1] ) ) )             && \
1430     ft_strncmp( (char*)(cur), (token), sizeof ( (token) ) - 1 ) == 0 )
1431 
1432 
1433 FT_END_HEADER
1434 
1435 #endif /* PSAUX_H_ */
1436 
1437 
1438 /* END */
1439