diff options
author | Michael Brasser <michael.brasser@nokia.com> | 2009-12-15 02:59:22 (GMT) |
---|---|---|
committer | Michael Brasser <michael.brasser@nokia.com> | 2009-12-15 02:59:22 (GMT) |
commit | 7ee38fe125ef9eebf7023ffd6af3f7b9c98c62d0 (patch) | |
tree | dbbc00403d5efa4e5bfeb47f6822e893780d35ed /tests/benchmarks/declarative | |
parent | 5533ff0fc0392f4a9d398646a4cc6f4140fc204a (diff) | |
download | Qt-7ee38fe125ef9eebf7023ffd6af3f7b9c98c62d0.zip Qt-7ee38fe125ef9eebf7023ffd6af3f7b9c98c62d0.tar.gz Qt-7ee38fe125ef9eebf7023ffd6af3f7b9c98c62d0.tar.bz2 |
Add basic text benchmark.
Diffstat (limited to 'tests/benchmarks/declarative')
-rw-r--r-- | tests/benchmarks/declarative/text/text.pro | 8 | ||||
-rw-r--r-- | tests/benchmarks/declarative/text/tst_text.cpp | 120 |
2 files changed, 128 insertions, 0 deletions
diff --git a/tests/benchmarks/declarative/text/text.pro b/tests/benchmarks/declarative/text/text.pro new file mode 100644 index 0000000..e4840b1 --- /dev/null +++ b/tests/benchmarks/declarative/text/text.pro @@ -0,0 +1,8 @@ +load(qttest_p4) +QT += script +TEMPLATE = app +TARGET = tst_text +macx:CONFIG -= app_bundle + +SOURCES += tst_text.cpp + diff --git a/tests/benchmarks/declarative/text/tst_text.cpp b/tests/benchmarks/declarative/text/tst_text.cpp new file mode 100644 index 0000000..4fd84e6 --- /dev/null +++ b/tests/benchmarks/declarative/text/tst_text.cpp @@ -0,0 +1,120 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** 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, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <qtest.h> +#include <QTextLayout> +#include <QPainter> +#include <QSize> +#include <qmath.h> + +class tst_text : public QObject +{ + Q_OBJECT +public: + tst_text() {} + +private slots: + void layout(); + void paintToPixmap(); +}; + +QSize setupTextLayout(QTextLayout *layout) +{ + bool wrap = true; + int wrapWidth = 300; + layout->setCacheEnabled(true); + + int height = 0; + qreal widthUsed = 0; + qreal lineWidth = 0; + + //set manual width + if (wrap) + lineWidth = wrapWidth; + + layout->beginLayout(); + + while (1) { + QTextLine line = layout->createLine(); + if (!line.isValid()) + break; + + if (wrap) + line.setLineWidth(lineWidth); + } + layout->endLayout(); + + for (int i = 0; i < layout->lineCount(); ++i) { + QTextLine line = layout->lineAt(i); + widthUsed = qMax(widthUsed, line.naturalTextWidth()); + line.setPosition(QPointF(0, height)); + height += int(line.height()); + } + return QSize(qCeil(widthUsed), height); +} + +void tst_text::layout() +{ + //get rid of initialization effects + QTextLayout layout("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); + setupTextLayout(&layout); + + QBENCHMARK { + QTextLayout layout("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); + setupTextLayout(&layout); + } +} + +void tst_text::paintToPixmap() +{ + QTextLayout layout("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."); + QSize size = setupTextLayout(&layout); + + QBENCHMARK { + QPixmap img(size); + img.fill(Qt::transparent); + QPainter p(&img); + layout.draw(&p, QPointF(0, 0)); + } +} + +QTEST_MAIN(tst_text) +#include "tst_text.moc" |