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 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
|
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <qdebug.h>
#include <private/qfontengine_p.h>
#include "qbitmap.h"
#include "qpainter.h"
#include "qpainterpath.h"
#include "qvarlengtharray.h"
#include <qmath.h>
#include <qendian.h>
#include <private/qharfbuzz_p.h>
QT_BEGIN_NAMESPACE
static inline bool qtransform_equals_no_translate(const QTransform &a, const QTransform &b)
{
if (a.type() <= QTransform::TxTranslate && b.type() <= QTransform::TxTranslate) {
return true;
} else {
// We always use paths for perspective text anyway, so no
// point in checking the full matrix...
Q_ASSERT(a.type() < QTransform::TxProject);
Q_ASSERT(b.type() < QTransform::TxProject);
return a.m11() == b.m11()
&& a.m12() == b.m12()
&& a.m21() == b.m21()
&& a.m22() == b.m22();
}
}
template<typename T>
static inline bool qSafeFromBigEndian(const uchar *source, const uchar *end, T *output)
{
if (source + sizeof(T) > end)
return false;
*output = qFromBigEndian<T>(source);
return true;
}
// Harfbuzz helper functions
static HB_Bool hb_stringToGlyphs(HB_Font font, const HB_UChar16 *string, hb_uint32 length, HB_Glyph *glyphs, hb_uint32 *numGlyphs, HB_Bool rightToLeft)
{
QFontEngine *fe = (QFontEngine *)font->userData;
QVarLengthGlyphLayoutArray qglyphs(*numGlyphs);
QTextEngine::ShaperFlags shaperFlags(QTextEngine::GlyphIndicesOnly);
if (rightToLeft)
shaperFlags |= QTextEngine::RightToLeft;
int nGlyphs = *numGlyphs;
bool result = fe->stringToCMap(reinterpret_cast<const QChar *>(string), length, &qglyphs, &nGlyphs, shaperFlags);
*numGlyphs = nGlyphs;
if (!result)
return false;
for (hb_uint32 i = 0; i < *numGlyphs; ++i)
glyphs[i] = qglyphs.glyphs[i];
return true;
}
static void hb_getAdvances(HB_Font font, const HB_Glyph *glyphs, hb_uint32 numGlyphs, HB_Fixed *advances, int flags)
{
QFontEngine *fe = (QFontEngine *)font->userData;
QVarLengthGlyphLayoutArray qglyphs(numGlyphs);
for (hb_uint32 i = 0; i < numGlyphs; ++i)
qglyphs.glyphs[i] = glyphs[i];
fe->recalcAdvances(&qglyphs, flags & HB_ShaperFlag_UseDesignMetrics ? QFlags<QTextEngine::ShaperFlag>(QTextEngine::DesignMetrics) : QFlags<QTextEngine::ShaperFlag>(0));
for (hb_uint32 i = 0; i < numGlyphs; ++i)
advances[i] = qglyphs.advances_x[i].value();
}
static HB_Bool hb_canRender(HB_Font font, const HB_UChar16 *string, hb_uint32 length)
{
QFontEngine *fe = (QFontEngine *)font->userData;
return fe->canRender(reinterpret_cast<const QChar *>(string), length);
}
static void hb_getGlyphMetrics(HB_Font font, HB_Glyph glyph, HB_GlyphMetrics *metrics)
{
QFontEngine *fe = (QFontEngine *)font->userData;
glyph_metrics_t m = fe->boundingBox(glyph);
metrics->x = m.x.value();
metrics->y = m.y.value();
metrics->width = m.width.value();
metrics->height = m.height.value();
metrics->xOffset = m.xoff.value();
metrics->yOffset = m.yoff.value();
}
static HB_Fixed hb_getFontMetric(HB_Font font, HB_FontMetric metric)
{
QFontEngine *fe = (QFontEngine *)font->userData;
switch (metric) {
case HB_FontAscent:
return fe->ascent().value();
break;
case HB_FontDescent:
return fe->descent().value();
break;
default:
return 0;
}
}
HB_Error QFontEngine::getPointInOutline(HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints)
{
Q_UNUSED(glyph)
Q_UNUSED(flags)
Q_UNUSED(point)
Q_UNUSED(xpos)
Q_UNUSED(ypos)
Q_UNUSED(nPoints)
return HB_Err_Not_Covered;
}
static HB_Error hb_getPointInOutline(HB_Font font, HB_Glyph glyph, int flags, hb_uint32 point, HB_Fixed *xpos, HB_Fixed *ypos, hb_uint32 *nPoints)
{
QFontEngine *fe = (QFontEngine *)font->userData;
return fe->getPointInOutline(glyph, flags, point, xpos, ypos, nPoints);
}
static const HB_FontClass hb_fontClass = {
hb_stringToGlyphs, hb_getAdvances, hb_canRender, hb_getPointInOutline,
hb_getGlyphMetrics, hb_getFontMetric
};
static HB_Error hb_getSFntTable(void *font, HB_Tag tableTag, HB_Byte *buffer, HB_UInt *length)
{
QFontEngine *fe = (QFontEngine *)font;
if (!fe->getSfntTableData(tableTag, buffer, length))
return HB_Err_Invalid_Argument;
return HB_Err_Ok;
}
// QFontEngine
QFontEngine::QFontEngine()
: QObject()
{
ref = 0;
cache_count = 0;
fsType = 0;
symbol = false;
memset(&hbFont, 0, sizeof(hbFont));
hbFont.klass = &hb_fontClass;
hbFont.userData = this;
hbFace = 0;
glyphFormat = -1;
}
QFontEngine::~QFontEngine()
{
m_glyphCaches.clear();
qHBFreeFace(hbFace);
}
QFixed QFontEngine::lineThickness() const
{
// ad hoc algorithm
int score = fontDef.weight * fontDef.pixelSize;
int lw = score / 700;
// looks better with thicker line for small pointsizes
if (lw < 2 && score >= 1050) lw = 2;
if (lw == 0) lw = 1;
return lw;
}
QFixed QFontEngine::underlinePosition() const
{
return ((lineThickness() * 2) + 3) / 6;
}
HB_Font QFontEngine::harfbuzzFont() const
{
if (!hbFont.x_ppem) {
QFixed emSquare = emSquareSize();
hbFont.x_ppem = fontDef.pixelSize;
hbFont.y_ppem = fontDef.pixelSize * fontDef.stretch / 100;
hbFont.x_scale = (QFixed(hbFont.x_ppem * (1 << 16)) / emSquare).value();
hbFont.y_scale = (QFixed(hbFont.y_ppem * (1 << 16)) / emSquare).value();
}
return &hbFont;
}
HB_Face QFontEngine::harfbuzzFace() const
{
if (!hbFace) {
hbFace = qHBNewFace(const_cast<QFontEngine *>(this), hb_getSFntTable);
Q_CHECK_PTR(hbFace);
}
return hbFace;
}
glyph_metrics_t QFontEngine::boundingBox(glyph_t glyph, const QTransform &matrix)
{
glyph_metrics_t metrics = boundingBox(glyph);
if (matrix.type() > QTransform::TxTranslate) {
return metrics.transformed(matrix);
}
return metrics;
}
QFixed QFontEngine::xHeight() const
{
QGlyphLayoutArray<8> glyphs;
int nglyphs = 7;
QChar x((ushort)'x');
stringToCMap(&x, 1, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly);
glyph_metrics_t bb = const_cast<QFontEngine *>(this)->boundingBox(glyphs.glyphs[0]);
return bb.height;
}
QFixed QFontEngine::averageCharWidth() const
{
QGlyphLayoutArray<8> glyphs;
int nglyphs = 7;
QChar x((ushort)'x');
stringToCMap(&x, 1, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly);
glyph_metrics_t bb = const_cast<QFontEngine *>(this)->boundingBox(glyphs.glyphs[0]);
return bb.xoff;
}
void QFontEngine::getGlyphPositions(const QGlyphLayout &glyphs, const QTransform &matrix, QTextItem::RenderFlags flags,
QVarLengthArray<glyph_t> &glyphs_out, QVarLengthArray<QFixedPoint> &positions)
{
QFixed xpos;
QFixed ypos;
const bool transform = matrix.m11() != 1.
|| matrix.m12() != 0.
|| matrix.m21() != 0.
|| matrix.m22() != 1.;
if (!transform) {
xpos = QFixed::fromReal(matrix.dx());
ypos = QFixed::fromReal(matrix.dy());
}
int current = 0;
if (flags & QTextItem::RightToLeft) {
int i = glyphs.numGlyphs;
int totalKashidas = 0;
while(i--) {
if (glyphs.attributes[i].dontPrint)
continue;
xpos += glyphs.advances_x[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
ypos += glyphs.advances_y[i];
totalKashidas += glyphs.justifications[i].nKashidas;
}
positions.resize(glyphs.numGlyphs+totalKashidas);
glyphs_out.resize(glyphs.numGlyphs+totalKashidas);
i = 0;
while(i < glyphs.numGlyphs) {
if (glyphs.attributes[i].dontPrint) {
++i;
continue;
}
xpos -= glyphs.advances_x[i];
ypos -= glyphs.advances_y[i];
QFixed gpos_x = xpos + glyphs.offsets[i].x;
QFixed gpos_y = ypos + glyphs.offsets[i].y;
if (transform) {
QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
gpos = gpos * matrix;
gpos_x = QFixed::fromReal(gpos.x());
gpos_y = QFixed::fromReal(gpos.y());
}
positions[current].x = gpos_x;
positions[current].y = gpos_y;
glyphs_out[current] = glyphs.glyphs[i];
++current;
if (glyphs.justifications[i].nKashidas) {
QChar ch(0x640); // Kashida character
QGlyphLayoutArray<8> g;
int nglyphs = 7;
stringToCMap(&ch, 1, &g, &nglyphs, 0);
for (uint k = 0; k < glyphs.justifications[i].nKashidas; ++k) {
xpos -= g.advances_x[0];
ypos -= g.advances_y[0];
QFixed gpos_x = xpos + glyphs.offsets[i].x;
QFixed gpos_y = ypos + glyphs.offsets[i].y;
if (transform) {
QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
gpos = gpos * matrix;
gpos_x = QFixed::fromReal(gpos.x());
gpos_y = QFixed::fromReal(gpos.y());
}
positions[current].x = gpos_x;
positions[current].y = gpos_y;
glyphs_out[current] = g.glyphs[0];
++current;
}
} else {
xpos -= QFixed::fromFixed(glyphs.justifications[i].space_18d6);
}
++i;
}
} else {
positions.resize(glyphs.numGlyphs);
glyphs_out.resize(glyphs.numGlyphs);
int i = 0;
if (!transform) {
while (i < glyphs.numGlyphs) {
if (!glyphs.attributes[i].dontPrint) {
positions[current].x = xpos + glyphs.offsets[i].x;
positions[current].y = ypos + glyphs.offsets[i].y;
glyphs_out[current] = glyphs.glyphs[i];
xpos += glyphs.advances_x[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
ypos += glyphs.advances_y[i];
++current;
}
++i;
}
} else {
while (i < glyphs.numGlyphs) {
if (!glyphs.attributes[i].dontPrint) {
QFixed gpos_x = xpos + glyphs.offsets[i].x;
QFixed gpos_y = ypos + glyphs.offsets[i].y;
QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
gpos = gpos * matrix;
positions[current].x = QFixed::fromReal(gpos.x());
positions[current].y = QFixed::fromReal(gpos.y());
glyphs_out[current] = glyphs.glyphs[i];
xpos += glyphs.advances_x[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
ypos += glyphs.advances_y[i];
++current;
}
++i;
}
}
}
positions.resize(current);
glyphs_out.resize(current);
Q_ASSERT(positions.size() == glyphs_out.size());
}
void QFontEngine::getGlyphBearings(glyph_t glyph, qreal *leftBearing, qreal *rightBearing)
{
glyph_metrics_t gi = boundingBox(glyph);
bool isValid = gi.isValid();
if (leftBearing != 0)
*leftBearing = isValid ? gi.x.toReal() : qreal(0.0);
if (rightBearing != 0)
*rightBearing = isValid ? (gi.xoff - gi.x - gi.width).toReal() : qreal(0.0);
}
glyph_metrics_t QFontEngine::tightBoundingBox(const QGlyphLayout &glyphs)
{
glyph_metrics_t overall;
QFixed ymax = 0;
QFixed xmax = 0;
for (int i = 0; i < glyphs.numGlyphs; i++) {
glyph_metrics_t bb = boundingBox(glyphs.glyphs[i]);
QFixed x = overall.xoff + glyphs.offsets[i].x + bb.x;
QFixed y = overall.yoff + glyphs.offsets[i].y + bb.y;
overall.x = qMin(overall.x, x);
overall.y = qMin(overall.y, y);
xmax = qMax(xmax, x + bb.width);
ymax = qMax(ymax, y + bb.height);
overall.xoff += bb.xoff;
overall.yoff += bb.yoff;
}
overall.height = qMax(overall.height, ymax - overall.y);
overall.width = xmax - overall.x;
return overall;
}
void QFontEngine::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path,
QTextItem::RenderFlags flags)
{
if (!glyphs.numGlyphs)
return;
QVarLengthArray<QFixedPoint> positions;
QVarLengthArray<glyph_t> positioned_glyphs;
QTransform matrix = QTransform::fromTranslate(x, y);
getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
addGlyphsToPath(positioned_glyphs.data(), positions.data(), positioned_glyphs.size(), path, flags);
}
#define GRID(x, y) grid[(y)*(w+1) + (x)]
#define SET(x, y) (*(image_data + (y)*bpl + ((x) >> 3)) & (0x80 >> ((x) & 7)))
enum { EdgeRight = 0x1,
EdgeDown = 0x2,
EdgeLeft = 0x4,
EdgeUp = 0x8
};
static void collectSingleContour(qreal x0, qreal y0, uint *grid, int x, int y, int w, int h, QPainterPath *path)
{
Q_UNUSED(h);
path->moveTo(x + x0, y + y0);
while (GRID(x, y)) {
if (GRID(x, y) & EdgeRight) {
while (GRID(x, y) & EdgeRight) {
GRID(x, y) &= ~EdgeRight;
++x;
}
Q_ASSERT(x <= w);
path->lineTo(x + x0, y + y0);
continue;
}
if (GRID(x, y) & EdgeDown) {
while (GRID(x, y) & EdgeDown) {
GRID(x, y) &= ~EdgeDown;
++y;
}
Q_ASSERT(y <= h);
path->lineTo(x + x0, y + y0);
continue;
}
if (GRID(x, y) & EdgeLeft) {
while (GRID(x, y) & EdgeLeft) {
GRID(x, y) &= ~EdgeLeft;
--x;
}
Q_ASSERT(x >= 0);
path->lineTo(x + x0, y + y0);
continue;
}
if (GRID(x, y) & EdgeUp) {
while (GRID(x, y) & EdgeUp) {
GRID(x, y) &= ~EdgeUp;
--y;
}
Q_ASSERT(y >= 0);
path->lineTo(x + x0, y + y0);
continue;
}
}
path->closeSubpath();
}
Q_GUI_EXPORT void qt_addBitmapToPath(qreal x0, qreal y0, const uchar *image_data, int bpl, int w, int h, QPainterPath *path)
{
uint *grid = new uint[(w+1)*(h+1)];
// set up edges
for (int y = 0; y <= h; ++y) {
for (int x = 0; x <= w; ++x) {
bool topLeft = (x == 0)|(y == 0) ? false : SET(x - 1, y - 1);
bool topRight = (x == w)|(y == 0) ? false : SET(x, y - 1);
bool bottomLeft = (x == 0)|(y == h) ? false : SET(x - 1, y);
bool bottomRight = (x == w)|(y == h) ? false : SET(x, y);
GRID(x, y) = 0;
if ((!topRight) & bottomRight)
GRID(x, y) |= EdgeRight;
if ((!bottomRight) & bottomLeft)
GRID(x, y) |= EdgeDown;
if ((!bottomLeft) & topLeft)
GRID(x, y) |= EdgeLeft;
if ((!topLeft) & topRight)
GRID(x, y) |= EdgeUp;
}
}
// collect edges
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
if (!GRID(x, y))
continue;
// found start of a contour, follow it
collectSingleContour(x0, y0, grid, x, y, w, h, path);
}
}
delete [] grid;
}
#undef GRID
#undef SET
void QFontEngine::addBitmapFontToPath(qreal x, qreal y, const QGlyphLayout &glyphs,
QPainterPath *path, QTextItem::RenderFlags flags)
{
// TODO what to do with 'flags' ??
Q_UNUSED(flags);
QFixed advanceX = QFixed::fromReal(x);
QFixed advanceY = QFixed::fromReal(y);
for (int i=0; i < glyphs.numGlyphs; ++i) {
glyph_metrics_t metrics = boundingBox(glyphs.glyphs[i]);
if (metrics.width.value() == 0 || metrics.height.value() == 0) {
advanceX += glyphs.advances_x[i];
advanceY += glyphs.advances_y[i];
continue;
}
const QImage alphaMask = alphaMapForGlyph(glyphs.glyphs[i]);
const int w = alphaMask.width();
const int h = alphaMask.height();
const int srcBpl = alphaMask.bytesPerLine();
QImage bitmap;
if (alphaMask.depth() == 1) {
bitmap = alphaMask;
} else {
bitmap = QImage(w, h, QImage::Format_Mono);
const uchar *imageData = alphaMask.bits();
const int destBpl = bitmap.bytesPerLine();
uchar *bitmapData = bitmap.bits();
for (int yi = 0; yi < h; ++yi) {
const uchar *src = imageData + yi*srcBpl;
uchar *dst = bitmapData + yi*destBpl;
for (int xi = 0; xi < w; ++xi) {
const int byte = xi / 8;
const int bit = xi % 8;
if (bit == 0)
dst[byte] = 0;
if (src[xi])
dst[byte] |= 128 >> bit;
}
}
}
const uchar *bitmap_data = bitmap.bits();
QFixedPoint offset = glyphs.offsets[i];
advanceX += offset.x;
advanceY += offset.y;
qt_addBitmapToPath((advanceX + metrics.x).toReal(), (advanceY + metrics.y).toReal(), bitmap_data, bitmap.bytesPerLine(), w, h, path);
advanceX += glyphs.advances_x[i];
advanceY += glyphs.advances_y[i];
}
}
void QFontEngine::addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions, int nGlyphs,
QPainterPath *path, QTextItem::RenderFlags flags)
{
qreal x = positions[0].x.toReal();
qreal y = positions[0].y.toReal();
QVarLengthGlyphLayoutArray g(nGlyphs);
for (int i = 0; i < nGlyphs; ++i) {
g.glyphs[i] = glyphs[i];
if (i < nGlyphs - 1) {
g.advances_x[i] = positions[i+1].x - positions[i].x;
g.advances_y[i] = positions[i+1].y - positions[i].y;
} else {
g.advances_x[i] = QFixed::fromReal(maxCharWidth());
g.advances_y[i] = 0;
}
}
addBitmapFontToPath(x, y, g, path, flags);
}
QImage QFontEngine::alphaMapForGlyph(glyph_t glyph, QFixed /*subPixelPosition*/)
{
// For font engines don't support subpixel positioning
return alphaMapForGlyph(glyph);
}
QImage QFontEngine::alphaMapForGlyph(glyph_t glyph, const QTransform &t)
{
QImage i = alphaMapForGlyph(glyph);
if (t.type() > QTransform::TxTranslate)
i = i.transformed(t).convertToFormat(QImage::Format_Indexed8);
Q_ASSERT(i.depth() <= 8); // To verify that transformed didn't change the format...
return i;
}
QImage QFontEngine::alphaMapForGlyph(glyph_t glyph, QFixed subPixelPosition, const QTransform &t)
{
if (! supportsSubPixelPositions())
return alphaMapForGlyph(glyph, t);
QImage i = alphaMapForGlyph(glyph, subPixelPosition);
if (t.type() > QTransform::TxTranslate)
i = i.transformed(t).convertToFormat(QImage::Format_Indexed8);
Q_ASSERT(i.depth() <= 8); // To verify that transformed didn't change the format...
return i;
}
QImage QFontEngine::alphaRGBMapForGlyph(glyph_t glyph, QFixed /*subPixelPosition*/, int /* margin */, const QTransform &t)
{
QImage alphaMask = alphaMapForGlyph(glyph, t);
QImage rgbMask(alphaMask.width(), alphaMask.height(), QImage::Format_RGB32);
QVector<QRgb> colorTable = alphaMask.colorTable();
for (int y=0; y<alphaMask.height(); ++y) {
uint *dst = (uint *) rgbMask.scanLine(y);
uchar *src = (uchar *) alphaMask.scanLine(y);
for (int x=0; x<alphaMask.width(); ++x) {
int val = qAlpha(colorTable.at(src[x]));
dst[x] = qRgb(val, val, val);
}
}
return rgbMask;
}
QImage QFontEngine::alphaMapForGlyph(glyph_t glyph)
{
glyph_metrics_t gm = boundingBox(glyph);
int glyph_x = qFloor(gm.x.toReal());
int glyph_y = qFloor(gm.y.toReal());
int glyph_width = qCeil((gm.x + gm.width).toReal()) - glyph_x;
int glyph_height = qCeil((gm.y + gm.height).toReal()) - glyph_y;
if (glyph_width <= 0 || glyph_height <= 0)
return QImage();
QFixedPoint pt;
pt.x = -glyph_x;
pt.y = -glyph_y; // the baseline
QPainterPath path;
QImage im(glyph_width + 4, glyph_height, QImage::Format_ARGB32_Premultiplied);
im.fill(Qt::transparent);
QPainter p(&im);
p.setRenderHint(QPainter::Antialiasing);
addGlyphsToPath(&glyph, &pt, 1, &path, 0);
p.setPen(Qt::NoPen);
p.setBrush(Qt::black);
p.drawPath(path);
p.end();
QImage indexed(im.width(), im.height(), QImage::Format_Indexed8);
QVector<QRgb> colors(256);
for (int i=0; i<256; ++i)
colors[i] = qRgba(0, 0, 0, i);
indexed.setColorTable(colors);
for (int y=0; y<im.height(); ++y) {
uchar *dst = (uchar *) indexed.scanLine(y);
uint *src = (uint *) im.scanLine(y);
for (int x=0; x<im.width(); ++x)
dst[x] = qAlpha(src[x]);
}
return indexed;
}
void QFontEngine::removeGlyphFromCache(glyph_t)
{
}
QFontEngine::Properties QFontEngine::properties() const
{
Properties p;
QByteArray psname = QFontEngine::convertToPostscriptFontFamilyName(fontDef.family.toUtf8());
psname += '-';
psname += QByteArray::number(fontDef.style);
psname += '-';
psname += QByteArray::number(fontDef.weight);
p.postscriptName = psname;
p.ascent = ascent();
p.descent = descent();
p.leading = leading();
p.emSquare = p.ascent;
p.boundingBox = QRectF(0, -p.ascent.toReal(), maxCharWidth(), (p.ascent + p.descent).toReal());
p.italicAngle = 0;
p.capHeight = p.ascent;
p.lineWidth = lineThickness();
return p;
}
void QFontEngine::getUnscaledGlyph(glyph_t glyph, QPainterPath *path, glyph_metrics_t *metrics)
{
*metrics = boundingBox(glyph);
QFixedPoint p;
p.x = 0;
p.y = 0;
addGlyphsToPath(&glyph, &p, 1, path, QFlag(0));
}
QByteArray QFontEngine::getSfntTable(uint tag) const
{
QByteArray table;
uint len = 0;
if (!getSfntTableData(tag, 0, &len))
return table;
if (!len)
return table;
table.resize(len);
if (!getSfntTableData(tag, reinterpret_cast<uchar *>(table.data()), &len))
return QByteArray();
return table;
}
void QFontEngine::setGlyphCache(void *key, QFontEngineGlyphCache *data)
{
Q_ASSERT(data);
GlyphCacheEntry entry;
entry.context = key;
entry.cache = data;
if (m_glyphCaches.contains(entry))
return;
// Limit the glyph caches to 4. This covers all 90 degree rotations and limits
// memory use when there is continuous or random rotation
if (m_glyphCaches.size() == 4)
m_glyphCaches.removeLast();
m_glyphCaches.push_front(entry);
}
QFontEngineGlyphCache *QFontEngine::glyphCache(void *key, QFontEngineGlyphCache::Type type, const QTransform &transform) const
{
for (QLinkedList<GlyphCacheEntry>::const_iterator it = m_glyphCaches.constBegin(), end = m_glyphCaches.constEnd(); it != end; ++it) {
QFontEngineGlyphCache *c = it->cache.data();
if (key == it->context
&& type == c->cacheType()
&& qtransform_equals_no_translate(c->m_transform, transform)) {
return c;
}
}
return 0;
}
#if defined(Q_WS_WIN) || defined(Q_WS_X11) || defined(Q_WS_QWS) || defined(Q_WS_QPA) || defined(Q_OS_SYMBIAN)
static inline QFixed kerning(int left, int right, const QFontEngine::KernPair *pairs, int numPairs)
{
uint left_right = (left << 16) + right;
left = 0, right = numPairs - 1;
while (left <= right) {
int middle = left + ( ( right - left ) >> 1 );
if(pairs[middle].left_right == left_right)
return pairs[middle].adjust;
if (pairs[middle].left_right < left_right)
left = middle + 1;
else
right = middle - 1;
}
return 0;
}
void QFontEngine::doKerning(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
{
int numPairs = kerning_pairs.size();
if(!numPairs)
return;
const KernPair *pairs = kerning_pairs.constData();
if(flags & QTextEngine::DesignMetrics) {
for(int i = 0; i < glyphs->numGlyphs - 1; ++i)
glyphs->advances_x[i] += kerning(glyphs->glyphs[i], glyphs->glyphs[i+1] , pairs, numPairs);
} else {
for(int i = 0; i < glyphs->numGlyphs - 1; ++i)
glyphs->advances_x[i] += qRound(kerning(glyphs->glyphs[i], glyphs->glyphs[i+1] , pairs, numPairs));
}
}
void QFontEngine::loadKerningPairs(QFixed scalingFactor)
{
kerning_pairs.clear();
QByteArray tab = getSfntTable(MAKE_TAG('k', 'e', 'r', 'n'));
if (tab.isEmpty())
return;
const uchar *table = reinterpret_cast<const uchar *>(tab.constData());
const uchar *end = table + tab.size();
quint16 version;
if (!qSafeFromBigEndian(table, end, &version))
return;
if (version != 0) {
// qDebug("wrong version");
return;
}
quint16 numTables;
if (!qSafeFromBigEndian(table + 2, end, &numTables))
return;
{
int offset = 4;
for(int i = 0; i < numTables; ++i) {
const uchar *header = table + offset;
quint16 version;
if (!qSafeFromBigEndian(header, end, &version))
goto end;
quint16 length;
if (!qSafeFromBigEndian(header + 2, end, &length))
goto end;
quint16 coverage;
if (!qSafeFromBigEndian(header + 4, end, &coverage))
goto end;
// qDebug("subtable: version=%d, coverage=%x",version, coverage);
if(version == 0 && coverage == 0x0001) {
if (offset + length > tab.size()) {
// qDebug("length ouf ot bounds");
goto end;
}
const uchar *data = table + offset + 6;
quint16 nPairs;
if (!qSafeFromBigEndian(data, end, &nPairs))
goto end;
if(nPairs * 6 + 8 > length - 6) {
// qDebug("corrupt table!");
// corrupt table
goto end;
}
int off = 8;
for(int i = 0; i < nPairs; ++i) {
QFontEngine::KernPair p;
quint16 tmp;
if (!qSafeFromBigEndian(data + off, end, &tmp))
goto end;
p.left_right = uint(tmp) << 16;
if (!qSafeFromBigEndian(data + off + 2, end, &tmp))
goto end;
p.left_right |= tmp;
if (!qSafeFromBigEndian(data + off + 4, end, &tmp))
goto end;
p.adjust = QFixed(int(short(tmp))) / scalingFactor;
kerning_pairs.append(p);
off += 6;
}
}
offset += length;
}
}
end:
qSort(kerning_pairs);
// for (int i = 0; i < kerning_pairs.count(); ++i)
// qDebug() << 'i' << i << "left_right" << hex << kerning_pairs.at(i).left_right;
}
#else
void QFontEngine::doKerning(QGlyphLayout *, QTextEngine::ShaperFlags) const
{
}
#endif
int QFontEngine::glyphCount() const
{
QByteArray maxpTable = getSfntTable(MAKE_TAG('m', 'a', 'x', 'p'));
if (maxpTable.size() < 6)
return 0;
const uchar *source = reinterpret_cast<const uchar *>(maxpTable.constData() + 4);
const uchar *end = source + maxpTable.size();
quint16 count = 0;
qSafeFromBigEndian(source, end, &count);
return count;
}
const uchar *QFontEngine::getCMap(const uchar *table, uint tableSize, bool *isSymbolFont, int *cmapSize)
{
const uchar *header = table;
const uchar *endPtr = table + tableSize;
// version check
quint16 version;
if (!qSafeFromBigEndian(header, endPtr, &version) || version != 0)
return 0;
quint16 numTables;
if (!qSafeFromBigEndian(header + 2, endPtr, &numTables))
return 0;
const uchar *maps = table + 4;
enum {
Invalid,
AppleRoman,
Symbol,
Unicode11,
Unicode,
MicrosoftUnicode,
MicrosoftUnicodeExtended
};
int symbolTable = -1;
int tableToUse = -1;
int score = Invalid;
for (int n = 0; n < numTables; ++n) {
quint16 platformId;
if (!qSafeFromBigEndian(maps + 8 * n, endPtr, &platformId))
return 0;
quint16 platformSpecificId;
if (!qSafeFromBigEndian(maps + 8 * n + 2, endPtr, &platformSpecificId))
return 0;
switch (platformId) {
case 0: // Unicode
if (score < Unicode &&
(platformSpecificId == 0 ||
platformSpecificId == 2 ||
platformSpecificId == 3)) {
tableToUse = n;
score = Unicode;
} else if (score < Unicode11 && platformSpecificId == 1) {
tableToUse = n;
score = Unicode11;
}
break;
case 1: // Apple
if (score < AppleRoman && platformSpecificId == 0) { // Apple Roman
tableToUse = n;
score = AppleRoman;
}
break;
case 3: // Microsoft
switch (platformSpecificId) {
case 0:
symbolTable = n;
if (score < Symbol) {
tableToUse = n;
score = Symbol;
}
break;
case 1:
if (score < MicrosoftUnicode) {
tableToUse = n;
score = MicrosoftUnicode;
}
break;
case 0xa:
if (score < MicrosoftUnicodeExtended) {
tableToUse = n;
score = MicrosoftUnicodeExtended;
}
break;
default:
break;
}
default:
break;
}
}
if(tableToUse < 0)
return 0;
resolveTable:
*isSymbolFont = (symbolTable > -1);
quint32 unicode_table;
if (!qSafeFromBigEndian(maps + 8 * tableToUse + 4, endPtr, &unicode_table))
return 0;
if (!unicode_table)
return 0;
// get the header of the unicode table
header = table + unicode_table;
quint16 format;
if (!qSafeFromBigEndian(header, endPtr, &format))
return 0;
quint32 length;
if (format < 8) {
quint16 tmp;
if (!qSafeFromBigEndian(header + 2, endPtr, &tmp))
return 0;
length = tmp;
} else {
if (!qSafeFromBigEndian(header + 4, endPtr, &length))
return 0;
}
if (table + unicode_table + length > endPtr)
return 0;
*cmapSize = length;
// To support symbol fonts that contain a unicode table for the symbol area
// we check the cmap tables and fall back to symbol font unless that would
// involve losing information from the unicode table
if (symbolTable > -1 && ((score == Unicode) || (score == Unicode11))) {
const uchar *selectedTable = table + unicode_table;
// Check that none of the latin1 range are in the unicode table
bool unicodeTableHasLatin1 = false;
for (int uc=0x00; uc<0x100; ++uc) {
if (getTrueTypeGlyphIndex(selectedTable, length, uc) != 0) {
unicodeTableHasLatin1 = true;
break;
}
}
// Check that at least one symbol char is in the unicode table
bool unicodeTableHasSymbols = false;
if (!unicodeTableHasLatin1) {
for (int uc=0xf000; uc<0xf100; ++uc) {
if (getTrueTypeGlyphIndex(selectedTable, length, uc) != 0) {
unicodeTableHasSymbols = true;
break;
}
}
}
// Fall back to symbol table
if (!unicodeTableHasLatin1 && unicodeTableHasSymbols) {
tableToUse = symbolTable;
score = Symbol;
goto resolveTable;
}
}
return table + unicode_table;
}
quint32 QFontEngine::getTrueTypeGlyphIndex(const uchar *cmap, int cmapSize, uint unicode)
{
const uchar *end = cmap + cmapSize;
quint16 format;
if (!qSafeFromBigEndian(cmap, end, &format))
return 0;
if (format == 0) {
const uchar *ptr = cmap + 6 + unicode;
if (unicode < 256 && ptr < end)
return quint32(*ptr);
} else if (format == 4) {
/* some fonts come with invalid cmap tables, where the last segment
specified end = start = rangeoffset = 0xffff, delta = 0x0001
Since 0xffff is never a valid Unicode char anyway, we just get rid of the issue
by returning 0 for 0xffff
*/
if(unicode >= 0xffff)
return 0;
quint16 segCountX2;
if (!qSafeFromBigEndian(cmap + 6, end, &segCountX2))
return 0;
const unsigned char *ends = cmap + 14;
int i = 0;
for (; i < segCountX2/2; ++i) {
quint16 codePoint;
if (!qSafeFromBigEndian(ends + 2 * i, end, &codePoint))
return 0;
if (codePoint >= unicode)
break;
}
const unsigned char *idx = ends + segCountX2 + 2 + 2*i;
quint16 startIndex;
if (!qSafeFromBigEndian(idx, end, &startIndex))
return 0;
if (startIndex > unicode)
return 0;
idx += segCountX2;
quint16 tmp;
if (!qSafeFromBigEndian(idx, end, &tmp))
return 0;
qint16 idDelta = qint16(tmp);
idx += segCountX2;
quint16 idRangeoffset_t;
if (!qSafeFromBigEndian(idx, end, &idRangeoffset_t))
return 0;
quint16 glyphIndex;
if (idRangeoffset_t) {
quint16 id;
if (!qSafeFromBigEndian(idRangeoffset_t + 2 * (unicode - startIndex) + idx, end, &id))
return 0;
if (id)
glyphIndex = (idDelta + id) % 0x10000;
else
glyphIndex = 0;
} else {
glyphIndex = (idDelta + unicode) % 0x10000;
}
return glyphIndex;
} else if (format == 6) {
quint16 tableSize;
if (!qSafeFromBigEndian(cmap + 2, end, &tableSize))
return 0;
quint16 firstCode6;
if (!qSafeFromBigEndian(cmap + 6, end, &firstCode6))
return 0;
if (unicode < firstCode6)
return 0;
quint16 entryCount6;
if (!qSafeFromBigEndian(cmap + 8, end, &entryCount6))
return 0;
if (entryCount6 * 2 + 10 > tableSize)
return 0;
quint16 sentinel6 = firstCode6 + entryCount6;
if (unicode >= sentinel6)
return 0;
quint16 entryIndex6 = unicode - firstCode6;
quint16 index = 0;
qSafeFromBigEndian(cmap + 10 + (entryIndex6 * 2), end, &index);
return index;
} else if (format == 12) {
quint32 nGroups;
if (!qSafeFromBigEndian(cmap + 12, end, &nGroups))
return 0;
cmap += 16; // move to start of groups
int left = 0, right = nGroups - 1;
while (left <= right) {
int middle = left + ( ( right - left ) >> 1 );
quint32 startCharCode;
if (!qSafeFromBigEndian(cmap + 12 * middle, end, &startCharCode))
return 0;
if(unicode < startCharCode)
right = middle - 1;
else {
quint32 endCharCode;
if (!qSafeFromBigEndian(cmap + 12 * middle + 4, end, &endCharCode))
return 0;
if (unicode <= endCharCode) {
quint32 index;
if (!qSafeFromBigEndian(cmap + 12 * middle + 8, end, &index))
return 0;
return index + unicode - startCharCode;
}
left = middle + 1;
}
}
} else {
qDebug("cmap table of format %d not implemented", format);
}
return 0;
}
QByteArray QFontEngine::convertToPostscriptFontFamilyName(const QByteArray &family)
{
QByteArray f = family;
f.replace(' ', "");
f.replace('(', "");
f.replace(')', "");
f.replace('<', "");
f.replace('>', "");
f.replace('[', "");
f.replace(']', "");
f.replace('{', "");
f.replace('}', "");
f.replace('/', "");
f.replace('%', "");
return f;
}
Q_GLOBAL_STATIC_WITH_INITIALIZER(QVector<QRgb>, qt_grayPalette, {
x->resize(256);
QRgb *it = x->data();
for (int i = 0; i < x->size(); ++i, ++it)
*it = 0xff000000 | i | (i<<8) | (i<<16);
})
const QVector<QRgb> &QFontEngine::grayPalette()
{
return *qt_grayPalette();
}
QFixed QFontEngine::lastRightBearing(const QGlyphLayout &glyphs, bool round)
{
if (glyphs.numGlyphs >= 1) {
glyph_t glyph = glyphs.glyphs[glyphs.numGlyphs - 1];
glyph_metrics_t gi = boundingBox(glyph);
if (gi.isValid())
return round ? QFixed(qRound(gi.xoff - gi.x - gi.width))
: QFixed(gi.xoff - gi.x - gi.width);
}
return 0;
}
// ------------------------------------------------------------------
// The box font engine
// ------------------------------------------------------------------
QFontEngineBox::QFontEngineBox(int size)
: _size(size)
{
cache_cost = sizeof(QFontEngineBox);
}
QFontEngineBox::~QFontEngineBox()
{
}
bool QFontEngineBox::stringToCMap(const QChar *, int len, QGlyphLayout *glyphs, int *nglyphs, QTextEngine::ShaperFlags) const
{
if (*nglyphs < len) {
*nglyphs = len;
return false;
}
for (int i = 0; i < len; i++) {
glyphs->glyphs[i] = 0;
glyphs->advances_x[i] = _size;
glyphs->advances_y[i] = 0;
}
*nglyphs = len;
glyphs->numGlyphs = len;
return true;
}
void QFontEngineBox::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags) const
{
for (int i = 0; i < glyphs->numGlyphs; i++) {
glyphs->advances_x[i] = _size;
glyphs->advances_y[i] = 0;
}
}
void QFontEngineBox::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags)
{
if (!glyphs.numGlyphs)
return;
QVarLengthArray<QFixedPoint> positions;
QVarLengthArray<glyph_t> positioned_glyphs;
QTransform matrix = QTransform::fromTranslate(x, y - _size);
getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
QSize s(_size - 3, _size - 3);
for (int k = 0; k < positions.size(); k++)
path->addRect(QRectF(positions[k].toPointF(), s));
}
glyph_metrics_t QFontEngineBox::boundingBox(const QGlyphLayout &glyphs)
{
glyph_metrics_t overall;
overall.width = _size*glyphs.numGlyphs;
overall.height = _size;
overall.xoff = overall.width;
return overall;
}
#if defined(Q_WS_QWS) || defined(Q_WS_QPA)
void QFontEngineBox::draw(QPaintEngine *p, qreal x, qreal y, const QTextItemInt &ti)
{
if (!ti.glyphs.numGlyphs)
return;
// any fixes here should probably also be done in QPaintEnginePrivate::drawBoxTextItem
QSize s(_size - 3, _size - 3);
QVarLengthArray<QFixedPoint> positions;
QVarLengthArray<glyph_t> glyphs;
QTransform matrix = QTransform::fromTranslate(x, y - _size);
ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
if (glyphs.size() == 0)
return;
QPainter *painter = p->painter();
painter->save();
painter->setBrush(Qt::NoBrush);
QPen pen = painter->pen();
pen.setWidthF(lineThickness().toReal());
painter->setPen(pen);
for (int k = 0; k < positions.size(); k++)
painter->drawRect(QRectF(positions[k].toPointF(), s));
painter->restore();
}
#endif
glyph_metrics_t QFontEngineBox::boundingBox(glyph_t)
{
return glyph_metrics_t(0, -_size, _size, _size, _size, 0);
}
QFixed QFontEngineBox::ascent() const
{
return _size;
}
QFixed QFontEngineBox::descent() const
{
return 0;
}
QFixed QFontEngineBox::leading() const
{
QFixed l = _size * QFixed::fromReal(qreal(0.15));
return l.ceil();
}
qreal QFontEngineBox::maxCharWidth() const
{
return _size;
}
#ifdef Q_WS_X11
int QFontEngineBox::cmap() const
{
return -1;
}
#endif
const char *QFontEngineBox::name() const
{
return "null";
}
bool QFontEngineBox::canRender(const QChar *, int)
{
return true;
}
QFontEngine::Type QFontEngineBox::type() const
{
return Box;
}
QImage QFontEngineBox::alphaMapForGlyph(glyph_t)
{
QImage image(_size, _size, QImage::Format_Indexed8);
QVector<QRgb> colors(256);
for (int i=0; i<256; ++i)
colors[i] = qRgba(0, 0, 0, i);
image.setColorTable(colors);
image.fill(0);
// can't use qpainter for index8; so use setPixel to draw our rectangle.
for (int i=2; i <= _size-3; ++i) {
image.setPixel(i, 2, 255);
image.setPixel(i, _size-3, 255);
image.setPixel(2, i, 255);
image.setPixel(_size-3, i, 255);
}
return image;
}
// ------------------------------------------------------------------
// Multi engine
// ------------------------------------------------------------------
static inline uchar highByte(glyph_t glyph)
{ return glyph >> 24; }
// strip high byte from glyph
static inline glyph_t stripped(glyph_t glyph)
{ return glyph & 0x00ffffff; }
QFontEngineMulti::QFontEngineMulti(int engineCount)
{
engines.fill(0, engineCount);
cache_cost = 0;
}
QFontEngineMulti::~QFontEngineMulti()
{
for (int i = 0; i < engines.size(); ++i) {
QFontEngine *fontEngine = engines.at(i);
if (fontEngine && !fontEngine->ref.deref())
delete fontEngine;
}
}
bool QFontEngineMulti::stringToCMap(const QChar *str, int len,
QGlyphLayout *glyphs, int *nglyphs,
QTextEngine::ShaperFlags flags) const
{
int ng = *nglyphs;
if (!engine(0)->stringToCMap(str, len, glyphs, &ng, flags))
return false;
int glyph_pos = 0;
for (int i = 0; i < len; ++i) {
bool surrogate = (str[i].isHighSurrogate() && i < len-1 && str[i+1].isLowSurrogate());
if (glyphs->glyphs[glyph_pos] == 0 && str[i].category() != QChar::Separator_Line) {
QGlyphLayoutInstance tmp = glyphs->instance(glyph_pos);
for (int x = 1; x < engines.size(); ++x) {
QFontEngine *engine = engines.at(x);
if (!engine) {
const_cast<QFontEngineMulti *>(this)->loadEngine(x);
engine = engines.at(x);
}
Q_ASSERT(engine != 0);
if (engine->type() == Box)
continue;
glyphs->advances_x[glyph_pos] = glyphs->advances_y[glyph_pos] = 0;
glyphs->offsets[glyph_pos] = QFixedPoint();
int num = 2;
QGlyphLayout offs = glyphs->mid(glyph_pos, num);
engine->stringToCMap(str + i, surrogate ? 2 : 1, &offs, &num, flags);
Q_ASSERT(num == 1); // surrogates only give 1 glyph
if (glyphs->glyphs[glyph_pos]) {
// set the high byte to indicate which engine the glyph came from
glyphs->glyphs[glyph_pos] |= (x << 24);
break;
}
}
// ensure we use metrics from the 1st font when we use the fallback image.
if (!glyphs->glyphs[glyph_pos]) {
glyphs->setInstance(glyph_pos, tmp);
}
}
if (surrogate)
++i;
++glyph_pos;
}
*nglyphs = ng;
glyphs->numGlyphs = ng;
return true;
}
glyph_metrics_t QFontEngineMulti::boundingBox(const QGlyphLayout &glyphs)
{
if (glyphs.numGlyphs <= 0)
return glyph_metrics_t();
glyph_metrics_t overall;
int which = highByte(glyphs.glyphs[0]);
int start = 0;
int end, i;
for (end = 0; end < glyphs.numGlyphs; ++end) {
const int e = highByte(glyphs.glyphs[end]);
if (e == which)
continue;
// set the high byte to zero
for (i = start; i < end; ++i)
glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
// merge the bounding box for this run
const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
overall.x = qMin(overall.x, gm.x);
overall.y = qMin(overall.y, gm.y);
overall.width = overall.xoff + gm.width;
overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
qMin(overall.y, gm.y);
overall.xoff += gm.xoff;
overall.yoff += gm.yoff;
// reset the high byte for all glyphs
const int hi = which << 24;
for (i = start; i < end; ++i)
glyphs.glyphs[i] = hi | glyphs.glyphs[i];
// change engine
start = end;
which = e;
}
// set the high byte to zero
for (i = start; i < end; ++i)
glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
// merge the bounding box for this run
const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
overall.x = qMin(overall.x, gm.x);
overall.y = qMin(overall.y, gm.y);
overall.width = overall.xoff + gm.width;
overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
qMin(overall.y, gm.y);
overall.xoff += gm.xoff;
overall.yoff += gm.yoff;
// reset the high byte for all glyphs
const int hi = which << 24;
for (i = start; i < end; ++i)
glyphs.glyphs[i] = hi | glyphs.glyphs[i];
return overall;
}
void QFontEngineMulti::getGlyphBearings(glyph_t glyph, qreal *leftBearing, qreal *rightBearing)
{
int which = highByte(glyph);
engine(which)->getGlyphBearings(stripped(glyph), leftBearing, rightBearing);
}
void QFontEngineMulti::addOutlineToPath(qreal x, qreal y, const QGlyphLayout &glyphs,
QPainterPath *path, QTextItem::RenderFlags flags)
{
if (glyphs.numGlyphs <= 0)
return;
int which = highByte(glyphs.glyphs[0]);
int start = 0;
int end, i;
if (flags & QTextItem::RightToLeft) {
for (int gl = 0; gl < glyphs.numGlyphs; gl++) {
x += glyphs.advances_x[gl].toReal();
y += glyphs.advances_y[gl].toReal();
}
}
for (end = 0; end < glyphs.numGlyphs; ++end) {
const int e = highByte(glyphs.glyphs[end]);
if (e == which)
continue;
if (flags & QTextItem::RightToLeft) {
for (i = start; i < end; ++i) {
x -= glyphs.advances_x[i].toReal();
y -= glyphs.advances_y[i].toReal();
}
}
// set the high byte to zero
for (i = start; i < end; ++i)
glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
// reset the high byte for all glyphs and update x and y
const int hi = which << 24;
for (i = start; i < end; ++i)
glyphs.glyphs[i] = hi | glyphs.glyphs[i];
if (!(flags & QTextItem::RightToLeft)) {
for (i = start; i < end; ++i) {
x += glyphs.advances_x[i].toReal();
y += glyphs.advances_y[i].toReal();
}
}
// change engine
start = end;
which = e;
}
if (flags & QTextItem::RightToLeft) {
for (i = start; i < end; ++i) {
x -= glyphs.advances_x[i].toReal();
y -= glyphs.advances_y[i].toReal();
}
}
// set the high byte to zero
for (i = start; i < end; ++i)
glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
// reset the high byte for all glyphs
const int hi = which << 24;
for (i = start; i < end; ++i)
glyphs.glyphs[i] = hi | glyphs.glyphs[i];
}
void QFontEngineMulti::recalcAdvances(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
{
if (glyphs->numGlyphs <= 0)
return;
int which = highByte(glyphs->glyphs[0]);
int start = 0;
int end, i;
for (end = 0; end < glyphs->numGlyphs; ++end) {
const int e = highByte(glyphs->glyphs[end]);
if (e == which)
continue;
// set the high byte to zero
for (i = start; i < end; ++i)
glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
QGlyphLayout offs = glyphs->mid(start, end - start);
engine(which)->recalcAdvances(&offs, flags);
// reset the high byte for all glyphs and update x and y
const int hi = which << 24;
for (i = start; i < end; ++i)
glyphs->glyphs[i] = hi | glyphs->glyphs[i];
// change engine
start = end;
which = e;
}
// set the high byte to zero
for (i = start; i < end; ++i)
glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
QGlyphLayout offs = glyphs->mid(start, end - start);
engine(which)->recalcAdvances(&offs, flags);
// reset the high byte for all glyphs
const int hi = which << 24;
for (i = start; i < end; ++i)
glyphs->glyphs[i] = hi | glyphs->glyphs[i];
}
void QFontEngineMulti::doKerning(QGlyphLayout *glyphs, QTextEngine::ShaperFlags flags) const
{
if (glyphs->numGlyphs <= 0)
return;
int which = highByte(glyphs->glyphs[0]);
int start = 0;
int end, i;
for (end = 0; end < glyphs->numGlyphs; ++end) {
const int e = highByte(glyphs->glyphs[end]);
if (e == which)
continue;
// set the high byte to zero
for (i = start; i < end; ++i)
glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
QGlyphLayout offs = glyphs->mid(start, end - start);
engine(which)->doKerning(&offs, flags);
// reset the high byte for all glyphs and update x and y
const int hi = which << 24;
for (i = start; i < end; ++i)
glyphs->glyphs[i] = hi | glyphs->glyphs[i];
// change engine
start = end;
which = e;
}
// set the high byte to zero
for (i = start; i < end; ++i)
glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
QGlyphLayout offs = glyphs->mid(start, end - start);
engine(which)->doKerning(&offs, flags);
// reset the high byte for all glyphs
const int hi = which << 24;
for (i = start; i < end; ++i)
glyphs->glyphs[i] = hi | glyphs->glyphs[i];
}
glyph_metrics_t QFontEngineMulti::boundingBox(glyph_t glyph)
{
const int which = highByte(glyph);
Q_ASSERT(which < engines.size());
return engine(which)->boundingBox(stripped(glyph));
}
QFixed QFontEngineMulti::ascent() const
{ return engine(0)->ascent(); }
QFixed QFontEngineMulti::descent() const
{ return engine(0)->descent(); }
QFixed QFontEngineMulti::leading() const
{
return engine(0)->leading();
}
QFixed QFontEngineMulti::xHeight() const
{
return engine(0)->xHeight();
}
QFixed QFontEngineMulti::averageCharWidth() const
{
return engine(0)->averageCharWidth();
}
QFixed QFontEngineMulti::lineThickness() const
{
return engine(0)->lineThickness();
}
QFixed QFontEngineMulti::underlinePosition() const
{
return engine(0)->underlinePosition();
}
qreal QFontEngineMulti::maxCharWidth() const
{
return engine(0)->maxCharWidth();
}
qreal QFontEngineMulti::minLeftBearing() const
{
return engine(0)->minLeftBearing();
}
qreal QFontEngineMulti::minRightBearing() const
{
return engine(0)->minRightBearing();
}
bool QFontEngineMulti::canRender(const QChar *string, int len)
{
if (engine(0)->canRender(string, len))
return true;
QVarLengthGlyphLayoutArray glyphs(len);
int nglyphs = len;
if (stringToCMap(string, len, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly) == false) {
glyphs.resize(nglyphs);
stringToCMap(string, len, &glyphs, &nglyphs, QTextEngine::GlyphIndicesOnly);
}
bool allExist = true;
for (int i = 0; i < nglyphs; i++) {
if (!glyphs.glyphs[i]) {
allExist = false;
break;
}
}
return allExist;
}
QImage QFontEngineMulti::alphaMapForGlyph(glyph_t)
{
Q_ASSERT(false);
return QImage();
}
QT_END_NAMESPACE
|