1 /***************************************************************************/
5 /* CID-keyed Type1 parser (body). */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 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 /***************************************************************************/
20 #include FT_INTERNAL_DEBUG_H
21 #include FT_INTERNAL_OBJECTS_H
22 #include FT_INTERNAL_STREAM_H
29 /*************************************************************************/
31 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
32 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
33 /* messages during execution. */
36 #define FT_COMPONENT trace_cidparse
39 /*************************************************************************/
40 /*************************************************************************/
41 /*************************************************************************/
43 /***** INPUT STREAM PARSER *****/
45 /*************************************************************************/
46 /*************************************************************************/
47 /*************************************************************************/
50 FT_LOCAL_DEF( FT_Error )
51 cid_parser_new( CID_Parser* parser,
57 FT_ULong base_offset, offset, ps_len;
62 FT_MEM_ZERO( parser, sizeof ( *parser ) );
63 psaux->ps_parser_funcs->init( &parser->root, 0, 0, memory );
65 parser->stream = stream;
67 base_offset = FT_STREAM_POS();
69 /* first of all, check the font format in the header */
70 if ( FT_FRAME_ENTER( 31 ) )
73 if ( ft_strncmp( (char *)stream->cursor,
74 "%!PS-Adobe-3.0 Resource-CIDFont", 31 ) )
76 FT_TRACE2(( " not a CID-keyed font\n" ));
77 error = CID_Err_Unknown_File_Format;
85 /* now, read the rest of the file until we find */
86 /* `StartData' or `/sfnts' */
88 FT_Byte buffer[256 + 10];
89 FT_Long read_len = 256 + 10; /* same as signed FT_Stream->size */
93 for ( offset = FT_STREAM_POS(); ; offset += 256 )
95 FT_Long stream_len; /* same as signed FT_Stream->size */
98 stream_len = stream->size - FT_STREAM_POS();
99 if ( stream_len == 0 )
101 FT_TRACE2(( "cid_parser_new: no `StartData' keyword found\n" ));
102 error = CID_Err_Invalid_File_Format;
106 read_len = FT_MIN( read_len, stream_len );
107 if ( FT_STREAM_READ( p, read_len ) )
110 if ( read_len < 256 )
113 limit = p + read_len - 10;
115 for ( p = buffer; p < limit; p++ )
117 if ( p[0] == 'S' && ft_strncmp( (char*)p, "StartData", 9 ) == 0 )
119 /* save offset of binary data after `StartData' */
120 offset += p - buffer + 10;
123 else if ( p[1] == 's' && ft_strncmp( (char*)p, "/sfnts", 6 ) == 0 )
125 offset += p - buffer + 7;
130 FT_MEM_MOVE( buffer, p, 10 );
137 /* We have found the start of the binary data or the `/sfnts' token. */
138 /* Now rewind and extract the frame corresponding to this PostScript */
141 ps_len = offset - base_offset;
142 if ( FT_STREAM_SEEK( base_offset ) ||
143 FT_FRAME_EXTRACT( ps_len, parser->postscript ) )
146 parser->data_offset = offset;
147 parser->postscript_len = ps_len;
148 parser->root.base = parser->postscript;
149 parser->root.cursor = parser->postscript;
150 parser->root.limit = parser->root.cursor + ps_len;
151 parser->num_dict = -1;
153 /* Finally, we check whether `StartData' or `/sfnts' was real -- */
154 /* it could be in a comment or string. We also get the arguments */
155 /* of `StartData' to find out whether the data is represented in */
156 /* binary or hex format. */
158 arg1 = parser->root.cursor;
159 cid_parser_skip_PS_token( parser );
160 cid_parser_skip_spaces ( parser );
161 arg2 = parser->root.cursor;
162 cid_parser_skip_PS_token( parser );
163 cid_parser_skip_spaces ( parser );
165 limit = parser->root.limit;
166 cur = parser->root.cursor;
168 while ( cur < limit )
170 if ( parser->root.error )
172 error = parser->root.error;
176 if ( cur[0] == 'S' && ft_strncmp( (char*)cur, "StartData", 9 ) == 0 )
178 if ( ft_strncmp( (char*)arg1, "(Hex)", 5 ) == 0 )
179 parser->binary_length = ft_atol( (const char *)arg2 );
181 limit = parser->root.limit;
182 cur = parser->root.cursor;
185 else if ( cur[1] == 's' && ft_strncmp( (char*)cur, "/sfnts", 6 ) == 0 )
187 FT_TRACE2(( "cid_parser_new: cannot handle Type 11 fonts\n" ));
188 error = CID_Err_Unknown_File_Format;
192 cid_parser_skip_PS_token( parser );
193 cid_parser_skip_spaces ( parser );
196 cur = parser->root.cursor;
199 /* we haven't found the correct `StartData'; go back and continue */
201 FT_FRAME_RELEASE( parser->postscript );
202 if ( !FT_STREAM_SEEK( offset ) )
211 cid_parser_done( CID_Parser* parser )
213 /* always free the private dictionary */
214 if ( parser->postscript )
216 FT_Stream stream = parser->stream;
219 FT_FRAME_RELEASE( parser->postscript );
221 parser->root.funcs.done( &parser->root );