1 /***************************************************************************/
5 /* Type 1 parser (body). */
7 /* Copyright 1996-2005, 2008, 2009, 2012 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 /* The Type 1 parser is in charge of the following: */
23 /* - provide an implementation of a growing sequence of objects called */
24 /* a `T1_Table' (used to build various tables needed by the loader). */
26 /* - opening .pfb and .pfa files to extract their top-level and private */
29 /* - read numbers, arrays & strings from any dictionary. */
31 /* See `t1load.c' to see how data is loaded from the font file. */
33 /*************************************************************************/
37 #include FT_INTERNAL_DEBUG_H
38 #include FT_INTERNAL_STREAM_H
39 #include FT_INTERNAL_POSTSCRIPT_AUX_H
46 /*************************************************************************/
48 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
49 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
50 /* messages during execution. */
53 #define FT_COMPONENT trace_t1parse
56 /*************************************************************************/
57 /*************************************************************************/
58 /*************************************************************************/
60 /***** INPUT STREAM PARSER *****/
62 /*************************************************************************/
63 /*************************************************************************/
64 /*************************************************************************/
67 /* see Adobe Technical Note 5040.Download_Fonts.pdf */
70 read_pfb_tag( FT_Stream stream,
82 if ( !FT_READ_USHORT( tag ) )
84 if ( tag == 0x8001U || tag == 0x8002U )
86 if ( !FT_READ_ULONG_LE( size ) )
98 check_type1_format( FT_Stream stream,
99 const char* header_string,
100 size_t header_length )
107 if ( FT_STREAM_SEEK( 0 ) )
110 error = read_pfb_tag( stream, &tag, &dummy );
114 /* We assume that the first segment in a PFB is always encoded as */
115 /* text. This might be wrong (and the specification doesn't insist */
116 /* on that), but we have never seen a counterexample. */
117 if ( tag != 0x8001U && FT_STREAM_SEEK( 0 ) )
120 if ( !FT_FRAME_ENTER( header_length ) )
124 if ( ft_memcmp( stream->cursor, header_string, header_length ) != 0 )
125 error = T1_Err_Unknown_File_Format;
135 FT_LOCAL_DEF( FT_Error )
136 T1_New_Parser( T1_Parser parser,
139 PSAux_Service psaux )
146 psaux->ps_parser_funcs->init( &parser->root, 0, 0, memory );
148 parser->stream = stream;
149 parser->base_len = 0;
150 parser->base_dict = 0;
151 parser->private_len = 0;
152 parser->private_dict = 0;
154 parser->in_memory = 0;
155 parser->single_block = 0;
157 /* check the header format */
158 error = check_type1_format( stream, "%!PS-AdobeFont", 14 );
161 if ( error != T1_Err_Unknown_File_Format )
164 error = check_type1_format( stream, "%!FontType", 10 );
167 FT_TRACE2(( " not a Type 1 font\n" ));
172 /******************************************************************/
174 /* Here a short summary of what is going on: */
176 /* When creating a new Type 1 parser, we try to locate and load */
177 /* the base dictionary if this is possible (i.e., for PFB */
178 /* files). Otherwise, we load the whole font into memory. */
180 /* When `loading' the base dictionary, we only setup pointers */
181 /* in the case of a memory-based stream. Otherwise, we */
182 /* allocate and load the base dictionary in it. */
184 /* parser->in_pfb is set if we are in a binary (`.pfb') font. */
185 /* parser->in_memory is set if we have a memory stream. */
188 /* try to compute the size of the base dictionary; */
189 /* look for a Postscript binary file tag, i.e., 0x8001 */
190 if ( FT_STREAM_SEEK( 0L ) )
193 error = read_pfb_tag( stream, &tag, &size );
197 if ( tag != 0x8001U )
199 /* assume that this is a PFA file for now; an error will */
200 /* be produced later when more things are checked */
201 if ( FT_STREAM_SEEK( 0L ) )
208 /* now, try to load `size' bytes of the `base' dictionary we */
209 /* found previously */
211 /* if it is a memory-based resource, set up pointers */
214 parser->base_dict = (FT_Byte*)stream->base + stream->pos;
215 parser->base_len = size;
216 parser->in_memory = 1;
218 /* check that the `size' field is valid */
219 if ( FT_STREAM_SKIP( size ) )
224 /* read segment in memory -- this is clumsy, but so does the format */
225 if ( FT_ALLOC( parser->base_dict, size ) ||
226 FT_STREAM_READ( parser->base_dict, size ) )
228 parser->base_len = size;
231 parser->root.base = parser->base_dict;
232 parser->root.cursor = parser->base_dict;
233 parser->root.limit = parser->root.cursor + parser->base_len;
236 if ( error && !parser->in_memory )
237 FT_FREE( parser->base_dict );
244 T1_Finalize_Parser( T1_Parser parser )
246 FT_Memory memory = parser->root.memory;
249 /* always free the private dictionary */
250 FT_FREE( parser->private_dict );
252 /* free the base dictionary only when we have a disk stream */
253 if ( !parser->in_memory )
254 FT_FREE( parser->base_dict );
256 parser->root.funcs.done( &parser->root );
260 FT_LOCAL_DEF( FT_Error )
261 T1_Get_Private_Dict( T1_Parser parser,
262 PSAux_Service psaux )
264 FT_Stream stream = parser->stream;
265 FT_Memory memory = parser->root.memory;
266 FT_Error error = T1_Err_Ok;
270 if ( parser->in_pfb )
272 /* in the case of the PFB format, the private dictionary can be */
273 /* made of several segments. We thus first read the number of */
274 /* segments to compute the total size of the private dictionary */
275 /* then re-read them into memory. */
276 FT_Long start_pos = FT_STREAM_POS();
280 parser->private_len = 0;
283 error = read_pfb_tag( stream, &tag, &size );
287 if ( tag != 0x8002U )
290 parser->private_len += size;
292 if ( FT_STREAM_SKIP( size ) )
296 /* Check that we have a private dictionary there */
297 /* and allocate private dictionary buffer */
298 if ( parser->private_len == 0 )
300 FT_ERROR(( "T1_Get_Private_Dict:"
301 " invalid private dictionary section\n" ));
302 error = T1_Err_Invalid_File_Format;
306 if ( FT_STREAM_SEEK( start_pos ) ||
307 FT_ALLOC( parser->private_dict, parser->private_len ) )
310 parser->private_len = 0;
313 error = read_pfb_tag( stream, &tag, &size );
314 if ( error || tag != 0x8002U )
320 if ( FT_STREAM_READ( parser->private_dict + parser->private_len,
324 parser->private_len += size;
329 /* We have already `loaded' the whole PFA font file into memory; */
330 /* if this is a memory resource, allocate a new block to hold */
331 /* the private dict. Otherwise, simply overwrite into the base */
332 /* dictionary block in the heap. */
334 /* first of all, look at the `eexec' keyword */
335 FT_Byte* cur = parser->base_dict;
336 FT_Byte* limit = cur + parser->base_len;
344 if ( c == 'e' && cur + 9 < limit ) /* 9 = 5 letters for `eexec' + */
345 /* whitespace + 4 chars */
347 if ( cur[1] == 'e' &&
356 FT_ERROR(( "T1_Get_Private_Dict:"
357 " could not find `eexec' keyword\n" ));
358 error = T1_Err_Invalid_File_Format;
363 /* check whether `eexec' was real -- it could be in a comment */
364 /* or string (as e.g. in u003043t.gsf from ghostscript) */
366 parser->root.cursor = parser->base_dict;
367 /* set limit to `eexec' + whitespace + 4 characters */
368 parser->root.limit = cur + 10;
370 cur = parser->root.cursor;
371 limit = parser->root.limit;
373 while ( cur < limit )
375 if ( *cur == 'e' && ft_strncmp( (char*)cur, "eexec", 5 ) == 0 )
378 T1_Skip_PS_Token( parser );
379 if ( parser->root.error )
381 T1_Skip_Spaces ( parser );
382 cur = parser->root.cursor;
385 /* we haven't found the correct `eexec'; go back and continue */
389 limit = parser->base_dict + parser->base_len;
392 /* now determine where to write the _encrypted_ binary private */
393 /* dictionary. We overwrite the base dictionary for disk-based */
394 /* resources and allocate a new block otherwise */
397 parser->root.limit = parser->base_dict + parser->base_len;
399 T1_Skip_PS_Token( parser );
400 cur = parser->root.cursor;
401 limit = parser->root.limit;
403 /* according to the Type1 spec, the first cipher byte must not be */
404 /* an ASCII whitespace character code (blank, tab, carriage return */
405 /* or line feed). We have seen Type 1 fonts with two line feed */
406 /* characters... So skip now all whitespace character codes. */
407 while ( cur < limit &&
415 FT_ERROR(( "T1_Get_Private_Dict:"
416 " `eexec' not properly terminated\n" ));
417 error = T1_Err_Invalid_File_Format;
421 size = parser->base_len - ( cur - parser->base_dict );
423 if ( parser->in_memory )
425 /* note that we allocate one more byte to put a terminating `0' */
426 if ( FT_ALLOC( parser->private_dict, size + 1 ) )
428 parser->private_len = size;
432 parser->single_block = 1;
433 parser->private_dict = parser->base_dict;
434 parser->private_len = size;
435 parser->base_dict = 0;
436 parser->base_len = 0;
439 /* now determine whether the private dictionary is encoded in binary */
440 /* or hexadecimal ASCII format -- decode it accordingly */
442 /* we need to access the next 4 bytes (after the final whitespace */
443 /* following the `eexec' keyword); if they all are hexadecimal */
444 /* digits, then we have a case of ASCII storage */
446 if ( cur + 3 < limit &&
447 ft_isxdigit( cur[0] ) && ft_isxdigit( cur[1] ) &&
448 ft_isxdigit( cur[2] ) && ft_isxdigit( cur[3] ) )
450 /* ASCII hexadecimal encoding */
454 parser->root.cursor = cur;
455 (void)psaux->ps_parser_funcs->to_bytes( &parser->root,
456 parser->private_dict,
460 parser->private_len = len;
462 /* put a safeguard */
463 parser->private_dict[len] = '\0';
466 /* binary encoding -- copy the private dict */
467 FT_MEM_MOVE( parser->private_dict, cur, size );
470 /* we now decrypt the encoded binary private dictionary */
471 psaux->t1_decrypt( parser->private_dict, parser->private_len, 55665U );
473 if ( parser->private_len < 4 )
475 FT_ERROR(( "T1_Get_Private_Dict:"
476 " invalid private dictionary section\n" ));
477 error = T1_Err_Invalid_File_Format;
481 /* replace the four random bytes at the beginning with whitespace */
482 parser->private_dict[0] = ' ';
483 parser->private_dict[1] = ' ';
484 parser->private_dict[2] = ' ';
485 parser->private_dict[3] = ' ';
487 parser->root.base = parser->private_dict;
488 parser->root.cursor = parser->private_dict;
489 parser->root.limit = parser->root.cursor + parser->private_len;