1 /***************************************************************************/
5 /* SFNT object management (base). */
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 /***************************************************************************/
24 #include FT_INTERNAL_SFNT_H
25 #include FT_INTERNAL_DEBUG_H
26 #include FT_TRUETYPE_IDS_H
27 #include FT_TRUETYPE_TAGS_H
28 #include FT_SERVICE_POSTSCRIPT_CMAPS_H
29 #include FT_SFNT_NAMES_H
33 #ifdef TT_CONFIG_OPTION_BDF
38 /*************************************************************************/
40 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
41 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
42 /* messages during execution. */
45 #define FT_COMPONENT trace_sfobjs
49 /* convert a UTF-16 name entry to ASCII */
51 tt_name_entry_ascii_from_utf16( TT_NameEntry entry,
54 FT_String* string = NULL;
56 FT_Byte* read = (FT_Byte*)entry->string;
60 len = (FT_UInt)entry->stringLength / 2;
62 if ( FT_NEW_ARRAY( string, len + 1 ) )
65 for ( n = 0; n < len; n++ )
67 code = FT_NEXT_USHORT( read );
72 if ( code < 32 || code > 127 )
75 string[n] = (char)code;
84 /* convert an Apple Roman or symbol name entry to ASCII */
86 tt_name_entry_ascii_from_other( TT_NameEntry entry,
89 FT_String* string = NULL;
91 FT_Byte* read = (FT_Byte*)entry->string;
95 len = (FT_UInt)entry->stringLength;
97 if ( FT_NEW_ARRAY( string, len + 1 ) )
100 for ( n = 0; n < len; n++ )
107 if ( code < 32 || code > 127 )
110 string[n] = (char)code;
119 typedef FT_String* (*TT_NameEntry_ConvertFunc)( TT_NameEntry entry,
123 /* documentation is in sfnt.h */
125 FT_LOCAL_DEF( FT_Error )
126 tt_face_get_name( TT_Face face,
130 FT_Memory memory = face->root.memory;
131 FT_Error error = FT_Err_Ok;
132 FT_String* result = NULL;
134 TT_NameEntryRec* rec;
135 FT_Int found_apple = -1;
136 FT_Int found_apple_roman = -1;
137 FT_Int found_apple_english = -1;
138 FT_Int found_win = -1;
139 FT_Int found_unicode = -1;
141 FT_Bool is_english = 0;
143 TT_NameEntry_ConvertFunc convert;
148 rec = face->name_table.names;
149 for ( n = 0; n < face->num_names; n++, rec++ )
151 /* According to the OpenType 1.3 specification, only Microsoft or */
152 /* Apple platform IDs might be used in the `name' table. The */
153 /* `Unicode' platform is reserved for the `cmap' table, and the */
154 /* `ISO' one is deprecated. */
156 /* However, the Apple TrueType specification doesn't say the same */
157 /* thing and goes to suggest that all Unicode `name' table entries */
158 /* should be coded in UTF-16 (in big-endian format I suppose). */
160 if ( rec->nameID == nameid && rec->stringLength > 0 )
162 switch ( rec->platformID )
164 case TT_PLATFORM_APPLE_UNICODE:
165 case TT_PLATFORM_ISO:
166 /* there is `languageID' to check there. We should use this */
167 /* field only as a last solution when nothing else is */
173 case TT_PLATFORM_MACINTOSH:
174 /* This is a bit special because some fonts will use either */
175 /* an English language id, or a Roman encoding id, to indicate */
176 /* the English version of its font name. */
178 if ( rec->languageID == TT_MAC_LANGID_ENGLISH )
179 found_apple_english = n;
180 else if ( rec->encodingID == TT_MAC_ID_ROMAN )
181 found_apple_roman = n;
184 case TT_PLATFORM_MICROSOFT:
185 /* we only take a non-English name when there is nothing */
186 /* else available in the font */
188 if ( found_win == -1 || ( rec->languageID & 0x3FF ) == 0x009 )
190 switch ( rec->encodingID )
192 case TT_MS_ID_SYMBOL_CS:
193 case TT_MS_ID_UNICODE_CS:
195 is_english = FT_BOOL( ( rec->languageID & 0x3FF ) == 0x009 );
211 found_apple = found_apple_roman;
212 if ( found_apple_english >= 0 )
213 found_apple = found_apple_english;
215 /* some fonts contain invalid Unicode or Macintosh formatted entries; */
216 /* we will thus favor names encoded in Windows formats if available */
217 /* (provided it is an English name) */
220 if ( found_win >= 0 && !( found_apple >= 0 && !is_english ) )
222 rec = face->name_table.names + found_win;
223 switch ( rec->encodingID )
225 /* all Unicode strings are encoded using UTF-16BE */
226 case TT_MS_ID_UNICODE_CS:
227 case TT_MS_ID_SYMBOL_CS:
228 convert = tt_name_entry_ascii_from_utf16;
232 /* Apparently, if this value is found in a name table entry, it is */
233 /* documented as `full Unicode repertoire'. Experience with the */
234 /* MsGothic font shipped with Windows Vista shows that this really */
235 /* means UTF-16 encoded names (UCS-4 values are only used within */
237 convert = tt_name_entry_ascii_from_utf16;
244 else if ( found_apple >= 0 )
246 rec = face->name_table.names + found_apple;
247 convert = tt_name_entry_ascii_from_other;
249 else if ( found_unicode >= 0 )
251 rec = face->name_table.names + found_unicode;
252 convert = tt_name_entry_ascii_from_utf16;
255 if ( rec && convert )
257 if ( rec->string == NULL )
259 FT_Stream stream = face->name_table.stream;
262 if ( FT_QNEW_ARRAY ( rec->string, rec->stringLength ) ||
263 FT_STREAM_SEEK( rec->stringOffset ) ||
264 FT_STREAM_READ( rec->string, rec->stringLength ) )
266 FT_FREE( rec->string );
267 rec->stringLength = 0;
273 result = convert( rec, memory );
283 sfnt_find_encoding( int platform_id,
286 typedef struct TEncoding_
290 FT_Encoding encoding;
295 const TEncoding tt_encodings[] =
297 { TT_PLATFORM_ISO, -1, FT_ENCODING_UNICODE },
299 { TT_PLATFORM_APPLE_UNICODE, -1, FT_ENCODING_UNICODE },
301 { TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, FT_ENCODING_APPLE_ROMAN },
303 { TT_PLATFORM_MICROSOFT, TT_MS_ID_SYMBOL_CS, FT_ENCODING_MS_SYMBOL },
304 { TT_PLATFORM_MICROSOFT, TT_MS_ID_UCS_4, FT_ENCODING_UNICODE },
305 { TT_PLATFORM_MICROSOFT, TT_MS_ID_UNICODE_CS, FT_ENCODING_UNICODE },
306 { TT_PLATFORM_MICROSOFT, TT_MS_ID_SJIS, FT_ENCODING_SJIS },
307 { TT_PLATFORM_MICROSOFT, TT_MS_ID_GB2312, FT_ENCODING_GB2312 },
308 { TT_PLATFORM_MICROSOFT, TT_MS_ID_BIG_5, FT_ENCODING_BIG5 },
309 { TT_PLATFORM_MICROSOFT, TT_MS_ID_WANSUNG, FT_ENCODING_WANSUNG },
310 { TT_PLATFORM_MICROSOFT, TT_MS_ID_JOHAB, FT_ENCODING_JOHAB }
313 const TEncoding *cur, *limit;
317 limit = cur + sizeof ( tt_encodings ) / sizeof ( tt_encodings[0] );
319 for ( ; cur < limit; cur++ )
321 if ( cur->platform_id == platform_id )
323 if ( cur->encoding_id == encoding_id ||
324 cur->encoding_id == -1 )
325 return cur->encoding;
329 return FT_ENCODING_NONE;
333 #define WRITE_USHORT( p, v ) \
336 *(p)++ = (FT_Byte)( (v) >> 8 ); \
337 *(p)++ = (FT_Byte)( (v) >> 0 ); \
341 #define WRITE_ULONG( p, v ) \
344 *(p)++ = (FT_Byte)( (v) >> 24 ); \
345 *(p)++ = (FT_Byte)( (v) >> 16 ); \
346 *(p)++ = (FT_Byte)( (v) >> 8 ); \
347 *(p)++ = (FT_Byte)( (v) >> 0 ); \
353 sfnt_stream_close( FT_Stream stream )
355 FT_Memory memory = stream->memory;
358 FT_FREE( stream->base );
362 stream->close = NULL;
366 FT_CALLBACK_DEF( int )
367 compare_offsets( const void* a,
370 WOFF_Table table1 = *(WOFF_Table*)a;
371 WOFF_Table table2 = *(WOFF_Table*)b;
373 FT_ULong offset1 = table1->Offset;
374 FT_ULong offset2 = table2->Offset;
377 if ( offset1 > offset2 )
379 else if ( offset1 < offset2 )
386 /* Replace `face->root.stream' with a stream containing the extracted */
387 /* SFNT of a WOFF font. */
390 woff_open_font( FT_Stream stream,
393 FT_Memory memory = stream->memory;
394 FT_Error error = FT_Err_Ok;
397 WOFF_Table tables = NULL;
398 WOFF_Table* indices = NULL;
400 FT_ULong woff_offset;
402 FT_Byte* sfnt = NULL;
403 FT_Stream sfnt_stream = NULL;
405 FT_Byte* sfnt_header;
406 FT_ULong sfnt_offset;
409 FT_ULong old_tag = 0;
411 static const FT_Frame_Field woff_header_fields[] =
414 #define FT_STRUCTURE WOFF_HeaderRec
416 FT_FRAME_START( 44 ),
417 FT_FRAME_ULONG ( signature ),
418 FT_FRAME_ULONG ( flavor ),
419 FT_FRAME_ULONG ( length ),
420 FT_FRAME_USHORT( num_tables ),
421 FT_FRAME_USHORT( reserved ),
422 FT_FRAME_ULONG ( totalSfntSize ),
423 FT_FRAME_USHORT( majorVersion ),
424 FT_FRAME_USHORT( minorVersion ),
425 FT_FRAME_ULONG ( metaOffset ),
426 FT_FRAME_ULONG ( metaLength ),
427 FT_FRAME_ULONG ( metaOrigLength ),
428 FT_FRAME_ULONG ( privOffset ),
429 FT_FRAME_ULONG ( privLength ),
434 FT_ASSERT( stream == face->root.stream );
435 FT_ASSERT( FT_STREAM_POS() == 0 );
437 if ( FT_STREAM_READ_FIELDS( woff_header_fields, &woff ) )
440 /* Make sure we don't recurse back here or hit TTC code. */
441 if ( woff.flavor == TTAG_wOFF || woff.flavor == TTAG_ttcf )
442 return FT_THROW( Invalid_Table );
444 /* Miscellaneous checks. */
445 if ( woff.length != stream->size ||
446 woff.num_tables == 0 ||
447 44 + woff.num_tables * 20UL >= woff.length ||
448 12 + woff.num_tables * 16UL >= woff.totalSfntSize ||
449 ( woff.totalSfntSize & 3 ) != 0 ||
450 ( woff.metaOffset == 0 && ( woff.metaLength != 0 ||
451 woff.metaOrigLength != 0 ) ) ||
452 ( woff.metaLength != 0 && woff.metaOrigLength == 0 ) ||
453 ( woff.privOffset == 0 && woff.privLength != 0 ) )
455 FT_ERROR(( "woff_font_open: invalid WOFF header\n" ));
456 return FT_THROW( Invalid_Table );
459 /* Don't trust `totalSfntSize' before thorough checks. */
460 if ( FT_ALLOC( sfnt, 12 + woff.num_tables * 16UL ) ||
461 FT_NEW( sfnt_stream ) )
466 /* Write sfnt header. */
468 FT_UInt searchRange, entrySelector, rangeShift, x;
480 searchRange = ( 1 << entrySelector ) * 16;
481 rangeShift = woff.num_tables * 16 - searchRange;
483 WRITE_ULONG ( sfnt_header, woff.flavor );
484 WRITE_USHORT( sfnt_header, woff.num_tables );
485 WRITE_USHORT( sfnt_header, searchRange );
486 WRITE_USHORT( sfnt_header, entrySelector );
487 WRITE_USHORT( sfnt_header, rangeShift );
490 /* While the entries in the sfnt header must be sorted by the */
491 /* tag value, the tables themselves are not. We thus have to */
492 /* sort them by offset and check that they don't overlap. */
494 if ( FT_NEW_ARRAY( tables, woff.num_tables ) ||
495 FT_NEW_ARRAY( indices, woff.num_tables ) )
499 " tag offset compLen origLen checksum\n"
500 " -------------------------------------------\n" ));
502 if ( FT_FRAME_ENTER( 20L * woff.num_tables ) )
505 for ( nn = 0; nn < woff.num_tables; nn++ )
507 WOFF_Table table = tables + nn;
509 table->Tag = FT_GET_TAG4();
510 table->Offset = FT_GET_ULONG();
511 table->CompLength = FT_GET_ULONG();
512 table->OrigLength = FT_GET_ULONG();
513 table->CheckSum = FT_GET_ULONG();
515 FT_TRACE2(( " %c%c%c%c %08lx %08lx %08lx %08lx\n",
516 (FT_Char)( table->Tag >> 24 ),
517 (FT_Char)( table->Tag >> 16 ),
518 (FT_Char)( table->Tag >> 8 ),
519 (FT_Char)( table->Tag ),
525 if ( table->Tag <= old_tag )
529 FT_ERROR(( "woff_font_open: table tags are not sorted\n" ));
530 error = FT_THROW( Invalid_Table );
534 old_tag = table->Tag;
540 /* Sort by offset. */
544 sizeof ( WOFF_Table ),
547 /* Check offsets and lengths. */
549 woff_offset = 44 + woff.num_tables * 20L;
550 sfnt_offset = 12 + woff.num_tables * 16L;
552 for ( nn = 0; nn < woff.num_tables; nn++ )
554 WOFF_Table table = indices[nn];
557 if ( table->Offset != woff_offset ||
558 table->CompLength > woff.length ||
559 table->Offset > woff.length - table->CompLength ||
560 table->OrigLength > woff.totalSfntSize ||
561 sfnt_offset > woff.totalSfntSize - table->OrigLength ||
562 table->CompLength > table->OrigLength )
564 FT_ERROR(( "woff_font_open: invalid table offsets\n" ));
565 error = FT_THROW( Invalid_Table );
569 table->OrigOffset = sfnt_offset;
571 /* The offsets must be multiples of 4. */
572 woff_offset += ( table->CompLength + 3 ) & ~3U;
573 sfnt_offset += ( table->OrigLength + 3 ) & ~3U;
579 * We don't decode and check the metadata block.
580 * We don't check table checksums either.
581 * But other than those, I think we implement all
582 * `MUST' checks from the spec.
585 if ( woff.metaOffset )
587 if ( woff.metaOffset != woff_offset ||
588 woff.metaOffset + woff.metaLength > woff.length )
590 FT_ERROR(( "woff_font_open:"
591 " invalid `metadata' offset or length\n" ));
592 error = FT_THROW( Invalid_Table );
596 /* We have padding only ... */
597 woff_offset += woff.metaLength;
600 if ( woff.privOffset )
602 /* ... if it isn't the last block. */
603 woff_offset = ( woff_offset + 3 ) & ~3U;
605 if ( woff.privOffset != woff_offset ||
606 woff.privOffset + woff.privLength > woff.length )
608 FT_ERROR(( "woff_font_open: invalid `private' offset or length\n" ));
609 error = FT_THROW( Invalid_Table );
613 /* No padding for the last block. */
614 woff_offset += woff.privLength;
617 if ( sfnt_offset != woff.totalSfntSize ||
618 woff_offset != woff.length )
620 FT_ERROR(( "woff_font_open: invalid `sfnt' table structure\n" ));
621 error = FT_THROW( Invalid_Table );
625 /* Now use `totalSfntSize'. */
626 if ( FT_REALLOC( sfnt,
627 12 + woff.num_tables * 16UL,
628 woff.totalSfntSize ) )
631 sfnt_header = sfnt + 12;
633 /* Write the tables. */
635 for ( nn = 0; nn < woff.num_tables; nn++ )
637 WOFF_Table table = tables + nn;
640 /* Write SFNT table entry. */
641 WRITE_ULONG( sfnt_header, table->Tag );
642 WRITE_ULONG( sfnt_header, table->CheckSum );
643 WRITE_ULONG( sfnt_header, table->OrigOffset );
644 WRITE_ULONG( sfnt_header, table->OrigLength );
646 /* Write table data. */
647 if ( FT_STREAM_SEEK( table->Offset ) ||
648 FT_FRAME_ENTER( table->CompLength ) )
651 if ( table->CompLength == table->OrigLength )
653 /* Uncompressed data; just copy. */
654 ft_memcpy( sfnt + table->OrigOffset,
660 #ifdef FT_CONFIG_OPTION_USE_ZLIB
662 /* Uncompress with zlib. */
663 FT_ULong output_len = table->OrigLength;
666 error = FT_Gzip_Uncompress( memory,
667 sfnt + table->OrigOffset, &output_len,
668 stream->cursor, table->CompLength );
671 if ( output_len != table->OrigLength )
673 FT_ERROR(( "woff_font_open: compressed table length mismatch\n" ));
674 error = FT_THROW( Invalid_Table );
678 #else /* !FT_CONFIG_OPTION_USE_ZLIB */
680 error = FT_THROW( Unimplemented_Feature );
683 #endif /* !FT_CONFIG_OPTION_USE_ZLIB */
688 /* We don't check whether the padding bytes in the WOFF file are */
689 /* actually '\0'. For the output, however, we do set them properly. */
690 sfnt_offset = table->OrigOffset + table->OrigLength;
691 while ( sfnt_offset & 3 )
693 sfnt[sfnt_offset] = '\0';
698 /* Ok! Finally ready. Swap out stream and return. */
699 FT_Stream_OpenMemory( sfnt_stream, sfnt, woff.totalSfntSize );
700 sfnt_stream->memory = stream->memory;
701 sfnt_stream->close = sfnt_stream_close;
705 ( face->root.face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 );
707 face->root.stream = sfnt_stream;
709 face->root.face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM;
718 FT_Stream_Close( sfnt_stream );
719 FT_FREE( sfnt_stream );
730 /* Fill in face->ttc_header. If the font is not a TTC, it is */
731 /* synthesized into a TTC with one offset table. */
733 sfnt_open_font( FT_Stream stream,
736 FT_Memory memory = stream->memory;
738 FT_ULong tag, offset;
740 static const FT_Frame_Field ttc_header_fields[] =
743 #define FT_STRUCTURE TTC_HeaderRec
746 FT_FRAME_LONG( version ),
747 FT_FRAME_LONG( count ), /* this is ULong in the specs */
752 face->ttc_header.tag = 0;
753 face->ttc_header.version = 0;
754 face->ttc_header.count = 0;
757 offset = FT_STREAM_POS();
759 if ( FT_READ_ULONG( tag ) )
762 if ( tag == TTAG_wOFF )
764 FT_TRACE2(( "sfnt_open_font: file is a WOFF; synthesizing SFNT\n" ));
766 if ( FT_STREAM_SEEK( offset ) )
769 error = woff_open_font( stream, face );
773 /* Swap out stream and retry! */
774 stream = face->root.stream;
778 if ( tag != 0x00010000UL &&
783 tag != 0x00020000UL )
785 FT_TRACE2(( " not a font using the SFNT container format\n" ));
786 return FT_THROW( Unknown_File_Format );
789 face->ttc_header.tag = TTAG_ttcf;
791 if ( tag == TTAG_ttcf )
796 FT_TRACE3(( "sfnt_open_font: file is a collection\n" ));
798 if ( FT_STREAM_READ_FIELDS( ttc_header_fields, &face->ttc_header ) )
801 if ( face->ttc_header.count == 0 )
802 return FT_THROW( Invalid_Table );
804 /* a rough size estimate: let's conservatively assume that there */
805 /* is just a single table info in each subfont header (12 + 16*1 = */
806 /* 28 bytes), thus we have (at least) `12 + 4*count' bytes for the */
807 /* size of the TTC header plus `28*count' bytes for all subfont */
809 if ( (FT_ULong)face->ttc_header.count > stream->size / ( 28 + 4 ) )
810 return FT_THROW( Array_Too_Large );
812 /* now read the offsets of each font in the file */
813 if ( FT_NEW_ARRAY( face->ttc_header.offsets, face->ttc_header.count ) )
816 if ( FT_FRAME_ENTER( face->ttc_header.count * 4L ) )
819 for ( n = 0; n < face->ttc_header.count; n++ )
820 face->ttc_header.offsets[n] = FT_GET_ULONG();
826 FT_TRACE3(( "sfnt_open_font: synthesize TTC\n" ));
828 face->ttc_header.version = 1 << 16;
829 face->ttc_header.count = 1;
831 if ( FT_NEW( face->ttc_header.offsets ) )
834 face->ttc_header.offsets[0] = offset;
841 FT_LOCAL_DEF( FT_Error )
842 sfnt_init_face( FT_Stream stream,
844 FT_Int face_instance_index,
846 FT_Parameter* params )
849 FT_Library library = face->root.driver->root.library;
854 /* for now, parameters are unused */
855 FT_UNUSED( num_params );
859 sfnt = (SFNT_Service)face->sfnt;
862 sfnt = (SFNT_Service)FT_Get_Module_Interface( library, "sfnt" );
865 FT_ERROR(( "sfnt_init_face: cannot access `sfnt' module\n" ));
866 return FT_THROW( Missing_Module );
870 face->goto_table = sfnt->goto_table;
873 FT_FACE_FIND_GLOBAL_SERVICE( face, face->psnames, POSTSCRIPT_CMAPS );
875 FT_TRACE2(( "SFNT driver\n" ));
877 error = sfnt_open_font( stream, face );
881 /* Stream may have changed in sfnt_open_font. */
882 stream = face->root.stream;
884 FT_TRACE2(( "sfnt_init_face: %08p, %ld\n", face, face_instance_index ));
886 face_index = FT_ABS( face_instance_index ) & 0xFFFF;
888 if ( face_index >= face->ttc_header.count )
890 if ( face_instance_index >= 0 )
891 return FT_THROW( Invalid_Argument );
896 if ( FT_STREAM_SEEK( face->ttc_header.offsets[face_index] ) )
899 /* check whether we have a valid TrueType file */
900 error = sfnt->load_font_dir( face, stream );
904 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
913 FT_UShort num_instances;
914 FT_UShort instance_size;
916 FT_Int instance_index;
919 instance_index = FT_ABS( face_instance_index ) >> 16;
921 /* test whether current face is a GX font with named instances */
922 if ( face->goto_table( face, TTAG_fvar, stream, &fvar_len ) ||
924 FT_READ_ULONG( version ) ||
925 FT_READ_USHORT( offset ) ||
926 FT_STREAM_SKIP( 2 ) ||
927 FT_READ_USHORT( num_axes ) ||
928 FT_READ_USHORT( axis_size ) ||
929 FT_READ_USHORT( num_instances ) ||
930 FT_READ_USHORT( instance_size ) )
940 /* check that the data is bound by the table length; */
941 /* based on similar code in function `TT_Get_MM_Var' */
942 if ( version != 0x00010000UL ||
945 instance_size != 4 + 4 * num_axes ||
946 num_instances > 0x7EFF ||
948 axis_size * num_axes +
949 instance_size * num_instances > fvar_len )
952 /* we don't support Multiple Master CFFs yet */
953 if ( !face->goto_table( face, TTAG_CFF, stream, 0 ) )
956 /* we support at most 2^15 - 1 instances */
957 if ( num_instances >= ( 1U << 15 ) - 1 )
959 if ( face_instance_index >= 0 )
960 return FT_THROW( Invalid_Argument );
965 /* instance indices in `face_instance_index' start with index 1, */
966 /* thus `>' and not `>=' */
967 if ( instance_index > num_instances )
969 if ( face_instance_index >= 0 )
970 return FT_THROW( Invalid_Argument );
975 face->root.style_flags = (FT_Long)num_instances << 16;
979 face->root.num_faces = face->ttc_header.count;
980 face->root.face_index = face_index;
989 FT_TRACE2(( "`" #x "' " )); \
990 FT_TRACE3(( "-->\n" )); \
992 error = sfnt->load_ ## x( face, stream ); \
994 FT_TRACE2(( "%s\n", ( !error ) \
996 : FT_ERR_EQ( error, Table_Missing ) \
998 : "failed to load" )); \
999 FT_TRACE3(( "\n" )); \
1002 #define LOADM_( x, vertical ) \
1005 FT_TRACE2(( "`%s" #x "' ", \
1006 vertical ? "vertical " : "" )); \
1007 FT_TRACE3(( "-->\n" )); \
1009 error = sfnt->load_ ## x( face, stream, vertical ); \
1011 FT_TRACE2(( "%s\n", ( !error ) \
1013 : FT_ERR_EQ( error, Table_Missing ) \
1015 : "failed to load" )); \
1016 FT_TRACE3(( "\n" )); \
1019 #define GET_NAME( id, field ) \
1022 error = tt_face_get_name( face, TT_NAME_ID_ ## id, field ); \
1028 FT_LOCAL_DEF( FT_Error )
1029 sfnt_load_face( FT_Stream stream,
1031 FT_Int face_instance_index,
1033 FT_Parameter* params )
1036 #ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES
1037 FT_Error psnames_error;
1039 FT_Bool has_outline;
1040 FT_Bool is_apple_sbit;
1041 FT_Bool is_apple_sbix;
1042 FT_Bool ignore_preferred_family = FALSE;
1043 FT_Bool ignore_preferred_subfamily = FALSE;
1045 SFNT_Service sfnt = (SFNT_Service)face->sfnt;
1047 FT_UNUSED( face_instance_index );
1050 /* Check parameters */
1056 for ( i = 0; i < num_params; i++ )
1058 if ( params[i].tag == FT_PARAM_TAG_IGNORE_PREFERRED_FAMILY )
1059 ignore_preferred_family = TRUE;
1060 else if ( params[i].tag == FT_PARAM_TAG_IGNORE_PREFERRED_SUBFAMILY )
1061 ignore_preferred_subfamily = TRUE;
1067 /* We now support two SFNT-based bitmapped font formats. They */
1068 /* are recognized easily as they do not include a `glyf' */
1071 /* The first format comes from Apple, and uses a table named */
1072 /* `bhed' instead of `head' to store the font header (using */
1073 /* the same format). It also doesn't include horizontal and */
1074 /* vertical metrics tables (i.e. `hhea' and `vhea' tables are */
1077 /* The other format comes from Microsoft, and is used with */
1078 /* WinCE/PocketPC. It looks like a standard TTF, except that */
1079 /* it doesn't contain outlines. */
1082 FT_TRACE2(( "sfnt_load_face: %08p\n\n", face ));
1084 /* do we have outlines in there? */
1085 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1086 has_outline = FT_BOOL( face->root.internal->incremental_interface != 0 ||
1087 tt_face_lookup_table( face, TTAG_glyf ) != 0 ||
1088 tt_face_lookup_table( face, TTAG_CFF ) != 0 );
1090 has_outline = FT_BOOL( tt_face_lookup_table( face, TTAG_glyf ) != 0 ||
1091 tt_face_lookup_table( face, TTAG_CFF ) != 0 );
1095 is_apple_sbix = !face->goto_table( face, TTAG_sbix, stream, 0 );
1097 /* Apple 'sbix' color bitmaps are rendered scaled and then the 'glyf'
1098 * outline rendered on top. We don't support that yet, so just ignore
1099 * the 'glyf' outline and advertise it as a bitmap-only font. */
1100 if ( is_apple_sbix )
1101 has_outline = FALSE;
1103 /* if this font doesn't contain outlines, we try to load */
1104 /* a `bhed' table */
1105 if ( !has_outline && sfnt->load_bhed )
1108 is_apple_sbit = FT_BOOL( !error );
1111 /* load the font header (`head' table) if this isn't an Apple */
1112 /* sbit font file */
1113 if ( !is_apple_sbit || is_apple_sbix )
1120 if ( face->header.Units_Per_EM == 0 )
1122 error = FT_THROW( Invalid_Table );
1127 /* the following tables are often not present in embedded TrueType */
1128 /* fonts within PDF documents, so don't check for them. */
1132 /* the following tables are optional in PCL fonts -- */
1133 /* don't check for errors */
1137 #ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES
1138 psnames_error = error;
1141 /* do not load the metrics headers and tables if this is an Apple */
1142 /* sbit font file */
1143 if ( !is_apple_sbit )
1145 /* load the `hhea' and `hmtx' tables */
1150 if ( FT_ERR_EQ( error, Table_Missing ) )
1152 error = FT_THROW( Hmtx_Table_Missing );
1154 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1155 /* If this is an incrementally loaded font and there are */
1156 /* overriding metrics, tolerate a missing `hmtx' table. */
1157 if ( face->root.internal->incremental_interface &&
1158 face->root.internal->incremental_interface->funcs->
1161 face->horizontal.number_Of_HMetrics = 0;
1167 else if ( FT_ERR_EQ( error, Table_Missing ) )
1169 /* No `hhea' table necessary for SFNT Mac fonts. */
1170 if ( face->format_tag == TTAG_true )
1172 FT_TRACE2(( "This is an SFNT Mac font.\n" ));
1179 error = FT_THROW( Horiz_Header_Missing );
1181 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1182 /* If this is an incrementally loaded font and there are */
1183 /* overriding metrics, tolerate a missing `hhea' table. */
1184 if ( face->root.internal->incremental_interface &&
1185 face->root.internal->incremental_interface->funcs->
1188 face->horizontal.number_Of_HMetrics = 0;
1199 /* try to load the `vhea' and `vmtx' tables */
1205 face->vertical_info = 1;
1208 if ( error && FT_ERR_NEQ( error, Table_Missing ) )
1214 /* we treat the table as missing if there are any errors */
1215 face->os2.version = 0xFFFFU;
1219 /* the optional tables */
1221 /* embedded bitmap support */
1222 if ( sfnt->load_eblc )
1227 /* a font which contains neither bitmaps nor outlines is */
1228 /* still valid (although rather useless in most cases); */
1229 /* however, you can find such stripped fonts in PDFs */
1230 if ( FT_ERR_EQ( error, Table_Missing ) )
1240 if ( FT_ERR_NEQ( error, Table_Missing ) )
1243 face->pclt.Version = 0;
1246 /* consider the kerning and gasp tables as optional */
1250 face->root.num_glyphs = face->max_profile.numGlyphs;
1252 /* Bit 8 of the `fsSelection' field in the `OS/2' table denotes */
1253 /* a WWS-only font face. `WWS' stands for `weight', width', and */
1254 /* `slope', a term used by Microsoft's Windows Presentation */
1255 /* Foundation (WPF). This flag has been introduced in version */
1256 /* 1.5 of the OpenType specification (May 2008). */
1258 face->root.family_name = NULL;
1259 face->root.style_name = NULL;
1260 if ( face->os2.version != 0xFFFFU && face->os2.fsSelection & 256 )
1262 if ( !ignore_preferred_family )
1263 GET_NAME( PREFERRED_FAMILY, &face->root.family_name );
1264 if ( !face->root.family_name )
1265 GET_NAME( FONT_FAMILY, &face->root.family_name );
1267 if ( !ignore_preferred_subfamily )
1268 GET_NAME( PREFERRED_SUBFAMILY, &face->root.style_name );
1269 if ( !face->root.style_name )
1270 GET_NAME( FONT_SUBFAMILY, &face->root.style_name );
1274 GET_NAME( WWS_FAMILY, &face->root.family_name );
1275 if ( !face->root.family_name && !ignore_preferred_family )
1276 GET_NAME( PREFERRED_FAMILY, &face->root.family_name );
1277 if ( !face->root.family_name )
1278 GET_NAME( FONT_FAMILY, &face->root.family_name );
1280 GET_NAME( WWS_SUBFAMILY, &face->root.style_name );
1281 if ( !face->root.style_name && !ignore_preferred_subfamily )
1282 GET_NAME( PREFERRED_SUBFAMILY, &face->root.style_name );
1283 if ( !face->root.style_name )
1284 GET_NAME( FONT_SUBFAMILY, &face->root.style_name );
1287 /* now set up root fields */
1289 FT_Face root = &face->root;
1290 FT_Long flags = root->face_flags;
1293 /*********************************************************************/
1295 /* Compute face flags. */
1297 if ( face->sbit_table_type == TT_SBIT_TABLE_TYPE_CBLC ||
1298 face->sbit_table_type == TT_SBIT_TABLE_TYPE_SBIX )
1299 flags |= FT_FACE_FLAG_COLOR; /* color glyphs */
1301 if ( has_outline == TRUE )
1302 flags |= FT_FACE_FLAG_SCALABLE; /* scalable outlines */
1304 /* The sfnt driver only supports bitmap fonts natively, thus we */
1305 /* don't set FT_FACE_FLAG_HINTER. */
1306 flags |= FT_FACE_FLAG_SFNT | /* SFNT file format */
1307 FT_FACE_FLAG_HORIZONTAL; /* horizontal data */
1309 #ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES
1310 if ( !psnames_error &&
1311 face->postscript.FormatType != 0x00030000L )
1312 flags |= FT_FACE_FLAG_GLYPH_NAMES;
1315 /* fixed width font? */
1316 if ( face->postscript.isFixedPitch )
1317 flags |= FT_FACE_FLAG_FIXED_WIDTH;
1319 /* vertical information? */
1320 if ( face->vertical_info )
1321 flags |= FT_FACE_FLAG_VERTICAL;
1323 /* kerning available ? */
1324 if ( TT_FACE_HAS_KERNING( face ) )
1325 flags |= FT_FACE_FLAG_KERNING;
1327 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1328 /* Don't bother to load the tables unless somebody asks for them. */
1329 /* No need to do work which will (probably) not be used. */
1330 if ( tt_face_lookup_table( face, TTAG_glyf ) != 0 &&
1331 tt_face_lookup_table( face, TTAG_fvar ) != 0 &&
1332 tt_face_lookup_table( face, TTAG_gvar ) != 0 )
1333 flags |= FT_FACE_FLAG_MULTIPLE_MASTERS;
1336 root->face_flags = flags;
1338 /*********************************************************************/
1340 /* Compute style flags. */
1344 if ( has_outline == TRUE && face->os2.version != 0xFFFFU )
1346 /* We have an OS/2 table; use the `fsSelection' field. Bit 9 */
1347 /* indicates an oblique font face. This flag has been */
1348 /* introduced in version 1.5 of the OpenType specification. */
1350 if ( face->os2.fsSelection & 512 ) /* bit 9 */
1351 flags |= FT_STYLE_FLAG_ITALIC;
1352 else if ( face->os2.fsSelection & 1 ) /* bit 0 */
1353 flags |= FT_STYLE_FLAG_ITALIC;
1355 if ( face->os2.fsSelection & 32 ) /* bit 5 */
1356 flags |= FT_STYLE_FLAG_BOLD;
1360 /* this is an old Mac font, use the header field */
1362 if ( face->header.Mac_Style & 1 )
1363 flags |= FT_STYLE_FLAG_BOLD;
1365 if ( face->header.Mac_Style & 2 )
1366 flags |= FT_STYLE_FLAG_ITALIC;
1369 root->style_flags |= flags;
1371 /*********************************************************************/
1373 /* Polish the charmaps. */
1375 /* Try to set the charmap encoding according to the platform & */
1376 /* encoding ID of each charmap. */
1379 tt_face_build_cmaps( face ); /* ignore errors */
1382 /* set the encoding fields */
1387 for ( m = 0; m < root->num_charmaps; m++ )
1389 FT_CharMap charmap = root->charmaps[m];
1392 charmap->encoding = sfnt_find_encoding( charmap->platform_id,
1393 charmap->encoding_id );
1396 if ( root->charmap == NULL &&
1397 charmap->encoding == FT_ENCODING_UNICODE )
1399 /* set 'root->charmap' to the first Unicode encoding we find */
1400 root->charmap = charmap;
1406 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
1409 * Now allocate the root array of FT_Bitmap_Size records and
1410 * populate them. Unfortunately, it isn't possible to indicate bit
1411 * depths in the FT_Bitmap_Size record. This is a design error.
1417 count = face->sbit_num_strikes;
1421 FT_Memory memory = face->root.stream->memory;
1422 FT_UShort em_size = face->header.Units_Per_EM;
1423 FT_Short avgwidth = face->os2.xAvgCharWidth;
1424 FT_Size_Metrics metrics;
1427 if ( em_size == 0 || face->os2.version == 0xFFFFU )
1433 if ( FT_NEW_ARRAY( root->available_sizes, count ) )
1436 for ( i = 0; i < count; i++ )
1438 FT_Bitmap_Size* bsize = root->available_sizes + i;
1441 error = sfnt->load_strike_metrics( face, i, &metrics );
1445 bsize->height = (FT_Short)( metrics.height >> 6 );
1446 bsize->width = (FT_Short)(
1447 ( avgwidth * metrics.x_ppem + em_size / 2 ) / em_size );
1449 bsize->x_ppem = metrics.x_ppem << 6;
1450 bsize->y_ppem = metrics.y_ppem << 6;
1453 bsize->size = metrics.y_ppem << 6;
1456 root->face_flags |= FT_FACE_FLAG_FIXED_SIZES;
1457 root->num_fixed_sizes = (FT_Int)count;
1461 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
1463 /* a font with no bitmaps and no outlines is scalable; */
1464 /* it has only empty glyphs then */
1465 if ( !FT_HAS_FIXED_SIZES( root ) && !FT_IS_SCALABLE( root ) )
1466 root->face_flags |= FT_FACE_FLAG_SCALABLE;
1469 /*********************************************************************/
1471 /* Set up metrics. */
1473 if ( FT_IS_SCALABLE( root ) )
1475 /* XXX What about if outline header is missing */
1476 /* (e.g. sfnt wrapped bitmap)? */
1477 root->bbox.xMin = face->header.xMin;
1478 root->bbox.yMin = face->header.yMin;
1479 root->bbox.xMax = face->header.xMax;
1480 root->bbox.yMax = face->header.yMax;
1481 root->units_per_EM = face->header.Units_Per_EM;
1484 /* XXX: Computing the ascender/descender/height is very different */
1485 /* from what the specification tells you. Apparently, we */
1486 /* must be careful because */
1488 /* - not all fonts have an OS/2 table; in this case, we take */
1489 /* the values in the horizontal header. However, these */
1490 /* values very often are not reliable. */
1492 /* - otherwise, the correct typographic values are in the */
1493 /* sTypoAscender, sTypoDescender & sTypoLineGap fields. */
1495 /* However, certain fonts have these fields set to 0. */
1496 /* Rather, they have usWinAscent & usWinDescent correctly */
1497 /* set (but with different values). */
1499 /* As an example, Arial Narrow is implemented through four */
1500 /* files ARIALN.TTF, ARIALNI.TTF, ARIALNB.TTF & ARIALNBI.TTF */
1502 /* Strangely, all fonts have the same values in their */
1503 /* sTypoXXX fields, except ARIALNB which sets them to 0. */
1505 /* On the other hand, they all have different */
1506 /* usWinAscent/Descent values -- as a conclusion, the OS/2 */
1507 /* table cannot be used to compute the text height reliably! */
1510 /* The ascender and descender are taken from the `hhea' table. */
1511 /* If zero, they are taken from the `OS/2' table. */
1513 root->ascender = face->horizontal.Ascender;
1514 root->descender = face->horizontal.Descender;
1516 root->height = root->ascender - root->descender +
1517 face->horizontal.Line_Gap;
1519 if ( !( root->ascender || root->descender ) )
1521 if ( face->os2.version != 0xFFFFU )
1523 if ( face->os2.sTypoAscender || face->os2.sTypoDescender )
1525 root->ascender = face->os2.sTypoAscender;
1526 root->descender = face->os2.sTypoDescender;
1528 root->height = root->ascender - root->descender +
1529 face->os2.sTypoLineGap;
1533 root->ascender = (FT_Short)face->os2.usWinAscent;
1534 root->descender = -(FT_Short)face->os2.usWinDescent;
1536 root->height = root->ascender - root->descender;
1541 root->max_advance_width =
1542 (FT_Short)face->horizontal.advance_Width_Max;
1543 root->max_advance_height =
1544 (FT_Short)( face->vertical_info ? face->vertical.advance_Height_Max
1547 /* See http://www.microsoft.com/OpenType/OTSpec/post.htm -- */
1548 /* Adjust underline position from top edge to centre of */
1549 /* stroke to convert TrueType meaning to FreeType meaning. */
1550 root->underline_position = face->postscript.underlinePosition -
1551 face->postscript.underlineThickness / 2;
1552 root->underline_thickness = face->postscript.underlineThickness;
1558 FT_TRACE2(( "sfnt_load_face: done\n" ));
1569 FT_LOCAL_DEF( void )
1570 sfnt_done_face( TT_Face face )
1579 memory = face->root.memory;
1580 sfnt = (SFNT_Service)face->sfnt;
1584 /* destroy the postscript names table if it is loaded */
1585 if ( sfnt->free_psnames )
1586 sfnt->free_psnames( face );
1588 /* destroy the embedded bitmaps table if it is loaded */
1589 if ( sfnt->free_eblc )
1590 sfnt->free_eblc( face );
1593 #ifdef TT_CONFIG_OPTION_BDF
1594 /* freeing the embedded BDF properties */
1595 tt_face_free_bdf_props( face );
1598 /* freeing the kerning table */
1599 tt_face_done_kern( face );
1601 /* freeing the collection table */
1602 FT_FREE( face->ttc_header.offsets );
1603 face->ttc_header.count = 0;
1605 /* freeing table directory */
1606 FT_FREE( face->dir_tables );
1607 face->num_tables = 0;
1610 FT_Stream stream = FT_FACE_STREAM( face );
1613 /* simply release the 'cmap' table frame */
1614 FT_FRAME_RELEASE( face->cmap_table );
1615 face->cmap_size = 0;
1618 /* freeing the horizontal metrics */
1620 FT_Stream stream = FT_FACE_STREAM( face );
1623 FT_FRAME_RELEASE( face->horz_metrics );
1624 FT_FRAME_RELEASE( face->vert_metrics );
1625 face->horz_metrics_size = 0;
1626 face->vert_metrics_size = 0;
1629 /* freeing the vertical ones, if any */
1630 if ( face->vertical_info )
1632 FT_FREE( face->vertical.long_metrics );
1633 FT_FREE( face->vertical.short_metrics );
1634 face->vertical_info = 0;
1637 /* freeing the gasp table */
1638 FT_FREE( face->gasp.gaspRanges );
1639 face->gasp.numRanges = 0;
1641 /* freeing the name table */
1643 sfnt->free_name( face );
1645 /* freeing family and style name */
1646 FT_FREE( face->root.family_name );
1647 FT_FREE( face->root.style_name );
1649 /* freeing sbit size table */
1650 FT_FREE( face->root.available_sizes );
1651 face->root.num_fixed_sizes = 0;
1653 FT_FREE( face->postscript_name );