1 /***************************************************************************/
5 /* Type 1 parser (body). */
7 /* Copyright 1996-2001 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
46 #include <string.h> /* for strncmp() */
49 /*************************************************************************/
51 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
52 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
53 /* messages during execution. */
56 #define FT_COMPONENT trace_t1parse
59 /*************************************************************************/
60 /*************************************************************************/
61 /*************************************************************************/
63 /***** INPUT STREAM PARSER *****/
65 /*************************************************************************/
66 /*************************************************************************/
67 /*************************************************************************/
70 #define IS_T1_WHITESPACE( c ) ( (c) == ' ' || (c) == '\t' )
71 #define IS_T1_LINESPACE( c ) ( (c) == '\r' || (c) == '\n' )
73 #define IS_T1_SPACE( c ) ( IS_T1_WHITESPACE( c ) || IS_T1_LINESPACE( c ) )
76 typedef struct PFB_Tag_
85 #define FT_STRUCTURE PFB_Tag
89 const FT_Frame_Field pfb_tag_fields[] =
92 FT_FRAME_USHORT ( tag ),
93 FT_FRAME_LONG_LE( size ),
99 read_pfb_tag( FT_Stream stream,
109 if ( !READ_Fields( pfb_tag_fields, &head ) )
111 if ( head.tag == 0x8001 || head.tag == 0x8002 )
121 FT_LOCAL_DEF FT_Error
122 T1_New_Parser( T1_ParserRec* parser,
125 PSAux_Interface* psaux )
132 psaux->t1_parser_funcs->init( &parser->root,0, 0, memory );
134 parser->stream = stream;
135 parser->base_len = 0;
136 parser->base_dict = 0;
137 parser->private_len = 0;
138 parser->private_dict = 0;
140 parser->in_memory = 0;
141 parser->single_block = 0;
143 /******************************************************************/
145 /* Here a short summary of what is going on: */
147 /* When creating a new Type 1 parser, we try to locate and load */
148 /* the base dictionary if this is possible (i.e. for PFB */
149 /* files). Otherwise, we load the whole font into memory. */
151 /* When `loading' the base dictionary, we only setup pointers */
152 /* in the case of a memory-based stream. Otherwise, we */
153 /* allocate and load the base dictionary in it. */
155 /* parser->in_pfb is set if we are in a binary (".pfb") font. */
156 /* parser->in_memory is set if we have a memory stream. */
159 /* try to compute the size of the base dictionary; */
160 /* look for a Postscript binary file tag, i.e 0x8001 */
161 if ( FILE_Seek( 0L ) )
164 error = read_pfb_tag( stream, &tag, &size );
170 /* assume that this is a PFA file for now; an error will */
171 /* be produced later when more things are checked */
172 if ( FILE_Seek( 0L ) )
179 /* now, try to load `size' bytes of the `base' dictionary we */
180 /* found previously */
182 /* if it is a memory-based resource, set up pointers */
185 parser->base_dict = (FT_Byte*)stream->base + stream->pos;
186 parser->base_len = size;
187 parser->in_memory = 1;
189 /* check that the `size' field is valid */
190 if ( FILE_Skip( size ) )
195 /* read segment in memory */
196 if ( ALLOC( parser->base_dict, size ) ||
197 FILE_Read( parser->base_dict, size ) )
199 parser->base_len = size;
202 /* Now check font format; we must see `%!PS-AdobeFont-1' */
203 /* or `%!FontType' */
206 ( strncmp( (const char*)parser->base_dict,
207 "%!PS-AdobeFont-1", 16 ) &&
208 strncmp( (const char*)parser->base_dict,
209 "%!FontType", 10 ) ) )
211 FT_TRACE2(( "[not a Type1 font]\n" ));
212 error = T1_Err_Unknown_File_Format;
216 parser->root.base = parser->base_dict;
217 parser->root.cursor = parser->base_dict;
218 parser->root.limit = parser->root.cursor + parser->base_len;
223 if ( error && !parser->in_memory )
224 FREE( parser->base_dict );
231 T1_Finalize_Parser( T1_ParserRec* parser )
233 FT_Memory memory = parser->root.memory;
236 /* always free the private dictionary */
237 FREE( parser->private_dict );
239 /* free the base dictionary only when we have a disk stream */
240 if ( !parser->in_memory )
241 FREE( parser->base_dict );
243 parser->root.funcs.done( &parser->root );
247 /* return the value of an hexadecimal digit */
254 d = (unsigned int)( c - '0' );
258 d = (unsigned int)( c - 'a' );
260 return (int)( d + 10 );
262 d = (unsigned int)( c - 'A' );
264 return (int)( d + 10 );
270 FT_LOCAL_DEF FT_Error
271 T1_Get_Private_Dict( T1_ParserRec* parser,
272 PSAux_Interface* psaux )
274 FT_Stream stream = parser->stream;
275 FT_Memory memory = parser->root.memory;
280 if ( parser->in_pfb )
282 /* in the case of the PFB format, the private dictionary can be */
283 /* made of several segments. We thus first read the number of */
284 /* segments to compute the total size of the private dictionary */
285 /* then re-read them into memory. */
286 FT_Long start_pos = FILE_Pos();
290 parser->private_len = 0;
293 error = read_pfb_tag( stream, &tag, &size );
300 parser->private_len += size;
302 if ( FILE_Skip( size ) )
306 /* Check that we have a private dictionary there */
307 /* and allocate private dictionary buffer */
308 if ( parser->private_len == 0 )
310 FT_ERROR(( "T1_Get_Private_Dict:" ));
311 FT_ERROR(( " invalid private dictionary section\n" ));
312 error = T1_Err_Invalid_File_Format;
316 if ( FILE_Seek( start_pos ) ||
317 ALLOC( parser->private_dict, parser->private_len ) )
320 parser->private_len = 0;
323 error = read_pfb_tag( stream, &tag, &size );
324 if ( error || tag != 0x8002 )
330 if ( FILE_Read( parser->private_dict + parser->private_len, size ) )
333 parser->private_len += size;
338 /* we have already `loaded' the whole PFA font file into memory; */
339 /* if this is a memory resource, allocate a new block to hold */
340 /* the private dict. Otherwise, simply overwrite into the base */
341 /* dictionary block in the heap. */
343 /* first of all, look at the `eexec' keyword */
344 FT_Byte* cur = parser->base_dict;
345 FT_Byte* limit = cur + parser->base_len;
352 if ( c == 'e' && cur + 9 < limit ) /* 9 = 5 letters for `eexec' + */
353 /* newline + 4 chars */
355 if ( cur[1] == 'e' && cur[2] == 'x' &&
356 cur[3] == 'e' && cur[4] == 'c' )
358 cur += 6; /* we skip the newling after the `eexec' */
360 /* XXX: Some fonts use DOS-linefeeds, i.e. \r\n; we need to */
361 /* skip the extra \n if we find it */
362 if ( cur[0] == '\n' )
371 FT_ERROR(( "T1_Get_Private_Dict:" ));
372 FT_ERROR(( " could not find `eexec' keyword\n" ));
373 error = T1_Err_Invalid_File_Format;
378 /* now determine where to write the _encrypted_ binary private */
379 /* dictionary. We overwrite the base dictionary for disk-based */
380 /* resources and allocate a new block otherwise */
382 size = (FT_Long)( parser->base_len - ( cur - parser->base_dict ) );
384 if ( parser->in_memory )
386 /* note that we allocate one more byte to put a terminating `0' */
387 if ( ALLOC( parser->private_dict, size + 1 ) )
389 parser->private_len = size;
393 parser->single_block = 1;
394 parser->private_dict = parser->base_dict;
395 parser->private_len = size;
396 parser->base_dict = 0;
397 parser->base_len = 0;
400 /* now determine whether the private dictionary is encoded in binary */
401 /* or hexadecimal ASCII format -- decode it accordingly */
403 /* we need to access the next 4 bytes (after the final \r following */
404 /* the `eexec' keyword); if they all are hexadecimal digits, then */
405 /* we have a case of ASCII storage */
407 if ( ( hexa_value( cur[0] ) | hexa_value( cur[1] ) |
408 hexa_value( cur[2] ) | hexa_value( cur[3] ) ) < 0 )
410 /* binary encoding -- `simply' copy the private dict */
411 MEM_Copy( parser->private_dict, cur, size );
415 /* ASCII hexadecimal encoding */
421 write = parser->private_dict;
424 for ( ;cur < limit; cur++ )
429 /* check for newline */
430 if ( cur[0] == '\r' || cur[0] == '\n' )
433 /* exit if we have a non-hexadecimal digit that isn't a newline */
434 hex1 = hexa_value( cur[0] );
435 if ( hex1 < 0 || cur + 1 >= limit )
438 /* otherwise, store byte */
439 *write++ = (FT_Byte)( ( hex1 << 4 ) | hexa_value( cur[1] ) );
444 /* put a safeguard */
445 parser->private_len = (FT_Int)( write - parser->private_dict );
450 /* we now decrypt the encoded binary private dictionary */
451 psaux->t1_decrypt( parser->private_dict, parser->private_len, 55665U );
452 parser->root.base = parser->private_dict;
453 parser->root.cursor = parser->private_dict;
454 parser->root.limit = parser->root.cursor + parser->private_len;