From 1d14067680f02f47b2f8ff375c44eb64633eec02 Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Wed, 18 Nov 2020 13:45:19 +0100 Subject: [PATCH] Fix weight when reading old serialized QFonts The QFont::fromString() needs to differ between strings produced before and after Qt 6.0 when interpreting the weight value, since in older strings this will be the legacy scale. Luckily the number of tokens in the string can be used for this purpose, since many tokens were added in Qt 6.0. This broke KDE, where font settings are stored in QSettings and serialized using QFont::toString() from Qt 5. Fixes: QTBUG-88589 Pick-to: 6.0.0 6.0 Change-Id: I199737fed61917f8b9d8f86176ead29a89eb8e0c Reviewed-by: Andy Shaw --- src/gui/text/qfont.cpp | 5 ++++- tests/auto/gui/kernel/qguivariant/test/tst_qguivariant.cpp | 2 +- tests/auto/gui/text/qfont/tst_qfont.cpp | 6 +++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/gui/text/qfont.cpp b/src/gui/text/qfont.cpp index 0e680a3c6d..2021363da5 100644 --- a/src/gui/text/qfont.cpp +++ b/src/gui/text/qfont.cpp @@ -2186,7 +2186,10 @@ bool QFont::fromString(const QString &descrip) if (l[2].toInt() > 0) setPixelSize(l[2].toInt()); setStyleHint((StyleHint) l[3].toInt()); - setWeight(QFont::Weight(l[4].toInt())); + if (count >= 16) + setWeight(QFont::Weight(l[4].toInt())); + else + setWeight(QFont::Weight(qt_legacyToOpenTypeWeight(l[4].toInt()))); setStyle((QFont::Style)l[5].toInt()); setUnderline(l[6].toInt()); setStrikeOut(l[7].toInt()); diff --git a/tests/auto/gui/kernel/qguivariant/test/tst_qguivariant.cpp b/tests/auto/gui/kernel/qguivariant/test/tst_qguivariant.cpp index 5a01263d80..9ef5ce75bf 100644 --- a/tests/auto/gui/kernel/qguivariant/test/tst_qguivariant.cpp +++ b/tests/auto/gui/kernel/qguivariant/test/tst_qguivariant.cpp @@ -334,7 +334,7 @@ void tst_QGuiVariant::toFont_data() QTest::addColumn("result"); QFont f("times",12,-1,false); - QTest::newRow( "string" ) << QVariant( QString( "times,12,-1,5,400,0,0,0,0,0" ) ) << f; + QTest::newRow( "string" ) << QVariant( QString( "times,12,-1,5,50,0,0,0,0,0" ) ) << f; } void tst_QGuiVariant::toFont() diff --git a/tests/auto/gui/text/qfont/tst_qfont.cpp b/tests/auto/gui/text/qfont/tst_qfont.cpp index 7af48eda01..2e87cc8e50 100644 --- a/tests/auto/gui/text/qfont/tst_qfont.cpp +++ b/tests/auto/gui/text/qfont/tst_qfont.cpp @@ -602,7 +602,7 @@ void tst_QFont::toAndFromString() // Since Qt 6.0 it was changed to include more information in the description, so // this checks for compatibility - const QString fontStringFrom515(QLatin1String("Times New Roman,18,-1,5,700,1,0,0,1,0,Regular")); + const QString fontStringFrom515(QLatin1String("Times New Roman,18,-1,5,75,1,0,0,1,0,Regular")); QFont fontFrom515("Times New Roman", 18); fontFrom515.setBold(true); fontFrom515.setItalic(true); @@ -629,7 +629,7 @@ void tst_QFont::toAndFromString() void tst_QFont::fromStringWithoutStyleName() { QFont font1; - font1.fromString("Noto Sans,12,-1,5,400,0,0,0,0,0,Regular"); + font1.fromString("Noto Sans,12,-1,5,50,0,0,0,0,0,Regular"); QFont font2 = font1; const QString str = "Times,16,-1,5,400,0,0,0,0,0,0,0,0,0,0,1"; @@ -640,7 +640,7 @@ void tst_QFont::fromStringWithoutStyleName() const QString fontStringFrom60( QLatin1String("Times New Roman,18,-1,5,700,1,0,0,1,0,1,0,150.5,2.5,50,2")); QFont font3; - font3.fromString("Noto Sans,12,-1,5,400,0,0,0,0,0,Regular"); + font3.fromString("Noto Sans,12,-1,5,50,0,0,0,0,0,Regular"); QFont font4 = font3; font4.fromString(fontStringFrom60); QCOMPARE(font4.toString(), fontStringFrom60);