author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
child 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
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(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
75 |
void elidedMultiLengthF(); |
0 | 76 |
void bearingIncludedInBoundingRect(); |
77 |
}; |
|
78 |
||
79 |
tst_QFontMetrics::tst_QFontMetrics() |
|
80 |
||
81 |
{ |
|
82 |
} |
|
83 |
||
84 |
tst_QFontMetrics::~tst_QFontMetrics() |
|
85 |
{ |
|
86 |
||
87 |
} |
|
88 |
||
89 |
void tst_QFontMetrics::init() |
|
90 |
{ |
|
91 |
} |
|
92 |
||
93 |
void tst_QFontMetrics::cleanup() |
|
94 |
{ |
|
95 |
} |
|
96 |
||
97 |
void tst_QFontMetrics::same() |
|
98 |
{ |
|
99 |
QFont font; |
|
100 |
font.setBold(true); |
|
101 |
QFontMetrics fm(font); |
|
102 |
const QString text = QLatin1String("Some stupid STRING"); |
|
103 |
QCOMPARE(fm.size(0, text), fm.size(0, text)) ; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
104 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
105 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
106 |
QImage image; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
107 |
QFontMetrics fm2(font, &image); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
108 |
QString text2 = QLatin1String("Foo Foo"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
109 |
QCOMPARE(fm2.size(0, text2), fm2.size(0, text2)); //used to crash |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
110 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
111 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
112 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
113 |
QImage image; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
114 |
QFontMetricsF fm3(font, &image); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
115 |
QString text2 = QLatin1String("Foo Foo"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
116 |
QCOMPARE(fm3.size(0, text2), fm3.size(0, text2)); //used to crash |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
117 |
} |
0 | 118 |
} |
119 |
||
120 |
||
121 |
void tst_QFontMetrics::metrics() |
|
122 |
{ |
|
123 |
QFont font; |
|
124 |
QFontDatabase fdb; |
|
125 |
||
126 |
// Query the QFontDatabase for a specific font, store the |
|
127 |
// result in family, style and size. |
|
128 |
QStringList families = fdb.families(); |
|
129 |
if (families.isEmpty()) |
|
130 |
return; |
|
131 |
||
132 |
QStringList::ConstIterator f_it, f_end = families.end(); |
|
133 |
for (f_it = families.begin(); f_it != f_end; ++f_it) { |
|
134 |
const QString &family = *f_it; |
|
135 |
||
136 |
QStringList styles = fdb.styles(family); |
|
137 |
QStringList::ConstIterator s_it, s_end = styles.end(); |
|
138 |
for (s_it = styles.begin(); s_it != s_end; ++s_it) { |
|
139 |
const QString &style = *s_it; |
|
140 |
||
141 |
if (fdb.isSmoothlyScalable(family, style)) { |
|
142 |
// smoothly scalable font... don't need to load every pointsize |
|
143 |
font = fdb.font(family, style, 12); |
|
144 |
||
145 |
QFontMetrics fontmetrics(font); |
|
146 |
QCOMPARE(fontmetrics.ascent() + fontmetrics.descent() + 1, |
|
147 |
fontmetrics.height()); |
|
148 |
||
149 |
QCOMPARE(fontmetrics.height() + fontmetrics.leading(), |
|
150 |
fontmetrics.lineSpacing()); |
|
151 |
} else { |
|
152 |
QList<int> sizes = fdb.pointSizes(family, style); |
|
153 |
QVERIFY(!sizes.isEmpty()); |
|
154 |
QList<int>::ConstIterator z_it, z_end = sizes.end(); |
|
155 |
for (z_it = sizes.begin(); z_it != z_end; ++z_it) { |
|
156 |
const int size = *z_it; |
|
157 |
||
158 |
// Initialize the font, and check if it is an exact match |
|
159 |
font = fdb.font(family, style, size); |
|
160 |
||
161 |
QFontMetrics fontmetrics(font); |
|
162 |
QCOMPARE(fontmetrics.ascent() + fontmetrics.descent() + 1, |
|
163 |
fontmetrics.height()); |
|
164 |
QCOMPARE(fontmetrics.height() + fontmetrics.leading(), |
|
165 |
fontmetrics.lineSpacing()); |
|
166 |
} |
|
167 |
} |
|
168 |
} |
|
169 |
} |
|
170 |
} |
|
171 |
||
172 |
void tst_QFontMetrics::boundingRect() |
|
173 |
{ |
|
174 |
QFont f; |
|
175 |
f.setPointSize(24); |
|
176 |
QFontMetrics fm(f); |
|
177 |
QRect r = fm.boundingRect(QChar('Y')); |
|
178 |
QVERIFY(r.top() < 0); |
|
179 |
r = fm.boundingRect(QString("Y")); |
|
180 |
QVERIFY(r.top() < 0); |
|
181 |
} |
|
182 |
||
183 |
void tst_QFontMetrics::elidedText_data() |
|
184 |
{ |
|
185 |
QTest::addColumn<QFont>("font"); |
|
186 |
QTest::addColumn<QString>("text"); |
|
187 |
||
188 |
QTest::newRow("helvetica hello") << QFont("helvetica",10) << QString("hello") ; |
|
189 |
QTest::newRow("helvetica hello &Bye") << QFont("helvetica",10) << QString("hello&Bye") ; |
|
190 |
} |
|
191 |
||
192 |
||
193 |
void tst_QFontMetrics::elidedText() |
|
194 |
{ |
|
195 |
QFETCH(QFont, font); |
|
196 |
QFETCH(QString, text); |
|
197 |
QFontMetrics fm(font); |
|
198 |
int w = fm.width(text); |
|
199 |
QString newtext = fm.elidedText(text,Qt::ElideRight,w+1, 0); |
|
200 |
QCOMPARE(text,newtext); // should not elide |
|
201 |
newtext = fm.elidedText(text,Qt::ElideRight,w-1, 0); |
|
202 |
QVERIFY(text != newtext); // should elide |
|
203 |
} |
|
204 |
||
205 |
void tst_QFontMetrics::veryNarrowElidedText() |
|
206 |
{ |
|
207 |
QFont f; |
|
208 |
QFontMetrics fm(f); |
|
209 |
QString text("hello"); |
|
210 |
QCOMPARE(fm.elidedText(text, Qt::ElideRight, 0), QString()); |
|
211 |
} |
|
212 |
||
213 |
void tst_QFontMetrics::averageCharWidth() |
|
214 |
{ |
|
215 |
QFont f; |
|
216 |
QFontMetrics fm(f); |
|
217 |
QVERIFY(fm.averageCharWidth() != 0); |
|
218 |
QFontMetricsF fmf(f); |
|
219 |
QVERIFY(fmf.averageCharWidth() != 0); |
|
220 |
} |
|
221 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
222 |
template<class FontMetrics> void elidedMultiLength_helper() |
0 | 223 |
{ |
224 |
QString text1 = "Long Text 1\x9cShorter\x9csmall"; |
|
225 |
QString text1_long = "Long Text 1"; |
|
226 |
QString text1_short = "Shorter"; |
|
227 |
QString text1_small = "small"; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
228 |
FontMetrics fm = FontMetrics(QFont()); |
0 | 229 |
int width_long = fm.size(0, text1_long).width(); |
230 |
QCOMPARE(fm.elidedText(text1,Qt::ElideRight, 8000), text1_long); |
|
231 |
QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_long + 1), text1_long); |
|
232 |
QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_long - 1), text1_short); |
|
233 |
int width_short = fm.size(0, text1_short).width(); |
|
234 |
QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_short + 1), text1_short); |
|
235 |
QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_short - 1), text1_small); |
|
236 |
||
237 |
// Not even wide enough for "small" - should use ellipsis |
|
238 |
QChar ellipsisChar(0x2026); |
|
239 |
QString text1_el = QString::fromLatin1("s") + ellipsisChar; |
|
240 |
int width_small = fm.width(text1_el); |
|
241 |
QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_small + 1), text1_el); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
242 |
} |
0 | 243 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
244 |
void tst_QFontMetrics::elidedMultiLength() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
245 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
246 |
elidedMultiLength_helper<QFontMetrics>(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
247 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
248 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
249 |
void tst_QFontMetrics::elidedMultiLengthF() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
250 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
251 |
elidedMultiLength_helper<QFontMetricsF>(); |
0 | 252 |
} |
253 |
||
254 |
void tst_QFontMetrics::bearingIncludedInBoundingRect() |
|
255 |
{ |
|
256 |
QFont font; |
|
257 |
font.setItalic(true); |
|
258 |
QRect brectItalic = QFontMetrics(font).boundingRect("ITALIC"); |
|
259 |
font.setItalic(false); |
|
260 |
QRect brectNormal = QFontMetrics(font).boundingRect("ITALIC"); |
|
261 |
||
262 |
QVERIFY(brectItalic.width() > brectNormal.width()); |
|
263 |
} |
|
264 |
||
265 |
QTEST_MAIN(tst_QFontMetrics) |
|
266 |
#include "tst_qfontmetrics.moc" |