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 = qreal(std::numeric_limits<
int>::max() >> 6) / fontDef.pixelSize;
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 = qreal(std::numeric_limits<
int>::max() >> 6) / fontDef.pixelSize;
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 = qreal(std::numeric_limits<
int>::max() >> 6) / fontDef.pixelSize;
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);
1159void QFontEngine::doKerning(QGlyphLayout *glyphs, QFontEngine::ShaperFlags flags)
const
1161 int numPairs = kerning_pairs.size();
1165 const KernPair *pairs = kerning_pairs.constData();
1167 if (flags & DesignMetrics) {
1168 for(
int i = 0; i < glyphs->numGlyphs - 1; ++i)
1169 glyphs->advances[i] += kerning(glyphs->glyphs[i], glyphs->glyphs[i+1] , pairs, numPairs);
1171 for(
int i = 0; i < glyphs->numGlyphs - 1; ++i)
1172 glyphs->advances[i] += qRound(kerning(glyphs->glyphs[i], glyphs->glyphs[i+1] , pairs, numPairs));
1176void QFontEngine::loadKerningPairs(QFixed scalingFactor)
1178 kerning_pairs.clear();
1180 QByteArray tab = getSfntTable(QFont::Tag(
"kern").value());
1184 const uchar *table =
reinterpret_cast<
const uchar *>(tab.constData());
1185 const uchar *end = table + tab.size();
1188 if (!qSafeFromBigEndian(table, end, &version))
1197 if (!qSafeFromBigEndian(table + 2, end, &numTables))
1202 for(
int i = 0; i < numTables; ++i) {
1203 const uchar *header = table + offset;
1206 if (!qSafeFromBigEndian(header, end, &version))
1210 if (!qSafeFromBigEndian(header + 2, end, &length))
1214 if (!qSafeFromBigEndian(header + 4, end, &coverage))
1218 if (version == 0 && coverage == 0x0001) {
1219 if (offset + length > tab.size()) {
1223 const uchar *data = table + offset + 6;
1226 if (!qSafeFromBigEndian(data, end, &nPairs))
1229 if (nPairs * 6 + 8 > length - 6) {
1236 for(
int i = 0; i < nPairs; ++i) {
1237 QFontEngine::KernPair p;
1240 if (!qSafeFromBigEndian(data + off, end, &tmp))
1243 p.left_right = uint(tmp) << 16;
1244 if (!qSafeFromBigEndian(data + off + 2, end, &tmp))
1247 p.left_right |= tmp;
1249 if (!qSafeFromBigEndian(data + off + 4, end, &tmp))
1252 p.adjust = QFixed(
int(
short(tmp))) / scalingFactor;
1253 kerning_pairs.append(p);
1261 std::sort(kerning_pairs.begin(), kerning_pairs.end());
1286const uchar *QFontEngine::getCMap(
const uchar *table, uint tableSize,
bool *isSymbolFont,
int *cmapSize)
1288 const uchar *header = table;
1289 const uchar *endPtr = table + tableSize;
1293 if (!qSafeFromBigEndian(header, endPtr, &version) || version != 0)
1297 if (!qSafeFromBigEndian(header + 2, endPtr, &numTables))
1300 const uchar *maps = table + 4;
1309 MicrosoftUnicodeExtended
1312 int symbolTable = -1;
1313 int tableToUse = -1;
1314 int score = Invalid;
1315 for (
int n = 0; n < numTables; ++n) {
1316 quint16 platformId = 0;
1317 if (!qSafeFromBigEndian(maps + 8 * n, endPtr, &platformId))
1320 quint16 platformSpecificId = 0;
1321 if (!qSafeFromBigEndian(maps + 8 * n + 2, endPtr, &platformSpecificId))
1324 switch (platformId) {
1326 if (score < Unicode &&
1327 (platformSpecificId == 0 ||
1328 platformSpecificId == 2 ||
1329 platformSpecificId == 3)) {
1332 }
else if (score < Unicode11 && platformSpecificId == 1) {
1338 if (score < AppleRoman && platformSpecificId == 0) {
1344 switch (platformSpecificId) {
1347 if (score < Symbol) {
1353 if (score < MicrosoftUnicode) {
1355 score = MicrosoftUnicode;
1359 if (score < MicrosoftUnicodeExtended) {
1361 score = MicrosoftUnicodeExtended;
1376 *isSymbolFont = (symbolTable > -1);
1378 quint32 unicode_table = 0;
1379 if (!qSafeFromBigEndian(maps + 8 * tableToUse + 4, endPtr, &unicode_table))
1386 header = table + unicode_table;
1389 if (!qSafeFromBigEndian(header, endPtr, &format))
1395 if (!qSafeFromBigEndian(header + 2, endPtr, &tmp))
1399 if (!qSafeFromBigEndian(header + 4, endPtr, &length))
1403 if (table + unicode_table + length > endPtr)
1410 if (symbolTable > -1 && ((score == Unicode) || (score == Unicode11))) {
1411 const uchar *selectedTable = table + unicode_table;
1414 bool unicodeTableHasLatin1 =
false;
1415 for (
int uc=0x00; uc<0x100; ++uc) {
1416 if (getTrueTypeGlyphIndex(selectedTable, length, uc) != 0) {
1417 unicodeTableHasLatin1 =
true;
1423 bool unicodeTableHasSymbols =
false;
1424 if (!unicodeTableHasLatin1) {
1425 for (
int uc=0xf000; uc<0xf100; ++uc) {
1426 if (getTrueTypeGlyphIndex(selectedTable, length, uc) != 0) {
1427 unicodeTableHasSymbols =
true;
1434 if (!unicodeTableHasLatin1 && unicodeTableHasSymbols) {
1435 tableToUse = symbolTable;
1441 return table + unicode_table;
1444quint32 QFontEngine::getTrueTypeGlyphIndex(
const uchar *cmap,
int cmapSize, uint unicode)
1446 const uchar *end = cmap + cmapSize;
1448 if (!qSafeFromBigEndian(cmap, end, &format))
1452 const uchar *ptr = cmap + 6 + unicode;
1453 if (unicode < 256 && ptr < end)
1454 return quint32(*ptr);
1455 }
else if (format == 4) {
1457
1458
1459
1460
1461 if (unicode >= 0xffff)
1464 quint16 segCountX2 = 0;
1465 if (!qSafeFromBigEndian(cmap + 6, end, &segCountX2))
1468 const unsigned char *ends = cmap + 14;
1471 for (; i < segCountX2/2; ++i) {
1472 quint16 codePoint = 0;
1473 if (!qSafeFromBigEndian(ends + 2 * i, end, &codePoint))
1475 if (codePoint >= unicode)
1479 const unsigned char *idx = ends + segCountX2 + 2 + 2*i;
1481 quint16 startIndex = 0;
1482 if (!qSafeFromBigEndian(idx, end, &startIndex))
1484 if (startIndex > unicode)
1490 if (!qSafeFromBigEndian(idx, end, &tmp))
1492 qint16 idDelta = qint16(tmp);
1496 quint16 idRangeoffset_t = 0;
1497 if (!qSafeFromBigEndian(idx, end, &idRangeoffset_t))
1500 quint16 glyphIndex = 0;
1501 if (idRangeoffset_t) {
1503 if (!qSafeFromBigEndian(idRangeoffset_t + 2 * (unicode - startIndex) + idx, end, &id))
1507 glyphIndex = (idDelta + id) % 0x10000;
1511 glyphIndex = (idDelta + unicode) % 0x10000;
1514 }
else if (format == 6) {
1515 quint16 tableSize = 0;
1516 if (!qSafeFromBigEndian(cmap + 2, end, &tableSize))
1519 quint16 firstCode6 = 0;
1520 if (!qSafeFromBigEndian(cmap + 6, end, &firstCode6))
1522 if (unicode < firstCode6)
1525 quint16 entryCount6 = 0;
1526 if (!qSafeFromBigEndian(cmap + 8, end, &entryCount6))
1528 if (entryCount6 * 2 + 10 > tableSize)
1531 quint16 sentinel6 = firstCode6 + entryCount6;
1532 if (unicode >= sentinel6)
1535 quint16 entryIndex6 = unicode - firstCode6;
1538 qSafeFromBigEndian(cmap + 10 + (entryIndex6 * 2), end, &index);
1540 }
else if (format == 12) {
1541 quint32 nGroups = 0;
1542 if (!qSafeFromBigEndian(cmap + 12, end, &nGroups))
1547 int left = 0, right = nGroups - 1;
1548 while (left <= right) {
1549 int middle = left + ( ( right - left ) >> 1 );
1551 quint32 startCharCode = 0;
1552 if (!qSafeFromBigEndian(cmap + 12 * middle, end, &startCharCode))
1555 if (unicode < startCharCode)
1558 quint32 endCharCode = 0;
1559 if (!qSafeFromBigEndian(cmap + 12 * middle + 4, end, &endCharCode))
1562 if (unicode <= endCharCode) {
1564 if (!qSafeFromBigEndian(cmap + 12 * middle + 8, end, &index))
1567 return index + unicode - startCharCode;
1573 qDebug(
"cmap table of format %d not implemented", format);
1689int QFontEngineBox::stringToCMap(
const QChar *str,
int len, QGlyphLayout *glyphs,
int *nglyphs, QFontEngine::ShaperFlags flags)
const
1691 Q_ASSERT(glyphs->numGlyphs >= *nglyphs);
1692 if (*nglyphs < len) {
1698 QStringIterator it(str, str + len);
1699 while (it.hasNext()) {
1701 glyphs->glyphs[ucs4Length++] = 1;
1704 *nglyphs = ucs4Length;
1705 glyphs->numGlyphs = ucs4Length;
1707 if (!(flags & GlyphIndicesOnly))
1708 recalcAdvances(glyphs, flags);
1719void QFontEngineBox::addOutlineToPath(qreal x, qreal y,
const QGlyphLayout &glyphs, QPainterPath *path, QTextItem::RenderFlags flags)
1721 if (!glyphs.numGlyphs)
1724 QVarLengthArray<QFixedPoint> positions;
1725 QVarLengthArray<glyph_t> positioned_glyphs;
1726 QTransform matrix = QTransform::fromTranslate(x, y - _size);
1727 getGlyphPositions(glyphs, matrix, flags, positioned_glyphs, positions);
1729 QSize s(_size - 3, _size - 3);
1730 for (
int k = 0; k < positions.size(); k++)
1731 path->addRect(QRectF(positions[k].toPointF(), s));
1743void QFontEngineBox::draw(QPaintEngine *p, qreal x, qreal y,
const QTextItemInt &ti)
1745 if (!ti.glyphs.numGlyphs)
1749 QSize s(_size - 3, _size - 3);
1751 QVarLengthArray<QFixedPoint> positions;
1752 QVarLengthArray<glyph_t> glyphs;
1753 QTransform matrix = QTransform::fromTranslate(x, y - _size);
1754 ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
1755 if (glyphs.size() == 0)
1759 QPainter *painter = p->painter();
1761 painter->setBrush(Qt::NoBrush);
1762 QPen pen = painter->pen();
1763 pen.setWidthF(lineThickness().toReal());
1764 painter->setPen(pen);
1765 for (
int k = 0; k < positions.size(); k++)
1766 painter->drawRect(QRectF(positions[k].toPointF(), s));
1838QFontEngineMulti::QFontEngineMulti(QFontEngine *engine,
int script,
const QStringList &fallbackFamilies)
1839 : QFontEngine(Multi),
1840 m_fallbackFamilies(fallbackFamilies),
1842 m_fallbackFamiliesQueried(!m_fallbackFamilies.isEmpty())
1844 Q_ASSERT(engine && engine->type() != QFontEngine::Multi);
1846 if (m_fallbackFamilies.isEmpty()) {
1848 m_fallbackFamilies << QString();
1851 m_engines.resize(m_fallbackFamilies.size() + 1);
1854 m_engines[0] = engine;
1856 fontDef = engine->fontDef;
1857 cache_cost = engine->cache_cost;
1942glyph_t QFontEngineMulti::glyphIndex(uint ucs4)
const
1944 glyph_t glyph = engine(0)->glyphIndex(ucs4);
1945 if (glyph == 0 && !isIgnorableChar(ucs4)) {
1946 if (!m_fallbackFamiliesQueried)
1947 const_cast<QFontEngineMulti *>(
this)->ensureFallbackFamiliesQueried();
1948 for (
int x = 1, n = qMin(m_engines.size(), 256); x < n; ++x) {
1949 QFontEngine *engine = m_engines.at(x);
1951 if (!shouldLoadFontEngineForCharacter(x, ucs4))
1953 const_cast<QFontEngineMulti *>(
this)->ensureEngineAt(x);
1954 engine = m_engines.at(x);
1956 Q_ASSERT(engine !=
nullptr);
1957 if (engine->type() == Box)
1960 glyph = engine->glyphIndex(ucs4);
1984int QFontEngineMulti::stringToCMap(
const QChar *str,
int len,
1985 QGlyphLayout *glyphs,
int *nglyphs,
1986 QFontEngine::ShaperFlags flags)
const
1988 const int originalNumGlyphs = glyphs->numGlyphs;
1989 int mappedGlyphCount = engine(0)->stringToCMap(str, len, glyphs, nglyphs, flags);
1990 if (mappedGlyphCount < 0)
1995 bool contextFontMerging = mappedGlyphCount < *nglyphs && (fontDef.styleStrategy & QFont::ContextFontMerging);
1996 if (contextFontMerging) {
1997 QVarLengthGlyphLayoutArray tempLayout(len);
1998 if (!m_fallbackFamiliesQueried)
1999 const_cast<QFontEngineMulti *>(
this)->ensureFallbackFamiliesQueried();
2001 int maxGlyphCount = 0;
2002 uchar engineIndex = 0;
2003 for (
int x = 1, n = qMin(m_engines.size(), 256); x < n; ++x) {
2004 int numGlyphs = len;
2005 const_cast<QFontEngineMulti *>(
this)->ensureEngineAt(x);
2006 maxGlyphCount = engine(x)->stringToCMap(str, len, &tempLayout, &numGlyphs, flags);
2009 if (maxGlyphCount > mappedGlyphCount) {
2010 *nglyphs = numGlyphs;
2011 glyphs->numGlyphs = originalNumGlyphs;
2012 glyphs->copy(&tempLayout);
2014 if (maxGlyphCount == numGlyphs)
2019 if (engineIndex > 0) {
2020 for (
int y = 0; y < glyphs->numGlyphs; ++y) {
2021 if (glyphs->glyphs[y] != 0)
2022 glyphs->glyphs[y] |= (engineIndex << 24);
2025 contextFontMerging =
false;
2028 mappedGlyphCount = maxGlyphCount;
2034 QStringIterator it(str, str + len);
2036 const bool enableVariationSelectorHack = disableEmojiSegmenter();
2037 char32_t previousUcs4 = 0;
2039 int lastFallback = -1;
2040 while (it.hasNext()) {
2041 const char32_t ucs4 = it.peekNext();
2046 if (lastFallback >= 0 && (ucs4 == 0x200d || ucs4 == 0x200c)) {
2047 QFontEngine *engine = m_engines.at(lastFallback);
2048 glyph_t glyph = engine->glyphIndex(ucs4);
2050 glyphs->glyphs[glyph_pos] = glyph;
2051 if (!(flags & GlyphIndicesOnly)) {
2052 QGlyphLayout g = glyphs->mid(glyph_pos, 1);
2053 engine->recalcAdvances(&g, flags);
2057 glyphs->glyphs[glyph_pos] |= (lastFallback << 24);
2065 if (glyphs->glyphs[glyph_pos] == 0 && !isIgnorableChar(ucs4)) {
2066 if (!m_fallbackFamiliesQueried)
2067 const_cast<QFontEngineMulti *>(
this)->ensureFallbackFamiliesQueried();
2068 for (
int x = contextFontMerging ? 0 : 1, n = qMin(m_engines.size(), 256); x < n; ++x) {
2069 QFontEngine *engine = m_engines.at(x);
2071 if (!shouldLoadFontEngineForCharacter(x, ucs4))
2073 const_cast<QFontEngineMulti *>(
this)->ensureEngineAt(x);
2074 engine = m_engines.at(x);
2078 Q_ASSERT(engine !=
nullptr);
2079 if (engine->type() == Box)
2082 glyph_t glyph = engine->glyphIndex(ucs4);
2084 glyphs->glyphs[glyph_pos] = glyph;
2085 if (!(flags & GlyphIndicesOnly)) {
2086 QGlyphLayout g = glyphs->mid(glyph_pos, 1);
2087 engine->recalcAdvances(&g, flags);
2093 glyphs->glyphs[glyph_pos] |= (x << 24);
2101 const int variantSelectorBlock = 0xFE00;
2102 if (enableVariationSelectorHack && (ucs4 & 0xFFF0) == variantSelectorBlock && glyph_pos > 0) {
2103 int selectorFontEngine = glyphs->glyphs[glyph_pos] >> 24;
2104 int precedingCharacterFontEngine = glyphs->glyphs[glyph_pos - 1] >> 24;
2106 if (selectorFontEngine != precedingCharacterFontEngine) {
2112 const QFontEngine *selectedEngine = m_engines.at(precedingCharacterFontEngine);
2113 const bool colorFont = selectedEngine->isColorFont();
2114 const char32_t vs15 = 0xFE0E;
2115 const char32_t vs16 = 0xFE0F;
2116 bool adaptVariantSelector = ucs4 < vs15
2117 || (ucs4 == vs15 && colorFont)
2118 || (ucs4 == vs16 && !colorFont);
2120 if (adaptVariantSelector) {
2121 QFontEngine *engine = m_engines.at(selectorFontEngine);
2122 glyph_t glyph = engine->glyphIndex(previousUcs4);
2124 glyphs->glyphs[glyph_pos - 1] = glyph;
2125 if (!(flags & GlyphIndicesOnly)) {
2126 QGlyphLayout g = glyphs->mid(glyph_pos - 1, 1);
2127 engine->recalcAdvances(&g, flags);
2131 glyphs->glyphs[glyph_pos - 1] |= (selectorFontEngine << 24);
2141 previousUcs4 = ucs4;
2144 *nglyphs = glyph_pos;
2145 glyphs->numGlyphs = glyph_pos;
2146 return mappedGlyphCount;
2156glyph_metrics_t QFontEngineMulti::boundingBox(
const QGlyphLayout &glyphs)
2158 if (glyphs.numGlyphs <= 0)
2159 return glyph_metrics_t();
2161 glyph_metrics_t overall;
2163 int which = highByte(glyphs.glyphs[0]);
2166 for (end = 0; end < glyphs.numGlyphs; ++end) {
2167 const int e = highByte(glyphs.glyphs[end]);
2172 for (i = start; i < end; ++i)
2173 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2176 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
2178 overall.x = qMin(overall.x, gm.x);
2179 overall.y = qMin(overall.y, gm.y);
2180 overall.width = overall.xoff + gm.width;
2181 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
2182 qMin(overall.y, gm.y);
2183 overall.xoff += gm.xoff;
2184 overall.yoff += gm.yoff;
2187 const int hi = which << 24;
2188 for (i = start; i < end; ++i)
2189 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2197 for (i = start; i < end; ++i)
2198 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2201 const glyph_metrics_t gm = engine(which)->boundingBox(glyphs.mid(start, end - start));
2203 overall.x = qMin(overall.x, gm.x);
2204 overall.y = qMin(overall.y, gm.y);
2205 overall.width = overall.xoff + gm.width;
2206 overall.height = qMax(overall.height + overall.y, gm.height + gm.y) -
2207 qMin(overall.y, gm.y);
2208 overall.xoff += gm.xoff;
2209 overall.yoff += gm.yoff;
2212 const int hi = which << 24;
2213 for (i = start; i < end; ++i)
2214 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2226void QFontEngineMulti::addOutlineToPath(qreal x, qreal y,
const QGlyphLayout &glyphs,
2227 QPainterPath *path, QTextItem::RenderFlags flags)
2229 if (glyphs.numGlyphs <= 0)
2232 int which = highByte(glyphs.glyphs[0]);
2235 if (flags & QTextItem::RightToLeft) {
2236 for (
int gl = 0; gl < glyphs.numGlyphs; gl++)
2237 x += glyphs.advances[gl].toReal();
2239 for (end = 0; end < glyphs.numGlyphs; ++end) {
2240 const int e = highByte(glyphs.glyphs[end]);
2244 if (flags & QTextItem::RightToLeft) {
2245 for (i = start; i < end; ++i)
2246 x -= glyphs.advances[i].toReal();
2250 for (i = start; i < end; ++i)
2251 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2252 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
2254 const int hi = which << 24;
2255 for (i = start; i < end; ++i)
2256 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2258 if (!(flags & QTextItem::RightToLeft)) {
2259 for (i = start; i < end; ++i)
2260 x += glyphs.advances[i].toReal();
2268 if (flags & QTextItem::RightToLeft) {
2269 for (i = start; i < end; ++i)
2270 x -= glyphs.advances[i].toReal();
2274 for (i = start; i < end; ++i)
2275 glyphs.glyphs[i] = stripped(glyphs.glyphs[i]);
2277 engine(which)->addOutlineToPath(x, y, glyphs.mid(start, end - start), path, flags);
2280 const int hi = which << 24;
2281 for (i = start; i < end; ++i)
2282 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
2285void QFontEngineMulti::recalcAdvances(QGlyphLayout *glyphs, QFontEngine::ShaperFlags flags)
const
2287 if (glyphs->numGlyphs <= 0)
2290 int which = highByte(glyphs->glyphs[0]);
2293 for (end = 0; end < glyphs->numGlyphs; ++end) {
2294 const int e = highByte(glyphs->glyphs[end]);
2299 for (i = start; i < end; ++i)
2300 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2302 QGlyphLayout offs = glyphs->mid(start, end - start);
2303 engine(which)->recalcAdvances(&offs, flags);
2306 const int hi = which << 24;
2307 for (i = start; i < end; ++i)
2308 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2316 for (i = start; i < end; ++i)
2317 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2319 QGlyphLayout offs = glyphs->mid(start, end - start);
2320 engine(which)->recalcAdvances(&offs, flags);
2323 const int hi = which << 24;
2324 for (i = start; i < end; ++i)
2325 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2328void QFontEngineMulti::doKerning(QGlyphLayout *glyphs, QFontEngine::ShaperFlags flags)
const
2330 if (glyphs->numGlyphs <= 0)
2333 int which = highByte(glyphs->glyphs[0]);
2336 for (end = 0; end < glyphs->numGlyphs; ++end) {
2337 const int e = highByte(glyphs->glyphs[end]);
2342 for (i = start; i < end; ++i)
2343 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2345 QGlyphLayout offs = glyphs->mid(start, end - start);
2346 engine(which)->doKerning(&offs, flags);
2349 const int hi = which << 24;
2350 for (i = start; i < end; ++i)
2351 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2359 for (i = start; i < end; ++i)
2360 glyphs->glyphs[i] = stripped(glyphs->glyphs[i]);
2362 QGlyphLayout offs = glyphs->mid(start, end - start);
2363 engine(which)->doKerning(&offs, flags);
2366 const int hi = which << 24;
2367 for (i = start; i < end; ++i)
2368 glyphs->glyphs[i] = hi | glyphs->glyphs[i];
2502QFontEngine *QFontEngineMulti::createMultiFontEngine(QFontEngine *fe,
int script)
2504 QFontEngine *engine =
nullptr;
2505 QFontCache::Key key(fe->fontDef, script,
true);
2506 QFontCache *fc = QFontCache::instance();
2512 const bool faceIsLocal = !fe->faceId().filename.isEmpty();
2513 QFontCache::EngineCache::Iterator it = fc->engineCache.find(key),
2514 end = fc->engineCache.end();
2515 while (it != end && it.key() == key) {
2516 Q_ASSERT(it.value().data->type() == QFontEngine::Multi);
2517 QFontEngineMulti *cachedEngine =
static_cast<QFontEngineMulti *>(it.value().data);
2518 if (fe == cachedEngine->engine(0) || (faceIsLocal && fe->faceId().filename == cachedEngine->engine(0)->faceId().filename)) {
2519 engine = cachedEngine;
2520 fc->updateHitCountAndTimeStamp(it.value());
2526 engine = QGuiApplicationPrivate::instance()->platformIntegration()->fontDatabase()->fontEngineMulti(fe, QFontDatabasePrivate::ExtendedScript(script));
2527 fc->insertEngine(key, engine, !faceIsLocal);