-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalType.Tables.TrueType.CharacterMaps.pas
More file actions
1363 lines (1134 loc) · 48.2 KB
/
PascalType.Tables.TrueType.CharacterMaps.pas
File metadata and controls
1363 lines (1134 loc) · 48.2 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.CharacterMaps;
////////////////////////////////////////////////////////////////////////////////
// //
// 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
Classes,
SysUtils,
Generics.Defaults,
PascalType.Types,
PascalType.Classes,
PascalType.Tables,
PascalType.Unicode,
PascalType.Tables.TrueType.cmap;
//------------------------------------------------------------------------------
//
// TPascalTypeFormat0CharacterMap
//
//------------------------------------------------------------------------------
// Format 0: Byte encoding table
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#format-0-byte-encoding-table
//------------------------------------------------------------------------------
type
TPascalTypeFormat0CharacterMap = class(TCustomPascalTypeCharacterMap)
private
FLength: Word; // This is the length in bytes of the subtable.
FLanguage: Word; // Please see "Note on the language field in 'cmap' subtables" in the cmap spec.
FGlyphIdArray: array[Byte] of Byte; // An array that maps character codes to glyph index values.
protected
class function GetFormat: Word; override;
public
constructor Create(AParent: TCustomPascalTypeTable); override;
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
function CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer; override;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeFormat2CharacterMap
//
//------------------------------------------------------------------------------
// Format 2: High-byte mapping through table
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#format-2-high-byte-mapping-through-table
//------------------------------------------------------------------------------
type
TPascalTypeFormat2CharacterMap = class(TCustomPascalTypeCharacterMap)
private
FLength: Word; // This is the length in bytes of the subtable.
FLanguage: Word; // Please see "Note on the language field in 'cmap' subtables" in the cmap spec.
protected
class function GetFormat: Word; override;
public
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
function CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer; override;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeFormat4CharacterMap
//
//------------------------------------------------------------------------------
// Format 4: Segment mapping to delta values
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#format-4-segment-mapping-to-delta-values
//------------------------------------------------------------------------------
type
TPascalTypeFormat4CharacterMap = class(TCustomPascalTypeCharacterMap)
private type
TSegment = record
StartCode: Word;
EndCode: Word;
end;
private
FLength: Word; // This is the length in bytes of the subtable.
FLanguage: Word; // Please see "Note on the language field in 'cmap' subtables" in the cmap spec.
FSegments: TArray<TSegment>; // Start and End characterCode for each segment, last=0xFFFF.
FIdDelta: array of SmallInt; // Delta for all character codes in segment.
FIdRangeOffset: array of Word; // Offsets into glyphIdArray or 0
FGlyphIdArray: TGlyphString; // Glyph index array (arbitrary length)
protected
class function GetFormat: Word; override;
public
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
function CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer; override;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeFormat6CharacterMap
//
//------------------------------------------------------------------------------
// Format 6: Trimmed table mapping
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#format-6-trimmed-table-mapping
//------------------------------------------------------------------------------
type
TPascalTypeFormat6CharacterMap = class(TCustomPascalTypeCharacterMap)
private
FLanguage: Word; // Please see "Note on the language field in 'cmap' subtables" in the cmap spec.
FFirstCode: Word; // First character code of subrange.
FGlyphIdArray: TGlyphString; // Array of glyph index values for character codes in the range.
function GetEntryCount: Word;
protected
class function GetFormat: Word; override;
public
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
function CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer; override;
property EntryCount: Word read GetEntryCount;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeFormat10CharacterMap
//
//------------------------------------------------------------------------------
// Format 10: Trimmed array
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#format-10-trimmed-array
//------------------------------------------------------------------------------
type
TPascalTypeFormat10CharacterMap = class(TCustomPascalTypeCharacterMap)
private
FLanguage: Cardinal;
FStartCharCode: Cardinal;
FGlyphIdArray: TGlyphString;
function GetEntryCount: Cardinal;
protected
class function GetFormat: Word; override;
public
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
function CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer; override;
property EntryCount: Cardinal read GetEntryCount;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeFormat12CharacterMap
//
//------------------------------------------------------------------------------
// Format 12: Segmented coverage
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#format-12-segmented-coverage
//------------------------------------------------------------------------------
type
TCharMapSegmentedCoverageRecord = packed record
StartCharCode: TPascalTypeCodePoint; // First character code in this group
EndCharCode: TPascalTypeCodePoint; // Last character code in this group
StartGlyphID: Cardinal; // Glyph index corresponding to the starting character code
end;
TPascalTypeFormat12CharacterMap = class(TCustomPascalTypeCharacterMap)
private class var
FComparer: IComparer<TCharMapSegmentedCoverageRecord>;
private
class destructor Destroy;
private
FLanguage: Cardinal; // Please see "Note on the language field in 'cmap' subtables" in this document.
FCoverageArray: TArray<TCharMapSegmentedCoverageRecord>;
protected
class function GetFormat: Word; override;
function MapCodePoint(ACodePoint: TPascalTypeCodePoint; const ACoverageRecord: TCharMapSegmentedCoverageRecord): Integer; virtual;
public
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
function CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer; override;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeFormat13CharacterMap
//
//------------------------------------------------------------------------------
// Format 13: Many-to-one range mappings
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#format-13-many-to-one-range-mappings
//------------------------------------------------------------------------------
type
TPascalTypeFormat13CharacterMap = class(TPascalTypeFormat12CharacterMap)
protected
class function GetFormat: Word; override;
function MapCodePoint(ACodePoint: TPascalTypeCodePoint; const ACoverageRecord: TCharMapSegmentedCoverageRecord): Integer; override;
public
end;
//------------------------------------------------------------------------------
//
// TPascalTypeFormat14CharacterMap
//
//------------------------------------------------------------------------------
// Format 14: Unicode Variation Sequences
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#format-14-unicode-variation-sequences
//------------------------------------------------------------------------------
type
TPascalTypeFormat14CharacterMap = class(TCustomPascalTypeCharacterMap)
private type
TUnicodeRange = record
StartUnicodeValue: TPascalTypeCodePoint; // First value in this range, 24-bit
AdditionalCount: Byte; // Number of additional values in this range
end;
TUVSMapping = record
UnicodeValue: TPascalTypeCodePoint; // Base Unicode value of the UVS, 24-bit
GlyphID: Word; // Glyph ID of the UVS
end;
TDefaultUVS = TArray<TUnicodeRange>;
TNonDefaultUVS = TArray<TUVSMapping>;
TVariationSelector = record
VariationSelector: Cardinal;
DefaultUVS: TDefaultUVS;
NonDefaultUVS: TNonDefaultUVS;
end;
TVariationSelectors = TArray<TVariationSelector>;
private
FVariationSelectors: TVariationSelectors;
protected
class function GetFormat: Word; override;
public
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
function CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer; override;
function TryGetGlyphID(ACodePoint, ANextCodePoint: TPascalTypeCodePoint; DefaultGlyphID: Word; var Index: integer; out GlyphID: Word): boolean;
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
implementation
uses
Math,
Generics.Collections,
PascalType.Math,
PascalType.ResourceStrings;
//------------------------------------------------------------------------------
//
// TPascalTypeFormat0CharacterMap
//
//------------------------------------------------------------------------------
constructor TPascalTypeFormat0CharacterMap.Create(AParent: TCustomPascalTypeTable);
var
GlyphIdIndex: Byte;
begin
inherited;
for GlyphIdIndex := Low(Byte) to High(Byte) do
FGlyphIdArray[GlyphIdIndex] := GlyphIdIndex;
end;
class function TPascalTypeFormat0CharacterMap.GetFormat: Word;
begin
Result := 0;
end;
procedure TPascalTypeFormat0CharacterMap.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
inherited;
// check (minimum) table size
if Stream.Position + 2*SizeOf(Word) + SizeOf(FGlyphIdArray) > Stream.Size then
raise EPascalTypeTableIncomplete.Create(RCStrTableIncomplete);
// read length
FLength := BigEndianValue.ReadWord(Stream);
// read language
FLanguage := BigEndianValue.ReadWord(Stream);
Stream.Read(FGlyphIdArray, SizeOf(FGlyphIdArray));
end;
procedure TPascalTypeFormat0CharacterMap.SaveToStream(Stream: TStream);
begin
inherited;
// write length
BigEndianValue.WriteWord(Stream, FLength);
// write language
BigEndianValue.WriteWord(Stream, FLanguage);
Stream.Write(FGlyphIdArray, SizeOf(FGlyphIdArray));
end;
procedure TPascalTypeFormat0CharacterMap.Assign(Source: TPersistent);
begin
inherited;
if Source is TPascalTypeFormat0CharacterMap then
begin
FLength := TPascalTypeFormat0CharacterMap(Source).FLength;
FLanguage := TPascalTypeFormat0CharacterMap(Source).FLanguage;
FGlyphIdArray := TPascalTypeFormat0CharacterMap(Source).FGlyphIdArray;
end;
end;
function TPascalTypeFormat0CharacterMap.CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer;
begin
if (ACodePoint <= High(FGlyphIdArray)) then
Result := FGlyphIdArray[ACodePoint]
else
// raise EPascalTypeError.CreateFmt(RCStrIndexOutOfBounds, [ACodePoint]);
Result := GlyphNotDef; // Not defined
end;
//------------------------------------------------------------------------------
//
// TPascalTypeFormat2CharacterMap
//
//------------------------------------------------------------------------------
class function TPascalTypeFormat2CharacterMap.GetFormat: Word;
begin
Result := 2;
end;
procedure TPascalTypeFormat2CharacterMap.Assign(Source: TPersistent);
begin
inherited;
if Source is TPascalTypeFormat2CharacterMap then
begin
FLength := TPascalTypeFormat2CharacterMap(Source).FLength;
FLanguage := TPascalTypeFormat2CharacterMap(Source).FLanguage;
end;
end;
function TPascalTypeFormat2CharacterMap.CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer;
begin
Result := ACodePoint;
end;
procedure TPascalTypeFormat2CharacterMap.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
inherited;
// check (minimum) table size
if Stream.Position + 2*SizeOf(Word) > Stream.Size then
raise EPascalTypeTableIncomplete.Create(RCStrTableIncomplete);
// read length
FLength := BigEndianValue.ReadWord(Stream);
// read language
FLanguage := BigEndianValue.ReadWord(Stream);
end;
procedure TPascalTypeFormat2CharacterMap.SaveToStream(Stream: TStream);
begin
inherited;
// write length
BigEndianValue.WriteWord(Stream, FLength);
// write language
BigEndianValue.WriteWord(Stream, FLanguage);
end;
//------------------------------------------------------------------------------
//
// TPascalTypeFormat4CharacterMap
//
//------------------------------------------------------------------------------
class function TPascalTypeFormat4CharacterMap.GetFormat: Word;
begin
Result := 4;
end;
procedure TPascalTypeFormat4CharacterMap.Assign(Source: TPersistent);
begin
inherited;
if Source is TPascalTypeFormat4CharacterMap then
begin
FLength := TPascalTypeFormat4CharacterMap(Source).FLength;
FLanguage := TPascalTypeFormat4CharacterMap(Source).FLanguage;
FSegments := Copy(TPascalTypeFormat4CharacterMap(Source).FSegments);
FIdDelta := Copy(TPascalTypeFormat4CharacterMap(Source).FIdDelta);
FIdRangeOffset := Copy(TPascalTypeFormat4CharacterMap(Source).FIdRangeOffset);
FGlyphIdArray := Copy(TPascalTypeFormat4CharacterMap(Source).FGlyphIdArray);
end;
end;
function TPascalTypeFormat4CharacterMap.CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer;
var
SegmentIndex: Word;
GlyphIndex: integer;
Lo, Hi: Integer;
begin
if (ACodePoint > High(Word)) then
Exit(GlyphNotDef);
// Binary search
Lo := Low(FSegments);
Hi := High(FSegments);
SegmentIndex := 0;
while (Lo <= Hi) do
begin
SegmentIndex := (Lo + Hi) div 2;
if (ACodePoint > FSegments[SegmentIndex].EndCode) then
Lo := Succ(SegmentIndex)
else
if (ACodePoint < FSegments[SegmentIndex].StartCode) then
Hi := Pred(SegmentIndex)
else
break;
end;
if (Lo > Hi) then
// Missing glyph
Exit(GlyphNotDef);
var RangeOffset := FIdRangeOffset[SegmentIndex];
if RangeOffset <> 0 then
begin
// Thank you Apple and Microsoft for the completely bonkers definition
// of this value.
GlyphIndex := (
(RangeOffset div 2 - Length(FIdRangeOffset)) +
integer(SegmentIndex + ACodePoint - FSegments[SegmentIndex].StartCode)
) and $0000FFFF;
Result := FGlyphIdArray[GlyphIndex];
// Check for missing character and add offset
if (Result <> 0) then
Result := (Result + FIdDelta[SegmentIndex]) and $0000FFFF;
end else
Result := (integer(ACodePoint) + FIdDelta[SegmentIndex]) and $0000FFFF;
end;
procedure TPascalTypeFormat4CharacterMap.LoadFromStream(Stream: TStream; Size: Cardinal);
var
StartPos: Int64;
SegIndex: Integer;
{$IFDEF StrictExceptions}
Value16: Word;
{$ENDIF}
Count: integer;
begin
StartPos := Stream.Position;
inherited;
// check (minimum) table size
if Stream.Position + 2*SizeOf(Word) > Stream.Size then
raise EPascalTypeTableIncomplete.Create(RCStrTableIncomplete);
// read length
FLength := BigEndianValue.ReadWord(Stream);
// check (minimum) table size
if StartPos + FLength > Stream.Size then
raise EPascalTypeTableIncomplete.Create(RCStrTableIncomplete);
// read language
FLanguage := BigEndianValue.ReadWord(Stream);
Count := BigEndianValue.ReadWord(Stream) div 2;
Stream.Seek(3*SizeOf(Word), soFromCurrent);
SetLength(FSegments, Count);
SetLength(FIdDelta, Count);
SetLength(FIdRangeOffset, Count);
// read end count
for SegIndex := 0 to High(FSegments) do
FSegments[SegIndex].EndCode := BigEndianValue.ReadWord(Stream);
{$IFDEF StrictExceptions}
// Confirm end code is valid (required for binary search)
// The specs state that the last entry MUST be $FFFF but we don't need it
// and it causes some Harfbuzz unit tests to fail. Naturally this is a
// problem wit the fonts but since we can survive it we might as well
// ignore the problem.
if FSegments[High(FSegments)].EndCode <> $FFFF then
raise EPascalTypeError.CreateFmt(RCStrCharMapErrorEndCount, [FSegments[High(FSegments)].EndCode]);
{$ENDIF}
{$IFDEF StrictExceptions}
// read reserved
Value16 := BigEndianValue.ReadWord(Stream);
// confirm reserved value is valid
if Value16 <> 0 then
raise EPascalTypeError.CreateFmt(RCStrCharMapErrorReserved, [Value16]);
{$ELSE}
// skip reserved
Stream.Seek(2, soFromCurrent);
{$ENDIF}
// read start count
for SegIndex := 0 to High(FSegments) do
begin
FSegments[SegIndex].StartCode := BigEndianValue.ReadWord(Stream);
{$IFDEF StrictExceptions}
// confirm start count is valid
if FSegments[SegIndex].StartCode > FSegments[SegIndex].EndCode then
raise EPascalTypeError.CreateFmt(RCStrCharMapErrorStartCount, [FSegments[SegIndex].StartCode]);
{$ENDIF}
end;
// read ID delta
for SegIndex := 0 to High(FIdDelta) do
Word(FIdDelta[SegIndex]) := BigEndianValue.ReadWord(Stream);
{$IFDEF StrictExceptions}
(*
Disabled: I see no reason why the last entry must be 1. It isn't documented either.
A value of 0 has been observed in the Architecture font. Actually the whole FIdDelta table is zero.
// confirm ID delta is valid
if FIdDelta[High(FIdDelta)] <> 1 then
raise EPascalTypeError.CreateFmt(RCStrCharMapErrorIdDelta, [FIdDelta[High(FIdDelta)]]);
*)
{$ENDIF}
// read ID range offset
for SegIndex := 0 to High(FIdRangeOffset) do
FIdRangeOffset[SegIndex] := BigEndianValue.ReadWord(Stream);
SetLength(FGlyphIdArray, (FLength - (Stream.Position - StartPos)) div SizeOf(Word));
// read glyph ID array
for SegIndex := 0 to High(FGlyphIdArray) do
FGlyphIdArray[SegIndex] := BigEndianValue.ReadWord(Stream);
end;
procedure TPascalTypeFormat4CharacterMap.SaveToStream(Stream: TStream);
begin
var StartPos := Stream.Position;
inherited;
var SegCount := Length(FSegments);
var SearchRange: Word;
var EntrySelector: Word;
if (SegCount > 0) then
begin
SearchRange := (1 shl FloorLog2(SegCount)) * 2;
EntrySelector := FloorLog2(SegCount);
end else
begin
SearchRange := 0;
EntrySelector := 0;
end;
// write length (to be updated later)
var LengthPos := Stream.Position;
BigEndianValue.WriteWord(Stream, 0);
// write language
BigEndianValue.WriteWord(Stream, FLanguage);
// write segCountX2
BigEndianValue.WriteWord(Stream, SegCount * 2);
// write searchRange
BigEndianValue.WriteWord(Stream, SearchRange);
// write entrySelector
BigEndianValue.WriteWord(Stream, EntrySelector);
// write rangeShift
BigEndianValue.WriteWord(Stream, (SegCount * 2) - SearchRange);
// write endCode
for var SegIndex := 0 to SegCount - 1 do
BigEndianValue.WriteWord(Stream, FSegments[SegIndex].EndCode);
// write reservedPad
BigEndianValue.WriteWord(Stream, 0);
// write startCode
for var SegIndex := 0 to SegCount - 1 do
BigEndianValue.WriteWord(Stream, FSegments[SegIndex].StartCode);
// write idDelta
for var SegIndex := 0 to SegCount - 1 do
BigEndianValue.WriteWord(Stream, Word(FIdDelta[SegIndex]));
// write idRangeOffset
for var SegIndex := 0 to SegCount - 1 do
BigEndianValue.WriteWord(Stream, FIdRangeOffset[SegIndex]);
// write glyph ID array
for var SegIndex := 0 to High(FGlyphIdArray) do
BigEndianValue.WriteWord(Stream, FGlyphIdArray[SegIndex]);
// update length
var CurrentPos := Stream.Position;
Stream.Position := LengthPos;
BigEndianValue.WriteWord(Stream, Word(CurrentPos - StartPos));
Stream.Position := CurrentPos;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeFormat6CharacterMap
//
//------------------------------------------------------------------------------
procedure TPascalTypeFormat6CharacterMap.Assign(Source: TPersistent);
begin
inherited;
if Source is TPascalTypeFormat6CharacterMap then
begin
FLanguage := TPascalTypeFormat6CharacterMap(Source).FLanguage;
FFirstCode := TPascalTypeFormat6CharacterMap(Source).FFirstCode;
FGlyphIdArray := Copy(TPascalTypeFormat6CharacterMap(Source).FGlyphIdArray);
end;
end;
function TPascalTypeFormat6CharacterMap.CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer;
begin
if (ACodePoint >= FFirstCode) and (integer(ACodePoint - FFirstCode) <= High(FGlyphIdArray)) then
Result := FGlyphIdArray[ACodePoint - FFirstCode]
else
Result := GlyphNotDef;
end;
function TPascalTypeFormat6CharacterMap.GetEntryCount: Word;
begin
Result := Length(FGlyphIdArray);
end;
class function TPascalTypeFormat6CharacterMap.GetFormat: Word;
begin
Result := 6
end;
procedure TPascalTypeFormat6CharacterMap.LoadFromStream(Stream: TStream; Size: Cardinal);
var
EntryIndex: Integer;
begin
inherited;
// read table size
// TableLength := BigEndianValue.ReadWord(Stream);
Stream.Seek(SizeOf(Word), soFromCurrent);
// read language
FLanguage := BigEndianValue.ReadWord(Stream);
// read first code
FFirstCode := BigEndianValue.ReadWord(Stream);
// read number of character codes in subrange
SetLength(FGlyphIdArray, BigEndianValue.ReadWord(Stream));
for EntryIndex := 0 to High(FGlyphIdArray) do
FGlyphIdArray[EntryIndex] := BigEndianValue.ReadWord(Stream);
end;
procedure TPascalTypeFormat6CharacterMap.SaveToStream(Stream: TStream);
var
EntryIndex: Integer;
begin
inherited;
// write table size
BigEndianValue.WriteWord(Stream, 8 + 2 * Length(FGlyphIdArray));
// write language
BigEndianValue.WriteWord(Stream, FLanguage);
// write first code
BigEndianValue.WriteWord(Stream, FFirstCode);
// write number of character codes in subrange
BigEndianValue.WriteWord(Stream, Length(FGlyphIdArray));
// write glyph indices
for EntryIndex := 0 to High(FGlyphIdArray) do
BigEndianValue.WriteWord(Stream, FGlyphIdArray[EntryIndex]);
end;
//------------------------------------------------------------------------------
//
// TPascalTypeFormat10CharacterMap
//
//------------------------------------------------------------------------------
procedure TPascalTypeFormat10CharacterMap.Assign(Source: TPersistent);
begin
inherited;
if Source is TPascalTypeFormat10CharacterMap then
begin
FLanguage := TPascalTypeFormat10CharacterMap(Source).FLanguage;
FStartCharCode := TPascalTypeFormat10CharacterMap(Source).FStartCharCode;
FGlyphIdArray := Copy(TPascalTypeFormat10CharacterMap(Source).FGlyphIdArray);
end;
end;
function TPascalTypeFormat10CharacterMap.CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer;
begin
if (ACodePoint >= FStartCharCode) and (ACodePoint < FStartCharCode + EntryCount) then
Result := FGlyphIdArray[ACodePoint - FStartCharCode]
else
Result := GlyphNotDef;
end;
function TPascalTypeFormat10CharacterMap.GetEntryCount: Cardinal;
begin
Result := Length(FGlyphIdArray);
end;
class function TPascalTypeFormat10CharacterMap.GetFormat: Word;
begin
Result := 10;
end;
procedure TPascalTypeFormat10CharacterMap.LoadFromStream(Stream: TStream; Size: Cardinal);
var
StartPos: Int64;
TableLength: Cardinal;
EntryIndex: Integer;
begin
StartPos := Stream.Position;
inherited;
// reserved
Stream.Seek(2, soFromCurrent);
// read table length
TableLength := BigEndianValue.ReadCardinal(Stream);
// read language
FLanguage := BigEndianValue.ReadCardinal(Stream);
// read start char code
FStartCharCode := BigEndianValue.ReadCardinal(Stream);
// read number of character codes in subrange
var EntryCount := BigEndianValue.ReadCardinal(Stream);
if EntryCount > 65535 then
raise EPascalTypeError.CreateFmt(RCStrValueOutOfBounds, [EntryCount]);
if (StartPos + TableLength > Stream.Size) or
(Stream.Position + (EntryCount * SizeOf(Word)) > StartPos + TableLength) then
raise EPascalTypeTableIncomplete.Create(RCStrTableIncomplete);
SetLength(FGlyphIdArray, EntryCount);
for EntryIndex := 0 to High(FGlyphIdArray) do
FGlyphIdArray[EntryIndex] := BigEndianValue.ReadWord(Stream);
// seek end of this table
Stream.Position := StartPos + TableLength;
end;
procedure TPascalTypeFormat10CharacterMap.SaveToStream(Stream: TStream);
var
StartPos: Int64;
CurrentPos: Int64;
EntryIndex: Integer;
begin
StartPos := Stream.Position;
inherited;
// reserved
BigEndianValue.WriteWord(Stream, 0);
// write table length (to be updated later)
BigEndianValue.WriteCardinal(Stream, 0);
// write language
BigEndianValue.WriteCardinal(Stream, FLanguage);
// write start char code
BigEndianValue.WriteCardinal(Stream, FStartCharCode);
// write number of character codes in subrange
BigEndianValue.WriteCardinal(Stream, EntryCount);
for EntryIndex := 0 to High(FGlyphIdArray) do
BigEndianValue.WriteWord(Stream, FGlyphIdArray[EntryIndex]);
// update table length
CurrentPos := Stream.Position;
Stream.Position := StartPos + 4;
BigEndianValue.WriteCardinal(Stream, Cardinal(CurrentPos - StartPos));
Stream.Position := CurrentPos;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeFormat12CharacterMap
//
//------------------------------------------------------------------------------
class destructor TPascalTypeFormat12CharacterMap.Destroy;
begin
FComparer := nil;
end;
class function TPascalTypeFormat12CharacterMap.GetFormat: Word;
begin
Result := 12;
end;
procedure TPascalTypeFormat12CharacterMap.Assign(Source: TPersistent);
begin
inherited;
if Source is TPascalTypeFormat12CharacterMap then
begin
FLanguage := TPascalTypeFormat12CharacterMap(Source).FLanguage;
FCoverageArray := Copy(TPascalTypeFormat12CharacterMap(Source).FCoverageArray);
end;
end;
function TPascalTypeFormat12CharacterMap.CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer;
begin
if (FComparer = nil) then
FComparer := TComparer<TCharMapSegmentedCoverageRecord>.Construct(
function(const ATableItem, ASearchItem: TCharMapSegmentedCoverageRecord): integer
begin
Result := integer(ATableItem.EndCharCode) - integer(ASearchItem.EndCharCode);
if (Result > 0) then
begin
Result := integer(ATableItem.StartCharCode) - integer(ASearchItem.StartCharCode);
if (Result < 0) then
Result := 0;
end;
end);
var SearchItem: TCharMapSegmentedCoverageRecord;
SearchItem.StartCharCode := ACodePoint;
SearchItem.EndCharCode := ACodePoint;
var Index: integer;
if (TArray.BinarySearch<TCharMapSegmentedCoverageRecord>(FCoverageArray, SearchItem, Index, FComparer)) then
Result := MapCodePoint(ACodePoint, FCoverageArray[Index])
else
Result := GlyphNotDef;
end;
function TPascalTypeFormat12CharacterMap.MapCodePoint(ACodePoint: TPascalTypeCodePoint;
const ACoverageRecord: TCharMapSegmentedCoverageRecord): Integer;
begin
Result := Integer(ACoverageRecord.StartGlyphID) + (Integer(ACodePoint) - Integer(ACoverageRecord.StartCharCode))
end;
procedure TPascalTypeFormat12CharacterMap.LoadFromStream(Stream: TStream; Size: Cardinal);
var
StartPos: Int64;
TableLength: Cardinal;
GroupIndex: Integer;
begin
StartPos := Stream.Position;
inherited;
{$IFDEF StrictExceptions}
if BigEndianValue.ReadWord(Stream) <> 0 then
raise EPascalTypeError.Create(RCStrReservedValueError);
{$ELSE}
Stream.Seek(2, soFromCurrent);
{$ENDIF}
// read table length
TableLength := BigEndianValue.ReadCardinal(Stream);
// read language
FLanguage := BigEndianValue.ReadCardinal(Stream);
// read group count
var GroupCount := BigEndianValue.ReadCardinal(Stream);
if GroupCount > 65535 then
raise EPascalTypeError.CreateFmt(RCStrValueOutOfBounds, [GroupCount]);
if (StartPos + TableLength > Stream.Size) or
(Stream.Position + (GroupCount * 12) > StartPos + TableLength) then
raise EPascalTypeTableIncomplete.Create(RCStrTableIncomplete);
SetLength(FCoverageArray, GroupCount);
for GroupIndex := 0 to High(FCoverageArray) do
begin
FCoverageArray[GroupIndex].StartCharCode := BigEndianValue.ReadCardinal(Stream);
FCoverageArray[GroupIndex].EndCharCode := BigEndianValue.ReadCardinal(Stream);
FCoverageArray[GroupIndex].StartGlyphID := BigEndianValue.ReadCardinal(Stream);
end;
// seek end of this table
// TODO : Why?
Stream.Position := StartPos + TableLength;
end;
procedure TPascalTypeFormat12CharacterMap.SaveToStream(Stream: TStream);
var
StartPos: Int64;
GroupIndex: Integer;
CurrentPos: Int64;
begin
StartPos := Stream.Position;
inherited;
// reserved
BigEndianValue.WriteWord(Stream, 0);
// write table length (to be updated later)
BigEndianValue.WriteCardinal(Stream, 0);
// write language
BigEndianValue.WriteCardinal(Stream, FLanguage);
// write group count
BigEndianValue.WriteCardinal(Stream, Length(FCoverageArray));
for GroupIndex := 0 to High(FCoverageArray) do
begin
BigEndianValue.WriteCardinal(Stream, FCoverageArray[GroupIndex].StartCharCode);
BigEndianValue.WriteCardinal(Stream, FCoverageArray[GroupIndex].EndCharCode);
BigEndianValue.WriteCardinal(Stream, FCoverageArray[GroupIndex].StartGlyphID);
end;
// update table length
CurrentPos := Stream.Position;