From 12f0663dbda6ae56d3307493ca34212f601dd3aa Mon Sep 17 00:00:00 2001 From: Yann Bodson Date: Tue, 21 Feb 2012 14:11:04 +1000 Subject: Fix font size calculation in headings in StyledText. Calculate the font size correctly even when the size is specified in pixels and update this size when the font changes. Also make sure that the text layout's font is set before parsing. Task-number: QTBUG-24458 Change-Id: Ida7723f6e4f4b9fd3a6878076f4beaf5bda8f7f7 Reviewed-by: Andrew den Exter --- src/quick/items/qquicktext.cpp | 15 ++- src/quick/items/qquicktext_p_p.h | 1 + src/quick/util/qdeclarativestyledtext.cpp | 43 ++++--- src/quick/util/qdeclarativestyledtext_p.h | 6 +- .../tst_qdeclarativestyledtext.cpp | 130 +++++++++++---------- .../qtquick2/qquicktext/data/pixelFontSizes.qml | 21 ++++ .../qtquick2/qquicktext/data/pointFontSizes.qml | 21 ++++ tests/auto/qtquick2/qquicktext/tst_qquicktext.cpp | 72 ++++++++++++ 8 files changed, 228 insertions(+), 81 deletions(-) create mode 100644 tests/auto/qtquick2/qquicktext/data/pixelFontSizes.qml create mode 100644 tests/auto/qtquick2/qquicktext/data/pointFontSizes.qml diff --git a/src/quick/items/qquicktext.cpp b/src/quick/items/qquicktext.cpp index 6f8aa383cd..44735c1895 100644 --- a/src/quick/items/qquicktext.cpp +++ b/src/quick/items/qquicktext.cpp @@ -85,7 +85,7 @@ QQuickTextPrivate::QQuickTextPrivate() , maximumLineCountValid(false), updateOnComponentComplete(true), richText(false) , styledText(false), singleline(false), internalWidthUpdate(false), requireImplicitWidth(false) , truncated(false), hAlignImplicit(true), rightToLeftText(false) - , layoutTextElided(false), textHasChanged(true), needToUpdateLayout(false) + , layoutTextElided(false), textHasChanged(true), needToUpdateLayout(false), formatModifiesFontSize(false) { } @@ -298,7 +298,11 @@ void QQuickTextPrivate::updateLayout() if (!richText) { if (textHasChanged) { if (styledText && !text.isEmpty()) { - QDeclarativeStyledText::parse(text, layout, imgTags, q->baseUrl(), qmlContext(q), !maximumLineCountValid); + layout.setFont(font); + // needs temporary bool because formatModifiesFontSize is in a bit-field + bool fontSizeModified = false; + QDeclarativeStyledText::parse(text, layout, imgTags, q->baseUrl(), qmlContext(q), !maximumLineCountValid, &fontSizeModified); + formatModifiesFontSize = fontSizeModified; } else { layout.clearAdditionalFormats(); multilengthEos = text.indexOf(QLatin1Char('\x9c')); @@ -1252,8 +1256,13 @@ void QQuickText::setFont(const QFont &font) d->font.setPointSizeF(size/2.0); } - if (oldFont != d->font) + if (oldFont != d->font) { + // if the format changes the size of the text + // with headings or tag, we need to re-parse + if (d->formatModifiesFontSize) + d->textHasChanged = true; d->updateLayout(); + } emit fontChanged(d->sourceFont); } diff --git a/src/quick/items/qquicktext_p_p.h b/src/quick/items/qquicktext_p_p.h index 555f41ff94..e060cc1cd2 100644 --- a/src/quick/items/qquicktext_p_p.h +++ b/src/quick/items/qquicktext_p_p.h @@ -148,6 +148,7 @@ public: bool layoutTextElided:1; bool textHasChanged:1; bool needToUpdateLayout:1; + bool formatModifiesFontSize:1; static const QChar elideChar; diff --git a/src/quick/util/qdeclarativestyledtext.cpp b/src/quick/util/qdeclarativestyledtext.cpp index 164e33cec1..ddb8cadd2a 100644 --- a/src/quick/util/qdeclarativestyledtext.cpp +++ b/src/quick/util/qdeclarativestyledtext.cpp @@ -69,6 +69,8 @@ QT_BEGIN_NAMESPACE +Q_GUI_EXPORT int qt_defaultDpi(); + class QDeclarativeStyledTextPrivate { public: @@ -85,9 +87,10 @@ public: QList &imgTags, const QUrl &baseUrl, QDeclarativeContext *context, - bool preloadImages) + bool preloadImages, + bool *fontSizeModified) : text(t), layout(l), imgTags(&imgTags), baseFont(layout.font()), baseUrl(baseUrl), hasNewLine(false), nbImages(0), updateImagePositions(false) - , preFormat(false), prependSpace(false), hasSpace(true), preloadImages(preloadImages), context(context) + , preFormat(false), prependSpace(false), hasSpace(true), preloadImages(preloadImages), fontSizeModified(fontSizeModified), context(context) { } @@ -103,7 +106,7 @@ public: void parseImageAttributes(const QChar *&ch, const QString &textIn, QString &textOut); QPair parseAttribute(const QChar *&ch, const QString &textIn); QStringRef parseValue(const QChar *&ch, const QString &textIn); - + void setFontSize(int size, QTextCharFormat &format); inline void skipSpace(const QChar *&ch) { while (ch->isSpace() && !ch->isNull()) @@ -126,6 +129,7 @@ public: bool prependSpace; bool hasSpace; bool preloadImages; + bool *fontSizeModified; QDeclarativeContext *context; static const QChar lessThan; @@ -160,8 +164,9 @@ QDeclarativeStyledText::QDeclarativeStyledText(const QString &string, QTextLayou QList &imgTags, const QUrl &baseUrl, QDeclarativeContext *context, - bool preloadImages) - : d(new QDeclarativeStyledTextPrivate(string, layout, imgTags, baseUrl, context, preloadImages)) + bool preloadImages, + bool *fontSizeModified) + : d(new QDeclarativeStyledTextPrivate(string, layout, imgTags, baseUrl, context, preloadImages, fontSizeModified)) { } @@ -174,11 +179,12 @@ void QDeclarativeStyledText::parse(const QString &string, QTextLayout &layout, QList &imgTags, const QUrl &baseUrl, QDeclarativeContext *context, - bool preloadImages) + bool preloadImages, + bool *fontSizeModified) { if (string.isEmpty()) return; - QDeclarativeStyledText styledText(string, layout, imgTags, baseUrl, context, preloadImages); + QDeclarativeStyledText styledText(string, layout, imgTags, baseUrl, context, preloadImages, fontSizeModified); styledText.d->parse(); } @@ -298,6 +304,20 @@ void QDeclarativeStyledTextPrivate::appendText(const QString &textIn, int start, hasNewLine = false; } +// +// Calculates and sets the correct font size in points +// depending on the size multiplier and base font. +// +void QDeclarativeStyledTextPrivate::setFontSize(int size, QTextCharFormat &format) +{ + static const qreal scaling[] = { 0.7, 0.8, 1.0, 1.2, 1.5, 2.0, 2.4 }; + if (baseFont.pointSizeF() != -1) + format.setFontPointSize(baseFont.pointSize() * scaling[size - 1]); + else + format.setFontPointSize(baseFont.pixelSize() * qreal(72.) / qreal(qt_defaultDpi()) * scaling[size - 1]); + *fontSizeModified = true; +} + bool QDeclarativeStyledTextPrivate::parseTag(const QChar *&ch, const QString &textIn, QString &textOut, QTextCharFormat &format) { skipSpace(ch); @@ -353,12 +373,11 @@ bool QDeclarativeStyledTextPrivate::parseTag(const QChar *&ch, const QString &te } else if (char0 == QLatin1Char('h') && tagLength == 2) { int level = tag.at(1).digitValue(); if (level >= 1 && level <= 6) { - static const qreal scaling[] = { 2.0, 1.5, 1.2, 1.0, 0.8, 0.7 }; if (!hasNewLine) textOut.append(QChar::LineSeparator); hasSpace = true; prependSpace = false; - format.setFontPointSize(baseFont.pointSize() * scaling[level - 1]); + setFontSize(7 - level, format); format.setFontWeight(QFont::Bold); return true; } @@ -550,10 +569,8 @@ bool QDeclarativeStyledTextPrivate::parseFontAttributes(const QChar *&ch, const int size = attr.second.toString().toInt(); if (attr.second.at(0) == QLatin1Char('-') || attr.second.at(0) == QLatin1Char('+')) size += 3; - if (size >= 1 && size <= 7) { - static const qreal scaling[] = { 0.7, 0.8, 1.0, 1.2, 1.5, 2.0, 2.4 }; - format.setFontPointSize(baseFont.pointSize() * scaling[size-1]); - } + if (size >= 1 && size <= 7) + setFontSize(size, format); } } while (!ch->isNull() && !attr.first.isEmpty()); diff --git a/src/quick/util/qdeclarativestyledtext_p.h b/src/quick/util/qdeclarativestyledtext_p.h index aa6ae3f869..4487a9e98a 100644 --- a/src/quick/util/qdeclarativestyledtext_p.h +++ b/src/quick/util/qdeclarativestyledtext_p.h @@ -85,14 +85,16 @@ public: QList &imgTags, const QUrl &baseUrl, QDeclarativeContext *context, - bool preloadImages); + bool preloadImages, + bool *fontSizeModified); private: QDeclarativeStyledText(const QString &string, QTextLayout &layout, QList &imgTags, const QUrl &baseUrl, QDeclarativeContext *context, - bool preloadImages); + bool preloadImages, + bool *fontSizeModified); ~QDeclarativeStyledText(); QDeclarativeStyledTextPrivate *d; diff --git a/tests/auto/qtquick2/qdeclarativestyledtext/tst_qdeclarativestyledtext.cpp b/tests/auto/qtquick2/qdeclarativestyledtext/tst_qdeclarativestyledtext.cpp index fd0d1abd29..ca3855c32a 100644 --- a/tests/auto/qtquick2/qdeclarativestyledtext/tst_qdeclarativestyledtext.cpp +++ b/tests/auto/qtquick2/qdeclarativestyledtext/tst_qdeclarativestyledtext.cpp @@ -88,68 +88,69 @@ void tst_qdeclarativestyledtext::textOutput_data() QTest::addColumn("input"); QTest::addColumn("output"); QTest::addColumn("formats"); - - QTest::newRow("bold") << "bold" << "bold" << (FormatList() << Format(Format::Bold, 0, 4)); - QTest::newRow("italic") << "italic" << "italic" << (FormatList() << Format(Format::Italic, 0, 6)); - QTest::newRow("underline") << "underline" << "underline" << (FormatList() << Format(Format::Underline, 0, 9)); - QTest::newRow("strong") << "strong" << "strong" << (FormatList() << Format(Format::Bold, 0, 6)); - QTest::newRow("underline") << "underline" << "underline" << (FormatList() << Format(Format::Underline, 0, 9)); - QTest::newRow("missing >") << "text") << "text") << "text<" << "text" << (FormatList() << Format(Format::Bold, 0, 4)); - QTest::newRow("missing ") << "text" << "text" << (FormatList() << Format(Format::Bold, 0, 4)); - QTest::newRow("nested") << "text italic bold" << "text italic bold" << (FormatList() << Format(Format::Bold, 0, 5) << Format(Format::Bold | Format::Italic, 5, 6) << Format(Format::Bold, 11, 5)); - QTest::newRow("bad nest") << "text italic" << "text italic" << (FormatList() << Format(Format::Bold, 0, 5) << Format(Format::Bold | Format::Italic, 5, 6)); - QTest::newRow("font color") << "red text" << "red text" << (FormatList() << Format(0, 0, 8)); - QTest::newRow("font color: single quote") << "red text" << "red text" << (FormatList() << Format(0, 0, 8)); - QTest::newRow("font size") << "text" << "text" << (FormatList() << Format(0, 0, 4)); - QTest::newRow("font empty") << "text" << "text" << FormatList(); - QTest::newRow("font bad 1") << "text" << "text" << FormatList(); - QTest::newRow("font bad 2") << "text" << "" << FormatList(); - QTest::newRow("extra close") << "text" << "text" << (FormatList() << Format(Format::Bold, 0, 4)); - QTest::newRow("extra space") << "text" << "text" << (FormatList() << Format(Format::Bold, 0, 4)); - QTest::newRow("entities") << "<b>this & that</b>" << "this & that" << FormatList(); - QTest::newRow("newline") << "text
more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList(); - QTest::newRow("paragraph") << "text

more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList(); - QTest::newRow("paragraph closed") << "text

more text

more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList(); - QTest::newRow("paragraph closed bold") << "text

more text

more text
" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << (FormatList() << Format(Format::Bold, 0, 24)); - QTest::newRow("self-closing newline") << "text
more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList(); - QTest::newRow("empty") << "" << "" << FormatList(); - QTest::newRow("unknown tag") << "underline not" << "underline not" << (FormatList() << Format(Format::Underline, 0, 9)); - QTest::newRow("ordered list") << "
  1. one
  2. two" << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("1.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("2.") + QString(2, QChar::Nbsp) + QLatin1String("two") << FormatList(); - QTest::newRow("ordered list closed") << "
    1. one
    2. two
    " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("1.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("2.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList(); - QTest::newRow("ordered list alpha") << "
    1. one
    2. two
    " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("a.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("b.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList(); - QTest::newRow("ordered list upper alpha") << "
    1. one
    2. two
    " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("A.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("B.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList(); - QTest::newRow("ordered list roman") << "
    1. one
    2. two
    " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("i.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("ii.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList(); - QTest::newRow("ordered list upper roman") << "
    1. one
    2. two
    " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("I.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("II.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList(); - QTest::newRow("ordered list bad") << "
    1. one
    2. two
    " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("1.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("2.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList(); - QTest::newRow("unordered list") << "
    • one
    • two" << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("two") << FormatList(); - QTest::newRow("unordered list closed") << "
      • one
      • two
      " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList(); - QTest::newRow("unordered list disc") << "
      • one
      • two
      " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + disc + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + disc + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList(); - QTest::newRow("unordered list square") << "
      • one
      • two
      " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + square + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + square + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList(); - QTest::newRow("unordered list bad") << "
      • one
      • two
      " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList(); - QTest::newRow("header close") << "

      head

      more" << QChar(QChar::LineSeparator) + QLatin1String("head") + QChar(QChar::LineSeparator) + QLatin1String("more") << (FormatList() << Format(Format::Bold, 0, 5)); - QTest::newRow("h0") << "head" << "head" << FormatList(); - QTest::newRow("h1") << "

      head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)); - QTest::newRow("h2") << "

      head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)); - QTest::newRow("h3") << "

      head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)); - QTest::newRow("h4") << "

      head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)); - QTest::newRow("h5") << "

      head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)); - QTest::newRow("h6") << "
      head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)); - QTest::newRow("h7") << "head" << "head" << FormatList(); - QTest::newRow("pre") << "normal
      pre text
      normal" << QLatin1String("normal") + QChar(QChar::LineSeparator) + QLatin1String("pre") + QChar(QChar::Nbsp) + QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("normal") << (FormatList() << Format(0, 6, 9)); - QTest::newRow("pre lb") << "normal
      pre\n text
      normal" << QLatin1String("normal") + QChar(QChar::LineSeparator) + QLatin1String("pre") + QChar(QChar::LineSeparator) + QChar(QChar::Nbsp) + QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("normal") << (FormatList() << Format(0, 6, 10)); - QTest::newRow("line feed") << "line\nfeed" << "line feed" << FormatList(); - QTest::newRow("leading whitespace") << " leading whitespace" << "leading whitespace" << FormatList(); - QTest::newRow("trailing whitespace") << "trailing whitespace " << "trailing whitespace" << FormatList(); - QTest::newRow("consecutive whitespace") << " consecutive \t \n whitespace" << "consecutive whitespace" << FormatList(); - QTest::newRow("space after newline") << "text
      more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList(); - QTest::newRow("space after paragraph") << "text

      more text

      more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList(); - QTest::newRow("space in header") << "

      head

      " << QChar(QChar::LineSeparator) + QLatin1String("head") + QChar(QChar::LineSeparator) << (FormatList() << Format(Format::Bold, 0, 5)); - QTest::newRow("space before bold") << "this is bold" << "this is bold" << (FormatList() << Format(Format::Bold, 8, 4)); - QTest::newRow("space leading bold") << "this is bold" << "this is bold" << (FormatList() << Format(Format::Bold, 7, 5)); - QTest::newRow("space trailing bold") << "this is bold " << "this is bold " << (FormatList() << Format(Format::Bold, 8, 5)); - QTest::newRow("img") << "ab" << "a b" << FormatList(); + QTest::addColumn("modifiesFontSize"); + + QTest::newRow("bold") << "bold" << "bold" << (FormatList() << Format(Format::Bold, 0, 4)) << false; + QTest::newRow("italic") << "italic" << "italic" << (FormatList() << Format(Format::Italic, 0, 6)) << false; + QTest::newRow("underline") << "underline" << "underline" << (FormatList() << Format(Format::Underline, 0, 9)) << false; + QTest::newRow("strong") << "strong" << "strong" << (FormatList() << Format(Format::Bold, 0, 6)) << false; + QTest::newRow("underline") << "underline" << "underline" << (FormatList() << Format(Format::Underline, 0, 9)) << false; + QTest::newRow("missing >") << "text") << "text") << "text<" << "text" << (FormatList() << Format(Format::Bold, 0, 4)) << false; + QTest::newRow("missing ") << "text" << "text" << (FormatList() << Format(Format::Bold, 0, 4)) << false; + QTest::newRow("nested") << "text italic bold" << "text italic bold" << (FormatList() << Format(Format::Bold, 0, 5) << Format(Format::Bold | Format::Italic, 5, 6) << Format(Format::Bold, 11, 5)) << false; + QTest::newRow("bad nest") << "text italic" << "text italic" << (FormatList() << Format(Format::Bold, 0, 5) << Format(Format::Bold | Format::Italic, 5, 6)) << false; + QTest::newRow("font color") << "red text" << "red text" << (FormatList() << Format(0, 0, 8)) << false; + QTest::newRow("font color: single quote") << "red text" << "red text" << (FormatList() << Format(0, 0, 8)) << false; + QTest::newRow("font size") << "text" << "text" << (FormatList() << Format(0, 0, 4)) << true; + QTest::newRow("font empty") << "text" << "text" << FormatList() << false; + QTest::newRow("font bad 1") << "text" << "text" << FormatList() << false; + QTest::newRow("font bad 2") << "text" << "" << FormatList() << false; + QTest::newRow("extra close") << "text" << "text" << (FormatList() << Format(Format::Bold, 0, 4)) << false; + QTest::newRow("extra space") << "text" << "text" << (FormatList() << Format(Format::Bold, 0, 4)) << false; + QTest::newRow("entities") << "<b>this & that</b>" << "this & that" << FormatList() << false; + QTest::newRow("newline") << "text
      more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList() << false; + QTest::newRow("paragraph") << "text

      more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList() << false; + QTest::newRow("paragraph closed") << "text

      more text

      more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList() << false; + QTest::newRow("paragraph closed bold") << "text

      more text

      more text
      " << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << (FormatList() << Format(Format::Bold, 0, 24)) << false; + QTest::newRow("self-closing newline") << "text
      more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList() << false; + QTest::newRow("empty") << "" << "" << FormatList() << false; + QTest::newRow("unknown tag") << "underline not" << "underline not" << (FormatList() << Format(Format::Underline, 0, 9)) << false; + QTest::newRow("ordered list") << "
      1. one
      2. two" << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("1.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("2.") + QString(2, QChar::Nbsp) + QLatin1String("two") << FormatList() << false; + QTest::newRow("ordered list closed") << "
        1. one
        2. two
        " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("1.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("2.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false; + QTest::newRow("ordered list alpha") << "
        1. one
        2. two
        " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("a.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("b.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false; + QTest::newRow("ordered list upper alpha") << "
        1. one
        2. two
        " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("A.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("B.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false; + QTest::newRow("ordered list roman") << "
        1. one
        2. two
        " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("i.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("ii.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false; + QTest::newRow("ordered list upper roman") << "
        1. one
        2. two
        " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("I.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("II.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false; + QTest::newRow("ordered list bad") << "
        1. one
        2. two
        " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("1.") + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + QLatin1String("2.") + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false; + QTest::newRow("unordered list") << "
        • one
        • two" << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("two") << FormatList() << false; + QTest::newRow("unordered list closed") << "
          • one
          • two
          " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false; + QTest::newRow("unordered list disc") << "
          • one
          • two
          " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + disc + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + disc + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false; + QTest::newRow("unordered list square") << "
          • one
          • two
          " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + square + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + square + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false; + QTest::newRow("unordered list bad") << "
          • one
          • two
          " << QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("one") + QChar(QChar::LineSeparator) + QString(6, QChar::Nbsp) + bullet + QString(2, QChar::Nbsp) + QLatin1String("two") + QChar(QChar::LineSeparator) << FormatList() << false; + QTest::newRow("header close") << "

          head

          more" << QChar(QChar::LineSeparator) + QLatin1String("head") + QChar(QChar::LineSeparator) + QLatin1String("more") << (FormatList() << Format(Format::Bold, 0, 5)) << true; + QTest::newRow("h0") << "head" << "head" << FormatList() << false; + QTest::newRow("h1") << "

          head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)) << true; + QTest::newRow("h2") << "

          head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)) << true; + QTest::newRow("h3") << "

          head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)) << true; + QTest::newRow("h4") << "

          head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)) << true; + QTest::newRow("h5") << "

          head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)) << true; + QTest::newRow("h6") << "
          head" << QChar(QChar::LineSeparator) + QLatin1String("head") << (FormatList() << Format(Format::Bold, 0, 5)) << true; + QTest::newRow("h7") << "head" << "head" << FormatList() << false; + QTest::newRow("pre") << "normal
          pre text
          normal" << QLatin1String("normal") + QChar(QChar::LineSeparator) + QLatin1String("pre") + QChar(QChar::Nbsp) + QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("normal") << (FormatList() << Format(0, 6, 9)) << false; + QTest::newRow("pre lb") << "normal
          pre\n text
          normal" << QLatin1String("normal") + QChar(QChar::LineSeparator) + QLatin1String("pre") + QChar(QChar::LineSeparator) + QChar(QChar::Nbsp) + QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("normal") << (FormatList() << Format(0, 6, 10)) << false; + QTest::newRow("line feed") << "line\nfeed" << "line feed" << FormatList() << false; + QTest::newRow("leading whitespace") << " leading whitespace" << "leading whitespace" << FormatList() << false; + QTest::newRow("trailing whitespace") << "trailing whitespace " << "trailing whitespace" << FormatList() << false; + QTest::newRow("consecutive whitespace") << " consecutive \t \n whitespace" << "consecutive whitespace" << FormatList() << false; + QTest::newRow("space after newline") << "text
          more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList() << false; + QTest::newRow("space after paragraph") << "text

          more text

          more text" << QLatin1String("text") + QChar(QChar::LineSeparator) + QLatin1String("more text") + QChar(QChar::LineSeparator) + QLatin1String("more text") << FormatList() << false; + QTest::newRow("space in header") << "

          head

          " << QChar(QChar::LineSeparator) + QLatin1String("head") + QChar(QChar::LineSeparator) << (FormatList() << Format(Format::Bold, 0, 5)) << true; + QTest::newRow("space before bold") << "this is bold" << "this is bold" << (FormatList() << Format(Format::Bold, 8, 4)) << false; + QTest::newRow("space leading bold") << "this is bold" << "this is bold" << (FormatList() << Format(Format::Bold, 7, 5)) << false; + QTest::newRow("space trailing bold") << "this is bold " << "this is bold " << (FormatList() << Format(Format::Bold, 8, 5)) << false; + QTest::newRow("img") << "ab" << "a b" << FormatList() << false; } void tst_qdeclarativestyledtext::textOutput() @@ -157,10 +158,12 @@ void tst_qdeclarativestyledtext::textOutput() QFETCH(QString, input); QFETCH(QString, output); QFETCH(FormatList, formats); + QFETCH(bool, modifiesFontSize); QTextLayout layout; QList imgTags; - QDeclarativeStyledText::parse(input, layout, imgTags, QUrl(), 0, false); + bool fontSizeModified = false; + QDeclarativeStyledText::parse(input, layout, imgTags, QUrl(), 0, false, &fontSizeModified); QCOMPARE(layout.text(), output); @@ -177,6 +180,7 @@ void tst_qdeclarativestyledtext::textOutput() QVERIFY(layoutFormats.at(i).format.fontItalic() == bool(formats.at(i).type & Format::Italic)); QVERIFY(layoutFormats.at(i).format.fontUnderline() == bool(formats.at(i).type & Format::Underline)); } + QCOMPARE(fontSizeModified, modifiesFontSize); } diff --git a/tests/auto/qtquick2/qquicktext/data/pixelFontSizes.qml b/tests/auto/qtquick2/qquicktext/data/pixelFontSizes.qml new file mode 100644 index 0000000000..e3d81b682e --- /dev/null +++ b/tests/auto/qtquick2/qquicktext/data/pixelFontSizes.qml @@ -0,0 +1,21 @@ +import QtQuick 2.0 + +Rectangle { + width: 400 + height: 200 + + property variant pixelSize: 6 + + Text { + objectName: "text" + font.pixelSize: parent.pixelSize + text: "This is
          a font
          size test." + } + + Text { + x: 200 + objectName: "textWithTag" + font.pixelSize: parent.pixelSize + text: "This is

          a font

          size test." + } +} diff --git a/tests/auto/qtquick2/qquicktext/data/pointFontSizes.qml b/tests/auto/qtquick2/qquicktext/data/pointFontSizes.qml new file mode 100644 index 0000000000..6feb8fecf8 --- /dev/null +++ b/tests/auto/qtquick2/qquicktext/data/pointFontSizes.qml @@ -0,0 +1,21 @@ +import QtQuick 2.0 + +Rectangle { + width: 400 + height: 200 + + property variant pointSize: 6 + + Text { + objectName: "text" + font.pointSize: parent.pointSize + text: "This is
          a font
          size test." + } + + Text { + x: 200 + objectName: "textWithTag" + font.pointSize: parent.pointSize + text: "This is

          a font

          size test." + } +} diff --git a/tests/auto/qtquick2/qquicktext/tst_qquicktext.cpp b/tests/auto/qtquick2/qquicktext/tst_qquicktext.cpp index d1c0f765d0..86d502f07e 100644 --- a/tests/auto/qtquick2/qquicktext/tst_qquicktext.cpp +++ b/tests/auto/qtquick2/qquicktext/tst_qquicktext.cpp @@ -122,6 +122,8 @@ private slots: void fontSizeModeMultiline(); void multilengthStrings_data(); void multilengthStrings(); + void fontFormatSizes_data(); + void fontFormatSizes(); private: QStringList standard; @@ -2427,6 +2429,76 @@ void tst_qquicktext::multilengthStrings() QCOMPARE(myText->truncated(), true); } +void tst_qquicktext::fontFormatSizes_data() +{ + QTest::addColumn("text"); + QTest::addColumn("textWithTag"); + QTest::addColumn("fontIsBigger"); + + QTest::newRow("fs1") << "Hello world!" << "Hello world!" << false; + QTest::newRow("fs2") << "Hello world!" << "Hello world!" << false; + QTest::newRow("fs3") << "Hello world!" << "Hello world!" << false; + QTest::newRow("fs4") << "Hello world!" << "Hello world!" << true; + QTest::newRow("fs5") << "Hello world!" << "Hello world!" << true; + QTest::newRow("fs6") << "Hello world!" << "Hello world!" << true; + QTest::newRow("fs7") << "Hello world!" << "Hello world!" << true; + QTest::newRow("h1") << "This is
          a font
          size test." << "This is

          a font

          size test." << true; + QTest::newRow("h2") << "This is
          a font
          size test." << "This is

          a font

          size test." << true; + QTest::newRow("h3") << "This is
          a font
          size test." << "This is

          a font

          size test." << true; + QTest::newRow("h4") << "This is
          a font
          size test." << "This is

          a font

          size test." << true; + QTest::newRow("h5") << "This is
          a font
          size test." << "This is
          a font
          size test." << false; + QTest::newRow("h6") << "This is
          a font
          size test." << "This is
          a font
          size test." << false; +} + +void tst_qquicktext::fontFormatSizes() +{ + QFETCH(QString, text); + QFETCH(QString, textWithTag); + QFETCH(bool, fontIsBigger); + + QQuickView *view = new QQuickView; + { + view->setSource(testFileUrl("pointFontSizes.qml")); + view->show(); + + QQuickText *qtext = view->rootObject()->findChild("text"); + QQuickText *qtextWithTag = view->rootObject()->findChild("textWithTag"); + QVERIFY(qtext != 0); + QVERIFY(qtextWithTag != 0); + + qtext->setText(text); + qtextWithTag->setText(textWithTag); + + for (int size = 6; size < 100; size += 4) { + view->rootObject()->setProperty("pointSize", size); + if (fontIsBigger) + QVERIFY(qtext->height() <= qtextWithTag->height()); + else + QVERIFY(qtext->height() >= qtextWithTag->height()); + } + } + + { + view->setSource(testFileUrl("pixelFontSizes.qml")); + QQuickText *qtext = view->rootObject()->findChild("text"); + QQuickText *qtextWithTag = view->rootObject()->findChild("textWithTag"); + QVERIFY(qtext != 0); + QVERIFY(qtextWithTag != 0); + + qtext->setText(text); + qtextWithTag->setText(textWithTag); + + for (int size = 6; size < 100; size += 4) { + view->rootObject()->setProperty("pixelSize", size); + if (fontIsBigger) + QVERIFY(qtext->height() <= qtextWithTag->height()); + else + QVERIFY(qtext->height() >= qtextWithTag->height()); + } + } + delete view; +} + QTEST_MAIN(tst_qquicktext) #include "tst_qquicktext.moc" -- cgit v1.2.3