1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
45 #include <QTextLayout>
48 #include "private/qdeclarativestyledtext_p.h"
51 QDeclarativeStyledText supports few tags:
56 <font color="color_name" size="1-7"></font>
58 The opening and closing tags must be correctly nested.
63 class QDeclarativeStyledTextPrivate
66 QDeclarativeStyledTextPrivate(const QString &t, QTextLayout &l) : text(t), layout(l), baseFont(layout.font()) {}
69 bool parseTag(const QChar *&ch, const QString &textIn, QString &textOut, QTextCharFormat &format);
70 bool parseCloseTag(const QChar *&ch, const QString &textIn);
71 void parseEntity(const QChar *&ch, const QString &textIn, QString &textOut);
72 bool parseFontAttributes(const QChar *&ch, const QString &textIn, QTextCharFormat &format);
73 QPair<QStringRef,QStringRef> parseAttribute(const QChar *&ch, const QString &textIn);
74 QStringRef parseValue(const QChar *&ch, const QString &textIn);
76 inline void skipSpace(const QChar *&ch) {
77 while (ch->isSpace() && !ch->isNull())
85 static const QChar lessThan;
86 static const QChar greaterThan;
87 static const QChar equals;
88 static const QChar singleQuote;
89 static const QChar doubleQuote;
90 static const QChar slash;
91 static const QChar ampersand;
94 const QChar QDeclarativeStyledTextPrivate::lessThan(QLatin1Char('<'));
95 const QChar QDeclarativeStyledTextPrivate::greaterThan(QLatin1Char('>'));
96 const QChar QDeclarativeStyledTextPrivate::equals(QLatin1Char('='));
97 const QChar QDeclarativeStyledTextPrivate::singleQuote(QLatin1Char('\''));
98 const QChar QDeclarativeStyledTextPrivate::doubleQuote(QLatin1Char('\"'));
99 const QChar QDeclarativeStyledTextPrivate::slash(QLatin1Char('/'));
100 const QChar QDeclarativeStyledTextPrivate::ampersand(QLatin1Char('&'));
102 QDeclarativeStyledText::QDeclarativeStyledText(const QString &string, QTextLayout &layout)
103 : d(new QDeclarativeStyledTextPrivate(string, layout))
107 QDeclarativeStyledText::~QDeclarativeStyledText()
112 void QDeclarativeStyledText::parse(const QString &string, QTextLayout &layout)
114 if (string.isEmpty())
116 QDeclarativeStyledText styledText(string, layout);
117 styledText.d->parse();
120 void QDeclarativeStyledTextPrivate::parse()
122 QList<QTextLayout::FormatRange> ranges;
123 QStack<QTextCharFormat> formatStack;
126 drawText.reserve(text.count());
131 const QChar *ch = text.constData();
132 while (!ch->isNull()) {
133 if (*ch == lessThan) {
135 drawText.append(QStringRef(&text, textStart, textLength));
136 if (rangeStart != drawText.length() && formatStack.count()) {
137 QTextLayout::FormatRange formatRange;
138 formatRange.format = formatStack.top();
139 formatRange.start = rangeStart;
140 formatRange.length = drawText.length() - rangeStart;
141 ranges.append(formatRange);
143 rangeStart = drawText.length();
147 if (parseCloseTag(ch, text)) {
148 if (formatStack.count())
152 QTextCharFormat format;
153 if (formatStack.count())
154 format = formatStack.top();
155 if (parseTag(ch, text, drawText, format))
156 formatStack.push(format);
158 textStart = ch - text.constData() + 1;
160 } else if (*ch == ampersand) {
162 drawText.append(QStringRef(&text, textStart, textLength));
163 parseEntity(ch, text, drawText);
164 textStart = ch - text.constData() + 1;
173 drawText.append(QStringRef(&text, textStart, textLength));
174 if (rangeStart != drawText.length() && formatStack.count()) {
175 QTextLayout::FormatRange formatRange;
176 formatRange.format = formatStack.top();
177 formatRange.start = rangeStart;
178 formatRange.length = drawText.length() - rangeStart;
179 ranges.append(formatRange);
182 layout.setText(drawText);
183 layout.setAdditionalFormats(ranges);
186 bool QDeclarativeStyledTextPrivate::parseTag(const QChar *&ch, const QString &textIn, QString &textOut, QTextCharFormat &format)
190 int tagStart = ch - textIn.constData();
192 while (!ch->isNull()) {
193 if (*ch == greaterThan) {
194 QStringRef tag(&textIn, tagStart, tagLength);
195 const QChar char0 = tag.at(0);
196 if (char0 == QLatin1Char('b')) {
198 format.setFontWeight(QFont::Bold);
199 else if (tagLength == 2 && tag.at(1) == QLatin1Char('r')) {
200 textOut.append(QChar(QChar::LineSeparator));
203 } else if (char0 == QLatin1Char('i')) {
205 format.setFontItalic(true);
208 } else if (ch->isSpace()) {
210 QStringRef tag(&textIn, tagStart, tagLength);
211 if (tag == QLatin1String("font"))
212 return parseFontAttributes(ch, textIn, format);
213 if (*ch == greaterThan || ch->isNull())
215 } else if (*ch != slash){
224 bool QDeclarativeStyledTextPrivate::parseCloseTag(const QChar *&ch, const QString &textIn)
228 int tagStart = ch - textIn.constData();
230 while (!ch->isNull()) {
231 if (*ch == greaterThan) {
232 QStringRef tag(&textIn, tagStart, tagLength);
233 const QChar char0 = tag.at(0);
234 if (char0 == QLatin1Char('b')) {
237 else if (tag.at(1) == QLatin1Char('r') && tagLength == 2)
239 } else if (char0 == QLatin1Char('i')) {
242 } else if (tag == QLatin1String("font")) {
246 } else if (!ch->isSpace()){
255 void QDeclarativeStyledTextPrivate::parseEntity(const QChar *&ch, const QString &textIn, QString &textOut)
257 int entityStart = ch - textIn.constData();
258 int entityLength = 0;
259 while (!ch->isNull()) {
260 if (*ch == QLatin1Char(';')) {
261 QStringRef entity(&textIn, entityStart, entityLength);
262 if (entity == QLatin1String("gt"))
263 textOut += QChar(62);
264 else if (entity == QLatin1String("lt"))
265 textOut += QChar(60);
266 else if (entity == QLatin1String("amp"))
267 textOut += QChar(38);
275 bool QDeclarativeStyledTextPrivate::parseFontAttributes(const QChar *&ch, const QString &textIn, QTextCharFormat &format)
278 QPair<QStringRef,QStringRef> attr;
280 attr = parseAttribute(ch, textIn);
281 if (attr.first == QLatin1String("color")) {
283 format.setForeground(QColor(attr.second.toString()));
284 } else if (attr.first == QLatin1String("size")) {
286 int size = attr.second.toString().toInt();
287 if (attr.second.at(0) == QLatin1Char('-') || attr.second.at(0) == QLatin1Char('+'))
289 if (size >= 1 && size <= 7) {
290 static const qreal scaling[] = { 0.7, 0.8, 1.0, 1.2, 1.5, 2.0, 2.4 };
291 format.setFontPointSize(baseFont.pointSize() * scaling[size-1]);
294 } while (!ch->isNull() && !attr.first.isEmpty());
299 QPair<QStringRef,QStringRef> QDeclarativeStyledTextPrivate::parseAttribute(const QChar *&ch, const QString &textIn)
303 int attrStart = ch - textIn.constData();
305 while (!ch->isNull()) {
306 if (*ch == greaterThan) {
308 } else if (*ch == equals) {
310 if (*ch != singleQuote && *ch != doubleQuote) {
311 while (*ch != greaterThan && !ch->isNull())
318 QStringRef attr(&textIn, attrStart, attrLength);
319 QStringRef val = parseValue(ch, textIn);
321 return QPair<QStringRef,QStringRef>(attr,val);
329 return QPair<QStringRef,QStringRef>();
332 QStringRef QDeclarativeStyledTextPrivate::parseValue(const QChar *&ch, const QString &textIn)
334 int valStart = ch - textIn.constData();
336 while (*ch != singleQuote && *ch != doubleQuote && !ch->isNull()) {
344 return QStringRef(&textIn, valStart, valLength);