-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalType.FontFace.SFNT.pas
More file actions
2362 lines (1979 loc) · 73.6 KB
/
PascalType.FontFace.SFNT.pas
File metadata and controls
2362 lines (1979 loc) · 73.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
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
unit PascalType.FontFace.SFNT;
////////////////////////////////////////////////////////////////////////////////
// //
// 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}
{$IFDEF CPUx86_64}
{$DEFINE PUREPASCAL}
{$ENDIF}
uses
Generics.Collections,
Classes, SysUtils, Types,
PascalType.Types,
PascalType.Classes,
PascalType.Unicode,
PascalType.GlyphString,
PascalType.FontFace,
PascalType.Tables,
PascalType.Tables.OpenType.GDEF,
PascalType.Tables.TrueType.Directory,
PascalType.Tables.TrueType.head,
PascalType.Tables.TrueType.name,
PascalType.Tables.TrueType.glyf,
PascalType.Tables.TrueType.cmap,
PascalType.Tables.TrueType.maxp,
PascalType.Tables.TrueType.hmtx,
PascalType.Tables.TrueType.hhea,
PascalType.Tables.TrueType.vhea,
PascalType.Tables.TrueType.vmtx,
PascalType.Tables.TrueType.os2,
PascalType.Tables.TrueType.post,
PascalType.Tables.TrueType.Panose,
PascalType.Tables.TrueType.gvar,
PascalType.Tables.OpenType.HVAR,
PascalType.Tables.OpenType.VVAR,
PascalType.Tables.OpenType.MVAR,
PascalType.Tables.OpenType.STAT,
PascalType.Tables.OpenType.PostScript.CFF,
PascalType.Tables.OpenType.Common.ValueRecord,
PascalType.Tables.Apple,
PascalType.Variation;
type
TTrueTypeGlyphMetric = record
HorizontalMetric: THorizontalMetric;
VerticalMetric: TVerticalMetric;
end;
type
TCustomPascalTypeFontFace = class;
//------------------------------------------------------------------------------
//
// TFontGlyphString
//
//------------------------------------------------------------------------------
// A glyph string with knowledge about the font
//------------------------------------------------------------------------------
TFontGlyph = class(TPascalTypeGlyph)
end;
TFontGlyphString = class(TPascalTypeGlyphString)
private
FFont: TCustomPascalTypeFontFace;
protected
function GetGlyphClassID(AGlyph: TPascalTypeGlyph): integer; override;
function GetGlyphByCodePoint(ACodePoint: TPascalTypeCodePoint): Integer; override;
function GetMarkAttachmentType(AGlyph: TPascalTypeGlyph): integer; override;
function IsInMarkFilteringSet(AGlyph: TPascalTypeGlyph; ASetIndex: integer): boolean; override;
class function GetGlyphClass: TPascalTypeGlyphClass; override;
procedure ApplyVariation(AGlyph: TPascalTypeGlyph; const AValueRecord: TOpenTypeValueRecord); override;
public
constructor Create(AFont: TCustomPascalTypeFontFace; const ACodePoints: TPascalTypeCodePoints); overload;
constructor Create(AFont: TCustomPascalTypeFontFace; const ACodePoints: TPascalTypeCodePoints; AClusterLevel: TClusterLevel); overload; virtual;
procedure HideDefaultIgnorables; override;
property Font: TCustomPascalTypeFontFace read FFont;
end;
TFontGlyphStringClass = class of TFontGlyphString;
//------------------------------------------------------------------------------
//
// TCustomPascalTypeFontFace
//
//------------------------------------------------------------------------------
// A TrueType/OpenType font
//------------------------------------------------------------------------------
TCustomPascalTypeFontFace = class abstract(TCustomPascalTypeFontFacePersistent, IPascalTypeFontFace)
strict protected const
{$IFDEF MSWINDOWS}
PreferredPlatform = piMicrosoft; // This is strange. Why wouldn't we always prefer Unicode?
PreferredLanguage = 1033;
{$ELSE}
PreferredPlatform = piUnicode;
PreferredLanguage = 3;
{$ENDIF}
strict protected type
TTableList = TObjectList<TCustomPascalTypeNamedTable>;
TTableLookup = TDictionary<TPascalTypeTag, TCustomPascalTypeNamedTable>;
strict private
FVersion: Cardinal;
FRootTable: TCustomPascalTypeTable;
// We keep the tables in two lists in order to avoid going through
// TDictionary.Values.ToArray when we just need a list of tables.
FTables: TTableList;
FTableLookup: TTableLookup;
// Required table shortcuts
FHeaderTable: TPascalTypeHeaderTable;
FHorizontalHeader: TPascalTypeHorizontalHeaderTable;
FMaximumProfile: TPascalTypeMaximumProfileTable;
FNameTable: TPascalTypeNameTable;
FPostScriptTable: TPascalTypePostscriptTable;
// Optional table shortcuts
FVerticalHeader: TPascalTypeVerticalHeaderTable;
FGlyphDefinitionTable: TOpenTypeGlyphDefinitionTable;
FCFFTable: TPascalTypeCompactFontFormatTable;
// Variations
FVariationTable: TPascalTypeFontVariationTable;
FMetricsVariationTable: TPascalTypeMetricsVariationTable;
FStyleAttributesTable: TPascalTypeStyleAttributesTable;
FVariationEngine: TPascalTypeVariationEngine;
private
function GetVariationCoordinates: TArray<TFixedPoint>;
procedure SetVariationCoordinates(const Value: TArray<TFixedPoint>);
function GetVariationEngine: TPascalTypeVariationEngine;
function GetFontName: string;
function GetFontStyle: TFontStyles;
function GetFontFamilyName: string;
function GetFontSubFamilyName: string;
function GetFontVersion: string;
function GetUniqueIdentifier: string;
function GetTable(Index: integer): TCustomPascalTypeNamedTable;
function GetTableCount: Integer;
protected
procedure Loaded; virtual;
procedure DoClear; virtual;
procedure DoClearCache; virtual;
procedure ClearCache;
procedure DirectoryTableLoaded(DirectoryTable: TPascalTypeDirectoryTable); virtual;
procedure LoadTablesFromStream(Stream: TStream; const TableList: TPascalTypeDirectoryTableList); virtual;
function LoadTableFromStream(Stream: TStream; const TableEntry: TDirectoryTableEntry): TCustomPascalTypeNamedTable; virtual;
function GetGlyphStringClass: TFontGlyphStringClass; virtual;
procedure TableAdded(ATable: TCustomPascalTypeNamedTable); virtual;
procedure AddTable(ATable: TCustomPascalTypeNamedTable); overload;
function GetTypographicAscender: SmallInt; virtual;
function GetTypographicDescender: SmallInt; virtual;
function GetTypographicLineGap: SmallInt; virtual;
// Variations
function GetAxisValue(Index: integer): Single;
procedure SetAxisValue(Index: integer; Value: Single);
function GetAxisValueByTag(const Tag: TPascalTypeTag): Single;
procedure SetAxisValueByTag(const Tag: TPascalTypeTag; Value: Single);
function GetAxisValueByName(const Name: string): Single;
procedure SetAxisValueByName(const Name: string; Value: Single);
property RootTable: TCustomPascalTypeTable read FRootTable;
property AllTables: TTableList read FTables;
property TableLookup: TTableLookup read FTableLookup;
{$IFDEF ChecksumTest}
procedure ValidateChecksum(Stream: TStream; TableEntry: TDirectoryTableEntry); virtual;
{$ENDIF}
public
// IPascalTypeFontFace
function GetTableByName(const ATableName: TPascalTypeTag): TCustomPascalTypeNamedTable; virtual;
function GetTableByTableClass(ATableClass: TCustomPascalTypeNamedTableClass): TCustomPascalTypeNamedTable; virtual;
function GetGlyphName(GlyphID: TGlyphID): string; virtual;
function GetAdvanceWidth(GlyphIndex: Word): Word; virtual;
function GetAdvanceHeight(GlyphIndex: Word): Word; virtual;
function GetVerticalOrigin(GlyphIndex: Word): TPoint; virtual;
function GetHasVariations: boolean;
property HasVariations: boolean read GetHasVariations;
function GetVariationsActive: boolean;
property VariationsActive: boolean read GetVariationsActive;
function GetVariationItemDelta(OuterIndex, InnerIndex: Word): Single;
function GetVariationMetricDelta(const Tag: TPascalTypeTag): Single;
function GetVariationNormalizedCoordinates: TArray<TFixedPoint>;
property VariationNormalizedCoordinates: TArray<TFixedPoint> read GetVariationNormalizedCoordinates;
{$if defined(PhantomPoints)}
function GetGlyphPointCount(GlyphID: TGlyphID): Integer; virtual;
{$ifend}
public
constructor Create; virtual;
destructor Destroy; override;
procedure Clear;
procedure LoadFromStream(Stream: TStream); override;
function CreateGlyphString(const ACodePoints: TPascalTypeCodePoints): TFontGlyphString; overload;
function CreateGlyphString(const ACodePoints: TPascalTypeCodePoints; AClusterLevel: TClusterLevel): TFontGlyphString; overload; virtual;
function AddTable(const ATableClass: TCustomPascalTypeNamedTableClass): TCustomPascalTypeNamedTable; overload;
function AddTable(const ATableType: TPascalTypeTag): TCustomPascalTypeNamedTable; overload;
function ContainsTable(const ATableType: TPascalTypeTag): Boolean;
function GetGlyphByCodePoint(ACodePoint: TPascalTypeCodePoint): Integer; overload; virtual; abstract;
function GetGlyphByCodePoint(ACodePoint, ANextCodePoint: TPascalTypeCodePoint; var AIndex: Integer): Integer; overload; virtual; abstract;
function GetGlyphByCodePoint(ACodePoint: Word): Integer; overload;
function HasGlyphByCodePoint(ACodePoint: TPascalTypeCodePoint): boolean;
function GetGlyphByCharacter(ACharacter: WideChar): Integer; overload;
function GetGlyphByCharacter(ACharacter: AnsiChar): Integer; overload;
public
function GetGlyphExtentsY(GlyphID: TGlyphID; var YMin, YMax: SmallInt): Boolean;
function GetGlyphExtents(GlyphID: TGlyphID; var XMin, YMin, XMax, YMax: SmallInt): Boolean; virtual;
// Font file version/type
property Version: Cardinal read FVersion;
property Tables[Index: integer]: TCustomPascalTypeNamedTable read GetTable;
property TableCount: integer read GetTableCount;
// Required tables
property HeaderTable: TPascalTypeHeaderTable read FHeaderTable;
property HorizontalHeader: TPascalTypeHorizontalHeaderTable read FHorizontalHeader;
property MaximumProfile: TPascalTypeMaximumProfileTable read FMaximumProfile;
property NameTable: TPascalTypeNameTable read FNameTable;
property PostScriptTable: TPascalTypePostscriptTable read FPostScriptTable;
// Optional tables
property VerticalHeader: TPascalTypeVerticalHeaderTable read FVerticalHeader;
property GlyphDefinitionTable: TOpenTypeGlyphDefinitionTable read FGlyphDefinitionTable;
property CFFTable: TPascalTypeCompactFontFormatTable read FCFFTable;
// Variation properties
property VariationCoordinates: TArray<TFixedPoint> read GetVariationCoordinates write SetVariationCoordinates;
property VariationEngine: TPascalTypeVariationEngine read GetVariationEngine;
property VariationTable: TPascalTypeFontVariationTable read FVariationTable;
function FindAxisByTag(const Tag: TPascalTypeTag): integer;
function FindAxisByName(const Name: string; PreferredPlatform: TPlatformID = piUnicode; PreferredLanguage: Word = 0): integer;
function GetAxisCount: Integer;
function GetAxisTag(Index: Integer): TPascalTypeTag;
function GetAxisName(Index: Integer; PreferredPlatform: TPlatformID = piUnicode; PreferredLanguage: Word = 0): string;
function GetAxisRange(Index: Integer; out MinValue, MaxValue, DefaultValue: Single): Boolean;
function IsAxisVisible(Index: Integer): Boolean;
function GetInstanceCount: Integer;
function GetInstanceName(Index: Integer; PreferredPlatform: TPlatformID = piUnicode; PreferredLanguage: Word = 0): string;
procedure SetInstance(const Name: string; PreferredPlatform: TPlatformID = piUnicode; PreferredLanguage: Word = 0);
property AxisValue[const Tag: TPascalTypeTag]: Single read GetAxisValueByTag write SetAxisValueByTag;
property AxisValueByName[const Name: string]: Single read GetAxisValueByName write SetAxisValueByName;
property AxisValueByIndex[Index: integer]: Single read GetAxisValue write SetAxisValue;
// Basic font properties
property FontFamilyName: string read GetFontFamilyName;
property FontName: string read GetFontName;
property FontStyle: TFontStyles read GetFontStyle;
property FontSubFamilyName: string read GetFontSubFamilyName;
property FontVersion: string read GetFontVersion;
property UniqueIdentifier: string read GetUniqueIdentifier;
// Font metrics
property TypographicAscender: SmallInt read GetTypographicAscender;
property TypographicDescender: SmallInt read GetTypographicDescender;
property TypographicLineGap: SmallInt read GetTypographicLineGap;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeFontFace
//
//------------------------------------------------------------------------------
// Complete font reader (maybe someday also a writer)
//------------------------------------------------------------------------------
TPascalTypeFontFace = class(TCustomPascalTypeFontFace)
public
const MaxCompositeGlyphDepth = 32; // Max recursion depth when resolving composite glyphs
strict private
// Shortcuts to required tables
FHorizontalMetrics: TPascalTypeHorizontalMetricsTable;
FCharacterMap: TPascalTypeCharacterMapTable;
FOS2Table: TPascalTypeOS2Table;
// Only required for vertical
FVerticalMetrics: TPascalTypeVerticalMetricsTable;
// Not required, but pretty important
FGlyphData: TTrueTypeFontGlyphDataTable;
private type
TPathCacheEntry = record
Path: TPascalTypePath;
XMin, YMin, XMax, YMax: SmallInt;
end;
TPathCache = TDictionary<Word, TPathCacheEntry>;
private
FPathCache: TPathCache;
private
function GetGlyphData(Index: Integer): TCustomTrueTypeFontGlyphData;
function GetPanose: TCustomPascalTypePanoseTable;
function GetBoundingBox: TRect;
function GetGlyphCount: Word;
protected
function DoGetGlyphPath(GlyphIndex: Word; Depth: integer): TPascalTypePath;
procedure GetGlyphPathCacheEntry(GlyphIndex: Word; var CacheEntry: TPathCacheEntry);
procedure Loaded; override;
procedure DoClear; override;
procedure DoClearCache; override;
procedure TableAdded(ATable: TCustomPascalTypeNamedTable); override;
public
destructor Destroy; override;
function CreateLayoutEngine: TObject; override;
procedure SaveToStream(Stream: TStream); override;
function GetGlyphMetric(GlyphIndex: Word): TTrueTypeGlyphMetric;
function GetAdvanceWidth(GlyphIndex: Word): Word; override;
function GetAdvanceHeight(GlyphIndex: Word): Word; override;
function GetVerticalOrigin(GlyphIndex: Word): TPoint; override;
protected
function GetTypographicAscender: SmallInt; override;
function GetTypographicDescender: SmallInt; override;
function GetTypographicLineGap: SmallInt; override;
public
function GetKerning(Last, Next: Word): Word;
function GetGlyphByCodePoint(ACodePoint: TPascalTypeCodePoint): Integer; override;
function GetGlyphByCodePoint(ACodePoint, ANextCodePoint: TPascalTypeCodePoint; var AIndex: Integer): Integer; override;
function GetGlyphExtents(GlyphID: TGlyphID; var XMin, YMin, XMax, YMax: SmallInt): Boolean; override;
{$if defined(PhantomPoints)}
function GetGlyphPointCount(GlyphID: TGlyphID): Integer; override;
{$ifend}
function GetGlyphPath(GlyphIndex: Word): TPascalTypePath; // TODO : Use TFloatPoint
property GlyphData[Index: Integer]: TCustomTrueTypeFontGlyphData read GetGlyphData;
// Redirected sub properties
property Panose: TCustomPascalTypePanoseTable read GetPanose;
property BoundingBox: TRect read GetBoundingBox;
property GlyphCount: Word read GetGlyphCount;
// Required tables
property HeaderTable;
property HorizontalHeader;
property MaximumProfile;
property NameTable;
property PostScriptTable;
property GlyphTable: TTrueTypeFontGlyphDataTable read FGlyphData;
property HorizontalMetrics: TPascalTypeHorizontalMetricsTable read FHorizontalMetrics;
property VerticalMetrics: TPascalTypeVerticalMetricsTable read FVerticalMetrics;
property CharacterMap: TPascalTypeCharacterMapTable read FCharacterMap;
property OS2Table: TPascalTypeOS2Table read FOS2Table;
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
implementation
uses
Math,
PascalType.Math,
PascalType.Tables.TrueType,
PascalType.Tables.OpenType.PostScript,
PascalType.Tables.OpenType.PostScript.CFF2,
PascalType.Tables.OpenType.PostScript.VORG,
PascalType.Tables.OpenType.ClassDefinition,
PascalType.Shaper.Layout.OpenType,
PascalType.ResourceStrings;
function CalculateCheckSum(Data: Pointer; Count: Integer): Cardinal; overload;
{$IFDEF PUREPASCAL}
begin
Result := 0;
while (Count > 0) do
begin
{$IFOPT Q+}{$DEFINE Q_PLUS}{$OVERFLOWCHECKS OFF}{$ENDIF}
Result := Result + Swap32(PCardinal(Data)^);
{$IFDEF Q_PLUS}{$OVERFLOWCHECKS ON}{$UNDEF Q_PLUS}{$ENDIF}
Inc(PCardinal(Data));
Dec(Count);
end;
{$ELSE}
asm
MOV ECX, EDX
XOR EDX, EDX
LEA EAX, EAX + ECX * 4
NEG ECX
JNL @Done
PUSH EBX
@Start:
MOV EBX, [EAX + ECX * 4].DWord
BSWAP EBX
ADD EDX, EBX
ADD ECX, 1
JS @Start
POP EBX
@Done:
MOV EAX, EDX
{$ENDIF}
end;
function CalculateCheckSum(Stream: TStream; Size: Cardinal): Cardinal; overload;
begin
// Ensure that at least one cardinal is in the stream
if (Size < 4) then
Exit(0);
Assert(Size <= (Stream.Size - Stream.Position));
// set position to beginning of the stream
// Stream.Position := 0;
if (Stream is TMemoryStream) then
begin
Result := CalculateCheckSum(PByte(TMemoryStream(Stream).Memory) + Stream.Position, Size div SizeOf(Cardinal))
end else
begin
Result := 0;
// read subsequent cardinals
while (Size >= SizeOf(Cardinal)) do
begin
{$IFOPT Q+}{$DEFINE Q_PLUS}{$OVERFLOWCHECKS OFF}{$ENDIF}
Result := Result + BigEndianValue.ReadCardinal(Stream);
{$IFDEF Q_PLUS}{$OVERFLOWCHECKS ON}{$UNDEF Q_PLUS}{$ENDIF}
Dec(Size, SizeOf(Cardinal));
end;
end;
end;
function CalculateCheckSum(Stream: TStream): Cardinal; overload;
begin
Result := CalculateCheckSum(Stream, Stream.Size-Stream.Position);
end;
//------------------------------------------------------------------------------
//
// TFontGlyphString
//
//------------------------------------------------------------------------------
constructor TFontGlyphString.Create(AFont: TCustomPascalTypeFontFace; const ACodePoints: TPascalTypeCodePoints);
begin
Create(AFont, ACodePoints, PascalTypeDefaultClusterLevel);
end;
constructor TFontGlyphString.Create(AFont: TCustomPascalTypeFontFace; const ACodePoints: TPascalTypeCodePoints; AClusterLevel: TClusterLevel);
begin
FFont := AFont;
inherited Create(ACodePoints, AClusterLevel);
end;
function TFontGlyphString.GetGlyphClassID(AGlyph: TPascalTypeGlyph): integer;
begin
if (Font.GlyphDefinitionTable <> nil) and (Font.GlyphDefinitionTable.GlyphClassDefinition <> nil) then
Result := Font.GlyphDefinitionTable.GlyphClassDefinition.ClassByGlyphID(AGlyph.GlyphID)
else
Result := inherited GetGlyphClassID(AGlyph);
// HarfBuzz/USE: Spacing marks (category Mc) are treated as bases.
// This ensures they correctly retain their advance width and are not skipped by IgnoreMarks.
if (Result = GlyphClassMark) then
begin
for var CodePoint in AGlyph.CodePoints do
if (PascalTypeUnicode.IsSpacingMark(CodePoint)) then
begin
Result := GlyphClassBase;
break;
end;
end;
end;
function TFontGlyphString.GetMarkAttachmentType(AGlyph: TPascalTypeGlyph): integer;
begin
if (Font.GlyphDefinitionTable <> nil) and (Font.GlyphDefinitionTable.MarkAttachmentClassDefinition <> nil) then
Result := Font.GlyphDefinitionTable.MarkAttachmentClassDefinition.ClassByGlyphID(AGlyph.GlyphID)
else
Result := inherited GetMarkAttachmentType(AGlyph);
end;
function TFontGlyphString.IsInMarkFilteringSet(AGlyph: TPascalTypeGlyph; ASetIndex: integer): boolean;
begin
if (Font.GlyphDefinitionTable <> nil) and (Font.GlyphDefinitionTable.MarkGlyphSet <> nil) then
Result := Font.GlyphDefinitionTable.MarkGlyphSet.IsInSet(ASetIndex, AGlyph.GlyphID)
else
Result := inherited IsInMarkFilteringSet(AGlyph, ASetIndex);
end;
function TFontGlyphString.GetGlyphByCodePoint(ACodePoint: TPascalTypeCodePoint): Integer;
begin
Result := Font.GetGlyphByCodePoint(ACodePoint);
end;
class function TFontGlyphString.GetGlyphClass: TPascalTypeGlyphClass;
begin
Result := TFontGlyph;
end;
procedure TFontGlyphString.HideDefaultIgnorables;
begin
if (sfRemoveDefaultIgnorables in Options) then
inherited
else
if (sfHideDefaultIgnorables in Options) then
begin
var i := 0;
while (i < Count) do
begin
var Glyph := Glyphs[i];
// Only hide if the glyph has not been substituted.
// If the font has a substitution for a default ignorable (e.g. U+200D to a specific glyph via 'rlig'),
// we want to keep that substituted glyph.
if (not Glyph.IsSubstituted) and (PascalTypeUnicode.IsDefaultIgnorableEx(Glyph.CodePoint)) then
begin
if (InvisibleGlyph <> GlyphNotDef) then
begin
Glyph.GlyphID := InvisibleGlyph;
Glyph.XAdvance := 0;
Glyph.YAdvance := 0;
Inc(i);
end else
begin
// Since the space character doesn't exist in the font we cannot use it
// to replace default ignorables. Instead we simply delete them.
// This matches the behavior of Harfbuzz (see: hb_ot_hide_default_ignorables).
Delete(i);
// Don't increment i because current index now points to next glyph
end;
end else
Inc(i);
end;
end;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeTableRoot
//
//------------------------------------------------------------------------------
// Root table. Just used to provide access to the font from the tables.
//------------------------------------------------------------------------------
type
TPascalTypeTableRoot = class(TCustomPascalTypeTable)
private
FFontFace: IPascalTypeFontFace;
protected
function GetFontFace: IPascalTypeFontFace; override;
public
constructor Create(const AFontFace: IPascalTypeFontFace); reintroduce;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
end;
constructor TPascalTypeTableRoot.Create(const AFontFace: IPascalTypeFontFace);
begin
inherited Create;
FFontFace := AFontFace;
end;
function TPascalTypeTableRoot.GetFontFace: IPascalTypeFontFace;
begin
Result := FFontFace;
end;
procedure TPascalTypeTableRoot.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
raise EPascalTypeError.Create(RCStrNotImplemented);
end;
procedure TPascalTypeTableRoot.SaveToStream(Stream: TStream);
begin
raise EPascalTypeError.Create(RCStrNotImplemented);
end;
{ TFontGlyphString }
procedure TFontGlyphString.ApplyVariation(AGlyph: TPascalTypeGlyph; const AValueRecord: TOpenTypeValueRecord);
begin
if (Font = nil) or (not Font.VariationsActive) then
exit;
if (AValueRecord.xPlaVarIndex <> $FFFFFFFF) then
AGlyph.XOffset := AGlyph.XOffset + Round(Font.GetVariationItemDelta(AValueRecord.xPlaVarIndex shr 16, AValueRecord.xPlaVarIndex and $FFFF));
if (AValueRecord.yPlaVarIndex <> $FFFFFFFF) then
AGlyph.YOffset := AGlyph.YOffset + Round(Font.GetVariationItemDelta(AValueRecord.yPlaVarIndex shr 16, AValueRecord.yPlaVarIndex and $FFFF));
if (AValueRecord.xAdvVarIndex <> $FFFFFFFF) then
AGlyph.XAdvance := AGlyph.XAdvance + Round(Font.GetVariationItemDelta(AValueRecord.xAdvVarIndex shr 16, AValueRecord.xAdvVarIndex and $FFFF));
if (AValueRecord.yAdvVarIndex <> $FFFFFFFF) then
AGlyph.YAdvance := AGlyph.YAdvance + Round(Font.GetVariationItemDelta(AValueRecord.yAdvVarIndex shr 16, AValueRecord.yAdvVarIndex and $FFFF));
end;
//------------------------------------------------------------------------------
//
// TCustomPascalTypeFontFace
//
//------------------------------------------------------------------------------
constructor TCustomPascalTypeFontFace.Create;
begin
inherited;
FRootTable := TPascalTypeTableRoot.Create(Self);
FTables := TTableList.Create;
FTableLookup := TTableLookup.Create;
end;
destructor TCustomPascalTypeFontFace.Destroy;
begin
FVariationEngine.Free;
FTableLookup.Free;
FTables.Free;
FRootTable.Free;
inherited;
end;
function TCustomPascalTypeFontFace.GetHasVariations: boolean;
begin
Result := (FVariationTable <> nil);
end;
function TCustomPascalTypeFontFace.GetVariationsActive: boolean;
begin
Result := (FVariationEngine <> nil) and (FVariationEngine.Active);
end;
function TCustomPascalTypeFontFace.GetVariationItemDelta(OuterIndex, InnerIndex: Word): Single;
begin
if (HasVariations) then
Result := VariationEngine.GetItemDelta(OuterIndex, InnerIndex)
else
Result := 0;
end;
function TCustomPascalTypeFontFace.GetVariationMetricDelta(const Tag: TPascalTypeTag): Single;
begin
if (HasVariations) then
Result := VariationEngine.GetMetricDelta(Tag)
else
Result := 0;
end;
function TCustomPascalTypeFontFace.GetVariationNormalizedCoordinates: TArray<TFixedPoint>;
begin
if (HasVariations) then
Result := VariationEngine.NormalizedCoordinates
else
Result := nil;
end;
function TCustomPascalTypeFontFace.GetVariationCoordinates: TArray<TFixedPoint>;
begin
if (HasVariations) then
Result := VariationEngine.UserCoordinates
else
Result := nil;
end;
procedure TCustomPascalTypeFontFace.SetVariationCoordinates(const Value: TArray<TFixedPoint>);
begin
if (HasVariations) then
begin
VariationEngine.SetUserCoordinates(Value);
ClearCache; // Clear caches when variations change
end;
end;
function TCustomPascalTypeFontFace.GetVariationEngine: TPascalTypeVariationEngine;
begin
if (FVariationEngine = nil) and (FVariationTable <> nil) then
FVariationEngine := TPascalTypeVariationEngine.Create(Self);
Result := FVariationEngine;
end;
function TCustomPascalTypeFontFace.FindAxisByTag(const Tag: TPascalTypeTag): integer;
begin
if (HasVariations) then
Result := VariationEngine.FindAxisByTag(Tag)
else
Result := -1;
end;
function TCustomPascalTypeFontFace.FindAxisByName(const Name: string; PreferredPlatform: TPlatformID; PreferredLanguage: Word): integer;
begin
if (HasVariations) then
Result := VariationEngine.FindAxisByName(Name, PreferredPlatform, PreferredLanguage)
else
Result := -1;
end;
function TCustomPascalTypeFontFace.GetAxisCount: Integer;
begin
if (FVariationTable <> nil) then
Result := FVariationTable.AxisCount
else
Result := 0;
end;
function TCustomPascalTypeFontFace.GetAxisTag(Index: Integer): TPascalTypeTag;
begin
if (FVariationTable <> nil) and (Index >= 0) and (Index < FVariationTable.AxisCount) then
Result := FVariationTable.Axis[Index].AxisTag
else
Result := 0;
end;
function TCustomPascalTypeFontFace.GetAxisName(Index: Integer; PreferredPlatform: TPlatformID; PreferredLanguage: Word): string;
begin
if (FVariationTable <> nil) and (Index >= 0) and (Index < FVariationTable.AxisCount) then
begin
if NameTable.TryGetName(TNameID(FVariationTable.Axis[Index].NameID), PreferredPlatform, PreferredLanguage, Result) then
Exit;
if (PreferredPlatform <> piUnicode) then
begin
if NameTable.TryGetName(TNameID(FVariationTable.Axis[Index].NameID), piUnicode, 0, Result) then
Exit;
end;
if (PreferredPlatform <> piMicrosoft) and (PreferredLanguage <> 1033) then
begin
if NameTable.TryGetName(TNameID(FVariationTable.Axis[Index].NameID), piMicrosoft, 1033, Result) then
Exit;
end;
end;
Result := '';
end;
function TCustomPascalTypeFontFace.GetAxisRange(Index: Integer; out MinValue, MaxValue, DefaultValue: Single): Boolean;
begin
if (FVariationTable <> nil) and (Index >= 0) and (Index < FVariationTable.AxisCount) then
begin
MinValue := FVariationTable.Axis[Index].MinValue.AsSingle;
MaxValue := FVariationTable.Axis[Index].MaxValue.AsSingle;
DefaultValue := FVariationTable.Axis[Index].DefaultValue.AsSingle;
Result := True;
end else
begin
MinValue := 0;
MaxValue := 0;
DefaultValue := 0;
Result := False;
end;
end;
function TCustomPascalTypeFontFace.IsAxisVisible(Index: Integer): Boolean;
const
HIDDEN_AXIS = $0001;
begin
if (FVariationTable <> nil) and (Index >= 0) and (Index < FVariationTable.AxisCount) then
Result := (FVariationTable.Axis[Index].Flags and HIDDEN_AXIS) = 0
else
Result := False;
end;
function TCustomPascalTypeFontFace.GetInstanceCount: Integer;
begin
if (FVariationTable <> nil) then
Result := FVariationTable.InstanceCount
else
Result := 0;
end;
function TCustomPascalTypeFontFace.GetInstanceName(Index: Integer; PreferredPlatform: TPlatformID; PreferredLanguage: Word): string;
begin
if (FVariationTable <> nil) and (Index >= 0) and (Index < FVariationTable.InstanceCount) then
begin
if NameTable.TryGetName(TNameID(FVariationTable.VariationInstance[Index].NameID), PreferredPlatform, PreferredLanguage, Result) then
Exit;
if (PreferredPlatform <> piUnicode) then
begin
if NameTable.TryGetName(TNameID(FVariationTable.VariationInstance[Index].NameID), piUnicode, 0, Result) then
Exit;
end;
if (PreferredPlatform <> piMicrosoft) and (PreferredLanguage <> 1033) then
begin
if NameTable.TryGetName(TNameID(FVariationTable.VariationInstance[Index].NameID), piMicrosoft, 1033, Result) then
Exit;
end;
end;
Result := '';
end;
procedure TCustomPascalTypeFontFace.SetInstance(const Name: string; PreferredPlatform: TPlatformID; PreferredLanguage: Word);
begin
if (HasVariations) then
begin
VariationEngine.SetInstance(Name, PreferredPlatform, PreferredLanguage);
ClearCache;
end;
end;
function TCustomPascalTypeFontFace.GetAxisValue(Index: integer): Single;
begin
if (HasVariations) then
Result := VariationEngine.GetAxisValue(Index)
else
Result := 0;
end;
procedure TCustomPascalTypeFontFace.SetAxisValue(Index: integer; Value: Single);
begin
if (HasVariations) then
begin
VariationEngine.SetAxisValue(Index, Value);
ClearCache;
end;
end;
function TCustomPascalTypeFontFace.GetAxisValueByTag(const Tag: TPascalTypeTag): Single;
begin
if (HasVariations) then
Result := VariationEngine.GetAxisValue(Tag)
else
Result := 0;
end;
procedure TCustomPascalTypeFontFace.SetAxisValueByTag(const Tag: TPascalTypeTag; Value: Single);
begin
if (HasVariations) then
begin
VariationEngine.SetAxisValue(Tag, Value);
ClearCache;
end;
end;
function TCustomPascalTypeFontFace.GetAxisValueByName(const Name: string): Single;
begin
if (HasVariations) then
Result := VariationEngine.GetAxisValue(Name)
else
Result := 0;
end;
procedure TCustomPascalTypeFontFace.SetAxisValueByName(const Name: string; Value: Single);
begin
if (HasVariations) then
begin
VariationEngine.SetAxisValue(Name, Value);
ClearCache;
end;
end;
function TCustomPascalTypeFontFace.GetTable(Index: integer): TCustomPascalTypeNamedTable;
begin
Result := FTables[Index];
end;
function TCustomPascalTypeFontFace.GetTableCount: Integer;
begin
Result := FTables.Count;
end;
procedure TCustomPascalTypeFontFace.DirectoryTableLoaded(DirectoryTable: TPascalTypeDirectoryTable);
begin
if (Version = sfntVersionAppleTT) then
begin
if (not DirectoryTable.Contains(ttLoca)) then
raise EPascalTypeError.Create(RCStrNoIndexToLocationTable);
if (not DirectoryTable.Contains(ttGlyf)) then
raise EPascalTypeError.Create(RCStrNoGlyphDataTable);
end;
end;
procedure TCustomPascalTypeFontFace.TableAdded(ATable: TCustomPascalTypeNamedTable);
begin
case ATable.TableType.AsCardinal of
ttHead:
FHeaderTable := ATable as TPascalTypeHeaderTable;
ttMaxp:
FMaximumProfile := ATable as TPascalTypeMaximumProfileTable;
ttHhea:
FHorizontalHeader := ATable as TPascalTypeHorizontalHeaderTable;
ttVhea:
FVerticalHeader := ATable as TPascalTypeVerticalHeaderTable;
ttName:
FNameTable := ATable as TPascalTypeNameTable;
ttPost:
FPostScriptTable := ATable as TPascalTypePostscriptTable;
ttGDEF:
FGlyphDefinitionTable := ATable as TOpenTypeGlyphDefinitionTable;
ttFvar:
FVariationTable := ATable as TPascalTypeFontVariationTable;
ttMVAR:
FMetricsVariationTable := ATable as TPascalTypeMetricsVariationTable;
ttSTAT:
FStyleAttributesTable := ATable as TPascalTypeStyleAttributesTable;
ttCFF:
FCFFTable := ATable as TPascalTypeCompactFontFormatTable;
end;
end;
procedure TCustomPascalTypeFontFace.AddTable(ATable: TCustomPascalTypeNamedTable);
begin
// Add to lookup first in case there's a duplicate table type
TableLookup.Add(ATable.TableType, ATable);
// Transfer ownership to list
FTables.Add(ATable);
try
TableAdded(ATable);
except
TableLookup.Remove(ATable.TableType);
FTables.Extract(ATable);
raise;
end;
end;
function TCustomPascalTypeFontFace.AddTable(const ATableClass: TCustomPascalTypeNamedTableClass): TCustomPascalTypeNamedTable;
begin
Result := ATableClass.Create(RootTable);
try
AddTable(Result);
except
Result.Free;
raise;
end;
end;
function TCustomPascalTypeFontFace.AddTable(const ATableType: TPascalTypeTag): TCustomPascalTypeNamedTable;
var