summaryrefslogtreecommitdiffstats
path: root/tests/auto/gui
diff options
context:
space:
mode:
authorPierre Rossi <pierre.rossi@digia.com>2012-02-10 20:42:13 +0100
committerThe Qt Project <gerrit-noreply@qt-project.org>2012-10-08 17:22:56 +0200
commit101d04681f4ceb7410681eae684534a206a9d90a (patch)
treebb037f84ac82fc9431a72ed7ec9ecdff18ddef7c /tests/auto/gui
parent0e8a2788d84c814b698848b699af8a2c8ba3179f (diff)
Handle additional format ranges when itemizing.
This is useful when the additional formats are used on a text layout using a raw font. It can also come in handy for input methods operating on a QTextDocument. We now consider all format range edges to generate the associated items. The capitalization can be overridden via the additionnal formats mechanism. Adds an autotest that checks that this works with font capitalization. Change-Id: I782d2c48d05b0dfbad480a9ca77198465292b358 Reviewed-by: Konstantin Ritt <ritt.ks@gmail.com>
Diffstat (limited to 'tests/auto/gui')
-rw-r--r--tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp
index 77f603af4f..2f8fab9e64 100644
--- a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp
+++ b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp
@@ -139,6 +139,11 @@ private slots:
void xToCursorForLigatures();
void cursorInNonStopChars();
+#ifndef QT_NO_RAWFONT
+ void additionalFormatsCapitalizationWithRawFont_data();
+ void additionalFormatsCapitalizationWithRawFont();
+#endif
+
private:
QFont testFont;
};
@@ -2012,5 +2017,108 @@ void tst_QTextLayout::cursorInNonStopChars()
QVERIFY(line.cursorToX(2) == line.cursorToX(3));
}
+#ifndef QT_NO_RAWFONT
+
+typedef QList<QTextLayout::FormatRange> FormatRangeList;
+Q_DECLARE_METATYPE(FormatRangeList)
+
+void tst_QTextLayout::additionalFormatsCapitalizationWithRawFont_data()
+{
+ QTest::addColumn<QString>("text");
+ QTest::addColumn<FormatRangeList>("additionalFormats");
+
+ QString textSample = QLatin1String("Code Less, Create More.");
+
+ QTest::newRow("No Format") << textSample << FormatRangeList();
+ QTextLayout::FormatRange range;
+ range.start = 0;
+ range.length = textSample.size();
+ range.format.setFontCapitalization(QFont::SmallCaps);
+ QTest::newRow("SmallCaps") << textSample << (FormatRangeList() << range);
+ range.format.setFontCapitalization(QFont::Capitalize);
+ QTest::newRow("Capitalize") << textSample.toLower() << (FormatRangeList() << range);
+ range.format.setFontCapitalization(QFont::AllLowercase);
+ QTest::newRow("AllLowercase") << textSample << (FormatRangeList() << range);
+
+ FormatRangeList rangeList;
+ range.start = 0;
+ range.length = 5;
+ range.format.setFontCapitalization(QFont::SmallCaps);
+ rangeList << range;
+ range.start = 5;
+ range.format.setFontCapitalization(QFont::AllLowercase);
+ rangeList << range;
+ range.start = 18;
+ range.format.setFontCapitalization(QFont::Capitalize);
+ rangeList << range;
+ QTest::newRow("MixAndMatch") << textSample << rangeList;
+}
+
+void tst_QTextLayout::additionalFormatsCapitalizationWithRawFont()
+{
+ QFETCH(QString, text);
+ QFETCH(FormatRangeList, additionalFormats);
+
+ QRawFont rawFont = QRawFont::fromFont(testFont);
+
+ QTextLayout layout(text);
+ layout.setRawFont(rawFont);
+ layout.setAdditionalFormats(additionalFormats);
+ layout.beginLayout();
+ layout.createLine();
+ layout.endLayout();
+
+ QString expectedChars;
+ QTextBoundaryFinder splitter(QTextBoundaryFinder::Word,text.constData(), text.length(), 0, 0);
+
+ for (int i = 0; i < text.size(); ++i) {
+ bool hasFormat = false;
+ Q_FOREACH (const QTextLayout::FormatRange& range, additionalFormats) {
+ if (i >= range.start && i < range.start + range.length) {
+ hasFormat = true;
+ const QChar ch(text.at(i));
+ switch (range.format.fontCapitalization()) {
+ case QFont::SmallCaps:
+ case QFont::AllUppercase:
+ expectedChars += ch.toUpper();
+ break;
+ case QFont::AllLowercase:
+ expectedChars += ch.toLower();
+ break;
+ case QFont::Capitalize:
+ splitter.setPosition(i);
+ if (splitter.boundaryReasons() & QTextBoundaryFinder::StartWord)
+ expectedChars += ch.toUpper();
+ else
+ expectedChars += ch;
+ break;
+ case QFont::MixedCase:
+ default:
+ expectedChars += ch;
+ break;
+ }
+ }
+ }
+ if (!hasFormat)
+ expectedChars += text.at(i);
+ }
+
+ QVERIFY(layout.glyphRuns().size() > 0);
+ rawFont = layout.glyphRuns().first().rawFont();
+ // Can't assume anything about the order of the glyphs we get once they're split into several glyphRuns.
+ // Let's just compare the sets:
+ QVector<quint32> expected = rawFont.glyphIndexesForString(expectedChars);
+ qSort(expected);
+ QVector<quint32> sortedOutput;
+ sortedOutput.reserve(expected.size());
+ Q_FOREACH (const QGlyphRun& run, layout.glyphRuns()) {
+ Q_FOREACH (quint32 glyphIndex, run.glyphIndexes())
+ sortedOutput.append(glyphIndex);
+ }
+ qSort(sortedOutput);
+ QCOMPARE(sortedOutput, expected);
+}
+#endif
+
QTEST_MAIN(tst_QTextLayout)
#include "tst_qtextlayout.moc"