/
ttgxvar.c
4662 lines (3794 loc) · 129 KB
/
ttgxvar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
*
* ttgxvar.c
*
* TrueType GX Font Variation loader
*
* Copyright (C) 2004-2024 by
* David Turner, Robert Wilhelm, Werner Lemberg, and George Williams.
*
* This file is part of the FreeType project, and may only be used,
* modified, and distributed under the terms of the FreeType project
* license, LICENSE.TXT. By continuing to use, modify, or distribute
* this file you indicate that you have read the license and
* understand and accept it fully.
*
*/
/**************************************************************************
*
* Apple documents the `fvar', `gvar', `cvar', and `avar' tables at
*
* https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6[fgca]var.html
*
* The documentation for `gvar' is not intelligible; `cvar' refers you
* to `gvar' and is thus also incomprehensible.
*
* The documentation for `avar' appears correct, but Apple has no fonts
* with an `avar' table, so it is hard to test.
*
* Many thanks to John Jenkins (at Apple) in figuring this out.
*
*
* Apple's `kern' table has some references to tuple indices, but as
* there is no indication where these indices are defined, nor how to
* interpolate the kerning values (different tuples have different
* classes) this issue is ignored.
*
*/
#include <ft2build.h>
#include <freetype/internal/ftdebug.h>
#include FT_CONFIG_CONFIG_H
#include <freetype/internal/ftcalc.h>
#include <freetype/internal/ftstream.h>
#include <freetype/internal/sfnt.h>
#include <freetype/internal/services/svmetric.h>
#include <freetype/tttags.h>
#include <freetype/ttnameid.h>
#include <freetype/ftmm.h>
#include <freetype/ftlist.h>
#include "ttpload.h"
#include "ttgxvar.h"
#include "tterrors.h"
#ifdef TT_CONFIG_OPTION_GX_VAR_SUPPORT
#define FT_Stream_FTell( stream ) \
(FT_ULong)( (stream)->cursor - (stream)->base )
#define FT_Stream_SeekSet( stream, off ) \
(stream)->cursor = \
( (off) < (FT_ULong)( (stream)->limit - (stream)->base ) ) \
? (stream)->base + (off) \
: (stream)->limit
/* some macros we need */
#define FT_fdot14ToFixed( x ) \
( (FT_Fixed)( (FT_ULong)(x) << 2 ) )
#define FT_intToFixed( i ) \
( (FT_Fixed)( (FT_ULong)(i) << 16 ) )
#define FT_fdot6ToFixed( i ) \
( (FT_Fixed)( (FT_ULong)(i) << 10 ) )
#define FT_fixedToInt( x ) \
( (FT_Short)( ( (x) + 0x8000U ) >> 16 ) )
#define FT_fixedToFdot6( x ) \
( (FT_Pos)( ( (x) + 0x200 ) >> 10 ) )
/**************************************************************************
*
* The macro FT_COMPONENT is used in trace mode. It is an implicit
* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
* messages during execution.
*/
#undef FT_COMPONENT
#define FT_COMPONENT ttgxvar
/*************************************************************************/
/*************************************************************************/
/***** *****/
/***** Internal Routines *****/
/***** *****/
/*************************************************************************/
/*************************************************************************/
/**************************************************************************
*
* The macro ALL_POINTS is used in `ft_var_readpackedpoints'. It
* indicates that there is a delta for every point without needing to
* enumerate all of them.
*/
/* ensure that value `0' has the same width as a pointer */
#define ALL_POINTS (FT_UShort*)~(FT_PtrDist)0
#define GX_PT_POINTS_ARE_WORDS 0x80U
#define GX_PT_POINT_RUN_COUNT_MASK 0x7FU
/**************************************************************************
*
* @Function:
* ft_var_readpackedpoints
*
* @Description:
* Read a set of points to which the following deltas will apply.
* Points are packed with a run length encoding.
*
* @Input:
* stream ::
* The data stream.
*
* size ::
* The size of the table holding the data.
*
* @Output:
* point_cnt ::
* The number of points read. A zero value means that
* all points in the glyph will be affected, without
* enumerating them individually.
*
* @Return:
* An array of FT_UShort containing the affected points or the
* special value ALL_POINTS.
*/
static FT_UShort*
ft_var_readpackedpoints( FT_Stream stream,
FT_ULong size,
FT_UInt *point_cnt )
{
FT_UShort *points = NULL;
FT_UInt n;
FT_UInt runcnt;
FT_UInt i, j;
FT_UShort first;
FT_Memory memory = stream->memory;
FT_Error error;
*point_cnt = 0;
n = FT_GET_BYTE();
if ( n == 0 )
return ALL_POINTS;
if ( n & GX_PT_POINTS_ARE_WORDS )
{
n &= GX_PT_POINT_RUN_COUNT_MASK;
n <<= 8;
n |= FT_GET_BYTE();
}
if ( n > size )
{
FT_TRACE1(( "ft_var_readpackedpoints: number of points too large\n" ));
return NULL;
}
/* in the nested loops below we increase `i' twice; */
/* it is faster to simply allocate one more slot */
/* than to add another test within the loop */
if ( FT_QNEW_ARRAY( points, n + 1 ) )
return NULL;
*point_cnt = n;
first = 0;
i = 0;
while ( i < n )
{
runcnt = FT_GET_BYTE();
if ( runcnt & GX_PT_POINTS_ARE_WORDS )
{
runcnt &= GX_PT_POINT_RUN_COUNT_MASK;
first += FT_GET_USHORT();
points[i++] = first;
/* first point not included in run count */
for ( j = 0; j < runcnt; j++ )
{
first += FT_GET_USHORT();
points[i++] = first;
if ( i >= n )
break;
}
}
else
{
first += FT_GET_BYTE();
points[i++] = first;
for ( j = 0; j < runcnt; j++ )
{
first += FT_GET_BYTE();
points[i++] = first;
if ( i >= n )
break;
}
}
}
return points;
}
#define GX_DT_DELTAS_ARE_ZERO 0x80U
#define GX_DT_DELTAS_ARE_WORDS 0x40U
#define GX_DT_DELTA_RUN_COUNT_MASK 0x3FU
/**************************************************************************
*
* @Function:
* ft_var_readpackeddeltas
*
* @Description:
* Read a set of deltas. These are packed slightly differently than
* points. In particular there is no overall count.
*
* @Input:
* stream ::
* The data stream.
*
* size ::
* The size of the table holding the data.
*
* delta_cnt ::
* The number of deltas to be read.
*
* @Return:
* An array of FT_Fixed containing the deltas for the affected
* points. (This only gets the deltas for one dimension. It will
* generally be called twice, once for x, once for y. When used in
* cvt table, it will only be called once.)
*
* We use FT_Fixed to avoid accumulation errors while summing up all
* deltas (the rounding to integer values happens as the very last
* step).
*/
static FT_Fixed*
ft_var_readpackeddeltas( FT_Stream stream,
FT_ULong size,
FT_UInt delta_cnt )
{
FT_Fixed *deltas = NULL;
FT_UInt runcnt, cnt;
FT_UInt i, j;
FT_UInt bytes_used;
FT_Memory memory = stream->memory;
FT_Error error;
if ( FT_QNEW_ARRAY( deltas, delta_cnt ) )
return NULL;
i = 0;
bytes_used = 0;
while ( i < delta_cnt && bytes_used < size )
{
runcnt = FT_GET_BYTE();
cnt = runcnt & GX_DT_DELTA_RUN_COUNT_MASK;
bytes_used++;
if ( runcnt & GX_DT_DELTAS_ARE_ZERO )
{
/* `cnt` + 1 zeroes get added */
for ( j = 0; j <= cnt && i < delta_cnt; j++ )
deltas[i++] = 0;
}
else if ( runcnt & GX_DT_DELTAS_ARE_WORDS )
{
/* `cnt` + 1 shorts from the stack */
bytes_used += 2 * ( cnt + 1 );
if ( bytes_used > size )
{
FT_TRACE1(( "ft_var_readpackeddeltas:"
" number of short deltas too large\n" ));
goto Fail;
}
for ( j = 0; j <= cnt && i < delta_cnt; j++ )
deltas[i++] = FT_intToFixed( FT_GET_SHORT() );
}
else
{
/* `cnt` + 1 signed bytes from the stack */
bytes_used += cnt + 1;
if ( bytes_used > size )
{
FT_TRACE1(( "ft_var_readpackeddeltas:"
" number of byte deltas too large\n" ));
goto Fail;
}
for ( j = 0; j <= cnt && i < delta_cnt; j++ )
deltas[i++] = FT_intToFixed( FT_GET_CHAR() );
}
if ( j <= cnt )
{
FT_TRACE1(( "ft_var_readpackeddeltas:"
" number of deltas too large\n" ));
goto Fail;
}
}
if ( i < delta_cnt )
{
FT_TRACE1(( "ft_var_readpackeddeltas: not enough deltas\n" ));
goto Fail;
}
return deltas;
Fail:
FT_FREE( deltas );
return NULL;
}
/**************************************************************************
*
* @Function:
* ft_var_load_avar
*
* @Description:
* Parse the `avar' table if present. It need not be, so we return
* nothing.
*
* @InOut:
* face ::
* The font face.
*/
static void
ft_var_load_avar( TT_Face face )
{
FT_Error error;
FT_Stream stream = FT_FACE_STREAM( face );
FT_Memory memory = stream->memory;
FT_Int i, j;
GX_Blend blend = face->blend;
GX_AVarSegment segment;
GX_AVarTable table;
FT_Long version;
FT_Long axisCount;
FT_ULong table_len;
#ifndef TT_CONFIG_OPTION_NO_BORING_EXPANSION
FT_ULong table_offset;
FT_ULong store_offset;
FT_ULong axisMap_offset;
#endif
FT_TRACE2(( "AVAR " ));
blend->avar_loaded = TRUE;
error = face->goto_table( face, TTAG_avar, stream, &table_len );
if ( error )
{
FT_TRACE2(( "is missing\n" ));
return;
}
#ifndef TT_CONFIG_OPTION_NO_BORING_EXPANSION
table_offset = FT_STREAM_POS();
#endif
if ( FT_FRAME_ENTER( table_len ) )
return;
version = FT_GET_LONG();
axisCount = FT_GET_LONG();
if ( version != 0x00010000L
#ifndef TT_CONFIG_OPTION_NO_BORING_EXPANSION
&& version != 0x00020000L
#endif
)
{
FT_TRACE2(( "bad table version\n" ));
goto Exit;
}
FT_TRACE2(( "loaded\n" ));
if ( axisCount != (FT_Long)blend->mmvar->num_axis )
{
FT_TRACE2(( "ft_var_load_avar:"
" number of axes in `avar' and `fvar'\n" ));
FT_TRACE2(( " table are different\n" ));
goto Exit;
}
if ( FT_NEW( blend->avar_table ) )
goto Exit;
table = blend->avar_table;
if ( FT_QNEW_ARRAY( table->avar_segment, axisCount ) )
goto Exit;
segment = &table->avar_segment[0];
for ( i = 0; i < axisCount; i++, segment++ )
{
FT_TRACE5(( " axis %d:\n", i ));
segment->pairCount = FT_GET_USHORT();
if ( (FT_ULong)segment->pairCount * 4 > table_len ||
FT_QNEW_ARRAY( segment->correspondence, segment->pairCount ) )
{
/* Failure. Free everything we have done so far. We must do */
/* it right now since loading the `avar' table is optional. */
for ( j = i - 1; j >= 0; j-- )
FT_FREE( table->avar_segment[j].correspondence );
FT_FREE( table->avar_segment );
goto Exit;
}
for ( j = 0; j < segment->pairCount; j++ )
{
segment->correspondence[j].fromCoord =
FT_fdot14ToFixed( FT_GET_SHORT() );
segment->correspondence[j].toCoord =
FT_fdot14ToFixed( FT_GET_SHORT() );
FT_TRACE5(( " mapping %.5f to %.5f\n",
(double)segment->correspondence[j].fromCoord / 65536,
(double)segment->correspondence[j].toCoord / 65536 ));
}
FT_TRACE5(( "\n" ));
}
#ifndef TT_CONFIG_OPTION_NO_BORING_EXPANSION
if ( version < 0x00020000L )
goto Exit;
axisMap_offset = FT_GET_ULONG();
store_offset = FT_GET_ULONG();
if ( store_offset )
{
error = tt_var_load_item_variation_store(
FT_FACE( face ),
table_offset + store_offset,
&table->itemStore );
if ( error )
goto Exit;
}
if ( axisMap_offset )
{
error = tt_var_load_delta_set_index_mapping(
FT_FACE( face ),
table_offset + axisMap_offset,
&table->axisMap,
&table->itemStore,
table_len );
if ( error )
goto Exit;
}
#endif
Exit:
FT_FRAME_EXIT();
}
FT_LOCAL_DEF( FT_Error )
tt_var_load_item_variation_store( FT_Face face, /* TT_Face */
FT_ULong offset,
GX_ItemVarStore itemStore )
{
TT_Face ttface = (TT_Face)face;
FT_Stream stream = FT_FACE_STREAM( face );
FT_Memory memory = stream->memory;
FT_Error error;
FT_UShort format;
FT_ULong region_offset;
FT_UInt data_count;
FT_UShort axis_count;
FT_UInt region_count;
FT_UInt i, j;
FT_Bool long_words;
GX_Blend blend = ttface->blend;
FT_ULong* dataOffsetArray = NULL;
if ( FT_STREAM_SEEK( offset ) ||
FT_READ_USHORT( format ) )
goto Exit;
if ( format != 1 )
{
FT_TRACE2(( "tt_var_load_item_variation_store: bad store format %d\n",
format ));
error = FT_THROW( Invalid_Table );
goto Exit;
}
/* read top level fields */
if ( FT_READ_ULONG( region_offset ) ||
FT_READ_USHORT( data_count ) )
goto Exit;
/* we need at least one entry in `itemStore->varData' */
if ( !data_count )
{
FT_TRACE2(( "tt_var_load_item_variation_store: missing varData\n" ));
error = FT_THROW( Invalid_Table );
goto Exit;
}
/* make temporary copy of item variation data offsets; */
/* we will parse region list first, then come back */
if ( FT_QNEW_ARRAY( dataOffsetArray, data_count ) )
goto Exit;
for ( i = 0; i < data_count; i++ )
{
if ( FT_READ_ULONG( dataOffsetArray[i] ) )
goto Exit;
}
/* parse array of region records (region list) */
if ( FT_STREAM_SEEK( offset + region_offset ) )
goto Exit;
if ( FT_READ_USHORT( axis_count ) ||
FT_READ_USHORT( region_count ) )
goto Exit;
if ( axis_count != (FT_Long)blend->mmvar->num_axis )
{
FT_TRACE2(( "tt_var_load_item_variation_store:"
" number of axes in item variation store\n" ));
FT_TRACE2(( " "
" and `fvar' table are different\n" ));
error = FT_THROW( Invalid_Table );
goto Exit;
}
itemStore->axisCount = axis_count;
/* new constraint in OpenType 1.8.4 */
if ( region_count >= 32768U )
{
FT_TRACE2(( "tt_var_load_item_variation_store:"
" too many variation region tables\n" ));
error = FT_THROW( Invalid_Table );
goto Exit;
}
if ( FT_NEW_ARRAY( itemStore->varRegionList, region_count ) )
goto Exit;
itemStore->regionCount = region_count;
for ( i = 0; i < itemStore->regionCount; i++ )
{
GX_AxisCoords axisCoords;
if ( FT_NEW_ARRAY( itemStore->varRegionList[i].axisList, axis_count ) )
goto Exit;
axisCoords = itemStore->varRegionList[i].axisList;
for ( j = 0; j < itemStore->axisCount; j++ )
{
FT_Short start, peak, end;
if ( FT_READ_SHORT( start ) ||
FT_READ_SHORT( peak ) ||
FT_READ_SHORT( end ) )
goto Exit;
axisCoords[j].startCoord = FT_fdot14ToFixed( start );
axisCoords[j].peakCoord = FT_fdot14ToFixed( peak );
axisCoords[j].endCoord = FT_fdot14ToFixed( end );
}
}
/* end of region list parse */
/* use dataOffsetArray now to parse varData items */
if ( FT_NEW_ARRAY( itemStore->varData, data_count ) )
goto Exit;
itemStore->dataCount = data_count;
for ( i = 0; i < data_count; i++ )
{
GX_ItemVarData varData = &itemStore->varData[i];
FT_UInt item_count;
FT_UShort word_delta_count;
FT_UInt region_idx_count;
FT_UInt per_region_size;
if ( FT_STREAM_SEEK( offset + dataOffsetArray[i] ) )
goto Exit;
if ( FT_READ_USHORT( item_count ) ||
FT_READ_USHORT( word_delta_count ) ||
FT_READ_USHORT( region_idx_count ) )
goto Exit;
long_words = !!( word_delta_count & 0x8000 );
word_delta_count &= 0x7FFF;
/* check some data consistency */
if ( word_delta_count > region_idx_count )
{
FT_TRACE2(( "bad short count %d or region count %d\n",
word_delta_count,
region_idx_count ));
error = FT_THROW( Invalid_Table );
goto Exit;
}
if ( region_idx_count > itemStore->regionCount )
{
FT_TRACE2(( "inconsistent regionCount %d in varData[%d]\n",
region_idx_count,
i ));
error = FT_THROW( Invalid_Table );
goto Exit;
}
/* parse region indices */
if ( FT_NEW_ARRAY( varData->regionIndices, region_idx_count ) )
goto Exit;
varData->regionIdxCount = region_idx_count;
varData->wordDeltaCount = word_delta_count;
varData->longWords = long_words;
for ( j = 0; j < varData->regionIdxCount; j++ )
{
if ( FT_READ_USHORT( varData->regionIndices[j] ) )
goto Exit;
if ( varData->regionIndices[j] >= itemStore->regionCount )
{
FT_TRACE2(( "bad region index %d\n",
varData->regionIndices[j] ));
error = FT_THROW( Invalid_Table );
goto Exit;
}
}
per_region_size = word_delta_count + region_idx_count;
if ( long_words )
per_region_size *= 2;
if ( FT_NEW_ARRAY( varData->deltaSet, per_region_size * item_count ) )
goto Exit;
if ( FT_Stream_Read( stream,
varData->deltaSet,
per_region_size * item_count ) )
{
FT_TRACE2(( "deltaSet read failed." ));
error = FT_THROW( Invalid_Table );
goto Exit;
}
varData->itemCount = item_count;
}
Exit:
FT_FREE( dataOffsetArray );
return error;
}
FT_LOCAL_DEF( FT_Error )
tt_var_load_delta_set_index_mapping( FT_Face face, /* TT_Face */
FT_ULong offset,
GX_DeltaSetIdxMap map,
GX_ItemVarStore itemStore,
FT_ULong table_len )
{
FT_Stream stream = FT_FACE_STREAM( face );
FT_Memory memory = stream->memory;
FT_Error error;
FT_Byte format;
FT_Byte entryFormat;
FT_UInt entrySize;
FT_UInt innerBitCount;
FT_UInt innerIndexMask;
FT_ULong i;
FT_UInt j;
if ( FT_STREAM_SEEK( offset ) ||
FT_READ_BYTE( format ) ||
FT_READ_BYTE( entryFormat ) )
goto Exit;
if ( format == 0 )
{
if ( FT_READ_USHORT( map->mapCount ) )
goto Exit;
}
else if ( format == 1 ) /* new in OpenType 1.9 */
{
if ( FT_READ_ULONG( map->mapCount ) )
goto Exit;
}
else
{
FT_TRACE2(( "bad map format %d\n", format ));
error = FT_THROW( Invalid_Table );
goto Exit;
}
if ( entryFormat & 0xC0 )
{
FT_TRACE2(( "bad entry format %d\n", format ));
error = FT_THROW( Invalid_Table );
goto Exit;
}
/* bytes per entry: 1, 2, 3, or 4 */
entrySize = ( ( entryFormat & 0x30 ) >> 4 ) + 1;
innerBitCount = ( entryFormat & 0x0F ) + 1;
innerIndexMask = ( 1 << innerBitCount ) - 1;
/* rough sanity check */
if ( map->mapCount * entrySize > table_len )
{
FT_TRACE1(( "tt_var_load_delta_set_index_mapping:"
" invalid number of delta-set index mappings\n" ));
error = FT_THROW( Invalid_Table );
goto Exit;
}
if ( FT_NEW_ARRAY( map->innerIndex, map->mapCount ) )
goto Exit;
if ( FT_NEW_ARRAY( map->outerIndex, map->mapCount ) )
goto Exit;
for ( i = 0; i < map->mapCount; i++ )
{
FT_UInt mapData = 0;
FT_UInt outerIndex, innerIndex;
/* read map data one unsigned byte at a time, big endian */
for ( j = 0; j < entrySize; j++ )
{
FT_Byte data;
if ( FT_READ_BYTE( data ) )
goto Exit;
mapData = ( mapData << 8 ) | data;
}
/* new in OpenType 1.8.4 */
if ( mapData == 0xFFFFFFFFUL )
{
/* no variation data for this item */
map->outerIndex[i] = 0xFFFFU;
map->innerIndex[i] = 0xFFFFU;
continue;
}
outerIndex = mapData >> innerBitCount;
if ( outerIndex >= itemStore->dataCount )
{
FT_TRACE2(( "outerIndex[%ld] == %d out of range\n",
i,
outerIndex ));
error = FT_THROW( Invalid_Table );
goto Exit;
}
map->outerIndex[i] = outerIndex;
innerIndex = mapData & innerIndexMask;
if ( innerIndex >= itemStore->varData[outerIndex].itemCount )
{
FT_TRACE2(( "innerIndex[%ld] == %d out of range\n",
i,
innerIndex ));
error = FT_THROW( Invalid_Table );
goto Exit;
}
map->innerIndex[i] = innerIndex;
}
Exit:
return error;
}
/**************************************************************************
*
* @Function:
* ft_var_load_hvvar
*
* @Description:
* If `vertical' is zero, parse the `HVAR' table and set
* `blend->hvar_loaded' to TRUE. On success, `blend->hvar_checked'
* is set to TRUE.
*
* If `vertical' is not zero, parse the `VVAR' table and set
* `blend->vvar_loaded' to TRUE. On success, `blend->vvar_checked'
* is set to TRUE.
*
* Some memory may remain allocated on error; it is always freed in
* `tt_done_blend', however.
*
* @InOut:
* face ::
* The font face.
*
* @Return:
* FreeType error code. 0 means success.
*/
static FT_Error
ft_var_load_hvvar( TT_Face face,
FT_Bool vertical )
{
FT_Stream stream = FT_FACE_STREAM( face );
FT_Memory memory = stream->memory;
GX_Blend blend = face->blend;
GX_HVVarTable table;
FT_Error error;
FT_UShort majorVersion;
FT_ULong table_len;
FT_ULong table_offset;
FT_ULong store_offset;
FT_ULong widthMap_offset;
if ( vertical )
{
blend->vvar_loaded = TRUE;
FT_TRACE2(( "VVAR " ));
error = face->goto_table( face, TTAG_VVAR, stream, &table_len );
}
else
{
blend->hvar_loaded = TRUE;
FT_TRACE2(( "HVAR " ));
error = face->goto_table( face, TTAG_HVAR, stream, &table_len );
}
if ( error )
{
FT_TRACE2(( "is missing\n" ));
goto Exit;
}
table_offset = FT_STREAM_POS();
/* skip minor version */
if ( FT_READ_USHORT( majorVersion ) ||
FT_STREAM_SKIP( 2 ) )
goto Exit;
if ( majorVersion != 1 )
{
FT_TRACE2(( "bad table version %d\n", majorVersion ));
error = FT_THROW( Invalid_Table );
goto Exit;
}
if ( FT_READ_ULONG( store_offset ) ||
FT_READ_ULONG( widthMap_offset ) )
goto Exit;
if ( vertical )
{
if ( FT_NEW( blend->vvar_table ) )
goto Exit;
table = blend->vvar_table;
}
else
{
if ( FT_NEW( blend->hvar_table ) )
goto Exit;
table = blend->hvar_table;
}
error = tt_var_load_item_variation_store(
FT_FACE( face ),
table_offset + store_offset,
&table->itemStore );
if ( error )
goto Exit;
if ( widthMap_offset )
{
error = tt_var_load_delta_set_index_mapping(
FT_FACE( face ),
table_offset + widthMap_offset,
&table->widthMap,
&table->itemStore,
table_len );
if ( error )
goto Exit;
}
FT_TRACE2(( "loaded\n" ));
error = FT_Err_Ok;
Exit:
if ( !error )
{
if ( vertical )
{
blend->vvar_checked = TRUE;
/* FreeType doesn't provide functions to quickly retrieve */
/* TSB, BSB, or VORG values; we thus don't have to implement */
/* support for those three item variation stores. */
face->variation_support |= TT_FACE_FLAG_VAR_VADVANCE;
}
else
{
blend->hvar_checked = TRUE;
/* FreeType doesn't provide functions to quickly retrieve */
/* LSB or RSB values; we thus don't have to implement */
/* support for those two item variation stores. */
face->variation_support |= TT_FACE_FLAG_VAR_HADVANCE;
}
}
return error;
}
FT_LOCAL_DEF( FT_ItemVarDelta )
tt_var_get_item_delta( FT_Face face, /* TT_Face */
GX_ItemVarStore itemStore,
FT_UInt outerIndex,
FT_UInt innerIndex )
{
TT_Face ttface = (TT_Face)face;
FT_Stream stream = FT_FACE_STREAM( face );
FT_Memory memory = stream->memory;
FT_Error error = FT_Err_Ok;
GX_ItemVarData varData;
FT_ItemVarDelta* deltaSet = NULL;
FT_ItemVarDelta deltaSetStack[16];
FT_Fixed* scalars = NULL;
FT_Fixed scalarsStack[16];