1 /***************************************************************************/
5 /* Type 1 parser (body). */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2005 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_CALC_H
39 #include FT_INTERNAL_STREAM_H
40 #include FT_INTERNAL_POSTSCRIPT_AUX_H
47 /*************************************************************************/
49 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
50 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
51 /* messages during execution. */
54 #define FT_COMPONENT trace_t1parse
57 /*************************************************************************/
58 /*************************************************************************/
59 /*************************************************************************/
61 /***** INPUT STREAM PARSER *****/
63 /*************************************************************************/
64 /*************************************************************************/
65 /*************************************************************************/
69 read_pfb_tag( FT_Stream stream
,
81 if ( !FT_READ_USHORT( tag
) )
83 if ( tag
== 0x8001U
|| tag
== 0x8002U
)
85 if ( !FT_READ_LONG_LE( size
) )
97 check_type1_format( FT_Stream stream
,
98 const char* header_string
,
99 size_t header_length
)
106 if ( FT_STREAM_SEEK( 0 ) )
109 error
= read_pfb_tag( stream
, &tag
, &size
);
113 if ( tag
!= 0x8001U
&& FT_STREAM_SEEK( 0 ) )
116 if ( !FT_FRAME_ENTER( header_length
) )
120 if ( ft_memcmp( stream
->cursor
, header_string
, header_length
) != 0 )
121 error
= T1_Err_Unknown_File_Format
;
131 FT_LOCAL_DEF( FT_Error
)
132 T1_New_Parser( T1_Parser parser
,
135 PSAux_Service psaux
)
142 psaux
->ps_parser_funcs
->init( &parser
->root
, 0, 0, memory
);
144 parser
->stream
= stream
;
145 parser
->base_len
= 0;
146 parser
->base_dict
= 0;
147 parser
->private_len
= 0;
148 parser
->private_dict
= 0;
150 parser
->in_memory
= 0;
151 parser
->single_block
= 0;
153 /* check the header format */
154 error
= check_type1_format( stream
, "%!PS-AdobeFont", 14 );
157 if ( error
!= T1_Err_Unknown_File_Format
)
160 error
= check_type1_format( stream
, "%!FontType", 10 );
163 FT_TRACE2(( "[not a Type1 font]\n" ));
168 /******************************************************************/
170 /* Here a short summary of what is going on: */
172 /* When creating a new Type 1 parser, we try to locate and load */
173 /* the base dictionary if this is possible (i.e. for PFB */
174 /* files). Otherwise, we load the whole font into memory. */
176 /* When `loading' the base dictionary, we only setup pointers */
177 /* in the case of a memory-based stream. Otherwise, we */
178 /* allocate and load the base dictionary in it. */
180 /* parser->in_pfb is set if we are in a binary (".pfb") font. */
181 /* parser->in_memory is set if we have a memory stream. */
184 /* try to compute the size of the base dictionary; */
185 /* look for a Postscript binary file tag, i.e 0x8001 */
186 if ( FT_STREAM_SEEK( 0L ) )
189 error
= read_pfb_tag( stream
, &tag
, &size
);
193 if ( tag
!= 0x8001U
)
195 /* assume that this is a PFA file for now; an error will */
196 /* be produced later when more things are checked */
197 if ( FT_STREAM_SEEK( 0L ) )
204 /* now, try to load `size' bytes of the `base' dictionary we */
205 /* found previously */
207 /* if it is a memory-based resource, set up pointers */
210 parser
->base_dict
= (FT_Byte
*)stream
->base
+ stream
->pos
;
211 parser
->base_len
= size
;
212 parser
->in_memory
= 1;
214 /* check that the `size' field is valid */
215 if ( FT_STREAM_SKIP( size
) )
220 /* read segment in memory - this is clumsy, but so does the format */
221 if ( FT_ALLOC( parser
->base_dict
, size
) ||
222 FT_STREAM_READ( parser
->base_dict
, size
) )
224 parser
->base_len
= size
;
227 parser
->root
.base
= parser
->base_dict
;
228 parser
->root
.cursor
= parser
->base_dict
;
229 parser
->root
.limit
= parser
->root
.cursor
+ parser
->base_len
;
232 if ( error
&& !parser
->in_memory
)
233 FT_FREE( parser
->base_dict
);
240 T1_Finalize_Parser( T1_Parser parser
)
242 FT_Memory memory
= parser
->root
.memory
;
245 /* always free the private dictionary */
246 FT_FREE( parser
->private_dict
);
248 /* free the base dictionary only when we have a disk stream */
249 if ( !parser
->in_memory
)
250 FT_FREE( parser
->base_dict
);
252 parser
->root
.funcs
.done( &parser
->root
);
256 FT_LOCAL_DEF( FT_Error
)
257 T1_Get_Private_Dict( T1_Parser parser
,
258 PSAux_Service psaux
)
260 FT_Stream stream
= parser
->stream
;
261 FT_Memory memory
= parser
->root
.memory
;
262 FT_Error error
= T1_Err_Ok
;
266 if ( parser
->in_pfb
)
268 /* in the case of the PFB format, the private dictionary can be */
269 /* made of several segments. We thus first read the number of */
270 /* segments to compute the total size of the private dictionary */
271 /* then re-read them into memory. */
272 FT_Long start_pos
= FT_STREAM_POS();
276 parser
->private_len
= 0;
279 error
= read_pfb_tag( stream
, &tag
, &size
);
283 if ( tag
!= 0x8002U
)
286 parser
->private_len
+= size
;
288 if ( FT_STREAM_SKIP( size
) )
292 /* Check that we have a private dictionary there */
293 /* and allocate private dictionary buffer */
294 if ( parser
->private_len
== 0 )
296 FT_ERROR(( "T1_Get_Private_Dict:" ));
297 FT_ERROR(( " invalid private dictionary section\n" ));
298 error
= T1_Err_Invalid_File_Format
;
302 if ( FT_STREAM_SEEK( start_pos
) ||
303 FT_ALLOC( parser
->private_dict
, parser
->private_len
) )
306 parser
->private_len
= 0;
309 error
= read_pfb_tag( stream
, &tag
, &size
);
310 if ( error
|| tag
!= 0x8002U
)
316 if ( FT_STREAM_READ( parser
->private_dict
+ parser
->private_len
,
320 parser
->private_len
+= size
;
325 /* We have already `loaded' the whole PFA font file into memory; */
326 /* if this is a memory resource, allocate a new block to hold */
327 /* the private dict. Otherwise, simply overwrite into the base */
328 /* dictionary block in the heap. */
330 /* first of all, look at the `eexec' keyword */
331 FT_Byte
* cur
= parser
->base_dict
;
332 FT_Byte
* limit
= cur
+ parser
->base_len
;
340 if ( c
== 'e' && cur
+ 9 < limit
) /* 9 = 5 letters for `eexec' + */
341 /* newline + 4 chars */
343 if ( cur
[1] == 'e' &&
352 FT_ERROR(( "T1_Get_Private_Dict:" ));
353 FT_ERROR(( " could not find `eexec' keyword\n" ));
354 error
= T1_Err_Invalid_File_Format
;
359 /* check whether `eexec' was real -- it could be in a comment */
360 /* or string (as e.g. in u003043t.gsf from ghostscript) */
362 parser
->root
.cursor
= parser
->base_dict
;
363 parser
->root
.limit
= cur
+ 9;
365 cur
= parser
->root
.cursor
;
366 limit
= parser
->root
.limit
;
368 while ( cur
< limit
)
370 if ( *cur
== 'e' && ft_strncmp( (char*)cur
, "eexec", 5 ) == 0 )
373 T1_Skip_PS_Token( parser
);
374 if ( parser
->root
.error
)
376 T1_Skip_Spaces ( parser
);
377 cur
= parser
->root
.cursor
;
380 /* we haven't found the correct `eexec'; go back and continue */
384 limit
= parser
->base_dict
+ parser
->base_len
;
387 /* now determine where to write the _encrypted_ binary private */
388 /* dictionary. We overwrite the base dictionary for disk-based */
389 /* resources and allocate a new block otherwise */
392 parser
->root
.limit
= parser
->base_dict
+ parser
->base_len
;
394 T1_Skip_PS_Token( parser
);
395 cur
= parser
->root
.cursor
;
402 else if ( *cur
== '\n' )
406 FT_ERROR(( "T1_Get_Private_Dict:" ));
407 FT_ERROR(( " `eexec' not properly terminated\n" ));
408 error
= T1_Err_Invalid_File_Format
;
412 size
= (FT_Long
)( parser
->base_len
- ( cur
- parser
->base_dict
) );
414 if ( parser
->in_memory
)
416 /* note that we allocate one more byte to put a terminating `0' */
417 if ( FT_ALLOC( parser
->private_dict
, size
+ 1 ) )
419 parser
->private_len
= size
;
423 parser
->single_block
= 1;
424 parser
->private_dict
= parser
->base_dict
;
425 parser
->private_len
= size
;
426 parser
->base_dict
= 0;
427 parser
->base_len
= 0;
430 /* now determine whether the private dictionary is encoded in binary */
431 /* or hexadecimal ASCII format -- decode it accordingly */
433 /* we need to access the next 4 bytes (after the final \r following */
434 /* the `eexec' keyword); if they all are hexadecimal digits, then */
435 /* we have a case of ASCII storage */
437 if ( ft_isxdigit( cur
[0] ) && ft_isxdigit( cur
[1] ) &&
438 ft_isxdigit( cur
[2] ) && ft_isxdigit( cur
[3] ) )
440 /* ASCII hexadecimal encoding */
444 parser
->root
.cursor
= cur
;
445 (void)psaux
->ps_parser_funcs
->to_bytes( &parser
->root
,
446 parser
->private_dict
,
450 parser
->private_len
= len
;
452 /* put a safeguard */
453 parser
->private_dict
[len
] = '\0';
456 /* binary encoding -- copy the private dict */
457 FT_MEM_MOVE( parser
->private_dict
, cur
, size
);
460 /* we now decrypt the encoded binary private dictionary */
461 psaux
->t1_decrypt( parser
->private_dict
, parser
->private_len
, 55665U );
463 /* replace the four random bytes at the beginning with whitespace */
464 parser
->private_dict
[0] = ' ';
465 parser
->private_dict
[1] = ' ';
466 parser
->private_dict
[2] = ' ';
467 parser
->private_dict
[3] = ' ';
469 parser
->root
.base
= parser
->private_dict
;
470 parser
->root
.cursor
= parser
->private_dict
;
471 parser
->root
.limit
= parser
->root
.cursor
+ parser
->private_len
;