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{};
548qreal QFontEngine::minRightBearing()
const
553 QByteArray hheaTable = getSfntTable(QFont::Tag(
"hhea").value());
555 const uchar *tableData =
reinterpret_cast<
const uchar *>(hheaTable.constData());
564 int unitsPerEm = emSquareSize().toInt();
565 qreal funitToPixelFactor = fontDef.pixelSize / unitsPerEm;
571 static const int largestValidBearing = 4 * unitsPerEm;
573 if (qAbs(minLeftSideBearing) < largestValidBearing)
574 m_minLeftBearing = minLeftSideBearing * funitToPixelFactor;
575 if (qAbs(minRightSideBearing) < largestValidBearing)
576 m_minRightBearing = minRightSideBearing * funitToPixelFactor;
585 static const ushort characterSubset[] = {
586 '(',
'C',
'F',
'K',
'V',
'X',
'Y',
']',
'_',
'f',
'r',
'|',
587 127, 205, 645, 884, 922, 1070, 12386
591 m_minLeftBearing = m_minRightBearing = std::numeric_limits<qreal>::max();
593 for (uint i = 0; i < (
sizeof(characterSubset) /
sizeof(ushort)); ++i) {
594 const glyph_t glyph = glyphIndex(characterSubset[i]);
598 glyph_metrics_t glyphMetrics =
const_cast<QFontEngine *>(
this)->boundingBox(glyph);
601 if (!glyphMetrics.width || !glyphMetrics.height)
604 m_minLeftBearing = qMin(m_minLeftBearing, glyphMetrics.leftBearing().toReal());
605 m_minRightBearing = qMin(m_minRightBearing, glyphMetrics.rightBearing().toReal());
610 qWarning() <<
"Failed to compute left/right minimum bearings for"
614 return m_minRightBearing;
627glyph_metrics_t QFontEngine::tightBoundingBox(
const QGlyphLayout &glyphs, QTextItem::RenderFlags flags)
629 glyph_metrics_t overall;
631 const bool isRtl = flags & QTextItem::RightToLeft;
633 for (
int i = 0; i < glyphs.numGlyphs; i++) {
635 if (glyphs.attributes[i].dontPrint)
638 overall.xoff += glyphs.effectiveAdvance(i);
644 for (
int i = 0; i < glyphs.numGlyphs; i++) {
646 if (glyphs.attributes[i].dontPrint)
649 glyph_metrics_t bb = boundingBox(glyphs.glyphs[i]);
652 overall.xoff -= glyphs.effectiveAdvance(i);
654 QFixed x = overall.xoff + glyphs.offsets[i].x + bb.x;
655 QFixed y = overall.yoff + glyphs.offsets[i].y + bb.y;
657 overall.x = qMin(overall.x, x);
658 overall.y = qMin(overall.y, y);
659 xmax = qMax(xmax, x.ceil() + bb.width);
660 ymax = qMax(ymax, y.ceil() + bb.height);
663 overall.xoff += glyphs.effectiveAdvance(i);
664 overall.yoff += bb.yoff;
666 overall.height = qMax(overall.height, ymax - overall.y);
667 overall.width = xmax - overall.x;
673void QFontEngine::addOutlineToPath(qreal x, qreal y,
const QGlyphLayout &glyphs, QPainterPath *path,
674 QTextItem::RenderFlags flags)
676 if (!glyphs.numGlyphs)
679 QVarLengthArray<QFixedPoint> positions;
680 QVarLengthArray<glyph_t> positioned_glyphs;
681 QTransform matrix = QTransform::fromTranslate(x, y);
682 getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
683 addGlyphsToPath(positioned_glyphs.data(), positions.data(), positioned_glyphs.size(), path, flags);
780void QFontEngine::addBitmapFontToPath(qreal x, qreal y,
const QGlyphLayout &glyphs,
781 QPainterPath *path, QTextItem::RenderFlags flags)
785 QFixed advanceX = QFixed::fromReal(x);
786 QFixed advanceY = QFixed::fromReal(y);
787 for (
int i=0; i < glyphs.numGlyphs; ++i) {
788 glyph_metrics_t metrics = boundingBox(glyphs.glyphs[i]);
789 if (metrics.width.value() == 0 || metrics.height.value() == 0) {
790 advanceX += glyphs.advances[i];
793 const QImage alphaMask = alphaMapForGlyph(glyphs.glyphs[i]);
795 const int w = alphaMask.width();
796 const int h = alphaMask.height();
797 const qsizetype srcBpl = alphaMask.bytesPerLine();
799 if (alphaMask.depth() == 1) {
802 bitmap = QImage(w, h, QImage::Format_Mono);
803 const uchar *imageData = alphaMask.bits();
804 const qsizetype destBpl = bitmap.bytesPerLine();
805 uchar *bitmapData = bitmap.bits();
807 for (
int yi = 0; yi < h; ++yi) {
808 const uchar *src = imageData + yi*srcBpl;
809 uchar *dst = bitmapData + yi*destBpl;
810 for (
int xi = 0; xi < w; ++xi) {
811 const int byte = xi / 8;
812 const int bit = xi % 8;
816 dst[byte] |= 128 >> bit;
820 const uchar *bitmap_data = bitmap.constBits();
821 QFixedPoint offset = glyphs.offsets[i];
822 advanceX += offset.x;
823 advanceY += offset.y;
824 qt_addBitmapToPath((advanceX + metrics.x).toReal(), (advanceY + metrics.y).toReal(), bitmap_data, bitmap.bytesPerLine(), w, h, path);
825 advanceX += glyphs.advances[i];
1013QImage QFontEngine::renderedPathForGlyph(glyph_t glyph,
const QColor &color)
1015 glyph_metrics_t gm = boundingBox(glyph);
1016 int glyph_x = qFloor(gm.x.toReal());
1017 int glyph_y = qFloor(gm.y.toReal());
1018 int glyph_width = qCeil((gm.x + gm.width).toReal()) - glyph_x;
1019 int glyph_height = qCeil((gm.y + gm.height).toReal()) - glyph_y;
1021 if (glyph_width <= 0 || glyph_height <= 0)
1027 path.setFillRule(Qt::WindingFill);
1028 QImage im(glyph_width, glyph_height, QImage::Format_ARGB32_Premultiplied);
1029 im.fill(Qt::transparent);
1031 p.setRenderHint(QPainter::Antialiasing);
1032 addGlyphsToPath(&glyph, &pt, 1, &path, { });
1033 p.setPen(Qt::NoPen);
1201void QFontEngine::loadKerningPairs(QFixed scalingFactor)
1203 kerning_pairs.clear();
1205 QByteArray tab = getSfntTable(QFont::Tag(
"kern").value());
1209 const uchar *table =
reinterpret_cast<
const uchar *>(tab.constData());
1210 const uchar *end = table + tab.size();
1213 if (!qSafeFromBigEndian(table, end, &version))
1222 if (!qSafeFromBigEndian(table + 2, end, &numTables))
1227 for(
int i = 0; i < numTables; ++i) {
1228 const uchar *header = table + offset;
1231 if (!qSafeFromBigEndian(header, end, &version))
1235 if (!qSafeFromBigEndian(header + 2, end, &length))
1239 if (!qSafeFromBigEndian(header + 4, end, &coverage))
1243 if (version == 0 && coverage == 0x0001) {
1244 if (offset + length > tab.size()) {
1248 const uchar *data = table + offset + 6;
1251 if (!qSafeFromBigEndian(data, end, &nPairs))
1254 if (nPairs * 6 + 8 > length - 6) {
1261 for(
int i = 0; i < nPairs; ++i) {
1262 QFontEngine::KernPair p;
1265 if (!qSafeFromBigEndian(data + off, end, &tmp))
1268 p.left_right = uint(tmp) << 16;
1269 if (!qSafeFromBigEndian(data + off + 2, end, &tmp))
1272 p.left_right |= tmp;
1274 if (!qSafeFromBigEndian(data + off + 4, end, &tmp))
1277 p.adjust = QFixed(
int(
short(tmp))) / scalingFactor;
1278 kerning_pairs.append(p);
1286 std::sort(kerning_pairs.begin(), kerning_pairs.end());
1335const uchar *QFontEngine::getCMap(
const uchar *table, uint tableSize,
bool *isSymbolFont,
int *cmapSize)
1337 const uchar *header = table;
1338 const uchar *endPtr = table + tableSize;
1342 if (!qSafeFromBigEndian(header, endPtr, &version) || version != 0)
1346 if (!qSafeFromBigEndian(header + 2, endPtr, &numTables))
1349 const uchar *maps = table + 4;
1358 MicrosoftUnicodeExtended
1361 int symbolTable = -1;
1362 int tableToUse = -1;
1363 int score = Invalid;
1364 for (
int n = 0; n < numTables; ++n) {
1365 quint16 platformId = 0;
1366 if (!qSafeFromBigEndian(maps + 8 * n, endPtr, &platformId))
1369 quint16 platformSpecificId = 0;
1370 if (!qSafeFromBigEndian(maps + 8 * n + 2, endPtr, &platformSpecificId))
1373 switch (platformId) {
1375 if (score < Unicode &&
1376 (platformSpecificId == 0 ||
1377 platformSpecificId == 2 ||
1378 platformSpecificId == 3)) {
1381 }
else if (score < Unicode11 && platformSpecificId == 1) {
1387 if (score < AppleRoman && platformSpecificId == 0) {
1393 switch (platformSpecificId) {
1396 if (score < Symbol) {
1402 if (score < MicrosoftUnicode) {
1404 score = MicrosoftUnicode;
1408 if (score < MicrosoftUnicodeExtended) {
1410 score = MicrosoftUnicodeExtended;
1425 *isSymbolFont = (symbolTable > -1);
1427 quint32 unicode_table = 0;
1428 if (!qSafeFromBigEndian(maps + 8 * tableToUse + 4, endPtr, &unicode_table))
1435 header = table + unicode_table;
1438 if (!qSafeFromBigEndian(header, endPtr, &format))
1444 if (!qSafeFromBigEndian(header + 2, endPtr, &tmp))
1448 if (!qSafeFromBigEndian(header + 4, endPtr, &length))
1452 if (table + unicode_table + length > endPtr)
1459 if (symbolTable > -1 && ((score == Unicode) || (score == Unicode11))) {
1460 const uchar *selectedTable = table + unicode_table;
1463 bool unicodeTableHasLatin1 =
false;
1464 for (
int uc=0x00; uc<0x100; ++uc) {
1465 if (getTrueTypeGlyphIndex(selectedTable, length, uc) != 0) {
1466 unicodeTableHasLatin1 =
true;
1472 bool unicodeTableHasSymbols =
false;
1473 if (!unicodeTableHasLatin1) {
1474 for (
int uc=0xf000; uc<0xf100; ++uc) {
1475 if (getTrueTypeGlyphIndex(selectedTable, length, uc) != 0) {
1476 unicodeTableHasSymbols =
true;
1483 if (!unicodeTableHasLatin1 && unicodeTableHasSymbols) {
1484 tableToUse = symbolTable;
1490 return table + unicode_table;
1493quint32 QFontEngine::getTrueTypeGlyphIndex(
const uchar *cmap,
int cmapSize, uint unicode)
1495 const uchar *end = cmap + cmapSize;
1497 if (!qSafeFromBigEndian(cmap, end, &format))
1501 const uchar *ptr = cmap + 6 + unicode;
1502 if (unicode < 256 && ptr < end)
1503 return quint32(*ptr);
1504 }
else if (format == 4) {
1506
1507
1508
1509
1510 if (unicode >= 0xffff)
1513 quint16 segCountX2 = 0;
1514 if (!qSafeFromBigEndian(cmap + 6, end, &segCountX2))
1517 const unsigned char *ends = cmap + 14;
1520 for (; i < segCountX2/2; ++i) {
1521 quint16 codePoint = 0;
1522 if (!qSafeFromBigEndian(ends + 2 * i, end, &codePoint))
1524 if (codePoint >= unicode)
1528 const unsigned char *idx = ends + segCountX2 + 2 + 2*i;
1530 quint16 startIndex = 0;
1531 if (!qSafeFromBigEndian(idx, end, &startIndex))
1533 if (startIndex > unicode)
1539 if (!qSafeFromBigEndian(idx, end, &tmp))
1541 qint16 idDelta = qint16(tmp);
1545 quint16 idRangeoffset_t = 0;
1546 if (!qSafeFromBigEndian(idx, end, &idRangeoffset_t))
1549 quint16 glyphIndex = 0;
1550 if (idRangeoffset_t) {
1552 if (!qSafeFromBigEndian(idRangeoffset_t + 2 * (unicode - startIndex) + idx, end, &id))
1556 glyphIndex = (idDelta + id) % 0x10000;
1560 glyphIndex = (idDelta + unicode) % 0x10000;
1563 }
else if (format == 6) {
1564 quint16 tableSize = 0;
1565 if (!qSafeFromBigEndian(cmap + 2, end, &tableSize))
1568 quint16 firstCode6 = 0;
1569 if (!qSafeFromBigEndian(cmap + 6, end, &firstCode6))
1571 if (unicode < firstCode6)
1574 quint16 entryCount6 = 0;
1575 if (!qSafeFromBigEndian(cmap + 8, end, &entryCount6))
1577 if (entryCount6 * 2 + 10 > tableSize)
1580 quint16 sentinel6 = firstCode6 + entryCount6;
1581 if (unicode >= sentinel6)
1584 quint16 entryIndex6 = unicode - firstCode6;
1587 qSafeFromBigEndian(cmap + 10 + (entryIndex6 * 2), end, &index);
1589 }
else if (format == 12) {
1590 quint32 nGroups = 0;
1591 if (!qSafeFromBigEndian(cmap + 12, end, &nGroups))
1596 int left = 0, right = nGroups - 1;
1597 while (left <= right) {
1598 int middle = left + ( ( right - left ) >> 1 );
1600 quint32 startCharCode = 0;
1601 if (!qSafeFromBigEndian(cmap + 12 * middle, end, &startCharCode))
1604 if (unicode < startCharCode)
1607 quint32 endCharCode = 0;
1608 if (!qSafeFromBigEndian(cmap + 12 * middle + 4, end, &endCharCode))
1611 if (unicode <= endCharCode) {
1613 if (!qSafeFromBigEndian(cmap + 12 * middle + 8, end, &index))
1616 return index + unicode - startCharCode;
1622 qDebug(
"cmap table of format %d not implemented", format);
1779void QFontEngineBox::addOutlineToPath(qreal x, qreal y,
const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags)
1781 if (!glyphs.numGlyphs)
1784 QVarLengthArray<QFixedPoint> positions;
1785 QVarLengthArray<glyph_t> positioned_glyphs;
1786 QTransform matrix = QTransform::fromTranslate(x, y - _size);
1787 getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
1789 QSize s(_size - 3, _size - 3);
1790 for (
int k = 0; k < positions.size(); k++)
1791 path->addRect(QRectF(positions[k].toPointF(), s));
1803void QFontEngineBox::draw(QPaintEngine *p, qreal x, qreal y,
const QTextItemInt &ti)
1805 if (!ti.glyphs.numGlyphs)
1809 QSize s(_size - 3, _size - 3);
1811 QVarLengthArray<QFixedPoint> positions;
1812 QVarLengthArray<glyph_t> glyphs;
1813 QTransform matrix = QTransform::fromTranslate(x, y - _size);
1814 ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
1815 if (glyphs.size() == 0)
1819 QPainter *painter = p->painter();
1821 painter->setBrush(Qt::NoBrush);
1822 QPen pen = painter->pen();
1823 pen.setWidthF(lineThickness().toReal());
1824 painter->setPen(pen);
1825 for (
int k = 0; k < positions.size(); k++)
1826 painter->drawRect(QRectF(positions[k].toPointF(), s));
2044int QFontEngineMulti::stringToCMap(
const QChar *str,
int len,
2045 QGlyphLayout *glyphs,
int *nglyphs,
2046 QFontEngine::ShaperFlags flags)
const
2048 qCDebug(lcTextFontCmap) <<
"Mapping characters in"
2049 << QString(str, len)
2050 <<
"; Shaper flags:" << flags;
2052 const int originalNumGlyphs = glyphs->numGlyphs;
2053 int mappedGlyphCount = engine(0)->stringToCMap(str, len, glyphs, nglyphs, flags);
2054 qCDebug(lcTextFontCmap) <<
" Main font mapped"
2057 << (nglyphs !=
nullptr ? *nglyphs : 0)
2060 if (mappedGlyphCount < 0)
2065 bool contextFontMerging = mappedGlyphCount < *nglyphs && (fontDef.styleStrategy & QFont::ContextFontMerging);
2066 if (contextFontMerging) {
2067 qCDebug(lcTextFontCmap) <<
" Context font merging, first pass";
2069 QVarLengthGlyphLayoutArray tempLayout(len);
2070 if (!m_fallbackFamiliesQueried)
2071 const_cast<QFontEngineMulti *>(
this)->ensureFallbackFamiliesQueried();
2073 int maxGlyphCount = 0;
2074 uchar engineIndex = 0;
2075 for (
int x = 1, n = qMin(m_engines.size(), 256); x < n; ++x) {
2076 int numGlyphs = len;
2077 const_cast<QFontEngineMulti *>(
this)->ensureEngineAt(x);
2078 maxGlyphCount = engine(x)->stringToCMap(str, len, &tempLayout, &numGlyphs, flags);
2079 qCDebug(lcTextFontCmap) <<
" Fallback" << x <<
"mapped" << maxGlyphCount;
2082 if (maxGlyphCount > mappedGlyphCount) {
2083 qCDebug(lcTextFontCmap) <<
" Better match!";
2084 *nglyphs = numGlyphs;
2085 glyphs->numGlyphs = originalNumGlyphs;
2086 glyphs->copy(&tempLayout);
2088 if (maxGlyphCount == numGlyphs)
2091 qCDebug(lcTextFontCmap) <<
" No improvement";
2095 if (engineIndex > 0) {
2096 for (
int y = 0; y < glyphs->numGlyphs; ++y) {
2097 if (glyphs->glyphs[y] != 0)
2098 glyphs->glyphs[y] |= (engineIndex << 24);
2101 contextFontMerging =
false;
2104 mappedGlyphCount = maxGlyphCount;
2110 QStringIterator it(str, str + len);
2112 const bool enableVariationSelectorHack = disableEmojiSegmenter();
2113 char32_t previousUcs4 = 0;
2115 int lastFallback = -1;
2116 while (it.hasNext()) {
2117 const char32_t ucs4 = it.peekNext();
2122 if (lastFallback >= 0 && (ucs4 == 0x200d || ucs4 == 0x200c)) {
2123 QFontEngine *engine = m_engines.at(lastFallback);
2124 glyph_t glyph = engine->glyphIndex(ucs4);
2126 qCDebug(lcTextFontCmap) <<
" Applying ZWJ/ZWNJ special case for"
2127 << QString::number(ucs4, 16)
2128 <<
", lastFallback:" << lastFallback;
2130 glyphs->glyphs[glyph_pos] = glyph;
2131 if (!(flags & GlyphIndicesOnly)) {
2132 QGlyphLayout g = glyphs->mid(glyph_pos, 1);
2133 engine->recalcAdvances(&g, flags);
2137 glyphs->glyphs[glyph_pos] |= (lastFallback << 24);
2145 if (glyphs->glyphs[glyph_pos] == 0 && !isIgnorableChar(ucs4)) {
2146 qCDebug(lcTextFontCmap) <<
" Unmapped character"
2147 << QString::number(ucs4, 16);
2149 if (!m_fallbackFamiliesQueried)
2150 const_cast<QFontEngineMulti *>(
this)->ensureFallbackFamiliesQueried();
2151 for (
int x = contextFontMerging ? 0 : 1, n = qMin(m_engines.size(), 256); x < n; ++x) {
2152 QFontEngine *engine = m_engines.at(x);
2154 if (!shouldLoadFontEngineForCharacter(x, ucs4)) {
2155 qCDebug(lcTextFontCmap) <<
" Skipping fallback #" << x;
2158 const_cast<QFontEngineMulti *>(
this)->ensureEngineAt(x);
2159 engine = m_engines.at(x);
2163 Q_ASSERT(engine !=
nullptr);
2164 if (engine->type() == Box)
2167 glyph_t glyph = engine->glyphIndex(ucs4);
2168 qCDebug(lcTextFontCmap) <<
" Fallback #" << x
2169 <<
", family:" << engine->fontDef.families
2170 <<
", styleName:" << engine->fontDef.styleName
2171 <<
", style:" << engine->fontDef.style
2172 <<
", weight:" << engine->fontDef.weight
2173 <<
", glyph mapping:" << glyph;
2175 glyphs->glyphs[glyph_pos] = glyph;
2176 if (!(flags & GlyphIndicesOnly)) {
2177 QGlyphLayout g = glyphs->mid(glyph_pos, 1);
2178 engine->recalcAdvances(&g, flags);
2184 glyphs->glyphs[glyph_pos] |= (x << 24);
2192 const int variantSelectorBlock = 0xFE00;
2193 if (enableVariationSelectorHack && (ucs4 & 0xFFF0) == variantSelectorBlock && glyph_pos > 0) {
2194 int selectorFontEngine = glyphs->glyphs[glyph_pos] >> 24;
2195 int precedingCharacterFontEngine = glyphs->glyphs[glyph_pos - 1] >> 24;
2197 if (selectorFontEngine != precedingCharacterFontEngine) {
2203 const QFontEngine *selectedEngine = m_engines.at(precedingCharacterFontEngine);
2204 const bool colorFont = selectedEngine->isColorFont();
2205 const char32_t vs15 = 0xFE0E;
2206 const char32_t vs16 = 0xFE0F;
2207 bool adaptVariantSelector = ucs4 < vs15
2208 || (ucs4 == vs15 && colorFont)
2209 || (ucs4 == vs16 && !colorFont);
2211 if (adaptVariantSelector) {
2212 QFontEngine *engine = m_engines.at(selectorFontEngine);
2213 glyph_t glyph = engine->glyphIndex(previousUcs4);
2215 glyphs->glyphs[glyph_pos - 1] = glyph;
2216 if (!(flags & GlyphIndicesOnly)) {
2217 QGlyphLayout g = glyphs->mid(glyph_pos - 1, 1);
2218 engine->recalcAdvances(&g, flags);
2222 glyphs->glyphs[glyph_pos - 1] |= (selectorFontEngine << 24);
2232 previousUcs4 = ucs4;
2235 *nglyphs = glyph_pos;
2236 glyphs->numGlyphs = glyph_pos;
2237 return mappedGlyphCount;
2247glyph_metrics_t QFontEngineMulti::boundingBox(
const QGlyphLayout &glyphs)
2249 if (glyphs.numGlyphs <= 0)
2250 return glyph_metrics_t();
2252 glyph_metrics_t overall;
2254 int which = highByte(glyphs.glyphs[0]);
2257 for (end = 0; end < glyphs.numGlyphs; ++end) {
2258 const int e = highByte(glyphs.glyphs[end]);
2263 for (i = start; i < end; ++i)
2264 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2267 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
2269 overall.x = qMin(overall.x, gm.x);
2270 overall.y = qMin(overall.y, gm.y);
2271 overall.width = overall.xoff + gm.width;
2272 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
2273 qMin(overall.y, gm.y);
2274 overall.xoff += gm.xoff;
2275 overall.yoff += gm.yoff;
2278 const int hi = which << 24;
2279 for (i = start; i < end; ++i)
2280 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2288 for (i = start; i < end; ++i)
2289 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2292 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
2294 overall.x = qMin(overall.x, gm.x);
2295 overall.y = qMin(overall.y, gm.y);
2296 overall.width = overall.xoff + gm.width;
2297 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
2298 qMin(overall.y, gm.y);
2299 overall.xoff += gm.xoff;
2300 overall.yoff += gm.yoff;
2303 const int hi = which << 24;
2304 for (i = start; i < end; ++i)
2305 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2317void QFontEngineMulti::addOutlineToPath(qreal x, qreal y,
const QGlyphLayout &glyphs,
2318 QPainterPath *path, QTextItem::RenderFlags flags)
2320 if (glyphs.numGlyphs <= 0)
2323 int which = highByte(glyphs.glyphs[0]);
2326 if (flags & QTextItem::RightToLeft) {
2327 for (
int gl = 0; gl < glyphs.numGlyphs; gl++)
2328 x += glyphs.advances[gl].toReal();
2330 for (end = 0; end < glyphs.numGlyphs; ++end) {
2331 const int e = highByte(glyphs.glyphs[end]);
2335 if (flags & QTextItem::RightToLeft) {
2336 for (i = start; i < end; ++i)
2337 x -= glyphs.advances[i].toReal();
2341 for (i = start; i < end; ++i)
2342 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2343 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
2345 const int hi = which << 24;
2346 for (i = start; i < end; ++i)
2347 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2349 if (!(flags & QTextItem::RightToLeft)) {
2350 for (i = start; i < end; ++i)
2351 x += glyphs.advances[i].toReal();
2359 if (flags & QTextItem::RightToLeft) {
2360 for (i = start; i < end; ++i)
2361 x -= glyphs.advances[i].toReal();
2365 for (i = start; i < end; ++i)
2366 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2368 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
2371 const int hi = which << 24;
2372 for (i = start; i < end; ++i)
2373 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2376void QFontEngineMulti::recalcAdvances(QGlyphLayout *glyphs, QFontEngine::ShaperFlags flags)
const
2378 if (glyphs->numGlyphs <= 0)
2381 int which = highByte(glyphs->glyphs[0]);
2384 for (end = 0; end < glyphs->numGlyphs; ++end) {
2385 const int e = highByte(glyphs->glyphs[end]);
2390 for (i = start; i < end; ++i)
2391 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2393 QGlyphLayout offs = glyphs->mid(start, end - start);
2394 engine(which)->recalcAdvances(&offs, flags);
2397 const int hi = which << 24;
2398 for (i = start; i < end; ++i)
2399 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2407 for (i = start; i < end; ++i)
2408 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2410 QGlyphLayout offs = glyphs->mid(start, end - start);
2411 engine(which)->recalcAdvances(&offs, flags);
2414 const int hi = which << 24;
2415 for (i = start; i < end; ++i)
2416 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2419void QFontEngineMulti::doKerning(QGlyphLayout *glyphs, QFontEngine::ShaperFlags flags)
const
2421 if (glyphs->numGlyphs <= 0)
2424 int which = highByte(glyphs->glyphs[0]);
2427 for (end = 0; end < glyphs->numGlyphs; ++end) {
2428 const int e = highByte(glyphs->glyphs[end]);
2433 for (i = start; i < end; ++i)
2434 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2436 QGlyphLayout offs = glyphs->mid(start, end - start);
2437 engine(which)->doKerning(&offs, flags);
2440 const int hi = which << 24;
2441 for (i = start; i < end; ++i)
2442 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2450 for (i = start; i < end; ++i)
2451 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2453 QGlyphLayout offs = glyphs->mid(start, end - start);
2454 engine(which)->doKerning(&offs, flags);
2457 const int hi = which << 24;
2458 for (i = start; i < end; ++i)
2459 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2593QFontEngine *QFontEngineMulti::createMultiFontEngine(QFontEngine *fe,
int script)
2595 QFontEngine *engine =
nullptr;
2596 QFontCache::Key key(fe->fontDef, script,
true);
2597 QFontCache *fc = QFontCache::instance();
2603 const bool faceIsLocal = !fe->faceId().filename.isEmpty();
2604 QFontCache::EngineCache::Iterator it = fc->engineCache.find(key),
2605 end = fc->engineCache.end();
2606 while (it != end && it.key() == key) {
2607 Q_ASSERT(it.value().data->type() == QFontEngine::Multi);
2608 QFontEngineMulti *cachedEngine =
static_cast<QFontEngineMulti *>(it.value().data);
2609 if (fe == cachedEngine->engine(0) || (faceIsLocal && fe->faceId().filename == cachedEngine->engine(0)->faceId().filename)) {
2610 engine = cachedEngine;
2611 fc->updateHitCountAndTimeStamp(it.value());
2617 engine = QGuiApplicationPrivate::instance()->platformIntegration()->fontDatabase()->fontEngineMulti(fe, QFontDatabasePrivate::ExtendedScript(script));
2618 fc->insertEngine(key, engine, !faceIsLocal);