1 /***************************************************************************/
5 /* Type 1 font loader (body). */
7 /* Copyright 1996-2001, 2002 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. */
46 /* This file is included twice here; the first time to generate parsing */
47 /* callback functions, the second to generate a table of keywords (with */
48 /* pointers to the associated callback). */
50 /* The function `parse_dict' simply scans *linearly* a given dictionary */
51 /* (either the top-level or private one) and calls the appropriate */
52 /* callback when it encounters an immediate keyword. */
54 /* This is by far the fastest way one can find to parse and read all */
57 /* This led to tremendous code size reduction. Note that later, the */
58 /* glyph loader will also be _greatly_ simplified, and the automatic */
59 /* hinter will replace the clumsy `t1hinter'. */
61 /*************************************************************************/
65 #include FT_INTERNAL_DEBUG_H
66 #include FT_CONFIG_CONFIG_H
67 #include FT_MULTIPLE_MASTERS_H
68 #include FT_INTERNAL_TYPE1_TYPES_H
75 /*************************************************************************/
77 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
78 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
79 /* messages during execution. */
82 #define FT_COMPONENT trace_t1load
85 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
88 /*************************************************************************/
89 /*************************************************************************/
91 /***** MULTIPLE MASTERS SUPPORT *****/
93 /*************************************************************************/
94 /*************************************************************************/
97 t1_allocate_blend( T1_Face face,
102 FT_Memory memory = face->root.memory;
109 if ( FT_NEW( blend ) )
115 /* allocate design data if needed */
116 if ( num_designs > 0 )
118 if ( blend->num_designs == 0 )
123 /* allocate the blend `private' and `font_info' dictionaries */
124 if ( FT_NEW_ARRAY( blend->font_infos[1], num_designs ) ||
125 FT_NEW_ARRAY( blend->privates[1], num_designs ) ||
126 FT_NEW_ARRAY( blend->bboxes[1], num_designs ) ||
127 FT_NEW_ARRAY( blend->weight_vector, num_designs * 2 ) )
130 blend->default_weight_vector = blend->weight_vector + num_designs;
132 blend->font_infos[0] = &face->type1.font_info;
133 blend->privates [0] = &face->type1.private_dict;
134 blend->bboxes [0] = &face->type1.font_bbox;
136 for ( nn = 2; nn <= num_designs; nn++ )
138 blend->privates[nn] = blend->privates [nn - 1] + 1;
139 blend->font_infos[nn] = blend->font_infos[nn - 1] + 1;
140 blend->bboxes[nn] = blend->bboxes [nn - 1] + 1;
143 blend->num_designs = num_designs;
145 else if ( blend->num_designs != num_designs )
149 /* allocate axis data if needed */
152 if ( blend->num_axis != 0 && blend->num_axis != num_axis )
155 blend->num_axis = num_axis;
158 /* allocate the blend design pos table if needed */
159 num_designs = blend->num_designs;
160 num_axis = blend->num_axis;
161 if ( num_designs && num_axis && blend->design_pos[0] == 0 )
166 if ( FT_NEW_ARRAY( blend->design_pos[0], num_designs * num_axis ) )
169 for ( n = 1; n < num_designs; n++ )
170 blend->design_pos[n] = blend->design_pos[0] + num_axis * n;
182 FT_LOCAL_DEF( FT_Error )
183 T1_Get_Multi_Master( T1_Face face,
184 FT_Multi_Master* master )
186 PS_Blend blend = face->blend;
191 error = T1_Err_Invalid_Argument;
195 master->num_axis = blend->num_axis;
196 master->num_designs = blend->num_designs;
198 for ( n = 0; n < blend->num_axis; n++ )
200 FT_MM_Axis* axis = master->axis + n;
201 PS_DesignMap map = blend->design_map + n;
204 axis->name = blend->axis_names[n];
205 axis->minimum = map->design_points[0];
206 axis->maximum = map->design_points[map->num_points - 1];
214 FT_LOCAL_DEF( FT_Error )
215 T1_Set_MM_Blend( T1_Face face,
219 PS_Blend blend = face->blend;
224 error = T1_Err_Invalid_Argument;
226 if ( blend && blend->num_axis == num_coords )
228 /* recompute the weight vector from the blend coordinates */
231 for ( n = 0; n < blend->num_designs; n++ )
233 FT_Fixed result = 0x10000L; /* 1.0 fixed */
236 for ( m = 0; m < blend->num_axis; m++ )
241 /* get current blend axis position */
243 if ( factor < 0 ) factor = 0;
244 if ( factor > 0x10000L ) factor = 0x10000L;
246 if ( ( n & ( 1 << m ) ) == 0 )
247 factor = 0x10000L - factor;
249 result = FT_MulFix( result, factor );
251 blend->weight_vector[n] = result;
260 FT_LOCAL_DEF( FT_Error )
261 T1_Set_MM_Design( T1_Face face,
265 PS_Blend blend = face->blend;
270 error = T1_Err_Invalid_Argument;
271 if ( blend && blend->num_axis == num_coords )
273 /* compute the blend coordinates through the blend design map */
274 FT_Fixed final_blends[T1_MAX_MM_DESIGNS];
277 for ( n = 0; n < blend->num_axis; n++ )
279 FT_Long design = coords[n];
281 PS_DesignMap map = blend->design_map + n;
282 FT_Fixed* designs = map->design_points;
283 FT_Fixed* blends = map->blend_points;
284 FT_Int before = -1, after = -1;
287 for ( p = 0; p < (FT_UInt)map->num_points; p++ )
289 FT_Fixed p_design = designs[p];
293 if ( design == p_design )
295 the_blend = blends[p];
299 if ( design < p_design )
308 /* now, interpolate if needed */
310 the_blend = blends[0];
312 else if ( after < 0 )
313 the_blend = blends[map->num_points - 1];
316 the_blend = FT_MulDiv( design - designs[before],
317 blends [after] - blends [before],
318 designs[after] - designs[before] );
321 final_blends[n] = the_blend;
324 error = T1_Set_MM_Blend( face, num_coords, final_blends );
332 T1_Done_Blend( T1_Face face )
334 FT_Memory memory = face->root.memory;
335 PS_Blend blend = face->blend;
340 FT_UInt num_designs = blend->num_designs;
341 FT_UInt num_axis = blend->num_axis;
345 /* release design pos table */
346 FT_FREE( blend->design_pos[0] );
347 for ( n = 1; n < num_designs; n++ )
348 blend->design_pos[n] = 0;
350 /* release blend `private' and `font info' dictionaries */
351 FT_FREE( blend->privates[1] );
352 FT_FREE( blend->font_infos[1] );
353 FT_FREE( blend->bboxes[1] );
355 for ( n = 0; n < num_designs; n++ )
357 blend->privates [n] = 0;
358 blend->font_infos[n] = 0;
359 blend->bboxes [n] = 0;
362 /* release weight vectors */
363 FT_FREE( blend->weight_vector );
364 blend->default_weight_vector = 0;
366 /* release axis names */
367 for ( n = 0; n < num_axis; n++ )
368 FT_FREE( blend->axis_names[n] );
370 /* release design map */
371 for ( n = 0; n < num_axis; n++ )
373 PS_DesignMap dmap = blend->design_map + n;
376 FT_FREE( dmap->design_points );
377 dmap->num_points = 0;
380 FT_FREE( face->blend );
386 parse_blend_axis_types( T1_Face face,
389 T1_TokenRec axis_tokens[ T1_MAX_MM_AXIS ];
396 /* take an array of objects */
397 T1_ToTokenArray( &loader->parser, axis_tokens,
398 T1_MAX_MM_AXIS, &num_axis );
399 if ( num_axis <= 0 || num_axis > T1_MAX_MM_AXIS )
401 FT_ERROR(( "parse_blend_axis_types: incorrect number of axes: %d\n",
403 error = T1_Err_Invalid_File_Format;
407 /* allocate blend if necessary */
408 error = t1_allocate_blend( face, 0, (FT_UInt)num_axis );
413 memory = face->root.memory;
415 /* each token is an immediate containing the name of the axis */
416 for ( n = 0; n < num_axis; n++ )
418 T1_Token token = axis_tokens + n;
423 /* skip first slash, if any */
424 if ( token->start[0] == '/' )
427 len = token->limit - token->start;
430 error = T1_Err_Invalid_File_Format;
434 if ( FT_ALLOC( blend->axis_names[n], len + 1 ) )
437 name = (FT_Byte*)blend->axis_names[n];
438 FT_MEM_COPY( name, token->start, len );
443 loader->parser.root.error = error;
448 parse_blend_design_positions( T1_Face face,
451 T1_TokenRec design_tokens[ T1_MAX_MM_DESIGNS ];
454 T1_Parser parser = &loader->parser;
460 /* get the array of design tokens - compute number of designs */
461 T1_ToTokenArray( parser, design_tokens, T1_MAX_MM_DESIGNS, &num_designs );
462 if ( num_designs <= 0 || num_designs > T1_MAX_MM_DESIGNS )
464 FT_ERROR(( "parse_blend_design_positions:" ));
465 FT_ERROR(( " incorrect number of designs: %d\n",
467 error = T1_Err_Invalid_File_Format;
472 FT_Byte* old_cursor = parser->root.cursor;
473 FT_Byte* old_limit = parser->root.limit;
478 num_axis = 0; /* make compiler happy */
480 for ( n = 0; n < (FT_UInt)num_designs; n++ )
482 T1_TokenRec axis_tokens[ T1_MAX_MM_DESIGNS ];
487 /* read axis/coordinates tokens */
488 token = design_tokens + n;
489 parser->root.cursor = token->start - 1;
490 parser->root.limit = token->limit + 1;
491 T1_ToTokenArray( parser, axis_tokens, T1_MAX_MM_AXIS, &n_axis );
496 error = t1_allocate_blend( face, num_designs, num_axis );
501 else if ( n_axis != num_axis )
503 FT_ERROR(( "parse_blend_design_positions: incorrect table\n" ));
504 error = T1_Err_Invalid_File_Format;
508 /* now, read each axis token into the design position */
509 for ( axis = 0; axis < n_axis; axis++ )
511 T1_Token token2 = axis_tokens + axis;
514 parser->root.cursor = token2->start;
515 parser->root.limit = token2->limit;
516 blend->design_pos[n][axis] = T1_ToFixed( parser, 0 );
520 loader->parser.root.cursor = old_cursor;
521 loader->parser.root.limit = old_limit;
525 loader->parser.root.error = error;
530 parse_blend_design_map( T1_Face face,
534 T1_Parser parser = &loader->parser;
536 T1_TokenRec axis_tokens[T1_MAX_MM_AXIS];
540 FT_Memory memory = face->root.memory;
543 T1_ToTokenArray( parser, axis_tokens, T1_MAX_MM_AXIS, &num_axis );
544 if ( num_axis <= 0 || num_axis > T1_MAX_MM_AXIS )
546 FT_ERROR(( "parse_blend_design_map: incorrect number of axes: %d\n",
548 error = T1_Err_Invalid_File_Format;
551 old_cursor = parser->root.cursor;
552 old_limit = parser->root.limit;
554 error = t1_allocate_blend( face, 0, num_axis );
559 /* now, read each axis design map */
560 for ( n = 0; n < num_axis; n++ )
562 PS_DesignMap map = blend->design_map + n;
564 FT_Int p, num_points;
567 token = axis_tokens + n;
568 parser->root.cursor = token->start;
569 parser->root.limit = token->limit;
571 /* count the number of map points */
573 FT_Byte* ptr = token->start;
574 FT_Byte* limit = token->limit;
578 for ( ; ptr < limit; ptr++ )
582 if ( num_points <= 0 || num_points > T1_MAX_MM_MAP_POINTS )
584 FT_ERROR(( "parse_blend_design_map: incorrect table\n" ));
585 error = T1_Err_Invalid_File_Format;
589 /* allocate design map data */
590 if ( FT_NEW_ARRAY( map->design_points, num_points * 2 ) )
592 map->blend_points = map->design_points + num_points;
593 map->num_points = (FT_Byte)num_points;
595 for ( p = 0; p < num_points; p++ )
597 map->design_points[p] = T1_ToInt( parser );
598 map->blend_points [p] = T1_ToFixed( parser, 0 );
602 parser->root.cursor = old_cursor;
603 parser->root.limit = old_limit;
606 parser->root.error = error;
611 parse_weight_vector( T1_Face face,
615 T1_Parser parser = &loader->parser;
616 PS_Blend blend = face->blend;
623 if ( !blend || blend->num_designs == 0 )
625 FT_ERROR(( "parse_weight_vector: too early!\n" ));
626 error = T1_Err_Invalid_File_Format;
630 T1_ToToken( parser, &master );
631 if ( master.type != T1_TOKEN_TYPE_ARRAY )
633 FT_ERROR(( "parse_weight_vector: incorrect format!\n" ));
634 error = T1_Err_Invalid_File_Format;
638 old_cursor = parser->root.cursor;
639 old_limit = parser->root.limit;
641 parser->root.cursor = master.start;
642 parser->root.limit = master.limit;
644 for ( n = 0; n < blend->num_designs; n++ )
646 blend->default_weight_vector[n] =
647 blend->weight_vector[n] = T1_ToFixed( parser, 0 );
650 parser->root.cursor = old_cursor;
651 parser->root.limit = old_limit;
654 parser->root.error = error;
658 /* the keyword `/shareddict' appears in some multiple master fonts */
659 /* with a lot of Postscript garbage behind it (that's completely out */
660 /* of spec!); we detect it and terminate the parsing */
663 parse_shared_dict( T1_Face face,
666 T1_Parser parser = &loader->parser;
671 parser->root.cursor = parser->root.limit;
672 parser->root.error = 0;
675 #endif /* T1_CONFIG_OPTION_NO_MM_SUPPORT */
678 /*************************************************************************/
679 /*************************************************************************/
681 /***** TYPE 1 SYMBOL PARSING *****/
683 /*************************************************************************/
684 /*************************************************************************/
687 /*************************************************************************/
689 /* First of all, define the token field static variables. This is a set */
690 /* of T1_FieldRec variables used later. */
692 /*************************************************************************/
696 t1_load_keyword( T1_Face face,
704 PS_Blend blend = face->blend;
707 /* if the keyword has a dedicated callback, call it */
708 if ( field->type == T1_FIELD_TYPE_CALLBACK )
710 field->reader( (FT_Face)face, loader );
711 error = loader->parser.root.error;
715 /* now, the keyword is either a simple field, or a table of fields; */
716 /* we are now going to take care of it */
717 switch ( field->location )
719 case T1_FIELD_LOCATION_FONT_INFO:
720 dummy_object = &face->type1.font_info;
721 objects = &dummy_object;
726 objects = (void**)blend->font_infos;
727 max_objects = blend->num_designs;
731 case T1_FIELD_LOCATION_PRIVATE:
732 dummy_object = &face->type1.private_dict;
733 objects = &dummy_object;
738 objects = (void**)blend->privates;
739 max_objects = blend->num_designs;
743 case T1_FIELD_LOCATION_BBOX:
744 dummy_object = &face->type1.font_bbox;
745 objects = &dummy_object;
750 objects = (void**)blend->bboxes;
751 max_objects = blend->num_designs;
756 dummy_object = &face->type1;
757 objects = &dummy_object;
761 if ( field->type == T1_FIELD_TYPE_INTEGER_ARRAY ||
762 field->type == T1_FIELD_TYPE_FIXED_ARRAY )
763 error = T1_Load_Field_Table( &loader->parser, field,
764 objects, max_objects, 0 );
766 error = T1_Load_Field( &loader->parser, field,
767 objects, max_objects, 0 );
775 is_space( FT_Byte c )
777 return ( c == ' ' || c == '\t' || c == '\r' || c == '\n' );
782 is_name_char( FT_Byte c )
784 /* Note: PostScript allows any non-delimiting, non-whitespace */
785 /* in a name (PS Ref Manual, 3rd Ed, p31) */
786 /* PostScript delimiters include (,),<,>,[,],{,},/ and % */
804 read_binary_data( T1_Parser parser,
809 FT_Byte* limit = parser->root.limit;
812 /* the binary data has the following format */
814 /* `size' [white*] RD white ....... ND */
817 T1_Skip_Spaces( parser );
818 cur = parser->root.cursor;
820 if ( cur < limit && (FT_Byte)( *cur - '0' ) < 10 )
822 *size = T1_ToInt( parser );
824 T1_Skip_Spaces( parser );
825 T1_Skip_Alpha ( parser ); /* `RD' or `-|' or something else */
827 /* there is only one whitespace char after the */
828 /* `RD' or `-|' token */
829 *base = parser->root.cursor + 1;
831 parser->root.cursor += *size + 1;
835 FT_ERROR(( "read_binary_data: invalid size field\n" ));
836 parser->root.error = T1_Err_Invalid_File_Format;
841 /* we will now define the routines used to handle */
842 /* the `/Encoding', `/Subrs', and `/CharStrings' */
846 parse_font_name( T1_Face face,
849 T1_Parser parser = &loader->parser;
851 FT_Memory memory = parser->root.memory;
858 if ( face->type1.font_name )
859 /* with synthetic fonts, it's possible we get here twice */
862 T1_Skip_Spaces( parser );
864 cur = parser->root.cursor;
865 limit = parser->root.limit;
867 if ( cur >= limit - 1 || *cur != '/' )
872 while ( cur2 < limit && is_name_char( *cur2 ) )
878 if ( FT_ALLOC( face->type1.font_name, len + 1 ) )
880 parser->root.error = error;
884 FT_MEM_COPY( face->type1.font_name, cur, len );
885 face->type1.font_name[len] = '\0';
887 parser->root.cursor = cur2;
893 parse_font_bbox( T1_Face face,
896 T1_Parser parser = &loader->parser;
898 FT_BBox* bbox = &face->type1.font_bbox;
901 (void)T1_ToFixedArray( parser, 4, temp, 0 );
902 bbox->xMin = FT_RoundFix( temp[0] );
903 bbox->yMin = FT_RoundFix( temp[1] );
904 bbox->xMax = FT_RoundFix( temp[2] );
905 bbox->yMax = FT_RoundFix( temp[3] );
911 parse_font_matrix( T1_Face face,
914 T1_Parser parser = &loader->parser;
915 FT_Matrix* matrix = &face->type1.font_matrix;
916 FT_Vector* offset = &face->type1.font_offset;
917 FT_Face root = (FT_Face)&face->root;
922 if ( matrix->xx || matrix->yx )
923 /* with synthetic fonts, it's possible we get here twice */
926 (void)T1_ToFixedArray( parser, 6, temp, 3 );
928 temp_scale = ABS( temp[3] );
930 /* Set Units per EM based on FontMatrix values. We set the value to */
931 /* 1000 / temp_scale, because temp_scale was already multiplied by */
932 /* 1000 (in t1_tofixed, from psobjs.c). */
934 root->units_per_EM = (FT_UShort)( FT_DivFix( 1000 * 0x10000L,
935 temp_scale ) >> 16 );
937 /* we need to scale the values by 1.0/temp_scale */
938 if ( temp_scale != 0x10000L )
940 temp[0] = FT_DivFix( temp[0], temp_scale );
941 temp[1] = FT_DivFix( temp[1], temp_scale );
942 temp[2] = FT_DivFix( temp[2], temp_scale );
943 temp[4] = FT_DivFix( temp[4], temp_scale );
944 temp[5] = FT_DivFix( temp[5], temp_scale );
948 matrix->xx = temp[0];
949 matrix->yx = temp[1];
950 matrix->xy = temp[2];
951 matrix->yy = temp[3];
953 /* note that the offsets must be expressed in integer font units */
954 offset->x = temp[4] >> 16;
955 offset->y = temp[5] >> 16;
960 parse_encoding( T1_Face face,
963 T1_Parser parser = &loader->parser;
964 FT_Byte* cur = parser->root.cursor;
965 FT_Byte* limit = parser->root.limit;
967 PSAux_Service psaux = (PSAux_Service)face->psaux;
970 /* skip whitespace */
971 while ( is_space( *cur ) )
976 FT_ERROR(( "parse_encoding: out of bounds!\n" ));
977 parser->root.error = T1_Err_Invalid_File_Format;
982 /* if we have a number, then the encoding is an array, */
983 /* and we must load it now */
984 if ( (FT_Byte)( *cur - '0' ) < 10 )
986 T1_Encoding encode = &face->type1.encoding;
988 PS_Table char_table = &loader->encoding_table;
989 FT_Memory memory = parser->root.memory;
993 if ( encode->char_index )
994 /* with synthetic fonts, it's possible we get here twice */
997 /* read the number of entries in the encoding, should be 256 */
998 count = (FT_Int)T1_ToInt( parser );
999 if ( parser->root.error )
1002 /* we use a T1_Table to store our charnames */
1003 loader->num_chars = encode->num_chars = count;
1004 if ( FT_NEW_ARRAY( encode->char_index, count ) ||
1005 FT_NEW_ARRAY( encode->char_name, count ) ||
1006 FT_SET_ERROR( psaux->ps_table_funcs->init(
1007 char_table, count, memory ) ) )
1009 parser->root.error = error;
1013 /* We need to `zero' out encoding_table.elements */
1014 for ( n = 0; n < count; n++ )
1016 char* notdef = (char *)".notdef";
1019 T1_Add_Table( char_table, n, notdef, 8 );
1022 /* Now, we will need to read a record of the form */
1023 /* ... charcode /charname ... for each entry in our table */
1025 /* We simply look for a number followed by an immediate */
1026 /* name. Note that this ignores correctly the sequence */
1027 /* that is often seen in type1 fonts: */
1029 /* 0 1 255 { 1 index exch /.notdef put } for dup */
1031 /* used to clean the encoding array before anything else. */
1033 /* We stop when we encounter a `def'. */
1035 cur = parser->root.cursor;
1036 limit = parser->root.limit;
1039 for ( ; cur < limit; )
1046 /* we stop when we encounter a `def' */
1047 if ( c == 'd' && cur + 3 < limit )
1049 if ( cur[1] == 'e' &&
1051 is_space( cur[-1] ) &&
1052 is_space( cur[3] ) )
1054 FT_TRACE6(( "encoding end\n" ));
1059 /* otherwise, we must find a number before anything else */
1060 if ( (FT_Byte)( c - '0' ) < 10 )
1065 parser->root.cursor = cur;
1066 charcode = (FT_Int)T1_ToInt( parser );
1067 cur = parser->root.cursor;
1069 /* skip whitespace */
1070 while ( cur < limit && is_space( *cur ) )
1073 if ( cur < limit && *cur == '/' )
1075 /* bingo, we have an immediate name -- it must be a */
1076 /* character name */
1077 FT_Byte* cur2 = cur + 1;
1081 while ( cur2 < limit && is_name_char( *cur2 ) )
1084 len = cur2 - cur - 1;
1086 parser->root.error = T1_Add_Table( char_table, charcode,
1088 char_table->elements[charcode][len] = '\0';
1089 if ( parser->root.error )
1099 face->type1.encoding_type = T1_ENCODING_TYPE_ARRAY;
1100 parser->root.cursor = cur;
1102 /* Otherwise, we should have either `StandardEncoding', */
1103 /* `ExpertEncoding', or `ISOLatin1Encoding' */
1106 if ( cur + 17 < limit &&
1107 ft_strncmp( (const char*)cur, "StandardEncoding", 16 ) == 0 )
1108 face->type1.encoding_type = T1_ENCODING_TYPE_STANDARD;
1110 else if ( cur + 15 < limit &&
1111 ft_strncmp( (const char*)cur, "ExpertEncoding", 14 ) == 0 )
1112 face->type1.encoding_type = T1_ENCODING_TYPE_EXPERT;
1114 else if ( cur + 18 < limit &&
1115 ft_strncmp( (const char*)cur, "ISOLatin1Encoding", 17 ) == 0 )
1116 face->type1.encoding_type = T1_ENCODING_TYPE_ISOLATIN1;
1120 FT_ERROR(( "parse_encoding: invalid token!\n" ));
1121 parser->root.error = T1_Err_Invalid_File_Format;
1128 parse_subrs( T1_Face face,
1131 T1_Parser parser = &loader->parser;
1132 PS_Table table = &loader->subrs;
1133 FT_Memory memory = parser->root.memory;
1137 PSAux_Service psaux = (PSAux_Service)face->psaux;
1140 if ( loader->num_subrs )
1141 /* with synthetic fonts, it's possible we get here twice */
1144 if ( parser->root.cursor + 2 > parser->root.limit &&
1145 parser->root.cursor[0] == '[' &&
1146 parser->root.cursor[1] == ']' )
1152 loader->num_subrs = (FT_Int)T1_ToInt( parser );
1153 if ( parser->root.error )
1156 /* position the parser right before the `dup' of the first subr */
1157 T1_Skip_Spaces( parser );
1158 T1_Skip_Alpha( parser ); /* `array' */
1159 T1_Skip_Spaces( parser );
1161 /* initialize subrs array */
1162 error = psaux->ps_table_funcs->init( table, loader->num_subrs, memory );
1166 /* the format is simple: */
1168 /* `index' + binary data */
1170 for ( n = 0; n < loader->num_subrs; n++ )
1176 /* If the next token isn't `dup', we are also done. This */
1177 /* happens when there are `holes' in the Subrs array. */
1178 if ( ft_strncmp( (char*)parser->root.cursor, "dup", 3 ) != 0 )
1181 idx = T1_ToInt( parser );
1183 if ( !read_binary_data( parser, &size, &base ) )
1186 /* The binary string is followed by one token, e.g. `NP' */
1187 /* (bound to `noaccess put') or by two separate tokens: */
1188 /* `noaccess' & `put'. We position the parser right */
1189 /* before the next `dup', if any. */
1190 T1_Skip_Spaces( parser );
1191 T1_Skip_Alpha( parser ); /* `NP' or `I' or `noaccess' */
1192 T1_Skip_Spaces( parser );
1194 if ( ft_strncmp( (char*)parser->root.cursor, "put", 3 ) == 0 )
1196 T1_Skip_Alpha( parser ); /* skip `put' */
1197 T1_Skip_Spaces( parser );
1200 /* some fonts use a value of -1 for lenIV to indicate that */
1201 /* the charstrings are unencoded */
1203 /* thanks to Tom Kacvinsky for pointing this out */
1205 if ( face->type1.private_dict.lenIV >= 0 )
1210 /* t1_decrypt() shouldn't write to base -- make temporary copy */
1211 if ( FT_ALLOC( temp, size ) )
1213 FT_MEM_COPY( temp, base, size );
1214 psaux->t1_decrypt( temp, size, 4330 );
1215 size -= face->type1.private_dict.lenIV;
1216 error = T1_Add_Table( table, idx,
1217 temp + face->type1.private_dict.lenIV, size );
1221 error = T1_Add_Table( table, idx, base, size );
1228 parser->root.error = error;
1233 parse_charstrings( T1_Face face,
1236 T1_Parser parser = &loader->parser;
1237 PS_Table code_table = &loader->charstrings;
1238 PS_Table name_table = &loader->glyph_names;
1239 PS_Table swap_table = &loader->swap_table;
1240 FT_Memory memory = parser->root.memory;
1243 PSAux_Service psaux = (PSAux_Service)face->psaux;
1246 FT_Byte* limit = parser->root.limit;
1248 FT_UInt notdef_index = 0;
1249 FT_Byte notdef_found = 0;
1252 if ( loader->num_glyphs )
1253 /* with synthetic fonts, it's possible we get here twice */
1256 loader->num_glyphs = (FT_Int)T1_ToInt( parser );
1257 if ( parser->root.error )
1260 /* initialize tables (leaving room for addition of .notdef, */
1261 /* if necessary). */
1263 error = psaux->ps_table_funcs->init( code_table,
1264 loader->num_glyphs + 1,
1269 error = psaux->ps_table_funcs->init( name_table,
1270 loader->num_glyphs + 1,
1275 /* Initialize table for swapping index notdef_index and */
1276 /* index 0 names and codes (if necessary). */
1278 error = psaux->ps_table_funcs->init( swap_table, 4, memory );
1291 /* the format is simple: */
1292 /* `/glyphname' + binary data */
1294 /* note that we stop when we find a `def' */
1296 T1_Skip_Spaces( parser );
1298 cur = parser->root.cursor;
1302 /* we stop when we find a `def' or `end' keyword */
1316 T1_Skip_Alpha( parser );
1319 FT_Byte* cur2 = cur + 1;
1323 while ( cur2 < limit && is_name_char( *cur2 ) )
1325 len = cur2 - cur - 1;
1327 error = T1_Add_Table( name_table, n, cur + 1, len + 1 );
1331 /* add a trailing zero to the name table */
1332 name_table->elements[n][len] = '\0';
1334 /* record index of /.notdef */
1335 if ( ft_strcmp( (const char*)".notdef",
1336 (const char*)(name_table->elements[n]) ) == 0 )
1342 parser->root.cursor = cur2;
1343 if ( !read_binary_data( parser, &size, &base ) )
1346 if ( face->type1.private_dict.lenIV >= 0 )
1351 /* t1_decrypt() shouldn't write to base -- make temporary copy */
1352 if ( FT_ALLOC( temp, size ) )
1354 FT_MEM_COPY( temp, base, size );
1355 psaux->t1_decrypt( temp, size, 4330 );
1356 size -= face->type1.private_dict.lenIV;
1357 error = T1_Add_Table( code_table, n,
1358 temp + face->type1.private_dict.lenIV, size );
1362 error = T1_Add_Table( code_table, n, base, size );
1367 if ( n >= loader->num_glyphs )
1372 loader->num_glyphs = n;
1374 /* if /.notdef is found but does not occupy index 0, do our magic. */
1375 if ( ft_strcmp( (const char*)".notdef",
1376 (const char*)name_table->elements[0] ) &&
1379 /* Swap glyph in index 0 with /.notdef glyph. First, add index 0 */
1380 /* name and code entries to swap_table. Then place notdef_index name */
1381 /* and code entries into swap_table. Then swap name and code */
1382 /* entries at indices notdef_index and 0 using values stored in */
1386 error = T1_Add_Table( swap_table, 0,
1387 name_table->elements[0],
1388 name_table->lengths [0] );
1393 error = T1_Add_Table( swap_table, 1,
1394 code_table->elements[0],
1395 code_table->lengths [0] );
1399 /* Index notdef_index name */
1400 error = T1_Add_Table( swap_table, 2,
1401 name_table->elements[notdef_index],
1402 name_table->lengths [notdef_index] );
1406 /* Index notdef_index code */
1407 error = T1_Add_Table( swap_table, 3,
1408 code_table->elements[notdef_index],
1409 code_table->lengths [notdef_index] );
1413 error = T1_Add_Table( name_table, notdef_index,
1414 swap_table->elements[0],
1415 swap_table->lengths [0] );
1419 error = T1_Add_Table( code_table, notdef_index,
1420 swap_table->elements[1],
1421 swap_table->lengths [1] );
1425 error = T1_Add_Table( name_table, 0,
1426 swap_table->elements[2],
1427 swap_table->lengths [2] );
1431 error = T1_Add_Table( code_table, 0,
1432 swap_table->elements[3],
1433 swap_table->lengths [3] );
1438 else if ( !notdef_found )
1440 /* notdef_index is already 0, or /.notdef is undefined in */
1441 /* charstrings dictionary. Worry about /.notdef undefined. */
1442 /* We take index 0 and add it to the end of the table(s) */
1443 /* and add our own /.notdef glyph to index 0. */
1445 /* 0 333 hsbw endchar */
1446 FT_Byte notdef_glyph[] = {0x8B, 0xF7, 0xE1, 0x0D, 0x0E};
1447 char* notdef_name = (char *)".notdef";
1450 error = T1_Add_Table( swap_table, 0,
1451 name_table->elements[0],
1452 name_table->lengths [0] );
1456 error = T1_Add_Table( swap_table, 1,
1457 code_table->elements[0],
1458 code_table->lengths [0] );
1462 error = T1_Add_Table( name_table, 0, notdef_name, 8 );
1466 error = T1_Add_Table( code_table, 0, notdef_glyph, 5 );
1471 error = T1_Add_Table( name_table, n,
1472 swap_table->elements[0],
1473 swap_table->lengths [0] );
1477 error = T1_Add_Table( code_table, n,
1478 swap_table->elements[1],
1479 swap_table->lengths [1] );
1483 /* we added a glyph. */
1484 loader->num_glyphs = n + 1;
1490 parser->root.error = error;
1495 const T1_FieldRec t1_keywords[] =
1498 #include "t1tokens.h"
1500 /* now add the special functions... */
1501 T1_FIELD_CALLBACK( "FontName", parse_font_name )
1503 T1_FIELD_CALLBACK( "FontBBox", parse_font_bbox )
1505 T1_FIELD_CALLBACK( "FontMatrix", parse_font_matrix )
1506 T1_FIELD_CALLBACK( "Encoding", parse_encoding )
1507 T1_FIELD_CALLBACK( "Subrs", parse_subrs )
1508 T1_FIELD_CALLBACK( "CharStrings", parse_charstrings )
1510 #ifndef T1_CONFIG_OPTION_NO_MM_SUPPORT
1511 T1_FIELD_CALLBACK( "BlendDesignPositions", parse_blend_design_positions )
1512 T1_FIELD_CALLBACK( "BlendDesignMap", parse_blend_design_map )
1513 T1_FIELD_CALLBACK( "BlendAxisTypes", parse_blend_axis_types )
1514 T1_FIELD_CALLBACK( "WeightVector", parse_weight_vector )
1515 T1_FIELD_CALLBACK( "shareddict", parse_shared_dict )
1518 { 0, T1_FIELD_LOCATION_CID_INFO, T1_FIELD_TYPE_NONE, 0, 0, 0, 0, 0 }
1523 parse_dict( T1_Face face,
1528 T1_Parser parser = &loader->parser;
1531 parser->root.cursor = base;
1532 parser->root.limit = base + size;
1533 parser->root.error = 0;
1536 FT_Byte* cur = base;
1537 FT_Byte* limit = cur + size;
1540 for ( ; cur < limit; cur++ )
1542 /* look for `FontDirectory', which causes problems on some fonts */
1543 if ( *cur == 'F' && cur + 25 < limit &&
1544 ft_strncmp( (char*)cur, "FontDirectory", 13 ) == 0 )
1549 /* skip the `FontDirectory' keyword */
1553 /* lookup the `known' keyword */
1554 while ( cur < limit && *cur != 'k' &&
1555 ft_strncmp( (char*)cur, "known", 5 ) )
1563 /* skip the `known' keyword and the token following it */
1565 loader->parser.root.cursor = cur;
1566 T1_ToToken( &loader->parser, &token );
1568 /* if the last token was an array, skip it! */
1569 if ( token.type == T1_TOKEN_TYPE_ARRAY )
1570 cur2 = parser->root.cursor;
1574 /* look for immediates */
1575 else if ( *cur == '/' && cur + 2 < limit )
1583 while ( cur2 < limit && is_name_char( *cur2 ) )
1587 if ( len > 0 && len < 22 )
1590 /* now, compare the immediate name to the keyword table */
1591 T1_Field keyword = (T1_Field)t1_keywords;
1599 name = (FT_Byte*)keyword->ident;
1603 if ( cur[0] == name[0] &&
1604 len == ft_strlen( (const char*)name ) )
1609 for ( n = 1; n < len; n++ )
1610 if ( cur[n] != name[n] )
1615 /* we found it -- run the parsing callback! */
1616 parser->root.cursor = cur2;
1617 T1_Skip_Spaces( parser );
1618 parser->root.error = t1_load_keyword( face,
1621 if ( parser->root.error )
1622 return parser->root.error;
1624 cur = parser->root.cursor;
1635 return parser->root.error;
1640 t1_init_loader( T1_Loader loader,
1645 FT_MEM_ZERO( loader, sizeof ( *loader ) );
1646 loader->num_glyphs = 0;
1647 loader->num_chars = 0;
1649 /* initialize the tables -- simply set their `init' field to 0 */
1650 loader->encoding_table.init = 0;
1651 loader->charstrings.init = 0;
1652 loader->glyph_names.init = 0;
1653 loader->subrs.init = 0;
1654 loader->swap_table.init = 0;
1655 loader->fontdata = 0;
1660 t1_done_loader( T1_Loader loader )
1662 T1_Parser parser = &loader->parser;
1665 /* finalize tables */
1666 T1_Release_Table( &loader->encoding_table );
1667 T1_Release_Table( &loader->charstrings );
1668 T1_Release_Table( &loader->glyph_names );
1669 T1_Release_Table( &loader->swap_table );
1670 T1_Release_Table( &loader->subrs );
1672 /* finalize parser */
1673 T1_Finalize_Parser( parser );
1677 FT_LOCAL_DEF( FT_Error )
1678 T1_Open_Face( T1_Face face )
1680 T1_LoaderRec loader;
1682 T1_Font type1 = &face->type1;
1685 PSAux_Service psaux = (PSAux_Service)face->psaux;
1688 t1_init_loader( &loader, face );
1691 type1->private_dict.lenIV = 4;
1693 /* default blue fuzz, we put it there since 0 is a valid value */
1694 type1->private_dict.blue_fuzz = 1;
1696 parser = &loader.parser;
1697 error = T1_New_Parser( parser,
1704 error = parse_dict( face, &loader, parser->base_dict, parser->base_len );
1708 error = T1_Get_Private_Dict( parser, psaux );
1712 error = parse_dict( face, &loader, parser->private_dict,
1713 parser->private_len );
1717 /* now, propagate the subrs, charstrings, and glyphnames tables */
1718 /* to the Type1 data */
1719 type1->num_glyphs = loader.num_glyphs;
1721 if ( loader.subrs.init )
1723 loader.subrs.init = 0;
1724 type1->num_subrs = loader.num_subrs;
1725 type1->subrs_block = loader.subrs.block;
1726 type1->subrs = loader.subrs.elements;
1727 type1->subrs_len = loader.subrs.lengths;
1730 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1731 if ( !face->root.internal->incremental_interface )
1733 if ( !loader.charstrings.init )
1735 FT_ERROR(( "T1_Open_Face: no charstrings array in face!\n" ));
1736 error = T1_Err_Invalid_File_Format;
1739 loader.charstrings.init = 0;
1740 type1->charstrings_block = loader.charstrings.block;
1741 type1->charstrings = loader.charstrings.elements;
1742 type1->charstrings_len = loader.charstrings.lengths;
1744 /* we copy the glyph names `block' and `elements' fields; */
1745 /* the `lengths' field must be released later */
1746 type1->glyph_names_block = loader.glyph_names.block;
1747 type1->glyph_names = (FT_String**)loader.glyph_names.elements;
1748 loader.glyph_names.block = 0;
1749 loader.glyph_names.elements = 0;
1751 /* we must now build type1.encoding when we have a custom array */
1752 if ( type1->encoding_type == T1_ENCODING_TYPE_ARRAY )
1754 FT_Int charcode, idx, min_char, max_char;
1756 FT_Byte* glyph_name;
1759 /* OK, we do the following: for each element in the encoding */
1760 /* table, look up the index of the glyph having the same name */
1761 /* the index is then stored in type1.encoding.char_index, and */
1762 /* a the name to type1.encoding.char_name */
1768 for ( ; charcode < loader.encoding_table.max_elems; charcode++ )
1770 type1->encoding.char_index[charcode] = 0;
1771 type1->encoding.char_name [charcode] = (char *)".notdef";
1773 char_name = loader.encoding_table.elements[charcode];
1775 for ( idx = 0; idx < type1->num_glyphs; idx++ )
1777 glyph_name = (FT_Byte*)type1->glyph_names[idx];
1778 if ( ft_strcmp( (const char*)char_name,
1779 (const char*)glyph_name ) == 0 )
1781 type1->encoding.char_index[charcode] = (FT_UShort)idx;
1782 type1->encoding.char_name [charcode] = (char*)glyph_name;
1784 /* Change min/max encoded char only if glyph name is */
1786 if ( ft_strcmp( (const char*)".notdef",
1787 (const char*)glyph_name ) != 0 )
1789 if ( charcode < min_char ) min_char = charcode;
1790 if ( charcode > max_char ) max_char = charcode;
1796 type1->encoding.code_first = min_char;
1797 type1->encoding.code_last = max_char;
1798 type1->encoding.num_chars = loader.num_chars;
1802 t1_done_loader( &loader );