182bool QFontEngine::supportsScript(QChar::Script script)
const
184 if (type() <= QFontEngine::Multi)
190 if (!scriptRequiresOpenType(script))
193#if QT_CONFIG(harfbuzz)
195 uint lenMort = 0, lenMorx = 0;
196 if (getSfntTableData(QFont::Tag(
"mort").value(),
nullptr, &lenMort)
197 || getSfntTableData(QFont::Tag(
"morx").value(),
nullptr, &lenMorx)) {
201 if (hb_face_t *face = hb_qt_face_get_for_engine(
const_cast<QFontEngine *>(
this))) {
202 unsigned int script_count = HB_OT_MAX_TAGS_PER_SCRIPT;
203 hb_tag_t script_tags[HB_OT_MAX_TAGS_PER_SCRIPT];
205 hb_ot_tags_from_script_and_language(hb_qt_script_to_script(script), HB_LANGUAGE_INVALID,
206 &script_count, script_tags,
209 if (hb_ot_layout_table_select_script(face, HB_OT_TAG_GSUB, script_count, script_tags,
nullptr,
nullptr))
269void QFontEngine::getGlyphPositions(
const QGlyphLayout &glyphs,
const QTransform &matrix, QTextItem::RenderFlags flags,
270 QVarLengthArray<glyph_t> &glyphs_out, QVarLengthArray<QFixedPoint> &positions)
275 const bool transform = matrix.m11() != 1.
276 || matrix.m12() != 0.
277 || matrix.m21() != 0.
278 || matrix.m22() != 1.;
280 xpos = QFixed::fromReal(matrix.dx());
281 ypos = QFixed::fromReal(matrix.dy());
285 if (flags & QTextItem::RightToLeft) {
286 int i = glyphs.numGlyphs;
287 int totalKashidas = 0;
289 if (glyphs.attributes[i].dontPrint)
291 xpos += glyphs.advances[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
292 totalKashidas += glyphs.justifications[i].nKashidas;
294 positions.resize(glyphs.numGlyphs+totalKashidas);
295 glyphs_out.resize(glyphs.numGlyphs+totalKashidas);
298 while(i < glyphs.numGlyphs) {
299 if (glyphs.attributes[i].dontPrint) {
303 xpos -= glyphs.advances[i];
305 QFixed gpos_x = xpos + glyphs.offsets[i].x;
306 QFixed gpos_y = ypos + glyphs.offsets[i].y;
308 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
309 gpos = gpos * matrix;
310 gpos_x = QFixed::fromReal(gpos.x());
311 gpos_y = QFixed::fromReal(gpos.y());
313 positions[current].x = gpos_x;
314 positions[current].y = gpos_y;
315 glyphs_out[current] = glyphs.glyphs[i];
317 if (glyphs.justifications[i].nKashidas) {
320 glyph_t kashidaGlyph = glyphIndex(ch.unicode());
325 g.glyphs = &kashidaGlyph;
326 g.advances = &kashidaWidth;
327 recalcAdvances(&g, { });
329 for (uint k = 0; k < glyphs.justifications[i].nKashidas; ++k) {
330 xpos -= kashidaWidth;
332 QFixed gpos_x = xpos + glyphs.offsets[i].x;
333 QFixed gpos_y = ypos + glyphs.offsets[i].y;
335 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
336 gpos = gpos * matrix;
337 gpos_x = QFixed::fromReal(gpos.x());
338 gpos_y = QFixed::fromReal(gpos.y());
340 positions[current].x = gpos_x;
341 positions[current].y = gpos_y;
342 glyphs_out[current] = kashidaGlyph;
346 xpos -= QFixed::fromFixed(glyphs.justifications[i].space_18d6);
351 positions.resize(glyphs.numGlyphs);
352 glyphs_out.resize(glyphs.numGlyphs);
355 while (i < glyphs.numGlyphs) {
356 if (!glyphs.attributes[i].dontPrint) {
357 positions[current].x = xpos + glyphs.offsets[i].x;
358 positions[current].y = ypos + glyphs.offsets[i].y;
359 glyphs_out[current] = glyphs.glyphs[i];
360 xpos += glyphs.advances[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
366 while (i < glyphs.numGlyphs) {
367 if (!glyphs.attributes[i].dontPrint) {
368 QFixed gpos_x = xpos + glyphs.offsets[i].x;
369 QFixed gpos_y = ypos + glyphs.offsets[i].y;
370 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
371 gpos = gpos * matrix;
372 positions[current].x = QFixed::fromReal(gpos.x());
373 positions[current].y = QFixed::fromReal(gpos.y());
374 glyphs_out[current] = glyphs.glyphs[i];
375 xpos += glyphs.advances[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
382 positions.resize(current);
383 glyphs_out.resize(current);
384 Q_ASSERT(positions.size() == glyphs_out.size());
396bool QFontEngine::processHheaTable()
const
398 QByteArray hhea = getSfntTable(QFont::Tag(
"hhea").value());
399 if (hhea.size() >= 10) {
400 auto ptr = hhea.constData();
401 qint16 ascent = qFromBigEndian<qint16>(ptr + 4);
402 qint16 descent = qFromBigEndian<qint16>(ptr + 6);
403 qint16 leading = qFromBigEndian<qint16>(ptr + 8);
406 if (ascent == 0 && descent == 0)
409 const qreal unitsPerEm = emSquareSize().toReal();
411 const auto limitForQFixed = qreal(std::numeric_limits<
int>::max() >> 6) / fontDef.pixelSize;
412 if (ascent > limitForQFixed || descent > limitForQFixed || leading > limitForQFixed)
414 m_ascent = QFixed::fromReal(ascent * fontDef.pixelSize / unitsPerEm);
415 m_descent = -QFixed::fromReal(descent * fontDef.pixelSize / unitsPerEm);
416 m_leading = QFixed::fromReal(leading * fontDef.pixelSize / unitsPerEm);
460bool QFontEngine::processOS2Table()
const
462 QByteArray os2 = getSfntTable(QFont::Tag(
"OS/2").value());
463 if (os2.size() >= 78) {
464 auto ptr = os2.constData();
465 quint16 fsSelection = qFromBigEndian<quint16>(ptr + 62);
466 qint16 typoAscent = qFromBigEndian<qint16>(ptr + 68);
467 qint16 typoDescent = qFromBigEndian<qint16>(ptr + 70);
468 qint16 typoLineGap = qFromBigEndian<qint16>(ptr + 72);
469 quint16 winAscent = qFromBigEndian<quint16>(ptr + 74);
470 quint16 winDescent = qFromBigEndian<quint16>(ptr + 76);
472 enum { USE_TYPO_METRICS = 0x80 };
473 const qreal unitsPerEm = emSquareSize().toReal();
474 if (preferTypoLineMetrics() || fsSelection & USE_TYPO_METRICS) {
476 if (typoAscent == 0 && typoDescent == 0)
479 const auto limitForQFixed = qreal(std::numeric_limits<
int>::max() >> 6) / fontDef.pixelSize;
480 if (typoAscent > limitForQFixed || typoDescent > limitForQFixed
481 || typoLineGap > limitForQFixed)
483 m_ascent = QFixed::fromReal(typoAscent * fontDef.pixelSize / unitsPerEm);
484 m_descent = -QFixed::fromReal(typoDescent * fontDef.pixelSize / unitsPerEm);
485 m_leading = QFixed::fromReal(typoLineGap * fontDef.pixelSize / unitsPerEm);
488 if (winAscent == 0 && winDescent == 0)
490 const auto limitForQFixed = qreal(std::numeric_limits<
int>::max() >> 6) / fontDef.pixelSize;
491 if (winAscent > limitForQFixed || winDescent > limitForQFixed)
493 m_ascent = QFixed::fromReal(winAscent * fontDef.pixelSize / unitsPerEm);
494 m_descent = QFixed::fromReal(winDescent * fontDef.pixelSize / unitsPerEm);
495 m_leading = QFixed{};
546qreal QFontEngine::minRightBearing()
const
551 QByteArray hheaTable = getSfntTable(QFont::Tag(
"hhea").value());
553 const uchar *tableData =
reinterpret_cast<
const uchar *>(hheaTable.constData());
555 const quint32 tableVersion = qFromBigEndian<quint32>(tableData);
556 if (tableVersion == 0x00010000) {
563 const int unitsPerEm = emSquareSize().toInt();
564 if (unitsPerEm > 0) {
565 const qreal funitToPixelFactor = fontDef.pixelSize / unitsPerEm;
571 const int largestValidBearing = 4 * unitsPerEm;
573 if (qAbs(
int(minLeftSideBearing)) < largestValidBearing)
574 m_minLeftBearing = minLeftSideBearing * funitToPixelFactor;
575 if (qAbs(
int(minRightSideBearing)) < largestValidBearing)
576 m_minRightBearing = minRightSideBearing * funitToPixelFactor;
587 static const ushort characterSubset[] = {
588 '(',
'C',
'F',
'K',
'V',
'X',
'Y',
']',
'_',
'f',
'r',
'|',
589 127, 205, 645, 884, 922, 1070, 12386
593 m_minLeftBearing = m_minRightBearing = std::numeric_limits<qreal>::max();
595 for (uint i = 0; i < (
sizeof(characterSubset) /
sizeof(ushort)); ++i) {
596 const glyph_t glyph = glyphIndex(characterSubset[i]);
600 glyph_metrics_t glyphMetrics =
const_cast<QFontEngine *>(
this)->boundingBox(glyph);
603 if (!glyphMetrics.width || !glyphMetrics.height)
606 m_minLeftBearing = qMin(m_minLeftBearing, glyphMetrics.leftBearing().toReal());
607 m_minRightBearing = qMin(m_minRightBearing, glyphMetrics.rightBearing().toReal());
612 qWarning() <<
"Failed to compute left/right minimum bearings for"
617 m_minRightBearing = qreal(QFIXED_MAX);
618 m_minLeftBearing = qreal(QFIXED_MAX);
622 return m_minRightBearing;
635glyph_metrics_t QFontEngine::tightBoundingBox(
const QGlyphLayout &glyphs, QTextItem::RenderFlags flags)
637 glyph_metrics_t overall;
639 const bool isRtl = flags & QTextItem::RightToLeft;
641 for (
int i = 0; i < glyphs.numGlyphs; i++) {
643 if (glyphs.attributes[i].dontPrint)
646 overall.xoff += glyphs.effectiveAdvance(i);
652 for (
int i = 0; i < glyphs.numGlyphs; i++) {
654 if (glyphs.attributes[i].dontPrint)
657 glyph_metrics_t bb = boundingBox(glyphs.glyphs[i]);
660 overall.xoff -= glyphs.effectiveAdvance(i);
662 QFixed x = overall.xoff + glyphs.offsets[i].x + bb.x;
663 QFixed y = overall.yoff + glyphs.offsets[i].y + bb.y;
665 overall.x = qMin(overall.x, x);
666 overall.y = qMin(overall.y, y);
667 xmax = qMax(xmax, x.ceil() + bb.width);
668 ymax = qMax(ymax, y.ceil() + bb.height);
671 overall.xoff += glyphs.effectiveAdvance(i);
672 overall.yoff += bb.yoff;
674 overall.height = qMax(overall.height, ymax - overall.y);
675 overall.width = xmax - overall.x;
681void QFontEngine::addOutlineToPath(qreal x, qreal y,
const QGlyphLayout &glyphs, QPainterPath *path,
682 QTextItem::RenderFlags flags)
684 if (!glyphs.numGlyphs)
687 QVarLengthArray<QFixedPoint> positions;
688 QVarLengthArray<glyph_t> positioned_glyphs;
689 QTransform matrix = QTransform::fromTranslate(x, y);
690 getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
691 addGlyphsToPath(positioned_glyphs.data(), positions.data(), positioned_glyphs.size(), path, flags);
788void QFontEngine::addBitmapFontToPath(qreal x, qreal y,
const QGlyphLayout &glyphs,
789 QPainterPath *path, QTextItem::RenderFlags flags)
793 QFixed advanceX = QFixed::fromReal(x);
794 QFixed advanceY = QFixed::fromReal(y);
795 for (
int i=0; i < glyphs.numGlyphs; ++i) {
796 glyph_metrics_t metrics = boundingBox(glyphs.glyphs[i]);
797 if (metrics.width.value() == 0 || metrics.height.value() == 0) {
798 advanceX += glyphs.advances[i];
801 const QImage alphaMask = alphaMapForGlyph(glyphs.glyphs[i]);
803 const int w = alphaMask.width();
804 const int h = alphaMask.height();
805 const qsizetype srcBpl = alphaMask.bytesPerLine();
807 if (alphaMask.depth() == 1) {
810 bitmap = QImage(w, h, QImage::Format_Mono);
811 const uchar *imageData = alphaMask.bits();
812 const qsizetype destBpl = bitmap.bytesPerLine();
813 uchar *bitmapData = bitmap.bits();
815 for (
int yi = 0; yi < h; ++yi) {
816 const uchar *src = imageData + yi*srcBpl;
817 uchar *dst = bitmapData + yi*destBpl;
818 for (
int xi = 0; xi < w; ++xi) {
819 const int byte = xi / 8;
820 const int bit = xi % 8;
824 dst[byte] |= 128 >> bit;
828 const uchar *bitmap_data = bitmap.constBits();
829 QFixedPoint offset = glyphs.offsets[i];
830 advanceX += offset.x;
831 advanceY += offset.y;
832 qt_addBitmapToPath((advanceX + metrics.x).toReal(), (advanceY + metrics.y).toReal(), bitmap_data, bitmap.bytesPerLine(), w, h, path);
833 advanceX += glyphs.advances[i];
1094QImage QFontEngine::renderedPathForGlyph(glyph_t glyph,
const QColor &color)
1096 glyph_metrics_t gm = boundingBox(glyph);
1097 int glyph_x = qFloor(gm.x.toReal());
1098 int glyph_y = qFloor(gm.y.toReal());
1099 int glyph_width = qCeil((gm.x + gm.width).toReal()) - glyph_x;
1100 int glyph_height = qCeil((gm.y + gm.height).toReal()) - glyph_y;
1102 if (glyph_width <= 0
1103 || glyph_height <= 0
1104 || glyphBoundingBoxExceedsLimit(glyph_width,
1106 fontDef.pixelSize * fontDef.pixelSize)) {
1114 path.setFillRule(Qt::WindingFill);
1115 QImage im(glyph_width, glyph_height, QImage::Format_ARGB32_Premultiplied);
1116 im.fill(Qt::transparent);
1118 p.setRenderHint(QPainter::Antialiasing);
1119 addGlyphsToPath(&glyph, &pt, 1, &path, { });
1120 p.setPen(Qt::NoPen);
1288void QFontEngine::loadKerningPairs(QFixed scalingFactor)
1290 kerning_pairs.clear();
1292 QByteArray tab = getSfntTable(QFont::Tag(
"kern").value());
1296 const uchar *table =
reinterpret_cast<
const uchar *>(tab.constData());
1297 const uchar *end = table + tab.size();
1300 if (!qSafeFromBigEndian(table, end, &version))
1309 if (!qSafeFromBigEndian(table + 2, end, &numTables))
1314 for(
int i = 0; i < numTables; ++i) {
1315 const uchar *header = table + offset;
1318 if (!qSafeFromBigEndian(header, end, &version))
1322 if (!qSafeFromBigEndian(header + 2, end, &length))
1326 if (!qSafeFromBigEndian(header + 4, end, &coverage))
1330 if (version == 0 && coverage == 0x0001) {
1331 if (offset + length > tab.size()) {
1335 const uchar *data = table + offset + 6;
1338 if (!qSafeFromBigEndian(data, end, &nPairs))
1341 if (nPairs * 6 + 8 > length - 6) {
1348 for(
int i = 0; i < nPairs; ++i) {
1349 QFontEngine::KernPair p;
1352 if (!qSafeFromBigEndian(data + off, end, &tmp))
1355 p.left_right = uint(tmp) << 16;
1356 if (!qSafeFromBigEndian(data + off + 2, end, &tmp))
1359 p.left_right |= tmp;
1361 if (!qSafeFromBigEndian(data + off + 4, end, &tmp))
1364 p.adjust = QFixed(
int(
short(tmp))) / scalingFactor;
1365 kerning_pairs.append(p);
1373 std::sort(kerning_pairs.begin(), kerning_pairs.end());
1423const uchar *QFontEngine::getCMap(
const uchar *table, uint tableSize,
bool *isSymbolFont,
int *cmapSize)
1425 const uchar *header = table;
1426 const uchar *endPtr = table + tableSize;
1430 if (!qSafeFromBigEndian(header, endPtr, &version) || version != 0)
1434 if (!qSafeFromBigEndian(header + 2, endPtr, &numTables))
1437 const uchar *maps = table + 4;
1446 MicrosoftUnicodeExtended
1449 int symbolTable = -1;
1450 int tableToUse = -1;
1451 int score = Invalid;
1452 for (
int n = 0; n < numTables; ++n) {
1453 quint16 platformId = 0;
1454 if (!qSafeFromBigEndian(maps + 8 * n, endPtr, &platformId))
1457 quint16 platformSpecificId = 0;
1458 if (!qSafeFromBigEndian(maps + 8 * n + 2, endPtr, &platformSpecificId))
1461 switch (platformId) {
1463 if (score < Unicode &&
1464 (platformSpecificId == 0 ||
1465 platformSpecificId == 2 ||
1466 platformSpecificId == 3)) {
1469 }
else if (score < Unicode11 && platformSpecificId == 1) {
1475 if (score < AppleRoman && platformSpecificId == 0) {
1481 switch (platformSpecificId) {
1484 if (score < Symbol) {
1490 if (score < MicrosoftUnicode) {
1492 score = MicrosoftUnicode;
1496 if (score < MicrosoftUnicodeExtended) {
1498 score = MicrosoftUnicodeExtended;
1513 *isSymbolFont = (symbolTable > -1);
1515 quint32 unicode_table = 0;
1516 if (!qSafeFromBigEndian(maps + 8 * tableToUse + 4, endPtr, &unicode_table))
1523 header = table + unicode_table;
1526 if (!qSafeFromBigEndian(header, endPtr, &format))
1532 if (!qSafeFromBigEndian(header + 2, endPtr, &tmp))
1536 if (!qSafeFromBigEndian(header + 4, endPtr, &length))
1540 if (table + unicode_table + length > endPtr)
1547 if (symbolTable > -1 && ((score == Unicode) || (score == Unicode11))) {
1548 const uchar *selectedTable = table + unicode_table;
1551 bool unicodeTableHasLatin1 =
false;
1552 for (
int uc=0x00; uc<0x100; ++uc) {
1553 if (getTrueTypeGlyphIndex(selectedTable, length, uc) != 0) {
1554 unicodeTableHasLatin1 =
true;
1560 bool unicodeTableHasSymbols =
false;
1561 if (!unicodeTableHasLatin1) {
1562 for (
int uc=0xf000; uc<0xf100; ++uc) {
1563 if (getTrueTypeGlyphIndex(selectedTable, length, uc) != 0) {
1564 unicodeTableHasSymbols =
true;
1571 if (!unicodeTableHasLatin1 && unicodeTableHasSymbols) {
1572 tableToUse = symbolTable;
1578 return table + unicode_table;
1581quint32 QFontEngine::getTrueTypeGlyphIndex(
const uchar *cmap,
int cmapSize, uint unicode)
1583 const uchar *end = cmap + cmapSize;
1585 if (!qSafeFromBigEndian(cmap, end, &format))
1589 const uchar *ptr = cmap + 6 + unicode;
1590 if (unicode < 256 && ptr < end)
1591 return quint32(*ptr);
1592 }
else if (format == 4) {
1594
1595
1596
1597
1598 if (unicode >= 0xffff)
1601 quint16 segCountX2 = 0;
1602 if (!qSafeFromBigEndian(cmap + 6, end, &segCountX2))
1605 const unsigned char *ends = cmap + 14;
1608 for (; i < segCountX2/2; ++i) {
1609 quint16 codePoint = 0;
1610 if (!qSafeFromBigEndian(ends + 2 * i, end, &codePoint))
1612 if (codePoint >= unicode)
1616 const unsigned char *idx = ends + segCountX2 + 2 + 2*i;
1618 quint16 startIndex = 0;
1619 if (!qSafeFromBigEndian(idx, end, &startIndex))
1621 if (startIndex > unicode)
1627 if (!qSafeFromBigEndian(idx, end, &tmp))
1629 qint16 idDelta = qint16(tmp);
1633 quint16 idRangeoffset_t = 0;
1634 if (!qSafeFromBigEndian(idx, end, &idRangeoffset_t))
1637 quint16 glyphIndex = 0;
1638 if (idRangeoffset_t) {
1640 if (!qSafeFromBigEndian(idRangeoffset_t + 2 * (unicode - startIndex) + idx, end, &id))
1644 glyphIndex = (idDelta + id) % 0x10000;
1648 glyphIndex = (idDelta + unicode) % 0x10000;
1651 }
else if (format == 6) {
1652 quint16 tableSize = 0;
1653 if (!qSafeFromBigEndian(cmap + 2, end, &tableSize))
1656 quint16 firstCode6 = 0;
1657 if (!qSafeFromBigEndian(cmap + 6, end, &firstCode6))
1659 if (unicode < firstCode6)
1662 quint16 entryCount6 = 0;
1663 if (!qSafeFromBigEndian(cmap + 8, end, &entryCount6))
1665 if (entryCount6 * 2 + 10 > tableSize)
1668 quint16 sentinel6 = firstCode6 + entryCount6;
1669 if (unicode >= sentinel6)
1672 quint16 entryIndex6 = unicode - firstCode6;
1675 qSafeFromBigEndian(cmap + 10 + (entryIndex6 * 2), end, &index);
1677 }
else if (format == 12) {
1678 quint32 nGroups = 0;
1679 if (!qSafeFromBigEndian(cmap + 12, end, &nGroups))
1684 int left = 0, right = nGroups - 1;
1685 while (left <= right) {
1686 int middle = left + ( ( right - left ) >> 1 );
1688 quint32 startCharCode = 0;
1689 if (!qSafeFromBigEndian(cmap + 12 * middle, end, &startCharCode))
1692 if (unicode < startCharCode)
1695 quint32 endCharCode = 0;
1696 if (!qSafeFromBigEndian(cmap + 12 * middle + 4, end, &endCharCode))
1699 if (unicode <= endCharCode) {
1701 if (!qSafeFromBigEndian(cmap + 12 * middle + 8, end, &index))
1706 return std::clamp(index + unicode - startCharCode, quint32(0), quint32(0xffff));
1712 qDebug(
"cmap table of format %d not implemented", format);
1869void QFontEngineBox::addOutlineToPath(qreal x, qreal y,
const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags)
1871 if (!glyphs.numGlyphs)
1874 QVarLengthArray<QFixedPoint> positions;
1875 QVarLengthArray<glyph_t> positioned_glyphs;
1876 QTransform matrix = QTransform::fromTranslate(x, y - _size);
1877 getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
1879 QSize s(_size - 3, _size - 3);
1880 for (
int k = 0; k < positions.size(); k++)
1881 path->addRect(QRectF(positions[k].toPointF(), s));
1893void QFontEngineBox::draw(QPaintEngine *p, qreal x, qreal y,
const QTextItemInt &ti)
1895 if (!ti.glyphs.numGlyphs)
1899 QSize s(_size - 3, _size - 3);
1901 QVarLengthArray<QFixedPoint> positions;
1902 QVarLengthArray<glyph_t> glyphs;
1903 QTransform matrix = QTransform::fromTranslate(x, y - _size);
1904 ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
1905 if (glyphs.size() == 0)
1909 QPainter *painter = p->painter();
1911 painter->setBrush(Qt::NoBrush);
1912 QPen pen = painter->pen();
1913 pen.setWidthF(lineThickness().toReal());
1914 painter->setPen(pen);
1915 for (
int k = 0; k < positions.size(); k++)
1916 painter->drawRect(QRectF(positions[k].toPointF(), s));
2136int QFontEngineMulti::stringToCMap(
const QChar *str,
int len,
2137 QGlyphLayout *glyphs,
int *nglyphs,
2138 QFontEngine::ShaperFlags flags)
const
2140 qCDebug(lcTextFontCmap) <<
"Mapping characters in"
2141 << QString(str, len)
2142 <<
"; Shaper flags:" << flags;
2144 const int originalNumGlyphs = glyphs->numGlyphs;
2145 int mappedGlyphCount = engine(0)->stringToCMap(str, len, glyphs, nglyphs, flags);
2146 qCDebug(lcTextFontCmap) <<
" Main font mapped"
2149 << (nglyphs !=
nullptr ? *nglyphs : 0)
2152 if (mappedGlyphCount < 0)
2157 bool contextFontMerging = mappedGlyphCount < *nglyphs && (fontDef.styleStrategy & QFont::ContextFontMerging);
2158 if (contextFontMerging) {
2159 qCDebug(lcTextFontCmap) <<
" Context font merging, first pass";
2161 QVarLengthGlyphLayoutArray tempLayout(len);
2162 if (!m_fallbackFamiliesQueried)
2163 const_cast<QFontEngineMulti *>(
this)->ensureFallbackFamiliesQueried();
2165 int maxGlyphCount = 0;
2166 uchar engineIndex = 0;
2167 for (
int x = 1, n = qMin(m_engines.size(), maximumEngineCount()); x < n; ++x) {
2168 int numGlyphs = len;
2169 const_cast<QFontEngineMulti *>(
this)->ensureEngineAt(x);
2170 maxGlyphCount = engine(x)->stringToCMap(str, len, &tempLayout, &numGlyphs, flags);
2171 qCDebug(lcTextFontCmap) <<
" Fallback" << x <<
"mapped" << maxGlyphCount;
2174 if (maxGlyphCount > mappedGlyphCount) {
2175 qCDebug(lcTextFontCmap) <<
" Better match!";
2176 *nglyphs = numGlyphs;
2177 glyphs->numGlyphs = originalNumGlyphs;
2178 glyphs->copy(&tempLayout);
2180 if (maxGlyphCount == numGlyphs)
2183 qCDebug(lcTextFontCmap) <<
" No improvement";
2187 if (engineIndex > 0) {
2188 for (
int y = 0; y < glyphs->numGlyphs; ++y) {
2189 if (glyphs->glyphs[y] != 0)
2190 glyphs->glyphs[y] |= (engineIndex << 24);
2193 contextFontMerging =
false;
2196 mappedGlyphCount = maxGlyphCount;
2202 QStringIterator it(str, str + len);
2204 const bool enableVariationSelectorHack = disableEmojiSegmenter();
2205 char32_t previousUcs4 = 0;
2207 int lastFallback = -1;
2208 while (it.hasNext()) {
2209 const char32_t ucs4 = it.peekNext();
2214 if (lastFallback >= 0 && (ucs4 == 0x200d || ucs4 == 0x200c)) {
2215 QFontEngine *engine = m_engines.at(lastFallback);
2216 glyph_t glyph = engine->glyphIndex(ucs4);
2218 qCDebug(lcTextFontCmap) <<
" Applying ZWJ/ZWNJ special case for"
2219 << QString::number(ucs4, 16)
2220 <<
", lastFallback:" << lastFallback;
2222 glyphs->glyphs[glyph_pos] = glyph;
2223 if (!(flags & GlyphIndicesOnly)) {
2224 QGlyphLayout g = glyphs->mid(glyph_pos, 1);
2225 engine->recalcAdvances(&g, flags);
2229 glyphs->glyphs[glyph_pos] |= (lastFallback << 24);
2237 if (glyphs->glyphs[glyph_pos] == 0 && !isIgnorableChar(ucs4)) {
2238 qCDebug(lcTextFontCmap) <<
" Unmapped character"
2239 << QString::number(ucs4, 16);
2241 if (!m_fallbackFamiliesQueried)
2242 const_cast<QFontEngineMulti *>(
this)->ensureFallbackFamiliesQueried();
2243 for (
int x = contextFontMerging ? 0 : 1, n = qMin(m_engines.size(), maximumEngineCount()); x < n; ++x) {
2244 QFontEngine *engine = m_engines.at(x);
2246 if (!shouldLoadFontEngineForCharacter(x, ucs4)) {
2247 qCDebug(lcTextFontCmap) <<
" Skipping fallback #" << x;
2250 const_cast<QFontEngineMulti *>(
this)->ensureEngineAt(x);
2251 engine = m_engines.at(x);
2255 Q_ASSERT(engine !=
nullptr);
2256 if (engine->type() == Box)
2259 glyph_t glyph = engine->glyphIndex(ucs4);
2260 qCDebug(lcTextFontCmap) <<
" Fallback #" << x
2261 <<
", family:" << engine->fontDef.families
2262 <<
", styleName:" << engine->fontDef.styleName
2263 <<
", style:" << engine->fontDef.style
2264 <<
", weight:" << engine->fontDef.weight
2265 <<
", glyph mapping:" << glyph;
2267 glyphs->glyphs[glyph_pos] = glyph;
2268 if (!(flags & GlyphIndicesOnly)) {
2269 QGlyphLayout g = glyphs->mid(glyph_pos, 1);
2270 engine->recalcAdvances(&g, flags);
2276 glyphs->glyphs[glyph_pos] |= (x << 24);
2284 const int variantSelectorBlock = 0xFE00;
2285 if (enableVariationSelectorHack && (ucs4 & 0xFFF0) == variantSelectorBlock && glyph_pos > 0) {
2286 int selectorFontEngine = glyphs->glyphs[glyph_pos] >> 24;
2287 int precedingCharacterFontEngine = glyphs->glyphs[glyph_pos - 1] >> 24;
2289 if (selectorFontEngine != precedingCharacterFontEngine) {
2295 const QFontEngine *selectedEngine = m_engines.at(precedingCharacterFontEngine);
2296 const bool colorFont = selectedEngine->isColorFont();
2297 const char32_t vs15 = 0xFE0E;
2298 const char32_t vs16 = 0xFE0F;
2299 bool adaptVariantSelector = ucs4 < vs15
2300 || (ucs4 == vs15 && colorFont)
2301 || (ucs4 == vs16 && !colorFont);
2303 if (adaptVariantSelector) {
2304 QFontEngine *engine = m_engines.at(selectorFontEngine);
2305 glyph_t glyph = engine->glyphIndex(previousUcs4);
2307 glyphs->glyphs[glyph_pos - 1] = glyph;
2308 if (!(flags & GlyphIndicesOnly)) {
2309 QGlyphLayout g = glyphs->mid(glyph_pos - 1, 1);
2310 engine->recalcAdvances(&g, flags);
2314 glyphs->glyphs[glyph_pos - 1] |= (selectorFontEngine << 24);
2324 previousUcs4 = ucs4;
2327 *nglyphs = glyph_pos;
2328 glyphs->numGlyphs = glyph_pos;
2329 return mappedGlyphCount;
2339glyph_metrics_t QFontEngineMulti::boundingBox(
const QGlyphLayout &glyphs)
2341 if (glyphs.numGlyphs <= 0)
2342 return glyph_metrics_t();
2344 glyph_metrics_t overall;
2346 int which = highByte(glyphs.glyphs[0]);
2349 for (end = 0; end < glyphs.numGlyphs; ++end) {
2350 const int e = highByte(glyphs.glyphs[end]);
2355 for (i = start; i < end; ++i)
2356 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2359 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
2361 overall.x = qMin(overall.x, gm.x);
2362 overall.y = qMin(overall.y, gm.y);
2363 overall.width = overall.xoff + gm.width;
2364 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
2365 qMin(overall.y, gm.y);
2366 overall.xoff += gm.xoff;
2367 overall.yoff += gm.yoff;
2370 const int hi = which << 24;
2371 for (i = start; i < end; ++i)
2372 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2380 for (i = start; i < end; ++i)
2381 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2384 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
2386 overall.x = qMin(overall.x, gm.x);
2387 overall.y = qMin(overall.y, gm.y);
2388 overall.width = overall.xoff + gm.width;
2389 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
2390 qMin(overall.y, gm.y);
2391 overall.xoff += gm.xoff;
2392 overall.yoff += gm.yoff;
2395 const int hi = which << 24;
2396 for (i = start; i < end; ++i)
2397 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2409void QFontEngineMulti::addOutlineToPath(qreal x, qreal y,
const QGlyphLayout &glyphs,
2410 QPainterPath *path, QTextItem::RenderFlags flags)
2412 if (glyphs.numGlyphs <= 0)
2415 int which = highByte(glyphs.glyphs[0]);
2418 if (flags & QTextItem::RightToLeft) {
2419 for (
int gl = 0; gl < glyphs.numGlyphs; gl++)
2420 x += glyphs.advances[gl].toReal();
2422 for (end = 0; end < glyphs.numGlyphs; ++end) {
2423 const int e = highByte(glyphs.glyphs[end]);
2427 if (flags & QTextItem::RightToLeft) {
2428 for (i = start; i < end; ++i)
2429 x -= glyphs.advances[i].toReal();
2433 for (i = start; i < end; ++i)
2434 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2435 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
2437 const int hi = which << 24;
2438 for (i = start; i < end; ++i)
2439 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2441 if (!(flags & QTextItem::RightToLeft)) {
2442 for (i = start; i < end; ++i)
2443 x += glyphs.advances[i].toReal();
2451 if (flags & QTextItem::RightToLeft) {
2452 for (i = start; i < end; ++i)
2453 x -= glyphs.advances[i].toReal();
2457 for (i = start; i < end; ++i)
2458 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2460 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
2463 const int hi = which << 24;
2464 for (i = start; i < end; ++i)
2465 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2468void QFontEngineMulti::recalcAdvances(QGlyphLayout *glyphs, QFontEngine::ShaperFlags flags)
const
2470 if (glyphs->numGlyphs <= 0)
2473 int which = highByte(glyphs->glyphs[0]);
2476 for (end = 0; end < glyphs->numGlyphs; ++end) {
2477 const int e = highByte(glyphs->glyphs[end]);
2482 for (i = start; i < end; ++i)
2483 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2485 QGlyphLayout offs = glyphs->mid(start, end - start);
2486 engine(which)->recalcAdvances(&offs, flags);
2489 const int hi = which << 24;
2490 for (i = start; i < end; ++i)
2491 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2499 for (i = start; i < end; ++i)
2500 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2502 QGlyphLayout offs = glyphs->mid(start, end - start);
2503 engine(which)->recalcAdvances(&offs, flags);
2506 const int hi = which << 24;
2507 for (i = start; i < end; ++i)
2508 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2511void QFontEngineMulti::doKerning(QGlyphLayout *glyphs, QFontEngine::ShaperFlags flags)
const
2513 if (glyphs->numGlyphs <= 0)
2516 int which = highByte(glyphs->glyphs[0]);
2519 for (end = 0; end < glyphs->numGlyphs; ++end) {
2520 const int e = highByte(glyphs->glyphs[end]);
2525 for (i = start; i < end; ++i)
2526 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2528 QGlyphLayout offs = glyphs->mid(start, end - start);
2529 engine(which)->doKerning(&offs, flags);
2532 const int hi = which << 24;
2533 for (i = start; i < end; ++i)
2534 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2542 for (i = start; i < end; ++i)
2543 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2545 QGlyphLayout offs = glyphs->mid(start, end - start);
2546 engine(which)->doKerning(&offs, flags);
2549 const int hi = which << 24;
2550 for (i = start; i < end; ++i)
2551 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2685QFontEngine *QFontEngineMulti::createMultiFontEngine(QFontEngine *fe,
int script)
2687 QFontEngine *engine =
nullptr;
2688 QFontCache::Key key(fe->fontDef, script,
true);
2689 QFontCache *fc = QFontCache::instance();
2695 const bool faceIsLocal = !fe->faceId().filename.isEmpty();
2696 QFontCache::EngineCache::Iterator it = fc->engineCache.find(key),
2697 end = fc->engineCache.end();
2698 while (it != end && it.key() == key) {
2699 Q_ASSERT(it.value().data->type() == QFontEngine::Multi);
2700 QFontEngineMulti *cachedEngine =
static_cast<QFontEngineMulti *>(it.value().data);
2701 if (fe == cachedEngine->engine(0) || (faceIsLocal && fe->faceId().filename == cachedEngine->engine(0)->faceId().filename)) {
2702 engine = cachedEngine;
2703 fc->updateHitCountAndTimeStamp(it.value());
2709 engine = QGuiApplicationPrivate::instance()->platformIntegration()->fontDatabase()->fontEngineMulti(fe, QFontDatabasePrivate::ExtendedScript(script));
2710 fc->insertEngine(key, engine, !faceIsLocal);