181bool QFontEngine::supportsScript(QChar::Script script)
const
183 if (type() <= QFontEngine::Multi)
189 if (!scriptRequiresOpenType(script))
192#if QT_CONFIG(harfbuzz)
194 uint lenMort = 0, lenMorx = 0;
195 if (getSfntTableData(QFont::Tag(
"mort").value(),
nullptr, &lenMort)
196 || getSfntTableData(QFont::Tag(
"morx").value(),
nullptr, &lenMorx)) {
200 if (hb_face_t *face = hb_qt_face_get_for_engine(
const_cast<QFontEngine *>(
this))) {
201 unsigned int script_count = HB_OT_MAX_TAGS_PER_SCRIPT;
202 hb_tag_t script_tags[HB_OT_MAX_TAGS_PER_SCRIPT];
204 hb_ot_tags_from_script_and_language(hb_qt_script_to_script(script), HB_LANGUAGE_INVALID,
205 &script_count, script_tags,
208 if (hb_ot_layout_table_select_script(face, HB_OT_TAG_GSUB, script_count, script_tags,
nullptr,
nullptr))
268void QFontEngine::getGlyphPositions(
const QGlyphLayout &glyphs,
const QTransform &matrix, QTextItem::RenderFlags flags,
269 QVarLengthArray<glyph_t> &glyphs_out, QVarLengthArray<QFixedPoint> &positions)
274 const bool transform = matrix.m11() != 1.
275 || matrix.m12() != 0.
276 || matrix.m21() != 0.
277 || matrix.m22() != 1.;
279 xpos = QFixed::fromReal(matrix.dx());
280 ypos = QFixed::fromReal(matrix.dy());
284 if (flags & QTextItem::RightToLeft) {
285 int i = glyphs.numGlyphs;
286 int totalKashidas = 0;
288 if (glyphs.attributes[i].dontPrint)
290 xpos += glyphs.advances[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
291 totalKashidas += glyphs.justifications[i].nKashidas;
293 positions.resize(glyphs.numGlyphs+totalKashidas);
294 glyphs_out.resize(glyphs.numGlyphs+totalKashidas);
297 while(i < glyphs.numGlyphs) {
298 if (glyphs.attributes[i].dontPrint) {
302 xpos -= glyphs.advances[i];
304 QFixed gpos_x = xpos + glyphs.offsets[i].x;
305 QFixed gpos_y = ypos + glyphs.offsets[i].y;
307 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
308 gpos = gpos * matrix;
309 gpos_x = QFixed::fromReal(gpos.x());
310 gpos_y = QFixed::fromReal(gpos.y());
312 positions[current].x = gpos_x;
313 positions[current].y = gpos_y;
314 glyphs_out[current] = glyphs.glyphs[i];
316 if (glyphs.justifications[i].nKashidas) {
319 glyph_t kashidaGlyph = glyphIndex(ch.unicode());
324 g.glyphs = &kashidaGlyph;
325 g.advances = &kashidaWidth;
326 recalcAdvances(&g, { });
328 for (uint k = 0; k < glyphs.justifications[i].nKashidas; ++k) {
329 xpos -= kashidaWidth;
331 QFixed gpos_x = xpos + glyphs.offsets[i].x;
332 QFixed gpos_y = ypos + glyphs.offsets[i].y;
334 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
335 gpos = gpos * matrix;
336 gpos_x = QFixed::fromReal(gpos.x());
337 gpos_y = QFixed::fromReal(gpos.y());
339 positions[current].x = gpos_x;
340 positions[current].y = gpos_y;
341 glyphs_out[current] = kashidaGlyph;
345 xpos -= QFixed::fromFixed(glyphs.justifications[i].space_18d6);
350 positions.resize(glyphs.numGlyphs);
351 glyphs_out.resize(glyphs.numGlyphs);
354 while (i < glyphs.numGlyphs) {
355 if (!glyphs.attributes[i].dontPrint) {
356 positions[current].x = xpos + glyphs.offsets[i].x;
357 positions[current].y = ypos + glyphs.offsets[i].y;
358 glyphs_out[current] = glyphs.glyphs[i];
359 xpos += glyphs.advances[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
365 while (i < glyphs.numGlyphs) {
366 if (!glyphs.attributes[i].dontPrint) {
367 QFixed gpos_x = xpos + glyphs.offsets[i].x;
368 QFixed gpos_y = ypos + glyphs.offsets[i].y;
369 QPointF gpos(gpos_x.toReal(), gpos_y.toReal());
370 gpos = gpos * matrix;
371 positions[current].x = QFixed::fromReal(gpos.x());
372 positions[current].y = QFixed::fromReal(gpos.y());
373 glyphs_out[current] = glyphs.glyphs[i];
374 xpos += glyphs.advances[i] + QFixed::fromFixed(glyphs.justifications[i].space_18d6);
381 positions.resize(current);
382 glyphs_out.resize(current);
383 Q_ASSERT(positions.size() == glyphs_out.size());
395bool QFontEngine::processHheaTable()
const
397 QByteArray hhea = getSfntTable(QFont::Tag(
"hhea").value());
398 if (hhea.size() >= 10) {
399 auto ptr = hhea.constData();
400 qint16 ascent = qFromBigEndian<qint16>(ptr + 4);
401 qint16 descent = qFromBigEndian<qint16>(ptr + 6);
402 qint16 leading = qFromBigEndian<qint16>(ptr + 8);
405 if (ascent == 0 && descent == 0)
408 const qreal unitsPerEm = emSquareSize().toReal();
410 const auto limitForQFixed = qreal(std::numeric_limits<
int>::max() >> 6) / fontDef.pixelSize;
411 if (ascent > limitForQFixed || descent > limitForQFixed || leading > limitForQFixed)
413 m_ascent = QFixed::fromReal(ascent * fontDef.pixelSize / unitsPerEm);
414 m_descent = -QFixed::fromReal(descent * fontDef.pixelSize / unitsPerEm);
415 m_leading = QFixed::fromReal(leading * fontDef.pixelSize / unitsPerEm);
423void QFontEngine::initializeHeightMetrics()
const
425 bool hasEmbeddedBitmaps =
426 !getSfntTable(QFont::Tag(
"EBLC").value()).isEmpty()
427 || !getSfntTable(QFont::Tag(
"CBLC").value()).isEmpty()
428 || !getSfntTable(QFont::Tag(
"bdat").value()).isEmpty();
435 static bool useSystemLineMetrics = qEnvironmentVariableIntValue(
"QT_USE_SYSTEM_LINE_METRICS") > 0;
436 if (!hasEmbeddedBitmaps && !useSystemLineMetrics) {
443 if (!supportsSubPixelPositions()) {
444 const QFixed actualHeight = m_ascent + m_descent + m_leading;
445 m_ascent = m_ascent.round();
446 m_descent = m_descent.round();
447 m_leading = actualHeight.round() - m_ascent - m_descent;
451 m_heightMetricsQueried =
true;
459bool QFontEngine::processOS2Table()
const
461 QByteArray os2 = getSfntTable(QFont::Tag(
"OS/2").value());
462 if (os2.size() >= 78) {
463 auto ptr = os2.constData();
464 quint16 fsSelection = qFromBigEndian<quint16>(ptr + 62);
465 qint16 typoAscent = qFromBigEndian<qint16>(ptr + 68);
466 qint16 typoDescent = qFromBigEndian<qint16>(ptr + 70);
467 qint16 typoLineGap = qFromBigEndian<qint16>(ptr + 72);
468 quint16 winAscent = qFromBigEndian<quint16>(ptr + 74);
469 quint16 winDescent = qFromBigEndian<quint16>(ptr + 76);
471 enum { USE_TYPO_METRICS = 0x80 };
472 const qreal unitsPerEm = emSquareSize().toReal();
473 if (preferTypoLineMetrics() || fsSelection & USE_TYPO_METRICS) {
475 if (typoAscent == 0 && typoDescent == 0)
478 const auto limitForQFixed = qreal(std::numeric_limits<
int>::max() >> 6) / fontDef.pixelSize;
479 if (typoAscent > limitForQFixed || typoDescent > limitForQFixed
480 || typoLineGap > limitForQFixed)
482 m_ascent = QFixed::fromReal(typoAscent * fontDef.pixelSize / unitsPerEm);
483 m_descent = -QFixed::fromReal(typoDescent * fontDef.pixelSize / unitsPerEm);
484 m_leading = QFixed::fromReal(typoLineGap * fontDef.pixelSize / unitsPerEm);
487 if (winAscent == 0 && winDescent == 0)
489 const auto limitForQFixed = qreal(std::numeric_limits<
int>::max() >> 6) / fontDef.pixelSize;
490 if (winAscent > limitForQFixed || winDescent > limitForQFixed)
492 m_ascent = QFixed::fromReal(winAscent * fontDef.pixelSize / unitsPerEm);
493 m_descent = QFixed::fromReal(winDescent * fontDef.pixelSize / unitsPerEm);
494 m_leading = QFixed{};
547qreal QFontEngine::minRightBearing()
const
552 QByteArray hheaTable = getSfntTable(QFont::Tag(
"hhea").value());
554 const uchar *tableData =
reinterpret_cast<
const uchar *>(hheaTable.constData());
563 int unitsPerEm = emSquareSize().toInt();
564 qreal funitToPixelFactor = fontDef.pixelSize / unitsPerEm;
570 static const int largestValidBearing = 4 * unitsPerEm;
572 if (qAbs(minLeftSideBearing) < largestValidBearing)
573 m_minLeftBearing = minLeftSideBearing * funitToPixelFactor;
574 if (qAbs(minRightSideBearing) < largestValidBearing)
575 m_minRightBearing = minRightSideBearing * funitToPixelFactor;
584 static const ushort characterSubset[] = {
585 '(',
'C',
'F',
'K',
'V',
'X',
'Y',
']',
'_',
'f',
'r',
'|',
586 127, 205, 645, 884, 922, 1070, 12386
590 m_minLeftBearing = m_minRightBearing = std::numeric_limits<qreal>::max();
592 for (uint i = 0; i < (
sizeof(characterSubset) /
sizeof(ushort)); ++i) {
593 const glyph_t glyph = glyphIndex(characterSubset[i]);
597 glyph_metrics_t glyphMetrics =
const_cast<QFontEngine *>(
this)->boundingBox(glyph);
600 if (!glyphMetrics.width || !glyphMetrics.height)
603 m_minLeftBearing = qMin(m_minLeftBearing, glyphMetrics.leftBearing().toReal());
604 m_minRightBearing = qMin(m_minRightBearing, glyphMetrics.rightBearing().toReal());
609 qWarning() <<
"Failed to compute left/right minimum bearings for"
610 << fontDef.families.first();
613 return m_minRightBearing;
626glyph_metrics_t QFontEngine::tightBoundingBox(
const QGlyphLayout &glyphs, QTextItem::RenderFlags flags)
628 glyph_metrics_t overall;
630 const bool isRtl = flags & QTextItem::RightToLeft;
632 for (
int i = 0; i < glyphs.numGlyphs; i++) {
634 if (glyphs.attributes[i].dontPrint)
637 overall.xoff += glyphs.effectiveAdvance(i);
643 for (
int i = 0; i < glyphs.numGlyphs; i++) {
645 if (glyphs.attributes[i].dontPrint)
648 glyph_metrics_t bb = boundingBox(glyphs.glyphs[i]);
651 overall.xoff -= glyphs.effectiveAdvance(i);
653 QFixed x = overall.xoff + glyphs.offsets[i].x + bb.x;
654 QFixed y = overall.yoff + glyphs.offsets[i].y + bb.y;
656 overall.x = qMin(overall.x, x);
657 overall.y = qMin(overall.y, y);
658 xmax = qMax(xmax, x.ceil() + bb.width);
659 ymax = qMax(ymax, y.ceil() + bb.height);
662 overall.xoff += glyphs.effectiveAdvance(i);
663 overall.yoff += bb.yoff;
665 overall.height = qMax(overall.height, ymax - overall.y);
666 overall.width = xmax - overall.x;
672void QFontEngine::addOutlineToPath(qreal x, qreal y,
const QGlyphLayout &glyphs, QPainterPath *path,
673 QTextItem::RenderFlags flags)
675 if (!glyphs.numGlyphs)
678 QVarLengthArray<QFixedPoint> positions;
679 QVarLengthArray<glyph_t> positioned_glyphs;
680 QTransform matrix = QTransform::fromTranslate(x, y);
681 getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
682 addGlyphsToPath(positioned_glyphs.data(), positions.data(), positioned_glyphs.size(), path, flags);
779void QFontEngine::addBitmapFontToPath(qreal x, qreal y,
const QGlyphLayout &glyphs,
780 QPainterPath *path, QTextItem::RenderFlags flags)
784 QFixed advanceX = QFixed::fromReal(x);
785 QFixed advanceY = QFixed::fromReal(y);
786 for (
int i=0; i < glyphs.numGlyphs; ++i) {
787 glyph_metrics_t metrics = boundingBox(glyphs.glyphs[i]);
788 if (metrics.width.value() == 0 || metrics.height.value() == 0) {
789 advanceX += glyphs.advances[i];
792 const QImage alphaMask = alphaMapForGlyph(glyphs.glyphs[i]);
794 const int w = alphaMask.width();
795 const int h = alphaMask.height();
796 const qsizetype srcBpl = alphaMask.bytesPerLine();
798 if (alphaMask.depth() == 1) {
801 bitmap = QImage(w, h, QImage::Format_Mono);
802 const uchar *imageData = alphaMask.bits();
803 const qsizetype destBpl = bitmap.bytesPerLine();
804 uchar *bitmapData = bitmap.bits();
806 for (
int yi = 0; yi < h; ++yi) {
807 const uchar *src = imageData + yi*srcBpl;
808 uchar *dst = bitmapData + yi*destBpl;
809 for (
int xi = 0; xi < w; ++xi) {
810 const int byte = xi / 8;
811 const int bit = xi % 8;
815 dst[byte] |= 128 >> bit;
819 const uchar *bitmap_data = bitmap.constBits();
820 QFixedPoint offset = glyphs.offsets[i];
821 advanceX += offset.x;
822 advanceY += offset.y;
823 qt_addBitmapToPath((advanceX + metrics.x).toReal(), (advanceY + metrics.y).toReal(), bitmap_data, bitmap.bytesPerLine(), w, h, path);
824 advanceX += glyphs.advances[i];
828void QFontEngine::addGlyphsToPath(glyph_t *glyphs, QFixedPoint *positions,
int nGlyphs,
829 QPainterPath *path, QTextItem::RenderFlags flags)
831 qreal x = positions[0].x.toReal();
832 qreal y = positions[0].y.toReal();
833 QVarLengthGlyphLayoutArray g(nGlyphs);
835 for (
int i = 0; i < nGlyphs - 1; ++i) {
836 g.glyphs[i] = glyphs[i];
837 g.advances[i] = positions[i + 1].x - positions[i].x;
839 g.glyphs[nGlyphs - 1] = glyphs[nGlyphs - 1];
840 g.advances[nGlyphs - 1] = QFixed::fromReal(maxCharWidth());
842 addBitmapFontToPath(x, y, g, path, flags);
874QImage QFontEngine::alphaRGBMapForGlyph(glyph_t glyph,
const QFixedPoint &,
const QTransform &t)
876 const QImage alphaMask = alphaMapForGlyph(glyph, t);
877 QImage rgbMask(alphaMask.width(), alphaMask.height(), QImage::Format_RGB32);
879 for (
int y=0; y<alphaMask.height(); ++y) {
880 uint *dst = (uint *) rgbMask.scanLine(y);
881 const uchar *src = alphaMask.constScanLine(y);
882 for (
int x=0; x<alphaMask.width(); ++x) {
884 dst[x] = qRgb(val, val, val);
969glyph_t QFontEngine::findGlyph(QLatin1StringView name)
const
973#if QT_CONFIG(harfbuzz)
974 result = queryHarfbuzz(
this, [name](hb_font_t *hbFont){
976 hb_codepoint_t glyph;
977 if (hb_font_get_glyph_from_name(hbFont, name.constData(), name.size(), &glyph))
978 return glyph_t(glyph);
983 for (glyph_t index = 0; index < uint(glyphCount()); ++index) {
984 if (name == glyphName(index))
991 constexpr auto gid =
"gid"_L1;
992 constexpr auto uni =
"uni"_L1;
993 if (name.startsWith(gid)) {
995 result = name.slice(gid.size()).toUInt(&ok);
996 if (ok && result < glyph_t(glyphCount()))
998 }
else if (name.startsWith(uni)) {
1000 const uint ucs4 = name.slice(uni.size()).toUInt(&ok, 16);
1002 result = glyphIndex(ucs4);
1003 if (result > 0 && result < glyph_t(glyphCount()))
1012QImage QFontEngine::renderedPathForGlyph(glyph_t glyph,
const QColor &color)
1014 glyph_metrics_t gm = boundingBox(glyph);
1015 int glyph_x = qFloor(gm.x.toReal());
1016 int glyph_y = qFloor(gm.y.toReal());
1017 int glyph_width = qCeil((gm.x + gm.width).toReal()) - glyph_x;
1018 int glyph_height = qCeil((gm.y + gm.height).toReal()) - glyph_y;
1020 if (glyph_width <= 0 || glyph_height <= 0)
1026 path.setFillRule(Qt::WindingFill);
1027 QImage im(glyph_width, glyph_height, QImage::Format_ARGB32_Premultiplied);
1028 im.fill(Qt::transparent);
1030 p.setRenderHint(QPainter::Antialiasing);
1031 addGlyphsToPath(&glyph, &pt, 1, &path, { });
1032 p.setPen(Qt::NoPen);
1183void QFontEngine::doKerning(QGlyphLayout *glyphs, QFontEngine::ShaperFlags flags)
const
1185 int numPairs = kerning_pairs.size();
1189 const KernPair *pairs = kerning_pairs.constData();
1191 if (flags & DesignMetrics) {
1192 for(
int i = 0; i < glyphs->numGlyphs - 1; ++i)
1193 glyphs->advances[i] += kerning(glyphs->glyphs[i], glyphs->glyphs[i+1] , pairs, numPairs);
1195 for(
int i = 0; i < glyphs->numGlyphs - 1; ++i)
1196 glyphs->advances[i] += qRound(kerning(glyphs->glyphs[i], glyphs->glyphs[i+1] , pairs, numPairs));
1200void QFontEngine::loadKerningPairs(QFixed scalingFactor)
1202 kerning_pairs.clear();
1204 QByteArray tab = getSfntTable(QFont::Tag(
"kern").value());
1208 const uchar *table =
reinterpret_cast<
const uchar *>(tab.constData());
1209 const uchar *end = table + tab.size();
1212 if (!qSafeFromBigEndian(table, end, &version))
1221 if (!qSafeFromBigEndian(table + 2, end, &numTables))
1226 for(
int i = 0; i < numTables; ++i) {
1227 const uchar *header = table + offset;
1230 if (!qSafeFromBigEndian(header, end, &version))
1234 if (!qSafeFromBigEndian(header + 2, end, &length))
1238 if (!qSafeFromBigEndian(header + 4, end, &coverage))
1242 if (version == 0 && coverage == 0x0001) {
1243 if (offset + length > tab.size()) {
1247 const uchar *data = table + offset + 6;
1250 if (!qSafeFromBigEndian(data, end, &nPairs))
1253 if (nPairs * 6 + 8 > length - 6) {
1260 for(
int i = 0; i < nPairs; ++i) {
1261 QFontEngine::KernPair p;
1264 if (!qSafeFromBigEndian(data + off, end, &tmp))
1267 p.left_right = uint(tmp) << 16;
1268 if (!qSafeFromBigEndian(data + off + 2, end, &tmp))
1271 p.left_right |= tmp;
1273 if (!qSafeFromBigEndian(data + off + 4, end, &tmp))
1276 p.adjust = QFixed(
int(
short(tmp))) / scalingFactor;
1277 kerning_pairs.append(p);
1285 std::sort(kerning_pairs.begin(), kerning_pairs.end());
1334const uchar *QFontEngine::getCMap(
const uchar *table, uint tableSize,
bool *isSymbolFont,
int *cmapSize)
1336 const uchar *header = table;
1337 const uchar *endPtr = table + tableSize;
1341 if (!qSafeFromBigEndian(header, endPtr, &version) || version != 0)
1345 if (!qSafeFromBigEndian(header + 2, endPtr, &numTables))
1348 const uchar *maps = table + 4;
1357 MicrosoftUnicodeExtended
1360 int symbolTable = -1;
1361 int tableToUse = -1;
1362 int score = Invalid;
1363 for (
int n = 0; n < numTables; ++n) {
1364 quint16 platformId = 0;
1365 if (!qSafeFromBigEndian(maps + 8 * n, endPtr, &platformId))
1368 quint16 platformSpecificId = 0;
1369 if (!qSafeFromBigEndian(maps + 8 * n + 2, endPtr, &platformSpecificId))
1372 switch (platformId) {
1374 if (score < Unicode &&
1375 (platformSpecificId == 0 ||
1376 platformSpecificId == 2 ||
1377 platformSpecificId == 3)) {
1380 }
else if (score < Unicode11 && platformSpecificId == 1) {
1386 if (score < AppleRoman && platformSpecificId == 0) {
1392 switch (platformSpecificId) {
1395 if (score < Symbol) {
1401 if (score < MicrosoftUnicode) {
1403 score = MicrosoftUnicode;
1407 if (score < MicrosoftUnicodeExtended) {
1409 score = MicrosoftUnicodeExtended;
1424 *isSymbolFont = (symbolTable > -1);
1426 quint32 unicode_table = 0;
1427 if (!qSafeFromBigEndian(maps + 8 * tableToUse + 4, endPtr, &unicode_table))
1434 header = table + unicode_table;
1437 if (!qSafeFromBigEndian(header, endPtr, &format))
1443 if (!qSafeFromBigEndian(header + 2, endPtr, &tmp))
1447 if (!qSafeFromBigEndian(header + 4, endPtr, &length))
1451 if (table + unicode_table + length > endPtr)
1458 if (symbolTable > -1 && ((score == Unicode) || (score == Unicode11))) {
1459 const uchar *selectedTable = table + unicode_table;
1462 bool unicodeTableHasLatin1 =
false;
1463 for (
int uc=0x00; uc<0x100; ++uc) {
1464 if (getTrueTypeGlyphIndex(selectedTable, length, uc) != 0) {
1465 unicodeTableHasLatin1 =
true;
1471 bool unicodeTableHasSymbols =
false;
1472 if (!unicodeTableHasLatin1) {
1473 for (
int uc=0xf000; uc<0xf100; ++uc) {
1474 if (getTrueTypeGlyphIndex(selectedTable, length, uc) != 0) {
1475 unicodeTableHasSymbols =
true;
1482 if (!unicodeTableHasLatin1 && unicodeTableHasSymbols) {
1483 tableToUse = symbolTable;
1489 return table + unicode_table;
1492quint32 QFontEngine::getTrueTypeGlyphIndex(
const uchar *cmap,
int cmapSize, uint unicode)
1494 const uchar *end = cmap + cmapSize;
1496 if (!qSafeFromBigEndian(cmap, end, &format))
1500 const uchar *ptr = cmap + 6 + unicode;
1501 if (unicode < 256 && ptr < end)
1502 return quint32(*ptr);
1503 }
else if (format == 4) {
1505
1506
1507
1508
1509 if (unicode >= 0xffff)
1512 quint16 segCountX2 = 0;
1513 if (!qSafeFromBigEndian(cmap + 6, end, &segCountX2))
1516 const unsigned char *ends = cmap + 14;
1519 for (; i < segCountX2/2; ++i) {
1520 quint16 codePoint = 0;
1521 if (!qSafeFromBigEndian(ends + 2 * i, end, &codePoint))
1523 if (codePoint >= unicode)
1527 const unsigned char *idx = ends + segCountX2 + 2 + 2*i;
1529 quint16 startIndex = 0;
1530 if (!qSafeFromBigEndian(idx, end, &startIndex))
1532 if (startIndex > unicode)
1538 if (!qSafeFromBigEndian(idx, end, &tmp))
1540 qint16 idDelta = qint16(tmp);
1544 quint16 idRangeoffset_t = 0;
1545 if (!qSafeFromBigEndian(idx, end, &idRangeoffset_t))
1548 quint16 glyphIndex = 0;
1549 if (idRangeoffset_t) {
1551 if (!qSafeFromBigEndian(idRangeoffset_t + 2 * (unicode - startIndex) + idx, end, &id))
1555 glyphIndex = (idDelta + id) % 0x10000;
1559 glyphIndex = (idDelta + unicode) % 0x10000;
1562 }
else if (format == 6) {
1563 quint16 tableSize = 0;
1564 if (!qSafeFromBigEndian(cmap + 2, end, &tableSize))
1567 quint16 firstCode6 = 0;
1568 if (!qSafeFromBigEndian(cmap + 6, end, &firstCode6))
1570 if (unicode < firstCode6)
1573 quint16 entryCount6 = 0;
1574 if (!qSafeFromBigEndian(cmap + 8, end, &entryCount6))
1576 if (entryCount6 * 2 + 10 > tableSize)
1579 quint16 sentinel6 = firstCode6 + entryCount6;
1580 if (unicode >= sentinel6)
1583 quint16 entryIndex6 = unicode - firstCode6;
1586 qSafeFromBigEndian(cmap + 10 + (entryIndex6 * 2), end, &index);
1588 }
else if (format == 12) {
1589 quint32 nGroups = 0;
1590 if (!qSafeFromBigEndian(cmap + 12, end, &nGroups))
1595 int left = 0, right = nGroups - 1;
1596 while (left <= right) {
1597 int middle = left + ( ( right - left ) >> 1 );
1599 quint32 startCharCode = 0;
1600 if (!qSafeFromBigEndian(cmap + 12 * middle, end, &startCharCode))
1603 if (unicode < startCharCode)
1606 quint32 endCharCode = 0;
1607 if (!qSafeFromBigEndian(cmap + 12 * middle + 4, end, &endCharCode))
1610 if (unicode <= endCharCode) {
1612 if (!qSafeFromBigEndian(cmap + 12 * middle + 8, end, &index))
1615 return index + unicode - startCharCode;
1621 qDebug(
"cmap table of format %d not implemented", format);
1748int QFontEngineBox::stringToCMap(
const QChar *str,
int len, QGlyphLayout *glyphs,
int *nglyphs, QFontEngine::ShaperFlags flags)
const
1750 Q_ASSERT(glyphs->numGlyphs >= *nglyphs);
1751 if (*nglyphs < len) {
1757 QStringIterator it(str, str + len);
1758 while (it.hasNext()) {
1760 glyphs->glyphs[ucs4Length++] = 1;
1763 *nglyphs = ucs4Length;
1764 glyphs->numGlyphs = ucs4Length;
1766 if (!(flags & GlyphIndicesOnly))
1767 recalcAdvances(glyphs, flags);
1778void QFontEngineBox::addOutlineToPath(qreal x, qreal y,
const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags)
1780 if (!glyphs.numGlyphs)
1783 QVarLengthArray<QFixedPoint> positions;
1784 QVarLengthArray<glyph_t> positioned_glyphs;
1785 QTransform matrix = QTransform::fromTranslate(x, y - _size);
1786 getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
1788 QSize s(_size - 3, _size - 3);
1789 for (
int k = 0; k < positions.size(); k++)
1790 path->addRect(QRectF(positions[k].toPointF(), s));
1802void QFontEngineBox::draw(QPaintEngine *p, qreal x, qreal y,
const QTextItemInt &ti)
1804 if (!ti.glyphs.numGlyphs)
1808 QSize s(_size - 3, _size - 3);
1810 QVarLengthArray<QFixedPoint> positions;
1811 QVarLengthArray<glyph_t> glyphs;
1812 QTransform matrix = QTransform::fromTranslate(x, y - _size);
1813 ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
1814 if (glyphs.size() == 0)
1818 QPainter *painter = p->painter();
1820 painter->setBrush(Qt::NoBrush);
1821 QPen pen = painter->pen();
1822 pen.setWidthF(lineThickness().toReal());
1823 painter->setPen(pen);
1824 for (
int k = 0; k < positions.size(); k++)
1825 painter->drawRect(QRectF(positions[k].toPointF(), s));
1897QFontEngineMulti::QFontEngineMulti(QFontEngine *engine,
int script,
const QStringList &fallbackFamilies)
1898 : QFontEngine(Multi),
1899 m_fallbackFamilies(fallbackFamilies),
1901 m_fallbackFamiliesQueried(!m_fallbackFamilies.isEmpty())
1903 Q_ASSERT(engine && engine->type() != QFontEngine::Multi);
1905 if (m_fallbackFamilies.isEmpty()) {
1907 m_fallbackFamilies << QString();
1910 m_engines.resize(m_fallbackFamilies.size() + 1);
1913 m_engines[0] = engine;
1915 fontDef = engine->fontDef;
1916 cache_cost = engine->cache_cost;
2001glyph_t QFontEngineMulti::glyphIndex(uint ucs4)
const
2003 glyph_t glyph = engine(0)->glyphIndex(ucs4);
2004 if (glyph == 0 && !isIgnorableChar(ucs4)) {
2005 if (!m_fallbackFamiliesQueried)
2006 const_cast<QFontEngineMulti *>(
this)->ensureFallbackFamiliesQueried();
2007 for (
int x = 1, n = qMin(m_engines.size(), 256); x < n; ++x) {
2008 QFontEngine *engine = m_engines.at(x);
2010 if (!shouldLoadFontEngineForCharacter(x, ucs4))
2012 const_cast<QFontEngineMulti *>(
this)->ensureEngineAt(x);
2013 engine = m_engines.at(x);
2015 Q_ASSERT(engine !=
nullptr);
2016 if (engine->type() == Box)
2019 glyph = engine->glyphIndex(ucs4);
2043int QFontEngineMulti::stringToCMap(
const QChar *str,
int len,
2044 QGlyphLayout *glyphs,
int *nglyphs,
2045 QFontEngine::ShaperFlags flags)
const
2047 const int originalNumGlyphs = glyphs->numGlyphs;
2048 int mappedGlyphCount = engine(0)->stringToCMap(str, len, glyphs, nglyphs, flags);
2049 if (mappedGlyphCount < 0)
2054 bool contextFontMerging = mappedGlyphCount < *nglyphs && (fontDef.styleStrategy & QFont::ContextFontMerging);
2055 if (contextFontMerging) {
2056 QVarLengthGlyphLayoutArray tempLayout(len);
2057 if (!m_fallbackFamiliesQueried)
2058 const_cast<QFontEngineMulti *>(
this)->ensureFallbackFamiliesQueried();
2060 int maxGlyphCount = 0;
2061 uchar engineIndex = 0;
2062 for (
int x = 1, n = qMin(m_engines.size(), 256); x < n; ++x) {
2063 int numGlyphs = len;
2064 const_cast<QFontEngineMulti *>(
this)->ensureEngineAt(x);
2065 maxGlyphCount = engine(x)->stringToCMap(str, len, &tempLayout, &numGlyphs, flags);
2068 if (maxGlyphCount > mappedGlyphCount) {
2069 *nglyphs = numGlyphs;
2070 glyphs->numGlyphs = originalNumGlyphs;
2071 glyphs->copy(&tempLayout);
2073 if (maxGlyphCount == numGlyphs)
2078 if (engineIndex > 0) {
2079 for (
int y = 0; y < glyphs->numGlyphs; ++y) {
2080 if (glyphs->glyphs[y] != 0)
2081 glyphs->glyphs[y] |= (engineIndex << 24);
2084 contextFontMerging =
false;
2087 mappedGlyphCount = maxGlyphCount;
2093 QStringIterator it(str, str + len);
2095 const bool enableVariationSelectorHack = disableEmojiSegmenter();
2096 char32_t previousUcs4 = 0;
2098 int lastFallback = -1;
2099 while (it.hasNext()) {
2100 const char32_t ucs4 = it.peekNext();
2105 if (lastFallback >= 0 && (ucs4 == 0x200d || ucs4 == 0x200c)) {
2106 QFontEngine *engine = m_engines.at(lastFallback);
2107 glyph_t glyph = engine->glyphIndex(ucs4);
2109 glyphs->glyphs[glyph_pos] = glyph;
2110 if (!(flags & GlyphIndicesOnly)) {
2111 QGlyphLayout g = glyphs->mid(glyph_pos, 1);
2112 engine->recalcAdvances(&g, flags);
2116 glyphs->glyphs[glyph_pos] |= (lastFallback << 24);
2124 if (glyphs->glyphs[glyph_pos] == 0 && !isIgnorableChar(ucs4)) {
2125 if (!m_fallbackFamiliesQueried)
2126 const_cast<QFontEngineMulti *>(
this)->ensureFallbackFamiliesQueried();
2127 for (
int x = contextFontMerging ? 0 : 1, n = qMin(m_engines.size(), 256); x < n; ++x) {
2128 QFontEngine *engine = m_engines.at(x);
2130 if (!shouldLoadFontEngineForCharacter(x, ucs4))
2132 const_cast<QFontEngineMulti *>(
this)->ensureEngineAt(x);
2133 engine = m_engines.at(x);
2137 Q_ASSERT(engine !=
nullptr);
2138 if (engine->type() == Box)
2141 glyph_t glyph = engine->glyphIndex(ucs4);
2143 glyphs->glyphs[glyph_pos] = glyph;
2144 if (!(flags & GlyphIndicesOnly)) {
2145 QGlyphLayout g = glyphs->mid(glyph_pos, 1);
2146 engine->recalcAdvances(&g, flags);
2152 glyphs->glyphs[glyph_pos] |= (x << 24);
2160 const int variantSelectorBlock = 0xFE00;
2161 if (enableVariationSelectorHack && (ucs4 & 0xFFF0) == variantSelectorBlock && glyph_pos > 0) {
2162 int selectorFontEngine = glyphs->glyphs[glyph_pos] >> 24;
2163 int precedingCharacterFontEngine = glyphs->glyphs[glyph_pos - 1] >> 24;
2165 if (selectorFontEngine != precedingCharacterFontEngine) {
2171 const QFontEngine *selectedEngine = m_engines.at(precedingCharacterFontEngine);
2172 const bool colorFont = selectedEngine->isColorFont();
2173 const char32_t vs15 = 0xFE0E;
2174 const char32_t vs16 = 0xFE0F;
2175 bool adaptVariantSelector = ucs4 < vs15
2176 || (ucs4 == vs15 && colorFont)
2177 || (ucs4 == vs16 && !colorFont);
2179 if (adaptVariantSelector) {
2180 QFontEngine *engine = m_engines.at(selectorFontEngine);
2181 glyph_t glyph = engine->glyphIndex(previousUcs4);
2183 glyphs->glyphs[glyph_pos - 1] = glyph;
2184 if (!(flags & GlyphIndicesOnly)) {
2185 QGlyphLayout g = glyphs->mid(glyph_pos - 1, 1);
2186 engine->recalcAdvances(&g, flags);
2190 glyphs->glyphs[glyph_pos - 1] |= (selectorFontEngine << 24);
2200 previousUcs4 = ucs4;
2203 *nglyphs = glyph_pos;
2204 glyphs->numGlyphs = glyph_pos;
2205 return mappedGlyphCount;
2215glyph_metrics_t QFontEngineMulti::boundingBox(
const QGlyphLayout &glyphs)
2217 if (glyphs.numGlyphs <= 0)
2218 return glyph_metrics_t();
2220 glyph_metrics_t overall;
2222 int which = highByte(glyphs.glyphs[0]);
2225 for (end = 0; end < glyphs.numGlyphs; ++end) {
2226 const int e = highByte(glyphs.glyphs[end]);
2231 for (i = start; i < end; ++i)
2232 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2235 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
2237 overall.x = qMin(overall.x, gm.x);
2238 overall.y = qMin(overall.y, gm.y);
2239 overall.width = overall.xoff + gm.width;
2240 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
2241 qMin(overall.y, gm.y);
2242 overall.xoff += gm.xoff;
2243 overall.yoff += gm.yoff;
2246 const int hi = which << 24;
2247 for (i = start; i < end; ++i)
2248 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2256 for (i = start; i < end; ++i)
2257 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2260 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
2262 overall.x = qMin(overall.x, gm.x);
2263 overall.y = qMin(overall.y, gm.y);
2264 overall.width = overall.xoff + gm.width;
2265 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
2266 qMin(overall.y, gm.y);
2267 overall.xoff += gm.xoff;
2268 overall.yoff += gm.yoff;
2271 const int hi = which << 24;
2272 for (i = start; i < end; ++i)
2273 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2285void QFontEngineMulti::addOutlineToPath(qreal x, qreal y,
const QGlyphLayout &glyphs,
2286 QPainterPath *path, QTextItem::RenderFlags flags)
2288 if (glyphs.numGlyphs <= 0)
2291 int which = highByte(glyphs.glyphs[0]);
2294 if (flags & QTextItem::RightToLeft) {
2295 for (
int gl = 0; gl < glyphs.numGlyphs; gl++)
2296 x += glyphs.advances[gl].toReal();
2298 for (end = 0; end < glyphs.numGlyphs; ++end) {
2299 const int e = highByte(glyphs.glyphs[end]);
2303 if (flags & QTextItem::RightToLeft) {
2304 for (i = start; i < end; ++i)
2305 x -= glyphs.advances[i].toReal();
2309 for (i = start; i < end; ++i)
2310 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2311 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
2313 const int hi = which << 24;
2314 for (i = start; i < end; ++i)
2315 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2317 if (!(flags & QTextItem::RightToLeft)) {
2318 for (i = start; i < end; ++i)
2319 x += glyphs.advances[i].toReal();
2327 if (flags & QTextItem::RightToLeft) {
2328 for (i = start; i < end; ++i)
2329 x -= glyphs.advances[i].toReal();
2333 for (i = start; i < end; ++i)
2334 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2336 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
2339 const int hi = which << 24;
2340 for (i = start; i < end; ++i)
2341 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2344void QFontEngineMulti::recalcAdvances(QGlyphLayout *glyphs, QFontEngine::ShaperFlags flags)
const
2346 if (glyphs->numGlyphs <= 0)
2349 int which = highByte(glyphs->glyphs[0]);
2352 for (end = 0; end < glyphs->numGlyphs; ++end) {
2353 const int e = highByte(glyphs->glyphs[end]);
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)->recalcAdvances(&offs, flags);
2365 const int hi = which << 24;
2366 for (i = start; i < end; ++i)
2367 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2375 for (i = start; i < end; ++i)
2376 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2378 QGlyphLayout offs = glyphs->mid(start, end - start);
2379 engine(which)->recalcAdvances(&offs, flags);
2382 const int hi = which << 24;
2383 for (i = start; i < end; ++i)
2384 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2387void QFontEngineMulti::doKerning(QGlyphLayout *glyphs, QFontEngine::ShaperFlags flags)
const
2389 if (glyphs->numGlyphs <= 0)
2392 int which = highByte(glyphs->glyphs[0]);
2395 for (end = 0; end < glyphs->numGlyphs; ++end) {
2396 const int e = highByte(glyphs->glyphs[end]);
2401 for (i = start; i < end; ++i)
2402 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2404 QGlyphLayout offs = glyphs->mid(start, end - start);
2405 engine(which)->doKerning(&offs, flags);
2408 const int hi = which << 24;
2409 for (i = start; i < end; ++i)
2410 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2418 for (i = start; i < end; ++i)
2419 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2421 QGlyphLayout offs = glyphs->mid(start, end - start);
2422 engine(which)->doKerning(&offs, flags);
2425 const int hi = which << 24;
2426 for (i = start; i < end; ++i)
2427 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2561QFontEngine *QFontEngineMulti::createMultiFontEngine(QFontEngine *fe,
int script)
2563 QFontEngine *engine =
nullptr;
2564 QFontCache::Key key(fe->fontDef, script,
true);
2565 QFontCache *fc = QFontCache::instance();
2571 const bool faceIsLocal = !fe->faceId().filename.isEmpty();
2572 QFontCache::EngineCache::Iterator it = fc->engineCache.find(key),
2573 end = fc->engineCache.end();
2574 while (it != end && it.key() == key) {
2575 Q_ASSERT(it.value().data->type() == QFontEngine::Multi);
2576 QFontEngineMulti *cachedEngine =
static_cast<QFontEngineMulti *>(it.value().data);
2577 if (fe == cachedEngine->engine(0) || (faceIsLocal && fe->faceId().filename == cachedEngine->engine(0)->faceId().filename)) {
2578 engine = cachedEngine;
2579 fc->updateHitCountAndTimeStamp(it.value());
2585 engine = QGuiApplicationPrivate::instance()->platformIntegration()->fontDatabase()->fontEngineMulti(fe, QFontDatabasePrivate::ExtendedScript(script));
2586 fc->insertEngine(key, engine, !faceIsLocal);