-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPascalType.Tables.TrueType.cmap.pas
More file actions
871 lines (727 loc) · 31.5 KB
/
PascalType.Tables.TrueType.cmap.pas
File metadata and controls
871 lines (727 loc) · 31.5 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
unit PascalType.Tables.TrueType.cmap;
////////////////////////////////////////////////////////////////////////////////
// //
// 'cmap' 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-2021 //
// by Christian-W. Budde. All Rights Reserved. //
// //
////////////////////////////////////////////////////////////////////////////////
interface
{$I PT_Compiler.inc}
uses
Generics.Collections,
Classes,
PascalType.Types,
PascalType.Classes,
PascalType.Unicode;
//------------------------------------------------------------------------------
//
// TCustomPascalTypeCharacterMap
//
//------------------------------------------------------------------------------
// Mapping sub table base class
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/cmap
//------------------------------------------------------------------------------
type
// 'cmap' tables
TCustomPascalTypeCharacterMap = class abstract(TCustomPascalTypeTable)
protected
class function GetFormat: Word; virtual; abstract;
public
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
function CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer; virtual; abstract;
property Format: Word read GetFormat;
end;
TPascalTypeCharacterMapClass = class of TCustomPascalTypeCharacterMap;
//------------------------------------------------------------------------------
//
// TCustomPascalTypeCharacterMapDirectory
//
//------------------------------------------------------------------------------
// Character to Glyph Index Mapping Table - Base class
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/cmap
//------------------------------------------------------------------------------
type
TCustomPascalTypeCharacterMapDirectory = class abstract(TCustomPascalTypeTable)
private
FCharacterMap: TCustomPascalTypeCharacterMap;
FEncodingID : Word;
protected
function GetPlatformID: TPlatformID; virtual; abstract;
procedure SetEncodingID(const Value: Word);
procedure EncodingIDChanged; virtual;
public
constructor Create(AParent: TCustomPascalTypeTable; AEncodingID: Word); reintroduce; virtual;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure LoadFromStream(Stream: TStream; Size: Cardinal = 0); override;
procedure SaveToStream(Stream: TStream); override;
function CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer; virtual;
property PlatformID: TPlatformID read GetPlatformID;
property EncodingID: Word read FEncodingID;// write SetEncodingID;
property CharacterMap: TCustomPascalTypeCharacterMap read FCharacterMap;
end;
TPascalTypeCharacterMapDirectoryClass = class of TCustomPascalTypeCharacterMapDirectory;
//------------------------------------------------------------------------------
//
// TPascalTypeCharacterMapUnicodeDirectory
//
//------------------------------------------------------------------------------
// Character to Glyph Index Mapping Table - Unicode platform (platform ID = 0)
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#unicode-platform-platform-id--0
//------------------------------------------------------------------------------
type
TPascalTypeCharacterMapUnicodeDirectory = class(TCustomPascalTypeCharacterMapDirectory)
private
procedure SetEncodingID(const Value: TUnicodeEncodingID);
function GetEncodingID: TUnicodeEncodingID;
protected
function GetPlatformID: TPlatformID; override;
public
property PlatformSpecificID: TUnicodeEncodingID read GetEncodingID write SetEncodingID;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeCharacterMapMacintoshDirectory
//
//------------------------------------------------------------------------------
// Character to Glyph Index Mapping Table - Macintosh platform (platform ID = 1)
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#macintosh-platform-platform-id--1
//------------------------------------------------------------------------------
// TODO : Do we even need to support this mapping? I think Apple uses Unicode now and it's obsolete
type
TPascalTypeCharacterMapMacintoshDirectory = class(TCustomPascalTypeCharacterMapDirectory)
private
procedure SetEncodingID(const Value: TAppleEncodingID);
function GetEncodingID: TAppleEncodingID;
protected
function GetPlatformID: TPlatformID; override;
public
property PlatformSpecificID: TAppleEncodingID read GetEncodingID write SetEncodingID;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeCharacterMapMicrosoftDirectory
//
//------------------------------------------------------------------------------
// Character to Glyph Index Mapping Table - Windows platform (platform ID = 3)
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#windows-platform-platform-id--3
//------------------------------------------------------------------------------
type
TPascalTypeCharacterMapMicrosoftDirectory = class(TCustomPascalTypeCharacterMapDirectory)
private
procedure SetEncodingID(const Value: TMicrosoftEncodingID);
function GetEncodingID: TMicrosoftEncodingID;
protected
function GetPlatformID: TPlatformID; override;
public
function CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer; override;
property PlatformSpecificID: TMicrosoftEncodingID read GetEncodingID write SetEncodingID;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeCharacterMapDirectoryGenericEntry
//
//------------------------------------------------------------------------------
// Character to Glyph Index Mapping Table - Custom platform (platform ID = 4)
// and OTF Windows NT compatibility mapping
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#custom-platform-platform-id--4-and-otf-windows-nt-compatibility-mapping
//------------------------------------------------------------------------------
type
TPascalTypeCharacterMapDirectoryGenericEntry = class(TCustomPascalTypeCharacterMapDirectory)
private
FPlatformID: TPlatformID;
protected
function GetPlatformID: TPlatformID; override;
public
constructor Create(AParent: TCustomPascalTypeTable; AEncodingID: Word); override;
procedure Assign(Source: TPersistent); override;
end;
//------------------------------------------------------------------------------
//
// TCustomPascalTypeCharacterMap
//
//------------------------------------------------------------------------------
// Character to Glyph Index Mapping Table
//------------------------------------------------------------------------------
// https://learn.microsoft.com/en-us/typography/opentype/spec/cmap
//------------------------------------------------------------------------------
type
TPascalTypeCharacterMapTable = class(TCustomPascalTypeNamedTable)
private
FVersion: Word; // Version number (Set to zero)
FMaps : TList<TCustomPascalTypeCharacterMapDirectory>;
function GetCharacterMapSubtableCount: Word;
function GetCharacterMapSubtable(Index: Integer): TCustomPascalTypeCharacterMapDirectory;
procedure SetVersion(const Value: Word);
private
FBestCharacterMap: TCustomPascalTypeCharacterMapDirectory;
protected
procedure CharacterMapDirectoryChanged; virtual;
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 GetGlyphByCharacter(ACodePoint: TPascalTypeCodePoint): Integer;
property Version: Word read FVersion write SetVersion;
property CharacterMapSubtableCount: Word read GetCharacterMapSubtableCount;
property CharacterMapSubtable[Index: Integer]: TCustomPascalTypeCharacterMapDirectory read GetCharacterMapSubtable; default;
end;
//------------------------------------------------------------------------------
//
// Character map registration
//
//------------------------------------------------------------------------------
type
PascalTypeCharacterMaps = record
class procedure RegisterCharacterMap(CharacterMapClass: TPascalTypeCharacterMapClass); static;
class procedure RegisterCharacterMaps(CharacterMapClasses: array of TPascalTypeCharacterMapClass); static;
class function FindCharacterMapByFormat(Format: Word): TPascalTypeCharacterMapClass; static;
end;
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
implementation
uses
SysUtils,
PascalType.Tables.TrueType.CharacterMaps, // Included so we get the classes registered
PascalType.Tables,
PascalType.FontFace.SFNT,
PascalType.Tables.TrueType.os2,
PascalType.ResourceStrings;
var
GCharacterMapClasses: array of TPascalTypeCharacterMapClass;
resourcestring
RCStrUnknownCharacterMap = 'Unknown character map (%d)';
RCStrCharacterMapNotSet = 'Character map not set properly!';
function IsPascalTypeCharacterMapRegistered(CharacterMapClass: TPascalTypeCharacterMapClass): Boolean;
var
CharacterMapClassIndex: Integer;
begin
Result := False;
for CharacterMapClassIndex := 0 to High(GCharacterMapClasses) do
if GCharacterMapClasses[CharacterMapClassIndex] = CharacterMapClass then
Exit(True);
end;
function CheckCharacterMapClassesValid: Boolean;
var
CharacterMapClassBaseIndex: Integer;
CharacterMapClassIndex : Integer;
begin
Result := True;
for CharacterMapClassBaseIndex := 0 to High(GCharacterMapClasses) do
for CharacterMapClassIndex := CharacterMapClassBaseIndex + 1 to High(GCharacterMapClasses) do
if GCharacterMapClasses[CharacterMapClassBaseIndex] = GCharacterMapClasses[CharacterMapClassIndex] then
Exit(False);
end;
class procedure PascalTypeCharacterMaps.RegisterCharacterMap(CharacterMapClass: TPascalTypeCharacterMapClass);
begin
Assert(IsPascalTypeCharacterMapRegistered(CharacterMapClass) = False);
SetLength(GCharacterMapClasses, Length(GCharacterMapClasses) + 1);
GCharacterMapClasses[High(GCharacterMapClasses)] := CharacterMapClass;
end;
class procedure PascalTypeCharacterMaps.RegisterCharacterMaps(CharacterMapClasses: array of TPascalTypeCharacterMapClass);
var
CharacterMapClassIndex: Integer;
begin
SetLength(GCharacterMapClasses, Length(GCharacterMapClasses) + Length(CharacterMapClasses));
for CharacterMapClassIndex := 0 to High(CharacterMapClasses) do
GCharacterMapClasses[Length(GCharacterMapClasses) - Length(CharacterMapClasses) + CharacterMapClassIndex] :=
CharacterMapClasses[CharacterMapClassIndex];
Assert(CheckCharacterMapClassesValid);
end;
class function PascalTypeCharacterMaps.FindCharacterMapByFormat(Format: Word): TPascalTypeCharacterMapClass;
var
CharacterMapClassIndex: Integer;
begin
Result := nil;
for CharacterMapClassIndex := 0 to High(GCharacterMapClasses) do
if GCharacterMapClasses[CharacterMapClassIndex].GetFormat = Format then
begin
Result := GCharacterMapClasses[CharacterMapClassIndex];
Exit;
end;
// raise EPascalTypeError.Create('Unknown Table Class: ' + TableType);
end;
//------------------------------------------------------------------------------
//
// TCustomPascalTypeCharacterMap
//
//------------------------------------------------------------------------------
procedure TCustomPascalTypeCharacterMap.LoadFromStream(Stream: TStream; Size: Cardinal);
begin
inherited;
if (BigEndianValue.ReadWord(Stream) <> Format) then
raise EPascalTypeError.Create('CharacterMap format mismatch');
end;
procedure TCustomPascalTypeCharacterMap.SaveToStream(Stream: TStream);
begin
inherited;
BigEndianValue.WriteWord(Stream, Format);
end;
//------------------------------------------------------------------------------
//
// TCustomPascalTypeCharacterMapDirectory
//
//------------------------------------------------------------------------------
constructor TCustomPascalTypeCharacterMapDirectory.Create(AParent: TCustomPascalTypeTable; AEncodingID: Word);
begin
inherited Create(AParent);
FEncodingID := AEncodingID;
end;
destructor TCustomPascalTypeCharacterMapDirectory.Destroy;
begin
FCharacterMap.Free;
inherited;
end;
procedure TCustomPascalTypeCharacterMapDirectory.Assign(Source: TPersistent);
begin
inherited;
if Source is TCustomPascalTypeCharacterMapDirectory then
begin
FEncodingID := TCustomPascalTypeCharacterMapDirectory(Source).FEncodingID;
// match character map type
if (FCharacterMap = nil) or (FCharacterMap.ClassType <> TCustomPascalTypeCharacterMapDirectory(Source).FCharacterMap.ClassType) then
begin
FreeAndNil(FCharacterMap);
// create new character map
FCharacterMap := TPascalTypeCharacterMapClass(TCustomPascalTypeCharacterMapDirectory(Source).FCharacterMap.ClassType).Create(Self);
end;
// assign character map
if (FCharacterMap <> nil) then
FCharacterMap.Assign(TCustomPascalTypeCharacterMapDirectory(Source).FCharacterMap);
end;
end;
procedure TCustomPascalTypeCharacterMapDirectory.EncodingIDChanged;
begin
Changed;
end;
function TCustomPascalTypeCharacterMapDirectory.CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer;
begin
if (FCharacterMap = nil) then
raise EPascalTypeError.Create(RCStrCharacterMapNotSet);
Result := FCharacterMap.CharacterToGlyph(ACodePoint);
end;
procedure TCustomPascalTypeCharacterMapDirectory.LoadFromStream(Stream: TStream; Size: Cardinal);
var
MapFormat : Word;
MapClass: TPascalTypeCharacterMapClass;
begin
inherited;
// check (minimum) table size
if Stream.Position + SizeOf(Word) > Stream.Size then
raise EPascalTypeTableIncomplete.Create(RCStrTableIncomplete);
FreeAndNil(FCharacterMap);
// read format
MapFormat := BigEndianValue.ReadWord(Stream);
MapClass := PascalTypeCharacterMaps.FindCharacterMapByFormat(MapFormat);
if (MapClass = nil) then
raise EPascalTypeError.CreateFmt(RCStrUnknownCharacterMap, [MapFormat]);
FCharacterMap := MapClass.Create(Self);
Stream.Seek(-SizeOf(Word), soFromCurrent);
FCharacterMap.LoadFromStream(Stream);
end;
procedure TCustomPascalTypeCharacterMapDirectory.SaveToStream(Stream: TStream);
begin
if (FCharacterMap <> nil) then
FCharacterMap.SaveToStream(Stream);
end;
procedure TCustomPascalTypeCharacterMapDirectory.SetEncodingID(const Value: Word);
begin
if Value <> FEncodingID then
begin
FEncodingID := Value;
EncodingIDChanged;
end;
end;
//------------------------------------------------------------------------------
//
// TPascalTypeCharacterMapUnicodeDirectory
//
//------------------------------------------------------------------------------
function TPascalTypeCharacterMapUnicodeDirectory.GetEncodingID: TUnicodeEncodingID;
begin
Result := TUnicodeEncodingID(EncodingID);
end;
function TPascalTypeCharacterMapUnicodeDirectory.GetPlatformID: TPlatformID;
begin
Result := piUnicode;
end;
procedure TPascalTypeCharacterMapUnicodeDirectory.SetEncodingID(const Value: TUnicodeEncodingID);
begin
inherited SetEncodingID(Ord(Value));
end;
//------------------------------------------------------------------------------
//
// TPascalTypeCharacterMapMacintoshDirectory
//
//------------------------------------------------------------------------------
function TPascalTypeCharacterMapMacintoshDirectory.GetEncodingID: TAppleEncodingID;
begin
Result := TAppleEncodingID(EncodingID);
end;
function TPascalTypeCharacterMapMacintoshDirectory.GetPlatformID: TPlatformID;
begin
Result := piApple;
end;
procedure TPascalTypeCharacterMapMacintoshDirectory.SetEncodingID(const Value: TAppleEncodingID);
begin
inherited SetEncodingID(Ord(Value));
end;
//------------------------------------------------------------------------------
//
// TPascalTypeCharacterMapMicrosoftDirectory
//
//------------------------------------------------------------------------------
function TPascalTypeCharacterMapMicrosoftDirectory.CharacterToGlyph(ACodePoint: TPascalTypeCodePoint): Integer;
var
OS2Table: TPascalTypeOS2Table;
begin
case PlatformSpecificID of
meUnicodeBMP:
Result := inherited CharacterToGlyph(ACodePoint);
// meSymbol included. How else are we going to use symbol fonts?
// Seen with: "Symbol"
meSymbol:
begin
// https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#windows-platform-platform-id--3
//
// The symbol encoding was created to support fonts with arbitrary ornaments or symbols
// not supported in Unicode or other standard encodings. A format 4 subtable would be used,
// typically with up to 224 graphic characters assigned at code positions beginning with 0xF020.
// This corresponds to a sub-range within the Unicode Private-Use Area (PUA), though this is not
// a Unicode encoding. In legacy usage, some applications would represent the symbol characters
// in text using a single-byte encoding, and then map 0x20 to the OS/2.usFirstCharIndex value in
// the font.
Result := inherited CharacterToGlyph(ACodePoint);
if (Result = 0) and (ACodePoint < $F000) then
begin
OS2Table := TPascalTypeFontFace(FontFace).OS2Table;
if (OS2Table <> nil) and
((OS2Table.CodePageRange = nil) or (OS2Table.CodePageRange.SupportsSymbolCharacterSet) or
((OS2Table.CodePageRange.AsCardinal[0] = 0) and (OS2Table.CodePageRange.AsCardinal[1] = 0))) then
// Using the offset in OS2Table.UnicodeFirstCharacterIndex, as the documentation suggests,
// works for very few symbols fonts. Using a hardcoded value of $F020 works for most.
// ACodePoint := Word(Integer(ACodePoint) - Ord(' ') + OS2Table.UnicodeFirstCharacterIndex)
ACodePoint := Word(Integer(ACodePoint) - Ord(' ') + $F020);
Result := inherited CharacterToGlyph(ACodePoint);
end;
end;
else
Result := inherited CharacterToGlyph(ACodePoint);
end;
end;
function TPascalTypeCharacterMapMicrosoftDirectory.GetEncodingID: TMicrosoftEncodingID;
begin
Result := TMicrosoftEncodingID(EncodingID);
end;
function TPascalTypeCharacterMapMicrosoftDirectory.GetPlatformID: TPlatformID;
begin
Result := piMicrosoft;
end;
procedure TPascalTypeCharacterMapMicrosoftDirectory.SetEncodingID(const Value: TMicrosoftEncodingID);
begin
inherited SetEncodingID(Ord(Value));
end;
//------------------------------------------------------------------------------
//
// TPascalTypeCharacterMapDirectoryGenericEntry
//
//------------------------------------------------------------------------------
procedure TPascalTypeCharacterMapDirectoryGenericEntry.Assign(Source: TPersistent);
begin
inherited;
if Source is TPascalTypeCharacterMapDirectoryGenericEntry then
FPlatformID := TPascalTypeCharacterMapDirectoryGenericEntry(Source).FPlatformID;
end;
constructor TPascalTypeCharacterMapDirectoryGenericEntry.Create(AParent: TCustomPascalTypeTable; AEncodingID: Word);
begin
inherited;
FPlatformID := piCustom;
end;
function TPascalTypeCharacterMapDirectoryGenericEntry.GetPlatformID: TPlatformID;
begin
Result := FPlatformID;
end;
//------------------------------------------------------------------------------
//
// TCustomPascalTypeCharacterMap
//
//------------------------------------------------------------------------------
destructor TPascalTypeCharacterMapTable.Destroy;
begin
FMaps.Free;
inherited;
end;
procedure TPascalTypeCharacterMapTable.Assign(Source: TPersistent);
var
i: Integer;
MapClass: TPascalTypeCharacterMapDirectoryClass;
SourceMap: TCustomPascalTypeCharacterMapDirectory;
Map: TCustomPascalTypeCharacterMapDirectory;
begin
inherited;
if Source is TPascalTypeCharacterMapTable then
begin
FBestCharacterMap := nil;
FVersion := TPascalTypeCharacterMapTable(Source).FVersion;
if (FMaps <> nil) then
FMaps.Clear;
// set length of map array
if (TPascalTypeCharacterMapTable(Source).CharacterMapSubtableCount > 0) then
begin
if (FMaps = nil) then
FMaps := TObjectList<TCustomPascalTypeCharacterMapDirectory>.Create;
FMaps.Capacity := TPascalTypeCharacterMapTable(Source).CharacterMapSubtableCount;
for i := 0 to TPascalTypeCharacterMapTable(Source).CharacterMapSubtableCount - 1 do
begin
SourceMap := TPascalTypeCharacterMapTable(Source).CharacterMapSubtable[i];
MapClass := TPascalTypeCharacterMapDirectoryClass(SourceMap.ClassType);
Map := MapClass.Create(Self, SourceMap.EncodingID);
FMaps.Add(Map);
Map.Assign(SourceMap);
end;
end;
end;
end;
function TPascalTypeCharacterMapTable.GetCharacterMapSubtable(Index: Integer): TCustomPascalTypeCharacterMapDirectory;
begin
if (FMaps = nil) or (Index < 0) or (Index > FMaps.Count) then
raise EPascalTypeError.CreateFmt(RCStrIndexOutOfBounds, [Index]);
Result := FMaps[Index];
end;
function TPascalTypeCharacterMapTable.GetCharacterMapSubtableCount: Word;
begin
if (FMaps <> nil) then
Result := FMaps.Count
else
Result := 0;
end;
function TPascalTypeCharacterMapTable.GetGlyphByCharacter(ACodePoint: TPascalTypeCodePoint): Integer;
function FindMap(PlatformID: TPlatformID; EncodingID: Word): TCustomPascalTypeCharacterMapDirectory;
begin
for Result in FMaps do
// TODO : Why filter on Result.CharacterMap.Format ?
if (Result.PlatformID = PlatformID) and (Result.EncodingID = EncodingID) and (Result.CharacterMap.Format in [0,2,4,6,10,12,13]) then
exit;
Result := nil;
end;
const
MapSearchOrder: array[0..8] of record
PlatformID: TPlatformID;
EncodingID: Word;
Symbol: boolean;
end = (
// The following search order mimics that of Harfbuzz
// 32-bit
(PlatformID: piMicrosoft; EncodingID: 10; Symbol: False),
(PlatformID: piUnicode; EncodingID: 6; Symbol: False),
(PlatformID: piUnicode; EncodingID: 4; Symbol: False),
// 16-bit
(PlatformID: piMicrosoft; EncodingID: 1; Symbol: False),
(PlatformID: piUnicode; EncodingID: 3; Symbol: False),
(PlatformID: piUnicode; EncodingID: 2; Symbol: False),
(PlatformID: piUnicode; EncodingID: 1; Symbol: False),
(PlatformID: piUnicode; EncodingID: 0; Symbol: False),
// Symbol
(PlatformID: piMicrosoft; EncodingID: 0; Symbol: True)
);
var
i: integer;
// Map: TCustomPascalTypeCharacterMapDirectory;
begin
if (FBestCharacterMap = nil) then
begin
if (FMaps = nil) then
Exit(GlyphNotDef);
// TODO : If font contains a symbol map then it is a symbol font and we should prefer the symbol map.
// DONE : Prefer 32-bit encoding over 16-bit; Prioritized map type list should be passed to FindMap:
for i := 0 to High(MapSearchOrder) do
begin
FBestCharacterMap := FindMap(MapSearchOrder[i].PlatformID, MapSearchOrder[i].EncodingID);
if (FBestCharacterMap <> nil) then
break;
end;
// If everything fails, just get one that we can handle
// TODO : Handle encoding
(*
if (FBestCharacterMap = nil) then
begin
for Map in FMaps do
begin
var Encoding := GetEncoding(Map.PlatformID, Map.EncodingID, 0); // TODO : How do we get Language here? Pass as parameter?
FBestCharacterMap := GetEncodingMap(Encoding);
if (FBestCharacterMap <> nil) then
break;
end;
end;
*)
if (FBestCharacterMap = nil) then
raise EPascalTypeError.Create(RCStrCharacterMapNotSet);
end;
Result := FBestCharacterMap.CharacterToGlyph(ACodePoint);
end;
class function TPascalTypeCharacterMapTable.GetTableType: TTableType;
begin
Result.AsAnsiChar := 'cmap';
end;
procedure TPascalTypeCharacterMapTable.LoadFromStream(Stream: TStream; Size: Cardinal);
var
StartPos : Int64;
i: Integer;
PlatformID: Word;
EncodingID: Word;
Offsets: array of Cardinal;
Map: TCustomPascalTypeCharacterMapDirectory;
begin
FBestCharacterMap := nil;
if (FMaps <> nil) then
FMaps.Clear;
// store stream start position
StartPos := Stream.Position;
inherited;
// Character Map Table Header:
// +----------------+---------------------------+-------------------------------------------------------------+
// | Type | Name | Description |
// +================+===========================+=============================================================+
// | uint16 | version | Table version number (=0). |
// +----------------+---------------------------+-------------------------------------------------------------+
// | uint16 | numTables | Number of encoding tables that follow. |
// +----------------+---------------------------+-------------------------------------------------------------+
// | EncodingRecord | encodingRecords[numTables]| Encoding tables. |
// +----------------+---------------------------+-------------------------------------------------------------+
// check (minimum) table size (table with at least one entry)
if Stream.Position + 4*SizeOf(Word) > Stream.Size then
raise EPascalTypeTableIncomplete.Create(RCStrTableIncomplete);
// read Version
FVersion := BigEndianValue.ReadWord(Stream);
// check version
if (FVersion <> 0) then
raise EPascalTypeError.Create(RCStrUnsupportedVersion);
// read subtable count
SetLength(Offsets, BigEndianValue.ReadWord(Stream));
if (Length(Offsets) = 0) then
exit;
// check (minimum) table size
if Stream.Position + Length(Offsets) * (2 * SizeOf(Word) + SizeOf(Cardinal)) > Stream.Size then
raise EPascalTypeTableIncomplete.Create(RCStrTableIncomplete);
if (FMaps = nil) then
FMaps := TObjectList<TCustomPascalTypeCharacterMapDirectory>.Create;
FMaps.Capacity := Length(Offsets);
// read directory entry
for i := 0 to High(Offsets) do
begin
// read Platform ID
PlatformID := BigEndianValue.ReadWord(Stream);
// read encoding ID
EncodingID := BigEndianValue.ReadWord(Stream);
// create character map based on encoding
case TPlatformID(PlatformID) of
piUnicode:
Map := TPascalTypeCharacterMapUnicodeDirectory.Create(Self, EncodingID);
piApple:
Map := TPascalTypeCharacterMapMacintoshDirectory.Create(Self, EncodingID);
piMicrosoft:
Map := TPascalTypeCharacterMapMicrosoftDirectory.Create(Self, EncodingID);
else
Map := TPascalTypeCharacterMapDirectoryGenericEntry.Create(Self, EncodingID);
TPascalTypeCharacterMapDirectoryGenericEntry(Map).FPlatformID := TPlatformID(PlatformID);
end;
FMaps.Add(Map);
// read and save offset
Offsets[i] := BigEndianValue.ReadCardinal(Stream);
end;
// load character map entries from stream
for i := 0 to High(Offsets) do
begin
Stream.Position := StartPos + Offsets[i];
FMaps[i].LoadFromStream(Stream);
end;
end;
procedure TPascalTypeCharacterMapTable.SaveToStream(Stream: TStream);
var
StartPos : Int64;
DirIndex : Integer;
Directory: array of Cardinal;
begin
// store stream start position
StartPos := Stream.Position;
inherited;
// write format type
BigEndianValue.WriteWord(Stream, FVersion);
// write directory entry count
BigEndianValue.WriteWord(Stream, CharacterMapSubtableCount);
if (CharacterMapSubtableCount > 0) then
begin
// offset directory
Stream.Seek(soFromCurrent, (2*SizeOf(Word)+SizeOf(Cardinal)) * CharacterMapSubtableCount);
// build directory (to be written later) and write data
SetLength(Directory, FMaps.Count);
for DirIndex := 0 to FMaps.Count-1 do
begin
Directory[DirIndex] := Cardinal(Stream.Position - StartPos);
FMaps[DirIndex].SaveToStream(Stream);
end;
// locate directory
Stream.Position := StartPos + 4;
for DirIndex := 0 to FMaps.Count-1 do
begin
// write format
BigEndianValue.WriteWord(Stream, Word(FMaps[DirIndex].PlatformID));
// write encoding ID
BigEndianValue.WriteWord(Stream, FMaps[DirIndex].EncodingID);
// write offset
BigEndianValue.WriteCardinal(Stream, Directory[DirIndex]);
end;
end;
end;
procedure TPascalTypeCharacterMapTable.SetVersion(const Value: Word);
begin
if FVersion <> Value then
begin
FVersion := Value;
CharacterMapDirectoryChanged;
end;
end;
procedure TPascalTypeCharacterMapTable.CharacterMapDirectoryChanged;
begin
Changed;
end;
initialization
PascalTypeTableClasses.RegisterTable(TPascalTypeCharacterMapTable);
end.