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
) )
78 face
->glyf_offset
= 0;
84 #ifdef FT_CONFIG_OPTION_INCREMENTAL
85 if ( face
->root
.internal
->incremental_interface
)
86 face
->glyf_offset
= 0;
89 face
->glyf_offset
= FT_STREAM_POS();
92 FT_TRACE2(( "Locations " ));
93 error
= face
->goto_table( face
, TTAG_loca
, stream
, &table_len
);
96 error
= FT_THROW( Locations_Missing
);
100 if ( face
->header
.Index_To_Loc_Format
!= 0 )
104 if ( table_len
>= 0x40000L
)
106 FT_TRACE2(( "table too large\n" ));
107 table_len
= 0x3FFFFL
;
109 face
->num_locations
= table_len
>> shift
;
115 if ( table_len
>= 0x20000L
)
117 FT_TRACE2(( "table too large\n" ));
118 table_len
= 0x1FFFFL
;
120 face
->num_locations
= table_len
>> shift
;
123 if ( face
->num_locations
!= (FT_ULong
)face
->root
.num_glyphs
+ 1 )
125 FT_TRACE2(( "glyph count mismatch! loca: %d, maxp: %d\n",
126 face
->num_locations
- 1, face
->root
.num_glyphs
));
128 /* we only handle the case where `maxp' gives a larger value */
129 if ( face
->num_locations
<= (FT_ULong
)face
->root
.num_glyphs
)
131 FT_ULong new_loca_len
=
132 ( (FT_ULong
)face
->root
.num_glyphs
+ 1 ) << shift
;
134 TT_Table entry
= face
->dir_tables
;
135 TT_Table limit
= entry
+ face
->num_tables
;
137 FT_Long pos
= (FT_Long
)FT_STREAM_POS();
138 FT_Long dist
= 0x7FFFFFFFL
;
142 /* compute the distance to next table in font file */
143 for ( ; entry
< limit
; entry
++ )
145 FT_Long diff
= (FT_Long
)entry
->Offset
- pos
;
148 if ( diff
> 0 && diff
< dist
)
157 /* `loca' is the last table */
158 dist
= (FT_Long
)stream
->size
- pos
;
161 if ( new_loca_len
<= (FT_ULong
)dist
)
163 face
->num_locations
= (FT_ULong
)face
->root
.num_glyphs
+ 1;
164 table_len
= new_loca_len
;
166 FT_TRACE2(( "adjusting num_locations to %d\n",
167 face
->num_locations
));
171 face
->root
.num_glyphs
= face
->num_locations
172 ? (FT_Long
)face
->num_locations
- 1 : 0;
174 FT_TRACE2(( "adjusting num_glyphs to %d\n",
175 face
->root
.num_glyphs
));
181 * Extract the frame. We don't need to decompress it since
182 * we are able to parse it directly.
184 if ( FT_FRAME_EXTRACT( table_len
, face
->glyph_locations
) )
187 FT_TRACE2(( "loaded\n" ));
194 FT_LOCAL_DEF( FT_ULong
)
195 tt_face_get_location( TT_Face face
,
206 if ( gindex
< face
->num_locations
)
208 if ( face
->header
.Index_To_Loc_Format
!= 0 )
210 p
= face
->glyph_locations
+ gindex
* 4;
211 p_limit
= face
->glyph_locations
+ face
->num_locations
* 4;
213 pos1
= FT_NEXT_ULONG( p
);
216 if ( p
+ 4 <= p_limit
)
217 pos2
= FT_NEXT_ULONG( p
);
221 p
= face
->glyph_locations
+ gindex
* 2;
222 p_limit
= face
->glyph_locations
+ face
->num_locations
* 2;
224 pos1
= FT_NEXT_USHORT( p
);
227 if ( p
+ 2 <= p_limit
)
228 pos2
= FT_NEXT_USHORT( p
);
235 /* Check broken location data. */
236 if ( pos1
> face
->glyf_len
)
238 FT_TRACE1(( "tt_face_get_location:"
239 " too large offset (0x%08lx) found for glyph index %ld,\n"
241 " exceeding the end of `glyf' table (0x%08lx)\n",
242 pos1
, gindex
, face
->glyf_len
));
247 if ( pos2
> face
->glyf_len
)
249 /* We try to sanitize the last `loca' entry. */
250 if ( gindex
== face
->num_locations
- 1 )
252 FT_TRACE1(( "tt_face_get_location:"
253 " too large offset (0x%08lx) found for glyph index %ld,\n"
255 " truncating at the end of `glyf' table (0x%08lx)\n",
256 pos2
, gindex
+ 1, face
->glyf_len
));
257 pos2
= face
->glyf_len
;
261 FT_TRACE1(( "tt_face_get_location:"
262 " too large offset (0x%08lx) found for glyph index %ld,\n"
264 " exceeding the end of `glyf' table (0x%08lx)\n",
265 pos2
, gindex
+ 1, face
->glyf_len
));
271 /* The `loca' table must be ordered; it refers to the length of */
272 /* an entry as the difference between the current and the next */
273 /* position. However, there do exist (malformed) fonts which */
274 /* don't obey this rule, so we are only able to provide an */
275 /* upper bound for the size. */
277 /* We get (intentionally) a wrong, non-zero result in case the */
278 /* `glyf' table is missing. */
280 *asize
= (FT_UInt
)( pos2
- pos1
);
282 *asize
= (FT_UInt
)( face
->glyf_len
- pos1
);
289 tt_face_done_loca( TT_Face face
)
291 FT_Stream stream
= face
->root
.stream
;
294 FT_FRAME_RELEASE( face
->glyph_locations
);
295 face
->num_locations
= 0;
300 /*************************************************************************/
303 /* tt_face_load_cvt */
306 /* Load the control value table into a face object. */
309 /* face :: A handle to the target face object. */
312 /* stream :: A handle to the input stream. */
315 /* FreeType error code. 0 means success. */
317 FT_LOCAL_DEF( FT_Error
)
318 tt_face_load_cvt( TT_Face face
,
321 #ifdef TT_USE_BYTECODE_INTERPRETER
324 FT_Memory memory
= stream
->memory
;
328 FT_TRACE2(( "CVT " ));
330 error
= face
->goto_table( face
, TTAG_cvt
, stream
, &table_len
);
333 FT_TRACE2(( "is missing\n" ));
342 face
->cvt_size
= table_len
/ 2;
344 if ( FT_NEW_ARRAY( face
->cvt
, face
->cvt_size
) )
347 if ( FT_FRAME_ENTER( face
->cvt_size
* 2L ) )
351 FT_Short
* cur
= face
->cvt
;
352 FT_Short
* limit
= cur
+ face
->cvt_size
;
355 for ( ; cur
< limit
; cur
++ )
356 *cur
= FT_GET_SHORT();
360 FT_TRACE2(( "loaded\n" ));
362 #ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
364 error
= tt_face_vary_cvt( face
, stream
);
370 #else /* !TT_USE_BYTECODE_INTERPRETER */
381 /*************************************************************************/
384 /* tt_face_load_fpgm */
387 /* Load the font program. */
390 /* face :: A handle to the target face object. */
393 /* stream :: A handle to the input stream. */
396 /* FreeType error code. 0 means success. */
398 FT_LOCAL_DEF( FT_Error
)
399 tt_face_load_fpgm( TT_Face face
,
402 #ifdef TT_USE_BYTECODE_INTERPRETER
408 FT_TRACE2(( "Font program " ));
410 /* The font program is optional */
411 error
= face
->goto_table( face
, TTAG_fpgm
, stream
, &table_len
);
414 face
->font_program
= NULL
;
415 face
->font_program_size
= 0;
418 FT_TRACE2(( "is missing\n" ));
422 face
->font_program_size
= table_len
;
423 if ( FT_FRAME_EXTRACT( table_len
, face
->font_program
) )
426 FT_TRACE2(( "loaded, %12d bytes\n", face
->font_program_size
));
432 #else /* !TT_USE_BYTECODE_INTERPRETER */
443 /*************************************************************************/
446 /* tt_face_load_prep */
449 /* Load the cvt program. */
452 /* face :: A handle to the target face object. */
455 /* stream :: A handle to the input stream. */
458 /* FreeType error code. 0 means success. */
460 FT_LOCAL_DEF( FT_Error
)
461 tt_face_load_prep( TT_Face face
,
464 #ifdef TT_USE_BYTECODE_INTERPRETER
470 FT_TRACE2(( "Prep program " ));
472 error
= face
->goto_table( face
, TTAG_prep
, stream
, &table_len
);
475 face
->cvt_program
= NULL
;
476 face
->cvt_program_size
= 0;
479 FT_TRACE2(( "is missing\n" ));
483 face
->cvt_program_size
= table_len
;
484 if ( FT_FRAME_EXTRACT( table_len
, face
->cvt_program
) )
487 FT_TRACE2(( "loaded, %12d bytes\n", face
->cvt_program_size
));
493 #else /* !TT_USE_BYTECODE_INTERPRETER */
504 /*************************************************************************/
507 /* tt_face_load_hdmx */
510 /* Load the `hdmx' table into the face object. */
513 /* face :: A handle to the target face object. */
515 /* stream :: A handle to the input stream. */
518 /* FreeType error code. 0 means success. */
521 FT_LOCAL_DEF( FT_Error
)
522 tt_face_load_hdmx( TT_Face face
,
526 FT_Memory memory
= stream
->memory
;
527 FT_UInt nn
, num_records
;
528 FT_ULong table_size
, record_size
;
533 /* this table is optional */
534 error
= face
->goto_table( face
, TTAG_hdmx
, stream
, &table_size
);
535 if ( error
|| table_size
< 8 )
538 if ( FT_FRAME_EXTRACT( table_size
, face
->hdmx_table
) )
541 p
= face
->hdmx_table
;
542 limit
= p
+ table_size
;
544 /* Given that `hdmx' tables are losing its importance (for example, */
545 /* variation fonts introduced in OpenType 1.8 must not have this */
546 /* table) we no longer test for a correct `version' field. */
548 num_records
= FT_NEXT_USHORT( p
);
549 record_size
= FT_NEXT_ULONG( p
);
551 /* The maximum number of bytes in an hdmx device record is the */
552 /* maximum number of glyphs + 2; this is 0xFFFF + 2, thus */
553 /* explaining why `record_size' is a long (which we read as */
554 /* unsigned long for convenience). In practice, two bytes are */
555 /* sufficient to hold the size value. */
557 /* There are at least two fonts, HANNOM-A and HANNOM-B version */
558 /* 2.0 (2005), which get this wrong: The upper two bytes of */
559 /* the size value are set to 0xFF instead of 0x00. We catch */
562 if ( record_size
>= 0xFFFF0000UL
)
563 record_size
&= 0xFFFFU
;
565 /* The limit for `num_records' is a heuristic value. */
566 if ( num_records
> 255 ||
568 ( record_size
> 0x10001L
||
569 record_size
< 4 ) ) )
571 error
= FT_THROW( Invalid_File_Format
);
575 if ( FT_NEW_ARRAY( face
->hdmx_record_sizes
, num_records
) )
578 for ( nn
= 0; nn
< num_records
; nn
++ )
580 if ( p
+ record_size
> limit
)
583 face
->hdmx_record_sizes
[nn
] = p
[0];
587 face
->hdmx_record_count
= nn
;
588 face
->hdmx_table_size
= table_size
;
589 face
->hdmx_record_size
= record_size
;
595 FT_FRAME_RELEASE( face
->hdmx_table
);
596 face
->hdmx_table_size
= 0;
602 tt_face_free_hdmx( TT_Face face
)
604 FT_Stream stream
= face
->root
.stream
;
605 FT_Memory memory
= stream
->memory
;
608 FT_FREE( face
->hdmx_record_sizes
);
609 FT_FRAME_RELEASE( face
->hdmx_table
);
613 /*************************************************************************/
615 /* Return the advance width table for a given pixel size if it is found */
616 /* in the font's `hdmx' table (if any). */
618 FT_LOCAL_DEF( FT_Byte
* )
619 tt_face_get_device_metrics( TT_Face face
,
624 FT_Byte
* result
= NULL
;
625 FT_ULong record_size
= face
->hdmx_record_size
;
626 FT_Byte
* record
= face
->hdmx_table
+ 8;
629 for ( nn
= 0; nn
< face
->hdmx_record_count
; nn
++ )
630 if ( face
->hdmx_record_sizes
[nn
] == ppem
)
633 if ( gindex
< record_size
)
634 result
= record
+ nn
* record_size
+ gindex
;