1 /***************************************************************************/
5 /* TrueType-specific tables loader (body). */
7 /* Copyright 1996-2002, 2004-2013 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_Long new_loca_len =
122 ( (FT_Long)( 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_Stream_Pos( stream );
128 FT_Long dist = 0x7FFFFFFFL;
131 /* compute the distance to next table in font file */
132 for ( ; entry < limit; entry++ )
134 FT_Long diff = entry->Offset - pos;
137 if ( diff > 0 && diff < dist )
141 if ( entry == limit )
143 /* `loca' is the last table */
144 dist = stream->size - pos;
147 if ( new_loca_len <= dist )
149 face->num_locations = face->root.num_glyphs + 1;
150 table_len = new_loca_len;
152 FT_TRACE2(( "adjusting num_locations to %d\n",
153 face->num_locations ));
159 * Extract the frame. We don't need to decompress it since
160 * we are able to parse it directly.
162 if ( FT_FRAME_EXTRACT( table_len, face->glyph_locations ) )
165 FT_TRACE2(( "loaded\n" ));
172 FT_LOCAL_DEF( FT_ULong )
173 tt_face_get_location( TT_Face face,
184 if ( gindex < face->num_locations )
186 if ( face->header.Index_To_Loc_Format != 0 )
188 p = face->glyph_locations + gindex * 4;
189 p_limit = face->glyph_locations + face->num_locations * 4;
191 pos1 = FT_NEXT_ULONG( p );
194 if ( p + 4 <= p_limit )
195 pos2 = FT_NEXT_ULONG( p );
199 p = face->glyph_locations + gindex * 2;
200 p_limit = face->glyph_locations + face->num_locations * 2;
202 pos1 = FT_NEXT_USHORT( p );
205 if ( p + 2 <= p_limit )
206 pos2 = FT_NEXT_USHORT( p );
213 /* Check broken location data */
214 if ( pos1 > face->glyf_len )
216 FT_TRACE1(( "tt_face_get_location:"
217 " too large offset=0x%08lx found for gid=0x%04lx,"
218 " exceeding the end of glyf table (0x%08lx)\n",
219 pos1, gindex, face->glyf_len ));
224 if ( pos2 > face->glyf_len )
226 FT_TRACE1(( "tt_face_get_location:"
227 " too large offset=0x%08lx found for gid=0x%04lx,"
228 " truncate at the end of glyf table (0x%08lx)\n",
229 pos2, gindex + 1, face->glyf_len ));
230 pos2 = face->glyf_len;
233 /* The `loca' table must be ordered; it refers to the length of */
234 /* an entry as the difference between the current and the next */
235 /* position. However, there do exist (malformed) fonts which */
236 /* don't obey this rule, so we are only able to provide an */
237 /* upper bound for the size. */
239 /* We get (intentionally) a wrong, non-zero result in case the */
240 /* `glyf' table is missing. */
242 *asize = (FT_UInt)( pos2 - pos1 );
244 *asize = (FT_UInt)( face->glyf_len - pos1 );
251 tt_face_done_loca( TT_Face face )
253 FT_Stream stream = face->root.stream;
256 FT_FRAME_RELEASE( face->glyph_locations );
257 face->num_locations = 0;
262 /*************************************************************************/
265 /* tt_face_load_cvt */
268 /* Load the control value table into a face object. */
271 /* face :: A handle to the target face object. */
274 /* stream :: A handle to the input stream. */
277 /* FreeType error code. 0 means success. */
279 FT_LOCAL_DEF( FT_Error )
280 tt_face_load_cvt( TT_Face face,
283 #ifdef TT_USE_BYTECODE_INTERPRETER
286 FT_Memory memory = stream->memory;
290 FT_TRACE2(( "CVT " ));
292 error = face->goto_table( face, TTAG_cvt, stream, &table_len );
295 FT_TRACE2(( "is missing\n" ));
304 face->cvt_size = table_len / 2;
306 if ( FT_NEW_ARRAY( face->cvt, face->cvt_size ) )
309 if ( FT_FRAME_ENTER( face->cvt_size * 2L ) )
313 FT_Short* cur = face->cvt;
314 FT_Short* limit = cur + face->cvt_size;
317 for ( ; cur < limit; cur++ )
318 *cur = FT_GET_SHORT();
322 FT_TRACE2(( "loaded\n" ));
324 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
326 error = tt_face_vary_cvt( face, stream );
332 #else /* !TT_USE_BYTECODE_INTERPRETER */
343 /*************************************************************************/
346 /* tt_face_load_fpgm */
349 /* Load the font program. */
352 /* face :: A handle to the target face object. */
355 /* stream :: A handle to the input stream. */
358 /* FreeType error code. 0 means success. */
360 FT_LOCAL_DEF( FT_Error )
361 tt_face_load_fpgm( TT_Face face,
364 #ifdef TT_USE_BYTECODE_INTERPRETER
370 FT_TRACE2(( "Font program " ));
372 /* The font program is optional */
373 error = face->goto_table( face, TTAG_fpgm, stream, &table_len );
376 face->font_program = NULL;
377 face->font_program_size = 0;
380 FT_TRACE2(( "is missing\n" ));
384 face->font_program_size = table_len;
385 if ( FT_FRAME_EXTRACT( table_len, face->font_program ) )
388 FT_TRACE2(( "loaded, %12d bytes\n", face->font_program_size ));
394 #else /* !TT_USE_BYTECODE_INTERPRETER */
405 /*************************************************************************/
408 /* tt_face_load_prep */
411 /* Load the cvt program. */
414 /* face :: A handle to the target face object. */
417 /* stream :: A handle to the input stream. */
420 /* FreeType error code. 0 means success. */
422 FT_LOCAL_DEF( FT_Error )
423 tt_face_load_prep( TT_Face face,
426 #ifdef TT_USE_BYTECODE_INTERPRETER
432 FT_TRACE2(( "Prep program " ));
434 error = face->goto_table( face, TTAG_prep, stream, &table_len );
437 face->cvt_program = NULL;
438 face->cvt_program_size = 0;
441 FT_TRACE2(( "is missing\n" ));
445 face->cvt_program_size = table_len;
446 if ( FT_FRAME_EXTRACT( table_len, face->cvt_program ) )
449 FT_TRACE2(( "loaded, %12d bytes\n", face->cvt_program_size ));
455 #else /* !TT_USE_BYTECODE_INTERPRETER */
466 /*************************************************************************/
469 /* tt_face_load_hdmx */
472 /* Load the `hdmx' table into the face object. */
475 /* face :: A handle to the target face object. */
477 /* stream :: A handle to the input stream. */
480 /* FreeType error code. 0 means success. */
483 FT_LOCAL_DEF( FT_Error )
484 tt_face_load_hdmx( TT_Face face,
488 FT_Memory memory = stream->memory;
489 FT_UInt version, nn, num_records;
490 FT_ULong table_size, record_size;
495 /* this table is optional */
496 error = face->goto_table( face, TTAG_hdmx, stream, &table_size );
497 if ( error || table_size < 8 )
500 if ( FT_FRAME_EXTRACT( table_size, face->hdmx_table ) )
503 p = face->hdmx_table;
504 limit = p + table_size;
506 version = FT_NEXT_USHORT( p );
507 num_records = FT_NEXT_USHORT( p );
508 record_size = FT_NEXT_ULONG( p );
510 /* The maximum number of bytes in an hdmx device record is the */
511 /* maximum number of glyphs + 2; this is 0xFFFF + 2, thus */
512 /* explaining why `record_size' is a long (which we read as */
513 /* unsigned long for convenience). In practice, two bytes are */
514 /* sufficient to hold the size value. */
516 /* There are at least two fonts, HANNOM-A and HANNOM-B version */
517 /* 2.0 (2005), which get this wrong: The upper two bytes of */
518 /* the size value are set to 0xFF instead of 0x00. We catch */
521 if ( record_size >= 0xFFFF0000UL )
522 record_size &= 0xFFFFU;
524 /* The limit for `num_records' is a heuristic value. */
527 record_size > 0x10001L ||
530 error = FT_THROW( Invalid_File_Format );
534 if ( FT_NEW_ARRAY( face->hdmx_record_sizes, num_records ) )
537 for ( nn = 0; nn < num_records; nn++ )
539 if ( p + record_size > limit )
542 face->hdmx_record_sizes[nn] = p[0];
546 face->hdmx_record_count = nn;
547 face->hdmx_table_size = table_size;
548 face->hdmx_record_size = record_size;
554 FT_FRAME_RELEASE( face->hdmx_table );
555 face->hdmx_table_size = 0;
561 tt_face_free_hdmx( TT_Face face )
563 FT_Stream stream = face->root.stream;
564 FT_Memory memory = stream->memory;
567 FT_FREE( face->hdmx_record_sizes );
568 FT_FRAME_RELEASE( face->hdmx_table );
572 /*************************************************************************/
574 /* Return the advance width table for a given pixel size if it is found */
575 /* in the font's `hdmx' table (if any). */
577 FT_LOCAL_DEF( FT_Byte* )
578 tt_face_get_device_metrics( TT_Face face,
583 FT_Byte* result = NULL;
584 FT_ULong record_size = face->hdmx_record_size;
585 FT_Byte* record = face->hdmx_table + 8;
588 for ( nn = 0; nn < face->hdmx_record_count; nn++ )
589 if ( face->hdmx_record_sizes[nn] == ppem )
592 if ( gindex < record_size )
593 result = record + nn * record_size + gindex;