QFont: Extend the string description to include the missing elements

This extends to/fromString to include style strategy, capitalization,
letter and word spacing and stretch. QFont::fromString() keeps
compatibility with strings from earlier versions as well.

Fixes: QTBUG-67687
Change-Id: I5e95a58f1cd850214af2a7d8906a214facd4e661
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
This commit is contained in:
Andy Shaw 2020-08-11 01:18:05 +02:00
parent 81e09ae404
commit 37c68503cb
3 changed files with 50 additions and 6 deletions

View File

@ -1988,7 +1988,13 @@ QString QFont::toString() const
QString::number((int) underline()) + comma +
QString::number((int) strikeOut()) + comma +
QString::number((int)fixedPitch()) + comma +
QString::number((int) false);
QString::number((int) false) + comma +
QString::number((int)capitalization()) + comma +
QString::number((int)letterSpacingType()) + comma +
QString::number(letterSpacing()) + comma +
QString::number(wordSpacing()) + comma +
QString::number(stretch()) + comma +
QString::number((int)styleStrategy());
QString fontStyle = styleName();
if (!fontStyle.isEmpty())
@ -2022,7 +2028,7 @@ bool QFont::fromString(const QString &descrip)
const auto sr = QStringView(descrip).trimmed();
const auto l = sr.split(QLatin1Char(','));
const int count = l.count();
if (!count || (count > 2 && count < 9) || count > 11 ||
if (!count || (count > 2 && count < 9) || count == 9 || count > 17 ||
l.first().isEmpty()) {
qWarning("QFont::fromString: Invalid description '%s'",
descrip.isEmpty() ? "(empty)" : descrip.toLatin1().data());
@ -2048,8 +2054,15 @@ 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 >= 16) {
setCapitalization((Capitalization)l[10].toInt());
setLetterSpacing((SpacingType)l[11].toInt(), l[12].toDouble());
setWordSpacing(l[13].toDouble());
setStretch(l[14].toInt());
setStyleStrategy((StyleStrategy)l[15].toInt());
}
if (count == 11 || count == 17)
d->request.styleName = l[count - 1].toString();
else
d->request.styleName.clear();
}

View File

@ -384,7 +384,7 @@ void tst_QGuiVariant::toString_data()
#endif
QFont font( "times", 12 );
QTest::newRow( "qfont" ) << QVariant::fromValue( font ) << QString("times,12,-1,5,50,0,0,0,0,0");
QTest::newRow("qfont") << QVariant::fromValue(font) << QString("times,12,-1,5,50,0,0,0,0,0,0,0,0,0,0,1");
QTest::newRow( "qcolor" ) << QVariant::fromValue( QColor( 10, 10, 10 ) ) << QString( "#0a0a0a" );
}

View File

@ -588,6 +588,30 @@ void tst_QFont::toAndFromString()
QCOMPARE(result, initial);
}
// 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,75,1,0,0,1,0,Regular"));
QFont fontFrom515("Times New Roman", 18);
fontFrom515.setBold(true);
fontFrom515.setItalic(true);
fontFrom515.setFixedPitch(true);
fontFrom515.setStyleName("Regular");
QFont from515String;
from515String.fromString(fontStringFrom515);
QCOMPARE(from515String, fontFrom515);
const QString fontStringFrom60(QLatin1String("Times New Roman,18,-1,5,75,1,0,0,1,0,1,0,150.5,2.5,50,2,Regular"));
QFont fontFrom60 = fontFrom515;
fontFrom60.setStyleStrategy(QFont::PreferBitmap);
fontFrom60.setCapitalization(QFont::AllUppercase);
fontFrom60.setLetterSpacing(QFont::PercentageSpacing, 150.5);
fontFrom60.setWordSpacing(2.5);
fontFrom60.setStretch(50);
QFont from60String;
from60String.fromString(fontStringFrom60);
QCOMPARE(fontFrom60.toString(), fontStringFrom60);
QCOMPARE(from60String, fontFrom60);
}
void tst_QFont::fromStringWithoutStyleName()
@ -596,10 +620,17 @@ void tst_QFont::fromStringWithoutStyleName()
font1.fromString("Noto Sans,12,-1,5,50,0,0,0,0,0,Regular");
QFont font2 = font1;
const QString str = "Times,16,-1,5,50,0,0,0,0,0";
const QString str = "Times,16,-1,5,50,0,0,0,0,0,0,0,0,0,0,1";
font2.fromString(str);
QCOMPARE(font2.toString(), str);
const QString fontStringFrom60(QLatin1String("Times New Roman,18,-1,5,75,1,0,0,1,0,1,0,150.5,2.5,50,2"));
QFont font3;
font3.fromString("Noto Sans,12,-1,5,50,0,0,0,0,0,Regular");
QFont font4 = font3;
font4.fromString(fontStringFrom60);
QCOMPARE(font4.toString(), fontStringFrom60);
}
void tst_QFont::fromDegenerateString_data()