177bool QFontEngine::supportsScript(QChar::Script script)
const
179 if (type() <= QFontEngine::Multi)
185 if (!scriptRequiresOpenType(script))
188#if QT_CONFIG(harfbuzz)
190 uint lenMort = 0, lenMorx = 0;
191 if (getSfntTableData(QFont::Tag(
"mort").value(),
nullptr, &lenMort)
192 || getSfntTableData(QFont::Tag(
"morx").value(),
nullptr, &lenMorx)) {
196 if (hb_face_t *face = hb_qt_face_get_for_engine(
const_cast<QFontEngine *>(
this))) {
197 unsigned int script_count = HB_OT_MAX_TAGS_PER_SCRIPT;
198 hb_tag_t script_tags[HB_OT_MAX_TAGS_PER_SCRIPT];
200 hb_ot_tags_from_script_and_language(hb_qt_script_to_script(script), HB_LANGUAGE_INVALID,
201 &script_count, script_tags,
204 if (hb_ot_layout_table_select_script(face, HB_OT_TAG_GSUB, script_count, script_tags,
nullptr,
nullptr))
263void QFontEngine::getGlyphPositions(
const QGlyphLayout &glyphs,
const QTransform &matrix, QTextItem::RenderFlags flags,
264 QVarLengthArray<glyph_t> &glyphs_out, QVarLengthArray<QFixedPoint> &positions)
269 const bool transform = matrix.m11() != 1.
270 || matrix.m12() != 0.
271 || matrix.m21() != 0.
272 || matrix.m22() != 1.;
274 xpos = QFixed::fromReal(matrix.dx());
275 ypos = QFixed::fromReal(matrix.dy());
279 if (flags & QTextItem::RightToLeft) {
280 int i = glyphs.numGlyphs;
281 int totalKashidas = 0;
283 if (glyphs.attributes[i].dontPrint)
285 xpos += glyphs.advances[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
286 totalKashidas += glyphs.justifications[i].nKashidas;
288 positions.resize(glyphs.numGlyphs+totalKashidas);
289 glyphs_out.resize(glyphs.numGlyphs+totalKashidas);
292 while(i < glyphs.numGlyphs) {
293 if (glyphs.attributes[i].dontPrint) {
297 xpos -= glyphs.advances[i];
299 QFixed gpos_x = xpos + glyphs.offsets[i].x;
300 QFixed gpos_y = ypos + glyphs.offsets[i].y;
302 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
303 gpos = gpos * matrix;
304 gpos_x = QFixed::fromReal(gpos.x());
305 gpos_y = QFixed::fromReal(gpos.y());
307 positions[current].x = gpos_x;
308 positions[current].y = gpos_y;
309 glyphs_out[current] = glyphs.glyphs[i];
311 if (glyphs.justifications[i].nKashidas) {
314 glyph_t kashidaGlyph = glyphIndex(ch.unicode());
319 g.glyphs = &kashidaGlyph;
320 g.advances = &kashidaWidth;
321 recalcAdvances(&g, { });
323 for (uint k = 0; k < glyphs.justifications[i].nKashidas; ++k) {
324 xpos -= kashidaWidth;
326 QFixed gpos_x = xpos + glyphs.offsets[i].x;
327 QFixed gpos_y = ypos + glyphs.offsets[i].y;
329 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
330 gpos = gpos * matrix;
331 gpos_x = QFixed::fromReal(gpos.x());
332 gpos_y = QFixed::fromReal(gpos.y());
334 positions[current].x = gpos_x;
335 positions[current].y = gpos_y;
336 glyphs_out[current] = kashidaGlyph;
340 xpos -= QFixed::fromFixed(glyphs.justifications[i].space_18d6);
345 positions.resize(glyphs.numGlyphs);
346 glyphs_out.resize(glyphs.numGlyphs);
349 while (i < glyphs.numGlyphs) {
350 if (!glyphs.attributes[i].dontPrint) {
351 positions[current].x = xpos + glyphs.offsets[i].x;
352 positions[current].y = ypos + glyphs.offsets[i].y;
353 glyphs_out[current] = glyphs.glyphs[i];
354 xpos += glyphs.advances[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
360 while (i < glyphs.numGlyphs) {
361 if (!glyphs.attributes[i].dontPrint) {
362 QFixed gpos_x = xpos + glyphs.offsets[i].x;
363 QFixed gpos_y = ypos + glyphs.offsets[i].y;
364 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
365 gpos = gpos * matrix;
366 positions[current].x = QFixed::fromReal(gpos.x());
367 positions[current].y = QFixed::fromReal(gpos.y());
368 glyphs_out[current] = glyphs.glyphs[i];
369 xpos += glyphs.advances[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
376 positions.resize(current);
377 glyphs_out.resize(current);
378 Q_ASSERT(positions.size() == glyphs_out.size());
390bool QFontEngine::processHheaTable()
const
392 QByteArray hhea = getSfntTable(QFont::Tag(
"hhea").value());
393 if (hhea.size() >= 10) {
394 auto ptr = hhea.constData();
395 qint16 ascent = qFromBigEndian<qint16>(ptr + 4);
396 qint16 descent = qFromBigEndian<qint16>(ptr + 6);
397 qint16 leading = qFromBigEndian<qint16>(ptr + 8);
400 if (ascent == 0 && descent == 0)
403 const qreal unitsPerEm = emSquareSize().toReal();
405 const auto limitForQFixed = std::numeric_limits<
int>::max() / (fontDef.pixelSize * 64);
406 if (ascent > limitForQFixed || descent > limitForQFixed || leading > limitForQFixed)
408 m_ascent = QFixed::fromReal(ascent * fontDef.pixelSize / unitsPerEm);
409 m_descent = -QFixed::fromReal(descent * fontDef.pixelSize / unitsPerEm);
410 m_leading = QFixed::fromReal(leading * fontDef.pixelSize / unitsPerEm);
418void QFontEngine::initializeHeightMetrics()
const
420 bool hasEmbeddedBitmaps =
421 !getSfntTable(QFont::Tag(
"EBLC").value()).isEmpty()
422 || !getSfntTable(QFont::Tag(
"CBLC").value()).isEmpty()
423 || !getSfntTable(QFont::Tag(
"bdat").value()).isEmpty();
430 static bool useSystemLineMetrics = qEnvironmentVariableIntValue(
"QT_USE_SYSTEM_LINE_METRICS") > 0;
431 if (!hasEmbeddedBitmaps && !useSystemLineMetrics) {
438 if (!supportsSubPixelPositions()) {
439 const QFixed actualHeight = m_ascent + m_descent + m_leading;
440 m_ascent = m_ascent.round();
441 m_descent = m_descent.round();
442 m_leading = actualHeight.round() - m_ascent - m_descent;
446 m_heightMetricsQueried =
true;
454bool QFontEngine::processOS2Table()
const
456 QByteArray os2 = getSfntTable(QFont::Tag(
"OS/2").value());
457 if (os2.size() >= 78) {
458 auto ptr = os2.constData();
459 quint16 fsSelection = qFromBigEndian<quint16>(ptr + 62);
460 qint16 typoAscent = qFromBigEndian<qint16>(ptr + 68);
461 qint16 typoDescent = qFromBigEndian<qint16>(ptr + 70);
462 qint16 typoLineGap = qFromBigEndian<qint16>(ptr + 72);
463 quint16 winAscent = qFromBigEndian<quint16>(ptr + 74);
464 quint16 winDescent = qFromBigEndian<quint16>(ptr + 76);
466 enum { USE_TYPO_METRICS = 0x80 };
467 const qreal unitsPerEm = emSquareSize().toReal();
468 if (preferTypoLineMetrics() || fsSelection & USE_TYPO_METRICS) {
470 if (typoAscent == 0 && typoDescent == 0)
473 const auto limitForQFixed = std::numeric_limits<
int>::max() / (fontDef.pixelSize * 64);
474 if (typoAscent > limitForQFixed || typoDescent > limitForQFixed
475 || typoLineGap > limitForQFixed)
477 m_ascent = QFixed::fromReal(typoAscent * fontDef.pixelSize / unitsPerEm);
478 m_descent = -QFixed::fromReal(typoDescent * fontDef.pixelSize / unitsPerEm);
479 m_leading = QFixed::fromReal(typoLineGap * fontDef.pixelSize / unitsPerEm);
482 if (winAscent == 0 && winDescent == 0)
484 const auto limitForQFixed = std::numeric_limits<
int>::max() / (fontDef.pixelSize * 64);
485 if (winAscent > limitForQFixed || winDescent > limitForQFixed)
487 m_ascent = QFixed::fromReal(winAscent * fontDef.pixelSize / unitsPerEm);
488 m_descent = QFixed::fromReal(winDescent * fontDef.pixelSize / unitsPerEm);
489 m_leading = QFixed{};
542qreal QFontEngine::minRightBearing()
const
547 QByteArray hheaTable = getSfntTable(QFont::Tag(
"hhea").value());
549 const uchar *tableData =
reinterpret_cast<
const uchar *>(hheaTable.constData());
558 int unitsPerEm = emSquareSize().toInt();
559 qreal funitToPixelFactor = fontDef.pixelSize / unitsPerEm;
565 static const int largestValidBearing = 4 * unitsPerEm;
567 if (qAbs(minLeftSideBearing) < largestValidBearing)
568 m_minLeftBearing = minLeftSideBearing * funitToPixelFactor;
569 if (qAbs(minRightSideBearing) < largestValidBearing)
570 m_minRightBearing = minRightSideBearing * funitToPixelFactor;
579 static const ushort characterSubset[] = {
580 '(',
'C',
'F',
'K',
'V',
'X',
'Y',
']',
'_',
'f',
'r',
'|',
581 127, 205, 645, 884, 922, 1070, 12386
585 m_minLeftBearing = m_minRightBearing = std::numeric_limits<qreal>::max();
587 for (uint i = 0; i < (
sizeof(characterSubset) /
sizeof(ushort)); ++i) {
588 const glyph_t glyph = glyphIndex(characterSubset[i]);
592 glyph_metrics_t glyphMetrics =
const_cast<QFontEngine *>(
this)->boundingBox(glyph);
595 if (!glyphMetrics.width || !glyphMetrics.height)
598 m_minLeftBearing = qMin(m_minLeftBearing, glyphMetrics.leftBearing().toReal());
599 m_minRightBearing = qMin(m_minRightBearing, glyphMetrics.rightBearing().toReal());
604 qWarning() <<
"Failed to compute left/right minimum bearings for"
605 << fontDef.families.first();
608 return m_minRightBearing;
621glyph_metrics_t QFontEngine::tightBoundingBox(
const QGlyphLayout &glyphs)
623 glyph_metrics_t overall;
627 for (
int i = 0; i < glyphs.numGlyphs; i++) {
629 if (!glyphs.advances[i] || glyphs.attributes[i].dontPrint)
631 glyph_metrics_t bb = boundingBox(glyphs.glyphs[i]);
632 QFixed x = overall.xoff + glyphs.offsets[i].x + bb.x;
633 QFixed y = overall.yoff + glyphs.offsets[i].y + bb.y;
634 overall.x = qMin(overall.x, x);
635 overall.y = qMin(overall.y, y);
636 xmax = qMax(xmax, x.ceil() + bb.width);
637 ymax = qMax(ymax, y.ceil() + bb.height);
638 overall.xoff += glyphs.effectiveAdvance(i);
639 overall.yoff += bb.yoff;
641 overall.height = qMax(overall.height, ymax - overall.y);
642 overall.width = xmax - overall.x;
648void QFontEngine::addOutlineToPath(qreal x, qreal y,
const QGlyphLayout &glyphs, QPainterPath *path,
649 QTextItem::RenderFlags flags)
651 if (!glyphs.numGlyphs)
654 QVarLengthArray<QFixedPoint> positions;
655 QVarLengthArray<glyph_t> positioned_glyphs;
656 QTransform matrix = QTransform::fromTranslate(x, y);
657 getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
658 addGlyphsToPath(positioned_glyphs.data(), positions.data(), positioned_glyphs.size(), path, flags);
755void QFontEngine::addBitmapFontToPath(qreal x, qreal y,
const QGlyphLayout &glyphs,
756 QPainterPath *path, QTextItem::RenderFlags flags)
760 QFixed advanceX = QFixed::fromReal(x);
761 QFixed advanceY = QFixed::fromReal(y);
762 for (
int i=0; i < glyphs.numGlyphs; ++i) {
763 glyph_metrics_t metrics = boundingBox(glyphs.glyphs[i]);
764 if (metrics.width.value() == 0 || metrics.height.value() == 0) {
765 advanceX += glyphs.advances[i];
768 const QImage alphaMask = alphaMapForGlyph(glyphs.glyphs[i]);
770 const int w = alphaMask.width();
771 const int h = alphaMask.height();
772 const qsizetype srcBpl = alphaMask.bytesPerLine();
774 if (alphaMask.depth() == 1) {
777 bitmap = QImage(w, h, QImage::Format_Mono);
778 const uchar *imageData = alphaMask.bits();
779 const qsizetype destBpl = bitmap.bytesPerLine();
780 uchar *bitmapData = bitmap.bits();
782 for (
int yi = 0; yi < h; ++yi) {
783 const uchar *src = imageData + yi*srcBpl;
784 uchar *dst = bitmapData + yi*destBpl;
785 for (
int xi = 0; xi < w; ++xi) {
786 const int byte = xi / 8;
787 const int bit = xi % 8;
791 dst[byte] |= 128 >> bit;
795 const uchar *bitmap_data = bitmap.constBits();
796 QFixedPoint offset = glyphs.offsets[i];
797 advanceX += offset.x;
798 advanceY += offset.y;
799 qt_addBitmapToPath((advanceX + metrics.x).toReal(), (advanceY + metrics.y).toReal(), bitmap_data, bitmap.bytesPerLine(), w, h, path);
800 advanceX += glyphs.advances[i];
804void QFontEngine::addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions,
int nGlyphs,
805 QPainterPath *path, QTextItem::RenderFlags flags)
807 qreal x = positions[0].x.toReal();
808 qreal y = positions[0].y.toReal();
809 QVarLengthGlyphLayoutArray g(nGlyphs);
811 for (
int i = 0; i < nGlyphs - 1; ++i) {
812 g.glyphs[i] = glyphs[i];
813 g.advances[i] = positions[i + 1].x - positions[i].x;
815 g.glyphs[nGlyphs - 1] = glyphs[nGlyphs - 1];
816 g.advances[nGlyphs - 1] = QFixed::fromReal(maxCharWidth());
818 addBitmapFontToPath(x, y, g, path, flags);
850QImage QFontEngine::alphaRGBMapForGlyph(glyph_t glyph,
const QFixedPoint &,
const QTransform &t)
852 const QImage alphaMask = alphaMapForGlyph(glyph, t);
853 QImage rgbMask(alphaMask.width(), alphaMask.height(), QImage::Format_RGB32);
855 for (
int y=0; y<alphaMask.height(); ++y) {
856 uint *dst = (uint *) rgbMask.scanLine(y);
857 const uchar *src = alphaMask.constScanLine(y);
858 for (
int x=0; x<alphaMask.width(); ++x) {
860 dst[x] = qRgb(val, val, val);
945glyph_t QFontEngine::findGlyph(QLatin1StringView name)
const
949#if QT_CONFIG(harfbuzz)
950 result = queryHarfbuzz(
this, [name](hb_font_t *hbFont){
952 hb_codepoint_t glyph;
953 if (hb_font_get_glyph_from_name(hbFont, name.constData(), name.size(), &glyph))
954 return glyph_t(glyph);
959 for (glyph_t index = 0; index < uint(glyphCount()); ++index) {
960 if (name == glyphName(index))
967 constexpr auto gid =
"gid"_L1;
968 constexpr auto uni =
"uni"_L1;
969 if (name.startsWith(gid)) {
971 result = name.slice(gid.size()).toUInt(&ok);
972 if (ok && result < glyph_t(glyphCount()))
974 }
else if (name.startsWith(uni)) {
976 const uint ucs4 = name.slice(uni.size()).toUInt(&ok, 16);
978 result = glyphIndex(ucs4);
979 if (result > 0 && result < glyph_t(glyphCount()))
988QImage QFontEngine::renderedPathForGlyph(glyph_t glyph,
const QColor &color)
990 glyph_metrics_t gm = boundingBox(glyph);
991 int glyph_x = qFloor(gm.x.toReal());
992 int glyph_y = qFloor(gm.y.toReal());
993 int glyph_width = qCeil((gm.x + gm.width).toReal()) - glyph_x;
994 int glyph_height = qCeil((gm.y + gm.height).toReal()) - glyph_y;
996 if (glyph_width <= 0 || glyph_height <= 0)
1002 path.setFillRule(Qt::WindingFill);
1003 QImage im(glyph_width, glyph_height, QImage::Format_ARGB32_Premultiplied);
1004 im.fill(Qt::transparent);
1006 p.setRenderHint(QPainter::Antialiasing);
1007 addGlyphsToPath(&glyph, &pt, 1, &path, { });
1008 p.setPen(Qt::NoPen);
1158void QFontEngine::doKerning(QGlyphLayout *glyphs, QFontEngine::ShaperFlags flags)
const
1160 int numPairs = kerning_pairs.size();
1164 const KernPair *pairs = kerning_pairs.constData();
1166 if (flags & DesignMetrics) {
1167 for(
int i = 0; i < glyphs->numGlyphs - 1; ++i)
1168 glyphs->advances[i] += kerning(glyphs->glyphs[i], glyphs->glyphs[i+1] , pairs, numPairs);
1170 for(
int i = 0; i < glyphs->numGlyphs - 1; ++i)
1171 glyphs->advances[i] += qRound(kerning(glyphs->glyphs[i], glyphs->glyphs[i+1] , pairs, numPairs));
1175void QFontEngine::loadKerningPairs(QFixed scalingFactor)
1177 kerning_pairs.clear();
1179 QByteArray tab = getSfntTable(QFont::Tag(
"kern").value());
1183 const uchar *table =
reinterpret_cast<
const uchar *>(tab.constData());
1184 const uchar *end = table + tab.size();
1187 if (!qSafeFromBigEndian(table, end, &version))
1196 if (!qSafeFromBigEndian(table + 2, end, &numTables))
1201 for(
int i = 0; i < numTables; ++i) {
1202 const uchar *header = table + offset;
1205 if (!qSafeFromBigEndian(header, end, &version))
1209 if (!qSafeFromBigEndian(header + 2, end, &length))
1213 if (!qSafeFromBigEndian(header + 4, end, &coverage))
1217 if (version == 0 && coverage == 0x0001) {
1218 if (offset + length > tab.size()) {
1222 const uchar *data = table + offset + 6;
1225 if (!qSafeFromBigEndian(data, end, &nPairs))
1228 if (nPairs * 6 + 8 > length - 6) {
1235 for(
int i = 0; i < nPairs; ++i) {
1236 QFontEngine::KernPair p;
1239 if (!qSafeFromBigEndian(data + off, end, &tmp))
1242 p.left_right = uint(tmp) << 16;
1243 if (!qSafeFromBigEndian(data + off + 2, end, &tmp))
1246 p.left_right |= tmp;
1248 if (!qSafeFromBigEndian(data + off + 4, end, &tmp))
1251 p.adjust = QFixed(
int(
short(tmp))) / scalingFactor;
1252 kerning_pairs.append(p);
1260 std::sort(kerning_pairs.begin(), kerning_pairs.end());
1285const uchar *QFontEngine::getCMap(
const uchar *table, uint tableSize,
bool *isSymbolFont,
int *cmapSize)
1287 const uchar *header = table;
1288 const uchar *endPtr = table + tableSize;
1292 if (!qSafeFromBigEndian(header, endPtr, &version) || version != 0)
1296 if (!qSafeFromBigEndian(header + 2, endPtr, &numTables))
1299 const uchar *maps = table + 4;
1308 MicrosoftUnicodeExtended
1311 int symbolTable = -1;
1312 int tableToUse = -1;
1313 int score = Invalid;
1314 for (
int n = 0; n < numTables; ++n) {
1315 quint16 platformId = 0;
1316 if (!qSafeFromBigEndian(maps + 8 * n, endPtr, &platformId))
1319 quint16 platformSpecificId = 0;
1320 if (!qSafeFromBigEndian(maps + 8 * n + 2, endPtr, &platformSpecificId))
1323 switch (platformId) {
1325 if (score < Unicode &&
1326 (platformSpecificId == 0 ||
1327 platformSpecificId == 2 ||
1328 platformSpecificId == 3)) {
1331 }
else if (score < Unicode11 && platformSpecificId == 1) {
1337 if (score < AppleRoman && platformSpecificId == 0) {
1343 switch (platformSpecificId) {
1346 if (score < Symbol) {
1352 if (score < MicrosoftUnicode) {
1354 score = MicrosoftUnicode;
1358 if (score < MicrosoftUnicodeExtended) {
1360 score = MicrosoftUnicodeExtended;
1375 *isSymbolFont = (symbolTable > -1);
1377 quint32 unicode_table = 0;
1378 if (!qSafeFromBigEndian(maps + 8 * tableToUse + 4, endPtr, &unicode_table))
1385 header = table + unicode_table;
1388 if (!qSafeFromBigEndian(header, endPtr, &format))
1394 if (!qSafeFromBigEndian(header + 2, endPtr, &tmp))
1398 if (!qSafeFromBigEndian(header + 4, endPtr, &length))
1402 if (table + unicode_table + length > endPtr)
1409 if (symbolTable > -1 && ((score == Unicode) || (score == Unicode11))) {
1410 const uchar *selectedTable = table + unicode_table;
1413 bool unicodeTableHasLatin1 =
false;
1414 for (
int uc=0x00; uc<0x100; ++uc) {
1415 if (getTrueTypeGlyphIndex(selectedTable, length, uc) != 0) {
1416 unicodeTableHasLatin1 =
true;
1422 bool unicodeTableHasSymbols =
false;
1423 if (!unicodeTableHasLatin1) {
1424 for (
int uc=0xf000; uc<0xf100; ++uc) {
1425 if (getTrueTypeGlyphIndex(selectedTable, length, uc) != 0) {
1426 unicodeTableHasSymbols =
true;
1433 if (!unicodeTableHasLatin1 && unicodeTableHasSymbols) {
1434 tableToUse = symbolTable;
1440 return table + unicode_table;
1443quint32 QFontEngine::getTrueTypeGlyphIndex(
const uchar *cmap,
int cmapSize, uint unicode)
1445 const uchar *end = cmap + cmapSize;
1447 if (!qSafeFromBigEndian(cmap, end, &format))
1451 const uchar *ptr = cmap + 6 + unicode;
1452 if (unicode < 256 && ptr < end)
1453 return quint32(*ptr);
1454 }
else if (format == 4) {
1456
1457
1458
1459
1460 if (unicode >= 0xffff)
1463 quint16 segCountX2 = 0;
1464 if (!qSafeFromBigEndian(cmap + 6, end, &segCountX2))
1467 const unsigned char *ends = cmap + 14;
1470 for (; i < segCountX2/2; ++i) {
1471 quint16 codePoint = 0;
1472 if (!qSafeFromBigEndian(ends + 2 * i, end, &codePoint))
1474 if (codePoint >= unicode)
1478 const unsigned char *idx = ends + segCountX2 + 2 + 2*i;
1480 quint16 startIndex = 0;
1481 if (!qSafeFromBigEndian(idx, end, &startIndex))
1483 if (startIndex > unicode)
1489 if (!qSafeFromBigEndian(idx, end, &tmp))
1491 qint16 idDelta = qint16(tmp);
1495 quint16 idRangeoffset_t = 0;
1496 if (!qSafeFromBigEndian(idx, end, &idRangeoffset_t))
1499 quint16 glyphIndex = 0;
1500 if (idRangeoffset_t) {
1502 if (!qSafeFromBigEndian(idRangeoffset_t + 2 * (unicode - startIndex) + idx, end, &id))
1506 glyphIndex = (idDelta + id) % 0x10000;
1510 glyphIndex = (idDelta + unicode) % 0x10000;
1513 }
else if (format == 6) {
1514 quint16 tableSize = 0;
1515 if (!qSafeFromBigEndian(cmap + 2, end, &tableSize))
1518 quint16 firstCode6 = 0;
1519 if (!qSafeFromBigEndian(cmap + 6, end, &firstCode6))
1521 if (unicode < firstCode6)
1524 quint16 entryCount6 = 0;
1525 if (!qSafeFromBigEndian(cmap + 8, end, &entryCount6))
1527 if (entryCount6 * 2 + 10 > tableSize)
1530 quint16 sentinel6 = firstCode6 + entryCount6;
1531 if (unicode >= sentinel6)
1534 quint16 entryIndex6 = unicode - firstCode6;
1537 qSafeFromBigEndian(cmap + 10 + (entryIndex6 * 2), end, &index);
1539 }
else if (format == 12) {
1540 quint32 nGroups = 0;
1541 if (!qSafeFromBigEndian(cmap + 12, end, &nGroups))
1546 int left = 0, right = nGroups - 1;
1547 while (left <= right) {
1548 int middle = left + ( ( right - left ) >> 1 );
1550 quint32 startCharCode = 0;
1551 if (!qSafeFromBigEndian(cmap + 12 * middle, end, &startCharCode))
1554 if (unicode < startCharCode)
1557 quint32 endCharCode = 0;
1558 if (!qSafeFromBigEndian(cmap + 12 * middle + 4, end, &endCharCode))
1561 if (unicode <= endCharCode) {
1563 if (!qSafeFromBigEndian(cmap + 12 * middle + 8, end, &index))
1566 return index + unicode - startCharCode;
1572 qDebug(
"cmap table of format %d not implemented", format);
1688int QFontEngineBox::stringToCMap(
const QChar *str,
int len, QGlyphLayout *glyphs,
int *nglyphs, QFontEngine::ShaperFlags flags)
const
1690 Q_ASSERT(glyphs->numGlyphs >= *nglyphs);
1691 if (*nglyphs < len) {
1697 QStringIterator it(str, str + len);
1698 while (it.hasNext()) {
1700 glyphs->glyphs[ucs4Length++] = 1;
1703 *nglyphs = ucs4Length;
1704 glyphs->numGlyphs = ucs4Length;
1706 if (!(flags & GlyphIndicesOnly))
1707 recalcAdvances(glyphs, flags);
1718void QFontEngineBox::addOutlineToPath(qreal x, qreal y,
const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags)
1720 if (!glyphs.numGlyphs)
1723 QVarLengthArray<QFixedPoint> positions;
1724 QVarLengthArray<glyph_t> positioned_glyphs;
1725 QTransform matrix = QTransform::fromTranslate(x, y - _size);
1726 getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
1728 QSize s(_size - 3, _size - 3);
1729 for (
int k = 0; k < positions.size(); k++)
1730 path->addRect(QRectF(positions[k].toPointF(), s));
1742void QFontEngineBox::draw(QPaintEngine *p, qreal x, qreal y,
const QTextItemInt &ti)
1744 if (!ti.glyphs.numGlyphs)
1748 QSize s(_size - 3, _size - 3);
1750 QVarLengthArray<QFixedPoint> positions;
1751 QVarLengthArray<glyph_t> glyphs;
1752 QTransform matrix = QTransform::fromTranslate(x, y - _size);
1753 ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
1754 if (glyphs.size() == 0)
1758 QPainter *painter = p->painter();
1760 painter->setBrush(Qt::NoBrush);
1761 QPen pen = painter->pen();
1762 pen.setWidthF(lineThickness().toReal());
1763 painter->setPen(pen);
1764 for (
int k = 0; k < positions.size(); k++)
1765 painter->drawRect(QRectF(positions[k].toPointF(), s));
1837QFontEngineMulti::QFontEngineMulti(QFontEngine *engine,
int script,
const QStringList &fallbackFamilies)
1838 : QFontEngine(Multi),
1839 m_fallbackFamilies(fallbackFamilies),
1841 m_fallbackFamiliesQueried(!m_fallbackFamilies.isEmpty())
1843 Q_ASSERT(engine && engine->type() != QFontEngine::Multi);
1845 if (m_fallbackFamilies.isEmpty()) {
1847 m_fallbackFamilies << QString();
1850 m_engines.resize(m_fallbackFamilies.size() + 1);
1853 m_engines[0] = engine;
1855 fontDef = engine->fontDef;
1856 cache_cost = engine->cache_cost;
1941glyph_t QFontEngineMulti::glyphIndex(uint ucs4)
const
1943 glyph_t glyph = engine(0)->glyphIndex(ucs4);
1944 if (glyph == 0 && !isIgnorableChar(ucs4)) {
1945 if (!m_fallbackFamiliesQueried)
1946 const_cast<QFontEngineMulti *>(
this)->ensureFallbackFamiliesQueried();
1947 for (
int x = 1, n = qMin(m_engines.size(), 256); x < n; ++x) {
1948 QFontEngine *engine = m_engines.at(x);
1950 if (!shouldLoadFontEngineForCharacter(x, ucs4))
1952 const_cast<QFontEngineMulti *>(
this)->ensureEngineAt(x);
1953 engine = m_engines.at(x);
1955 Q_ASSERT(engine !=
nullptr);
1956 if (engine->type() == Box)
1959 glyph = engine->glyphIndex(ucs4);
1983int QFontEngineMulti::stringToCMap(
const QChar *str,
int len,
1984 QGlyphLayout *glyphs,
int *nglyphs,
1985 QFontEngine::ShaperFlags flags)
const
1987 const int originalNumGlyphs = glyphs->numGlyphs;
1988 int mappedGlyphCount = engine(0)->stringToCMap(str, len, glyphs, nglyphs, flags);
1989 if (mappedGlyphCount < 0)
1994 bool contextFontMerging = mappedGlyphCount < *nglyphs && (fontDef.styleStrategy & QFont::ContextFontMerging);
1995 if (contextFontMerging) {
1996 QVarLengthGlyphLayoutArray tempLayout(len);
1997 if (!m_fallbackFamiliesQueried)
1998 const_cast<QFontEngineMulti *>(
this)->ensureFallbackFamiliesQueried();
2000 int maxGlyphCount = 0;
2001 uchar engineIndex = 0;
2002 for (
int x = 1, n = qMin(m_engines.size(), 256); x < n; ++x) {
2003 int numGlyphs = len;
2004 const_cast<QFontEngineMulti *>(
this)->ensureEngineAt(x);
2005 maxGlyphCount = engine(x)->stringToCMap(str, len, &tempLayout, &numGlyphs, flags);
2008 if (maxGlyphCount > mappedGlyphCount) {
2009 *nglyphs = numGlyphs;
2010 glyphs->numGlyphs = originalNumGlyphs;
2011 glyphs->copy(&tempLayout);
2013 if (maxGlyphCount == numGlyphs)
2018 if (engineIndex > 0) {
2019 for (
int y = 0; y < glyphs->numGlyphs; ++y) {
2020 if (glyphs->glyphs[y] != 0)
2021 glyphs->glyphs[y] |= (engineIndex << 24);
2024 contextFontMerging =
false;
2027 mappedGlyphCount = maxGlyphCount;
2033 QStringIterator it(str, str + len);
2035 const bool enableVariationSelectorHack = disableEmojiSegmenter();
2036 char32_t previousUcs4 = 0;
2038 int lastFallback = -1;
2039 while (it.hasNext()) {
2040 const char32_t ucs4 = it.peekNext();
2045 if (lastFallback >= 0 && (ucs4 == 0x200d || ucs4 == 0x200c)) {
2046 QFontEngine *engine = m_engines.at(lastFallback);
2047 glyph_t glyph = engine->glyphIndex(ucs4);
2049 glyphs->glyphs[glyph_pos] = glyph;
2050 if (!(flags & GlyphIndicesOnly)) {
2051 QGlyphLayout g = glyphs->mid(glyph_pos, 1);
2052 engine->recalcAdvances(&g, flags);
2056 glyphs->glyphs[glyph_pos] |= (lastFallback << 24);
2064 if (glyphs->glyphs[glyph_pos] == 0 && !isIgnorableChar(ucs4)) {
2065 if (!m_fallbackFamiliesQueried)
2066 const_cast<QFontEngineMulti *>(
this)->ensureFallbackFamiliesQueried();
2067 for (
int x = contextFontMerging ? 0 : 1, n = qMin(m_engines.size(), 256); x < n; ++x) {
2068 QFontEngine *engine = m_engines.at(x);
2070 if (!shouldLoadFontEngineForCharacter(x, ucs4))
2072 const_cast<QFontEngineMulti *>(
this)->ensureEngineAt(x);
2073 engine = m_engines.at(x);
2077 Q_ASSERT(engine !=
nullptr);
2078 if (engine->type() == Box)
2081 glyph_t glyph = engine->glyphIndex(ucs4);
2083 glyphs->glyphs[glyph_pos] = glyph;
2084 if (!(flags & GlyphIndicesOnly)) {
2085 QGlyphLayout g = glyphs->mid(glyph_pos, 1);
2086 engine->recalcAdvances(&g, flags);
2092 glyphs->glyphs[glyph_pos] |= (x << 24);
2100 const int variantSelectorBlock = 0xFE00;
2101 if (enableVariationSelectorHack && (ucs4 & 0xFFF0) == variantSelectorBlock && glyph_pos > 0) {
2102 int selectorFontEngine = glyphs->glyphs[glyph_pos] >> 24;
2103 int precedingCharacterFontEngine = glyphs->glyphs[glyph_pos - 1] >> 24;
2105 if (selectorFontEngine != precedingCharacterFontEngine) {
2111 const QFontEngine *selectedEngine = m_engines.at(precedingCharacterFontEngine);
2112 const bool colorFont = selectedEngine->isColorFont();
2113 const char32_t vs15 = 0xFE0E;
2114 const char32_t vs16 = 0xFE0F;
2115 bool adaptVariantSelector = ucs4 < vs15
2116 || (ucs4 == vs15 && colorFont)
2117 || (ucs4 == vs16 && !colorFont);
2119 if (adaptVariantSelector) {
2120 QFontEngine *engine = m_engines.at(selectorFontEngine);
2121 glyph_t glyph = engine->glyphIndex(previousUcs4);
2123 glyphs->glyphs[glyph_pos - 1] = glyph;
2124 if (!(flags & GlyphIndicesOnly)) {
2125 QGlyphLayout g = glyphs->mid(glyph_pos - 1, 1);
2126 engine->recalcAdvances(&g, flags);
2130 glyphs->glyphs[glyph_pos - 1] |= (selectorFontEngine << 24);
2140 previousUcs4 = ucs4;
2143 *nglyphs = glyph_pos;
2144 glyphs->numGlyphs = glyph_pos;
2145 return mappedGlyphCount;
2155glyph_metrics_t QFontEngineMulti::boundingBox(
const QGlyphLayout &glyphs)
2157 if (glyphs.numGlyphs <= 0)
2158 return glyph_metrics_t();
2160 glyph_metrics_t overall;
2162 int which = highByte(glyphs.glyphs[0]);
2165 for (end = 0; end < glyphs.numGlyphs; ++end) {
2166 const int e = highByte(glyphs.glyphs[end]);
2171 for (i = start; i < end; ++i)
2172 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2175 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
2177 overall.x = qMin(overall.x, gm.x);
2178 overall.y = qMin(overall.y, gm.y);
2179 overall.width = overall.xoff + gm.width;
2180 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
2181 qMin(overall.y, gm.y);
2182 overall.xoff += gm.xoff;
2183 overall.yoff += gm.yoff;
2186 const int hi = which << 24;
2187 for (i = start; i < end; ++i)
2188 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2196 for (i = start; i < end; ++i)
2197 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2200 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
2202 overall.x = qMin(overall.x, gm.x);
2203 overall.y = qMin(overall.y, gm.y);
2204 overall.width = overall.xoff + gm.width;
2205 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
2206 qMin(overall.y, gm.y);
2207 overall.xoff += gm.xoff;
2208 overall.yoff += gm.yoff;
2211 const int hi = which << 24;
2212 for (i = start; i < end; ++i)
2213 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2225void QFontEngineMulti::addOutlineToPath(qreal x, qreal y,
const QGlyphLayout &glyphs,
2226 QPainterPath *path, QTextItem::RenderFlags flags)
2228 if (glyphs.numGlyphs <= 0)
2231 int which = highByte(glyphs.glyphs[0]);
2234 if (flags & QTextItem::RightToLeft) {
2235 for (
int gl = 0; gl < glyphs.numGlyphs; gl++)
2236 x += glyphs.advances[gl].toReal();
2238 for (end = 0; end < glyphs.numGlyphs; ++end) {
2239 const int e = highByte(glyphs.glyphs[end]);
2243 if (flags & QTextItem::RightToLeft) {
2244 for (i = start; i < end; ++i)
2245 x -= glyphs.advances[i].toReal();
2249 for (i = start; i < end; ++i)
2250 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2251 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
2253 const int hi = which << 24;
2254 for (i = start; i < end; ++i)
2255 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2257 if (!(flags & QTextItem::RightToLeft)) {
2258 for (i = start; i < end; ++i)
2259 x += glyphs.advances[i].toReal();
2267 if (flags & QTextItem::RightToLeft) {
2268 for (i = start; i < end; ++i)
2269 x -= glyphs.advances[i].toReal();
2273 for (i = start; i < end; ++i)
2274 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2276 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
2279 const int hi = which << 24;
2280 for (i = start; i < end; ++i)
2281 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2284void QFontEngineMulti::recalcAdvances(QGlyphLayout *glyphs, QFontEngine::ShaperFlags flags)
const
2286 if (glyphs->numGlyphs <= 0)
2289 int which = highByte(glyphs->glyphs[0]);
2292 for (end = 0; end < glyphs->numGlyphs; ++end) {
2293 const int e = highByte(glyphs->glyphs[end]);
2298 for (i = start; i < end; ++i)
2299 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2301 QGlyphLayout offs = glyphs->mid(start, end - start);
2302 engine(which)->recalcAdvances(&offs, flags);
2305 const int hi = which << 24;
2306 for (i = start; i < end; ++i)
2307 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2315 for (i = start; i < end; ++i)
2316 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2318 QGlyphLayout offs = glyphs->mid(start, end - start);
2319 engine(which)->recalcAdvances(&offs, flags);
2322 const int hi = which << 24;
2323 for (i = start; i < end; ++i)
2324 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2327void QFontEngineMulti::doKerning(QGlyphLayout *glyphs, QFontEngine::ShaperFlags flags)
const
2329 if (glyphs->numGlyphs <= 0)
2332 int which = highByte(glyphs->glyphs[0]);
2335 for (end = 0; end < glyphs->numGlyphs; ++end) {
2336 const int e = highByte(glyphs->glyphs[end]);
2341 for (i = start; i < end; ++i)
2342 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2344 QGlyphLayout offs = glyphs->mid(start, end - start);
2345 engine(which)->doKerning(&offs, flags);
2348 const int hi = which << 24;
2349 for (i = start; i < end; ++i)
2350 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2358 for (i = start; i < end; ++i)
2359 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2361 QGlyphLayout offs = glyphs->mid(start, end - start);
2362 engine(which)->doKerning(&offs, flags);
2365 const int hi = which << 24;
2366 for (i = start; i < end; ++i)
2367 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2501QFontEngine *QFontEngineMulti::createMultiFontEngine(QFontEngine *fe,
int script)
2503 QFontEngine *engine =
nullptr;
2504 QFontCache::Key key(fe->fontDef, script,
true);
2505 QFontCache *fc = QFontCache::instance();
2511 const bool faceIsLocal = !fe->faceId().filename.isEmpty();
2512 QFontCache::EngineCache::Iterator it = fc->engineCache.find(key),
2513 end = fc->engineCache.end();
2514 while (it != end && it.key() == key) {
2515 Q_ASSERT(it.value().data->type() == QFontEngine::Multi);
2516 QFontEngineMulti *cachedEngine =
static_cast<QFontEngineMulti *>(it.value().data);
2517 if (fe == cachedEngine->engine(0) || (faceIsLocal && fe->faceId().filename == cachedEngine->engine(0)->faceId().filename)) {
2518 engine = cachedEngine;
2519 fc->updateHitCountAndTimeStamp(it.value());
2525 engine = QGuiApplicationPrivate::instance()->platformIntegration()->fontDatabase()->fontEngineMulti(fe, QFontDatabasePrivate::ExtendedScript(script));
2526 fc->insertEngine(key, engine, !faceIsLocal);