1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtTest/QtTest>
31
32
33#include <qfont.h>
34#include <private/qfont_p.h>
35#include <private/qfontengine_p.h>
36
37class tst_QFontCache : public QObject
38{
39Q_OBJECT
40
41public:
42 tst_QFontCache();
43 virtual ~tst_QFontCache();
44
45private slots:
46 void engineData_data();
47 void engineData();
48 void engineDataFamilies_data();
49 void engineDataFamilies();
50
51 void clear();
52};
53
54#ifdef QT_BUILD_INTERNAL
55QT_BEGIN_NAMESPACE
56// qfontdatabase.cpp
57Q_AUTOTEST_EXPORT void qt_setQtEnableTestFont(bool value);
58// qfontengine.cpp
59Q_AUTOTEST_EXPORT void QFontEngine_startCollectingEngines();
60Q_AUTOTEST_EXPORT QList<QFontEngine *> QFontEngine_stopCollectingEngines();
61QT_END_NAMESPACE
62#endif
63
64tst_QFontCache::tst_QFontCache()
65{
66}
67
68tst_QFontCache::~tst_QFontCache()
69{
70}
71
72void tst_QFontCache::engineData_data()
73{
74 QTest::addColumn<QString>(name: "family");
75 QTest::addColumn<QString>(name: "cacheKey");
76
77 QTest::newRow(dataTag: "unquoted-family-name") << QString("Times New Roman") << QString("Times New Roman");
78 QTest::newRow(dataTag: "quoted-family-name") << QString("'Times New Roman'") << QString("Times New Roman");
79 QTest::newRow(dataTag: "invalid") << QString("invalid") << QString("invalid");
80 QTest::newRow(dataTag: "multiple") << QString("invalid, Times New Roman") << QString("invalid,Times New Roman");
81 QTest::newRow(dataTag: "multiple spaces") << QString("invalid, Times New Roman ") << QString("invalid,Times New Roman");
82 QTest::newRow(dataTag: "multiple spaces quotes") << QString("'invalid', Times New Roman ") << QString("invalid,Times New Roman");
83 QTest::newRow(dataTag: "multiple2") << QString("invalid, Times New Roman , foobar, 'baz'") << QString("invalid,Times New Roman,foobar,baz");
84 QTest::newRow(dataTag: "invalid spaces") << QString("invalid spaces, Times New Roman ") << QString("invalid spaces,Times New Roman");
85 QTest::newRow(dataTag: "invalid spaces quotes") << QString("'invalid spaces', 'Times New Roman' ") << QString("invalid spaces,Times New Roman");
86}
87
88void tst_QFontCache::engineData()
89{
90 QFETCH(QString, family);
91 QFETCH(QString, cacheKey);
92
93 QFont f(family);
94 f.exactMatch(); // loads engine
95
96 QFontPrivate *d = QFontPrivate::get(font: f);
97
98 QFontDef req = d->request;
99 // copy-pasted from QFontDatabase::load(), to engineer the cache key
100 if (req.pixelSize == -1) {
101 req.pixelSize = std::floor(x: ((req.pointSize * d->dpi) / 72) * 100 + 0.5) / 100;
102 req.pixelSize = qRound(d: req.pixelSize);
103 }
104 if (req.pointSize < 0)
105 req.pointSize = req.pixelSize*72.0/d->dpi;
106
107 req.family = cacheKey;
108
109 QFontEngineData *engineData = QFontCache::instance()->findEngineData(def: req);
110
111 QCOMPARE(engineData, QFontPrivate::get(f)->engineData);
112}
113
114void tst_QFontCache::engineDataFamilies_data()
115{
116 QTest::addColumn<QStringList>(name: "families");
117
118 const QStringList multiple = { QLatin1String("invalid"), QLatin1String("Times New Roman") };
119 const QStringList multipleQuotes = { QLatin1String("'invalid'"), QLatin1String("Times New Roman") };
120 const QStringList multiple2 = { QLatin1String("invalid"), QLatin1String("Times New Roman"),
121 QLatin1String("foobar"), QLatin1String("'baz'") };
122
123 QTest::newRow(dataTag: "unquoted-family-name") << QStringList(QLatin1String("Times New Roman"));
124 QTest::newRow(dataTag: "quoted-family-name") << QStringList(QLatin1String("Times New Roman"));
125 QTest::newRow(dataTag: "invalid") << QStringList(QLatin1String("invalid"));
126 QTest::newRow(dataTag: "multiple") << multiple;
127 QTest::newRow(dataTag: "multiple spaces quotes") << multipleQuotes;
128 QTest::newRow(dataTag: "multiple2") << multiple2;
129}
130
131void tst_QFontCache::engineDataFamilies()
132{
133 QFETCH(QStringList, families);
134
135 QFont f;
136 f.setFamily(QString()); // Unset the family as taken from the QGuiApplication default
137 f.setFamilies(families);
138 f.exactMatch(); // loads engine
139 QFontPrivate *d = QFontPrivate::get(font: f);
140
141 QFontDef req = d->request;
142 // copy-pasted from QFontDatabase::load(), to engineer the cache key
143 if (req.pixelSize == -1) {
144 req.pixelSize = std::floor(x: ((req.pointSize * d->dpi) / 72) * 100 + 0.5) / 100;
145 req.pixelSize = qRound(d: req.pixelSize);
146 }
147 if (req.pointSize < 0)
148 req.pointSize = req.pixelSize*72.0/d->dpi;
149
150 req.families = families;
151
152 QFontEngineData *engineData = QFontCache::instance()->findEngineData(def: req);
153
154 QCOMPARE(engineData, QFontPrivate::get(f)->engineData);
155}
156
157void tst_QFontCache::clear()
158{
159#ifdef QT_BUILD_INTERNAL
160 QFontEngine_startCollectingEngines();
161#else
162 // must not crash, at very least ;)
163#endif
164
165 QFontEngine *fontEngine = 0;
166
167#ifdef QT_BUILD_INTERNAL
168 {
169 // we're never caching the box (and the "test") font engines
170 // let's ensure we're not leaking them as well as the cached ones
171 qt_setQtEnableTestFont(value: true);
172
173 QFont f;
174 f.setFamily("__Qt__Box__Engine__");
175 f.exactMatch(); // loads engine
176 }
177#endif
178 {
179 QFontDatabase db;
180
181 QFont f;
182 f.setStyleHint(QFont::Serif);
183 const QString familyForHint(f.defaultFamily());
184
185 // it should at least return a family that is available
186 QVERIFY(db.hasFamily(familyForHint));
187 f.exactMatch(); // loads engine
188
189 fontEngine = QFontPrivate::get(font: f)->engineForScript(script: QChar::Script_Common);
190 QVERIFY(fontEngine);
191 QVERIFY(QFontCache::instance()->engineCacheCount.value(fontEngine) > 0); // ensure it is cached
192
193 // acquire the engine to use it somewhere else:
194 // (e.g. like the we do in QFontSubset() or like QRawFont does in fromFont())
195 fontEngine->ref.ref();
196
197 // cache the engine once again; there is a special case when the engine is cached more than once
198 QFontCache::instance()->insertEngine(key: QFontCache::Key(QFontDef(), 0, 1), engine: fontEngine);
199 }
200
201 // use it:
202 // e.g. fontEngine->stringToCMap(..);
203
204 // and whilst it is alive, don't hesitate to add/remove the app-local fonts:
205 // (QFontDatabase::{add,remove}ApplicationFont() clears the cache)
206 QFontCache::instance()->clear();
207
208 // release the acquired engine:
209 if (fontEngine) {
210 if (!fontEngine->ref.deref())
211 delete fontEngine;
212 fontEngine = 0;
213 }
214
215 // we may even exit the application now:
216 QFontCache::instance()->cleanup();
217
218#ifdef QT_BUILD_INTERNAL
219 QList<QFontEngine *> leakedEngines = QFontEngine_stopCollectingEngines();
220for (int i = 0; i < leakedEngines.size(); ++i) qWarning() << i << leakedEngines.at(i) << leakedEngines.at(i)->ref.loadRelaxed();
221 // and we are not leaking!
222 QCOMPARE(leakedEngines.size(), 0);
223#endif
224}
225
226QTEST_MAIN(tst_QFontCache)
227#include "tst_qfontcache.moc"
228

source code of qtbase/tests/auto/gui/text/qfontcache/tst_qfontcache.cpp