1 /***************************************************************************/
5 /* Load the basic TrueType tables, i.e., tables that can be either in */
6 /* TTF or OTF fonts (body). */
8 /* Copyright 1996-2016 by */
9 /* David Turner, Robert Wilhelm, and Werner Lemberg. */
11 /* This file is part of the FreeType project, and may only be used, */
12 /* modified, and distributed under the terms of the FreeType project */
13 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
14 /* this file you indicate that you have read the license and */
15 /* understand and accept it fully. */
17 /***************************************************************************/
21 #include FT_INTERNAL_DEBUG_H
22 #include FT_INTERNAL_STREAM_H
23 #include FT_TRUETYPE_TAGS_H
29 /*************************************************************************/
31 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
32 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
33 /* messages during execution. */
36 #define FT_COMPONENT trace_ttload
39 /*************************************************************************/
42 /* tt_face_lookup_table */
45 /* Looks for a TrueType table by name. */
48 /* face :: A face object handle. */
50 /* tag :: The searched tag. */
53 /* A pointer to the table directory entry. 0 if not found. */
55 FT_LOCAL_DEF( TT_Table
)
56 tt_face_lookup_table( TT_Face face
,
61 #ifdef FT_DEBUG_LEVEL_TRACE
62 FT_Bool zero_length
= FALSE
;
66 FT_TRACE4(( "tt_face_lookup_table: %08p, `%c%c%c%c' -- ",
68 (FT_Char
)( tag
>> 24 ),
69 (FT_Char
)( tag
>> 16 ),
70 (FT_Char
)( tag
>> 8 ),
73 entry
= face
->dir_tables
;
74 limit
= entry
+ face
->num_tables
;
76 for ( ; entry
< limit
; entry
++ )
78 /* For compatibility with Windows, we consider */
79 /* zero-length tables the same as missing tables. */
80 if ( entry
->Tag
== tag
)
82 if ( entry
->Length
!= 0 )
84 FT_TRACE4(( "found table.\n" ));
87 #ifdef FT_DEBUG_LEVEL_TRACE
93 #ifdef FT_DEBUG_LEVEL_TRACE
95 FT_TRACE4(( "ignoring empty table\n" ));
97 FT_TRACE4(( "could not find table\n" ));
104 /*************************************************************************/
107 /* tt_face_goto_table */
110 /* Looks for a TrueType table by name, then seek a stream to it. */
113 /* face :: A face object handle. */
115 /* tag :: The searched tag. */
117 /* stream :: The stream to seek when the table is found. */
120 /* length :: The length of the table if found, undefined otherwise. */
123 /* FreeType error code. 0 means success. */
125 FT_LOCAL_DEF( FT_Error
)
126 tt_face_goto_table( TT_Face face
,
135 table
= tt_face_lookup_table( face
, tag
);
139 *length
= table
->Length
;
141 if ( FT_STREAM_SEEK( table
->Offset
) )
145 error
= FT_THROW( Table_Missing
);
154 /* - check that `num_tables' is valid (and adjust it if necessary); */
155 /* also return the number of valid table entries */
157 /* - look for a `head' table, check its size, and parse it to check */
158 /* whether its `magic' field is correctly set */
160 /* - errors (except errors returned by stream handling) */
162 /* SFNT_Err_Unknown_File_Format: */
163 /* no table is defined in directory, it is not sfnt-wrapped */
165 /* SFNT_Err_Table_Missing: */
166 /* table directory is valid, but essential tables */
167 /* (head/bhed/SING) are missing */
170 check_table_dir( SFNT_Header sfnt
,
175 FT_UShort nn
, valid_entries
= 0;
176 FT_UInt has_head
= 0, has_sing
= 0, has_meta
= 0;
177 FT_ULong offset
= sfnt
->offset
+ 12;
179 static const FT_Frame_Field table_dir_entry_fields
[] =
182 #define FT_STRUCTURE TT_TableRec
184 FT_FRAME_START( 16 ),
185 FT_FRAME_ULONG( Tag
),
186 FT_FRAME_ULONG( CheckSum
),
187 FT_FRAME_ULONG( Offset
),
188 FT_FRAME_ULONG( Length
),
193 if ( FT_STREAM_SEEK( offset
) )
196 for ( nn
= 0; nn
< sfnt
->num_tables
; nn
++ )
201 if ( FT_STREAM_READ_FIELDS( table_dir_entry_fields
, &table
) )
204 FT_TRACE2(( "check_table_dir:"
205 " can read only %d table%s in font (instead of %d)\n",
206 nn
, nn
== 1 ? "" : "s", sfnt
->num_tables
));
207 sfnt
->num_tables
= nn
;
211 /* we ignore invalid tables */
213 if ( table
.Offset
> stream
->size
)
215 FT_TRACE2(( "check_table_dir: table entry %d invalid\n", nn
));
218 else if ( table
.Length
> stream
->size
- table
.Offset
)
220 /* Some tables have such a simple structure that clipping its */
221 /* contents is harmless. This also makes FreeType less sensitive */
222 /* to invalid table lengths (which programs like Acroread seem to */
223 /* ignore in general). */
225 if ( table
.Tag
== TTAG_hmtx
||
226 table
.Tag
== TTAG_vmtx
)
230 FT_TRACE2(( "check_table_dir: table entry %d invalid\n", nn
));
237 if ( table
.Tag
== TTAG_head
|| table
.Tag
== TTAG_bhed
)
242 #ifndef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
243 if ( table
.Tag
== TTAG_head
)
248 * The table length should be 0x36, but certain font tools make it
249 * 0x38, so we will just check that it is greater.
251 * Note that according to the specification, the table must be
252 * padded to 32-bit lengths, but this doesn't apply to the value of
253 * its `Length' field!
256 if ( table
.Length
< 0x36 )
258 FT_TRACE2(( "check_table_dir:"
259 " `head' or `bhed' table too small\n" ));
260 error
= FT_THROW( Table_Missing
);
264 if ( FT_STREAM_SEEK( table
.Offset
+ 12 ) ||
265 FT_READ_ULONG( magic
) )
268 if ( magic
!= 0x5F0F3CF5UL
)
269 FT_TRACE2(( "check_table_dir:"
270 " invalid magic number in `head' or `bhed' table\n"));
272 if ( FT_STREAM_SEEK( offset
+ ( nn
+ 1 ) * 16 ) )
275 else if ( table
.Tag
== TTAG_SING
)
277 else if ( table
.Tag
== TTAG_META
)
281 *valid
= valid_entries
;
283 if ( !valid_entries
)
285 FT_TRACE2(( "check_table_dir: no valid tables found\n" ));
286 error
= FT_THROW( Unknown_File_Format
);
290 /* if `sing' and `meta' tables are present, there is no `head' table */
291 if ( has_head
|| ( has_sing
&& has_meta
) )
298 FT_TRACE2(( "check_table_dir:" ));
299 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
300 FT_TRACE2(( " neither `head', `bhed', nor `sing' table found\n" ));
302 FT_TRACE2(( " neither `head' nor `sing' table found\n" ));
304 error
= FT_THROW( Table_Missing
);
312 /*************************************************************************/
315 /* tt_face_load_font_dir */
318 /* Loads the header of a SFNT font file. */
321 /* face :: A handle to the target face object. */
323 /* stream :: The input stream. */
326 /* sfnt :: The SFNT header. */
329 /* FreeType error code. 0 means success. */
332 /* The stream cursor must be at the beginning of the font directory. */
334 FT_LOCAL_DEF( FT_Error
)
335 tt_face_load_font_dir( TT_Face face
,
340 FT_Memory memory
= stream
->memory
;
341 FT_UShort nn
, valid_entries
;
343 static const FT_Frame_Field offset_table_fields
[] =
346 #define FT_STRUCTURE SFNT_HeaderRec
349 FT_FRAME_USHORT( num_tables
),
350 FT_FRAME_USHORT( search_range
),
351 FT_FRAME_USHORT( entry_selector
),
352 FT_FRAME_USHORT( range_shift
),
357 FT_TRACE2(( "tt_face_load_font_dir: %08p\n", face
));
359 /* read the offset table */
361 sfnt
.offset
= FT_STREAM_POS();
363 if ( FT_READ_ULONG( sfnt
.format_tag
) ||
364 FT_STREAM_READ_FIELDS( offset_table_fields
, &sfnt
) )
367 /* many fonts don't have these fields set correctly */
369 if ( sfnt
.search_range
!= 1 << ( sfnt
.entry_selector
+ 4 ) ||
370 sfnt
.search_range
+ sfnt
.range_shift
!= sfnt
.num_tables
<< 4 )
371 return FT_THROW( Unknown_File_Format
);
374 /* load the table directory */
376 FT_TRACE2(( "-- Number of tables: %10u\n", sfnt
.num_tables
));
377 FT_TRACE2(( "-- Format version: 0x%08lx\n", sfnt
.format_tag
));
379 if ( sfnt
.format_tag
!= TTAG_OTTO
)
382 error
= check_table_dir( &sfnt
, stream
, &valid_entries
);
385 FT_TRACE2(( "tt_face_load_font_dir:"
386 " invalid table directory for TrueType\n" ));
391 valid_entries
= sfnt
.num_tables
;
393 face
->num_tables
= valid_entries
;
394 face
->format_tag
= sfnt
.format_tag
;
396 if ( FT_QNEW_ARRAY( face
->dir_tables
, face
->num_tables
) )
399 if ( FT_STREAM_SEEK( sfnt
.offset
+ 12 ) ||
400 FT_FRAME_ENTER( sfnt
.num_tables
* 16L ) )
404 " tag offset length checksum\n"
405 " ----------------------------------\n" ));
408 for ( nn
= 0; nn
< sfnt
.num_tables
; nn
++ )
415 entry
.Tag
= FT_GET_TAG4();
416 entry
.CheckSum
= FT_GET_ULONG();
417 entry
.Offset
= FT_GET_ULONG();
418 entry
.Length
= FT_GET_ULONG();
420 /* ignore invalid tables that can't be sanitized */
422 if ( entry
.Offset
> stream
->size
)
424 else if ( entry
.Length
> stream
->size
- entry
.Offset
)
426 if ( entry
.Tag
== TTAG_hmtx
||
427 entry
.Tag
== TTAG_vmtx
)
429 #ifdef FT_DEBUG_LEVEL_TRACE
430 FT_ULong old_length
= entry
.Length
;
434 /* make metrics table length a multiple of 4 */
435 entry
.Length
= ( stream
->size
- entry
.Offset
) & ~3U;
437 FT_TRACE2(( " %c%c%c%c %08lx %08lx %08lx"
438 " (sanitized; original length %08lx)",
439 (FT_Char
)( entry
.Tag
>> 24 ),
440 (FT_Char
)( entry
.Tag
>> 16 ),
441 (FT_Char
)( entry
.Tag
>> 8 ),
442 (FT_Char
)( entry
.Tag
),
451 #ifdef FT_DEBUG_LEVEL_TRACE
453 FT_TRACE2(( " %c%c%c%c %08lx %08lx %08lx",
454 (FT_Char
)( entry
.Tag
>> 24 ),
455 (FT_Char
)( entry
.Tag
>> 16 ),
456 (FT_Char
)( entry
.Tag
>> 8 ),
457 (FT_Char
)( entry
.Tag
),
463 /* ignore duplicate tables – the first one wins */
465 for ( i
= 0; i
< valid_entries
; i
++ )
467 if ( face
->dir_tables
[i
].Tag
== entry
.Tag
)
475 FT_TRACE2(( " (duplicate, ignored)\n" ));
482 /* we finally have a valid entry */
483 face
->dir_tables
[valid_entries
++] = entry
;
487 /* final adjustment to number of tables */
488 face
->num_tables
= valid_entries
;
492 FT_TRACE2(( "table directory loaded\n\n" ));
499 /*************************************************************************/
502 /* tt_face_load_any */
505 /* Loads any font table into client memory. */
508 /* face :: The face object to look for. */
510 /* tag :: The tag of table to load. Use the value 0 if you want */
511 /* to access the whole font file, else set this parameter */
512 /* to a valid TrueType table tag that you can forge with */
513 /* the MAKE_TT_TAG macro. */
515 /* offset :: The starting offset in the table (or the file if */
518 /* length :: The address of the decision variable: */
520 /* If length == NULL: */
521 /* Loads the whole table. Returns an error if */
524 /* If *length == 0: */
525 /* Exits immediately; returning the length of the given */
526 /* table or of the font file, depending on the value of */
529 /* If *length != 0: */
530 /* Loads the next `length' bytes of table or font, */
531 /* starting at offset `offset' (in table or font too). */
534 /* buffer :: The address of target buffer. */
537 /* FreeType error code. 0 means success. */
539 FT_LOCAL_DEF( FT_Error
)
540 tt_face_load_any( TT_Face face
,
554 /* look for tag in font directory */
555 table
= tt_face_lookup_table( face
, tag
);
558 error
= FT_THROW( Table_Missing
);
562 offset
+= table
->Offset
;
563 size
= table
->Length
;
566 /* tag == 0 -- the user wants to access the font file directly */
567 size
= face
->root
.stream
->size
;
569 if ( length
&& *length
== 0 )
579 stream
= face
->root
.stream
;
580 /* the `if' is syntactic sugar for picky compilers */
581 if ( FT_STREAM_READ_AT( offset
, buffer
, size
) )
589 /*************************************************************************/
592 /* tt_face_load_generic_header */
595 /* Loads the TrueType table `head' or `bhed'. */
598 /* face :: A handle to the target face object. */
600 /* stream :: The input stream. */
603 /* FreeType error code. 0 means success. */
606 tt_face_load_generic_header( TT_Face face
,
613 static const FT_Frame_Field header_fields
[] =
616 #define FT_STRUCTURE TT_Header
618 FT_FRAME_START( 54 ),
619 FT_FRAME_ULONG ( Table_Version
),
620 FT_FRAME_ULONG ( Font_Revision
),
621 FT_FRAME_LONG ( CheckSum_Adjust
),
622 FT_FRAME_LONG ( Magic_Number
),
623 FT_FRAME_USHORT( Flags
),
624 FT_FRAME_USHORT( Units_Per_EM
),
625 FT_FRAME_LONG ( Created
[0] ),
626 FT_FRAME_LONG ( Created
[1] ),
627 FT_FRAME_LONG ( Modified
[0] ),
628 FT_FRAME_LONG ( Modified
[1] ),
629 FT_FRAME_SHORT ( xMin
),
630 FT_FRAME_SHORT ( yMin
),
631 FT_FRAME_SHORT ( xMax
),
632 FT_FRAME_SHORT ( yMax
),
633 FT_FRAME_USHORT( Mac_Style
),
634 FT_FRAME_USHORT( Lowest_Rec_PPEM
),
635 FT_FRAME_SHORT ( Font_Direction
),
636 FT_FRAME_SHORT ( Index_To_Loc_Format
),
637 FT_FRAME_SHORT ( Glyph_Data_Format
),
642 error
= face
->goto_table( face
, tag
, stream
, 0 );
646 header
= &face
->header
;
648 if ( FT_STREAM_READ_FIELDS( header_fields
, header
) )
651 FT_TRACE3(( "Units per EM: %4u\n", header
->Units_Per_EM
));
652 FT_TRACE3(( "IndexToLoc: %4d\n", header
->Index_To_Loc_Format
));
659 FT_LOCAL_DEF( FT_Error
)
660 tt_face_load_head( TT_Face face
,
663 return tt_face_load_generic_header( face
, stream
, TTAG_head
);
667 #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
669 FT_LOCAL_DEF( FT_Error
)
670 tt_face_load_bhed( TT_Face face
,
673 return tt_face_load_generic_header( face
, stream
, TTAG_bhed
);
676 #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
679 /*************************************************************************/
682 /* tt_face_load_max_profile */
685 /* Loads the maximum profile into a face object. */
688 /* face :: A handle to the target face object. */
690 /* stream :: The input stream. */
693 /* FreeType error code. 0 means success. */
695 FT_LOCAL_DEF( FT_Error
)
696 tt_face_load_maxp( TT_Face face
,
700 TT_MaxProfile
* maxProfile
= &face
->max_profile
;
702 static const FT_Frame_Field maxp_fields
[] =
705 #define FT_STRUCTURE TT_MaxProfile
708 FT_FRAME_LONG ( version
),
709 FT_FRAME_USHORT( numGlyphs
),
713 static const FT_Frame_Field maxp_fields_extra
[] =
715 FT_FRAME_START( 26 ),
716 FT_FRAME_USHORT( maxPoints
),
717 FT_FRAME_USHORT( maxContours
),
718 FT_FRAME_USHORT( maxCompositePoints
),
719 FT_FRAME_USHORT( maxCompositeContours
),
720 FT_FRAME_USHORT( maxZones
),
721 FT_FRAME_USHORT( maxTwilightPoints
),
722 FT_FRAME_USHORT( maxStorage
),
723 FT_FRAME_USHORT( maxFunctionDefs
),
724 FT_FRAME_USHORT( maxInstructionDefs
),
725 FT_FRAME_USHORT( maxStackElements
),
726 FT_FRAME_USHORT( maxSizeOfInstructions
),
727 FT_FRAME_USHORT( maxComponentElements
),
728 FT_FRAME_USHORT( maxComponentDepth
),
733 error
= face
->goto_table( face
, TTAG_maxp
, stream
, 0 );
737 if ( FT_STREAM_READ_FIELDS( maxp_fields
, maxProfile
) )
740 maxProfile
->maxPoints
= 0;
741 maxProfile
->maxContours
= 0;
742 maxProfile
->maxCompositePoints
= 0;
743 maxProfile
->maxCompositeContours
= 0;
744 maxProfile
->maxZones
= 0;
745 maxProfile
->maxTwilightPoints
= 0;
746 maxProfile
->maxStorage
= 0;
747 maxProfile
->maxFunctionDefs
= 0;
748 maxProfile
->maxInstructionDefs
= 0;
749 maxProfile
->maxStackElements
= 0;
750 maxProfile
->maxSizeOfInstructions
= 0;
751 maxProfile
->maxComponentElements
= 0;
752 maxProfile
->maxComponentDepth
= 0;
754 if ( maxProfile
->version
>= 0x10000L
)
756 if ( FT_STREAM_READ_FIELDS( maxp_fields_extra
, maxProfile
) )
759 /* XXX: an adjustment that is necessary to load certain */
760 /* broken fonts like `Keystrokes MT' :-( */
762 /* We allocate 64 function entries by default when */
763 /* the maxFunctionDefs value is smaller. */
765 if ( maxProfile
->maxFunctionDefs
< 64 )
766 maxProfile
->maxFunctionDefs
= 64;
768 /* we add 4 phantom points later */
769 if ( maxProfile
->maxTwilightPoints
> ( 0xFFFFU
- 4 ) )
771 FT_TRACE0(( "tt_face_load_maxp:"
772 " too much twilight points in `maxp' table;\n"
774 " some glyphs might be rendered incorrectly\n" ));
776 maxProfile
->maxTwilightPoints
= 0xFFFFU
- 4;
779 /* we arbitrarily limit recursion to avoid stack exhaustion */
780 if ( maxProfile
->maxComponentDepth
> 100 )
782 FT_TRACE0(( "tt_face_load_maxp:"
783 " abnormally large component depth (%d) set to 100\n",
784 maxProfile
->maxComponentDepth
));
785 maxProfile
->maxComponentDepth
= 100;
789 FT_TRACE3(( "numGlyphs: %u\n", maxProfile
->numGlyphs
));
796 /*************************************************************************/
799 /* tt_face_load_name */
802 /* Loads the name records. */
805 /* face :: A handle to the target face object. */
807 /* stream :: The input stream. */
810 /* FreeType error code. 0 means success. */
812 FT_LOCAL_DEF( FT_Error
)
813 tt_face_load_name( TT_Face face
,
817 FT_Memory memory
= stream
->memory
;
818 FT_ULong table_pos
, table_len
;
819 FT_ULong storage_start
, storage_limit
;
823 static const FT_Frame_Field name_table_fields
[] =
826 #define FT_STRUCTURE TT_NameTableRec
829 FT_FRAME_USHORT( format
),
830 FT_FRAME_USHORT( numNameRecords
),
831 FT_FRAME_USHORT( storageOffset
),
835 static const FT_Frame_Field name_record_fields
[] =
838 #define FT_STRUCTURE TT_NameEntryRec
840 /* no FT_FRAME_START */
841 FT_FRAME_USHORT( platformID
),
842 FT_FRAME_USHORT( encodingID
),
843 FT_FRAME_USHORT( languageID
),
844 FT_FRAME_USHORT( nameID
),
845 FT_FRAME_USHORT( stringLength
),
846 FT_FRAME_USHORT( stringOffset
),
851 table
= &face
->name_table
;
852 table
->stream
= stream
;
854 error
= face
->goto_table( face
, TTAG_name
, stream
, &table_len
);
858 table_pos
= FT_STREAM_POS();
861 if ( FT_STREAM_READ_FIELDS( name_table_fields
, table
) )
864 /* Some popular Asian fonts have an invalid `storageOffset' value */
865 /* (it should be at least "6 + 12*num_names"). However, the string */
866 /* offsets, computed as "storageOffset + entry->stringOffset", are */
867 /* valid pointers within the name table... */
869 /* We thus can't check `storageOffset' right now. */
871 storage_start
= table_pos
+ 6 + 12*table
->numNameRecords
;
872 storage_limit
= table_pos
+ table_len
;
874 if ( storage_start
> storage_limit
)
876 FT_ERROR(( "tt_face_load_name: invalid `name' table\n" ));
877 error
= FT_THROW( Name_Table_Missing
);
881 /* Allocate the array of name records. */
882 count
= table
->numNameRecords
;
883 table
->numNameRecords
= 0;
885 if ( FT_NEW_ARRAY( table
->names
, count
) ||
886 FT_FRAME_ENTER( count
* 12 ) )
889 /* Load the name records and determine how much storage is needed */
890 /* to hold the strings themselves. */
892 TT_NameEntryRec
* entry
= table
->names
;
895 for ( ; count
> 0; count
-- )
897 if ( FT_STREAM_READ_FIELDS( name_record_fields
, entry
) )
900 /* check that the name is not empty */
901 if ( entry
->stringLength
== 0 )
904 /* check that the name string is within the table */
905 entry
->stringOffset
+= table_pos
+ table
->storageOffset
;
906 if ( entry
->stringOffset
< storage_start
||
907 entry
->stringOffset
+ entry
->stringLength
> storage_limit
)
909 /* invalid entry - ignore it */
910 entry
->stringOffset
= 0;
911 entry
->stringLength
= 0;
918 table
->numNameRecords
= (FT_UInt
)( entry
- table
->names
);
923 /* everything went well, update face->num_names */
924 face
->num_names
= (FT_UShort
) table
->numNameRecords
;
931 /*************************************************************************/
934 /* tt_face_free_names */
937 /* Frees the name records. */
940 /* face :: A handle to the target face object. */
943 tt_face_free_name( TT_Face face
)
945 FT_Memory memory
= face
->root
.driver
->root
.memory
;
946 TT_NameTable table
= &face
->name_table
;
947 TT_NameEntry entry
= table
->names
;
948 FT_UInt count
= table
->numNameRecords
;
953 for ( ; count
> 0; count
--, entry
++ )
955 FT_FREE( entry
->string
);
956 entry
->stringLength
= 0;
959 /* free strings table */
960 FT_FREE( table
->names
);
963 table
->numNameRecords
= 0;
965 table
->storageOffset
= 0;
969 /*************************************************************************/
972 /* tt_face_load_cmap */
975 /* Loads the cmap directory in a face object. The cmaps themselves */
976 /* are loaded on demand in the `ttcmap.c' module. */
979 /* face :: A handle to the target face object. */
981 /* stream :: A handle to the input stream. */
984 /* FreeType error code. 0 means success. */
987 FT_LOCAL_DEF( FT_Error
)
988 tt_face_load_cmap( TT_Face face
,
994 error
= face
->goto_table( face
, TTAG_cmap
, stream
, &face
->cmap_size
);
998 if ( FT_FRAME_EXTRACT( face
->cmap_size
, face
->cmap_table
) )
1007 /*************************************************************************/
1010 /* tt_face_load_os2 */
1013 /* Loads the OS2 table. */
1016 /* face :: A handle to the target face object. */
1018 /* stream :: A handle to the input stream. */
1021 /* FreeType error code. 0 means success. */
1023 FT_LOCAL_DEF( FT_Error
)
1024 tt_face_load_os2( TT_Face face
,
1030 static const FT_Frame_Field os2_fields
[] =
1033 #define FT_STRUCTURE TT_OS2
1035 FT_FRAME_START( 78 ),
1036 FT_FRAME_USHORT( version
),
1037 FT_FRAME_SHORT ( xAvgCharWidth
),
1038 FT_FRAME_USHORT( usWeightClass
),
1039 FT_FRAME_USHORT( usWidthClass
),
1040 FT_FRAME_SHORT ( fsType
),
1041 FT_FRAME_SHORT ( ySubscriptXSize
),
1042 FT_FRAME_SHORT ( ySubscriptYSize
),
1043 FT_FRAME_SHORT ( ySubscriptXOffset
),
1044 FT_FRAME_SHORT ( ySubscriptYOffset
),
1045 FT_FRAME_SHORT ( ySuperscriptXSize
),
1046 FT_FRAME_SHORT ( ySuperscriptYSize
),
1047 FT_FRAME_SHORT ( ySuperscriptXOffset
),
1048 FT_FRAME_SHORT ( ySuperscriptYOffset
),
1049 FT_FRAME_SHORT ( yStrikeoutSize
),
1050 FT_FRAME_SHORT ( yStrikeoutPosition
),
1051 FT_FRAME_SHORT ( sFamilyClass
),
1052 FT_FRAME_BYTE ( panose
[0] ),
1053 FT_FRAME_BYTE ( panose
[1] ),
1054 FT_FRAME_BYTE ( panose
[2] ),
1055 FT_FRAME_BYTE ( panose
[3] ),
1056 FT_FRAME_BYTE ( panose
[4] ),
1057 FT_FRAME_BYTE ( panose
[5] ),
1058 FT_FRAME_BYTE ( panose
[6] ),
1059 FT_FRAME_BYTE ( panose
[7] ),
1060 FT_FRAME_BYTE ( panose
[8] ),
1061 FT_FRAME_BYTE ( panose
[9] ),
1062 FT_FRAME_ULONG ( ulUnicodeRange1
),
1063 FT_FRAME_ULONG ( ulUnicodeRange2
),
1064 FT_FRAME_ULONG ( ulUnicodeRange3
),
1065 FT_FRAME_ULONG ( ulUnicodeRange4
),
1066 FT_FRAME_BYTE ( achVendID
[0] ),
1067 FT_FRAME_BYTE ( achVendID
[1] ),
1068 FT_FRAME_BYTE ( achVendID
[2] ),
1069 FT_FRAME_BYTE ( achVendID
[3] ),
1071 FT_FRAME_USHORT( fsSelection
),
1072 FT_FRAME_USHORT( usFirstCharIndex
),
1073 FT_FRAME_USHORT( usLastCharIndex
),
1074 FT_FRAME_SHORT ( sTypoAscender
),
1075 FT_FRAME_SHORT ( sTypoDescender
),
1076 FT_FRAME_SHORT ( sTypoLineGap
),
1077 FT_FRAME_USHORT( usWinAscent
),
1078 FT_FRAME_USHORT( usWinDescent
),
1082 /* `OS/2' version 1 and newer */
1083 static const FT_Frame_Field os2_fields_extra1
[] =
1085 FT_FRAME_START( 8 ),
1086 FT_FRAME_ULONG( ulCodePageRange1
),
1087 FT_FRAME_ULONG( ulCodePageRange2
),
1091 /* `OS/2' version 2 and newer */
1092 static const FT_Frame_Field os2_fields_extra2
[] =
1094 FT_FRAME_START( 10 ),
1095 FT_FRAME_SHORT ( sxHeight
),
1096 FT_FRAME_SHORT ( sCapHeight
),
1097 FT_FRAME_USHORT( usDefaultChar
),
1098 FT_FRAME_USHORT( usBreakChar
),
1099 FT_FRAME_USHORT( usMaxContext
),
1103 /* `OS/2' version 5 and newer */
1104 static const FT_Frame_Field os2_fields_extra5
[] =
1106 FT_FRAME_START( 4 ),
1107 FT_FRAME_USHORT( usLowerOpticalPointSize
),
1108 FT_FRAME_USHORT( usUpperOpticalPointSize
),
1113 /* We now support old Mac fonts where the OS/2 table doesn't */
1114 /* exist. Simply put, we set the `version' field to 0xFFFF */
1115 /* and test this value each time we need to access the table. */
1116 error
= face
->goto_table( face
, TTAG_OS2
, stream
, 0 );
1122 if ( FT_STREAM_READ_FIELDS( os2_fields
, os2
) )
1125 os2
->ulCodePageRange1
= 0;
1126 os2
->ulCodePageRange2
= 0;
1128 os2
->sCapHeight
= 0;
1129 os2
->usDefaultChar
= 0;
1130 os2
->usBreakChar
= 0;
1131 os2
->usMaxContext
= 0;
1132 os2
->usLowerOpticalPointSize
= 0;
1133 os2
->usUpperOpticalPointSize
= 0xFFFF;
1135 if ( os2
->version
>= 0x0001 )
1137 /* only version 1 tables */
1138 if ( FT_STREAM_READ_FIELDS( os2_fields_extra1
, os2
) )
1141 if ( os2
->version
>= 0x0002 )
1143 /* only version 2 tables */
1144 if ( FT_STREAM_READ_FIELDS( os2_fields_extra2
, os2
) )
1147 if ( os2
->version
>= 0x0005 )
1149 /* only version 5 tables */
1150 if ( FT_STREAM_READ_FIELDS( os2_fields_extra5
, os2
) )
1156 FT_TRACE3(( "sTypoAscender: %4d\n", os2
->sTypoAscender
));
1157 FT_TRACE3(( "sTypoDescender: %4d\n", os2
->sTypoDescender
));
1158 FT_TRACE3(( "usWinAscent: %4u\n", os2
->usWinAscent
));
1159 FT_TRACE3(( "usWinDescent: %4u\n", os2
->usWinDescent
));
1160 FT_TRACE3(( "fsSelection: 0x%2x\n", os2
->fsSelection
));
1167 /*************************************************************************/
1170 /* tt_face_load_postscript */
1173 /* Loads the Postscript table. */
1176 /* face :: A handle to the target face object. */
1178 /* stream :: A handle to the input stream. */
1181 /* FreeType error code. 0 means success. */
1183 FT_LOCAL_DEF( FT_Error
)
1184 tt_face_load_post( TT_Face face
,
1188 TT_Postscript
* post
= &face
->postscript
;
1190 static const FT_Frame_Field post_fields
[] =
1193 #define FT_STRUCTURE TT_Postscript
1195 FT_FRAME_START( 32 ),
1196 FT_FRAME_ULONG( FormatType
),
1197 FT_FRAME_ULONG( italicAngle
),
1198 FT_FRAME_SHORT( underlinePosition
),
1199 FT_FRAME_SHORT( underlineThickness
),
1200 FT_FRAME_ULONG( isFixedPitch
),
1201 FT_FRAME_ULONG( minMemType42
),
1202 FT_FRAME_ULONG( maxMemType42
),
1203 FT_FRAME_ULONG( minMemType1
),
1204 FT_FRAME_ULONG( maxMemType1
),
1209 error
= face
->goto_table( face
, TTAG_post
, stream
, 0 );
1213 if ( FT_STREAM_READ_FIELDS( post_fields
, post
) )
1216 /* we don't load the glyph names, we do that in another */
1217 /* module (ttpost). */
1219 FT_TRACE3(( "FormatType: 0x%x\n", post
->FormatType
));
1220 FT_TRACE3(( "isFixedPitch: %s\n", post
->isFixedPitch
1221 ? " yes" : " no" ));
1227 /*************************************************************************/
1230 /* tt_face_load_pclt */
1233 /* Loads the PCL 5 Table. */
1236 /* face :: A handle to the target face object. */
1238 /* stream :: A handle to the input stream. */
1241 /* FreeType error code. 0 means success. */
1243 FT_LOCAL_DEF( FT_Error
)
1244 tt_face_load_pclt( TT_Face face
,
1247 static const FT_Frame_Field pclt_fields
[] =
1250 #define FT_STRUCTURE TT_PCLT
1252 FT_FRAME_START( 54 ),
1253 FT_FRAME_ULONG ( Version
),
1254 FT_FRAME_ULONG ( FontNumber
),
1255 FT_FRAME_USHORT( Pitch
),
1256 FT_FRAME_USHORT( xHeight
),
1257 FT_FRAME_USHORT( Style
),
1258 FT_FRAME_USHORT( TypeFamily
),
1259 FT_FRAME_USHORT( CapHeight
),
1260 FT_FRAME_USHORT( SymbolSet
),
1261 FT_FRAME_BYTES ( TypeFace
, 16 ),
1262 FT_FRAME_BYTES ( CharacterComplement
, 8 ),
1263 FT_FRAME_BYTES ( FileName
, 6 ),
1264 FT_FRAME_CHAR ( StrokeWeight
),
1265 FT_FRAME_CHAR ( WidthType
),
1266 FT_FRAME_BYTE ( SerifStyle
),
1267 FT_FRAME_BYTE ( Reserved
),
1272 TT_PCLT
* pclt
= &face
->pclt
;
1275 /* optional table */
1276 error
= face
->goto_table( face
, TTAG_PCLT
, stream
, 0 );
1280 if ( FT_STREAM_READ_FIELDS( pclt_fields
, pclt
) )
1288 /*************************************************************************/
1291 /* tt_face_load_gasp */
1294 /* Loads the `gasp' table into a face object. */
1297 /* face :: A handle to the target face object. */
1299 /* stream :: The input stream. */
1302 /* FreeType error code. 0 means success. */
1304 FT_LOCAL_DEF( FT_Error
)
1305 tt_face_load_gasp( TT_Face face
,
1309 FT_Memory memory
= stream
->memory
;
1311 FT_UInt j
,num_ranges
;
1312 TT_GaspRange gaspranges
= NULL
;
1315 /* the gasp table is optional */
1316 error
= face
->goto_table( face
, TTAG_gasp
, stream
, 0 );
1320 if ( FT_FRAME_ENTER( 4L ) )
1323 face
->gasp
.version
= FT_GET_USHORT();
1324 face
->gasp
.numRanges
= FT_GET_USHORT();
1328 /* only support versions 0 and 1 of the table */
1329 if ( face
->gasp
.version
>= 2 )
1331 face
->gasp
.numRanges
= 0;
1332 error
= FT_THROW( Invalid_Table
);
1336 num_ranges
= face
->gasp
.numRanges
;
1337 FT_TRACE3(( "numRanges: %u\n", num_ranges
));
1339 if ( FT_QNEW_ARRAY( face
->gasp
.gaspRanges
, num_ranges
) ||
1340 FT_FRAME_ENTER( num_ranges
* 4L ) )
1343 gaspranges
= face
->gasp
.gaspRanges
;
1345 for ( j
= 0; j
< num_ranges
; j
++ )
1347 gaspranges
[j
].maxPPEM
= FT_GET_USHORT();
1348 gaspranges
[j
].gaspFlag
= FT_GET_USHORT();
1350 FT_TRACE3(( "gaspRange %d: rangeMaxPPEM %5d, rangeGaspBehavior 0x%x\n",
1352 gaspranges
[j
].maxPPEM
,
1353 gaspranges
[j
].gaspFlag
));