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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
|
/*
* Copyright (C) 2014-2017 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 "config.h"
#include "SVGToOTFFontConversion.h"
#include "CSSStyleDeclaration.h"
#include "ElementChildIteratorInlines.h"
#include "Glyph.h"
#include "HTMLParserIdioms.h"
#include "SVGElementTypeHelpers.h"
#include "SVGFontElement.h"
#include "SVGFontFaceElement.h"
#include "SVGGlyphElement.h"
#include "SVGHKernElement.h"
#include "SVGMissingGlyphElement.h"
#include "SVGPathParser.h"
#include "SVGPathStringViewSource.h"
#include "SVGVKernElement.h"
#include <wtf/Vector.h>
#include <wtf/text/StringToIntegerConversion.h>
#include <wtf/text/StringView.h>
namespace WebCore {
template <typename V>
static inline void append32(V& result, uint32_t value)
{
result.append(value >> 24);
result.append(value >> 16);
result.append(value >> 8);
result.append(value);
}
class SVGToOTFFontConverter {
public:
SVGToOTFFontConverter(const SVGFontElement&);
bool convertSVGToOTFFont();
Vector<uint8_t> releaseResult()
{
return WTFMove(m_result);
}
bool error() const
{
return m_error;
}
private:
struct GlyphData {
GlyphData(Vector<char>&& charString, const SVGGlyphElement* glyphElement, float horizontalAdvance, float verticalAdvance, FloatRect boundingBox, const String& codepoints)
: boundingBox(boundingBox)
, charString(charString)
, codepoints(codepoints)
, glyphElement(glyphElement)
, horizontalAdvance(horizontalAdvance)
, verticalAdvance(verticalAdvance)
{
}
FloatRect boundingBox;
Vector<char> charString;
String codepoints;
const SVGGlyphElement* glyphElement;
float horizontalAdvance;
float verticalAdvance;
};
class Placeholder {
public:
Placeholder(SVGToOTFFontConverter& converter, size_t baseOfOffset)
: m_converter(converter)
, m_baseOfOffset(baseOfOffset)
, m_location(m_converter.m_result.size())
{
m_converter.append16(0);
}
Placeholder(Placeholder&& other)
: m_converter(other.m_converter)
, m_baseOfOffset(other.m_baseOfOffset)
, m_location(other.m_location)
#if ASSERT_ENABLED
, m_active(other.m_active)
#endif
{
#if ASSERT_ENABLED
other.m_active = false;
#endif
}
void populate()
{
ASSERT(m_active);
size_t delta = m_converter.m_result.size() - m_baseOfOffset;
ASSERT(delta < std::numeric_limits<uint16_t>::max());
m_converter.overwrite16(m_location, delta);
#if ASSERT_ENABLED
m_active = false;
#endif
}
~Placeholder()
{
ASSERT(!m_active);
}
private:
SVGToOTFFontConverter& m_converter;
const size_t m_baseOfOffset;
const size_t m_location;
#if ASSERT_ENABLED
bool m_active = { true };
#endif
};
struct KerningData {
KerningData(uint16_t glyph1, uint16_t glyph2, int16_t adjustment)
: glyph1(glyph1)
, glyph2(glyph2)
, adjustment(adjustment)
{
}
uint16_t glyph1;
uint16_t glyph2;
int16_t adjustment;
};
Placeholder placeholder(size_t baseOfOffset)
{
return Placeholder(*this, baseOfOffset);
}
void append32(uint32_t value)
{
WebCore::append32(m_result, value);
}
void append32BitCode(const char code[4])
{
m_result.append(code[0]);
m_result.append(code[1]);
m_result.append(code[2]);
m_result.append(code[3]);
}
void append16(uint16_t value)
{
m_result.append(value >> 8);
m_result.append(value);
}
void grow(size_t delta)
{
m_result.grow(m_result.size() + delta);
}
void overwrite32(unsigned location, uint32_t value)
{
ASSERT(m_result.size() >= location + 4);
m_result[location] = value >> 24;
m_result[location + 1] = value >> 16;
m_result[location + 2] = value >> 8;
m_result[location + 3] = value;
}
void overwrite16(unsigned location, uint16_t value)
{
ASSERT(m_result.size() >= location + 2);
m_result[location] = value >> 8;
m_result[location + 1] = value;
}
static const size_t headerSize = 12;
static const size_t directoryEntrySize = 16;
uint32_t calculateChecksum(size_t startingOffset, size_t endingOffset) const;
void processGlyphElement(const SVGElement& glyphOrMissingGlyphElement, const SVGGlyphElement*, float defaultHorizontalAdvance, float defaultVerticalAdvance, const String& codepoints, std::optional<FloatRect>& boundingBox);
typedef void (SVGToOTFFontConverter::*FontAppendingFunction)();
void appendTable(const char identifier[4], FontAppendingFunction);
void appendFormat12CMAPTable(const Vector<std::pair<UChar32, Glyph>>& codepointToGlyphMappings);
void appendFormat4CMAPTable(const Vector<std::pair<UChar32, Glyph>>& codepointToGlyphMappings);
void appendCMAPTable();
void appendGSUBTable();
void appendHEADTable();
void appendHHEATable();
void appendHMTXTable();
void appendVHEATable();
void appendVMTXTable();
void appendKERNTable();
void appendMAXPTable();
void appendNAMETable();
void appendOS2Table();
void appendPOSTTable();
void appendCFFTable();
void appendVORGTable();
void appendLigatureGlyphs();
static bool compareCodepointsLexicographically(const GlyphData&, const GlyphData&);
void appendValidCFFString(const String&);
Vector<char> transcodeGlyphPaths(float width, const SVGElement& glyphOrMissingGlyphElement, std::optional<FloatRect>& boundingBox) const;
void addCodepointRanges(const UnicodeRanges&, HashSet<Glyph>& glyphSet) const;
void addCodepoints(const HashSet<String>& codepoints, HashSet<Glyph>& glyphSet) const;
void addGlyphNames(const HashSet<String>& glyphNames, HashSet<Glyph>& glyphSet) const;
void addKerningPair(Vector<KerningData>&, SVGKerningPair&&) const;
template<typename T> size_t appendKERNSubtable(std::optional<SVGKerningPair> (T::*buildKerningPair)() const, uint16_t coverage);
size_t finishAppendingKERNSubtable(Vector<KerningData>, uint16_t coverage);
void appendLigatureSubtable(size_t subtableRecordLocation);
void appendArabicReplacementSubtable(size_t subtableRecordLocation, ASCIILiteral arabicForm);
void appendScriptSubtable(unsigned featureCount);
Vector<Glyph, 1> glyphsForCodepoint(UChar32) const;
Glyph firstGlyph(const Vector<Glyph, 1>&, UChar32) const;
template<typename T> T scaleUnitsPerEm(T value) const
{
return value * s_outputUnitsPerEm / m_inputUnitsPerEm;
}
Vector<GlyphData> m_glyphs;
HashMap<String, Glyph> m_glyphNameToIndexMap; // SVG 1.1: "It is recommended that glyph names be unique within a font."
HashMap<String, Vector<Glyph, 1>> m_codepointsToIndicesMap;
Vector<uint8_t> m_result;
Vector<char, 17> m_emptyGlyphCharString;
FloatRect m_boundingBox;
const SVGFontElement& m_fontElement;
const SVGFontFaceElement* m_fontFaceElement;
const SVGMissingGlyphElement* m_missingGlyphElement;
String m_fontFamily;
float m_advanceWidthMax;
float m_advanceHeightMax;
float m_minRightSideBearing;
static const unsigned s_outputUnitsPerEm = 1000;
unsigned m_inputUnitsPerEm;
int m_lineGap;
int m_xHeight;
int m_capHeight;
int m_ascent;
int m_descent;
unsigned m_featureCountGSUB;
unsigned m_tablesAppendedCount;
uint8_t m_weight;
bool m_italic;
bool m_error { false };
};
static uint16_t roundDownToPowerOfTwo(uint16_t x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
return (x >> 1) + 1;
}
static uint16_t integralLog2(uint16_t x)
{
uint16_t result = 0;
while (x >>= 1)
++result;
return result;
}
void SVGToOTFFontConverter::appendFormat12CMAPTable(const Vector<std::pair<UChar32, Glyph>>& mappings)
{
// Braindead scheme: One segment for each character
ASSERT(m_glyphs.size() < 0xFFFF);
auto subtableLocation = m_result.size();
append32(12 << 16); // Format 12
append32(0); // Placeholder for byte length
append32(0); // Language independent
append32(0); // Placeholder for nGroups
for (auto& mapping : mappings) {
append32(mapping.first); // startCharCode
append32(mapping.first); // endCharCode
append32(mapping.second); // startGlyphCode
}
overwrite32(subtableLocation + 4, m_result.size() - subtableLocation);
overwrite32(subtableLocation + 12, mappings.size());
}
void SVGToOTFFontConverter::appendFormat4CMAPTable(const Vector<std::pair<UChar32, Glyph>>& bmpMappings)
{
auto subtableLocation = m_result.size();
append16(4); // Format 4
append16(0); // Placeholder for length in bytes
append16(0); // Language independent
uint16_t segCount = bmpMappings.size() + 1;
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
for (auto& mapping : bmpMappings)
append16(mapping.first); // startCharCode
append16(0xFFFF);
append16(0); // reserved
// Starting character codes
for (auto& mapping : bmpMappings)
append16(mapping.first); // startCharCode
append16(0xFFFF);
// idDelta
for (auto& mapping : bmpMappings)
append16(static_cast<uint16_t>(mapping.second) - static_cast<uint16_t>(mapping.first)); // startCharCode
append16(0x0001);
// idRangeOffset
for (size_t i = 0; i < bmpMappings.size(); ++i)
append16(0); // startCharCode
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>(m_result.size() - subtableLocation));
}
void SVGToOTFFontConverter::appendCMAPTable()
{
auto startingOffset = m_result.size();
append16(0);
append16(3); // Number of subtables
append16(0); // Unicode
append16(3); // Unicode version 2.2+
append32(28); // Byte offset of subtable
append16(3); // Microsoft
append16(1); // Unicode BMP
auto format4OffsetLocation = m_result.size();
append32(0); // Byte offset of subtable
append16(3); // Microsoft
append16(10); // Unicode
append32(28); // Byte offset of subtable
Vector<std::pair<UChar32, Glyph>> mappings;
UChar32 previousCodepoint = std::numeric_limits<UChar32>::max();
for (size_t i = 0; i < m_glyphs.size(); ++i) {
auto& glyph = m_glyphs[i];
UChar32 codepoint;
auto codePoints = StringView(glyph.codepoints).codePoints();
auto iterator = codePoints.begin();
if (iterator == codePoints.end())
codepoint = 0;
else {
codepoint = *iterator;
++iterator;
// Don't map ligatures here.
if (iterator != codePoints.end() || codepoint == previousCodepoint)
continue;
}
mappings.append(std::make_pair(codepoint, Glyph(i)));
previousCodepoint = codepoint;
}
appendFormat12CMAPTable(mappings);
Vector<std::pair<UChar32, Glyph>> bmpMappings;
for (auto& mapping : mappings) {
if (mapping.first < 0x10000)
bmpMappings.append(mapping);
}
overwrite32(format4OffsetLocation, m_result.size() - startingOffset);
appendFormat4CMAPTable(bmpMappings);
}
void SVGToOTFFontConverter::appendHEADTable()
{
append32(0x00010000); // Version
append32(0x00010000); // Revision
append32(0); // Checksum placeholder; to be overwritten by the caller.
append32(0x5F0F3CF5); // Magic number.
append16((1 << 9) | 1);
append16(s_outputUnitsPerEm);
append32(0); // First half of creation date
append32(0); // Last half of creation date
append32(0); // First half of modification date
append32(0); // Last half of modification date
append16(clampTo<int16_t>(m_boundingBox.x()));
append16(clampTo<int16_t>(m_boundingBox.y()));
append16(clampTo<int16_t>(m_boundingBox.maxX()));
append16(clampTo<int16_t>(m_boundingBox.maxY()));
append16((m_italic ? 1 << 1 : 0) | (m_weight >= 7 ? 1 : 0));
append16(3); // Smallest readable size in pixels
append16(0); // Might contain LTR or RTL glyphs
append16(0); // Short offsets in the 'loca' table. However, CFF fonts don't have a 'loca' table so this is irrelevant
append16(0); // Glyph data format
}
void SVGToOTFFontConverter::appendHHEATable()
{
append32(0x00010000); // Version
append16(clampTo<int16_t>(m_ascent));
append16(clampTo<int16_t>(-m_descent));
// WebKit SVG font rendering has hard coded the line gap to be 1/10th of the font size since 2008 (see r29719).
append16(clampTo<int16_t>(m_lineGap));
append16(clampTo<uint16_t>(m_advanceWidthMax));
append16(clampTo<int16_t>(m_boundingBox.x())); // Minimum left side bearing
append16(clampTo<int16_t>(m_minRightSideBearing)); // Minimum right side bearing
append16(clampTo<int16_t>(m_boundingBox.maxX())); // X maximum extent
// Since WebKit draws the caret and ignores the following values, it doesn't matter what we set them to.
append16(1); // Vertical caret
append16(0); // Vertical caret
append16(0); // "Set value to 0 for non-slanted fonts"
append32(0); // Reserved
append32(0); // Reserved
append16(0); // Current format
append16(m_glyphs.size()); // Number of advance widths in HMTX table
}
void SVGToOTFFontConverter::appendHMTXTable()
{
for (auto& glyph : m_glyphs) {
append16(clampTo<uint16_t>(glyph.horizontalAdvance));
append16(clampTo<int16_t>(glyph.boundingBox.x()));
}
}
void SVGToOTFFontConverter::appendMAXPTable()
{
append32(0x00010000); // Version
append16(m_glyphs.size());
append16(0xFFFF); // Maximum number of points in non-compound glyph
append16(0xFFFF); // Maximum number of contours in non-compound glyph
append16(0xFFFF); // Maximum number of points in compound glyph
append16(0xFFFF); // Maximum number of contours in compound glyph
append16(2); // Maximum number of zones
append16(0); // Maximum number of points used in zone 0
append16(0); // Maximum number of storage area locations
append16(0); // Maximum number of function definitions
append16(0); // Maximum number of instruction definitions
append16(0); // Maximum stack depth
append16(0); // Maximum size of instructions
append16(m_glyphs.size()); // Maximum number of glyphs referenced at top level
append16(0); // No compound glyphs
}
void SVGToOTFFontConverter::appendNAMETable()
{
append16(0); // Format selector
append16(1); // Number of name records in table
append16(18); // Offset in bytes to the beginning of name character strings
append16(0); // Unicode
append16(3); // Unicode version 2.0 or later
append16(0); // Language
append16(1); // Name identifier. 1 = Font family
append16(m_fontFamily.length() * 2);
append16(0); // Offset into name data
for (auto codeUnit : StringView(m_fontFamily).codeUnits())
append16(codeUnit);
}
void SVGToOTFFontConverter::appendOS2Table()
{
int16_t averageAdvance = s_outputUnitsPerEm;
auto horizAdvX = parseHTMLInteger(m_fontElement.attributeWithoutSynchronization(SVGNames::horiz_adv_xAttr));
if (!horizAdvX && m_missingGlyphElement)
horizAdvX = parseHTMLInteger(m_missingGlyphElement->attributeWithoutSynchronization(SVGNames::horiz_adv_xAttr));
if (horizAdvX)
averageAdvance = clampTo<int16_t>(scaleUnitsPerEm(*horizAdvX));
append16(2); // Version
append16(clampTo<int16_t>(averageAdvance));
append16(m_weight); // 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
unsigned numPanoseBytes = 0;
const unsigned panoseSize = 10;
char panoseBytes[panoseSize];
if (m_fontFaceElement) {
auto segments = StringView(m_fontFaceElement->attributeWithoutSynchronization(SVGNames::panose_1Attr)).split(' ');
for (auto segment : segments) {
if (numPanoseBytes == panoseSize) {
++numPanoseBytes;
break;
}
if (auto value = parseIntegerAllowingTrailingJunk<uint8_t>(segment))
panoseBytes[numPanoseBytes++] = *value;
}
}
if (numPanoseBytes != panoseSize)
memset(panoseBytes, 0, panoseSize);
m_result.append(panoseBytes, panoseSize);
for (int i = 0; i < 4; ++i)
append32(0); // "Bit assignments are pending. Set to 0"
append32(0x544B4257); // Font Vendor. "WBKT"
append16((m_weight >= 7 ? 1 << 5 : 0) | (m_italic ? 1 : 0)); // Font Patterns.
append16(0); // First unicode index
append16(0xFFFF); // Last unicode index
append16(clampTo<int16_t>(m_ascent)); // Typographical ascender
append16(clampTo<int16_t>(-m_descent)); // Typographical descender
append16(clampTo<int16_t>(m_lineGap)); // Typographical line gap
append16(clampTo<uint16_t>(m_ascent)); // Windows-specific ascent
append16(clampTo<uint16_t>(m_descent)); // 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>(m_xHeight)); // x-height
append16(clampTo<int16_t>(m_capHeight)); // 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 SVGToOTFFontConverter::appendPOSTTable()
{
append32(0x00030000); // Format. Printing is undefined
append32(0); // Italic angle in degrees
append16(0); // Underline position
append16(0); // Underline thickness
append32(0); // Monospaced
append32(0); // "Minimum memory usage when a TrueType font is downloaded as a Type 42 font"
append32(0); // "Maximum memory usage when a TrueType font is downloaded as a Type 42 font"
append32(0); // "Minimum memory usage when a TrueType font is downloaded as a Type 1 font"
append32(0); // "Maximum memory usage when a TrueType font is downloaded as a Type 1 font"
}
static bool isValidStringForCFF(const String& string)
{
for (auto c : StringView(string).codeUnits()) {
if (c < 33 || c > 126)
return false;
}
return true;
}
void SVGToOTFFontConverter::appendValidCFFString(const String& string)
{
ASSERT(isValidStringForCFF(string));
for (auto c : StringView(string).codeUnits())
m_result.append(c);
}
void SVGToOTFFontConverter::appendCFFTable()
{
auto startingOffset = m_result.size();
// Header
m_result.append(1); // Major version
m_result.append(0); // Minor version
m_result.append(4); // Header size
m_result.append(4); // Offsets within CFF table are 4 bytes long
// Name INDEX
String fontName;
if (m_fontFaceElement) {
// FIXME: fontFamily() here might not be quite what we want.
String potentialFontName = m_fontFamily;
if (isValidStringForCFF(potentialFontName))
fontName = potentialFontName;
}
append16(1); // INDEX contains 1 element
m_result.append(4); // Offsets in this INDEX are 4 bytes long
append32(1); // 1-index offset of name data
append32(fontName.length() + 1); // 1-index offset just past end of name data
appendValidCFFString(fontName);
String weight;
if (m_fontFaceElement) {
auto& potentialWeight = m_fontFaceElement->attributeWithoutSynchronization(SVGNames::font_weightAttr);
if (isValidStringForCFF(potentialWeight))
weight = potentialWeight;
}
bool hasWeight = !weight.isNull();
const char operand32Bit = 29;
const char fullNameKey = 2;
const char familyNameKey = 3;
const char weightKey = 4;
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 + (hasWeight ? 6 : 0);
// Top DICT INDEX.
append16(1); // INDEX contains 1 element
m_result.append(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
#if ASSERT_ENABLED
unsigned topDictStart = m_result.size();
#endif
m_result.append(operand32Bit);
append32(userDefinedStringStartIndex);
m_result.append(fullNameKey);
m_result.append(operand32Bit);
append32(userDefinedStringStartIndex);
m_result.append(familyNameKey);
if (hasWeight) {
m_result.append(operand32Bit);
append32(userDefinedStringStartIndex + 2);
m_result.append(weightKey);
}
m_result.append(operand32Bit);
append32(clampTo<int32_t>(m_boundingBox.x()));
m_result.append(operand32Bit);
append32(clampTo<int32_t>(m_boundingBox.y()));
m_result.append(operand32Bit);
append32(clampTo<int32_t>(m_boundingBox.width()));
m_result.append(operand32Bit);
append32(clampTo<int32_t>(m_boundingBox.height()));
m_result.append(fontBBoxKey);
m_result.append(operand32Bit);
unsigned charsetOffsetLocation = m_result.size();
append32(0); // Offset of Charset info. Will be overwritten later.
m_result.append(charsetIndexKey);
m_result.append(operand32Bit);
unsigned charstringsOffsetLocation = m_result.size();
append32(0); // Offset of CharStrings INDEX. Will be overwritten later.
m_result.append(charstringsIndexKey);
m_result.append(operand32Bit);
append32(0); // 0-sized private dict
m_result.append(operand32Bit);
append32(0); // no location for private dict
m_result.append(privateDictIndexKey); // Private dict size and offset
ASSERT(m_result.size() == topDictStart + sizeOfTopIndex);
// String INDEX
String unknownCharacter = "UnknownChar"_s;
append16(2 + (hasWeight ? 1 : 0)); // Number of elements in INDEX
m_result.append(4); // Offsets in this INDEX are 4 bytes long
uint32_t offset = 1;
append32(offset);
offset += fontName.length();
append32(offset);
offset += unknownCharacter.length();
append32(offset);
if (hasWeight) {
offset += weight.length();
append32(offset);
}
appendValidCFFString(fontName);
appendValidCFFString(unknownCharacter);
appendValidCFFString(weight);
append16(0); // Empty subroutine INDEX
// Charset info
overwrite32(charsetOffsetLocation, m_result.size() - startingOffset);
m_result.append(0);
for (Glyph i = 1; i < m_glyphs.size(); ++i)
append16(userDefinedStringStartIndex + 1);
// CharStrings INDEX
overwrite32(charstringsOffsetLocation, m_result.size() - startingOffset);
append16(m_glyphs.size());
m_result.append(4); // Offsets in this INDEX are 4 bytes long
offset = 1;
append32(offset);
for (auto& glyph : m_glyphs) {
offset += glyph.charString.size();
append32(offset);
}
for (auto& glyph : m_glyphs)
m_result.appendVector(glyph.charString);
}
Glyph SVGToOTFFontConverter::firstGlyph(const Vector<Glyph, 1>& v, UChar32 codepoint) const
{
#if !ASSERT_ENABLED
UNUSED_PARAM(codepoint);
#endif
ASSERT(!v.isEmpty());
if (v.isEmpty())
return 0;
#if ASSERT_ENABLED
auto codePoints = StringView(m_glyphs[v[0]].codepoints).codePoints();
auto codePointsIterator = codePoints.begin();
ASSERT(codePointsIterator != codePoints.end());
ASSERT(codepoint == *codePointsIterator);
#endif
return v[0];
}
void SVGToOTFFontConverter::appendLigatureSubtable(size_t subtableRecordLocation)
{
typedef std::pair<Vector<Glyph, 3>, Glyph> LigaturePair;
Vector<LigaturePair> ligaturePairs;
for (Glyph glyphIndex = 0; glyphIndex < m_glyphs.size(); ++glyphIndex) {
ligaturePairs.append(LigaturePair(Vector<Glyph, 3>(), glyphIndex));
Vector<Glyph, 3>& ligatureGlyphs = ligaturePairs.last().first;
auto codePoints = StringView(m_glyphs[glyphIndex].codepoints).codePoints();
// FIXME: https://bugs.webkit.org/show_bug.cgi?id=138592 This needs to be done in codepoint space, not glyph space
for (auto codePoint : codePoints)
ligatureGlyphs.append(firstGlyph(glyphsForCodepoint(codePoint), codePoint));
if (ligatureGlyphs.size() < 2)
ligaturePairs.removeLast();
}
if (ligaturePairs.size() > std::numeric_limits<uint16_t>::max())
ligaturePairs.clear();
std::sort(ligaturePairs.begin(), ligaturePairs.end(), [](auto& lhs, auto& rhs) {
return lhs.first[0] < rhs.first[0];
});
Vector<size_t> overlappingFirstGlyphSegmentLengths;
if (!ligaturePairs.isEmpty()) {
Glyph previousFirstGlyph = ligaturePairs[0].first[0];
size_t segmentStart = 0;
for (size_t i = 0; i < ligaturePairs.size(); ++i) {
auto& ligaturePair = ligaturePairs[i];
if (ligaturePair.first[0] != previousFirstGlyph) {
overlappingFirstGlyphSegmentLengths.append(i - segmentStart);
segmentStart = i;
previousFirstGlyph = ligaturePairs[0].first[0];
}
}
overlappingFirstGlyphSegmentLengths.append(ligaturePairs.size() - segmentStart);
}
overwrite16(subtableRecordLocation + 6, m_result.size() - subtableRecordLocation);
auto subtableLocation = m_result.size();
append16(1); // Format 1
append16(0); // Placeholder for offset to coverage table, relative to beginning of substitution table
append16(ligaturePairs.size()); // Number of LigatureSet tables
grow(overlappingFirstGlyphSegmentLengths.size() * 2); // Placeholder for offset to LigatureSet table
Vector<size_t> ligatureSetTableLocations;
for (size_t i = 0; i < overlappingFirstGlyphSegmentLengths.size(); ++i) {
overwrite16(subtableLocation + 6 + 2 * i, m_result.size() - subtableLocation);
ligatureSetTableLocations.append(m_result.size());
append16(overlappingFirstGlyphSegmentLengths[i]); // LigatureCount
grow(overlappingFirstGlyphSegmentLengths[i] * 2); // Placeholder for offset to Ligature table
}
ASSERT(ligatureSetTableLocations.size() == overlappingFirstGlyphSegmentLengths.size());
size_t ligaturePairIndex = 0;
for (size_t i = 0; i < overlappingFirstGlyphSegmentLengths.size(); ++i) {
for (size_t j = 0; j < overlappingFirstGlyphSegmentLengths[i]; ++j) {
overwrite16(ligatureSetTableLocations[i] + 2 + 2 * j, m_result.size() - ligatureSetTableLocations[i]);
auto& ligaturePair = ligaturePairs[ligaturePairIndex];
append16(ligaturePair.second);
append16(ligaturePair.first.size());
for (size_t k = 1; k < ligaturePair.first.size(); ++k)
append16(ligaturePair.first[k]);
++ligaturePairIndex;
}
}
ASSERT(ligaturePairIndex == ligaturePairs.size());
// Coverage table
overwrite16(subtableLocation + 2, m_result.size() - subtableLocation);
append16(1); // CoverageFormat
append16(ligatureSetTableLocations.size()); // GlyphCount
ligaturePairIndex = 0;
for (auto segmentLength : overlappingFirstGlyphSegmentLengths) {
auto& ligaturePair = ligaturePairs[ligaturePairIndex];
ASSERT(ligaturePair.first.size() > 1);
append16(ligaturePair.first[0]);
ligaturePairIndex += segmentLength;
}
}
void SVGToOTFFontConverter::appendArabicReplacementSubtable(size_t subtableRecordLocation, ASCIILiteral arabicForm)
{
Vector<std::pair<Glyph, Glyph>> arabicFinalReplacements;
for (auto& pair : m_codepointsToIndicesMap) {
for (auto glyphIndex : pair.value) {
auto& glyph = m_glyphs[glyphIndex];
if (glyph.glyphElement && equalIgnoringASCIICase(glyph.glyphElement->attributeWithoutSynchronization(SVGNames::arabic_formAttr), arabicForm))
arabicFinalReplacements.append(std::make_pair(pair.value[0], glyphIndex));
}
}
if (arabicFinalReplacements.size() > std::numeric_limits<uint16_t>::max())
arabicFinalReplacements.clear();
overwrite16(subtableRecordLocation + 6, m_result.size() - subtableRecordLocation);
auto subtableLocation = m_result.size();
append16(2); // Format 2
Placeholder toCoverageTable = placeholder(subtableLocation);
append16(arabicFinalReplacements.size()); // GlyphCount
for (auto& pair : arabicFinalReplacements)
append16(pair.second);
toCoverageTable.populate();
append16(1); // CoverageFormat
append16(arabicFinalReplacements.size()); // GlyphCount
for (auto& pair : arabicFinalReplacements)
append16(pair.first);
}
void SVGToOTFFontConverter::appendScriptSubtable(unsigned featureCount)
{
auto dfltScriptTableLocation = m_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, m_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(m_featureCountGSUB++); // Features indices
}
void SVGToOTFFontConverter::appendGSUBTable()
{
auto tableLocation = m_result.size();
auto headerSize = 10;
append32(0x00010000); // Version
append16(headerSize); // Offset to ScriptList
Placeholder toFeatureList = placeholder(tableLocation);
Placeholder toLookupList = placeholder(tableLocation);
ASSERT(tableLocation + headerSize == m_result.size());
// ScriptList
auto scriptListLocation = m_result.size();
append16(2); // Number of ScriptRecords
append32BitCode("DFLT");
append16(0); // Placeholder for offset of Script table, relative to beginning of ScriptList
append32BitCode("arab");
append16(0); // Placeholder for offset of Script table, relative to beginning of ScriptList
overwrite16(scriptListLocation + 6, m_result.size() - scriptListLocation);
appendScriptSubtable(1);
overwrite16(scriptListLocation + 12, m_result.size() - scriptListLocation);
appendScriptSubtable(4);
const unsigned featureCount = 5;
// FeatureList
toFeatureList.populate();
auto featureListLocation = m_result.size();
size_t featureListSize = 2 + 6 * featureCount;
size_t featureTableSize = 6;
append16(featureCount); // FeatureCount
append32BitCode("liga");
append16(featureListSize + featureTableSize * 0); // Offset of feature table, relative to beginning of FeatureList table
append32BitCode("fina");
append16(featureListSize + featureTableSize * 1); // Offset of feature table, relative to beginning of FeatureList table
append32BitCode("medi");
append16(featureListSize + featureTableSize * 2); // Offset of feature table, relative to beginning of FeatureList table
append32BitCode("init");
append16(featureListSize + featureTableSize * 3); // Offset of feature table, relative to beginning of FeatureList table
append32BitCode("rlig");
append16(featureListSize + featureTableSize * 4); // Offset of feature table, relative to beginning of FeatureList table
ASSERT_UNUSED(featureListLocation, featureListLocation + featureListSize == m_result.size());
for (unsigned i = 0; i < featureCount; ++i) {
auto featureTableStart = m_result.size();
append16(0); // FeatureParams "= NULL ... reserved"
append16(1); // LookupCount
append16(i); // LookupListIndex
ASSERT_UNUSED(featureTableStart, featureTableStart + featureTableSize == m_result.size());
}
// LookupList
toLookupList.populate();
auto lookupListLocation = m_result.size();
append16(featureCount); // LookupCount
for (unsigned i = 0; i < featureCount; ++i)
append16(0); // Placeholder for offset to feature table, relative to beginning of LookupList
size_t subtableRecordLocations[featureCount];
for (unsigned i = 0; i < featureCount; ++i) {
subtableRecordLocations[i] = m_result.size();
overwrite16(lookupListLocation + 2 + 2 * i, m_result.size() - lookupListLocation);
switch (i) {
case 4:
append16(3); // Type 3: "Replace one glyph with one of many glyphs"
break;
case 0:
append16(4); // Type 4: "Replace multiple glyphs with one glyph"
break;
default:
append16(1); // Type 1: "Replace one glyph with one glyph"
break;
}
append16(0); // LookupFlag
append16(1); // SubTableCount
append16(0); // Placeholder for offset to subtable, relative to beginning of Lookup table
}
appendLigatureSubtable(subtableRecordLocations[0]);
appendArabicReplacementSubtable(subtableRecordLocations[1], "terminal"_s);
appendArabicReplacementSubtable(subtableRecordLocations[2], "medial"_s);
appendArabicReplacementSubtable(subtableRecordLocations[3], "initial"_s);
// Manually append empty "rlig" subtable
overwrite16(subtableRecordLocations[4] + 6, m_result.size() - subtableRecordLocations[4]);
append16(1); // Format 1
append16(6); // offset to coverage table, relative to beginning of substitution table
append16(0); // AlternateSetCount
append16(1); // CoverageFormat
append16(0); // GlyphCount
}
void SVGToOTFFontConverter::appendVORGTable()
{
append16(1); // Major version
append16(0); // Minor version
auto vertOriginY = parseHTMLInteger(m_fontElement.attributeWithoutSynchronization(SVGNames::vert_origin_yAttr));
if (!vertOriginY && m_missingGlyphElement)
vertOriginY = parseHTMLInteger(m_missingGlyphElement->attributeWithoutSynchronization(SVGNames::vert_origin_yAttr));
append16(clampTo<int16_t>(scaleUnitsPerEm(vertOriginY.value_or(0))));
auto tableSizeOffset = m_result.size();
append16(0); // Place to write table size.
for (Glyph i = 0; i < m_glyphs.size(); ++i) {
if (auto* glyph = m_glyphs[i].glyphElement) {
if (auto verticalOriginY = parseHTMLInteger(glyph->attributeWithoutSynchronization(SVGNames::vert_origin_yAttr))) {
append16(i);
append16(clampTo<int16_t>(scaleUnitsPerEm(*verticalOriginY)));
}
}
}
ASSERT(!((m_result.size() - tableSizeOffset - 2) % 4));
overwrite16(tableSizeOffset, (m_result.size() - tableSizeOffset - 2) / 4);
}
void SVGToOTFFontConverter::appendVHEATable()
{
float height = m_ascent + m_descent;
append32(0x00011000); // Version
append16(clampTo<int16_t>(height / 2)); // Vertical typographic ascender (vertical baseline to the right)
append16(clampTo<int16_t>(-static_cast<int>(height / 2))); // Vertical typographic descender
append16(clampTo<int16_t>(s_outputUnitsPerEm / 10)); // Vertical typographic line gap
// FIXME: m_unitsPerEm is almost certainly not correct
append16(clampTo<int16_t>(m_advanceHeightMax));
append16(clampTo<int16_t>(s_outputUnitsPerEm - m_boundingBox.maxY())); // Minimum top side bearing
append16(clampTo<int16_t>(m_boundingBox.y())); // Minimum bottom side bearing
append16(clampTo<int16_t>(s_outputUnitsPerEm - m_boundingBox.y())); // Y maximum extent
// Since WebKit draws the caret and ignores the following values, it doesn't matter what we set them to.
append16(1); // Vertical caret
append16(0); // Vertical caret
append16(0); // "Set value to 0 for non-slanted fonts"
append32(0); // Reserved
append32(0); // Reserved
append16(0); // "Set to 0"
append16(m_glyphs.size()); // Number of advance heights in VMTX table
}
void SVGToOTFFontConverter::appendVMTXTable()
{
for (auto& glyph : m_glyphs) {
append16(clampTo<uint16_t>(glyph.verticalAdvance));
append16(clampTo<int16_t>(s_outputUnitsPerEm - glyph.boundingBox.maxY())); // top side bearing
}
}
static String codepointToString(UChar32 codepoint)
{
UChar buffer[2];
uint8_t length = 0;
UBool error = false;
U16_APPEND(buffer, length, 2, codepoint, error);
return error ? String() : String(buffer, length);
}
Vector<Glyph, 1> SVGToOTFFontConverter::glyphsForCodepoint(UChar32 codepoint) const
{
return m_codepointsToIndicesMap.get(codepointToString(codepoint));
}
void SVGToOTFFontConverter::addCodepointRanges(const UnicodeRanges& unicodeRanges, HashSet<Glyph>& glyphSet) const
{
for (auto& unicodeRange : unicodeRanges) {
for (auto codepoint = unicodeRange.first; codepoint <= unicodeRange.second; ++codepoint) {
for (auto index : glyphsForCodepoint(codepoint))
glyphSet.add(index);
}
}
}
void SVGToOTFFontConverter::addCodepoints(const HashSet<String>& codepoints, HashSet<Glyph>& glyphSet) const
{
for (auto& codepointString : codepoints) {
for (auto index : m_codepointsToIndicesMap.get(codepointString))
glyphSet.add(index);
}
}
void SVGToOTFFontConverter::addGlyphNames(const HashSet<String>& glyphNames, HashSet<Glyph>& glyphSet) const
{
for (auto& glyphName : glyphNames) {
if (Glyph glyph = m_glyphNameToIndexMap.get(glyphName))
glyphSet.add(glyph);
}
}
void SVGToOTFFontConverter::addKerningPair(Vector<KerningData>& data, SVGKerningPair&& kerningPair) const
{
HashSet<Glyph> glyphSet1;
HashSet<Glyph> glyphSet2;
addCodepointRanges(kerningPair.unicodeRange1, glyphSet1);
addCodepointRanges(kerningPair.unicodeRange2, glyphSet2);
addGlyphNames(kerningPair.glyphName1, glyphSet1);
addGlyphNames(kerningPair.glyphName2, glyphSet2);
addCodepoints(kerningPair.unicodeName1, glyphSet1);
addCodepoints(kerningPair.unicodeName2, glyphSet2);
// FIXME: Use table format 2 so we don't have to append each of these one by one.
for (auto& glyph1 : glyphSet1) {
for (auto& glyph2 : glyphSet2)
data.append(KerningData(glyph1, glyph2, clampTo<int16_t>(-scaleUnitsPerEm(kerningPair.kerning))));
}
}
template<typename T> inline size_t SVGToOTFFontConverter::appendKERNSubtable(std::optional<SVGKerningPair> (T::*buildKerningPair)() const, uint16_t coverage)
{
Vector<KerningData> kerningData;
for (auto& element : childrenOfType<T>(m_fontElement)) {
if (auto kerningPair = (element.*buildKerningPair)())
addKerningPair(kerningData, WTFMove(*kerningPair));
}
return finishAppendingKERNSubtable(WTFMove(kerningData), coverage);
}
size_t SVGToOTFFontConverter::finishAppendingKERNSubtable(Vector<KerningData> kerningData, uint16_t coverage)
{
std::sort(kerningData.begin(), kerningData.end(), [](auto& a, auto& b) {
return a.glyph1 < b.glyph1 || (a.glyph1 == b.glyph1 && a.glyph2 < b.glyph2);
});
size_t sizeOfKerningDataTable = 14 + 6 * kerningData.size();
if (sizeOfKerningDataTable > std::numeric_limits<uint16_t>::max()) {
kerningData.clear();
sizeOfKerningDataTable = 14;
}
append16(0); // Version of subtable
append16(sizeOfKerningDataTable); // Length of this subtable
append16(coverage); // Table coverage bitfield
uint16_t roundedNumKerningPairs = roundDownToPowerOfTwo(kerningData.size());
append16(kerningData.size());
append16(roundedNumKerningPairs * 6); // searchRange: "The largest power of two less than or equal to the value of nPairs, multiplied by the size in bytes of an entry in the table."
append16(integralLog2(roundedNumKerningPairs)); // entrySelector: "log2 of the largest power of two less than or equal to the value of nPairs."
append16((kerningData.size() - roundedNumKerningPairs) * 6); // rangeShift: "The value of nPairs minus the largest power of two less than or equal to nPairs,
// and then multiplied by the size in bytes of an entry in the table."
for (auto& kerningDataElement : kerningData) {
append16(kerningDataElement.glyph1);
append16(kerningDataElement.glyph2);
append16(kerningDataElement.adjustment);
}
return sizeOfKerningDataTable;
}
void SVGToOTFFontConverter::appendKERNTable()
{
append16(0); // Version
append16(2); // Number of subtables
#if ASSERT_ENABLED
auto subtablesOffset = m_result.size();
#endif
size_t sizeOfHorizontalSubtable = appendKERNSubtable<SVGHKernElement>(&SVGHKernElement::buildHorizontalKerningPair, 1);
ASSERT_UNUSED(sizeOfHorizontalSubtable, subtablesOffset + sizeOfHorizontalSubtable == m_result.size());
size_t sizeOfVerticalSubtable = appendKERNSubtable<SVGVKernElement>(&SVGVKernElement::buildVerticalKerningPair, 0);
ASSERT_UNUSED(sizeOfVerticalSubtable, subtablesOffset + sizeOfHorizontalSubtable + sizeOfVerticalSubtable == m_result.size());
}
template <typename V>
static void writeCFFEncodedNumber(V& vector, float number)
{
vector.append(0xFF);
// Convert to 16.16 fixed-point
append32(vector, clampTo<int32_t>(number * 0x10000));
}
static const char rLineTo = 0x05;
static const char rrCurveTo = 0x08;
static const char endChar = 0x0e;
static const char rMoveTo = 0x15;
class CFFBuilder final : public SVGPathConsumer {
public:
CFFBuilder(Vector<char>& cffData, float width, FloatPoint origin, float unitsPerEmScalar)
: m_cffData(cffData)
, m_unitsPerEmScalar(unitsPerEmScalar)
{
writeCFFEncodedNumber(m_cffData, std::floor(width)); // hmtx table can't encode fractional FUnit values, and the CFF table needs to agree with hmtx.
writeCFFEncodedNumber(m_cffData, origin.x());
writeCFFEncodedNumber(m_cffData, origin.y());
m_cffData.append(rMoveTo);
}
std::optional<FloatRect> boundingBox() const
{
return m_boundingBox;
}
private:
void updateBoundingBox(FloatPoint point)
{
if (!m_boundingBox) {
m_boundingBox = FloatRect(point, FloatSize());
return;
}
m_boundingBox.value().extend(point);
}
void writePoint(FloatPoint destination)
{
updateBoundingBox(destination);
FloatSize delta = destination - m_current;
writeCFFEncodedNumber(m_cffData, delta.width());
writeCFFEncodedNumber(m_cffData, delta.height());
m_current = destination;
}
void moveTo(const FloatPoint& targetPoint, bool closed, PathCoordinateMode mode) final
{
if (closed && !m_cffData.isEmpty())
closePath();
FloatPoint scaledTargetPoint = FloatPoint(targetPoint.x() * m_unitsPerEmScalar, targetPoint.y() * m_unitsPerEmScalar);
FloatPoint destination = mode == AbsoluteCoordinates ? scaledTargetPoint : m_current + scaledTargetPoint;
writePoint(destination);
m_cffData.append(rMoveTo);
m_startingPoint = m_current;
}
void unscaledLineTo(const FloatPoint& targetPoint)
{
writePoint(targetPoint);
m_cffData.append(rLineTo);
}
void lineTo(const FloatPoint& targetPoint, PathCoordinateMode mode) final
{
FloatPoint scaledTargetPoint = FloatPoint(targetPoint.x() * m_unitsPerEmScalar, targetPoint.y() * m_unitsPerEmScalar);
FloatPoint destination = mode == AbsoluteCoordinates ? scaledTargetPoint : m_current + scaledTargetPoint;
unscaledLineTo(destination);
}
void curveToCubic(const FloatPoint& point1, const FloatPoint& point2, const FloatPoint& point3, PathCoordinateMode mode) final
{
FloatPoint scaledPoint1 = FloatPoint(point1.x() * m_unitsPerEmScalar, point1.y() * m_unitsPerEmScalar);
FloatPoint scaledPoint2 = FloatPoint(point2.x() * m_unitsPerEmScalar, point2.y() * m_unitsPerEmScalar);
FloatPoint scaledPoint3 = FloatPoint(point3.x() * m_unitsPerEmScalar, point3.y() * m_unitsPerEmScalar);
if (mode == RelativeCoordinates) {
scaledPoint1 += m_current;
scaledPoint2 += m_current;
scaledPoint3 += m_current;
}
writePoint(scaledPoint1);
writePoint(scaledPoint2);
writePoint(scaledPoint3);
m_cffData.append(rrCurveTo);
}
void closePath() final
{
if (m_current != m_startingPoint)
unscaledLineTo(m_startingPoint);
}
void incrementPathSegmentCount() final { }
bool continueConsuming() final { return true; }
void lineToHorizontal(float, PathCoordinateMode) final { ASSERT_NOT_REACHED(); }
void lineToVertical(float, PathCoordinateMode) final { ASSERT_NOT_REACHED(); }
void curveToCubicSmooth(const FloatPoint&, const FloatPoint&, PathCoordinateMode) final { ASSERT_NOT_REACHED(); }
void curveToQuadratic(const FloatPoint&, const FloatPoint&, PathCoordinateMode) final { ASSERT_NOT_REACHED(); }
void curveToQuadraticSmooth(const FloatPoint&, PathCoordinateMode) final { ASSERT_NOT_REACHED(); }
void arcTo(float, float, float, bool, bool, const FloatPoint&, PathCoordinateMode) final { ASSERT_NOT_REACHED(); }
Vector<char>& m_cffData;
FloatPoint m_startingPoint;
FloatPoint m_current;
std::optional<FloatRect> m_boundingBox;
float m_unitsPerEmScalar;
};
Vector<char> SVGToOTFFontConverter::transcodeGlyphPaths(float width, const SVGElement& glyphOrMissingGlyphElement, std::optional<FloatRect>& boundingBox) const
{
Vector<char> result;
auto& dAttribute = glyphOrMissingGlyphElement.attributeWithoutSynchronization(SVGNames::dAttr);
if (dAttribute.isEmpty()) {
writeCFFEncodedNumber(result, width);
writeCFFEncodedNumber(result, 0);
writeCFFEncodedNumber(result, 0);
result.append(rMoveTo);
result.append(endChar);
return result;
}
// FIXME: If we are vertical, use vert_origin_x and vert_origin_y
bool ok;
float horizontalOriginX = scaleUnitsPerEm(glyphOrMissingGlyphElement.attributeWithoutSynchronization(SVGNames::horiz_origin_xAttr).toFloat(&ok));
if (!ok && m_fontFaceElement)
horizontalOriginX = scaleUnitsPerEm(m_fontFaceElement->horizontalOriginX());
float horizontalOriginY = scaleUnitsPerEm(glyphOrMissingGlyphElement.attributeWithoutSynchronization(SVGNames::horiz_origin_yAttr).toFloat(&ok));
if (!ok && m_fontFaceElement)
horizontalOriginY = scaleUnitsPerEm(m_fontFaceElement->horizontalOriginY());
CFFBuilder builder(result, width, FloatPoint(horizontalOriginX, horizontalOriginY), static_cast<float>(s_outputUnitsPerEm) / m_inputUnitsPerEm);
SVGPathStringViewSource source(dAttribute);
ok = SVGPathParser::parse(source, builder);
if (!ok)
return { };
boundingBox = builder.boundingBox();
result.append(endChar);
return result;
}
void SVGToOTFFontConverter::processGlyphElement(const SVGElement& glyphOrMissingGlyphElement, const SVGGlyphElement* glyphElement, float defaultHorizontalAdvance, float defaultVerticalAdvance, const String& codepoints, std::optional<FloatRect>& boundingBox)
{
bool ok;
float horizontalAdvance = scaleUnitsPerEm(glyphOrMissingGlyphElement.attributeWithoutSynchronization(SVGNames::horiz_adv_xAttr).toFloat(&ok));
if (!ok)
horizontalAdvance = defaultHorizontalAdvance;
m_advanceWidthMax = std::max(m_advanceWidthMax, horizontalAdvance);
float verticalAdvance = scaleUnitsPerEm(glyphOrMissingGlyphElement.attributeWithoutSynchronization(SVGNames::vert_adv_yAttr).toFloat(&ok));
if (!ok)
verticalAdvance = defaultVerticalAdvance;
m_advanceHeightMax = std::max(m_advanceHeightMax, verticalAdvance);
std::optional<FloatRect> glyphBoundingBox;
auto path = transcodeGlyphPaths(horizontalAdvance, glyphOrMissingGlyphElement, glyphBoundingBox);
if (!path.size()) {
// It's better to use a fallback font rather than use a font without all its glyphs.
m_error = true;
}
if (!boundingBox)
boundingBox = glyphBoundingBox;
else if (glyphBoundingBox)
boundingBox.value().unite(glyphBoundingBox.value());
if (glyphBoundingBox)
m_minRightSideBearing = std::min(m_minRightSideBearing, horizontalAdvance - glyphBoundingBox.value().maxX());
m_glyphs.append(GlyphData(WTFMove(path), glyphElement, horizontalAdvance, verticalAdvance, valueOrDefault(glyphBoundingBox), codepoints));
}
void SVGToOTFFontConverter::appendLigatureGlyphs()
{
HashSet<UChar32> ligatureCodepoints;
HashSet<UChar32> nonLigatureCodepoints;
for (auto& glyph : m_glyphs) {
auto codePoints = StringView(glyph.codepoints).codePoints();
auto codePointsIterator = codePoints.begin();
if (codePointsIterator == codePoints.end())
continue;
UChar32 codepoint = *codePointsIterator;
++codePointsIterator;
if (codePointsIterator == codePoints.end())
nonLigatureCodepoints.add(codepoint);
else {
ligatureCodepoints.add(codepoint);
for (; codePointsIterator != codePoints.end(); ++codePointsIterator)
ligatureCodepoints.add(*codePointsIterator);
}
}
for (auto codepoint : nonLigatureCodepoints)
ligatureCodepoints.remove(codepoint);
for (auto codepoint : ligatureCodepoints) {
auto codepoints = codepointToString(codepoint);
if (!codepoints.isNull())
m_glyphs.append(GlyphData(Vector<char>(m_emptyGlyphCharString), nullptr, s_outputUnitsPerEm, s_outputUnitsPerEm, FloatRect(), codepoints));
}
}
bool SVGToOTFFontConverter::compareCodepointsLexicographically(const GlyphData& data1, const GlyphData& data2)
{
auto codePoints1 = StringView(data1.codepoints).codePoints();
auto codePoints2 = StringView(data2.codepoints).codePoints();
auto iterator1 = codePoints1.begin();
auto iterator2 = codePoints2.begin();
while (iterator1 != codePoints1.end() && iterator2 != codePoints2.end()) {
UChar32 codepoint1, codepoint2;
codepoint1 = *iterator1;
codepoint2 = *iterator2;
if (codepoint1 < codepoint2)
return true;
if (codepoint1 > codepoint2)
return false;
++iterator1;
++iterator2;
}
if (iterator1 == codePoints1.end() && iterator2 == codePoints2.end()) {
bool firstIsIsolated = data1.glyphElement && equalLettersIgnoringASCIICase(data1.glyphElement->attributeWithoutSynchronization(SVGNames::arabic_formAttr), "isolated"_s);
bool secondIsIsolated = data2.glyphElement && equalLettersIgnoringASCIICase(data2.glyphElement->attributeWithoutSynchronization(SVGNames::arabic_formAttr), "isolated"_s);
return firstIsIsolated && !secondIsIsolated;
}
return iterator1 == codePoints1.end();
}
static void populateEmptyGlyphCharString(Vector<char, 17>& o, unsigned unitsPerEm)
{
writeCFFEncodedNumber(o, unitsPerEm);
writeCFFEncodedNumber(o, 0);
writeCFFEncodedNumber(o, 0);
o.append(rMoveTo);
o.append(endChar);
}
SVGToOTFFontConverter::SVGToOTFFontConverter(const SVGFontElement& fontElement)
: m_fontElement(fontElement)
, m_fontFaceElement(childrenOfType<SVGFontFaceElement>(m_fontElement).first())
, m_missingGlyphElement(childrenOfType<SVGMissingGlyphElement>(m_fontElement).first())
, m_advanceWidthMax(0)
, m_advanceHeightMax(0)
, m_minRightSideBearing(std::numeric_limits<float>::max())
, m_featureCountGSUB(0)
, m_tablesAppendedCount(0)
, m_weight(5)
, m_italic(false)
{
if (!m_fontFaceElement) {
m_inputUnitsPerEm = 1;
m_ascent = s_outputUnitsPerEm;
m_descent = 1;
m_xHeight = s_outputUnitsPerEm;
m_capHeight = m_ascent;
} else {
m_inputUnitsPerEm = m_fontFaceElement->unitsPerEm();
m_ascent = scaleUnitsPerEm(m_fontFaceElement->ascent());
m_descent = scaleUnitsPerEm(m_fontFaceElement->descent());
m_xHeight = scaleUnitsPerEm(m_fontFaceElement->xHeight());
m_capHeight = scaleUnitsPerEm(m_fontFaceElement->capHeight());
// Some platforms, including OS X, use 0 ascent and descent to mean that the platform should synthesize
// a value based on a heuristic. However, SVG fonts can legitimately have 0 for ascent or descent.
// Specifing a single FUnit gets us as close to 0 as we can without triggering the synthesis.
if (!m_ascent)
m_ascent = 1;
if (!m_descent)
m_descent = 1;
}
float defaultHorizontalAdvance = m_fontFaceElement ? scaleUnitsPerEm(m_fontFaceElement->horizontalAdvanceX()) : 0;
float defaultVerticalAdvance = m_fontFaceElement ? scaleUnitsPerEm(m_fontFaceElement->verticalAdvanceY()) : 0;
m_lineGap = s_outputUnitsPerEm / 10;
populateEmptyGlyphCharString(m_emptyGlyphCharString, s_outputUnitsPerEm);
std::optional<FloatRect> boundingBox;
if (m_missingGlyphElement)
processGlyphElement(*m_missingGlyphElement, nullptr, defaultHorizontalAdvance, defaultVerticalAdvance, String(), boundingBox);
else {
m_glyphs.append(GlyphData(Vector<char>(m_emptyGlyphCharString), nullptr, s_outputUnitsPerEm, s_outputUnitsPerEm, FloatRect(), String()));
boundingBox = FloatRect(0, 0, s_outputUnitsPerEm, s_outputUnitsPerEm);
}
for (auto& glyphElement : childrenOfType<SVGGlyphElement>(m_fontElement)) {
auto& unicodeAttribute = glyphElement.attributeWithoutSynchronization(SVGNames::unicodeAttr);
if (!unicodeAttribute.isEmpty()) // If we can never actually trigger this glyph, ignore it completely
processGlyphElement(glyphElement, &glyphElement, defaultHorizontalAdvance, defaultVerticalAdvance, unicodeAttribute, boundingBox);
}
m_boundingBox = valueOrDefault(boundingBox);
appendLigatureGlyphs();
if (m_glyphs.size() > std::numeric_limits<Glyph>::max()) {
m_glyphs.clear();
return;
}
std::sort(m_glyphs.begin(), m_glyphs.end(), &compareCodepointsLexicographically);
for (Glyph i = 0; i < m_glyphs.size(); ++i) {
GlyphData& glyph = m_glyphs[i];
if (glyph.glyphElement) {
auto& glyphName = glyph.glyphElement->attributeWithoutSynchronization(SVGNames::glyph_nameAttr);
if (!glyphName.isNull())
m_glyphNameToIndexMap.add(glyphName, i);
}
if (m_codepointsToIndicesMap.isValidKey(glyph.codepoints)) {
auto& glyphVector = m_codepointsToIndicesMap.add(glyph.codepoints, Vector<Glyph>()).iterator->value;
// Prefer isolated arabic forms
if (glyph.glyphElement && equalLettersIgnoringASCIICase(glyph.glyphElement->attributeWithoutSynchronization(SVGNames::arabic_formAttr), "isolated"_s))
glyphVector.insert(0, i);
else
glyphVector.append(i);
}
}
// FIXME: Handle commas.
if (m_fontFaceElement) {
for (auto segment : StringView(m_fontFaceElement->attributeWithoutSynchronization(SVGNames::font_weightAttr)).split(' ')) {
if (equalLettersIgnoringASCIICase(segment, "bold"_s)) {
m_weight = 7;
break;
}
if (auto value = parseIntegerAllowingTrailingJunk<uint16_t>(segment); value && *value < 1000) {
m_weight = (*value + 50) / 100;
break;
}
}
for (auto segment : StringView(m_fontFaceElement->attributeWithoutSynchronization(SVGNames::font_styleAttr)).split(' ')) {
if (equalLettersIgnoringASCIICase(segment, "italic"_s) || equalLettersIgnoringASCIICase(segment, "oblique"_s)) {
m_italic = true;
break;
}
}
}
if (m_fontFaceElement)
m_fontFamily = m_fontFaceElement->fontFamily();
}
static inline bool isFourByteAligned(size_t x)
{
return !(x & 3);
}
uint32_t SVGToOTFFontConverter::calculateChecksum(size_t startingOffset, size_t endingOffset) const
{
ASSERT(isFourByteAligned(endingOffset - startingOffset));
uint32_t sum = 0;
for (size_t offset = startingOffset; offset < endingOffset; offset += 4) {
sum += static_cast<unsigned char>(m_result[offset + 3])
| (static_cast<unsigned char>(m_result[offset + 2]) << 8)
| (static_cast<unsigned char>(m_result[offset + 1]) << 16)
| (static_cast<unsigned char>(m_result[offset]) << 24);
}
return sum;
}
void SVGToOTFFontConverter::appendTable(const char identifier[4], FontAppendingFunction appendingFunction)
{
size_t offset = m_result.size();
ASSERT(isFourByteAligned(offset));
(this->*appendingFunction)();
size_t unpaddedSize = m_result.size() - offset;
while (!isFourByteAligned(m_result.size()))
m_result.append(0);
ASSERT(isFourByteAligned(m_result.size()));
size_t directoryEntryOffset = headerSize + m_tablesAppendedCount * directoryEntrySize;
m_result[directoryEntryOffset] = identifier[0];
m_result[directoryEntryOffset + 1] = identifier[1];
m_result[directoryEntryOffset + 2] = identifier[2];
m_result[directoryEntryOffset + 3] = identifier[3];
overwrite32(directoryEntryOffset + 4, calculateChecksum(offset, m_result.size()));
overwrite32(directoryEntryOffset + 8, offset);
overwrite32(directoryEntryOffset + 12, unpaddedSize);
++m_tablesAppendedCount;
}
bool SVGToOTFFontConverter::convertSVGToOTFFont()
{
if (m_glyphs.isEmpty())
return false;
uint16_t numTables = 14;
uint16_t roundedNumTables = roundDownToPowerOfTwo(numTables);
uint16_t searchRange = roundedNumTables * 16; // searchRange: "(Maximum power of 2 <= numTables) x 16."
m_result.append('O');
m_result.append('T');
m_result.append('T');
m_result.append('O');
append16(numTables);
append16(searchRange);
append16(integralLog2(roundedNumTables)); // entrySelector: "Log2(maximum power of 2 <= numTables)."
append16(numTables * 16 - searchRange); // rangeShift: "NumTables x 16-searchRange."
ASSERT(m_result.size() == headerSize);
// Leave space for the directory entries.
for (size_t i = 0; i < directoryEntrySize * numTables; ++i)
m_result.append(0);
appendTable("CFF ", &SVGToOTFFontConverter::appendCFFTable);
appendTable("GSUB", &SVGToOTFFontConverter::appendGSUBTable);
appendTable("OS/2", &SVGToOTFFontConverter::appendOS2Table);
appendTable("VORG", &SVGToOTFFontConverter::appendVORGTable);
appendTable("cmap", &SVGToOTFFontConverter::appendCMAPTable);
auto headTableOffset = m_result.size();
appendTable("head", &SVGToOTFFontConverter::appendHEADTable);
appendTable("hhea", &SVGToOTFFontConverter::appendHHEATable);
appendTable("hmtx", &SVGToOTFFontConverter::appendHMTXTable);
appendTable("kern", &SVGToOTFFontConverter::appendKERNTable);
appendTable("maxp", &SVGToOTFFontConverter::appendMAXPTable);
appendTable("name", &SVGToOTFFontConverter::appendNAMETable);
appendTable("post", &SVGToOTFFontConverter::appendPOSTTable);
appendTable("vhea", &SVGToOTFFontConverter::appendVHEATable);
appendTable("vmtx", &SVGToOTFFontConverter::appendVMTXTable);
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, m_result.size()));
return true;
}
std::optional<Vector<uint8_t>> convertSVGToOTFFont(const SVGFontElement& element)
{
SVGToOTFFontConverter converter(element);
if (converter.error())
return std::nullopt;
if (!converter.convertSVGToOTFFont())
return std::nullopt;
return converter.releaseResult();
}
}
|