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 if (families.size() != other.families.size())
88 QString this_family, this_foundry, other_family, other_foundry;
89 for (
int i = 0; i < families.size(); ++i) {
90 QFontDatabasePrivate::parseFontName(families.at(i), this_foundry, this_family);
91 QFontDatabasePrivate::parseFontName(other.families.at(i), other_foundry, other_family);
92 if (this_family != other_family || this_foundry != other_foundry)
96 if (variableAxisValues != other.variableAxisValues)
99 return (styleHint == other.styleHint
100 && styleStrategy == other.styleStrategy
101 && weight == other.weight
102 && style == other.style
103 && this_family == other_family
104 && (styleName.isEmpty() || other.styleName.isEmpty() || styleName == other.styleName)
105 && (this_foundry.isEmpty()
106 || other_foundry.isEmpty()
107 || this_foundry == other_foundry)
115 if (QCoreApplication::instance()->testAttribute(Qt::AA_Use96Dpi))
121 if (
const QScreen *screen = QGuiApplication::primaryScreen())
122 return qRound(screen->logicalDotsPerInchX());
130 if (QCoreApplication::instance()->testAttribute(Qt::AA_Use96Dpi))
136 if (
const QScreen *screen = QGuiApplication::primaryScreen())
137 return qRound(screen->logicalDotsPerInchY());
145 return qt_defaultDpiY();
151 static constexpr std::array<
int, 2> legacyToOpenTypeMap[] = {
152 { 0, QFont::Thin }, { 12, QFont::ExtraLight }, { 25, QFont::Light },
153 { 50, QFont::Normal }, { 57, QFont::Medium }, { 63, QFont::DemiBold },
154 { 75, QFont::Bold }, { 81, QFont::ExtraBold }, { 87, QFont::Black },
157 int closestDist = INT_MAX;
161 for (
auto mapping : legacyToOpenTypeMap) {
162 const int weightOld = mapping[ inverted];
163 const int weightNew = mapping[!inverted];
164 const int dist = qAbs(weightOld - weight);
165 if (dist < closestDist) {
180 QStringList familyList;
181 if (family.isEmpty())
183 const auto list = QStringView{family}.split(u',');
184 const int numFamilies = list.size();
185 familyList.reserve(numFamilies);
186 for (
int i = 0; i < numFamilies; ++i) {
187 auto str = list.at(i).trimmed();
188 if ((str.startsWith(u'"') && str.endsWith(u'"'))
189 || (str.startsWith(u'\'') && str.endsWith(u'\''))) {
190 str = str.mid(1, str.size() - 2);
192 familyList << str.toString();
209QFontPrivate::QFontPrivate()
210 : engineData(
nullptr), dpi(qt_defaultDpi()),
211 underline(
false), overline(
false), strikeOut(
false), kerning(
true),
212 capital(0), letterSpacingIsAbsolute(
false), scFont(
nullptr)
216QFontPrivate::QFontPrivate(
const QFontPrivate &other)
217 : request(other.request), engineData(
nullptr), dpi(other.dpi),
218 underline(other.underline), overline(other.overline),
219 strikeOut(other.strikeOut), kerning(other.kerning),
220 capital(other.capital), letterSpacingIsAbsolute(other.letterSpacingIsAbsolute),
221 letterSpacing(other.letterSpacing), wordSpacing(other.wordSpacing),
222 features(other.features), scFont(other.scFont)
224 if (scFont && scFont !=
this)
228QFontPrivate::~QFontPrivate()
230 if (engineData && !engineData->ref.deref())
232 engineData =
nullptr;
233 if (scFont && scFont !=
this) {
234 if (!scFont->ref.deref())
242#define QT_FONT_ENGINE_FROM_DATA(data, script) data->engines[script]
244QFontEngine *QFontPrivate::engineForScript(
int script)
const
246 QMutexLocker locker(qt_fontdatabase_mutex());
247 if (script <= QChar::Script_Latin)
248 script = QChar::Script_Common;
249 if (engineData && engineData->fontCacheId != QFontCache::instance()->id()) {
251 if (!engineData->ref.deref())
253 engineData =
nullptr;
256 QFontDatabasePrivate::load(
this, script);
260void QFontPrivate::alterCharForCapitalization(QChar &c)
const {
262 case QFont::AllUppercase:
263 case QFont::SmallCaps:
266 case QFont::AllLowercase:
269 case QFont::MixedCase:
274QFontPrivate *QFontPrivate::smallCapsFontPrivate()
const
278 QFont font(
const_cast<QFontPrivate *>(
this));
279 qreal pointSize = font.pointSizeF();
281 font.setPointSizeF(pointSize * .7);
283 font.setPixelSize((font.pixelSize() * 7 + 5) / 10);
284 scFont = font.d.data();
291void QFontPrivate::resolve(uint mask,
const QFontPrivate *other)
293 Q_ASSERT(other !=
nullptr);
297 if ((mask & QFont::AllPropertiesResolved) == QFont::AllPropertiesResolved)
return;
300 if (!(mask & QFont::FamiliesResolved))
301 request.families = other->request.families;
303 if (! (mask & QFont::StyleNameResolved))
304 request.styleName = other->request.styleName;
306 if (! (mask & QFont::SizeResolved)) {
307 request.pointSize = other->request.pointSize;
308 request.pixelSize = other->request.pixelSize;
311 if (! (mask & QFont::StyleHintResolved))
312 request.styleHint = other->request.styleHint;
314 if (! (mask & QFont::StyleStrategyResolved))
315 request.styleStrategy = other->request.styleStrategy;
317 if (! (mask & QFont::WeightResolved))
318 request.weight = other->request.weight;
320 if (! (mask & QFont::StyleResolved))
321 request.style = other->request.style;
323 if (! (mask & QFont::FixedPitchResolved))
324 request.fixedPitch = other->request.fixedPitch;
326 if (! (mask & QFont::StretchResolved))
327 request.stretch = other->request.stretch;
329 if (! (mask & QFont::HintingPreferenceResolved))
330 request.hintingPreference = other->request.hintingPreference;
332 if (! (mask & QFont::UnderlineResolved))
333 underline = other->underline;
335 if (! (mask & QFont::OverlineResolved))
336 overline = other->overline;
338 if (! (mask & QFont::StrikeOutResolved))
339 strikeOut = other->strikeOut;
341 if (! (mask & QFont::KerningResolved))
342 kerning = other->kerning;
344 if (! (mask & QFont::LetterSpacingResolved)) {
345 letterSpacing = other->letterSpacing;
346 letterSpacingIsAbsolute = other->letterSpacingIsAbsolute;
348 if (! (mask & QFont::WordSpacingResolved))
349 wordSpacing = other->wordSpacing;
350 if (! (mask & QFont::CapitalizationResolved))
351 capital = other->capital;
353 if (!(mask & QFont::FeaturesResolved))
354 features = other->features;
356 if (!(mask & QFont::VariableAxesResolved))
357 request.variableAxisValues = other->request.variableAxisValues;
360bool QFontPrivate::hasVariableAxis(QFont::Tag tag,
float value)
const
362 return request.variableAxisValues.contains(tag) && request.variableAxisValues.value(tag) == value;
365void QFontPrivate::setVariableAxis(QFont::Tag tag,
float value)
367 request.variableAxisValues.insert(tag, value);
370void QFontPrivate::unsetVariableAxis(QFont::Tag tag)
372 request.variableAxisValues.remove(tag);
375void QFontPrivate::setFeature(QFont::Tag tag, quint32 value)
377 features.insert(tag, value);
380void QFontPrivate::unsetFeature(QFont::Tag tag)
382 features.remove(tag);
389 memset(engines, 0, QFontDatabasePrivate::ScriptCount *
sizeof(QFontEngine *));
394 Q_ASSERT(ref.loadRelaxed() == 0);
395 for (
int i = 0; i < QFontDatabasePrivate::ScriptCount; ++i) {
397 if (!engines[i]->ref.deref())
399 engines[i] =
nullptr;
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
566
567
568
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
597
598
599
600
601
602
603
604
605
606
607
608
609
610
613
614
615
616
617
618
621
622
623
624QFont::QFont(
const QFont &font,
const QPaintDevice *pd)
625 : resolve_mask(font.resolve_mask)
628 const int dpi = pd->logicalDpiY();
629 if (font.d->dpi != dpi) {
630 d =
new QFontPrivate(*font.d);
638
639
640QFont::QFont(QFontPrivate *data)
641 : d(data), resolve_mask(QFont::AllPropertiesResolved)
646
647
650 if (d->ref.loadRelaxed() == 1) {
651 if (d->engineData && !d->engineData->ref.deref())
652 delete d->engineData;
653 d->engineData =
nullptr;
654 if (d->scFont && d->scFont != d.data()) {
655 if (!d->scFont->ref.deref())
666
667
668
669
670
671void QFontPrivate::detachButKeepEngineData(QFont *font)
673 if (font->d->ref.loadRelaxed() == 1)
676 QFontEngineData *engineData = font->d->engineData;
678 engineData->ref.ref();
680 font->d->engineData = engineData;
684
685
686
687
689 : d(QGuiApplicationPrivate::instance() ? QGuiApplication::font().d.data() :
new QFontPrivate()), resolve_mask(0)
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715QFont::QFont(
const QString &family,
int pointSize,
int weight,
bool italic)
716 : d(
new QFontPrivate()), resolve_mask(QFont::FamiliesResolved)
718 if (pointSize <= 0) {
721 resolve_mask |= QFont::SizeResolved;
727 resolve_mask |= QFont::WeightResolved | QFont::StyleResolved;
731 resolve_mask |= QFont::StyleResolved;
733 d->request.families = splitIntoFamilies(family);
734 d->request.pointSize = qreal(pointSize);
735 d->request.pixelSize = -1;
736 d->request.weight = weight;
737 d->request.style = italic ? QFont::StyleItalic : QFont::StyleNormal;
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758QFont::QFont(
const QStringList &families,
int pointSize,
int weight,
bool italic)
759 : d(
new QFontPrivate()), resolve_mask(QFont::FamiliesResolved)
764 resolve_mask |= QFont::SizeResolved;
769 resolve_mask |= QFont::WeightResolved | QFont::StyleResolved;
772 resolve_mask |= QFont::StyleResolved;
774 d->request.families = families;
775 d->request.pointSize = qreal(pointSize);
776 d->request.pixelSize = -1;
777 d->request.weight = weight;
778 d->request.style = italic ? QFont::StyleItalic : QFont::StyleNormal;
782
783
784QFont::QFont(
const QFont &font)
785 : d(font.d), resolve_mask(font.resolve_mask)
790
791
797
798
799QFont &QFont::operator=(
const QFont &font)
802 resolve_mask = font.resolve_mask;
807
808
809
810
813
814
815
816
817
818QString QFont::family()
const
820 return d->request.families.isEmpty() ? QString() : d->request.families.constFirst();
824
825
826
827
828
829
830
831
832
833
834
835
836void QFont::setFamily(
const QString &family)
838 setFamilies(QStringList(family));
842
843
844
845
846
847
848
849
850QString QFont::styleName()
const
852 return d->request.styleName;
856
857
858
859
860
861
862
863
864
865
866
867
868void QFont::setStyleName(
const QString &styleName)
870 if ((resolve_mask & QFont::StyleNameResolved) && d->request.styleName == styleName)
875 d->request.styleName = styleName;
876 resolve_mask |= QFont::StyleNameResolved;
880
881
882
883
884
885int QFont::pointSize()
const
887 return qRound(d->request.pointSize);
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
944
945
946
949
950
951
952
953
954
955
956
957
958void QFont::setHintingPreference(HintingPreference hintingPreference)
960 if ((resolve_mask & QFont::HintingPreferenceResolved) && d->request.hintingPreference == hintingPreference)
965 d->request.hintingPreference = hintingPreference;
967 resolve_mask |= QFont::HintingPreferenceResolved;
971
972
973
974
975QFont::HintingPreference QFont::hintingPreference()
const
977 return QFont::HintingPreference(d->request.hintingPreference);
981
982
983
984
985
986void QFont::setPointSize(
int pointSize)
988 if (pointSize <= 0) {
989 qWarning(
"QFont::setPointSize: Point size <= 0 (%d), must be greater than 0", pointSize);
993 if ((resolve_mask & QFont::SizeResolved) && d->request.pointSize == qreal(pointSize))
998 d->request.pointSize = qreal(pointSize);
999 d->request.pixelSize = -1;
1001 resolve_mask |= QFont::SizeResolved;
1005
1006
1007
1008
1009
1010
1011void QFont::setPointSizeF(qreal pointSize)
1013 if (pointSize <= 0) {
1014 qWarning(
"QFont::setPointSizeF: Point size <= 0 (%f), must be greater than 0", pointSize);
1018 if ((resolve_mask & QFont::SizeResolved) && d->request.pointSize == pointSize)
1023 d->request.pointSize = pointSize;
1024 d->request.pixelSize = -1;
1026 resolve_mask |= QFont::SizeResolved;
1030
1031
1032
1033
1034
1035qreal QFont::pointSizeF()
const
1037 return d->request.pointSize;
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050void QFont::setPixelSize(
int pixelSize)
1052 if (pixelSize <= 0) {
1053 qWarning(
"QFont::setPixelSize: Pixel size <= 0 (%d)", pixelSize);
1057 if ((resolve_mask & QFont::SizeResolved) && d->request.pixelSize == qreal(pixelSize))
1062 d->request.pixelSize = pixelSize;
1063 d->request.pointSize = -1;
1065 resolve_mask |= QFont::SizeResolved;
1069
1070
1071
1072
1073
1074
1075int QFont::pixelSize()
const
1077 return d->request.pixelSize;
1081
1082
1083
1084
1085
1086
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1102
1103
1104
1105
1106QFont::Style QFont::style()
const
1108 return (QFont::Style)d->request.style;
1113
1114
1115
1116
1117void QFont::setStyle(Style style)
1119 if ((resolve_mask & QFont::StyleResolved) && d->request.style == style)
1124 d->request.style = style;
1125 resolve_mask |= QFont::StyleResolved;
1129
1130
1131
1132
1133
1134QFont::Weight QFont::weight()
const
1136 return static_cast<Weight>(d->request.weight);
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1158#if QT_DEPRECATED_SINCE(6
, 0
)
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174void QFont::setLegacyWeight(
int legacyWeight)
1176 setWeight(QFont::Weight(qt_legacyToOpenTypeWeight(legacyWeight)));
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192int QFont::legacyWeight()
const
1194 return qt_openTypeToLegacyWeight(weight());
1199
1200
1201
1202
1203
1204
1205
1206void QFont::setWeight(QFont::Weight weight)
1209 if (weightValue !=
static_cast<
int>(weight)) {
1210 qWarning() <<
"QFont::setWeight: Weight must be between 1 and 1000, attempted to set "
1211 <<
static_cast<
int>(weight);
1214 if ((resolve_mask & QFont::WeightResolved) && d->request.weight == weightValue)
1219 d->request.weight = weightValue;
1220 resolve_mask |= QFont::WeightResolved;
1224
1225
1226
1227
1228
1229
1230
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1248
1249
1250
1251
1252bool QFont::underline()
const
1254 return d->underline;
1258
1259
1260
1261
1262
1263void QFont::setUnderline(
bool enable)
1265 if ((resolve_mask & QFont::UnderlineResolved) && d->underline == enable)
1268 QFontPrivate::detachButKeepEngineData(
this);
1270 d->underline = enable;
1271 resolve_mask |= QFont::UnderlineResolved;
1275
1276
1277
1278
1279bool QFont::overline()
const
1285
1286
1287
1288
1289void QFont::setOverline(
bool enable)
1291 if ((resolve_mask & QFont::OverlineResolved) && d->overline == enable)
1294 QFontPrivate::detachButKeepEngineData(
this);
1296 d->overline = enable;
1297 resolve_mask |= QFont::OverlineResolved;
1301
1302
1303
1304
1305bool QFont::strikeOut()
const
1307 return d->strikeOut;
1311
1312
1313
1314
1315
1316void QFont::setStrikeOut(
bool enable)
1318 if ((resolve_mask & QFont::StrikeOutResolved) && d->strikeOut == enable)
1321 QFontPrivate::detachButKeepEngineData(
this);
1323 d->strikeOut = enable;
1324 resolve_mask |= QFont::StrikeOutResolved;
1328
1329
1330
1331
1332bool QFont::fixedPitch()
const
1334 return d->request.fixedPitch;
1338
1339
1340
1341
1342
1343void QFont::setFixedPitch(
bool enable)
1345 if ((resolve_mask & QFont::FixedPitchResolved) && d->request.fixedPitch == enable)
1350 d->request.fixedPitch = enable;
1351 d->request.ignorePitch =
false;
1352 resolve_mask |= QFont::FixedPitchResolved;
1356
1357
1358
1359
1360bool QFont::kerning()
const
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376void QFont::setKerning(
bool enable)
1378 if ((resolve_mask & QFont::KerningResolved) && d->kerning == enable)
1381 QFontPrivate::detachButKeepEngineData(
this);
1383 d->kerning = enable;
1384 resolve_mask |= QFont::KerningResolved;
1388
1389
1390
1391
1392
1393
1394
1395QFont::StyleStrategy QFont::styleStrategy()
const
1397 return (StyleStrategy) d->request.styleStrategy;
1401
1402
1403
1404
1405
1406
1407
1408QFont::StyleHint QFont::styleHint()
const
1410 return (StyleHint) d->request.styleHint;
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
1443
1444
1445
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
1507
1508
1509
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523void QFont::setStyleHint(StyleHint hint, StyleStrategy strategy)
1525 if ((resolve_mask & (QFont::StyleHintResolved | QFont::StyleStrategyResolved)) &&
1526 (StyleHint) d->request.styleHint == hint &&
1527 (StyleStrategy) d->request.styleStrategy == strategy)
1532 d->request.styleHint = hint;
1533 d->request.styleStrategy = strategy;
1534 resolve_mask |= QFont::StyleHintResolved;
1535 resolve_mask |= QFont::StyleStrategyResolved;
1540
1541
1542
1543
1544void QFont::setStyleStrategy(StyleStrategy s)
1546 if ((resolve_mask & QFont::StyleStrategyResolved) &&
1547 s == (StyleStrategy)d->request.styleStrategy)
1552 d->request.styleStrategy = s;
1553 resolve_mask |= QFont::StyleStrategyResolved;
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1578
1579
1580
1581
1582int QFont::stretch()
const
1584 return d->request.stretch;
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606void QFont::setStretch(
int factor)
1608 if (factor < 0 || factor > 4000) {
1609 qWarning(
"QFont::setStretch: Parameter '%d' out of range", factor);
1613 if ((resolve_mask & QFont::StretchResolved) &&
1614 d->request.stretch == (uint)factor)
1619 d->request.stretch = (uint)factor;
1620 resolve_mask |= QFont::StretchResolved;
1624
1625
1626
1627
1628
1629
1630
1631
1634
1635
1636
1637
1638
1639qreal QFont::letterSpacing()
const
1641 return d->letterSpacing.toReal();
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656void QFont::setLetterSpacing(SpacingType type, qreal spacing)
1658 const QFixed newSpacing = QFixed::fromReal(spacing);
1659 const bool absoluteSpacing = type == AbsoluteSpacing;
1660 if ((resolve_mask & QFont::LetterSpacingResolved) &&
1661 d->letterSpacingIsAbsolute == absoluteSpacing &&
1662 d->letterSpacing == newSpacing)
1665 QFontPrivate::detachButKeepEngineData(
this);
1667 d->letterSpacing = newSpacing;
1668 d->letterSpacingIsAbsolute = absoluteSpacing;
1669 resolve_mask |= QFont::LetterSpacingResolved;
1673
1674
1675
1676
1677
1678QFont::SpacingType QFont::letterSpacingType()
const
1680 return d->letterSpacingIsAbsolute ? AbsoluteSpacing : PercentageSpacing;
1684
1685
1686
1687
1688
1689qreal QFont::wordSpacing()
const
1691 return d->wordSpacing.toReal();
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708void QFont::setWordSpacing(qreal spacing)
1710 const QFixed newSpacing = QFixed::fromReal(spacing);
1711 if ((resolve_mask & QFont::WordSpacingResolved) &&
1712 d->wordSpacing == newSpacing)
1715 QFontPrivate::detachButKeepEngineData(
this);
1717 d->wordSpacing = newSpacing;
1718 resolve_mask |= QFont::WordSpacingResolved;
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1736
1737
1738
1739
1740
1741
1742
1743void QFont::setCapitalization(Capitalization caps)
1745 if ((resolve_mask & QFont::CapitalizationResolved) &&
1746 capitalization() == caps)
1749 QFontPrivate::detachButKeepEngineData(
this);
1752 resolve_mask |= QFont::CapitalizationResolved;
1756
1757
1758
1759
1760
1761QFont::Capitalization QFont::capitalization()
const
1763 return static_cast<QFont::Capitalization> (d->capital);
1767
1768
1769
1770
1771
1772bool QFont::exactMatch()
const
1774 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
1775 Q_ASSERT(engine !=
nullptr);
1776 return d->request.exactMatch(engine->fontDef);
1780
1781
1782
1783
1784
1785
1786
1787
1788bool QFont::operator==(
const QFont &f)
const
1791 || (f.d->request == d->request
1792 && f.d->request.pointSize == d->request.pointSize
1793 && f.d->underline == d->underline
1794 && f.d->overline == d->overline
1795 && f.d->strikeOut == d->strikeOut
1796 && f.d->kerning == d->kerning
1797 && f.d->capital == d->capital
1798 && f.d->letterSpacingIsAbsolute == d->letterSpacingIsAbsolute
1799 && f.d->letterSpacing == d->letterSpacing
1800 && f.d->wordSpacing == d->wordSpacing
1801 && f.d->features == d->features
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817bool QFont::operator<(
const QFont &f)
const
1819 if (f.d == d)
return false;
1821 const QFontDef &r1 = f.d->request;
1822 const QFontDef &r2 = d->request;
1823 if (r1.pointSize != r2.pointSize)
return r1.pointSize < r2.pointSize;
1824 if (r1.pixelSize != r2.pixelSize)
return r1.pixelSize < r2.pixelSize;
1825 if (r1.weight != r2.weight)
return r1.weight < r2.weight;
1826 if (r1.style != r2.style)
return r1.style < r2.style;
1827 if (r1.stretch != r2.stretch)
return r1.stretch < r2.stretch;
1828 if (r1.styleHint != r2.styleHint)
return r1.styleHint < r2.styleHint;
1829 if (r1.styleStrategy != r2.styleStrategy)
return r1.styleStrategy < r2.styleStrategy;
1830 if (r1.families != r2.families)
return r1.families < r2.families;
1831 if (f.d->capital != d->capital)
return f.d->capital < d->capital;
1833 if (f.d->letterSpacingIsAbsolute != d->letterSpacingIsAbsolute)
return f.d->letterSpacingIsAbsolute < d->letterSpacingIsAbsolute;
1834 if (f.d->letterSpacing != d->letterSpacing)
return f.d->letterSpacing < d->letterSpacing;
1835 if (f.d->wordSpacing != d->wordSpacing)
return f.d->wordSpacing < d->wordSpacing;
1837 int f1attrs = (f.d->underline << 3) + (f.d->overline << 2) + (f.d->strikeOut<<1) + f.d->kerning;
1838 int f2attrs = (d->underline << 3) + (d->overline << 2) + (d->strikeOut<<1) + d->kerning;
1839 if (f1attrs != f2attrs)
return f1attrs < f2attrs;
1841 if (d->features.size() != f.d->features.size())
1842 return f.d->features.size() < d->features.size();
1845 auto it = d->features.constBegin();
1846 auto jt = f.d->features.constBegin();
1847 for (; it != d->features.constEnd(); ++it, ++jt) {
1848 if (it.key() != jt.key())
1849 return jt.key() < it.key();
1850 if (it.value() != jt.value())
1851 return jt.value() < it.value();
1855 if (r1.variableAxisValues.size() != r2.variableAxisValues.size())
1856 return r1.variableAxisValues.size() < r2.variableAxisValues.size();
1859 auto it = r1.variableAxisValues.constBegin();
1860 auto jt = r2.variableAxisValues.constBegin();
1861 for (; it != r1.variableAxisValues.constEnd(); ++it, ++jt) {
1862 if (it.key() != jt.key())
1863 return jt.key() < it.key();
1864 if (it.value() != jt.value())
1865 return jt.value() < it.value();
1874
1875
1876
1877
1878
1879
1880
1881
1882bool QFont::operator!=(
const QFont &f)
const
1884 return !(operator==(f));
1888
1889
1890QFont::operator QVariant()
const
1892 return QVariant::fromValue(*
this);
1896
1897
1898
1899
1900
1901
1902bool QFont::isCopyOf(
const QFont & f)
const
1908
1909
1910
1911QFont QFont::resolve(
const QFont &other)
const
1913 if (resolve_mask == 0 || (resolve_mask == other.resolve_mask && *
this == other)) {
1915 o.resolve_mask = resolve_mask;
1921 font.d->resolve(resolve_mask, other.d.data());
1927
1928
1929
1932
1933
1934
1938
1939
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955QString QFont::substitute(
const QString &familyName)
1957 QFontSubst *fontSubst = globalFontSubst();
1958 Q_ASSERT(fontSubst !=
nullptr);
1959 QFontSubst::ConstIterator it = fontSubst->constFind(familyName.toLower());
1960 if (it != fontSubst->constEnd() && !(*it).isEmpty())
1961 return (*it).first();
1968
1969
1970
1971
1972
1973
1974
1975
1976QStringList QFont::substitutes(
const QString &familyName)
1978 QFontSubst *fontSubst = globalFontSubst();
1979 Q_ASSERT(fontSubst !=
nullptr);
1980 return fontSubst->value(familyName.toLower(), QStringList());
1985
1986
1987
1988
1989
1990
1991
1992
1993void QFont::insertSubstitution(
const QString &familyName,
1994 const QString &substituteName)
1996 QFontSubst *fontSubst = globalFontSubst();
1997 Q_ASSERT(fontSubst !=
nullptr);
1998 QStringList &list = (*fontSubst)[familyName.toLower()];
1999 QString s = substituteName.toLower();
2000 if (!list.contains(s))
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015void QFont::insertSubstitutions(
const QString &familyName,
2016 const QStringList &substituteNames)
2018 QFontSubst *fontSubst = globalFontSubst();
2019 Q_ASSERT(fontSubst !=
nullptr);
2020 QStringList &list = (*fontSubst)[familyName.toLower()];
2021 for (
const QString &substituteName : substituteNames) {
2022 const QString lowerSubstituteName = substituteName.toLower();
2023 if (!list.contains(lowerSubstituteName))
2024 list.append(lowerSubstituteName);
2029
2030
2031
2032
2033
2034void QFont::removeSubstitutions(
const QString &familyName)
2036 QFontSubst *fontSubst = globalFontSubst();
2037 Q_ASSERT(fontSubst !=
nullptr);
2038 fontSubst->remove(familyName.toLower());
2042
2043
2044
2045
2046QStringList QFont::substitutions()
2048 QFontSubst *fontSubst = globalFontSubst();
2049 Q_ASSERT(fontSubst !=
nullptr);
2050 QStringList ret = fontSubst->keys();
2056#ifndef QT_NO_DATASTREAM
2058
2059
2060
2063 Q_ASSERT(f !=
nullptr);
2065 if (f->request.style)
2073 if (f->request.fixedPitch)
2077 if (version >= QDataStream::Qt_4_0) {
2081 if (f->request.style == QFont::StyleOblique)
2088 Q_ASSERT(f !=
nullptr);
2090 if (f->request.ignorePitch)
2092 if (f->letterSpacingIsAbsolute)
2098
2099
2100
2103 Q_ASSERT(f !=
nullptr);
2104 f->request.style = (bits & 0x01) != 0 ? QFont::StyleItalic : QFont::StyleNormal;
2105 f->underline = (bits & 0x02) != 0;
2106 f->overline = (bits & 0x40) != 0;
2107 f->strikeOut = (bits & 0x04) != 0;
2108 f->request.fixedPitch = (bits & 0x08) != 0;
2110 if (version >= QDataStream::Qt_4_0)
2111 f->kerning = (bits & 0x10) != 0;
2112 if ((bits & 0x80) != 0)
2113 f->request.style = QFont::StyleOblique;
2118 Q_ASSERT(f !=
nullptr);
2119 f->request.ignorePitch = (bits & 0x01) != 0;
2120 f->letterSpacingIsAbsolute = (bits & 0x02) != 0;
2125
2126
2127
2128
2129
2130QString QFont::key()
const
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161QString QFont::toString()
const
2163 const QChar comma(u',');
2164 QString fontDescription = family() + comma +
2165 QString::number( pointSizeF()) + comma +
2166 QString::number( pixelSize()) + comma +
2167 QString::number((
int) styleHint()) + comma +
2168 QString::number( weight()) + comma +
2169 QString::number((
int) style()) + comma +
2170 QString::number((
int) underline()) + comma +
2171 QString::number((
int) strikeOut()) + comma +
2172 QString::number((
int)fixedPitch()) + comma +
2173 QString::number((
int)
false) + comma +
2174 QString::number((
int)capitalization()) + comma +
2175 QString::number((
int)letterSpacingType()) + comma +
2176 QString::number(letterSpacing()) + comma +
2177 QString::number(wordSpacing()) + comma +
2178 QString::number(stretch()) + comma +
2179 QString::number((
int)styleStrategy());
2181 QString fontStyle = styleName();
2182 if (!fontStyle.isEmpty())
2183 fontDescription += comma + fontStyle;
2185 return fontDescription;
2189
2190
2191
2192
2195 return qHash(QFontPrivate::get(font)->request, seed);
2200
2201
2202
2203
2204
2205
2206bool QFont::fromString(
const QString &descrip)
2208 const auto sr = QStringView(descrip).trimmed();
2209 const auto l = sr.split(u',');
2210 const int count = l.size();
2211 if (!count || (count > 2 && count < 9) || count == 9 || count > 17 ||
2212 l.first().isEmpty()) {
2213 qWarning(
"QFont::fromString: Invalid description '%s'",
2214 descrip.isEmpty() ?
"(empty)" : descrip.toLatin1().data());
2218 setFamily(l[0].toString());
2219 if (count > 1 && l[1].toDouble() > 0.0)
2220 setPointSizeF(l[1].toDouble());
2222 setStyleHint((StyleHint) l[2].toInt());
2223 setWeight(QFont::Weight(l[3].toInt()));
2224 setItalic(l[4].toInt());
2225 setUnderline(l[5].toInt());
2226 setStrikeOut(l[6].toInt());
2227 setFixedPitch(l[7].toInt());
2228 }
else if (count >= 10) {
2229 if (l[2].toInt() > 0)
2230 setPixelSize(l[2].toInt());
2231 setStyleHint((StyleHint) l[3].toInt());
2233 setWeight(QFont::Weight(l[4].toInt()));
2235 setWeight(QFont::Weight(qt_legacyToOpenTypeWeight(l[4].toInt())));
2236 setStyle((QFont::Style)l[5].toInt());
2237 setUnderline(l[6].toInt());
2238 setStrikeOut(l[7].toInt());
2239 setFixedPitch(l[8].toInt());
2241 setCapitalization((Capitalization)l[10].toInt());
2242 setLetterSpacing((SpacingType)l[11].toInt(), l[12].toDouble());
2243 setWordSpacing(l[13].toDouble());
2244 setStretch(l[14].toInt());
2245 setStyleStrategy((StyleStrategy)l[15].toInt());
2247 if (count == 11 || count == 17)
2248 d->request.styleName = l[count - 1].toString();
2250 d->request.styleName.clear();
2253 if (count >= 9 && !d->request.fixedPitch)
2254 d->request.ignorePitch =
true;
2260
2261
2262
2263
2264
2265
2266void QFont::initialize()
2271
2272
2273
2274
2275void QFont::cleanup()
2277 QFontCache::cleanup();
2281
2282
2283
2284void QFont::cacheStatistics()
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
2314
2315
2316
2319
2320
2321
2322
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2338
2339
2340
2341
2342
2345
2346
2347
2350
2351
2352
2353
2354
2355
2358
2359
2360
2361
2362
2363
2366
2367
2368
2369
2370
2371
2374
2375
2376
2377
2378
2379
2380
2383
2384
2385
2386
2387
2388
2389
2390
2391std::optional<QFont::Tag> QFont::Tag::fromString(QAnyStringView view)
noexcept
2393 if (view.size() != 4) {
2394 qWarning(
"The tag name must be exactly 4 characters long!");
2395 return std::nullopt;
2397 const QFont::Tag maybeTag = view.visit([](
auto view) {
2398 using CharType =
decltype(view.at(0));
2399 if constexpr (std::is_same_v<CharType,
char>) {
2400 const char bytes[5] = { view.at(0), view.at(1), view.at(2), view.at(3), 0 };
2403 const char bytes[5] = { view.at(0).toLatin1(), view.at(1).toLatin1(),
2404 view.at(2).toLatin1(), view.at(3).toLatin1(), 0 };
2408 return maybeTag.isValid() ? std::optional<Tag>(maybeTag) : std::nullopt;
2412
2413
2414
2415
2416
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
2454
2455
2456void QFont::setVariableAxis(Tag tag,
float value)
2458 if (tag.isValid()) {
2459 if (resolve_mask & QFont::VariableAxesResolved && d->hasVariableAxis(tag, value))
2464 d->setVariableAxis(tag, value);
2465 resolve_mask |= QFont::VariableAxesResolved;
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479void QFont::unsetVariableAxis(Tag tag)
2481 if (tag.isValid()) {
2484 d->unsetVariableAxis(tag);
2485 resolve_mask |= QFont::VariableAxesResolved;
2490
2491
2492
2493
2494
2495
2496
2497
2498QList<QFont::Tag> QFont::variableAxisTags()
const
2500 return d->request.variableAxisValues.keys();
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513float QFont::variableAxisValue(Tag tag)
const
2515 return d->request.variableAxisValues.value(tag);
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528bool QFont::isVariableAxisSet(Tag tag)
const
2530 return d->request.variableAxisValues.contains(tag);
2534
2535
2536
2537
2538
2539
2540
2541
2542void QFont::clearVariableAxes()
2544 if (d->request.variableAxisValues.isEmpty())
2548 d->request.variableAxisValues.clear();
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
2586
2587
2588void QFont::setFeature(Tag tag, quint32 value)
2590 if (tag.isValid()) {
2591 d->detachButKeepEngineData(
this);
2592 d->setFeature(tag, value);
2593 resolve_mask |= QFont::FeaturesResolved;
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612void QFont::unsetFeature(Tag tag)
2614 if (tag.isValid()) {
2615 d->detachButKeepEngineData(
this);
2616 d->unsetFeature(tag);
2617 resolve_mask |= QFont::FeaturesResolved;
2622
2623
2624
2625
2626
2627
2628
2629
2630QList<QFont::Tag> QFont::featureTags()
const
2632 return d->features.keys();
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645quint32 QFont::featureValue(Tag tag)
const
2647 return d->features.value(tag);
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660bool QFont::isFeatureSet(Tag tag)
const
2662 return d->features.contains(tag);
2666
2667
2668
2669
2670
2671
2672
2673
2674void QFont::clearFeatures()
2676 if (d->features.isEmpty())
2679 d->detachButKeepEngineData(
this);
2680 d->features.clear();
2685 QFont::StyleHint styleHint,
2686 QFontDatabasePrivate::ExtendedScript script);
2689
2690
2691
2692
2693
2694
2695
2696QString QFont::defaultFamily()
const
2698 const QStringList fallbacks = qt_fallbacksForFamily(QString(),
2700 QFont::StyleHint(d->request.styleHint),
2701 QFontDatabasePrivate::Script_Common);
2702 if (!fallbacks.isEmpty())
2703 return fallbacks.first();
2708
2709
2710
2711
2712
2713
2714
2715
2717QStringList QFont::families()
const
2719 return d->request.families;
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2739void QFont::setFamilies(
const QStringList &families)
2741 if ((resolve_mask & QFont::FamiliesResolved) && d->request.families == families)
2744 d->request.families = families;
2745 resolve_mask |= QFont::FamiliesResolved;
2750
2751
2752#ifndef QT_NO_DATASTREAM
2755
2756
2757
2758
2759
2760
2761
2762QDataStream &operator<<(QDataStream &s,
const QFont &font)
2764 if (s.version() == 1) {
2765 s << font.d->request.families.constFirst().toLatin1();
2767 s << font.d->request.families.constFirst();
2768 if (s.version() >= QDataStream::Qt_5_4)
2769 s << font.d->request.styleName;
2772 if (s.version() >= QDataStream::Qt_4_0) {
2774 double pointSize = font.d->request.pointSize;
2775 qint32 pixelSize = font.d->request.pixelSize;
2778 }
else if (s.version() <= 3) {
2779 qint16 pointSize = (qint16) (font.d->request.pointSize * 10);
2780 if (pointSize < 0) {
2781 pointSize = (qint16)QFontInfo(font).pointSize() * 10;
2785 s << (qint16) (font.d->request.pointSize * 10);
2786 s << (qint16) font.d->request.pixelSize;
2789 s << (quint8) font.d->request.styleHint;
2790 if (s.version() >= QDataStream::Qt_3_1) {
2793 if (s.version() >= QDataStream::Qt_5_4)
2794 s << (quint16) font.d->request.styleStrategy;
2796 s << (quint8) font.d->request.styleStrategy;
2799 if (s.version() < QDataStream::Qt_6_0)
2800 s << quint8(0) << quint8(qt_openTypeToLegacyWeight(font.d->request.weight));
2802 s << quint16(font.d->request.weight);
2804 s << get_font_bits(s.version(), font.d.data());
2805 if (s.version() >= QDataStream::Qt_4_3)
2806 s << (quint16)font.d->request.stretch;
2807 if (s.version() >= QDataStream::Qt_4_4)
2808 s << get_extended_font_bits(font.d.data());
2809 if (s.version() >= QDataStream::Qt_4_5) {
2810 s << font.d->letterSpacing.value();
2811 s << font.d->wordSpacing.value();
2813 if (s.version() >= QDataStream::Qt_5_4)
2814 s << (quint8)font.d->request.hintingPreference;
2815 if (s.version() >= QDataStream::Qt_5_6)
2816 s << (quint8)font.d->capital;
2817 if (s.version() >= QDataStream::Qt_5_13) {
2818 if (s.version() < QDataStream::Qt_6_0)
2819 s << font.d->request.families.mid(1);
2821 s << font.d->request.families;
2823 if (s.version() >= QDataStream::Qt_6_6)
2824 s << font.d->features;
2825 if (s.version() >= QDataStream::Qt_6_7)
2826 s << font.d->request.variableAxisValues;
2832
2833
2834
2835
2836
2837
2838
2841 font.d =
new QFontPrivate;
2842 font.resolve_mask = QFont::AllPropertiesResolved;
2844 quint8 styleHint, bits;
2845 quint16 styleStrategy = QFont::PreferDefault;
2847 if (s.version() == 1) {
2850 font.d->request.families = QStringList(QString::fromLatin1(fam));
2854 font.d->request.families = QStringList(fam);
2855 if (s.version() >= QDataStream::Qt_5_4)
2856 s >> font.d->request.styleName;
2859 if (s.version() >= QDataStream::Qt_4_0) {
2865 font.d->request.pointSize = qreal(pointSize);
2866 font.d->request.pixelSize = pixelSize;
2868 qint16 pointSize, pixelSize = -1;
2870 if (s.version() >= 4)
2872 font.d->request.pointSize = qreal(pointSize / 10.);
2873 font.d->request.pixelSize = pixelSize;
2876 if (s.version() >= QDataStream::Qt_3_1) {
2877 if (s.version() >= QDataStream::Qt_5_4) {
2880 quint8 tempStyleStrategy;
2881 s >> tempStyleStrategy;
2882 styleStrategy = tempStyleStrategy;
2886 if (s.version() < QDataStream::Qt_6_0) {
2891 font.d->request.weight = qt_legacyToOpenTypeWeight(weight);
2895 font.d->request.weight = weight;
2900 font.d->request.styleHint = styleHint;
2901 font.d->request.styleStrategy = styleStrategy;
2903 set_font_bits(s.version(), bits, font.d.data());
2905 if (s.version() >= QDataStream::Qt_4_3) {
2908 font.d->request.stretch = stretch;
2911 if (s.version() >= QDataStream::Qt_4_4) {
2912 quint8 extendedBits;
2914 set_extended_font_bits(extendedBits, font.d.data());
2916 if (s.version() >= QDataStream::Qt_4_5) {
2919 font.d->letterSpacing.setValue(value);
2921 font.d->wordSpacing.setValue(value);
2923 if (s.version() >= QDataStream::Qt_5_4) {
2926 font.d->request.hintingPreference = QFont::HintingPreference(value);
2928 if (s.version() >= QDataStream::Qt_5_6) {
2931 font.d->capital = QFont::Capitalization(value);
2933 if (s.version() >= QDataStream::Qt_5_13) {
2936 if (s.version() < QDataStream::Qt_6_0)
2937 font.d->request.families.append(value);
2939 font.d->request.families = value;
2941 if (s.version() >= QDataStream::Qt_6_6) {
2942 font.d->features.clear();
2943 s >> font.d->features;
2945 if (s.version() >= QDataStream::Qt_6_7) {
2946 font.d->request.variableAxisValues.clear();
2947 s >> font.d->request.variableAxisValues;
2953QDataStream &operator<<(QDataStream &stream, QFont::Tag tag)
2955 stream << tag.value();
2959QDataStream &
operator>>(QDataStream &stream, QFont::Tag &tag)
2963 if (
const auto maybeTag = QFont::Tag::fromValue(value))
2966 stream.setStatus(QDataStream::ReadCorruptData);
2974
2975
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
3049
3050
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068QFontInfo::QFontInfo(
const QFont &font)
3074
3075
3076QFontInfo::QFontInfo(
const QFontInfo &fi)
3082
3083
3084QFontInfo::~QFontInfo()
3089
3090
3091QFontInfo &QFontInfo::operator=(
const QFontInfo &fi)
3098
3099
3100
3101
3104
3105
3106
3107
3108QString QFontInfo::family()
const
3110 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3111 Q_ASSERT(engine !=
nullptr);
3112 return engine->fontDef.families.isEmpty() ? QString() : engine->fontDef.families.constFirst();
3116
3117
3118
3119
3120
3121
3122
3123QString QFontInfo::styleName()
const
3125 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3126 Q_ASSERT(engine !=
nullptr);
3127 return engine->fontDef.styleName;
3131
3132
3133
3134
3135int QFontInfo::pointSize()
const
3137 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3138 Q_ASSERT(engine !=
nullptr);
3139 return qRound(engine->fontDef.pointSize);
3143
3144
3145
3146
3147qreal QFontInfo::pointSizeF()
const
3149 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3150 Q_ASSERT(engine !=
nullptr);
3151 return engine->fontDef.pointSize;
3155
3156
3157
3158
3159int QFontInfo::pixelSize()
const
3161 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3162 Q_ASSERT(engine !=
nullptr);
3163 return engine->fontDef.pixelSize;
3167
3168
3169
3170
3171bool QFontInfo::italic()
const
3173 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3174 Q_ASSERT(engine !=
nullptr);
3175 return engine->fontDef.style != QFont::StyleNormal;
3179
3180
3181
3182
3183QFont::Style QFontInfo::style()
const
3185 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3186 Q_ASSERT(engine !=
nullptr);
3187 return (QFont::Style)engine->fontDef.style;
3191#if QT_DEPRECATED_SINCE(6
, 0
)
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205int QFontInfo::legacyWeight()
const
3207 return qt_openTypeToLegacyWeight(weight());
3213
3214
3215
3216
3217int QFontInfo::weight()
const
3219 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3220 Q_ASSERT(engine !=
nullptr);
3221 return engine->fontDef.weight;
3226
3227
3228
3229
3230
3231
3232
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244bool QFontInfo::underline()
const
3246 return d->underline;
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259bool QFontInfo::overline()
const
3265
3266
3267
3268
3269
3270
3271
3272bool QFontInfo::strikeOut()
const
3274 return d->strikeOut;
3278
3279
3280
3281
3282bool QFontInfo::fixedPitch()
const
3284 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3285 Q_ASSERT(engine !=
nullptr);
3287 if (!engine->fontDef.fixedPitchComputed) {
3288 QChar ch[2] = { u'i', u'm' };
3289 QGlyphLayoutArray<2> g;
3291 if (engine->stringToCMap(ch, 2, &g, &l, {}) < 0)
3294 engine->fontDef.fixedPitch = g.advances[0] == g.advances[1];
3295 engine->fontDef.fixedPitchComputed =
true;
3298 return engine->fontDef.fixedPitch;
3302
3303
3304
3305
3306
3307
3308QFont::StyleHint QFontInfo::styleHint()
const
3310 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3311 Q_ASSERT(engine !=
nullptr);
3312 return (QFont::StyleHint) engine->fontDef.styleHint;
3316
3317
3318
3319
3320
3321bool QFontInfo::exactMatch()
const
3323 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3324 Q_ASSERT(engine !=
nullptr);
3325 return d->request.exactMatch(engine->fontDef);
3329
3330
3331
3332
3333
3334
3335
3336QList<QFontVariableAxis> QFontInfo::variableAxes()
const
3338 QFontEngine *engine = d->engineForScript(QChar::Script_Common);
3339 Q_ASSERT(engine !=
nullptr);
3340 return engine->variableAxes();
3348using namespace std::chrono_literals;
3350#ifdef QFONTCACHE_DEBUG
3352static constexpr auto fast_timeout = 1s;
3353static constexpr auto slow_timeout = 5s;
3359#ifndef QFONTCACHE_MIN_COST
3360# define QFONTCACHE_MIN_COST 4
*1024
3365QFontCache *QFontCache::instance()
3367 QFontCache *&fontCache = theFontCache()->localData();
3369 fontCache =
new QFontCache;
3373void QFontCache::cleanup()
3375 QThreadStorage<QFontCache *> *cache =
nullptr;
3377 cache = theFontCache();
3378 } QT_CATCH (
const std::bad_alloc &) {
3381 if (cache && cache->hasLocalData())
3382 cache->setLocalData(
nullptr);
3385Q_CONSTINIT
static QBasicAtomicInt font_cache_id = Q_BASIC_ATOMIC_INITIALIZER(0);
3387QFontCache::QFontCache()
3388 : QObject(), total_cost(0), max_cost(min_cost),
3389 current_timestamp(0), fast(
false),
3390 autoClean(QGuiApplication::instance()
3391 && (QGuiApplication::instance()->thread() == QThread::currentThread())),
3392 m_id(font_cache_id.fetchAndAddRelaxed(1) + 1)
3396QFontCache::~QFontCache()
3401void QFontCache::clear()
3404 EngineDataCache::Iterator it = engineDataCache.begin(),
3405 end = engineDataCache.end();
3407 QFontEngineData *data = it.value();
3408 for (
int i = 0; i < QFontDatabasePrivate::ScriptCount; ++i) {
3409 if (data->engines[i]) {
3410 if (!data->engines[i]->ref.deref()) {
3411 Q_ASSERT(engineCacheCount.value(data->engines[i]) == 0);
3412 delete data->engines[i];
3414 data->engines[i] =
nullptr;
3417 if (!data->ref.deref()) {
3420 FC_DEBUG(
"QFontCache::clear: engineData %p still has refcount %d",
3421 data, data->ref.loadRelaxed());
3427 engineDataCache.clear();
3430 bool mightHaveEnginesLeftForCleanup;
3432 mightHaveEnginesLeftForCleanup =
false;
3433 for (EngineCache::Iterator it = engineCache.begin(), end = engineCache.end();
3435 QFontEngine *engine = it.value().data;
3437 const int cacheCount = --engineCacheCount[engine];
3438 Q_ASSERT(cacheCount >= 0);
3439 if (!engine->ref.deref()) {
3440 Q_ASSERT(cacheCount == 0);
3441 mightHaveEnginesLeftForCleanup = engine->type() == QFontEngine::Multi;
3443 }
else if (cacheCount == 0) {
3444 FC_DEBUG(
"QFontCache::clear: engine %p still has refcount %d",
3445 engine, engine->ref.loadRelaxed());
3447 it.value().data =
nullptr;
3450 }
while (mightHaveEnginesLeftForCleanup);
3452 engineCache.clear();
3453 engineCacheCount.clear();
3457 max_cost = min_cost;
3461QFontEngineData *QFontCache::findEngineData(
const QFontDef &def)
const
3463 EngineDataCache::ConstIterator it = engineDataCache.constFind(def);
3464 if (it == engineDataCache.constEnd())
3471void QFontCache::insertEngineData(
const QFontDef &def, QFontEngineData *engineData)
3473#ifdef QFONTCACHE_DEBUG
3474 FC_DEBUG(
"QFontCache: inserting new engine data %p", engineData);
3475 if (engineDataCache.contains(def)) {
3476 FC_DEBUG(
" QFontCache already contains engine data %p for key=(%g %g %d %d %d)",
3477 engineDataCache.value(def), def.pointSize,
3478 def.pixelSize, def.weight, def.style, def.fixedPitch);
3481 Q_ASSERT(!engineDataCache.contains(def));
3483 engineData->ref.ref();
3488 engineDataCache.insert(def, engineData);
3489 increaseCost(
sizeof(QFontEngineData));
3492QFontEngine *QFontCache::findEngine(
const Key &key)
3494 EngineCache::Iterator it = engineCache.find(key),
3495 end = engineCache.end();
3496 if (it == end)
return nullptr;
3498 Q_ASSERT(it.value().data !=
nullptr);
3499 Q_ASSERT(key.multi == (it.value().data->type() == QFontEngine::Multi));
3502 updateHitCountAndTimeStamp(it.value());
3504 return it.value().data;
3507void QFontCache::updateHitCountAndTimeStamp(Engine &value)
3510 value.timestamp = ++current_timestamp;
3512 FC_DEBUG(
"QFontCache: found font engine\n"
3513 " %p: timestamp %4u hits %3u ref %2d/%2d, type %d",
3514 value.data, value.timestamp, value.hits,
3515 value.data->ref.loadRelaxed(), engineCacheCount.value(value.data),
3516 value.data->type());
3519void QFontCache::insertEngine(
const Key &key, QFontEngine *engine,
bool insertMulti)
3521 Q_ASSERT(engine !=
nullptr);
3522 Q_ASSERT(key.multi == (engine->type() == QFontEngine::Multi));
3524#ifdef QFONTCACHE_DEBUG
3525 FC_DEBUG(
"QFontCache: inserting new engine %p, refcount %d", engine, engine->ref.loadRelaxed());
3526 if (!insertMulti && engineCache.contains(key)) {
3527 FC_DEBUG(
" QFontCache already contains engine %p for key=(%g %g %d %d %d)",
3528 engineCache.value(key).data, key.def.pointSize,
3529 key.def.pixelSize, key.def.weight, key.def.style, key.def.fixedPitch);
3537 Engine data(engine);
3538 data.timestamp = ++current_timestamp;
3541 engineCache.insert(key, data);
3543 engineCache.replace(key, data);
3545 if (++engineCacheCount[engine] == 1)
3546 increaseCost(engine->cache_cost);
3549void QFontCache::increaseCost(uint cost)
3551 cost = (cost + 512) / 1024;
3552 cost = cost > 0 ? cost : 1;
3555 FC_DEBUG(
" COST: increased %u kb, total_cost %u kb, max_cost %u kb",
3556 cost, total_cost, max_cost);
3558 if (total_cost > max_cost) {
3559 max_cost = total_cost;
3564 if (!timer.isActive() || ! fast) {
3565 FC_DEBUG(
" TIMER: starting fast timer (%d s)",
static_cast<
int>(fast_timeout.count()));
3567 timer.start(fast_timeout,
this);
3573void QFontCache::decreaseCost(uint cost)
3575 cost = (cost + 512) / 1024;
3576 cost = cost > 0 ? cost : 1;
3577 Q_ASSERT(cost <= total_cost);
3580 FC_DEBUG(
" COST: decreased %u kb, total_cost %u kb, max_cost %u kb",
3581 cost, total_cost, max_cost);
3584void QFontCache::timerEvent(QTimerEvent *)
3586 FC_DEBUG(
"QFontCache::timerEvent: performing cache maintenance (timestamp %u)",
3589 if (total_cost <= max_cost && max_cost <= min_cost) {
3590 FC_DEBUG(
" cache redused sufficiently, stopping timer");
3600void QFontCache::decreaseCache()
3603 uint in_use_cost = 0;
3609 const uint engine_data_cost =
3610 sizeof(QFontEngineData) > 1024 ?
sizeof(QFontEngineData) : 1024;
3612 EngineDataCache::ConstIterator it = engineDataCache.constBegin(),
3613 end = engineDataCache.constEnd();
3614 for (; it != end; ++it) {
3615 FC_DEBUG(
" %p: ref %2d", it.value(),
int(it.value()->ref.loadRelaxed()));
3617 if (it.value()->ref.loadRelaxed() != 1)
3618 in_use_cost += engine_data_cost;
3625 EngineCache::ConstIterator it = engineCache.constBegin(),
3626 end = engineCache.constEnd();
3627 for (; it != end; ++it) {
3628 FC_DEBUG(
" %p: timestamp %4u hits %2u ref %2d/%2d, cost %u bytes",
3629 it.value().data, it.value().timestamp, it.value().hits,
3630 it.value().data->ref.loadRelaxed(), engineCacheCount.value(it.value().data),
3631 it.value().data->cache_cost);
3633 if (it.value().data->ref.loadRelaxed() > engineCacheCount.value(it.value().data))
3634 in_use_cost += it.value().data->cache_cost / engineCacheCount.value(it.value().data);
3638 in_use_cost += engineCache.size();
3641 in_use_cost = (in_use_cost + 512) / 1024;
3644
3645
3646
3647
3648
3649
3650
3651 uint new_max_cost = qMax(qMax(max_cost / 2, in_use_cost), min_cost);
3653 FC_DEBUG(
" after sweep, in use %u kb, total %u kb, max %u kb, new max %u kb",
3654 in_use_cost, total_cost, max_cost, new_max_cost);
3657 if (new_max_cost == max_cost) {
3659 FC_DEBUG(
" cannot shrink cache, slowing timer");
3661 if (timer.isActive()) {
3662 timer.start(slow_timeout,
this);
3667 }
else if (! fast) {
3668 FC_DEBUG(
" dropping into passing gear");
3670 timer.start(fast_timeout,
this);
3675 max_cost = new_max_cost;
3681 EngineDataCache::Iterator it = engineDataCache.begin();
3682 while (it != engineDataCache.end()) {
3683 if (it.value()->ref.loadRelaxed() == 1) {
3685 decreaseCost(
sizeof(QFontEngineData));
3686 it.value()->ref.deref();
3688 it = engineDataCache.erase(it);
3698 bool cost_decreased;
3700 cost_decreased =
false;
3702 EngineCache::Iterator it = engineCache.begin(),
3703 end = engineCache.end();
3706 uint least_popular = ~0u;
3708 EngineCache::Iterator jt = end;
3710 for ( ; it != end; ++it) {
3711 if (it.value().data->ref.loadRelaxed() != engineCacheCount.value(it.value().data))
3714 if (it.value().timestamp < oldest && it.value().hits <= least_popular) {
3715 oldest = it.value().timestamp;
3716 least_popular = it.value().hits;
3723 FC_DEBUG(
" %p: timestamp %4u hits %2u ref %2d/%2d, type %d",
3724 it.value().data, it.value().timestamp, it.value().hits,
3725 it.value().data->ref.loadRelaxed(), engineCacheCount.value(it.value().data),
3726 it.value().data->type());
3728 QFontEngine *fontEngine = it.value().data;
3730 it = engineCache.begin();
3731 while (it != engineCache.end()) {
3732 if (it.value().data == fontEngine) {
3733 fontEngine->ref.deref();
3734 it = engineCache.erase(it);
3740 Q_ASSERT(fontEngine->ref.loadRelaxed() == 0);
3741 decreaseCost(fontEngine->cache_cost);
3743 engineCacheCount.remove(fontEngine);
3745 cost_decreased =
true;
3747 }
while (cost_decreased && total_cost > max_cost);
3751#ifndef QT_NO_DEBUG_STREAM
3752QDebug operator<<(QDebug stream,
const QFont &font)
3754 QDebugStateSaver saver(stream);
3755 stream.nospace().noquote();
3758 if (stream.verbosity() == QDebug::DefaultVerbosity) {
3759 stream << font.toString() <<
")";
3763 QString fontDescription;
3764 QDebug debug(&fontDescription);
3767 const QFont defaultFont(
new QFontPrivate);
3769 for (
int property = QFont::SizeResolved; property < QFont::AllPropertiesResolved; property <<= 1) {
3770 const bool resolved = (font.resolve_mask & property) != 0;
3771 if (!resolved && stream.verbosity() == QDebug::MinimumVerbosity)
3774 #define QFONT_DEBUG_SKIP_DEFAULT(prop)
3775 if ((font.prop() == defaultFont.prop()) && stream.verbosity() == 1
)
3778 QDebugStateSaver saver(debug);
3781 case QFont::SizeResolved:
3782 if (font.pointSizeF() >= 0)
3783 debug << font.pointSizeF() <<
"pt";
3784 else if (font.pixelSize() >= 0)
3785 debug << font.pixelSize() <<
"px";
3789 case QFont::StyleHintResolved:
3791 debug.verbosity(1) << font.styleHint();
break;
3792 case QFont::StyleStrategyResolved:
3794 debug.verbosity(1) << font.styleStrategy();
break;
3795 case QFont::WeightResolved:
3796 debug.verbosity(1) << QFont::Weight(font.weight());
break;
3797 case QFont::StyleResolved:
3799 debug.verbosity(0) << font.style();
break;
3800 case QFont::UnderlineResolved:
3802 debug <<
"underline=" << font.underline();
break;
3803 case QFont::OverlineResolved:
3805 debug <<
"overline=" << font.overline();
break;
3806 case QFont::StrikeOutResolved:
3808 debug <<
"strikeOut=" << font.strikeOut();
break;
3809 case QFont::FixedPitchResolved:
3811 debug <<
"fixedPitch=" << font.fixedPitch();
break;
3812 case QFont::StretchResolved:
3814 debug.verbosity(0) << QFont::Stretch(font.stretch());
break;
3815 case QFont::KerningResolved:
3817 debug <<
"kerning=" << font.kerning();
break;
3818 case QFont::CapitalizationResolved:
3820 debug.verbosity(0) << font.capitalization();
break;
3821 case QFont::LetterSpacingResolved:
3823 debug <<
"letterSpacing=" << font.letterSpacing();
3824 debug.verbosity(0) <<
" (" << font.letterSpacingType() <<
")";
3826 case QFont::HintingPreferenceResolved:
3828 debug.verbosity(0) << font.hintingPreference();
break;
3829 case QFont::StyleNameResolved:
3831 debug <<
"styleName=" << font.styleName();
break;
3836 #undef QFONT_DEBUG_SKIP_DEFAULT
3841 if (stream.verbosity() > QDebug::MinimumVerbosity)
3842 debug.verbosity(0) <<
"resolveMask=" << QFlags<QFont::ResolveProperties>(font.resolve_mask);
3844 fontDescription.chop(2);
3846 stream << fontDescription <<
')';
3851QDebug operator<<(QDebug debug, QFont::Tag tag)
3853 QDebugStateSaver saver(debug);
3854 debug.noquote() << tag.toString();
3861#include "moc_qfont.cpp"
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