summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKonstantin Shegunov <kshegunov@gmail.com>2016-07-29 09:24:58 +0300
committerKonstantin Shegunov <kshegunov@gmail.com>2016-08-03 11:47:58 +0000
commit5dd907bf639f43be7f2cdfdd44ec3795f1b31bf6 (patch)
treeec880e94319486587fc78e529f02afb3d4f45e2d
parent5a133a404ebaa5233d5370ace10ba9532b9c6adc (diff)
Save QFont's style name if present
Font style names are quite irregular and the simplistic matching implemented in QFontDatabase::styleString(const QFont &) is unable to properly resolve the style name when font is recreated from a string. This causes the fonts before and after serialization to be considered different, even though they are not. The from/toString methods were made to write and respect the exact font style. [ChangeLog][QtGui][Important Behavior Changes] QFont::toString() and QFont::key() were modified to save the font's style name if one is set, invalidating any stored font identifiers. QFont::fromString() was also adjusted to accommodate the change. Task-number: QTBUG-54936 Change-Id: Ibc7c54119acdd8f0950d6049cc89f859bf981504 Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io> Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
-rw-r--r--src/gui/text/qfont.cpp13
-rw-r--r--tests/auto/gui/text/qfont/tst_qfont.cpp21
2 files changed, 32 insertions, 2 deletions
diff --git a/src/gui/text/qfont.cpp b/src/gui/text/qfont.cpp
index 12631b3a73..0fa32dc56d 100644
--- a/src/gui/text/qfont.cpp
+++ b/src/gui/text/qfont.cpp
@@ -2001,7 +2001,7 @@ QString QFont::key() const
QString QFont::toString() const
{
const QChar comma(QLatin1Char(','));
- return family() + comma +
+ QString fontDescription = family() + comma +
QString::number( pointSizeF()) + comma +
QString::number( pixelSize()) + comma +
QString::number((int) styleHint()) + comma +
@@ -2011,6 +2011,12 @@ QString QFont::toString() const
QString::number((int) strikeOut()) + comma +
QString::number((int)fixedPitch()) + comma +
QString::number((int) false);
+
+ QString fontStyle = styleName();
+ if (!fontStyle.isEmpty())
+ fontDescription += comma + fontStyle;
+
+ return fontDescription;
}
/*!
@@ -2054,7 +2060,7 @@ bool QFont::fromString(const QString &descrip)
setUnderline(l[5].toInt());
setStrikeOut(l[6].toInt());
setFixedPitch(l[7].toInt());
- } else if (count == 10) {
+ } else if (count >= 10) {
if (l[2].toInt() > 0)
setPixelSize(l[2].toInt());
setStyleHint((StyleHint) l[3].toInt());
@@ -2063,7 +2069,10 @@ bool QFont::fromString(const QString &descrip)
setUnderline(l[6].toInt());
setStrikeOut(l[7].toInt());
setFixedPitch(l[8].toInt());
+ if (count == 11)
+ d->request.styleName = l[10].toString();
}
+
if (count >= 9 && !d->request.fixedPitch) // assume 'false' fixedPitch equals default
d->request.ignorePitch = true;
diff --git a/tests/auto/gui/text/qfont/tst_qfont.cpp b/tests/auto/gui/text/qfont/tst_qfont.cpp
index 894b197968..ca984a26a5 100644
--- a/tests/auto/gui/text/qfont/tst_qfont.cpp
+++ b/tests/auto/gui/text/qfont/tst_qfont.cpp
@@ -62,6 +62,7 @@ private slots:
void styleName();
void defaultFamily_data();
void defaultFamily();
+ void toAndFromString();
void sharing();
};
@@ -539,6 +540,26 @@ void tst_QFont::defaultFamily()
QVERIFY2(isAcceptable, msgNotAcceptableFont(familyForHint, acceptableFamilies));
}
+void tst_QFont::toAndFromString()
+{
+ QFont defaultFont = QGuiApplication::font();
+ QString family = defaultFont.family();
+
+ QFontDatabase fdb;
+ const QStringList stylesList = fdb.styles(family);
+ if (stylesList.size() == 0)
+ QSKIP("Default font doesn't have any styles");
+
+ for (const QString &style : stylesList) {
+ QFont result;
+ QFont initial = fdb.font(family, style, defaultFont.pointSize());
+
+ result.fromString(initial.toString());
+
+ QCOMPARE(result, initial);
+ }
+}
+
void tst_QFont::sharing()
{
// QFontCache references the engineData