Skip to content
0.2.0-dev Download ZIP
3774 lines ·108.5 KB Raw
1/****************************************************************************
2 *
3 * ttvarc.c
4 *
5 * TrueType and OpenType VARC (Variable Composites) support (body).
6 *
7 * Copyright (C) 2025-2026 by
8 * Behdad Esfahbod, 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 /**************************************************************************
20 *
21 * VARC table specification:
22 *
23 * https://github.com/harfbuzz/boring-expansion-spec/blob/main/VARC.md
24 *
25 */
26
27
28#include <freetype/internal/ftdebug.h>
29#include <freetype/internal/ftstream.h>
30#include <freetype/internal/ftcalc.h>
31#include <freetype/internal/ftobjs.h>
32#include <freetype/tttags.h>
33#include <freetype/ftmm.h>
34#include <freetype/fttrigon.h>
35#include <freetype/ftoutln.h>
36#include <ft2build.h>
37
38#ifdef TT_CONFIG_OPTION_VARC
39
40#include "ttvarc.h"
41
42#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
43#include "ttgxvar.h"
44#endif
45
46
47 /**************************************************************************
48 *
49 * The macro FT_COMPONENT is used in trace mode. It is an implicit
50 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
51 * messages during execution.
52 */
53#undef FT_COMPONENT
54#define FT_COMPONENT ttvarc
55
56
57 FT_LOCAL_DEF( FT_Error )
58 tt_face_init_varc_axes( FT_Face face,
59 FT_UInt axis_count )
60 {
61#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
62 return tt_var_init_blend( face, axis_count );
63#else
64 FT_UNUSED( face );
65 FT_UNUSED( axis_count );
66
67 return FT_Err_Ok;
68#endif
69 }
70
71
72 /* Macro for bounds checking when reading from table */
73#define CHECK_TABLE_BOUNDS( p, size ) \
74 ( (FT_Byte*)(p) >= \
75 (FT_Byte*)varc->table && \
76 (FT_Byte*)(p) + (size) <= \
77 (FT_Byte*)varc->table + varc->table_size )
78
79 /* Stack allocation limits for performance. */
80#define VARC_STACK_AXIS_COUNT 64
81#define VARC_STACK_DELTA_COUNT 128
82#define VARC_STACK_COORD_COUNT 64
83#define VARC_STACK_INDICES_COUNT 64
84
85
86 /**************************************************************************
87 *
88 * Utility Functions
89 *
90 */
91
92 /**************************************************************************
93 *
94 * @Function:
95 * read_uint32var
96 *
97 * @Description:
98 * Read a variable-length uint32 value (1-5 bytes).
99 * Similar to UTF-8 encoding but for integers.
100 *
101 * @Input:
102 * p ::
103 * Pointer to the byte stream (will be advanced).
104 *
105 * limit ::
106 * Maximum valid address in the stream.
107 *
108 * @Output:
109 * value ::
110 * The decoded uint32 value.
111 *
112 * @Return:
113 * FreeType error code. 0 means success.
114 */
115 static FT_Error
116 read_uint32var( FT_Byte** p,
117 FT_Byte* limit,
118 FT_UInt32* value )
119 {
120 FT_Byte b0;
121 FT_UInt32 result;
122
123
124 if ( *p >= limit )
125 return FT_THROW( Invalid_Table );
126
127 b0 = *(*p)++;
128
129 /* single byte: 0xxxxxxx */
130 if ( b0 < 0x80 )
131 {
132 *value = b0;
133 return FT_Err_Ok;
134 }
135
136 /* two bytes: 10xxxxxx xxxxxxxx */
137 if ( b0 < 0xC0 )
138 {
139 if ( *p >= limit )
140 return FT_THROW( Invalid_Table );
141 result = ( ( b0 - 0x80 ) << 8 ) |
142 *(*p)++;
143 *value = result;
144 return FT_Err_Ok;
145 }
146
147 /* three bytes: 110xxxxx xxxxxxxx xxxxxxxx */
148 if ( b0 < 0xE0 )
149 {
150 if ( *p + 1 >= limit )
151 return FT_THROW( Invalid_Table );
152 result = ( ( b0 - 0xC0 ) << 16 ) |
153 ( (*p)[0] << 8 ) |
154 (*p)[1];
155 *p += 2;
156 *value = result;
157 return FT_Err_Ok;
158 }
159
160 /* four bytes: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx */
161 if ( b0 < 0xF0 )
162 {
163 if ( *p + 2 >= limit )
164 return FT_THROW( Invalid_Table );
165 result = ( ( b0 - 0xE0 ) << 24 ) |
166 ( (*p)[0] << 16 ) |
167 ( (*p)[1] << 8 ) |
168 (*p)[2];
169 *p += 3;
170 *value = result;
171 return FT_Err_Ok;
172 }
173
174 /* five bytes: 11110000 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx */
175 if ( b0 == 0xF0 )
176 {
177 if ( *p + 3 >= limit )
178 return FT_THROW( Invalid_Table );
179 result = ( (*p)[0] << 24 ) |
180 ( (*p)[1] << 16 ) |
181 ( (*p)[2] << 8 ) |
182 (*p)[3];
183 *p += 4;
184 *value = result;
185 return FT_Err_Ok;
186 }
187
188 /* invalid encoding */
189 return FT_THROW( Invalid_Table );
190 }
191
192
193 /**************************************************************************
194 *
195 * @Function:
196 * read_fword
197 *
198 * @Description:
199 * Read a signed 16-bit FWORD value.
200 *
201 * @Input:
202 * p ::
203 * Pointer to the byte stream (will be advanced).
204 *
205 * limit ::
206 * Maximum valid address in the stream.
207 *
208 * @Output:
209 * value ::
210 * The decoded FWORD value as 26.6 fixed-point.
211 *
212 * @Return:
213 * FreeType error code. 0 means success.
214 */
215 static FT_Error
216 read_fword( FT_Byte** p,
217 FT_Byte* limit,
218 FT_Pos* value )
219 {
220 FT_Short val;
221
222
223 if ( *p + 1 >= limit )
224 return FT_THROW( Invalid_Table );
225
226 val = (FT_Short)( ( (FT_UShort)(*p)[0] << 8 ) |
227 (FT_UShort)(*p)[1] );
228 *p += 2;
229
230 /* FWORD is a signed 16-bit integer, */
231 /* shift left by 6 for FT_Pos (26.6 format). */
232 *value = (FT_Pos)val * 64;
233
234 return FT_Err_Ok;
235 }
236
237
238 /**************************************************************************
239 *
240 * @Function:
241 * read_f4dot12
242 *
243 * @Description:
244 * Read a signed F4DOT12 value (4.12 fixed-point).
245 *
246 * @Input:
247 * p ::
248 * Pointer to the byte stream (will be advanced).
249 *
250 * limit ::
251 * Maximum valid address in the stream.
252 *
253 * @Output:
254 * value ::
255 * The decoded value as 16.16 fixed-point.
256 *
257 * @Return:
258 * FreeType error code. 0 means success.
259 */
260 static FT_Error
261 read_f4dot12( FT_Byte** p,
262 FT_Byte* limit,
263 FT_Fixed* value )
264 {
265 FT_Short val;
266
267
268 if ( *p + 1 >= limit )
269 return FT_THROW( Invalid_Table );
270
271 val = (FT_Short)( ( (FT_UShort)(*p)[0] << 8 ) |
272 (FT_UShort)(*p)[1] );
273 *p += 2;
274
275 /* Convert F4DOT12 (4.12) to F16DOT16 (16.16) by shifting left 4. */
276 *value = (FT_Fixed)val * 16;
277
278 return FT_Err_Ok;
279 }
280
281
282 /**************************************************************************
283 *
284 * @Function:
285 * read_f6dot10
286 *
287 * @Description:
288 * Read a signed F6DOT10 value (6.10 fixed-point).
289 *
290 * @Input:
291 * p ::
292 * Pointer to the byte stream (will be advanced).
293 *
294 * limit ::
295 * Maximum valid address in the stream.
296 *
297 * @Output:
298 * value ::
299 * The decoded value as 16.16 fixed-point.
300 *
301 * @Return:
302 * FreeType error code. 0 means success.
303 */
304 static FT_Error
305 read_f6dot10( FT_Byte** p,
306 FT_Byte* limit,
307 FT_Fixed* value )
308 {
309 FT_Short val;
310
311
312 if ( *p + 1 >= limit )
313 return FT_THROW( Invalid_Table );
314
315 val = (FT_Short)( ( (FT_UShort)(*p)[0] << 8 ) |
316 (FT_UShort)(*p)[1] );
317 *p += 2;
318
319 /* Simply shift left by 6. */
320 *value = (FT_Fixed)val * 64;
321
322 return FT_Err_Ok;
323 }
324
325
326 /**************************************************************************
327 *
328 * Coverage Table Functions
329 *
330 */
331
332 /**************************************************************************
333 *
334 * @Function:
335 * tt_varc_get_coverage
336 *
337 * @Description:
338 * Check whether a glyph is covered by the 'VARC' table using binary
339 * search.
340 *
341 * @Input:
342 * varc ::
343 * The 'VARC' table structure.
344 *
345 * glyph_index ::
346 * The glyph ID to check.
347 *
348 * @Return:
349 * Coverage index if found, -1 otherwise.
350 */
351 static FT_Int
352 tt_varc_get_coverage( TT_Varc varc,
353 FT_UInt glyph_index )
354 {
355 FT_Byte* p;
356 FT_UInt format;
357 FT_UInt count;
358
359
360 if ( !varc || !varc->coverage )
361 return -1;
362
363 p = varc->coverage;
364
365 /* Check bounds for format and count. */
366 if ( !CHECK_TABLE_BOUNDS( p, 4 ) )
367 return -1;
368
369 format = ( p[0] << 8 ) | p[1];
370 p += 2;
371
372 /* formats 1 and 3: list of 16-bit or 24-bit glyph IDs */
373 if ( format == 1 || format == 3 )
374 {
375 FT_UInt glyph_size = format == 1 ? 2 : 3;
376
377
378 if ( !CHECK_TABLE_BOUNDS( p, glyph_size ) )
379 return -1;
380
381 count = format == 1 ? FT_NEXT_USHORT( p )
382 : FT_NEXT_UOFF3( p );
383
384 if ( !CHECK_TABLE_BOUNDS( p, count * glyph_size ) )
385 return -1;
386
387 /* binary search */
388 {
389 FT_UInt min = 0;
390 FT_UInt max = count;
391
392
393 while ( min < max )
394 {
395 FT_UInt mid = ( min + max ) >> 1;
396 FT_Byte* gid_p = p + mid * glyph_size;
397 FT_UInt gid = format == 1 ? FT_PEEK_USHORT( gid_p )
398 : FT_PEEK_UOFF3( gid_p );
399
400
401 if ( gid == glyph_index )
402 return (FT_Int)mid;
403 else if ( gid < glyph_index )
404 min = mid + 1;
405 else
406 max = mid;
407 }
408 }
409 }
410
411 /* formats 2 and 4: range list with 16-bit or 24-bit values */
412 else if ( format == 2 || format == 4 )
413 {
414 FT_UInt value_size = format == 2 ? 2 : 3;
415 FT_UInt record_size = value_size * 3;
416 FT_UInt min, max;
417
418
419 if ( !CHECK_TABLE_BOUNDS( p, value_size ) )
420 return -1;
421
422 count = format == 2 ? FT_NEXT_USHORT( p )
423 : FT_NEXT_UOFF3( p );
424
425 if ( !CHECK_TABLE_BOUNDS( p, count * record_size ) )
426 return -1;
427
428 /* binary search the sorted ranges */
429 min = 0;
430 max = count;
431 while ( min < max )
432 {
433 FT_UInt mid = ( min + max ) >> 1;
434 FT_Byte* rec = p + mid * record_size;
435 FT_UInt start = format == 2 ? FT_PEEK_USHORT( rec )
436 : FT_PEEK_UOFF3( rec );
437 FT_UInt end = format == 2 ? FT_PEEK_USHORT( rec + value_size )
438 : FT_PEEK_UOFF3( rec + value_size );
439
440
441 if ( glyph_index < start )
442 max = mid;
443 else if ( glyph_index > end )
444 min = mid + 1;
445 else
446 {
447 FT_Byte* index_p = rec + value_size * 2;
448 FT_UInt index = format == 2 ? FT_PEEK_USHORT( index_p )
449 : FT_PEEK_UOFF3( index_p );
450
451
452 return (FT_Int)( index + glyph_index - start );
453 }
454 }
455 }
456
457 return -1;
458 }
459
460
461 /**************************************************************************
462 *
463 * CFF2IndexOf Functions
464 *
465 */
466
467 /**************************************************************************
468 *
469 * @Function:
470 * tt_varc_read_cff2_index_entry
471 *
472 * @Description:
473 * Read entry `index` from a CFF2-style INDEX:
474 *
475 * count (uint32) | offSize (uint8) | offset[count + 1] | data
476 *
477 * Each offset is `offSize` bytes and 1-based. On success `*data`
478 * points to the entry's bytes and `*size` is its length. Every read
479 * and the returned range are validated against `table_limit`; the
480 * caller must pass an `index_data` pointer that lies within the table.
481 *
482 * @Return:
483 * FreeType error code. 0 means success.
484 */
485 static FT_Error
486 tt_varc_read_cff2_index_entry( FT_Byte* index_data,
487 FT_Byte* table_limit,
488 FT_UInt index,
489 FT_Byte** data,
490 FT_UInt* size )
491 {
492 FT_Byte* p = index_data;
493 FT_UInt32 count;
494 FT_UInt offSize;
495 FT_ULong offset1 = 0;
496 FT_ULong offset2 = 0;
497 FT_Byte* offset_base;
498
499 FT_UInt i;
500
501
502 if ( !p || p + 4 > table_limit )
503 return FT_THROW( Invalid_Table );
504
505 count = ( (FT_UInt32)p[0] << 24 ) |
506 ( (FT_UInt32)p[1] << 16 ) |
507 ( (FT_UInt32)p[2] << 8 ) |
508 (FT_UInt32)p[3];
509 p += 4;
510
511 if ( p >= table_limit )
512 return FT_THROW( Invalid_Table );
513
514 offSize = *p++;
515 if ( offSize < 1 || offSize > 4 || index >= count )
516 return FT_THROW( Invalid_Table );
517
518 /* division avoids overflow in the offset-array bounds check */
519 if ( count >= (FT_UInt32)( ( table_limit - p ) / offSize ) )
520 return FT_THROW( Invalid_Table );
521
522 offset_base = p + ( count + 1 ) * offSize;
523
524 p += index * offSize;
525 for ( i = 0; i < offSize; i++ )
526 offset1 = ( offset1 << 8 ) | p[i];
527
528 p += offSize;
529 for ( i = 0; i < offSize; i++ )
530 offset2 = ( offset2 << 8 ) | p[i];
531
532 /* offsets are 1-based and must stay within the table */
533 if ( offset2 < offset1 ||
534 offset1 < 1 ||
535 offset2 - 1 > (FT_ULong)( table_limit - offset_base ) )
536 return FT_THROW( Invalid_Table );
537
538 *data = offset_base + offset1 - 1;
539 *size = (FT_UInt)( offset2 - offset1 );
540
541 return FT_Err_Ok;
542 }
543
544
545 /**************************************************************************
546 *
547 * @Function:
548 * tt_varc_get_glyph_record
549 *
550 * @Description:
551 * Get the glyph record from the `CFF2IndexOf` structure.
552 *
553 * @Input:
554 * varc ::
555 * The 'VARC' table structure.
556 *
557 * glyph_index ::
558 * The glyph ID.
559 *
560 * @Output:
561 * record_data ::
562 * Pointer to the glyph record data.
563 *
564 * record_size ::
565 * Size of the glyph record in bytes.
566 *
567 * @Return:
568 * FreeType error code. 0 means success.
569 */
570 static FT_Error
571 tt_varc_get_glyph_record( TT_Varc varc,
572 FT_UInt glyph_index,
573 FT_Byte** record_data,
574 FT_UInt* record_size )
575 {
576 FT_Error error;
577 FT_Int coverage_index;
578
579
580 if ( !varc || !varc->var_composite_glyphs )
581 return FT_THROW( Invalid_Table );
582
583 coverage_index = tt_varc_get_coverage( varc, glyph_index );
584 if ( coverage_index < 0 )
585 return FT_THROW( Invalid_Glyph_Index );
586
587 error = tt_varc_read_cff2_index_entry(
588 varc->var_composite_glyphs,
589 (FT_Byte*)varc->table + varc->table_size,
590 (FT_UInt)coverage_index,
591 record_data, record_size );
592 if ( error )
593 return error;
594
595 /* a component record is at most 64KB */
596 if ( *record_size > 0xFFFFU )
597 return FT_THROW( Invalid_Table );
598
599 return FT_Err_Ok;
600 }
601
602
603 /**************************************************************************
604 *
605 * Component Parsing Functions
606 *
607 */
608
609 /**************************************************************************
610 *
611 * @Function:
612 * tt_varc_parse_component
613 *
614 * @Description:
615 * Parse a single VARC component record.
616 *
617 * @Input:
618 * face ::
619 * The font face.
620 *
621 * varc ::
622 * The 'VARC' table structure.
623 *
624 * p ::
625 * Pointer to component data (will be advanced).
626 *
627 * limit ::
628 * End of component data.
629 *
630 * axis_values_buffer ::
631 * A stack buffer to be used for small arrays.
632 *
633 * axis_values_buffer_size ::
634 * The size of `axis_values_buffer`. If the number of parsed axis
635 * values exceeds this size, an array gets allocated on the heap.
636 *
637 * @Output:
638 * component ::
639 * The parsed component structure.
640 *
641 * @Return:
642 * FreeType error code. 0 means success.
643 */
644 static FT_Error
645 tt_varc_parse_component( TT_Face face,
646 TT_Varc varc,
647 FT_Byte** p,
648 FT_Byte* limit,
649 TT_VarcComponent component,
650 FT_Fixed* axis_values_buffer,
651 FT_UInt axis_values_buffer_size )
652 {
653 FT_Error error;
654 FT_Memory memory = face->root.memory;
655
656 FT_UInt32 flags32;
657
658
659 FT_ZERO( component );
660
661 /* read flags (uint32var) */
662 error = read_uint32var( p, limit, &flags32 );
663 if ( error )
664 return error;
665
666 component->flags = flags32;
667 /* read GID */
668 if ( component->flags & VARC_GID_IS_24BIT )
669 {
670 if ( *p + 2 >= limit )
671 return FT_THROW( Invalid_Table );
672 component->gid = ( (*p)[0] << 16 ) |
673 ( (*p)[1] << 8 ) |
674 (*p)[2];
675 *p += 3;
676 }
677 else
678 {
679 if ( *p + 1 >= limit )
680 return FT_THROW( Invalid_Table );
681 component->gid = ( (*p)[0] << 8 ) |
682 (*p)[1];
683 *p += 2;
684 }
685
686 /* validate GID */
687 if ( component->gid >= (FT_UInt)face->root.num_glyphs )
688 {
689 /* For now, don't fail - the font might be using extended GID space. */
690 /* return FT_THROW( Invalid_Glyph_Index ); */
691 }
692
693 /* read condition index */
694 if ( component->flags & VARC_HAVE_CONDITION )
695 {
696 error = read_uint32var( p, limit, &component->condition_index );
697 if ( error )
698 return error;
699 }
700
701 /* read axis indices index */
702 if ( component->flags & VARC_HAVE_AXES )
703 {
704 error = read_uint32var( p, limit, &component->axis_indices_index );
705 if ( error )
706 return error;
707 }
708
709 /* read axis values (TupleValues) */
710 if ( component->flags & VARC_HAVE_AXES )
711 {
712 FT_UInt axis_count = 0;
713 FT_Byte* table_limit = (FT_Byte*)varc->table + varc->table_size;
714 FT_Byte* tuple_data;
715 FT_UInt tuple_size;
716
717
718 if ( !varc->axis_indices_list )
719 return FT_THROW( Invalid_Table );
720
721 error = tt_varc_read_cff2_index_entry( varc->axis_indices_list,
722 table_limit,
723 component->axis_indices_index,
724 &tuple_data, &tuple_size );
725 if ( error )
726 return error;
727
728 /* decode the TupleValues just to count the axis values */
729 {
730 FT_Byte* tuple_limit = tuple_data + tuple_size;
731 FT_Byte* tp = tuple_data;
732
733
734 while ( tp < tuple_limit )
735 {
736 FT_Byte control = *tp++;
737 FT_UInt cnt = ( control & 0x3F ) + 1;
738
739
740 if ( ( control & 0xC0 ) == 0xC0 )
741 {
742 /* 32-bit values */
743 if ( tp + 4 * cnt > tuple_limit )
744 break;
745 tp += 4 * cnt;
746 }
747 else if ( control & 0x80 )
748 ; /* zeros - no data bytes */
749 else if ( control & 0x40 )
750 {
751 /* 2-byte values */
752 if ( tp + 2 * cnt > tuple_limit )
753 break;
754 tp += 2 * cnt;
755 }
756 else
757 {
758 /* 1-byte values */
759 if ( tp + cnt > tuple_limit )
760 break;
761 tp += cnt;
762 }
763
764 axis_count += cnt;
765 }
766 }
767
768 component->num_axis_values = axis_count;
769
770 if ( axis_count > 0 )
771 {
772 FT_UInt i;
773 FT_UInt j;
774
775
776 /* use caller's stack buffer for small arrays, heap for large */
777 if ( axis_count <= axis_values_buffer_size )
778 {
779 component->axis_values = axis_values_buffer;
780 component->axis_values_on_heap = FALSE;
781 }
782 else
783 {
784 if ( FT_NEW_ARRAY( component->axis_values, axis_count ) )
785 return error;
786 component->axis_values_on_heap = TRUE;
787 }
788
789 /* read axis values from TupleValues (packed deltas format) */
790 i = 0;
791 while ( i < axis_count )
792 {
793 FT_Byte control;
794 FT_UInt cnt;
795
796
797 /* read control byte */
798 if ( *p >= limit )
799 goto Fail;
800 control = *(*p)++;
801
802 /* extract run count (lower 6 bits), add 1 */
803 cnt = ( control & 0x3F ) + 1;
804 if ( cnt > axis_count - i )
805 cnt = axis_count - i;
806
807 /* check top 2 bits for encoding type */
808 if ( ( control & 0xC0 ) == 0xC0 )
809 {
810 /* Both bits set = 32-bit signed integers */
811 if ( *p + 4 * cnt > limit )
812 goto Fail;
813 for ( j = 0; j < cnt; j++ )
814 {
815 /* read as signed 32-bit integer, */
816 /* reinterpret as F2DOT14 -> F16DOT16 */
817 FT_Int32 val = (FT_Int32)( ( (FT_UInt32)(*p)[0] << 24 ) |
818 ( (FT_UInt32)(*p)[1] << 16 ) |
819 ( (FT_UInt32)(*p)[2] << 8 ) |
820 (FT_UInt32)(*p)[3] );
821
822
823 component->axis_values[i++] = (FT_Fixed)MUL_INT32( val, 4 );
824 *p += 4;
825 }
826 }
827 else if ( control & 0x80 )
828 {
829 /* bit 7 set = zeros */
830 for ( j = 0; j < cnt; j++ )
831 component->axis_values[i++] = 0;
832 }
833 else if ( control & 0x40 )
834 {
835 /* bit 6 set = 2-byte signed integers */
836 if ( *p + 2 * cnt > limit )
837 goto Fail;
838 for ( j = 0; j < cnt; j++ )
839 {
840 /* read as signed 16-bit integer, */
841 /* reinterpret as F2DOT14 -> F16DOT16 */
842 FT_Short val = (FT_Short)( ( (FT_UShort)(*p)[0] << 8 ) |
843 (FT_UShort)(*p)[1] );
844
845
846 component->axis_values[i++] = (FT_Fixed)( val * 4 );
847 *p += 2;
848 }
849 }
850 else
851 {
852 /* neither bit set = 1-byte signed integers */
853 if ( *p + cnt > limit )
854 goto Fail;
855 for ( j = 0; j < cnt; j++ )
856 {
857 /* read signed byte integer, */
858 /* reinterpret as F2DOT14 -> F16DOT16 */
859 FT_Char val = (FT_Char)(*(*p)++);
860
861
862 component->axis_values[i++] = (FT_Fixed)( val * 4 );
863 }
864 }
865 }
866 }
867 }
868
869 /* read axis values variation index */
870 if ( component->flags & VARC_AXES_HAVE_VARIATION )
871 {
872 error = read_uint32var( p, limit, &component->axis_values_var_index );
873 if ( error )
874 goto Fail;
875 }
876
877 /* read transform variation index */
878 if ( component->flags & VARC_TRANSFORM_HAS_VARIATION )
879 {
880 error = read_uint32var( p, limit, &component->transform_var_index );
881 if ( error )
882 goto Fail;
883 }
884
885 /* initialize transform to identity */
886 component->translate_x = 0;
887 component->translate_y = 0;
888 component->rotation = 0;
889 component->scale_x = 0x10000; /* 1.0 in 16.16 */
890 component->scale_y = 0x10000; /* 1.0 in 16.16 */
891 component->skew_x = 0;
892 component->skew_y = 0;
893 component->tcenter_x = 0;
894 component->tcenter_y = 0;
895
896 /* read transform components - each field is optional based on flags */
897 if ( component->flags & VARC_HAVE_TRANSLATE_X )
898 {
899 error = read_fword( p, limit, &component->translate_x );
900 if ( error )
901 goto Fail;
902 }
903
904 if ( component->flags & VARC_HAVE_TRANSLATE_Y )
905 {
906 error = read_fword( p, limit, &component->translate_y );
907 if ( error )
908 goto Fail;
909 }
910
911 if ( component->flags & VARC_HAVE_ROTATION )
912 {
913 error = read_f4dot12( p, limit, &component->rotation );
914 if ( error )
915 goto Fail;
916 }
917
918 if ( component->flags & VARC_HAVE_SCALE_X )
919 {
920 error = read_f6dot10( p, limit, &component->scale_x );
921 if ( error )
922 goto Fail;
923 }
924
925 if ( component->flags & VARC_HAVE_SCALE_Y )
926 {
927 error = read_f6dot10( p, limit, &component->scale_y );
928 if ( error )
929 goto Fail;
930 }
931 else if ( component->flags & VARC_HAVE_SCALE_X )
932 {
933 /* ScaleY defaults to ScaleX if ScaleX is present */
934 component->scale_y = component->scale_x;
935 }
936
937 if ( component->flags & VARC_HAVE_SKEW_X )
938 {
939 error = read_f4dot12( p, limit, &component->skew_x );
940 if ( error )
941 goto Fail;
942 }
943
944 if ( component->flags & VARC_HAVE_SKEW_Y )
945 {
946 error = read_f4dot12( p, limit, &component->skew_y );
947 if ( error )
948 goto Fail;
949 }
950
951 if ( component->flags & VARC_HAVE_TCENTER_X )
952 {
953 error = read_fword( p, limit, &component->tcenter_x );
954 if ( error )
955 goto Fail;
956 }
957
958 if ( component->flags & VARC_HAVE_TCENTER_Y )
959 {
960 error = read_fword( p, limit, &component->tcenter_y );
961 if ( error )
962 goto Fail;
963 }
964
965 /* Skip one uint32var for each set reserved flag bit (bits 15-31), */
966 /* keeping the byte stream aligned for forward-compatible */
967 /* extensions, as HarfBuzz, skrifa, and fontTools do. */
968 {
969 FT_UInt32 reserved = component->flags & VARC_RESERVED_MASK;
970
971
972 while ( reserved )
973 {
974 FT_UInt32 ignore;
975
976
977 error = read_uint32var( p, limit, &ignore );
978 if ( error )
979 goto Fail;
980
981 reserved &= reserved - 1;
982 }
983 }
984
985 return FT_Err_Ok;
986
987 Fail:
988 /* the axis-value reading loop jumps here on truncated data */
989 /* without setting `error`; never report success on failure */
990 if ( !error )
991 error = FT_THROW( Invalid_Table );
992 if ( component->axis_values && component->axis_values_on_heap )
993 FT_FREE( component->axis_values );
994 return error;
995 }
996
997
998 /**************************************************************************
999 *
1000 * @Function:
1001 * tt_varc_free_component
1002 *
1003 * @Description:
1004 * Free resources allocated for a component.
1005 *
1006 * @Input:
1007 * face ::
1008 * The font face.
1009 *
1010 * component ::
1011 * The component to free.
1012 */
1013 static void
1014 tt_varc_free_component( TT_Face face,
1015 TT_VarcComponent component )
1016 {
1017 FT_Memory memory = face->root.memory;
1018
1019
1020 if ( component->axis_values && component->axis_values_on_heap )
1021 FT_FREE( component->axis_values );
1022 }
1023
1024
1025 /**************************************************************************
1026 *
1027 * MultiItemVariationStore Functions
1028 *
1029 */
1030
1031 /**************************************************************************
1032 *
1033 * @Function:
1034 * tt_varc_evaluate_region
1035 *
1036 * @Description:
1037 * Evaluate a variation region against current coordinates.
1038 * Return a scalar value (0.0 to 1.0) indicating how much this
1039 * region contributes at the current variation location.
1040 *
1041 * @Input:
1042 * region_data ::
1043 * Pointer to region data (axis ranges).
1044 *
1045 * limit ::
1046 * End of valid data.
1047 *
1048 * coords ::
1049 * Current normalized coordinates (F2DOT14).
1050 *
1051 * coord_count ::
1052 * Number of coordinates.
1053 *
1054 * @Return:
1055 * Scalar value as FT_Fixed (16.16 format), 0 to 0x10000 (1.0).
1056 */
1057 static FT_Fixed
1058 tt_varc_evaluate_region( FT_Byte* region_data,
1059 FT_Byte* limit,
1060 FT_Fixed* coords,
1061 FT_UInt coord_count )
1062 {
1063 FT_Byte* p = region_data;
1064 FT_Fixed scalar = 0x10000; /* start with 1.0 in 16.16 */
1065 FT_UInt axis_count;
1066
1067 FT_UInt i;
1068
1069
1070 if ( !coords || coord_count == 0 )
1071 return scalar; /* no coords available, return 1.0 */
1072
1073 /* `SparseVariationRegion` table:
1074 *
1075 * regionAxisCount (u16) - number of non-default axes
1076 * axisOffsets (u32[regionAxisCount]) - relative to this region
1077 * for each sparse axis:
1078 * axisIndex (u16)
1079 * startCoord (F2DOT14)
1080 * peakCoord (F2DOT14)
1081 * endCoord (F2DOT14)
1082 */
1083
1084 if ( limit - p < 2 )
1085 return 0;
1086
1087 /* read sparse axis count (u16) */
1088 axis_count = ( (FT_UInt)p[0] << 8 ) | p[1];
1089 p += 2;
1090
1091 if ( axis_count > (FT_ULong)( limit - p ) / 4 )
1092 return 0;
1093
1094 /* process each axis range */
1095 for ( i = 0; i < axis_count; i++ )
1096 {
1097 FT_UInt16 axis_index;
1098 FT_Short min_val, peak_val, max_val;
1099 FT_Fixed coord;
1100 FT_Fixed axis_scalar;
1101 FT_UInt32 axis_offset;
1102 FT_Byte* axis;
1103
1104 FT_Fixed min_16;
1105 FT_Fixed peak_16;
1106 FT_Fixed max_16;
1107
1108
1109 axis_offset = ( (FT_UInt32)p[0] << 24 ) |
1110 ( (FT_UInt32)p[1] << 16 ) |
1111 ( (FT_UInt32)p[2] << 8 ) |
1112 (FT_UInt32)p[3];
1113 p += 4;
1114
1115 if ( axis_offset > (FT_ULong)( limit - region_data ) ||
1116 (FT_ULong)( limit - region_data ) - axis_offset < 8 )
1117 return 0;
1118
1119 axis = region_data + axis_offset;
1120 axis_index = ( (FT_UInt16)axis[0] << 8 ) | axis[1];
1121 axis += 2;
1122
1123 /* read min, peak, max (F2DOT14) - each is 2 bytes, */
1124 /* advance after each read */
1125 min_val = (FT_Short)( ( (FT_UShort)axis[0] << 8 ) |
1126 (FT_UShort)axis[1] );
1127 axis += 2;
1128 peak_val = (FT_Short)( ( (FT_UShort)axis[0] << 8 ) |
1129 (FT_UShort)axis[1] );
1130 axis += 2;
1131 max_val = (FT_Short)( ( (FT_UShort)axis[0] << 8 ) |
1132 (FT_UShort)axis[1] );
1133
1134 /* get coordinate for this axis */
1135 if ( axis_index >= coord_count )
1136 continue; /* skip unknown axes */
1137
1138 coord = coords[axis_index];
1139
1140 /* convert F2DOT14 to F16DOT16 for comparison */
1141 min_16 = (FT_Fixed)min_val * 4;
1142 peak_16 = (FT_Fixed)peak_val * 4;
1143 max_16 = (FT_Fixed)max_val * 4;
1144
1145 /* Evaluate this axis with the sparse-region tent function, */
1146 /* matching HarfBuzz (`VarRegionAxis::evaluate`) and skrifa */
1147 /* (`SparseVariationRegion::compute_scalar`). */
1148 if ( peak_16 == 0 )
1149 continue; /* axis not part of region -> factor 1 */
1150
1151 if ( coord == peak_16 )
1152 continue; /* at peak -> factor 1 */
1153
1154 if ( coord == 0 )
1155 return 0; /* default location -> region gives 0 */
1156
1157 if ( min_16 > peak_16 ||
1158 peak_16 > max_16 ||
1159 ( min_16 < 0 && max_16 > 0 ) )
1160 continue; /* ill-formed axis -> factor 1 */
1161
1162 if ( coord < min_16 || coord > max_16 )
1163 return 0; /* outside range -> region gives 0 */
1164
1165 if ( coord < peak_16 )
1166 axis_scalar = FT_DivFix( coord - min_16, peak_16 - min_16 );
1167 else
1168 axis_scalar = FT_DivFix( max_16 - coord, max_16 - peak_16 );
1169
1170 /* multiply into overall scalar */
1171 scalar = FT_MulFix( scalar, axis_scalar );
1172
1173 /* early exit if scalar becomes 0 */
1174 if ( scalar == 0 )
1175 return 0;
1176 }
1177
1178 return scalar;
1179 }
1180
1181
1182 /**************************************************************************
1183 *
1184 * MultiItemVariationStore Functions
1185 *
1186 */
1187
1188 /**************************************************************************
1189 *
1190 * @Function:
1191 * tt_varc_read_tuple_deltas
1192 *
1193 * @Description:
1194 * Read a tuple of delta values from TupleValues (packed deltas).
1195 * Return plain integers that will be scaled later.
1196 *
1197 * @Input:
1198 * data ::
1199 * Pointer to TupleValues data.
1200 *
1201 * limit ::
1202 * End of valid data.
1203 *
1204 * num_deltas ::
1205 * Number of deltas to read.
1206 *
1207 * @Output:
1208 * deltas ::
1209 * Array to store deltas (as plain integers).
1210 *
1211 * bytes_consumed ::
1212 * The number of bytes consumed for this tuple.
1213 *
1214 * @Return:
1215 * FreeType error code. 0 means success.
1216 */
1217 static FT_Error
1218 tt_varc_read_tuple_deltas( FT_Byte* data,
1219 FT_Byte* limit,
1220 FT_UInt num_deltas,
1221 FT_Int32* deltas,
1222 FT_UInt* bytes_consumed )
1223 {
1224 FT_Byte* p = data;
1225 FT_Byte* start = data;
1226
1227 FT_UInt i = 0;
1228
1229
1230 /* read deltas using TupleValues (packed delta) format */
1231 while ( i < num_deltas && p < limit )
1232 {
1233 FT_Byte control;
1234 FT_UInt cnt, j;
1235
1236
1237 /* read control byte */
1238 if ( p >= limit )
1239 return FT_THROW( Invalid_Table );
1240
1241 control = *p++;
1242
1243 /* extract run count (lower 6 bits), add 1 */
1244 cnt = ( control & 0x3F ) + 1;
1245 if ( cnt > num_deltas - i )
1246 cnt = num_deltas - i;
1247
1248 /* check top 2 bits for encoding type */
1249 if ( ( control & 0xC0 ) == 0xC0 )
1250 {
1251 /* both bits set = 32-bit plain integer values */
1252 if ( p + 4 * cnt > limit )
1253 return FT_THROW( Invalid_Table );
1254
1255 for ( j = 0; j < cnt; j++ )
1256 {
1257 /* read as signed 32-bit plain integer */
1258 FT_Int32 val = (FT_Int32)( ( (FT_UInt32)p[0] << 24 ) |
1259 ( (FT_UInt32)p[1] << 16 ) |
1260 ( (FT_UInt32)p[2] << 8 ) |
1261 (FT_UInt32)p[3] );
1262
1263
1264 deltas[i++] = val; /* store as plain integer */
1265 p += 4;
1266 }
1267 }
1268 else if ( control & 0x80 )
1269 {
1270 /* bit 7 set = zeros */
1271 for ( j = 0; j < cnt; j++ )
1272 deltas[i++] = 0;
1273 }
1274 else if ( control & 0x40 )
1275 {
1276 /* bit 6 set = 2-byte signed integer values */
1277 if ( p + 2 * cnt > limit )
1278 return FT_THROW( Invalid_Table );
1279
1280 for ( j = 0; j < cnt; j++ )
1281 {
1282 /* read as signed 16-bit plain integer */
1283 FT_Short val = (FT_Short)( ( (FT_UShort)p[0] << 8 ) |
1284 (FT_UShort)p[1] );
1285
1286
1287 deltas[i++] = val; /* store as plain integer */
1288 p += 2;
1289 }
1290 }
1291 else
1292 {
1293 /* neither bit set = 1-byte signed integer values */
1294 if ( p + cnt > limit )
1295 return FT_THROW( Invalid_Table );
1296
1297 for ( j = 0; j < cnt; j++ )
1298 {
1299 /* read signed byte as plain integer */
1300 FT_Char val = (FT_Char)(*p++);
1301
1302
1303 deltas[i++] = val; /* store as plain integer */
1304 }
1305 }
1306 }
1307
1308 /* if we hit the end before reading all deltas, */
1309 /* fill remaining with zeros */
1310 while ( i < num_deltas )
1311 deltas[i++] = 0;
1312
1313 /* return the number of bytes consumed */
1314 *bytes_consumed = (FT_UInt)( p - start );
1315
1316 return FT_Err_Ok;
1317 }
1318
1319
1320 /**************************************************************************
1321 *
1322 * @Function:
1323 * tt_varc_get_item_deltas
1324 *
1325 * @Description:
1326 * Get a tuple of deltas from the `MultiItemVariationStore`.
1327 * Unlike `ItemVariationStore`, which returns a single delta,
1328 * `MultiItemVariationStore` returns multiple deltas (a tuple).
1329 *
1330 * @Input:
1331 * face ::
1332 * The font face.
1333 *
1334 * varc ::
1335 * The VARC table.
1336 *
1337 * var_index ::
1338 * Variation index (outer << 16 | inner).
1339 *
1340 * num_deltas ::
1341 * Number of deltas expected in the tuple.
1342 *
1343 * shift ::
1344 * Number of bits to preserve from fractional interpolation.
1345 * The raw integer deltas are effectively shifted left by this
1346 * amount. Use shift=2 for F2DOT14 data (returns F16DOT16),
1347 * shift=4 for F4DOT12 data (returns F16DOT16), etc.
1348 * Maximum value is 16.
1349 *
1350 * current_coords ::
1351 * Current normalized coordinates (F2DOT14).
1352 *
1353 * num_coords ::
1354 * Number of coords in `current_coords`.
1355 *
1356 * condition_defaults ::
1357 * Optional defaults for condition values. If non-NULL, return each
1358 * unrounded value's positive sign (0 or 1), instead of scaled deltas.
1359 *
1360 * @Output:
1361 * deltas ::
1362 * Array to store the deltas (must have space for `num_deltas`).
1363 * Values are raw integer deltas scaled left by `shift` bits.
1364 *
1365 * @Return:
1366 * FreeType error code. 0 means success.
1367 */
1368 static FT_Error
1369 tt_varc_get_item_deltas( TT_Face face,
1370 TT_Varc varc,
1371 FT_UInt32 var_index,
1372 FT_UInt num_deltas,
1373 FT_Long* deltas,
1374 FT_UInt shift,
1375 FT_Fixed* current_coords,
1376 FT_UInt num_coords,
1377 FT_Short* condition_defaults )
1378 {
1379#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
1380
1381 FT_Error error;
1382 FT_Memory memory = face->root.memory;
1383
1384 FT_Byte* mvs_data; /* MultiItemVariationStore */
1385 FT_Byte* table_limit;
1386 FT_UInt16 format;
1387 FT_UInt32 regions_offset;
1388 FT_UInt16 data_sets_count;
1389 FT_UInt outer, inner;
1390 FT_Byte* p;
1391 FT_Byte* data_set_offsets;
1392 FT_UInt32 data_set_offset;
1393
1394 FT_Byte* mvd_data; /* MultiVarData */
1395 FT_Byte mvd_format;
1396 FT_UInt16 region_count;
1397 FT_Byte* delta_sets;
1398 FT_UInt32 delta_sets_offset;
1399 FT_Byte* tuple_data;
1400 FT_UInt tuple_size;
1401
1402 FT_Int64 stack_accumulators[VARC_STACK_DELTA_COUNT];
1403 /* deltas * regions */
1404 FT_Int32 stack_all_deltas[VARC_STACK_DELTA_COUNT * 16];
1405 FT_Int64* accumulators = NULL;
1406 FT_Byte* regions_data = NULL;
1407 FT_Byte* regions_limit = NULL;
1408 FT_UInt32 region_list_count = 0;
1409
1410 FT_UInt total_deltas;
1411 FT_Int32* all_deltas = NULL;
1412 FT_UInt bytes_consumed = 0;
1413 FT_Byte* region_indices_ptr;
1414 FT_UInt region_idx;
1415
1416 FT_UInt i;
1417
1418
1419 if ( !varc->multi_var_store_loaded || !varc->multi_var_store )
1420 {
1421 /* no variation store - return zeros */
1422 for ( i = 0; i < num_deltas; i++ )
1423 deltas[i] = condition_defaults ? FT_BOOL( condition_defaults[i] > 0 )
1424 : 0;
1425 return FT_Err_Ok;
1426 }
1427
1428 mvs_data = varc->multi_var_store;
1429 table_limit = (FT_Byte*)varc->table + varc->table_size;
1430
1431 /* split `var_index` into `outer` and `inner` */
1432 outer = var_index >> 16;
1433 inner = var_index & 0xFFFF;
1434
1435 /* parse `MultiItemVariationStore` header */
1436 p = mvs_data;
1437
1438 if ( p + 8 > table_limit )
1439 return FT_THROW( Invalid_Table );
1440
1441 /* read format (u16) - must be 1 */
1442 format = ( (FT_UInt16)p[0] << 8 ) | p[1];
1443 p += 2;
1444
1445 if ( format != 1 )
1446 return FT_THROW( Invalid_Table );
1447
1448 /* read regions offset (u32) */
1449 regions_offset = ( (FT_UInt32)p[0] << 24 ) |
1450 ( (FT_UInt32)p[1] << 16 ) |
1451 ( (FT_UInt32)p[2] << 8 ) |
1452 (FT_UInt32)p[3];
1453 p += 4;
1454
1455 /* read dataSets count (u16) */
1456 data_sets_count = ( (FT_UInt16)p[0] << 8 ) | p[1];
1457 p += 2;
1458
1459 /* validate outer index */
1460 if ( outer >= data_sets_count )
1461 return FT_THROW( Invalid_Table );
1462
1463 /* read data set offset for outer index */
1464 data_set_offsets = p;
1465 p = data_set_offsets + outer * 4;
1466
1467 if ( p + 4 > table_limit )
1468 return FT_THROW( Invalid_Table );
1469
1470 data_set_offset = ( (FT_UInt32)p[0] << 24 ) |
1471 ( (FT_UInt32)p[1] << 16 ) |
1472 ( (FT_UInt32)p[2] << 8 ) |
1473 (FT_UInt32)p[3];
1474
1475 /* get `MultiVarData` */
1476 if ( data_set_offset > (FT_UInt32)( table_limit - mvs_data ) )
1477 return FT_THROW( Invalid_Table );
1478
1479 mvd_data = mvs_data + data_set_offset;
1480
1481 if ( table_limit - mvd_data < 7 )
1482 return FT_THROW( Invalid_Table );
1483
1484 /* parse `MultiVarData` header */
1485 p = mvd_data;
1486
1487 /* read format (u8) - must be 1 */
1488 mvd_format = *p++;
1489
1490 if ( mvd_format != 1 )
1491 return FT_THROW( Invalid_Table );
1492
1493 /* read `regionIndices` count (u16) */
1494 region_count = ( (FT_UInt16)p[0] << 8 ) | p[1];
1495 p += 2;
1496
1497 if ( region_count > (FT_ULong)( table_limit - p ) / 2 )
1498 return FT_THROW( Invalid_Table );
1499 p += region_count * 2;
1500
1501 if ( table_limit - p < 4 )
1502 return FT_THROW( Invalid_Table );
1503
1504 delta_sets_offset = ( (FT_UInt32)p[0] << 24 ) |
1505 ( (FT_UInt32)p[1] << 16 ) |
1506 ( (FT_UInt32)p[2] << 8 ) |
1507 (FT_UInt32)p[3];
1508 if ( !delta_sets_offset ||
1509 delta_sets_offset > (FT_ULong)( table_limit - mvd_data ) ||
1510 (FT_ULong)( table_limit - mvd_data ) - delta_sets_offset < 4 )
1511 return FT_THROW( Invalid_Table );
1512 delta_sets = mvd_data + delta_sets_offset;
1513
1514 /* read tuple from INDEX2 at inner index */
1515 error = tt_varc_read_cff2_index_entry( delta_sets, table_limit, inner,
1516 &tuple_data, &tuple_size );
1517 if ( error )
1518 return error;
1519
1520 /* allocate 64-bit accumulators for each delta */
1521 if ( num_deltas <= VARC_STACK_DELTA_COUNT )
1522 accumulators = stack_accumulators;
1523 else
1524 {
1525 if ( FT_NEW_ARRAY( accumulators, num_deltas ) )
1526 {
1527 error = FT_THROW( Out_Of_Memory );
1528 goto Cleanup;
1529 }
1530 }
1531
1532 /* initialize accumulators to zero */
1533 FT_ARRAY_ZERO( accumulators, num_deltas );
1534 if ( condition_defaults )
1535 {
1536 for ( i = 0; i < num_deltas; i++ )
1537 {
1538#ifdef FT_INT64
1539 accumulators[i] = (FT_Int64)condition_defaults[i] * 0x10000L;
1540#else
1541 accumulators[i].lo = (FT_UInt32)(FT_Int32)condition_defaults[i] << 16;
1542 accumulators[i].hi = condition_defaults[i] < 0 ? 0xFFFFFFFFU : 0;
1543#endif
1544 }
1545 }
1546
1547 /* parse `SparseVarRegionList` to get region definitions */
1548 if ( regions_offset > 0 &&
1549 regions_offset <= (FT_ULong)( table_limit - mvs_data ) )
1550 {
1551 regions_data = mvs_data + regions_offset;
1552 regions_limit = table_limit;
1553
1554 /* read region count (u32) */
1555 if ( regions_limit - regions_data >= 4 )
1556 {
1557 region_list_count = ( (FT_UInt32)regions_data[0] << 24 ) |
1558 ( (FT_UInt32)regions_data[1] << 16 ) |
1559 ( (FT_UInt32)regions_data[2] << 8 ) |
1560 (FT_UInt32)regions_data[3];
1561 regions_data += 4;
1562
1563 if ( region_list_count >
1564 (FT_ULong)( regions_limit - regions_data ) / 4 )
1565 regions_data = NULL;
1566 }
1567 else
1568 regions_data = NULL;
1569 }
1570
1571 if ( !regions_data )
1572 {
1573 if ( accumulators != stack_accumulators )
1574 FT_FREE( accumulators );
1575 error = FT_THROW( Invalid_Table );
1576 goto Cleanup;
1577 }
1578
1579 /* read ALL deltas from the flat tuple */
1580 /* (region_count * num_deltas values) */
1581
1582 /* a wrapped product would under-size `all_deltas` below */
1583 if ( num_deltas != 0 && region_count > FT_UINT_MAX / num_deltas )
1584 {
1585 if ( accumulators != stack_accumulators )
1586 FT_FREE( accumulators );
1587 error = FT_THROW( Invalid_Table );
1588 goto Cleanup;
1589 }
1590
1591 total_deltas = region_count * num_deltas;
1592
1593 /* allocate space for all deltas */
1594 if ( total_deltas <= VARC_STACK_DELTA_COUNT * 16 )
1595 all_deltas = stack_all_deltas;
1596 else
1597 {
1598 if ( FT_NEW_ARRAY( all_deltas, total_deltas ) )
1599 {
1600 if ( accumulators != stack_accumulators )
1601 FT_FREE( accumulators );
1602 error = FT_THROW( Out_Of_Memory );
1603 goto Cleanup;
1604 }
1605 }
1606
1607 /* read all deltas from TupleValues */
1608 error = tt_varc_read_tuple_deltas( tuple_data, tuple_data + tuple_size,
1609 total_deltas, all_deltas,
1610 &bytes_consumed );
1611 if ( error )
1612 {
1613 if ( all_deltas != stack_all_deltas )
1614 FT_FREE( all_deltas );
1615 if ( accumulators != stack_accumulators )
1616 FT_FREE( accumulators );
1617 goto Cleanup;
1618 }
1619
1620 /* parse regionIndices and accumulate scaled deltas */
1621
1622 /* after format(u8) + region_count(u16) */
1623 region_indices_ptr = mvd_data + 3;
1624
1625 for ( region_idx = 0; region_idx < region_count; region_idx++ )
1626 {
1627 FT_UInt16 region_index;
1628 FT_Fixed region_scalar;
1629
1630 FT_UInt k;
1631
1632
1633 /* read region index from `regionIndices` array (u16) */
1634 if ( region_indices_ptr + 2 > table_limit )
1635 break;
1636
1637 region_index = ( (FT_UInt16)region_indices_ptr[0] << 8 ) |
1638 region_indices_ptr[1];
1639 region_indices_ptr += 2;
1640
1641 /* evaluate region to get scalar */
1642 if ( regions_data && current_coords )
1643 {
1644 /* SparseVarRegionList format:
1645 *
1646 * u32: regionCount
1647 * u32[regionCount]: array of offsets (from start of list)
1648 * to each region
1649 */
1650
1651 /* back to start (before count) */
1652 FT_Byte* region_list_start = regions_data - 4;
1653 FT_Byte* region_offset_ptr;
1654
1655
1656 if ( region_index < region_list_count &&
1657 region_index < (FT_ULong)( regions_limit - regions_data ) / 4 )
1658 {
1659 FT_UInt32 region_offset;
1660
1661
1662 region_offset_ptr = regions_data + region_index * 4;
1663 region_offset = ( (FT_UInt32)region_offset_ptr[0] << 24 ) |
1664 ( (FT_UInt32)region_offset_ptr[1] << 16 ) |
1665 ( (FT_UInt32)region_offset_ptr[2] << 8 ) |
1666 (FT_UInt32)region_offset_ptr[3];
1667
1668
1669 if ( region_offset <
1670 (FT_UInt32)( regions_limit - region_list_start ) )
1671 region_scalar =
1672 tt_varc_evaluate_region( region_list_start + region_offset,
1673 regions_limit,
1674 current_coords,
1675 num_coords );
1676 else
1677 region_scalar = 0;
1678 }
1679 else
1680 region_scalar = 0;
1681 }
1682 else
1683 {
1684 /* no coords or regions - use 1.0 for first region, 0 for others */
1685 region_scalar = ( region_idx == 0 ) ? 0x10000 : 0;
1686 }
1687
1688 /* scale and accumulate deltas for this region; */
1689 /* tuple is organized as regions-major: */
1690 /* */
1691 /* [d0_r0, d1_r0, ..., dN_r0, d0_r1, ...] */
1692 if ( region_scalar != 0 )
1693 {
1694 FT_UInt base_offset = region_idx * num_deltas;
1695
1696
1697 for ( k = 0; k < num_deltas; k++ )
1698 {
1699 /* plain integer */
1700 FT_Int32 region_delta = all_deltas[base_offset + k];
1701
1702 /* accumulate using 64-bit math: accum += delta * scalar */
1703#ifdef FT_INT64
1704 FT_Int64 product = (FT_Int64)region_delta * region_scalar;
1705
1706
1707 accumulators[k] += product;
1708#else /* !FT_INT64 */
1709 /* 32-bit fallback */
1710 if ( (FT_UInt32)region_delta + 0x8000U <= 0xFFFFU )
1711 {
1712 /* fast path - multiplication fits in 32 bits */
1713 FT_Int32 product = region_delta * region_scalar;
1714
1715
1716 accumulators[k].lo += (FT_UInt32)product;
1717 if ( accumulators[k].lo < (FT_UInt32)product )
1718 accumulators[k].hi += 1;
1719 if ( product < 0 )
1720 accumulators[k].hi -= 1;
1721 }
1722 else
1723 {
1724 /* slow path - full 64-bit signed multiplication */
1725 FT_UInt32 a = (FT_UInt32)ULABS( region_delta );
1726 FT_UInt32 b = (FT_UInt32)ULABS( region_scalar );
1727
1728 FT_UInt32 a_lo = a & 0xFFFF;
1729 FT_UInt32 a_hi = a >> 16;
1730 FT_UInt32 b_lo = b & 0xFFFF;
1731 FT_UInt32 b_hi = b >> 16;
1732
1733 FT_UInt32 mid = ( a_lo * b_hi ) + ( a_hi * b_lo );
1734 FT_UInt32 lo = a_lo * b_lo;
1735 FT_UInt32 hi = a_hi * b_hi;
1736
1737
1738 hi += ( mid >> 16 ) +
1739 ( ( ( lo >> 16 ) + ( mid & 0xFFFF ) ) >> 16 );
1740 lo += ( mid << 16 );
1741
1742 if ( ( region_delta < 0 ) != ( region_scalar < 0 ) )
1743 {
1744 lo = ~lo + 1;
1745 hi = ~hi + !lo;
1746 }
1747
1748 accumulators[k].lo += lo;
1749 if ( accumulators[k].lo < lo )
1750 accumulators[k].hi += 1;
1751 accumulators[k].hi += hi;
1752 }
1753#endif /* !FT_INT64 */
1754 }
1755 }
1756 }
1757
1758 /* free the flat delta array */
1759 if ( all_deltas != stack_all_deltas )
1760 FT_FREE( all_deltas );
1761
1762 /* Convert 64-bit accumulators to deltas with rounding. */
1763 /* Accumulators hold: raw_int_delta * region_scalar_16.16 */
1764 /* We shift right by (16 - shift) to preserve `shift` fractional */
1765 /* bits from the interpolation. */
1766 {
1767 FT_Int right_shift = 16 - shift;
1768 FT_Long rounding = right_shift > 0 ? ( 1L << ( right_shift - 1 ) )
1769 : 0;
1770
1771
1772 for ( i = 0; i < num_deltas; i++ )
1773 {
1774#ifdef FT_INT64
1775 if ( condition_defaults )
1776 deltas[i] = FT_BOOL( accumulators[i] > 0 );
1777 else
1778 deltas[i] =
1779 (FT_Long)( ( accumulators[i] + rounding ) >> right_shift );
1780#else
1781 /* 32-bit fallback */
1782 FT_UInt32 hi = accumulators[i].hi;
1783 FT_UInt32 lo = accumulators[i].lo;
1784
1785
1786 if ( condition_defaults )
1787 {
1788 deltas[i] = FT_BOOL( !( hi & 0x80000000U ) && ( hi || lo ) );
1789 continue;
1790 }
1791
1792 /* add rounding */
1793 lo += (FT_UInt32)rounding;
1794 if ( lo < (FT_UInt32)rounding )
1795 hi += 1;
1796
1797 /* shift right by (16 - shift) bits */
1798 if ( right_shift == 0 )
1799 deltas[i] = (FT_Long)(FT_Int32)lo;
1800 else
1801 deltas[i] = (FT_Long)(FT_Int32)( ( hi << ( 16 + shift ) ) |
1802 ( lo >> right_shift ) );
1803#endif
1804 }
1805 }
1806
1807 /* free accumulators */
1808 if ( accumulators != stack_accumulators )
1809 FT_FREE( accumulators );
1810
1811 error = FT_Err_Ok;
1812
1813 Cleanup:
1814 return error;
1815
1816#else /* !TT_CONFIG_OPTION_GX_VAR_SUPPORT */
1817
1818 FT_UNUSED( face );
1819 FT_UNUSED( varc );
1820 FT_UNUSED( var_index );
1821 FT_UNUSED( num_deltas );
1822 FT_UNUSED( deltas );
1823 FT_UNUSED( shift );
1824 FT_UNUSED( current_coords );
1825 FT_UNUSED( num_coords );
1826 FT_UNUSED( condition_defaults );
1827
1828 return FT_THROW( Unimplemented_Feature );
1829
1830#endif /* !TT_CONFIG_OPTION_GX_VAR_SUPPORT */
1831 }
1832
1833
1834 /**************************************************************************
1835 *
1836 * Condition Evaluation
1837 *
1838 */
1839
1840 /**************************************************************************
1841 *
1842 * @Function:
1843 * tt_varc_get_condition
1844 *
1845 * @Description:
1846 * Look up a 'Condition' by index in the top-level `ConditionList`.
1847 * ConditionList:
1848 * uint32 count, then Offset32[count] from its start.
1849 *
1850 * @Input:
1851 * varc ::
1852 * The 'VARC' table structure.
1853 *
1854 * cond_index ::
1855 * The index into the `ConditionList` structure.
1856 *
1857 * table_limit ::
1858 * End of valid table data.
1859 *
1860 * @Return:
1861 * The condition offset.
1862 */
1863 static FT_Byte*
1864 tt_varc_get_condition( TT_Varc varc,
1865 FT_UInt32 cond_index,
1866 FT_Byte* table_limit )
1867 {
1868 FT_Byte* cl = varc->condition_list;
1869 FT_UInt32 count;
1870 FT_Byte* p;
1871 FT_UInt32 off;
1872
1873
1874 if ( !cl || cl + 4 > table_limit )
1875 return NULL;
1876
1877 count = ( (FT_UInt32)cl[0] << 24 ) |
1878 ( (FT_UInt32)cl[1] << 16 ) |
1879 ( (FT_UInt32)cl[2] << 8 ) |
1880 (FT_UInt32)cl[3];
1881 if ( cond_index >= count )
1882 return NULL;
1883
1884 /* bound the index with division so `cond_index * 4` cannot overflow */
1885 if ( cond_index > (FT_UInt32)( ( table_limit - cl - 4 ) / 4 ) )
1886 return NULL;
1887
1888 p = cl + 4 + cond_index * 4;
1889 if ( p + 4 > table_limit )
1890 return NULL;
1891
1892 off = ( (FT_UInt32)p[0] << 24 ) |
1893 ( (FT_UInt32)p[1] << 16 ) |
1894 ( (FT_UInt32)p[2] << 8 ) |
1895 (FT_UInt32)p[3];
1896
1897 if ( off >= (FT_UInt32)( table_limit - cl ) )
1898 return NULL;
1899
1900 return cl + off;
1901 }
1902
1903
1904 /**************************************************************************
1905 *
1906 * @Function:
1907 * tt_varc_eval_condition
1908 *
1909 * @Description:
1910 * Evaluate a 'Condition', matching HarfBuzz/skrifa/fontTools.
1911 * Sub-condition offsets in the And/Or/Negate formats are 24-bit and
1912 * relative to the start of the containing condition.
1913 *
1914 * @Input:
1915 * face ::
1916 * The font face.
1917 *
1918 * varc ::
1919 * The VARC table.
1920 *
1921 * cond ::
1922 * Pointer to the Condition.
1923 *
1924 * table_limit ::
1925 * End of valid table data.
1926 *
1927 * coords ::
1928 * Current normalized coordinates (F2DOT14).
1929 *
1930 * coord_count ::
1931 * Number of coordinates.
1932 *
1933 * depth ::
1934 * Recursion depth.
1935 *
1936 * @Return:
1937 * TRUE when the condition is satisfied, FALSE otherwise.
1938 */
1939 static FT_Bool
1940 tt_varc_eval_condition( TT_Face face,
1941 TT_Varc varc,
1942 FT_Byte* cond,
1943 FT_Byte* table_limit,
1944 FT_Fixed* coords,
1945 FT_UInt num_coords,
1946 FT_UInt depth )
1947 {
1948 FT_UInt format;
1949
1950
1951 /* A zero condition offset represents True. Top-level zero offsets */
1952 /* resolve to the start of the ConditionList. */
1953 if ( cond == varc->condition_list )
1954 return TRUE;
1955
1956 if ( !cond ||
1957 depth > TT_VARC_MAX_NESTING_LEVEL ||
1958 cond + 2 > table_limit )
1959 return FALSE;
1960
1961 format = ( (FT_UInt)cond[0] << 8 ) | cond[1];
1962
1963 switch ( format )
1964 {
1965 case 1: /* ConditionAxisRange */
1966 {
1967 FT_UInt axis_index;
1968 FT_Short min_val, max_val;
1969 FT_Fixed coord;
1970
1971
1972 if ( cond + 8 > table_limit )
1973 return FALSE;
1974
1975 axis_index = ( (FT_UInt)cond[2] << 8 ) | cond[3];
1976
1977 min_val = (FT_Short)( ( (FT_UShort)cond[4] << 8 ) |
1978 (FT_UShort)cond[5] );
1979 max_val = (FT_Short)( ( (FT_UShort)cond[6] << 8 ) |
1980 (FT_UShort)cond[7] );
1981
1982 coord = ( coords && axis_index < num_coords ) ? coords[axis_index]
1983 : 0;
1984
1985 /* min_val/max_val are F2DOT14; coords are 16.16 (F2DOT14 << 2) */
1986 return FT_BOOL( coord >= ( (FT_Fixed)min_val * 4 ) &&
1987 coord <= ( (FT_Fixed)max_val * 4 ) );
1988 }
1989
1990 case 2: /* ConditionValue */
1991 {
1992 FT_Short default_value;
1993 FT_UInt32 var_idx;
1994 FT_Long positive;
1995
1996
1997 if ( cond + 8 > table_limit )
1998 return FALSE;
1999
2000 default_value = (FT_Short)( ( (FT_UShort)cond[2] << 8 ) |
2001 (FT_UShort)cond[3] );
2002
2003 var_idx = ( (FT_UInt32)cond[4] << 24 ) |
2004 ( (FT_UInt32)cond[5] << 16 ) |
2005 ( (FT_UInt32)cond[6] << 8 ) |
2006 (FT_UInt32)cond[7];
2007
2008 /* Compare the unrounded default plus interpolated delta. */
2009 /* Even a small positive fraction makes a condition true. */
2010 positive = FT_BOOL( default_value > 0 );
2011 if ( var_idx != 0xFFFFFFFFUL )
2012 (void)tt_varc_get_item_deltas( face, varc, var_idx, 1,
2013 &positive, 0,
2014 coords, num_coords,
2015 &default_value );
2016
2017 return FT_BOOL( positive );
2018 }
2019
2020 case 3: /* ConditionAnd */
2021 case 4: /* ConditionOr */
2022 {
2023 FT_UInt count, i;
2024 FT_Byte* p;
2025 FT_Bool is_and = FT_BOOL( format == 3 );
2026
2027
2028 if ( cond + 3 > table_limit )
2029 return FALSE;
2030
2031 count = cond[2];
2032 p = cond + 3;
2033
2034 if ( p + (FT_Offset)count * 3 > table_limit )
2035 return FALSE;
2036
2037 for ( i = 0; i < count; i++ )
2038 {
2039 FT_UInt32 sub_off = ( (FT_UInt32)p[0] << 16 ) |
2040 ( (FT_UInt32)p[1] << 8 ) |
2041 (FT_UInt32)p[2];
2042 FT_Bool r;
2043
2044
2045 p += 3;
2046 r = sub_off == 0 ? TRUE
2047 : tt_varc_eval_condition( face, varc,
2048 cond + sub_off,
2049 table_limit,
2050 coords, num_coords,
2051 depth + 1 );
2052
2053 if ( is_and && !r )
2054 return FALSE;
2055 if ( !is_and && r )
2056 return TRUE;
2057 }
2058
2059 return is_and;
2060 }
2061
2062 case 5: /* ConditionNegate */
2063 {
2064 FT_UInt32 sub_off;
2065
2066
2067 if ( cond + 5 > table_limit )
2068 return FALSE;
2069
2070 sub_off = ( (FT_UInt32)cond[2] << 16 ) |
2071 ( (FT_UInt32)cond[3] << 8 ) |
2072 (FT_UInt32)cond[4];
2073
2074 if ( sub_off == 0 )
2075 return FALSE;
2076
2077 return FT_BOOL( !tt_varc_eval_condition( face, varc, cond + sub_off,
2078 table_limit, coords,
2079 num_coords, depth + 1 ) );
2080 }
2081
2082 default:
2083 /* Unknown format: treat as not satisfied (matches fontTools). */
2084 return FALSE;
2085 }
2086 }
2087
2088
2089 /**************************************************************************
2090 *
2091 * Axis Index Reading
2092 *
2093 */
2094
2095 /**************************************************************************
2096 *
2097 * @Function:
2098 * tt_varc_get_axis_indices
2099 *
2100 * @Description:
2101 * Read axis indices from the axis indices list (TupleList of integers).
2102 *
2103 * @Input:
2104 * varc ::
2105 * The VARC table.
2106 *
2107 * axis_indices_index ::
2108 * Index into the TupleList.
2109 *
2110 * num_indices ::
2111 * Number of indices to read.
2112 *
2113 * @Output:
2114 * indices ::
2115 * Array to store axis indices (caller allocates).
2116 *
2117 * @Return:
2118 * FreeType error code. 0 means success.
2119 */
2120 static FT_Error
2121 tt_varc_get_axis_indices( TT_Varc varc,
2122 FT_UInt axis_indices_index,
2123 FT_UInt num_indices,
2124 FT_UInt* indices )
2125 {
2126 FT_Error error;
2127
2128 FT_Byte* table_limit = (FT_Byte*)varc->table + varc->table_size;
2129 FT_Byte* tuple_data;
2130 FT_UInt tuple_size;
2131 FT_Byte* tuple_limit;
2132
2133 FT_Byte* p;
2134 FT_UInt i, j;
2135
2136
2137 if ( !varc || !varc->axis_indices_list || !indices )
2138 return FT_THROW( Invalid_Argument );
2139
2140 error = tt_varc_read_cff2_index_entry( varc->axis_indices_list,
2141 table_limit,
2142 axis_indices_index,
2143 &tuple_data, &tuple_size );
2144 if ( error )
2145 return error;
2146
2147 tuple_limit = tuple_data + tuple_size;
2148 p = tuple_data;
2149
2150 /* read indices using TupleValues encoding */
2151 i = 0;
2152 while ( i < num_indices && p < tuple_limit )
2153 {
2154 FT_Byte control;
2155 FT_UInt cnt;
2156
2157
2158 control = *p++;
2159 cnt = ( control & 0x3F ) + 1;
2160
2161 if ( cnt > num_indices - i )
2162 cnt = num_indices - i;
2163
2164 if ( ( control & 0xC0 ) == 0xC0 )
2165 {
2166 /* 32-bit values */
2167 if ( p + 4 * cnt > tuple_limit )
2168 return FT_THROW( Invalid_Table );
2169
2170 for ( j = 0; j < cnt && i < num_indices; j++, i++ )
2171 {
2172 indices[i] = ( (FT_UInt)p[0] << 24 ) |
2173 ( (FT_UInt)p[1] << 16 ) |
2174 ( (FT_UInt)p[2] << 8 ) |
2175 (FT_UInt)p[3];
2176 p += 4;
2177 }
2178 }
2179 else if ( control & 0x80 )
2180 {
2181 /* zeros */
2182 for ( j = 0; j < cnt && i < num_indices; j++, i++ )
2183 indices[i] = 0;
2184 }
2185 else if ( control & 0x40 )
2186 {
2187 /* 2-byte values */
2188 if ( p + 2 * cnt > tuple_limit )
2189 return FT_THROW( Invalid_Table );
2190
2191 for ( j = 0; j < cnt && i < num_indices; j++, i++ )
2192 {
2193 indices[i] = ( (FT_UInt)p[0] << 8 ) |
2194 (FT_UInt)p[1];
2195 p += 2;
2196 }
2197 }
2198 else
2199 {
2200 /* 1-byte values */
2201 if ( p + cnt > tuple_limit )
2202 return FT_THROW( Invalid_Table );
2203
2204 for ( j = 0; j < cnt && i < num_indices; j++, i++ )
2205 indices[i] = *p++;
2206 }
2207 }
2208
2209 if ( i < num_indices )
2210 return FT_THROW( Invalid_Table );
2211
2212 return FT_Err_Ok;
2213 }
2214
2215
2216 /**************************************************************************
2217 *
2218 * Variation Delta Application Functions
2219 *
2220 */
2221
2222 /**************************************************************************
2223 *
2224 * @Function:
2225 * tt_varc_apply_axis_deltas
2226 *
2227 * @Description:
2228 * Apply variation deltas to axis values using the
2229 * `MultiItemVariationStore`.
2230 *
2231 * @Input:
2232 * face ::
2233 * The font face.
2234 *
2235 * varc ::
2236 * The VARC table.
2237 *
2238 * component ::
2239 * The component whose axis values to vary.
2240 *
2241 * current_coords ::
2242 * Current normalized coordinates (F2DOT14).
2243 *
2244 * num_coords ::
2245 * Number of coords in `current_coords`.
2246 */
2247 static void
2248 tt_varc_apply_axis_deltas( TT_Face face,
2249 TT_Varc varc,
2250 TT_VarcComponent component,
2251 FT_Fixed* current_coords,
2252 FT_UInt num_coords )
2253 {
2254#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
2255
2256 FT_Error error;
2257 FT_Memory memory = face->root.memory;
2258
2259 FT_Long stack_deltas[VARC_STACK_DELTA_COUNT];
2260 FT_Long* deltas = NULL;
2261
2262 FT_UInt i;
2263
2264
2265 if ( !varc->multi_var_store_loaded || component->num_axis_values == 0 )
2266 return;
2267
2268 /* use stack allocation for small arrays, heap for large */
2269 if ( component->num_axis_values <= VARC_STACK_DELTA_COUNT )
2270 deltas = stack_deltas;
2271 else
2272 {
2273 if ( FT_NEW_ARRAY( deltas, component->num_axis_values ) )
2274 return;
2275 }
2276
2277 /* Get deltas with shift=2: raw F2DOT14 integers are returned */
2278 /* already shifted to F16DOT16, matching `axis_values` format. */
2279 error = tt_varc_get_item_deltas( face, varc,
2280 component->axis_values_var_index,
2281 component->num_axis_values,
2282 deltas, 2,
2283 current_coords,
2284 num_coords, NULL );
2285 if ( !error )
2286 {
2287 for ( i = 0; i < component->num_axis_values; i++ )
2288 component->axis_values[i] += deltas[i];
2289 }
2290#ifdef FT_DEBUG_LEVEL_TRACE
2291 else
2292 FT_TRACE2(( "tt_varc_apply_axis_deltas: variation index"
2293 " 0x%08lx failed (error 0x%x)\n",
2294 (FT_ULong)component->axis_values_var_index, error ));
2295#endif
2296
2297 if ( deltas != stack_deltas )
2298 FT_FREE( deltas );
2299
2300#else /* !TT_CONFIG_OPTION_GX_VAR_SUPPORT */
2301
2302 FT_UNUSED( face );
2303 FT_UNUSED( varc );
2304 FT_UNUSED( component );
2305 FT_UNUSED( current_coords );
2306 FT_UNUSED( num_coords );
2307
2308#endif /* !TT_CONFIG_OPTION_GX_VAR_SUPPORT */
2309 }
2310
2311
2312 /**************************************************************************
2313 *
2314 * @Function:
2315 * tt_varc_apply_transform_deltas
2316 *
2317 * @Description:
2318 * Apply variation deltas to transform values using the
2319 * `MultiItemVariationStore`.
2320 *
2321 * @Input:
2322 * face ::
2323 * The font face.
2324 *
2325 * varc ::
2326 * The VARC table.
2327 *
2328 * component ::
2329 * The component whose transform values to vary.
2330 *
2331 * current_coords ::
2332 * Current normalized coordinates (F2DOT14).
2333 *
2334 * num_coords ::
2335 * Number of coords in `current_coords`.
2336 */
2337 static void
2338 tt_varc_apply_transform_deltas( TT_Face face,
2339 TT_Varc varc,
2340 TT_VarcComponent component,
2341 FT_Fixed* current_coords,
2342 FT_UInt num_coords )
2343 {
2344#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
2345
2346 FT_Error error;
2347 FT_Memory memory = face->root.memory;
2348
2349 FT_Long stack_deltas[VARC_STACK_DELTA_COUNT];
2350 FT_Long* deltas = NULL;
2351 FT_UInt num_deltas = 0;
2352 FT_UInt delta_index = 0;
2353
2354
2355 if ( !varc->multi_var_store_loaded )
2356 return;
2357
2358 /* count how many transform components are present */
2359 if ( component->flags & VARC_HAVE_TRANSLATE_X ) num_deltas++;
2360 if ( component->flags & VARC_HAVE_TRANSLATE_Y ) num_deltas++;
2361 if ( component->flags & VARC_HAVE_ROTATION ) num_deltas++;
2362 if ( component->flags & VARC_HAVE_SCALE_X ) num_deltas++;
2363 if ( component->flags & VARC_HAVE_SCALE_Y ) num_deltas++;
2364 if ( component->flags & VARC_HAVE_SKEW_X ) num_deltas++;
2365 if ( component->flags & VARC_HAVE_SKEW_Y ) num_deltas++;
2366 if ( component->flags & VARC_HAVE_TCENTER_X ) num_deltas++;
2367 if ( component->flags & VARC_HAVE_TCENTER_Y ) num_deltas++;
2368
2369 if ( num_deltas == 0 )
2370 return;
2371
2372 /* use stack allocation for small arrays, heap for large */
2373 if ( num_deltas <= VARC_STACK_DELTA_COUNT )
2374 deltas = stack_deltas;
2375 else
2376 {
2377 if ( FT_NEW_ARRAY( deltas, num_deltas ) )
2378 return;
2379 }
2380
2381 /* Get deltas with shift=4: raw integers shifted left by 4. */
2382 /* F4DOT12 fields (rotation, skew) become F16DOT16 directly. */
2383 /* FWORD and F6DOT10 fields need an additional << 2 at callsite. */
2384 error = tt_varc_get_item_deltas( face, varc,
2385 component->transform_var_index,
2386 num_deltas,
2387 deltas, 4,
2388 current_coords,
2389 num_coords, NULL );
2390 if ( !error )
2391 {
2392 /* Apply deltas to transform components in order. */
2393 /* Deltas are already shifted left by 4 from get_item_deltas. */
2394 /* F4DOT12 fields (shift=4): add directly (already F16DOT16). */
2395 /* FWORD fields (shift=6): need << 2 more for 26.6 format. */
2396 /* F6DOT10 fields (shift=6): need << 2 more for F16DOT16. */
2397 delta_index = 0;
2398
2399 if ( component->flags & VARC_HAVE_TRANSLATE_X )
2400 {
2401 /* FWORD: 4+2=6 */
2402 component->translate_x += deltas[delta_index] * 4;
2403 delta_index++;
2404 }
2405
2406 if ( component->flags & VARC_HAVE_TRANSLATE_Y )
2407 {
2408 /* FWORD: 4+2=6 */
2409 component->translate_y += deltas[delta_index] * 4;
2410 delta_index++;
2411 }
2412
2413 if ( component->flags & VARC_HAVE_ROTATION )
2414 {
2415 /* F4DOT12: already 16.16 */
2416 component->rotation += deltas[delta_index];
2417 delta_index++;
2418 }
2419
2420 if ( component->flags & VARC_HAVE_SCALE_X )
2421 {
2422 /* F6DOT10: 4+2=6 */
2423 component->scale_x += deltas[delta_index] * 4;
2424 delta_index++;
2425 }
2426
2427 if ( component->flags & VARC_HAVE_SCALE_Y )
2428 {
2429 /* F6DOT10: 4+2=6 */
2430 component->scale_y += deltas[delta_index] * 4;
2431 delta_index++;
2432 }
2433
2434 if ( component->flags & VARC_HAVE_SKEW_X )
2435 {
2436 /* F4DOT12: already 16.16 */
2437 component->skew_x += deltas[delta_index];
2438 delta_index++;
2439 }
2440
2441 if ( component->flags & VARC_HAVE_SKEW_Y )
2442 {
2443 /* F4DOT12: already 16.16 */
2444 component->skew_y += deltas[delta_index];
2445 delta_index++;
2446 }
2447
2448 if ( component->flags & VARC_HAVE_TCENTER_X )
2449 {
2450 /* FWORD: 4+2=6 */
2451 component->tcenter_x += deltas[delta_index] * 4;
2452 delta_index++;
2453 }
2454
2455 if ( component->flags & VARC_HAVE_TCENTER_Y )
2456 {
2457 /* FWORD: 4+2=6 */
2458 component->tcenter_y += deltas[delta_index] * 4;
2459 delta_index++;
2460 }
2461
2462 /* When ScaleY is absent it mirrors ScaleX. Re-apply the mirror */
2463 /* after variation so a varying uniform scale stays uniform, */
2464 /* matching fontTools and skrifa (which redo this post-delta). */
2465 if ( ( component->flags & VARC_HAVE_SCALE_X ) &&
2466 !( component->flags & VARC_HAVE_SCALE_Y ) )
2467 component->scale_y = component->scale_x;
2468 }
2469#ifdef FT_DEBUG_LEVEL_TRACE
2470 else
2471 FT_TRACE2(( "tt_varc_apply_transform_deltas: variation index"
2472 " 0x%08lx failed (error 0x%x)\n",
2473 (FT_ULong)component->transform_var_index, error ));
2474#endif
2475
2476 if ( deltas != stack_deltas )
2477 FT_FREE( deltas );
2478
2479#else /* !TT_CONFIG_OPTION_GX_VAR_SUPPORT */
2480
2481 FT_UNUSED( face );
2482 FT_UNUSED( varc );
2483 FT_UNUSED( component );
2484 FT_UNUSED( current_coords );
2485 FT_UNUSED( num_coords );
2486
2487#endif /* !TT_CONFIG_OPTION_GX_VAR_SUPPORT */
2488 }
2489
2490
2491 /**************************************************************************
2492 *
2493 * Transform Matrix Functions
2494 *
2495 */
2496
2497 /**************************************************************************
2498 *
2499 * @Function:
2500 * tt_varc_build_transform
2501 *
2502 * @Description:
2503 * Build a transformation matrix from component transform values.
2504 *
2505 * Transformation order (per VARC spec):
2506 *
2507 * 1. translate to transformation center (-tCenterX, -tCenterY)
2508 * 2. apply scale (scaleX, scaleY)
2509 * 3. apply skew (skewX, skewY)
2510 * 4. apply rotation (rotation * pi)
2511 * 5. translate back from center (tCenterX, tCenterY)
2512 * 6. apply final translation (translateX, translateY)
2513 *
2514 * @Input:
2515 * component ::
2516 * The component with transform values.
2517 *
2518 * @Output:
2519 * matrix ::
2520 * The resulting 2x2 transformation matrix.
2521 *
2522 * offset ::
2523 * The translation vector.
2524 */
2525 static void
2526 tt_varc_build_transform( TT_VarcComponent component,
2527 FT_Matrix* matrix,
2528 FT_Vector* offset )
2529 {
2530 FT_Fixed scale_x = component->scale_x;
2531 FT_Fixed scale_y = component->scale_y;
2532 FT_Fixed skew_x = component->skew_x;
2533 FT_Fixed skew_y = component->skew_y;
2534 FT_Fixed rotation = component->rotation;
2535
2536
2537 /* Build rotation matrix */
2538 if ( rotation != 0 )
2539 {
2540 FT_Angle angle;
2541 FT_Vector v;
2542
2543
2544 /* Rotation is F4DOT12 in multiples of pi, converted to F16DOT16 by */
2545 /* << 4. FT_ANGLE_PI = 180 * 65536 (degrees), so this converts pi */
2546 /* multiples to degrees. */
2547 angle = FT_MulFix( rotation, FT_ANGLE_PI );
2548
2549 FT_Vector_Unit( &v, angle );
2550
2551 /* Rotation matrix: [cos -sin]
2552 [sin cos] */
2553 matrix->xx = v.x;
2554 matrix->xy = -v.y;
2555 matrix->yx = v.y;
2556 matrix->yy = v.x;
2557 }
2558 else
2559 {
2560 matrix->xx = 0x10000;
2561 matrix->xy = 0;
2562 matrix->yx = 0;
2563 matrix->yy = 0x10000;
2564 }
2565
2566 /* Apply scale - scale each column, not row. */
2567 /* First column (xx, yx) represents X-axis, scaled by `scale_x`. */
2568 /* Second column (xy, yy) represents Y-axis, scaled by `scale_y`. */
2569 if ( scale_x != 0x10000 || scale_y != 0x10000 )
2570 {
2571 matrix->xx = FT_MulFix( matrix->xx, scale_x );
2572 matrix->yx = FT_MulFix( matrix->yx, scale_x );
2573 matrix->xy = FT_MulFix( matrix->xy, scale_y );
2574 matrix->yy = FT_MulFix( matrix->yy, scale_y );
2575 }
2576
2577 /* apply skew */
2578 if ( skew_x != 0 || skew_y != 0 )
2579 {
2580 FT_Matrix skew_matrix;
2581 FT_Fixed tan_x, tan_y;
2582
2583
2584 /* Convert skew angles to tangent values. */
2585 /* Skew is in radians * pi, so we need to compute tan(skew * pi). */
2586 tan_x = skew_x ? FT_Tan( FT_MulFix( skew_x, FT_ANGLE_PI ) ) : 0;
2587 tan_y = skew_y ? FT_Tan( FT_MulFix( skew_y, FT_ANGLE_PI ) ) : 0;
2588
2589 /* The transform uses skew(-skewX, skewY), so only the xy term */
2590 /* (the skewX contribution) is negated; the yx (skewY) term is */
2591 /* positive. Matches HarfBuzz/skrifa/fontTools. */
2592 skew_matrix.xx = 0x10000;
2593 skew_matrix.xy = -tan_x;
2594 skew_matrix.yx = tan_y;
2595 skew_matrix.yy = 0x10000;
2596
2597 /* Apply skew on the RIGHT so the linear part is Rot*Scale*Skew */
2598 /* (skew applied to the point first). `FT_Matrix_Multiply(a, b)` */
2599 /* computes b = a*b, so this leaves skew_matrix = matrix*skew; */
2600 /* copy it back into matrix. */
2601 FT_Matrix_Multiply( matrix, &skew_matrix );
2602 *matrix = skew_matrix;
2603 }
2604
2605 /* calculate translation with center point transformation */
2606 offset->x = component->translate_x;
2607 offset->y = component->translate_y;
2608
2609 if ( component->tcenter_x != 0 || component->tcenter_y != 0 )
2610 {
2611 FT_Vector center;
2612
2613
2614 center.x = component->tcenter_x;
2615 center.y = component->tcenter_y;
2616
2617 /* offset += center - matrix * center */
2618 offset->x += center.x - FT_MulFix( matrix->xx, center.x ) -
2619 FT_MulFix( matrix->xy, center.y );
2620 offset->y += center.y - FT_MulFix( matrix->yx, center.x ) -
2621 FT_MulFix( matrix->yy, center.y );
2622 }
2623 }
2624
2625
2626 /**************************************************************************
2627 *
2628 * Recursion Context Functions
2629 *
2630 */
2631
2632 /**************************************************************************
2633 *
2634 * @Function:
2635 * tt_varc_context_push
2636 *
2637 * @Description:
2638 * Push a glyph ID onto the recursion stack.
2639 *
2640 * @Input:
2641 * context ::
2642 * The recursion context.
2643 *
2644 * glyph_index ::
2645 * The glyph ID to push.
2646 *
2647 * @Return:
2648 * FreeType error code. Returns `FT_Err_Invalid_Table` if cycle
2649 * detected or max depth exceeded.
2650 */
2651 static FT_Error
2652 tt_varc_context_push( TT_VarcContext context,
2653 FT_UInt glyph_index )
2654 {
2655 FT_UInt i;
2656
2657
2658 /* check max recursion depth */
2659 if ( context->recursion_depth >= TT_VARC_MAX_NESTING_LEVEL )
2660 {
2661 FT_TRACE2(( "tt_varc_context_push: nesting limit reached"
2662 " for glyph %u\n",
2663 glyph_index ));
2664 return FT_THROW( Invalid_Table );
2665 }
2666
2667 /* check for cycles */
2668 for ( i = 0; i < context->recursion_depth; i++ )
2669 {
2670 if ( context->visited_glyphs[i] == glyph_index )
2671 {
2672 FT_TRACE2(( "tt_varc_context_push: cycle detected at glyph %u\n",
2673 glyph_index ));
2674 return FT_THROW( Invalid_Table ); /* cycle detected */
2675 }
2676 }
2677
2678 /* push onto stack */
2679 context->visited_glyphs[context->recursion_depth] = glyph_index;
2680 context->recursion_depth++;
2681
2682 return FT_Err_Ok;
2683 }
2684
2685
2686 /**************************************************************************
2687 *
2688 * @Function:
2689 * tt_varc_context_pop
2690 *
2691 * @Description:
2692 * Pop a glyph ID from the recursion stack.
2693 *
2694 * @Input:
2695 * context ::
2696 * The recursion context.
2697 */
2698 static void
2699 tt_varc_context_pop( TT_VarcContext context )
2700 {
2701 if ( context->recursion_depth > 0 )
2702 context->recursion_depth--;
2703 }
2704
2705
2706 /**************************************************************************
2707 *
2708 * VARC Table Loading Functions
2709 *
2710 */
2711
2712 /**************************************************************************
2713 *
2714 * @Function:
2715 * tt_face_load_varc
2716 *
2717 * @Description:
2718 * Load the 'VARC' table from the font file.
2719 *
2720 * @Input:
2721 * face ::
2722 * The font face.
2723 *
2724 * stream ::
2725 * The font stream.
2726 *
2727 * @Return:
2728 * FreeType error code. 0 means success.
2729 */
2730 FT_LOCAL_DEF( FT_Error )
2731 tt_face_load_varc( FT_Face ftface,
2732 FT_Stream stream )
2733 {
2734 TT_Face face = (TT_Face)ftface;
2735 FT_Memory memory = face->root.memory;
2736 FT_Error error;
2737
2738 FT_Byte* table = NULL;
2739 FT_ULong table_size;
2740 TT_Varc varc = NULL;
2741 FT_Byte* p;
2742
2743
2744 /* Initialize VARC loading flag */
2745 face->varc_loading_components = FALSE;
2746 face->varc_context = NULL;
2747
2748 FT_TRACE2(( "VARC " ));
2749
2750 error = face->goto_table( face, TTAG_VARC, stream, &table_size );
2751 if ( error )
2752 {
2753 FT_TRACE2(( "is missing\n" ));
2754 goto Exit;
2755 }
2756 if ( table_size < 24 ) /* minimum header size */
2757 {
2758 FT_TRACE2(( "is too short\n" ));
2759 goto Exit;
2760 }
2761
2762 if ( FT_FRAME_EXTRACT( table_size, table ) )
2763 {
2764 FT_TRACE2(( "could not be read\n" ));
2765 goto Exit;
2766 }
2767
2768 if ( FT_NEW( varc ) )
2769 {
2770 FT_TRACE2(( "could not be loaded\n" ));
2771 goto Fail;
2772 }
2773
2774 varc->table = table;
2775 varc->table_size = table_size;
2776
2777 p = table;
2778
2779 /* read header */
2780 varc->version_major = ( p[0] << 8 ) | p[1];
2781 varc->version_minor = ( p[2] << 8 ) | p[3];
2782 p += 4;
2783
2784 /* check version */
2785 if ( varc->version_major != 1 || varc->version_minor != 0 )
2786 {
2787 FT_TRACE2(( "has unsupported version %u.%u\n",
2788 varc->version_major, varc->version_minor ));
2789 error = FT_THROW( Invalid_Table );
2790 goto Fail;
2791 }
2792
2793 /* read offsets */
2794 varc->coverage_offset = ( (FT_ULong)p[0] << 24 ) |
2795 ( (FT_ULong)p[1] << 16 ) |
2796 ( (FT_ULong)p[2] << 8 ) |
2797 (FT_ULong)p[3];
2798 p += 4;
2799
2800 varc->multi_var_store_offset = ( (FT_ULong)p[0] << 24 ) |
2801 ( (FT_ULong)p[1] << 16 ) |
2802 ( (FT_ULong)p[2] << 8 ) |
2803 (FT_ULong)p[3];
2804 p += 4;
2805
2806 varc->condition_list_offset = ( (FT_ULong)p[0] << 24 ) |
2807 ( (FT_ULong)p[1] << 16 ) |
2808 ( (FT_ULong)p[2] << 8 ) |
2809 (FT_ULong)p[3];
2810 p += 4;
2811
2812 varc->axis_indices_list_offset = ( (FT_ULong)p[0] << 24 ) |
2813 ( (FT_ULong)p[1] << 16 ) |
2814 ( (FT_ULong)p[2] << 8 ) |
2815 (FT_ULong)p[3];
2816 p += 4;
2817
2818 varc->var_composite_glyphs_offset = ( (FT_ULong)p[0] << 24 ) |
2819 ( (FT_ULong)p[1] << 16 ) |
2820 ( (FT_ULong)p[2] << 8 ) |
2821 (FT_ULong)p[3];
2822
2823 /* validate offsets */
2824 if ( varc->coverage_offset >= table_size ||
2825 varc->var_composite_glyphs_offset >= table_size )
2826 {
2827 FT_TRACE2(( "has invalid offsets\n" ));
2828 error = FT_THROW( Invalid_Table );
2829 goto Fail;
2830 }
2831
2832 /* set up pointers */
2833 varc->coverage = table + varc->coverage_offset;
2834 varc->var_composite_glyphs = table + varc->var_composite_glyphs_offset;
2835
2836 if ( varc->condition_list_offset > 0 &&
2837 varc->condition_list_offset < table_size )
2838 varc->condition_list = table + varc->condition_list_offset;
2839 else
2840 varc->condition_list = NULL;
2841
2842 if ( varc->axis_indices_list_offset > 0 &&
2843 varc->axis_indices_list_offset < table_size )
2844 varc->axis_indices_list = table + varc->axis_indices_list_offset;
2845 else
2846 varc->axis_indices_list = NULL;
2847
2848#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
2849
2850 /* `MultiItemVariationStore` is NOT the same as `ItemVariationStore`! */
2851 /* `MultiItemVariationStore` stores TUPLES of deltas, */
2852 /* not single deltas. */
2853 varc->multi_var_store_loaded = FALSE;
2854 varc->multi_var_store = NULL;
2855
2856 if ( varc->multi_var_store_offset > 0 &&
2857 varc->multi_var_store_offset < table_size )
2858 {
2859 /* point to the `MultiItemVariationStore` data in the 'VARC' table */
2860 varc->multi_var_store = table + varc->multi_var_store_offset;
2861 varc->multi_var_store_loaded = TRUE;
2862
2863 /* TODO: Implement proper MultiItemVariationStore parser:
2864 * - Format: u16 (must be 1)
2865 * - Regions: Offset32 to SparseVarRegionList
2866 * - DataSets: Array16 of Offset32 to MultiVarData
2867 * MultiVarData format:
2868 * - Format: u8 (must be 1)
2869 * - RegionIndices: Array16 of u16
2870 * - DeltaSets: INDEX2 of TupleValues (CFF2-style index)
2871 */
2872 }
2873
2874#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
2875
2876 face->varc = varc;
2877
2878 FT_TRACE2(( "loaded\n" ));
2879 FT_TRACE4(( " version %u.%u, %lu bytes\n",
2880 varc->version_major,
2881 varc->version_minor,
2882 varc->table_size ));
2883 FT_TRACE4(( " offsets: coverage %lu, variation store %lu,\n",
2884 varc->coverage_offset,
2885 varc->multi_var_store_offset ));
2886 FT_TRACE4(( " conditions %lu, axis indices %lu, glyphs %lu\n",
2887 varc->condition_list_offset,
2888 varc->axis_indices_list_offset,
2889 varc->var_composite_glyphs_offset ));
2890 goto Exit;
2891
2892 Fail:
2893 FT_FREE( varc );
2894 FT_FRAME_RELEASE( table );
2895
2896 Exit:
2897 return error;
2898 }
2899
2900
2901 /**************************************************************************
2902 *
2903 * @Function:
2904 * tt_face_free_varc
2905 *
2906 * @Description:
2907 * Free the 'VARC' table resources.
2908 *
2909 * @Input:
2910 * face ::
2911 * The font face.
2912 */
2913 FT_LOCAL_DEF( void )
2914 tt_face_free_varc( FT_Face ftface )
2915 {
2916 TT_Face face = (TT_Face)ftface;
2917 FT_Memory memory = face->root.memory;
2918 FT_Stream stream = face->root.stream;
2919 TT_Varc varc = (TT_Varc)face->varc;
2920
2921
2922 if ( varc )
2923 {
2924#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
2925 /* `MultiItemVariationStore` cleanup. */
2926 /* No special cleanup needed - */
2927 /* we just point to data in the 'VARC' table. */
2928 varc->multi_var_store = NULL;
2929 varc->multi_var_store_loaded = FALSE;
2930#endif
2931
2932 FT_FRAME_RELEASE( varc->table );
2933 FT_FREE( varc );
2934 face->varc = NULL;
2935 }
2936 }
2937
2938
2939 /**************************************************************************
2940 *
2941 * @Function:
2942 * tt_face_has_varc_glyph
2943 *
2944 * @Description:
2945 * Check whether a glyph has VARC data.
2946 *
2947 * @Input:
2948 * face ::
2949 * The font face.
2950 *
2951 * glyph_index ::
2952 * The glyph ID to check.
2953 *
2954 * @Return:
2955 * TRUE if the glyph has VARC data, FALSE otherwise.
2956 */
2957 FT_LOCAL_DEF( FT_Bool )
2958 tt_face_has_varc_glyph( FT_Face ftface,
2959 FT_UInt glyph_index )
2960 {
2961 TT_Face face = (TT_Face)ftface;
2962 TT_Varc varc = (TT_Varc)face->varc;
2963 FT_Int coverage_idx;
2964
2965
2966 if ( !varc )
2967 return FALSE;
2968
2969 coverage_idx = tt_varc_get_coverage( varc, glyph_index );
2970 return coverage_idx >= 0;
2971 }
2972
2973
2974 /**************************************************************************
2975 *
2976 * VARC Glyph Loading Functions
2977 *
2978 */
2979
2980#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
2981
2982 /**************************************************************************
2983 *
2984 * @Function:
2985 * tt_varc_set_normalized_coords
2986 *
2987 * @Description:
2988 * Lightweight coordinate swap for VARC component loading.
2989 * Unlike `FT_Set_Var_Blend_Coordinates`, this only sets the normalized
2990 * coordinates and invalidates the gvar tuple scalar cache. It skips
2991 * CVT reload, MVAR, auto-hinter invalidation, PS name construction,
2992 * and design coordinate computation - none of which are needed when
2993 * loading VARC components with `FT_LOAD_NO_HINTING`.
2994 *
2995 * @Input:
2996 * face ::
2997 * The font face.
2998 *
2999 * coords ::
3000 * Current normalized coordinates (F2DOT14).
3001 *
3002 * coord_count ::
3003 * Number of coordinates.
3004 */
3005 static void
3006 tt_varc_set_normalized_coords( TT_Face face,
3007 FT_Fixed* coords,
3008 FT_UInt num_coords )
3009 {
3010 GX_Blend blend = face->blend;
3011 FT_UInt i;
3012
3013
3014 if ( !blend || !blend->normalizedcoords )
3015 return;
3016
3017 if ( num_coords > blend->num_axis )
3018 num_coords = blend->num_axis;
3019
3020 FT_MEM_COPY( blend->normalizedcoords,
3021 coords,
3022 num_coords * sizeof ( FT_Fixed ) );
3023
3024 /* zero out remaining axes */
3025 for ( i = num_coords; i < blend->num_axis; i++ )
3026 blend->normalizedcoords[i] = 0;
3027
3028 /* update `doblend` flag */
3029 face->doblend = FALSE;
3030 for ( i = 0; i < blend->num_axis; i++ )
3031 {
3032 if ( blend->normalizedcoords[i] )
3033 {
3034 face->doblend = TRUE;
3035 break;
3036 }
3037 }
3038
3039 /* Update face variation flag so IS_DEFAULT_INSTANCE is correct. */
3040 /* Without this, `load_truetype_glyph` skips gvar deltas entirely. */
3041 if ( face->doblend )
3042 face->root.face_flags |= FT_FACE_FLAG_VARIATION;
3043 else
3044 face->root.face_flags &= ~FT_FACE_FLAG_VARIATION;
3045
3046 /* invalidate gvar tuple scalar cache */
3047 for ( i = 0; i < blend->tuplecount; i++ )
3048 blend->tuplescalars[i] = (FT_Fixed)-0x20000L;
3049 }
3050
3051#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
3052
3053
3054 /**************************************************************************
3055 *
3056 * @Function:
3057 * tt_face_load_varc_glyph
3058 *
3059 * @Description:
3060 * Load a VARC glyph into the glyph slot.
3061 *
3062 * @Input:
3063 * ftface ::
3064 * The font face.
3065 *
3066 * glyph_slot ::
3067 * The glyph slot to load into.
3068 *
3069 * glyph_index ::
3070 * The glyph ID to load.
3071 *
3072 * load_flags ::
3073 * Loading flags.
3074 *
3075 * @Return:
3076 * FreeType error code. 0 means success.
3077 */
3078 FT_LOCAL_DEF( FT_Error )
3079 tt_face_load_varc_glyph( FT_Face ftface,
3080 FT_GlyphSlot glyph_slot,
3081 FT_UInt glyph_index,
3082 FT_Int32 load_flags )
3083 {
3084 TT_Face face = (TT_Face)ftface;
3085 FT_Memory memory = face->root.memory;
3086 FT_Error error;
3087
3088 TT_Varc varc = (TT_Varc)face->varc;
3089 FT_Byte* record_data;
3090 FT_UInt record_size;
3091
3092 FT_Byte* p = NULL;
3093 FT_Byte* limit;
3094
3095 TT_VarcContext context;
3096 FT_Bool context_owner = FALSE;
3097
3098 TT_GlyphSlot slot = (TT_GlyphSlot)glyph_slot;
3099 FT_GlyphLoader gloader;
3100
3101 FT_Fixed* parent_coords = NULL;
3102 FT_UInt num_coords = 0;
3103#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
3104 FT_Bool static_axes = FALSE;
3105#endif
3106
3107 FT_Fixed stack_new_coords[VARC_STACK_COORD_COUNT];
3108 FT_UInt stack_axis_indices[VARC_STACK_INDICES_COUNT];
3109 FT_Fixed stack_axis_values[VARC_STACK_AXIS_COUNT];
3110
3111 FT_Matrix saved_transform_matrix;
3112 FT_Vector saved_transform_delta;
3113 FT_UInt saved_transform_flags;
3114
3115
3116 if ( !varc )
3117 return FT_THROW( Invalid_Table );
3118
3119 /* get or create recursion context */
3120 context = (TT_VarcContext)face->varc_context;
3121 if ( !context )
3122 {
3123 /* first call - create new context on heap */
3124 if ( FT_NEW( context ) )
3125 return error;
3126
3127 FT_ZERO( context );
3128 face->varc_context = context;
3129 context_owner = TRUE;
3130
3131 /* save font's current variation coordinates */
3132 /* for inheritance at depth 1 */
3133#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
3134
3135 {
3136 FT_MM_Var* master;
3137
3138
3139 /* `FT_Get_MM_Var` ensures `face->blend` is initialized */
3140 /* (it's lazy) */
3141 error = FT_Get_MM_Var( (FT_Face)face, &master );
3142 if ( !error && master )
3143 {
3144 if ( master->num_axis > 0 )
3145 context->num_font_coords = master->num_axis;
3146 FT_Done_MM_Var( face->root.driver->root.library,
3147 master );
3148 }
3149 else if ( error &&
3150 !FT_HAS_MULTIPLE_MASTERS( (FT_Face)face ) )
3151 {
3152 if ( !face->blend )
3153 (void)tt_var_init_gvar( (FT_Face)face );
3154
3155 if ( face->blend && !face->blend->mmvar )
3156 {
3157 (void)tt_get_var_blend( (FT_Face)face,
3158 &context->num_font_coords,
3159 NULL,
3160 NULL,
3161 NULL );
3162 static_axes = TRUE;
3163 }
3164 }
3165 error = FT_Err_Ok; /* non-fatal */
3166 }
3167
3168 if ( context->num_font_coords > 0 )
3169 {
3170 if ( !FT_NEW_ARRAY( context->font_coords,
3171 context->num_font_coords ) )
3172 {
3173 if ( static_axes )
3174 FT_MEM_COPY( context->font_coords,
3175 face->blend->normalizedcoords,
3176 context->num_font_coords * sizeof ( FT_Fixed ) );
3177 else
3178 error = FT_Get_Var_Blend_Coordinates(
3179 (FT_Face)face,
3180 context->num_font_coords,
3181 context->font_coords );
3182
3183 if ( error )
3184 {
3185 FT_FREE( context->font_coords );
3186 context->num_font_coords = 0;
3187 }
3188 else
3189 {
3190 FT_Generic saved_autohint = face->root.autohint;
3191
3192
3193 /* set initial current_coords to font_coords */
3194 context->current_coords = context->font_coords;
3195 context->num_current_coords = context->num_font_coords;
3196
3197 if ( !static_axes )
3198 {
3199 /* Force blend initialization: allocate */
3200 /* `normalizedcoords`, load 'gvar', etc. This ensures */
3201 /* `tt_varc_set_normalized_coords` can work as a */
3202 /* lightweight coordinate swapper later. */
3203 face->root.autohint.data = NULL;
3204 face->root.autohint.finalizer = NULL;
3205
3206 FT_Set_Var_Blend_Coordinates( (FT_Face)face,
3207 context->num_font_coords,
3208 context->font_coords );
3209
3210 face->root.autohint = saved_autohint;
3211 }
3212 }
3213 }
3214 }
3215
3216#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
3217 }
3218
3219 /* Save the face's current transform (user's transform at top level, */
3220 /* parent's VARC transform at recursive levels). We restore or clear */
3221 /* this before returning so `FT_Load_Glyph`'s post-processing does the */
3222 /* right thing. */
3223 saved_transform_matrix = face->root.internal->transform_matrix;
3224 saved_transform_delta = face->root.internal->transform_delta;
3225 saved_transform_flags = face->root.internal->transform_flags;
3226
3227 /* get parent coords from context for delta evaluation */
3228 parent_coords = context->current_coords;
3229 num_coords = context->num_current_coords;
3230
3231 /* push this glyph onto recursion stack */
3232 error = tt_varc_context_push( context, glyph_index );
3233 if ( error )
3234 {
3235 if ( context_owner )
3236 face->varc_context = NULL;
3237 return error;
3238 }
3239
3240 /* get glyph record */
3241 error = tt_varc_get_glyph_record( varc, glyph_index,
3242 &record_data, &record_size );
3243 if ( error )
3244 goto Cleanup;
3245
3246 p = record_data;
3247 limit = record_data + record_size;
3248
3249 FT_TRACE4(( "VARC glyph %u: nesting level %u, %u-byte record\n",
3250 glyph_index, context->recursion_depth, record_size ));
3251
3252 /* Allocate a reusable temp glyph slot for component loading. */
3253 /* `FT_New_GlyphSlot` prepends to `face->glyph` list, so */
3254 /* `FT_Load_Glyph` will load into this slot. One slot per */
3255 /* recursion level suffices. */
3256 {
3257 FT_GlyphSlot temp_glyph;
3258
3259
3260 error = FT_New_GlyphSlot( (FT_Face)face, &temp_glyph );
3261 if ( error )
3262 goto Cleanup;
3263 }
3264
3265 /* Accumulate into the slot's own loader, like the glyf composite */
3266 /* path; the slot owns this storage and frees it on destruction. */
3267 gloader = slot->internal->loader;
3268 FT_GlyphLoader_Rewind( gloader );
3269
3270 /* parse components until we run out of data */
3271 while ( p < limit )
3272 {
3273 TT_VarcComponentRec component;
3274 FT_GlyphSlot component_slot;
3275
3276 FT_Matrix matrix;
3277 FT_Vector offset;
3278 FT_Matrix composed_matrix;
3279 FT_Vector composed_delta;
3280
3281 FT_Matrix saved_parent_matrix;
3282 FT_Vector saved_parent_delta;
3283 FT_Bool saved_has_parent;
3284
3285 FT_Fixed* new_coords;
3286 FT_Bool new_coords_on_heap;
3287 FT_Bool has_axis_override;
3288
3289
3290 /* parse component */
3291 error = tt_varc_parse_component( face, varc,
3292 &p, limit,
3293 &component,
3294 stack_axis_values,
3295 VARC_STACK_AXIS_COUNT );
3296 if ( error )
3297 {
3298 /* skip malformed component and continue with others */
3299 FT_TRACE2(( "tt_face_load_varc_glyph: malformed component"
3300 " in glyph %u (error 0x%x)\n",
3301 glyph_index, error ));
3302 break; /* exit loop - can't safely continue parsing */
3303 /* if we don't know component size */
3304 }
3305
3306 FT_TRACE5(( " component glyph %u, flags 0x%08lx\n",
3307 component.gid, (FT_ULong)component.flags ));
3308
3309 /* Evaluate the component's condition, if any. When it is not */
3310 /* satisfied, the component is not rendered, matching HarfBuzz, */
3311 /* skrifa, and fontTools. */
3312 if ( component.flags & VARC_HAVE_CONDITION )
3313 {
3314 FT_Byte* table_limit = (FT_Byte*)varc->table + varc->table_size;
3315 FT_Byte* cond =
3316 tt_varc_get_condition( varc,
3317 component.condition_index,
3318 table_limit );
3319
3320
3321#ifdef FT_DEBUG_LEVEL_TRACE
3322 if ( !cond )
3323 FT_TRACE2(( " component glyph %u has invalid condition %lu;"
3324 " ignoring condition\n",
3325 component.gid,
3326 (FT_ULong)component.condition_index ));
3327#endif
3328 if ( cond &&
3329 !tt_varc_eval_condition( face, varc, cond, table_limit,
3330 parent_coords, num_coords, 0 ) )
3331 {
3332 FT_TRACE5(( " condition %lu is false; skipping component\n",
3333 (FT_ULong)component.condition_index ));
3334 tt_varc_free_component( face, &component );
3335 continue;
3336 }
3337#ifdef FT_DEBUG_LEVEL_TRACE
3338 else
3339 FT_TRACE6(( " condition %lu is true\n",
3340 (FT_ULong)component.condition_index ));
3341#endif
3342 }
3343
3344 /* apply variation deltas to axis values if present */
3345 if ( component.flags & VARC_AXES_HAVE_VARIATION )
3346 tt_varc_apply_axis_deltas( face, varc, &component,
3347 parent_coords, num_coords );
3348
3349 /* apply variation deltas to transform values if present */
3350 if ( component.flags & VARC_TRANSFORM_HAS_VARIATION )
3351 tt_varc_apply_transform_deltas( face, varc, &component,
3352 parent_coords, num_coords );
3353
3354 /* build transformation matrix */
3355 tt_varc_build_transform( &component, &matrix, &offset );
3356
3357 /* apply axis value overrides if present */
3358 new_coords = NULL;
3359 new_coords_on_heap = FALSE;
3360 has_axis_override = FALSE;
3361
3362#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
3363
3364 if ( ( ( component.num_axis_values > 0 && component.axis_values ) ||
3365 ( component.flags & VARC_RESET_UNSPECIFIED_AXES ) ) &&
3366 num_coords > 0 &&
3367 parent_coords )
3368 {
3369 /* allocate new_coords only */
3370 /* (`parent_coords` replaces `saved_coords`) */
3371 if ( num_coords <= VARC_STACK_COORD_COUNT )
3372 new_coords = stack_new_coords;
3373 else
3374 {
3375 if ( FT_NEW_ARRAY( new_coords, num_coords ) )
3376 goto Skip_Axis_Override;
3377 new_coords_on_heap = TRUE;
3378 }
3379
3380 /* start with current coordinates or reset to default */
3381 if ( component.flags & VARC_RESET_UNSPECIFIED_AXES )
3382 {
3383 /* Reset unspecified axes to the font's current instance */
3384 /* coordinates, matching HarfBuzz (font->coords), skrifa, and */
3385 /* fontTools. Axes explicitly specified below are overridden */
3386 /* afterwards. */
3387 FT_UInt j;
3388 FT_UInt ncopy = context->num_font_coords < num_coords
3389 ? context->num_font_coords : num_coords;
3390
3391
3392 if ( context->font_coords )
3393 FT_MEM_COPY( new_coords, context->font_coords,
3394 ncopy * sizeof ( FT_Fixed ) );
3395 else
3396 ncopy = 0;
3397
3398 for ( j = ncopy; j < num_coords; j++ )
3399 new_coords[j] = 0; /* default for axes beyond `font_coords` */
3400 }
3401 else
3402 {
3403 /* inherit from parent coords (handles both depth 1 and > 1) */
3404 FT_MEM_COPY( new_coords, parent_coords,
3405 num_coords * sizeof ( FT_Fixed ) );
3406 }
3407
3408 /* read axis indices and apply values */
3409 if ( component.flags & VARC_HAVE_AXES &&
3410 varc->axis_indices_list )
3411 {
3412 FT_UInt* axis_indices = NULL;
3413 FT_UInt j;
3414
3415
3416 /* allocate array for axis indices */
3417 if ( component.num_axis_values <= VARC_STACK_INDICES_COUNT )
3418 axis_indices = stack_axis_indices;
3419 else
3420 {
3421 if ( FT_NEW_ARRAY( axis_indices, component.num_axis_values ) )
3422 {
3423 if ( new_coords_on_heap )
3424 FT_FREE( new_coords );
3425 new_coords = NULL;
3426 goto Skip_Axis_Override;
3427 }
3428 }
3429
3430 /* read axis indices from TupleList */
3431 error = tt_varc_get_axis_indices( varc,
3432 component.axis_indices_index,
3433 component.num_axis_values,
3434 axis_indices );
3435 if ( error )
3436 {
3437 FT_TRACE2(( " component glyph %u has invalid axis indices"
3438 " (error 0x%x)\n",
3439 component.gid, error ));
3440 if ( axis_indices != stack_axis_indices )
3441 FT_FREE( axis_indices );
3442 if ( new_coords_on_heap )
3443 FT_FREE( new_coords );
3444 new_coords = NULL;
3445 goto Skip_Axis_Override;
3446 }
3447
3448 /* apply axis values using the indices */
3449 for ( j = 0; j < component.num_axis_values; j++ )
3450 {
3451 FT_UInt axis_idx = axis_indices[j];
3452 FT_Fixed v = component.axis_values[j];
3453
3454
3455 if ( axis_idx < num_coords )
3456 {
3457 /* Round to the nearest F2DOT14 grid point (a multiple of 4 */
3458 /* in 16.16) before the value feeds the leaf's gvar */
3459 /* interpolation, matching HarfBuzz (roundf) and skrifa. */
3460 if ( v >= 0 )
3461 v = ( v + 2 ) & ~(FT_Fixed)3;
3462 else
3463 v = NEG_LONG( ( NEG_LONG( v ) + 2 ) & ~(FT_Fixed)3 );
3464
3465 new_coords[axis_idx] = v;
3466 FT_TRACE7(( " axis %u: %.5f\n",
3467 axis_idx, (double)v / 65536 ));
3468 }
3469#ifdef FT_DEBUG_LEVEL_TRACE
3470 else
3471 FT_TRACE2(( " component glyph %u has out-of-range axis %u\n",
3472 component.gid, axis_idx ));
3473#endif
3474 }
3475
3476 if ( axis_indices != stack_axis_indices )
3477 FT_FREE( axis_indices );
3478 }
3479
3480 /* Apply the new normalized coordinates. Use lightweight path */
3481 /* that only swaps coords and invalidates the gvar tuple cache. */
3482 tt_varc_set_normalized_coords( face, new_coords, num_coords );
3483 has_axis_override = TRUE;
3484 }
3485
3486 Skip_Axis_Override:
3487
3488#endif /* TT_CONFIG_OPTION_GX_VAR_SUPPORT */
3489
3490 /* Load component - allow recursive VARC loading. */
3491 /* Compose transforms: we want (T1*T2)(leaf) not T1(T2(leaf)). */
3492
3493 /* save current parent transform */
3494 saved_parent_matrix = context->parent_matrix;
3495 saved_parent_delta = context->parent_delta;
3496 saved_has_parent = context->has_parent_transform;
3497
3498 /* compose with parent transform if present */
3499 if ( context->has_parent_transform )
3500 {
3501 /* Compose: final = parent * child. `FT_Matrix_Multiply(a, b)` */
3502 /* computes b = a*b, so seed the result with the child matrix */
3503 /* and left-multiply by the parent; this matches the */
3504 /* composed_delta below (the translation of parent*child) and */
3505 /* HarfBuzz/skrifa. */
3506 composed_matrix = matrix;
3507 FT_Matrix_Multiply( &context->parent_matrix, &composed_matrix );
3508
3509 /* transform offset by parent matrix and add parent delta */
3510 composed_delta.x = FT_MulFix( offset.x, context->parent_matrix.xx ) +
3511 FT_MulFix( offset.y, context->parent_matrix.xy ) +
3512 context->parent_delta.x;
3513 composed_delta.y = FT_MulFix( offset.x, context->parent_matrix.yx ) +
3514 FT_MulFix( offset.y, context->parent_matrix.yy ) +
3515 context->parent_delta.y;
3516 }
3517 else
3518 {
3519 /* no parent transform, use component transform directly */
3520 composed_matrix = matrix;
3521 composed_delta = offset;
3522 }
3523
3524 /* store composed transform in context for child VARC components */
3525 context->parent_matrix = composed_matrix;
3526 context->parent_delta = composed_delta;
3527 context->has_parent_transform = TRUE;
3528
3529 FT_TRACE7(( " matrix: [ %.5f %.5f; %.5f %.5f ]\n",
3530 (double)composed_matrix.xx / 65536,
3531 (double)composed_matrix.xy / 65536,
3532 (double)composed_matrix.yx / 65536,
3533 (double)composed_matrix.yy / 65536 ));
3534 FT_TRACE7(( " offset: (%.5f, %.5f)\n",
3535 (double)composed_delta.x / 64,
3536 (double)composed_delta.y / 64 ));
3537
3538
3539 /* set child's coords in context for recursive VARC processing */
3540#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
3541 if ( has_axis_override )
3542 {
3543 context->current_coords = new_coords;
3544 context->num_current_coords = num_coords;
3545 }
3546#endif
3547
3548 /* Set composed VARC transform on face so `FT_Load_Glyph` applies */
3549 /* it. The matrix is dimensionless (16.16) and works in any */
3550 /* coordinate system. The delta must match the outline's coordinate */
3551 /* space: 26.6 device pixels (scaled) or integer font units */
3552 /* (NO_SCALE). */
3553 {
3554 FT_Face_Internal internal = face->root.internal;
3555
3556
3557 internal->transform_matrix = composed_matrix;
3558 internal->transform_flags = 0;
3559
3560 if ( ( composed_matrix.xy | composed_matrix.yx ) ||
3561 composed_matrix.xx != 0x10000L ||
3562 composed_matrix.yy != 0x10000L )
3563 internal->transform_flags |= 1;
3564
3565 if ( load_flags & FT_LOAD_NO_SCALE )
3566 {
3567 /* NO_SCALE: outline in integer font units */
3568 internal->transform_delta.x = ( composed_delta.x + 32 ) >> 6;
3569 internal->transform_delta.y = ( composed_delta.y + 32 ) >> 6;
3570 }
3571 else
3572 {
3573 FT_Size size = face->root.size;
3574
3575
3576 if ( size )
3577 {
3578 FT_Fixed x_scale = size->metrics.x_scale;
3579 FT_Fixed y_scale = size->metrics.y_scale;
3580
3581
3582 /* Convert from 26.6 font units to 26.6 device pixels. */
3583 /* 26.6 * 16.16 = 42.22; >> 22 gives 26.6. */
3584#ifdef FT_INT64
3585 internal->transform_delta.x =
3586 (FT_Pos)( ( (FT_Int64)composed_delta.x * x_scale +
3587 0x200000L ) >> 22 );
3588 internal->transform_delta.y =
3589 (FT_Pos)( ( (FT_Int64)composed_delta.y * y_scale +
3590 0x200000L ) >> 22 );
3591#else
3592 internal->transform_delta.x =
3593 FT_MulDiv( composed_delta.x, x_scale, 0x400000L );
3594 internal->transform_delta.y =
3595 FT_MulDiv( composed_delta.y, y_scale, 0x400000L );
3596#endif
3597 }
3598 else
3599 {
3600 internal->transform_delta.x = ( composed_delta.x + 32 ) >> 6;
3601 internal->transform_delta.y = ( composed_delta.y + 32 ) >> 6;
3602 }
3603 }
3604
3605 if ( internal->transform_delta.x | internal->transform_delta.y )
3606 internal->transform_flags |= 2;
3607 }
3608
3609 /* Load component. Strip IGNORE_TRANSFORM so our VARC transform */
3610 /* is applied by `FT_Load_Glyph`'s post-processing. Add NO_HINTING */
3611 /* to avoid auto-fitter reentrancy: the auto-fitter initializes */
3612 /* per-face metrics lazily, and `FT_Set_Var_Blend_Coordinates` */
3613 /* (called below to change axis values) frees that state, causing */
3614 /* use-after-free if the auto-fitter was mid-initialization. */
3615 {
3616 FT_Int32 component_load_flags =
3617 ( load_flags & ~(FT_Int32)FT_LOAD_IGNORE_TRANSFORM ) |
3618 FT_LOAD_NO_HINTING;
3619
3620
3621 error = FT_Load_Glyph( (FT_Face)face, component.gid,
3622 component_load_flags );
3623 }
3624
3625#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
3626 /* restore parent coordinates */
3627 if ( has_axis_override )
3628 {
3629 tt_varc_set_normalized_coords( face, parent_coords, num_coords );
3630
3631 context->current_coords = parent_coords;
3632 context->num_current_coords = num_coords;
3633
3634 if ( new_coords_on_heap )
3635 FT_FREE( new_coords );
3636 }
3637#endif
3638
3639 /* restore parent transform in context */
3640 context->parent_matrix = saved_parent_matrix;
3641 context->parent_delta = saved_parent_delta;
3642 context->has_parent_transform = saved_has_parent;
3643
3644 component_slot = face->root.glyph;
3645
3646 if ( error )
3647 {
3648 FT_TRACE2(( " component glyph %u failed (error 0x%x); skipped\n",
3649 component.gid, error ));
3650 tt_varc_free_component( face, &component );
3651 continue; /* skip failed component, continue with others */
3652 }
3653
3654 /* `FT_Load_Glyph` already applied our composed transform.
3655 *
3656 * For base glyphs: the driver loaded and scaled, then
3657 * `FT_Load_Glyph` applied our matrix + delta.
3658 *
3659 * For VARC sub-components: the recursive handler processed
3660 * them and cleared the face transform before returning, so
3661 * `FT_Load_Glyph`'s post-processing was a no-op.
3662 */
3663 if ( component_slot->outline.n_points > 0 )
3664 {
3665 FT_Outline* src = &component_slot->outline;
3666 FT_Outline* cur = &gloader->current.outline;
3667
3668
3669 /* ensure capacity in gloader with geometric growth */
3670 error = FT_GLYPHLOADER_CHECK_POINTS( gloader,
3671 src->n_points,
3672 src->n_contours );
3673 if ( error )
3674 {
3675 tt_varc_free_component( face, &component );
3676 goto Cleanup;
3677 }
3678
3679 FT_ARRAY_COPY( cur->points, src->points, src->n_points );
3680 FT_ARRAY_COPY( cur->tags, src->tags, src->n_points );
3681 FT_ARRAY_COPY( cur->contours, src->contours, src->n_contours );
3682
3683 cur->n_points = src->n_points;
3684 cur->n_contours = src->n_contours;
3685
3686 FT_GlyphLoader_Add( gloader );
3687
3688 FT_TRACE5(( " appended %d points in %d contours\n",
3689 src->n_points, src->n_contours ));
3690 }
3691
3692 tt_varc_free_component( face, &component );
3693 }
3694
3695 /* Hand the loader's accumulated outline to the slot. */
3696 {
3697 FT_Outline* base = &gloader->base.outline;
3698
3699
3700 if ( base->n_points > 0 )
3701 {
3702 slot->outline = *base;
3703 slot->format = FT_GLYPH_FORMAT_OUTLINE;
3704 }
3705
3706 FT_TRACE4(( "VARC glyph %u: %d points in %d contours\n",
3707 glyph_index, base->n_points, base->n_contours ));
3708 }
3709
3710 error = FT_Err_Ok;
3711
3712 Cleanup:
3713 /* Free the temp glyph slot for this recursion level. */
3714 /* `face->root.glyph` points to the temp slot we allocated above; */
3715 /* `FT_Done_GlyphSlot` removes it and restores the previous head. */
3716 if ( face->root.glyph != glyph_slot )
3717 FT_Done_GlyphSlot( face->root.glyph );
3718
3719 /* pop from recursion stack */
3720 tt_varc_context_pop( context );
3721
3722 /* clean up context if we own it */
3723 if ( context_owner )
3724 {
3725#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
3726 /* Restore font's original coordinates in case we bailed out */
3727 /* mid-component with overridden axis values still active. */
3728 if ( context->font_coords )
3729 tt_varc_set_normalized_coords( face,
3730 context->font_coords,
3731 context->num_font_coords );
3732#endif
3733 if ( context->font_coords )
3734 FT_FREE( context->font_coords );
3735 FT_FREE( context );
3736 face->varc_context = NULL;
3737 }
3738
3739 /* Manage face transform for `FT_Load_Glyph`'s post-processing. */
3740 /* At top level: restore user's transform so it gets applied once. */
3741 /* At recursive levels: clear to identity so the parent's */
3742 /* `FT_Load_Glyph` doesn't apply twice. */
3743 if ( context_owner )
3744 {
3745 face->root.internal->transform_matrix = saved_transform_matrix;
3746 face->root.internal->transform_delta = saved_transform_delta;
3747 face->root.internal->transform_flags = saved_transform_flags;
3748 }
3749 else
3750 {
3751 face->root.internal->transform_matrix.xx = 0x10000L;
3752 face->root.internal->transform_matrix.xy = 0;
3753 face->root.internal->transform_matrix.yx = 0;
3754 face->root.internal->transform_matrix.yy = 0x10000L;
3755 face->root.internal->transform_delta.x = 0;
3756 face->root.internal->transform_delta.y = 0;
3757 face->root.internal->transform_flags = 0;
3758 }
3759
3760#ifdef FT_DEBUG_LEVEL_TRACE
3761 if ( error )
3762 FT_TRACE2(( "tt_face_load_varc_glyph: glyph %u failed"
3763 " (error 0x%x)\n",
3764 glyph_index, error ));
3765#endif
3766
3767 return error;
3768 }
3769
3770
3771#endif /* TT_CONFIG_OPTION_VARC */
3772
3773
3774/* END */