1 /****************************************************************************
5 * CID-keyed Type1 font loader (body).
7 * Copyright (C) 1996-2020 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.
20 #include <freetype/internal/ftdebug.h>
21 #include FT_CONFIG_CONFIG_H
22 #include <freetype/ftmm.h>
23 #include <freetype/internal/t1types.h>
24 #include <freetype/internal/psaux.h>
31 /**************************************************************************
33 * The macro FT_COMPONENT is used in trace mode. It is an implicit
34 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
35 * messages during execution.
38 #define FT_COMPONENT cidload
41 /* read a single offset */
42 FT_LOCAL_DEF( FT_ULong )
43 cid_get_offset( FT_Byte* *start,
50 for ( result = 0; offsize > 0; offsize-- )
61 /*************************************************************************/
62 /*************************************************************************/
64 /***** TYPE 1 SYMBOL PARSING *****/
66 /*************************************************************************/
67 /*************************************************************************/
71 cid_load_keyword( CID_Face face,
73 const T1_Field keyword )
76 CID_Parser* parser = &loader->parser;
79 CID_FaceInfo cid = &face->cid;
82 /* if the keyword has a dedicated callback, call it */
83 if ( keyword->type == T1_FIELD_TYPE_CALLBACK )
85 FT_TRACE4(( " %s", keyword->ident ));
87 keyword->reader( (FT_Face)face, parser );
88 error = parser->root.error;
92 /* we must now compute the address of our target object */
93 switch ( keyword->location )
95 case T1_FIELD_LOCATION_CID_INFO:
96 object = (FT_Byte*)cid;
99 case T1_FIELD_LOCATION_FONT_INFO:
100 object = (FT_Byte*)&cid->font_info;
103 case T1_FIELD_LOCATION_FONT_EXTRA:
104 object = (FT_Byte*)&face->font_extra;
107 case T1_FIELD_LOCATION_BBOX:
108 object = (FT_Byte*)&cid->font_bbox;
116 if ( parser->num_dict < 0 || parser->num_dict >= cid->num_dicts )
118 FT_ERROR(( "cid_load_keyword: invalid use of `%s'\n",
120 error = FT_THROW( Syntax_Error );
124 dict = cid->font_dicts + parser->num_dict;
125 switch ( keyword->location )
127 case T1_FIELD_LOCATION_PRIVATE:
128 object = (FT_Byte*)&dict->private_dict;
132 object = (FT_Byte*)dict;
137 FT_TRACE4(( " %s", keyword->ident ));
139 dummy_object = object;
141 /* now, load the keyword data in the object's field(s) */
142 if ( keyword->type == T1_FIELD_TYPE_INTEGER_ARRAY ||
143 keyword->type == T1_FIELD_TYPE_FIXED_ARRAY )
144 error = cid_parser_load_field_table( &loader->parser, keyword,
147 error = cid_parser_load_field( &loader->parser,
148 keyword, &dummy_object );
157 FT_CALLBACK_DEF( void )
158 cid_parse_font_matrix( CID_Face face,
162 FT_Face root = (FT_Face)&face->root;
167 if ( parser->num_dict >= 0 && parser->num_dict < face->cid.num_dicts )
174 dict = face->cid.font_dicts + parser->num_dict;
175 matrix = &dict->font_matrix;
176 offset = &dict->font_offset;
178 /* input is scaled by 1000 to accommodate default FontMatrix */
179 result = cid_parser_to_fixed_array( parser, 6, temp, 3 );
183 FT_ERROR(( "cid_parse_font_matrix: not enough matrix elements\n" ));
187 FT_TRACE4(( " [%f %f %f %f %f %f]\n",
188 (double)temp[0] / 65536 / 1000,
189 (double)temp[1] / 65536 / 1000,
190 (double)temp[2] / 65536 / 1000,
191 (double)temp[3] / 65536 / 1000,
192 (double)temp[4] / 65536 / 1000,
193 (double)temp[5] / 65536 / 1000 ));
195 temp_scale = FT_ABS( temp[3] );
197 if ( temp_scale == 0 )
199 FT_ERROR(( "cid_parse_font_matrix: invalid font matrix\n" ));
204 if ( temp_scale != 0x10000L )
206 /* set units per EM based on FontMatrix values */
207 root->units_per_EM = (FT_UShort)FT_DivFix( 1000, temp_scale );
209 temp[0] = FT_DivFix( temp[0], temp_scale );
210 temp[1] = FT_DivFix( temp[1], temp_scale );
211 temp[2] = FT_DivFix( temp[2], temp_scale );
212 temp[4] = FT_DivFix( temp[4], temp_scale );
213 temp[5] = FT_DivFix( temp[5], temp_scale );
214 temp[3] = temp[3] < 0 ? -0x10000L : 0x10000L;
217 matrix->xx = temp[0];
218 matrix->yx = temp[1];
219 matrix->xy = temp[2];
220 matrix->yy = temp[3];
222 if ( !FT_Matrix_Check( matrix ) )
224 FT_ERROR(( "t1_parse_font_matrix: invalid font matrix\n" ));
225 parser->root.error = FT_THROW( Invalid_File_Format );
229 /* note that the font offsets are expressed in integer font units */
230 offset->x = temp[4] >> 16;
231 offset->y = temp[5] >> 16;
239 FT_CALLBACK_DEF( void )
240 parse_fd_array( CID_Face face,
243 CID_FaceInfo cid = &face->cid;
244 FT_Memory memory = face->root.memory;
245 FT_Stream stream = parser->stream;
246 FT_Error error = FT_Err_Ok;
250 num_dicts = cid_parser_to_int( parser );
253 FT_ERROR(( "parse_fd_array: invalid number of dictionaries\n" ));
257 FT_TRACE4(( " %ld\n", num_dicts ));
260 * A single entry in the FDArray must (at least) contain the following
261 * structure elements.
263 * %ADOBeginFontDict 18
265 * /FontMatrix [X X X X] 22
266 * /Private X dict begin 22
271 * This needs 18+13+22+22+4+4+16=99 bytes or more. Normally, you also
272 * need a `dup X' at the very beginning and a `put' at the end, so a
273 * rough guess using 100 bytes as the minimum is justified.
275 if ( (FT_ULong)num_dicts > stream->size / 100 )
277 FT_TRACE0(( "parse_fd_array: adjusting FDArray size"
278 " (from %ld to %ld)\n",
280 stream->size / 100 ));
281 num_dicts = (FT_Long)( stream->size / 100 );
284 if ( !cid->font_dicts )
289 if ( FT_NEW_ARRAY( cid->font_dicts, num_dicts ) )
292 cid->num_dicts = num_dicts;
294 /* set some default values (the same as for Type 1 fonts) */
295 for ( n = 0; n < cid->num_dicts; n++ )
297 CID_FaceDict dict = cid->font_dicts + n;
300 dict->private_dict.blue_shift = 7;
301 dict->private_dict.blue_fuzz = 1;
302 dict->private_dict.lenIV = 4;
303 dict->private_dict.expansion_factor = (FT_Fixed)( 0.06 * 0x10000L );
304 dict->private_dict.blue_scale = (FT_Fixed)(
305 0.039625 * 0x10000L * 1000 );
314 /* By mistake, `expansion_factor' appears both in PS_PrivateRec */
315 /* and CID_FaceDictRec (both are public header files and can't */
316 /* changed). We simply copy the value. */
318 FT_CALLBACK_DEF( void )
319 parse_expansion_factor( CID_Face face,
325 if ( parser->num_dict >= 0 && parser->num_dict < face->cid.num_dicts )
327 dict = face->cid.font_dicts + parser->num_dict;
329 dict->expansion_factor = cid_parser_to_fixed( parser, 0 );
330 dict->private_dict.expansion_factor = dict->expansion_factor;
332 FT_TRACE4(( "%ld\n", dict->expansion_factor ));
339 /* By mistake, `CID_FaceDictRec' doesn't contain a field for the */
340 /* `FontName' keyword. FreeType doesn't need it, but it is nice */
341 /* to catch it for producing better trace output. */
343 FT_CALLBACK_DEF( void )
344 parse_font_name( CID_Face face,
347 #ifdef FT_DEBUG_LEVEL_TRACE
348 if ( parser->num_dict >= 0 && parser->num_dict < face->cid.num_dicts )
354 cid_parser_to_token( parser, &token );
356 len = (FT_UInt)( token.limit - token.start );
358 FT_TRACE4(( " %.*s\n", len, token.start ));
360 FT_TRACE4(( " <no value>\n" ));
372 const T1_FieldRec cid_field_records[] =
375 #include "cidtoken.h"
377 T1_FIELD_CALLBACK( "FDArray", parse_fd_array, 0 )
378 T1_FIELD_CALLBACK( "FontMatrix", cid_parse_font_matrix, 0 )
379 T1_FIELD_CALLBACK( "ExpansionFactor", parse_expansion_factor, 0 )
380 T1_FIELD_CALLBACK( "FontName", parse_font_name, 0 )
382 { 0, T1_FIELD_LOCATION_CID_INFO, T1_FIELD_TYPE_NONE, 0, 0, 0, 0, 0, 0 }
387 cid_parse_dict( CID_Face face,
392 CID_Parser* parser = &loader->parser;
395 parser->root.cursor = base;
396 parser->root.limit = base + size;
397 parser->root.error = FT_Err_Ok;
401 FT_Byte* limit = cur + size;
409 parser->root.cursor = cur;
410 cid_parser_skip_spaces( parser );
412 if ( parser->root.cursor >= limit )
413 newlimit = limit - 1 - 17;
415 newlimit = parser->root.cursor - 17;
417 /* look for `%ADOBeginFontDict' */
418 for ( ; cur < newlimit; cur++ )
421 ft_strncmp( (char*)cur, "%ADOBeginFontDict", 17 ) == 0 )
423 /* if /FDArray was found, then cid->num_dicts is > 0, and */
424 /* we can start increasing parser->num_dict */
425 if ( face->cid.num_dicts > 0 )
429 #ifdef FT_DEBUG_LEVEL_TRACE
430 FT_TRACE4(( " FontDict %d", parser->num_dict ));
431 if ( parser->num_dict > face->cid.num_dicts )
432 FT_TRACE4(( " (ignored)" ));
439 cur = parser->root.cursor;
440 /* no error can occur in cid_parser_skip_spaces */
444 cid_parser_skip_PS_token( parser );
445 if ( parser->root.cursor >= limit || parser->root.error )
448 /* look for immediates */
449 if ( *cur == '/' && cur + 2 < limit )
455 len = (FT_UInt)( parser->root.cursor - cur );
457 if ( len > 0 && len < 22 )
459 /* now compare the immediate name to the keyword table */
460 T1_Field keyword = (T1_Field)cid_field_records;
468 name = (FT_Byte*)keyword->ident;
472 if ( cur[0] == name[0] &&
473 len == ft_strlen( (const char*)name ) )
478 for ( n = 1; n < len; n++ )
479 if ( cur[n] != name[n] )
484 /* we found it - run the parsing callback */
485 parser->root.error = cid_load_keyword( face,
488 if ( parser->root.error )
489 return parser->root.error;
498 cur = parser->root.cursor;
501 if ( !face->cid.num_dicts )
503 FT_ERROR(( "cid_parse_dict: No font dictionary found\n" ));
504 return FT_THROW( Invalid_File_Format );
508 return parser->root.error;
512 /* read the subrmap and the subrs of each font dict */
514 cid_read_subrs( CID_Face face )
516 CID_FaceInfo cid = &face->cid;
517 FT_Memory memory = face->root.memory;
518 FT_Stream stream = face->cid_stream;
522 FT_UInt max_offsets = 0;
523 FT_ULong* offsets = NULL;
524 PSAux_Service psaux = (PSAux_Service)face->psaux;
527 if ( FT_NEW_ARRAY( face->subrs, cid->num_dicts ) )
531 for ( n = 0; n < cid->num_dicts; n++, subr++ )
533 CID_FaceDict dict = cid->font_dicts + n;
534 FT_Int lenIV = dict->private_dict.lenIV;
535 FT_UInt count, num_subrs = dict->num_subrs;
543 /* reallocate offsets array if needed */
544 if ( num_subrs + 1 > max_offsets )
546 FT_UInt new_max = FT_PAD_CEIL( num_subrs + 1, 4 );
549 if ( new_max <= max_offsets )
551 error = FT_THROW( Syntax_Error );
555 if ( FT_RENEW_ARRAY( offsets, max_offsets, new_max ) )
558 max_offsets = new_max;
561 /* read the subrmap's offsets */
562 if ( FT_STREAM_SEEK( cid->data_offset + dict->subrmap_offset ) ||
563 FT_FRAME_ENTER( ( num_subrs + 1 ) * (FT_UInt)dict->sd_bytes ) )
566 p = (FT_Byte*)stream->cursor;
567 for ( count = 0; count <= num_subrs; count++ )
568 offsets[count] = cid_get_offset( &p, (FT_Byte)dict->sd_bytes );
572 /* offsets must be ordered */
573 for ( count = 1; count <= num_subrs; count++ )
574 if ( offsets[count - 1] > offsets[count] )
576 FT_ERROR(( "cid_read_subrs: offsets are not ordered\n" ));
577 error = FT_THROW( Invalid_File_Format );
581 if ( offsets[num_subrs] > stream->size - cid->data_offset )
583 FT_ERROR(( "cid_read_subrs: too large `subrs' offsets\n" ));
584 error = FT_THROW( Invalid_File_Format );
588 /* now, compute the size of subrs charstrings, */
589 /* allocate, and read them */
590 data_len = offsets[num_subrs] - offsets[0];
592 if ( FT_NEW_ARRAY( subr->code, num_subrs + 1 ) ||
593 FT_ALLOC( subr->code[0], data_len ) )
596 if ( FT_STREAM_SEEK( cid->data_offset + offsets[0] ) ||
597 FT_STREAM_READ( subr->code[0], data_len ) )
600 /* set up pointers */
601 for ( count = 1; count <= num_subrs; count++ )
606 len = offsets[count] - offsets[count - 1];
607 subr->code[count] = subr->code[count - 1] + len;
610 /* decrypt subroutines, but only if lenIV >= 0 */
613 for ( count = 0; count < num_subrs; count++ )
618 len = offsets[count + 1] - offsets[count];
619 psaux->t1_decrypt( subr->code[count], len, 4330 );
623 subr->num_subrs = (FT_Int)num_subrs;
633 for ( n = 0; n < cid->num_dicts; n++ )
635 if ( face->subrs[n].code )
636 FT_FREE( face->subrs[n].code[0] );
638 FT_FREE( face->subrs[n].code );
640 FT_FREE( face->subrs );
647 cid_init_loader( CID_Loader* loader,
657 cid_done_loader( CID_Loader* loader )
659 CID_Parser* parser = &loader->parser;
662 /* finalize parser */
663 cid_parser_done( parser );
668 cid_hex_to_binary( FT_Byte* data,
673 FT_Stream stream = face->root.stream;
681 FT_Bool upper_nibble, done;
684 if ( FT_STREAM_SEEK( offset ) )
688 dlimit = d + data_len;
699 FT_ULong oldpos = FT_STREAM_POS();
700 FT_ULong size = stream->size - oldpos;
705 error = FT_THROW( Syntax_Error );
709 if ( FT_STREAM_READ( buffer, 256 > size ? size : 256 ) )
712 plimit = p + FT_STREAM_POS() - oldpos;
715 if ( ft_isdigit( *p ) )
716 val = (FT_Byte)( *p - '0' );
717 else if ( *p >= 'a' && *p <= 'f' )
718 val = (FT_Byte)( *p - 'a' + 10 );
719 else if ( *p >= 'A' && *p <= 'F' )
720 val = (FT_Byte)( *p - 'A' + 10 );
721 else if ( *p == ' ' ||
731 else if ( *p == '>' )
738 error = FT_THROW( Syntax_Error );
743 *d = (FT_Byte)( val << 4 );
746 *d = (FT_Byte)( *d + val );
750 upper_nibble = (FT_Byte)( 1 - upper_nibble );
765 FT_LOCAL_DEF( FT_Error )
766 cid_face_open( CID_Face face,
771 FT_Memory memory = face->root.memory;
775 CID_FaceInfo cid = &face->cid;
777 FT_ULong binary_length;
781 cid_init_loader( &loader, face );
783 parser = &loader.parser;
784 error = cid_parser_new( parser, face->root.stream, face->root.memory,
785 (PSAux_Service)face->psaux );
789 error = cid_parse_dict( face, &loader,
791 parser->postscript_len );
795 if ( face_index < 0 )
798 if ( FT_NEW( face->cid_stream ) )
801 if ( parser->binary_length )
803 if ( parser->binary_length >
804 face->root.stream->size - parser->data_offset )
806 FT_TRACE0(( "cid_face_open: adjusting length of binary data\n"
807 " (from %ld to %ld bytes)\n",
808 parser->binary_length,
809 face->root.stream->size - parser->data_offset ));
810 parser->binary_length = face->root.stream->size -
814 /* we must convert the data section from hexadecimal to binary */
815 if ( FT_ALLOC( face->binary_data, parser->binary_length ) ||
816 FT_SET_ERROR( cid_hex_to_binary( face->binary_data,
817 parser->binary_length,
822 FT_Stream_OpenMemory( face->cid_stream,
823 face->binary_data, parser->binary_length );
824 cid->data_offset = 0;
828 *face->cid_stream = *face->root.stream;
829 cid->data_offset = loader.parser.data_offset;
834 if ( cid->fd_bytes < 0 || cid->gd_bytes < 1 )
836 FT_ERROR(( "cid_face_open:"
837 " Invalid `FDBytes' or `GDBytes' value\n" ));
838 error = FT_THROW( Invalid_File_Format );
842 /* allow at most 32bit offsets */
843 if ( cid->fd_bytes > 4 || cid->gd_bytes > 4 )
845 FT_ERROR(( "cid_face_open:"
846 " Values of `FDBytes' or `GDBytes' larger than 4\n"
848 " are not supported\n" ));
849 error = FT_THROW( Invalid_File_Format );
853 binary_length = face->cid_stream->size - cid->data_offset;
854 entry_len = (FT_ULong)( cid->fd_bytes + cid->gd_bytes );
856 for ( n = 0; n < cid->num_dicts; n++ )
858 CID_FaceDict dict = cid->font_dicts + n;
861 /* the upper limits are ad-hoc values */
862 if ( dict->private_dict.blue_shift > 1000 ||
863 dict->private_dict.blue_shift < 0 )
865 FT_TRACE2(( "cid_face_open:"
866 " setting unlikely BlueShift value %d to default (7)\n",
867 dict->private_dict.blue_shift ));
868 dict->private_dict.blue_shift = 7;
871 if ( dict->private_dict.blue_fuzz > 1000 ||
872 dict->private_dict.blue_fuzz < 0 )
874 FT_TRACE2(( "cid_face_open:"
875 " setting unlikely BlueFuzz value %d to default (1)\n",
876 dict->private_dict.blue_fuzz ));
877 dict->private_dict.blue_fuzz = 1;
880 if ( dict->sd_bytes < 0 ||
881 ( dict->num_subrs && dict->sd_bytes < 1 ) )
883 FT_ERROR(( "cid_face_open: Invalid `SDBytes' value\n" ));
884 error = FT_THROW( Invalid_File_Format );
888 if ( dict->sd_bytes > 4 )
890 FT_ERROR(( "cid_face_open:"
891 " Values of `SDBytes' larger than 4"
892 " are not supported\n" ));
893 error = FT_THROW( Invalid_File_Format );
897 if ( dict->subrmap_offset > binary_length )
899 FT_ERROR(( "cid_face_open: Invalid `SubrMapOffset' value\n" ));
900 error = FT_THROW( Invalid_File_Format );
904 /* `num_subrs' is scanned as a signed integer */
905 if ( (FT_Int)dict->num_subrs < 0 ||
907 dict->num_subrs > ( binary_length - dict->subrmap_offset ) /
908 (FT_UInt)dict->sd_bytes ) )
910 FT_ERROR(( "cid_face_open: Invalid `SubrCount' value\n" ));
911 error = FT_THROW( Invalid_File_Format );
916 if ( cid->cidmap_offset > binary_length )
918 FT_ERROR(( "cid_face_open: Invalid `CIDMapOffset' value\n" ));
919 error = FT_THROW( Invalid_File_Format );
925 ( binary_length - cid->cidmap_offset ) / entry_len )
927 FT_ERROR(( "cid_face_open: Invalid `CIDCount' value\n" ));
928 error = FT_THROW( Invalid_File_Format );
932 /* we can now safely proceed */
933 error = cid_read_subrs( face );
936 cid_done_loader( &loader );