1 /****************************************************************************
5 * TrueType-specific tables loader (body).
7 * Copyright (C) 1996-2023 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 shift = face->header.Index_To_Loc_Format != 0 ? 2 : 1;
103 if ( table_len > 0x10000UL << shift )
105 FT_TRACE2(( "table too large\n" ));
106 table_len = 0x10000UL << shift;
109 face->num_locations = table_len >> shift;
111 if ( face->num_locations != (FT_ULong)face->root.num_glyphs + 1 )
113 FT_TRACE2(( "glyph count mismatch! loca: %ld, maxp: %ld\n",
114 face->num_locations - 1, face->root.num_glyphs ));
116 /* we only handle the case where `maxp' gives a larger value */
117 if ( face->num_locations < (FT_ULong)face->root.num_glyphs + 1 )
119 FT_ULong new_loca_len =
120 ( (FT_ULong)face->root.num_glyphs + 1 ) << shift;
122 TT_Table entry = face->dir_tables;
123 TT_Table limit = entry + face->num_tables;
125 FT_Long pos = (FT_Long)FT_STREAM_POS();
126 FT_Long dist = 0x7FFFFFFFL;
130 /* compute the distance to next table in font file */
131 for ( ; entry < limit; entry++ )
133 FT_Long diff = (FT_Long)entry->Offset - pos;
136 if ( diff > 0 && diff < dist )
145 /* `loca' is the last table */
146 dist = (FT_Long)stream->size - pos;
149 if ( new_loca_len <= (FT_ULong)dist )
151 face->num_locations = (FT_ULong)face->root.num_glyphs + 1;
152 table_len = new_loca_len;
154 FT_TRACE2(( "adjusting num_locations to %ld\n",
155 face->num_locations ));
159 face->root.num_glyphs = face->num_locations
160 ? (FT_Long)face->num_locations - 1 : 0;
162 FT_TRACE2(( "adjusting num_glyphs to %ld\n",
163 face->root.num_glyphs ));
169 * Extract the frame. We don't need to decompress it since
170 * we are able to parse it directly.
172 if ( FT_FRAME_EXTRACT( table_len, face->glyph_locations ) )
175 FT_TRACE2(( "loaded\n" ));
182 FT_LOCAL_DEF( FT_ULong )
183 tt_face_get_location( FT_Face face, /* TT_Face */
187 TT_Face ttface = (TT_Face)face;
195 if ( gindex < ttface->num_locations )
197 if ( ttface->header.Index_To_Loc_Format != 0 )
199 p = ttface->glyph_locations + gindex * 4;
200 p_limit = ttface->glyph_locations + ttface->num_locations * 4;
202 pos1 = FT_NEXT_ULONG( p );
205 if ( p + 4 <= p_limit )
206 pos2 = FT_NEXT_ULONG( p );
210 p = ttface->glyph_locations + gindex * 2;
211 p_limit = ttface->glyph_locations + ttface->num_locations * 2;
213 pos1 = FT_NEXT_USHORT( p );
216 if ( p + 2 <= p_limit )
217 pos2 = FT_NEXT_USHORT( p );
224 /* Check broken location data. */
225 if ( pos1 > ttface->glyf_len )
227 FT_TRACE1(( "tt_face_get_location:"
228 " too large offset (0x%08lx) found for glyph index %d,\n",
231 " exceeding the end of `glyf' table (0x%08lx)\n",
237 if ( pos2 > ttface->glyf_len )
239 /* We try to sanitize the last `loca' entry. */
240 if ( gindex == ttface->num_locations - 2 )
242 FT_TRACE1(( "tt_face_get_location:"
243 " too large size (%ld bytes) found for glyph index %d,\n",
244 pos2 - pos1, gindex ));
246 " truncating at the end of `glyf' table to %ld bytes\n",
247 ttface->glyf_len - pos1 ));
248 pos2 = ttface->glyf_len;
252 FT_TRACE1(( "tt_face_get_location:"
253 " too large offset (0x%08lx) found for glyph index %d,\n",
256 " exceeding the end of `glyf' table (0x%08lx)\n",
263 /* The `loca' table must be ordered; it refers to the length of */
264 /* an entry as the difference between the current and the next */
265 /* position. However, there do exist (malformed) fonts which */
266 /* don't obey this rule, so we are only able to provide an */
267 /* upper bound for the size. */
269 /* We get (intentionally) a wrong, non-zero result in case the */
270 /* `glyf' table is missing. */
272 *asize = (FT_ULong)( pos2 - pos1 );
274 *asize = (FT_ULong)( ttface->glyf_len - pos1 );
281 tt_face_done_loca( TT_Face face )
283 FT_Stream stream = face->root.stream;
286 FT_FRAME_RELEASE( face->glyph_locations );
287 face->num_locations = 0;
292 /**************************************************************************
298 * Load the control value table into a face object.
302 * A handle to the target face object.
306 * A handle to the input stream.
309 * FreeType error code. 0 means success.
311 FT_LOCAL_DEF( FT_Error )
312 tt_face_load_cvt( TT_Face face,
315 #ifdef TT_USE_BYTECODE_INTERPRETER
318 FT_Memory memory = stream->memory;
322 FT_TRACE2(( "CVT " ));
324 error = face->goto_table( face, TTAG_cvt, stream, &table_len );
327 FT_TRACE2(( "is missing\n" ));
336 face->cvt_size = table_len / 2;
338 if ( FT_QNEW_ARRAY( face->cvt, face->cvt_size ) )
341 if ( FT_FRAME_ENTER( face->cvt_size * 2L ) )
345 FT_Int32* cur = face->cvt;
346 FT_Int32* limit = cur + face->cvt_size;
349 for ( ; cur < limit; cur++ )
350 *cur = FT_GET_SHORT() * 64;
354 FT_TRACE2(( "loaded\n" ));
356 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
358 error = tt_face_vary_cvt( face, stream );
364 #else /* !TT_USE_BYTECODE_INTERPRETER */
375 /**************************************************************************
381 * Load the font program.
385 * A handle to the target face object.
389 * A handle to the input stream.
392 * FreeType error code. 0 means success.
394 FT_LOCAL_DEF( FT_Error )
395 tt_face_load_fpgm( TT_Face face,
398 #ifdef TT_USE_BYTECODE_INTERPRETER
404 FT_TRACE2(( "Font program " ));
406 /* The font program is optional */
407 error = face->goto_table( face, TTAG_fpgm, stream, &table_len );
410 face->font_program = NULL;
411 face->font_program_size = 0;
414 FT_TRACE2(( "is missing\n" ));
418 face->font_program_size = table_len;
419 if ( FT_FRAME_EXTRACT( table_len, face->font_program ) )
422 FT_TRACE2(( "loaded, %12ld bytes\n", face->font_program_size ));
428 #else /* !TT_USE_BYTECODE_INTERPRETER */
439 /**************************************************************************
445 * Load the cvt program.
449 * A handle to the target face object.
453 * A handle to the input stream.
456 * FreeType error code. 0 means success.
458 FT_LOCAL_DEF( FT_Error )
459 tt_face_load_prep( TT_Face face,
462 #ifdef TT_USE_BYTECODE_INTERPRETER
468 FT_TRACE2(( "Prep program " ));
470 error = face->goto_table( face, TTAG_prep, stream, &table_len );
473 face->cvt_program = NULL;
474 face->cvt_program_size = 0;
477 FT_TRACE2(( "is missing\n" ));
481 face->cvt_program_size = table_len;
482 if ( FT_FRAME_EXTRACT( table_len, face->cvt_program ) )
485 FT_TRACE2(( "loaded, %12ld bytes\n", face->cvt_program_size ));
491 #else /* !TT_USE_BYTECODE_INTERPRETER */
502 FT_COMPARE_DEF( int )
503 compare_ppem( const void* a,
506 return **(FT_Byte**)a - **(FT_Byte**)b;
510 /**************************************************************************
516 * Load the `hdmx' table into the face object.
520 * A handle to the target face object.
523 * A handle to the input stream.
526 * FreeType error code. 0 means success.
529 FT_LOCAL_DEF( FT_Error )
530 tt_face_load_hdmx( TT_Face face,
534 FT_Memory memory = stream->memory;
535 FT_UInt nn, num_records;
536 FT_ULong table_size, record_size;
541 /* this table is optional */
542 error = face->goto_table( face, TTAG_hdmx, stream, &table_size );
543 if ( error || table_size < 8 )
546 if ( FT_FRAME_EXTRACT( table_size, face->hdmx_table ) )
549 p = face->hdmx_table;
550 limit = p + table_size;
552 /* Given that `hdmx' tables are losing its importance (for example, */
553 /* variation fonts introduced in OpenType 1.8 must not have this */
554 /* table) we no longer test for a correct `version' field. */
556 num_records = FT_NEXT_USHORT( p );
557 record_size = FT_NEXT_ULONG( p );
559 /* There are at least two fonts, HANNOM-A and HANNOM-B version */
560 /* 2.0 (2005), which get this wrong: The upper two bytes of */
561 /* the size value are set to 0xFF instead of 0x00. We catch */
564 if ( record_size >= 0xFFFF0000UL )
565 record_size &= 0xFFFFU;
567 FT_TRACE2(( "Hdmx " ));
569 /* The limit for `num_records' is a heuristic value. */
570 if ( num_records > 255 || num_records == 0 )
572 FT_TRACE2(( "with unreasonable %u records rejected\n", num_records ));
576 /* Out-of-spec tables are rejected. The record size must be */
577 /* equal to the number of glyphs + 2 + 32-bit padding. */
578 if ( (FT_Long)record_size != ( ( face->root.num_glyphs + 2 + 3 ) & ~3 ) )
580 FT_TRACE2(( "with record size off by %ld bytes rejected\n",
581 (FT_Long)record_size -
582 ( ( face->root.num_glyphs + 2 + 3 ) & ~3 ) ));
586 if ( FT_QNEW_ARRAY( face->hdmx_records, num_records ) )
589 for ( nn = 0; nn < num_records; nn++ )
591 if ( p + record_size > limit )
593 face->hdmx_records[nn] = p;
597 /* The records must be already sorted by ppem but it does not */
598 /* hurt to make sure so that the binary search works later. */
599 ft_qsort( face->hdmx_records, nn, sizeof ( FT_Byte* ), compare_ppem );
601 face->hdmx_record_count = nn;
602 face->hdmx_table_size = table_size;
603 face->hdmx_record_size = record_size;
605 FT_TRACE2(( "%ux%lu loaded\n", num_records, record_size ));
611 FT_FRAME_RELEASE( face->hdmx_table );
612 face->hdmx_table_size = 0;
618 tt_face_free_hdmx( TT_Face face )
620 FT_Stream stream = face->root.stream;
621 FT_Memory memory = stream->memory;
624 FT_FREE( face->hdmx_records );
625 FT_FRAME_RELEASE( face->hdmx_table );
629 /**************************************************************************
631 * Return the advance width table for a given pixel size if it is found
632 * in the font's `hdmx' table (if any). The records must be sorted for
633 * the binary search to work properly.
635 FT_LOCAL_DEF( FT_Byte* )
636 tt_face_get_device_metrics( TT_Face face,
641 FT_UInt max = face->hdmx_record_count;
643 FT_Byte* result = NULL;
648 mid = ( min + max ) >> 1;
650 if ( face->hdmx_records[mid][0] > ppem )
652 else if ( face->hdmx_records[mid][0] < ppem )
656 result = face->hdmx_records[mid] + 2 + gindex;