1 /***************************************************************************/
5 /* Type 1 parser (body). */
7 /* Copyright 1996-2001, 2002 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 /*************************************************************************/
68 #define IS_T1_WHITESPACE( c ) ( (c) == ' ' || (c) == '\t' )
69 #define IS_T1_LINESPACE( c ) ( (c) == '\r' || (c) == '\n' || (c) == '\0' || (c) == '\f' )
71 #define IS_T1_SPACE( c ) ( IS_T1_WHITESPACE( c ) || IS_T1_LINESPACE( c ) )
74 typedef struct PFB_Tag_
83 #define FT_STRUCTURE PFB_Tag
87 const FT_Frame_Field pfb_tag_fields
[] =
90 FT_FRAME_USHORT ( tag
),
91 FT_FRAME_LONG_LE( size
),
97 read_pfb_tag( FT_Stream stream
,
107 if ( !FT_STREAM_READ_FIELDS( pfb_tag_fields
, &head
) )
109 if ( head
.tag
== 0x8001U
|| head
.tag
== 0x8002U
)
119 FT_LOCAL_DEF( FT_Error
)
120 T1_New_Parser( T1_Parser parser
,
123 PSAux_Service psaux
)
130 psaux
->ps_parser_funcs
->init( &parser
->root
,0, 0, memory
);
132 parser
->stream
= stream
;
133 parser
->base_len
= 0;
134 parser
->base_dict
= 0;
135 parser
->private_len
= 0;
136 parser
->private_dict
= 0;
138 parser
->in_memory
= 0;
139 parser
->single_block
= 0;
141 /******************************************************************/
143 /* Here a short summary of what is going on: */
145 /* When creating a new Type 1 parser, we try to locate and load */
146 /* the base dictionary if this is possible (i.e. for PFB */
147 /* files). Otherwise, we load the whole font into memory. */
149 /* When `loading' the base dictionary, we only setup pointers */
150 /* in the case of a memory-based stream. Otherwise, we */
151 /* allocate and load the base dictionary in it. */
153 /* parser->in_pfb is set if we are in a binary (".pfb") font. */
154 /* parser->in_memory is set if we have a memory stream. */
157 /* try to compute the size of the base dictionary; */
158 /* look for a Postscript binary file tag, i.e 0x8001 */
159 if ( FT_STREAM_SEEK( 0L ) )
162 error
= read_pfb_tag( stream
, &tag
, &size
);
166 if ( tag
!= 0x8001U
)
168 /* assume that this is a PFA file for now; an error will */
169 /* be produced later when more things are checked */
170 if ( FT_STREAM_SEEK( 0L ) )
177 /* now, try to load `size' bytes of the `base' dictionary we */
178 /* found previously */
180 /* if it is a memory-based resource, set up pointers */
183 parser
->base_dict
= (FT_Byte
*)stream
->base
+ stream
->pos
;
184 parser
->base_len
= size
;
185 parser
->in_memory
= 1;
187 /* check that the `size' field is valid */
188 if ( FT_STREAM_SKIP( size
) )
193 /* read segment in memory */
194 if ( FT_ALLOC( parser
->base_dict
, size
) ||
195 FT_STREAM_READ( parser
->base_dict
, size
) )
197 parser
->base_len
= size
;
200 /* Now check font format; we must see `%!PS-AdobeFont-1' */
201 /* or `%!FontType' */
204 ( ft_strncmp( (const char*)parser
->base_dict
,
205 "%!PS-AdobeFont-1", 16 ) &&
206 ft_strncmp( (const char*)parser
->base_dict
,
207 "%!FontType", 10 ) ) )
209 FT_TRACE2(( "[not a Type1 font]\n" ));
210 error
= T1_Err_Unknown_File_Format
;
214 parser
->root
.base
= parser
->base_dict
;
215 parser
->root
.cursor
= parser
->base_dict
;
216 parser
->root
.limit
= parser
->root
.cursor
+ parser
->base_len
;
221 if ( error
&& !parser
->in_memory
)
222 FT_FREE( parser
->base_dict
);
229 T1_Finalize_Parser( T1_Parser parser
)
231 FT_Memory memory
= parser
->root
.memory
;
234 /* always free the private dictionary */
235 FT_FREE( parser
->private_dict
);
237 /* free the base dictionary only when we have a disk stream */
238 if ( !parser
->in_memory
)
239 FT_FREE( parser
->base_dict
);
241 parser
->root
.funcs
.done( &parser
->root
);
245 /* return the value of an hexadecimal digit */
252 d
= (unsigned int)( c
- '0' );
256 d
= (unsigned int)( c
- 'a' );
258 return (int)( d
+ 10 );
260 d
= (unsigned int)( c
- 'A' );
262 return (int)( d
+ 10 );
268 FT_LOCAL_DEF( FT_Error
)
269 T1_Get_Private_Dict( T1_Parser parser
,
270 PSAux_Service psaux
)
272 FT_Stream stream
= parser
->stream
;
273 FT_Memory memory
= parser
->root
.memory
;
278 if ( parser
->in_pfb
)
280 /* in the case of the PFB format, the private dictionary can be */
281 /* made of several segments. We thus first read the number of */
282 /* segments to compute the total size of the private dictionary */
283 /* then re-read them into memory. */
284 FT_Long start_pos
= FT_STREAM_POS();
288 parser
->private_len
= 0;
291 error
= read_pfb_tag( stream
, &tag
, &size
);
295 if ( tag
!= 0x8002U
)
298 parser
->private_len
+= size
;
300 if ( FT_STREAM_SKIP( size
) )
304 /* Check that we have a private dictionary there */
305 /* and allocate private dictionary buffer */
306 if ( parser
->private_len
== 0 )
308 FT_ERROR(( "T1_Get_Private_Dict:" ));
309 FT_ERROR(( " invalid private dictionary section\n" ));
310 error
= T1_Err_Invalid_File_Format
;
314 if ( FT_STREAM_SEEK( start_pos
) ||
315 FT_ALLOC( parser
->private_dict
, parser
->private_len
) )
318 parser
->private_len
= 0;
321 error
= read_pfb_tag( stream
, &tag
, &size
);
322 if ( error
|| tag
!= 0x8002U
)
328 if ( FT_STREAM_READ( parser
->private_dict
+ parser
->private_len
, size
) )
331 parser
->private_len
+= size
;
336 /* we have already `loaded' the whole PFA font file into memory; */
337 /* if this is a memory resource, allocate a new block to hold */
338 /* the private dict. Otherwise, simply overwrite into the base */
339 /* dictionary block in the heap. */
341 /* first of all, look at the `eexec' keyword */
342 FT_Byte
* cur
= parser
->base_dict
;
343 FT_Byte
* limit
= cur
+ parser
->base_len
;
350 if ( c
== 'e' && cur
+ 9 < limit
) /* 9 = 5 letters for `eexec' + */
351 /* newline + 4 chars */
353 if ( cur
[1] == 'e' && cur
[2] == 'x' &&
354 cur
[3] == 'e' && cur
[4] == 'c' )
356 cur
+= 6; /* we skip the newling after the `eexec' */
358 /* XXX: Some fonts use DOS-linefeeds, i.e. \r\n; we need to */
359 /* skip the extra \n if we find it */
360 if ( cur
[0] == '\n' )
369 FT_ERROR(( "T1_Get_Private_Dict:" ));
370 FT_ERROR(( " could not find `eexec' keyword\n" ));
371 error
= T1_Err_Invalid_File_Format
;
376 /* now determine where to write the _encrypted_ binary private */
377 /* dictionary. We overwrite the base dictionary for disk-based */
378 /* resources and allocate a new block otherwise */
380 size
= (FT_Long
)( parser
->base_len
- ( cur
- parser
->base_dict
) );
382 if ( parser
->in_memory
)
384 /* note that we allocate one more byte to put a terminating `0' */
385 if ( FT_ALLOC( parser
->private_dict
, size
+ 1 ) )
387 parser
->private_len
= size
;
391 parser
->single_block
= 1;
392 parser
->private_dict
= parser
->base_dict
;
393 parser
->private_len
= size
;
394 parser
->base_dict
= 0;
395 parser
->base_len
= 0;
398 /* now determine whether the private dictionary is encoded in binary */
399 /* or hexadecimal ASCII format -- decode it accordingly */
401 /* we need to access the next 4 bytes (after the final \r following */
402 /* the `eexec' keyword); if they all are hexadecimal digits, then */
403 /* we have a case of ASCII storage */
405 if ( ( hexa_value( cur
[0] ) | hexa_value( cur
[1] ) |
406 hexa_value( cur
[2] ) | hexa_value( cur
[3] ) ) < 0 )
408 /* binary encoding -- `simply' copy the private dict */
409 FT_MEM_COPY( parser
->private_dict
, cur
, size
);
413 /* ASCII hexadecimal encoding */
419 write
= parser
->private_dict
;
422 for ( ;cur
< limit
; cur
++ )
427 /* check for newline */
428 if ( cur
[0] == '\r' || cur
[0] == '\n' )
431 /* exit if we have a non-hexadecimal digit that isn't a newline */
432 hex1
= hexa_value( cur
[0] );
433 if ( hex1
< 0 || cur
+ 1 >= limit
)
436 /* otherwise, store byte */
437 *write
++ = (FT_Byte
)( ( hex1
<< 4 ) | hexa_value( cur
[1] ) );
442 /* put a safeguard */
443 parser
->private_len
= (FT_Int
)( write
- parser
->private_dict
);
448 /* we now decrypt the encoded binary private dictionary */
449 psaux
->t1_decrypt( parser
->private_dict
, parser
->private_len
, 55665U );
450 parser
->root
.base
= parser
->private_dict
;
451 parser
->root
.cursor
= parser
->private_dict
;
452 parser
->root
.limit
= parser
->root
.cursor
+ parser
->private_len
;