Bringing apdf from vendor into main branch.
[AROS-Contrib.git] / apdf / freetype2 / type1 / t1parse.c
blob2f9184df45e5e5302cff0700c2aa5f2375a6eceb
1 /***************************************************************************/
2 /* */
3 /* t1parse.c */
4 /* */
5 /* Type 1 parser (body). */
6 /* */
7 /* Copyright 1996-2001, 2002 by */
8 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
9 /* */
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. */
15 /* */
16 /***************************************************************************/
19 /*************************************************************************/
20 /* */
21 /* The Type 1 parser is in charge of the following: */
22 /* */
23 /* - provide an implementation of a growing sequence of objects called */
24 /* a `T1_Table' (used to build various tables needed by the loader). */
25 /* */
26 /* - opening .pfb and .pfa files to extract their top-level and private */
27 /* dictionaries. */
28 /* */
29 /* - read numbers, arrays & strings from any dictionary. */
30 /* */
31 /* See `t1load.c' to see how data is loaded from the font file. */
32 /* */
33 /*************************************************************************/
36 #include <ft2build.h>
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
42 #include "t1parse.h"
44 #include "t1errors.h"
47 /*************************************************************************/
48 /* */
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. */
52 /* */
53 #undef FT_COMPONENT
54 #define FT_COMPONENT trace_t1parse
57 /*************************************************************************/
58 /*************************************************************************/
59 /*************************************************************************/
60 /***** *****/
61 /***** INPUT STREAM PARSER *****/
62 /***** *****/
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_
76 FT_UShort tag;
77 FT_Long size;
79 } PFB_Tag;
82 #undef FT_STRUCTURE
83 #define FT_STRUCTURE PFB_Tag
86 static
87 const FT_Frame_Field pfb_tag_fields[] =
89 FT_FRAME_START( 6 ),
90 FT_FRAME_USHORT ( tag ),
91 FT_FRAME_LONG_LE( size ),
92 FT_FRAME_END
96 static FT_Error
97 read_pfb_tag( FT_Stream stream,
98 FT_UShort* tag,
99 FT_Long* size )
101 FT_Error error;
102 PFB_Tag head;
105 *tag = 0;
106 *size = 0;
107 if ( !FT_STREAM_READ_FIELDS( pfb_tag_fields, &head ) )
109 if ( head.tag == 0x8001U || head.tag == 0x8002U )
111 *tag = head.tag;
112 *size = head.size;
115 return error;
119 FT_LOCAL_DEF( FT_Error )
120 T1_New_Parser( T1_Parser parser,
121 FT_Stream stream,
122 FT_Memory memory,
123 PSAux_Service psaux )
125 FT_Error error;
126 FT_UShort tag;
127 FT_Long size;
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;
137 parser->in_pfb = 0;
138 parser->in_memory = 0;
139 parser->single_block = 0;
141 /******************************************************************/
142 /* */
143 /* Here a short summary of what is going on: */
144 /* */
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. */
148 /* */
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. */
152 /* */
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. */
155 /* */
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 ) )
160 goto Exit;
162 error = read_pfb_tag( stream, &tag, &size );
163 if ( error )
164 goto Exit;
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 ) )
171 goto Exit;
172 size = stream->size;
174 else
175 parser->in_pfb = 1;
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 */
181 if ( !stream->read )
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 ) )
189 goto Exit;
191 else
193 /* read segment in memory */
194 if ( FT_ALLOC( parser->base_dict, size ) ||
195 FT_STREAM_READ( parser->base_dict, size ) )
196 goto Exit;
197 parser->base_len = size;
200 /* Now check font format; we must see `%!PS-AdobeFont-1' */
201 /* or `%!FontType' */
203 if ( size <= 16 ||
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;
212 else
214 parser->root.base = parser->base_dict;
215 parser->root.cursor = parser->base_dict;
216 parser->root.limit = parser->root.cursor + parser->base_len;
220 Exit:
221 if ( error && !parser->in_memory )
222 FT_FREE( parser->base_dict );
224 return error;
228 FT_LOCAL_DEF( void )
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 */
246 static int
247 hexa_value( char c )
249 unsigned int d;
252 d = (unsigned int)( c - '0' );
253 if ( d <= 9 )
254 return (int)d;
256 d = (unsigned int)( c - 'a' );
257 if ( d <= 5 )
258 return (int)( d + 10 );
260 d = (unsigned int)( c - 'A' );
261 if ( d <= 5 )
262 return (int)( d + 10 );
264 return -1;
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;
274 FT_Error error = 0;
275 FT_Long size;
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();
285 FT_UShort tag;
288 parser->private_len = 0;
289 for (;;)
291 error = read_pfb_tag( stream, &tag, &size );
292 if ( error )
293 goto Fail;
295 if ( tag != 0x8002U )
296 break;
298 parser->private_len += size;
300 if ( FT_STREAM_SKIP( size ) )
301 goto Fail;
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;
311 goto Fail;
314 if ( FT_STREAM_SEEK( start_pos ) ||
315 FT_ALLOC( parser->private_dict, parser->private_len ) )
316 goto Fail;
318 parser->private_len = 0;
319 for (;;)
321 error = read_pfb_tag( stream, &tag, &size );
322 if ( error || tag != 0x8002U )
324 error = T1_Err_Ok;
325 break;
328 if ( FT_STREAM_READ( parser->private_dict + parser->private_len, size ) )
329 goto Fail;
331 parser->private_len += size;
334 else
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;
344 FT_Byte c;
347 for (;;)
349 c = cur[0];
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' )
361 cur++;
363 break;
366 cur++;
367 if ( cur >= limit )
369 FT_ERROR(( "T1_Get_Private_Dict:" ));
370 FT_ERROR(( " could not find `eexec' keyword\n" ));
371 error = T1_Err_Invalid_File_Format;
372 goto Exit;
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 ) )
386 goto Fail;
387 parser->private_len = size;
389 else
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 );
411 else
413 /* ASCII hexadecimal encoding */
415 FT_Byte* write;
416 FT_Int count;
419 write = parser->private_dict;
420 count = 0;
422 for ( ;cur < limit; cur++ )
424 int hex1;
427 /* check for newline */
428 if ( cur[0] == '\r' || cur[0] == '\n' )
429 continue;
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 )
434 break;
436 /* otherwise, store byte */
437 *write++ = (FT_Byte)( ( hex1 << 4 ) | hexa_value( cur[1] ) );
438 count++;
439 cur++;
442 /* put a safeguard */
443 parser->private_len = (FT_Int)( write - parser->private_dict );
444 *write++ = 0;
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;
454 Fail:
455 Exit:
456 return error;
460 /* END */