1 /****************************************************************************
5 * Type 1 font loader (body).
7 * Copyright (C) 1996-2020 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.
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
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'.
64 #include <freetype/internal/ftdebug.h>
65 #include FT_CONFIG_CONFIG_H
66 #include <freetype/ftmm.h>
67 #include <freetype/internal/t1types.h>
68 #include <freetype/internal/ftcalc.h>
69 #include <freetype/internal/fthash.h>
75 #ifdef FT_CONFIG_OPTION_INCREMENTAL
76 #define IS_INCREMENTAL FT_BOOL( face->root.internal->incremental_interface )
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 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;
312 FT_UShort* axis_flags;
314 FT_Offset mmvar_size;
315 FT_Offset axis_flags_size;
319 error = T1_Get_Multi_Master( face, &mmaster );
323 /* the various `*_size' variables, which we also use as */
324 /* offsets into the `mmvar' array, must be multiples of the */
325 /* pointer size (except the last one); without such an */
326 /* alignment there might be runtime errors due to */
327 /* misaligned addresses */
329 #define ALIGN_SIZE( n ) \
330 ( ( (n) + sizeof (void*) - 1 ) & ~( sizeof (void*) - 1 ) )
332 mmvar_size = ALIGN_SIZE( sizeof ( FT_MM_Var ) );
333 axis_flags_size = ALIGN_SIZE( mmaster.num_axis *
334 sizeof ( FT_UShort ) );
335 axis_size = mmaster.num_axis * sizeof ( FT_Var_Axis );
337 if ( FT_ALLOC( mmvar, mmvar_size +
342 mmvar->num_axis = mmaster.num_axis;
343 mmvar->num_designs = mmaster.num_designs;
344 mmvar->num_namedstyles = 0; /* Not supported */
346 /* while axis flags are meaningless here, we have to provide the array */
347 /* to make `FT_Get_Var_Axis_Flags' work: the function expects that the */
348 /* values directly follow the data of `FT_MM_Var' */
349 axis_flags = (FT_UShort*)( (char*)mmvar + mmvar_size );
350 for ( i = 0; i < mmaster.num_axis; i++ )
353 mmvar->axis = (FT_Var_Axis*)( (char*)axis_flags + axis_flags_size );
354 mmvar->namedstyle = NULL;
356 for ( i = 0; i < mmaster.num_axis; i++ )
358 mmvar->axis[i].name = mmaster.axis[i].name;
359 mmvar->axis[i].minimum = INT_TO_FIXED( mmaster.axis[i].minimum );
360 mmvar->axis[i].maximum = INT_TO_FIXED( mmaster.axis[i].maximum );
361 mmvar->axis[i].strid = ~0U; /* Does not apply */
362 mmvar->axis[i].tag = ~0U; /* Does not apply */
364 if ( !mmvar->axis[i].name )
367 if ( ft_strcmp( mmvar->axis[i].name, "Weight" ) == 0 )
368 mmvar->axis[i].tag = FT_MAKE_TAG( 'w', 'g', 'h', 't' );
369 else if ( ft_strcmp( mmvar->axis[i].name, "Width" ) == 0 )
370 mmvar->axis[i].tag = FT_MAKE_TAG( 'w', 'd', 't', 'h' );
371 else if ( ft_strcmp( mmvar->axis[i].name, "OpticalSize" ) == 0 )
372 mmvar->axis[i].tag = FT_MAKE_TAG( 'o', 'p', 's', 'z' );
375 mm_weights_unmap( blend->default_weight_vector,
379 for ( i = 0; i < mmaster.num_axis; i++ )
380 mmvar->axis[i].def = mm_axis_unmap( &blend->design_map[i],
391 t1_set_mm_blend( T1_Face face,
395 PS_Blend blend = face->blend;
398 FT_Bool have_diff = 0;
402 return FT_THROW( Invalid_Argument );
404 if ( num_coords > blend->num_axis )
405 num_coords = blend->num_axis;
407 /* recompute the weight vector from the blend coordinates */
408 for ( n = 0; n < blend->num_designs; n++ )
410 FT_Fixed result = 0x10000L; /* 1.0 fixed */
414 for ( m = 0; m < blend->num_axis; m++ )
416 /* use a default value if we don't have a coordinate */
417 if ( m >= num_coords )
423 /* get current blend axis position */
425 if ( ( n & ( 1 << m ) ) == 0 )
426 factor = 0x10000L - factor;
433 else if ( factor >= 0x10000L )
436 result = FT_MulFix( result, factor );
439 if ( blend->weight_vector[n] != result )
441 blend->weight_vector[n] = result;
446 /* return value -1 indicates `no change' */
447 return have_diff ? FT_Err_Ok : -1;
451 FT_LOCAL_DEF( FT_Error )
452 T1_Set_MM_Blend( T1_Face face,
459 error = t1_set_mm_blend( face, num_coords, coords );
464 face->root.face_flags |= FT_FACE_FLAG_VARIATION;
466 face->root.face_flags &= ~FT_FACE_FLAG_VARIATION;
472 FT_LOCAL_DEF( FT_Error )
473 T1_Get_MM_Blend( T1_Face face,
477 PS_Blend blend = face->blend;
479 FT_Fixed axiscoords[4];
484 return FT_THROW( Invalid_Argument );
486 mm_weights_unmap( blend->weight_vector,
491 if ( num_coords > blend->num_axis )
493 FT_TRACE2(( "T1_Get_MM_Blend: only using first %d of %d coordinates\n",
494 blend->num_axis, num_coords ));
495 nc = blend->num_axis;
498 for ( i = 0; i < nc; i++ )
499 coords[i] = axiscoords[i];
500 for ( ; i < num_coords; i++ )
507 FT_LOCAL_DEF( FT_Error )
508 T1_Set_MM_WeightVector( T1_Face face,
510 FT_Fixed* weightvector )
512 PS_Blend blend = face->blend;
517 return FT_THROW( Invalid_Argument );
519 if ( !len && !weightvector )
521 for ( i = 0; i < blend->num_designs; i++ )
522 blend->weight_vector[i] = blend->default_weight_vector[i];
527 return FT_THROW( Invalid_Argument );
529 n = len < blend->num_designs ? len : blend->num_designs;
531 for ( i = 0; i < n; i++ )
532 blend->weight_vector[i] = weightvector[i];
534 for ( ; i < blend->num_designs; i++ )
535 blend->weight_vector[i] = (FT_Fixed)0;
538 face->root.face_flags |= FT_FACE_FLAG_VARIATION;
540 face->root.face_flags &= ~FT_FACE_FLAG_VARIATION;
547 FT_LOCAL_DEF( FT_Error )
548 T1_Get_MM_WeightVector( T1_Face face,
550 FT_Fixed* weightvector )
552 PS_Blend blend = face->blend;
557 return FT_THROW( Invalid_Argument );
559 if ( *len < blend->num_designs )
561 *len = blend->num_designs;
562 return FT_THROW( Invalid_Argument );
565 for ( i = 0; i < blend->num_designs; i++ )
566 weightvector[i] = blend->weight_vector[i];
567 for ( ; i < *len; i++ )
568 weightvector[i] = (FT_Fixed)0;
570 *len = blend->num_designs;
576 FT_LOCAL_DEF( FT_Error )
577 T1_Set_MM_Design( T1_Face face,
582 PS_Blend blend = face->blend;
584 FT_Fixed final_blends[T1_MAX_MM_DESIGNS];
588 return FT_THROW( Invalid_Argument );
590 if ( num_coords > blend->num_axis )
591 num_coords = blend->num_axis;
593 /* compute the blend coordinates through the blend design map */
595 for ( n = 0; n < blend->num_axis; n++ )
599 PS_DesignMap map = blend->design_map + n;
600 FT_Long* designs = map->design_points;
601 FT_Fixed* blends = map->blend_points;
602 FT_Int before = -1, after = -1;
605 /* use a default value if we don't have a coordinate */
606 if ( n < num_coords )
609 design = ( designs[map->num_points - 1] - designs[0] ) / 2;
611 for ( p = 0; p < (FT_UInt)map->num_points; p++ )
613 FT_Long p_design = designs[p];
617 if ( design == p_design )
619 the_blend = blends[p];
623 if ( design < p_design )
632 /* now interpolate if necessary */
634 the_blend = blends[0];
636 else if ( after < 0 )
637 the_blend = blends[map->num_points - 1];
640 the_blend = FT_MulDiv( design - designs[before],
641 blends [after] - blends [before],
642 designs[after] - designs[before] );
645 final_blends[n] = the_blend;
648 error = t1_set_mm_blend( face, blend->num_axis, final_blends );
653 face->root.face_flags |= FT_FACE_FLAG_VARIATION;
655 face->root.face_flags &= ~FT_FACE_FLAG_VARIATION;
661 /* MM fonts don't have named instances, so only the design is reset */
663 FT_LOCAL_DEF( FT_Error )
664 T1_Reset_MM_Blend( T1_Face face,
665 FT_UInt instance_index )
667 FT_UNUSED( instance_index );
669 return T1_Set_MM_Blend( face, 0, NULL );
673 /**************************************************************************
675 * Just a wrapper around T1_Set_MM_Design to support the different
676 * arguments needed by the GX var distortable fonts.
678 FT_LOCAL_DEF( FT_Error )
679 T1_Set_Var_Design( T1_Face face,
683 FT_Long lcoords[T1_MAX_MM_AXIS];
687 if ( num_coords > T1_MAX_MM_AXIS )
688 num_coords = T1_MAX_MM_AXIS;
690 for ( i = 0; i < num_coords; i++ )
691 lcoords[i] = FIXED_TO_INT( coords[i] );
693 return T1_Set_MM_Design( face, num_coords, lcoords );
697 FT_LOCAL_DEF( FT_Error )
698 T1_Get_Var_Design( T1_Face face,
702 PS_Blend blend = face->blend;
704 FT_Fixed axiscoords[4];
709 return FT_THROW( Invalid_Argument );
711 mm_weights_unmap( blend->weight_vector,
716 if ( num_coords > blend->num_axis )
718 FT_TRACE2(( "T1_Get_Var_Design:"
719 " only using first %d of %d coordinates\n",
720 blend->num_axis, num_coords ));
721 nc = blend->num_axis;
724 for ( i = 0; i < nc; i++ )
725 coords[i] = mm_axis_unmap( &blend->design_map[i], axiscoords[i] );
726 for ( ; i < num_coords; i++ )
734 T1_Done_Blend( T1_Face face )
736 FT_Memory memory = face->root.memory;
737 PS_Blend blend = face->blend;
742 FT_UInt num_designs = blend->num_designs;
743 FT_UInt num_axis = blend->num_axis;
747 /* release design pos table */
748 FT_FREE( blend->design_pos[0] );
749 for ( n = 1; n < num_designs; n++ )
750 blend->design_pos[n] = NULL;
752 /* release blend `private' and `font info' dictionaries */
753 FT_FREE( blend->privates[1] );
754 FT_FREE( blend->font_infos[1] );
755 FT_FREE( blend->bboxes[1] );
757 for ( n = 0; n < num_designs; n++ )
759 blend->privates [n] = NULL;
760 blend->font_infos[n] = NULL;
761 blend->bboxes [n] = NULL;
764 /* release weight vectors */
765 FT_FREE( blend->weight_vector );
766 blend->default_weight_vector = NULL;
768 /* release axis names */
769 for ( n = 0; n < num_axis; n++ )
770 FT_FREE( blend->axis_names[n] );
772 /* release design map */
773 for ( n = 0; n < num_axis; n++ )
775 PS_DesignMap dmap = blend->design_map + n;
778 FT_FREE( dmap->design_points );
779 dmap->num_points = 0;
782 FT_FREE( face->blend );
788 parse_blend_axis_types( T1_Face face,
791 T1_TokenRec axis_tokens[T1_MAX_MM_AXIS];
793 FT_Error error = FT_Err_Ok;
798 /* take an array of objects */
799 T1_ToTokenArray( &loader->parser, axis_tokens,
800 T1_MAX_MM_AXIS, &num_axis );
803 error = FT_ERR( Ignore );
806 if ( num_axis == 0 || num_axis > T1_MAX_MM_AXIS )
808 FT_ERROR(( "parse_blend_axis_types: incorrect number of axes: %d\n",
810 error = FT_THROW( Invalid_File_Format );
814 /* allocate blend if necessary */
815 error = t1_allocate_blend( face, 0, (FT_UInt)num_axis );
822 memory = face->root.memory;
824 /* each token is an immediate containing the name of the axis */
825 for ( n = 0; n < num_axis; n++ )
827 T1_Token token = axis_tokens + n;
832 /* skip first slash, if any */
833 if ( token->start[0] == '/' )
836 len = (FT_UInt)( token->limit - token->start );
839 error = FT_THROW( Invalid_File_Format );
843 FT_TRACE4(( " /%.*s", len, token->start ));
845 name = (FT_Byte*)blend->axis_names[n];
848 FT_TRACE0(( "parse_blend_axis_types:"
849 " overwriting axis name `%s' with `%.*s'\n",
850 name, len, token->start ));
854 if ( FT_ALLOC( blend->axis_names[n], len + 1 ) )
857 name = (FT_Byte*)blend->axis_names[n];
858 FT_MEM_COPY( name, token->start, len );
862 FT_TRACE4(( "]\n" ));
865 loader->parser.root.error = error;
870 parse_blend_design_positions( T1_Face face,
873 T1_TokenRec design_tokens[T1_MAX_MM_DESIGNS];
876 T1_Parser parser = &loader->parser;
878 FT_Error error = FT_Err_Ok;
882 /* get the array of design tokens -- compute number of designs */
883 T1_ToTokenArray( parser, design_tokens,
884 T1_MAX_MM_DESIGNS, &num_designs );
885 if ( num_designs < 0 )
887 error = FT_ERR( Ignore );
890 if ( num_designs == 0 || num_designs > T1_MAX_MM_DESIGNS )
892 FT_ERROR(( "parse_blend_design_positions:"
893 " incorrect number of designs: %d\n",
895 error = FT_THROW( Invalid_File_Format );
900 FT_Byte* old_cursor = parser->root.cursor;
901 FT_Byte* old_limit = parser->root.limit;
906 num_axis = 0; /* make compiler happy */
910 for ( n = 0; n < num_designs; n++ )
912 T1_TokenRec axis_tokens[T1_MAX_MM_AXIS];
917 /* read axis/coordinates tokens */
918 token = design_tokens + n;
919 parser->root.cursor = token->start;
920 parser->root.limit = token->limit;
921 T1_ToTokenArray( parser, axis_tokens, T1_MAX_MM_AXIS, &n_axis );
925 if ( n_axis <= 0 || n_axis > T1_MAX_MM_AXIS )
927 FT_ERROR(( "parse_blend_design_positions:"
928 " invalid number of axes: %d\n",
930 error = FT_THROW( Invalid_File_Format );
935 error = t1_allocate_blend( face,
936 (FT_UInt)num_designs,
942 else if ( n_axis != num_axis )
944 FT_ERROR(( "parse_blend_design_positions: incorrect table\n" ));
945 error = FT_THROW( Invalid_File_Format );
949 /* now read each axis token into the design position */
950 FT_TRACE4(( " [" )) ;
951 for ( axis = 0; axis < n_axis; axis++ )
953 T1_Token token2 = axis_tokens + axis;
956 parser->root.cursor = token2->start;
957 parser->root.limit = token2->limit;
958 blend->design_pos[n][axis] = T1_ToFixed( parser, 0 );
959 FT_TRACE4(( " %f", (double)blend->design_pos[n][axis] / 65536 ));
964 FT_TRACE4(( "]\n" ));
966 loader->parser.root.cursor = old_cursor;
967 loader->parser.root.limit = old_limit;
971 loader->parser.root.error = error;
976 parse_blend_design_map( T1_Face face,
979 FT_Error error = FT_Err_Ok;
980 T1_Parser parser = &loader->parser;
982 T1_TokenRec axis_tokens[T1_MAX_MM_AXIS];
986 FT_Memory memory = face->root.memory;
989 T1_ToTokenArray( parser, axis_tokens,
990 T1_MAX_MM_AXIS, &num_axis );
993 error = FT_ERR( Ignore );
996 if ( num_axis == 0 || num_axis > T1_MAX_MM_AXIS )
998 FT_ERROR(( "parse_blend_design_map: incorrect number of axes: %d\n",
1000 error = FT_THROW( Invalid_File_Format );
1004 old_cursor = parser->root.cursor;
1005 old_limit = parser->root.limit;
1007 error = t1_allocate_blend( face, 0, (FT_UInt)num_axis );
1010 blend = face->blend;
1012 FT_TRACE4(( " [" ));
1014 /* now read each axis design map */
1015 for ( n = 0; n < num_axis; n++ )
1017 PS_DesignMap map = blend->design_map + n;
1018 T1_Token axis_token;
1019 T1_TokenRec point_tokens[T1_MAX_MM_MAP_POINTS];
1020 FT_Int p, num_points;
1023 axis_token = axis_tokens + n;
1025 parser->root.cursor = axis_token->start;
1026 parser->root.limit = axis_token->limit;
1027 T1_ToTokenArray( parser, point_tokens,
1028 T1_MAX_MM_MAP_POINTS, &num_points );
1030 FT_TRACE4(( " [" ));
1032 if ( num_points <= 0 || num_points > T1_MAX_MM_MAP_POINTS )
1034 FT_ERROR(( "parse_blend_design_map: incorrect table\n" ));
1035 error = FT_THROW( Invalid_File_Format );
1039 if ( map->design_points )
1041 FT_ERROR(( "parse_blend_design_map: duplicate table\n" ));
1042 error = FT_THROW( Invalid_File_Format );
1046 /* allocate design map data */
1047 if ( FT_NEW_ARRAY( map->design_points, num_points * 2 ) )
1049 map->blend_points = map->design_points + num_points;
1050 map->num_points = (FT_Byte)num_points;
1052 for ( p = 0; p < num_points; p++ )
1054 T1_Token point_token;
1057 point_token = point_tokens + p;
1059 /* don't include delimiting brackets */
1060 parser->root.cursor = point_token->start + 1;
1061 parser->root.limit = point_token->limit - 1;
1063 map->design_points[p] = T1_ToInt( parser );
1064 map->blend_points [p] = T1_ToFixed( parser, 0 );
1066 FT_TRACE4(( " [%ld %f]",
1067 map->design_points[p],
1068 (double)map->blend_points[p] / 65536 ));
1074 FT_TRACE4(( "]\n" ));
1076 parser->root.cursor = old_cursor;
1077 parser->root.limit = old_limit;
1080 parser->root.error = error;
1085 parse_weight_vector( T1_Face face,
1088 T1_TokenRec design_tokens[T1_MAX_MM_DESIGNS];
1090 FT_Error error = FT_Err_Ok;
1091 T1_Parser parser = &loader->parser;
1092 PS_Blend blend = face->blend;
1095 FT_Byte* old_cursor;
1099 T1_ToTokenArray( parser, design_tokens,
1100 T1_MAX_MM_DESIGNS, &num_designs );
1101 if ( num_designs < 0 )
1103 error = FT_ERR( Ignore );
1106 if ( num_designs == 0 || num_designs > T1_MAX_MM_DESIGNS )
1108 FT_ERROR(( "parse_weight_vector:"
1109 " incorrect number of designs: %d\n",
1111 error = FT_THROW( Invalid_File_Format );
1115 if ( !blend || !blend->num_designs )
1117 error = t1_allocate_blend( face, (FT_UInt)num_designs, 0 );
1120 blend = face->blend;
1122 else if ( blend->num_designs != (FT_UInt)num_designs )
1124 FT_ERROR(( "parse_weight_vector:"
1125 " /BlendDesignPosition and /WeightVector have\n"
1127 " different number of elements\n" ));
1128 error = FT_THROW( Invalid_File_Format );
1132 old_cursor = parser->root.cursor;
1133 old_limit = parser->root.limit;
1137 for ( n = 0; n < num_designs; n++ )
1139 token = design_tokens + n;
1140 parser->root.cursor = token->start;
1141 parser->root.limit = token->limit;
1143 blend->default_weight_vector[n] =
1144 blend->weight_vector[n] = T1_ToFixed( parser, 0 );
1146 FT_TRACE4(( " %f", (double)blend->weight_vector[n] / 65536 ));
1149 FT_TRACE4(( "]\n" ));
1151 parser->root.cursor = old_cursor;
1152 parser->root.limit = old_limit;
1155 parser->root.error = error;
1159 /* e.g., /BuildCharArray [0 0 0 0 0 0 0 0] def */
1160 /* we're only interested in the number of array elements */
1162 parse_buildchar( T1_Face face,
1165 face->len_buildchar = (FT_UInt)T1_ToFixedArray( &loader->parser,
1168 #ifdef FT_DEBUG_LEVEL_TRACE
1173 FT_TRACE4(( " [" ));
1174 for ( i = 0; i < face->len_buildchar; i++ )
1175 FT_TRACE4(( " 0" ));
1177 FT_TRACE4(( "]\n" ));
1185 #endif /* !T1_CONFIG_OPTION_NO_MM_SUPPORT */
1190 /*************************************************************************/
1191 /*************************************************************************/
1193 /***** TYPE 1 SYMBOL PARSING *****/
1195 /*************************************************************************/
1196 /*************************************************************************/
1199 t1_load_keyword( T1_Face face,
1201 const T1_Field field )
1206 FT_UInt max_objects;
1207 PS_Blend blend = face->blend;
1210 if ( blend && blend->num_designs == 0 )
1213 /* if the keyword has a dedicated callback, call it */
1214 if ( field->type == T1_FIELD_TYPE_CALLBACK )
1216 FT_TRACE4(( " %s", field->ident ));
1218 field->reader( (FT_Face)face, loader );
1219 error = loader->parser.root.error;
1223 /* now, the keyword is either a simple field, or a table of fields; */
1224 /* we are now going to take care of it */
1225 switch ( field->location )
1227 case T1_FIELD_LOCATION_FONT_INFO:
1228 dummy_object = &face->type1.font_info;
1229 objects = &dummy_object;
1234 objects = (void**)blend->font_infos;
1235 max_objects = blend->num_designs;
1239 case T1_FIELD_LOCATION_FONT_EXTRA:
1240 dummy_object = &face->type1.font_extra;
1241 objects = &dummy_object;
1245 case T1_FIELD_LOCATION_PRIVATE:
1246 dummy_object = &face->type1.private_dict;
1247 objects = &dummy_object;
1252 objects = (void**)blend->privates;
1253 max_objects = blend->num_designs;
1257 case T1_FIELD_LOCATION_BBOX:
1258 dummy_object = &face->type1.font_bbox;
1259 objects = &dummy_object;
1264 objects = (void**)blend->bboxes;
1265 max_objects = blend->num_designs;
1269 case T1_FIELD_LOCATION_LOADER:
1270 dummy_object = loader;
1271 objects = &dummy_object;
1275 case T1_FIELD_LOCATION_FACE:
1276 dummy_object = face;
1277 objects = &dummy_object;
1281 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
1282 case T1_FIELD_LOCATION_BLEND:
1283 dummy_object = face->blend;
1284 objects = &dummy_object;
1290 dummy_object = &face->type1;
1291 objects = &dummy_object;
1295 FT_TRACE4(( " %s", field->ident ));
1299 if ( field->type == T1_FIELD_TYPE_INTEGER_ARRAY ||
1300 field->type == T1_FIELD_TYPE_FIXED_ARRAY )
1301 error = T1_Load_Field_Table( &loader->parser, field,
1302 objects, max_objects, 0 );
1304 error = T1_Load_Field( &loader->parser, field,
1305 objects, max_objects, 0 );
1309 FT_TRACE1(( "t1_load_keyword: ignoring keyword `%s'"
1310 " which is not valid at this point\n"
1311 " (probably due to missing keywords)\n",
1316 FT_TRACE4(( "\n" ));
1324 parse_private( T1_Face face,
1329 loader->keywords_encountered |= T1_PRIVATE;
1331 FT_TRACE4(( "\n" ));
1335 /* return 1 in case of success */
1338 read_binary_data( T1_Parser parser,
1341 FT_Bool incremental )
1344 FT_Byte* limit = parser->root.limit;
1347 /* the binary data has one of the following formats */
1349 /* `size' [white*] RD white ....... ND */
1350 /* `size' [white*] -| white ....... |- */
1353 T1_Skip_Spaces( parser );
1355 cur = parser->root.cursor;
1357 if ( cur < limit && ft_isdigit( *cur ) )
1359 FT_Long s = T1_ToInt( parser );
1362 T1_Skip_PS_Token( parser ); /* `RD' or `-|' or something else */
1364 /* there is only one whitespace char after the */
1365 /* `RD' or `-|' token */
1366 *base = parser->root.cursor + 1;
1368 if ( s >= 0 && s < limit - *base )
1370 parser->root.cursor += s + 1;
1371 *size = (FT_ULong)s;
1372 return !parser->root.error;
1378 FT_ERROR(( "read_binary_data: invalid size field\n" ));
1379 parser->root.error = FT_THROW( Invalid_File_Format );
1386 /* We now define the routines to handle the `/Encoding', `/Subrs', */
1387 /* and `/CharStrings' dictionaries. */
1390 t1_parse_font_matrix( T1_Face face,
1393 T1_Parser parser = &loader->parser;
1394 FT_Matrix* matrix = &face->type1.font_matrix;
1395 FT_Vector* offset = &face->type1.font_offset;
1396 FT_Face root = (FT_Face)&face->root;
1398 FT_Fixed temp_scale;
1402 /* input is scaled by 1000 to accommodate default FontMatrix */
1403 result = T1_ToFixedArray( parser, 6, temp, 3 );
1407 parser->root.error = FT_THROW( Invalid_File_Format );
1411 FT_TRACE4(( " [%f %f %f %f %f %f]\n",
1412 (double)temp[0] / 65536 / 1000,
1413 (double)temp[1] / 65536 / 1000,
1414 (double)temp[2] / 65536 / 1000,
1415 (double)temp[3] / 65536 / 1000,
1416 (double)temp[4] / 65536 / 1000,
1417 (double)temp[5] / 65536 / 1000 ));
1419 temp_scale = FT_ABS( temp[3] );
1421 if ( temp_scale == 0 )
1423 FT_ERROR(( "t1_parse_font_matrix: invalid font matrix\n" ));
1424 parser->root.error = FT_THROW( Invalid_File_Format );
1429 if ( temp_scale != 0x10000L )
1431 /* set units per EM based on FontMatrix values */
1432 root->units_per_EM = (FT_UShort)FT_DivFix( 1000, temp_scale );
1434 temp[0] = FT_DivFix( temp[0], temp_scale );
1435 temp[1] = FT_DivFix( temp[1], temp_scale );
1436 temp[2] = FT_DivFix( temp[2], temp_scale );
1437 temp[4] = FT_DivFix( temp[4], temp_scale );
1438 temp[5] = FT_DivFix( temp[5], temp_scale );
1439 temp[3] = temp[3] < 0 ? -0x10000L : 0x10000L;
1441 matrix->xx = temp[0];
1442 matrix->yx = temp[1];
1443 matrix->xy = temp[2];
1444 matrix->yy = temp[3];
1446 if ( !FT_Matrix_Check( matrix ) )
1448 FT_ERROR(( "t1_parse_font_matrix: invalid font matrix\n" ));
1449 parser->root.error = FT_THROW( Invalid_File_Format );
1453 /* note that the offsets must be expressed in integer font units */
1454 offset->x = temp[4] >> 16;
1455 offset->y = temp[5] >> 16;
1460 parse_encoding( T1_Face face,
1463 T1_Parser parser = &loader->parser;
1465 FT_Byte* limit = parser->root.limit;
1467 PSAux_Service psaux = (PSAux_Service)face->psaux;
1470 T1_Skip_Spaces( parser );
1471 cur = parser->root.cursor;
1474 FT_ERROR(( "parse_encoding: out of bounds\n" ));
1475 parser->root.error = FT_THROW( Invalid_File_Format );
1479 /* if we have a number or `[', the encoding is an array, */
1480 /* and we must load it now */
1481 if ( ft_isdigit( *cur ) || *cur == '[' )
1483 T1_Encoding encode = &face->type1.encoding;
1484 FT_Int count, array_size, n;
1485 PS_Table char_table = &loader->encoding_table;
1486 FT_Memory memory = parser->root.memory;
1488 FT_Bool only_immediates = 0;
1491 /* read the number of entries in the encoding; should be 256 */
1495 only_immediates = 1;
1496 parser->root.cursor++;
1499 count = (FT_Int)T1_ToInt( parser );
1504 FT_TRACE2(( "parse_encoding:"
1505 " only using first 256 encoding array entries\n" ));
1509 T1_Skip_Spaces( parser );
1510 if ( parser->root.cursor >= limit )
1513 /* PostScript happily allows overwriting of encoding arrays */
1514 if ( encode->char_index )
1516 FT_FREE( encode->char_index );
1517 FT_FREE( encode->char_name );
1518 T1_Release_Table( char_table );
1521 /* we use a T1_Table to store our charnames */
1522 loader->num_chars = encode->num_chars = array_size;
1523 if ( FT_NEW_ARRAY( encode->char_index, array_size ) ||
1524 FT_NEW_ARRAY( encode->char_name, array_size ) ||
1525 FT_SET_ERROR( psaux->ps_table_funcs->init(
1526 char_table, array_size, memory ) ) )
1528 parser->root.error = error;
1532 /* We need to `zero' out encoding_table.elements */
1533 for ( n = 0; n < array_size; n++ )
1534 (void)T1_Add_Table( char_table, n, ".notdef", 8 );
1536 /* Now we need to read records of the form */
1538 /* ... charcode /charname ... */
1540 /* for each entry in our table. */
1542 /* We simply look for a number followed by an immediate */
1543 /* name. Note that this ignores correctly the sequence */
1544 /* that is often seen in type1 fonts: */
1546 /* 0 1 255 { 1 index exch /.notdef put } for dup */
1548 /* used to clean the encoding array before anything else. */
1550 /* Alternatively, if the array is directly given as */
1552 /* /Encoding [ ... ] */
1554 /* we only read immediates. */
1557 T1_Skip_Spaces( parser );
1559 while ( parser->root.cursor < limit )
1561 cur = parser->root.cursor;
1563 /* we stop when we encounter a `def' or `]' */
1564 if ( *cur == 'd' && cur + 3 < limit )
1566 if ( cur[1] == 'e' &&
1568 IS_PS_DELIM( cur[3] ) )
1570 FT_TRACE6(( "encoding end\n" ));
1577 FT_TRACE6(( "encoding end\n" ));
1582 /* check whether we've found an entry */
1583 if ( ft_isdigit( *cur ) || only_immediates )
1588 if ( only_immediates )
1592 charcode = (FT_Int)T1_ToInt( parser );
1593 T1_Skip_Spaces( parser );
1595 /* protect against invalid charcode */
1596 if ( cur == parser->root.cursor )
1598 parser->root.error = FT_THROW( Unknown_File_Format );
1603 cur = parser->root.cursor;
1605 if ( cur + 2 < limit && *cur == '/' && n < count )
1612 parser->root.cursor = cur;
1613 T1_Skip_PS_Token( parser );
1614 if ( parser->root.cursor >= limit )
1616 if ( parser->root.error )
1619 len = (FT_UInt)( parser->root.cursor - cur );
1621 if ( n < array_size )
1623 parser->root.error = T1_Add_Table( char_table, charcode,
1625 if ( parser->root.error )
1627 char_table->elements[charcode][len] = '\0';
1632 else if ( only_immediates )
1634 /* Since the current position is not updated for */
1635 /* immediates-only mode we would get an infinite loop if */
1636 /* we don't do anything here. */
1638 /* This encoding array is not valid according to the type1 */
1639 /* specification (it might be an encoding for a CID type1 */
1640 /* font, however), so we conclude that this font is NOT a */
1642 parser->root.error = FT_THROW( Unknown_File_Format );
1648 T1_Skip_PS_Token( parser );
1649 if ( parser->root.error )
1653 T1_Skip_Spaces( parser );
1656 #ifdef FT_DEBUG_LEVEL_TRACE
1657 FT_TRACE4(( " [" ));
1659 /* XXX show encoding vector */
1660 FT_TRACE4(( "..." ));
1662 FT_TRACE4(( "]\n" ));
1665 face->type1.encoding_type = T1_ENCODING_TYPE_ARRAY;
1666 parser->root.cursor = cur;
1669 /* Otherwise, we should have either `StandardEncoding', */
1670 /* `ExpertEncoding', or `ISOLatin1Encoding' */
1673 if ( cur + 17 < limit &&
1674 ft_strncmp( (const char*)cur, "StandardEncoding", 16 ) == 0 )
1676 face->type1.encoding_type = T1_ENCODING_TYPE_STANDARD;
1677 FT_TRACE4(( " StandardEncoding\n" ));
1680 else if ( cur + 15 < limit &&
1681 ft_strncmp( (const char*)cur, "ExpertEncoding", 14 ) == 0 )
1683 face->type1.encoding_type = T1_ENCODING_TYPE_EXPERT;
1684 FT_TRACE4(( " ExpertEncoding\n" ));
1687 else if ( cur + 18 < limit &&
1688 ft_strncmp( (const char*)cur, "ISOLatin1Encoding", 17 ) == 0 )
1690 face->type1.encoding_type = T1_ENCODING_TYPE_ISOLATIN1;
1691 FT_TRACE4(( " ISOLatin1Encoding\n" ));
1696 parser->root.error = FT_ERR( Ignore );
1697 FT_TRACE4(( "<unknown>\n" ));
1704 parse_subrs( T1_Face face,
1707 T1_Parser parser = &loader->parser;
1708 PS_Table table = &loader->subrs;
1709 FT_Memory memory = parser->root.memory;
1714 PSAux_Service psaux = (PSAux_Service)face->psaux;
1717 T1_Skip_Spaces( parser );
1719 /* test for empty array */
1720 if ( parser->root.cursor < parser->root.limit &&
1721 *parser->root.cursor == '[' )
1723 T1_Skip_PS_Token( parser );
1724 T1_Skip_Spaces ( parser );
1725 if ( parser->root.cursor >= parser->root.limit ||
1726 *parser->root.cursor != ']' )
1727 parser->root.error = FT_THROW( Invalid_File_Format );
1731 num_subrs = (FT_Int)T1_ToInt( parser );
1732 if ( num_subrs < 0 )
1734 parser->root.error = FT_THROW( Invalid_File_Format );
1738 /* we certainly need more than 8 bytes per subroutine */
1739 if ( parser->root.limit >= parser->root.cursor &&
1740 num_subrs > ( parser->root.limit - parser->root.cursor ) >> 3 )
1743 * There are two possibilities. Either the font contains an invalid
1744 * value for `num_subrs', or we have a subsetted font where the
1745 * subroutine indices are not adjusted, e.g.
1750 * dup 681 { ... } NP
1753 * In both cases, we use a number hash that maps from subr indices to
1754 * actual array elements.
1757 FT_TRACE0(( "parse_subrs: adjusting number of subroutines"
1758 " (from %d to %ld)\n",
1760 ( parser->root.limit - parser->root.cursor ) >> 3 ));
1761 num_subrs = ( parser->root.limit - parser->root.cursor ) >> 3;
1763 if ( !loader->subrs_hash )
1765 if ( FT_NEW( loader->subrs_hash ) )
1768 error = ft_hash_num_init( loader->subrs_hash, memory );
1774 /* position the parser right before the `dup' of the first subr */
1775 T1_Skip_PS_Token( parser ); /* `array' */
1776 if ( parser->root.error )
1778 T1_Skip_Spaces( parser );
1780 /* initialize subrs array -- with synthetic fonts it is possible */
1781 /* we get here twice */
1782 if ( !loader->num_subrs )
1784 error = psaux->ps_table_funcs->init( table, num_subrs, memory );
1789 /* the format is simple: */
1791 /* `index' + binary data */
1793 for ( count = 0; ; count++ )
1800 /* If we are out of data, or if the next token isn't `dup', */
1802 if ( parser->root.cursor + 4 >= parser->root.limit ||
1803 ft_strncmp( (char*)parser->root.cursor, "dup", 3 ) != 0 )
1806 T1_Skip_PS_Token( parser ); /* `dup' */
1808 idx = T1_ToInt( parser );
1810 if ( !read_binary_data( parser, &size, &base, IS_INCREMENTAL ) )
1813 /* The binary string is followed by one token, e.g. `NP' */
1814 /* (bound to `noaccess put') or by two separate tokens: */
1815 /* `noaccess' & `put'. We position the parser right */
1816 /* before the next `dup', if any. */
1817 T1_Skip_PS_Token( parser ); /* `NP' or `|' or `noaccess' */
1818 if ( parser->root.error )
1820 T1_Skip_Spaces ( parser );
1822 if ( parser->root.cursor + 4 < parser->root.limit &&
1823 ft_strncmp( (char*)parser->root.cursor, "put", 3 ) == 0 )
1825 T1_Skip_PS_Token( parser ); /* skip `put' */
1826 T1_Skip_Spaces ( parser );
1829 /* if we use a hash, the subrs index is the key, and a running */
1830 /* counter specified for `T1_Add_Table' acts as the value */
1831 if ( loader->subrs_hash )
1833 ft_hash_num_insert( idx, count, loader->subrs_hash, memory );
1837 /* with synthetic fonts it is possible we get here twice */
1838 if ( loader->num_subrs )
1841 /* some fonts use a value of -1 for lenIV to indicate that */
1842 /* the charstrings are unencoded */
1844 /* thanks to Tom Kacvinsky for pointing this out */
1846 if ( face->type1.private_dict.lenIV >= 0 )
1848 FT_Byte* temp = NULL;
1851 /* some fonts define empty subr records -- this is not totally */
1852 /* compliant to the specification (which says they should at */
1853 /* least contain a `return'), but we support them anyway */
1854 if ( size < (FT_ULong)face->type1.private_dict.lenIV )
1856 error = FT_THROW( Invalid_File_Format );
1860 /* t1_decrypt() shouldn't write to base -- make temporary copy */
1861 if ( FT_ALLOC( temp, size ) )
1863 FT_MEM_COPY( temp, base, size );
1864 psaux->t1_decrypt( temp, size, 4330 );
1865 size -= (FT_ULong)face->type1.private_dict.lenIV;
1866 error = T1_Add_Table( table, (FT_Int)idx,
1867 temp + face->type1.private_dict.lenIV, size );
1871 error = T1_Add_Table( table, (FT_Int)idx, base, size );
1876 if ( !loader->num_subrs )
1877 loader->num_subrs = num_subrs;
1879 #ifdef FT_DEBUG_LEVEL_TRACE
1880 FT_TRACE4(( " <" ));
1882 /* XXX show subrs? */
1883 FT_TRACE4(( "%d elements", num_subrs ));
1885 FT_TRACE4(( ">\n" ));
1891 parser->root.error = error;
1895 #define TABLE_EXTEND 5
1899 parse_charstrings( T1_Face face,
1902 T1_Parser parser = &loader->parser;
1903 PS_Table code_table = &loader->charstrings;
1904 PS_Table name_table = &loader->glyph_names;
1905 PS_Table swap_table = &loader->swap_table;
1906 FT_Memory memory = parser->root.memory;
1909 PSAux_Service psaux = (PSAux_Service)face->psaux;
1911 FT_Byte* cur = parser->root.cursor;
1912 FT_Byte* limit = parser->root.limit;
1913 FT_Int n, num_glyphs;
1914 FT_Int notdef_index = 0;
1915 FT_Byte notdef_found = 0;
1918 num_glyphs = (FT_Int)T1_ToInt( parser );
1919 if ( num_glyphs < 0 )
1921 error = FT_THROW( Invalid_File_Format );
1925 /* we certainly need more than 8 bytes per glyph */
1926 if ( num_glyphs > ( limit - cur ) >> 3 )
1928 FT_TRACE0(( "parse_charstrings: adjusting number of glyphs"
1929 " (from %d to %ld)\n",
1930 num_glyphs, ( limit - cur ) >> 3 ));
1931 num_glyphs = ( limit - cur ) >> 3;
1934 /* some fonts like Optima-Oblique not only define the /CharStrings */
1935 /* array but access it also */
1936 if ( num_glyphs == 0 || parser->root.error )
1939 /* initialize tables, leaving space for addition of .notdef, */
1940 /* if necessary, and a few other glyphs to handle buggy */
1941 /* fonts which have more glyphs than specified. */
1943 /* for some non-standard fonts like `Optima' which provides */
1944 /* different outlines depending on the resolution it is */
1945 /* possible to get here twice */
1946 if ( !loader->num_glyphs )
1948 error = psaux->ps_table_funcs->init(
1949 code_table, num_glyphs + 1 + TABLE_EXTEND, memory );
1953 error = psaux->ps_table_funcs->init(
1954 name_table, num_glyphs + 1 + TABLE_EXTEND, memory );
1958 /* Initialize table for swapping index notdef_index and */
1959 /* index 0 names and codes (if necessary). */
1961 error = psaux->ps_table_funcs->init( swap_table, 4, memory );
1974 /* the format is simple: */
1975 /* `/glyphname' + binary data */
1977 T1_Skip_Spaces( parser );
1979 cur = parser->root.cursor;
1983 /* we stop when we find a `def' or `end' keyword */
1984 if ( cur + 3 < limit && IS_PS_DELIM( cur[3] ) )
1986 if ( cur[0] == 'd' &&
1990 /* There are fonts which have this: */
1992 /* /CharStrings 118 dict def */
1994 /* CharStrings begin */
1997 /* To catch this we ignore `def' if */
1998 /* no charstring has actually been */
2004 if ( cur[0] == 'e' &&
2010 T1_Skip_PS_Token( parser );
2011 if ( parser->root.cursor >= limit )
2013 error = FT_THROW( Invalid_File_Format );
2016 if ( parser->root.error )
2024 if ( cur + 2 >= limit )
2026 error = FT_THROW( Invalid_File_Format );
2030 cur++; /* skip `/' */
2031 len = (FT_UInt)( parser->root.cursor - cur );
2033 if ( !read_binary_data( parser, &size, &base, IS_INCREMENTAL ) )
2036 /* for some non-standard fonts like `Optima' which provides */
2037 /* different outlines depending on the resolution it is */
2038 /* possible to get here twice */
2039 if ( loader->num_glyphs )
2042 error = T1_Add_Table( name_table, n, cur, len + 1 );
2046 /* add a trailing zero to the name table */
2047 name_table->elements[n][len] = '\0';
2049 /* record index of /.notdef */
2051 ft_strcmp( ".notdef",
2052 (const char*)(name_table->elements[n]) ) == 0 )
2058 if ( face->type1.private_dict.lenIV >= 0 &&
2059 n < num_glyphs + TABLE_EXTEND )
2061 FT_Byte* temp = NULL;
2064 if ( size <= (FT_ULong)face->type1.private_dict.lenIV )
2066 error = FT_THROW( Invalid_File_Format );
2070 /* t1_decrypt() shouldn't write to base -- make temporary copy */
2071 if ( FT_ALLOC( temp, size ) )
2073 FT_MEM_COPY( temp, base, size );
2074 psaux->t1_decrypt( temp, size, 4330 );
2075 size -= (FT_ULong)face->type1.private_dict.lenIV;
2076 error = T1_Add_Table( code_table, n,
2077 temp + face->type1.private_dict.lenIV, size );
2081 error = T1_Add_Table( code_table, n, base, size );
2091 error = FT_THROW( Invalid_File_Format );
2095 loader->num_glyphs = n;
2097 /* if /.notdef is found but does not occupy index 0, do our magic. */
2098 if ( notdef_found &&
2099 ft_strcmp( ".notdef", (const char*)name_table->elements[0] ) )
2101 /* Swap glyph in index 0 with /.notdef glyph. First, add index 0 */
2102 /* name and code entries to swap_table. Then place notdef_index */
2103 /* name and code entries into swap_table. Then swap name and code */
2104 /* entries at indices notdef_index and 0 using values stored in */
2108 error = T1_Add_Table( swap_table, 0,
2109 name_table->elements[0],
2110 name_table->lengths [0] );
2115 error = T1_Add_Table( swap_table, 1,
2116 code_table->elements[0],
2117 code_table->lengths [0] );
2121 /* Index notdef_index name */
2122 error = T1_Add_Table( swap_table, 2,
2123 name_table->elements[notdef_index],
2124 name_table->lengths [notdef_index] );
2128 /* Index notdef_index code */
2129 error = T1_Add_Table( swap_table, 3,
2130 code_table->elements[notdef_index],
2131 code_table->lengths [notdef_index] );
2135 error = T1_Add_Table( name_table, notdef_index,
2136 swap_table->elements[0],
2137 swap_table->lengths [0] );
2141 error = T1_Add_Table( code_table, notdef_index,
2142 swap_table->elements[1],
2143 swap_table->lengths [1] );
2147 error = T1_Add_Table( name_table, 0,
2148 swap_table->elements[2],
2149 swap_table->lengths [2] );
2153 error = T1_Add_Table( code_table, 0,
2154 swap_table->elements[3],
2155 swap_table->lengths [3] );
2160 else if ( !notdef_found )
2162 /* notdef_index is already 0, or /.notdef is undefined in */
2163 /* charstrings dictionary. Worry about /.notdef undefined. */
2164 /* We take index 0 and add it to the end of the table(s) */
2165 /* and add our own /.notdef glyph to index 0. */
2167 /* 0 333 hsbw endchar */
2168 FT_Byte notdef_glyph[] = { 0x8B, 0xF7, 0xE1, 0x0D, 0x0E };
2171 error = T1_Add_Table( swap_table, 0,
2172 name_table->elements[0],
2173 name_table->lengths [0] );
2177 error = T1_Add_Table( swap_table, 1,
2178 code_table->elements[0],
2179 code_table->lengths [0] );
2183 error = T1_Add_Table( name_table, 0, ".notdef", 8 );
2187 error = T1_Add_Table( code_table, 0, notdef_glyph, 5 );
2192 error = T1_Add_Table( name_table, n,
2193 swap_table->elements[0],
2194 swap_table->lengths [0] );
2198 error = T1_Add_Table( code_table, n,
2199 swap_table->elements[1],
2200 swap_table->lengths [1] );
2204 /* we added a glyph. */
2205 loader->num_glyphs += 1;
2208 #ifdef FT_DEBUG_LEVEL_TRACE
2209 FT_TRACE4(( " <" ));
2211 /* XXX show charstrings? */
2212 FT_TRACE4(( "%d elements", loader->num_glyphs ));
2214 FT_TRACE4(( ">\n" ));
2220 parser->root.error = error;
2224 /**************************************************************************
2226 * Define the token field static variables. This is a set of
2227 * T1_FieldRec variables.
2233 const T1_FieldRec t1_keywords[] =
2236 #include "t1tokens.h"
2238 /* now add the special functions... */
2239 T1_FIELD_CALLBACK( "FontMatrix", t1_parse_font_matrix,
2240 T1_FIELD_DICT_FONTDICT )
2241 T1_FIELD_CALLBACK( "Encoding", parse_encoding,
2242 T1_FIELD_DICT_FONTDICT )
2243 T1_FIELD_CALLBACK( "Subrs", parse_subrs,
2244 T1_FIELD_DICT_PRIVATE )
2245 T1_FIELD_CALLBACK( "CharStrings", parse_charstrings,
2246 T1_FIELD_DICT_PRIVATE )
2247 T1_FIELD_CALLBACK( "Private", parse_private,
2248 T1_FIELD_DICT_FONTDICT )
2250 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
2251 T1_FIELD_CALLBACK( "BlendDesignPositions", parse_blend_design_positions,
2252 T1_FIELD_DICT_FONTDICT )
2253 T1_FIELD_CALLBACK( "BlendDesignMap", parse_blend_design_map,
2254 T1_FIELD_DICT_FONTDICT )
2255 T1_FIELD_CALLBACK( "BlendAxisTypes", parse_blend_axis_types,
2256 T1_FIELD_DICT_FONTDICT )
2257 T1_FIELD_CALLBACK( "WeightVector", parse_weight_vector,
2258 T1_FIELD_DICT_FONTDICT )
2259 T1_FIELD_CALLBACK( "BuildCharArray", parse_buildchar,
2260 T1_FIELD_DICT_PRIVATE )
2263 { 0, T1_FIELD_LOCATION_CID_INFO, T1_FIELD_TYPE_NONE, 0, 0, 0, 0, 0, 0 }
2268 parse_dict( T1_Face face,
2273 T1_Parser parser = &loader->parser;
2274 FT_Byte *limit, *start_binary = NULL;
2275 FT_Bool have_integer = 0;
2278 parser->root.cursor = base;
2279 parser->root.limit = base + size;
2280 parser->root.error = FT_Err_Ok;
2282 limit = parser->root.limit;
2284 T1_Skip_Spaces( parser );
2286 while ( parser->root.cursor < limit )
2291 cur = parser->root.cursor;
2293 /* look for `eexec' */
2294 if ( IS_PS_TOKEN( cur, limit, "eexec" ) )
2297 /* look for `closefile' which ends the eexec section */
2298 else if ( IS_PS_TOKEN( cur, limit, "closefile" ) )
2301 /* in a synthetic font the base font starts after a */
2302 /* `FontDictionary' token that is placed after a Private dict */
2303 else if ( IS_PS_TOKEN( cur, limit, "FontDirectory" ) )
2305 if ( loader->keywords_encountered & T1_PRIVATE )
2306 loader->keywords_encountered |=
2307 T1_FONTDIR_AFTER_PRIVATE;
2308 parser->root.cursor += 13;
2311 /* check whether we have an integer */
2312 else if ( ft_isdigit( *cur ) )
2315 T1_Skip_PS_Token( parser );
2316 if ( parser->root.error )
2321 /* in valid Type 1 fonts we don't see `RD' or `-|' directly */
2322 /* since those tokens are handled by parse_subrs and */
2323 /* parse_charstrings */
2324 else if ( *cur == 'R' && cur + 6 < limit && *(cur + 1) == 'D' &&
2331 parser->root.cursor = start_binary;
2332 if ( !read_binary_data( parser, &s, &b, IS_INCREMENTAL ) )
2333 return FT_THROW( Invalid_File_Format );
2337 else if ( *cur == '-' && cur + 6 < limit && *(cur + 1) == '|' &&
2344 parser->root.cursor = start_binary;
2345 if ( !read_binary_data( parser, &s, &b, IS_INCREMENTAL ) )
2346 return FT_THROW( Invalid_File_Format );
2350 /* look for immediates */
2351 else if ( *cur == '/' && cur + 2 < limit )
2358 parser->root.cursor = cur;
2359 T1_Skip_PS_Token( parser );
2360 if ( parser->root.error )
2363 len = (FT_UInt)( parser->root.cursor - cur );
2365 if ( len > 0 && len < 22 && parser->root.cursor < limit )
2367 /* now compare the immediate name to the keyword table */
2368 T1_Field keyword = (T1_Field)t1_keywords;
2376 name = (FT_Byte*)keyword->ident;
2380 if ( cur[0] == name[0] &&
2381 len == ft_strlen( (const char *)name ) &&
2382 ft_memcmp( cur, name, len ) == 0 )
2384 /* We found it -- run the parsing callback! */
2385 /* We record every instance of every field */
2386 /* (until we reach the base font of a */
2387 /* synthetic font) to deal adequately with */
2388 /* multiple master fonts; this is also */
2389 /* necessary because later PostScript */
2390 /* definitions override earlier ones. */
2392 /* Once we encounter `FontDirectory' after */
2393 /* `/Private', we know that this is a synthetic */
2394 /* font; except for `/CharStrings' we are not */
2395 /* interested in anything that follows this */
2396 /* `FontDirectory'. */
2398 /* MM fonts have more than one /Private token at */
2399 /* the top level; let's hope that all the junk */
2400 /* that follows the first /Private token is not */
2401 /* interesting to us. */
2403 /* According to Adobe Tech Note #5175 (CID-Keyed */
2404 /* Font Installation for ATM Software) a `begin' */
2405 /* must be followed by exactly one `end', and */
2406 /* `begin' -- `end' pairs must be accurately */
2407 /* paired. We could use this to distinguish */
2408 /* between the global Private and the Private */
2409 /* dict that is a member of the Blend dict. */
2411 const FT_UInt dict =
2412 ( loader->keywords_encountered & T1_PRIVATE )
2413 ? T1_FIELD_DICT_PRIVATE
2414 : T1_FIELD_DICT_FONTDICT;
2417 if ( !( dict & keyword->dict ) )
2419 FT_TRACE1(( "parse_dict: found `%s' but ignoring it"
2420 " since it is in the wrong dictionary\n",
2425 if ( !( loader->keywords_encountered &
2426 T1_FONTDIR_AFTER_PRIVATE ) ||
2427 ft_strcmp( (const char*)name, "CharStrings" ) == 0 )
2429 parser->root.error = t1_load_keyword( face,
2432 if ( parser->root.error )
2434 if ( FT_ERR_EQ( parser->root.error, Ignore ) )
2435 parser->root.error = FT_Err_Ok;
2437 return parser->root.error;
2451 T1_Skip_PS_Token( parser );
2452 if ( parser->root.error )
2457 T1_Skip_Spaces( parser );
2461 return parser->root.error;
2466 t1_init_loader( T1_Loader loader,
2476 t1_done_loader( T1_Loader loader )
2478 T1_Parser parser = &loader->parser;
2479 FT_Memory memory = parser->root.memory;
2482 /* finalize tables */
2483 T1_Release_Table( &loader->encoding_table );
2484 T1_Release_Table( &loader->charstrings );
2485 T1_Release_Table( &loader->glyph_names );
2486 T1_Release_Table( &loader->swap_table );
2487 T1_Release_Table( &loader->subrs );
2490 ft_hash_num_free( loader->subrs_hash, memory );
2491 FT_FREE( loader->subrs_hash );
2493 /* finalize parser */
2494 T1_Finalize_Parser( parser );
2498 FT_LOCAL_DEF( FT_Error )
2499 T1_Open_Face( T1_Face face )
2501 T1_LoaderRec loader;
2503 T1_Font type1 = &face->type1;
2504 PS_Private priv = &type1->private_dict;
2507 PSAux_Service psaux = (PSAux_Service)face->psaux;
2510 t1_init_loader( &loader, face );
2512 /* default values */
2515 face->len_buildchar = 0;
2517 priv->blue_shift = 7;
2518 priv->blue_fuzz = 1;
2520 priv->expansion_factor = (FT_Fixed)( 0.06 * 0x10000L );
2521 priv->blue_scale = (FT_Fixed)( 0.039625 * 0x10000L * 1000 );
2523 parser = &loader.parser;
2524 error = T1_New_Parser( parser,
2531 FT_TRACE4(( " top dictionary:\n" ));
2532 error = parse_dict( face, &loader,
2533 parser->base_dict, parser->base_len );
2537 error = T1_Get_Private_Dict( parser, psaux );
2541 FT_TRACE4(( " private dictionary:\n" ));
2542 error = parse_dict( face, &loader,
2543 parser->private_dict, parser->private_len );
2547 /* ensure even-ness of `num_blue_values' */
2548 priv->num_blue_values &= ~1;
2550 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
2552 /* we don't support Multiple Master fonts with intermediate designs; */
2553 /* this implies that `num_designs' must be equal to `2^^num_axis' */
2555 face->blend->num_designs != ( 1U << face->blend->num_axis ) )
2557 FT_ERROR(( "T1_Open_Face:"
2558 " number-of-designs != 2 ^^ number-of-axes\n" ));
2559 T1_Done_Blend( face );
2563 face->blend->num_default_design_vector != 0 &&
2564 face->blend->num_default_design_vector != face->blend->num_axis )
2566 /* we don't use it currently so just warn, reset, and ignore */
2567 FT_ERROR(( "T1_Open_Face(): /DesignVector contains %u entries "
2568 "while there are %u axes.\n",
2569 face->blend->num_default_design_vector,
2570 face->blend->num_axis ));
2572 face->blend->num_default_design_vector = 0;
2575 /* the following can happen for MM instances; we then treat the */
2576 /* font as a normal PS font */
2578 ( !face->blend->num_designs || !face->blend->num_axis ) )
2579 T1_Done_Blend( face );
2581 /* another safety check */
2587 for ( i = 0; i < face->blend->num_axis; i++ )
2588 if ( !face->blend->design_map[i].num_points )
2590 T1_Done_Blend( face );
2597 if ( face->len_buildchar > 0 )
2599 FT_Memory memory = face->root.memory;
2602 if ( FT_NEW_ARRAY( face->buildchar, face->len_buildchar ) )
2604 FT_ERROR(( "T1_Open_Face: cannot allocate BuildCharArray\n" ));
2605 face->len_buildchar = 0;
2611 face->len_buildchar = 0;
2613 #endif /* !T1_CONFIG_OPTION_NO_MM_SUPPORT */
2615 /* now, propagate the subrs, charstrings, and glyphnames tables */
2616 /* to the Type1 data */
2617 type1->num_glyphs = loader.num_glyphs;
2619 if ( loader.subrs.init )
2621 type1->num_subrs = loader.num_subrs;
2622 type1->subrs_block = loader.subrs.block;
2623 type1->subrs = loader.subrs.elements;
2624 type1->subrs_len = loader.subrs.lengths;
2625 type1->subrs_hash = loader.subrs_hash;
2627 /* prevent `t1_done_loader' from freeing the propagated data */
2628 loader.subrs.init = 0;
2629 loader.subrs_hash = NULL;
2632 if ( !IS_INCREMENTAL )
2633 if ( !loader.charstrings.init )
2635 FT_ERROR(( "T1_Open_Face: no `/CharStrings' array in face\n" ));
2636 error = FT_THROW( Invalid_File_Format );
2639 loader.charstrings.init = 0;
2640 type1->charstrings_block = loader.charstrings.block;
2641 type1->charstrings = loader.charstrings.elements;
2642 type1->charstrings_len = loader.charstrings.lengths;
2644 /* we copy the glyph names `block' and `elements' fields; */
2645 /* the `lengths' field must be released later */
2646 type1->glyph_names_block = loader.glyph_names.block;
2647 type1->glyph_names = (FT_String**)loader.glyph_names.elements;
2648 loader.glyph_names.block = NULL;
2649 loader.glyph_names.elements = NULL;
2651 /* we must now build type1.encoding when we have a custom array */
2652 if ( type1->encoding_type == T1_ENCODING_TYPE_ARRAY )
2654 FT_Int charcode, idx, min_char, max_char;
2657 /* OK, we do the following: for each element in the encoding */
2658 /* table, look up the index of the glyph having the same name */
2659 /* the index is then stored in type1.encoding.char_index, and */
2660 /* the name to type1.encoding.char_name */
2666 for ( ; charcode < loader.encoding_table.max_elems; charcode++ )
2668 const FT_String* char_name =
2669 (const FT_String*)loader.encoding_table.elements[charcode];
2672 type1->encoding.char_index[charcode] = 0;
2673 type1->encoding.char_name [charcode] = ".notdef";
2676 for ( idx = 0; idx < type1->num_glyphs; idx++ )
2678 const FT_String* glyph_name = type1->glyph_names[idx];
2681 if ( ft_strcmp( char_name, glyph_name ) == 0 )
2683 type1->encoding.char_index[charcode] = (FT_UShort)idx;
2684 type1->encoding.char_name [charcode] = glyph_name;
2686 /* Change min/max encoded char only if glyph name is */
2688 if ( ft_strcmp( ".notdef", glyph_name ) != 0 )
2690 if ( charcode < min_char )
2691 min_char = charcode;
2692 if ( charcode >= max_char )
2693 max_char = charcode + 1;
2700 type1->encoding.code_first = min_char;
2701 type1->encoding.code_last = max_char;
2702 type1->encoding.num_chars = loader.num_chars;
2705 /* some sanitizing to avoid overflows later on; */
2706 /* the upper limits are ad-hoc values */
2707 if ( priv->blue_shift > 1000 || priv->blue_shift < 0 )
2709 FT_TRACE2(( "T1_Open_Face:"
2710 " setting unlikely BlueShift value %d to default (7)\n",
2711 priv->blue_shift ));
2712 priv->blue_shift = 7;
2715 if ( priv->blue_fuzz > 1000 || priv->blue_fuzz < 0 )
2717 FT_TRACE2(( "T1_Open_Face:"
2718 " setting unlikely BlueFuzz value %d to default (1)\n",
2720 priv->blue_fuzz = 1;
2724 t1_done_loader( &loader );