From e8ac2b958d92319da7c725f0c30f8ba5fe06497b Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 18 May 2009 14:59:14 +0200 Subject: QCss: font-family handle fallback font specs if one specify more than one parameter in font-family, e.g., font-family: Verdana, Arial Qt should fallback on the second font if the first cannot be found. QFont::setFamily handle the case when the family name contains a comas, so we do not need to handle that specially in the css parser code. Task-number: 252311 Reviewed-by: Thomas Zander --- doc/src/stylesheet.qdoc | 4 ---- src/gui/text/qcssparser.cpp | 19 ++++++++++++------- tests/auto/qcssparser/tst_cssparser.cpp | 13 +++++++++---- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/doc/src/stylesheet.qdoc b/doc/src/stylesheet.qdoc index c0d13da..f895918 100644 --- a/doc/src/stylesheet.qdoc +++ b/doc/src/stylesheet.qdoc @@ -1872,10 +1872,6 @@ \snippet doc/src/snippets/code/doc_src_stylesheet.qdoc 54 - \note If you specify more than one parameter in \c font-family, - e.g., \c{font-family: Verdana, Arial}, Qt will only use the first - font. If it cannot be found, Qt uses the system fallbacks instead. - \row \o \c font-size \o \l{#Font Size}{Font Size} diff --git a/src/gui/text/qcssparser.cpp b/src/gui/text/qcssparser.cpp index 8214e54..38621c1 100644 --- a/src/gui/text/qcssparser.cpp +++ b/src/gui/text/qcssparser.cpp @@ -1161,13 +1161,20 @@ static bool setFontWeightFromValue(const Value &value, QFont *font) return true; } -static bool setFontFamilyFromValues(const QVector &values, QFont *font) +/** \internal + * parse the font family from the values (starting from index \a start) + * and set it the \a font + * \returns true if a family was extracted. + */ +static bool setFontFamilyFromValues(const QVector &values, QFont *font, int start = 0) { QString family; - for (int i = 0; i < values.count(); ++i) { + for (int i = start; i < values.count(); ++i) { const Value &v = values.at(i); - if (v.type == Value::TermOperatorComma) - break; + if (v.type == Value::TermOperatorComma) { + family += QLatin1Char(','); + continue; + } const QString str = v.variant.toString(); if (str.isEmpty()) break; @@ -1221,9 +1228,7 @@ static void parseShorthandFontProperty(const QVector &values, QFont *font } if (i < values.count()) { - QString fam = values.at(i).variant.toString(); - if (!fam.isEmpty()) - font->setFamily(fam); + setFontFamilyFromValues(values, font, i); } } diff --git a/tests/auto/qcssparser/tst_cssparser.cpp b/tests/auto/qcssparser/tst_cssparser.cpp index ab6bad6..7c4fac1 100644 --- a/tests/auto/qcssparser/tst_cssparser.cpp +++ b/tests/auto/qcssparser/tst_cssparser.cpp @@ -123,7 +123,7 @@ static void debug(const QVector &symbols, int index = -1) qDebug() << "failure at index" << index; } -static void debug(const QCss::Parser &p) { debug(p.symbols); } +//static void debug(const QCss::Parser &p) { debug(p.symbols); } void tst_CssParser::scanner() { @@ -1473,7 +1473,12 @@ void tst_CssParser::extractFontFamily_data() QTest::newRow("quoted-family-name") << "font-family: 'Times New Roman'" << QString("Times New Roman"); QTest::newRow("unquoted-family-name") << "font-family: Times New Roman" << QString("Times New Roman"); QTest::newRow("unquoted-family-name2") << "font-family: Times New Roman" << QString("Times New Roman"); - QTest::newRow("multiple") << "font-family: Times New Roman , foobar, 'baz'" << QString("Times New Roman"); + QTest::newRow("multiple") << "font-family: Times New Roman , foobar, 'baz'" << QString("Times New Roman"); + QTest::newRow("multiple2") << "font-family: invalid, Times New Roman " << QString("Times New Roman"); + QTest::newRow("invalid") << "font-family: invalid" << QFont().family(); + QTest::newRow("shorthand") << "font: 12pt Times New Roman" << QString("Times New Roman"); + QTest::newRow("shorthand multiple quote") << "font: 12pt invalid, \"Times New Roman\" " << QString("Times New Roman"); + QTest::newRow("shorthand multiple") << "font: 12pt invalid, Times New Roman " << QString("Times New Roman"); } void tst_CssParser::extractFontFamily() @@ -1497,8 +1502,8 @@ void tst_CssParser::extractFontFamily() int adjustment = 0; QFont fnt; extractor.extractFont(&fnt, &adjustment); - - QTEST(fnt.family(), "expectedFamily"); + QFontInfo info(fnt); + QTEST(info.family(), "expectedFamily"); } void tst_CssParser::extractBorder_data() -- cgit v0.12