-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalType.Tables.OpenType.Common.Variation.pas
More file actions
769 lines (638 loc) · 25 KB
/
PascalType.Tables.OpenType.Common.Variation.pas
File metadata and controls
769 lines (638 loc) · 25 KB
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
unit PascalType.Tables.OpenType.Common.Variation;
////////////////////////////////////////////////////////////////////////////////
// //
// Version: MPL 1.1 or LGPL 2.1 with linking exception //
// //
// The contents of this file are subject to the Mozilla Public License //
// Version 1.1 (the "License"); you may not use this file except in //
// compliance with the License. You may obtain a copy of the License at //
// http://www.mozilla.org/MPL/ //
// //
// Software distributed under the License is distributed on an "AS IS" //
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the //
// License for the specific language governing rights and limitations under //
// the License. //
// //
// Alternatively, the contents of this file may be used under the terms of //
// the Free Pascal modified version of the GNU Lesser General Public //
// License Version 2.1 (the "FPC modified LGPL License"), in which case the //
// provisions of this license are applicable instead of those above. //
// Please see the file LICENSE.txt for additional information concerning //
// this license. //
// //
// The code is part of the PascalType Project //
// //
// The initial developer of this code is Anders Melander //
// //
// Portions created by Anders Melander are Copyright (C) 2025-2026 //
// by Anders Melander. All Rights Reserved. //
// //
////////////////////////////////////////////////////////////////////////////////
interface
{$I PT_Compiler.inc}
uses
Classes,
SysUtils,
Generics.Collections,
PascalType.Types,
PascalType.Classes,
PascalType.Tables,
PascalType.Tables.OpenType.Common.ValueRecord;
type
TRegionAxisCoordinates = record
StartCoord: TShortFrac;
PeakCoord: TShortFrac;
EndCoord: TShortFrac;
end;
TVariationRegion = TArray<TRegionAxisCoordinates>;
TVariationRegionList = class(TCustomPascalTypeTable)
private
FRegions: TArray<TVariationRegion>;
function GetRegionCount: Integer;
function GetAxisCount: Word;
public
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
function CalculateScalar(RegionIndex: Integer; const NormalizedCoords: TArray<TFixedPoint>): Single;
property AxisCount: Word read GetAxisCount;
property RegionCount: Integer read GetRegionCount;
end;
TItemVariationData = class(TCustomPascalTypeTable)
private
FRegionIndices: TArray<Word>;
FDeltas: TArray<TArray<SmallInt>>;
function GetRegionIndexCount: Integer;
function GetRegionIndex(Index: Integer): Word;
function GetItemCount: Word;
public
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
function GetDelta(ItemIndex: Integer; RegionIndex: Integer; RegionList: TVariationRegionList; const NormalizedCoords: TArray<TFixedPoint>): Single;
function GetItemDelta(ItemIndex: Integer; RegionList: TVariationRegionList; const NormalizedCoords: TArray<TFixedPoint>): Single;
property ItemCount: Word read GetItemCount;
property RegionIndexCount: Integer read GetRegionIndexCount;
property RegionIndices[Index: Integer]: Word read GetRegionIndex;
end;
TItemVariationStore = class(TCustomPascalTypeTable)
private
FFormat: Word;
FRegionList: TVariationRegionList;
FDataSets: TObjectList<TItemVariationData>;
function GetDataSetCount: Integer;
function GetDataSet(Index: Integer): TItemVariationData;
public
constructor Create(AParent: TCustomPascalTypeTable); override;
destructor Destroy; override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
function GetDelta(OuterIndex, InnerIndex: Word; const NormalizedCoords: TArray<TFixedPoint>): Single;
property RegionList: TVariationRegionList read FRegionList;
property DataSetCount: Integer read GetDataSetCount;
property DataSets[Index: Integer]: TItemVariationData read GetDataSet;
end;
TTupleVariationHeader = class(TCustomPascalTypeTable)
private
FVariationDataSize: Word;
FTupleIndex: Word;
FPeakTuple: TArray<TShortFrac>;
FIntermediateStartTuple: TArray<TShortFrac>;
FIntermediateEndTuple: TArray<TShortFrac>;
public
procedure LoadFromStream(Stream: TStream; AxisCount: Integer; SharedTuples: TArray<TArray<TShortFrac>>); reintroduce; virtual;
function CalculateScalar(const NormalizedCoords: TArray<TFixedPoint>): Single;
property VariationDataSize: Word read FVariationDataSize;
property TupleIndex: Word read FTupleIndex;
end;
TDeltaSetIndexMap = class(TCustomPascalTypeTable)
private
FFormat: Byte;
FEntryFormat: Word;
FMapData: TArray<Cardinal>;
public
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
function GetIndex(GlyphIndex: Word; out Outer, Inner: Word): Boolean;
end;
procedure ResolveVariationIndices(Stream: TStream; BasePos: Int64; var ValueRecord: TOpenTypeValueRecord);
function DecodePackedPoints(Stream: TStream; NumPointsInGlyph: Integer): TArray<Integer>;
function DecodePackedDeltas(Stream: TStream; NumPoints: Integer): TArray<Integer>;
(*
** https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#tuplevariationheader
**
** tupleIndex format
*)
const
EMBEDDED_PEAK_TUPLE = $8000;// Flag indicating that this tuple variation
// header includes an embedded peak tuple
// record, immediately after the tupleIndex
// field. If set, the low 12 bits of the
// tupleIndex value are ignored.
// Note that this must always be set within the
// 'cvar' table.
INTERMEDIATE_REGION = $4000;// Flag indicating that this tuple variation
// table applies to an intermediate region
// within the variation space. If set, the
// header includes the two intermediate-region,
// start and end tuple records, immediately
// after the peak tuple record (if present).
PRIVATE_POINT_NUMBERS = $2000;// Flag indicating that the serialized data for
// this tuple variation table includes packed
// "point" number data. If set, this tuple
// variation table uses that number data; if
// clear, this tuple variation table uses
// shared number data found at the start of the
// serialized data for this glyph variation
// data or 'cvar' table.
TUPLE_INDEX_MASK = $0FFF;// Mask for the low 12 bits to give the shared
// tuple records index.
(*
** https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#tuple-variation-store-header
**
** tupleVariationCount format
*)
const
SHARED_POINT_NUMBERS = $8000;// Flag indicating that some or all tuple
// variation tables reference a shared set of
// "point" numbers. These shared numbers are
// represented as packed point number data at
// the start of the serialized data.
COUNT_MASK = $0FFF;// Mask for the low bits to give the number of
// tuple variation tables.
(*
** https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#associating-target-items-to-variation-data
**
** entryFormat field masks
*)
const
INNER_INDEX_BIT_COUNT_MASK = $0F; // Mask for the low 4 bits, which give
// the count of bits minus one that are
// used in each entry for the inner-
// level index.
MAP_ENTRY_SIZE_MASK = $30; // Mask for bits that indicate the size
// in bytes minus one of each entry.
(*
** https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#packed-point-numbers
**
** Point number data run control byte
*)
const
POINTS_ARE_WORDS = $80; // Flag indicating the data type used for point
// numbers in this run. If set, the point numbers
// are stored as unsigned 16-bit values (uint16);
// if clear, the point numbers are stored as
// unsigned bytes (uint8).
POINT_RUN_COUNT_MASK = $7F; // Mask for the low 7 bits of the control byte to
// give the number of point number elements, minus 1.
(*
** https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#packed-point-numbers
**
** Packaed delta control byte
*)
const
DELTAS_ARE_ZERO = $80; // Flag indicating that this run contains no
// data (no explicit delta values are stored),
// and that all of the deltas for this run are
// zero.
DELTAS_ARE_WORDS = $40; // Flag indicating the data type for delta
// values in the run. If set, the run contains
// 16-bit signed deltas (int16); if clear, the
// run contains 8-bit signed deltas (int8).
DELTA_RUN_COUNT_MASK = $3F; // Mask for the low 6 bits to provide the
// number of delta values in the run, minus one.
implementation
uses
PascalType.ResourceStrings;
{ TVariationRegionList }
function TVariationRegionList.GetAxisCount: Word;
begin
if (Length(FRegions) > 0) then
Result := Length(FRegions[0])
else
Result := 0;
end;
function TVariationRegionList.GetRegionCount: Integer;
begin
Result := Length(FRegions);
end;
procedure TVariationRegionList.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
var AxisCount := BigEndianValue.ReadWord(Stream);
var RegionCount := BigEndianValue.ReadWord(Stream);
SetLength(FRegions, RegionCount);
for var i := 0 to RegionCount - 1 do
begin
SetLength(FRegions[i], AxisCount);
for var j := 0 to AxisCount - 1 do
begin
FRegions[i][j].StartCoord := BigEndianValue.ReadSmallInt(Stream);
FRegions[i][j].PeakCoord := BigEndianValue.ReadSmallInt(Stream);
FRegions[i][j].EndCoord := BigEndianValue.ReadSmallInt(Stream);
end;
end;
end;
procedure TVariationRegionList.SaveToStream(Stream: TStream);
begin
raise EPascalTypeNotImplemented.Create(RCStrNotImplemented);
end;
function TVariationRegionList.CalculateScalar(RegionIndex: Integer; const NormalizedCoords: TArray<TFixedPoint>): Single;
begin
if (RegionIndex < 0) or (RegionIndex > High(FRegions)) then
Exit(0);
var Region := FRegions[RegionIndex];
Result := 1.0;
for var i := 0 to High(Region) do
begin
var V: Single;
if i >= Length(NormalizedCoords) then
V := 0
else
V := NormalizedCoords[i].AsSingle;
var AxisScalar: Single;
if (Region[i].PeakCoord = 0) then
AxisScalar := 1.0
else
if (V = Region[i].PeakCoord / 16384.0) then
AxisScalar := 1.0
else
if (V <= Region[i].StartCoord / 16384.0) or (V >= Region[i].EndCoord / 16384.0) then
AxisScalar := 0.0
else
if (V < Region[i].PeakCoord / 16384.0) then
AxisScalar := (V - Region[i].StartCoord / 16384.0) / (Region[i].PeakCoord / 16384.0 - Region[i].StartCoord / 16384.0)
else
AxisScalar := (Region[i].EndCoord / 16384.0 - V) / (Region[i].EndCoord / 16384.0 - Region[i].PeakCoord / 16384.0);
Result := Result * AxisScalar;
if Result = 0 then
break;
end;
end;
{ TItemVariationData }
procedure TItemVariationData.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
var ItemCount := BigEndianValue.ReadWord(Stream);
var ShortDeltaCount := BigEndianValue.ReadWord(Stream);
var RegionIndexCount := BigEndianValue.ReadWord(Stream);
SetLength(FRegionIndices, RegionIndexCount);
for var i := 0 to High(FRegionIndices) do
FRegionIndices[i] := BigEndianValue.ReadWord(Stream);
SetLength(FDeltas, ItemCount);
for var i := 0 to High(FDeltas) do
begin
SetLength(FDeltas[i], RegionIndexCount);
for var j := 0 to ShortDeltaCount - 1 do
FDeltas[i][j] := BigEndianValue.ReadSmallInt(Stream);
for var j := ShortDeltaCount to RegionIndexCount - 1 do
FDeltas[i][j] := ShortInt(BigEndianValue.ReadByte(Stream));
end;
end;
procedure TItemVariationData.SaveToStream(Stream: TStream);
begin
raise EPascalTypeNotImplemented.Create(RCStrNotImplemented);
end;
function TItemVariationData.GetRegionIndexCount: Integer;
begin
Result := Length(FRegionIndices);
end;
function TItemVariationData.GetRegionIndex(Index: Integer): Word;
begin
Result := FRegionIndices[Index];
end;
function TItemVariationData.GetDelta(ItemIndex: Integer; RegionIndex: Integer; RegionList: TVariationRegionList; const NormalizedCoords: TArray<TFixedPoint>): Single;
begin
if (ItemIndex < 0) or (ItemIndex > High(FDeltas)) or (RegionIndex < 0) or (RegionIndex >= Length(FRegionIndices)) then
Exit(0);
Result := FDeltas[ItemIndex][RegionIndex] * RegionList.CalculateScalar(FRegionIndices[RegionIndex], NormalizedCoords);
end;
function TItemVariationData.GetItemCount: Word;
begin
Result := Length(FDeltas);
end;
function TItemVariationData.GetItemDelta(ItemIndex: Integer; RegionList: TVariationRegionList; const NormalizedCoords: TArray<TFixedPoint>): Single;
begin
if (Length(FDeltas) = 0) or (ItemIndex > High(FDeltas)) then
Exit(0);
Result := 0;
for var i := 0 to High(FRegionIndices) do
Result := Result + FDeltas[ItemIndex][i] * RegionList.CalculateScalar(FRegionIndices[i], NormalizedCoords);
end;
{ TItemVariationStore }
constructor TItemVariationStore.Create(AParent: TCustomPascalTypeTable);
begin
inherited;
FRegionList := TVariationRegionList.Create(Self);
FDataSets := TObjectList<TItemVariationData>.Create(True);
end;
destructor TItemVariationStore.Destroy;
begin
FDataSets.Free;
FRegionList.Free;
inherited;
end;
procedure TItemVariationStore.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
var StartPos := Stream.Position;
FFormat := BigEndianValue.ReadWord(Stream);
if FFormat <> 1 then
raise EPascalTypeError.Create(RCStrUnsupportedVersion);
var RegionListOffset := BigEndianValue.ReadCardinal(Stream);
var ItemVariationDataCount := BigEndianValue.ReadWord(Stream);
var Offsets: TArray<Cardinal>;
SetLength(Offsets, ItemVariationDataCount);
for var i := 0 to ItemVariationDataCount - 1 do
Offsets[i] := BigEndianValue.ReadCardinal(Stream);
Stream.Position := StartPos + RegionListOffset;
FRegionList.LoadFromStream(Stream);
for var i := 0 to ItemVariationDataCount - 1 do
begin
Stream.Position := StartPos + Offsets[i];
var DataSet := TItemVariationData.Create(Self);
FDataSets.Add(DataSet);
DataSet.LoadFromStream(Stream);
end;
end;
procedure TItemVariationStore.SaveToStream(Stream: TStream);
begin
raise EPascalTypeNotImplemented.Create(RCStrNotImplemented);
end;
function TItemVariationStore.GetDataSetCount: Integer;
begin
Result := FDataSets.Count;
end;
function TItemVariationStore.GetDataSet(Index: Integer): TItemVariationData;
begin
Result := FDataSets[Index];
end;
function TItemVariationStore.GetDelta(OuterIndex, InnerIndex: Word; const NormalizedCoords: TArray<TFixedPoint>): Single;
begin
if OuterIndex >= FDataSets.Count then
Exit(0);
Result := FDataSets[OuterIndex].GetItemDelta(InnerIndex, FRegionList, NormalizedCoords);
end;
{ TTupleVariationHeader }
procedure TTupleVariationHeader.LoadFromStream(Stream: TStream; AxisCount: Integer; SharedTuples: TArray<TArray<TShortFrac>>);
begin
FVariationDataSize := BigEndianValue.ReadWord(Stream);
FTupleIndex := BigEndianValue.ReadWord(Stream);
// Embedded peak tuple
if (FTupleIndex and EMBEDDED_PEAK_TUPLE <> 0) then
begin
SetLength(FPeakTuple, AxisCount);
for var i := 0 to AxisCount - 1 do
FPeakTuple[i] := BigEndianValue.ReadSmallInt(Stream);
end else
begin
var SharedIndex := FTupleIndex and TUPLE_INDEX_MASK;
if (SharedIndex < Length(SharedTuples)) then
FPeakTuple := Copy(SharedTuples[SharedIndex])
else
SetLength(FPeakTuple, AxisCount);
end;
// Intermediate tuples
if (FTupleIndex and INTERMEDIATE_REGION <> 0) then
begin
SetLength(FIntermediateStartTuple, AxisCount);
for var i := 0 to AxisCount - 1 do
FIntermediateStartTuple[i] := BigEndianValue.ReadSmallInt(Stream);
SetLength(FIntermediateEndTuple, AxisCount);
for var i := 0 to AxisCount - 1 do
FIntermediateEndTuple[i] := BigEndianValue.ReadSmallInt(Stream);
end;
end;
function TTupleVariationHeader.CalculateScalar(const NormalizedCoords: TArray<TFixedPoint>): Single;
begin
Result := 1.0;
for var i := 0 to High(FPeakTuple) do
begin
var V: Single;
if i >= Length(NormalizedCoords) then
V := 0
else
V := NormalizedCoords[i].AsSingle;
var Peak := FPeakTuple[i] / 16384.0;
var AxisScalar: Single;
if Peak = 0 then
AxisScalar := 1.0
else if V = Peak then
AxisScalar := 1.0
else
begin
var Start, End_: Single;
if Length(FIntermediateStartTuple) > 0 then
begin
Start := FIntermediateStartTuple[i] / 16384.0;
End_ := FIntermediateEndTuple[i] / 16384.0;
end else
begin
if Peak < 0 then
begin
Start := Peak;
End_ := 0;
end else
begin
Start := 0;
End_ := Peak;
end;
end;
if (V <= Start) or (V >= End_) then
AxisScalar := 0
else if V < Peak then
AxisScalar := (V - Start) / (Peak - Start)
else
AxisScalar := (End_ - V) / (End_ - Peak);
end;
Result := Result * AxisScalar;
if Result = 0 then
Break;
end;
end;
{ TDeltaSetIndexMap }
procedure TDeltaSetIndexMap.LoadFromStream(Stream: TStream; Size: Cardinal);
var
MapCount: Cardinal;
begin
// A DeltaSetIndexMap table can be in one of two formats.
// Format 0: format (uint8=0), entryFormat (uint8), mapCount (uint16), mapData (uint8[])
// Format 1: format (uint8=1), entryFormat (uint8), mapCount (uint32), mapData (uint8[])
FFormat := BigEndianValue.ReadByte(Stream);
FEntryFormat := BigEndianValue.ReadByte(Stream);
case FFormat of
0: MapCount := BigEndianValue.ReadWord(Stream);
1: MapCount := BigEndianValue.ReadCardinal(Stream);
else
raise EPascalTypeError.Create(RCStrUnknownFormat);
end;
var EntrySize := ((FEntryFormat and MAP_ENTRY_SIZE_MASK) shr 4) + 1;
// Sanity check for mapCount if Size is provided
if (Size > 0) then
begin
var HeaderSize: Cardinal;
if (FFormat = 0) then
HeaderSize := 4
else
HeaderSize := 6;
if (HeaderSize + MapCount * EntrySize > Size) then
MapCount := (Size - HeaderSize) div EntrySize;
end;
SetLength(FMapData, MapCount);
for var i := 0 to High(FMapData) do
begin
case EntrySize of
1: FMapData[i] := BigEndianValue.ReadByte(Stream);
2: FMapData[i] := BigEndianValue.ReadWord(Stream);
3: FMapData[i] := BigEndianValue.ReadUInt24(Stream);
4: FMapData[i] := BigEndianValue.ReadCardinal(Stream);
end;
end;
end;
procedure TDeltaSetIndexMap.SaveToStream(Stream: TStream);
begin
raise EPascalTypeNotImplemented.Create(RCStrNotImplemented);
end;
function TDeltaSetIndexMap.GetIndex(GlyphIndex: Word; out Outer, Inner: Word): Boolean;
begin
if (Length(FMapData) = 0) then
begin
Outer := 0;
Inner := GlyphIndex;
Exit(True);
end;
(*
https://learn.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#associating-target-items-to-variation-data
"The mapCount field indicates the number of delta-set index mapping entries.
The logical entries in the mapData array use a base-zero index. In the
context in which a DeltaSetIndexMap table is used, if an index into the
mapping array is used that is greater than or equal to mapCount, then the
last logical entry of the mapping array is used."
*)
if (GlyphIndex > High(FMapData)) then
// Repeat last entry
GlyphIndex := High(FMapData);
var EntryData := FMapData[GlyphIndex];
var InnerBitCount := (FEntryFormat and INNER_INDEX_BIT_COUNT_MASK) + 1;
Inner := EntryData and ((1 shl InnerBitCount) - 1);
Outer := EntryData shr InnerBitCount;
Result := True;
end;
function DecodePackedPoints(Stream: TStream; NumPointsInGlyph: Integer): TArray<Integer>;
begin
var b := BigEndianValue.ReadByte(Stream);
if (b = 0) then
begin
SetLength(Result, NumPointsInGlyph);
for var i := 0 to High(Result) do
Result[i] := i;
Exit;
end;
var Count: Integer;
if (b and POINTS_ARE_WORDS <> 0) then
Count := ((b and POINT_RUN_COUNT_MASK) shl 8) or BigEndianValue.ReadByte(Stream)
else
Count := b;
var Points := TList<Integer>.Create;
try
Points.Capacity := Count;
var i := 0;
while (i < Count) do
begin
var Control := BigEndianValue.ReadByte(Stream);
var RunCount := (Control and POINT_RUN_COUNT_MASK) + 1;
if (Control and POINTS_ARE_WORDS <> 0) then
begin
// 16-bit
for var n := 0 to RunCount - 1 do
begin
var Val := BigEndianValue.ReadWord(Stream);
if (i > 0) then
Points.Add(Points[i-1] + Val)
else
Points.Add(Val);
Inc(i);
end;
end else
begin
// 8-bit
for var n := 0 to RunCount - 1 do
begin
var Val := BigEndianValue.ReadByte(Stream);
if (i > 0) then
Points.Add(Points[i-1] + Val)
else
Points.Add(Val);
Inc(i);
end;
end;
end;
Result := Points.ToArray;
finally
Points.Free;
end;
end;
function DecodePackedDeltas(Stream: TStream; NumPoints: Integer): TArray<Integer>;
begin
SetLength(Result, NumPoints);
var i := 0;
while i < NumPoints do
begin
var Control := BigEndianValue.ReadByte(Stream);
var RunCount := (Control and DELTA_RUN_COUNT_MASK) + 1;
if (Control and DELTAS_ARE_ZERO <> 0) then
begin
// deltas are zero
for var n := 0 to RunCount - 1 do
begin
if (i < NumPoints) then
Result[i] := 0;
Inc(i);
end;
end else
if (Control and DELTAS_ARE_WORDS <> 0) then
begin
// 16-bit
for var n := 0 to RunCount - 1 do
begin
if (i <= High(Result)) then
begin
var Val := BigEndianValue.ReadSmallInt(Stream);
Result[i] := Val;
end else
Stream.Position := Stream.Position + SizeOf(SmallInt);
Inc(i);
end;
end else
begin
// 8-bit
for var n := 0 to RunCount - 1 do
begin
if (i <= High(Result)) then
begin
var Val := ShortInt(BigEndianValue.ReadByte(Stream));
Result[i] := Val;
end else
Stream.Position := Stream.Position + 1;
Inc(i);
end;
end;
end;
end;
procedure ResolveVariationIndices(Stream: TStream; BasePos: Int64; var ValueRecord: TOpenTypeValueRecord);
function ReadVarIndex(Offset: Word): Cardinal;
begin
Result := $FFFFFFFF;
if Offset = 0 then
Exit;
var SavedPos := Stream.Position;
Stream.Position := BasePos + Offset;
var Outer := BigEndianValue.ReadWord(Stream);
var Inner := BigEndianValue.ReadWord(Stream);
var Format := BigEndianValue.ReadWord(Stream);
if Format = $8000 then
Result := (Cardinal(Outer) shl 16) or Inner;
Stream.Position := SavedPos;
end;
begin
ValueRecord.xPlaVarIndex := ReadVarIndex(ValueRecord.xPlaDeviceOffset);
ValueRecord.yPlaVarIndex := ReadVarIndex(ValueRecord.yPlaDeviceOffset);
ValueRecord.xAdvVarIndex := ReadVarIndex(ValueRecord.xAdvDeviceOffset);
ValueRecord.yAdvVarIndex := ReadVarIndex(ValueRecord.yAdvDeviceOffset);
end;
end.