1 /***************************************************************************/
5 /* Type 1 font loader (body). */
7 /* Copyright 1996-2011 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
74 #ifdef FT_CONFIG_OPTION_INCREMENTAL
75 #define IS_INCREMENTAL ( face->root.internal->incremental_interface != 0 )
77 #define IS_INCREMENTAL 0
81 /*************************************************************************/
83 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
84 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
85 /* messages during execution. */
88 #define FT_COMPONENT trace_t1load
91 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
94 /*************************************************************************/
95 /*************************************************************************/
97 /***** MULTIPLE MASTERS SUPPORT *****/
99 /*************************************************************************/
100 /*************************************************************************/
103 t1_allocate_blend( T1_Face face,
108 FT_Memory memory = face->root.memory;
109 FT_Error error = T1_Err_Ok;
115 if ( FT_NEW( blend ) )
118 blend->num_default_design_vector = 0;
123 /* allocate design data if needed */
124 if ( num_designs > 0 )
126 if ( blend->num_designs == 0 )
131 /* allocate the blend `private' and `font_info' dictionaries */
132 if ( FT_NEW_ARRAY( blend->font_infos[1], num_designs ) ||
133 FT_NEW_ARRAY( blend->privates[1], num_designs ) ||
134 FT_NEW_ARRAY( blend->bboxes[1], num_designs ) ||
135 FT_NEW_ARRAY( blend->weight_vector, num_designs * 2 ) )
138 blend->default_weight_vector = blend->weight_vector + num_designs;
140 blend->font_infos[0] = &face->type1.font_info;
141 blend->privates [0] = &face->type1.private_dict;
142 blend->bboxes [0] = &face->type1.font_bbox;
144 for ( nn = 2; nn <= num_designs; nn++ )
146 blend->privates[nn] = blend->privates [nn - 1] + 1;
147 blend->font_infos[nn] = blend->font_infos[nn - 1] + 1;
148 blend->bboxes[nn] = blend->bboxes [nn - 1] + 1;
151 blend->num_designs = num_designs;
153 else if ( blend->num_designs != num_designs )
157 /* allocate axis data if needed */
160 if ( blend->num_axis != 0 && blend->num_axis != num_axis )
163 blend->num_axis = num_axis;
166 /* allocate the blend design pos table if needed */
167 num_designs = blend->num_designs;
168 num_axis = blend->num_axis;
169 if ( num_designs && num_axis && blend->design_pos[0] == 0 )
174 if ( FT_NEW_ARRAY( blend->design_pos[0], num_designs * num_axis ) )
177 for ( n = 1; n < num_designs; n++ )
178 blend->design_pos[n] = blend->design_pos[0] + num_axis * n;
185 error = T1_Err_Invalid_File_Format;
190 FT_LOCAL_DEF( FT_Error )
191 T1_Get_Multi_Master( T1_Face face,
192 FT_Multi_Master* master )
194 PS_Blend blend = face->blend;
199 error = T1_Err_Invalid_Argument;
203 master->num_axis = blend->num_axis;
204 master->num_designs = blend->num_designs;
206 for ( n = 0; n < blend->num_axis; n++ )
208 FT_MM_Axis* axis = master->axis + n;
209 PS_DesignMap map = blend->design_map + n;
212 axis->name = blend->axis_names[n];
213 axis->minimum = map->design_points[0];
214 axis->maximum = map->design_points[map->num_points - 1];
224 /*************************************************************************/
226 /* Given a normalized (blend) coordinate, figure out the design */
227 /* coordinate appropriate for that value. */
229 FT_LOCAL_DEF( FT_Fixed )
230 mm_axis_unmap( PS_DesignMap axismap,
236 if ( ncv <= axismap->blend_points[0] )
237 return INT_TO_FIXED( axismap->design_points[0] );
239 for ( j = 1; j < axismap->num_points; ++j )
241 if ( ncv <= axismap->blend_points[j] )
243 FT_Fixed t = FT_MulDiv( ncv - axismap->blend_points[j - 1],
245 axismap->blend_points[j] -
246 axismap->blend_points[j - 1] );
248 return INT_TO_FIXED( axismap->design_points[j - 1] ) +
250 axismap->design_points[j] -
251 axismap->design_points[j - 1],
256 return INT_TO_FIXED( axismap->design_points[axismap->num_points - 1] );
260 /*************************************************************************/
262 /* Given a vector of weights, one for each design, figure out the */
263 /* normalized axis coordinates which gave rise to those weights. */
266 mm_weights_unmap( FT_Fixed* weights,
267 FT_Fixed* axiscoords,
270 FT_ASSERT( axis_count <= T1_MAX_MM_AXIS );
272 if ( axis_count == 1 )
273 axiscoords[0] = weights[1];
275 else if ( axis_count == 2 )
277 axiscoords[0] = weights[3] + weights[1];
278 axiscoords[1] = weights[3] + weights[2];
281 else if ( axis_count == 3 )
283 axiscoords[0] = weights[7] + weights[5] + weights[3] + weights[1];
284 axiscoords[1] = weights[7] + weights[6] + weights[3] + weights[2];
285 axiscoords[2] = weights[7] + weights[6] + weights[5] + weights[4];
290 axiscoords[0] = weights[15] + weights[13] + weights[11] + weights[9] +
291 weights[7] + weights[5] + weights[3] + weights[1];
292 axiscoords[1] = weights[15] + weights[14] + weights[11] + weights[10] +
293 weights[7] + weights[6] + weights[3] + weights[2];
294 axiscoords[2] = weights[15] + weights[14] + weights[13] + weights[12] +
295 weights[7] + weights[6] + weights[5] + weights[4];
296 axiscoords[3] = weights[15] + weights[14] + weights[13] + weights[12] +
297 weights[11] + weights[10] + weights[9] + weights[8];
302 /*************************************************************************/
304 /* Just a wrapper around T1_Get_Multi_Master to support the different */
305 /* arguments needed by the GX var distortable fonts. */
307 FT_LOCAL_DEF( FT_Error )
308 T1_Get_MM_Var( T1_Face face,
311 FT_Memory memory = face->root.memory;
312 FT_MM_Var *mmvar = NULL;
313 FT_Multi_Master mmaster;
316 FT_Fixed axiscoords[T1_MAX_MM_AXIS];
317 PS_Blend blend = face->blend;
320 error = T1_Get_Multi_Master( face, &mmaster );
323 if ( FT_ALLOC( mmvar,
324 sizeof ( FT_MM_Var ) +
325 mmaster.num_axis * sizeof ( FT_Var_Axis ) ) )
328 mmvar->num_axis = mmaster.num_axis;
329 mmvar->num_designs = mmaster.num_designs;
330 mmvar->num_namedstyles = (FT_UInt)-1; /* Does not apply */
331 mmvar->axis = (FT_Var_Axis*)&mmvar[1];
332 /* Point to axes after MM_Var struct */
333 mmvar->namedstyle = NULL;
335 for ( i = 0 ; i < mmaster.num_axis; ++i )
337 mmvar->axis[i].name = mmaster.axis[i].name;
338 mmvar->axis[i].minimum = INT_TO_FIXED( mmaster.axis[i].minimum);
339 mmvar->axis[i].maximum = INT_TO_FIXED( mmaster.axis[i].maximum);
340 mmvar->axis[i].def = ( mmvar->axis[i].minimum +
341 mmvar->axis[i].maximum ) / 2;
342 /* Does not apply. But this value is in range */
343 mmvar->axis[i].strid = (FT_UInt)-1; /* Does not apply */
344 mmvar->axis[i].tag = (FT_ULong)-1; /* Does not apply */
346 if ( ft_strcmp( mmvar->axis[i].name, "Weight" ) == 0 )
347 mmvar->axis[i].tag = FT_MAKE_TAG( 'w', 'g', 'h', 't' );
348 else if ( ft_strcmp( mmvar->axis[i].name, "Width" ) == 0 )
349 mmvar->axis[i].tag = FT_MAKE_TAG( 'w', 'd', 't', 'h' );
350 else if ( ft_strcmp( mmvar->axis[i].name, "OpticalSize" ) == 0 )
351 mmvar->axis[i].tag = FT_MAKE_TAG( 'o', 'p', 's', 'z' );
354 if ( blend->num_designs == ( 1U << blend->num_axis ) )
356 mm_weights_unmap( blend->default_weight_vector,
360 for ( i = 0; i < mmaster.num_axis; ++i )
361 mmvar->axis[i].def = mm_axis_unmap( &blend->design_map[i],
372 FT_LOCAL_DEF( FT_Error )
373 T1_Set_MM_Blend( T1_Face face,
377 PS_Blend blend = face->blend;
382 error = T1_Err_Invalid_Argument;
384 if ( blend && blend->num_axis == num_coords )
386 /* recompute the weight vector from the blend coordinates */
389 for ( n = 0; n < blend->num_designs; n++ )
391 FT_Fixed result = 0x10000L; /* 1.0 fixed */
394 for ( m = 0; m < blend->num_axis; m++ )
399 /* get current blend axis position */
401 if ( factor < 0 ) factor = 0;
402 if ( factor > 0x10000L ) factor = 0x10000L;
404 if ( ( n & ( 1 << m ) ) == 0 )
405 factor = 0x10000L - factor;
407 result = FT_MulFix( result, factor );
409 blend->weight_vector[n] = result;
419 FT_LOCAL_DEF( FT_Error )
420 T1_Set_MM_Design( T1_Face face,
424 PS_Blend blend = face->blend;
429 error = T1_Err_Invalid_Argument;
430 if ( blend && blend->num_axis == num_coords )
432 /* compute the blend coordinates through the blend design map */
433 FT_Fixed final_blends[T1_MAX_MM_DESIGNS];
436 for ( n = 0; n < blend->num_axis; n++ )
438 FT_Long design = coords[n];
440 PS_DesignMap map = blend->design_map + n;
441 FT_Long* designs = map->design_points;
442 FT_Fixed* blends = map->blend_points;
443 FT_Int before = -1, after = -1;
446 for ( p = 0; p < (FT_UInt)map->num_points; p++ )
448 FT_Long p_design = designs[p];
452 if ( design == p_design )
454 the_blend = blends[p];
458 if ( design < p_design )
467 /* now interpolate if necessary */
469 the_blend = blends[0];
471 else if ( after < 0 )
472 the_blend = blends[map->num_points - 1];
475 the_blend = FT_MulDiv( design - designs[before],
476 blends [after] - blends [before],
477 designs[after] - designs[before] );
480 final_blends[n] = the_blend;
483 error = T1_Set_MM_Blend( face, num_coords, final_blends );
490 /*************************************************************************/
492 /* Just a wrapper around T1_Set_MM_Design to support the different */
493 /* arguments needed by the GX var distortable fonts. */
495 FT_LOCAL_DEF( FT_Error )
496 T1_Set_Var_Design( T1_Face face,
500 FT_Long lcoords[4]; /* maximum axis count is 4 */
505 error = T1_Err_Invalid_Argument;
506 if ( num_coords <= 4 && num_coords > 0 )
508 for ( i = 0; i < num_coords; ++i )
509 lcoords[i] = FIXED_TO_INT( coords[i] );
510 error = T1_Set_MM_Design( face, num_coords, lcoords );
518 T1_Done_Blend( T1_Face face )
520 FT_Memory memory = face->root.memory;
521 PS_Blend blend = face->blend;
526 FT_UInt num_designs = blend->num_designs;
527 FT_UInt num_axis = blend->num_axis;
531 /* release design pos table */
532 FT_FREE( blend->design_pos[0] );
533 for ( n = 1; n < num_designs; n++ )
534 blend->design_pos[n] = 0;
536 /* release blend `private' and `font info' dictionaries */
537 FT_FREE( blend->privates[1] );
538 FT_FREE( blend->font_infos[1] );
539 FT_FREE( blend->bboxes[1] );
541 for ( n = 0; n < num_designs; n++ )
543 blend->privates [n] = 0;
544 blend->font_infos[n] = 0;
545 blend->bboxes [n] = 0;
548 /* release weight vectors */
549 FT_FREE( blend->weight_vector );
550 blend->default_weight_vector = 0;
552 /* release axis names */
553 for ( n = 0; n < num_axis; n++ )
554 FT_FREE( blend->axis_names[n] );
556 /* release design map */
557 for ( n = 0; n < num_axis; n++ )
559 PS_DesignMap dmap = blend->design_map + n;
562 FT_FREE( dmap->design_points );
563 dmap->num_points = 0;
566 FT_FREE( face->blend );
572 parse_blend_axis_types( T1_Face face,
575 T1_TokenRec axis_tokens[T1_MAX_MM_AXIS];
577 FT_Error error = T1_Err_Ok;
582 /* take an array of objects */
583 T1_ToTokenArray( &loader->parser, axis_tokens,
584 T1_MAX_MM_AXIS, &num_axis );
587 error = T1_Err_Ignore;
590 if ( num_axis == 0 || num_axis > T1_MAX_MM_AXIS )
592 FT_ERROR(( "parse_blend_axis_types: incorrect number of axes: %d\n",
594 error = T1_Err_Invalid_File_Format;
598 /* allocate blend if necessary */
599 error = t1_allocate_blend( face, 0, (FT_UInt)num_axis );
604 memory = face->root.memory;
606 /* each token is an immediate containing the name of the axis */
607 for ( n = 0; n < num_axis; n++ )
609 T1_Token token = axis_tokens + n;
614 /* skip first slash, if any */
615 if ( token->start[0] == '/' )
618 len = token->limit - token->start;
621 error = T1_Err_Invalid_File_Format;
625 if ( FT_ALLOC( blend->axis_names[n], len + 1 ) )
628 name = (FT_Byte*)blend->axis_names[n];
629 FT_MEM_COPY( name, token->start, len );
634 loader->parser.root.error = error;
639 parse_blend_design_positions( T1_Face face,
642 T1_TokenRec design_tokens[T1_MAX_MM_DESIGNS];
645 T1_Parser parser = &loader->parser;
647 FT_Error error = T1_Err_Ok;
651 /* get the array of design tokens -- compute number of designs */
652 T1_ToTokenArray( parser, design_tokens,
653 T1_MAX_MM_DESIGNS, &num_designs );
654 if ( num_designs < 0 )
656 error = T1_Err_Ignore;
659 if ( num_designs == 0 || num_designs > T1_MAX_MM_DESIGNS )
661 FT_ERROR(( "parse_blend_design_positions:"
662 " incorrect number of designs: %d\n",
664 error = T1_Err_Invalid_File_Format;
669 FT_Byte* old_cursor = parser->root.cursor;
670 FT_Byte* old_limit = parser->root.limit;
675 num_axis = 0; /* make compiler happy */
677 for ( n = 0; n < num_designs; n++ )
679 T1_TokenRec axis_tokens[T1_MAX_MM_AXIS];
684 /* read axis/coordinates tokens */
685 token = design_tokens + n;
686 parser->root.cursor = token->start;
687 parser->root.limit = token->limit;
688 T1_ToTokenArray( parser, axis_tokens, T1_MAX_MM_AXIS, &n_axis );
692 if ( n_axis <= 0 || n_axis > T1_MAX_MM_AXIS )
694 FT_ERROR(( "parse_blend_design_positions:"
695 " invalid number of axes: %d\n",
697 error = T1_Err_Invalid_File_Format;
702 error = t1_allocate_blend( face, num_designs, num_axis );
707 else if ( n_axis != num_axis )
709 FT_ERROR(( "parse_blend_design_positions: incorrect table\n" ));
710 error = T1_Err_Invalid_File_Format;
714 /* now read each axis token into the design position */
715 for ( axis = 0; axis < n_axis; axis++ )
717 T1_Token token2 = axis_tokens + axis;
720 parser->root.cursor = token2->start;
721 parser->root.limit = token2->limit;
722 blend->design_pos[n][axis] = T1_ToFixed( parser, 0 );
726 loader->parser.root.cursor = old_cursor;
727 loader->parser.root.limit = old_limit;
731 loader->parser.root.error = error;
736 parse_blend_design_map( T1_Face face,
739 FT_Error error = T1_Err_Ok;
740 T1_Parser parser = &loader->parser;
742 T1_TokenRec axis_tokens[T1_MAX_MM_AXIS];
746 FT_Memory memory = face->root.memory;
749 T1_ToTokenArray( parser, axis_tokens,
750 T1_MAX_MM_AXIS, &num_axis );
753 error = T1_Err_Ignore;
756 if ( num_axis == 0 || num_axis > T1_MAX_MM_AXIS )
758 FT_ERROR(( "parse_blend_design_map: incorrect number of axes: %d\n",
760 error = T1_Err_Invalid_File_Format;
764 old_cursor = parser->root.cursor;
765 old_limit = parser->root.limit;
767 error = t1_allocate_blend( face, 0, num_axis );
772 /* now read each axis design map */
773 for ( n = 0; n < num_axis; n++ )
775 PS_DesignMap map = blend->design_map + n;
777 T1_TokenRec point_tokens[T1_MAX_MM_MAP_POINTS];
778 FT_Int p, num_points;
781 axis_token = axis_tokens + n;
783 parser->root.cursor = axis_token->start;
784 parser->root.limit = axis_token->limit;
785 T1_ToTokenArray( parser, point_tokens,
786 T1_MAX_MM_MAP_POINTS, &num_points );
788 if ( num_points <= 0 || num_points > T1_MAX_MM_MAP_POINTS )
790 FT_ERROR(( "parse_blend_design_map: incorrect table\n" ));
791 error = T1_Err_Invalid_File_Format;
795 /* allocate design map data */
796 if ( FT_NEW_ARRAY( map->design_points, num_points * 2 ) )
798 map->blend_points = map->design_points + num_points;
799 map->num_points = (FT_Byte)num_points;
801 for ( p = 0; p < num_points; p++ )
803 T1_Token point_token;
806 point_token = point_tokens + p;
808 /* don't include delimiting brackets */
809 parser->root.cursor = point_token->start + 1;
810 parser->root.limit = point_token->limit - 1;
812 map->design_points[p] = T1_ToInt( parser );
813 map->blend_points [p] = T1_ToFixed( parser, 0 );
817 parser->root.cursor = old_cursor;
818 parser->root.limit = old_limit;
821 parser->root.error = error;
826 parse_weight_vector( T1_Face face,
829 T1_TokenRec design_tokens[T1_MAX_MM_DESIGNS];
831 FT_Error error = T1_Err_Ok;
832 T1_Parser parser = &loader->parser;
833 PS_Blend blend = face->blend;
840 T1_ToTokenArray( parser, design_tokens,
841 T1_MAX_MM_DESIGNS, &num_designs );
842 if ( num_designs < 0 )
844 error = T1_Err_Ignore;
847 if ( num_designs == 0 || num_designs > T1_MAX_MM_DESIGNS )
849 FT_ERROR(( "parse_weight_vector:"
850 " incorrect number of designs: %d\n",
852 error = T1_Err_Invalid_File_Format;
856 if ( !blend || !blend->num_designs )
858 error = t1_allocate_blend( face, num_designs, 0 );
863 else if ( blend->num_designs != (FT_UInt)num_designs )
865 FT_ERROR(( "parse_weight_vector:"
866 " /BlendDesignPosition and /WeightVector have\n"
868 " different number of elements\n" ));
869 error = T1_Err_Invalid_File_Format;
873 old_cursor = parser->root.cursor;
874 old_limit = parser->root.limit;
876 for ( n = 0; n < num_designs; n++ )
878 token = design_tokens + n;
879 parser->root.cursor = token->start;
880 parser->root.limit = token->limit;
882 blend->default_weight_vector[n] =
883 blend->weight_vector[n] = T1_ToFixed( parser, 0 );
886 parser->root.cursor = old_cursor;
887 parser->root.limit = old_limit;
890 parser->root.error = error;
894 /* e.g., /BuildCharArray [0 0 0 0 0 0 0 0] def */
895 /* we're only interested in the number of array elements */
897 parse_buildchar( T1_Face face,
900 face->len_buildchar = T1_ToFixedArray( &loader->parser, 0, NULL, 0 );
906 #endif /* !T1_CONFIG_OPTION_NO_MM_SUPPORT */
911 /*************************************************************************/
912 /*************************************************************************/
914 /***** TYPE 1 SYMBOL PARSING *****/
916 /*************************************************************************/
917 /*************************************************************************/
920 t1_load_keyword( T1_Face face,
922 const T1_Field field )
928 PS_Blend blend = face->blend;
931 if ( blend && blend->num_designs == 0 )
934 /* if the keyword has a dedicated callback, call it */
935 if ( field->type == T1_FIELD_TYPE_CALLBACK )
937 field->reader( (FT_Face)face, loader );
938 error = loader->parser.root.error;
942 /* now, the keyword is either a simple field, or a table of fields; */
943 /* we are now going to take care of it */
944 switch ( field->location )
946 case T1_FIELD_LOCATION_FONT_INFO:
947 dummy_object = &face->type1.font_info;
948 objects = &dummy_object;
953 objects = (void**)blend->font_infos;
954 max_objects = blend->num_designs;
958 case T1_FIELD_LOCATION_FONT_EXTRA:
959 dummy_object = &face->type1.font_extra;
960 objects = &dummy_object;
964 case T1_FIELD_LOCATION_PRIVATE:
965 dummy_object = &face->type1.private_dict;
966 objects = &dummy_object;
971 objects = (void**)blend->privates;
972 max_objects = blend->num_designs;
976 case T1_FIELD_LOCATION_BBOX:
977 dummy_object = &face->type1.font_bbox;
978 objects = &dummy_object;
983 objects = (void**)blend->bboxes;
984 max_objects = blend->num_designs;
988 case T1_FIELD_LOCATION_LOADER:
989 dummy_object = loader;
990 objects = &dummy_object;
994 case T1_FIELD_LOCATION_FACE:
996 objects = &dummy_object;
1000 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
1001 case T1_FIELD_LOCATION_BLEND:
1002 dummy_object = face->blend;
1003 objects = &dummy_object;
1009 dummy_object = &face->type1;
1010 objects = &dummy_object;
1014 if ( field->type == T1_FIELD_TYPE_INTEGER_ARRAY ||
1015 field->type == T1_FIELD_TYPE_FIXED_ARRAY )
1016 error = T1_Load_Field_Table( &loader->parser, field,
1017 objects, max_objects, 0 );
1019 error = T1_Load_Field( &loader->parser, field,
1020 objects, max_objects, 0 );
1028 parse_private( T1_Face face,
1033 loader->keywords_encountered |= T1_PRIVATE;
1038 read_binary_data( T1_Parser parser,
1041 FT_Bool incremental )
1044 FT_Byte* limit = parser->root.limit;
1047 /* the binary data has one of the following formats */
1049 /* `size' [white*] RD white ....... ND */
1050 /* `size' [white*] -| white ....... |- */
1053 T1_Skip_Spaces( parser );
1055 cur = parser->root.cursor;
1057 if ( cur < limit && ft_isdigit( *cur ) )
1059 FT_Long s = T1_ToInt( parser );
1062 T1_Skip_PS_Token( parser ); /* `RD' or `-|' or something else */
1064 /* there is only one whitespace char after the */
1065 /* `RD' or `-|' token */
1066 *base = parser->root.cursor + 1;
1068 if ( s >= 0 && s < limit - *base )
1070 parser->root.cursor += s + 1;
1072 return !parser->root.error;
1078 FT_ERROR(( "read_binary_data: invalid size field\n" ));
1079 parser->root.error = T1_Err_Invalid_File_Format;
1086 /* We now define the routines to handle the `/Encoding', `/Subrs', */
1087 /* and `/CharStrings' dictionaries. */
1090 parse_font_matrix( T1_Face face,
1093 T1_Parser parser = &loader->parser;
1094 FT_Matrix* matrix = &face->type1.font_matrix;
1095 FT_Vector* offset = &face->type1.font_offset;
1096 FT_Face root = (FT_Face)&face->root;
1098 FT_Fixed temp_scale;
1102 result = T1_ToFixedArray( parser, 6, temp, 3 );
1106 parser->root.error = T1_Err_Invalid_File_Format;
1110 temp_scale = FT_ABS( temp[3] );
1112 if ( temp_scale == 0 )
1114 FT_ERROR(( "parse_font_matrix: invalid font matrix\n" ));
1115 parser->root.error = T1_Err_Invalid_File_Format;
1119 /* Set Units per EM based on FontMatrix values. We set the value to */
1120 /* 1000 / temp_scale, because temp_scale was already multiplied by */
1121 /* 1000 (in t1_tofixed, from psobjs.c). */
1123 root->units_per_EM = (FT_UShort)( FT_DivFix( 1000 * 0x10000L,
1124 temp_scale ) >> 16 );
1126 /* we need to scale the values by 1.0/temp_scale */
1127 if ( temp_scale != 0x10000L )
1129 temp[0] = FT_DivFix( temp[0], temp_scale );
1130 temp[1] = FT_DivFix( temp[1], temp_scale );
1131 temp[2] = FT_DivFix( temp[2], temp_scale );
1132 temp[4] = FT_DivFix( temp[4], temp_scale );
1133 temp[5] = FT_DivFix( temp[5], temp_scale );
1134 temp[3] = temp[3] < 0 ? -0x10000L : 0x10000L;
1137 matrix->xx = temp[0];
1138 matrix->yx = temp[1];
1139 matrix->xy = temp[2];
1140 matrix->yy = temp[3];
1142 /* note that the offsets must be expressed in integer font units */
1143 offset->x = temp[4] >> 16;
1144 offset->y = temp[5] >> 16;
1149 parse_encoding( T1_Face face,
1152 T1_Parser parser = &loader->parser;
1154 FT_Byte* limit = parser->root.limit;
1156 PSAux_Service psaux = (PSAux_Service)face->psaux;
1159 T1_Skip_Spaces( parser );
1160 cur = parser->root.cursor;
1163 FT_ERROR(( "parse_encoding: out of bounds\n" ));
1164 parser->root.error = T1_Err_Invalid_File_Format;
1168 /* if we have a number or `[', the encoding is an array, */
1169 /* and we must load it now */
1170 if ( ft_isdigit( *cur ) || *cur == '[' )
1172 T1_Encoding encode = &face->type1.encoding;
1174 PS_Table char_table = &loader->encoding_table;
1175 FT_Memory memory = parser->root.memory;
1177 FT_Bool only_immediates = 0;
1180 /* read the number of entries in the encoding; should be 256 */
1184 only_immediates = 1;
1185 parser->root.cursor++;
1188 count = (FT_Int)T1_ToInt( parser );
1190 T1_Skip_Spaces( parser );
1191 if ( parser->root.cursor >= limit )
1194 /* we use a T1_Table to store our charnames */
1195 loader->num_chars = encode->num_chars = count;
1196 if ( FT_NEW_ARRAY( encode->char_index, count ) ||
1197 FT_NEW_ARRAY( encode->char_name, count ) ||
1198 FT_SET_ERROR( psaux->ps_table_funcs->init(
1199 char_table, count, memory ) ) )
1201 parser->root.error = error;
1205 /* We need to `zero' out encoding_table.elements */
1206 for ( n = 0; n < count; n++ )
1208 char* notdef = (char *)".notdef";
1211 T1_Add_Table( char_table, n, notdef, 8 );
1214 /* Now we need to read records of the form */
1216 /* ... charcode /charname ... */
1218 /* for each entry in our table. */
1220 /* We simply look for a number followed by an immediate */
1221 /* name. Note that this ignores correctly the sequence */
1222 /* that is often seen in type1 fonts: */
1224 /* 0 1 255 { 1 index exch /.notdef put } for dup */
1226 /* used to clean the encoding array before anything else. */
1228 /* Alternatively, if the array is directly given as */
1230 /* /Encoding [ ... ] */
1232 /* we only read immediates. */
1235 T1_Skip_Spaces( parser );
1237 while ( parser->root.cursor < limit )
1239 cur = parser->root.cursor;
1241 /* we stop when we encounter a `def' or `]' */
1242 if ( *cur == 'd' && cur + 3 < limit )
1244 if ( cur[1] == 'e' &&
1246 IS_PS_DELIM( cur[3] ) )
1248 FT_TRACE6(( "encoding end\n" ));
1255 FT_TRACE6(( "encoding end\n" ));
1260 /* check whether we've found an entry */
1261 if ( ft_isdigit( *cur ) || only_immediates )
1266 if ( only_immediates )
1270 charcode = (FT_Int)T1_ToInt( parser );
1271 T1_Skip_Spaces( parser );
1274 cur = parser->root.cursor;
1276 if ( *cur == '/' && cur + 2 < limit && n < count )
1283 parser->root.cursor = cur;
1284 T1_Skip_PS_Token( parser );
1285 if ( parser->root.error )
1288 len = parser->root.cursor - cur;
1290 parser->root.error = T1_Add_Table( char_table, charcode,
1292 if ( parser->root.error )
1294 char_table->elements[charcode][len] = '\0';
1298 else if ( only_immediates )
1300 /* Since the current position is not updated for */
1301 /* immediates-only mode we would get an infinite loop if */
1302 /* we don't do anything here. */
1304 /* This encoding array is not valid according to the type1 */
1305 /* specification (it might be an encoding for a CID type1 */
1306 /* font, however), so we conclude that this font is NOT a */
1308 parser->root.error = FT_Err_Unknown_File_Format;
1314 T1_Skip_PS_Token( parser );
1315 if ( parser->root.error )
1319 T1_Skip_Spaces( parser );
1322 face->type1.encoding_type = T1_ENCODING_TYPE_ARRAY;
1323 parser->root.cursor = cur;
1326 /* Otherwise, we should have either `StandardEncoding', */
1327 /* `ExpertEncoding', or `ISOLatin1Encoding' */
1330 if ( cur + 17 < limit &&
1331 ft_strncmp( (const char*)cur, "StandardEncoding", 16 ) == 0 )
1332 face->type1.encoding_type = T1_ENCODING_TYPE_STANDARD;
1334 else if ( cur + 15 < limit &&
1335 ft_strncmp( (const char*)cur, "ExpertEncoding", 14 ) == 0 )
1336 face->type1.encoding_type = T1_ENCODING_TYPE_EXPERT;
1338 else if ( cur + 18 < limit &&
1339 ft_strncmp( (const char*)cur, "ISOLatin1Encoding", 17 ) == 0 )
1340 face->type1.encoding_type = T1_ENCODING_TYPE_ISOLATIN1;
1343 parser->root.error = T1_Err_Ignore;
1349 parse_subrs( T1_Face face,
1352 T1_Parser parser = &loader->parser;
1353 PS_Table table = &loader->subrs;
1354 FT_Memory memory = parser->root.memory;
1358 PSAux_Service psaux = (PSAux_Service)face->psaux;
1361 T1_Skip_Spaces( parser );
1363 /* test for empty array */
1364 if ( parser->root.cursor < parser->root.limit &&
1365 *parser->root.cursor == '[' )
1367 T1_Skip_PS_Token( parser );
1368 T1_Skip_Spaces ( parser );
1369 if ( parser->root.cursor >= parser->root.limit ||
1370 *parser->root.cursor != ']' )
1371 parser->root.error = T1_Err_Invalid_File_Format;
1375 num_subrs = (FT_Int)T1_ToInt( parser );
1377 /* position the parser right before the `dup' of the first subr */
1378 T1_Skip_PS_Token( parser ); /* `array' */
1379 if ( parser->root.error )
1381 T1_Skip_Spaces( parser );
1383 /* initialize subrs array -- with synthetic fonts it is possible */
1384 /* we get here twice */
1385 if ( !loader->num_subrs )
1387 error = psaux->ps_table_funcs->init( table, num_subrs, memory );
1392 /* the format is simple: */
1394 /* `index' + binary data */
1402 /* If we are out of data, or if the next token isn't `dup', */
1404 if ( parser->root.cursor + 4 >= parser->root.limit ||
1405 ft_strncmp( (char*)parser->root.cursor, "dup", 3 ) != 0 )
1408 T1_Skip_PS_Token( parser ); /* `dup' */
1410 idx = T1_ToInt( parser );
1412 if ( !read_binary_data( parser, &size, &base, IS_INCREMENTAL ) )
1415 /* The binary string is followed by one token, e.g. `NP' */
1416 /* (bound to `noaccess put') or by two separate tokens: */
1417 /* `noaccess' & `put'. We position the parser right */
1418 /* before the next `dup', if any. */
1419 T1_Skip_PS_Token( parser ); /* `NP' or `|' or `noaccess' */
1420 if ( parser->root.error )
1422 T1_Skip_Spaces ( parser );
1424 if ( parser->root.cursor + 4 < parser->root.limit &&
1425 ft_strncmp( (char*)parser->root.cursor, "put", 3 ) == 0 )
1427 T1_Skip_PS_Token( parser ); /* skip `put' */
1428 T1_Skip_Spaces ( parser );
1431 /* with synthetic fonts it is possible we get here twice */
1432 if ( loader->num_subrs )
1435 /* some fonts use a value of -1 for lenIV to indicate that */
1436 /* the charstrings are unencoded */
1438 /* thanks to Tom Kacvinsky for pointing this out */
1440 if ( face->type1.private_dict.lenIV >= 0 )
1445 /* some fonts define empty subr records -- this is not totally */
1446 /* compliant to the specification (which says they should at */
1447 /* least contain a `return'), but we support them anyway */
1448 if ( size < face->type1.private_dict.lenIV )
1450 error = T1_Err_Invalid_File_Format;
1454 /* t1_decrypt() shouldn't write to base -- make temporary copy */
1455 if ( FT_ALLOC( temp, size ) )
1457 FT_MEM_COPY( temp, base, size );
1458 psaux->t1_decrypt( temp, size, 4330 );
1459 size -= face->type1.private_dict.lenIV;
1460 error = T1_Add_Table( table, (FT_Int)idx,
1461 temp + face->type1.private_dict.lenIV, size );
1465 error = T1_Add_Table( table, (FT_Int)idx, base, size );
1470 if ( !loader->num_subrs )
1471 loader->num_subrs = num_subrs;
1476 parser->root.error = error;
1480 #define TABLE_EXTEND 5
1484 parse_charstrings( T1_Face face,
1487 T1_Parser parser = &loader->parser;
1488 PS_Table code_table = &loader->charstrings;
1489 PS_Table name_table = &loader->glyph_names;
1490 PS_Table swap_table = &loader->swap_table;
1491 FT_Memory memory = parser->root.memory;
1494 PSAux_Service psaux = (PSAux_Service)face->psaux;
1497 FT_Byte* limit = parser->root.limit;
1498 FT_Int n, num_glyphs;
1499 FT_UInt notdef_index = 0;
1500 FT_Byte notdef_found = 0;
1503 num_glyphs = (FT_Int)T1_ToInt( parser );
1504 /* some fonts like Optima-Oblique not only define the /CharStrings */
1505 /* array but access it also */
1506 if ( num_glyphs == 0 || parser->root.error )
1509 /* initialize tables, leaving space for addition of .notdef, */
1510 /* if necessary, and a few other glyphs to handle buggy */
1511 /* fonts which have more glyphs than specified. */
1513 /* for some non-standard fonts like `Optima' which provides */
1514 /* different outlines depending on the resolution it is */
1515 /* possible to get here twice */
1516 if ( !loader->num_glyphs )
1518 error = psaux->ps_table_funcs->init(
1519 code_table, num_glyphs + 1 + TABLE_EXTEND, memory );
1523 error = psaux->ps_table_funcs->init(
1524 name_table, num_glyphs + 1 + TABLE_EXTEND, memory );
1528 /* Initialize table for swapping index notdef_index and */
1529 /* index 0 names and codes (if necessary). */
1531 error = psaux->ps_table_funcs->init( swap_table, 4, memory );
1544 /* the format is simple: */
1545 /* `/glyphname' + binary data */
1547 T1_Skip_Spaces( parser );
1549 cur = parser->root.cursor;
1553 /* we stop when we find a `def' or `end' keyword */
1554 if ( cur + 3 < limit && IS_PS_DELIM( cur[3] ) )
1556 if ( cur[0] == 'd' &&
1560 /* There are fonts which have this: */
1562 /* /CharStrings 118 dict def */
1564 /* CharStrings begin */
1567 /* To catch this we ignore `def' if */
1568 /* no charstring has actually been */
1574 if ( cur[0] == 'e' &&
1580 T1_Skip_PS_Token( parser );
1581 if ( parser->root.error )
1589 if ( cur + 1 >= limit )
1591 error = T1_Err_Invalid_File_Format;
1595 cur++; /* skip `/' */
1596 len = parser->root.cursor - cur;
1598 if ( !read_binary_data( parser, &size, &base, IS_INCREMENTAL ) )
1601 /* for some non-standard fonts like `Optima' which provides */
1602 /* different outlines depending on the resolution it is */
1603 /* possible to get here twice */
1604 if ( loader->num_glyphs )
1607 error = T1_Add_Table( name_table, n, cur, len + 1 );
1611 /* add a trailing zero to the name table */
1612 name_table->elements[n][len] = '\0';
1614 /* record index of /.notdef */
1616 ft_strcmp( ".notdef",
1617 (const char*)(name_table->elements[n]) ) == 0 )
1623 if ( face->type1.private_dict.lenIV >= 0 &&
1624 n < num_glyphs + TABLE_EXTEND )
1629 if ( size <= face->type1.private_dict.lenIV )
1631 error = T1_Err_Invalid_File_Format;
1635 /* t1_decrypt() shouldn't write to base -- make temporary copy */
1636 if ( FT_ALLOC( temp, size ) )
1638 FT_MEM_COPY( temp, base, size );
1639 psaux->t1_decrypt( temp, size, 4330 );
1640 size -= face->type1.private_dict.lenIV;
1641 error = T1_Add_Table( code_table, n,
1642 temp + face->type1.private_dict.lenIV, size );
1646 error = T1_Add_Table( code_table, n, base, size );
1654 loader->num_glyphs = n;
1656 /* if /.notdef is found but does not occupy index 0, do our magic. */
1657 if ( notdef_found &&
1658 ft_strcmp( ".notdef", (const char*)name_table->elements[0] ) )
1660 /* Swap glyph in index 0 with /.notdef glyph. First, add index 0 */
1661 /* name and code entries to swap_table. Then place notdef_index */
1662 /* name and code entries into swap_table. Then swap name and code */
1663 /* entries at indices notdef_index and 0 using values stored in */
1667 error = T1_Add_Table( swap_table, 0,
1668 name_table->elements[0],
1669 name_table->lengths [0] );
1674 error = T1_Add_Table( swap_table, 1,
1675 code_table->elements[0],
1676 code_table->lengths [0] );
1680 /* Index notdef_index name */
1681 error = T1_Add_Table( swap_table, 2,
1682 name_table->elements[notdef_index],
1683 name_table->lengths [notdef_index] );
1687 /* Index notdef_index code */
1688 error = T1_Add_Table( swap_table, 3,
1689 code_table->elements[notdef_index],
1690 code_table->lengths [notdef_index] );
1694 error = T1_Add_Table( name_table, notdef_index,
1695 swap_table->elements[0],
1696 swap_table->lengths [0] );
1700 error = T1_Add_Table( code_table, notdef_index,
1701 swap_table->elements[1],
1702 swap_table->lengths [1] );
1706 error = T1_Add_Table( name_table, 0,
1707 swap_table->elements[2],
1708 swap_table->lengths [2] );
1712 error = T1_Add_Table( code_table, 0,
1713 swap_table->elements[3],
1714 swap_table->lengths [3] );
1719 else if ( !notdef_found )
1721 /* notdef_index is already 0, or /.notdef is undefined in */
1722 /* charstrings dictionary. Worry about /.notdef undefined. */
1723 /* We take index 0 and add it to the end of the table(s) */
1724 /* and add our own /.notdef glyph to index 0. */
1726 /* 0 333 hsbw endchar */
1727 FT_Byte notdef_glyph[] = { 0x8B, 0xF7, 0xE1, 0x0D, 0x0E };
1728 char* notdef_name = (char *)".notdef";
1731 error = T1_Add_Table( swap_table, 0,
1732 name_table->elements[0],
1733 name_table->lengths [0] );
1737 error = T1_Add_Table( swap_table, 1,
1738 code_table->elements[0],
1739 code_table->lengths [0] );
1743 error = T1_Add_Table( name_table, 0, notdef_name, 8 );
1747 error = T1_Add_Table( code_table, 0, notdef_glyph, 5 );
1752 error = T1_Add_Table( name_table, n,
1753 swap_table->elements[0],
1754 swap_table->lengths [0] );
1758 error = T1_Add_Table( code_table, n,
1759 swap_table->elements[1],
1760 swap_table->lengths [1] );
1764 /* we added a glyph. */
1765 loader->num_glyphs += 1;
1771 parser->root.error = error;
1775 /*************************************************************************/
1777 /* Define the token field static variables. This is a set of */
1778 /* T1_FieldRec variables. */
1780 /*************************************************************************/
1784 const T1_FieldRec t1_keywords[] =
1787 #include "t1tokens.h"
1789 /* now add the special functions... */
1790 T1_FIELD_CALLBACK( "FontMatrix", parse_font_matrix,
1791 T1_FIELD_DICT_FONTDICT )
1792 T1_FIELD_CALLBACK( "Encoding", parse_encoding,
1793 T1_FIELD_DICT_FONTDICT )
1794 T1_FIELD_CALLBACK( "Subrs", parse_subrs,
1795 T1_FIELD_DICT_PRIVATE )
1796 T1_FIELD_CALLBACK( "CharStrings", parse_charstrings,
1797 T1_FIELD_DICT_PRIVATE )
1798 T1_FIELD_CALLBACK( "Private", parse_private,
1799 T1_FIELD_DICT_FONTDICT )
1801 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
1802 T1_FIELD_CALLBACK( "BlendDesignPositions", parse_blend_design_positions,
1803 T1_FIELD_DICT_FONTDICT )
1804 T1_FIELD_CALLBACK( "BlendDesignMap", parse_blend_design_map,
1805 T1_FIELD_DICT_FONTDICT )
1806 T1_FIELD_CALLBACK( "BlendAxisTypes", parse_blend_axis_types,
1807 T1_FIELD_DICT_FONTDICT )
1808 T1_FIELD_CALLBACK( "WeightVector", parse_weight_vector,
1809 T1_FIELD_DICT_FONTDICT )
1810 T1_FIELD_CALLBACK( "BuildCharArray", parse_buildchar,
1811 T1_FIELD_DICT_PRIVATE )
1814 { 0, T1_FIELD_LOCATION_CID_INFO, T1_FIELD_TYPE_NONE, 0, 0, 0, 0, 0, 0 }
1818 #define T1_FIELD_COUNT \
1819 ( sizeof ( t1_keywords ) / sizeof ( t1_keywords[0] ) )
1823 parse_dict( T1_Face face,
1828 T1_Parser parser = &loader->parser;
1829 FT_Byte *limit, *start_binary = NULL;
1830 FT_Bool have_integer = 0;
1833 parser->root.cursor = base;
1834 parser->root.limit = base + size;
1835 parser->root.error = T1_Err_Ok;
1837 limit = parser->root.limit;
1839 T1_Skip_Spaces( parser );
1841 while ( parser->root.cursor < limit )
1846 cur = parser->root.cursor;
1848 /* look for `eexec' */
1849 if ( IS_PS_TOKEN( cur, limit, "eexec" ) )
1852 /* look for `closefile' which ends the eexec section */
1853 else if ( IS_PS_TOKEN( cur, limit, "closefile" ) )
1856 /* in a synthetic font the base font starts after a */
1857 /* `FontDictionary' token that is placed after a Private dict */
1858 else if ( IS_PS_TOKEN( cur, limit, "FontDirectory" ) )
1860 if ( loader->keywords_encountered & T1_PRIVATE )
1861 loader->keywords_encountered |=
1862 T1_FONTDIR_AFTER_PRIVATE;
1863 parser->root.cursor += 13;
1866 /* check whether we have an integer */
1867 else if ( ft_isdigit( *cur ) )
1870 T1_Skip_PS_Token( parser );
1871 if ( parser->root.error )
1876 /* in valid Type 1 fonts we don't see `RD' or `-|' directly */
1877 /* since those tokens are handled by parse_subrs and */
1878 /* parse_charstrings */
1879 else if ( *cur == 'R' && cur + 6 < limit && *(cur + 1) == 'D' &&
1886 parser->root.cursor = start_binary;
1887 if ( !read_binary_data( parser, &s, &b, IS_INCREMENTAL ) )
1888 return T1_Err_Invalid_File_Format;
1892 else if ( *cur == '-' && cur + 6 < limit && *(cur + 1) == '|' &&
1899 parser->root.cursor = start_binary;
1900 if ( !read_binary_data( parser, &s, &b, IS_INCREMENTAL ) )
1901 return T1_Err_Invalid_File_Format;
1905 /* look for immediates */
1906 else if ( *cur == '/' && cur + 2 < limit )
1913 parser->root.cursor = cur;
1914 T1_Skip_PS_Token( parser );
1915 if ( parser->root.error )
1918 len = parser->root.cursor - cur;
1920 if ( len > 0 && len < 22 && parser->root.cursor < limit )
1922 /* now compare the immediate name to the keyword table */
1923 T1_Field keyword = (T1_Field)t1_keywords;
1931 name = (FT_Byte*)keyword->ident;
1935 if ( cur[0] == name[0] &&
1936 len == (FT_PtrDist)ft_strlen( (const char *)name ) &&
1937 ft_memcmp( cur, name, len ) == 0 )
1939 /* We found it -- run the parsing callback! */
1940 /* We record every instance of every field */
1941 /* (until we reach the base font of a */
1942 /* synthetic font) to deal adequately with */
1943 /* multiple master fonts; this is also */
1944 /* necessary because later PostScript */
1945 /* definitions override earlier ones. */
1947 /* Once we encounter `FontDirectory' after */
1948 /* `/Private', we know that this is a synthetic */
1949 /* font; except for `/CharStrings' we are not */
1950 /* interested in anything that follows this */
1951 /* `FontDirectory'. */
1953 /* MM fonts have more than one /Private token at */
1954 /* the top level; let's hope that all the junk */
1955 /* that follows the first /Private token is not */
1956 /* interesting to us. */
1958 /* According to Adobe Tech Note #5175 (CID-Keyed */
1959 /* Font Installation for ATM Software) a `begin' */
1960 /* must be followed by exactly one `end', and */
1961 /* `begin' -- `end' pairs must be accurately */
1962 /* paired. We could use this to distinguish */
1963 /* between the global Private and the Private */
1964 /* dict that is a member of the Blend dict. */
1966 const FT_UInt dict =
1967 ( loader->keywords_encountered & T1_PRIVATE )
1968 ? T1_FIELD_DICT_PRIVATE
1969 : T1_FIELD_DICT_FONTDICT;
1971 if ( !( dict & keyword->dict ) )
1973 FT_TRACE1(( "parse_dict: found %s but ignoring it "
1974 "since it is in the wrong dictionary\n",
1979 if ( !( loader->keywords_encountered &
1980 T1_FONTDIR_AFTER_PRIVATE ) ||
1981 ft_strcmp( (const char*)name, "CharStrings" ) == 0 )
1983 parser->root.error = t1_load_keyword( face,
1986 if ( parser->root.error != T1_Err_Ok )
1988 if ( FT_ERROR_BASE( parser->root.error ) == FT_Err_Ignore )
1989 parser->root.error = T1_Err_Ok;
1991 return parser->root.error;
2005 T1_Skip_PS_Token( parser );
2006 if ( parser->root.error )
2011 T1_Skip_Spaces( parser );
2015 return parser->root.error;
2020 t1_init_loader( T1_Loader loader,
2025 FT_MEM_ZERO( loader, sizeof ( *loader ) );
2026 loader->num_glyphs = 0;
2027 loader->num_chars = 0;
2029 /* initialize the tables -- simply set their `init' field to 0 */
2030 loader->encoding_table.init = 0;
2031 loader->charstrings.init = 0;
2032 loader->glyph_names.init = 0;
2033 loader->subrs.init = 0;
2034 loader->swap_table.init = 0;
2035 loader->fontdata = 0;
2036 loader->keywords_encountered = 0;
2041 t1_done_loader( T1_Loader loader )
2043 T1_Parser parser = &loader->parser;
2046 /* finalize tables */
2047 T1_Release_Table( &loader->encoding_table );
2048 T1_Release_Table( &loader->charstrings );
2049 T1_Release_Table( &loader->glyph_names );
2050 T1_Release_Table( &loader->swap_table );
2051 T1_Release_Table( &loader->subrs );
2053 /* finalize parser */
2054 T1_Finalize_Parser( parser );
2058 FT_LOCAL_DEF( FT_Error )
2059 T1_Open_Face( T1_Face face )
2061 T1_LoaderRec loader;
2063 T1_Font type1 = &face->type1;
2064 PS_Private priv = &type1->private_dict;
2067 PSAux_Service psaux = (PSAux_Service)face->psaux;
2070 t1_init_loader( &loader, face );
2072 /* default values */
2075 face->len_buildchar = 0;
2077 priv->blue_shift = 7;
2078 priv->blue_fuzz = 1;
2080 priv->expansion_factor = (FT_Fixed)( 0.06 * 0x10000L );
2081 priv->blue_scale = (FT_Fixed)( 0.039625 * 0x10000L * 1000 );
2083 parser = &loader.parser;
2084 error = T1_New_Parser( parser,
2091 error = parse_dict( face, &loader,
2092 parser->base_dict, parser->base_len );
2096 error = T1_Get_Private_Dict( parser, psaux );
2100 error = parse_dict( face, &loader,
2101 parser->private_dict, parser->private_len );
2105 /* ensure even-ness of `num_blue_values' */
2106 priv->num_blue_values &= ~1;
2108 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
2111 face->blend->num_default_design_vector != 0 &&
2112 face->blend->num_default_design_vector != face->blend->num_axis )
2114 /* we don't use it currently so just warn, reset, and ignore */
2115 FT_ERROR(( "T1_Open_Face(): /DesignVector contains %u entries "
2116 "while there are %u axes.\n",
2117 face->blend->num_default_design_vector,
2118 face->blend->num_axis ));
2120 face->blend->num_default_design_vector = 0;
2123 /* the following can happen for MM instances; we then treat the */
2124 /* font as a normal PS font */
2126 ( !face->blend->num_designs || !face->blend->num_axis ) )
2127 T1_Done_Blend( face );
2129 /* another safety check */
2135 for ( i = 0; i < face->blend->num_axis; i++ )
2136 if ( !face->blend->design_map[i].num_points )
2138 T1_Done_Blend( face );
2145 if ( face->len_buildchar > 0 )
2147 FT_Memory memory = face->root.memory;
2150 if ( FT_NEW_ARRAY( face->buildchar, face->len_buildchar ) )
2152 FT_ERROR(( "T1_Open_Face: cannot allocate BuildCharArray\n" ));
2153 face->len_buildchar = 0;
2159 face->len_buildchar = 0;
2161 #endif /* !T1_CONFIG_OPTION_NO_MM_SUPPORT */
2163 /* now, propagate the subrs, charstrings, and glyphnames tables */
2164 /* to the Type1 data */
2165 type1->num_glyphs = loader.num_glyphs;
2167 if ( loader.subrs.init )
2169 loader.subrs.init = 0;
2170 type1->num_subrs = loader.num_subrs;
2171 type1->subrs_block = loader.subrs.block;
2172 type1->subrs = loader.subrs.elements;
2173 type1->subrs_len = loader.subrs.lengths;
2176 if ( !IS_INCREMENTAL )
2177 if ( !loader.charstrings.init )
2179 FT_ERROR(( "T1_Open_Face: no `/CharStrings' array in face\n" ));
2180 error = T1_Err_Invalid_File_Format;
2183 loader.charstrings.init = 0;
2184 type1->charstrings_block = loader.charstrings.block;
2185 type1->charstrings = loader.charstrings.elements;
2186 type1->charstrings_len = loader.charstrings.lengths;
2188 /* we copy the glyph names `block' and `elements' fields; */
2189 /* the `lengths' field must be released later */
2190 type1->glyph_names_block = loader.glyph_names.block;
2191 type1->glyph_names = (FT_String**)loader.glyph_names.elements;
2192 loader.glyph_names.block = 0;
2193 loader.glyph_names.elements = 0;
2195 /* we must now build type1.encoding when we have a custom array */
2196 if ( type1->encoding_type == T1_ENCODING_TYPE_ARRAY )
2198 FT_Int charcode, idx, min_char, max_char;
2200 FT_Byte* glyph_name;
2203 /* OK, we do the following: for each element in the encoding */
2204 /* table, look up the index of the glyph having the same name */
2205 /* the index is then stored in type1.encoding.char_index, and */
2206 /* the name to type1.encoding.char_name */
2212 for ( ; charcode < loader.encoding_table.max_elems; charcode++ )
2214 type1->encoding.char_index[charcode] = 0;
2215 type1->encoding.char_name [charcode] = (char *)".notdef";
2217 char_name = loader.encoding_table.elements[charcode];
2219 for ( idx = 0; idx < type1->num_glyphs; idx++ )
2221 glyph_name = (FT_Byte*)type1->glyph_names[idx];
2222 if ( ft_strcmp( (const char*)char_name,
2223 (const char*)glyph_name ) == 0 )
2225 type1->encoding.char_index[charcode] = (FT_UShort)idx;
2226 type1->encoding.char_name [charcode] = (char*)glyph_name;
2228 /* Change min/max encoded char only if glyph name is */
2230 if ( ft_strcmp( (const char*)".notdef",
2231 (const char*)glyph_name ) != 0 )
2233 if ( charcode < min_char )
2234 min_char = charcode;
2235 if ( charcode >= max_char )
2236 max_char = charcode + 1;
2243 type1->encoding.code_first = min_char;
2244 type1->encoding.code_last = max_char;
2245 type1->encoding.num_chars = loader.num_chars;
2249 t1_done_loader( &loader );