1 /***************************************************************************/
5 /* Type 42 font parser (body). */
7 /* Copyright 2002 by Roberto Alameda. */
9 /* This file is part of the FreeType project, and may only be used, */
10 /* modified, and distributed under the terms of the FreeType project */
11 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
12 /* this file you indicate that you have read the license and */
13 /* understand and accept it fully. */
15 /***************************************************************************/
20 #include FT_INTERNAL_DEBUG_H
21 #include FT_INTERNAL_STREAM_H
23 #include FT_INTERNAL_POSTSCRIPT_AUX_H
26 /*************************************************************************/
28 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
29 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
30 /* messages during execution. */
33 #define FT_COMPONENT trace_t42
37 t42_parse_font_name( T42_Face face,
41 t42_parse_font_bbox( T42_Face face,
45 t42_parse_font_matrix( T42_Face face,
48 t42_parse_encoding( T42_Face face,
52 t42_parse_charstrings( T42_Face face,
56 t42_parse_sfnts( T42_Face face,
61 T1_FieldRec t42_keywords[] = {
64 #define FT_STRUCTURE T1_FontInfo
66 #define T1CODE T1_FIELD_LOCATION_FONT_INFO
68 T1_FIELD_STRING ( "version", version )
69 T1_FIELD_STRING ( "Notice", notice )
70 T1_FIELD_STRING ( "FullName", full_name )
71 T1_FIELD_STRING ( "FamilyName", family_name )
72 T1_FIELD_STRING ( "Weight", weight )
73 T1_FIELD_NUM ( "ItalicAngle", italic_angle )
74 T1_FIELD_TYPE_BOOL( "isFixedPitch", is_fixed_pitch )
75 T1_FIELD_NUM ( "UnderlinePosition", underline_position )
76 T1_FIELD_NUM ( "UnderlineThickness", underline_thickness )
79 #define FT_STRUCTURE T1_FontRec
81 #define T1CODE T1_FIELD_LOCATION_FONT_DICT
83 T1_FIELD_NUM( "PaintType", paint_type )
84 T1_FIELD_NUM( "FontType", font_type )
85 T1_FIELD_NUM( "StrokeWidth", stroke_width )
87 T1_FIELD_CALLBACK( "FontName", t42_parse_font_name )
88 T1_FIELD_CALLBACK( "FontBBox", t42_parse_font_bbox )
89 T1_FIELD_CALLBACK( "FontMatrix", t42_parse_font_matrix )
90 T1_FIELD_CALLBACK( "Encoding", t42_parse_encoding )
91 T1_FIELD_CALLBACK( "CharStrings", t42_parse_charstrings )
92 T1_FIELD_CALLBACK( "sfnts", t42_parse_sfnts )
94 { 0, T1_FIELD_LOCATION_CID_INFO, T1_FIELD_TYPE_NONE, 0, 0, 0, 0, 0 }
98 #define T1_Add_Table( p, i, o, l ) (p)->funcs.add( (p), i, o, l )
99 #define T1_Done_Table( p ) \
102 if ( (p)->funcs.done ) \
103 (p)->funcs.done( p ); \
105 #define T1_Release_Table( p ) \
108 if ( (p)->funcs.release ) \
109 (p)->funcs.release( p ); \
112 #define T1_Skip_Spaces( p ) (p)->root.funcs.skip_spaces( &(p)->root )
113 #define T1_Skip_Alpha( p ) (p)->root.funcs.skip_alpha ( &(p)->root )
115 #define T1_ToInt( p ) (p)->root.funcs.to_int( &(p)->root )
116 #define T1_ToFixed( p, t ) (p)->root.funcs.to_fixed( &(p)->root, t )
118 #define T1_ToCoordArray( p, m, c ) \
119 (p)->root.funcs.to_coord_array( &(p)->root, m, c )
120 #define T1_ToFixedArray( p, m, f, t ) \
121 (p)->root.funcs.to_fixed_array( &(p)->root, m, f, t )
122 #define T1_ToToken( p, t ) \
123 (p)->root.funcs.to_token( &(p)->root, t )
124 #define T1_ToTokenArray( p, t, m, c ) \
125 (p)->root.funcs.to_token_array( &(p)->root, t, m, c )
127 #define T1_Load_Field( p, f, o, m, pf ) \
128 (p)->root.funcs.load_field( &(p)->root, f, o, m, pf )
129 #define T1_Load_Field_Table( p, f, o, m, pf ) \
130 (p)->root.funcs.load_field_table( &(p)->root, f, o, m, pf )
133 /********************* Parsing Functions ******************/
135 FT_LOCAL_DEF( FT_Error )
136 t42_parser_init( T42_Parser parser,
139 PSAux_Service psaux )
141 FT_Error error = T42_Err_Ok;
145 psaux->ps_parser_funcs->init( &parser->root, 0, 0, memory );
147 parser->stream = stream;
148 parser->base_len = 0;
149 parser->base_dict = 0;
150 parser->in_memory = 0;
152 /*******************************************************************/
154 /* Here a short summary of what is going on: */
156 /* When creating a new Type 42 parser, we try to locate and load */
157 /* the base dictionary, loading the whole font into memory. */
159 /* When `loading' the base dictionary, we only setup pointers in */
160 /* the case of a memory-based stream. Otherwise, we allocate */
161 /* and load the base dictionary in it. */
163 /* parser->in_memory is set if we have a memory stream. */
166 if ( FT_STREAM_SEEK( 0L ) )
171 /* now, try to load `size' bytes of the `base' dictionary we */
172 /* found previously */
174 /* if it is a memory-based resource, set up pointers */
177 parser->base_dict = (FT_Byte*)stream->base + stream->pos;
178 parser->base_len = size;
179 parser->in_memory = 1;
181 /* check that the `size' field is valid */
182 if ( FT_STREAM_SKIP( size ) )
187 /* read segment in memory */
188 if ( FT_ALLOC( parser->base_dict, size ) ||
189 FT_STREAM_READ( parser->base_dict, size ) )
192 parser->base_len = size;
195 /* Now check font format; we must see `%!PS-TrueTypeFont' */
197 ( ft_strncmp( (const char*)parser->base_dict,
198 "%!PS-TrueTypeFont", 17) ) )
199 error = T42_Err_Unknown_File_Format;
202 parser->root.base = parser->base_dict;
203 parser->root.cursor = parser->base_dict;
204 parser->root.limit = parser->root.cursor + parser->base_len;
208 if ( error && !parser->in_memory )
209 FT_FREE( parser->base_dict );
216 t42_parser_done( T42_Parser parser )
218 FT_Memory memory = parser->root.memory;
221 /* free the base dictionary only when we have a disk stream */
222 if ( !parser->in_memory )
223 FT_FREE( parser->base_dict );
225 parser->root.funcs.done( &parser->root );
230 t42_is_alpha( FT_Byte c )
232 /* Note: we must accept "+" as a valid character, as it is used in */
233 /* embedded type1 fonts in PDF documents. */
235 return ( ft_isalnum( c ) ||
244 t42_is_space( FT_Byte c )
246 return ( c == ' ' || c == '\t' || c == '\r' || c == '\n' );
251 t42_parse_font_name( T42_Face face,
254 T42_Parser parser = &loader->parser;
256 FT_Memory memory = parser->root.memory;
263 T1_Skip_Spaces( parser );
265 cur = parser->root.cursor;
266 limit = parser->root.limit;
268 if ( cur >= limit - 1 ||
269 ( *cur != '/' && *cur != '(') )
274 while ( cur2 < limit && t42_is_alpha( *cur2 ) )
277 len = (FT_Int)( cur2 - cur );
280 if ( FT_ALLOC( face->type1.font_name, len + 1 ) )
282 parser->root.error = error;
286 FT_MEM_COPY( face->type1.font_name, cur, len );
287 face->type1.font_name[len] = '\0';
289 parser->root.cursor = cur2;
294 t42_parse_font_bbox( T42_Face face,
297 T42_Parser parser = &loader->parser;
298 FT_BBox* bbox = &face->type1.font_bbox;
300 bbox->xMin = T1_ToInt( parser );
301 bbox->yMin = T1_ToInt( parser );
302 bbox->xMax = T1_ToInt( parser );
303 bbox->yMax = T1_ToInt( parser );
308 t42_parse_font_matrix( T42_Face face,
311 T42_Parser parser = &loader->parser;
312 FT_Matrix* matrix = &face->type1.font_matrix;
313 FT_Vector* offset = &face->type1.font_offset;
314 FT_Face root = (FT_Face)&face->root;
319 (void)T1_ToFixedArray( parser, 6, temp, 3 );
321 temp_scale = ABS( temp[3] );
323 /* Set Units per EM based on FontMatrix values. We set the value to */
324 /* 1000 / temp_scale, because temp_scale was already multiplied by */
325 /* 1000 (in t1_tofixed, from psobjs.c). */
327 root->units_per_EM = (FT_UShort)( FT_DivFix( 1000 * 0x10000L,
328 temp_scale ) >> 16 );
330 /* we need to scale the values by 1.0/temp_scale */
331 if ( temp_scale != 0x10000L ) {
332 temp[0] = FT_DivFix( temp[0], temp_scale );
333 temp[1] = FT_DivFix( temp[1], temp_scale );
334 temp[2] = FT_DivFix( temp[2], temp_scale );
335 temp[4] = FT_DivFix( temp[4], temp_scale );
336 temp[5] = FT_DivFix( temp[5], temp_scale );
340 matrix->xx = temp[0];
341 matrix->yx = temp[1];
342 matrix->xy = temp[2];
343 matrix->yy = temp[3];
345 /* note that the offsets must be expressed in integer font units */
346 offset->x = temp[4] >> 16;
347 offset->y = temp[5] >> 16;
352 t42_parse_encoding( T42_Face face,
355 T42_Parser parser = &loader->parser;
356 FT_Byte* cur = parser->root.cursor;
357 FT_Byte* limit = parser->root.limit;
359 PSAux_Service psaux = (PSAux_Service)face->psaux;
362 /* skip whitespace */
363 while ( t42_is_space( *cur ) )
368 FT_ERROR(( "t42_parse_encoding: out of bounds!\n" ));
369 parser->root.error = T42_Err_Invalid_File_Format;
374 /* if we have a number, then the encoding is an array, */
375 /* and we must load it now */
376 if ( (FT_Byte)( *cur - '0' ) < 10 )
378 T1_Encoding encode = &face->type1.encoding;
380 PS_Table char_table = &loader->encoding_table;
381 FT_Memory memory = parser->root.memory;
385 /* read the number of entries in the encoding, should be 256 */
386 count = T1_ToInt( parser );
387 if ( parser->root.error )
390 /* we use a T1_Table to store our charnames */
391 loader->num_chars = encode->num_chars = count;
392 if ( FT_NEW_ARRAY( encode->char_index, count ) ||
393 FT_NEW_ARRAY( encode->char_name, count ) ||
394 FT_SET_ERROR( psaux->ps_table_funcs->init(
395 char_table, count, memory ) ) )
397 parser->root.error = error;
401 /* We need to `zero' out encoding_table.elements */
402 for ( n = 0; n < count; n++ )
404 char* notdef = (char *)".notdef";
407 T1_Add_Table( char_table, n, notdef, 8 );
410 /* Now, we will need to read a record of the form */
411 /* ... charcode /charname ... for each entry in our table */
413 /* We simply look for a number followed by an immediate */
414 /* name. Note that this ignores correctly the sequence */
415 /* that is often seen in type1 fonts: */
417 /* 0 1 255 { 1 index exch /.notdef put } for dup */
419 /* used to clean the encoding array before anything else. */
421 /* We stop when we encounter a `def'. */
423 cur = parser->root.cursor;
424 limit = parser->root.limit;
427 for ( ; cur < limit; )
434 /* we stop when we encounter a `def' */
435 if ( c == 'd' && cur + 3 < limit )
437 if ( cur[1] == 'e' &&
439 t42_is_space( cur[-1] ) &&
440 t42_is_space( cur[3] ) )
442 FT_TRACE6(( "encoding end\n" ));
447 /* otherwise, we must find a number before anything else */
448 if ( (FT_Byte)( c - '0' ) < 10 )
453 parser->root.cursor = cur;
454 charcode = T1_ToInt( parser );
455 cur = parser->root.cursor;
457 /* skip whitespace */
458 while ( cur < limit && t42_is_space( *cur ) )
461 if ( cur < limit && *cur == '/' )
463 /* bingo, we have an immediate name -- it must be a */
465 FT_Byte* cur2 = cur + 1;
469 while ( cur2 < limit && t42_is_alpha( *cur2 ) )
472 len = (FT_Int)( cur2 - cur - 1 );
474 parser->root.error = T1_Add_Table( char_table, charcode,
476 char_table->elements[charcode][len] = '\0';
477 if ( parser->root.error )
487 face->type1.encoding_type = T1_ENCODING_TYPE_ARRAY;
488 parser->root.cursor = cur;
490 /* Otherwise, we should have either `StandardEncoding', */
491 /* `ExpertEncoding', or `ISOLatin1Encoding' */
494 if ( cur + 17 < limit &&
495 ft_strncmp( (const char*)cur, "StandardEncoding", 16 ) == 0 )
496 face->type1.encoding_type = T1_ENCODING_TYPE_STANDARD;
498 else if ( cur + 15 < limit &&
499 ft_strncmp( (const char*)cur, "ExpertEncoding", 14 ) == 0 )
500 face->type1.encoding_type = T1_ENCODING_TYPE_EXPERT;
502 else if ( cur + 18 < limit &&
503 ft_strncmp( (const char*)cur, "ISOLatin1Encoding", 17 ) == 0 )
504 face->type1.encoding_type = T1_ENCODING_TYPE_ISOLATIN1;
507 FT_ERROR(( "t42_parse_encoding: invalid token!\n" ));
508 parser->root.error = T42_Err_Invalid_File_Format;
515 t42_hexval( FT_Byte v )
519 d = (FT_UInt)( v - 'A' );
526 d = (FT_UInt)( v - 'a' );
533 d = (FT_UInt)( v - '0' );
545 t42_parse_sfnts( T42_Face face,
548 T42_Parser parser = &loader->parser;
549 FT_Memory memory = parser->root.memory;
550 FT_Byte* cur = parser->root.cursor;
551 FT_Byte* limit = parser->root.limit;
553 FT_Int num_tables = 0, status;
554 FT_ULong count, ttf_size = 0, string_size = 0;
555 FT_Bool in_string = 0;
559 /* The format is `/sfnts [ <...> <...> ... ] def' */
561 while ( t42_is_space( *cur ) )
571 FT_ERROR(( "t42_parse_sfnts: can't find begin of sfnts vector!\n" ));
572 error = T42_Err_Invalid_File_Format;
576 while ( cur < limit - 2 )
578 while ( t42_is_space( *cur ) )
584 parser->root.cursor = cur++;
596 FT_ERROR(( "t42_parse_sfnts: found unpaired `>'!\n" ));
597 error = T42_Err_Invalid_File_Format;
601 /* A string can have, as a last byte, */
602 /* a zero byte for padding. If so, ignore it */
603 if ( ( v == 0 ) && ( string_size % 2 == 1 ) )
612 /* Comment found; skip till end of line */
613 while ( *cur != '\n' )
619 FT_ERROR(( "t42_parse_sfnts: found `%' in string!\n" ));
620 error = T42_Err_Invalid_File_Format;
625 if ( !ft_xdigit( *cur ) || !ft_xdigit( *(cur + 1) ) )
627 FT_ERROR(( "t42_parse_sfnts: found non-hex characters in string" ));
628 error = T42_Err_Invalid_File_Format;
632 v = (FT_Byte)( 16 * t42_hexval( cur[0] ) + t42_hexval( cur[1] ) );
639 case 0: /* The '[' was read, so load offset table, 12 bytes */
642 face->ttf_data[count++] = v;
647 num_tables = 16 * face->ttf_data[4] + face->ttf_data[5];
649 ttf_size = 12 + 16 * num_tables;
651 if ( FT_REALLOC( face->ttf_data, 12, ttf_size ) )
654 /* No break, fall-through */
656 case 1: /* The offset table is read; read now the table directory */
657 if ( count < ttf_size )
659 face->ttf_data[count++] = v;
668 for ( i = 0; i < num_tables; i++ )
670 FT_Byte* p = face->ttf_data + 12 + 16*i + 12;
672 len = FT_PEEK_ULONG( p );
674 /* Pad to a 4-byte boundary length */
675 ttf_size += ( len + 3 ) & ~3;
679 face->ttf_size = ttf_size;
681 if ( FT_REALLOC( face->ttf_data, 12 + 16 * num_tables,
685 /* No break, fall-through */
687 case 2: /* We are reading normal tables; just swallow them */
688 face->ttf_data[count++] = v;
693 /* If control reaches this point, the format was not valid */
694 error = T42_Err_Invalid_File_Format;
697 parser->root.error = error;
702 t42_parse_charstrings( T42_Face face,
705 T42_Parser parser = &loader->parser;
706 PS_Table code_table = &loader->charstrings;
707 PS_Table name_table = &loader->glyph_names;
708 FT_Memory memory = parser->root.memory;
711 PSAux_Service psaux = (PSAux_Service)face->psaux;
714 FT_Byte* limit = parser->root.limit;
718 loader->num_glyphs = T1_ToInt( parser );
719 if ( parser->root.error )
722 /* initialize tables */
724 error = psaux->ps_table_funcs->init( code_table,
730 error = psaux->ps_table_funcs->init( name_table,
740 /* the format is simple: */
741 /* `/glyphname' + index + def */
743 /* note that we stop when we find an `end' */
745 T1_Skip_Spaces( parser );
747 cur = parser->root.cursor;
751 /* we stop when we find an `end' keyword */
759 T1_Skip_Alpha( parser );
762 FT_Byte* cur2 = cur + 1;
766 while ( cur2 < limit && t42_is_alpha( *cur2 ) )
768 len = (FT_Int)( cur2 - cur - 1 );
770 error = T1_Add_Table( name_table, n, cur + 1, len + 1 );
774 /* add a trailing zero to the name table */
775 name_table->elements[n][len] = '\0';
777 parser->root.cursor = cur2;
778 T1_Skip_Spaces( parser );
780 cur2 = cur = parser->root.cursor;
784 while ( cur2 < limit && t42_is_alpha( *cur2 ) )
786 len = (FT_Int)( cur2 - cur );
788 error = T1_Add_Table( code_table, n, cur, len + 1 );
792 code_table->elements[n][len] = '\0';
795 if ( n >= loader->num_glyphs )
800 /* Index 0 must be a .notdef element */
801 if ( ft_strcmp( (char *)name_table->elements[0], ".notdef" ) )
803 FT_ERROR(( "t42_parse_charstrings: Index 0 is not `.notdef'!\n" ));
804 error = T42_Err_Invalid_File_Format;
808 loader->num_glyphs = n;
812 parser->root.error = error;
817 t42_load_keyword( T42_Face face,
824 FT_UInt max_objects = 0;
827 /* if the keyword has a dedicated callback, call it */
828 if ( field->type == T1_FIELD_TYPE_CALLBACK ) {
829 field->reader( (FT_Face)face, loader );
830 error = loader->parser.root.error;
834 /* now, the keyword is either a simple field, or a table of fields; */
835 /* we are now going to take care of it */
836 switch ( field->location )
838 case T1_FIELD_LOCATION_FONT_INFO:
839 dummy_object = &face->type1.font_info;
840 objects = &dummy_object;
844 dummy_object = &face->type1;
845 objects = &dummy_object;
848 if ( field->type == T1_FIELD_TYPE_INTEGER_ARRAY ||
849 field->type == T1_FIELD_TYPE_FIXED_ARRAY )
850 error = T1_Load_Field_Table( &loader->parser, field,
851 objects, max_objects, 0 );
853 error = T1_Load_Field( &loader->parser, field,
854 objects, max_objects, 0 );
861 FT_LOCAL_DEF( FT_Error )
862 t42_parse_dict( T42_Face face,
867 T42_Parser parser = &loader->parser;
869 FT_Byte* limit = cur + size;
870 FT_UInt n_keywords = sizeof ( t42_keywords ) /
871 sizeof ( t42_keywords[0] );
874 parser->root.cursor = base;
875 parser->root.limit = base + size;
876 parser->root.error = 0;
878 for ( ; cur < limit; cur++ )
880 /* look for `FontDirectory', which causes problems on some fonts */
881 if ( *cur == 'F' && cur + 25 < limit &&
882 ft_strncmp( (char*)cur, "FontDirectory", 13 ) == 0 )
887 /* skip the `FontDirectory' keyword */
891 /* lookup the `known' keyword */
892 while ( cur < limit && *cur != 'k' &&
893 ft_strncmp( (char*)cur, "known", 5 ) )
901 /* skip the `known' keyword and the token following it */
903 loader->parser.root.cursor = cur;
904 T1_ToToken( &loader->parser, &token );
906 /* if the last token was an array, skip it! */
907 if ( token.type == T1_TOKEN_TYPE_ARRAY )
908 cur2 = parser->root.cursor;
912 /* look for immediates */
913 else if ( *cur == '/' && cur + 2 < limit )
921 while ( cur2 < limit && t42_is_alpha( *cur2 ) )
924 len = (FT_UInt)( cur2 - cur );
925 if ( len > 0 && len < 22 ) /* XXX What shall it this 22? */
927 /* now, compare the immediate name to the keyword table */
929 /* Loop through all known keywords */
930 for ( i = 0; i < n_keywords; i++ )
932 T1_Field keyword = (T1_Field)&t42_keywords[i];
933 FT_Byte *name = (FT_Byte*)keyword->ident;
939 if ( ( len == ft_strlen( (const char *)name ) ) &&
940 ( ft_memcmp( cur, name, len ) == 0 ) )
942 /* we found it -- run the parsing callback! */
943 parser->root.cursor = cur2;
944 T1_Skip_Spaces( parser );
945 parser->root.error = t42_load_keyword(face,
948 if ( parser->root.error )
949 return parser->root.error;
950 cur = parser->root.cursor;
957 return parser->root.error;
962 t42_loader_init( T42_Loader loader,
967 FT_MEM_ZERO( loader, sizeof ( *loader ) );
968 loader->num_glyphs = 0;
969 loader->num_chars = 0;
971 /* initialize the tables -- simply set their `init' field to 0 */
972 loader->encoding_table.init = 0;
973 loader->charstrings.init = 0;
974 loader->glyph_names.init = 0;
979 t42_loader_done( T42_Loader loader )
981 T42_Parser parser = &loader->parser;
984 /* finalize tables */
985 T1_Release_Table( &loader->encoding_table );
986 T1_Release_Table( &loader->charstrings );
987 T1_Release_Table( &loader->glyph_names );
989 /* finalize parser */
990 t42_parser_done( parser );