1 /***************************************************************************/
5 /* OpenType and CFF data/program tables loader (body) */
7 /* Copyright 1996-2001 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
23 #include FT_INTERNAL_POSTSCRIPT_NAMES_H
24 #include FT_TRUETYPE_TAGS_H
32 /*************************************************************************/
34 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
35 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
36 /* messages during execution. */
39 #define FT_COMPONENT trace_cffload
41 /* read a CFF offset from memory */
43 cff_get_offset( FT_Byte* p,
49 for ( result = 0; off_size > 0; off_size-- )
60 cff_new_index( CFF_Index* index,
65 FT_Memory memory = stream->memory;
69 MEM_Set( index, 0, sizeof ( *index ) );
71 index->stream = stream;
72 if ( !READ_UShort( count ) &&
81 /* there is at least one element; read the offset size, */
82 /* then access the offset table to compute the index's total size */
83 if ( READ_Byte( offsize ) )
86 index->stream = stream;
88 index->off_size = offsize;
89 data_size = (FT_ULong)( count + 1 ) * offsize;
91 if ( ALLOC_ARRAY( index->offsets, count + 1, FT_ULong ) ||
92 ACCESS_Frame( data_size ) )
95 poff = index->offsets;
96 p = (FT_Byte*)stream->cursor;
98 for ( ; (FT_Short)count >= 0; count-- )
100 poff[0] = cff_get_offset( p, offsize );
107 index->data_offset = FILE_Pos();
108 data_size = poff[-1] - 1;
113 if ( EXTRACT_Frame( data_size, index->bytes ) )
119 if ( FILE_Skip( data_size ) )
126 FREE( index->offsets );
133 cff_done_index( CFF_Index* index )
137 FT_Stream stream = index->stream;
138 FT_Memory memory = stream->memory;
142 RELEASE_Frame( index->bytes );
144 FREE( index->offsets );
145 MEM_Set( index, 0, sizeof ( *index ) );
151 cff_explicit_index( CFF_Index* index,
155 FT_Memory memory = index->stream->memory;
156 FT_UInt n, offset, old_offset;
162 if ( index->count > 0 && !ALLOC_ARRAY( t, index->count + 1, FT_Byte* ) )
165 for ( n = 0; n <= index->count; n++ )
167 offset = index->offsets[n];
171 t[n] = index->bytes + offset - 1;
182 FT_LOCAL_DEF FT_Error
183 CFF_Access_Element( CFF_Index* index,
186 FT_ULong* pbyte_len )
191 if ( index && index->count > element )
193 /* compute start and end offsets */
194 FT_ULong off1, off2 = 0;
197 off1 = index->offsets[element];
203 off2 = index->offsets[element];
205 } while ( off2 == 0 && element < index->count );
214 *pbyte_len = off2 - off1;
218 /* this index was completely loaded in memory, that's easy */
219 *pbytes = index->bytes + off1 - 1;
223 /* this index is still on disk/file, access it through a frame */
224 FT_Stream stream = index->stream;
227 if ( FILE_Seek( index->data_offset + off1 - 1 ) ||
228 EXTRACT_Frame( off2 - off1, *pbytes ) )
234 /* empty index element */
240 error = CFF_Err_Invalid_Argument;
248 CFF_Forget_Element( CFF_Index* index,
251 if ( index->bytes == 0 )
253 FT_Stream stream = index->stream;
256 RELEASE_Frame( *pbytes );
261 FT_LOCAL_DEF FT_String*
262 CFF_Get_Name( CFF_Index* index,
265 FT_Memory memory = index->stream->memory;
272 error = CFF_Access_Element( index, element, &bytes, &byte_len );
276 if ( !ALLOC( name, byte_len + 1 ) )
278 MEM_Copy( name, bytes, byte_len );
281 CFF_Forget_Element( index, &bytes );
288 FT_LOCAL_DEF FT_String*
289 CFF_Get_String( CFF_Index* index,
291 PSNames_Interface* interface )
293 /* if it is not a standard string, return it */
295 return CFF_Get_Name( index, sid - 391 );
297 /* that's a standard string, fetch a copy from the PSName module */
300 const char* adobe_name = interface->adobe_std_strings( sid );
306 FT_Memory memory = index->stream->memory;
310 len = (FT_UInt)strlen( adobe_name );
311 if ( !ALLOC( name, len + 1 ) )
313 MEM_Copy( name, adobe_name, len );
323 /*************************************************************************/
324 /*************************************************************************/
326 /*** FD Select table support ***/
328 /*************************************************************************/
329 /*************************************************************************/
333 CFF_Done_FD_Select( CFF_FD_Select* select,
337 RELEASE_Frame( select->data );
339 select->data_size = 0;
341 select->range_count = 0;
346 CFF_Load_FD_Select( CFF_FD_Select* select,
357 if ( FILE_Seek( offset ) || READ_Byte( format ) )
360 select->format = format;
361 select->cache_count = 0; /* clear cache */
365 case 0: /* format 0, that's simple */
366 select->data_size = num_glyphs;
369 case 3: /* format 3, a tad more complex */
370 if ( READ_UShort( num_ranges ) )
373 select->data_size = num_ranges * 3 + 2;
376 if ( EXTRACT_Frame( select->data_size, select->data ) )
380 default: /* hmm... that's wrong */
381 error = CFF_Err_Invalid_File_Format;
390 CFF_Get_FD( CFF_FD_Select* select,
391 FT_UInt glyph_index )
396 switch ( select->format )
399 fd = select->data[glyph_index];
403 /* first, compare to cache */
404 if ( (FT_UInt)(glyph_index-select->cache_first) < select->cache_count )
406 fd = select->cache_fd;
410 /* then, lookup the ranges array */
412 FT_Byte* p = select->data;
413 FT_Byte* p_limit = p + select->data_size;
415 FT_UInt first, limit;
418 first = NEXT_UShort( p );
421 if ( glyph_index < first )
425 limit = NEXT_UShort( p );
427 if ( glyph_index < limit )
432 select->cache_first = first;
433 select->cache_count = limit-first;
434 select->cache_fd = fd2;
439 } while ( p < p_limit );
451 /*************************************************************************/
452 /*************************************************************************/
454 /*** CFF font support ***/
456 /*************************************************************************/
457 /*************************************************************************/
460 CFF_Done_Encoding( CFF_Encoding* encoding,
463 FT_Memory memory = stream->memory;
466 FREE( encoding->codes );
467 FREE( encoding->sids );
468 encoding->format = 0;
469 encoding->offset = 0;
476 CFF_Done_Charset( CFF_Charset* charset,
479 FT_Memory memory = stream->memory;
482 FREE( charset->sids );
490 CFF_Load_Charset( CFF_Charset* charset,
493 FT_ULong base_offset,
496 FT_Memory memory = stream->memory;
501 charset->offset = base_offset + offset;
503 /* Get the format of the table. */
504 if ( FILE_Seek( charset->offset ) ||
505 READ_Byte( charset->format ) )
508 /* If the the offset is greater than 2, we have to parse the */
515 /* Allocate memory for sids. */
516 if ( ALLOC( charset->sids, num_glyphs * sizeof ( FT_UShort ) ) )
519 /* assign the .notdef glyph */
520 charset->sids[0] = 0;
522 switch ( charset->format )
525 for ( j = 1; j < num_glyphs; j++ )
527 if ( READ_UShort( glyph_sid ) )
530 charset->sids[j] = glyph_sid;
543 while ( j < num_glyphs )
546 /* Read the first glyph sid of the range. */
547 if ( READ_UShort( glyph_sid ) )
550 /* Read the number of glyphs in the range. */
551 if ( charset->format == 2 )
553 if ( READ_UShort( nleft ) )
558 if ( READ_Byte( nleft ) )
562 /* Fill in the range of sids -- `nleft + 1' glyphs. */
563 for ( i = 0; i <= nleft; i++, j++, glyph_sid++ )
564 charset->sids[j] = glyph_sid;
570 FT_ERROR(( "CFF_Load_Charset: invalid table format!\n" ));
571 error = CFF_Err_Invalid_File_Format;
577 /* Parse default tables corresponding to offset == 0, 1, or 2. */
578 /* CFF specification intimates the following: */
580 /* In order to use a predefined charset, the following must be */
581 /* true: The charset constructed for the glyphs in the font's */
582 /* charstrings dictionary must match the predefined charset in */
583 /* the first num_glyphs, and hence must match the predefined */
584 /* charset *exactly*. */
589 if ( num_glyphs != 229 )
591 FT_ERROR(("CFF_Load_Charset: implicit charset not equal to\n"
592 "predefined charset (Adobe ISO-Latin)!\n" ));
593 error = CFF_Err_Invalid_File_Format;
597 /* Allocate memory for sids. */
598 if ( ALLOC( charset->sids, num_glyphs * sizeof ( FT_UShort ) ) )
601 /* Copy the predefined charset into the allocated memory. */
602 MEM_Copy( charset->sids, cff_isoadobe_charset,
603 num_glyphs * sizeof ( FT_UShort ) );
608 if ( num_glyphs != 166 )
610 FT_ERROR(( "CFF_Load_Charset: implicit charset not equal to\n"
611 "predefined charset (Adobe Expert)!\n" ));
612 error = CFF_Err_Invalid_File_Format;
616 /* Allocate memory for sids. */
617 if ( ALLOC( charset->sids, num_glyphs * sizeof ( FT_UShort ) ) )
620 /* Copy the predefined charset into the allocated memory. */
621 MEM_Copy( charset->sids, cff_expert_charset,
622 num_glyphs * sizeof ( FT_UShort ) );
627 if ( num_glyphs != 87 )
629 FT_ERROR(( "CFF_Load_Charset: implicit charset not equal to\n"
630 "predefined charset (Adobe Expert Subset)!\n" ));
631 error = CFF_Err_Invalid_File_Format;
635 /* Allocate memory for sids. */
636 if ( ALLOC( charset->sids, num_glyphs * sizeof ( FT_UShort ) ) )
639 /* Copy the predefined charset into the allocated memory. */
640 MEM_Copy( charset->sids, cff_expertsubset_charset,
641 num_glyphs * sizeof ( FT_UShort ) );
646 error = CFF_Err_Invalid_File_Format;
653 /* Clean up if there was an error. */
658 FREE( charset->sids );
669 CFF_Load_Encoding( CFF_Encoding* encoding,
670 CFF_Charset* charset,
673 FT_ULong base_offset,
676 FT_Memory memory = stream->memory;
684 /* Check for charset->sids. If we do not have this, we fail. */
685 if ( !charset->sids )
687 error = CFF_Err_Invalid_File_Format;
691 /* Allocate memory for sids/codes -- there are at most 256 sids/codes */
692 /* for an encoding. */
693 if ( ALLOC( encoding->sids, 256 * sizeof ( FT_UShort ) ) ||
694 ALLOC( encoding->codes, 256 * sizeof ( FT_UShort ) ) )
697 /* Zero out the code to gid/sid mappings. */
698 for ( j = 0; j < 255; j++ )
700 encoding->sids [j] = 0;
701 encoding->codes[j] = 0;
704 /* Note: The encoding table in a CFF font is indexed by glyph index, */
705 /* where the first encoded glyph index is 1. Hence, we read the char */
706 /* code (`glyph_code') at index j and make the assignment: */
708 /* encoding->codes[glyph_code] = j + 1 */
710 /* We also make the assignment: */
712 /* encoding->sids[glyph_code] = charset->sids[j + 1] */
714 /* This gives us both a code to GID and a code to SID mapping. */
719 encoding->offset = base_offset + offset;
721 /* we need to parse the table to determine its size */
722 if ( FILE_Seek( encoding->offset ) ||
723 READ_Byte( encoding->format ) ||
727 switch ( encoding->format & 0x7F )
730 for ( j = 1; j <= count; j++ )
732 if ( READ_Byte( glyph_code ) )
735 /* Make sure j is not too big. */
736 if ( j > num_glyphs )
739 /* Assign code to GID mapping. */
740 encoding->codes[glyph_code] = (FT_UShort)j;
742 /* Assign code to SID mapping. */
743 encoding->sids[glyph_code] = charset->sids[j];
755 /* Parse the Format1 ranges. */
756 for ( j = 0; j < count; j++, i += nleft )
758 /* Read the first glyph code of the range. */
759 if ( READ_Byte( glyph_code ) )
762 /* Read the number of codes in the range. */
763 if ( READ_Byte( nleft ) )
766 /* Increment nleft, so we read `nleft + 1' codes/sids. */
769 /* Fill in the range of codes/sids. */
770 for ( k = i; k < nleft + i; k++, glyph_code++ )
772 /* Make sure k is not too big. */
773 if ( k > num_glyphs )
776 /* Assign code to GID mapping. */
777 encoding->codes[glyph_code] = (FT_UShort)k;
779 /* Assign code to SID mapping. */
780 encoding->sids[glyph_code] = charset->sids[k];
787 FT_ERROR(( "CFF_Load_Encoding: invalid table format!\n" ));
788 error = CFF_Err_Invalid_File_Format;
792 /* Parse supplemental encodings, if any. */
793 if ( encoding->format & 0x80 )
798 /* count supplements */
799 if ( READ_Byte( count ) )
802 for ( j = 0; j < count; j++ )
804 /* Read supplemental glyph code. */
805 if ( READ_Byte( glyph_code ) )
808 /* Read the SID associated with this glyph code. */
809 if ( READ_UShort( glyph_sid ) )
812 /* Assign code to SID mapping. */
813 encoding->sids[glyph_code] = glyph_sid;
815 /* First, lookup GID which has been assigned to */
817 for ( glyph_id = 0; glyph_id < num_glyphs; glyph_id++ )
819 if ( charset->sids[glyph_id] == glyph_sid )
823 /* Now, make the assignment. */
824 encoding->codes[glyph_code] = (FT_UShort)glyph_id;
833 /* We take into account the fact a CFF font can use a predefined */
834 /* encoding without containing all of the glyphs encoded by this */
835 /* encoding (see the note at the end of section 12 in the CFF */
836 /* specification). */
841 /* First, copy the code to SID mapping. */
842 MEM_Copy( encoding->sids, cff_standard_encoding,
843 256 * sizeof ( FT_UShort ) );
845 /* Construct code to GID mapping from code */
846 /* to SID mapping and charset. */
847 for ( j = 0; j < 256; j++ )
849 /* If j is encoded, find the GID for it. */
850 if ( encoding->sids[j] )
852 for ( i = 1; i < num_glyphs; i++ )
853 /* We matched, so break. */
854 if ( charset->sids[i] == encoding->sids[j] )
857 /* i will be equal to num_glyphs if we exited the above */
858 /* loop without a match. In this case, we also have to */
859 /* fix the code to SID mapping. */
860 if ( i == num_glyphs )
862 encoding->codes[j] = 0;
863 encoding->sids [j] = 0;
866 encoding->codes[j] = (FT_UShort)i;
872 /* First, copy the code to SID mapping. */
873 MEM_Copy( encoding->sids, cff_expert_encoding,
874 256 * sizeof ( FT_UShort ) );
876 /* Construct code to GID mapping from code to SID mapping */
878 for ( j = 0; j < 256; j++ )
880 /* If j is encoded, find the GID for it. */
881 if ( encoding->sids[j] )
883 for ( i = 1; i < num_glyphs; i++ )
884 /* We matched, so break. */
885 if ( charset->sids[i] == encoding->sids[j] )
888 /* i will be equal to num_glyphs if we exited the above */
889 /* loop without a match. In this case, we also have to */
890 /* fix the code to SID mapping. */
891 if ( i == num_glyphs )
893 encoding->codes[j] = 0;
894 encoding->sids [j] = 0;
897 encoding->codes[j] = (FT_UShort)i;
903 FT_ERROR(( "CFF_Load_Encoding: invalid table format!\n" ));
904 error = CFF_Err_Invalid_File_Format;
911 /* Clean up if there was an error. */
914 if ( encoding->sids || encoding->codes )
916 if ( encoding->sids )
917 FREE( encoding->sids );
919 if ( encoding->codes )
920 FREE( encoding->codes );
933 CFF_Load_SubFont( CFF_SubFont* font,
937 FT_ULong base_offset )
943 CFF_Font_Dict* top = &font->font_dict;
944 CFF_Private* priv = &font->private_dict;
947 CFF_Parser_Init( &parser, CFF_CODE_TOPDICT, &font->font_dict );
950 MEM_Set( top, 0, sizeof ( *top ) );
952 top->underline_position = -100;
953 top->underline_thickness = 50;
954 top->charstring_type = 2;
955 top->font_matrix.xx = 0x10000L;
956 top->font_matrix.yy = 0x10000L;
957 top->cid_count = 8720;
959 error = CFF_Access_Element( index, font_index, &dict, &dict_len ) ||
960 CFF_Parser_Run( &parser, dict, dict + dict_len );
962 CFF_Forget_Element( index, &dict );
967 /* if it is a CID font, we stop there */
968 if ( top->cid_registry )
971 /* parse the private dictionary, if any */
972 if ( top->private_offset && top->private_size )
975 MEM_Set( priv, 0, sizeof ( *priv ) );
977 priv->blue_shift = 7;
980 priv->expansion_factor = (FT_Fixed)0.06 * 0x10000L;
981 priv->blue_scale = (FT_Fixed)0.039625 * 0x10000L;
983 CFF_Parser_Init( &parser, CFF_CODE_PRIVATE, priv );
985 if ( FILE_Seek( base_offset + font->font_dict.private_offset ) ||
986 ACCESS_Frame( font->font_dict.private_size ) )
989 error = CFF_Parser_Run( &parser,
990 (FT_Byte*)stream->cursor,
991 (FT_Byte*)stream->limit );
997 /* read the local subrs, if any */
998 if ( priv->local_subrs_offset )
1000 if ( FILE_Seek( base_offset + top->private_offset +
1001 priv->local_subrs_offset ) )
1004 error = cff_new_index( &font->local_subrs_index, stream, 1 );
1008 font->num_local_subrs = font->local_subrs_index.count;
1009 error = cff_explicit_index( &font->local_subrs_index,
1010 &font->local_subrs );
1021 CFF_Done_SubFont( FT_Memory memory,
1022 CFF_SubFont* subfont )
1026 cff_done_index( &subfont->local_subrs_index );
1027 FREE( subfont->local_subrs );
1032 FT_LOCAL_DEF FT_Error
1033 CFF_Load_Font( FT_Stream stream,
1037 static const FT_Frame_Field cff_header_fields[] =
1040 #define FT_STRUCTURE CFF_Font
1042 FT_FRAME_START( 4 ),
1043 FT_FRAME_BYTE( version_major ),
1044 FT_FRAME_BYTE( version_minor ),
1045 FT_FRAME_BYTE( header_size ),
1046 FT_FRAME_BYTE( absolute_offsize ),
1051 FT_Memory memory = stream->memory;
1052 FT_ULong base_offset;
1053 CFF_Font_Dict* dict;
1056 MEM_Set( font, 0, sizeof ( *font ) );
1058 font->stream = stream;
1059 font->memory = memory;
1060 dict = &font->top_font.font_dict;
1061 base_offset = FILE_Pos();
1063 /* read CFF font header */
1064 if ( READ_Fields( cff_header_fields, font ) )
1068 if ( font->version_major != 1 ||
1069 font->header_size < 4 ||
1070 font->absolute_offsize > 4 )
1072 FT_TRACE2(( "[not a CFF font header!]\n" ));
1073 error = CFF_Err_Unknown_File_Format;
1077 /* skip the rest of the header */
1078 if ( FILE_Skip( font->header_size - 4 ) )
1081 /* read the name, top dict, string and global subrs index */
1082 if ( FT_SET_ERROR( cff_new_index( &font->name_index, stream, 0 )) ||
1083 FT_SET_ERROR( cff_new_index( &font->font_dict_index, stream, 0 )) ||
1084 FT_SET_ERROR( cff_new_index( &font->string_index, stream, 0 )) ||
1085 FT_SET_ERROR( cff_new_index( &font->global_subrs_index, stream, 1 )) )
1088 /* well, we don't really forget the `disabled' fonts... */
1089 font->num_faces = font->name_index.count;
1090 if ( face_index >= (FT_Int)font->num_faces )
1092 FT_ERROR(( "CFF_Load_Font: incorrect face index = %d\n",
1094 error = CFF_Err_Invalid_Argument;
1097 /* in case of a font format check, simply exit now */
1098 if ( face_index < 0 )
1101 /* now, parse the top-level font dictionary */
1102 error = CFF_Load_SubFont( &font->top_font,
1103 &font->font_dict_index,
1110 /* now, check for a CID font */
1111 if ( dict->cid_registry )
1118 /* this is a CID-keyed font, we must now allocate a table of */
1119 /* sub-fonts, then load each of them separately */
1120 if ( FILE_Seek( base_offset + dict->cid_fd_array_offset ) )
1123 error = cff_new_index( &fd_index, stream, 0 );
1127 if ( fd_index.count > CFF_MAX_CID_FONTS )
1129 FT_ERROR(( "CFF_Load_Font: FD array too large in CID font\n" ));
1133 /* allocate & read each font dict independently */
1134 font->num_subfonts = fd_index.count;
1135 if ( ALLOC_ARRAY( sub, fd_index.count, CFF_SubFont ) )
1138 /* setup pointer table */
1139 for ( index = 0; index < fd_index.count; index++ )
1140 font->subfonts[index] = sub + index;
1142 /* now load each sub font independently */
1143 for ( index = 0; index < fd_index.count; index++ )
1145 sub = font->subfonts[index];
1146 error = CFF_Load_SubFont( sub, &fd_index, index,
1147 stream, base_offset );
1152 /* now load the FD Select array */
1153 error = CFF_Load_FD_Select( &font->fd_select,
1156 base_offset + dict->cid_fd_select_offset );
1159 cff_done_index( &fd_index );
1165 font->num_subfonts = 0;
1167 /* read the charstrings index now */
1168 if ( dict->charstrings_offset == 0 )
1170 FT_ERROR(( "CFF_Load_Font: no charstrings offset!\n" ));
1171 error = CFF_Err_Unknown_File_Format;
1175 if ( FILE_Seek( base_offset + dict->charstrings_offset ) )
1178 error = cff_new_index( &font->charstrings_index, stream, 0 );
1182 /* explicit the global subrs */
1183 font->num_global_subrs = font->global_subrs_index.count;
1184 font->num_glyphs = font->charstrings_index.count;
1186 error = cff_explicit_index( &font->global_subrs_index,
1187 &font->global_subrs ) ;
1192 /* read the Charset and Encoding tables when available */
1193 error = CFF_Load_Charset( &font->charset, font->num_glyphs, stream,
1194 base_offset, dict->charset_offset );
1198 error = CFF_Load_Encoding( &font->encoding,
1203 dict->encoding_offset );
1207 /* get the font name */
1208 font->font_name = CFF_Get_Name( &font->name_index, face_index );
1216 CFF_Done_Font( CFF_Font* font )
1218 FT_Memory memory = font->memory;
1222 cff_done_index( &font->global_subrs_index );
1223 cff_done_index( &font->string_index );
1224 cff_done_index( &font->font_dict_index );
1225 cff_done_index( &font->name_index );
1226 cff_done_index( &font->charstrings_index );
1228 /* release font dictionaries */
1229 for ( index = 0; index < font->num_subfonts; index++ )
1230 CFF_Done_SubFont( memory, font->subfonts[index] );
1232 CFF_Done_Encoding( &font->encoding, font->stream );
1233 CFF_Done_Charset( &font->charset, font->stream );
1235 CFF_Done_SubFont( memory, &font->top_font );
1237 CFF_Done_FD_Select( &font->fd_select, font->stream );
1239 FREE( font->global_subrs );
1240 FREE( font->font_name );