Fix CSS line-height property multiplier value handling

CSS style such as "line-height: 1.5;" should be used as a multiplier,
but Qt uses it as percentage which makes line spacing way too small. To
workaround this, convert it to percent and use as
QTextBlockFormat::ProportionalHeight instead.

[ChangeLog][QtGui][Important Behvior Changes] Changed CSS line-height
property with multiplier to follow CSS spec

Task-number: QTBUG-56848
Task-number: QTCREATORBUG-17683
Change-Id: Icc98f7c0d4d07542a220702c287f23fa450ef875
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
This commit is contained in:
Joni Poikelin 2017-02-15 08:59:23 +02:00
parent 6ac4f2ccef
commit ecf440d89d
3 changed files with 62 additions and 2 deletions

View File

@ -493,7 +493,7 @@ static QString quoteNewline(const QString &s)
QTextHtmlParserNode::QTextHtmlParserNode()
: parent(0), id(Html_unknown),
cssFloat(QTextFrameFormat::InFlow), hasOwnListStyle(false), hasOwnLineHeightType(false),
cssFloat(QTextFrameFormat::InFlow), hasOwnListStyle(false), hasOwnLineHeightType(false), hasLineHeightMultiplier(false),
hasCssListIndent(false), isEmptyParagraph(false), isTextFrame(false), isRootFrame(false),
displayMode(QTextHtmlElement::DisplayInline), hasHref(false),
listStyle(QTextListFormat::ListStyleUndefined), imageWidth(-1), imageHeight(-1), tableBorder(0),
@ -1216,6 +1216,11 @@ void QTextHtmlParserNode::applyCssDeclarations(const QVector<QCss::Declaration>
else
lineHeightType = QTextBlockFormat::SingleHeight;
if (hasLineHeightMultiplier) {
qreal lineHeight = blockFormat.lineHeight() / 100.0;
blockFormat.setProperty(QTextBlockFormat::LineHeight, lineHeight);
}
blockFormat.setProperty(QTextBlockFormat::LineHeightType, lineHeightType);
hasOwnLineHeightType = true;
}
@ -1227,9 +1232,14 @@ void QTextHtmlParserNode::applyCssDeclarations(const QVector<QCss::Declaration>
lineHeightType = QTextBlockFormat::MinimumHeight;
} else {
bool ok;
QString value = decl.d->values.first().toString();
QCss::Value cssValue = decl.d->values.first();
QString value = cssValue.toString();
lineHeight = value.toDouble(&ok);
if (ok) {
if (!hasOwnLineHeightType && cssValue.type == QCss::Value::Number) {
lineHeight *= 100.0;
hasLineHeightMultiplier = true;
}
lineHeightType = QTextBlockFormat::ProportionalHeight;
} else {
lineHeight = 0.0;

View File

@ -173,6 +173,7 @@ struct QTextHtmlParserNode {
uint cssFloat : 2;
uint hasOwnListStyle : 1;
uint hasOwnLineHeightType : 1;
uint hasLineHeightMultiplier : 1;
uint hasCssListIndent : 1;
uint isEmptyParagraph : 1;
uint isTextFrame : 1;

View File

@ -187,6 +187,7 @@ private slots:
void cssInheritance();
void lineHeightType();
void cssLineHeightMultiplier();
private:
void backgroundImage_checkExpectedHtml(const QTextDocument &doc);
void buildRegExpData();
@ -3396,6 +3397,33 @@ void tst_QTextDocument::lineHeightType()
QCOMPARE(format.lineHeight(), 10.0);
}
{
QTextDocument td;
td.setHtml("<html><head><style type=\"text/css\">body { -qt-line-height-type: fixed; line-height: 10; -qt-line-height-type: fixed; }</style></head><body>Foobar</body></html>");
QTextBlock block = td.begin();
QTextBlockFormat format = block.blockFormat();
QCOMPARE(int(format.lineHeightType()), int(QTextBlockFormat::FixedHeight));
QCOMPARE(format.lineHeight(), 10.0);
}
{
QTextDocument td;
td.setHtml("<html><head><style type=\"text/css\">body { -qt-line-height-type: proportional; line-height: 3; }</style></head><body>Foobar</body></html>");
QTextBlock block = td.begin();
QTextBlockFormat format = block.blockFormat();
QCOMPARE(int(format.lineHeightType()), int(QTextBlockFormat::ProportionalHeight));
QCOMPARE(format.lineHeight(), 3.0);
}
{
QTextDocument td;
td.setHtml("<html><head><style type=\"text/css\">body { line-height: 2.5; -qt-line-height-type: proportional; }</style></head><body>Foobar</body></html>");
QTextBlock block = td.begin();
QTextBlockFormat format = block.blockFormat();
QCOMPARE(int(format.lineHeightType()), int(QTextBlockFormat::ProportionalHeight));
QCOMPARE(format.lineHeight(), 2.5);
}
{
QTextDocument td;
td.setHtml("<html><head><style type=\"text/css\">body { line-height: 33; -qt-line-height-type: minimum; }</style></head><body>Foobar</body></html>");
@ -3424,5 +3452,26 @@ void tst_QTextDocument::lineHeightType()
}
}
void tst_QTextDocument::cssLineHeightMultiplier()
{
{
QTextDocument td;
td.setHtml("<html><head><style type=\"text/css\">body { line-height: 10; }</style></head><body>Foobar</body></html>");
QTextBlock block = td.begin();
QTextBlockFormat format = block.blockFormat();
QCOMPARE(int(format.lineHeightType()), int(QTextBlockFormat::ProportionalHeight));
QCOMPARE(format.lineHeight(), 1000.0);
}
{
QTextDocument td;
td.setHtml("<html><head><style type=\"text/css\">body {line-height: 1.38; }</style></head><body>Foobar</body></html>");
QTextBlock block = td.begin();
QTextBlockFormat format = block.blockFormat();
QCOMPARE(int(format.lineHeightType()), int(QTextBlockFormat::ProportionalHeight));
QCOMPARE(format.lineHeight(), 138.0);
}
}
QTEST_MAIN(tst_QTextDocument)
#include "tst_qtextdocument.moc"