1 /****************************************************************************
5 * TrueType-specific tables loader (body).
7 * Copyright (C) 1996-2019 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.
20 #include FT_INTERNAL_DEBUG_H
21 #include FT_INTERNAL_OBJECTS_H
22 #include FT_INTERNAL_STREAM_H
23 #include FT_TRUETYPE_TAGS_H
27 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
34 /**************************************************************************
36 * The macro FT_COMPONENT is used in trace mode. It is an implicit
37 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
38 * messages during execution.
41 #define FT_COMPONENT ttpload
44 /**************************************************************************
50 * Load the locations table.
54 * A handle to the target face object.
61 * FreeType error code. 0 means success.
63 FT_LOCAL_DEF( FT_Error )
64 tt_face_load_loca( TT_Face face,
72 /* we need the size of the `glyf' table for malformed `loca' tables */
73 error = face->goto_table( face, TTAG_glyf, stream, &face->glyf_len );
75 /* it is possible that a font doesn't have a glyf table at all */
76 /* or its size is zero */
77 if ( FT_ERR_EQ( error, Table_Missing ) )
80 face->glyf_offset = 0;
86 #ifdef FT_CONFIG_OPTION_INCREMENTAL
87 if ( face->root.internal->incremental_interface )
88 face->glyf_offset = 0;
91 face->glyf_offset = FT_STREAM_POS();
94 FT_TRACE2(( "Locations " ));
95 error = face->goto_table( face, TTAG_loca, stream, &table_len );
98 error = FT_THROW( Locations_Missing );
102 if ( face->header.Index_To_Loc_Format != 0 )
106 if ( table_len >= 0x40000L )
108 FT_TRACE2(( "table too large\n" ));
109 table_len = 0x3FFFFL;
111 face->num_locations = table_len >> shift;
117 if ( table_len >= 0x20000L )
119 FT_TRACE2(( "table too large\n" ));
120 table_len = 0x1FFFFL;
122 face->num_locations = table_len >> shift;
125 if ( face->num_locations != (FT_ULong)face->root.num_glyphs + 1 )
127 FT_TRACE2(( "glyph count mismatch! loca: %d, maxp: %d\n",
128 face->num_locations - 1, face->root.num_glyphs ));
130 /* we only handle the case where `maxp' gives a larger value */
131 if ( face->num_locations <= (FT_ULong)face->root.num_glyphs )
133 FT_ULong new_loca_len =
134 ( (FT_ULong)face->root.num_glyphs + 1 ) << shift;
136 TT_Table entry = face->dir_tables;
137 TT_Table limit = entry + face->num_tables;
139 FT_Long pos = (FT_Long)FT_STREAM_POS();
140 FT_Long dist = 0x7FFFFFFFL;
144 /* compute the distance to next table in font file */
145 for ( ; entry < limit; entry++ )
147 FT_Long diff = (FT_Long)entry->Offset - pos;
150 if ( diff > 0 && diff < dist )
159 /* `loca' is the last table */
160 dist = (FT_Long)stream->size - pos;
163 if ( new_loca_len <= (FT_ULong)dist )
165 face->num_locations = (FT_ULong)face->root.num_glyphs + 1;
166 table_len = new_loca_len;
168 FT_TRACE2(( "adjusting num_locations to %d\n",
169 face->num_locations ));
173 face->root.num_glyphs = face->num_locations
174 ? (FT_Long)face->num_locations - 1 : 0;
176 FT_TRACE2(( "adjusting num_glyphs to %d\n",
177 face->root.num_glyphs ));
183 * Extract the frame. We don't need to decompress it since
184 * we are able to parse it directly.
186 if ( FT_FRAME_EXTRACT( table_len, face->glyph_locations ) )
189 FT_TRACE2(( "loaded\n" ));
196 FT_LOCAL_DEF( FT_ULong )
197 tt_face_get_location( TT_Face face,
208 if ( gindex < face->num_locations )
210 if ( face->header.Index_To_Loc_Format != 0 )
212 p = face->glyph_locations + gindex * 4;
213 p_limit = face->glyph_locations + face->num_locations * 4;
215 pos1 = FT_NEXT_ULONG( p );
218 if ( p + 4 <= p_limit )
219 pos2 = FT_NEXT_ULONG( p );
223 p = face->glyph_locations + gindex * 2;
224 p_limit = face->glyph_locations + face->num_locations * 2;
226 pos1 = FT_NEXT_USHORT( p );
229 if ( p + 2 <= p_limit )
230 pos2 = FT_NEXT_USHORT( p );
237 /* Check broken location data. */
238 if ( pos1 > face->glyf_len )
240 FT_TRACE1(( "tt_face_get_location:"
241 " too large offset (0x%08lx) found for glyph index %ld,\n"
243 " exceeding the end of `glyf' table (0x%08lx)\n",
244 pos1, gindex, face->glyf_len ));
249 if ( pos2 > face->glyf_len )
251 /* We try to sanitize the last `loca' entry. */
252 if ( gindex == face->num_locations - 2 )
254 FT_TRACE1(( "tt_face_get_location:"
255 " too large size (%ld bytes) found for glyph index %ld,\n"
257 " truncating at the end of `glyf' table to %ld bytes\n",
258 pos2 - pos1, gindex, face->glyf_len - pos1 ));
259 pos2 = face->glyf_len;
263 FT_TRACE1(( "tt_face_get_location:"
264 " too large offset (0x%08lx) found for glyph index %ld,\n"
266 " exceeding the end of `glyf' table (0x%08lx)\n",
267 pos2, gindex + 1, face->glyf_len ));
273 /* The `loca' table must be ordered; it refers to the length of */
274 /* an entry as the difference between the current and the next */
275 /* position. However, there do exist (malformed) fonts which */
276 /* don't obey this rule, so we are only able to provide an */
277 /* upper bound for the size. */
279 /* We get (intentionally) a wrong, non-zero result in case the */
280 /* `glyf' table is missing. */
282 *asize = (FT_UInt)( pos2 - pos1 );
284 *asize = (FT_UInt)( face->glyf_len - pos1 );
291 tt_face_done_loca( TT_Face face )
293 FT_Stream stream = face->root.stream;
296 FT_FRAME_RELEASE( face->glyph_locations );
297 face->num_locations = 0;
302 /**************************************************************************
308 * Load the control value table into a face object.
312 * A handle to the target face object.
316 * A handle to the input stream.
319 * FreeType error code. 0 means success.
321 FT_LOCAL_DEF( FT_Error )
322 tt_face_load_cvt( TT_Face face,
325 #ifdef TT_USE_BYTECODE_INTERPRETER
328 FT_Memory memory = stream->memory;
332 FT_TRACE2(( "CVT " ));
334 error = face->goto_table( face, TTAG_cvt, stream, &table_len );
337 FT_TRACE2(( "is missing\n" ));
346 face->cvt_size = table_len / 2;
348 if ( FT_NEW_ARRAY( face->cvt, face->cvt_size ) )
351 if ( FT_FRAME_ENTER( face->cvt_size * 2L ) )
355 FT_Int32* cur = face->cvt;
356 FT_Int32* limit = cur + face->cvt_size;
359 for ( ; cur < limit; cur++ )
360 *cur = FT_GET_SHORT() * 64;
364 FT_TRACE2(( "loaded\n" ));
366 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
368 error = tt_face_vary_cvt( face, stream );
374 #else /* !TT_USE_BYTECODE_INTERPRETER */
385 /**************************************************************************
391 * Load the font program.
395 * A handle to the target face object.
399 * A handle to the input stream.
402 * FreeType error code. 0 means success.
404 FT_LOCAL_DEF( FT_Error )
405 tt_face_load_fpgm( TT_Face face,
408 #ifdef TT_USE_BYTECODE_INTERPRETER
414 FT_TRACE2(( "Font program " ));
416 /* The font program is optional */
417 error = face->goto_table( face, TTAG_fpgm, stream, &table_len );
420 face->font_program = NULL;
421 face->font_program_size = 0;
424 FT_TRACE2(( "is missing\n" ));
428 face->font_program_size = table_len;
429 if ( FT_FRAME_EXTRACT( table_len, face->font_program ) )
432 FT_TRACE2(( "loaded, %12d bytes\n", face->font_program_size ));
438 #else /* !TT_USE_BYTECODE_INTERPRETER */
449 /**************************************************************************
455 * Load the cvt program.
459 * A handle to the target face object.
463 * A handle to the input stream.
466 * FreeType error code. 0 means success.
468 FT_LOCAL_DEF( FT_Error )
469 tt_face_load_prep( TT_Face face,
472 #ifdef TT_USE_BYTECODE_INTERPRETER
478 FT_TRACE2(( "Prep program " ));
480 error = face->goto_table( face, TTAG_prep, stream, &table_len );
483 face->cvt_program = NULL;
484 face->cvt_program_size = 0;
487 FT_TRACE2(( "is missing\n" ));
491 face->cvt_program_size = table_len;
492 if ( FT_FRAME_EXTRACT( table_len, face->cvt_program ) )
495 FT_TRACE2(( "loaded, %12d bytes\n", face->cvt_program_size ));
501 #else /* !TT_USE_BYTECODE_INTERPRETER */
512 /**************************************************************************
518 * Load the `hdmx' table into the face object.
522 * A handle to the target face object.
525 * A handle to the input stream.
528 * FreeType error code. 0 means success.
531 FT_LOCAL_DEF( FT_Error )
532 tt_face_load_hdmx( TT_Face face,
536 FT_Memory memory = stream->memory;
537 FT_UInt nn, num_records;
538 FT_ULong table_size, record_size;
543 /* this table is optional */
544 error = face->goto_table( face, TTAG_hdmx, stream, &table_size );
545 if ( error || table_size < 8 )
548 if ( FT_FRAME_EXTRACT( table_size, face->hdmx_table ) )
551 p = face->hdmx_table;
552 limit = p + table_size;
554 /* Given that `hdmx' tables are losing its importance (for example, */
555 /* variation fonts introduced in OpenType 1.8 must not have this */
556 /* table) we no longer test for a correct `version' field. */
558 num_records = FT_NEXT_USHORT( p );
559 record_size = FT_NEXT_ULONG( p );
561 /* The maximum number of bytes in an hdmx device record is the */
562 /* maximum number of glyphs + 2; this is 0xFFFF + 2, thus */
563 /* explaining why `record_size' is a long (which we read as */
564 /* unsigned long for convenience). In practice, two bytes are */
565 /* sufficient to hold the size value. */
567 /* There are at least two fonts, HANNOM-A and HANNOM-B version */
568 /* 2.0 (2005), which get this wrong: The upper two bytes of */
569 /* the size value are set to 0xFF instead of 0x00. We catch */
572 if ( record_size >= 0xFFFF0000UL )
573 record_size &= 0xFFFFU;
575 /* The limit for `num_records' is a heuristic value. */
576 if ( num_records > 255 ||
578 ( record_size > 0x10001L ||
579 record_size < 4 ) ) )
581 error = FT_THROW( Invalid_File_Format );
585 if ( FT_NEW_ARRAY( face->hdmx_record_sizes, num_records ) )
588 for ( nn = 0; nn < num_records; nn++ )
590 if ( p + record_size > limit )
593 face->hdmx_record_sizes[nn] = p[0];
597 face->hdmx_record_count = nn;
598 face->hdmx_table_size = table_size;
599 face->hdmx_record_size = record_size;
605 FT_FRAME_RELEASE( face->hdmx_table );
606 face->hdmx_table_size = 0;
612 tt_face_free_hdmx( TT_Face face )
614 FT_Stream stream = face->root.stream;
615 FT_Memory memory = stream->memory;
618 FT_FREE( face->hdmx_record_sizes );
619 FT_FRAME_RELEASE( face->hdmx_table );
623 /**************************************************************************
625 * Return the advance width table for a given pixel size if it is found
626 * in the font's `hdmx' table (if any).
628 FT_LOCAL_DEF( FT_Byte* )
629 tt_face_get_device_metrics( TT_Face face,
634 FT_Byte* result = NULL;
635 FT_ULong record_size = face->hdmx_record_size;
636 FT_Byte* record = face->hdmx_table + 8;
639 for ( nn = 0; nn < face->hdmx_record_count; nn++ )
640 if ( face->hdmx_record_sizes[nn] == ppem )
643 if ( gindex < record_size )
644 result = record + nn * record_size + gindex;