1 /***************************************************************************/
5 /* SFNT object management (base). */
7 /* Copyright 1996-2008, 2010-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 /***************************************************************************/
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 /*************************************************************************/
126 /* tt_face_get_name */
129 /* Returns a given ENGLISH name record in ASCII. */
132 /* face :: A handle to the source face object. */
134 /* nameid :: The name id of the name record to return. */
137 /* name :: The address of a string pointer. NULL if no name is */
141 /* FreeType error code. 0 means success. */
144 tt_face_get_name( TT_Face face,
148 FT_Memory memory = face->root.memory;
149 FT_Error error = FT_Err_Ok;
150 FT_String* result = NULL;
152 TT_NameEntryRec* rec;
153 FT_Int found_apple = -1;
154 FT_Int found_apple_roman = -1;
155 FT_Int found_apple_english = -1;
156 FT_Int found_win = -1;
157 FT_Int found_unicode = -1;
159 FT_Bool is_english = 0;
161 TT_NameEntry_ConvertFunc convert;
166 rec = face->name_table.names;
167 for ( n = 0; n < face->num_names; n++, rec++ )
169 /* According to the OpenType 1.3 specification, only Microsoft or */
170 /* Apple platform IDs might be used in the `name' table. The */
171 /* `Unicode' platform is reserved for the `cmap' table, and the */
172 /* `ISO' one is deprecated. */
174 /* However, the Apple TrueType specification doesn't say the same */
175 /* thing and goes to suggest that all Unicode `name' table entries */
176 /* should be coded in UTF-16 (in big-endian format I suppose). */
178 if ( rec->nameID == nameid && rec->stringLength > 0 )
180 switch ( rec->platformID )
182 case TT_PLATFORM_APPLE_UNICODE:
183 case TT_PLATFORM_ISO:
184 /* there is `languageID' to check there. We should use this */
185 /* field only as a last solution when nothing else is */
191 case TT_PLATFORM_MACINTOSH:
192 /* This is a bit special because some fonts will use either */
193 /* an English language id, or a Roman encoding id, to indicate */
194 /* the English version of its font name. */
196 if ( rec->languageID == TT_MAC_LANGID_ENGLISH )
197 found_apple_english = n;
198 else if ( rec->encodingID == TT_MAC_ID_ROMAN )
199 found_apple_roman = n;
202 case TT_PLATFORM_MICROSOFT:
203 /* we only take a non-English name when there is nothing */
204 /* else available in the font */
206 if ( found_win == -1 || ( rec->languageID & 0x3FF ) == 0x009 )
208 switch ( rec->encodingID )
210 case TT_MS_ID_SYMBOL_CS:
211 case TT_MS_ID_UNICODE_CS:
213 is_english = FT_BOOL( ( rec->languageID & 0x3FF ) == 0x009 );
229 found_apple = found_apple_roman;
230 if ( found_apple_english >= 0 )
231 found_apple = found_apple_english;
233 /* some fonts contain invalid Unicode or Macintosh formatted entries; */
234 /* we will thus favor names encoded in Windows formats if available */
235 /* (provided it is an English name) */
238 if ( found_win >= 0 && !( found_apple >= 0 && !is_english ) )
240 rec = face->name_table.names + found_win;
241 switch ( rec->encodingID )
243 /* all Unicode strings are encoded using UTF-16BE */
244 case TT_MS_ID_UNICODE_CS:
245 case TT_MS_ID_SYMBOL_CS:
246 convert = tt_name_entry_ascii_from_utf16;
250 /* Apparently, if this value is found in a name table entry, it is */
251 /* documented as `full Unicode repertoire'. Experience with the */
252 /* MsGothic font shipped with Windows Vista shows that this really */
253 /* means UTF-16 encoded names (UCS-4 values are only used within */
255 convert = tt_name_entry_ascii_from_utf16;
262 else if ( found_apple >= 0 )
264 rec = face->name_table.names + found_apple;
265 convert = tt_name_entry_ascii_from_other;
267 else if ( found_unicode >= 0 )
269 rec = face->name_table.names + found_unicode;
270 convert = tt_name_entry_ascii_from_utf16;
273 if ( rec && convert )
275 if ( rec->string == NULL )
277 FT_Stream stream = face->name_table.stream;
280 if ( FT_QNEW_ARRAY ( rec->string, rec->stringLength ) ||
281 FT_STREAM_SEEK( rec->stringOffset ) ||
282 FT_STREAM_READ( rec->string, rec->stringLength ) )
284 FT_FREE( rec->string );
285 rec->stringLength = 0;
291 result = convert( rec, memory );
301 sfnt_find_encoding( int platform_id,
304 typedef struct TEncoding_
308 FT_Encoding encoding;
313 const TEncoding tt_encodings[] =
315 { TT_PLATFORM_ISO, -1, FT_ENCODING_UNICODE },
317 { TT_PLATFORM_APPLE_UNICODE, -1, FT_ENCODING_UNICODE },
319 { TT_PLATFORM_MACINTOSH, TT_MAC_ID_ROMAN, FT_ENCODING_APPLE_ROMAN },
321 { TT_PLATFORM_MICROSOFT, TT_MS_ID_SYMBOL_CS, FT_ENCODING_MS_SYMBOL },
322 { TT_PLATFORM_MICROSOFT, TT_MS_ID_UCS_4, FT_ENCODING_UNICODE },
323 { TT_PLATFORM_MICROSOFT, TT_MS_ID_UNICODE_CS, FT_ENCODING_UNICODE },
324 { TT_PLATFORM_MICROSOFT, TT_MS_ID_SJIS, FT_ENCODING_SJIS },
325 { TT_PLATFORM_MICROSOFT, TT_MS_ID_GB2312, FT_ENCODING_GB2312 },
326 { TT_PLATFORM_MICROSOFT, TT_MS_ID_BIG_5, FT_ENCODING_BIG5 },
327 { TT_PLATFORM_MICROSOFT, TT_MS_ID_WANSUNG, FT_ENCODING_WANSUNG },
328 { TT_PLATFORM_MICROSOFT, TT_MS_ID_JOHAB, FT_ENCODING_JOHAB }
331 const TEncoding *cur, *limit;
335 limit = cur + sizeof ( tt_encodings ) / sizeof ( tt_encodings[0] );
337 for ( ; cur < limit; cur++ )
339 if ( cur->platform_id == platform_id )
341 if ( cur->encoding_id == encoding_id ||
342 cur->encoding_id == -1 )
343 return cur->encoding;
347 return FT_ENCODING_NONE;
351 #define WRITE_USHORT( p, v ) \
354 *(p)++ = (FT_Byte)( (v) >> 8 ); \
355 *(p)++ = (FT_Byte)( (v) >> 0 ); \
359 #define WRITE_ULONG( p, v ) \
362 *(p)++ = (FT_Byte)( (v) >> 24 ); \
363 *(p)++ = (FT_Byte)( (v) >> 16 ); \
364 *(p)++ = (FT_Byte)( (v) >> 8 ); \
365 *(p)++ = (FT_Byte)( (v) >> 0 ); \
371 sfnt_stream_close( FT_Stream stream )
373 FT_Memory memory = stream->memory;
376 FT_FREE( stream->base );
384 FT_CALLBACK_DEF( int )
385 compare_offsets( const void* a,
388 WOFF_Table table1 = *(WOFF_Table*)a;
389 WOFF_Table table2 = *(WOFF_Table*)b;
391 FT_ULong offset1 = table1->Offset;
392 FT_ULong offset2 = table2->Offset;
395 if ( offset1 > offset2 )
397 else if ( offset1 < offset2 )
404 /* Replace `face->root.stream' with a stream containing the extracted */
405 /* SFNT of a WOFF font. */
408 woff_open_font( FT_Stream stream,
411 FT_Memory memory = stream->memory;
412 FT_Error error = FT_Err_Ok;
415 WOFF_Table tables = NULL;
416 WOFF_Table* indices = NULL;
418 FT_ULong woff_offset;
420 FT_Byte* sfnt = NULL;
421 FT_Stream sfnt_stream = NULL;
423 FT_Byte* sfnt_header;
424 FT_ULong sfnt_offset;
427 FT_ULong old_tag = 0;
429 static const FT_Frame_Field woff_header_fields[] =
432 #define FT_STRUCTURE WOFF_HeaderRec
434 FT_FRAME_START( 44 ),
435 FT_FRAME_ULONG ( signature ),
436 FT_FRAME_ULONG ( flavor ),
437 FT_FRAME_ULONG ( length ),
438 FT_FRAME_USHORT( num_tables ),
439 FT_FRAME_USHORT( reserved ),
440 FT_FRAME_ULONG ( totalSfntSize ),
441 FT_FRAME_USHORT( majorVersion ),
442 FT_FRAME_USHORT( minorVersion ),
443 FT_FRAME_ULONG ( metaOffset ),
444 FT_FRAME_ULONG ( metaLength ),
445 FT_FRAME_ULONG ( metaOrigLength ),
446 FT_FRAME_ULONG ( privOffset ),
447 FT_FRAME_ULONG ( privLength ),
452 FT_ASSERT( stream == face->root.stream );
453 FT_ASSERT( FT_STREAM_POS() == 0 );
455 if ( FT_STREAM_READ_FIELDS( woff_header_fields, &woff ) )
458 /* Make sure we don't recurse back here or hit TTC code. */
459 if ( woff.flavor == TTAG_wOFF || woff.flavor == TTAG_ttcf )
460 return FT_THROW( Invalid_Table );
462 /* Miscellaneous checks. */
463 if ( woff.length != stream->size ||
464 woff.num_tables == 0 ||
465 44 + woff.num_tables * 20UL >= woff.length ||
466 12 + woff.num_tables * 16UL >= woff.totalSfntSize ||
467 ( woff.totalSfntSize & 3 ) != 0 ||
468 ( woff.metaOffset == 0 && ( woff.metaLength != 0 ||
469 woff.metaOrigLength != 0 ) ) ||
470 ( woff.metaLength != 0 && woff.metaOrigLength == 0 ) ||
471 ( woff.privOffset == 0 && woff.privLength != 0 ) )
472 return FT_THROW( Invalid_Table );
474 if ( FT_ALLOC( sfnt, woff.totalSfntSize ) ||
475 FT_NEW( sfnt_stream ) )
480 /* Write sfnt header. */
482 FT_UInt searchRange, entrySelector, rangeShift, x;
494 searchRange = ( 1 << entrySelector ) * 16;
495 rangeShift = woff.num_tables * 16 - searchRange;
497 WRITE_ULONG ( sfnt_header, woff.flavor );
498 WRITE_USHORT( sfnt_header, woff.num_tables );
499 WRITE_USHORT( sfnt_header, searchRange );
500 WRITE_USHORT( sfnt_header, entrySelector );
501 WRITE_USHORT( sfnt_header, rangeShift );
504 /* While the entries in the sfnt header must be sorted by the */
505 /* tag value, the tables themselves are not. We thus have to */
506 /* sort them by offset and check that they don't overlap. */
508 if ( FT_NEW_ARRAY( tables, woff.num_tables ) ||
509 FT_NEW_ARRAY( indices, woff.num_tables ) )
513 " tag offset compLen origLen checksum\n"
514 " -------------------------------------------\n" ));
516 if ( FT_FRAME_ENTER( 20L * woff.num_tables ) )
519 for ( nn = 0; nn < woff.num_tables; nn++ )
521 WOFF_Table table = tables + nn;
523 table->Tag = FT_GET_TAG4();
524 table->Offset = FT_GET_ULONG();
525 table->CompLength = FT_GET_ULONG();
526 table->OrigLength = FT_GET_ULONG();
527 table->CheckSum = FT_GET_ULONG();
529 FT_TRACE2(( " %c%c%c%c %08lx %08lx %08lx %08lx\n",
530 (FT_Char)( table->Tag >> 24 ),
531 (FT_Char)( table->Tag >> 16 ),
532 (FT_Char)( table->Tag >> 8 ),
533 (FT_Char)( table->Tag ),
539 if ( table->Tag <= old_tag )
542 error = FT_THROW( Invalid_Table );
546 old_tag = table->Tag;
552 /* Sort by offset. */
556 sizeof ( WOFF_Table ),
559 /* Check offsets and lengths. */
561 woff_offset = 44 + woff.num_tables * 20L;
562 sfnt_offset = 12 + woff.num_tables * 16L;
564 for ( nn = 0; nn < woff.num_tables; nn++ )
566 WOFF_Table table = indices[nn];
569 if ( table->Offset != woff_offset ||
570 table->CompLength > woff.length ||
571 table->Offset > woff.length - table->CompLength ||
572 table->OrigLength > woff.totalSfntSize ||
573 sfnt_offset > woff.totalSfntSize - table->OrigLength ||
574 table->CompLength > table->OrigLength )
576 error = FT_THROW( Invalid_Table );
580 table->OrigOffset = sfnt_offset;
582 /* The offsets must be multiples of 4. */
583 woff_offset += ( table->CompLength + 3 ) & ~3;
584 sfnt_offset += ( table->OrigLength + 3 ) & ~3;
590 * We don't decode and check the metadata block.
591 * We don't check table checksums either.
592 * But other than those, I think we implement all
593 * `MUST' checks from the spec.
596 if ( woff.metaOffset )
598 if ( woff.metaOffset != woff_offset ||
599 woff.metaOffset + woff.metaLength > woff.length )
601 error = FT_THROW( Invalid_Table );
605 /* We have padding only ... */
606 woff_offset += woff.metaLength;
609 if ( woff.privOffset )
611 /* ... if it isn't the last block. */
612 woff_offset = ( woff_offset + 3 ) & ~3;
614 if ( woff.privOffset != woff_offset ||
615 woff.privOffset + woff.privLength > woff.length )
617 error = FT_THROW( Invalid_Table );
621 /* No padding for the last block. */
622 woff_offset += woff.privLength;
625 if ( sfnt_offset != woff.totalSfntSize ||
626 woff_offset != woff.length )
628 error = FT_THROW( Invalid_Table );
632 /* Write the tables. */
634 for ( nn = 0; nn < woff.num_tables; nn++ )
636 WOFF_Table table = tables + nn;
639 /* Write SFNT table entry. */
640 WRITE_ULONG( sfnt_header, table->Tag );
641 WRITE_ULONG( sfnt_header, table->CheckSum );
642 WRITE_ULONG( sfnt_header, table->OrigOffset );
643 WRITE_ULONG( sfnt_header, table->OrigLength );
645 /* Write table data. */
646 if ( FT_STREAM_SEEK( table->Offset ) ||
647 FT_FRAME_ENTER( table->CompLength ) )
650 if ( table->CompLength == table->OrigLength )
652 /* Uncompressed data; just copy. */
653 ft_memcpy( sfnt + table->OrigOffset,
659 #ifdef FT_CONFIG_OPTION_USE_ZLIB
661 /* Uncompress with zlib. */
662 FT_ULong output_len = table->OrigLength;
665 error = FT_Gzip_Uncompress( memory,
666 sfnt + table->OrigOffset, &output_len,
667 stream->cursor, table->CompLength );
670 if ( output_len != table->OrigLength )
672 error = FT_THROW( Invalid_Table );
676 #else /* !FT_CONFIG_OPTION_USE_ZLIB */
678 error = FT_THROW( Unimplemented_Feature );
681 #endif /* !FT_CONFIG_OPTION_USE_ZLIB */
686 /* We don't check whether the padding bytes in the WOFF file are */
687 /* actually '\0'. For the output, however, we do set them properly. */
688 sfnt_offset = table->OrigOffset + table->OrigLength;
689 while ( sfnt_offset & 3 )
691 sfnt[sfnt_offset] = '\0';
696 /* Ok! Finally ready. Swap out stream and return. */
697 FT_Stream_OpenMemory( sfnt_stream, sfnt, woff.totalSfntSize );
698 sfnt_stream->memory = stream->memory;
699 sfnt_stream->close = sfnt_stream_close;
703 ( face->root.face_flags & FT_FACE_FLAG_EXTERNAL_STREAM ) != 0 );
705 face->root.stream = sfnt_stream;
707 face->root.face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM;
716 FT_Stream_Close( sfnt_stream );
717 FT_FREE( sfnt_stream );
728 /* Fill in face->ttc_header. If the font is not a TTC, it is */
729 /* synthesized into a TTC with one offset table. */
731 sfnt_open_font( FT_Stream stream,
734 FT_Memory memory = stream->memory;
736 FT_ULong tag, offset;
738 static const FT_Frame_Field ttc_header_fields[] =
741 #define FT_STRUCTURE TTC_HeaderRec
744 FT_FRAME_LONG( version ),
745 FT_FRAME_LONG( count ), /* this is ULong in the specs */
750 face->ttc_header.tag = 0;
751 face->ttc_header.version = 0;
752 face->ttc_header.count = 0;
755 offset = FT_STREAM_POS();
757 if ( FT_READ_ULONG( tag ) )
760 if ( tag == TTAG_wOFF )
762 FT_TRACE2(( "sfnt_open_font: file is a WOFF; synthesizing SFNT\n" ));
764 if ( FT_STREAM_SEEK( offset ) )
767 error = woff_open_font( stream, face );
771 /* Swap out stream and retry! */
772 stream = face->root.stream;
776 if ( tag != 0x00010000UL &&
781 tag != 0x00020000UL )
783 FT_TRACE2(( " not a font using the SFNT container format\n" ));
784 return FT_THROW( Unknown_File_Format );
787 face->ttc_header.tag = TTAG_ttcf;
789 if ( tag == TTAG_ttcf )
794 FT_TRACE3(( "sfnt_open_font: file is a collection\n" ));
796 if ( FT_STREAM_READ_FIELDS( ttc_header_fields, &face->ttc_header ) )
799 if ( face->ttc_header.count == 0 )
800 return FT_THROW( Invalid_Table );
802 /* a rough size estimate: let's conservatively assume that there */
803 /* is just a single table info in each subfont header (12 + 16*1 = */
804 /* 28 bytes), thus we have (at least) `12 + 4*count' bytes for the */
805 /* size of the TTC header plus `28*count' bytes for all subfont */
807 if ( (FT_ULong)face->ttc_header.count > stream->size / ( 28 + 4 ) )
808 return FT_THROW( Array_Too_Large );
810 /* now read the offsets of each font in the file */
811 if ( FT_NEW_ARRAY( face->ttc_header.offsets, face->ttc_header.count ) )
814 if ( FT_FRAME_ENTER( face->ttc_header.count * 4L ) )
817 for ( n = 0; n < face->ttc_header.count; n++ )
818 face->ttc_header.offsets[n] = FT_GET_ULONG();
824 FT_TRACE3(( "sfnt_open_font: synthesize TTC\n" ));
826 face->ttc_header.version = 1 << 16;
827 face->ttc_header.count = 1;
829 if ( FT_NEW( face->ttc_header.offsets ) )
832 face->ttc_header.offsets[0] = offset;
839 FT_LOCAL_DEF( FT_Error )
840 sfnt_init_face( FT_Stream stream,
844 FT_Parameter* params )
847 FT_Library library = face->root.driver->root.library;
851 /* for now, parameters are unused */
852 FT_UNUSED( num_params );
856 sfnt = (SFNT_Service)face->sfnt;
859 sfnt = (SFNT_Service)FT_Get_Module_Interface( library, "sfnt" );
862 FT_ERROR(( "sfnt_init_face: cannot access `sfnt' module\n" ));
863 return FT_THROW( Missing_Module );
867 face->goto_table = sfnt->goto_table;
870 FT_FACE_FIND_GLOBAL_SERVICE( face, face->psnames, POSTSCRIPT_CMAPS );
872 FT_TRACE2(( "SFNT driver\n" ));
874 error = sfnt_open_font( stream, face );
878 /* Stream may have changed in sfnt_open_font. */
879 stream = face->root.stream;
881 FT_TRACE2(( "sfnt_init_face: %08p, %ld\n", face, face_index ));
883 if ( face_index < 0 )
886 if ( face_index >= face->ttc_header.count )
887 return FT_THROW( Invalid_Argument );
889 if ( FT_STREAM_SEEK( face->ttc_header.offsets[face_index] ) )
892 /* check that we have a valid TrueType file */
893 error = sfnt->load_font_dir( face, stream );
897 face->root.num_faces = face->ttc_header.count;
898 face->root.face_index = face_index;
907 FT_TRACE2(( "`" #x "' " )); \
908 FT_TRACE3(( "-->\n" )); \
910 error = sfnt->load_ ## x( face, stream ); \
912 FT_TRACE2(( "%s\n", ( !error ) \
914 : FT_ERR_EQ( error, Table_Missing ) \
916 : "failed to load" )); \
917 FT_TRACE3(( "\n" )); \
920 #define LOADM_( x, vertical ) \
923 FT_TRACE2(( "`%s" #x "' ", \
924 vertical ? "vertical " : "" )); \
925 FT_TRACE3(( "-->\n" )); \
927 error = sfnt->load_ ## x( face, stream, vertical ); \
929 FT_TRACE2(( "%s\n", ( !error ) \
931 : FT_ERR_EQ( error, Table_Missing ) \
933 : "failed to load" )); \
934 FT_TRACE3(( "\n" )); \
937 #define GET_NAME( id, field ) \
940 error = tt_face_get_name( face, TT_NAME_ID_ ## id, field ); \
946 FT_LOCAL_DEF( FT_Error )
947 sfnt_load_face( FT_Stream stream,
951 FT_Parameter* params )
954 #ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES
955 FT_Error psnames_error;
958 FT_Bool is_apple_sbit;
959 FT_Bool is_apple_sbix;
960 FT_Bool ignore_preferred_family = FALSE;
961 FT_Bool ignore_preferred_subfamily = FALSE;
963 SFNT_Service sfnt = (SFNT_Service)face->sfnt;
965 FT_UNUSED( face_index );
968 /* Check parameters */
974 for ( i = 0; i < num_params; i++ )
976 if ( params[i].tag == FT_PARAM_TAG_IGNORE_PREFERRED_FAMILY )
977 ignore_preferred_family = TRUE;
978 else if ( params[i].tag == FT_PARAM_TAG_IGNORE_PREFERRED_SUBFAMILY )
979 ignore_preferred_subfamily = TRUE;
985 /* We now support two SFNT-based bitmapped font formats. They */
986 /* are recognized easily as they do not include a `glyf' */
989 /* The first format comes from Apple, and uses a table named */
990 /* `bhed' instead of `head' to store the font header (using */
991 /* the same format). It also doesn't include horizontal and */
992 /* vertical metrics tables (i.e. `hhea' and `vhea' tables are */
995 /* The other format comes from Microsoft, and is used with */
996 /* WinCE/PocketPC. It looks like a standard TTF, except that */
997 /* it doesn't contain outlines. */
1000 FT_TRACE2(( "sfnt_load_face: %08p\n\n", face ));
1002 /* do we have outlines in there? */
1003 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1004 has_outline = FT_BOOL( face->root.internal->incremental_interface != 0 ||
1005 tt_face_lookup_table( face, TTAG_glyf ) != 0 ||
1006 tt_face_lookup_table( face, TTAG_CFF ) != 0 );
1008 has_outline = FT_BOOL( tt_face_lookup_table( face, TTAG_glyf ) != 0 ||
1009 tt_face_lookup_table( face, TTAG_CFF ) != 0 );
1013 is_apple_sbix = !face->goto_table( face, TTAG_sbix, stream, 0 );
1015 /* Apple 'sbix' color bitmaps are rendered scaled and then the 'glyf'
1016 * outline rendered on top. We don't support that yet, so just ignore
1017 * the 'glyf' outline and advertise it as a bitmap-only font. */
1018 if ( is_apple_sbix )
1019 has_outline = FALSE;
1021 /* if this font doesn't contain outlines, we try to load */
1022 /* a `bhed' table */
1023 if ( !has_outline && sfnt->load_bhed )
1026 is_apple_sbit = FT_BOOL( !error );
1029 /* load the font header (`head' table) if this isn't an Apple */
1030 /* sbit font file */
1031 if ( !is_apple_sbit || is_apple_sbix )
1038 if ( face->header.Units_Per_EM == 0 )
1040 error = FT_THROW( Invalid_Table );
1045 /* the following tables are often not present in embedded TrueType */
1046 /* fonts within PDF documents, so don't check for them. */
1050 /* the following tables are optional in PCL fonts -- */
1051 /* don't check for errors */
1055 #ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES
1056 psnames_error = error;
1059 /* do not load the metrics headers and tables if this is an Apple */
1060 /* sbit font file */
1061 if ( !is_apple_sbit )
1063 /* load the `hhea' and `hmtx' tables */
1068 if ( FT_ERR_EQ( error, Table_Missing ) )
1070 error = FT_THROW( Hmtx_Table_Missing );
1072 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1073 /* If this is an incrementally loaded font and there are */
1074 /* overriding metrics, tolerate a missing `hmtx' table. */
1075 if ( face->root.internal->incremental_interface &&
1076 face->root.internal->incremental_interface->funcs->
1079 face->horizontal.number_Of_HMetrics = 0;
1085 else if ( FT_ERR_EQ( error, Table_Missing ) )
1087 /* No `hhea' table necessary for SFNT Mac fonts. */
1088 if ( face->format_tag == TTAG_true )
1090 FT_TRACE2(( "This is an SFNT Mac font.\n" ));
1097 error = FT_THROW( Horiz_Header_Missing );
1099 #ifdef FT_CONFIG_OPTION_INCREMENTAL
1100 /* If this is an incrementally loaded font and there are */
1101 /* overriding metrics, tolerate a missing `hhea' table. */
1102 if ( face->root.internal->incremental_interface &&
1103 face->root.internal->incremental_interface->funcs->
1106 face->horizontal.number_Of_HMetrics = 0;
1117 /* try to load the `vhea' and `vmtx' tables */
1123 face->vertical_info = 1;
1126 if ( error && FT_ERR_NEQ( error, Table_Missing ) )
1132 /* we treat the table as missing if there are any errors */
1133 face->os2.version = 0xFFFFU;
1137 /* the optional tables */
1139 /* embedded bitmap support */
1140 if ( sfnt->load_eblc )
1145 /* a font which contains neither bitmaps nor outlines is */
1146 /* still valid (although rather useless in most cases); */
1147 /* however, you can find such stripped fonts in PDFs */
1148 if ( FT_ERR_EQ( error, Table_Missing ) )
1158 if ( FT_ERR_NEQ( error, Table_Missing ) )
1161 face->pclt.Version = 0;
1164 /* consider the kerning and gasp tables as optional */
1168 face->root.num_glyphs = face->max_profile.numGlyphs;
1170 /* Bit 8 of the `fsSelection' field in the `OS/2' table denotes */
1171 /* a WWS-only font face. `WWS' stands for `weight', width', and */
1172 /* `slope', a term used by Microsoft's Windows Presentation */
1173 /* Foundation (WPF). This flag has been introduced in version */
1174 /* 1.5 of the OpenType specification (May 2008). */
1176 face->root.family_name = NULL;
1177 face->root.style_name = NULL;
1178 if ( face->os2.version != 0xFFFFU && face->os2.fsSelection & 256 )
1180 if ( !ignore_preferred_family )
1181 GET_NAME( PREFERRED_FAMILY, &face->root.family_name );
1182 if ( !face->root.family_name )
1183 GET_NAME( FONT_FAMILY, &face->root.family_name );
1185 if ( !ignore_preferred_subfamily )
1186 GET_NAME( PREFERRED_SUBFAMILY, &face->root.style_name );
1187 if ( !face->root.style_name )
1188 GET_NAME( FONT_SUBFAMILY, &face->root.style_name );
1192 GET_NAME( WWS_FAMILY, &face->root.family_name );
1193 if ( !face->root.family_name && !ignore_preferred_family )
1194 GET_NAME( PREFERRED_FAMILY, &face->root.family_name );
1195 if ( !face->root.family_name )
1196 GET_NAME( FONT_FAMILY, &face->root.family_name );
1198 GET_NAME( WWS_SUBFAMILY, &face->root.style_name );
1199 if ( !face->root.style_name && !ignore_preferred_subfamily )
1200 GET_NAME( PREFERRED_SUBFAMILY, &face->root.style_name );
1201 if ( !face->root.style_name )
1202 GET_NAME( FONT_SUBFAMILY, &face->root.style_name );
1205 /* now set up root fields */
1207 FT_Face root = &face->root;
1208 FT_Long flags = root->face_flags;
1211 /*********************************************************************/
1213 /* Compute face flags. */
1215 if ( face->sbit_table_type == TT_SBIT_TABLE_TYPE_CBLC ||
1216 face->sbit_table_type == TT_SBIT_TABLE_TYPE_SBIX )
1217 flags |= FT_FACE_FLAG_COLOR; /* color glyphs */
1219 if ( has_outline == TRUE )
1220 flags |= FT_FACE_FLAG_SCALABLE; /* scalable outlines */
1222 /* The sfnt driver only supports bitmap fonts natively, thus we */
1223 /* don't set FT_FACE_FLAG_HINTER. */
1224 flags |= FT_FACE_FLAG_SFNT | /* SFNT file format */
1225 FT_FACE_FLAG_HORIZONTAL; /* horizontal data */
1227 #ifdef TT_CONFIG_OPTION_POSTSCRIPT_NAMES
1228 if ( !psnames_error &&
1229 face->postscript.FormatType != 0x00030000L )
1230 flags |= FT_FACE_FLAG_GLYPH_NAMES;
1233 /* fixed width font? */
1234 if ( face->postscript.isFixedPitch )
1235 flags |= FT_FACE_FLAG_FIXED_WIDTH;
1237 /* vertical information? */
1238 if ( face->vertical_info )
1239 flags |= FT_FACE_FLAG_VERTICAL;
1241 /* kerning available ? */
1242 if ( TT_FACE_HAS_KERNING( face ) )
1243 flags |= FT_FACE_FLAG_KERNING;
1245 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1246 /* Don't bother to load the tables unless somebody asks for them. */
1247 /* No need to do work which will (probably) not be used. */
1248 if ( tt_face_lookup_table( face, TTAG_glyf ) != 0 &&
1249 tt_face_lookup_table( face, TTAG_fvar ) != 0 &&
1250 tt_face_lookup_table( face, TTAG_gvar ) != 0 )
1251 flags |= FT_FACE_FLAG_MULTIPLE_MASTERS;
1254 root->face_flags = flags;
1256 /*********************************************************************/
1258 /* Compute style flags. */
1262 if ( has_outline == TRUE && face->os2.version != 0xFFFFU )
1264 /* We have an OS/2 table; use the `fsSelection' field. Bit 9 */
1265 /* indicates an oblique font face. This flag has been */
1266 /* introduced in version 1.5 of the OpenType specification. */
1268 if ( face->os2.fsSelection & 512 ) /* bit 9 */
1269 flags |= FT_STYLE_FLAG_ITALIC;
1270 else if ( face->os2.fsSelection & 1 ) /* bit 0 */
1271 flags |= FT_STYLE_FLAG_ITALIC;
1273 if ( face->os2.fsSelection & 32 ) /* bit 5 */
1274 flags |= FT_STYLE_FLAG_BOLD;
1278 /* this is an old Mac font, use the header field */
1280 if ( face->header.Mac_Style & 1 )
1281 flags |= FT_STYLE_FLAG_BOLD;
1283 if ( face->header.Mac_Style & 2 )
1284 flags |= FT_STYLE_FLAG_ITALIC;
1287 root->style_flags = flags;
1289 /*********************************************************************/
1291 /* Polish the charmaps. */
1293 /* Try to set the charmap encoding according to the platform & */
1294 /* encoding ID of each charmap. */
1297 tt_face_build_cmaps( face ); /* ignore errors */
1300 /* set the encoding fields */
1305 for ( m = 0; m < root->num_charmaps; m++ )
1307 FT_CharMap charmap = root->charmaps[m];
1310 charmap->encoding = sfnt_find_encoding( charmap->platform_id,
1311 charmap->encoding_id );
1314 if ( root->charmap == NULL &&
1315 charmap->encoding == FT_ENCODING_UNICODE )
1317 /* set 'root->charmap' to the first Unicode encoding we find */
1318 root->charmap = charmap;
1324 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
1327 * Now allocate the root array of FT_Bitmap_Size records and
1328 * populate them. Unfortunately, it isn't possible to indicate bit
1329 * depths in the FT_Bitmap_Size record. This is a design error.
1335 count = face->sbit_num_strikes;
1339 FT_Memory memory = face->root.stream->memory;
1340 FT_UShort em_size = face->header.Units_Per_EM;
1341 FT_Short avgwidth = face->os2.xAvgCharWidth;
1342 FT_Size_Metrics metrics;
1345 if ( em_size == 0 || face->os2.version == 0xFFFFU )
1351 if ( FT_NEW_ARRAY( root->available_sizes, count ) )
1354 for ( i = 0; i < count; i++ )
1356 FT_Bitmap_Size* bsize = root->available_sizes + i;
1359 error = sfnt->load_strike_metrics( face, i, &metrics );
1363 bsize->height = (FT_Short)( metrics.height >> 6 );
1364 bsize->width = (FT_Short)(
1365 ( avgwidth * metrics.x_ppem + em_size / 2 ) / em_size );
1367 bsize->x_ppem = metrics.x_ppem << 6;
1368 bsize->y_ppem = metrics.y_ppem << 6;
1371 bsize->size = metrics.y_ppem << 6;
1374 root->face_flags |= FT_FACE_FLAG_FIXED_SIZES;
1375 root->num_fixed_sizes = (FT_Int)count;
1379 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
1381 /* a font with no bitmaps and no outlines is scalable; */
1382 /* it has only empty glyphs then */
1383 if ( !FT_HAS_FIXED_SIZES( root ) && !FT_IS_SCALABLE( root ) )
1384 root->face_flags |= FT_FACE_FLAG_SCALABLE;
1387 /*********************************************************************/
1389 /* Set up metrics. */
1391 if ( FT_IS_SCALABLE( root ) )
1393 /* XXX What about if outline header is missing */
1394 /* (e.g. sfnt wrapped bitmap)? */
1395 root->bbox.xMin = face->header.xMin;
1396 root->bbox.yMin = face->header.yMin;
1397 root->bbox.xMax = face->header.xMax;
1398 root->bbox.yMax = face->header.yMax;
1399 root->units_per_EM = face->header.Units_Per_EM;
1402 /* XXX: Computing the ascender/descender/height is very different */
1403 /* from what the specification tells you. Apparently, we */
1404 /* must be careful because */
1406 /* - not all fonts have an OS/2 table; in this case, we take */
1407 /* the values in the horizontal header. However, these */
1408 /* values very often are not reliable. */
1410 /* - otherwise, the correct typographic values are in the */
1411 /* sTypoAscender, sTypoDescender & sTypoLineGap fields. */
1413 /* However, certain fonts have these fields set to 0. */
1414 /* Rather, they have usWinAscent & usWinDescent correctly */
1415 /* set (but with different values). */
1417 /* As an example, Arial Narrow is implemented through four */
1418 /* files ARIALN.TTF, ARIALNI.TTF, ARIALNB.TTF & ARIALNBI.TTF */
1420 /* Strangely, all fonts have the same values in their */
1421 /* sTypoXXX fields, except ARIALNB which sets them to 0. */
1423 /* On the other hand, they all have different */
1424 /* usWinAscent/Descent values -- as a conclusion, the OS/2 */
1425 /* table cannot be used to compute the text height reliably! */
1428 /* The ascender and descender are taken from the `hhea' table. */
1429 /* If zero, they are taken from the `OS/2' table. */
1431 root->ascender = face->horizontal.Ascender;
1432 root->descender = face->horizontal.Descender;
1434 root->height = (FT_Short)( root->ascender - root->descender +
1435 face->horizontal.Line_Gap );
1437 if ( !( root->ascender || root->descender ) )
1439 if ( face->os2.version != 0xFFFFU )
1441 if ( face->os2.sTypoAscender || face->os2.sTypoDescender )
1443 root->ascender = face->os2.sTypoAscender;
1444 root->descender = face->os2.sTypoDescender;
1446 root->height = (FT_Short)( root->ascender - root->descender +
1447 face->os2.sTypoLineGap );
1451 root->ascender = (FT_Short)face->os2.usWinAscent;
1452 root->descender = -(FT_Short)face->os2.usWinDescent;
1454 root->height = (FT_UShort)( root->ascender - root->descender );
1459 root->max_advance_width = face->horizontal.advance_Width_Max;
1460 root->max_advance_height = (FT_Short)( face->vertical_info
1461 ? face->vertical.advance_Height_Max
1464 /* See http://www.microsoft.com/OpenType/OTSpec/post.htm -- */
1465 /* Adjust underline position from top edge to centre of */
1466 /* stroke to convert TrueType meaning to FreeType meaning. */
1467 root->underline_position = face->postscript.underlinePosition -
1468 face->postscript.underlineThickness / 2;
1469 root->underline_thickness = face->postscript.underlineThickness;
1475 FT_TRACE2(( "sfnt_load_face: done\n" ));
1486 FT_LOCAL_DEF( void )
1487 sfnt_done_face( TT_Face face )
1496 memory = face->root.memory;
1497 sfnt = (SFNT_Service)face->sfnt;
1501 /* destroy the postscript names table if it is loaded */
1502 if ( sfnt->free_psnames )
1503 sfnt->free_psnames( face );
1505 /* destroy the embedded bitmaps table if it is loaded */
1506 if ( sfnt->free_eblc )
1507 sfnt->free_eblc( face );
1510 #ifdef TT_CONFIG_OPTION_BDF
1511 /* freeing the embedded BDF properties */
1512 tt_face_free_bdf_props( face );
1515 /* freeing the kerning table */
1516 tt_face_done_kern( face );
1518 /* freeing the collection table */
1519 FT_FREE( face->ttc_header.offsets );
1520 face->ttc_header.count = 0;
1522 /* freeing table directory */
1523 FT_FREE( face->dir_tables );
1524 face->num_tables = 0;
1527 FT_Stream stream = FT_FACE_STREAM( face );
1530 /* simply release the 'cmap' table frame */
1531 FT_FRAME_RELEASE( face->cmap_table );
1532 face->cmap_size = 0;
1535 /* freeing the horizontal metrics */
1537 FT_Stream stream = FT_FACE_STREAM( face );
1540 FT_FRAME_RELEASE( face->horz_metrics );
1541 FT_FRAME_RELEASE( face->vert_metrics );
1542 face->horz_metrics_size = 0;
1543 face->vert_metrics_size = 0;
1546 /* freeing the vertical ones, if any */
1547 if ( face->vertical_info )
1549 FT_FREE( face->vertical.long_metrics );
1550 FT_FREE( face->vertical.short_metrics );
1551 face->vertical_info = 0;
1554 /* freeing the gasp table */
1555 FT_FREE( face->gasp.gaspRanges );
1556 face->gasp.numRanges = 0;
1558 /* freeing the name table */
1560 sfnt->free_name( face );
1562 /* freeing family and style name */
1563 FT_FREE( face->root.family_name );
1564 FT_FREE( face->root.style_name );
1566 /* freeing sbit size table */
1567 FT_FREE( face->root.available_sizes );
1568 face->root.num_fixed_sizes = 0;
1570 FT_FREE( face->postscript_name );