forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFontCreator.cpp
More file actions
1208 lines (1066 loc) · 50.8 KB
/
FontCreator.cpp
File metadata and controls
1208 lines (1066 loc) · 50.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
/*
* Copyright (C) 2015 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "FontCreator.h"
#include <array>
#include <cassert>
#include <string>
static const size_t headerSize = 12;
static const size_t directoryEntrySize = 16;
static const int16_t unitsPerEm = 1024;
static const uint16_t numGlyphs = 26 * 2 + 1;
static inline uint16_t integralLog2(uint16_t x)
{
uint16_t result = 0;
while (x >>= 1)
++result;
return result;
}
static inline uint16_t roundDownToPowerOfTwo(uint16_t x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
return (x >> 1) + 1;
}
static inline bool isFourByteAligned(size_t x)
{
return !(x & 3);
}
// Assumption: T2 can hold every value that a T1 can hold.
template<typename T1, typename T2> static inline T1 clampTo(T2 x)
{
x = std::min(x, static_cast<T2>(std::numeric_limits<T1>::max()));
x = std::max(x, static_cast<T2>(std::numeric_limits<T1>::min()));
return static_cast<T1>(x);
}
template <typename V>
static inline void append32(V& result, uint32_t value)
{
result.push_back(value >> 24);
result.push_back(value >> 16);
result.push_back(value >> 8);
result.push_back(value);
}
template <typename V>
static void writeCFFEncodedNumber(V& vector, float number)
{
vector.push_back(0xFF);
// Convert to 16.16 fixed-point
append32(vector, clampTo<int32_t>(number * 0x10000));
}
static const char rLineTo = 0x05;
static const char endChar = 0x0e;
static const char rMoveTo = 0x15;
class CFFBuilder {
public:
CFFBuilder(float width, std::pair<float, float> origin)
{
writeCFFEncodedNumber(result, width);
writeCFFEncodedNumber(result, origin.first);
writeCFFEncodedNumber(result, origin.second);
result.push_back(rMoveTo);
}
std::vector<uint8_t> takeResult()
{
result.push_back(endChar);
return std::move(result);
}
void moveTo(std::pair<float, float> targetPoint)
{
writePoint(targetPoint);
result.push_back(rMoveTo);
startingPoint = current;
}
void lineTo(std::pair<float, float> targetPoint)
{
writePoint(targetPoint);
result.push_back(rLineTo);
}
void closePath()
{
if (current != startingPoint)
lineTo(startingPoint);
}
private:
void writePoint(std::pair<float, float> destination)
{
std::pair<float, float> delta = std::make_pair(destination.first - current.first, destination.second - current.second);
writeCFFEncodedNumber(result, delta.first);
writeCFFEncodedNumber(result, delta.second);
current = destination;
}
std::vector<uint8_t> result;
std::pair<float, float> startingPoint;
std::pair<float, float> current;
};
class GLYFBuilder {
public:
GLYFBuilder(float, std::pair<float, float>)
{
}
std::vector<uint8_t> takeResult()
{
std::vector<uint8_t> result;
append16(result, endPtsOfContours.size());
append16(result, clampTo<int16_t>(minX));
append16(result, clampTo<int16_t>(minY));
append16(result, clampTo<int16_t>(maxX));
append16(result, clampTo<int16_t>(maxY));
for (uint16_t p : endPtsOfContours)
append16(result, p);
append16(result, 0);
for (uint8_t f : flags)
result.push_back(f);
for (uint16_t c : xCoordinates)
append16(result, c);
for (uint16_t c : yCoordinates)
append16(result, c);
return result;
}
void moveTo(std::pair<float, float> targetPoint)
{
writePoint(targetPoint, true);
startingPoint = current;
}
void lineTo(std::pair<float, float> targetPoint)
{
writePoint(targetPoint, true);
}
void closePath()
{
if (current != startingPoint)
lineTo(startingPoint);
endPtsOfContours.push_back(pointCount - 1);
}
private:
void writePoint(std::pair<float, float> destination, bool onCurve)
{
flags.push_back(onCurve ? 1 : 0); // Flags
std::pair<float, float> delta = std::make_pair(destination.first - current.first, destination.second - current.second);
xCoordinates.push_back(delta.first);
yCoordinates.push_back(delta.second);
current = destination;
minX = std::min(minX, destination.first);
maxX = std::max(maxX, destination.first);
minY = std::min(minY, destination.second);
maxY = std::max(maxY, destination.second);
++pointCount;
}
static void append16(std::vector<uint8_t>& destination, uint16_t value)
{
destination.push_back(value >> 8);
destination.push_back(value);
}
std::vector<uint16_t> endPtsOfContours;
std::vector<uint8_t> flags;
std::vector<int16_t> xCoordinates;
std::vector<int16_t> yCoordinates;
std::pair<float, float> startingPoint;
std::pair<float, float> current;
float minX { std::numeric_limits<float>::max() };
float maxX { std::numeric_limits<float>::min() };
float minY { std::numeric_limits<float>::max() };
float maxY { std::numeric_limits<float>::min() };
unsigned pointCount { 0 };
};
template <typename T>
std::vector<uint8_t> generateBoxCharString()
{
T builder(unitsPerEm, std::make_pair(0.f, 0.f));
builder.moveTo(std::make_pair(200.f, 200.f));
builder.lineTo(std::make_pair(200.f, 800.f));
builder.lineTo(std::make_pair(800.f, 800.f));
builder.lineTo(std::make_pair(800.f, 200.f));
builder.closePath();
return builder.takeResult();
}
template <typename T>
std::vector<uint8_t> generateCheckCharString()
{
T builder(unitsPerEm, std::make_pair(0.f, 0.f));
builder.moveTo(std::make_pair(200.f, 500.f));
builder.lineTo(std::make_pair(250.f, 550.f));
builder.lineTo(std::make_pair(500.f, 300.f));
builder.lineTo(std::make_pair(900.f, 700.f));
builder.lineTo(std::make_pair(950.f, 650.f));
builder.lineTo(std::make_pair(500.f, 200.f));
builder.closePath();
return builder.takeResult();
}
template <typename T>
std::vector<uint8_t> generateXCharString()
{
T builder(unitsPerEm, std::make_pair(0.f, 0.f));
builder.moveTo(std::make_pair(500.0f, 550.0f));
builder.lineTo(std::make_pair(900.f, 950.f));
builder.lineTo(std::make_pair(950.f, 900.f));
builder.lineTo(std::make_pair(550.f, 500.f));
builder.lineTo(std::make_pair(950.f, 100.f));
builder.lineTo(std::make_pair(900.f, 50.f));
builder.lineTo(std::make_pair(500.f, 450.f));
builder.lineTo(std::make_pair(100.f, 50.f));
builder.lineTo(std::make_pair(50.f , 100.f));
builder.lineTo(std::make_pair(450.f, 500.f));
builder.lineTo(std::make_pair(50.f , 900.f));
builder.lineTo(std::make_pair(100.f, 950.f));
builder.closePath();
return builder.takeResult();
}
template<typename T>
const T& itemForGlyph(uint16_t glyph, const T& boxCharString, const T& checkCharString, const T& xCharString)
{
if (!glyph)
return boxCharString;
if (glyph == 1)
return checkCharString;
return xCharString;
}
struct FeatureSelector {
uint16_t selector;
std::string name;
uint16_t stringIndex;
bool defaultSelector;
};
struct FeatureType {
uint16_t type;
std::string name;
uint16_t stringIndex;
size_t settingTableOffsetLocation;
std::vector<FeatureSelector> selectors;
bool exclusive;
};
class Generator {
public:
std::vector<uint8_t> generate(Type type)
{
if (type == Type::OpenType)
name = "FontWithFeaturesOTF";
else
name = "FontWithFeaturesTTF";
featureDescription = generateFeatureDescription();
uint16_t numTables = type == Type::OpenType ? 10 : 12;
uint16_t roundedNumTables = roundDownToPowerOfTwo(numTables);
uint16_t searchRange = roundedNumTables * 16; // searchRange: "(Maximum power of 2 <= numTables) x 16."
if (type == Type::OpenType) {
result.push_back('O');
result.push_back('T');
result.push_back('T');
result.push_back('O');
} else {
result.push_back('t');
result.push_back('r');
result.push_back('u');
result.push_back('e');
}
append16(numTables);
append16(searchRange);
append16(integralLog2(roundedNumTables)); // entrySelector: "Log2(maximum power of 2 <= numTables)."
append16(numTables * 16 - searchRange); // rangeShift: "NumTables x 16-searchRange."
assert(result.size() == headerSize);
// Leave space for the directory entries.
for (size_t i = 0; i < directoryEntrySize * numTables; ++i)
result.push_back(0);
if (type == Type::OpenType) {
appendTable("CFF ", &Generator::appendCFFTable);
appendTable("GSUB", &Generator::appendGSUBTable);
}
appendTable("OS/2", &Generator::appendOS2Table);
appendTable("cmap", &Generator::appendCMAPTable);
if (type == Type::TrueType) {
appendTable("feat", &Generator::appendFEATTable);
appendTable("glyf", &Generator::appendGLYFTable);
}
auto headTableOffset = result.size();
appendTable("head", &Generator::appendHEADTable);
appendTable("hhea", &Generator::appendHHEATable);
appendTable("hmtx", &Generator::appendHMTXTable);
if (type == Type::TrueType)
appendTable("loca", &Generator::appendLOCATable);
appendTable("maxp", &Generator::appendMAXPTable);
if (type == Type::TrueType)
appendTable("morx", &Generator::appendMORXTable);
appendTable("name", &Generator::appendNAMETable);
appendTable("post", &Generator::appendPOSTTable);
assert(numTables == m_tablesAppendedCount);
// checksumAdjustment: "To compute: set it to 0, calculate the checksum for the 'head' table and put it in the table directory,
// sum the entire font as uint32, then store B1B0AFBA - sum. The checksum for the 'head' table will now be wrong. That is OK."
overwrite32(headTableOffset + 8, 0xB1B0AFBAU - calculateChecksum(0, result.size()));
return std::move(result);
}
private:
class Placeholder {
public:
Placeholder(Generator& generator, size_t baseOfOffset)
: generator(generator)
, baseOfOffset(baseOfOffset)
, location(generator.result.size())
{
generator.append16(0);
}
Placeholder(Placeholder&& other)
: generator(other.generator)
, baseOfOffset(other.baseOfOffset)
, location(other.location)
, active(other.active)
{
other.active = false;
}
void populate()
{
assert(active);
size_t delta = generator.result.size() - baseOfOffset;
assert(delta < std::numeric_limits<uint16_t>::max());
generator.overwrite16(location, delta);
active = false;
}
~Placeholder()
{
assert(!active);
}
private:
Generator& generator;
const size_t baseOfOffset;
const size_t location;
bool active { true };
};
Placeholder placeholder(size_t baseOfOffset)
{
return Placeholder(*this, baseOfOffset);
}
void append16(uint16_t value)
{
result.push_back(value >> 8);
result.push_back(value);
}
void append32(uint32_t value)
{
::append32(result, value);
}
void append32BitCode(const char code[4])
{
result.push_back(code[0]);
result.push_back(code[1]);
result.push_back(code[2]);
result.push_back(code[3]);
}
void overwrite16(size_t location, uint16_t value)
{
assert(result.size() >= location + 2);
result[location] = value >> 8;
result[location + 1] = value;
}
void overwrite32(size_t location, uint32_t value)
{
assert(result.size() >= location + 4);
result[location] = value >> 24;
result[location + 1] = value >> 16;
result[location + 2] = value >> 8;
result[location + 3] = value;
}
void insertSelector(std::vector<FeatureSelector>& selectors, uint16_t selector, std::string selectorString, bool defaultSelector)
{
selectors.push_back({selector, selectorString, m_stringIndex++, defaultSelector});
}
void insertFeature(std::vector<FeatureType>& result, uint16_t type, std::string typeString, uint16_t selector, std::string selectorString, bool exclusive)
{
// O(n) but performance is not an issue here
for (size_t i = 0; i < result.size(); ++i) {
if (result[i].type == type) {
insertSelector(result[i].selectors, selector, selectorString, false);
return;
}
}
result.push_back({type, typeString, m_stringIndex++, 0, std::vector<FeatureSelector>(), exclusive});
insertSelector(result[result.size() - 1].selectors, selector, selectorString, true);
}
static const uint16_t kCharacterShapeType = 20;
static const uint16_t kContextualAlternatesType = 36;
static const uint16_t kFractionsType = 11;
static const uint16_t kLetterCaseType = 3;
static const uint16_t kLigaturesType = 1;
static const uint16_t kLowerCaseType = 37;
static const uint16_t kNumberCaseType = 21;
static const uint16_t kNumberSpacingType = 6;
static const uint16_t kRubyKanaType = 28;
static const uint16_t kStyleOptionsType = 19;
static const uint16_t kTextSpacingType = 22;
static const uint16_t kTypographicExtrasType = 14;
static const uint16_t kUpperCaseType = 38;
static const uint16_t kVerticalPositionType = 10;
static const uint16_t kCommonLigaturesOffSelector = 3;
static const uint16_t kCommonLigaturesOnSelector = 2;
static const uint16_t kContextualAlternatesOffSelector = 1;
static const uint16_t kContextualAlternatesOnSelector = 0;
static const uint16_t kContextualLigaturesOffSelector = 19;
static const uint16_t kContextualLigaturesOnSelector = 18;
static const uint16_t kDiagonalFractionsSelector = 2;
static const uint16_t kHistoricalLigaturesOffSelector = 21;
static const uint16_t kHistoricalLigaturesOnSelector = 20;
static const uint16_t kInferiorsSelector = 2;
static const uint16_t kJIS1978CharactersSelector = 2;
static const uint16_t kJIS1983CharactersSelector = 3;
static const uint16_t kJIS1990CharactersSelector = 4;
static const uint16_t kJIS2004CharactersSelector = 11;
static const uint16_t kLowerCaseNumbersSelector = 0;
static const uint16_t kLowerCasePetiteCapsSelector = 2;
static const uint16_t kLowerCaseSmallCapsSelector = 1;
static const uint16_t kMonospacedNumbersSelector = 0;
static const uint16_t kMonospacedTextSelector = 1;
static const uint16_t kOrdinalsSelector = 3;
static const uint16_t kProportionalNumbersSelector = 1;
static const uint16_t kProportionalTextSelector = 0;
static const uint16_t kRareLigaturesOffSelector = 5;
static const uint16_t kRareLigaturesOnSelector = 4;
static const uint16_t kRubyKanaOnSelector = 2;
static const uint16_t kRubyKanaSelector = 1;
static const uint16_t kSimplifiedCharactersSelector = 1;
static const uint16_t kSlashedZeroOnSelector = 4;
static const uint16_t kSuperiorsSelector = 1;
static const uint16_t kTitlingCapsSelector = 4;
static const uint16_t kTraditionalCharactersSelector = 0;
static const uint16_t kUpperCaseNumbersSelector = 1;
static const uint16_t kUpperCasePetiteCapsSelector = 2;
static const uint16_t kUpperCaseSmallCapsSelector = 1;
static const uint16_t kVerticalFractionsSelector = 1;
static const uint16_t defaultUnusedSelector = 99;
std::vector<FeatureType> generateFeatureDescription()
{
std::vector<FeatureType> result;
// For any given feature type, the first selector inside it is the default selector for that type.
insertFeature(result, kLigaturesType, "kLigaturesType", kCommonLigaturesOnSelector, "kCommonLigaturesOnSelector", false);
insertFeature(result, kLigaturesType, "kLigaturesType", kContextualLigaturesOnSelector, "kContextualLigaturesOnSelector", false);
insertFeature(result, kLigaturesType, "kLigaturesType", kCommonLigaturesOffSelector, "kCommonLigaturesOffSelector", false);
insertFeature(result, kLigaturesType, "kLigaturesType", kContextualLigaturesOffSelector, "kContextualLigaturesOffSelector", false);
insertFeature(result, kLigaturesType, "kLigaturesType", kRareLigaturesOnSelector, "kRareLigaturesOnSelector", false);
insertFeature(result, kLigaturesType, "kLigaturesType", kRareLigaturesOffSelector, "kRareLigaturesOffSelector", false);
insertFeature(result, kLigaturesType, "kLigaturesType", kHistoricalLigaturesOnSelector, "kHistoricalLigaturesOnSelector", false);
insertFeature(result, kLigaturesType, "kLigaturesType", kHistoricalLigaturesOffSelector, "kHistoricalLigaturesOffSelector", false);
insertFeature(result, kContextualAlternatesType, "kContextualAlternatesType", kContextualAlternatesOnSelector, "kContextualAlternatesOnSelector", false);
insertFeature(result, kContextualAlternatesType, "kContextualAlternatesType", kContextualAlternatesOffSelector, "kContextualAlternatesOffSelector", false);
insertFeature(result, kVerticalPositionType, "kVerticalPositionType", defaultUnusedSelector, "defaultUnusedSelector", true);
insertFeature(result, kVerticalPositionType, "kVerticalPositionType", kInferiorsSelector, "kInferiorsSelector", true);
insertFeature(result, kVerticalPositionType, "kVerticalPositionType", kSuperiorsSelector, "kSuperiorsSelector", true);
insertFeature(result, kLowerCaseType, "kLowerCaseType", defaultUnusedSelector, "defaultUnusedSelector", true);
insertFeature(result, kUpperCaseType, "kUpperCaseType", defaultUnusedSelector, "defaultUnusedSelector", true);
insertFeature(result, kLowerCaseType, "kLowerCaseType", kLowerCaseSmallCapsSelector, "kLowerCaseSmallCapsSelector", true);
insertFeature(result, kUpperCaseType, "kUpperCaseType", kUpperCaseSmallCapsSelector, "kUpperCaseSmallCapsSelector", true);
insertFeature(result, kLowerCaseType, "kLowerCaseType", kLowerCasePetiteCapsSelector, "kLowerCasePetiteCapsSelector", true);
insertFeature(result, kUpperCaseType, "kUpperCaseType", kUpperCasePetiteCapsSelector, "kUpperCasePetiteCapsSelector", true);
insertFeature(result, kLetterCaseType, "kLetterCaseType", defaultUnusedSelector, "defaultUnusedSelector", true);
insertFeature(result, kLetterCaseType, "kLetterCaseType", 14, "14", true);
insertFeature(result, kStyleOptionsType, "kStyleOptionsType", defaultUnusedSelector, "defaultUnusedSelector", true);
insertFeature(result, kStyleOptionsType, "kStyleOptionsType", kTitlingCapsSelector, "kTitlingCapsSelector", true);
insertFeature(result, kNumberCaseType, "kNumberCaseType", defaultUnusedSelector, "defaultUnusedSelector", true);
insertFeature(result, kNumberCaseType, "kNumberCaseType", kUpperCaseNumbersSelector, "kUpperCaseNumbersSelector", true);
insertFeature(result, kNumberCaseType, "kNumberCaseType", kLowerCaseNumbersSelector, "kLowerCaseNumbersSelector", true);
insertFeature(result, kNumberSpacingType, "kNumberSpacingType", defaultUnusedSelector, "defaultUnusedSelector", true);
insertFeature(result, kNumberSpacingType, "kNumberSpacingType", kProportionalNumbersSelector, "kProportionalNumbersSelector", true);
insertFeature(result, kNumberSpacingType, "kNumberSpacingType", kMonospacedNumbersSelector, "kMonospacedNumbersSelector", true);
insertFeature(result, kFractionsType, "kFractionsType", defaultUnusedSelector, "defaultUnusedSelector", true);
insertFeature(result, kFractionsType, "kFractionsType", kDiagonalFractionsSelector, "kDiagonalFractionsSelector", true);
insertFeature(result, kFractionsType, "kFractionsType", kVerticalFractionsSelector, "kVerticalFractionsSelector", true);
insertFeature(result, kVerticalPositionType, "kVerticalPositionType", kOrdinalsSelector, "kOrdinalsSelector", true);
insertFeature(result, kTypographicExtrasType, "kTypographicExtrasType", kSlashedZeroOnSelector, "kSlashedZeroOnSelector", false);
insertFeature(result, kLigaturesType, "kLigaturesType", kHistoricalLigaturesOnSelector, "kHistoricalLigaturesOnSelector", false);
insertFeature(result, kCharacterShapeType, "kCharacterShapeType", defaultUnusedSelector, "defaultUnusedSelector", true);
insertFeature(result, kCharacterShapeType, "kCharacterShapeType", kJIS1978CharactersSelector, "kJIS1978CharactersSelector", true);
insertFeature(result, kCharacterShapeType, "kCharacterShapeType", kJIS1983CharactersSelector, "kJIS1983CharactersSelector", true);
insertFeature(result, kCharacterShapeType, "kCharacterShapeType", kJIS1990CharactersSelector, "kJIS1990CharactersSelector", true);
insertFeature(result, kCharacterShapeType, "kCharacterShapeType", kJIS2004CharactersSelector, "kJIS2004CharactersSelector", true);
insertFeature(result, kCharacterShapeType, "kCharacterShapeType", kSimplifiedCharactersSelector, "kSimplifiedCharactersSelector", true);
insertFeature(result, kCharacterShapeType, "kCharacterShapeType", kTraditionalCharactersSelector, "kTraditionalCharactersSelector", true);
insertFeature(result, kTextSpacingType, "kTextSpacingType", defaultUnusedSelector, "defaultUnusedSelector", true);
insertFeature(result, kTextSpacingType, "kTextSpacingType", kMonospacedTextSelector, "kMonospacedTextSelector", true);
insertFeature(result, kTextSpacingType, "kTextSpacingType", kProportionalTextSelector, "kProportionalTextSelector", true);
insertFeature(result, kRubyKanaType, "kRubyKanaType", kRubyKanaOnSelector, "kRubyKanaOnSelector", false);
return result;
}
void appendCFFTable()
{
auto startingOffset = result.size();
// Header
result.push_back(1); // Major version
result.push_back(0); // Minor version
result.push_back(4); // Header size
result.push_back(4); // Offsets within CFF table are 4 bytes long
// Name INDEX
std::string fontName = name;
append16(1); // INDEX contains 1 element
result.push_back(4); // Offsets in this INDEX are 4 bytes long
append32(1); // 1-index offset of name data
append32(static_cast<uint32_t>(fontName.length() + 1)); // 1-index offset just past end of name data
for (char c : fontName)
result.push_back(c);
const char operand32Bit = 29;
const char fullNameKey = 2;
const char familyNameKey = 3;
const char fontBBoxKey = 5;
const char charsetIndexKey = 15;
const char charstringsIndexKey = 17;
const char privateDictIndexKey = 18;
const uint32_t userDefinedStringStartIndex = 391;
const unsigned sizeOfTopIndex = 56;
// Top DICT INDEX.
append16(1); // INDEX contains 1 element
result.push_back(4); // Offsets in this INDEX are 4 bytes long
append32(1); // 1-index offset of DICT data
append32(1 + sizeOfTopIndex); // 1-index offset just past end of DICT data
// DICT information
size_t topDictStart = result.size();
result.push_back(operand32Bit);
append32(userDefinedStringStartIndex);
result.push_back(fullNameKey);
result.push_back(operand32Bit);
append32(userDefinedStringStartIndex);
result.push_back(familyNameKey);
result.push_back(operand32Bit);
append32(clampTo<int32_t>(0)); // Bounding box x
result.push_back(operand32Bit);
append32(clampTo<int32_t>(-1)); // Bounding box y
result.push_back(operand32Bit);
append32(clampTo<int32_t>(unitsPerEm)); // Bounding box max x
result.push_back(operand32Bit);
append32(clampTo<int32_t>(unitsPerEm)); // Bounding box max y
result.push_back(fontBBoxKey);
result.push_back(operand32Bit);
size_t charsetOffsetLocation = result.size();
append32(0); // Offset of Charset info. Will be overwritten later.
result.push_back(charsetIndexKey);
result.push_back(operand32Bit);
size_t charstringsOffsetLocation = result.size();
append32(0); // Offset of CharStrings INDEX. Will be overwritten later.
result.push_back(charstringsIndexKey);
result.push_back(operand32Bit);
append32(0); // 0-sized private dict
result.push_back(operand32Bit);
append32(0); // no location for private dict
result.push_back(privateDictIndexKey); // Private dict size and offset
assert(result.size() == topDictStart + sizeOfTopIndex);
// String INDEX
append16(1); // Number of elements in INDEX
result.push_back(4); // Offsets in this INDEX are 4 bytes long
uint32_t offset = 1;
append32(offset);
offset += fontName.length();
append32(offset);
for (char c : fontName)
result.push_back(c);
append16(0); // Empty subroutine INDEX
// Charset info
overwrite32(charsetOffsetLocation, static_cast<uint32_t>(result.size() - startingOffset));
result.push_back(0);
for (int i = 1; i < numGlyphs; ++i)
append16(i);
// CharStrings INDEX
std::vector<uint8_t> boxCharString = generateBoxCharString<CFFBuilder>();
std::vector<uint8_t> checkCharString = generateCheckCharString<CFFBuilder>();
std::vector<uint8_t> xCharString = generateXCharString<CFFBuilder>();
assert(numGlyphs > 26);
overwrite32(charstringsOffsetLocation, static_cast<uint32_t>(result.size() - startingOffset));
append16(numGlyphs);
result.push_back(4); // Offsets in this INDEX are 4 bytes long
offset = 1;
append32(offset);
for (uint16_t glyph = 0; glyph < numGlyphs; ++glyph) {
offset += itemForGlyph(glyph, boxCharString, checkCharString, xCharString).size();
append32(offset);
}
for (uint16_t glyph = 0; glyph < numGlyphs; ++glyph) {
const std::vector<uint8_t>& charString = itemForGlyph(glyph, boxCharString, checkCharString, xCharString);
result.insert(result.end(), charString.begin(), charString.end());
}
}
// Keep in sync with loca
void appendGLYFTable()
{
std::vector<uint8_t> boxCharString = generateBoxCharString<GLYFBuilder>();
std::vector<uint8_t> checkCharString = generateCheckCharString<GLYFBuilder>();
std::vector<uint8_t> xCharString = generateXCharString<GLYFBuilder>();
for (uint16_t glyph = 0; glyph < numGlyphs; ++glyph) {
const std::vector<uint8_t>& charString = itemForGlyph(glyph, boxCharString, checkCharString, xCharString);
result.insert(result.end(), charString.begin(), charString.end());
}
}
// Keep in sync with glyf
void appendLOCATable()
{
std::vector<uint8_t> boxCharString = generateBoxCharString<GLYFBuilder>();
std::vector<uint8_t> checkCharString = generateCheckCharString<GLYFBuilder>();
std::vector<uint8_t> xCharString = generateXCharString<GLYFBuilder>();
uint32_t index = 0;
for (uint16_t glyph = 0; glyph < numGlyphs; ++glyph) {
append32(index);
index += itemForGlyph(glyph, boxCharString, checkCharString, xCharString).size();
}
append32(index);
}
void appendFEATTable()
{
size_t tableLocation = result.size();
append32(0x00010000); // Version
append16(featureDescription.size()); // Number of entries in the feature name array
append16(0); // reserved
append32(0); // reserved
// Feature name array
for (FeatureType& type : featureDescription) {
append16(type.type); // Feature type
append16(type.selectors.size()); // Number of settings
type.settingTableOffsetLocation = result.size();
append32(0); // Offset in bytes from beginning of this table to feature's setting name array
append16(type.exclusive ? 0x8000 : 0); // Flags. 0x8000 = Exclusive
append16(type.stringIndex + m_baseStringIndex); // Index in the name table for the name of this feature
}
// Setting name array
for (FeatureType& type : featureDescription) {
overwrite32(type.settingTableOffsetLocation, static_cast<uint32_t>(result.size() - tableLocation));
for (FeatureSelector& selector : type.selectors) {
append16(selector.selector); // Setting: kNormalPositionSelector (initial setting is default)
append16(selector.stringIndex + m_baseStringIndex); // Index in the name table for the name of this setting
}
}
}
void appendMetamorphosisChain(const FeatureType& type, const FeatureSelector& selector, uint16_t glyphToReplace, uint16_t withMe)
{
size_t chainLocation = result.size();
append32(type.exclusive && selector.defaultSelector ? 1 : 0); // Default flags
size_t chainSizeLocation = result.size();
append32(0); // Placeholder for chain length in bytes (padded to multiple of 4)
append32(2); // Number of feature subtable entries
append32(1); // Number of subtables in the chain
// Feature table
append16(type.type); // Feature type
append16(selector.selector); // Feature selector
append32(1); // Enable flags
append32(0xFFFFFFFF); // disable flags
// Feature table 2
append16(0); // Feature type: kAllTypographicFeaturesType
append16(1); // Feature selector: kAllTypeFeaturesOffSelector
append32(0); // Enable flags
append32(0); // disable flags
// Metamorphosis subtable
size_t metamorphosisSubtableSizeLocation = result.size();
append32(0); // Placeholder for chain length in bytes (padded to multiple of 4)
append32(4); // Coverage flags and subtable type. Subtable type 4: Noncontextual ("swash") subtable
append32(1); // subFeature flags
// Non-contextual glyph substitution subtable
append16(6); // Lookup format: sorted list of (glyph index, lookup value) pairs
// BinSrchHeader
append16(4); // Size of a lookup unit for this search in bytes
append16(1); // Number of units to be searched
append16(4); // Search range: The value of unitSize times the largest power of 2 that is less than or equal to the value of nUnits.
append16(0); // Entry selector: The log base 2 of the largest power of 2 less than or equal to the value of nUnits.
append16(0); // Range shift: The value of unitSize times the difference of the value of nUnits minus the largest power of 2 less than or equal to the value of nUnits.
// Entries
append16(glyphToReplace);
append16(withMe);
overwrite32(metamorphosisSubtableSizeLocation, static_cast<uint32_t>(result.size() - metamorphosisSubtableSizeLocation));
while (result.size() % 4)
result.push_back(0);
overwrite32(chainSizeLocation, static_cast<uint32_t>(result.size() - chainLocation));
}
void appendMORXTable()
{
append16(2); // Version
append16(0); // Unused
size_t numberOfChainsLocation = result.size();
append32(0); // Number of metamorphosis chains placeholder
int count = 0;
for (FeatureType& type : featureDescription) {
for (FeatureSelector& selector : type.selectors) {
appendMetamorphosisChain(type, selector, count + 3, 1);
count++;
}
}
overwrite32(numberOfChainsLocation, count);
}
void appendSubstitutionSubtable(size_t subtableRecordLocation, uint16_t iGetReplaced, uint16_t replacedWithMe)
{
overwrite16(subtableRecordLocation + 6, result.size() - subtableRecordLocation);
auto subtableLocation = result.size();
append16(2); // Format 2
append16(0); // Placeholder for offset to coverage table, relative to beginning of substitution table
append16(1); // GlyphCount
append16(replacedWithMe); // Substitute with this glyph.
// Coverage table
overwrite16(subtableLocation + 2, result.size() - subtableLocation);
append16(1); // CoverageFormat
append16(1); // GlyphCount
append16(iGetReplaced); // This glyph is covered in the coverage.
}
void appendScriptSubtable(uint16_t featureCount)
{
auto dfltScriptTableLocation = result.size();
append16(0); // Placeholder for offset of default language system table, relative to beginning of Script table
append16(0); // Number of following language system tables
// LangSys table
overwrite16(dfltScriptTableLocation, result.size() - dfltScriptTableLocation);
append16(0); // LookupOrder "= NULL ... reserved"
append16(0xFFFF); // No features are required
append16(featureCount); // Number of FeatureIndex values
for (uint16_t i = 0; i < featureCount; ++i)
append16(i); // Features indices
}
struct Feature {
bool operator<(const Feature& o) const
{
return tag < o.tag;
}
std::array<char, 5> tag;
uint16_t glyphToReplace;
};
void appendGSUBTable()
{
std::vector<Feature> features {{{"liga"}, 3}, {{"clig"}, 4}, {{"dlig"}, 5}, {{"hlig"}, 6}, {{"calt"}, 7}, {{"subs"}, 8}, {{"sups"}, 9}, {{"smcp"}, 10}, {{"c2sc"}, 11}, {{"pcap"}, 12}, {{"c2pc"}, 13}, {{"unic"}, 14}, {{"titl"}, 15}, {{"lnum"}, 16}, {{"onum"}, 17}, {{"pnum"}, 18}, {{"tnum"}, 19}, {{"frac"}, 20}, {{"afrc"}, 21}, {{"ordn"}, 22}, {{"zero"}, 23}, {{"hist"}, 24}, {{"jp78"}, 25}, {{"jp83"}, 26}, {{"jp90"}, 27}, {{"jp04"}, 28}, {{"smpl"}, 29}, {{"trad"}, 30}, {{"fwid"}, 31}, {{"pwid"}, 32}, {{"ruby"}, 33}};
std::sort(features.begin(), features.end());
auto tableLocation = result.size();
auto headerSize = 10;
append32(0x00010000); // Version
append16(headerSize); // Offset to ScriptList
Placeholder toFeatureList = placeholder(tableLocation);
Placeholder toLookupList = placeholder(tableLocation);
assert(tableLocation + headerSize == result.size());
// ScriptList
auto scriptListLocation = result.size();
append16(1); // Number of ScriptRecords
append32BitCode("DFLT");
append16(0); // Placeholder for offset of Script table, relative to beginning of ScriptList
overwrite16(scriptListLocation + 6, result.size() - scriptListLocation);
appendScriptSubtable(static_cast<uint16_t>(features.size()));
// FeatureList
toFeatureList.populate();
auto featureListLocation = result.size();
size_t featureListSize = 2 + 6 * features.size();
size_t featureTableSize = 6;
append16(features.size()); // FeatureCount
for (unsigned i = 0; i < features.size(); ++i) {
auto& code = features[i];
append32BitCode(code.tag.data()); // Feature name
append16(featureListSize + featureTableSize * i); // Offset of feature table, relative to beginning of FeatureList table
}
assert(featureListLocation + featureListSize == result.size());
for (unsigned i = 0; i < features.size(); ++i) {
auto featureTableStart = result.size();
append16(0); // FeatureParams "= NULL ... reserved"
append16(1); // LookupCount
append16(i); // LookupListIndex
assert(featureTableStart + featureTableSize == result.size());
}
// LookupList
toLookupList.populate();
auto lookupListLocation = result.size();
append16(features.size()); // LookupCount
for (unsigned i = 0; i < features.size(); ++i)
append16(0); // Placeholder for offset to lookup table, relative to beginning of LookupList
size_t subtableRecordLocations[features.size()];
for (unsigned i = 0; i < features.size(); ++i) {
subtableRecordLocations[i] = result.size();
overwrite16(lookupListLocation + 2 + 2 * i, result.size() - lookupListLocation);
append16(1); // Type 1: "Replace one glyph with one glyph"
append16(0); // LookupFlag
append16(1); // SubTableCount
append16(0); // Placeholder for offset to subtable, relative to beginning of Lookup table
}
for (unsigned i = 0; i < features.size(); ++i)
appendSubstitutionSubtable(subtableRecordLocations[i], features[i].glyphToReplace, 1);
}
void appendOS2Table()
{
append16(2); // Version
append16(clampTo<int16_t>(unitsPerEm)); // Average advance
append16(clampTo<uint16_t>(500)); // Weight class
append16(5); // Width class
append16(0); // Protected font
// WebKit handles these superscripts and subscripts
append16(0); // Subscript X Size
append16(0); // Subscript Y Size
append16(0); // Subscript X Offset
append16(0); // Subscript Y Offset
append16(0); // Superscript X Size
append16(0); // Superscript Y Size
append16(0); // Superscript X Offset
append16(0); // Superscript Y Offset
append16(0); // Strikeout width
append16(0); // Strikeout Position
append16(0); // No classification
for (int i = 0; i < 10; ++i)
result.push_back(0);
for (int i = 0; i < 4; ++i)
append32(0); // "Bit assignments are pending. Set to 0"
append32(0x544B4257); // Font Vendor. "WBKT"
append16(0); // Font Patterns.
append16(0); // First unicode index
append16(0xFFFF); // Last unicode index
append16(clampTo<int16_t>(unitsPerEm)); // Typographical ascender
append16(clampTo<int16_t>(-1)); // Typographical descender
append16(clampTo<int16_t>(unitsPerEm / 10)); // Typographical line gap
append16(clampTo<uint16_t>(unitsPerEm)); // Windows-specific ascent
append16(clampTo<uint16_t>(0)); // Windows-specific descent
append32(0xFF10FC07); // Bitmask for supported codepages (Part 1). Report all pages as supported.
append32(0x0000FFFF); // Bitmask for supported codepages (Part 2). Report all pages as supported.
append16(clampTo<int16_t>(unitsPerEm / 2)); // x-height
append16(clampTo<int16_t>(unitsPerEm)); // Cap-height
append16(0); // Default char
append16(' '); // Break character
append16(3); // Maximum context needed to perform font features
append16(3); // Smallest optical point size
append16(0xFFFF); // Largest optical point size
}
void appendFormat12CMAPTable()
{
// Braindead scheme: One segment for each character
auto subtableLocation = result.size();
append32(12 << 16); // Format 12
append32(0); // Placeholder for byte length
append32(0); // Language independent
append32(2); // nGroups
append32('A'); // startCharCode
append32('Z'); // endCharCode
append32(1); // startGlyphCode
append32('a'); // startCharCode
append32('z'); // endCharCode
append32(27); // startGlyphCode
overwrite32(subtableLocation + 4, static_cast<uint32_t>(result.size() - subtableLocation));
}
void appendFormat4CMAPTable()
{
auto subtableLocation = result.size();
append16(4); // Format 4
append16(0); // Placeholder for length in bytes
append16(0); // Language independent
uint16_t segCount = 3;
append16(clampTo<uint16_t>(2 * segCount)); // segCountX2: "2 x segCount"
uint16_t originalSearchRange = roundDownToPowerOfTwo(segCount);
uint16_t searchRange = clampTo<uint16_t>(2 * originalSearchRange); // searchRange: "2 x (2**floor(log2(segCount)))"
append16(searchRange);
append16(integralLog2(originalSearchRange)); // entrySelector: "log2(searchRange/2)"
append16(clampTo<uint16_t>((2 * segCount) - searchRange)); // rangeShift: "2 x segCount - searchRange"
// Ending character codes
append16('Z');
append16('z');
append16(0xFFFF);
append16(0); // reserved
// Starting character codes
append16('A');
append16('a');
append16(0xFFFF);
// idDelta
append16(static_cast<uint16_t>(27) - static_cast<uint16_t>('A'));
append16(static_cast<uint16_t>(1) - static_cast<uint16_t>('a'));
append16(0x0001);
// idRangeOffset
append16(0); // idRangeOffset
append16(0);
append16(0);
// Fonts strive to hold 2^16 glyphs, but with the current encoding scheme, we write 8 bytes per codepoint into this subtable.
// Because the size of this subtable must be represented as a 16-bit number, we are limiting the number of glyphs we support to 2^13.
// FIXME: If we hit this limit in the wild, use a more compact encoding scheme for this subtable.
overwrite16(subtableLocation + 2, clampTo<uint16_t>(result.size() - subtableLocation));
}