-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalType.Tables.OpenType.Positioning.ChainedContext.pas
More file actions
920 lines (753 loc) · 36.8 KB
/
PascalType.Tables.OpenType.Positioning.ChainedContext.pas
File metadata and controls
920 lines (753 loc) · 36.8 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
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
unit PascalType.Tables.OpenType.Positioning.ChainedContext;
////////////////////////////////////////////////////////////////////////////////
// //
// 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 Christian-W. Budde //
// //
// Portions created by Christian-W. Budde are Copyright (C) 2010-2017 //
// by Christian-W. Budde. All Rights Reserved. //
// //
////////////////////////////////////////////////////////////////////////////////
interface
{$I PT_Compiler.inc}
uses
Generics.Collections,
Generics.Defaults,
Classes,
PascalType.Types,
PascalType.Classes,
PascalType.GlyphString,
PascalType.Tables.OpenType.Lookup,
PascalType.Tables.OpenType.Positioning,
PascalType.Tables.OpenType.ClassDefinition,
PascalType.Tables.OpenType.Coverage;
//------------------------------------------------------------------------------
//
// TOpenTypePositioningLookupTableChainedContext
//
//------------------------------------------------------------------------------
// LookupType 6: Chained Contexts Substitution Subtable
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/gsub#lookuptype-6-chained-contexts-substitution-subtable
//------------------------------------------------------------------------------
type
TOpenTypePositioningLookupTableChainedContext = class(TCustomOpenTypePositioningLookupTable)
public type
TGlyphContextPositioning = (
gccpInvalid = 0,
gccpSimple = 1,
gccpClass = 2,
gccpCoverage = 3
);
protected
function GetSubTableClass(ASubFormat: Word): TOpenTypeLookupSubTableClass; override;
public
end;
//------------------------------------------------------------------------------
//
// TOpenTypePositioningSubTableChainedContextSimple
//
//------------------------------------------------------------------------------
// Chained Contexts Substitution Format 1: Simple Glyph Contexts
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/gsub#61-chained-contexts-substitution-format-1-simple-glyph-contexts
//------------------------------------------------------------------------------
type
TOpenTypePositioningSubTableChainedContextSimple = class(TCustomOpenTypePositioningSubTable)
public type
TChainedSequenceRuleSets = TCustomOpenTypeLookupSubTableWithCoverage.TChainedSequenceRuleSets;
private
FSequenceRules: TChainedSequenceRuleSets;
protected
procedure DoSaveToStream(Stream: TStream); override;
public
constructor Create(AParent: TCustomPascalTypeTable); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
function Apply(var AGlyphIterator: TPascalTypeGlyphIterator): boolean; override;
property SequenceRules: TChainedSequenceRuleSets read FSequenceRules write FSequenceRules;
end;
//------------------------------------------------------------------------------
//
// TOpenTypePositioningSubTableChainedContextClass
//
//------------------------------------------------------------------------------
// Chained Sequence Context Format 2: class-based glyph contexts
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/chapter2#chained-sequence-context-format-2-class-based-glyph-contexts
//------------------------------------------------------------------------------
type
TOpenTypePositioningSubTableChainedContextClass = class(TCustomOpenTypePositioningSubTable)
public type
TChainedSequenceRuleSets = TCustomOpenTypeLookupSubTableWithCoverage.TChainedSequenceRuleSets;
private type
TClassDefinitions = TCustomOpenTypeLookupSubTableWithCoverage.TClassDefinitions;
private
FSequenceRules: TChainedSequenceRuleSets;
FClassDefinitions: TClassDefinitions;
protected
procedure SetClassDefinitions(Index: TContextPart; const Value: TCustomOpenTypeClassDefinitionTable);
procedure DoSaveToStream(Stream: TStream); override;
public
constructor Create(AParent: TCustomPascalTypeTable); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
function Apply(var AGlyphIterator: TPascalTypeGlyphIterator): boolean; override;
property SequenceRules: TChainedSequenceRuleSets read FSequenceRules write FSequenceRules;
property BacktrackClassDefinitions: TCustomOpenTypeClassDefinitionTable index cpBacktrack read FClassDefinitions[cpBacktrack] write SetClassDefinitions;
property InputClassDefinitions: TCustomOpenTypeClassDefinitionTable index cpInput read FClassDefinitions[cpInput] write SetClassDefinitions;
property LookaheadClassDefinitions: TCustomOpenTypeClassDefinitionTable index cpLookahead read FClassDefinitions[cpLookahead] write SetClassDefinitions;
end;
//------------------------------------------------------------------------------
//
// TOpenTypePositioningSubTableChainedContextCoverage
//
//------------------------------------------------------------------------------
// Chained Sequence Context Format 3: coverage-based glyph contexts
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/chapter2#chained-sequence-context-format-3-coverage-based-glyph-contexts
//------------------------------------------------------------------------------
type
TOpenTypePositioningSubTableChainedContextCoverage = class(TCustomOpenTypeLookupSubTable)
private type
TCoverageTables = TCustomOpenTypeLookupSubTable.TCoverageTables;
TContextCoverageTables = TCustomOpenTypeLookupSubTable.TContextCoverageTables;
private
FSequenceRules: TCustomOpenTypeLookupSubTable.TSequenceLookupRecords;
FCoverageTables: TContextCoverageTables;
protected
procedure SetCoverageTables(Index: TContextPart; Value: TCoverageTables);
public
constructor Create(AParent: TCustomPascalTypeTable); override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
function Apply(var AGlyphIterator: TPascalTypeGlyphIterator): boolean; override;
property SequenceRules: TCustomOpenTypeLookupSubTable.TSequenceLookupRecords read FSequenceRules write FSequenceRules;
property BacktrackCoverageTables: TCoverageTables index cpBacktrack read FCoverageTables[cpBacktrack] write SetCoverageTables;
property InputCoverageTables: TCoverageTables index cpInput read FCoverageTables[cpInput] write SetCoverageTables;
property LookaheadCoverageTables: TCoverageTables index cpLookahead read FCoverageTables[cpLookahead] write SetCoverageTables;
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
implementation
uses
SysUtils,
PascalType.Unicode,
PascalType.ResourceStrings,
PascalType.Tables.OpenType.GPOS;
//------------------------------------------------------------------------------
//
// TOpenTypePositioningLookupTableChainedContext
//
//------------------------------------------------------------------------------
function TOpenTypePositioningLookupTableChainedContext.GetSubTableClass(ASubFormat: Word): TOpenTypeLookupSubTableClass;
begin
case TGlyphContextPositioning(ASubFormat) of
gccpSimple:
Result := TOpenTypePositioningSubTableChainedContextSimple;
gccpClass:
Result := TOpenTypePositioningSubTableChainedContextClass;
gccpCoverage:
Result := TOpenTypePositioningSubTableChainedContextCoverage;
else
Result := nil;
end;
end;
//------------------------------------------------------------------------------
//
// TOpenTypePositioningSubTableChainedContextSimple
//
//------------------------------------------------------------------------------
constructor TOpenTypePositioningSubTableChainedContextSimple.Create(AParent: TCustomPascalTypeTable);
begin
inherited;
end;
destructor TOpenTypePositioningSubTableChainedContextSimple.Destroy;
begin
inherited;
end;
procedure TOpenTypePositioningSubTableChainedContextSimple.Assign(Source: TPersistent);
begin
inherited;
if Source is TOpenTypePositioningSubTableChainedContextSimple then
begin
FSequenceRules := Copy(TOpenTypePositioningSubTableChainedContextSimple(Source).FSequenceRules);
for var i := 0 to High(FSequenceRules) do
begin
FSequenceRules[i] := Copy(FSequenceRules[i]);
for var j := 0 to High(FSequenceRules[i]) do
begin
FSequenceRules[i][j].BacktrackSequence := Copy(FSequenceRules[i][j].BacktrackSequence);
FSequenceRules[i][j].InputSequence := Copy(FSequenceRules[i][j].InputSequence);
FSequenceRules[i][j].LookaheadSequence := Copy(FSequenceRules[i][j].LookaheadSequence);
FSequenceRules[i][j].SequenceLookupRecords := Copy(FSequenceRules[i][j].SequenceLookupRecords);
end;
end;
end;
end;
procedure TOpenTypePositioningSubTableChainedContextSimple.LoadFromStream(Stream: TStream; Size: Cardinal);
var
StartPos: Int64;
SavePos: Int64;
i, j, k: integer;
SequenceRuleSetOffsets: array of Word;
SequenceRuleOffsets: array of Word;
Sequence: TGlyphString;
SequenceLookupRecords: TArray<TSequenceLookupRecord>;
begin
// +----------+------------------------------+--------------------------------------------+
// | Type | Name | Description |
// +==========+==============================+============================================+
// | uint16 | format | Format identifier: format = 1. |
// +----------+------------------------------+--------------------------------------------+
// | Offset16 | coverageOffset | Offset to Coverage table. |
// +----------+------------------------------+--------------------------------------------+
// | uint16 | chainPosRuleSetCount | Number of ChainPosRuleSet tables. |
// +----------+------------------------------+--------------------------------------------+
// | Offset16 | chainPosRuleSetOffsets[count]| Array of offsets to tables. |
// +----------+------------------------------+--------------------------------------------+
// Test font: "Monoid Regular" (font does not appears to contain valid data for this table)
SetLength(FSequenceRules, 0);
StartPos := Stream.Position;
inherited;
// check (minimum) table size
if Stream.Position + SizeOf(Word) > Stream.Size then
raise EPascalTypeError.Create(RCStrTableIncomplete);
// Read list of sequence rule set offsets
SetLength(SequenceRuleSetOffsets, BigEndianValue.ReadWord(Stream));
for i := 0 to High(SequenceRuleSetOffsets) do
SequenceRuleSetOffsets[i] := BigEndianValue.ReadWord(Stream);
SavePos := Stream.Position;
SetLength(FSequenceRules, Length(SequenceRuleSetOffsets));
// Read a lists of Sequence Rule Sets
for i := 0 to High(SequenceRuleSetOffsets) do
begin
Stream.Position := StartPos + SequenceRuleSetOffsets[i];
// Read list of offsets to Sequence Rule Sets
// Offsets are from beginning of the Sequence Rule Set table
SetLength(SequenceRuleOffsets, BigEndianValue.ReadWord(Stream));
for j := 0 to High(SequenceRuleOffsets) do
SequenceRuleOffsets[j] := BigEndianValue.ReadWord(Stream);
// Read a Sequence Rule Set
SetLength(FSequenceRules[i], length(SequenceRuleOffsets));
for j := 0 to High(SequenceRuleOffsets) do
begin
if (SequenceRuleOffsets[j] = 0) then
continue;
Stream.Position := StartPos + SequenceRuleSetOffsets[i] + SequenceRuleOffsets[j];
// Read BacktrackSequence
SetLength(FSequenceRules[i][j].BacktrackSequence, BigEndianValue.ReadWord(Stream));
Sequence := FSequenceRules[i][j].BacktrackSequence;
for k := 0 to High(Sequence) do
Sequence[k] := BigEndianValue.ReadWord(Stream);
// Read InputSequence
SetLength(FSequenceRules[i][j].InputSequence, BigEndianValue.ReadWord(Stream));
Sequence := FSequenceRules[i][j].InputSequence;
if (Length(Sequence) > 0) then
begin
// First component isn't used
Sequence[0] := 0;
// Read remaining from input sequence list
for k := 1 to High(Sequence) do
Sequence[k] := BigEndianValue.ReadWord(Stream);
end;
// Read LookaheadSequence
SetLength(FSequenceRules[i][j].LookaheadSequence, BigEndianValue.ReadWord(Stream));
Sequence := FSequenceRules[i][j].LookaheadSequence;
for k := 0 to High(Sequence) do
Sequence[k] := BigEndianValue.ReadWord(Stream);
// Read SequenceLookupRecords
SetLength(FSequenceRules[i][j].SequenceLookupRecords, BigEndianValue.ReadWord(Stream));
SequenceLookupRecords := FSequenceRules[i][j].SequenceLookupRecords;
for k := 0 to High(SequenceLookupRecords) do
begin
SequenceLookupRecords[k].SequenceIndex := BigEndianValue.ReadWord(Stream);
SequenceLookupRecords[k].LookupListIndex := BigEndianValue.ReadWord(Stream);
end;
end;
end;
Stream.Position := SavePos;
end;
procedure TOpenTypePositioningSubTableChainedContextSimple.DoSaveToStream(Stream: TStream);
begin
var StartPos := Stream.Position;
inherited;
var RuleSetOffsets: TArray<Word>;
SetLength(RuleSetOffsets, Length(FSequenceRules));
BigEndianValue.WriteWord(Stream, Length(RuleSetOffsets));
var RuleSetOffsetPos := Stream.Position;
Stream.Seek(Length(RuleSetOffsets) * SizeOf(Word), soFromCurrent);
for var i := 0 to High(FSequenceRules) do
begin
if (Length(FSequenceRules[i]) = 0) then
begin
RuleSetOffsets[i] := 0;
continue;
end;
RuleSetOffsets[i] := Stream.Position - StartPos;
var RuleSetStartPos := Stream.Position;
BigEndianValue.WriteWord(Stream, Length(FSequenceRules[i]));
var RuleOffsetPos := Stream.Position;
Stream.Seek(Length(FSequenceRules[i]) * SizeOf(Word), soFromCurrent);
var RuleOffsets: TArray<Word>;
SetLength(RuleOffsets, Length(FSequenceRules[i]));
for var j := 0 to High(FSequenceRules[i]) do
begin
RuleOffsets[j] := Stream.Position - RuleSetStartPos;
BigEndianValue.WriteWord(Stream, Length(FSequenceRules[i][j].BacktrackSequence));
for var k := 0 to High(FSequenceRules[i][j].BacktrackSequence) do
BigEndianValue.WriteWord(Stream, FSequenceRules[i][j].BacktrackSequence[k]);
BigEndianValue.WriteWord(Stream, Length(FSequenceRules[i][j].InputSequence));
for var k := 1 to High(FSequenceRules[i][j].InputSequence) do
BigEndianValue.WriteWord(Stream, FSequenceRules[i][j].InputSequence[k]);
BigEndianValue.WriteWord(Stream, Length(FSequenceRules[i][j].LookaheadSequence));
for var k := 0 to High(FSequenceRules[i][j].LookaheadSequence) do
BigEndianValue.WriteWord(Stream, FSequenceRules[i][j].LookaheadSequence[k]);
BigEndianValue.WriteWord(Stream, Length(FSequenceRules[i][j].SequenceLookupRecords));
for var k := 0 to High(FSequenceRules[i][j].SequenceLookupRecords) do
begin
BigEndianValue.WriteWord(Stream, FSequenceRules[i][j].SequenceLookupRecords[k].SequenceIndex);
BigEndianValue.WriteWord(Stream, FSequenceRules[i][j].SequenceLookupRecords[k].LookupListIndex);
end;
end;
var SavePos := Stream.Position;
Stream.Position := RuleOffsetPos;
for var j := 0 to High(RuleOffsets) do
BigEndianValue.WriteWord(Stream, RuleOffsets[j]);
Stream.Position := SavePos;
end;
var SavePos := Stream.Position;
// Fix up RuleSet Offsets
Stream.Position := RuleSetOffsetPos;
for var i := 0 to High(RuleSetOffsets) do
BigEndianValue.WriteWord(Stream, RuleSetOffsets[i]);
Stream.Position := SavePos;
end;
function TOpenTypePositioningSubTableChainedContextSimple.Apply(var AGlyphIterator: TPascalTypeGlyphIterator): boolean;
begin
Result := ApplyChainedContextSimple(FSequenceRules, AGlyphIterator);
end;
//------------------------------------------------------------------------------
//
// TOpenTypePositioningSubTableChainedContextClass
//
//------------------------------------------------------------------------------
constructor TOpenTypePositioningSubTableChainedContextClass.Create(AParent: TCustomPascalTypeTable);
begin
inherited;
end;
destructor TOpenTypePositioningSubTableChainedContextClass.Destroy;
var
Part: TContextPart;
begin
for Part := Low(FClassDefinitions) to High(FClassDefinitions) do
FClassDefinitions[Part].Free;
inherited;
end;
procedure TOpenTypePositioningSubTableChainedContextClass.Assign(Source: TPersistent);
begin
inherited;
if Source is TOpenTypePositioningSubTableChainedContextClass then
begin
for var Part := Low(FClassDefinitions) to High(FClassDefinitions) do
FreeAndNil(FClassDefinitions[Part]);
FSequenceRules := Copy(TOpenTypePositioningSubTableChainedContextClass(Source).FSequenceRules);
for var i := 0 to High(FSequenceRules) do
begin
FSequenceRules[i] := Copy(FSequenceRules[i]);
for var j := 0 to High(FSequenceRules[i]) do
begin
FSequenceRules[i][j].BacktrackSequence := Copy(FSequenceRules[i][j].BacktrackSequence);
FSequenceRules[i][j].InputSequence := Copy(FSequenceRules[i][j].InputSequence);
FSequenceRules[i][j].LookaheadSequence := Copy(FSequenceRules[i][j].LookaheadSequence);
FSequenceRules[i][j].SequenceLookupRecords := Copy(FSequenceRules[i][j].SequenceLookupRecords);
end;
end;
// Assignment via property setter makes a copy
for var Part := Low(TContextPart) to High(TContextPart) do
SetClassDefinitions(Part, TOpenTypePositioningSubTableChainedContextClass(Source).FClassDefinitions[Part]);
end;
end;
procedure TOpenTypePositioningSubTableChainedContextClass.LoadFromStream(Stream: TStream; Size: Cardinal);
var
StartPos: Int64;
SavePos: Int64;
Part: TContextPart;
i, j, k: integer;
SequenceRuleSetOffsets: array of Word;
SequenceRuleOffsets: array of Word;
Sequence: TGlyphString;
SequenceLookupRecords: TArray<TSequenceLookupRecord>;
ClassDefOffset: array[TContextPart] of Word;
ClassDefinitionFormat: TClassDefinitionFormat;
ClassDefinitionTableClass: TOpenTypeClassDefinitionTableClass;
begin
// +----------+-------------------------------+--------------------------------------------+
// | Type | Name | Description |
// +==========+===============================+============================================+
// | uint16 | format | Format identifier: format = 2. |
// +----------+-------------------------------+--------------------------------------------+
// | Offset16 | coverageOffset | Offset to Coverage table. |
// +----------+-------------------------------+--------------------------------------------+
// | Offset16 | backtrackClassDefOffset | Offset to backtrack ClassDef table. |
// +----------+-------------------------------+--------------------------------------------+
// | Offset16 | inputClassDefOffset | Offset to input ClassDef table. |
// +----------+-------------------------------+--------------------------------------------+
// | Offset16 | lookaheadClassDefOffset | Offset to lookahead ClassDef table. |
// +----------+-------------------------------+--------------------------------------------+
// | uint16 | chainPosClassSetCount | Number of ChainPosClassSet tables. |
// +----------+-------------------------------+--------------------------------------------+
// | Offset16 | chainPosClassSetOffsets[count]| Array of offsets to tables. |
// +----------+-------------------------------+--------------------------------------------+
// Test font: "Arial Unicode MS", "Segoe UI Variable" (i¨ j¨ i´)
SetLength(FSequenceRules, 0);
for Part := Low(FClassDefinitions) to High(FClassDefinitions) do
FreeAndNil(FClassDefinitions[Part]);
StartPos := Stream.Position;
inherited;
// check (minimum) table size
if Stream.Position + 4 * SizeOf(Word) > Stream.Size then
raise EPascalTypeError.Create(RCStrTableIncomplete);
// Get the offsets to the class definition tables
for Part := Low(ClassDefOffset) to High(ClassDefOffset) do
ClassDefOffset[Part] := BigEndianValue.ReadWord(Stream);
// Read list of sequence rule set offsets
SetLength(SequenceRuleSetOffsets, BigEndianValue.ReadWord(Stream));
for i := 0 to High(SequenceRuleSetOffsets) do
SequenceRuleSetOffsets[i] := BigEndianValue.ReadWord(Stream);
SavePos := Stream.Position;
// Read the class definition tables
for Part := Low(FClassDefinitions) to High(FClassDefinitions) do
begin
Stream.Position := StartPos + ClassDefOffset[Part];
ClassDefinitionFormat := TClassDefinitionFormat(BigEndianValue.ReadWord(Stream));
ClassDefinitionTableClass := TCustomOpenTypeClassDefinitionTable.ClassByFormat(ClassDefinitionFormat);
if (ClassDefinitionTableClass <> nil) then
begin
FClassDefinitions[Part] := ClassDefinitionTableClass.Create(Self);
Stream.Position := StartPos + ClassDefOffset[Part];
FClassDefinitions[Part].LoadFromStream(Stream);
end;
end;
// Read a lists of Sequence Rule Sets
SetLength(FSequenceRules, Length(SequenceRuleSetOffsets));
for i := 0 to High(SequenceRuleSetOffsets) do
begin
if (SequenceRuleSetOffsets[i] = 0) then
continue;
Stream.Position := StartPos + SequenceRuleSetOffsets[i];
// Read list of offsets to Sequence Rule Sets
// Offsets are from beginning of the Sequence Rule Set table
SetLength(SequenceRuleOffsets, BigEndianValue.ReadWord(Stream));
for j := 0 to High(SequenceRuleOffsets) do
SequenceRuleOffsets[j] := BigEndianValue.ReadWord(Stream);
// Read a Sequence Rule Set
SetLength(FSequenceRules[i], Length(SequenceRuleOffsets));
for j := 0 to High(SequenceRuleOffsets) do
begin
if (SequenceRuleOffsets[j] = 0) then
continue;
Stream.Position := StartPos + SequenceRuleSetOffsets[i] + SequenceRuleOffsets[j];
// Read BacktrackSequence
SetLength(FSequenceRules[i][j].BacktrackSequence, BigEndianValue.ReadWord(Stream));
Sequence := FSequenceRules[i][j].BacktrackSequence;
for k := 0 to High(Sequence) do
Sequence[k] := BigEndianValue.ReadWord(Stream);
// Read InputSequence
SetLength(FSequenceRules[i][j].InputSequence, BigEndianValue.ReadWord(Stream));
Sequence := FSequenceRules[i][j].InputSequence;
if (Length(Sequence) > 0) then
begin
// First component is matched via coverage table
Sequence[0] := 0;
// Read remaining from input sequence list
for k := 1 to High(Sequence) do
Sequence[k] := BigEndianValue.ReadWord(Stream);
end;
// Read LookaheadSequence
SetLength(FSequenceRules[i][j].LookaheadSequence, BigEndianValue.ReadWord(Stream));
Sequence := FSequenceRules[i][j].LookaheadSequence;
for k := 0 to High(Sequence) do
Sequence[k] := BigEndianValue.ReadWord(Stream);
// Read SequenceLookupRecords
SetLength(FSequenceRules[i][j].SequenceLookupRecords, BigEndianValue.ReadWord(Stream));
SequenceLookupRecords := FSequenceRules[i][j].SequenceLookupRecords;
for k := 0 to High(SequenceLookupRecords) do
begin
SequenceLookupRecords[k].SequenceIndex := BigEndianValue.ReadWord(Stream);
SequenceLookupRecords[k].LookupListIndex := BigEndianValue.ReadWord(Stream);
end;
end;
end;
Stream.Position := SavePos;
end;
procedure TOpenTypePositioningSubTableChainedContextClass.DoSaveToStream(Stream: TStream);
var
StartPos: Int64;
ClassDefOffsetsPos: Int64;
RuleSetOffsetPos: Int64;
SavePos: Int64;
RuleSetOffsets: array of Word;
RuleOffsets: array of Word;
ClassDefOffsets: array [TContextPart] of Word;
begin
StartPos := Stream.Position;
inherited;
ClassDefOffsetsPos := Stream.Position;
Stream.Seek(3 * SizeOf(Word), soFromCurrent); // Placeholders for ClassDef offsets
SetLength(RuleSetOffsets, Length(FSequenceRules));
BigEndianValue.WriteWord(Stream, Length(RuleSetOffsets));
RuleSetOffsetPos := Stream.Position;
Stream.Seek(Length(RuleSetOffsets) * SizeOf(Word), soFromCurrent);
for var i := cpBacktrack to cpLookahead do
if (FClassDefinitions[i] <> nil) then
begin
ClassDefOffsets[i] := Stream.Position - StartPos;
FClassDefinitions[i].SaveToStream(Stream);
end else
ClassDefOffsets[i] := 0;
for var i := 0 to High(FSequenceRules) do
begin
if (Length(FSequenceRules[i]) = 0) then
begin
RuleSetOffsets[i] := 0;
continue;
end;
RuleSetOffsets[i] := Stream.Position - StartPos;
var RuleSetStartPos := Stream.Position;
BigEndianValue.WriteWord(Stream, Length(FSequenceRules[i]));
var RuleOffsetPos := Stream.Position;
Stream.Seek(Length(FSequenceRules[i]) * SizeOf(Word), soFromCurrent);
SetLength(RuleOffsets, Length(FSequenceRules[i]));
for var j := 0 to High(FSequenceRules[i]) do
begin
RuleOffsets[j] := Stream.Position - RuleSetStartPos;
BigEndianValue.WriteWord(Stream, Length(FSequenceRules[i][j].BacktrackSequence));
for var k := 0 to High(FSequenceRules[i][j].BacktrackSequence) do
BigEndianValue.WriteWord(Stream, FSequenceRules[i][j].BacktrackSequence[k]);
BigEndianValue.WriteWord(Stream, Length(FSequenceRules[i][j].InputSequence));
for var k := 1 to High(FSequenceRules[i][j].InputSequence) do
BigEndianValue.WriteWord(Stream, FSequenceRules[i][j].InputSequence[k]);
BigEndianValue.WriteWord(Stream, Length(FSequenceRules[i][j].LookaheadSequence));
for var k := 0 to High(FSequenceRules[i][j].LookaheadSequence) do
BigEndianValue.WriteWord(Stream, FSequenceRules[i][j].LookaheadSequence[k]);
BigEndianValue.WriteWord(Stream, Length(FSequenceRules[i][j].SequenceLookupRecords));
for var k := 0 to High(FSequenceRules[i][j].SequenceLookupRecords) do
begin
BigEndianValue.WriteWord(Stream, FSequenceRules[i][j].SequenceLookupRecords[k].SequenceIndex);
BigEndianValue.WriteWord(Stream, FSequenceRules[i][j].SequenceLookupRecords[k].LookupListIndex);
end;
end;
SavePos := Stream.Position;
Stream.Position := RuleOffsetPos;
for var j := 0 to High(RuleOffsets) do
BigEndianValue.WriteWord(Stream, RuleOffsets[j]);
Stream.Position := SavePos;
end;
SavePos := Stream.Position;
// Fix up ClassDef offsets
Stream.Position := ClassDefOffsetsPos;
BigEndianValue.WriteWord(Stream, ClassDefOffsets[cpBacktrack]);
BigEndianValue.WriteWord(Stream, ClassDefOffsets[cpInput]);
BigEndianValue.WriteWord(Stream, ClassDefOffsets[cpLookahead]);
// Fix up RuleSet offsets
Stream.Position := RuleSetOffsetPos;
for var i := 0 to High(RuleSetOffsets) do
BigEndianValue.WriteWord(Stream, RuleSetOffsets[i]);
Stream.Position := SavePos;
end;
procedure TOpenTypePositioningSubTableChainedContextClass.SetClassDefinitions(Index: TContextPart; const Value: TCustomOpenTypeClassDefinitionTable);
begin
FreeAndNil(FClassDefinitions[Index]);
if (Value <> nil) then
begin
FClassDefinitions[Index] := TOpenTypeClassDefinitionTableClass(Value.ClassType).Create(Self);
FClassDefinitions[Index].Assign(Value);
end;
end;
function TOpenTypePositioningSubTableChainedContextClass.Apply(var AGlyphIterator: TPascalTypeGlyphIterator): boolean;
begin
Result := ApplyChainedContextClass(FSequenceRules, FClassDefinitions, AGlyphIterator);
end;
//------------------------------------------------------------------------------
//
// TOpenTypePositioningSubTableChainedContextCoverage
//
//------------------------------------------------------------------------------
constructor TOpenTypePositioningSubTableChainedContextCoverage.Create(AParent: TCustomPascalTypeTable);
var
Part: TContextPart;
begin
inherited;
for Part := Low(TContextPart) to High(TContextPart) do
FCoverageTables[Part] := TObjectList<TCustomOpenTypeCoverageTable>.Create;
end;
destructor TOpenTypePositioningSubTableChainedContextCoverage.Destroy;
var
Part: TContextPart;
begin
for Part := Low(TContextPart) to High(TContextPart) do
FCoverageTables[Part].Free;
inherited;
end;
procedure TOpenTypePositioningSubTableChainedContextCoverage.Assign(Source: TPersistent);
var
Part: TContextPart;
begin
inherited;
if Source is TOpenTypePositioningSubTableChainedContextCoverage then
begin
FSequenceRules := Copy(TOpenTypePositioningSubTableChainedContextCoverage(Source).FSequenceRules);
// Assignment via property setter makes a copy
for Part := Low(TContextPart) to High(TContextPart) do
SetCoverageTables(Part, TOpenTypePositioningSubTableChainedContextCoverage(Source).FCoverageTables[Part]);
end;
end;
procedure TOpenTypePositioningSubTableChainedContextCoverage.LoadFromStream(Stream: TStream; Size: Cardinal);
var
StartPos: Int64;
Part: TContextPart;
i: integer;
CoverageOffsets: array[TContextPart] of array of Word;
CoverageTable: TCustomOpenTypeCoverageTable;
begin
// +----------+--------------------------------+--------------------------------------------+
// | Type | Name | Description |
// +==========+================================+============================================+
// | uint16 | format | Format identifier: format = 3. |
// +----------+--------------------------------+--------------------------------------------+
// | uint16 | backtrackGlyphCount | Number of backtrack glyphs. |
// +----------+--------------------------------+--------------------------------------------+
// | Offset16 | backtrackCoverageOffsets[count]| Array of offsets to tables. |
// +----------+--------------------------------+--------------------------------------------+
// | uint16 | inputGlyphCount | Number of input glyphs. |
// +----------+--------------------------------+--------------------------------------------+
// | Offset16 | inputCoverageOffsets[count] | Array of offsets to tables. |
// +----------+--------------------------------+--------------------------------------------+
// | uint16 | lookaheadGlyphCount | Number of lookahead glyphs. |
// +----------+--------------------------------+--------------------------------------------+
// | Offset16 | lookaheadCoverageOffsets[count]| Array of offsets to tables. |
// +----------+--------------------------------+--------------------------------------------+
// | uint16 | posCount | Number of PosLookupRecords. |
// +----------+--------------------------------+--------------------------------------------+
// | record | posLookupRecords[posCount] | Array of LookupRecords. |
// +----------+--------------------------------+--------------------------------------------+
// Test font: "Arial"
SetLength(FSequenceRules, 0);
for Part := Low(FCoverageTables) to High(FCoverageTables) do
FCoverageTables[Part].Clear;
StartPos := Stream.Position;
inherited;
// check (minimum) table size
if Stream.Position + 4 * SizeOf(Word) > Stream.Size then
raise EPascalTypeError.Create(RCStrTableIncomplete);
// Read coverage table offsets
for Part := Low(FCoverageTables) to High(FCoverageTables) do
begin
SetLength(CoverageOffsets[Part], BigEndianValue.ReadWord(Stream));
for i := 0 to High(CoverageOffsets[Part]) do
CoverageOffsets[Part][i] := BigEndianValue.ReadWord(Stream);
end;
// Read Sequence Rule
SetLength(FSequenceRules, BigEndianValue.ReadWord(Stream));
for i := 0 to High(FSequenceRules) do
begin
FSequenceRules[i].SequenceIndex := BigEndianValue.ReadWord(Stream);
FSequenceRules[i].LookupListIndex := BigEndianValue.ReadWord(Stream);
end;
// Read the coverage tables
for Part := Low(FCoverageTables) to High(FCoverageTables) do
begin
FCoverageTables[Part].Capacity := Length(CoverageOffsets[Part]);
for i := 0 to High(CoverageOffsets[Part]) do
begin
Stream.Position := StartPos + CoverageOffsets[Part][i];
CoverageTable := TCustomOpenTypeCoverageTable.CreateFromStream(Stream);
FCoverageTables[Part].Add(CoverageTable);
end;
end;
end;
procedure TOpenTypePositioningSubTableChainedContextCoverage.SaveToStream(Stream: TStream);
var
StartPos: Int64;
SavePos: Int64;
i, j: integer;
CoverageOffsets: array [TContextPart] of array of Word;
Part: TContextPart;
begin
StartPos := Stream.Position;
inherited;
for Part := cpBacktrack to cpLookahead do
BigEndianValue.WriteWord(Stream, FCoverageTables[Part].Count);
var CoverageOffsetPos := Stream.Position;
for Part := cpBacktrack to cpLookahead do
Stream.Seek(FCoverageTables[Part].Count * SizeOf(Word), soFromCurrent);
BigEndianValue.WriteWord(Stream, Length(FSequenceRules));
for i := 0 to High(FSequenceRules) do
begin
BigEndianValue.WriteWord(Stream, FSequenceRules[i].SequenceIndex);
BigEndianValue.WriteWord(Stream, FSequenceRules[i].LookupListIndex);
end;
for Part := cpBacktrack to cpLookahead do
begin
SetLength(CoverageOffsets[Part], FCoverageTables[Part].Count);
for j := 0 to FCoverageTables[Part].Count - 1 do
begin
CoverageOffsets[Part, j] := Stream.Position - StartPos;
FCoverageTables[Part][j].SaveToStream(Stream);
end;
end;
SavePos := Stream.Position;
Stream.Position := CoverageOffsetPos;
for Part := cpBacktrack to cpLookahead do
for j := 0 to High(CoverageOffsets[Part]) do
BigEndianValue.WriteWord(Stream, CoverageOffsets[Part, j]);
Stream.Position := SavePos;
end;
procedure TOpenTypePositioningSubTableChainedContextCoverage.SetCoverageTables(Index: TContextPart; Value: TCoverageTables);
var
CoverageTable: TCustomOpenTypeCoverageTable;
NewCoverageTable: TCustomOpenTypeCoverageTable;
begin
FCoverageTables[Index].Clear;
if (Value = nil) then
exit;
for CoverageTable in Value do
begin
NewCoverageTable := CoverageTable.Clone;
FCoverageTables[Index].Add(NewCoverageTable);
end;
end;
function TOpenTypePositioningSubTableChainedContextCoverage.Apply(var AGlyphIterator: TPascalTypeGlyphIterator): boolean;
begin
Result := ApplyChainedContextCoverage(FCoverageTables, FSequenceRules, AGlyphIterator);
end;
//------------------------------------------------------------------------------
initialization
TCustomOpenTypePositioningLookupTable.RegisterPositioningFormat(gpChainedContext, TOpenTypePositioningLookupTableChainedContext);
end.