1 /***************************************************************************/
5 /* Type 42 font parser (body). */
7 /* Copyright 2002-2014 by */
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 /***************************************************************************/
21 #include FT_INTERNAL_DEBUG_H
22 #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_matrix( T42_Face face
,
40 t42_parse_encoding( T42_Face face
,
44 t42_parse_charstrings( T42_Face face
,
48 t42_parse_sfnts( T42_Face face
,
52 /* as Type42 fonts have no Private dict, */
53 /* we set the last argument of T1_FIELD_XXX to 0 */
55 T1_FieldRec t42_keywords
[] =
59 #define FT_STRUCTURE T1_FontInfo
61 #define T1CODE T1_FIELD_LOCATION_FONT_INFO
63 T1_FIELD_STRING( "version", version
, 0 )
64 T1_FIELD_STRING( "Notice", notice
, 0 )
65 T1_FIELD_STRING( "FullName", full_name
, 0 )
66 T1_FIELD_STRING( "FamilyName", family_name
, 0 )
67 T1_FIELD_STRING( "Weight", weight
, 0 )
68 T1_FIELD_NUM ( "ItalicAngle", italic_angle
, 0 )
69 T1_FIELD_BOOL ( "isFixedPitch", is_fixed_pitch
, 0 )
70 T1_FIELD_NUM ( "UnderlinePosition", underline_position
, 0 )
71 T1_FIELD_NUM ( "UnderlineThickness", underline_thickness
, 0 )
74 #define FT_STRUCTURE PS_FontExtraRec
76 #define T1CODE T1_FIELD_LOCATION_FONT_EXTRA
78 T1_FIELD_NUM ( "FSType", fs_type
, 0 )
81 #define FT_STRUCTURE T1_FontRec
83 #define T1CODE T1_FIELD_LOCATION_FONT_DICT
85 T1_FIELD_KEY ( "FontName", font_name
, 0 )
86 T1_FIELD_NUM ( "PaintType", paint_type
, 0 )
87 T1_FIELD_NUM ( "FontType", font_type
, 0 )
88 T1_FIELD_FIXED( "StrokeWidth", stroke_width
, 0 )
91 #define FT_STRUCTURE FT_BBox
93 #define T1CODE T1_FIELD_LOCATION_BBOX
95 T1_FIELD_BBOX("FontBBox", xMin
, 0 )
97 T1_FIELD_CALLBACK( "FontMatrix", t42_parse_font_matrix
, 0 )
98 T1_FIELD_CALLBACK( "Encoding", t42_parse_encoding
, 0 )
99 T1_FIELD_CALLBACK( "CharStrings", t42_parse_charstrings
, 0 )
100 T1_FIELD_CALLBACK( "sfnts", t42_parse_sfnts
, 0 )
102 { 0, T1_FIELD_LOCATION_CID_INFO
, T1_FIELD_TYPE_NONE
, 0, 0, 0, 0, 0, 0 }
106 #define T1_Add_Table( p, i, o, l ) (p)->funcs.add( (p), i, o, l )
107 #define T1_Done_Table( p ) \
110 if ( (p)->funcs.done ) \
111 (p)->funcs.done( p ); \
113 #define T1_Release_Table( p ) \
116 if ( (p)->funcs.release ) \
117 (p)->funcs.release( p ); \
120 #define T1_Skip_Spaces( p ) (p)->root.funcs.skip_spaces( &(p)->root )
121 #define T1_Skip_PS_Token( p ) (p)->root.funcs.skip_PS_token( &(p)->root )
123 #define T1_ToInt( p ) \
124 (p)->root.funcs.to_int( &(p)->root )
125 #define T1_ToBytes( p, b, m, n, d ) \
126 (p)->root.funcs.to_bytes( &(p)->root, b, m, n, d )
128 #define T1_ToFixedArray( p, m, f, t ) \
129 (p)->root.funcs.to_fixed_array( &(p)->root, m, f, t )
130 #define T1_ToToken( p, t ) \
131 (p)->root.funcs.to_token( &(p)->root, t )
133 #define T1_Load_Field( p, f, o, m, pf ) \
134 (p)->root.funcs.load_field( &(p)->root, f, o, m, pf )
135 #define T1_Load_Field_Table( p, f, o, m, pf ) \
136 (p)->root.funcs.load_field_table( &(p)->root, f, o, m, pf )
139 /********************* Parsing Functions ******************/
141 FT_LOCAL_DEF( FT_Error
)
142 t42_parser_init( T42_Parser parser
,
145 PSAux_Service psaux
)
147 FT_Error error
= FT_Err_Ok
;
151 psaux
->ps_parser_funcs
->init( &parser
->root
, 0, 0, memory
);
153 parser
->stream
= stream
;
154 parser
->base_len
= 0;
155 parser
->base_dict
= 0;
156 parser
->in_memory
= 0;
158 /*******************************************************************/
160 /* Here a short summary of what is going on: */
162 /* When creating a new Type 42 parser, we try to locate and load */
163 /* the base dictionary, loading the whole font into memory. */
165 /* When `loading' the base dictionary, we only set up pointers */
166 /* in the case of a memory-based stream. Otherwise, we allocate */
167 /* and load the base dictionary in it. */
169 /* parser->in_memory is set if we have a memory stream. */
172 if ( FT_STREAM_SEEK( 0L ) ||
173 FT_FRAME_ENTER( 17 ) )
176 if ( ft_memcmp( stream
->cursor
, "%!PS-TrueTypeFont", 17 ) != 0 )
178 FT_TRACE2(( " not a Type42 font\n" ));
179 error
= FT_THROW( Unknown_File_Format
);
184 if ( error
|| FT_STREAM_SEEK( 0 ) )
189 /* now, try to load `size' bytes of the `base' dictionary we */
190 /* found previously */
192 /* if it is a memory-based resource, set up pointers */
195 parser
->base_dict
= (FT_Byte
*)stream
->base
+ stream
->pos
;
196 parser
->base_len
= size
;
197 parser
->in_memory
= 1;
199 /* check that the `size' field is valid */
200 if ( FT_STREAM_SKIP( size
) )
205 /* read segment in memory */
206 if ( FT_ALLOC( parser
->base_dict
, size
) ||
207 FT_STREAM_READ( parser
->base_dict
, size
) )
210 parser
->base_len
= size
;
213 parser
->root
.base
= parser
->base_dict
;
214 parser
->root
.cursor
= parser
->base_dict
;
215 parser
->root
.limit
= parser
->root
.cursor
+ parser
->base_len
;
218 if ( error
&& !parser
->in_memory
)
219 FT_FREE( parser
->base_dict
);
226 t42_parser_done( T42_Parser parser
)
228 FT_Memory memory
= parser
->root
.memory
;
231 /* free the base dictionary only when we have a disk stream */
232 if ( !parser
->in_memory
)
233 FT_FREE( parser
->base_dict
);
235 parser
->root
.funcs
.done( &parser
->root
);
240 t42_is_space( FT_Byte c
)
242 return ( c
== ' ' || c
== '\t' ||
243 c
== '\r' || c
== '\n' || c
== '\f' ||
249 t42_parse_font_matrix( T42_Face face
,
252 T42_Parser parser
= &loader
->parser
;
253 FT_Matrix
* matrix
= &face
->type1
.font_matrix
;
254 FT_Vector
* offset
= &face
->type1
.font_offset
;
255 FT_Face root
= (FT_Face
)&face
->root
;
261 result
= T1_ToFixedArray( parser
, 6, temp
, 3 );
265 parser
->root
.error
= FT_THROW( Invalid_File_Format
);
269 temp_scale
= FT_ABS( temp
[3] );
271 if ( temp_scale
== 0 )
273 FT_ERROR(( "t1_parse_font_matrix: invalid font matrix\n" ));
274 parser
->root
.error
= FT_THROW( Invalid_File_Format
);
278 /* Set Units per EM based on FontMatrix values. We set the value to */
279 /* 1000 / temp_scale, because temp_scale was already multiplied by */
280 /* 1000 (in t1_tofixed, from psobjs.c). */
282 root
->units_per_EM
= (FT_UShort
)FT_DivFix( 1000, temp_scale
);
284 /* we need to scale the values by 1.0/temp_scale */
285 if ( temp_scale
!= 0x10000L
)
287 temp
[0] = FT_DivFix( temp
[0], temp_scale
);
288 temp
[1] = FT_DivFix( temp
[1], temp_scale
);
289 temp
[2] = FT_DivFix( temp
[2], temp_scale
);
290 temp
[4] = FT_DivFix( temp
[4], temp_scale
);
291 temp
[5] = FT_DivFix( temp
[5], temp_scale
);
292 temp
[3] = temp
[3] < 0 ? -0x10000L
: 0x10000L
;
295 matrix
->xx
= temp
[0];
296 matrix
->yx
= temp
[1];
297 matrix
->xy
= temp
[2];
298 matrix
->yy
= temp
[3];
300 /* note that the offsets must be expressed in integer font units */
301 offset
->x
= temp
[4] >> 16;
302 offset
->y
= temp
[5] >> 16;
307 t42_parse_encoding( T42_Face face
,
310 T42_Parser parser
= &loader
->parser
;
312 FT_Byte
* limit
= parser
->root
.limit
;
314 PSAux_Service psaux
= (PSAux_Service
)face
->psaux
;
317 T1_Skip_Spaces( parser
);
318 cur
= parser
->root
.cursor
;
321 FT_ERROR(( "t42_parse_encoding: out of bounds\n" ));
322 parser
->root
.error
= FT_THROW( Invalid_File_Format
);
326 /* if we have a number or `[', the encoding is an array, */
327 /* and we must load it now */
328 if ( ft_isdigit( *cur
) || *cur
== '[' )
330 T1_Encoding encode
= &face
->type1
.encoding
;
332 PS_Table char_table
= &loader
->encoding_table
;
333 FT_Memory memory
= parser
->root
.memory
;
335 FT_Bool only_immediates
= 0;
338 /* read the number of entries in the encoding; should be 256 */
343 parser
->root
.cursor
++;
346 count
= (FT_Int
)T1_ToInt( parser
);
348 T1_Skip_Spaces( parser
);
349 if ( parser
->root
.cursor
>= limit
)
352 /* we use a T1_Table to store our charnames */
353 loader
->num_chars
= encode
->num_chars
= count
;
354 if ( FT_NEW_ARRAY( encode
->char_index
, count
) ||
355 FT_NEW_ARRAY( encode
->char_name
, count
) ||
356 FT_SET_ERROR( psaux
->ps_table_funcs
->init(
357 char_table
, count
, memory
) ) )
359 parser
->root
.error
= error
;
363 /* We need to `zero' out encoding_table.elements */
364 for ( n
= 0; n
< count
; n
++ )
366 char* notdef
= (char *)".notdef";
369 T1_Add_Table( char_table
, n
, notdef
, 8 );
372 /* Now we need to read records of the form */
374 /* ... charcode /charname ... */
376 /* for each entry in our table. */
378 /* We simply look for a number followed by an immediate */
379 /* name. Note that this ignores correctly the sequence */
380 /* that is often seen in type42 fonts: */
382 /* 0 1 255 { 1 index exch /.notdef put } for dup */
384 /* used to clean the encoding array before anything else. */
386 /* Alternatively, if the array is directly given as */
388 /* /Encoding [ ... ] */
390 /* we only read immediates. */
393 T1_Skip_Spaces( parser
);
395 while ( parser
->root
.cursor
< limit
)
397 cur
= parser
->root
.cursor
;
399 /* we stop when we encounter `def' or `]' */
400 if ( *cur
== 'd' && cur
+ 3 < limit
)
402 if ( cur
[1] == 'e' &&
404 t42_is_space( cur
[3] ) )
406 FT_TRACE6(( "encoding end\n" ));
413 FT_TRACE6(( "encoding end\n" ));
418 /* check whether we have found an entry */
419 if ( ft_isdigit( *cur
) || only_immediates
)
424 if ( only_immediates
)
428 charcode
= (FT_Int
)T1_ToInt( parser
);
429 T1_Skip_Spaces( parser
);
432 cur
= parser
->root
.cursor
;
434 if ( cur
+ 2 < limit
&& *cur
== '/' && n
< count
)
441 parser
->root
.cursor
= cur
;
442 T1_Skip_PS_Token( parser
);
443 if ( parser
->root
.cursor
>= limit
)
445 if ( parser
->root
.error
)
448 len
= parser
->root
.cursor
- cur
;
450 parser
->root
.error
= T1_Add_Table( char_table
, charcode
,
452 if ( parser
->root
.error
)
454 char_table
->elements
[charcode
][len
] = '\0';
458 else if ( only_immediates
)
460 /* Since the current position is not updated for */
461 /* immediates-only mode we would get an infinite loop if */
462 /* we don't do anything here. */
464 /* This encoding array is not valid according to the type1 */
465 /* specification (it might be an encoding for a CID type1 */
466 /* font, however), so we conclude that this font is NOT a */
468 parser
->root
.error
= FT_THROW( Unknown_File_Format
);
474 T1_Skip_PS_Token( parser
);
475 if ( parser
->root
.error
)
479 T1_Skip_Spaces( parser
);
482 face
->type1
.encoding_type
= T1_ENCODING_TYPE_ARRAY
;
483 parser
->root
.cursor
= cur
;
486 /* Otherwise, we should have either `StandardEncoding', */
487 /* `ExpertEncoding', or `ISOLatin1Encoding' */
490 if ( cur
+ 17 < limit
&&
491 ft_strncmp( (const char*)cur
, "StandardEncoding", 16 ) == 0 )
492 face
->type1
.encoding_type
= T1_ENCODING_TYPE_STANDARD
;
494 else if ( cur
+ 15 < limit
&&
495 ft_strncmp( (const char*)cur
, "ExpertEncoding", 14 ) == 0 )
496 face
->type1
.encoding_type
= T1_ENCODING_TYPE_EXPERT
;
498 else if ( cur
+ 18 < limit
&&
499 ft_strncmp( (const char*)cur
, "ISOLatin1Encoding", 17 ) == 0 )
500 face
->type1
.encoding_type
= T1_ENCODING_TYPE_ISOLATIN1
;
503 parser
->root
.error
= FT_THROW( Ignore
);
508 typedef enum T42_Load_Status_
518 t42_parse_sfnts( T42_Face face
,
521 T42_Parser parser
= &loader
->parser
;
522 FT_Memory memory
= parser
->root
.memory
;
524 FT_Byte
* limit
= parser
->root
.limit
;
526 FT_Int num_tables
= 0;
527 FT_ULong count
, ttf_size
= 0;
529 FT_Long n
, string_size
, old_string_size
, real_size
;
530 FT_Byte
* string_buf
= NULL
;
531 FT_Bool allocated
= 0;
533 T42_Load_Status status
;
538 /* /sfnts [ <hexstring> <hexstring> ... ] def */
543 /* <num_bin_bytes> RD <binary data> */
544 /* <num_bin_bytes> RD <binary data> */
548 /* with exactly one space after the `RD' token. */
550 T1_Skip_Spaces( parser
);
552 if ( parser
->root
.cursor
>= limit
|| *parser
->root
.cursor
++ != '[' )
554 FT_ERROR(( "t42_parse_sfnts: can't find begin of sfnts vector\n" ));
555 error
= FT_THROW( Invalid_File_Format
);
559 T1_Skip_Spaces( parser
);
560 status
= BEFORE_START
;
565 while ( parser
->root
.cursor
< limit
)
567 cur
= parser
->root
.cursor
;
571 parser
->root
.cursor
++;
575 else if ( *cur
== '<' )
577 T1_Skip_PS_Token( parser
);
578 if ( parser
->root
.error
)
581 /* don't include delimiters */
582 string_size
= (FT_Long
)( ( parser
->root
.cursor
- cur
- 2 + 1 ) / 2 );
583 if ( FT_REALLOC( string_buf
, old_string_size
, string_size
) )
588 parser
->root
.cursor
= cur
;
589 (void)T1_ToBytes( parser
, string_buf
, string_size
, &real_size
, 1 );
590 old_string_size
= string_size
;
591 string_size
= real_size
;
594 else if ( ft_isdigit( *cur
) )
598 FT_ERROR(( "t42_parse_sfnts: "
599 "can't handle mixed binary and hex strings\n" ));
600 error
= FT_THROW( Invalid_File_Format
);
604 string_size
= T1_ToInt( parser
);
605 if ( string_size
< 0 )
607 FT_ERROR(( "t42_parse_sfnts: invalid string size\n" ));
608 error
= FT_THROW( Invalid_File_Format
);
612 T1_Skip_PS_Token( parser
); /* `RD' */
613 if ( parser
->root
.error
)
616 string_buf
= parser
->root
.cursor
+ 1; /* one space after `RD' */
618 if ( limit
- parser
->root
.cursor
< string_size
)
620 FT_ERROR(( "t42_parse_sfnts: too many binary data\n" ));
621 error
= FT_THROW( Invalid_File_Format
);
625 parser
->root
.cursor
+= string_size
+ 1;
630 FT_ERROR(( "t42_parse_sfnts: invalid data in sfnts array\n" ));
631 error
= FT_THROW( Invalid_File_Format
);
635 /* A string can have a trailing zero (odd) byte for padding. */
637 if ( ( string_size
& 1 ) && string_buf
[string_size
- 1] == 0 )
642 FT_ERROR(( "t42_parse_sfnts: invalid string\n" ));
643 error
= FT_THROW( Invalid_File_Format
);
647 for ( n
= 0; n
< string_size
; n
++ )
652 /* load offset table, 12 bytes */
655 face
->ttf_data
[count
++] = string_buf
[n
];
660 num_tables
= 16 * face
->ttf_data
[4] + face
->ttf_data
[5];
661 status
= BEFORE_TABLE_DIR
;
662 ttf_size
= 12 + 16 * num_tables
;
664 if ( FT_REALLOC( face
->ttf_data
, 12, ttf_size
) )
669 case BEFORE_TABLE_DIR
:
670 /* the offset table is read; read the table directory */
671 if ( count
< ttf_size
)
673 face
->ttf_data
[count
++] = string_buf
[n
];
682 for ( i
= 0; i
< num_tables
; i
++ )
684 FT_Byte
* p
= face
->ttf_data
+ 12 + 16 * i
+ 12;
687 len
= FT_PEEK_ULONG( p
);
689 /* Pad to a 4-byte boundary length */
690 ttf_size
+= ( len
+ 3 ) & ~3;
693 status
= OTHER_TABLES
;
694 face
->ttf_size
= ttf_size
;
696 /* there are no more than 256 tables, so no size check here */
697 if ( FT_REALLOC( face
->ttf_data
, 12 + 16 * num_tables
,
704 /* all other tables are just copied */
705 if ( count
>= ttf_size
)
707 FT_ERROR(( "t42_parse_sfnts: too many binary data\n" ));
708 error
= FT_THROW( Invalid_File_Format
);
711 face
->ttf_data
[count
++] = string_buf
[n
];
715 T1_Skip_Spaces( parser
);
718 /* if control reaches this point, the format was not valid */
719 error
= FT_THROW( Invalid_File_Format
);
722 parser
->root
.error
= error
;
726 FT_FREE( string_buf
);
731 t42_parse_charstrings( T42_Face face
,
734 T42_Parser parser
= &loader
->parser
;
735 PS_Table code_table
= &loader
->charstrings
;
736 PS_Table name_table
= &loader
->glyph_names
;
737 PS_Table swap_table
= &loader
->swap_table
;
738 FT_Memory memory
= parser
->root
.memory
;
741 PSAux_Service psaux
= (PSAux_Service
)face
->psaux
;
744 FT_Byte
* limit
= parser
->root
.limit
;
746 FT_UInt notdef_index
= 0;
747 FT_Byte notdef_found
= 0;
750 T1_Skip_Spaces( parser
);
752 if ( parser
->root
.cursor
>= limit
)
754 FT_ERROR(( "t42_parse_charstrings: out of bounds\n" ));
755 error
= FT_THROW( Invalid_File_Format
);
759 if ( ft_isdigit( *parser
->root
.cursor
) )
761 loader
->num_glyphs
= (FT_UInt
)T1_ToInt( parser
);
762 if ( parser
->root
.error
)
765 else if ( *parser
->root
.cursor
== '<' )
767 /* We have `<< ... >>'. Count the number of `/' in the dictionary */
768 /* to get its size. */
772 T1_Skip_PS_Token( parser
);
773 if ( parser
->root
.error
)
775 T1_Skip_Spaces( parser
);
776 cur
= parser
->root
.cursor
;
778 while ( parser
->root
.cursor
< limit
)
780 if ( *parser
->root
.cursor
== '/' )
782 else if ( *parser
->root
.cursor
== '>' )
784 loader
->num_glyphs
= count
;
785 parser
->root
.cursor
= cur
; /* rewind */
788 T1_Skip_PS_Token( parser
);
789 if ( parser
->root
.error
)
791 T1_Skip_Spaces( parser
);
796 FT_ERROR(( "t42_parse_charstrings: invalid token\n" ));
797 error
= FT_THROW( Invalid_File_Format
);
801 if ( parser
->root
.cursor
>= limit
)
803 FT_ERROR(( "t42_parse_charstrings: out of bounds\n" ));
804 error
= FT_THROW( Invalid_File_Format
);
808 /* initialize tables */
810 error
= psaux
->ps_table_funcs
->init( code_table
,
816 error
= psaux
->ps_table_funcs
->init( name_table
,
822 /* Initialize table for swapping index notdef_index and */
823 /* index 0 names and codes (if necessary). */
825 error
= psaux
->ps_table_funcs
->init( swap_table
, 4, memory
);
833 /* The format is simple: */
834 /* `/glyphname' + index [+ def] */
836 T1_Skip_Spaces( parser
);
838 cur
= parser
->root
.cursor
;
842 /* We stop when we find an `end' keyword or '>' */
847 t42_is_space( cur
[3] ) )
852 T1_Skip_PS_Token( parser
);
853 if ( parser
->root
.error
)
861 if ( cur
+ 1 >= limit
)
863 FT_ERROR(( "t42_parse_charstrings: out of bounds\n" ));
864 error
= FT_THROW( Invalid_File_Format
);
868 cur
++; /* skip `/' */
869 len
= parser
->root
.cursor
- cur
;
871 error
= T1_Add_Table( name_table
, n
, cur
, len
+ 1 );
875 /* add a trailing zero to the name table */
876 name_table
->elements
[n
][len
] = '\0';
878 /* record index of /.notdef */
880 ft_strcmp( ".notdef",
881 (const char*)(name_table
->elements
[n
]) ) == 0 )
887 T1_Skip_Spaces( parser
);
889 cur
= parser
->root
.cursor
;
891 (void)T1_ToInt( parser
);
892 if ( parser
->root
.cursor
>= limit
)
894 FT_ERROR(( "t42_parse_charstrings: out of bounds\n" ));
895 error
= FT_THROW( Invalid_File_Format
);
899 len
= parser
->root
.cursor
- cur
;
901 error
= T1_Add_Table( code_table
, n
, cur
, len
+ 1 );
905 code_table
->elements
[n
][len
] = '\0';
908 if ( n
>= loader
->num_glyphs
)
913 loader
->num_glyphs
= n
;
917 FT_ERROR(( "t42_parse_charstrings: no /.notdef glyph\n" ));
918 error
= FT_THROW( Invalid_File_Format
);
922 /* if /.notdef does not occupy index 0, do our magic. */
923 if ( ft_strcmp( (const char*)".notdef",
924 (const char*)name_table
->elements
[0] ) )
926 /* Swap glyph in index 0 with /.notdef glyph. First, add index 0 */
927 /* name and code entries to swap_table. Then place notdef_index */
928 /* name and code entries into swap_table. Then swap name and code */
929 /* entries at indices notdef_index and 0 using values stored in */
933 error
= T1_Add_Table( swap_table
, 0,
934 name_table
->elements
[0],
935 name_table
->lengths
[0] );
940 error
= T1_Add_Table( swap_table
, 1,
941 code_table
->elements
[0],
942 code_table
->lengths
[0] );
946 /* Index notdef_index name */
947 error
= T1_Add_Table( swap_table
, 2,
948 name_table
->elements
[notdef_index
],
949 name_table
->lengths
[notdef_index
] );
953 /* Index notdef_index code */
954 error
= T1_Add_Table( swap_table
, 3,
955 code_table
->elements
[notdef_index
],
956 code_table
->lengths
[notdef_index
] );
960 error
= T1_Add_Table( name_table
, notdef_index
,
961 swap_table
->elements
[0],
962 swap_table
->lengths
[0] );
966 error
= T1_Add_Table( code_table
, notdef_index
,
967 swap_table
->elements
[1],
968 swap_table
->lengths
[1] );
972 error
= T1_Add_Table( name_table
, 0,
973 swap_table
->elements
[2],
974 swap_table
->lengths
[2] );
978 error
= T1_Add_Table( code_table
, 0,
979 swap_table
->elements
[3],
980 swap_table
->lengths
[3] );
989 parser
->root
.error
= error
;
994 t42_load_keyword( T42_Face face
,
1001 FT_UInt max_objects
= 0;
1004 /* if the keyword has a dedicated callback, call it */
1005 if ( field
->type
== T1_FIELD_TYPE_CALLBACK
)
1007 field
->reader( (FT_Face
)face
, loader
);
1008 error
= loader
->parser
.root
.error
;
1012 /* now the keyword is either a simple field or a table of fields; */
1013 /* we are now going to take care of it */
1015 switch ( field
->location
)
1017 case T1_FIELD_LOCATION_FONT_INFO
:
1018 dummy_object
= &face
->type1
.font_info
;
1021 case T1_FIELD_LOCATION_FONT_EXTRA
:
1022 dummy_object
= &face
->type1
.font_extra
;
1025 case T1_FIELD_LOCATION_BBOX
:
1026 dummy_object
= &face
->type1
.font_bbox
;
1030 dummy_object
= &face
->type1
;
1033 objects
= &dummy_object
;
1035 if ( field
->type
== T1_FIELD_TYPE_INTEGER_ARRAY
||
1036 field
->type
== T1_FIELD_TYPE_FIXED_ARRAY
)
1037 error
= T1_Load_Field_Table( &loader
->parser
, field
,
1038 objects
, max_objects
, 0 );
1040 error
= T1_Load_Field( &loader
->parser
, field
,
1041 objects
, max_objects
, 0 );
1048 FT_LOCAL_DEF( FT_Error
)
1049 t42_parse_dict( T42_Face face
,
1054 T42_Parser parser
= &loader
->parser
;
1056 FT_Int n_keywords
= (FT_Int
)( sizeof ( t42_keywords
) /
1057 sizeof ( t42_keywords
[0] ) );
1060 parser
->root
.cursor
= base
;
1061 parser
->root
.limit
= base
+ size
;
1062 parser
->root
.error
= FT_Err_Ok
;
1064 limit
= parser
->root
.limit
;
1066 T1_Skip_Spaces( parser
);
1068 while ( parser
->root
.cursor
< limit
)
1073 cur
= parser
->root
.cursor
;
1075 /* look for `FontDirectory' which causes problems for some fonts */
1076 if ( *cur
== 'F' && cur
+ 25 < limit
&&
1077 ft_strncmp( (char*)cur
, "FontDirectory", 13 ) == 0 )
1082 /* skip the `FontDirectory' keyword */
1083 T1_Skip_PS_Token( parser
);
1084 T1_Skip_Spaces ( parser
);
1085 cur
= cur2
= parser
->root
.cursor
;
1087 /* look up the `known' keyword */
1088 while ( cur
< limit
)
1090 if ( *cur
== 'k' && cur
+ 5 < limit
&&
1091 ft_strncmp( (char*)cur
, "known", 5 ) == 0 )
1094 T1_Skip_PS_Token( parser
);
1095 if ( parser
->root
.error
)
1097 T1_Skip_Spaces ( parser
);
1098 cur
= parser
->root
.cursor
;
1106 /* skip the `known' keyword and the token following it */
1107 T1_Skip_PS_Token( parser
);
1108 T1_ToToken( parser
, &token
);
1110 /* if the last token was an array, skip it! */
1111 if ( token
.type
== T1_TOKEN_TYPE_ARRAY
)
1112 cur2
= parser
->root
.cursor
;
1114 parser
->root
.cursor
= cur2
;
1117 /* look for immediates */
1118 else if ( *cur
== '/' && cur
+ 2 < limit
)
1125 parser
->root
.cursor
= cur
;
1126 T1_Skip_PS_Token( parser
);
1127 if ( parser
->root
.error
)
1130 len
= parser
->root
.cursor
- cur
;
1132 if ( len
> 0 && len
< 22 && parser
->root
.cursor
< limit
)
1137 /* now compare the immediate name to the keyword table */
1139 /* loop through all known keywords */
1140 for ( i
= 0; i
< n_keywords
; i
++ )
1142 T1_Field keyword
= (T1_Field
)&t42_keywords
[i
];
1143 FT_Byte
*name
= (FT_Byte
*)keyword
->ident
;
1149 if ( cur
[0] == name
[0] &&
1150 len
== (FT_PtrDist
)ft_strlen( (const char *)name
) &&
1151 ft_memcmp( cur
, name
, len
) == 0 )
1153 /* we found it -- run the parsing callback! */
1154 parser
->root
.error
= t42_load_keyword( face
,
1157 if ( parser
->root
.error
)
1158 return parser
->root
.error
;
1166 T1_Skip_PS_Token( parser
);
1167 if ( parser
->root
.error
)
1171 T1_Skip_Spaces( parser
);
1175 return parser
->root
.error
;
1179 FT_LOCAL_DEF( void )
1180 t42_loader_init( T42_Loader loader
,
1185 FT_MEM_ZERO( loader
, sizeof ( *loader
) );
1186 loader
->num_glyphs
= 0;
1187 loader
->num_chars
= 0;
1189 /* initialize the tables -- simply set their `init' field to 0 */
1190 loader
->encoding_table
.init
= 0;
1191 loader
->charstrings
.init
= 0;
1192 loader
->glyph_names
.init
= 0;
1196 FT_LOCAL_DEF( void )
1197 t42_loader_done( T42_Loader loader
)
1199 T42_Parser parser
= &loader
->parser
;
1202 /* finalize tables */
1203 T1_Release_Table( &loader
->encoding_table
);
1204 T1_Release_Table( &loader
->charstrings
);
1205 T1_Release_Table( &loader
->glyph_names
);
1206 T1_Release_Table( &loader
->swap_table
);
1208 /* finalize parser */
1209 t42_parser_done( parser
);