1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qlibraryinfo.h"
43 #include <QtCore/qsettings.h>
45 #include "qfontengine_qpa_p.h"
46 #include "qplatformdefs.h"
48 #include <QtGui/private/qguiapplication_p.h>
49 #include <QtGui/qplatformfontdatabase_qpa.h>
51 #include <QtCore/qmath.h>
55 Q_GUI_EXPORT void qt_registerFont(const QString &familyName, const QString &foundryname, int weight,
56 QFont::Style style, int stretch, bool antialiased, bool scalable, int pixelSize,
57 const QSupportedWritingSystems &writingSystems, void *handle)
59 QFontDatabasePrivate *d = privateDb();
60 // qDebug() << "Adding font" << familyname << weight << italic << pixelSize << file << fileIndex << antialiased;
61 QtFontStyle::Key styleKey;
62 styleKey.style = style;
63 styleKey.weight = weight;
64 styleKey.stretch = stretch;
65 QtFontFamily *f = d->family(familyName, true);
67 for (int i = 0; i < QFontDatabase::WritingSystemsCount; ++i) {
68 if (writingSystems.supported(QFontDatabase::WritingSystem(i))) {
69 f->writingSystems[i] = QtFontFamily::Supported;
71 f->writingSystems[i] = QtFontFamily::Unsupported;
75 QtFontFoundry *foundry = f->foundry(foundryname, true);
76 QtFontStyle *fontStyle = foundry->style(styleKey, QString(), true);
77 fontStyle->smoothScalable = scalable;
78 fontStyle->antialiased = antialiased;
79 QtFontSize *size = fontStyle->pixelSize(pixelSize?pixelSize:SMOOTH_SCALABLE, true);
80 size->handle = handle;
83 static QStringList fallbackFamilies(const QString &family, const QFont::Style &style, const QFont::StyleHint &styleHint, const QUnicodeTables::Script &script)
85 QStringList retList = QGuiApplicationPrivate::platformIntegration()->fontDatabase()->fallbacksForFamily(family,style,styleHint,script);
86 QFontDatabasePrivate *db = privateDb();
88 QStringList::iterator i;
89 for (i = retList.begin(); i != retList.end(); ++i) {
90 bool contains = false;
91 for (int j = 0; j < db->count; j++) {
92 QtFontFamily *qtFamily = db->families[j];
93 if (!(i->compare(qtFamily->name,Qt::CaseInsensitive))) {
106 static void initializeDb()
108 static int initialized = false;
111 //init by asking for the platformfontdb for the first time :)
112 QGuiApplicationPrivate::platformIntegration()->fontDatabase()->populateFontDatabase();
117 static inline void load(const QString & = QString(), int = -1)
123 QFontEngine *loadSingleEngine(int script,
124 const QFontDef &request,
125 QtFontFoundry *foundry,
126 QtFontStyle *style, QtFontSize *size)
131 int pixelSize = size->pixelSize;
132 if (!pixelSize || (style->smoothScalable && pixelSize == SMOOTH_SCALABLE))
133 pixelSize = request.pixelSize;
135 QFontDef def = request;
136 def.pixelSize = pixelSize;
138 QFontCache::Key key(def,script);
139 QFontEngine *engine = QFontCache::instance()->findEngine(key);
141 QPlatformFontDatabase *pfdb = QGuiApplicationPrivate::platformIntegration()->fontDatabase();
142 engine = pfdb->fontEngine(def,QUnicodeTables::Script(script),size->handle);
144 QFontCache::Key key(def,script);
145 QFontCache::instance()->instance()->insertEngine(key,engine);
152 QFontEngine *loadEngine(int script, const QFontDef &request,
153 QtFontFamily *family, QtFontFoundry *foundry,
154 QtFontStyle *style, QtFontSize *size)
157 QFontEngine *engine = loadSingleEngine(script, request, foundry, style, size);
158 //make sure that the db has all fallback families
159 if (engine && engine->type() != QFontEngine::Multi
160 && !(request.styleStrategy & QFont::NoFontMerging) && !engine->symbol ) {
162 if (family && !family->askedForFallback) {
163 QFont::Style fontStyle = QFont::Style(style->key.style);
164 QFont::StyleHint styleHint = QFont::StyleHint(request.styleHint);
165 if (styleHint == QFont::AnyStyle && request.fixedPitch)
166 styleHint = QFont::TypeWriter;
167 family->fallbackFamilies = fallbackFamilies(family->name,fontStyle,styleHint,QUnicodeTables::Script(script));
169 family->askedForFallback = true;
172 QStringList fallbacks = privateDb()->fallbackFamilies;
173 if (family && !family->fallbackFamilies.isEmpty())
174 fallbacks = family->fallbackFamilies;
176 engine = new QFontEngineMultiQPA(engine, script, fallbacks);
182 static void registerFont(QFontDatabasePrivate::ApplicationFont *fnt)
184 QFontDatabasePrivate *db = privateDb();
186 fnt->families = QGuiApplicationPrivate::platformIntegration()->fontDatabase()->addApplicationFont(fnt->data,fnt->fileName);
188 db->reregisterAppFonts = true;
191 bool QFontDatabase::removeApplicationFont(int handle)
193 QMutexLocker locker(fontDatabaseMutex());
195 QFontDatabasePrivate *db = privateDb();
196 if (handle < 0 || handle >= db->applicationFonts.count())
199 db->applicationFonts[handle] = QFontDatabasePrivate::ApplicationFont();
201 db->reregisterAppFonts = true;
206 bool QFontDatabase::removeAllApplicationFonts()
208 QMutexLocker locker(fontDatabaseMutex());
210 QFontDatabasePrivate *db = privateDb();
211 if (db->applicationFonts.isEmpty())
214 db->applicationFonts.clear();
219 bool QFontDatabase::supportsThreadedFontRendering()
228 QFontDatabase::findFont(int script, const QFontPrivate *fp,
229 const QFontDef &request)
231 QMutexLocker locker(fontDatabaseMutex());
233 const int force_encoding_id = -1;
235 if (!privateDb()->count)
239 QFontCache::Key key(request, script);
240 engine = QFontCache::instance()->findEngine(key);
242 qDebug() << "Cache hit level 1";
246 QString family_name, foundry_name;
248 parseFontName(request.family, foundry_name, family_name);
250 if (qt_enable_test_font && request.family == QLatin1String("__Qt__Box__Engine__")) {
251 engine =new QTestFontEngine(request.pixelSize);
252 engine->fontDef = request;
256 match(script, request, family_name, foundry_name, force_encoding_id, &desc);
257 if (desc.family != 0 && desc.foundry != 0 && desc.style != 0) {
258 engine = loadEngine(script, request, desc.family, desc.foundry, desc.style, desc.size);
260 FM_DEBUG(" NO MATCH FOUND\n");
263 if (engine && engine->type() != QFontEngine::TestFontEngine) {
264 initFontDef(desc, request, &engine->fontDef);
267 QFontDef def = request;
268 if (def.family.isEmpty()) {
269 def.family = fp->request.family;
270 def.family = def.family.left(def.family.indexOf(QLatin1Char(',')));
276 if (!request.family.isEmpty()) {
277 QStringList fallbacks = fallbackFamilies(request.family,QFont::Style(request.style),QFont::StyleHint(request.styleHint),QUnicodeTables::Script(script));
278 for (int i = 0; !engine && i < fallbacks.size(); i++) {
279 QFontDef def = request;
280 def.family = fallbacks.at(i);
281 QFontCache::Key key(def,script);
282 engine = QFontCache::instance()->findEngine(key);
285 match(script, def, def.family, QLatin1String(""), 0, &desc);
286 if (desc.family == 0 && desc.foundry == 0 && desc.style == 0) {
289 engine = loadEngine(script, def, desc.family, desc.foundry, desc.style, desc.size);
291 initFontDef(desc, def, &engine->fontDef);
298 engine = new QFontEngineBox(request.pixelSize);
300 FM_DEBUG("returning box engine");
303 if (fp && fp->dpi > 0) {
304 engine->fontDef.pointSize = qreal(double((engine->fontDef.pixelSize * 72) / fp->dpi));
306 engine->fontDef.pointSize = request.pointSize;
312 void QFontDatabase::load(const QFontPrivate *d, int script)
314 QFontDef req = d->request;
316 if (req.pixelSize == -1) {
317 req.pixelSize = floor(((req.pointSize * d->dpi) / 72) * 100 + 0.5) / 100;
318 req.pixelSize = qRound(req.pixelSize);
320 if (req.pointSize < 0)
321 req.pointSize = req.pixelSize*72.0/d->dpi;
323 req.weight = QFont::Normal;
324 if (req.stretch == 0)
327 QFontCache::Key key(req, script);
330 getEngineData(d, key);
332 // the cached engineData could have already loaded the engine we want
333 if (d->engineData->engines[script])
336 QFontEngine *fe = QFontCache::instance()->findEngine(key);
338 // list of families to try
339 QStringList family_list;
341 if (!req.family.isEmpty()) {
342 family_list = familyList(req);
344 // add the default family
345 QString defaultFamily = QGuiApplication::font().family();
346 if (! family_list.contains(defaultFamily))
347 family_list << defaultFamily;
351 // null family means find the first font matching the specified script
352 family_list << QString();
354 QStringList::ConstIterator it = family_list.constBegin(), end = family_list.constEnd();
355 for (; !fe && it != end; ++it) {
358 fe = QFontDatabase::findFont(script, d, req);
359 if (fe && (fe->type()==QFontEngine::Box) && !req.family.isEmpty())
363 if (fe->symbol || (d->request.styleStrategy & QFont::NoFontMerging)) {
364 for (int i = 0; i < QUnicodeTables::ScriptCount; ++i) {
365 if (!d->engineData->engines[i]) {
366 d->engineData->engines[i] = fe;
371 d->engineData->engines[script] = fe;