1 /***************************************************************************/
5 /* CID-keyed Type1 font loader (body). */
7 /* Copyright 1996-2006, 2009, 2011-2014 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_CONFIG_CONFIG_H
22 #include FT_MULTIPLE_MASTERS_H
23 #include FT_INTERNAL_TYPE1_TYPES_H
30 /*************************************************************************/
32 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
33 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
34 /* messages during execution. */
37 #define FT_COMPONENT trace_cidload
40 /* read a single offset */
41 FT_LOCAL_DEF( FT_Long )
42 cid_get_offset( FT_Byte* *start,
49 for ( result = 0; offsize > 0; offsize-- )
56 return (FT_Long)result;
60 /*************************************************************************/
61 /*************************************************************************/
63 /***** TYPE 1 SYMBOL PARSING *****/
65 /*************************************************************************/
66 /*************************************************************************/
70 cid_load_keyword( CID_Face face,
72 const T1_Field keyword )
75 CID_Parser* parser = &loader->parser;
78 CID_FaceInfo cid = &face->cid;
81 /* if the keyword has a dedicated callback, call it */
82 if ( keyword->type == T1_FIELD_TYPE_CALLBACK )
84 keyword->reader( (FT_Face)face, parser );
85 error = parser->root.error;
89 /* we must now compute the address of our target object */
90 switch ( keyword->location )
92 case T1_FIELD_LOCATION_CID_INFO:
93 object = (FT_Byte*)cid;
96 case T1_FIELD_LOCATION_FONT_INFO:
97 object = (FT_Byte*)&cid->font_info;
100 case T1_FIELD_LOCATION_FONT_EXTRA:
101 object = (FT_Byte*)&face->font_extra;
104 case T1_FIELD_LOCATION_BBOX:
105 object = (FT_Byte*)&cid->font_bbox;
113 if ( parser->num_dict < 0 || parser->num_dict >= cid->num_dicts )
115 FT_ERROR(( "cid_load_keyword: invalid use of `%s'\n",
117 error = FT_THROW( Syntax_Error );
121 dict = cid->font_dicts + parser->num_dict;
122 switch ( keyword->location )
124 case T1_FIELD_LOCATION_PRIVATE:
125 object = (FT_Byte*)&dict->private_dict;
129 object = (FT_Byte*)dict;
134 dummy_object = object;
136 /* now, load the keyword data in the object's field(s) */
137 if ( keyword->type == T1_FIELD_TYPE_INTEGER_ARRAY ||
138 keyword->type == T1_FIELD_TYPE_FIXED_ARRAY )
139 error = cid_parser_load_field_table( &loader->parser, keyword,
142 error = cid_parser_load_field( &loader->parser,
143 keyword, &dummy_object );
149 FT_CALLBACK_DEF( FT_Error )
150 cid_parse_font_matrix( CID_Face face,
154 FT_Face root = (FT_Face)&face->root;
159 if ( parser->num_dict >= 0 && parser->num_dict < face->cid.num_dicts )
166 dict = face->cid.font_dicts + parser->num_dict;
167 matrix = &dict->font_matrix;
168 offset = &dict->font_offset;
170 result = cid_parser_to_fixed_array( parser, 6, temp, 3 );
173 return FT_THROW( Invalid_File_Format );
175 temp_scale = FT_ABS( temp[3] );
177 if ( temp_scale == 0 )
179 FT_ERROR(( "cid_parse_font_matrix: invalid font matrix\n" ));
180 return FT_THROW( Invalid_File_Format );
183 /* Set Units per EM based on FontMatrix values. We set the value to */
184 /* 1000 / temp_scale, because temp_scale was already multiplied by */
185 /* 1000 (in t1_tofixed, from psobjs.c). */
187 root->units_per_EM = (FT_UShort)FT_DivFix( 1000, temp_scale );
189 /* we need to scale the values by 1.0/temp[3] */
190 if ( temp_scale != 0x10000L )
192 temp[0] = FT_DivFix( temp[0], temp_scale );
193 temp[1] = FT_DivFix( temp[1], temp_scale );
194 temp[2] = FT_DivFix( temp[2], temp_scale );
195 temp[4] = FT_DivFix( temp[4], temp_scale );
196 temp[5] = FT_DivFix( temp[5], temp_scale );
197 temp[3] = temp[3] < 0 ? -0x10000L : 0x10000L;
200 matrix->xx = temp[0];
201 matrix->yx = temp[1];
202 matrix->xy = temp[2];
203 matrix->yy = temp[3];
205 /* note that the font offsets are expressed in integer font units */
206 offset->x = temp[4] >> 16;
207 offset->y = temp[5] >> 16;
214 FT_CALLBACK_DEF( FT_Error )
215 parse_fd_array( CID_Face face,
218 CID_FaceInfo cid = &face->cid;
219 FT_Memory memory = face->root.memory;
220 FT_Error error = FT_Err_Ok;
224 num_dicts = cid_parser_to_int( parser );
226 if ( !cid->font_dicts )
231 if ( FT_NEW_ARRAY( cid->font_dicts, num_dicts ) )
234 cid->num_dicts = (FT_UInt)num_dicts;
236 /* don't forget to set a few defaults */
237 for ( n = 0; n < cid->num_dicts; n++ )
239 CID_FaceDict dict = cid->font_dicts + n;
242 /* default value for lenIV */
243 dict->private_dict.lenIV = 4;
252 /* by mistake, `expansion_factor' appears both in PS_PrivateRec */
253 /* and CID_FaceDictRec (both are public header files and can't */
254 /* changed); we simply copy the value */
256 FT_CALLBACK_DEF( FT_Error )
257 parse_expansion_factor( CID_Face face,
263 if ( parser->num_dict >= 0 && parser->num_dict < face->cid.num_dicts )
265 dict = face->cid.font_dicts + parser->num_dict;
267 dict->expansion_factor = cid_parser_to_fixed( parser, 0 );
268 dict->private_dict.expansion_factor = dict->expansion_factor;
276 const T1_FieldRec cid_field_records[] =
279 #include "cidtoken.h"
281 T1_FIELD_CALLBACK( "FDArray", parse_fd_array, 0 )
282 T1_FIELD_CALLBACK( "FontMatrix", cid_parse_font_matrix, 0 )
283 T1_FIELD_CALLBACK( "ExpansionFactor", parse_expansion_factor, 0 )
285 { 0, T1_FIELD_LOCATION_CID_INFO, T1_FIELD_TYPE_NONE, 0, 0, 0, 0, 0, 0 }
290 cid_parse_dict( CID_Face face,
295 CID_Parser* parser = &loader->parser;
298 parser->root.cursor = base;
299 parser->root.limit = base + size;
300 parser->root.error = FT_Err_Ok;
304 FT_Byte* limit = cur + size;
312 parser->root.cursor = cur;
313 cid_parser_skip_spaces( parser );
315 if ( parser->root.cursor >= limit )
316 newlimit = limit - 1 - 17;
318 newlimit = parser->root.cursor - 17;
320 /* look for `%ADOBeginFontDict' */
321 for ( ; cur < newlimit; cur++ )
324 ft_strncmp( (char*)cur, "%ADOBeginFontDict", 17 ) == 0 )
326 /* if /FDArray was found, then cid->num_dicts is > 0, and */
327 /* we can start increasing parser->num_dict */
328 if ( face->cid.num_dicts > 0 )
333 cur = parser->root.cursor;
334 /* no error can occur in cid_parser_skip_spaces */
338 cid_parser_skip_PS_token( parser );
339 if ( parser->root.cursor >= limit || parser->root.error )
342 /* look for immediates */
343 if ( *cur == '/' && cur + 2 < limit )
349 len = parser->root.cursor - cur;
351 if ( len > 0 && len < 22 )
353 /* now compare the immediate name to the keyword table */
354 T1_Field keyword = (T1_Field)cid_field_records;
362 name = (FT_Byte*)keyword->ident;
366 if ( cur[0] == name[0] &&
367 len == (FT_PtrDist)ft_strlen( (const char*)name ) )
372 for ( n = 1; n < len; n++ )
373 if ( cur[n] != name[n] )
378 /* we found it - run the parsing callback */
379 parser->root.error = cid_load_keyword( face,
382 if ( parser->root.error )
383 return parser->root.error;
392 cur = parser->root.cursor;
395 return parser->root.error;
399 /* read the subrmap and the subrs of each font dict */
401 cid_read_subrs( CID_Face face )
403 CID_FaceInfo cid = &face->cid;
404 FT_Memory memory = face->root.memory;
405 FT_Stream stream = face->cid_stream;
409 FT_UInt max_offsets = 0;
410 FT_ULong* offsets = 0;
411 PSAux_Service psaux = (PSAux_Service)face->psaux;
414 if ( FT_NEW_ARRAY( face->subrs, cid->num_dicts ) )
418 for ( n = 0; n < cid->num_dicts; n++, subr++ )
420 CID_FaceDict dict = cid->font_dicts + n;
421 FT_Int lenIV = dict->private_dict.lenIV;
422 FT_UInt count, num_subrs = dict->num_subrs;
427 /* Check for possible overflow. */
428 if ( num_subrs == FT_UINT_MAX )
430 error = FT_THROW( Syntax_Error );
434 /* reallocate offsets array if needed */
435 if ( num_subrs + 1 > max_offsets )
437 FT_UInt new_max = FT_PAD_CEIL( num_subrs + 1, 4 );
440 if ( new_max <= max_offsets )
442 error = FT_THROW( Syntax_Error );
446 if ( FT_RENEW_ARRAY( offsets, max_offsets, new_max ) )
449 max_offsets = new_max;
452 /* read the subrmap's offsets */
453 if ( FT_STREAM_SEEK( cid->data_offset + dict->subrmap_offset ) ||
454 FT_FRAME_ENTER( ( num_subrs + 1 ) * dict->sd_bytes ) )
457 p = (FT_Byte*)stream->cursor;
458 for ( count = 0; count <= num_subrs; count++ )
459 offsets[count] = cid_get_offset( &p, (FT_Byte)dict->sd_bytes );
463 /* offsets must be ordered */
464 for ( count = 1; count <= num_subrs; count++ )
465 if ( offsets[count - 1] > offsets[count] )
468 /* now, compute the size of subrs charstrings, */
469 /* allocate, and read them */
470 data_len = offsets[num_subrs] - offsets[0];
472 if ( FT_NEW_ARRAY( subr->code, num_subrs + 1 ) ||
473 FT_ALLOC( subr->code[0], data_len ) )
476 if ( FT_STREAM_SEEK( cid->data_offset + offsets[0] ) ||
477 FT_STREAM_READ( subr->code[0], data_len ) )
480 /* set up pointers */
481 for ( count = 1; count <= num_subrs; count++ )
486 len = offsets[count] - offsets[count - 1];
487 subr->code[count] = subr->code[count - 1] + len;
490 /* decrypt subroutines, but only if lenIV >= 0 */
493 for ( count = 0; count < num_subrs; count++ )
498 len = offsets[count + 1] - offsets[count];
499 psaux->t1_decrypt( subr->code[count], len, 4330 );
503 subr->num_subrs = num_subrs;
513 for ( n = 0; n < cid->num_dicts; n++ )
515 if ( face->subrs[n].code )
516 FT_FREE( face->subrs[n].code[0] );
518 FT_FREE( face->subrs[n].code );
520 FT_FREE( face->subrs );
527 cid_init_loader( CID_Loader* loader,
532 FT_MEM_ZERO( loader, sizeof ( *loader ) );
537 cid_done_loader( CID_Loader* loader )
539 CID_Parser* parser = &loader->parser;
542 /* finalize parser */
543 cid_parser_done( parser );
548 cid_hex_to_binary( FT_Byte* data,
553 FT_Stream stream = face->root.stream;
561 FT_Bool upper_nibble, done;
564 if ( FT_STREAM_SEEK( offset ) )
568 dlimit = d + data_len;
579 FT_ULong oldpos = FT_STREAM_POS();
580 FT_ULong size = stream->size - oldpos;
585 error = FT_THROW( Syntax_Error );
589 if ( FT_STREAM_READ( buffer, 256 > size ? size : 256 ) )
592 plimit = p + FT_STREAM_POS() - oldpos;
595 if ( ft_isdigit( *p ) )
596 val = (FT_Byte)( *p - '0' );
597 else if ( *p >= 'a' && *p <= 'f' )
598 val = (FT_Byte)( *p - 'a' );
599 else if ( *p >= 'A' && *p <= 'F' )
600 val = (FT_Byte)( *p - 'A' + 10 );
601 else if ( *p == ' ' ||
611 else if ( *p == '>' )
618 error = FT_THROW( Syntax_Error );
623 *d = (FT_Byte)( val << 4 );
626 *d = (FT_Byte)( *d + val );
630 upper_nibble = (FT_Byte)( 1 - upper_nibble );
645 FT_LOCAL_DEF( FT_Error )
646 cid_face_open( CID_Face face,
651 FT_Memory memory = face->root.memory;
655 cid_init_loader( &loader, face );
657 parser = &loader.parser;
658 error = cid_parser_new( parser, face->root.stream, face->root.memory,
659 (PSAux_Service)face->psaux );
663 error = cid_parse_dict( face, &loader,
665 parser->postscript_len );
669 if ( face_index < 0 )
672 if ( FT_NEW( face->cid_stream ) )
675 if ( parser->binary_length )
677 /* we must convert the data section from hexadecimal to binary */
678 if ( FT_ALLOC( face->binary_data, parser->binary_length ) ||
679 cid_hex_to_binary( face->binary_data, parser->binary_length,
680 parser->data_offset, face ) )
683 FT_Stream_OpenMemory( face->cid_stream,
684 face->binary_data, parser->binary_length );
685 face->cid.data_offset = 0;
689 *face->cid_stream = *face->root.stream;
690 face->cid.data_offset = loader.parser.data_offset;
693 error = cid_read_subrs( face );
696 cid_done_loader( &loader );