Handle multiple font-families in <font> face attribute

This seems to be a common use case, and to be expected from pastes
of MSWord documents.

Change-Id: I5849d7f51408e76f15a0b03c2118649f118af1d6
Fixes: QTBUG-66794
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Allan Sandfeld Jensen 2019-06-20 13:07:04 +02:00
parent 2021b36ebd
commit 6ff0614b61
2 changed files with 34 additions and 1 deletions

View File

@ -1505,7 +1505,16 @@ void QTextHtmlParser::applyAttributes(const QStringList &attributes)
n -= 3; n -= 3;
node->charFormat.setProperty(QTextFormat::FontSizeAdjustment, n); node->charFormat.setProperty(QTextFormat::FontSizeAdjustment, n);
} else if (key == QLatin1String("face")) { } else if (key == QLatin1String("face")) {
if (value.contains(QLatin1Char(','))) {
const QStringList values = value.split(QLatin1Char(','));
QStringList families;
for (const QString &family : values)
families << family.trimmed();
node->charFormat.setFontFamilies(families);
node->charFormat.setFontFamily(families.at(0));
} else {
node->charFormat.setFontFamily(value); node->charFormat.setFontFamily(value);
}
} else if (key == QLatin1String("color")) { } else if (key == QLatin1String("color")) {
QColor c; c.setNamedColor(value); QColor c; c.setNamedColor(value);
if (!c.isValid()) if (!c.isValid())

View File

@ -188,6 +188,9 @@ private slots:
void lineHeightType(); void lineHeightType();
void cssLineHeightMultiplier(); void cssLineHeightMultiplier();
void fontTagFace();
private: private:
void backgroundImage_checkExpectedHtml(const QTextDocument &doc); void backgroundImage_checkExpectedHtml(const QTextDocument &doc);
void buildRegExpData(); void buildRegExpData();
@ -3498,5 +3501,26 @@ void tst_QTextDocument::cssLineHeightMultiplier()
} }
} }
void tst_QTextDocument::fontTagFace()
{
{
QTextDocument td;
td.setHtml("<html><body><font face='Times'>Foobar</font></body></html>");
QTextFragment fragment = td.begin().begin().fragment();
QTextCharFormat format = fragment.charFormat();
QCOMPARE(format.fontFamily(), QLatin1String("Times"));
}
{
QTextDocument td;
td.setHtml("<html><body><font face='Times, serif'>Foobar</font></body></html>");
QTextFragment fragment = td.begin().begin().fragment();
QTextCharFormat format = fragment.charFormat();
QCOMPARE(format.fontFamily(), QLatin1String("Times"));
QStringList expectedFamilies = { QLatin1String("Times"), QLatin1String("serif") };
QCOMPARE(format.fontFamilies().toStringList(), expectedFamilies);
}
}
QTEST_MAIN(tst_QTextDocument) QTEST_MAIN(tst_QTextDocument)
#include "tst_qtextdocument.moc" #include "tst_qtextdocument.moc"