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 "qplatformfontdatabase_qpa.h"
43 #include <QtGui/private/qfontengine_p.h>
44 #include <QtGui/private/qfontengine_qpa_p.h>
45 #include <QtCore/QLibraryInfo>
46 #include <QtCore/QDir>
50 extern void qt_registerFont(const QString &familyname, const QString &foundryname, int weight,
51 QFont::Style style, int stretch, bool antialiased,bool scalable, int pixelSize,
52 const QSupportedWritingSystems &writingSystems, void *hanlde);
55 \fn void QPlatformFontDatabase::registerQPF2Font(const QByteArray &dataArray, void *handle)
57 Registers the pre-rendered QPF2 font contained in the given \a dataArray.
61 void QPlatformFontDatabase::registerQPF2Font(const QByteArray &dataArray, void *handle)
63 if (dataArray.size() == 0)
66 const uchar *data = reinterpret_cast<const uchar *>(dataArray.constData());
67 if (QFontEngineQPA::verifyHeader(data, dataArray.size())) {
68 QString fontName = QFontEngineQPA::extractHeaderField(data, QFontEngineQPA::Tag_FontName).toString();
69 int pixelSize = QFontEngineQPA::extractHeaderField(data, QFontEngineQPA::Tag_PixelSize).toInt();
70 QVariant weight = QFontEngineQPA::extractHeaderField(data, QFontEngineQPA::Tag_Weight);
71 QVariant style = QFontEngineQPA::extractHeaderField(data, QFontEngineQPA::Tag_Style);
72 QByteArray writingSystemBits = QFontEngineQPA::extractHeaderField(data, QFontEngineQPA::Tag_WritingSystems).toByteArray();
74 if (!fontName.isEmpty() && pixelSize) {
75 QFont::Weight fontWeight = QFont::Normal;
76 if (weight.type() == QVariant::Int || weight.type() == QVariant::UInt)
77 fontWeight = QFont::Weight(weight.toInt());
79 QFont::Style fontStyle = static_cast<QFont::Style>(style.toInt());
81 QSupportedWritingSystems writingSystems;
82 for (int i = 0; i < writingSystemBits.count(); ++i) {
83 uchar currentByte = writingSystemBits.at(i);
84 for (int j = 0; j < 8; ++j) {
86 writingSystems.setSupported(QFontDatabase::WritingSystem(i * 8 + j));
90 QFont::Stretch stretch = QFont::Unstretched;
91 registerFont(fontName,QString(),fontWeight,fontStyle,stretch,true,false,pixelSize,writingSystems,handle);
94 qDebug() << "header verification of QPF2 font failed. maybe it is corrupt?";
99 \fn void QPlatformFontDatabase::registerFont(const QString &familyName,
100 const QString &foundryName, QFont::Weight weight, QFont::Style style,
101 QFont::Stretch stretch, bool antialiased, bool scalable, int pixelSize,
102 const QSupportedWritingSystems &writingSystems, void *usrPtr)
104 Registers a font with the given set of attributes describing the font's
105 foundry, family name, style and stretch information, pixel size, and
106 supported writing systems. Additional information about whether the font
107 can be scaled and antialiased can also be provided.
109 The foundry name and font family are described by \a foundryName and
110 \a familyName. The font weight (light, normal, bold, etc.), style (normal,
111 oblique, italic) and stretch information (condensed, expanded, unstretched,
112 etc.) are specified by \a weight, \a style and \a stretch.
114 Some fonts can be antialiased and scaled; \a scalable and \a antialiased
115 can be set to true for fonts with these attributes. The intended pixel
116 size of non-scalable fonts is specified by \a pixelSize; this value will be
117 ignored for scalable fonts.
119 The writing systems supported by the font are specified by the
120 \a writingSystems argument.
122 \sa registerQPF2Font()
124 void QPlatformFontDatabase::registerFont(const QString &familyname, const QString &foundryname, QFont::Weight weight,
125 QFont::Style style, QFont::Stretch stretch, bool antialiased, bool scalable, int pixelSize,
126 const QSupportedWritingSystems &writingSystems, void *usrPtr)
130 qt_registerFont(familyname,foundryname,weight,style,stretch,antialiased,scalable,pixelSize,writingSystems,usrPtr);
133 class QWritingSystemsPrivate
136 QWritingSystemsPrivate()
138 , vector(QFontDatabase::WritingSystemsCount,false)
142 QWritingSystemsPrivate(const QWritingSystemsPrivate *other)
144 , vector(other->vector)
149 QVector<bool> vector;
153 Constructs a new object to handle supported writing systems.
155 QSupportedWritingSystems::QSupportedWritingSystems()
157 d = new QWritingSystemsPrivate;
161 Constructs a copy of the \a other writing systems object.
163 QSupportedWritingSystems::QSupportedWritingSystems(const QSupportedWritingSystems &other)
170 Constructs a copy of the \a other writing systems object.
172 QSupportedWritingSystems &QSupportedWritingSystems::operator=(const QSupportedWritingSystems &other)
184 Destroys the supported writing systems object.
186 QSupportedWritingSystems::~QSupportedWritingSystems()
195 void QSupportedWritingSystems::detach()
198 QWritingSystemsPrivate *newd = new QWritingSystemsPrivate(d);
206 Sets or clears support for the specified \a writingSystem based on the
207 value given by \a support.
209 void QSupportedWritingSystems::setSupported(QFontDatabase::WritingSystem writingSystem, bool support)
212 d->vector[writingSystem] = support;
216 Returns true if the writing system specified by \a writingSystem is
217 supported; otherwise returns false.
219 bool QSupportedWritingSystems::supported(QFontDatabase::WritingSystem writingSystem) const
221 return d->vector.at(writingSystem);
225 \class QSupportedWritingSystems
226 \brief The QSupportedWritingSystems class is used when registering fonts with the internal Qt
230 Its to provide an easy to use interface for indicating what writing systems a specific font
236 This function is called once at startup by Qt's internal font database.
237 Reimplement this function in a subclass for a convenient place to initialize
238 the internal font database.
240 The default implementation looks in the fontDir() location and registers all
243 void QPlatformFontDatabase::populateFontDatabase()
245 QString fontpath = fontDir();
247 if(!QFile::exists(fontpath)) {
248 qFatal("QFontDatabase: Cannot find font directory %s - is Qt installed correctly?",
249 qPrintable(fontpath));
253 dir.setNameFilters(QStringList() << QLatin1String("*.qpf2"));
255 for (int i = 0; i < int(dir.count()); ++i) {
256 const QByteArray fileName = QFile::encodeName(dir.absoluteFilePath(dir[i]));
257 QFile file(QString::fromLocal8Bit(fileName));
258 if (file.open(QFile::ReadOnly)) {
259 const QByteArray fileData = file.readAll();
260 QByteArray *fileDataPtr = new QByteArray(fileData);
261 registerQPF2Font(fileData, fileDataPtr);
267 Returns the font engine that can be used to render the font described by
268 the font definition, \a fontDef, in the specified \a script.
270 QFontEngine *QPlatformFontDatabase::fontEngine(const QFontDef &fontDef, QUnicodeTables::Script script, void *handle)
274 QByteArray *fileDataPtr = static_cast<QByteArray *>(handle);
275 QFontEngineQPA *engine = new QFontEngineQPA(fontDef,*fileDataPtr);
276 //qDebug() << fontDef.pixelSize << fontDef.weight << fontDef.style << fontDef.stretch << fontDef.styleHint << fontDef.styleStrategy << fontDef.family << script;
280 QFontEngine *QPlatformFontDatabase::fontEngine(const QByteArray &fontData, qreal pixelSize,
281 QFont::HintingPreference hintingPreference)
285 Q_UNUSED(hintingPreference);
286 qWarning("This plugin does not support font engines created directly from font data");
291 Returns a list of alternative fonts for the specified \a family and
292 \a style and \a script using the \a styleHint given.
294 QStringList QPlatformFontDatabase::fallbacksForFamily(const QString family, const QFont::Style &style, const QFont::StyleHint &styleHint, const QUnicodeTables::Script &script) const
300 return QStringList();
304 Adds an application font described by the font contained supplied \a fontData
305 or using the font contained in the file referenced by \a fileName. Returns
306 a list of family names, or an empty list if the font could not be added.
308 \note The default implementation of this function does not add an application
309 font. Subclasses should reimplement this function to perform the necessary
310 loading and registration of fonts.
312 QStringList QPlatformFontDatabase::addApplicationFont(const QByteArray &fontData, const QString &fileName)
317 qWarning("This plugin does not support application fonts");
318 return QStringList();
322 Releases the specified font \a handle.
324 void QPlatformFontDatabase::releaseHandle(void *handle)
326 QByteArray *fileDataPtr = static_cast<QByteArray *>(handle);
331 Returns the directory containing the fonts used by the database.
333 QString QPlatformFontDatabase::fontDir() const
335 QString fontpath = QString::fromLocal8Bit(qgetenv("QT_QPA_FONTDIR"));
336 if (fontpath.isEmpty()) {
337 #ifndef QT_NO_SETTINGS
338 fontpath = QLibraryInfo::location(QLibraryInfo::LibrariesPath);
339 fontpath += QLatin1String("/fonts");
347 \class QPlatformFontDatabase
348 \brief The QPlatformFontDatabase class makes it possible to customize how fonts
349 are discovered and how they are rendered
353 QPlatformFontDatabase is the superclass which is intended to let platform implementations use
354 native font handling.
356 Qt has its internal font database which it uses to discover available fonts on the
357 user's system. To be able to populate this database subclass this class, and
358 reimplement populateFontDatabase().
360 Use the function registerFont() to populate the internal font database.
362 Sometimes a specified font does not have the required glyphs; in such a case, the
363 fallbackForFamily() function is called automatically to find alternative font
364 families that can supply alternatives to the missing glyphs.
366 \sa QSupportedWritingSystems