21#include <private/qfontengine_p.h>
22#include <private/qpainter_p.h>
23#include <private/qtextengine_p.h>
26#include <qpa/qplatformscreen.h>
27#include <qpa/qplatformintegration.h>
28#include <qpa/qplatformfontdatabase.h>
29#include <QtGui/private/qguiapplication_p.h>
31#include <QtCore/QMutexLocker>
32#include <QtCore/QMutex>
37#ifdef QFONTCACHE_DEBUG
38# define FC_DEBUG qDebug
40# define FC_DEBUG if (false) qDebug
45#ifndef QFONTCACHE_DECREASE_TRIGGER_LIMIT
46# define QFONTCACHE_DECREASE_TRIGGER_LIMIT 256
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 if (pixelSize != -1 && other.pixelSize != -1) {
70 if (pixelSize != other.pixelSize)
72 }
else if (pointSize != -1 && other.pointSize != -1) {
73 if (pointSize != other.pointSize)
85 QString this_family, this_foundry, other_family, other_foundry;
86 for (
int i = 0; i < families.size(); ++i) {
87 QFontDatabasePrivate::parseFontName(families.at(i), this_foundry, this_family);
88 QFontDatabasePrivate::parseFontName(other.families.at(i), other_foundry, other_family);
89 if (this_family != other_family || this_foundry != other_foundry)
93 if (variableAxisValues != other.variableAxisValues)
96 return (styleHint == other.styleHint
97 && styleStrategy == other.styleStrategy
98 && weight == other.weight
99 && style == other.style
100 && this_family == other_family
101 && (styleName.isEmpty() || other.styleName.isEmpty() || styleName == other.styleName)
102 && (this_foundry.isEmpty()
103 || other_foundry.isEmpty()
104 || this_foundry == other_foundry)
112 if (QCoreApplication::instance()->testAttribute(Qt::AA_Use96Dpi))
118 if (
const QScreen *screen = QGuiApplication::primaryScreen())
119 return qRound(screen->logicalDotsPerInchX());
127 if (QCoreApplication::instance()->testAttribute(Qt::AA_Use96Dpi))
133 if (
const QScreen *screen = QGuiApplication::primaryScreen())
134 return qRound(screen->logicalDotsPerInchY());
142 return qt_defaultDpiY();
148 static constexpr std::array<
int, 2> legacyToOpenTypeMap[] = {
149 { 0, QFont::Thin }, { 12, QFont::ExtraLight }, { 25, QFont::Light },
150 { 50, QFont::Normal }, { 57, QFont::Medium }, { 63, QFont::DemiBold },
151 { 75, QFont::Bold }, { 81, QFont::ExtraBold }, { 87, QFont::Black },
154 int closestDist = INT_MAX;
158 for (
auto mapping : legacyToOpenTypeMap) {
159 const int weightOld = mapping[ inverted];
160 const int weightNew = mapping[!inverted];
161 const int dist = qAbs(weightOld - weight);
162 if (dist < closestDist) {
177 QStringList familyList;
178 if (family.isEmpty())
180 const auto list = QStringView{family}.split(u',');
181 const int numFamilies = list.size();
182 familyList.reserve(numFamilies);
183 for (
int i = 0; i < numFamilies; ++i) {
184 auto str = list.at(i).trimmed();
185 if ((str.startsWith(u'"') && str.endsWith(u'"'))
186 || (str.startsWith(u'\'') && str.endsWith(u'\''))) {
187 str = str.mid(1, str.size() - 2);
189 familyList << str.toString();
206QFontPrivate::QFontPrivate()
207 : engineData(
nullptr), dpi(qt_defaultDpi()),
208 underline(
false), overline(
false), strikeOut(
false), kerning(
true),
209 capital(0), letterSpacingIsAbsolute(
false), scFont(
nullptr)
213QFontPrivate::QFontPrivate(
const QFontPrivate &other)
214 : request(other.request), engineData(
nullptr), dpi(other.dpi),
215 underline(other.underline), overline(other.overline),
216 strikeOut(other.strikeOut), kerning(other.kerning),
217 capital(other.capital), letterSpacingIsAbsolute(other.letterSpacingIsAbsolute),
218 letterSpacing(other.letterSpacing), wordSpacing(other.wordSpacing),
219 features(other.features), scFont(other.scFont)
221 if (scFont && scFont !=
this)
225QFontPrivate::~QFontPrivate()
227 if (engineData && !engineData->ref.deref())
229 engineData =
nullptr;
230 if (scFont && scFont !=
this) {
231 if (!scFont->ref.deref())
239#define QT_FONT_ENGINE_FROM_DATA(data, script) data->engines[script]
241QFontEngine *QFontPrivate::engineForScript(
int script)
const
243 QMutexLocker locker(qt_fontdatabase_mutex());
244 if (script <= QChar::Script_Latin)
245 script = QChar::Script_Common;
246 if (engineData && engineData->fontCacheId != QFontCache::instance()->id()) {
248 if (!engineData->ref.deref())
250 engineData =
nullptr;
253 QFontDatabasePrivate::load(
this, script);
257void QFontPrivate::alterCharForCapitalization(QChar &c)
const {
259 case QFont::AllUppercase:
260 case QFont::SmallCaps:
263 case QFont::AllLowercase:
266 case QFont::MixedCase:
271QFontPrivate *QFontPrivate::smallCapsFontPrivate()
const
275 QFont font(
const_cast<QFontPrivate *>(
this));
276 qreal pointSize = font.pointSizeF();
278 font.setPointSizeF(pointSize * .7);
280 font.setPixelSize((font.pixelSize() * 7 + 5) / 10);
281 scFont = font.d.data();
288void QFontPrivate::resolve(uint mask,
const QFontPrivate *other)
290 Q_ASSERT(other !=
nullptr);
294 if ((mask & QFont::AllPropertiesResolved) == QFont::AllPropertiesResolved)
return;
297 if (!(mask & QFont::FamiliesResolved))
298 request.families = other->request.families;
300 if (! (mask & QFont::StyleNameResolved))
301 request.styleName = other->request.styleName;
303 if (! (mask & QFont::SizeResolved)) {
304 request.pointSize = other->request.pointSize;
305 request.pixelSize = other->request.pixelSize;
308 if (! (mask & QFont::StyleHintResolved))
309 request.styleHint = other->request.styleHint;
311 if (! (mask & QFont::StyleStrategyResolved))
312 request.styleStrategy = other->request.styleStrategy;
314 if (! (mask & QFont::WeightResolved))
315 request.weight = other->request.weight;
317 if (! (mask & QFont::StyleResolved))
318 request.style = other->request.style;
320 if (! (mask & QFont::FixedPitchResolved))
321 request.fixedPitch = other->request.fixedPitch;
323 if (! (mask & QFont::StretchResolved))
324 request.stretch = other->request.stretch;
326 if (! (mask & QFont::HintingPreferenceResolved))
327 request.hintingPreference = other->request.hintingPreference;
329 if (! (mask & QFont::UnderlineResolved))
330 underline = other->underline;
332 if (! (mask & QFont::OverlineResolved))
333 overline = other->overline;
335 if (! (mask & QFont::StrikeOutResolved))
336 strikeOut = other->strikeOut;
338 if (! (mask & QFont::KerningResolved))
339 kerning = other->kerning;
341 if (! (mask & QFont::LetterSpacingResolved)) {
342 letterSpacing = other->letterSpacing;
343 letterSpacingIsAbsolute = other->letterSpacingIsAbsolute;
345 if (! (mask & QFont::WordSpacingResolved))
346 wordSpacing = other->wordSpacing;
347 if (! (mask & QFont::CapitalizationResolved))
348 capital = other->capital;
350 if (!(mask & QFont::FeaturesResolved))
351 features = other->features;
353 if (!(mask & QFont::VariableAxesResolved))
354 request.variableAxisValues = other->request.variableAxisValues;
357bool QFontPrivate::hasVariableAxis(QFont::Tag tag,
float value)
const
359 return request.variableAxisValues.contains(tag) && request.variableAxisValues.value(tag) == value;
362void QFontPrivate::setVariableAxis(QFont::Tag tag,
float value)
364 request.variableAxisValues.insert(tag, value);
367void QFontPrivate::unsetVariableAxis(QFont::Tag tag)
369 request.variableAxisValues.remove(tag);
372void QFontPrivate::setFeature(QFont::Tag tag, quint32 value)
374 features.insert(tag, value);
377void QFontPrivate::unsetFeature(QFont::Tag tag)
379 features.remove(tag);
386 memset(engines, 0, QFontDatabasePrivate::ScriptCount *
sizeof(QFontEngine *));
391 Q_ASSERT(ref.loadRelaxed() == 0);
392 for (
int i = 0; i < QFontDatabasePrivate::ScriptCount; ++i) {
394 if (!engines[i]->ref.deref())
396 engines[i] =
nullptr;
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
594
595
596
597
598
599
600
601
602
603
604
605
606
607
610
611
612
613
614
615
618
619
620
621QFont::QFont(
const QFont &font,
const QPaintDevice *pd)
622 : resolve_mask(font.resolve_mask)
625 const int dpi = pd->logicalDpiY();
626 if (font.d->dpi != dpi) {
627 d =
new QFontPrivate(*font.d);
635
636
637QFont::QFont(QFontPrivate *data)
638 : d(data), resolve_mask(QFont::AllPropertiesResolved)
643
644
647 if (d->ref.loadRelaxed() == 1) {
648 if (d->engineData && !d->engineData->ref.deref())
649 delete d->engineData;
650 d->engineData =
nullptr;
651 if (d->scFont && d->scFont != d.data()) {
652 if (!d->scFont->ref.deref())
663
664
665
666
667
668void QFontPrivate::detachButKeepEngineData(QFont *font)
670 if (font->d->ref.loadRelaxed() == 1)
673 QFontEngineData *engineData = font->d->engineData;
675 engineData->ref.ref();
677 font->d->engineData = engineData;
681
682
683
684
686 : d(QGuiApplicationPrivate::instance() ? QGuiApplication::font().d.data() :
new QFontPrivate()), resolve_mask(0)
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712QFont::QFont(
const QString &family,
int pointSize,
int weight,
bool italic)
713 : d(
new QFontPrivate()), resolve_mask(QFont::FamiliesResolved)
715 if (pointSize <= 0) {
718 resolve_mask |= QFont::SizeResolved;
724 resolve_mask |= QFont::WeightResolved | QFont::StyleResolved;
728 resolve_mask |= QFont::StyleResolved;
730 d->request.families = splitIntoFamilies(family);
731 d->request.pointSize = qreal(pointSize);
732 d->request.pixelSize = -1;
733 d->request.weight = weight;
734 d->request.style = italic ? QFont::StyleItalic : QFont::StyleNormal;
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755QFont::QFont(
const QStringList &families,
int pointSize,
int weight,
bool italic)
756 : d(
new QFontPrivate()), resolve_mask(QFont::FamiliesResolved)
761 resolve_mask |= QFont::SizeResolved;
766 resolve_mask |= QFont::WeightResolved | QFont::StyleResolved;
769 resolve_mask |= QFont::StyleResolved;
771 d->request.families = families;
772 d->request.pointSize = qreal(pointSize);
773 d->request.pixelSize = -1;
774 d->request.weight = weight;
775 d->request.style = italic ? QFont::StyleItalic : QFont::StyleNormal;
779
780
781QFont::QFont(
const QFont &font)
782 : d(font.d), resolve_mask(font.resolve_mask)
787
788
794
795
796QFont &QFont::operator=(
const QFont &font)
799 resolve_mask = font.resolve_mask;
804
805
806
807
810
811
812
813
814
815QString QFont::family()
const
817 return d->request.families.isEmpty() ? QString() : d->request.families.constFirst();
821
822
823
824
825
826
827
828
829
830
831
832
833void QFont::setFamily(
const QString &family)
835 setFamilies(QStringList(family));
839
840
841
842
843
844
845
846
847QString QFont::styleName()
const
849 return d->request.styleName;
853
854
855
856
857
858
859
860
861
862
863
864
865void QFont::setStyleName(
const QString &styleName)
867 if ((resolve_mask & QFont::StyleNameResolved) && d->request.styleName == styleName)
872 d->request.styleName = styleName;
873 resolve_mask |= QFont::StyleNameResolved;
877
878
879
880
881
882int QFont::pointSize()
const
884 return qRound(d->request.pointSize);
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
946
947
948
949
950
951
952
953
954
955void QFont::setHintingPreference(HintingPreference hintingPreference)
957 if ((resolve_mask & QFont::HintingPreferenceResolved) && d->request.hintingPreference == hintingPreference)
962 d->request.hintingPreference = hintingPreference;
964 resolve_mask |= QFont::HintingPreferenceResolved;
968
969
970
971
972QFont::HintingPreference QFont::hintingPreference()
const
974 return QFont::HintingPreference(d->request.hintingPreference);
978
979
980
981
982
983void QFont::setPointSize(
int pointSize)
985 if (pointSize <= 0) {
986 qWarning(
"QFont::setPointSize: Point size <= 0 (%d), must be greater than 0", pointSize);
990 if ((resolve_mask & QFont::SizeResolved) && d->request.pointSize == qreal(pointSize))
995 d->request.pointSize = qreal(pointSize);
996 d->request.pixelSize = -1;
998 resolve_mask |= QFont::SizeResolved;
1002
1003
1004
1005
1006
1007
1008void QFont::setPointSizeF(qreal pointSize)
1010 if (pointSize <= 0) {
1011 qWarning(
"QFont::setPointSizeF: Point size <= 0 (%f), must be greater than 0", pointSize);
1015 if ((resolve_mask & QFont::SizeResolved) && d->request.pointSize == pointSize)
1020 d->request.pointSize = pointSize;
1021 d->request.pixelSize = -1;
1023 resolve_mask |= QFont::SizeResolved;
1027
1028
1029
1030
1031
1032qreal QFont::pointSizeF()
const
1034 return d->request.pointSize;
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047void QFont::setPixelSize(
int pixelSize)
1049 if (pixelSize <= 0) {
1050 qWarning(
"QFont::setPixelSize: Pixel size <= 0 (%d)", pixelSize);
1054 if ((resolve_mask & QFont::SizeResolved) && d->request.pixelSize == qreal(pixelSize))
1059 d->request.pixelSize = pixelSize;
1060 d->request.pointSize = -1;
1062 resolve_mask |= QFont::SizeResolved;
1066
1067
1068
1069
1070
1071
1072int QFont::pixelSize()
const
1074 return d->request.pixelSize;
1078
1079
1080
1081
1082
1083
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1099
1100
1101
1102
1103QFont::Style QFont::style()
const
1105 return (QFont::Style)d->request.style;
1110
1111
1112
1113
1114void QFont::setStyle(Style style)
1116 if ((resolve_mask & QFont::StyleResolved) && d->request.style == style)
1121 d->request.style = style;
1122 resolve_mask |= QFont::StyleResolved;
1126
1127
1128
1129
1130
1131QFont::Weight QFont::weight()
const
1133 return static_cast<Weight>(d->request.weight);
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1155#if QT_DEPRECATED_SINCE(6
, 0
)
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171void QFont::setLegacyWeight(
int legacyWeight)
1173 setWeight(QFont::Weight(qt_legacyToOpenTypeWeight(legacyWeight)));
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189int QFont::legacyWeight()
const
1191 return qt_openTypeToLegacyWeight(weight());
1196
1197
1198
1199
1200
1201
1202
1203void QFont::setWeight(QFont::Weight weight)
1206 if (weightValue !=
static_cast<
int>(weight)) {
1207 qWarning() <<
"QFont::setWeight: Weight must be between 1 and 1000, attempted to set "
1208 <<
static_cast<
int>(weight);
1211 if ((resolve_mask & QFont::WeightResolved) && d->request.weight == weightValue)
1216 d->request.weight = weightValue;
1217 resolve_mask |= QFont::WeightResolved;
1221
1222
1223
1224
1225
1226
1227
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1245
1246
1247
1248
1249bool QFont::underline()
const
1251 return d->underline;
1255
1256
1257
1258
1259
1260void QFont::setUnderline(
bool enable)
1262 if ((resolve_mask & QFont::UnderlineResolved) && d->underline == enable)
1265 QFontPrivate::detachButKeepEngineData(
this);
1267 d->underline = enable;
1268 resolve_mask |= QFont::UnderlineResolved;
1272
1273
1274
1275
1276bool QFont::overline()
const
1282
1283
1284
1285
1286void QFont::setOverline(
bool enable)
1288 if ((resolve_mask & QFont::OverlineResolved) && d->overline == enable)
1291 QFontPrivate::detachButKeepEngineData(
this);
1293 d->overline = enable;
1294 resolve_mask |= QFont::OverlineResolved;
1298
1299
1300
1301
1302bool QFont::strikeOut()
const
1304 return d->strikeOut;
1308
1309
1310
1311
1312
1313void QFont::setStrikeOut(
bool enable)
1315 if ((resolve_mask & QFont::StrikeOutResolved) && d->strikeOut == enable)
1318 QFontPrivate::detachButKeepEngineData(
this);
1320 d->strikeOut = enable;
1321 resolve_mask |= QFont::StrikeOutResolved;
1325
1326
1327
1328
1329bool QFont::fixedPitch()
const
1331 return d->request.fixedPitch;
1335
1336
1337
1338
1339
1340void QFont::setFixedPitch(
bool enable)
1342 if ((resolve_mask & QFont::FixedPitchResolved) && d->request.fixedPitch == enable)
1347 d->request.fixedPitch = enable;
1348 d->request.ignorePitch =
false;
1349 resolve_mask |= QFont::FixedPitchResolved;
1353
1354
1355
1356
1357bool QFont::kerning()
const
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373void QFont::setKerning(
bool enable)
1375 if ((resolve_mask & QFont::KerningResolved) && d->kerning == enable)
1378 QFontPrivate::detachButKeepEngineData(
this);
1380 d->kerning = enable;
1381 resolve_mask |= QFont::KerningResolved;
1385
1386
1387
1388
1389
1390
1391
1392QFont::StyleStrategy QFont::styleStrategy()
const
1394 return (StyleStrategy) d->request.styleStrategy;
1398
1399
1400
1401
1402
1403
1404
1405QFont::StyleHint QFont::styleHint()
const
1407 return (StyleHint) d->request.styleHint;
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520void QFont::setStyleHint(StyleHint hint, StyleStrategy strategy)
1522 if ((resolve_mask & (QFont::StyleHintResolved | QFont::StyleStrategyResolved)) &&
1523 (StyleHint) d->request.styleHint == hint &&
1524 (StyleStrategy) d->request.styleStrategy == strategy)
1529 d->request.styleHint = hint;
1530 d->request.styleStrategy = strategy;
1531 resolve_mask |= QFont::StyleHintResolved;
1532 resolve_mask |= QFont::StyleStrategyResolved;
1537
1538
1539
1540
1541void QFont::setStyleStrategy(StyleStrategy s)
1543 if ((resolve_mask & QFont::StyleStrategyResolved) &&
1544 s == (StyleStrategy)d->request.styleStrategy)
1549 d->request.styleStrategy = s;
1550 resolve_mask |= QFont::StyleStrategyResolved;
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1575
1576
1577
1578
1579int QFont::stretch()
const
1581 return d->request.stretch;
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603void QFont::setStretch(
int factor)
1605 if (factor < 0 || factor > 4000) {
1606 qWarning(
"QFont::setStretch: Parameter '%d' out of range", factor);
1610 if ((resolve_mask & QFont::StretchResolved) &&
1611 d->request.stretch == (uint)factor)
1616 d->request.stretch = (uint)factor;
1617 resolve_mask |= QFont::StretchResolved;
1621
1622
1623
1624
1625
1626
1627
1628
1631
1632
1633
1634
1635
1636qreal QFont::letterSpacing()
const
1638 return d->letterSpacing.toReal();
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653void QFont::setLetterSpacing(SpacingType type, qreal spacing)
1655 const QFixed newSpacing = QFixed::fromReal(spacing);
1656 const bool absoluteSpacing = type == AbsoluteSpacing;
1657 if ((resolve_mask & QFont::LetterSpacingResolved) &&
1658 d->letterSpacingIsAbsolute == absoluteSpacing &&
1659 d->letterSpacing == newSpacing)
1662 QFontPrivate::detachButKeepEngineData(
this);
1664 d->letterSpacing = newSpacing;
1665 d->letterSpacingIsAbsolute = absoluteSpacing;
1666 resolve_mask |= QFont::LetterSpacingResolved;
1670
1671
1672
1673
1674
1675QFont::SpacingType QFont::letterSpacingType()
const
1677 return d->letterSpacingIsAbsolute ? AbsoluteSpacing : PercentageSpacing;
1681
1682
1683
1684
1685
1686qreal QFont::wordSpacing()
const
1688 return d->wordSpacing.toReal();
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705void QFont::setWordSpacing(qreal spacing)
1707 const QFixed newSpacing = QFixed::fromReal(spacing);
1708 if ((resolve_mask & QFont::WordSpacingResolved) &&
1709 d->wordSpacing == newSpacing)
1712 QFontPrivate::detachButKeepEngineData(
this);
1714 d->wordSpacing = newSpacing;
1715 resolve_mask |= QFont::WordSpacingResolved;
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1733
1734
1735
1736
1737
1738
1739
1740void QFont::setCapitalization(Capitalization caps)
1742 if ((resolve_mask & QFont::CapitalizationResolved) &&
1743 capitalization() == caps)
1746 QFontPrivate::detachButKeepEngineData(
this);
1749 resolve_mask |= QFont::CapitalizationResolved;
1753
1754
1755
1756
1757
1758QFont::Capitalization QFont::capitalization()
const
1760 return static_cast<QFont::Capitalization> (d->capital);
1764
1765
1766
1767
1768
1769bool QFont::exactMatch()
const
1771 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
1772 Q_ASSERT(engine !=
nullptr);
1773 return d->request.exactMatch(engine->fontDef);
1777
1778
1779
1780
1781
1782
1783
1784
1785bool QFont::operator==(
const QFont &f)
const
1788 || (f.d->request == d->request
1789 && f.d->request.pointSize == d->request.pointSize
1790 && f.d->underline == d->underline
1791 && f.d->overline == d->overline
1792 && f.d->strikeOut == d->strikeOut
1793 && f.d->kerning == d->kerning
1794 && f.d->capital == d->capital
1795 && f.d->letterSpacingIsAbsolute == d->letterSpacingIsAbsolute
1796 && f.d->letterSpacing == d->letterSpacing
1797 && f.d->wordSpacing == d->wordSpacing
1798 && f.d->features == d->features
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814bool QFont::operator<(
const QFont &f)
const
1816 if (f.d == d)
return false;
1818 const QFontDef &r1 = f.d->request;
1819 const QFontDef &r2 = d->request;
1820 if (r1.pointSize != r2.pointSize)
return r1.pointSize < r2.pointSize;
1821 if (r1.pixelSize != r2.pixelSize)
return r1.pixelSize < r2.pixelSize;
1822 if (r1.weight != r2.weight)
return r1.weight < r2.weight;
1823 if (r1.style != r2.style)
return r1.style < r2.style;
1824 if (r1.stretch != r2.stretch)
return r1.stretch < r2.stretch;
1825 if (r1.styleHint != r2.styleHint)
return r1.styleHint < r2.styleHint;
1826 if (r1.styleStrategy != r2.styleStrategy)
return r1.styleStrategy < r2.styleStrategy;
1827 if (r1.families != r2.families)
return r1.families < r2.families;
1828 if (f.d->capital != d->capital)
return f.d->capital < d->capital;
1830 if (f.d->letterSpacingIsAbsolute != d->letterSpacingIsAbsolute)
return f.d->letterSpacingIsAbsolute < d->letterSpacingIsAbsolute;
1831 if (f.d->letterSpacing != d->letterSpacing)
return f.d->letterSpacing < d->letterSpacing;
1832 if (f.d->wordSpacing != d->wordSpacing)
return f.d->wordSpacing < d->wordSpacing;
1834 int f1attrs = (f.d->underline << 3) + (f.d->overline << 2) + (f.d->strikeOut<<1) + f.d->kerning;
1835 int f2attrs = (d->underline << 3) + (d->overline << 2) + (d->strikeOut<<1) + d->kerning;
1836 if (f1attrs != f2attrs)
return f1attrs < f2attrs;
1838 if (d->features.size() != f.d->features.size())
1839 return f.d->features.size() < d->features.size();
1842 auto it = d->features.constBegin();
1843 auto jt = f.d->features.constBegin();
1844 for (; it != d->features.constEnd(); ++it, ++jt) {
1845 if (it.key() != jt.key())
1846 return jt.key() < it.key();
1847 if (it.value() != jt.value())
1848 return jt.value() < it.value();
1852 if (r1.variableAxisValues.size() != r2.variableAxisValues.size())
1853 return r1.variableAxisValues.size() < r2.variableAxisValues.size();
1856 auto it = r1.variableAxisValues.constBegin();
1857 auto jt = r2.variableAxisValues.constBegin();
1858 for (; it != r1.variableAxisValues.constEnd(); ++it, ++jt) {
1859 if (it.key() != jt.key())
1860 return jt.key() < it.key();
1861 if (it.value() != jt.value())
1862 return jt.value() < it.value();
1871
1872
1873
1874
1875
1876
1877
1878
1879bool QFont::operator!=(
const QFont &f)
const
1881 return !(operator==(f));
1885
1886
1887QFont::operator QVariant()
const
1889 return QVariant::fromValue(*
this);
1893
1894
1895
1896
1897
1898
1899bool QFont::isCopyOf(
const QFont & f)
const
1905
1906
1907
1908QFont QFont::resolve(
const QFont &other)
const
1910 if (resolve_mask == 0 || (resolve_mask == other.resolve_mask && *
this == other)) {
1912 o.resolve_mask = resolve_mask;
1918 font.d->resolve(resolve_mask, other.d.data());
1924
1925
1926
1929
1930
1931
1935
1936
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952QString QFont::substitute(
const QString &familyName)
1954 QFontSubst *fontSubst = globalFontSubst();
1955 Q_ASSERT(fontSubst !=
nullptr);
1956 QFontSubst::ConstIterator it = fontSubst->constFind(familyName.toLower());
1957 if (it != fontSubst->constEnd() && !(*it).isEmpty())
1958 return (*it).first();
1965
1966
1967
1968
1969
1970
1971
1972
1973QStringList QFont::substitutes(
const QString &familyName)
1975 QFontSubst *fontSubst = globalFontSubst();
1976 Q_ASSERT(fontSubst !=
nullptr);
1977 return fontSubst->value(familyName.toLower(), QStringList());
1982
1983
1984
1985
1986
1987
1988
1989
1990void QFont::insertSubstitution(
const QString &familyName,
1991 const QString &substituteName)
1993 QFontSubst *fontSubst = globalFontSubst();
1994 Q_ASSERT(fontSubst !=
nullptr);
1995 QStringList &list = (*fontSubst)[familyName.toLower()];
1996 QString s = substituteName.toLower();
1997 if (!list.contains(s))
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012void QFont::insertSubstitutions(
const QString &familyName,
2013 const QStringList &substituteNames)
2015 QFontSubst *fontSubst = globalFontSubst();
2016 Q_ASSERT(fontSubst !=
nullptr);
2017 QStringList &list = (*fontSubst)[familyName.toLower()];
2018 for (
const QString &substituteName : substituteNames) {
2019 const QString lowerSubstituteName = substituteName.toLower();
2020 if (!list.contains(lowerSubstituteName))
2021 list.append(lowerSubstituteName);
2026
2027
2028
2029
2030
2031void QFont::removeSubstitutions(
const QString &familyName)
2033 QFontSubst *fontSubst = globalFontSubst();
2034 Q_ASSERT(fontSubst !=
nullptr);
2035 fontSubst->remove(familyName.toLower());
2039
2040
2041
2042
2043QStringList QFont::substitutions()
2045 QFontSubst *fontSubst = globalFontSubst();
2046 Q_ASSERT(fontSubst !=
nullptr);
2047 QStringList ret = fontSubst->keys();
2053#ifndef QT_NO_DATASTREAM
2055
2056
2057
2060 Q_ASSERT(f !=
nullptr);
2062 if (f->request.style)
2070 if (f->request.fixedPitch)
2074 if (version >= QDataStream::Qt_4_0) {
2078 if (f->request.style == QFont::StyleOblique)
2085 Q_ASSERT(f !=
nullptr);
2087 if (f->request.ignorePitch)
2089 if (f->letterSpacingIsAbsolute)
2095
2096
2097
2100 Q_ASSERT(f !=
nullptr);
2101 f->request.style = (bits & 0x01) != 0 ? QFont::StyleItalic : QFont::StyleNormal;
2102 f->underline = (bits & 0x02) != 0;
2103 f->overline = (bits & 0x40) != 0;
2104 f->strikeOut = (bits & 0x04) != 0;
2105 f->request.fixedPitch = (bits & 0x08) != 0;
2107 if (version >= QDataStream::Qt_4_0)
2108 f->kerning = (bits & 0x10) != 0;
2109 if ((bits & 0x80) != 0)
2110 f->request.style = QFont::StyleOblique;
2115 Q_ASSERT(f !=
nullptr);
2116 f->request.ignorePitch = (bits & 0x01) != 0;
2117 f->letterSpacingIsAbsolute = (bits & 0x02) != 0;
2122
2123
2124
2125
2126
2127QString QFont::key()
const
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158QString QFont::toString()
const
2160 const QChar comma(u',');
2161 QString fontDescription = family() + comma +
2162 QString::number( pointSizeF()) + comma +
2163 QString::number( pixelSize()) + comma +
2164 QString::number((
int) styleHint()) + comma +
2165 QString::number( weight()) + comma +
2166 QString::number((
int) style()) + comma +
2167 QString::number((
int) underline()) + comma +
2168 QString::number((
int) strikeOut()) + comma +
2169 QString::number((
int)fixedPitch()) + comma +
2170 QString::number((
int)
false) + comma +
2171 QString::number((
int)capitalization()) + comma +
2172 QString::number((
int)letterSpacingType()) + comma +
2173 QString::number(letterSpacing()) + comma +
2174 QString::number(wordSpacing()) + comma +
2175 QString::number(stretch()) + comma +
2176 QString::number((
int)styleStrategy());
2178 QString fontStyle = styleName();
2179 if (!fontStyle.isEmpty())
2180 fontDescription += comma + fontStyle;
2182 return fontDescription;
2186
2187
2188
2189
2192 return qHash(QFontPrivate::get(font)->request, seed);
2197
2198
2199
2200
2201
2202
2203bool QFont::fromString(
const QString &descrip)
2205 const auto sr = QStringView(descrip).trimmed();
2206 const auto l = sr.split(u',');
2207 const int count = l.size();
2208 if (!count || (count > 2 && count < 9) || count == 9 || count > 17 ||
2209 l.first().isEmpty()) {
2210 qWarning(
"QFont::fromString: Invalid description '%s'",
2211 descrip.isEmpty() ?
"(empty)" : descrip.toLatin1().data());
2215 setFamily(l[0].toString());
2216 if (count > 1 && l[1].toDouble() > 0.0)
2217 setPointSizeF(l[1].toDouble());
2219 setStyleHint((StyleHint) l[2].toInt());
2220 setWeight(QFont::Weight(l[3].toInt()));
2221 setItalic(l[4].toInt());
2222 setUnderline(l[5].toInt());
2223 setStrikeOut(l[6].toInt());
2224 setFixedPitch(l[7].toInt());
2225 }
else if (count >= 10) {
2226 if (l[2].toInt() > 0)
2227 setPixelSize(l[2].toInt());
2228 setStyleHint((StyleHint) l[3].toInt());
2230 setWeight(QFont::Weight(l[4].toInt()));
2232 setWeight(QFont::Weight(qt_legacyToOpenTypeWeight(l[4].toInt())));
2233 setStyle((QFont::Style)l[5].toInt());
2234 setUnderline(l[6].toInt());
2235 setStrikeOut(l[7].toInt());
2236 setFixedPitch(l[8].toInt());
2238 setCapitalization((Capitalization)l[10].toInt());
2239 setLetterSpacing((SpacingType)l[11].toInt(), l[12].toDouble());
2240 setWordSpacing(l[13].toDouble());
2241 setStretch(l[14].toInt());
2242 setStyleStrategy((StyleStrategy)l[15].toInt());
2244 if (count == 11 || count == 17)
2245 d->request.styleName = l[count - 1].toString();
2247 d->request.styleName.clear();
2250 if (count >= 9 && !d->request.fixedPitch)
2251 d->request.ignorePitch =
true;
2257
2258
2259
2260
2261
2262
2263void QFont::initialize()
2268
2269
2270
2271
2272void QFont::cleanup()
2274 QFontCache::cleanup();
2278
2279
2280
2281void QFont::cacheStatistics()
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2316
2317
2318
2319
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2335
2336
2337
2338
2339
2342
2343
2344
2347
2348
2349
2350
2351
2352
2355
2356
2357
2358
2359
2360
2363
2364
2365
2366
2367
2368
2371
2372
2373
2374
2375
2376
2377
2380
2381
2382
2383
2384
2385
2386
2387
2388std::optional<QFont::Tag> QFont::Tag::fromString(QAnyStringView view)
noexcept
2390 if (view.size() != 4) {
2391 qWarning(
"The tag name must be exactly 4 characters long!");
2392 return std::nullopt;
2394 const QFont::Tag maybeTag = view.visit([](
auto view) {
2395 using CharType =
decltype(view.at(0));
2396 if constexpr (std::is_same_v<CharType,
char>) {
2397 const char bytes[5] = { view.at(0), view.at(1), view.at(2), view.at(3), 0 };
2400 const char bytes[5] = { view.at(0).toLatin1(), view.at(1).toLatin1(),
2401 view.at(2).toLatin1(), view.at(3).toLatin1(), 0 };
2405 return maybeTag.isValid() ? std::optional<Tag>(maybeTag) : std::nullopt;
2409
2410
2411
2412
2413
2414
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454void QFont::setVariableAxis(Tag tag,
float value)
2456 if (tag.isValid()) {
2457 if (resolve_mask & QFont::VariableAxesResolved && d->hasVariableAxis(tag, value))
2462 d->setVariableAxis(tag, value);
2463 resolve_mask |= QFont::VariableAxesResolved;
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477void QFont::unsetVariableAxis(Tag tag)
2479 if (tag.isValid()) {
2482 d->unsetVariableAxis(tag);
2483 resolve_mask |= QFont::VariableAxesResolved;
2488
2489
2490
2491
2492
2493
2494
2495
2496QList<QFont::Tag> QFont::variableAxisTags()
const
2498 return d->request.variableAxisValues.keys();
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511float QFont::variableAxisValue(Tag tag)
const
2513 return d->request.variableAxisValues.value(tag);
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526bool QFont::isVariableAxisSet(Tag tag)
const
2528 return d->request.variableAxisValues.contains(tag);
2532
2533
2534
2535
2536
2537
2538
2539
2540void QFont::clearVariableAxes()
2542 if (d->request.variableAxisValues.isEmpty())
2546 d->request.variableAxisValues.clear();
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586void QFont::setFeature(Tag tag, quint32 value)
2588 if (tag.isValid()) {
2589 d->detachButKeepEngineData(
this);
2590 d->setFeature(tag, value);
2591 resolve_mask |= QFont::FeaturesResolved;
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610void QFont::unsetFeature(Tag tag)
2612 if (tag.isValid()) {
2613 d->detachButKeepEngineData(
this);
2614 d->unsetFeature(tag);
2615 resolve_mask |= QFont::FeaturesResolved;
2620
2621
2622
2623
2624
2625
2626
2627
2628QList<QFont::Tag> QFont::featureTags()
const
2630 return d->features.keys();
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643quint32 QFont::featureValue(Tag tag)
const
2645 return d->features.value(tag);
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658bool QFont::isFeatureSet(Tag tag)
const
2660 return d->features.contains(tag);
2664
2665
2666
2667
2668
2669
2670
2671
2672void QFont::clearFeatures()
2674 if (d->features.isEmpty())
2677 d->detachButKeepEngineData(
this);
2678 d->features.clear();
2683 QFont::StyleHint styleHint,
2684 QFontDatabasePrivate::ExtendedScript script);
2687
2688
2689
2690
2691
2692
2693
2694QString QFont::defaultFamily()
const
2696 const QStringList fallbacks = qt_fallbacksForFamily(QString(),
2698 QFont::StyleHint(d->request.styleHint),
2699 QFontDatabasePrivate::Script_Common);
2700 if (!fallbacks.isEmpty())
2701 return fallbacks.first();
2706
2707
2708
2709
2710
2711
2712
2713
2715QStringList QFont::families()
const
2717 return d->request.families;
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2737void QFont::setFamilies(
const QStringList &families)
2739 if ((resolve_mask & QFont::FamiliesResolved) && d->request.families == families)
2742 d->request.families = families;
2743 resolve_mask |= QFont::FamiliesResolved;
2748
2749
2750#ifndef QT_NO_DATASTREAM
2753
2754
2755
2756
2757
2758
2759
2760QDataStream &operator<<(QDataStream &s,
const QFont &font)
2762 if (s.version() == 1) {
2763 s << font.d->request.families.constFirst().toLatin1();
2765 s << font.d->request.families.constFirst();
2766 if (s.version() >= QDataStream::Qt_5_4)
2767 s << font.d->request.styleName;
2770 if (s.version() >= QDataStream::Qt_4_0) {
2772 double pointSize = font.d->request.pointSize;
2773 qint32 pixelSize = font.d->request.pixelSize;
2776 }
else if (s.version() <= 3) {
2777 qint16 pointSize = (qint16) (font.d->request.pointSize * 10);
2778 if (pointSize < 0) {
2779 pointSize = (qint16)QFontInfo(font).pointSize() * 10;
2783 s << (qint16) (font.d->request.pointSize * 10);
2784 s << (qint16) font.d->request.pixelSize;
2787 s << (quint8) font.d->request.styleHint;
2788 if (s.version() >= QDataStream::Qt_3_1) {
2791 if (s.version() >= QDataStream::Qt_5_4)
2792 s << (quint16) font.d->request.styleStrategy;
2794 s << (quint8) font.d->request.styleStrategy;
2797 if (s.version() < QDataStream::Qt_6_0)
2798 s << quint8(0) << quint8(qt_openTypeToLegacyWeight(font.d->request.weight));
2800 s << quint16(font.d->request.weight);
2802 s << get_font_bits(s.version(), font.d.data());
2803 if (s.version() >= QDataStream::Qt_4_3)
2804 s << (quint16)font.d->request.stretch;
2805 if (s.version() >= QDataStream::Qt_4_4)
2806 s << get_extended_font_bits(font.d.data());
2807 if (s.version() >= QDataStream::Qt_4_5) {
2808 s << font.d->letterSpacing.value();
2809 s << font.d->wordSpacing.value();
2811 if (s.version() >= QDataStream::Qt_5_4)
2812 s << (quint8)font.d->request.hintingPreference;
2813 if (s.version() >= QDataStream::Qt_5_6)
2814 s << (quint8)font.d->capital;
2815 if (s.version() >= QDataStream::Qt_5_13) {
2816 if (s.version() < QDataStream::Qt_6_0)
2817 s << font.d->request.families.mid(1);
2819 s << font.d->request.families;
2821 if (s.version() >= QDataStream::Qt_6_6)
2822 s << font.d->features;
2823 if (s.version() >= QDataStream::Qt_6_7)
2824 s << font.d->request.variableAxisValues;
2830
2831
2832
2833
2834
2835
2836
2839 font.d =
new QFontPrivate;
2840 font.resolve_mask = QFont::AllPropertiesResolved;
2842 quint8 styleHint, bits;
2843 quint16 styleStrategy = QFont::PreferDefault;
2845 if (s.version() == 1) {
2848 font.d->request.families = QStringList(QString::fromLatin1(fam));
2852 font.d->request.families = QStringList(fam);
2853 if (s.version() >= QDataStream::Qt_5_4)
2854 s >> font.d->request.styleName;
2857 if (s.version() >= QDataStream::Qt_4_0) {
2863 font.d->request.pointSize = qreal(pointSize);
2864 font.d->request.pixelSize = pixelSize;
2866 qint16 pointSize, pixelSize = -1;
2868 if (s.version() >= 4)
2870 font.d->request.pointSize = qreal(pointSize / 10.);
2871 font.d->request.pixelSize = pixelSize;
2874 if (s.version() >= QDataStream::Qt_3_1) {
2875 if (s.version() >= QDataStream::Qt_5_4) {
2878 quint8 tempStyleStrategy;
2879 s >> tempStyleStrategy;
2880 styleStrategy = tempStyleStrategy;
2884 if (s.version() < QDataStream::Qt_6_0) {
2889 font.d->request.weight = qt_legacyToOpenTypeWeight(weight);
2893 font.d->request.weight = weight;
2898 font.d->request.styleHint = styleHint;
2899 font.d->request.styleStrategy = styleStrategy;
2901 set_font_bits(s.version(), bits, font.d.data());
2903 if (s.version() >= QDataStream::Qt_4_3) {
2906 font.d->request.stretch = stretch;
2909 if (s.version() >= QDataStream::Qt_4_4) {
2910 quint8 extendedBits;
2912 set_extended_font_bits(extendedBits, font.d.data());
2914 if (s.version() >= QDataStream::Qt_4_5) {
2917 font.d->letterSpacing.setValue(value);
2919 font.d->wordSpacing.setValue(value);
2921 if (s.version() >= QDataStream::Qt_5_4) {
2924 font.d->request.hintingPreference = QFont::HintingPreference(value);
2926 if (s.version() >= QDataStream::Qt_5_6) {
2929 font.d->capital = QFont::Capitalization(value);
2931 if (s.version() >= QDataStream::Qt_5_13) {
2934 if (s.version() < QDataStream::Qt_6_0)
2935 font.d->request.families.append(value);
2937 font.d->request.families = value;
2939 if (s.version() >= QDataStream::Qt_6_6) {
2940 font.d->features.clear();
2941 s >> font.d->features;
2943 if (s.version() >= QDataStream::Qt_6_7) {
2944 font.d->request.variableAxisValues.clear();
2945 s >> font.d->request.variableAxisValues;
2951QDataStream &operator<<(QDataStream &stream, QFont::Tag tag)
2953 stream << tag.value();
2957QDataStream &
operator>>(QDataStream &stream, QFont::Tag &tag)
2961 if (
const auto maybeTag = QFont::Tag::fromValue(value))
2964 stream.setStatus(QDataStream::ReadCorruptData);
2972
2973
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066QFontInfo::QFontInfo(
const QFont &font)
3072
3073
3074QFontInfo::QFontInfo(
const QFontInfo &fi)
3080
3081
3082QFontInfo::~QFontInfo()
3087
3088
3089QFontInfo &QFontInfo::operator=(
const QFontInfo &fi)
3096
3097
3098
3099
3102
3103
3104
3105
3106QString QFontInfo::family()
const
3108 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3109 Q_ASSERT(engine !=
nullptr);
3110 return engine->fontDef.families.isEmpty() ? QString() : engine->fontDef.families.constFirst();
3114
3115
3116
3117
3118
3119
3120
3121QString QFontInfo::styleName()
const
3123 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3124 Q_ASSERT(engine !=
nullptr);
3125 return engine->fontDef.styleName;
3129
3130
3131
3132
3133int QFontInfo::pointSize()
const
3135 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3136 Q_ASSERT(engine !=
nullptr);
3137 return qRound(engine->fontDef.pointSize);
3141
3142
3143
3144
3145qreal QFontInfo::pointSizeF()
const
3147 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3148 Q_ASSERT(engine !=
nullptr);
3149 return engine->fontDef.pointSize;
3153
3154
3155
3156
3157int QFontInfo::pixelSize()
const
3159 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3160 Q_ASSERT(engine !=
nullptr);
3161 return engine->fontDef.pixelSize;
3165
3166
3167
3168
3169bool QFontInfo::italic()
const
3171 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3172 Q_ASSERT(engine !=
nullptr);
3173 return engine->fontDef.style != QFont::StyleNormal;
3177
3178
3179
3180
3181QFont::Style QFontInfo::style()
const
3183 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3184 Q_ASSERT(engine !=
nullptr);
3185 return (QFont::Style)engine->fontDef.style;
3189#if QT_DEPRECATED_SINCE(6
, 0
)
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203int QFontInfo::legacyWeight()
const
3205 return qt_openTypeToLegacyWeight(weight());
3211
3212
3213
3214
3215int QFontInfo::weight()
const
3217 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3218 Q_ASSERT(engine !=
nullptr);
3219 return engine->fontDef.weight;
3224
3225
3226
3227
3228
3229
3230
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242bool QFontInfo::underline()
const
3244 return d->underline;
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257bool QFontInfo::overline()
const
3263
3264
3265
3266
3267
3268
3269
3270bool QFontInfo::strikeOut()
const
3272 return d->strikeOut;
3276
3277
3278
3279
3280bool QFontInfo::fixedPitch()
const
3282 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3283 Q_ASSERT(engine !=
nullptr);
3285 if (!engine->fontDef.fixedPitchComputed) {
3286 QChar ch[2] = { u'i', u'm' };
3287 QGlyphLayoutArray<2> g;
3289 if (engine->stringToCMap(ch, 2, &g, &l, {}) < 0)
3292 engine->fontDef.fixedPitch = g.advances[0] == g.advances[1];
3293 engine->fontDef.fixedPitchComputed =
true;
3296 return engine->fontDef.fixedPitch;
3300
3301
3302
3303
3304
3305
3306QFont::StyleHint QFontInfo::styleHint()
const
3308 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3309 Q_ASSERT(engine !=
nullptr);
3310 return (QFont::StyleHint) engine->fontDef.styleHint;
3314
3315
3316
3317
3318
3319bool QFontInfo::exactMatch()
const
3321 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3322 Q_ASSERT(engine !=
nullptr);
3323 return d->request.exactMatch(engine->fontDef);
3327
3328
3329
3330
3331
3332
3333
3334QList<QFontVariableAxis> QFontInfo::variableAxes()
const
3336 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3337 Q_ASSERT(engine !=
nullptr);
3338 return engine->variableAxes();
3346using namespace std::chrono_literals;
3348#ifdef QFONTCACHE_DEBUG
3350static constexpr auto fast_timeout = 1s;
3351static constexpr auto slow_timeout = 5s;
3357#ifndef QFONTCACHE_MIN_COST
3358# define QFONTCACHE_MIN_COST 4
*1024
3363QFontCache *QFontCache::instance()
3365 QFontCache *&fontCache = theFontCache()->localData();
3367 fontCache =
new QFontCache;
3371void QFontCache::cleanup()
3373 QThreadStorage<QFontCache *> *cache =
nullptr;
3375 cache = theFontCache();
3376 } QT_CATCH (
const std::bad_alloc &) {
3379 if (cache && cache->hasLocalData())
3380 cache->setLocalData(
nullptr);
3383Q_CONSTINIT
static QBasicAtomicInt font_cache_id = Q_BASIC_ATOMIC_INITIALIZER(0);
3385QFontCache::QFontCache()
3386 : QObject(), total_cost(0), max_cost(min_cost),
3387 current_timestamp(0), fast(
false),
3388 autoClean(QGuiApplication::instance()
3389 && (QGuiApplication::instance()->thread() == QThread::currentThread())),
3390 m_id(font_cache_id.fetchAndAddRelaxed(1) + 1)
3394QFontCache::~QFontCache()
3399void QFontCache::clear()
3402 EngineDataCache::Iterator it = engineDataCache.begin(),
3403 end = engineDataCache.end();
3405 QFontEngineData *data = it.value();
3406 for (
int i = 0; i < QFontDatabasePrivate::ScriptCount; ++i) {
3407 if (data->engines[i]) {
3408 if (!data->engines[i]->ref.deref()) {
3409 Q_ASSERT(engineCacheCount.value(data->engines[i]) == 0);
3410 delete data->engines[i];
3412 data->engines[i] =
nullptr;
3415 if (!data->ref.deref()) {
3418 FC_DEBUG(
"QFontCache::clear: engineData %p still has refcount %d",
3419 data, data->ref.loadRelaxed());
3425 engineDataCache.clear();
3428 bool mightHaveEnginesLeftForCleanup;
3430 mightHaveEnginesLeftForCleanup =
false;
3431 for (EngineCache::Iterator it = engineCache.begin(), end = engineCache.end();
3433 QFontEngine *engine = it.value().data;
3435 const int cacheCount = --engineCacheCount[engine];
3436 Q_ASSERT(cacheCount >= 0);
3437 if (!engine->ref.deref()) {
3438 Q_ASSERT(cacheCount == 0);
3439 mightHaveEnginesLeftForCleanup = engine->type() == QFontEngine::Multi;
3441 }
else if (cacheCount == 0) {
3442 FC_DEBUG(
"QFontCache::clear: engine %p still has refcount %d",
3443 engine, engine->ref.loadRelaxed());
3445 it.value().data =
nullptr;
3448 }
while (mightHaveEnginesLeftForCleanup);
3450 engineCache.clear();
3451 engineCacheCount.clear();
3455 max_cost = min_cost;
3459QFontEngineData *QFontCache::findEngineData(
const QFontDef &def)
const
3461 EngineDataCache::ConstIterator it = engineDataCache.constFind(def);
3462 if (it == engineDataCache.constEnd())
3469void QFontCache::insertEngineData(
const QFontDef &def, QFontEngineData *engineData)
3471#ifdef QFONTCACHE_DEBUG
3472 FC_DEBUG(
"QFontCache: inserting new engine data %p", engineData);
3473 if (engineDataCache.contains(def)) {
3474 FC_DEBUG(
" QFontCache already contains engine data %p for key=(%g %g %d %d %d)",
3475 engineDataCache.value(def), def.pointSize,
3476 def.pixelSize, def.weight, def.style, def.fixedPitch);
3479 Q_ASSERT(!engineDataCache.contains(def));
3481 engineData->ref.ref();
3486 engineDataCache.insert(def, engineData);
3487 increaseCost(
sizeof(QFontEngineData));
3490QFontEngine *QFontCache::findEngine(
const Key &key)
3492 EngineCache::Iterator it = engineCache.find(key),
3493 end = engineCache.end();
3494 if (it == end)
return nullptr;
3496 Q_ASSERT(it.value().data !=
nullptr);
3497 Q_ASSERT(key.multi == (it.value().data->type() == QFontEngine::Multi));
3500 updateHitCountAndTimeStamp(it.value());
3502 return it.value().data;
3505void QFontCache::updateHitCountAndTimeStamp(Engine &value)
3508 value.timestamp = ++current_timestamp;
3510 FC_DEBUG(
"QFontCache: found font engine\n"
3511 " %p: timestamp %4u hits %3u ref %2d/%2d, type %d",
3512 value.data, value.timestamp, value.hits,
3513 value.data->ref.loadRelaxed(), engineCacheCount.value(value.data),
3514 value.data->type());
3517void QFontCache::insertEngine(
const Key &key, QFontEngine *engine,
bool insertMulti)
3519 Q_ASSERT(engine !=
nullptr);
3520 Q_ASSERT(key.multi == (engine->type() == QFontEngine::Multi));
3522#ifdef QFONTCACHE_DEBUG
3523 FC_DEBUG(
"QFontCache: inserting new engine %p, refcount %d", engine, engine->ref.loadRelaxed());
3524 if (!insertMulti && engineCache.contains(key)) {
3525 FC_DEBUG(
" QFontCache already contains engine %p for key=(%g %g %d %d %d)",
3526 engineCache.value(key).data, key.def.pointSize,
3527 key.def.pixelSize, key.def.weight, key.def.style, key.def.fixedPitch);
3535 Engine data(engine);
3536 data.timestamp = ++current_timestamp;
3539 engineCache.insert(key, data);
3541 engineCache.replace(key, data);
3543 if (++engineCacheCount[engine] == 1)
3544 increaseCost(engine->cache_cost);
3547void QFontCache::increaseCost(uint cost)
3549 cost = (cost + 512) / 1024;
3550 cost = cost > 0 ? cost : 1;
3553 FC_DEBUG(
" COST: increased %u kb, total_cost %u kb, max_cost %u kb",
3554 cost, total_cost, max_cost);
3556 if (total_cost > max_cost) {
3557 max_cost = total_cost;
3562 if (!timer.isActive() || ! fast) {
3563 FC_DEBUG(
" TIMER: starting fast timer (%d s)",
static_cast<
int>(fast_timeout.count()));
3565 timer.start(fast_timeout,
this);
3571void QFontCache::decreaseCost(uint cost)
3573 cost = (cost + 512) / 1024;
3574 cost = cost > 0 ? cost : 1;
3575 Q_ASSERT(cost <= total_cost);
3578 FC_DEBUG(
" COST: decreased %u kb, total_cost %u kb, max_cost %u kb",
3579 cost, total_cost, max_cost);
3582void QFontCache::timerEvent(QTimerEvent *)
3584 FC_DEBUG(
"QFontCache::timerEvent: performing cache maintenance (timestamp %u)",
3587 if (total_cost <= max_cost && max_cost <= min_cost) {
3588 FC_DEBUG(
" cache redused sufficiently, stopping timer");
3598void QFontCache::decreaseCache()
3601 uint in_use_cost = 0;
3607 const uint engine_data_cost =
3608 sizeof(QFontEngineData) > 1024 ?
sizeof(QFontEngineData) : 1024;
3610 EngineDataCache::ConstIterator it = engineDataCache.constBegin(),
3611 end = engineDataCache.constEnd();
3612 for (; it != end; ++it) {
3613 FC_DEBUG(
" %p: ref %2d", it.value(),
int(it.value()->ref.loadRelaxed()));
3615 if (it.value()->ref.loadRelaxed() != 1)
3616 in_use_cost += engine_data_cost;
3623 EngineCache::ConstIterator it = engineCache.constBegin(),
3624 end = engineCache.constEnd();
3625 for (; it != end; ++it) {
3626 FC_DEBUG(
" %p: timestamp %4u hits %2u ref %2d/%2d, cost %u bytes",
3627 it.value().data, it.value().timestamp, it.value().hits,
3628 it.value().data->ref.loadRelaxed(), engineCacheCount.value(it.value().data),
3629 it.value().data->cache_cost);
3631 if (it.value().data->ref.loadRelaxed() > engineCacheCount.value(it.value().data))
3632 in_use_cost += it.value().data->cache_cost / engineCacheCount.value(it.value().data);
3636 in_use_cost += engineCache.size();
3639 in_use_cost = (in_use_cost + 512) / 1024;
3642
3643
3644
3645
3646
3647
3648
3649 uint new_max_cost = qMax(qMax(max_cost / 2, in_use_cost), min_cost);
3651 FC_DEBUG(
" after sweep, in use %u kb, total %u kb, max %u kb, new max %u kb",
3652 in_use_cost, total_cost, max_cost, new_max_cost);
3655 if (new_max_cost == max_cost) {
3657 FC_DEBUG(
" cannot shrink cache, slowing timer");
3659 if (timer.isActive()) {
3660 timer.start(slow_timeout,
this);
3665 }
else if (! fast) {
3666 FC_DEBUG(
" dropping into passing gear");
3668 timer.start(fast_timeout,
this);
3673 max_cost = new_max_cost;
3679 EngineDataCache::Iterator it = engineDataCache.begin();
3680 while (it != engineDataCache.end()) {
3681 if (it.value()->ref.loadRelaxed() == 1) {
3683 decreaseCost(
sizeof(QFontEngineData));
3684 it.value()->ref.deref();
3686 it = engineDataCache.erase(it);
3696 bool cost_decreased;
3698 cost_decreased =
false;
3700 EngineCache::Iterator it = engineCache.begin(),
3701 end = engineCache.end();
3704 uint least_popular = ~0u;
3706 EngineCache::Iterator jt = end;
3708 for ( ; it != end; ++it) {
3709 if (it.value().data->ref.loadRelaxed() != engineCacheCount.value(it.value().data))
3712 if (it.value().timestamp < oldest && it.value().hits <= least_popular) {
3713 oldest = it.value().timestamp;
3714 least_popular = it.value().hits;
3721 FC_DEBUG(
" %p: timestamp %4u hits %2u ref %2d/%2d, type %d",
3722 it.value().data, it.value().timestamp, it.value().hits,
3723 it.value().data->ref.loadRelaxed(), engineCacheCount.value(it.value().data),
3724 it.value().data->type());
3726 QFontEngine *fontEngine = it.value().data;
3728 it = engineCache.begin();
3729 while (it != engineCache.end()) {
3730 if (it.value().data == fontEngine) {
3731 fontEngine->ref.deref();
3732 it = engineCache.erase(it);
3738 Q_ASSERT(fontEngine->ref.loadRelaxed() == 0);
3739 decreaseCost(fontEngine->cache_cost);
3741 engineCacheCount.remove(fontEngine);
3743 cost_decreased =
true;
3745 }
while (cost_decreased && total_cost > max_cost);
3749#ifndef QT_NO_DEBUG_STREAM
3750QDebug operator<<(QDebug stream,
const QFont &font)
3752 QDebugStateSaver saver(stream);
3753 stream.nospace().noquote();
3756 if (stream.verbosity() == QDebug::DefaultVerbosity) {
3757 stream << font.toString() <<
")";
3761 QString fontDescription;
3762 QDebug debug(&fontDescription);
3765 const QFont defaultFont(
new QFontPrivate);
3767 for (
int property = QFont::SizeResolved; property < QFont::AllPropertiesResolved; property <<= 1) {
3768 const bool resolved = (font.resolve_mask & property) != 0;
3769 if (!resolved && stream.verbosity() == QDebug::MinimumVerbosity)
3772 #define QFONT_DEBUG_SKIP_DEFAULT(prop)
3773 if ((font.prop() == defaultFont.prop()) && stream.verbosity() == 1
)
3776 QDebugStateSaver saver(debug);
3779 case QFont::SizeResolved:
3780 if (font.pointSizeF() >= 0)
3781 debug << font.pointSizeF() <<
"pt";
3782 else if (font.pixelSize() >= 0)
3783 debug << font.pixelSize() <<
"px";
3787 case QFont::StyleHintResolved:
3789 debug.verbosity(1) << font.styleHint();
break;
3790 case QFont::StyleStrategyResolved:
3792 debug.verbosity(1) << font.styleStrategy();
break;
3793 case QFont::WeightResolved:
3794 debug.verbosity(1) << QFont::Weight(font.weight());
break;
3795 case QFont::StyleResolved:
3797 debug.verbosity(0) << font.style();
break;
3798 case QFont::UnderlineResolved:
3800 debug <<
"underline=" << font.underline();
break;
3801 case QFont::OverlineResolved:
3803 debug <<
"overline=" << font.overline();
break;
3804 case QFont::StrikeOutResolved:
3806 debug <<
"strikeOut=" << font.strikeOut();
break;
3807 case QFont::FixedPitchResolved:
3809 debug <<
"fixedPitch=" << font.fixedPitch();
break;
3810 case QFont::StretchResolved:
3812 debug.verbosity(0) << QFont::Stretch(font.stretch());
break;
3813 case QFont::KerningResolved:
3815 debug <<
"kerning=" << font.kerning();
break;
3816 case QFont::CapitalizationResolved:
3818 debug.verbosity(0) << font.capitalization();
break;
3819 case QFont::LetterSpacingResolved:
3821 debug <<
"letterSpacing=" << font.letterSpacing();
3822 debug.verbosity(0) <<
" (" << font.letterSpacingType() <<
")";
3824 case QFont::HintingPreferenceResolved:
3826 debug.verbosity(0) << font.hintingPreference();
break;
3827 case QFont::StyleNameResolved:
3829 debug <<
"styleName=" << font.styleName();
break;
3834 #undef QFONT_DEBUG_SKIP_DEFAULT
3839 if (stream.verbosity() > QDebug::MinimumVerbosity)
3840 debug.verbosity(0) <<
"resolveMask=" << QFlags<QFont::ResolveProperties>(font.resolve_mask);
3842 fontDescription.chop(2);
3844 stream << fontDescription <<
')';
3849QDebug operator<<(QDebug debug, QFont::Tag tag)
3851 QDebugStateSaver saver(debug);
3852 debug.noquote() << tag.toString();
3859#include "moc_qfont.cpp"
Combined button and popup list for selecting options.
QDataStream & operator>>(QDataStream &s, QKeyCombination &combination)
Q_GUI_EXPORT int qt_openTypeToLegacyWeight(int weight)
static void set_font_bits(int version, quint8 bits, QFontPrivate *f)
QRecursiveMutex * qt_fontdatabase_mutex()
static int convertWeights(int weight, bool inverted)
static QStringList splitIntoFamilies(const QString &family)
Q_GUI_EXPORT int qt_legacyToOpenTypeWeight(int weight)
static constexpr auto fast_timeout
QHash< QString, QStringList > QFontSubst
#define QFONT_DEBUG_SKIP_DEFAULT(prop)
#define QFONTCACHE_MIN_COST
#define QT_FONT_ENGINE_FROM_DATA(data, script)
Q_GUI_EXPORT int qt_defaultDpiY()
static constexpr auto slow_timeout
Q_GUI_EXPORT int qt_defaultDpiX()
static quint8 get_extended_font_bits(const QFontPrivate *f)
Q_GUI_EXPORT int qt_defaultDpi()
#define QFONTCACHE_DECREASE_TRIGGER_LIMIT
QStringList qt_fallbacksForFamily(const QString &family, QFont::Style style, QFont::StyleHint styleHint, QFontDatabasePrivate::ExtendedScript script)
static quint8 get_font_bits(int version, const QFontPrivate *f)
static void set_extended_font_bits(quint8 bits, QFontPrivate *f)
Q_CONSTINIT Q_GUI_EXPORT bool qt_is_tty_app
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex)
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept
bool exactMatch(const QFontDef &other) const