0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
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
|
|
14 |
** this package.
|
|
15 |
**
|
|
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.
|
|
23 |
**
|
|
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.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
|
|
43 |
#include <QtTest/QtTest>
|
|
44 |
#include <qfont.h>
|
|
45 |
#include <qfontmetrics.h>
|
|
46 |
#include <qfontdatabase.h>
|
|
47 |
#include <qstringlist.h>
|
|
48 |
#include <qlist.h>
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
//TESTED_CLASS=
|
|
53 |
//TESTED_FILES=
|
|
54 |
|
|
55 |
class tst_QFontMetrics : public QObject
|
|
56 |
{
|
|
57 |
Q_OBJECT
|
|
58 |
|
|
59 |
public:
|
|
60 |
tst_QFontMetrics();
|
|
61 |
virtual ~tst_QFontMetrics();
|
|
62 |
|
|
63 |
public slots:
|
|
64 |
void init();
|
|
65 |
void cleanup();
|
|
66 |
private slots:
|
|
67 |
void same();
|
|
68 |
void metrics();
|
|
69 |
void boundingRect();
|
|
70 |
void elidedText_data();
|
|
71 |
void elidedText();
|
|
72 |
void veryNarrowElidedText();
|
|
73 |
void averageCharWidth();
|
|
74 |
void elidedMultiLength();
|
|
75 |
void bearingIncludedInBoundingRect();
|
|
76 |
};
|
|
77 |
|
|
78 |
tst_QFontMetrics::tst_QFontMetrics()
|
|
79 |
|
|
80 |
{
|
|
81 |
}
|
|
82 |
|
|
83 |
tst_QFontMetrics::~tst_QFontMetrics()
|
|
84 |
{
|
|
85 |
|
|
86 |
}
|
|
87 |
|
|
88 |
void tst_QFontMetrics::init()
|
|
89 |
{
|
|
90 |
}
|
|
91 |
|
|
92 |
void tst_QFontMetrics::cleanup()
|
|
93 |
{
|
|
94 |
}
|
|
95 |
|
|
96 |
void tst_QFontMetrics::same()
|
|
97 |
{
|
|
98 |
QFont font;
|
|
99 |
font.setBold(true);
|
|
100 |
QFontMetrics fm(font);
|
|
101 |
const QString text = QLatin1String("Some stupid STRING");
|
|
102 |
QCOMPARE(fm.size(0, text), fm.size(0, text)) ;
|
|
103 |
}
|
|
104 |
|
|
105 |
|
|
106 |
void tst_QFontMetrics::metrics()
|
|
107 |
{
|
|
108 |
QFont font;
|
|
109 |
QFontDatabase fdb;
|
|
110 |
|
|
111 |
// Query the QFontDatabase for a specific font, store the
|
|
112 |
// result in family, style and size.
|
|
113 |
QStringList families = fdb.families();
|
|
114 |
if (families.isEmpty())
|
|
115 |
return;
|
|
116 |
|
|
117 |
QStringList::ConstIterator f_it, f_end = families.end();
|
|
118 |
for (f_it = families.begin(); f_it != f_end; ++f_it) {
|
|
119 |
const QString &family = *f_it;
|
|
120 |
|
|
121 |
QStringList styles = fdb.styles(family);
|
|
122 |
QStringList::ConstIterator s_it, s_end = styles.end();
|
|
123 |
for (s_it = styles.begin(); s_it != s_end; ++s_it) {
|
|
124 |
const QString &style = *s_it;
|
|
125 |
|
|
126 |
if (fdb.isSmoothlyScalable(family, style)) {
|
|
127 |
// smoothly scalable font... don't need to load every pointsize
|
|
128 |
font = fdb.font(family, style, 12);
|
|
129 |
|
|
130 |
QFontMetrics fontmetrics(font);
|
|
131 |
QCOMPARE(fontmetrics.ascent() + fontmetrics.descent() + 1,
|
|
132 |
fontmetrics.height());
|
|
133 |
|
|
134 |
QCOMPARE(fontmetrics.height() + fontmetrics.leading(),
|
|
135 |
fontmetrics.lineSpacing());
|
|
136 |
} else {
|
|
137 |
QList<int> sizes = fdb.pointSizes(family, style);
|
|
138 |
QVERIFY(!sizes.isEmpty());
|
|
139 |
QList<int>::ConstIterator z_it, z_end = sizes.end();
|
|
140 |
for (z_it = sizes.begin(); z_it != z_end; ++z_it) {
|
|
141 |
const int size = *z_it;
|
|
142 |
|
|
143 |
// Initialize the font, and check if it is an exact match
|
|
144 |
font = fdb.font(family, style, size);
|
|
145 |
|
|
146 |
QFontMetrics fontmetrics(font);
|
|
147 |
QCOMPARE(fontmetrics.ascent() + fontmetrics.descent() + 1,
|
|
148 |
fontmetrics.height());
|
|
149 |
QCOMPARE(fontmetrics.height() + fontmetrics.leading(),
|
|
150 |
fontmetrics.lineSpacing());
|
|
151 |
}
|
|
152 |
}
|
|
153 |
}
|
|
154 |
}
|
|
155 |
}
|
|
156 |
|
|
157 |
void tst_QFontMetrics::boundingRect()
|
|
158 |
{
|
|
159 |
QFont f;
|
|
160 |
f.setPointSize(24);
|
|
161 |
QFontMetrics fm(f);
|
|
162 |
QRect r = fm.boundingRect(QChar('Y'));
|
|
163 |
QVERIFY(r.top() < 0);
|
|
164 |
r = fm.boundingRect(QString("Y"));
|
|
165 |
QVERIFY(r.top() < 0);
|
|
166 |
}
|
|
167 |
|
|
168 |
void tst_QFontMetrics::elidedText_data()
|
|
169 |
{
|
|
170 |
QTest::addColumn<QFont>("font");
|
|
171 |
QTest::addColumn<QString>("text");
|
|
172 |
|
|
173 |
QTest::newRow("helvetica hello") << QFont("helvetica",10) << QString("hello") ;
|
|
174 |
QTest::newRow("helvetica hello &Bye") << QFont("helvetica",10) << QString("hello&Bye") ;
|
|
175 |
}
|
|
176 |
|
|
177 |
|
|
178 |
void tst_QFontMetrics::elidedText()
|
|
179 |
{
|
|
180 |
QFETCH(QFont, font);
|
|
181 |
QFETCH(QString, text);
|
|
182 |
QFontMetrics fm(font);
|
|
183 |
int w = fm.width(text);
|
|
184 |
QString newtext = fm.elidedText(text,Qt::ElideRight,w+1, 0);
|
|
185 |
QCOMPARE(text,newtext); // should not elide
|
|
186 |
newtext = fm.elidedText(text,Qt::ElideRight,w-1, 0);
|
|
187 |
QVERIFY(text != newtext); // should elide
|
|
188 |
}
|
|
189 |
|
|
190 |
void tst_QFontMetrics::veryNarrowElidedText()
|
|
191 |
{
|
|
192 |
QFont f;
|
|
193 |
QFontMetrics fm(f);
|
|
194 |
QString text("hello");
|
|
195 |
QCOMPARE(fm.elidedText(text, Qt::ElideRight, 0), QString());
|
|
196 |
}
|
|
197 |
|
|
198 |
void tst_QFontMetrics::averageCharWidth()
|
|
199 |
{
|
|
200 |
QFont f;
|
|
201 |
QFontMetrics fm(f);
|
|
202 |
QVERIFY(fm.averageCharWidth() != 0);
|
|
203 |
QFontMetricsF fmf(f);
|
|
204 |
QVERIFY(fmf.averageCharWidth() != 0);
|
|
205 |
}
|
|
206 |
|
|
207 |
void tst_QFontMetrics::elidedMultiLength()
|
|
208 |
{
|
|
209 |
QString text1 = "Long Text 1\x9cShorter\x9csmall";
|
|
210 |
QString text1_long = "Long Text 1";
|
|
211 |
QString text1_short = "Shorter";
|
|
212 |
QString text1_small = "small";
|
|
213 |
QFontMetrics fm = QFontMetrics(QFont());
|
|
214 |
int width_long = fm.size(0, text1_long).width();
|
|
215 |
QCOMPARE(fm.elidedText(text1,Qt::ElideRight, 8000), text1_long);
|
|
216 |
QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_long + 1), text1_long);
|
|
217 |
QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_long - 1), text1_short);
|
|
218 |
int width_short = fm.size(0, text1_short).width();
|
|
219 |
QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_short + 1), text1_short);
|
|
220 |
QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_short - 1), text1_small);
|
|
221 |
|
|
222 |
// Not even wide enough for "small" - should use ellipsis
|
|
223 |
QChar ellipsisChar(0x2026);
|
|
224 |
QString text1_el = QString::fromLatin1("s") + ellipsisChar;
|
|
225 |
int width_small = fm.width(text1_el);
|
|
226 |
QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_small + 1), text1_el);
|
|
227 |
|
|
228 |
}
|
|
229 |
|
|
230 |
void tst_QFontMetrics::bearingIncludedInBoundingRect()
|
|
231 |
{
|
|
232 |
QFont font;
|
|
233 |
font.setItalic(true);
|
|
234 |
QRect brectItalic = QFontMetrics(font).boundingRect("ITALIC");
|
|
235 |
font.setItalic(false);
|
|
236 |
QRect brectNormal = QFontMetrics(font).boundingRect("ITALIC");
|
|
237 |
|
|
238 |
QVERIFY(brectItalic.width() > brectNormal.width());
|
|
239 |
}
|
|
240 |
|
|
241 |
QTEST_MAIN(tst_QFontMetrics)
|
|
242 |
#include "tst_qfontmetrics.moc"
|