-
Notifications
You must be signed in to change notification settings - Fork 3
/
StyledText.cpp
813 lines (750 loc) · 29 KB
/
StyledText.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtQuick module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QStack>
#include <QVector>
#include <QPainter>
#include <QTextLayout>
#include <QDebug>
#include <qmath.h>
#include "StyledText.h"
#include <QQmlContext>
/*
StyledText supports few tags:
<b></b> - bold
<strong></strong> - bold
<i></i> - italic
<br> - new line
<p> - paragraph
<u> - underlined text
<font color="color_name" size="1-7"></font>
<h1> to <h6> - headers
<a href=""> - anchor
<ol type="">, <ul type=""> and <li> - ordered and unordered lists
<pre></pre> - preformated
<img src=""> - images
The opening and closing tags must be correctly nested.
*/
int qt_defaultDpi();
class StyledTextPrivate
{
public:
enum ListType { Ordered, Unordered };
enum ListFormat { Bullet, Disc, Square, Decimal, LowerAlpha, UpperAlpha, LowerRoman, UpperRoman };
struct List {
int level;
ListType type;
ListFormat format;
};
StyledTextPrivate(const QString &t, QTextLayout &l,
QList<StyledTextImgTag*> &imgTags,
const QUrl &baseUrl,
QQmlContext *context,
bool preloadImages,
bool *fontSizeModified, QTextCharFormat defaultFormat)
: text(t), layout(l), imgTags(&imgTags), baseFont(layout.font()), baseUrl(baseUrl), hasNewLine(true), nbImages(0), updateImagePositions(false)
, preFormat(false), prependSpace(false), hasSpace(true), preloadImages(preloadImages), fontSizeModified(fontSizeModified), context(context), defaultFormat(defaultFormat)
{
}
void parse();
void appendText(const QString &textIn, int start, int length, QString &textOut);
bool parseTag(const QChar *&ch, const QString &textIn, QString &textOut, QTextCharFormat &format);
bool parseCloseTag(const QChar *&ch, const QString &textIn, QString &textOut);
void parseEntity(const QChar *&ch, const QString &textIn, QString &textOut);
bool parseFontAttributes(const QChar *&ch, const QString &textIn, QTextCharFormat &format);
bool parseOrderedListAttributes(const QChar *&ch, const QString &textIn);
bool parseUnorderedListAttributes(const QChar *&ch, const QString &textIn);
bool parseAnchorAttributes(const QChar *&ch, const QString &textIn, QTextCharFormat &format);
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())
++ch;
}
static QString toAlpha(int value, bool upper);
static QString toRoman(int value, bool upper);
QString text;
QTextLayout &layout;
QList<StyledTextImgTag*> *imgTags;
QFont baseFont;
QStack<List> listStack;
QUrl baseUrl;
bool hasNewLine;
int nbImages;
bool updateImagePositions;
bool preFormat;
bool prependSpace;
bool hasSpace;
bool preloadImages;
bool *fontSizeModified;
QQmlContext *context;
QTextCharFormat defaultFormat;
static const QChar lessThan;
static const QChar greaterThan;
static const QChar equals;
static const QChar singleQuote;
static const QChar doubleQuote;
static const QChar slash;
static const QChar ampersand;
static const QChar bullet;
static const QChar disc;
static const QChar square;
static const QChar lineFeed;
static const QChar space;
static const int tabsize = 6;
};
const QChar StyledTextPrivate::lessThan(QLatin1Char('<'));
const QChar StyledTextPrivate::greaterThan(QLatin1Char('>'));
const QChar StyledTextPrivate::equals(QLatin1Char('='));
const QChar StyledTextPrivate::singleQuote(QLatin1Char('\''));
const QChar StyledTextPrivate::doubleQuote(QLatin1Char('\"'));
const QChar StyledTextPrivate::slash(QLatin1Char('/'));
const QChar StyledTextPrivate::ampersand(QLatin1Char('&'));
const QChar StyledTextPrivate::bullet(0x2022);
const QChar StyledTextPrivate::disc(0x25e6);
const QChar StyledTextPrivate::square(0x25a1);
const QChar StyledTextPrivate::lineFeed(QLatin1Char('\n'));
const QChar StyledTextPrivate::space(QLatin1Char(' '));
StyledText::StyledText(const QString &string, QTextLayout &layout,
QList<StyledTextImgTag*> &imgTags,
const QUrl &baseUrl,
QQmlContext *context,
bool preloadImages,
bool *fontSizeModified, QTextCharFormat defaultFormat)
: d(new StyledTextPrivate(string, layout, imgTags, baseUrl, context, preloadImages, fontSizeModified, defaultFormat))
{
}
StyledText::~StyledText()
{
delete d;
}
void StyledText::parse(const QString &string, QTextLayout &layout,
QList<StyledTextImgTag*> &imgTags,
const QUrl &baseUrl,
QQmlContext *context,
bool preloadImages,
bool *fontSizeModified, QTextCharFormat defaultFormat)
{
if (string.isEmpty())
return;
StyledText styledText(string, layout, imgTags, baseUrl, context, preloadImages, fontSizeModified, defaultFormat);
styledText.d->parse();
}
void StyledTextPrivate::parse()
{
QList<QTextLayout::FormatRange> ranges;
QStack<QTextCharFormat> formatStack;
formatStack.push(defaultFormat);
QString drawText;
drawText.reserve(text.count());
updateImagePositions = !imgTags->isEmpty();
int textStart = 0;
int textLength = 0;
int rangeStart = 0;
bool formatChanged = true;
const QChar *ch = text.constData();
while (!ch->isNull()) {
if (*ch == lessThan) {
if (textLength) {
appendText(text, textStart, textLength, drawText);
} else if (prependSpace) {
drawText.append(space);
prependSpace = false;
hasSpace = true;
}
if (rangeStart != drawText.length() && formatStack.count()) {
if (formatChanged) {
QTextLayout::FormatRange formatRange;
formatRange.format = formatStack.top();
formatRange.start = rangeStart;
formatRange.length = drawText.length() - rangeStart;
ranges.append(formatRange);
formatChanged = false;
} else if (ranges.count()) {
ranges.last().length += drawText.length() - rangeStart;
}
}
rangeStart = drawText.length();
++ch;
if (*ch == slash) {
++ch;
if (parseCloseTag(ch, text, drawText)) {
if (formatStack.count()) {
formatChanged = true;
formatStack.pop();
}
}
} else {
QTextCharFormat format;
if (formatStack.count())
format = formatStack.top();
if (parseTag(ch, text, drawText, format)) {
formatChanged = true;
formatStack.push(format);
}
}
textStart = ch - text.constData() + 1;
textLength = 0;
} else if (*ch == ampersand) {
++ch;
appendText(text, textStart, textLength, drawText);
parseEntity(ch, text, drawText);
textStart = ch - text.constData() + 1;
textLength = 0;
} else if (ch->isSpace()) {
if (textLength)
appendText(text, textStart, textLength, drawText);
if (!preFormat) {
prependSpace = !hasSpace;
for (const QChar *n = ch + 1; !n->isNull() && n->isSpace(); ++n)
ch = n;
hasNewLine = false;
} else if (*ch == lineFeed) {
drawText.append(QChar(QChar::LineSeparator));
hasNewLine = true;
} else {
drawText.append(QChar(QChar::Nbsp));
hasNewLine = false;
}
textStart = ch - text.constData() + 1;
textLength = 0;
} else {
++textLength;
}
if (!ch->isNull())
++ch;
}
if (textLength)
appendText(text, textStart, textLength, drawText);
if (rangeStart != drawText.length() && formatStack.count()) {
if (formatChanged) {
QTextLayout::FormatRange formatRange;
formatRange.format = formatStack.top();
formatRange.start = rangeStart;
formatRange.length = drawText.length() - rangeStart;
ranges.append(formatRange);
} else if (ranges.count()) {
ranges.last().length += drawText.length() - rangeStart;
}
}
layout.setText(drawText);
layout.setAdditionalFormats(ranges);
}
void StyledTextPrivate::appendText(const QString &textIn, int start, int length, QString &textOut)
{
if (prependSpace)
textOut.append(space);
textOut.append(QStringRef(&textIn, start, length));
prependSpace = false;
hasSpace = false;
hasNewLine = false;
}
//
// Calculates and sets the correct font size in points
// depending on the size multiplier and base font.
//
void StyledTextPrivate::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 StyledTextPrivate::parseTag(const QChar *&ch, const QString &textIn, QString &textOut, QTextCharFormat &format)
{
skipSpace(ch);
int tagStart = ch - textIn.constData();
int tagLength = 0;
while (!ch->isNull()) {
if (*ch == greaterThan) {
if (tagLength == 0)
return false;
QStringRef tag(&textIn, tagStart, tagLength);
const QChar char0 = tag.at(0);
if (char0 == QLatin1Char('b')) {
if (tagLength == 1) {
format.setFontWeight(QFont::Bold);
return true;
} else if (tagLength == 2 && tag.at(1) == QLatin1Char('r')) {
textOut.append(QChar(QChar::LineSeparator));
hasSpace = true;
prependSpace = false;
return false;
}
} else if (char0 == QLatin1Char('i')) {
if (tagLength == 1) {
format.setFontItalic(true);
return true;
}
} else if (char0 == QLatin1Char('p')) {
if (tagLength == 1) {
if (!hasNewLine)
textOut.append(QChar::LineSeparator);
hasSpace = true;
prependSpace = false;
} else if (tag == QLatin1String("pre")) {
preFormat = true;
if (!hasNewLine)
textOut.append(QChar::LineSeparator);
format.setFontFamily(QString::fromLatin1("Courier New,courier"));
format.setFontFixedPitch(true);
return true;
}
} else if (char0 == QLatin1Char('u')) {
if (tagLength == 1) {
format.setFontUnderline(true);
return true;
} else if (tag == QLatin1String("ul")) {
List listItem;
listItem.level = 0;
listItem.type = Unordered;
listItem.format = Bullet;
listStack.push(listItem);
}
} else if (char0 == QLatin1Char('h') && tagLength == 2) {
int level = tag.at(1).digitValue();
if (level >= 1 && level <= 6) {
if (!hasNewLine)
textOut.append(QChar::LineSeparator);
hasSpace = true;
prependSpace = false;
setFontSize(7 - level, format);
format.setFontWeight(QFont::Bold);
return true;
}
} else if (tag == QLatin1String("strong")) {
format.setFontWeight(QFont::Bold);
return true;
} else if (tag == QLatin1String("ol")) {
List listItem;
listItem.level = 0;
listItem.type = Ordered;
listItem.format = Decimal;
listStack.push(listItem);
} else if (tag == QLatin1String("li")) {
if (!hasNewLine)
textOut.append(QChar(QChar::LineSeparator));
if (!listStack.isEmpty()) {
int count = ++listStack.top().level;
for (int i = 0; i < listStack.size(); ++i)
textOut += QString(tabsize, QChar::Nbsp);
switch (listStack.top().format) {
case Decimal:
textOut += QString::number(count) + QLatin1Char('.');
break;
case LowerAlpha:
textOut += toAlpha(count, false) + QLatin1Char('.');
break;
case UpperAlpha:
textOut += toAlpha(count, true) + QLatin1Char('.');
break;
case LowerRoman:
textOut += toRoman(count, false) + QLatin1Char('.');
break;
case UpperRoman:
textOut += toRoman(count, true) + QLatin1Char('.');
break;
case Bullet:
textOut += bullet;
break;
case Disc:
textOut += disc;
break;
case Square:
textOut += square;
break;
}
textOut += QString(2, QChar::Nbsp);
}
}
return false;
} else if (ch->isSpace()) {
// may have params.
QStringRef tag(&textIn, tagStart, tagLength);
if (tag == QLatin1String("font"))
return parseFontAttributes(ch, textIn, format);
if (tag == QLatin1String("ol")) {
parseOrderedListAttributes(ch, textIn);
return false; // doesn't modify format
}
if (tag == QLatin1String("ul")) {
parseUnorderedListAttributes(ch, textIn);
return false; // doesn't modify format
}
if (tag == QLatin1String("a")) {
return parseAnchorAttributes(ch, textIn, format);
}
if (tag == QLatin1String("img")) {
parseImageAttributes(ch, textIn, textOut);
return false;
}
if (*ch == greaterThan || ch->isNull())
continue;
} else if (*ch != slash) {
tagLength++;
}
++ch;
}
return false;
}
bool StyledTextPrivate::parseCloseTag(const QChar *&ch, const QString &textIn, QString &textOut)
{
skipSpace(ch);
int tagStart = ch - textIn.constData();
int tagLength = 0;
while (!ch->isNull()) {
if (*ch == greaterThan) {
if (tagLength == 0)
return false;
QStringRef tag(&textIn, tagStart, tagLength);
const QChar char0 = tag.at(0);
hasNewLine = false;
if (char0 == QLatin1Char('b')) {
if (tagLength == 1)
return true;
else if (tag.at(1) == QLatin1Char('r') && tagLength == 2)
return false;
} else if (char0 == QLatin1Char('i')) {
if (tagLength == 1)
return true;
} else if (char0 == QLatin1Char('a')) {
if (tagLength == 1)
return true;
} else if (char0 == QLatin1Char('p')) {
if (tagLength == 1) {
textOut.append(QChar::LineSeparator);
hasNewLine = true;
hasSpace = true;
return false;
} else if (tag == QLatin1String("pre")) {
preFormat = false;
if (!hasNewLine)
textOut.append(QChar::LineSeparator);
hasNewLine = true;
hasSpace = true;
return true;
}
} else if (char0 == QLatin1Char('u')) {
if (tagLength == 1)
return true;
else if (tag == QLatin1String("ul")) {
if (!listStack.isEmpty()) {
listStack.pop();
if (!listStack.count())
textOut.append(QChar::LineSeparator);
}
return false;
}
} else if (char0 == QLatin1Char('h') && tagLength == 2) {
textOut.append(QChar::LineSeparator);
hasNewLine = true;
hasSpace = true;
return true;
} else if (tag == QLatin1String("font")) {
return true;
} else if (tag == QLatin1String("strong")) {
return true;
} else if (tag == QLatin1String("ol")) {
if (!listStack.isEmpty()) {
listStack.pop();
if (!listStack.count())
textOut.append(QChar::LineSeparator);
}
return false;
} else if (tag == QLatin1String("li")) {
return false;
}
return false;
} else if (!ch->isSpace()){
tagLength++;
}
++ch;
}
return false;
}
void StyledTextPrivate::parseEntity(const QChar *&ch, const QString &textIn, QString &textOut)
{
int entityStart = ch - textIn.constData();
int entityLength = 0;
while (!ch->isNull()) {
if (*ch == QLatin1Char(';')) {
QStringRef entity(&textIn, entityStart, entityLength);
if (entity == QLatin1String("gt"))
textOut += QChar(62);
else if (entity == QLatin1String("lt"))
textOut += QChar(60);
else if (entity == QLatin1String("amp"))
textOut += QChar(38);
else if (entity == QLatin1String("quot"))
textOut += QChar(34);
return;
}
++entityLength;
++ch;
}
}
bool StyledTextPrivate::parseFontAttributes(const QChar *&ch, const QString &textIn, QTextCharFormat &format)
{
bool valid = false;
QPair<QStringRef,QStringRef> attr;
do {
attr = parseAttribute(ch, textIn);
if (attr.first == QLatin1String("color")) {
valid = true;
format.setForeground(QColor(attr.second.toString()));
} else if (attr.first == QLatin1String("size")) {
valid = true;
int size = attr.second.toString().toInt();
if (attr.second.at(0) == QLatin1Char('-') || attr.second.at(0) == QLatin1Char('+'))
size += 3;
if (size >= 1 && size <= 7)
setFontSize(size, format);
}
} while (!ch->isNull() && !attr.first.isEmpty());
return valid;
}
bool StyledTextPrivate::parseOrderedListAttributes(const QChar *&ch, const QString &textIn)
{
bool valid = false;
List listItem;
listItem.level = 0;
listItem.type = Ordered;
listItem.format = Decimal;
QPair<QStringRef,QStringRef> attr;
do {
attr = parseAttribute(ch, textIn);
if (attr.first == QLatin1String("type")) {
valid = true;
if (attr.second == QLatin1String("a"))
listItem.format = LowerAlpha;
else if (attr.second == QLatin1String("A"))
listItem.format = UpperAlpha;
else if (attr.second == QLatin1String("i"))
listItem.format = LowerRoman;
else if (attr.second == QLatin1String("I"))
listItem.format = UpperRoman;
}
} while (!ch->isNull() && !attr.first.isEmpty());
listStack.push(listItem);
return valid;
}
bool StyledTextPrivate::parseUnorderedListAttributes(const QChar *&ch, const QString &textIn)
{
bool valid = false;
List listItem;
listItem.level = 0;
listItem.type = Unordered;
listItem.format = Bullet;
QPair<QStringRef,QStringRef> attr;
do {
attr = parseAttribute(ch, textIn);
if (attr.first == QLatin1String("type")) {
valid = true;
if (attr.second == QLatin1String("disc"))
listItem.format = Disc;
else if (attr.second == QLatin1String("square"))
listItem.format = Square;
}
} while (!ch->isNull() && !attr.first.isEmpty());
listStack.push(listItem);
return valid;
}
bool StyledTextPrivate::parseAnchorAttributes(const QChar *&ch, const QString &textIn, QTextCharFormat &format)
{
bool valid = false;
QPair<QStringRef,QStringRef> attr;
do {
attr = parseAttribute(ch, textIn);
if (attr.first == QLatin1String("href")) {
format.setAnchorHref(attr.second.toString());
format.setAnchor(true);
format.setFontUnderline(true);
valid = true;
}
} while (!ch->isNull() && !attr.first.isEmpty());
return valid;
}
void StyledTextPrivate::parseImageAttributes(const QChar *&ch, const QString &textIn, QString &textOut)
{
qreal imgWidth = 0.0;
if (!updateImagePositions) {
StyledTextImgTag *image = new StyledTextImgTag;
image->position = textOut.length() + 1;
QPair<QStringRef,QStringRef> attr;
do {
attr = parseAttribute(ch, textIn);
if (attr.first == QLatin1String("src")) {
image->url = QUrl(attr.second.toString());
} else if (attr.first == QLatin1String("width")) {
image->size.setWidth(attr.second.toString().toInt());
} else if (attr.first == QLatin1String("height")) {
image->size.setHeight(attr.second.toString().toInt());
} else if (attr.first == QLatin1String("align")) {
if (attr.second.toString() == QLatin1String("top")) {
image->align = StyledTextImgTag::Top;
} else if (attr.second.toString() == QLatin1String("middle")) {
image->align = StyledTextImgTag::Middle;
}
}
} while (!ch->isNull() && !attr.first.isEmpty());
if (preloadImages && !image->size.isValid()) {
// if we don't know its size but the image is a local image,
// we load it in the pixmap cache and save its implicit size
// to avoid a relayout later on.
QUrl url = baseUrl.resolved(image->url);
if (url.isLocalFile()) {
image->pix = QImage(url.toLocalFile());
image->size = image->pix.size();
}
}
imgWidth = image->size.width();
imgTags->append(image);
} else {
// if we already have a list of img tags for this text
// we only want to update the positions of these tags.
StyledTextImgTag *image = imgTags->value(nbImages);
image->position = textOut.length() + 1;
imgWidth = image->size.width();
QPair<QStringRef,QStringRef> attr;
do {
attr = parseAttribute(ch, textIn);
} while (!ch->isNull() && !attr.first.isEmpty());
nbImages++;
}
QFontMetricsF fm(layout.font());
QString padding(qFloor(imgWidth / fm.width(QChar::Nbsp)), QChar::Nbsp);
textOut += QLatin1Char(' ');
textOut += padding;
textOut += QLatin1Char(' ');
}
QPair<QStringRef,QStringRef> StyledTextPrivate::parseAttribute(const QChar *&ch, const QString &textIn)
{
skipSpace(ch);
int attrStart = ch - textIn.constData();
int attrLength = 0;
while (!ch->isNull()) {
if (*ch == greaterThan) {
break;
} else if (*ch == equals) {
++ch;
if (*ch != singleQuote && *ch != doubleQuote) {
while (*ch != greaterThan && !ch->isNull())
++ch;
break;
}
++ch;
if (!attrLength)
break;
QStringRef attr(&textIn, attrStart, attrLength);
QStringRef val = parseValue(ch, textIn);
if (!val.isEmpty())
return QPair<QStringRef,QStringRef>(attr,val);
break;
} else {
++attrLength;
}
++ch;
}
return QPair<QStringRef,QStringRef>();
}
QStringRef StyledTextPrivate::parseValue(const QChar *&ch, const QString &textIn)
{
int valStart = ch - textIn.constData();
int valLength = 0;
while (*ch != singleQuote && *ch != doubleQuote && !ch->isNull()) {
++valLength;
++ch;
}
if (ch->isNull())
return QStringRef();
++ch; // skip quote
return QStringRef(&textIn, valStart, valLength);
}
QString StyledTextPrivate::toAlpha(int value, bool upper)
{
const char baseChar = upper ? 'A' : 'a';
QString result;
int c = value;
while (c > 0) {
c--;
result.prepend(QChar(baseChar + (c % 26)));
c /= 26;
}
return result;
}
QString StyledTextPrivate::toRoman(int value, bool upper)
{
QString result = QLatin1String("?");
// works for up to 4999 items
if (value < 5000) {
QByteArray romanNumeral;
static const char romanSymbolsLower[] = "iiivixxxlxcccdcmmmm";
static const char romanSymbolsUpper[] = "IIIVIXXXLXCCCDCMMMM";
QByteArray romanSymbols;
if (!upper)
romanSymbols = QByteArray::fromRawData(romanSymbolsLower, sizeof(romanSymbolsLower));
else
romanSymbols = QByteArray::fromRawData(romanSymbolsUpper, sizeof(romanSymbolsUpper));
int c[] = { 1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000 };
int n = value;
for (int i = 12; i >= 0; n %= c[i], i--) {
int q = n / c[i];
if (q > 0) {
int startDigit = i + (i + 3) / 4;
int numDigits;
if (i % 4) {
if ((i - 2) % 4)
numDigits = 2;
else
numDigits = 1;
}
else
numDigits = q;
romanNumeral.append(romanSymbols.mid(startDigit, numDigits));
}
}
result = QString::fromLatin1(romanNumeral);
}
return result;
}