1 /***************************************************************************/
5 /* CID-keyed Type1 font loader (body). */
7 /* Copyright 1996-2016 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_ULong )
42 cid_get_offset( FT_Byte* *start,
49 for ( result = 0; offsize > 0; offsize-- )
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 /* input is scaled by 1000 to accommodate default FontMatrix */
171 result = cid_parser_to_fixed_array( parser, 6, temp, 3 );
174 return FT_THROW( Invalid_File_Format );
176 temp_scale = FT_ABS( temp[3] );
178 if ( temp_scale == 0 )
180 FT_ERROR(( "cid_parse_font_matrix: invalid font matrix\n" ));
181 return FT_THROW( Invalid_File_Format );
185 if ( temp_scale != 0x10000L )
187 /* set units per EM based on FontMatrix values */
188 root->units_per_EM = (FT_UShort)FT_DivFix( 1000, temp_scale );
190 temp[0] = FT_DivFix( temp[0], temp_scale );
191 temp[1] = FT_DivFix( temp[1], temp_scale );
192 temp[2] = FT_DivFix( temp[2], temp_scale );
193 temp[4] = FT_DivFix( temp[4], temp_scale );
194 temp[5] = FT_DivFix( temp[5], temp_scale );
195 temp[3] = temp[3] < 0 ? -0x10000L : 0x10000L;
198 matrix->xx = temp[0];
199 matrix->yx = temp[1];
200 matrix->xy = temp[2];
201 matrix->yy = temp[3];
203 /* note that the font offsets are expressed in integer font units */
204 offset->x = temp[4] >> 16;
205 offset->y = temp[5] >> 16;
212 FT_CALLBACK_DEF( FT_Error )
213 parse_fd_array( CID_Face face,
216 CID_FaceInfo cid = &face->cid;
217 FT_Memory memory = face->root.memory;
218 FT_Stream stream = parser->stream;
219 FT_Error error = FT_Err_Ok;
223 num_dicts = cid_parser_to_int( parser );
226 FT_ERROR(( "parse_fd_array: invalid number of dictionaries\n" ));
227 error = FT_THROW( Invalid_File_Format );
232 * A single entry in the FDArray must (at least) contain the following
233 * structure elements.
235 * %ADOBeginFontDict 18
237 * /FontMatrix [X X X X] 22
238 * /Private X dict begin 22
243 * This needs 18+13+22+22+4+4+16=99 bytes or more. Normally, you also
244 * need a `dup X' at the very beginning and a `put' at the end, so a
245 * rough guess using 100 bytes as the minimum is justified.
247 if ( (FT_ULong)num_dicts > stream->size / 100 )
249 FT_TRACE0(( "parse_fd_array: adjusting FDArray size"
250 " (from %d to %d)\n",
252 stream->size / 100 ));
253 num_dicts = (FT_Long)( stream->size / 100 );
256 if ( !cid->font_dicts )
261 if ( FT_NEW_ARRAY( cid->font_dicts, num_dicts ) )
264 cid->num_dicts = num_dicts;
266 /* don't forget to set a few defaults */
267 for ( n = 0; n < cid->num_dicts; n++ )
269 CID_FaceDict dict = cid->font_dicts + n;
272 /* default value for lenIV */
273 dict->private_dict.lenIV = 4;
282 /* by mistake, `expansion_factor' appears both in PS_PrivateRec */
283 /* and CID_FaceDictRec (both are public header files and can't */
284 /* changed); we simply copy the value */
286 FT_CALLBACK_DEF( FT_Error )
287 parse_expansion_factor( CID_Face face,
293 if ( parser->num_dict >= 0 && parser->num_dict < face->cid.num_dicts )
295 dict = face->cid.font_dicts + parser->num_dict;
297 dict->expansion_factor = cid_parser_to_fixed( parser, 0 );
298 dict->private_dict.expansion_factor = dict->expansion_factor;
306 const T1_FieldRec cid_field_records[] =
309 #include "cidtoken.h"
311 T1_FIELD_CALLBACK( "FDArray", parse_fd_array, 0 )
312 T1_FIELD_CALLBACK( "FontMatrix", cid_parse_font_matrix, 0 )
313 T1_FIELD_CALLBACK( "ExpansionFactor", parse_expansion_factor, 0 )
315 { 0, T1_FIELD_LOCATION_CID_INFO, T1_FIELD_TYPE_NONE, 0, 0, 0, 0, 0, 0 }
320 cid_parse_dict( CID_Face face,
325 CID_Parser* parser = &loader->parser;
328 parser->root.cursor = base;
329 parser->root.limit = base + size;
330 parser->root.error = FT_Err_Ok;
334 FT_Byte* limit = cur + size;
342 parser->root.cursor = cur;
343 cid_parser_skip_spaces( parser );
345 if ( parser->root.cursor >= limit )
346 newlimit = limit - 1 - 17;
348 newlimit = parser->root.cursor - 17;
350 /* look for `%ADOBeginFontDict' */
351 for ( ; cur < newlimit; cur++ )
354 ft_strncmp( (char*)cur, "%ADOBeginFontDict", 17 ) == 0 )
356 /* if /FDArray was found, then cid->num_dicts is > 0, and */
357 /* we can start increasing parser->num_dict */
358 if ( face->cid.num_dicts > 0 )
363 cur = parser->root.cursor;
364 /* no error can occur in cid_parser_skip_spaces */
368 cid_parser_skip_PS_token( parser );
369 if ( parser->root.cursor >= limit || parser->root.error )
372 /* look for immediates */
373 if ( *cur == '/' && cur + 2 < limit )
379 len = (FT_UInt)( parser->root.cursor - cur );
381 if ( len > 0 && len < 22 )
383 /* now compare the immediate name to the keyword table */
384 T1_Field keyword = (T1_Field)cid_field_records;
392 name = (FT_Byte*)keyword->ident;
396 if ( cur[0] == name[0] &&
397 len == ft_strlen( (const char*)name ) )
402 for ( n = 1; n < len; n++ )
403 if ( cur[n] != name[n] )
408 /* we found it - run the parsing callback */
409 parser->root.error = cid_load_keyword( face,
412 if ( parser->root.error )
413 return parser->root.error;
422 cur = parser->root.cursor;
425 if ( !face->cid.num_dicts )
427 FT_ERROR(( "cid_parse_dict: No font dictionary found\n" ));
428 return FT_THROW( Invalid_File_Format );
432 return parser->root.error;
436 /* read the subrmap and the subrs of each font dict */
438 cid_read_subrs( CID_Face face )
440 CID_FaceInfo cid = &face->cid;
441 FT_Memory memory = face->root.memory;
442 FT_Stream stream = face->cid_stream;
446 FT_UInt max_offsets = 0;
447 FT_ULong* offsets = NULL;
448 PSAux_Service psaux = (PSAux_Service)face->psaux;
451 if ( FT_NEW_ARRAY( face->subrs, cid->num_dicts ) )
455 for ( n = 0; n < cid->num_dicts; n++, subr++ )
457 CID_FaceDict dict = cid->font_dicts + n;
458 FT_Int lenIV = dict->private_dict.lenIV;
459 FT_UInt count, num_subrs = dict->num_subrs;
464 /* reallocate offsets array if needed */
465 if ( num_subrs + 1 > max_offsets )
467 FT_UInt new_max = FT_PAD_CEIL( num_subrs + 1, 4 );
470 if ( new_max <= max_offsets )
472 error = FT_THROW( Syntax_Error );
476 if ( FT_RENEW_ARRAY( offsets, max_offsets, new_max ) )
479 max_offsets = new_max;
482 /* read the subrmap's offsets */
483 if ( FT_STREAM_SEEK( cid->data_offset + dict->subrmap_offset ) ||
484 FT_FRAME_ENTER( ( num_subrs + 1 ) * (FT_UInt)dict->sd_bytes ) )
487 p = (FT_Byte*)stream->cursor;
488 for ( count = 0; count <= num_subrs; count++ )
489 offsets[count] = cid_get_offset( &p, (FT_Byte)dict->sd_bytes );
493 /* offsets must be ordered */
494 for ( count = 1; count <= num_subrs; count++ )
495 if ( offsets[count - 1] > offsets[count] )
497 FT_ERROR(( "cid_read_subrs: offsets are not ordered\n" ));
498 error = FT_THROW( Invalid_File_Format );
502 if ( offsets[num_subrs] > stream->size - cid->data_offset )
504 FT_ERROR(( "cid_read_subrs: too large `subrs' offsets\n" ));
505 error = FT_THROW( Invalid_File_Format );
509 /* now, compute the size of subrs charstrings, */
510 /* allocate, and read them */
511 data_len = offsets[num_subrs] - offsets[0];
513 if ( FT_NEW_ARRAY( subr->code, num_subrs + 1 ) ||
514 FT_ALLOC( subr->code[0], data_len ) )
517 if ( FT_STREAM_SEEK( cid->data_offset + offsets[0] ) ||
518 FT_STREAM_READ( subr->code[0], data_len ) )
521 /* set up pointers */
522 for ( count = 1; count <= num_subrs; count++ )
527 len = offsets[count] - offsets[count - 1];
528 subr->code[count] = subr->code[count - 1] + len;
531 /* decrypt subroutines, but only if lenIV >= 0 */
534 for ( count = 0; count < num_subrs; count++ )
539 len = offsets[count + 1] - offsets[count];
540 psaux->t1_decrypt( subr->code[count], len, 4330 );
544 subr->num_subrs = (FT_Int)num_subrs;
554 for ( n = 0; n < cid->num_dicts; n++ )
556 if ( face->subrs[n].code )
557 FT_FREE( face->subrs[n].code[0] );
559 FT_FREE( face->subrs[n].code );
561 FT_FREE( face->subrs );
568 cid_init_loader( CID_Loader* loader,
573 FT_MEM_ZERO( loader, sizeof ( *loader ) );
578 cid_done_loader( CID_Loader* loader )
580 CID_Parser* parser = &loader->parser;
583 /* finalize parser */
584 cid_parser_done( parser );
589 cid_hex_to_binary( FT_Byte* data,
594 FT_Stream stream = face->root.stream;
602 FT_Bool upper_nibble, done;
605 if ( FT_STREAM_SEEK( offset ) )
609 dlimit = d + data_len;
620 FT_ULong oldpos = FT_STREAM_POS();
621 FT_ULong size = stream->size - oldpos;
626 error = FT_THROW( Syntax_Error );
630 if ( FT_STREAM_READ( buffer, 256 > size ? size : 256 ) )
633 plimit = p + FT_STREAM_POS() - oldpos;
636 if ( ft_isdigit( *p ) )
637 val = (FT_Byte)( *p - '0' );
638 else if ( *p >= 'a' && *p <= 'f' )
639 val = (FT_Byte)( *p - 'a' );
640 else if ( *p >= 'A' && *p <= 'F' )
641 val = (FT_Byte)( *p - 'A' + 10 );
642 else if ( *p == ' ' ||
652 else if ( *p == '>' )
659 error = FT_THROW( Syntax_Error );
664 *d = (FT_Byte)( val << 4 );
667 *d = (FT_Byte)( *d + val );
671 upper_nibble = (FT_Byte)( 1 - upper_nibble );
686 FT_LOCAL_DEF( FT_Error )
687 cid_face_open( CID_Face face,
692 FT_Memory memory = face->root.memory;
696 CID_FaceInfo cid = &face->cid;
698 FT_ULong binary_length;
702 cid_init_loader( &loader, face );
704 parser = &loader.parser;
705 error = cid_parser_new( parser, face->root.stream, face->root.memory,
706 (PSAux_Service)face->psaux );
710 error = cid_parse_dict( face, &loader,
712 parser->postscript_len );
716 if ( face_index < 0 )
719 if ( FT_NEW( face->cid_stream ) )
722 if ( parser->binary_length )
724 if ( parser->binary_length >
725 face->root.stream->size - parser->data_offset )
727 FT_TRACE0(( "cid_face_open: adjusting length of binary data\n"
728 " (from %d to %d bytes)\n",
729 parser->binary_length,
730 face->root.stream->size - parser->data_offset ));
731 parser->binary_length = face->root.stream->size -
735 /* we must convert the data section from hexadecimal to binary */
736 if ( FT_ALLOC( face->binary_data, parser->binary_length ) ||
737 cid_hex_to_binary( face->binary_data, parser->binary_length,
738 parser->data_offset, face ) )
741 FT_Stream_OpenMemory( face->cid_stream,
742 face->binary_data, parser->binary_length );
743 cid->data_offset = 0;
747 *face->cid_stream = *face->root.stream;
748 cid->data_offset = loader.parser.data_offset;
753 if ( cid->fd_bytes < 0 || cid->gd_bytes < 1 )
755 FT_ERROR(( "cid_parse_dict:"
756 " Invalid `FDBytes' or `GDBytes' value\n" ));
757 error = FT_THROW( Invalid_File_Format );
761 /* allow at most 32bit offsets */
762 if ( cid->fd_bytes > 4 || cid->gd_bytes > 4 )
764 FT_ERROR(( "cid_parse_dict:"
765 " Values of `FDBytes' or `GDBytes' larger than 4\n"
767 " are not supported\n" ));
768 error = FT_THROW( Invalid_File_Format );
772 binary_length = face->cid_stream->size - cid->data_offset;
773 entry_len = (FT_ULong)( cid->fd_bytes + cid->gd_bytes );
775 for ( n = 0; n < cid->num_dicts; n++ )
777 CID_FaceDict dict = cid->font_dicts + n;
780 if ( dict->sd_bytes < 0 )
782 FT_ERROR(( "cid_parse_dict: Invalid `SDBytes' value\n" ));
783 error = FT_THROW( Invalid_File_Format );
787 if ( dict->sd_bytes > 4 )
789 FT_ERROR(( "cid_parse_dict:"
790 " Values of `SDBytes' larger than 4"
791 " are not supported\n" ));
792 error = FT_THROW( Invalid_File_Format );
796 if ( dict->subrmap_offset > binary_length )
798 FT_ERROR(( "cid_parse_dict: Invalid `SubrMapOffset' value\n" ));
799 error = FT_THROW( Invalid_File_Format );
803 /* `num_subrs' is scanned as a signed integer */
804 if ( (FT_Int)dict->num_subrs < 0 ||
806 dict->num_subrs > ( binary_length - dict->subrmap_offset ) /
807 (FT_UInt)dict->sd_bytes ) )
809 FT_ERROR(( "cid_parse_dict: Invalid `SubrCount' value\n" ));
810 error = FT_THROW( Invalid_File_Format );
815 if ( cid->cidmap_offset > binary_length )
817 FT_ERROR(( "cid_parse_dict: Invalid `CIDMapOffset' value\n" ));
818 error = FT_THROW( Invalid_File_Format );
824 ( binary_length - cid->cidmap_offset ) / entry_len )
826 FT_ERROR(( "cid_parse_dict: Invalid `CIDCount' value\n" ));
827 error = FT_THROW( Invalid_File_Format );
831 /* we can now safely proceed */
832 error = cid_read_subrs( face );
835 cid_done_loader( &loader );