1 /***************************************************************************/
5 /* TrueType-specific tables loader (body). */
7 /* Copyright 1996-2016 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 /***************************************************************************/
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 trace_ttpload
44 /*************************************************************************/
47 /* tt_face_load_loca */
50 /* Load the locations table. */
53 /* face :: A handle to the target face object. */
56 /* stream :: The input stream. */
59 /* FreeType error code. 0 means success. */
61 FT_LOCAL_DEF( FT_Error )
62 tt_face_load_loca( TT_Face face,
70 /* we need the size of the `glyf' table for malformed `loca' tables */
71 error = face->goto_table( face, TTAG_glyf, stream, &face->glyf_len );
73 /* it is possible that a font doesn't have a glyf table at all */
74 /* or its size is zero */
75 if ( FT_ERR_EQ( error, Table_Missing ) )
80 FT_TRACE2(( "Locations " ));
81 error = face->goto_table( face, TTAG_loca, stream, &table_len );
84 error = FT_THROW( Locations_Missing );
88 if ( face->header.Index_To_Loc_Format != 0 )
92 if ( table_len >= 0x40000L )
94 FT_TRACE2(( "table too large\n" ));
95 error = FT_THROW( Invalid_Table );
98 face->num_locations = table_len >> shift;
104 if ( table_len >= 0x20000L )
106 FT_TRACE2(( "table too large\n" ));
107 error = FT_THROW( Invalid_Table );
110 face->num_locations = table_len >> shift;
113 if ( face->num_locations != (FT_ULong)face->root.num_glyphs + 1 )
115 FT_TRACE2(( "glyph count mismatch! loca: %d, maxp: %d\n",
116 face->num_locations - 1, face->root.num_glyphs ));
118 /* we only handle the case where `maxp' gives a larger value */
119 if ( face->num_locations <= (FT_ULong)face->root.num_glyphs )
121 FT_ULong new_loca_len =
122 ( (FT_ULong)face->root.num_glyphs + 1 ) << shift;
124 TT_Table entry = face->dir_tables;
125 TT_Table limit = entry + face->num_tables;
127 FT_Long pos = (FT_Long)FT_STREAM_POS();
128 FT_Long dist = 0x7FFFFFFFL;
132 /* compute the distance to next table in font file */
133 for ( ; entry < limit; entry++ )
135 FT_Long diff = (FT_Long)entry->Offset - pos;
138 if ( diff > 0 && diff < dist )
147 /* `loca' is the last table */
148 dist = (FT_Long)stream->size - pos;
151 if ( new_loca_len <= (FT_ULong)dist )
153 face->num_locations = (FT_ULong)face->root.num_glyphs + 1;
154 table_len = new_loca_len;
156 FT_TRACE2(( "adjusting num_locations to %d\n",
157 face->num_locations ));
161 face->root.num_glyphs = face->num_locations
162 ? (FT_Long)face->num_locations - 1 : 0;
164 FT_TRACE2(( "adjusting num_glyphs to %d\n",
165 face->root.num_glyphs ));
171 * Extract the frame. We don't need to decompress it since
172 * we are able to parse it directly.
174 if ( FT_FRAME_EXTRACT( table_len, face->glyph_locations ) )
177 FT_TRACE2(( "loaded\n" ));
184 FT_LOCAL_DEF( FT_ULong )
185 tt_face_get_location( TT_Face face,
196 if ( gindex < face->num_locations )
198 if ( face->header.Index_To_Loc_Format != 0 )
200 p = face->glyph_locations + gindex * 4;
201 p_limit = face->glyph_locations + face->num_locations * 4;
203 pos1 = FT_NEXT_ULONG( p );
206 if ( p + 4 <= p_limit )
207 pos2 = FT_NEXT_ULONG( p );
211 p = face->glyph_locations + gindex * 2;
212 p_limit = face->glyph_locations + face->num_locations * 2;
214 pos1 = FT_NEXT_USHORT( p );
217 if ( p + 2 <= p_limit )
218 pos2 = FT_NEXT_USHORT( p );
225 /* Check broken location data */
226 if ( pos1 > face->glyf_len )
228 FT_TRACE1(( "tt_face_get_location:"
229 " too large offset=0x%08lx found for gid=0x%04lx,\n"
231 " exceeding the end of glyf table (0x%08lx)\n",
232 pos1, gindex, face->glyf_len ));
237 if ( pos2 > face->glyf_len )
239 FT_TRACE1(( "tt_face_get_location:"
240 " too large offset=0x%08lx found for gid=0x%04lx,\n"
242 " truncate at the end of glyf table (0x%08lx)\n",
243 pos2, gindex + 1, face->glyf_len ));
244 pos2 = face->glyf_len;
247 /* The `loca' table must be ordered; it refers to the length of */
248 /* an entry as the difference between the current and the next */
249 /* position. However, there do exist (malformed) fonts which */
250 /* don't obey this rule, so we are only able to provide an */
251 /* upper bound for the size. */
253 /* We get (intentionally) a wrong, non-zero result in case the */
254 /* `glyf' table is missing. */
256 *asize = (FT_UInt)( pos2 - pos1 );
258 *asize = (FT_UInt)( face->glyf_len - pos1 );
265 tt_face_done_loca( TT_Face face )
267 FT_Stream stream = face->root.stream;
270 FT_FRAME_RELEASE( face->glyph_locations );
271 face->num_locations = 0;
276 /*************************************************************************/
279 /* tt_face_load_cvt */
282 /* Load the control value table into a face object. */
285 /* face :: A handle to the target face object. */
288 /* stream :: A handle to the input stream. */
291 /* FreeType error code. 0 means success. */
293 FT_LOCAL_DEF( FT_Error )
294 tt_face_load_cvt( TT_Face face,
297 #ifdef TT_USE_BYTECODE_INTERPRETER
300 FT_Memory memory = stream->memory;
304 FT_TRACE2(( "CVT " ));
306 error = face->goto_table( face, TTAG_cvt, stream, &table_len );
309 FT_TRACE2(( "is missing\n" ));
318 face->cvt_size = table_len / 2;
320 if ( FT_NEW_ARRAY( face->cvt, face->cvt_size ) )
323 if ( FT_FRAME_ENTER( face->cvt_size * 2L ) )
327 FT_Short* cur = face->cvt;
328 FT_Short* limit = cur + face->cvt_size;
331 for ( ; cur < limit; cur++ )
332 *cur = FT_GET_SHORT();
336 FT_TRACE2(( "loaded\n" ));
338 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
340 error = tt_face_vary_cvt( face, stream );
346 #else /* !TT_USE_BYTECODE_INTERPRETER */
357 /*************************************************************************/
360 /* tt_face_load_fpgm */
363 /* Load the font program. */
366 /* face :: A handle to the target face object. */
369 /* stream :: A handle to the input stream. */
372 /* FreeType error code. 0 means success. */
374 FT_LOCAL_DEF( FT_Error )
375 tt_face_load_fpgm( TT_Face face,
378 #ifdef TT_USE_BYTECODE_INTERPRETER
384 FT_TRACE2(( "Font program " ));
386 /* The font program is optional */
387 error = face->goto_table( face, TTAG_fpgm, stream, &table_len );
390 face->font_program = NULL;
391 face->font_program_size = 0;
394 FT_TRACE2(( "is missing\n" ));
398 face->font_program_size = table_len;
399 if ( FT_FRAME_EXTRACT( table_len, face->font_program ) )
402 FT_TRACE2(( "loaded, %12d bytes\n", face->font_program_size ));
408 #else /* !TT_USE_BYTECODE_INTERPRETER */
419 /*************************************************************************/
422 /* tt_face_load_prep */
425 /* Load the cvt program. */
428 /* face :: A handle to the target face object. */
431 /* stream :: A handle to the input stream. */
434 /* FreeType error code. 0 means success. */
436 FT_LOCAL_DEF( FT_Error )
437 tt_face_load_prep( TT_Face face,
440 #ifdef TT_USE_BYTECODE_INTERPRETER
446 FT_TRACE2(( "Prep program " ));
448 error = face->goto_table( face, TTAG_prep, stream, &table_len );
451 face->cvt_program = NULL;
452 face->cvt_program_size = 0;
455 FT_TRACE2(( "is missing\n" ));
459 face->cvt_program_size = table_len;
460 if ( FT_FRAME_EXTRACT( table_len, face->cvt_program ) )
463 FT_TRACE2(( "loaded, %12d bytes\n", face->cvt_program_size ));
469 #else /* !TT_USE_BYTECODE_INTERPRETER */
480 /*************************************************************************/
483 /* tt_face_load_hdmx */
486 /* Load the `hdmx' table into the face object. */
489 /* face :: A handle to the target face object. */
491 /* stream :: A handle to the input stream. */
494 /* FreeType error code. 0 means success. */
497 FT_LOCAL_DEF( FT_Error )
498 tt_face_load_hdmx( TT_Face face,
502 FT_Memory memory = stream->memory;
503 FT_UInt version, nn, num_records;
504 FT_ULong table_size, record_size;
509 /* this table is optional */
510 error = face->goto_table( face, TTAG_hdmx, stream, &table_size );
511 if ( error || table_size < 8 )
514 if ( FT_FRAME_EXTRACT( table_size, face->hdmx_table ) )
517 p = face->hdmx_table;
518 limit = p + table_size;
520 version = FT_NEXT_USHORT( p );
521 num_records = FT_NEXT_USHORT( p );
522 record_size = FT_NEXT_ULONG( p );
524 /* The maximum number of bytes in an hdmx device record is the */
525 /* maximum number of glyphs + 2; this is 0xFFFF + 2, thus */
526 /* explaining why `record_size' is a long (which we read as */
527 /* unsigned long for convenience). In practice, two bytes are */
528 /* sufficient to hold the size value. */
530 /* There are at least two fonts, HANNOM-A and HANNOM-B version */
531 /* 2.0 (2005), which get this wrong: The upper two bytes of */
532 /* the size value are set to 0xFF instead of 0x00. We catch */
535 if ( record_size >= 0xFFFF0000UL )
536 record_size &= 0xFFFFU;
538 /* The limit for `num_records' is a heuristic value. */
541 record_size > 0x10001L ||
544 error = FT_THROW( Invalid_File_Format );
548 if ( FT_NEW_ARRAY( face->hdmx_record_sizes, num_records ) )
551 for ( nn = 0; nn < num_records; nn++ )
553 if ( p + record_size > limit )
556 face->hdmx_record_sizes[nn] = p[0];
560 face->hdmx_record_count = nn;
561 face->hdmx_table_size = table_size;
562 face->hdmx_record_size = record_size;
568 FT_FRAME_RELEASE( face->hdmx_table );
569 face->hdmx_table_size = 0;
575 tt_face_free_hdmx( TT_Face face )
577 FT_Stream stream = face->root.stream;
578 FT_Memory memory = stream->memory;
581 FT_FREE( face->hdmx_record_sizes );
582 FT_FRAME_RELEASE( face->hdmx_table );
586 /*************************************************************************/
588 /* Return the advance width table for a given pixel size if it is found */
589 /* in the font's `hdmx' table (if any). */
591 FT_LOCAL_DEF( FT_Byte* )
592 tt_face_get_device_metrics( TT_Face face,
597 FT_Byte* result = NULL;
598 FT_ULong record_size = face->hdmx_record_size;
599 FT_Byte* record = face->hdmx_table + 8;
602 for ( nn = 0; nn < face->hdmx_record_count; nn++ )
603 if ( face->hdmx_record_sizes[nn] == ppem )
606 if ( gindex < record_size )
607 result = record + nn * record_size + gindex;