-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalType.Tables.TrueType.glyf.pas
More file actions
1480 lines (1235 loc) · 46.8 KB
/
PascalType.Tables.TrueType.glyf.pas
File metadata and controls
1480 lines (1235 loc) · 46.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
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.Tables.TrueType.glyf;
////////////////////////////////////////////////////////////////////////////////
// //
// 'glyf' table type //
// //
////////////////////////////////////////////////////////////////////////////////
// //
// 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,
Classes,
PascalType.Types,
PascalType.Classes,
PascalType.Tables;
//------------------------------------------------------------------------------
//
// TTrueTypeFontGlyphInstructionTable
//
//------------------------------------------------------------------------------
// TrueType instructions
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/tt_instructing_glyphs
//------------------------------------------------------------------------------
type
TTrueTypeFontGlyphInstructionTable = class(TCustomPascalTypeTable)
private
FInstructions: array of Byte;
function GetInstruction(Index: Integer): Byte;
function GetInstructionCount: Integer;
public
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
property Instruction[Index: Integer]: Byte read GetInstruction;
property InstructionCount: Integer read GetInstructionCount;
end;
//------------------------------------------------------------------------------
//
// TCustomTrueTypeFontGlyphData
//
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/glyf#glyph-headers
//------------------------------------------------------------------------------
// Base class for simple and composite glyphs
//------------------------------------------------------------------------------
type
TCustomTrueTypeFontGlyphData = class abstract(TCustomPascalTypeTable)
private
procedure SetNumberOfContours(const Value: SmallInt);
procedure SetXMax(const Value: SmallInt);
procedure SetXMin(const Value: SmallInt);
procedure SetYMax(const Value: SmallInt);
procedure SetYMin(const Value: SmallInt);
function GetGlyphIndex: Integer;
protected
FNumberOfContours: SmallInt; // If the number of contours is greater than or equal to zero, this is a single glyph; if negative, this is a composite glyph.
FXMin : SmallInt; // Minimum x for coordinate data.
FYMin : SmallInt; // Minimum y for coordinate data.
FXMax : SmallInt; // Maximum x for coordinate data.
FYMax : SmallInt; // Maximum y for coordinate data.
FInstructions: TTrueTypeFontGlyphInstructionTable;
function GetIsComposite: boolean; virtual; abstract;
function GetContourCount: Integer; virtual; abstract;
function GetGlyphCount: Integer; virtual; abstract;
procedure GlyphIndexChanged; virtual;
procedure NumberOfContoursChanged; virtual;
procedure XMaxChanged; virtual;
procedure XMinChanged; virtual;
procedure YMaxChanged; virtual;
procedure YMinChanged; virtual;
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;
property IsComposite: boolean read GetIsComposite;
// Number of glyphs in this glyph data; Always 1 for simple glyphs
property GlyphCount: Integer read GetGlyphCount;
property NumberOfContours: SmallInt read FNumberOfContours write SetNumberOfContours;
property XMin: SmallInt read FXMin write SetXMin;
property YMin: SmallInt read FYMin write SetYMin;
property XMax: SmallInt read FXMax write SetXMax;
property YMax: SmallInt read FYMax write SetYMax;
// Index of glyph in the 'glyf' table - Expensive!
property GlyphIndex: Integer read GetGlyphIndex;
property Instructions: TTrueTypeFontGlyphInstructionTable read FInstructions;
// ContourCount: Count of contours in leaf (simple) glyphs
property ContourCount: Integer read GetContourCount;
end;
TTrueTypeFontGlyphDataClass = class of TCustomTrueTypeFontGlyphData;
//------------------------------------------------------------------------------
//
// TPascalTypeTrueTypeContour
//
//------------------------------------------------------------------------------
// Glyph contour: A list of points
//------------------------------------------------------------------------------
type
TPascalTypeTrueTypeContour = class(TPersistent)
private
FPoints: TPascalTypeContour;
function GetPoint(Index: Integer): TContourPoint;
function GetPointCount: Integer;
procedure SetPoint(Index: Integer; const Value: TContourPoint);
procedure SetPointCount(const Value: Integer);
function GetIsClockwise: Boolean;
function GetArea: Single;
protected
procedure PointCountChanged; virtual;
property Points: TPascalTypeContour read FPoints;
public
procedure Assign(Source: TPersistent); override;
property Area : Single read GetArea;
property IsClockwise : Boolean read GetIsClockwise;
// Note: Point[PointCount] by design wraps around and returns Point[0]
property Point[Index: Integer]: TContourPoint read GetPoint write SetPoint;
property PointCount: Integer read GetPointCount write SetPointCount;
end;
//------------------------------------------------------------------------------
//
// TTrueTypeFontSimpleGlyphData
//
//------------------------------------------------------------------------------
// Simple glyph
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/glyf#simple-glyph-description
//------------------------------------------------------------------------------
type
TTrueTypeFontSimpleGlyphData = class(TCustomTrueTypeFontGlyphData)
public
const
GLYF_ON_CURVE = $01;
GLYF_X_SHORT_VECTOR = $02;
GLYF_Y_SHORT_VECTOR = $04;
GLYF_REPEAT_FLAG = $08;
GLYF_X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR = $10;
GLYF_Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR = $20;
GLYF_OVERLAP_SIMPLE = $40;
GLYF_RESERVED8 = $80;
GLYF_RESERVED = GLYF_RESERVED8;
strict private
FPath: TList<TPascalTypeTrueTypeContour>;
private
function GetContour(Index: Integer): TPascalTypeTrueTypeContour;
function GetPath: TPascalTypePath;
protected
function GetIsComposite: boolean; override;
function GetContourCount: Integer; override;
function GetGlyphCount: Integer; override;
public
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
property Contour[Index: Integer]: TPascalTypeTrueTypeContour read GetContour;
property Path: TPascalTypePath read GetPath;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeCompositeGlyph
//
//------------------------------------------------------------------------------
// Composite glyph component
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/glyf#composite-glyph-description
//------------------------------------------------------------------------------
type
TPascalTypeCompositeGlyph = class(TCustomPascalTypeTable)
public
const
GLYF_ARG_1_AND_2_ARE_WORDS = $0001;
GLYF_ARGS_ARE_XY_VALUES = $0002;
GLYF_ROUND_XY_TO_GRID = $0004;
GLYF_WE_HAVE_A_SCALE = $0008;
GLYF_RESERVED5 = $0010;
GLYF_MORE_COMPONENTS = $0020;
GLYF_WE_HAVE_AN_X_AND_Y_SCALE = $0040;
GLYF_WE_HAVE_A_TWO_BY_TWO = $0080;
GLYF_WE_HAVE_INSTRUCTIONS = $0100;
GLYF_USE_MY_METRICS = $0200;
GLYF_OVERLAP_COMPOUND = $0400;
GLYF_SCALED_COMPONENT_OFFSET = $0800;
GLYF_UNSCALED_COMPONENT_OFFSET = $1000;
GLYF_RESERVED13 = $2000;
GLYF_RESERVED14 = $4000;
GLYF_RESERVED15 = $8000;
GLYF_RESERVED = GLYF_RESERVED5 or GLYF_RESERVED13 or GLYF_RESERVED14 or GLYF_RESERVED15;
private
FFlags: Word; // Component flag
FGlyphIndex: Word; // Glyph index of component
FOffsetXY: array[0..1] of SmallInt;
FPointIndex: array[0..1] of Word;
FAffineTransformationMatrix: TSmallScaleMatrix;
procedure SetFlags(const Value: Word);
procedure SetGlyphIndex(const Value: Word);
function GetHasAffineTransformationMatrix: boolean;
function GetArgsAreOffset: boolean;
function GetArgsArePointIndex: boolean;
protected
procedure FlagsChanged; virtual;
procedure GlyphIndexChanged; virtual;
function FlagMoreComponents: boolean;
function FlagHasInstructions: boolean;
function FlagHasAffineTransformationMatrix: boolean;
function FlagArgsAreOffset: boolean;
public
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
property Flags: Word read FFlags write SetFlags;
property GlyphIndex: Word read FGlyphIndex write SetGlyphIndex;
property HasOffset: boolean read GetArgsAreOffset;
property HasGlyphPointIndex: boolean read GetArgsArePointIndex;
property OffsetX: SmallInt read FOffsetXY[0];
property OffsetY: SmallInt read FOffsetXY[1];
property ParentGlyphPointIndex: Word read FPointIndex[1];
property ChildGlyphPointIndex: Word read FPointIndex[0];
property HasAffineTransformationMatrix: boolean read GetHasAffineTransformationMatrix;
property AffineTransformationMatrix: TSmallScaleMatrix read FAffineTransformationMatrix write FAffineTransformationMatrix;
end;
//------------------------------------------------------------------------------
//
// TTrueTypeFontCompositeGlyphData
//
//------------------------------------------------------------------------------
// Composite glyph component collection
//------------------------------------------------------------------------------
type
TTrueTypeFontCompositeGlyphData = class(TCustomTrueTypeFontGlyphData)
private
FGlyphs: TList<TPascalTypeCompositeGlyph>;
function GetCompositeGlyph(Index: Integer): TPascalTypeCompositeGlyph;
protected
function GetIsComposite: boolean; override;
function GetContourCount: Integer; override;
function GetGlyphCount: Integer; override;
public
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
function Add: TPascalTypeCompositeGlyph;
property Glyph[Index: Integer]: TPascalTypeCompositeGlyph read GetCompositeGlyph;
end;
//------------------------------------------------------------------------------
//
// TTrueTypeFontGlyphDataTable
//
//------------------------------------------------------------------------------
// The 'glyf' table - A collection of glyphs
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/glyf
//------------------------------------------------------------------------------
type
TTrueTypeFontGlyphDataTable = class(TCustomPascalTypeNamedTable)
strict private
FGlyphDataList: TList<TCustomTrueTypeFontGlyphData>;
private
function GetGlyphDataCount: Integer;
function GetGlyphData(Index: Integer): TCustomTrueTypeFontGlyphData;
public
destructor Destroy; override;
class function GetTableType: TTableType; override;
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
function Add(AGlyphDataClass: TTrueTypeFontGlyphDataClass): TCustomTrueTypeFontGlyphData;
property GlyphDataCount: Integer read GetGlyphDataCount;
property GlyphData[Index: Integer]: TCustomTrueTypeFontGlyphData read GetGlyphData;
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
implementation
uses
SysUtils,
PascalType.FontFace.SFNT,
PascalType.ResourceStrings,
PascalType.Tables.TrueType,
PascalType.Tables.TrueType.maxp;
//------------------------------------------------------------------------------
//
// TTrueTypeFontGlyphInstructionTable
//
//------------------------------------------------------------------------------
procedure TTrueTypeFontGlyphInstructionTable.Assign(Source: TPersistent);
begin
inherited;
if Source is TTrueTypeFontGlyphInstructionTable then
FInstructions := Copy(TTrueTypeFontGlyphInstructionTable(Source).FInstructions);
end;
function TTrueTypeFontGlyphInstructionTable.GetInstruction
(Index: Integer): Byte;
begin
if (Index > High(FInstructions)) then
raise EPascalTypeError.CreateFmt(RCStrIndexOutOfBounds, [Index]);
Result := FInstructions[Index];
end;
function TTrueTypeFontGlyphInstructionTable.GetInstructionCount: Integer;
begin
Result := Length(FInstructions);
end;
procedure TTrueTypeFontGlyphInstructionTable.LoadFromStream(Stream: TStream; Size: Cardinal);
var
Value16 : Word;
MaxProfile: TPascalTypeMaximumProfileTable;
begin
MaxProfile := TPascalTypeMaximumProfileTable(FontFace.GetTableByTableName('maxp'));
Assert(MaxProfile <> nil);
// read instruction size
Value16 := BigEndianValue.ReadWord(Stream);
// check if instructions shall be ignored
if Value16 = $FFFF then
Exit;
{$IFDEF WarningExceptions}
// check if too many instuctions are present -> possible stream error
if Value16 > MaxProfile.MaxSizeOfInstruction then
// ... but probably just an error in the font
raise EPascalTypeError.CreateFmt(RCStrTooManyInstructions, [Value16]);
{$ENDIF}
// set instruction length
SetLength(FInstructions, Value16);
// read instructions
if (Value16 > 0) then
Stream.Read(FInstructions[0], Length(FInstructions));
end;
procedure TTrueTypeFontGlyphInstructionTable.SaveToStream(Stream: TStream);
begin
// write instruction size
BigEndianValue.WriteWord(Stream, Length(FInstructions));
// write instructions
Stream.Write(FInstructions[0], Length(FInstructions));
end;
//------------------------------------------------------------------------------
//
// TCustomTrueTypeFontGlyphData
//
//------------------------------------------------------------------------------
constructor TCustomTrueTypeFontGlyphData.Create(AParent: TCustomPascalTypeTable);
begin
inherited;
FInstructions := TTrueTypeFontGlyphInstructionTable.Create(Self);
end;
destructor TCustomTrueTypeFontGlyphData.Destroy;
begin
FreeAndNil(FInstructions);
inherited;
end;
function TCustomTrueTypeFontGlyphData.GetGlyphIndex: Integer;
var
GlyphDataTable: TTrueTypeFontGlyphDataTable;
i: Integer;
begin
GlyphDataTable := TTrueTypeFontGlyphDataTable(FontFace.GetTableByTableName('glyf'));
Result := -1;
if (GlyphDataTable = nil) then
exit;
for i := 0 to GlyphDataTable.GlyphDataCount - 1 do
if GlyphDataTable.GlyphData[i] = Self then
Exit(i);
end;
procedure TCustomTrueTypeFontGlyphData.GlyphIndexChanged;
begin
Changed;
end;
procedure TCustomTrueTypeFontGlyphData.Assign(Source: TPersistent);
begin
inherited;
if Source is TCustomTrueTypeFontGlyphData then
begin
FNumberOfContours := TCustomTrueTypeFontGlyphData(Source).FNumberOfContours;
FXMin := TCustomTrueTypeFontGlyphData(Source).FXMin;
FYMin := TCustomTrueTypeFontGlyphData(Source).FYMin;
FXMax := TCustomTrueTypeFontGlyphData(Source).FXMax;
FYMax := TCustomTrueTypeFontGlyphData(Source).FYMax;
end;
end;
procedure TCustomTrueTypeFontGlyphData.LoadFromStream(Stream: TStream; Size: Cardinal);
var
MaxProfile: TPascalTypeMaximumProfileTable;
begin
// get maximum profile
MaxProfile := TPascalTypeMaximumProfileTable(FontFace.GetTableByTableClass(TPascalTypeMaximumProfileTable));
if Stream.Position + SizeOf(SmallInt) > Stream.Size then
raise EPascalTypeTableIncomplete.Create(RCStrTableIncomplete);
// read number of contours
FNumberOfContours := BigEndianValue.ReadSmallInt(Stream);
// check if maximum number of contours are exceeded
if (FNumberOfContours > 0) and (Word(FNumberOfContours) > MaxProfile.MaxContours) then
raise EPascalTypeError.CreateFmt(RCStrTooManyContours, [FNumberOfContours, MaxProfile.MaxContours]);
// check if glyph contains any information at all
if FNumberOfContours = 0 then
Exit;
if Stream.Position + 4*SizeOf(SmallInt) > Stream.Size then
raise EPascalTypeTableIncomplete.Create(RCStrTableIncomplete);
// read XMin
FXMin := BigEndianValue.ReadSmallInt(Stream);
// read YMin
FYMin := BigEndianValue.ReadSmallInt(Stream);
// read XMax
FXMax := BigEndianValue.ReadSmallInt(Stream);
// read YMax
FYMax := BigEndianValue.ReadSmallInt(Stream);
// Assert(FXMin <= FXMax);
// Assert(FYMin <= FYMax);
end;
procedure TCustomTrueTypeFontGlyphData.SaveToStream(Stream: TStream);
begin
// write number of contours
BigEndianValue.WriteWord(Stream, FNumberOfContours);
// write XMin
BigEndianValue.WriteWord(Stream, FXMin);
// write YMin
BigEndianValue.WriteWord(Stream, FYMin);
// write XMax
BigEndianValue.WriteWord(Stream, FXMax);
// write YMax
BigEndianValue.WriteWord(Stream, FYMax);
end;
procedure TCustomTrueTypeFontGlyphData.SetNumberOfContours(const Value: SmallInt);
begin
if FNumberOfContours <> Value then
begin
FNumberOfContours := Value;
NumberOfContoursChanged;
end;
end;
procedure TCustomTrueTypeFontGlyphData.SetXMax(const Value: SmallInt);
begin
if FXMax <> Value then
begin
FXMax := Value;
XMaxChanged;
end;
end;
procedure TCustomTrueTypeFontGlyphData.SetXMin(const Value: SmallInt);
begin
if FXMin <> Value then
begin
FXMin := Value;
XMinChanged;
end;
end;
procedure TCustomTrueTypeFontGlyphData.SetYMax(const Value: SmallInt);
begin
if FYMax <> Value then
begin
FYMax := Value;
YMaxChanged;
end;
end;
procedure TCustomTrueTypeFontGlyphData.SetYMin(const Value: SmallInt);
begin
if FYMin <> Value then
begin
FYMin := Value;
YMinChanged;
end;
end;
procedure TCustomTrueTypeFontGlyphData.NumberOfContoursChanged;
begin
Changed;
end;
procedure TCustomTrueTypeFontGlyphData.XMaxChanged;
begin
Changed;
end;
procedure TCustomTrueTypeFontGlyphData.XMinChanged;
begin
Changed;
end;
procedure TCustomTrueTypeFontGlyphData.YMaxChanged;
begin
Changed;
end;
procedure TCustomTrueTypeFontGlyphData.YMinChanged;
begin
Changed;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeTrueTypeContour
//
//------------------------------------------------------------------------------
procedure TPascalTypeTrueTypeContour.Assign(Source: TPersistent);
begin
inherited;
if Source is TPascalTypeTrueTypeContour then
// Note: Dynamic arrays are reference types. This just copies the pointer.
// See SetPoint.
FPoints := TPascalTypeTrueTypeContour(Source).Points;
end;
function TPascalTypeTrueTypeContour.GetArea: Single;
var
i: Integer;
begin
if Length(FPoints) < 3 then
Exit(0);
// Shoelace formula
Result := 0;
for i := 0 to High(FPoints) - 1 do
Result := Result + (FPoints[i].XPos * FPoints[i + 1].YPos - FPoints[i + 1].XPos * FPoints[i].YPos);
// Add the last segment connecting back to the start
Result := Result + (FPoints[High(FPoints)].XPos * FPoints[0].YPos - FPoints[0].XPos * FPoints[High(FPoints)].YPos);
Result := Result * 0.5;
end;
function TPascalTypeTrueTypeContour.GetIsClockwise: Boolean;
begin
Result := (Area >= 0);
end;
function TPascalTypeTrueTypeContour.GetPoint(Index: Integer): TContourPoint;
begin
if (Index >= 0) and (Index <= High(FPoints)) then
Result := FPoints[Index]
else
if (Index = Length(FPoints)) then
Result := FPoints[0] // Wrap around to first
else
raise EPascalTypeError.CreateFmt(RCStrIndexOutOfBounds, [Index]);
end;
procedure TPascalTypeTrueTypeContour.SetPoint(Index: Integer; const Value: TContourPoint);
begin
if (Index < 0) or (Index > High(FPoints)) then
raise EPascalTypeError.CreateFmt(RCStrIndexOutOfBounds, [Index]);
// Ensure that our array has a reference count of 1 (in case it was copied with Assign)
SetLength(FPoints, Length(FPoints));
FPoints[Index] := Value;
end;
function TPascalTypeTrueTypeContour.GetPointCount: Integer;
begin
Result := Length(FPoints);
end;
procedure TPascalTypeTrueTypeContour.SetPointCount(const Value: Integer);
begin
if Value <> Length(FPoints) then
begin
SetLength(FPoints, Value);
PointCountChanged;
end;
end;
procedure TPascalTypeTrueTypeContour.PointCountChanged;
begin
// TODO: PointCountChanged;
end;
//------------------------------------------------------------------------------
//
// TTrueTypeFontSimpleGlyphData
//
//------------------------------------------------------------------------------
destructor TTrueTypeFontSimpleGlyphData.Destroy;
begin
FPath.Free;
inherited;
end;
procedure TTrueTypeFontSimpleGlyphData.Assign(Source: TPersistent);
begin
inherited;
if Source is TTrueTypeFontSimpleGlyphData then
begin
if (FPath <> nil) then
FPath.Clear;
if (TTrueTypeFontSimpleGlyphData(Source).ContourCount = 0) then
exit;
if (FPath = nil) then
FPath := TObjectList<TPascalTypeTrueTypeContour>.Create;
FPath.Capacity := TTrueTypeFontSimpleGlyphData(Source).FPath.Count;
for var i := 0 to TTrueTypeFontSimpleGlyphData(Source).FPath.Count-1 do
begin
var Contour := TPascalTypeTrueTypeContour.Create;
FPath.Add(Contour);
Contour.Assign(TTrueTypeFontSimpleGlyphData(Source).FPath.List[i]);
end;
end;
end;
function TTrueTypeFontSimpleGlyphData.GetContour(Index: Integer): TPascalTypeTrueTypeContour;
begin
Result := FPath[Index];
end;
function TTrueTypeFontSimpleGlyphData.GetContourCount: Integer;
begin
if (FPath <> nil) then
Result := FPath.Count
else
Result := 0;
end;
function TTrueTypeFontSimpleGlyphData.GetGlyphCount: Integer;
begin
Result := 1;
end;
function TTrueTypeFontSimpleGlyphData.GetIsComposite: boolean;
begin
Result := False;
end;
function TTrueTypeFontSimpleGlyphData.GetPath: TPascalTypePath;
begin
if (FPath = nil) or (FPath.Count = 0) then
exit; // Dynamic array is already initialized to "empty"
SetLength(Result, FPath.Count);
for var i := 0 to FPath.Count-1 do
Result[i] := FPath[i].Points;
end;
procedure TTrueTypeFontSimpleGlyphData.LoadFromStream(Stream: TStream; Size: Cardinal);
var
ContourIndex: Integer;
PointIndex : Integer;
PointCount : Integer;
LastPoint : Integer;
Contour : TPascalTypeTrueTypeContour;
MaxProfile : TPascalTypeMaximumProfileTable;
EndPointIndexOfContour: array of SmallInt;
Flag : Byte;
FlagCount : Byte;
Value8 : Byte;
ContourPoint: PContourPoint;
begin
inherited;
// Check if glyph contains any information at all
if FNumberOfContours = 0 then
begin
if (FPath <> nil) then
FPath.Clear;
Exit;
end;
// Get maximum profile
MaxProfile := TPascalTypeMaximumProfileTable(FontFace.GetTableByTableClass(TPascalTypeMaximumProfileTable));
// set end points of contours array size
SetLength(EndPointIndexOfContour, FNumberOfContours);
// Read end points
PointCount := -1;
var LastEndPoint: Integer := -1;
for ContourIndex := 0 to FNumberOfContours - 1 do
begin
// read number of contours
PointCount := BigEndianValue.ReadWord(Stream);
EndPointIndexOfContour[ContourIndex] := PointCount;
if PointCount <= LastEndPoint then
raise EPascalTypeError.Create(RCStrInvalidGlyphData);
LastEndPoint := PointCount;
end;
// Increase last end point to get the true point count
Inc(PointCount);
// Check if maximum points are exceeded
if (PointCount > MaxProfile.MaxPoints) then
raise EPascalTypeError.CreateFmt(RCStrTooManyPoints, [PointCount]);
// Read instructions
FInstructions.LoadFromStream(Stream);
if (FPath = nil) then
begin
if (FNumberOfContours = 0) then
exit;
FPath := TObjectList<TPascalTypeTrueTypeContour>.Create;
end else
FPath.Clear;
FPath.Capacity := FNumberOfContours;
for ContourIndex := 0 to FNumberOfContours - 1 do
begin
Contour := TPascalTypeTrueTypeContour.Create;
FPath.Add(Contour);
if ContourIndex = 0 then
Contour.PointCount := EndPointIndexOfContour[0] + 1
else
Contour.PointCount := (EndPointIndexOfContour[ContourIndex] - EndPointIndexOfContour[ContourIndex - 1]);
end;
// Contour flags
FlagCount := 0;
for ContourIndex := 0 to FNumberOfContours - 1 do
begin
Contour := FPath.List[ContourIndex];
for PointIndex := 0 to High(Contour.FPoints) do
begin
if (FlagCount = 0) then
begin
Stream.Read(Flag, 1);
{$IFDEF StrictExceptions}
if (Flag and GLYF_RESERVED <> 0) then
raise EPascalTypeError.CreateFmt(RCStrGlyphDataFlagReservedError, [PointIndex, PointCount]);
{$ENDIF}
if (Flag and GLYF_REPEAT_FLAG <> 0) then
// Read repeat count
Stream.Read(FlagCount, 1);
end else
Dec(FlagCount);
Contour.FPoints[PointIndex].Flags := Flag;
end;
// raise EPascalTypeError.CreateFmt(RCStrGlyphDataFlagRepeatError, [PointIndex + FlagCount, PointCount]);
end;
// Read x-coordinates
LastPoint := 0;
for ContourIndex := 0 to FNumberOfContours - 1 do
begin
Contour := FPath.List[ContourIndex];
for PointIndex := 0 to High(Contour.FPoints) do
begin
ContourPoint := @(Contour.FPoints[PointIndex]);
// Check for short or long version
if (ContourPoint.Flags and GLYF_X_SHORT_VECTOR <> 0) then
begin
Stream.Read(Value8, 1);
if (ContourPoint.Flags and GLYF_X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR <> 0) then
Inc(LastPoint, Value8)
else
Dec(LastPoint, Value8);
end else
if (ContourPoint.Flags and GLYF_X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR = 0) then
Inc(LastPoint, BigEndianValue.ReadSmallInt(Stream));
// else: No bytes read. See: https://github.com/MicrosoftDocs/typography-issues/issues/765
ContourPoint.XPos := LastPoint;
end;
end;
// Read y-coordinates
LastPoint := 0;
for ContourIndex := 0 to FNumberOfContours - 1 do
begin
Contour := FPath.List[ContourIndex];
for PointIndex := 0 to High(Contour.FPoints) do
begin
ContourPoint := @(Contour.FPoints[PointIndex]);
// Check for short or long version
if (ContourPoint.Flags and GLYF_Y_SHORT_VECTOR <> 0) then
begin
Stream.Read(Value8, 1);
if (ContourPoint.Flags and GLYF_Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR <> 0) then
Inc(LastPoint, Value8)
else
Dec(LastPoint, Value8);
end else
if (ContourPoint.Flags and GLYF_Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR = 0) then
Inc(LastPoint, BigEndianValue.ReadSmallInt(Stream));
// else: No bytes read. See: https://github.com/MicrosoftDocs/typography-issues/issues/765
ContourPoint.YPos := LastPoint;
end;
end;
// Normalize flags to avoid conflict with PT_CUBIC_CONTROL
for ContourIndex := 0 to FNumberOfContours - 1 do
begin
Contour := FPath.List[ContourIndex];
for PointIndex := 0 to High(Contour.FPoints) do
begin
if (Contour.FPoints[PointIndex].Flags and GLYF_ON_CURVE <> 0) then
Contour.FPoints[PointIndex].Flags := PT_ON_CURVE
else
Contour.FPoints[PointIndex].Flags := PT_QUAD_CONTROL;
end;
end;
end;
procedure TTrueTypeFontSimpleGlyphData.SaveToStream(Stream: TStream);
begin
raise EPascalTypeNotImplemented.Create(RCStrNotImplemented);
end;
//------------------------------------------------------------------------------
//
// TPascalTypeCompositeGlyph
//
//------------------------------------------------------------------------------
procedure TPascalTypeCompositeGlyph.Assign(Source: TPersistent);
begin
inherited;
if Source is TPascalTypeCompositeGlyph then
begin
FFlags := TPascalTypeCompositeGlyph(Source).FFlags;
FGlyphIndex := TPascalTypeCompositeGlyph(Source).FGlyphIndex;
FOffsetXY := TPascalTypeCompositeGlyph(Source).FOffsetXY;
FPointIndex := TPascalTypeCompositeGlyph(Source).FPointIndex;
FAffineTransformationMatrix := TPascalTypeCompositeGlyph(Source).FAffineTransformationMatrix;
end;
end;
procedure TPascalTypeCompositeGlyph.LoadFromStream(Stream: TStream; Size: Cardinal);
var
Bytes: array [0..1] of Byte;
ShortInts: array [0..1] of ShortInt;
{$IFDEF UseFloatingPoint}
const
CFixedPoint2Dot14Scale: Single = 1 / 16384;
{$ENDIF}
begin
inherited;
// read flags
FFlags := BigEndianValue.ReadWord(Stream);
{$IFDEF StrictExceptions}
// make sure the GLYF_RESERVED flag is set to 0
// if (FFlags and GLYF_RESERVED <> 0) then
// raise EPascalTypeError.Create(RCStrCompositeGlyphFlagError);
{$ENDIF}
// read glyph index
FGlyphIndex := BigEndianValue.ReadWord(Stream);
// read argument 1
if (FFlags and GLYF_ARG_1_AND_2_ARE_WORDS <> 0) then
begin
if (FFlags and GLYF_ARGS_ARE_XY_VALUES <> 0) then
begin
FOffsetXY[0] := BigEndianValue.ReadSmallInt(Stream);
FOffsetXY[1] := BigEndianValue.ReadSmallInt(Stream);
FPointIndex[0] := 0;
FPointIndex[1] := 0;
end else
begin
FPointIndex[0] := BigEndianValue.ReadWord(Stream);
FPointIndex[1] := BigEndianValue.ReadWord(Stream);
FOffsetXY[0] := 0;
FOffsetXY[1] := 0;
end;
end else
begin
if (FFlags and GLYF_ARGS_ARE_XY_VALUES <> 0) then
begin
Stream.Read(ShortInts[0], 1);
Stream.Read(ShortInts[1], 1);
FOffsetXY[0] := ShortInts[0];
FOffsetXY[1] := ShortInts[1];
FPointIndex[0] := 0;
FPointIndex[1] := 0;