1 /***************************************************************************/
5 /* Type 1 font loader (body). */
7 /* Copyright 1996-2018 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
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. */
16 /***************************************************************************/
19 /*************************************************************************/
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. */
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 */
33 /* Indeed, nearly all data definitions follow a simple pattern like */
35 /* ... /Field <data> ... */
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. */
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. This file is included twice here; the first */
45 /* time to generate parsing callback functions, the second time to */
46 /* generate a table of keywords (with pointers to the associated */
47 /* callback functions). */
49 /* The function `parse_dict' simply scans *linearly* a given dictionary */
50 /* (either the top-level or private one) and calls the appropriate */
51 /* callback when it encounters an immediate keyword. */
53 /* This is by far the fastest way one can find to parse and read all */
56 /* This led to tremendous code size reduction. Note that later, the */
57 /* glyph loader will also be _greatly_ simplified, and the automatic */
58 /* hinter will replace the clumsy `t1hinter'. */
60 /*************************************************************************/
64 #include FT_INTERNAL_DEBUG_H
65 #include FT_CONFIG_CONFIG_H
66 #include FT_MULTIPLE_MASTERS_H
67 #include FT_INTERNAL_TYPE1_TYPES_H
68 #include FT_INTERNAL_CALC_H
69 #include FT_INTERNAL_HASH_H
75 #ifdef FT_CONFIG_OPTION_INCREMENTAL
76 #define IS_INCREMENTAL (FT_Bool)( face->root.internal->incremental_interface != 0 )
78 #define IS_INCREMENTAL 0
82 /*************************************************************************/
84 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
85 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
86 /* messages during execution. */
89 #define FT_COMPONENT trace_t1load
92 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
95 /*************************************************************************/
96 /*************************************************************************/
98 /***** MULTIPLE MASTERS SUPPORT *****/
100 /*************************************************************************/
101 /*************************************************************************/
104 t1_allocate_blend( T1_Face face,
109 FT_Memory memory = face->root.memory;
110 FT_Error error = FT_Err_Ok;
116 if ( FT_NEW( blend ) )
119 blend->num_default_design_vector = 0;
124 /* allocate design data if needed */
125 if ( num_designs > 0 )
127 if ( blend->num_designs == 0 )
132 /* allocate the blend `private' and `font_info' dictionaries */
133 if ( FT_NEW_ARRAY( blend->font_infos[1], num_designs ) ||
134 FT_NEW_ARRAY( blend->privates [1], num_designs ) ||
135 FT_NEW_ARRAY( blend->bboxes [1], num_designs ) ||
136 FT_NEW_ARRAY( blend->weight_vector, num_designs * 2 ) )
139 blend->default_weight_vector = blend->weight_vector + num_designs;
141 blend->font_infos[0] = &face->type1.font_info;
142 blend->privates [0] = &face->type1.private_dict;
143 blend->bboxes [0] = &face->type1.font_bbox;
145 for ( nn = 2; nn <= num_designs; nn++ )
147 blend->font_infos[nn] = blend->font_infos[nn - 1] + 1;
148 blend->privates [nn] = blend->privates [nn - 1] + 1;
149 blend->bboxes [nn] = blend->bboxes [nn - 1] + 1;
152 blend->num_designs = num_designs;
154 else if ( blend->num_designs != num_designs )
158 /* allocate axis data if needed */
161 if ( blend->num_axis != 0 && blend->num_axis != num_axis )
164 blend->num_axis = num_axis;
167 /* allocate the blend design pos table if needed */
168 num_designs = blend->num_designs;
169 num_axis = blend->num_axis;
170 if ( num_designs && num_axis && blend->design_pos[0] == 0 )
175 if ( FT_NEW_ARRAY( blend->design_pos[0], num_designs * num_axis ) )
178 for ( n = 1; n < num_designs; n++ )
179 blend->design_pos[n] = blend->design_pos[0] + num_axis * n;
186 error = FT_THROW( Invalid_File_Format );
191 FT_LOCAL_DEF( FT_Error )
192 T1_Get_Multi_Master( T1_Face face,
193 FT_Multi_Master* master )
195 PS_Blend blend = face->blend;
200 error = FT_THROW( Invalid_Argument );
204 master->num_axis = blend->num_axis;
205 master->num_designs = blend->num_designs;
207 for ( n = 0; n < blend->num_axis; n++ )
209 FT_MM_Axis* axis = master->axis + n;
210 PS_DesignMap map = blend->design_map + n;
213 axis->name = blend->axis_names[n];
214 axis->minimum = map->design_points[0];
215 axis->maximum = map->design_points[map->num_points - 1];
225 /*************************************************************************/
227 /* Given a normalized (blend) coordinate, figure out the design */
228 /* coordinate appropriate for that value. */
231 mm_axis_unmap( PS_DesignMap axismap,
237 if ( ncv <= axismap->blend_points[0] )
238 return INT_TO_FIXED( axismap->design_points[0] );
240 for ( j = 1; j < axismap->num_points; j++ )
242 if ( ncv <= axismap->blend_points[j] )
243 return INT_TO_FIXED( axismap->design_points[j - 1] ) +
244 ( axismap->design_points[j] - axismap->design_points[j - 1] ) *
245 FT_DivFix( ncv - axismap->blend_points[j - 1],
246 axismap->blend_points[j] -
247 axismap->blend_points[j - 1] );
250 return INT_TO_FIXED( axismap->design_points[axismap->num_points - 1] );
254 /*************************************************************************/
256 /* Given a vector of weights, one for each design, figure out the */
257 /* normalized axis coordinates which gave rise to those weights. */
260 mm_weights_unmap( FT_Fixed* weights,
261 FT_Fixed* axiscoords,
264 FT_ASSERT( axis_count <= T1_MAX_MM_AXIS );
266 if ( axis_count == 1 )
267 axiscoords[0] = weights[1];
269 else if ( axis_count == 2 )
271 axiscoords[0] = weights[3] + weights[1];
272 axiscoords[1] = weights[3] + weights[2];
275 else if ( axis_count == 3 )
277 axiscoords[0] = weights[7] + weights[5] + weights[3] + weights[1];
278 axiscoords[1] = weights[7] + weights[6] + weights[3] + weights[2];
279 axiscoords[2] = weights[7] + weights[6] + weights[5] + weights[4];
284 axiscoords[0] = weights[15] + weights[13] + weights[11] + weights[9] +
285 weights[7] + weights[5] + weights[3] + weights[1];
286 axiscoords[1] = weights[15] + weights[14] + weights[11] + weights[10] +
287 weights[7] + weights[6] + weights[3] + weights[2];
288 axiscoords[2] = weights[15] + weights[14] + weights[13] + weights[12] +
289 weights[7] + weights[6] + weights[5] + weights[4];
290 axiscoords[3] = weights[15] + weights[14] + weights[13] + weights[12] +
291 weights[11] + weights[10] + weights[9] + weights[8];
296 /*************************************************************************/
298 /* Just a wrapper around T1_Get_Multi_Master to support the different */
299 /* arguments needed by the GX var distortable fonts. */
301 FT_LOCAL_DEF( FT_Error )
302 T1_Get_MM_Var( T1_Face face,
305 FT_Memory memory = face->root.memory;
306 FT_MM_Var *mmvar = NULL;
307 FT_Multi_Master mmaster;
310 FT_Fixed axiscoords[T1_MAX_MM_AXIS];
311 PS_Blend blend = face->blend;
314 error = T1_Get_Multi_Master( face, &mmaster );
317 if ( FT_ALLOC( mmvar,
318 sizeof ( FT_MM_Var ) +
319 mmaster.num_axis * sizeof ( FT_Var_Axis ) ) )
322 mmvar->num_axis = mmaster.num_axis;
323 mmvar->num_designs = mmaster.num_designs;
324 mmvar->num_namedstyles = 0; /* Not supported */
325 mmvar->axis = (FT_Var_Axis*)&mmvar[1];
326 /* Point to axes after MM_Var struct */
327 mmvar->namedstyle = NULL;
329 for ( i = 0; i < mmaster.num_axis; i++ )
331 mmvar->axis[i].name = mmaster.axis[i].name;
332 mmvar->axis[i].minimum = INT_TO_FIXED( mmaster.axis[i].minimum );
333 mmvar->axis[i].maximum = INT_TO_FIXED( mmaster.axis[i].maximum );
334 mmvar->axis[i].def = ( mmvar->axis[i].minimum +
335 mmvar->axis[i].maximum ) / 2;
336 /* Does not apply. But this value is in range */
337 mmvar->axis[i].strid = ~0U; /* Does not apply */
338 mmvar->axis[i].tag = ~0U; /* Does not apply */
340 if ( !mmvar->axis[i].name )
343 if ( ft_strcmp( mmvar->axis[i].name, "Weight" ) == 0 )
344 mmvar->axis[i].tag = FT_MAKE_TAG( 'w', 'g', 'h', 't' );
345 else if ( ft_strcmp( mmvar->axis[i].name, "Width" ) == 0 )
346 mmvar->axis[i].tag = FT_MAKE_TAG( 'w', 'd', 't', 'h' );
347 else if ( ft_strcmp( mmvar->axis[i].name, "OpticalSize" ) == 0 )
348 mmvar->axis[i].tag = FT_MAKE_TAG( 'o', 'p', 's', 'z' );
351 if ( blend->num_designs == ( 1U << blend->num_axis ) )
353 mm_weights_unmap( blend->default_weight_vector,
357 for ( i = 0; i < mmaster.num_axis; i++ )
358 mmvar->axis[i].def = mm_axis_unmap( &blend->design_map[i],
370 t1_set_mm_blend( T1_Face face,
374 PS_Blend blend = face->blend;
377 FT_Bool have_diff = 0;
381 return FT_THROW( Invalid_Argument );
383 if ( num_coords > blend->num_axis )
384 num_coords = blend->num_axis;
386 /* recompute the weight vector from the blend coordinates */
387 for ( n = 0; n < blend->num_designs; n++ )
389 FT_Fixed result = 0x10000L; /* 1.0 fixed */
392 for ( m = 0; m < blend->num_axis; m++ )
397 /* get current blend axis position; */
398 /* use a default value if we don't have a coordinate */
399 factor = m < num_coords ? coords[m] : 0x8000;
402 if ( factor > 0x10000L )
405 if ( ( n & ( 1 << m ) ) == 0 )
406 factor = 0x10000L - factor;
408 result = FT_MulFix( result, factor );
411 if ( blend->weight_vector[n] != result )
413 blend->weight_vector[n] = result;
418 /* return value -1 indicates `no change' */
419 return have_diff ? FT_Err_Ok : -1;
423 FT_LOCAL_DEF( FT_Error )
424 T1_Set_MM_Blend( T1_Face face,
431 error = t1_set_mm_blend( face, num_coords, coords );
436 face->root.face_flags |= FT_FACE_FLAG_VARIATION;
438 face->root.face_flags &= ~FT_FACE_FLAG_VARIATION;
444 FT_LOCAL_DEF( FT_Error )
445 T1_Get_MM_Blend( T1_Face face,
449 PS_Blend blend = face->blend;
451 FT_Fixed axiscoords[4];
456 return FT_THROW( Invalid_Argument );
458 mm_weights_unmap( blend->weight_vector,
463 if ( num_coords > blend->num_axis )
465 FT_TRACE2(( "T1_Get_MM_Blend: only using first %d of %d coordinates\n",
466 blend->num_axis, num_coords ));
467 nc = blend->num_axis;
470 for ( i = 0; i < nc; i++ )
471 coords[i] = axiscoords[i];
472 for ( ; i < num_coords; i++ )
479 FT_LOCAL_DEF( FT_Error )
480 T1_Set_MM_Design( T1_Face face,
485 PS_Blend blend = face->blend;
487 FT_Fixed final_blends[T1_MAX_MM_DESIGNS];
491 return FT_THROW( Invalid_Argument );
493 if ( num_coords > blend->num_axis )
494 num_coords = blend->num_axis;
496 /* compute the blend coordinates through the blend design map */
498 for ( n = 0; n < blend->num_axis; n++ )
502 PS_DesignMap map = blend->design_map + n;
503 FT_Long* designs = map->design_points;
504 FT_Fixed* blends = map->blend_points;
505 FT_Int before = -1, after = -1;
508 /* use a default value if we don't have a coordinate */
509 if ( n < num_coords )
512 design = ( designs[map->num_points - 1] - designs[0] ) / 2;
514 for ( p = 0; p < (FT_UInt)map->num_points; p++ )
516 FT_Long p_design = designs[p];
520 if ( design == p_design )
522 the_blend = blends[p];
526 if ( design < p_design )
535 /* now interpolate if necessary */
537 the_blend = blends[0];
539 else if ( after < 0 )
540 the_blend = blends[map->num_points - 1];
543 the_blend = FT_MulDiv( design - designs[before],
544 blends [after] - blends [before],
545 designs[after] - designs[before] );
548 final_blends[n] = the_blend;
551 error = t1_set_mm_blend( face, blend->num_axis, final_blends );
556 face->root.face_flags |= FT_FACE_FLAG_VARIATION;
558 face->root.face_flags &= ~FT_FACE_FLAG_VARIATION;
564 /* MM fonts don't have named instances, so only the design is reset */
566 FT_LOCAL_DEF( FT_Error )
567 T1_Reset_MM_Blend( T1_Face face,
568 FT_UInt instance_index )
570 FT_UNUSED( instance_index );
572 return T1_Set_MM_Blend( face, 0, NULL );
576 /*************************************************************************/
578 /* Just a wrapper around T1_Set_MM_Design to support the different */
579 /* arguments needed by the GX var distortable fonts. */
581 FT_LOCAL_DEF( FT_Error )
582 T1_Set_Var_Design( T1_Face face,
586 FT_Long lcoords[T1_MAX_MM_AXIS];
590 if ( num_coords > T1_MAX_MM_AXIS )
591 num_coords = T1_MAX_MM_AXIS;
593 for ( i = 0; i < num_coords; i++ )
594 lcoords[i] = FIXED_TO_INT( coords[i] );
596 return T1_Set_MM_Design( face, num_coords, lcoords );
600 FT_LOCAL_DEF( FT_Error )
601 T1_Get_Var_Design( T1_Face face,
605 PS_Blend blend = face->blend;
607 FT_Fixed axiscoords[4];
612 return FT_THROW( Invalid_Argument );
614 mm_weights_unmap( blend->weight_vector,
619 if ( num_coords > blend->num_axis )
621 FT_TRACE2(( "T1_Get_Var_Design:"
622 " only using first %d of %d coordinates\n",
623 blend->num_axis, num_coords ));
624 nc = blend->num_axis;
627 for ( i = 0; i < nc; i++ )
628 coords[i] = mm_axis_unmap( &blend->design_map[i], axiscoords[i] );
629 for ( ; i < num_coords; i++ )
637 T1_Done_Blend( T1_Face face )
639 FT_Memory memory = face->root.memory;
640 PS_Blend blend = face->blend;
645 FT_UInt num_designs = blend->num_designs;
646 FT_UInt num_axis = blend->num_axis;
650 /* release design pos table */
651 FT_FREE( blend->design_pos[0] );
652 for ( n = 1; n < num_designs; n++ )
653 blend->design_pos[n] = NULL;
655 /* release blend `private' and `font info' dictionaries */
656 FT_FREE( blend->privates[1] );
657 FT_FREE( blend->font_infos[1] );
658 FT_FREE( blend->bboxes[1] );
660 for ( n = 0; n < num_designs; n++ )
662 blend->privates [n] = NULL;
663 blend->font_infos[n] = NULL;
664 blend->bboxes [n] = NULL;
667 /* release weight vectors */
668 FT_FREE( blend->weight_vector );
669 blend->default_weight_vector = NULL;
671 /* release axis names */
672 for ( n = 0; n < num_axis; n++ )
673 FT_FREE( blend->axis_names[n] );
675 /* release design map */
676 for ( n = 0; n < num_axis; n++ )
678 PS_DesignMap dmap = blend->design_map + n;
681 FT_FREE( dmap->design_points );
682 dmap->num_points = 0;
685 FT_FREE( face->blend );
691 parse_blend_axis_types( T1_Face face,
694 T1_TokenRec axis_tokens[T1_MAX_MM_AXIS];
696 FT_Error error = FT_Err_Ok;
701 /* take an array of objects */
702 T1_ToTokenArray( &loader->parser, axis_tokens,
703 T1_MAX_MM_AXIS, &num_axis );
706 error = FT_ERR( Ignore );
709 if ( num_axis == 0 || num_axis > T1_MAX_MM_AXIS )
711 FT_ERROR(( "parse_blend_axis_types: incorrect number of axes: %d\n",
713 error = FT_THROW( Invalid_File_Format );
717 /* allocate blend if necessary */
718 error = t1_allocate_blend( face, 0, (FT_UInt)num_axis );
723 memory = face->root.memory;
725 /* each token is an immediate containing the name of the axis */
726 for ( n = 0; n < num_axis; n++ )
728 T1_Token token = axis_tokens + n;
733 /* skip first slash, if any */
734 if ( token->start[0] == '/' )
737 len = (FT_UInt)( token->limit - token->start );
740 error = FT_THROW( Invalid_File_Format );
744 name = (FT_Byte*)blend->axis_names[n];
747 FT_TRACE0(( "parse_blend_axis_types:"
748 " overwriting axis name `%s' with `%*.s'\n",
749 name, len, token->start ));
753 if ( FT_ALLOC( blend->axis_names[n], len + 1 ) )
756 name = (FT_Byte*)blend->axis_names[n];
757 FT_MEM_COPY( name, token->start, len );
762 loader->parser.root.error = error;
767 parse_blend_design_positions( T1_Face face,
770 T1_TokenRec design_tokens[T1_MAX_MM_DESIGNS];
773 T1_Parser parser = &loader->parser;
775 FT_Error error = FT_Err_Ok;
779 /* get the array of design tokens -- compute number of designs */
780 T1_ToTokenArray( parser, design_tokens,
781 T1_MAX_MM_DESIGNS, &num_designs );
782 if ( num_designs < 0 )
784 error = FT_ERR( Ignore );
787 if ( num_designs == 0 || num_designs > T1_MAX_MM_DESIGNS )
789 FT_ERROR(( "parse_blend_design_positions:"
790 " incorrect number of designs: %d\n",
792 error = FT_THROW( Invalid_File_Format );
797 FT_Byte* old_cursor = parser->root.cursor;
798 FT_Byte* old_limit = parser->root.limit;
803 num_axis = 0; /* make compiler happy */
805 for ( n = 0; n < num_designs; n++ )
807 T1_TokenRec axis_tokens[T1_MAX_MM_AXIS];
812 /* read axis/coordinates tokens */
813 token = design_tokens + n;
814 parser->root.cursor = token->start;
815 parser->root.limit = token->limit;
816 T1_ToTokenArray( parser, axis_tokens, T1_MAX_MM_AXIS, &n_axis );
820 if ( n_axis <= 0 || n_axis > T1_MAX_MM_AXIS )
822 FT_ERROR(( "parse_blend_design_positions:"
823 " invalid number of axes: %d\n",
825 error = FT_THROW( Invalid_File_Format );
830 error = t1_allocate_blend( face,
831 (FT_UInt)num_designs,
837 else if ( n_axis != num_axis )
839 FT_ERROR(( "parse_blend_design_positions: incorrect table\n" ));
840 error = FT_THROW( Invalid_File_Format );
844 /* now read each axis token into the design position */
845 for ( axis = 0; axis < n_axis; axis++ )
847 T1_Token token2 = axis_tokens + axis;
850 parser->root.cursor = token2->start;
851 parser->root.limit = token2->limit;
852 blend->design_pos[n][axis] = T1_ToFixed( parser, 0 );
856 loader->parser.root.cursor = old_cursor;
857 loader->parser.root.limit = old_limit;
861 loader->parser.root.error = error;
866 parse_blend_design_map( T1_Face face,
869 FT_Error error = FT_Err_Ok;
870 T1_Parser parser = &loader->parser;
872 T1_TokenRec axis_tokens[T1_MAX_MM_AXIS];
876 FT_Memory memory = face->root.memory;
879 T1_ToTokenArray( parser, axis_tokens,
880 T1_MAX_MM_AXIS, &num_axis );
883 error = FT_ERR( Ignore );
886 if ( num_axis == 0 || num_axis > T1_MAX_MM_AXIS )
888 FT_ERROR(( "parse_blend_design_map: incorrect number of axes: %d\n",
890 error = FT_THROW( Invalid_File_Format );
894 old_cursor = parser->root.cursor;
895 old_limit = parser->root.limit;
897 error = t1_allocate_blend( face, 0, (FT_UInt)num_axis );
902 /* now read each axis design map */
903 for ( n = 0; n < num_axis; n++ )
905 PS_DesignMap map = blend->design_map + n;
907 T1_TokenRec point_tokens[T1_MAX_MM_MAP_POINTS];
908 FT_Int p, num_points;
911 axis_token = axis_tokens + n;
913 parser->root.cursor = axis_token->start;
914 parser->root.limit = axis_token->limit;
915 T1_ToTokenArray( parser, point_tokens,
916 T1_MAX_MM_MAP_POINTS, &num_points );
918 if ( num_points <= 0 || num_points > T1_MAX_MM_MAP_POINTS )
920 FT_ERROR(( "parse_blend_design_map: incorrect table\n" ));
921 error = FT_THROW( Invalid_File_Format );
925 if ( map->design_points )
927 FT_ERROR(( "parse_blend_design_map: duplicate table\n" ));
928 error = FT_THROW( Invalid_File_Format );
932 /* allocate design map data */
933 if ( FT_NEW_ARRAY( map->design_points, num_points * 2 ) )
935 map->blend_points = map->design_points + num_points;
936 map->num_points = (FT_Byte)num_points;
938 for ( p = 0; p < num_points; p++ )
940 T1_Token point_token;
943 point_token = point_tokens + p;
945 /* don't include delimiting brackets */
946 parser->root.cursor = point_token->start + 1;
947 parser->root.limit = point_token->limit - 1;
949 map->design_points[p] = T1_ToInt( parser );
950 map->blend_points [p] = T1_ToFixed( parser, 0 );
954 parser->root.cursor = old_cursor;
955 parser->root.limit = old_limit;
958 parser->root.error = error;
963 parse_weight_vector( T1_Face face,
966 T1_TokenRec design_tokens[T1_MAX_MM_DESIGNS];
968 FT_Error error = FT_Err_Ok;
969 T1_Parser parser = &loader->parser;
970 PS_Blend blend = face->blend;
977 T1_ToTokenArray( parser, design_tokens,
978 T1_MAX_MM_DESIGNS, &num_designs );
979 if ( num_designs < 0 )
981 error = FT_ERR( Ignore );
984 if ( num_designs == 0 || num_designs > T1_MAX_MM_DESIGNS )
986 FT_ERROR(( "parse_weight_vector:"
987 " incorrect number of designs: %d\n",
989 error = FT_THROW( Invalid_File_Format );
993 if ( !blend || !blend->num_designs )
995 error = t1_allocate_blend( face, (FT_UInt)num_designs, 0 );
1000 else if ( blend->num_designs != (FT_UInt)num_designs )
1002 FT_ERROR(( "parse_weight_vector:"
1003 " /BlendDesignPosition and /WeightVector have\n"
1005 " different number of elements\n" ));
1006 error = FT_THROW( Invalid_File_Format );
1010 old_cursor = parser->root.cursor;
1011 old_limit = parser->root.limit;
1013 for ( n = 0; n < num_designs; n++ )
1015 token = design_tokens + n;
1016 parser->root.cursor = token->start;
1017 parser->root.limit = token->limit;
1019 blend->default_weight_vector[n] =
1020 blend->weight_vector[n] = T1_ToFixed( parser, 0 );
1023 parser->root.cursor = old_cursor;
1024 parser->root.limit = old_limit;
1027 parser->root.error = error;
1031 /* e.g., /BuildCharArray [0 0 0 0 0 0 0 0] def */
1032 /* we're only interested in the number of array elements */
1034 parse_buildchar( T1_Face face,
1037 face->len_buildchar = (FT_UInt)T1_ToFixedArray( &loader->parser,
1043 #endif /* !T1_CONFIG_OPTION_NO_MM_SUPPORT */
1048 /*************************************************************************/
1049 /*************************************************************************/
1051 /***** TYPE 1 SYMBOL PARSING *****/
1053 /*************************************************************************/
1054 /*************************************************************************/
1057 t1_load_keyword( T1_Face face,
1059 const T1_Field field )
1064 FT_UInt max_objects;
1065 PS_Blend blend = face->blend;
1068 if ( blend && blend->num_designs == 0 )
1071 /* if the keyword has a dedicated callback, call it */
1072 if ( field->type == T1_FIELD_TYPE_CALLBACK )
1074 field->reader( (FT_Face)face, loader );
1075 error = loader->parser.root.error;
1079 /* now, the keyword is either a simple field, or a table of fields; */
1080 /* we are now going to take care of it */
1081 switch ( field->location )
1083 case T1_FIELD_LOCATION_FONT_INFO:
1084 dummy_object = &face->type1.font_info;
1085 objects = &dummy_object;
1090 objects = (void**)blend->font_infos;
1091 max_objects = blend->num_designs;
1095 case T1_FIELD_LOCATION_FONT_EXTRA:
1096 dummy_object = &face->type1.font_extra;
1097 objects = &dummy_object;
1101 case T1_FIELD_LOCATION_PRIVATE:
1102 dummy_object = &face->type1.private_dict;
1103 objects = &dummy_object;
1108 objects = (void**)blend->privates;
1109 max_objects = blend->num_designs;
1113 case T1_FIELD_LOCATION_BBOX:
1114 dummy_object = &face->type1.font_bbox;
1115 objects = &dummy_object;
1120 objects = (void**)blend->bboxes;
1121 max_objects = blend->num_designs;
1125 case T1_FIELD_LOCATION_LOADER:
1126 dummy_object = loader;
1127 objects = &dummy_object;
1131 case T1_FIELD_LOCATION_FACE:
1132 dummy_object = face;
1133 objects = &dummy_object;
1137 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
1138 case T1_FIELD_LOCATION_BLEND:
1139 dummy_object = face->blend;
1140 objects = &dummy_object;
1146 dummy_object = &face->type1;
1147 objects = &dummy_object;
1153 if ( field->type == T1_FIELD_TYPE_INTEGER_ARRAY ||
1154 field->type == T1_FIELD_TYPE_FIXED_ARRAY )
1155 error = T1_Load_Field_Table( &loader->parser, field,
1156 objects, max_objects, 0 );
1158 error = T1_Load_Field( &loader->parser, field,
1159 objects, max_objects, 0 );
1163 FT_TRACE1(( "t1_load_keyword: ignoring keyword `%s'"
1164 " which is not valid at this point\n"
1165 " (probably due to missing keywords)\n",
1176 parse_private( T1_Face face,
1181 loader->keywords_encountered |= T1_PRIVATE;
1185 /* return 1 in case of success */
1188 read_binary_data( T1_Parser parser,
1191 FT_Bool incremental )
1194 FT_Byte* limit = parser->root.limit;
1197 /* the binary data has one of the following formats */
1199 /* `size' [white*] RD white ....... ND */
1200 /* `size' [white*] -| white ....... |- */
1203 T1_Skip_Spaces( parser );
1205 cur = parser->root.cursor;
1207 if ( cur < limit && ft_isdigit( *cur ) )
1209 FT_Long s = T1_ToInt( parser );
1212 T1_Skip_PS_Token( parser ); /* `RD' or `-|' or something else */
1214 /* there is only one whitespace char after the */
1215 /* `RD' or `-|' token */
1216 *base = parser->root.cursor + 1;
1218 if ( s >= 0 && s < limit - *base )
1220 parser->root.cursor += s + 1;
1221 *size = (FT_ULong)s;
1222 return !parser->root.error;
1228 FT_ERROR(( "read_binary_data: invalid size field\n" ));
1229 parser->root.error = FT_THROW( Invalid_File_Format );
1236 /* We now define the routines to handle the `/Encoding', `/Subrs', */
1237 /* and `/CharStrings' dictionaries. */
1240 t1_parse_font_matrix( T1_Face face,
1243 T1_Parser parser = &loader->parser;
1244 FT_Matrix* matrix = &face->type1.font_matrix;
1245 FT_Vector* offset = &face->type1.font_offset;
1246 FT_Face root = (FT_Face)&face->root;
1248 FT_Fixed temp_scale;
1252 /* input is scaled by 1000 to accommodate default FontMatrix */
1253 result = T1_ToFixedArray( parser, 6, temp, 3 );
1257 parser->root.error = FT_THROW( Invalid_File_Format );
1261 temp_scale = FT_ABS( temp[3] );
1263 if ( temp_scale == 0 )
1265 FT_ERROR(( "t1_parse_font_matrix: invalid font matrix\n" ));
1266 parser->root.error = FT_THROW( Invalid_File_Format );
1271 if ( temp_scale != 0x10000L )
1273 /* set units per EM based on FontMatrix values */
1274 root->units_per_EM = (FT_UShort)FT_DivFix( 1000, temp_scale );
1276 temp[0] = FT_DivFix( temp[0], temp_scale );
1277 temp[1] = FT_DivFix( temp[1], temp_scale );
1278 temp[2] = FT_DivFix( temp[2], temp_scale );
1279 temp[4] = FT_DivFix( temp[4], temp_scale );
1280 temp[5] = FT_DivFix( temp[5], temp_scale );
1281 temp[3] = temp[3] < 0 ? -0x10000L : 0x10000L;
1284 matrix->xx = temp[0];
1285 matrix->yx = temp[1];
1286 matrix->xy = temp[2];
1287 matrix->yy = temp[3];
1289 /* note that the offsets must be expressed in integer font units */
1290 offset->x = temp[4] >> 16;
1291 offset->y = temp[5] >> 16;
1296 parse_encoding( T1_Face face,
1299 T1_Parser parser = &loader->parser;
1301 FT_Byte* limit = parser->root.limit;
1303 PSAux_Service psaux = (PSAux_Service)face->psaux;
1306 T1_Skip_Spaces( parser );
1307 cur = parser->root.cursor;
1310 FT_ERROR(( "parse_encoding: out of bounds\n" ));
1311 parser->root.error = FT_THROW( Invalid_File_Format );
1315 /* if we have a number or `[', the encoding is an array, */
1316 /* and we must load it now */
1317 if ( ft_isdigit( *cur ) || *cur == '[' )
1319 T1_Encoding encode = &face->type1.encoding;
1320 FT_Int count, array_size, n;
1321 PS_Table char_table = &loader->encoding_table;
1322 FT_Memory memory = parser->root.memory;
1324 FT_Bool only_immediates = 0;
1327 /* read the number of entries in the encoding; should be 256 */
1331 only_immediates = 1;
1332 parser->root.cursor++;
1335 count = (FT_Int)T1_ToInt( parser );
1340 FT_TRACE2(( "parse_encoding:"
1341 " only using first 256 encoding array entries\n" ));
1345 T1_Skip_Spaces( parser );
1346 if ( parser->root.cursor >= limit )
1349 /* PostScript happily allows overwriting of encoding arrays */
1350 if ( encode->char_index )
1352 FT_FREE( encode->char_index );
1353 FT_FREE( encode->char_name );
1354 T1_Release_Table( char_table );
1357 /* we use a T1_Table to store our charnames */
1358 loader->num_chars = encode->num_chars = array_size;
1359 if ( FT_NEW_ARRAY( encode->char_index, array_size ) ||
1360 FT_NEW_ARRAY( encode->char_name, array_size ) ||
1361 FT_SET_ERROR( psaux->ps_table_funcs->init(
1362 char_table, array_size, memory ) ) )
1364 parser->root.error = error;
1368 /* We need to `zero' out encoding_table.elements */
1369 for ( n = 0; n < array_size; n++ )
1371 char* notdef = (char *)".notdef";
1374 (void)T1_Add_Table( char_table, n, notdef, 8 );
1377 /* Now we need to read records of the form */
1379 /* ... charcode /charname ... */
1381 /* for each entry in our table. */
1383 /* We simply look for a number followed by an immediate */
1384 /* name. Note that this ignores correctly the sequence */
1385 /* that is often seen in type1 fonts: */
1387 /* 0 1 255 { 1 index exch /.notdef put } for dup */
1389 /* used to clean the encoding array before anything else. */
1391 /* Alternatively, if the array is directly given as */
1393 /* /Encoding [ ... ] */
1395 /* we only read immediates. */
1398 T1_Skip_Spaces( parser );
1400 while ( parser->root.cursor < limit )
1402 cur = parser->root.cursor;
1404 /* we stop when we encounter a `def' or `]' */
1405 if ( *cur == 'd' && cur + 3 < limit )
1407 if ( cur[1] == 'e' &&
1409 IS_PS_DELIM( cur[3] ) )
1411 FT_TRACE6(( "encoding end\n" ));
1418 FT_TRACE6(( "encoding end\n" ));
1423 /* check whether we've found an entry */
1424 if ( ft_isdigit( *cur ) || only_immediates )
1429 if ( only_immediates )
1433 charcode = (FT_Int)T1_ToInt( parser );
1434 T1_Skip_Spaces( parser );
1436 /* protect against invalid charcode */
1437 if ( cur == parser->root.cursor )
1439 parser->root.error = FT_THROW( Unknown_File_Format );
1444 cur = parser->root.cursor;
1446 if ( cur + 2 < limit && *cur == '/' && n < count )
1453 parser->root.cursor = cur;
1454 T1_Skip_PS_Token( parser );
1455 if ( parser->root.cursor >= limit )
1457 if ( parser->root.error )
1460 len = (FT_UInt)( parser->root.cursor - cur );
1462 if ( n < array_size )
1464 parser->root.error = T1_Add_Table( char_table, charcode,
1466 if ( parser->root.error )
1468 char_table->elements[charcode][len] = '\0';
1473 else if ( only_immediates )
1475 /* Since the current position is not updated for */
1476 /* immediates-only mode we would get an infinite loop if */
1477 /* we don't do anything here. */
1479 /* This encoding array is not valid according to the type1 */
1480 /* specification (it might be an encoding for a CID type1 */
1481 /* font, however), so we conclude that this font is NOT a */
1483 parser->root.error = FT_THROW( Unknown_File_Format );
1489 T1_Skip_PS_Token( parser );
1490 if ( parser->root.error )
1494 T1_Skip_Spaces( parser );
1497 face->type1.encoding_type = T1_ENCODING_TYPE_ARRAY;
1498 parser->root.cursor = cur;
1501 /* Otherwise, we should have either `StandardEncoding', */
1502 /* `ExpertEncoding', or `ISOLatin1Encoding' */
1505 if ( cur + 17 < limit &&
1506 ft_strncmp( (const char*)cur, "StandardEncoding", 16 ) == 0 )
1507 face->type1.encoding_type = T1_ENCODING_TYPE_STANDARD;
1509 else if ( cur + 15 < limit &&
1510 ft_strncmp( (const char*)cur, "ExpertEncoding", 14 ) == 0 )
1511 face->type1.encoding_type = T1_ENCODING_TYPE_EXPERT;
1513 else if ( cur + 18 < limit &&
1514 ft_strncmp( (const char*)cur, "ISOLatin1Encoding", 17 ) == 0 )
1515 face->type1.encoding_type = T1_ENCODING_TYPE_ISOLATIN1;
1518 parser->root.error = FT_ERR( Ignore );
1524 parse_subrs( T1_Face face,
1527 T1_Parser parser = &loader->parser;
1528 PS_Table table = &loader->subrs;
1529 FT_Memory memory = parser->root.memory;
1534 PSAux_Service psaux = (PSAux_Service)face->psaux;
1537 T1_Skip_Spaces( parser );
1539 /* test for empty array */
1540 if ( parser->root.cursor < parser->root.limit &&
1541 *parser->root.cursor == '[' )
1543 T1_Skip_PS_Token( parser );
1544 T1_Skip_Spaces ( parser );
1545 if ( parser->root.cursor >= parser->root.limit ||
1546 *parser->root.cursor != ']' )
1547 parser->root.error = FT_THROW( Invalid_File_Format );
1551 num_subrs = (FT_Int)T1_ToInt( parser );
1552 if ( num_subrs < 0 )
1554 parser->root.error = FT_THROW( Invalid_File_Format );
1558 /* we certainly need more than 8 bytes per subroutine */
1559 if ( parser->root.limit >= parser->root.cursor &&
1560 num_subrs > ( parser->root.limit - parser->root.cursor ) >> 3 )
1563 * There are two possibilities. Either the font contains an invalid
1564 * value for `num_subrs', or we have a subsetted font where the
1565 * subroutine indices are not adjusted, e.g.
1570 * dup 681 { ... } NP
1573 * In both cases, we use a number hash that maps from subr indices to
1574 * actual array elements.
1577 FT_TRACE0(( "parse_subrs: adjusting number of subroutines"
1578 " (from %d to %d)\n",
1580 ( parser->root.limit - parser->root.cursor ) >> 3 ));
1581 num_subrs = ( parser->root.limit - parser->root.cursor ) >> 3;
1583 if ( !loader->subrs_hash )
1585 if ( FT_NEW( loader->subrs_hash ) )
1588 error = ft_hash_num_init( loader->subrs_hash, memory );
1594 /* position the parser right before the `dup' of the first subr */
1595 T1_Skip_PS_Token( parser ); /* `array' */
1596 if ( parser->root.error )
1598 T1_Skip_Spaces( parser );
1600 /* initialize subrs array -- with synthetic fonts it is possible */
1601 /* we get here twice */
1602 if ( !loader->num_subrs )
1604 error = psaux->ps_table_funcs->init( table, num_subrs, memory );
1609 /* the format is simple: */
1611 /* `index' + binary data */
1613 for ( count = 0; ; count++ )
1620 /* If we are out of data, or if the next token isn't `dup', */
1622 if ( parser->root.cursor + 4 >= parser->root.limit ||
1623 ft_strncmp( (char*)parser->root.cursor, "dup", 3 ) != 0 )
1626 T1_Skip_PS_Token( parser ); /* `dup' */
1628 idx = T1_ToInt( parser );
1630 if ( !read_binary_data( parser, &size, &base, IS_INCREMENTAL ) )
1633 /* The binary string is followed by one token, e.g. `NP' */
1634 /* (bound to `noaccess put') or by two separate tokens: */
1635 /* `noaccess' & `put'. We position the parser right */
1636 /* before the next `dup', if any. */
1637 T1_Skip_PS_Token( parser ); /* `NP' or `|' or `noaccess' */
1638 if ( parser->root.error )
1640 T1_Skip_Spaces ( parser );
1642 if ( parser->root.cursor + 4 < parser->root.limit &&
1643 ft_strncmp( (char*)parser->root.cursor, "put", 3 ) == 0 )
1645 T1_Skip_PS_Token( parser ); /* skip `put' */
1646 T1_Skip_Spaces ( parser );
1649 /* if we use a hash, the subrs index is the key, and a running */
1650 /* counter specified for `T1_Add_Table' acts as the value */
1651 if ( loader->subrs_hash )
1653 ft_hash_num_insert( idx, count, loader->subrs_hash, memory );
1657 /* with synthetic fonts it is possible we get here twice */
1658 if ( loader->num_subrs )
1661 /* some fonts use a value of -1 for lenIV to indicate that */
1662 /* the charstrings are unencoded */
1664 /* thanks to Tom Kacvinsky for pointing this out */
1666 if ( face->type1.private_dict.lenIV >= 0 )
1668 FT_Byte* temp = NULL;
1671 /* some fonts define empty subr records -- this is not totally */
1672 /* compliant to the specification (which says they should at */
1673 /* least contain a `return'), but we support them anyway */
1674 if ( size < (FT_ULong)face->type1.private_dict.lenIV )
1676 error = FT_THROW( Invalid_File_Format );
1680 /* t1_decrypt() shouldn't write to base -- make temporary copy */
1681 if ( FT_ALLOC( temp, size ) )
1683 FT_MEM_COPY( temp, base, size );
1684 psaux->t1_decrypt( temp, size, 4330 );
1685 size -= (FT_ULong)face->type1.private_dict.lenIV;
1686 error = T1_Add_Table( table, (FT_Int)idx,
1687 temp + face->type1.private_dict.lenIV, size );
1691 error = T1_Add_Table( table, (FT_Int)idx, base, size );
1696 if ( !loader->num_subrs )
1697 loader->num_subrs = num_subrs;
1702 parser->root.error = error;
1706 #define TABLE_EXTEND 5
1710 parse_charstrings( T1_Face face,
1713 T1_Parser parser = &loader->parser;
1714 PS_Table code_table = &loader->charstrings;
1715 PS_Table name_table = &loader->glyph_names;
1716 PS_Table swap_table = &loader->swap_table;
1717 FT_Memory memory = parser->root.memory;
1720 PSAux_Service psaux = (PSAux_Service)face->psaux;
1722 FT_Byte* cur = parser->root.cursor;
1723 FT_Byte* limit = parser->root.limit;
1724 FT_Int n, num_glyphs;
1725 FT_Int notdef_index = 0;
1726 FT_Byte notdef_found = 0;
1729 num_glyphs = (FT_Int)T1_ToInt( parser );
1730 if ( num_glyphs < 0 )
1732 error = FT_THROW( Invalid_File_Format );
1736 /* we certainly need more than 8 bytes per glyph */
1737 if ( num_glyphs > ( limit - cur ) >> 3 )
1739 FT_TRACE0(( "parse_charstrings: adjusting number of glyphs"
1740 " (from %d to %d)\n",
1741 num_glyphs, ( limit - cur ) >> 3 ));
1742 num_glyphs = ( limit - cur ) >> 3;
1745 /* some fonts like Optima-Oblique not only define the /CharStrings */
1746 /* array but access it also */
1747 if ( num_glyphs == 0 || parser->root.error )
1750 /* initialize tables, leaving space for addition of .notdef, */
1751 /* if necessary, and a few other glyphs to handle buggy */
1752 /* fonts which have more glyphs than specified. */
1754 /* for some non-standard fonts like `Optima' which provides */
1755 /* different outlines depending on the resolution it is */
1756 /* possible to get here twice */
1757 if ( !loader->num_glyphs )
1759 error = psaux->ps_table_funcs->init(
1760 code_table, num_glyphs + 1 + TABLE_EXTEND, memory );
1764 error = psaux->ps_table_funcs->init(
1765 name_table, num_glyphs + 1 + TABLE_EXTEND, memory );
1769 /* Initialize table for swapping index notdef_index and */
1770 /* index 0 names and codes (if necessary). */
1772 error = psaux->ps_table_funcs->init( swap_table, 4, memory );
1785 /* the format is simple: */
1786 /* `/glyphname' + binary data */
1788 T1_Skip_Spaces( parser );
1790 cur = parser->root.cursor;
1794 /* we stop when we find a `def' or `end' keyword */
1795 if ( cur + 3 < limit && IS_PS_DELIM( cur[3] ) )
1797 if ( cur[0] == 'd' &&
1801 /* There are fonts which have this: */
1803 /* /CharStrings 118 dict def */
1805 /* CharStrings begin */
1808 /* To catch this we ignore `def' if */
1809 /* no charstring has actually been */
1815 if ( cur[0] == 'e' &&
1821 T1_Skip_PS_Token( parser );
1822 if ( parser->root.cursor >= limit )
1824 error = FT_THROW( Invalid_File_Format );
1827 if ( parser->root.error )
1835 if ( cur + 2 >= limit )
1837 error = FT_THROW( Invalid_File_Format );
1841 cur++; /* skip `/' */
1842 len = (FT_UInt)( parser->root.cursor - cur );
1844 if ( !read_binary_data( parser, &size, &base, IS_INCREMENTAL ) )
1847 /* for some non-standard fonts like `Optima' which provides */
1848 /* different outlines depending on the resolution it is */
1849 /* possible to get here twice */
1850 if ( loader->num_glyphs )
1853 error = T1_Add_Table( name_table, n, cur, len + 1 );
1857 /* add a trailing zero to the name table */
1858 name_table->elements[n][len] = '\0';
1860 /* record index of /.notdef */
1862 ft_strcmp( ".notdef",
1863 (const char*)(name_table->elements[n]) ) == 0 )
1869 if ( face->type1.private_dict.lenIV >= 0 &&
1870 n < num_glyphs + TABLE_EXTEND )
1872 FT_Byte* temp = NULL;
1875 if ( size <= (FT_ULong)face->type1.private_dict.lenIV )
1877 error = FT_THROW( Invalid_File_Format );
1881 /* t1_decrypt() shouldn't write to base -- make temporary copy */
1882 if ( FT_ALLOC( temp, size ) )
1884 FT_MEM_COPY( temp, base, size );
1885 psaux->t1_decrypt( temp, size, 4330 );
1886 size -= (FT_ULong)face->type1.private_dict.lenIV;
1887 error = T1_Add_Table( code_table, n,
1888 temp + face->type1.private_dict.lenIV, size );
1892 error = T1_Add_Table( code_table, n, base, size );
1902 error = FT_THROW( Invalid_File_Format );
1906 loader->num_glyphs = n;
1908 /* if /.notdef is found but does not occupy index 0, do our magic. */
1909 if ( notdef_found &&
1910 ft_strcmp( ".notdef", (const char*)name_table->elements[0] ) )
1912 /* Swap glyph in index 0 with /.notdef glyph. First, add index 0 */
1913 /* name and code entries to swap_table. Then place notdef_index */
1914 /* name and code entries into swap_table. Then swap name and code */
1915 /* entries at indices notdef_index and 0 using values stored in */
1919 error = T1_Add_Table( swap_table, 0,
1920 name_table->elements[0],
1921 name_table->lengths [0] );
1926 error = T1_Add_Table( swap_table, 1,
1927 code_table->elements[0],
1928 code_table->lengths [0] );
1932 /* Index notdef_index name */
1933 error = T1_Add_Table( swap_table, 2,
1934 name_table->elements[notdef_index],
1935 name_table->lengths [notdef_index] );
1939 /* Index notdef_index code */
1940 error = T1_Add_Table( swap_table, 3,
1941 code_table->elements[notdef_index],
1942 code_table->lengths [notdef_index] );
1946 error = T1_Add_Table( name_table, notdef_index,
1947 swap_table->elements[0],
1948 swap_table->lengths [0] );
1952 error = T1_Add_Table( code_table, notdef_index,
1953 swap_table->elements[1],
1954 swap_table->lengths [1] );
1958 error = T1_Add_Table( name_table, 0,
1959 swap_table->elements[2],
1960 swap_table->lengths [2] );
1964 error = T1_Add_Table( code_table, 0,
1965 swap_table->elements[3],
1966 swap_table->lengths [3] );
1971 else if ( !notdef_found )
1973 /* notdef_index is already 0, or /.notdef is undefined in */
1974 /* charstrings dictionary. Worry about /.notdef undefined. */
1975 /* We take index 0 and add it to the end of the table(s) */
1976 /* and add our own /.notdef glyph to index 0. */
1978 /* 0 333 hsbw endchar */
1979 FT_Byte notdef_glyph[] = { 0x8B, 0xF7, 0xE1, 0x0D, 0x0E };
1980 char* notdef_name = (char *)".notdef";
1983 error = T1_Add_Table( swap_table, 0,
1984 name_table->elements[0],
1985 name_table->lengths [0] );
1989 error = T1_Add_Table( swap_table, 1,
1990 code_table->elements[0],
1991 code_table->lengths [0] );
1995 error = T1_Add_Table( name_table, 0, notdef_name, 8 );
1999 error = T1_Add_Table( code_table, 0, notdef_glyph, 5 );
2004 error = T1_Add_Table( name_table, n,
2005 swap_table->elements[0],
2006 swap_table->lengths [0] );
2010 error = T1_Add_Table( code_table, n,
2011 swap_table->elements[1],
2012 swap_table->lengths [1] );
2016 /* we added a glyph. */
2017 loader->num_glyphs += 1;
2023 parser->root.error = error;
2027 /*************************************************************************/
2029 /* Define the token field static variables. This is a set of */
2030 /* T1_FieldRec variables. */
2032 /*************************************************************************/
2036 const T1_FieldRec t1_keywords[] =
2039 #include "t1tokens.h"
2041 /* now add the special functions... */
2042 T1_FIELD_CALLBACK( "FontMatrix", t1_parse_font_matrix,
2043 T1_FIELD_DICT_FONTDICT )
2044 T1_FIELD_CALLBACK( "Encoding", parse_encoding,
2045 T1_FIELD_DICT_FONTDICT )
2046 T1_FIELD_CALLBACK( "Subrs", parse_subrs,
2047 T1_FIELD_DICT_PRIVATE )
2048 T1_FIELD_CALLBACK( "CharStrings", parse_charstrings,
2049 T1_FIELD_DICT_PRIVATE )
2050 T1_FIELD_CALLBACK( "Private", parse_private,
2051 T1_FIELD_DICT_FONTDICT )
2053 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
2054 T1_FIELD_CALLBACK( "BlendDesignPositions", parse_blend_design_positions,
2055 T1_FIELD_DICT_FONTDICT )
2056 T1_FIELD_CALLBACK( "BlendDesignMap", parse_blend_design_map,
2057 T1_FIELD_DICT_FONTDICT )
2058 T1_FIELD_CALLBACK( "BlendAxisTypes", parse_blend_axis_types,
2059 T1_FIELD_DICT_FONTDICT )
2060 T1_FIELD_CALLBACK( "WeightVector", parse_weight_vector,
2061 T1_FIELD_DICT_FONTDICT )
2062 T1_FIELD_CALLBACK( "BuildCharArray", parse_buildchar,
2063 T1_FIELD_DICT_PRIVATE )
2066 { 0, T1_FIELD_LOCATION_CID_INFO, T1_FIELD_TYPE_NONE, 0, 0, 0, 0, 0, 0 }
2071 parse_dict( T1_Face face,
2076 T1_Parser parser = &loader->parser;
2077 FT_Byte *limit, *start_binary = NULL;
2078 FT_Bool have_integer = 0;
2081 parser->root.cursor = base;
2082 parser->root.limit = base + size;
2083 parser->root.error = FT_Err_Ok;
2085 limit = parser->root.limit;
2087 T1_Skip_Spaces( parser );
2089 while ( parser->root.cursor < limit )
2094 cur = parser->root.cursor;
2096 /* look for `eexec' */
2097 if ( IS_PS_TOKEN( cur, limit, "eexec" ) )
2100 /* look for `closefile' which ends the eexec section */
2101 else if ( IS_PS_TOKEN( cur, limit, "closefile" ) )
2104 /* in a synthetic font the base font starts after a */
2105 /* `FontDictionary' token that is placed after a Private dict */
2106 else if ( IS_PS_TOKEN( cur, limit, "FontDirectory" ) )
2108 if ( loader->keywords_encountered & T1_PRIVATE )
2109 loader->keywords_encountered |=
2110 T1_FONTDIR_AFTER_PRIVATE;
2111 parser->root.cursor += 13;
2114 /* check whether we have an integer */
2115 else if ( ft_isdigit( *cur ) )
2118 T1_Skip_PS_Token( parser );
2119 if ( parser->root.error )
2124 /* in valid Type 1 fonts we don't see `RD' or `-|' directly */
2125 /* since those tokens are handled by parse_subrs and */
2126 /* parse_charstrings */
2127 else if ( *cur == 'R' && cur + 6 < limit && *(cur + 1) == 'D' &&
2134 parser->root.cursor = start_binary;
2135 if ( !read_binary_data( parser, &s, &b, IS_INCREMENTAL ) )
2136 return FT_THROW( Invalid_File_Format );
2140 else if ( *cur == '-' && cur + 6 < limit && *(cur + 1) == '|' &&
2147 parser->root.cursor = start_binary;
2148 if ( !read_binary_data( parser, &s, &b, IS_INCREMENTAL ) )
2149 return FT_THROW( Invalid_File_Format );
2153 /* look for immediates */
2154 else if ( *cur == '/' && cur + 2 < limit )
2161 parser->root.cursor = cur;
2162 T1_Skip_PS_Token( parser );
2163 if ( parser->root.error )
2166 len = (FT_UInt)( parser->root.cursor - cur );
2168 if ( len > 0 && len < 22 && parser->root.cursor < limit )
2170 /* now compare the immediate name to the keyword table */
2171 T1_Field keyword = (T1_Field)t1_keywords;
2179 name = (FT_Byte*)keyword->ident;
2183 if ( cur[0] == name[0] &&
2184 len == ft_strlen( (const char *)name ) &&
2185 ft_memcmp( cur, name, len ) == 0 )
2187 /* We found it -- run the parsing callback! */
2188 /* We record every instance of every field */
2189 /* (until we reach the base font of a */
2190 /* synthetic font) to deal adequately with */
2191 /* multiple master fonts; this is also */
2192 /* necessary because later PostScript */
2193 /* definitions override earlier ones. */
2195 /* Once we encounter `FontDirectory' after */
2196 /* `/Private', we know that this is a synthetic */
2197 /* font; except for `/CharStrings' we are not */
2198 /* interested in anything that follows this */
2199 /* `FontDirectory'. */
2201 /* MM fonts have more than one /Private token at */
2202 /* the top level; let's hope that all the junk */
2203 /* that follows the first /Private token is not */
2204 /* interesting to us. */
2206 /* According to Adobe Tech Note #5175 (CID-Keyed */
2207 /* Font Installation for ATM Software) a `begin' */
2208 /* must be followed by exactly one `end', and */
2209 /* `begin' -- `end' pairs must be accurately */
2210 /* paired. We could use this to distinguish */
2211 /* between the global Private and the Private */
2212 /* dict that is a member of the Blend dict. */
2214 const FT_UInt dict =
2215 ( loader->keywords_encountered & T1_PRIVATE )
2216 ? T1_FIELD_DICT_PRIVATE
2217 : T1_FIELD_DICT_FONTDICT;
2219 if ( !( dict & keyword->dict ) )
2221 FT_TRACE1(( "parse_dict: found `%s' but ignoring it"
2222 " since it is in the wrong dictionary\n",
2227 if ( !( loader->keywords_encountered &
2228 T1_FONTDIR_AFTER_PRIVATE ) ||
2229 ft_strcmp( (const char*)name, "CharStrings" ) == 0 )
2231 parser->root.error = t1_load_keyword( face,
2234 if ( parser->root.error )
2236 if ( FT_ERR_EQ( parser->root.error, Ignore ) )
2237 parser->root.error = FT_Err_Ok;
2239 return parser->root.error;
2253 T1_Skip_PS_Token( parser );
2254 if ( parser->root.error )
2259 T1_Skip_Spaces( parser );
2263 return parser->root.error;
2268 t1_init_loader( T1_Loader loader,
2278 t1_done_loader( T1_Loader loader )
2280 T1_Parser parser = &loader->parser;
2281 FT_Memory memory = parser->root.memory;
2284 /* finalize tables */
2285 T1_Release_Table( &loader->encoding_table );
2286 T1_Release_Table( &loader->charstrings );
2287 T1_Release_Table( &loader->glyph_names );
2288 T1_Release_Table( &loader->swap_table );
2289 T1_Release_Table( &loader->subrs );
2292 ft_hash_num_free( loader->subrs_hash, memory );
2293 FT_FREE( loader->subrs_hash );
2295 /* finalize parser */
2296 T1_Finalize_Parser( parser );
2300 FT_LOCAL_DEF( FT_Error )
2301 T1_Open_Face( T1_Face face )
2303 T1_LoaderRec loader;
2305 T1_Font type1 = &face->type1;
2306 PS_Private priv = &type1->private_dict;
2309 PSAux_Service psaux = (PSAux_Service)face->psaux;
2312 t1_init_loader( &loader, face );
2314 /* default values */
2317 face->len_buildchar = 0;
2319 priv->blue_shift = 7;
2320 priv->blue_fuzz = 1;
2322 priv->expansion_factor = (FT_Fixed)( 0.06 * 0x10000L );
2323 priv->blue_scale = (FT_Fixed)( 0.039625 * 0x10000L * 1000 );
2325 parser = &loader.parser;
2326 error = T1_New_Parser( parser,
2333 error = parse_dict( face, &loader,
2334 parser->base_dict, parser->base_len );
2338 error = T1_Get_Private_Dict( parser, psaux );
2342 error = parse_dict( face, &loader,
2343 parser->private_dict, parser->private_len );
2347 /* ensure even-ness of `num_blue_values' */
2348 priv->num_blue_values &= ~1;
2350 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
2353 face->blend->num_default_design_vector != 0 &&
2354 face->blend->num_default_design_vector != face->blend->num_axis )
2356 /* we don't use it currently so just warn, reset, and ignore */
2357 FT_ERROR(( "T1_Open_Face(): /DesignVector contains %u entries "
2358 "while there are %u axes.\n",
2359 face->blend->num_default_design_vector,
2360 face->blend->num_axis ));
2362 face->blend->num_default_design_vector = 0;
2365 /* the following can happen for MM instances; we then treat the */
2366 /* font as a normal PS font */
2368 ( !face->blend->num_designs || !face->blend->num_axis ) )
2369 T1_Done_Blend( face );
2371 /* another safety check */
2377 for ( i = 0; i < face->blend->num_axis; i++ )
2378 if ( !face->blend->design_map[i].num_points )
2380 T1_Done_Blend( face );
2387 if ( face->len_buildchar > 0 )
2389 FT_Memory memory = face->root.memory;
2392 if ( FT_NEW_ARRAY( face->buildchar, face->len_buildchar ) )
2394 FT_ERROR(( "T1_Open_Face: cannot allocate BuildCharArray\n" ));
2395 face->len_buildchar = 0;
2401 face->len_buildchar = 0;
2403 #endif /* !T1_CONFIG_OPTION_NO_MM_SUPPORT */
2405 /* now, propagate the subrs, charstrings, and glyphnames tables */
2406 /* to the Type1 data */
2407 type1->num_glyphs = loader.num_glyphs;
2409 if ( loader.subrs.init )
2411 type1->num_subrs = loader.num_subrs;
2412 type1->subrs_block = loader.subrs.block;
2413 type1->subrs = loader.subrs.elements;
2414 type1->subrs_len = loader.subrs.lengths;
2415 type1->subrs_hash = loader.subrs_hash;
2417 /* prevent `t1_done_loader' from freeing the propagated data */
2418 loader.subrs.init = 0;
2419 loader.subrs_hash = NULL;
2422 if ( !IS_INCREMENTAL )
2423 if ( !loader.charstrings.init )
2425 FT_ERROR(( "T1_Open_Face: no `/CharStrings' array in face\n" ));
2426 error = FT_THROW( Invalid_File_Format );
2429 loader.charstrings.init = 0;
2430 type1->charstrings_block = loader.charstrings.block;
2431 type1->charstrings = loader.charstrings.elements;
2432 type1->charstrings_len = loader.charstrings.lengths;
2434 /* we copy the glyph names `block' and `elements' fields; */
2435 /* the `lengths' field must be released later */
2436 type1->glyph_names_block = loader.glyph_names.block;
2437 type1->glyph_names = (FT_String**)loader.glyph_names.elements;
2438 loader.glyph_names.block = NULL;
2439 loader.glyph_names.elements = NULL;
2441 /* we must now build type1.encoding when we have a custom array */
2442 if ( type1->encoding_type == T1_ENCODING_TYPE_ARRAY )
2444 FT_Int charcode, idx, min_char, max_char;
2445 FT_Byte* glyph_name;
2448 /* OK, we do the following: for each element in the encoding */
2449 /* table, look up the index of the glyph having the same name */
2450 /* the index is then stored in type1.encoding.char_index, and */
2451 /* the name to type1.encoding.char_name */
2457 for ( ; charcode < loader.encoding_table.max_elems; charcode++ )
2462 type1->encoding.char_index[charcode] = 0;
2463 type1->encoding.char_name [charcode] = (char *)".notdef";
2465 char_name = loader.encoding_table.elements[charcode];
2467 for ( idx = 0; idx < type1->num_glyphs; idx++ )
2469 glyph_name = (FT_Byte*)type1->glyph_names[idx];
2470 if ( ft_strcmp( (const char*)char_name,
2471 (const char*)glyph_name ) == 0 )
2473 type1->encoding.char_index[charcode] = (FT_UShort)idx;
2474 type1->encoding.char_name [charcode] = (char*)glyph_name;
2476 /* Change min/max encoded char only if glyph name is */
2478 if ( ft_strcmp( (const char*)".notdef",
2479 (const char*)glyph_name ) != 0 )
2481 if ( charcode < min_char )
2482 min_char = charcode;
2483 if ( charcode >= max_char )
2484 max_char = charcode + 1;
2491 type1->encoding.code_first = min_char;
2492 type1->encoding.code_last = max_char;
2493 type1->encoding.num_chars = loader.num_chars;
2496 /* some sanitizing to avoid overflows later on; */
2497 /* the upper limits are ad-hoc values */
2498 if ( priv->blue_shift > 1000 || priv->blue_shift < 0 )
2500 FT_TRACE2(( "T1_Open_Face:"
2501 " setting unlikely BlueShift value %d to default (7)\n",
2502 priv->blue_shift ));
2503 priv->blue_shift = 7;
2506 if ( priv->blue_fuzz > 1000 || priv->blue_fuzz < 0 )
2508 FT_TRACE2(( "T1_Open_Face:"
2509 " setting unlikely BlueFuzz value %d to default (1)\n",
2511 priv->blue_fuzz = 1;
2515 t1_done_loader( &loader );