1 /****************************************************************************
5 * TrueType-specific tables loader (body).
7 * Copyright (C) 1996-2020 by
8 * David Turner, Robert Wilhelm, and Werner Lemberg.
10 * This file is part of the FreeType project, and may only be used,
11 * modified, and distributed under the terms of the FreeType project
12 * license, LICENSE.TXT. By continuing to use, modify, or distribute
13 * this file you indicate that you have read the license and
14 * understand and accept it fully.
19 #include <freetype/internal/ftdebug.h>
20 #include <freetype/internal/ftobjs.h>
21 #include <freetype/internal/ftstream.h>
22 #include <freetype/tttags.h>
26 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
33 /**************************************************************************
35 * The macro FT_COMPONENT is used in trace mode. It is an implicit
36 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
37 * messages during execution.
40 #define FT_COMPONENT ttpload
43 /**************************************************************************
49 * Load the locations table.
53 * A handle to the target face object.
60 * FreeType error code. 0 means success.
62 FT_LOCAL_DEF( FT_Error )
63 tt_face_load_loca( TT_Face face,
71 /* we need the size of the `glyf' table for malformed `loca' tables */
72 error = face->goto_table( face, TTAG_glyf, stream, &face->glyf_len );
74 /* it is possible that a font doesn't have a glyf table at all */
75 /* or its size is zero */
76 if ( FT_ERR_EQ( error, Table_Missing ) )
79 face->glyf_offset = 0;
85 #ifdef FT_CONFIG_OPTION_INCREMENTAL
86 if ( face->root.internal->incremental_interface )
87 face->glyf_offset = 0;
90 face->glyf_offset = FT_STREAM_POS();
93 FT_TRACE2(( "Locations " ));
94 error = face->goto_table( face, TTAG_loca, stream, &table_len );
97 error = FT_THROW( Locations_Missing );
101 if ( face->header.Index_To_Loc_Format != 0 )
105 if ( table_len >= 0x40000L )
107 FT_TRACE2(( "table too large\n" ));
108 table_len = 0x3FFFFL;
110 face->num_locations = table_len >> shift;
116 if ( table_len >= 0x20000L )
118 FT_TRACE2(( "table too large\n" ));
119 table_len = 0x1FFFFL;
121 face->num_locations = table_len >> shift;
124 if ( face->num_locations != (FT_ULong)face->root.num_glyphs + 1 )
126 FT_TRACE2(( "glyph count mismatch! loca: %ld, maxp: %ld\n",
127 face->num_locations - 1, face->root.num_glyphs ));
129 /* we only handle the case where `maxp' gives a larger value */
130 if ( face->num_locations <= (FT_ULong)face->root.num_glyphs )
132 FT_ULong new_loca_len =
133 ( (FT_ULong)face->root.num_glyphs + 1 ) << shift;
135 TT_Table entry = face->dir_tables;
136 TT_Table limit = entry + face->num_tables;
138 FT_Long pos = (FT_Long)FT_STREAM_POS();
139 FT_Long dist = 0x7FFFFFFFL;
143 /* compute the distance to next table in font file */
144 for ( ; entry < limit; entry++ )
146 FT_Long diff = (FT_Long)entry->Offset - pos;
149 if ( diff > 0 && diff < dist )
158 /* `loca' is the last table */
159 dist = (FT_Long)stream->size - pos;
162 if ( new_loca_len <= (FT_ULong)dist )
164 face->num_locations = (FT_ULong)face->root.num_glyphs + 1;
165 table_len = new_loca_len;
167 FT_TRACE2(( "adjusting num_locations to %ld\n",
168 face->num_locations ));
172 face->root.num_glyphs = face->num_locations
173 ? (FT_Long)face->num_locations - 1 : 0;
175 FT_TRACE2(( "adjusting num_glyphs to %ld\n",
176 face->root.num_glyphs ));
182 * Extract the frame. We don't need to decompress it since
183 * we are able to parse it directly.
185 if ( FT_FRAME_EXTRACT( table_len, face->glyph_locations ) )
188 FT_TRACE2(( "loaded\n" ));
195 FT_LOCAL_DEF( FT_ULong )
196 tt_face_get_location( TT_Face face,
207 if ( gindex < face->num_locations )
209 if ( face->header.Index_To_Loc_Format != 0 )
211 p = face->glyph_locations + gindex * 4;
212 p_limit = face->glyph_locations + face->num_locations * 4;
214 pos1 = FT_NEXT_ULONG( p );
217 if ( p + 4 <= p_limit )
218 pos2 = FT_NEXT_ULONG( p );
222 p = face->glyph_locations + gindex * 2;
223 p_limit = face->glyph_locations + face->num_locations * 2;
225 pos1 = FT_NEXT_USHORT( p );
228 if ( p + 2 <= p_limit )
229 pos2 = FT_NEXT_USHORT( p );
236 /* Check broken location data. */
237 if ( pos1 > face->glyf_len )
239 FT_TRACE1(( "tt_face_get_location:"
240 " too large offset (0x%08lx) found for glyph index %d,\n"
242 " exceeding the end of `glyf' table (0x%08lx)\n",
243 pos1, gindex, face->glyf_len ));
248 if ( pos2 > face->glyf_len )
250 /* We try to sanitize the last `loca' entry. */
251 if ( gindex == face->num_locations - 2 )
253 FT_TRACE1(( "tt_face_get_location:"
254 " too large size (%ld bytes) found for glyph index %d,\n"
256 " truncating at the end of `glyf' table to %ld bytes\n",
257 pos2 - pos1, gindex, face->glyf_len - pos1 ));
258 pos2 = face->glyf_len;
262 FT_TRACE1(( "tt_face_get_location:"
263 " too large offset (0x%08lx) found for glyph index %d,\n"
265 " exceeding the end of `glyf' table (0x%08lx)\n",
266 pos2, gindex + 1, face->glyf_len ));
272 /* The `loca' table must be ordered; it refers to the length of */
273 /* an entry as the difference between the current and the next */
274 /* position. However, there do exist (malformed) fonts which */
275 /* don't obey this rule, so we are only able to provide an */
276 /* upper bound for the size. */
278 /* We get (intentionally) a wrong, non-zero result in case the */
279 /* `glyf' table is missing. */
281 *asize = (FT_UInt)( pos2 - pos1 );
283 *asize = (FT_UInt)( face->glyf_len - pos1 );
290 tt_face_done_loca( TT_Face face )
292 FT_Stream stream = face->root.stream;
295 FT_FRAME_RELEASE( face->glyph_locations );
296 face->num_locations = 0;
301 /**************************************************************************
307 * Load the control value table into a face object.
311 * A handle to the target face object.
315 * A handle to the input stream.
318 * FreeType error code. 0 means success.
320 FT_LOCAL_DEF( FT_Error )
321 tt_face_load_cvt( TT_Face face,
324 #ifdef TT_USE_BYTECODE_INTERPRETER
327 FT_Memory memory = stream->memory;
331 FT_TRACE2(( "CVT " ));
333 error = face->goto_table( face, TTAG_cvt, stream, &table_len );
336 FT_TRACE2(( "is missing\n" ));
345 face->cvt_size = table_len / 2;
347 if ( FT_NEW_ARRAY( face->cvt, face->cvt_size ) )
350 if ( FT_FRAME_ENTER( face->cvt_size * 2L ) )
354 FT_Int32* cur = face->cvt;
355 FT_Int32* limit = cur + face->cvt_size;
358 for ( ; cur < limit; cur++ )
359 *cur = FT_GET_SHORT() * 64;
363 FT_TRACE2(( "loaded\n" ));
365 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
367 error = tt_face_vary_cvt( face, stream );
373 #else /* !TT_USE_BYTECODE_INTERPRETER */
384 /**************************************************************************
390 * Load the font program.
394 * A handle to the target face object.
398 * A handle to the input stream.
401 * FreeType error code. 0 means success.
403 FT_LOCAL_DEF( FT_Error )
404 tt_face_load_fpgm( TT_Face face,
407 #ifdef TT_USE_BYTECODE_INTERPRETER
413 FT_TRACE2(( "Font program " ));
415 /* The font program is optional */
416 error = face->goto_table( face, TTAG_fpgm, stream, &table_len );
419 face->font_program = NULL;
420 face->font_program_size = 0;
423 FT_TRACE2(( "is missing\n" ));
427 face->font_program_size = table_len;
428 if ( FT_FRAME_EXTRACT( table_len, face->font_program ) )
431 FT_TRACE2(( "loaded, %12ld bytes\n", face->font_program_size ));
437 #else /* !TT_USE_BYTECODE_INTERPRETER */
448 /**************************************************************************
454 * Load the cvt program.
458 * A handle to the target face object.
462 * A handle to the input stream.
465 * FreeType error code. 0 means success.
467 FT_LOCAL_DEF( FT_Error )
468 tt_face_load_prep( TT_Face face,
471 #ifdef TT_USE_BYTECODE_INTERPRETER
477 FT_TRACE2(( "Prep program " ));
479 error = face->goto_table( face, TTAG_prep, stream, &table_len );
482 face->cvt_program = NULL;
483 face->cvt_program_size = 0;
486 FT_TRACE2(( "is missing\n" ));
490 face->cvt_program_size = table_len;
491 if ( FT_FRAME_EXTRACT( table_len, face->cvt_program ) )
494 FT_TRACE2(( "loaded, %12ld bytes\n", face->cvt_program_size ));
500 #else /* !TT_USE_BYTECODE_INTERPRETER */
511 /**************************************************************************
517 * Load the `hdmx' table into the face object.
521 * A handle to the target face object.
524 * A handle to the input stream.
527 * FreeType error code. 0 means success.
530 FT_LOCAL_DEF( FT_Error )
531 tt_face_load_hdmx( TT_Face face,
535 FT_Memory memory = stream->memory;
536 FT_UInt nn, num_records;
537 FT_ULong table_size, record_size;
542 /* this table is optional */
543 error = face->goto_table( face, TTAG_hdmx, stream, &table_size );
544 if ( error || table_size < 8 )
547 if ( FT_FRAME_EXTRACT( table_size, face->hdmx_table ) )
550 p = face->hdmx_table;
551 limit = p + table_size;
553 /* Given that `hdmx' tables are losing its importance (for example, */
554 /* variation fonts introduced in OpenType 1.8 must not have this */
555 /* table) we no longer test for a correct `version' field. */
557 num_records = FT_NEXT_USHORT( p );
558 record_size = FT_NEXT_ULONG( p );
560 /* The maximum number of bytes in an hdmx device record is the */
561 /* maximum number of glyphs + 2; this is 0xFFFF + 2, thus */
562 /* explaining why `record_size' is a long (which we read as */
563 /* unsigned long for convenience). In practice, two bytes are */
564 /* sufficient to hold the size value. */
566 /* There are at least two fonts, HANNOM-A and HANNOM-B version */
567 /* 2.0 (2005), which get this wrong: The upper two bytes of */
568 /* the size value are set to 0xFF instead of 0x00. We catch */
571 if ( record_size >= 0xFFFF0000UL )
572 record_size &= 0xFFFFU;
574 /* The limit for `num_records' is a heuristic value. */
575 if ( num_records > 255 ||
577 ( record_size > 0x10001L ||
578 record_size < 4 ) ) )
580 error = FT_THROW( Invalid_File_Format );
584 if ( FT_NEW_ARRAY( face->hdmx_record_sizes, num_records ) )
587 for ( nn = 0; nn < num_records; nn++ )
589 if ( p + record_size > limit )
592 face->hdmx_record_sizes[nn] = p[0];
596 face->hdmx_record_count = nn;
597 face->hdmx_table_size = table_size;
598 face->hdmx_record_size = record_size;
604 FT_FRAME_RELEASE( face->hdmx_table );
605 face->hdmx_table_size = 0;
611 tt_face_free_hdmx( TT_Face face )
613 FT_Stream stream = face->root.stream;
614 FT_Memory memory = stream->memory;
617 FT_FREE( face->hdmx_record_sizes );
618 FT_FRAME_RELEASE( face->hdmx_table );
622 /**************************************************************************
624 * Return the advance width table for a given pixel size if it is found
625 * in the font's `hdmx' table (if any).
627 FT_LOCAL_DEF( FT_Byte* )
628 tt_face_get_device_metrics( TT_Face face,
633 FT_Byte* result = NULL;
634 FT_ULong record_size = face->hdmx_record_size;
635 FT_Byte* record = FT_OFFSET( face->hdmx_table, 8 );
638 for ( nn = 0; nn < face->hdmx_record_count; nn++ )
639 if ( face->hdmx_record_sizes[nn] == ppem )
642 if ( gindex < record_size )
643 result = record + nn * record_size + gindex;