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/util/qdeclarativestyledtext.cpp | 43 +++++++++++++++++++++---------- src/quick/util/qdeclarativestyledtext_p.h | 6 +++-- 2 files changed, 34 insertions(+), 15 deletions(-) (limited to 'src/quick/util') 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; -- cgit v1.2.3