aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorYann Bodson <yann.bodson@nokia.com>2012-02-21 14:11:04 +1000
committerQt by Nokia <qt-info@nokia.com>2012-02-23 03:00:31 +0100
commit12f0663dbda6ae56d3307493ca34212f601dd3aa (patch)
treee3f9087898a31550fb36f9b8144a9f5ea0ecf7ca /src
parenta08546507fe0ce356e4183e557d9408295c80610 (diff)
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 <andrew.den-exter@nokia.com>
Diffstat (limited to 'src')
-rw-r--r--src/quick/items/qquicktext.cpp15
-rw-r--r--src/quick/items/qquicktext_p_p.h1
-rw-r--r--src/quick/util/qdeclarativestyledtext.cpp43
-rw-r--r--src/quick/util/qdeclarativestyledtext_p.h6
4 files changed, 47 insertions, 18 deletions
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 <font> 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<QDeclarativeStyledTextImgTag*> &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<QStringRef,QStringRef> 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<QDeclarativeStyledTextImgTag*> &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<QDeclarativeStyledTextImgTag*> &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<QDeclarativeStyledTextImgTag*> &imgTags,
const QUrl &baseUrl,
QDeclarativeContext *context,
- bool preloadImages);
+ bool preloadImages,
+ bool *fontSizeModified);
private:
QDeclarativeStyledText(const QString &string, QTextLayout &layout,
QList<QDeclarativeStyledTextImgTag*> &imgTags,
const QUrl &baseUrl,
QDeclarativeContext *context,
- bool preloadImages);
+ bool preloadImages,
+ bool *fontSizeModified);
~QDeclarativeStyledText();
QDeclarativeStyledTextPrivate *d;