-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalType.Tables.OpenType.Common.FeatureVariation.pas
More file actions
545 lines (446 loc) · 19.6 KB
/
PascalType.Tables.OpenType.Common.FeatureVariation.pas
File metadata and controls
545 lines (446 loc) · 19.6 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
unit PascalType.Tables.OpenType.Common.FeatureVariation;
////////////////////////////////////////////////////////////////////////////////
// //
// 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,
PascalType.Tables.OpenType.Feature;
//------------------------------------------------------------------------------
//
// Feature variations
//
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/chapter2#feature-variations
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//
// Condition table
//
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/chapter2#condition-table
//------------------------------------------------------------------------------
type
TOpenTypeConditionTableClass = class of TCustomOpenTypeConditionTable;
TCustomOpenTypeConditionTable = class abstract(TCustomPascalTypeTable)
private class var
FConditionTableClasses: TDictionary<Word, TOpenTypeConditionTableClass>;
private
class destructor Destroy;
public
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
function IsMatch(const NormalizedCoords: TArray<TFixedPoint>): Boolean; virtual; abstract;
class function Format: Word; virtual; abstract;
class procedure RegisterConditionTableClass;
class function FindClassByFormat(Format: Word): TOpenTypeConditionTableClass;
end;
//------------------------------------------------------------------------------
//
// Condition table format 1: font variation axis range
//
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/chapter2#condition-table-format-1-font-variation-axis-range
//------------------------------------------------------------------------------
type
TOpenTypeConditionTableVariationRegion = class(TCustomOpenTypeConditionTable)
private
FAxisIndex: Word;
FFilterRangeMin: TShortFrac;
FFilterRangeMax: TShortFrac;
FFilterRangeMinValue: Single;
FFilterRangeMaxValue: Single;
public
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
function IsMatch(const NormalizedCoords: TArray<TFixedPoint>): Boolean; override;
class function Format: Word; override;
property AxisIndex: Word read FAxisIndex;
property FilterRangeMin: TShortFrac read FFilterRangeMin;
property FilterRangeMax: TShortFrac read FFilterRangeMax;
end;
//------------------------------------------------------------------------------
//
// ConditionSet table
//
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/chapter2#conditionset-table
//------------------------------------------------------------------------------
type
TOpenTypeConditionSetTable = class(TCustomPascalTypeTable)
private
FConditions: TObjectList<TCustomOpenTypeConditionTable>;
function GetConditionCount: Integer;
function GetCondition(Index: Integer): TCustomOpenTypeConditionTable;
public
constructor Create(AParent: TCustomPascalTypeTable); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
function IsMatch(const NormalizedCoords: TArray<TFixedPoint>): Boolean;
property ConditionCount: Integer read GetConditionCount;
property Conditions[Index: Integer]: TCustomOpenTypeConditionTable read GetCondition; default;
end;
//------------------------------------------------------------------------------
//
// FeatureTableSubstitution table
//
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/chapter2#featuretablesubstitution-table
//------------------------------------------------------------------------------
type
TOpenTypeFeatureTableSubstitutionTable = class(TCustomPascalTypeTable)
private type
TOpenTypeFeatureTableSubstitutionRecord = record
FeatureIndex: Word;
AlternateFeatureTable: TCustomOpenTypeFeatureTable;
end;
private
FSubstitutions: TArray<TOpenTypeFeatureTableSubstitutionRecord>;
function GetSubstitutionCount: Integer;
public
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
function FindSubstitution(FeatureIndex: Word): TCustomOpenTypeFeatureTable;
property SubstitutionCount: Integer read GetSubstitutionCount;
end;
//------------------------------------------------------------------------------
//
// FeatureVariations table
//
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/chapter2#featurevariations-table
//------------------------------------------------------------------------------
type
TOpenTypeFeatureVariationsTable = class(TCustomPascalTypeTable)
private type
TOpenTypeFeatureVariationRecord = record
ConditionSetTable: TOpenTypeConditionSetTable;
FeatureTableSubstitutionTable: TOpenTypeFeatureTableSubstitutionTable;
end;
private
FVersion: Cardinal;
FVariationRecords: TArray<TOpenTypeFeatureVariationRecord>;
function GetVariationRecordCount: Integer;
public
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
function GetSubstitution(const NormalizedCoords: TArray<TFixedPoint>): TOpenTypeFeatureTableSubstitutionTable;
property VariationRecordCount: Integer read GetVariationRecordCount;
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
implementation
uses
Generics.Defaults,
PascalType.ResourceStrings,
PascalType.Tables.OpenType.Common;
{ TCustomOpenTypeConditionTable }
class destructor TCustomOpenTypeConditionTable.Destroy;
begin
FConditionTableClasses.Free;
end;
class function TCustomOpenTypeConditionTable.FindClassByFormat(Format: Word): TOpenTypeConditionTableClass;
begin
if (FConditionTableClasses = nil) or (not FConditionTableClasses.TryGetValue(Format, Result)) then
Result := nil;
end;
procedure TCustomOpenTypeConditionTable.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
inherited;
// Format is already read by TOpenTypeConditionSetTable.LoadFromStream
end;
class procedure TCustomOpenTypeConditionTable.RegisterConditionTableClass;
begin
if (FConditionTableClasses = nil) then
FConditionTableClasses:= TDictionary<Word, TOpenTypeConditionTableClass>.Create;
FConditionTableClasses.Add(Format, Self);
end;
{ TOpenTypeConditionTableVariationRegion }
procedure TOpenTypeConditionTableVariationRegion.Assign(Source: TPersistent);
begin
inherited;
if (Source is TOpenTypeConditionTableVariationRegion) then
begin
FAxisIndex := TOpenTypeConditionTableVariationRegion(Source).FAxisIndex;
FFilterRangeMin := TOpenTypeConditionTableVariationRegion(Source).FFilterRangeMin;
FFilterRangeMax := TOpenTypeConditionTableVariationRegion(Source).FFilterRangeMax;
end;
end;
procedure TOpenTypeConditionTableVariationRegion.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
inherited;
FAxisIndex := BigEndianValue.ReadWord(Stream);
FFilterRangeMin := BigEndianValue.ReadSmallInt(Stream);
FFilterRangeMax := BigEndianValue.ReadSmallInt(Stream);
FFilterRangeMinValue := FFilterRangeMin / 16384.0;
FFilterRangeMaxValue := FFilterRangeMax / 16384.0;
end;
class function TOpenTypeConditionTableVariationRegion.Format: Word;
begin
Result := 1;
end;
function TOpenTypeConditionTableVariationRegion.IsMatch(const NormalizedCoords: TArray<TFixedPoint>): Boolean;
begin
if (FAxisIndex >= Length(NormalizedCoords)) then
Exit(False);
var Coord := NormalizedCoords[FAxisIndex].AsSingle;
Result := (Coord >= FFilterRangeMinValue) and (Coord <= FFilterRangeMaxValue);
end;
{ TOpenTypeConditionSetTable }
constructor TOpenTypeConditionSetTable.Create(AParent: TCustomPascalTypeTable);
begin
inherited;
FConditions := TObjectList<TCustomOpenTypeConditionTable>.Create(True);
end;
destructor TOpenTypeConditionSetTable.Destroy;
begin
FConditions.Free;
inherited;
end;
procedure TOpenTypeConditionSetTable.Assign(Source: TPersistent);
begin
inherited;
if (Source is TOpenTypeConditionSetTable) then
begin
FConditions.Clear;
for var i := 0 to TOpenTypeConditionSetTable(Source).ConditionCount - 1 do
begin
var SrcCondition := TOpenTypeConditionSetTable(Source).Conditions[i];
var DstCondition := TOpenTypeConditionTableClass(SrcCondition.ClassType).Create(Self);
FConditions.Add(DstCondition);
DstCondition.Assign(SrcCondition);
end;
end;
end;
function TOpenTypeConditionSetTable.GetCondition(Index: Integer): TCustomOpenTypeConditionTable;
begin
Result := FConditions[Index];
end;
function TOpenTypeConditionSetTable.GetConditionCount: Integer;
begin
Result := FConditions.Count;
end;
function TOpenTypeConditionSetTable.IsMatch(const NormalizedCoords: TArray<TFixedPoint>): Boolean;
begin
if (FConditions.Count = 0) then
Exit(True);
for var Condition in FConditions do
if not Condition.IsMatch(NormalizedCoords) then
Exit(False);
Result := True;
end;
procedure TOpenTypeConditionSetTable.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
var StartPos := Stream.Position;
inherited;
var ConditionCount := BigEndianValue.ReadWord(Stream);
var Offsets: TArray<Cardinal>;
SetLength(Offsets, ConditionCount);
for var i := 0 to ConditionCount - 1 do
Offsets[i] := BigEndianValue.ReadCardinal(Stream);
for var i := 0 to ConditionCount - 1 do
begin
Stream.Position := StartPos + Offsets[i];
var Format := BigEndianValue.ReadWord(Stream);
var ConditionClass := TCustomOpenTypeConditionTable.FindClassByFormat(Format);
if (ConditionClass = nil) then
continue; // Unsupported condition format
var Condition := ConditionClass.Create(Self);
FConditions.Add(Condition);
Condition.LoadFromStream(Stream);
end;
end;
{ TOpenTypeFeatureTableSubstitutionTable }
destructor TOpenTypeFeatureTableSubstitutionTable.Destroy;
begin
for var i := 0 to High(FSubstitutions) do
FSubstitutions[i].AlternateFeatureTable.Free;
inherited;
end;
procedure TOpenTypeFeatureTableSubstitutionTable.Assign(Source: TPersistent);
begin
inherited;
if (Source is TOpenTypeFeatureTableSubstitutionTable) then
begin
for var i := 0 to High(FSubstitutions) do
FSubstitutions[i].AlternateFeatureTable.Free;
SetLength(FSubstitutions, TOpenTypeFeatureTableSubstitutionTable(Source).SubstitutionCount);
for var i := 0 to High(FSubstitutions) do
begin
var SrcSubst := TOpenTypeFeatureTableSubstitutionTable(Source).FSubstitutions[i];
FSubstitutions[i].FeatureIndex := SrcSubst.FeatureIndex;
FSubstitutions[i].AlternateFeatureTable := TCustomOpenTypeFeatureTable(SrcSubst.AlternateFeatureTable.ClassType).Create(Self);
FSubstitutions[i].AlternateFeatureTable.Assign(SrcSubst.AlternateFeatureTable);
end;
end;
end;
function TOpenTypeFeatureTableSubstitutionTable.FindSubstitution(FeatureIndex: Word): TCustomOpenTypeFeatureTable;
begin
for var i := 0 to High(FSubstitutions) do
if (FSubstitutions[i].FeatureIndex = FeatureIndex) then
Exit(FSubstitutions[i].AlternateFeatureTable);
Result := nil;
end;
function TOpenTypeFeatureTableSubstitutionTable.GetSubstitutionCount: Integer;
begin
Result := Length(FSubstitutions);
end;
procedure TOpenTypeFeatureTableSubstitutionTable.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
var StartPos := Stream.Position;
inherited;
var Version := BigEndianValue.ReadCardinal(Stream);
if (Version <> $00010000) then
raise EPascalTypeError.Create(RCStrUnsupportedVersion);
var SubstitutionCount := BigEndianValue.ReadWord(Stream);
SetLength(FSubstitutions, SubstitutionCount);
// FeatureRecord only contains the offset to the Feature Table. To determine
// the correct class for specialized feature parameter parsing, we must
// retrieve the feature's Tag from the GSUB/GPOS table's FeatureListTable.
var CommonTable: TCustomOpenTypeCommonTable := nil;
if (Parent <> nil) and (Parent.Parent <> nil) then
CommonTable := Parent.Parent as TCustomOpenTypeCommonTable;
for var i := 0 to SubstitutionCount - 1 do
begin
FSubstitutions[i].FeatureIndex := BigEndianValue.ReadWord(Stream);
var AlternateFeatureOffset := BigEndianValue.ReadCardinal(Stream);
var SavePos := Stream.Position;
Stream.Position := StartPos + AlternateFeatureOffset;
var FeatureClass: TOpenTypeFeatureTableClass := nil;
var Tag: TTableType := 0;
if (FSubstitutions[i].FeatureIndex < CommonTable.FeatureListTable.FeatureCount) then
begin
Tag := CommonTable.FeatureListTable.Feature[FSubstitutions[i].FeatureIndex].TableType;
FeatureClass := FindFeatureByType(Tag);
end;
if (FeatureClass = nil) then
FeatureClass := TOpenTypeFeatureTableGeneric;
var FeatureTable := FeatureClass.Create(Self);
FSubstitutions[i].AlternateFeatureTable := FeatureTable;
if (FeatureTable is TOpenTypeFeatureTableGeneric) then
TOpenTypeFeatureTableGeneric(FeatureTable).TableType := Tag;
FeatureTable.LoadFromStream(Stream);
Stream.Position := SavePos;
end;
end;
{ TOpenTypeFeatureVariationsTable }
destructor TOpenTypeFeatureVariationsTable.Destroy;
begin
for var i := 0 to High(FVariationRecords) do
begin
FVariationRecords[i].ConditionSetTable.Free;
FVariationRecords[i].FeatureTableSubstitutionTable.Free;
end;
inherited;
end;
procedure TOpenTypeFeatureVariationsTable.Assign(Source: TPersistent);
begin
inherited;
if (Source is TOpenTypeFeatureVariationsTable) then
begin
for var i := 0 to High(FVariationRecords) do
begin
FVariationRecords[i].ConditionSetTable.Free;
FVariationRecords[i].FeatureTableSubstitutionTable.Free;
end;
FVersion := TOpenTypeFeatureVariationsTable(Source).FVersion;
SetLength(FVariationRecords, TOpenTypeFeatureVariationsTable(Source).VariationRecordCount);
for var i := 0 to High(FVariationRecords) do
begin
var SrcRecord := TOpenTypeFeatureVariationsTable(Source).FVariationRecords[i];
if (SrcRecord.ConditionSetTable <> nil) then
begin
FVariationRecords[i].ConditionSetTable := TOpenTypeConditionSetTable.Create(Self);
FVariationRecords[i].ConditionSetTable.Assign(SrcRecord.ConditionSetTable);
end else
FVariationRecords[i].ConditionSetTable := nil;
if (SrcRecord.FeatureTableSubstitutionTable <> nil) then
begin
FVariationRecords[i].FeatureTableSubstitutionTable := TOpenTypeFeatureTableSubstitutionTable.Create(Self);
FVariationRecords[i].FeatureTableSubstitutionTable.Assign(SrcRecord.FeatureTableSubstitutionTable);
end else
FVariationRecords[i].FeatureTableSubstitutionTable := nil;
end;
end;
end;
function TOpenTypeFeatureVariationsTable.GetSubstitution(const NormalizedCoords: TArray<TFixedPoint>): TOpenTypeFeatureTableSubstitutionTable;
begin
for var i := 0 to High(FVariationRecords) do
if FVariationRecords[i].ConditionSetTable.IsMatch(NormalizedCoords) then
Exit(FVariationRecords[i].FeatureTableSubstitutionTable);
Result := nil;
end;
function TOpenTypeFeatureVariationsTable.GetVariationRecordCount: Integer;
begin
Result := Length(FVariationRecords);
end;
procedure TOpenTypeFeatureVariationsTable.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
var StartPos := Stream.Position;
inherited;
FVersion := BigEndianValue.ReadCardinal(Stream);
if (FVersion <> $00010000) then
raise EPascalTypeError.Create(RCStrUnsupportedVersion);
var RecordCount := BigEndianValue.ReadCardinal(Stream);
SetLength(FVariationRecords, RecordCount);
for var i := 0 to RecordCount - 1 do
begin
var ConditionSetOffset := BigEndianValue.ReadCardinal(Stream);
var FeatureTableSubstitutionOffset := BigEndianValue.ReadCardinal(Stream);
var SavePos := Stream.Position;
if (ConditionSetOffset <> 0) then
begin
Stream.Position := StartPos + ConditionSetOffset;
FVariationRecords[i].ConditionSetTable := TOpenTypeConditionSetTable.Create(Self);
FVariationRecords[i].ConditionSetTable.LoadFromStream(Stream);
end else
FVariationRecords[i].ConditionSetTable := nil;
if (FeatureTableSubstitutionOffset <> 0) then
begin
Stream.Position := StartPos + FeatureTableSubstitutionOffset;
FVariationRecords[i].FeatureTableSubstitutionTable := TOpenTypeFeatureTableSubstitutionTable.Create(Self);
FVariationRecords[i].FeatureTableSubstitutionTable.LoadFromStream(Stream);
end else
FVariationRecords[i].FeatureTableSubstitutionTable := nil;
Stream.Position := SavePos;
end;
end;
initialization
TOpenTypeConditionTableVariationRegion.RegisterConditionTableClass;
end.