Skip to content
0.2.0-dev Download ZIP
674 lines ·16.6 KB Raw
1/****************************************************************************
2 *
3 * ttpload.c
4 *
5 * TrueType-specific tables loader (body).
6 *
7 * Copyright (C) 1996-2026 by
8 * David Turner, Robert Wilhelm, and Werner Lemberg.
9 *
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.
15 *
16 */
17
18
19#include <freetype/internal/ftdebug.h>
20#include <freetype/internal/ftobjs.h>
21#include <freetype/internal/ftstream.h>
22#include <freetype/tttags.h>
23
24#include "ttpload.h"
25
26#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
27#include "ttgxvar.h"
28#endif
29
30#include "tterrors.h"
31
32
33 /**************************************************************************
34 *
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.
38 */
39#undef FT_COMPONENT
40#define FT_COMPONENT ttpload
41
42
43 /**************************************************************************
44 *
45 * @Function:
46 * tt_face_load_loca
47 *
48 * @Description:
49 * Load the locations table.
50 *
51 * @InOut:
52 * face ::
53 * A handle to the target face object.
54 *
55 * @Input:
56 * stream ::
57 * The input stream.
58 *
59 * @Return:
60 * FreeType error code. 0 means success.
61 */
62 FT_LOCAL_DEF( FT_Error )
63 tt_face_load_loca( TT_Face face,
64 FT_Stream stream )
65 {
66 FT_Error error;
67 FT_ULong table_len;
68 FT_Int shift;
69
70
71 /* we need the size of the `glyf' table for malformed `loca' tables */
72 error = face->goto_table( face,
73 face->is_extended_glyf ? TTAG_GLYF
74 : TTAG_glyf,
75 stream,
76 &face->glyf_len );
77
78 /* it is possible that a font doesn't have a glyf table at all */
79 /* or its size is zero */
80 if ( FT_ERR_EQ( error, Table_Missing ) )
81 {
82 face->glyf_len = 0;
83 face->glyf_offset = 0;
84 }
85 else if ( error )
86 goto Exit;
87 else
88 {
89#ifdef FT_CONFIG_OPTION_INCREMENTAL
90 if ( face->root.internal->incremental_interface )
91 face->glyf_offset = 0;
92 else
93#endif
94 face->glyf_offset = FT_STREAM_POS();
95 }
96
97 FT_TRACE2(( "Locations " ));
98 error = face->goto_table( face,
99 face->is_extended_glyf ? TTAG_LOCA
100 : TTAG_loca,
101 stream,
102 &table_len );
103 if ( error )
104 {
105 error = FT_THROW( Locations_Missing );
106 goto Exit;
107 }
108
109 shift = face->header.Index_To_Loc_Format != 0 ? 2 : 1;
110
111 if ( !face->is_extended_glyf &&
112 table_len > 0x10000UL << shift )
113 {
114 FT_TRACE2(( "table too large\n" ));
115 table_len = 0x10000UL << shift;
116 }
117
118 face->num_locations = table_len >> shift;
119
120 if ( face->num_locations != (FT_ULong)face->root.num_glyphs + 1 )
121 {
122 FT_TRACE2(( "glyph count mismatch! loca: %lu, maxp: %ld\n",
123 face->num_locations - 1, face->root.num_glyphs ));
124
125 /* we only handle the case where `maxp' gives a larger value */
126 if ( face->num_locations < (FT_ULong)face->root.num_glyphs + 1 )
127 {
128 FT_ULong new_loca_len =
129 ( (FT_ULong)face->root.num_glyphs + 1 ) << shift;
130
131 TT_Table entry = face->dir_tables;
132 TT_Table limit = entry + face->num_tables;
133
134 FT_Long pos = (FT_Long)FT_STREAM_POS();
135 FT_Long dist = 0x7FFFFFFFL;
136 FT_Bool found = 0;
137
138
139 /* compute the distance to next table in font file */
140 for ( ; entry < limit; entry++ )
141 {
142 FT_Long diff = (FT_Long)entry->Offset - pos;
143
144
145 if ( diff > 0 && diff < dist )
146 {
147 dist = diff;
148 found = 1;
149 }
150 }
151
152 if ( !found )
153 {
154 /* `loca' is the last table */
155 dist = (FT_Long)stream->size - pos;
156 }
157
158 if ( new_loca_len <= (FT_ULong)dist )
159 {
160 face->num_locations = (FT_ULong)face->root.num_glyphs + 1;
161 table_len = new_loca_len;
162
163 FT_TRACE2(( "adjusting num_locations to %lu\n",
164 face->num_locations ));
165 }
166 else
167 {
168 face->root.num_glyphs = face->num_locations
169 ? (FT_Long)face->num_locations - 1 : 0;
170
171 FT_TRACE2(( "adjusting num_glyphs to %ld\n",
172 face->root.num_glyphs ));
173 }
174 }
175 }
176
177 /*
178 * Extract the frame. We don't need to decompress it since
179 * we are able to parse it directly.
180 */
181 if ( FT_FRAME_EXTRACT( table_len, face->glyph_locations ) )
182 goto Exit;
183
184 FT_TRACE2(( "loaded\n" ));
185
186 Exit:
187 return error;
188 }
189
190
191 FT_LOCAL_DEF( FT_ULong )
192 tt_face_get_location( FT_Face face, /* TT_Face */
193 FT_UInt gindex,
194 FT_ULong *asize )
195 {
196 TT_Face ttface = (TT_Face)face;
197 FT_ULong pos1, pos2;
198 FT_Byte* p;
199 FT_Byte* p_limit;
200
201
202 pos1 = pos2 = 0;
203
204 if ( gindex < ttface->num_locations )
205 {
206 if ( ttface->header.Index_To_Loc_Format != 0 )
207 {
208 p = ttface->glyph_locations + gindex * 4;
209 p_limit = ttface->glyph_locations + ttface->num_locations * 4;
210
211 pos1 = FT_NEXT_ULONG( p );
212 pos2 = pos1;
213
214 if ( p + 4 <= p_limit )
215 pos2 = FT_NEXT_ULONG( p );
216 }
217 else
218 {
219 p = ttface->glyph_locations + gindex * 2;
220 p_limit = ttface->glyph_locations + ttface->num_locations * 2;
221
222 pos1 = FT_NEXT_USHORT( p );
223 pos2 = pos1;
224
225 if ( p + 2 <= p_limit )
226 pos2 = FT_NEXT_USHORT( p );
227
228 pos1 <<= 1;
229 pos2 <<= 1;
230 }
231 }
232
233 /* Check broken location data. */
234 if ( pos1 > ttface->glyf_len )
235 {
236 FT_TRACE1(( "tt_face_get_location:"
237 " too large offset (0x%08lx) found for glyph index %u,\n",
238 pos1, gindex ));
239 FT_TRACE1(( " "
240 " exceeding the end of `glyf' table (0x%08lx)\n",
241 ttface->glyf_len ));
242 *asize = 0;
243 return 0;
244 }
245
246 if ( pos2 > ttface->glyf_len )
247 {
248 /* We try to sanitize the last `loca' entry. */
249 if ( gindex == ttface->num_locations - 2 )
250 {
251 FT_TRACE1(( "tt_face_get_location:"
252 " too large size (%lu bytes) found for glyph index %u,\n",
253 pos2 - pos1, gindex ));
254 FT_TRACE1(( " "
255 " truncating at the end of `glyf' table to %lu bytes\n",
256 ttface->glyf_len - pos1 ));
257 pos2 = ttface->glyf_len;
258 }
259 else
260 {
261 FT_TRACE1(( "tt_face_get_location:"
262 " too large offset (0x%08lx) found for glyph index %u,\n",
263 pos2, gindex + 1 ));
264 FT_TRACE1(( " "
265 " exceeding the end of `glyf' table (0x%08lx)\n",
266 ttface->glyf_len ));
267 *asize = 0;
268 return 0;
269 }
270 }
271
272 /* The `loca' table must be ordered; it refers to the length of */
273 /* an entry as the difference between the current and the next */
274 /* position. However, there do exist (malformed) fonts which */
275 /* don't obey this rule, so we are only able to provide an */
276 /* upper bound for the size. */
277 /* */
278 /* We get (intentionally) a wrong, non-zero result in case the */
279 /* `glyf' table is missing. */
280 if ( pos2 >= pos1 )
281 *asize = (FT_ULong)( pos2 - pos1 );
282 else
283 *asize = (FT_ULong)( ttface->glyf_len - pos1 );
284
285 return pos1;
286 }
287
288
289 FT_LOCAL_DEF( void )
290 tt_face_done_loca( TT_Face face )
291 {
292 FT_Stream stream = face->root.stream;
293
294
295 FT_FRAME_RELEASE( face->glyph_locations );
296 face->num_locations = 0;
297 }
298
299
300
301 /**************************************************************************
302 *
303 * @Function:
304 * tt_face_load_cvt
305 *
306 * @Description:
307 * Load the control value table into a face object.
308 *
309 * @InOut:
310 * face ::
311 * A handle to the target face object.
312 *
313 * @Input:
314 * stream ::
315 * A handle to the input stream.
316 *
317 * @Return:
318 * FreeType error code. 0 means success.
319 */
320 FT_LOCAL_DEF( FT_Error )
321 tt_face_load_cvt( TT_Face face,
322 FT_Stream stream )
323 {
324#ifdef TT_USE_BYTECODE_INTERPRETER
325
326 FT_Error error;
327 FT_Memory memory = stream->memory;
328 FT_ULong table_len;
329
330
331 FT_TRACE2(( "CVT " ));
332
333 error = face->goto_table( face, TTAG_cvt, stream, &table_len );
334 if ( error )
335 {
336 FT_TRACE2(( "is missing\n" ));
337
338 face->cvt_size = 0;
339 face->cvt = NULL;
340 error = FT_Err_Ok;
341
342 goto Exit;
343 }
344
345 face->cvt_size = table_len / 2;
346
347 if ( FT_QNEW_ARRAY( face->cvt, face->cvt_size ) )
348 goto Exit;
349
350 if ( FT_FRAME_ENTER( face->cvt_size * 2L ) )
351 goto Exit;
352
353 {
354 FT_Int32* cur = face->cvt;
355 FT_Int32* limit = cur + face->cvt_size;
356
357
358 for ( ; cur < limit; cur++ )
359 *cur = FT_GET_SHORT() * 64;
360 }
361
362 FT_FRAME_EXIT();
363 FT_TRACE2(( "loaded\n" ));
364
365#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
366 if ( face->doblend )
367 error = tt_face_vary_cvt( face, stream );
368#endif
369
370 Exit:
371 return error;
372
373#else /* !TT_USE_BYTECODE_INTERPRETER */
374
375 FT_UNUSED( face );
376 FT_UNUSED( stream );
377
378 return FT_Err_Ok;
379
380#endif
381 }
382
383
384 /**************************************************************************
385 *
386 * @Function:
387 * tt_face_load_fpgm
388 *
389 * @Description:
390 * Load the font program.
391 *
392 * @InOut:
393 * face ::
394 * A handle to the target face object.
395 *
396 * @Input:
397 * stream ::
398 * A handle to the input stream.
399 *
400 * @Return:
401 * FreeType error code. 0 means success.
402 */
403 FT_LOCAL_DEF( FT_Error )
404 tt_face_load_fpgm( TT_Face face,
405 FT_Stream stream )
406 {
407#ifdef TT_USE_BYTECODE_INTERPRETER
408
409 FT_Error error;
410 FT_ULong table_len;
411
412
413 FT_TRACE2(( "Font program " ));
414
415 /* The font program is optional */
416 error = face->goto_table( face, TTAG_fpgm, stream, &table_len );
417 if ( error )
418 {
419 face->font_program = NULL;
420 face->font_program_size = 0;
421 error = FT_Err_Ok;
422
423 FT_TRACE2(( "is missing\n" ));
424 }
425 else
426 {
427 face->font_program_size = table_len;
428 if ( FT_FRAME_EXTRACT( table_len, face->font_program ) )
429 goto Exit;
430
431 FT_TRACE2(( "loaded, %12lu bytes\n", face->font_program_size ));
432 }
433
434 Exit:
435 return error;
436
437#else /* !TT_USE_BYTECODE_INTERPRETER */
438
439 FT_UNUSED( face );
440 FT_UNUSED( stream );
441
442 return FT_Err_Ok;
443
444#endif
445 }
446
447
448 /**************************************************************************
449 *
450 * @Function:
451 * tt_face_load_prep
452 *
453 * @Description:
454 * Load the cvt program.
455 *
456 * @InOut:
457 * face ::
458 * A handle to the target face object.
459 *
460 * @Input:
461 * stream ::
462 * A handle to the input stream.
463 *
464 * @Return:
465 * FreeType error code. 0 means success.
466 */
467 FT_LOCAL_DEF( FT_Error )
468 tt_face_load_prep( TT_Face face,
469 FT_Stream stream )
470 {
471#ifdef TT_USE_BYTECODE_INTERPRETER
472
473 FT_Error error;
474 FT_ULong table_len;
475
476
477 FT_TRACE2(( "Prep program " ));
478
479 error = face->goto_table( face, TTAG_prep, stream, &table_len );
480 if ( error )
481 {
482 face->cvt_program = NULL;
483 face->cvt_program_size = 0;
484 error = FT_Err_Ok;
485
486 FT_TRACE2(( "is missing\n" ));
487 }
488 else
489 {
490 face->cvt_program_size = table_len;
491 if ( FT_FRAME_EXTRACT( table_len, face->cvt_program ) )
492 goto Exit;
493
494 FT_TRACE2(( "loaded, %12lu bytes\n", face->cvt_program_size ));
495 }
496
497 Exit:
498 return error;
499
500#else /* !TT_USE_BYTECODE_INTERPRETER */
501
502 FT_UNUSED( face );
503 FT_UNUSED( stream );
504
505 return FT_Err_Ok;
506
507#endif
508 }
509
510
511 FT_COMPARE_DEF( int )
512 compare_ppem( const void* a,
513 const void* b )
514 {
515 return **(FT_Byte**)a - **(FT_Byte**)b;
516 }
517
518
519 /**************************************************************************
520 *
521 * @Function:
522 * tt_face_load_hdmx
523 *
524 * @Description:
525 * Load the `hdmx' table into the face object.
526 *
527 * @Input:
528 * face ::
529 * A handle to the target face object.
530 *
531 * stream ::
532 * A handle to the input stream.
533 *
534 * @Return:
535 * FreeType error code. 0 means success.
536 */
537
538 FT_LOCAL_DEF( FT_Error )
539 tt_face_load_hdmx( TT_Face face,
540 FT_Stream stream )
541 {
542 FT_Error error;
543 FT_Memory memory = stream->memory;
544 FT_UInt nn, num_records;
545 FT_ULong table_size, record_size;
546 FT_Byte* p;
547 FT_Byte* limit;
548
549
550 /* this table is optional */
551 error = face->goto_table( face, TTAG_hdmx, stream, &table_size );
552 if ( error || table_size < 8 )
553 return FT_Err_Ok;
554
555 if ( FT_FRAME_EXTRACT( table_size, face->hdmx_table ) )
556 goto Exit;
557
558 p = face->hdmx_table;
559 limit = p + table_size;
560
561 /* Given that `hdmx' tables are losing its importance (for example, */
562 /* variation fonts introduced in OpenType 1.8 must not have this */
563 /* table) we no longer test for a correct `version' field. */
564 p += 2;
565 num_records = FT_NEXT_USHORT( p );
566 record_size = FT_NEXT_ULONG( p );
567
568 /* There are at least two fonts, HANNOM-A and HANNOM-B version */
569 /* 2.0 (2005), which get this wrong: The upper two bytes of */
570 /* the size value are set to 0xFF instead of 0x00. We catch */
571 /* and fix this. */
572
573 if ( record_size >= 0xFFFF0000UL )
574 record_size &= 0xFFFFU;
575
576 FT_TRACE2(( "Hdmx " ));
577
578 /* The limit for `num_records' is a heuristic value. */
579 if ( num_records > 255 || num_records == 0 )
580 {
581 FT_TRACE2(( "with unreasonable %u records rejected\n", num_records ));
582 goto Fail;
583 }
584
585 /* Out-of-spec tables are rejected. The record size must be */
586 /* equal to the number of glyphs + 2 + 32-bit padding. */
587 if ( (FT_Long)record_size != ( ( face->root.num_glyphs + 2 + 3 ) & ~3 ) )
588 {
589 FT_TRACE2(( "with record size off by %ld bytes rejected\n",
590 (FT_Long)record_size -
591 ( ( face->root.num_glyphs + 2 + 3 ) & ~3 ) ));
592 goto Fail;
593 }
594
595 if ( FT_QNEW_ARRAY( face->hdmx_records, num_records ) )
596 goto Fail;
597
598 for ( nn = 0; nn < num_records; nn++ )
599 {
600 if ( p + record_size > limit )
601 break;
602 face->hdmx_records[nn] = p;
603 p += record_size;
604 }
605
606 /* The records must be already sorted by ppem but it does not */
607 /* hurt to make sure so that the binary search works later. */
608 ft_qsort( face->hdmx_records, nn, sizeof ( FT_Byte* ), compare_ppem );
609
610 face->hdmx_record_count = nn;
611 face->hdmx_table_size = table_size;
612 face->hdmx_record_size = record_size;
613
614 FT_TRACE2(( "%ux%lu loaded\n", num_records, record_size ));
615
616 Exit:
617 return error;
618
619 Fail:
620 FT_FRAME_RELEASE( face->hdmx_table );
621 face->hdmx_table_size = 0;
622 goto Exit;
623 }
624
625
626 FT_LOCAL_DEF( void )
627 tt_face_free_hdmx( TT_Face face )
628 {
629 FT_Stream stream = face->root.stream;
630 FT_Memory memory = stream->memory;
631
632
633 FT_FREE( face->hdmx_records );
634 FT_FRAME_RELEASE( face->hdmx_table );
635 }
636
637
638 /**************************************************************************
639 *
640 * Return the advance width table for a given pixel size if it is found
641 * in the font's `hdmx' table (if any). The records must be sorted for
642 * the binary search to work properly.
643 */
644 FT_LOCAL_DEF( FT_Byte* )
645 tt_face_get_device_metrics( TT_Face face,
646 FT_UInt ppem,
647 FT_UInt gindex )
648 {
649 FT_UInt min = 0;
650 FT_UInt max = face->hdmx_record_count;
651 FT_UInt mid;
652 FT_Byte* result = NULL;
653
654
655 while ( min < max )
656 {
657 mid = ( min + max ) >> 1;
658
659 if ( face->hdmx_records[mid][0] > ppem )
660 max = mid;
661 else if ( face->hdmx_records[mid][0] < ppem )
662 min = mid + 1;
663 else
664 {
665 result = face->hdmx_records[mid] + 2 + gindex;
666 break;
667 }
668 }
669
670 return result;
671 }
672
673
674/* END */