1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qplatformfontdatabase.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,
52 bool scalable, int pixelSize, bool fixedPitch,
53 const QSupportedWritingSystems &writingSystems, void *hanlde);
56 \fn void QPlatformFontDatabase::registerQPF2Font(const QByteArray &dataArray, void *handle)
58 Registers the pre-rendered QPF2 font contained in the given \a dataArray.
62 void QPlatformFontDatabase::registerQPF2Font(const QByteArray &dataArray, void *handle)
64 if (dataArray.size() == 0)
67 const uchar *data = reinterpret_cast<const uchar *>(dataArray.constData());
68 if (QFontEngineQPA::verifyHeader(data, dataArray.size())) {
69 QString fontName = QFontEngineQPA::extractHeaderField(data, QFontEngineQPA::Tag_FontName).toString();
70 int pixelSize = QFontEngineQPA::extractHeaderField(data, QFontEngineQPA::Tag_PixelSize).toInt();
71 QVariant weight = QFontEngineQPA::extractHeaderField(data, QFontEngineQPA::Tag_Weight);
72 QVariant style = QFontEngineQPA::extractHeaderField(data, QFontEngineQPA::Tag_Style);
73 QByteArray writingSystemBits = QFontEngineQPA::extractHeaderField(data, QFontEngineQPA::Tag_WritingSystems).toByteArray();
75 if (!fontName.isEmpty() && pixelSize) {
76 QFont::Weight fontWeight = QFont::Normal;
77 if (weight.type() == QVariant::Int || weight.type() == QVariant::UInt)
78 fontWeight = QFont::Weight(weight.toInt());
80 QFont::Style fontStyle = static_cast<QFont::Style>(style.toInt());
82 QSupportedWritingSystems writingSystems;
83 for (int i = 0; i < writingSystemBits.count(); ++i) {
84 uchar currentByte = writingSystemBits.at(i);
85 for (int j = 0; j < 8; ++j) {
87 writingSystems.setSupported(QFontDatabase::WritingSystem(i * 8 + j));
91 QFont::Stretch stretch = QFont::Unstretched;
92 registerFont(fontName,QString(),fontWeight,fontStyle,stretch,true,false,pixelSize,false,writingSystems,handle);
95 qDebug() << "header verification of QPF2 font failed. maybe it is corrupt?";
100 Registers a font with the given set of attributes describing the font's
101 foundry, family name, style and stretch information, pixel size, and
102 supported writing systems. Additional information about whether the font
103 can be scaled and antialiased can also be provided.
105 The foundry name and font family are described by \a foundryName and
106 \a familyName. The font weight (light, normal, bold, etc.), style (normal,
107 oblique, italic) and stretch information (condensed, expanded, unstretched,
108 etc.) are specified by \a weight, \a style and \a stretch.
110 Some fonts can be antialiased and scaled; \a scalable and \a antialiased
111 can be set to true for fonts with these attributes. The intended pixel
112 size of non-scalable fonts is specified by \a pixelSize; this value will be
113 ignored for scalable fonts.
115 The writing systems supported by the font are specified by the
116 \a writingSystems argument.
118 \sa registerQPF2Font()
120 void QPlatformFontDatabase::registerFont(const QString &familyname, const QString &foundryname, QFont::Weight weight,
121 QFont::Style style, QFont::Stretch stretch, bool antialiased,
122 bool scalable, int pixelSize, bool fixedPitch,
123 const QSupportedWritingSystems &writingSystems, void *usrPtr)
128 qt_registerFont(familyname, foundryname, weight, style,
129 stretch, antialiased, scalable, pixelSize,
130 fixedPitch, 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()
197 if (d->ref.load() != 1) {
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
238 QPlatformFontDatabase::~QPlatformFontDatabase()
243 This function is called once at startup by Qt's internal font database.
244 Reimplement this function in a subclass for a convenient place to initialize
245 the internal font database.
247 The default implementation looks in the fontDir() location and registers all
250 void QPlatformFontDatabase::populateFontDatabase()
252 QString fontpath = fontDir();
253 if(!QFile::exists(fontpath)) {
254 qWarning("QFontDatabase: Cannot find font directory '%s' - is Qt installed correctly?",
255 qPrintable(QDir::toNativeSeparators(fontpath)));
260 dir.setNameFilters(QStringList() << QLatin1String("*.qpf2"));
262 for (int i = 0; i < int(dir.count()); ++i) {
263 const QByteArray fileName = QFile::encodeName(dir.absoluteFilePath(dir[i]));
264 QFile file(QString::fromLocal8Bit(fileName));
265 if (file.open(QFile::ReadOnly)) {
266 const QByteArray fileData = file.readAll();
267 QByteArray *fileDataPtr = new QByteArray(fileData);
268 registerQPF2Font(fileData, fileDataPtr);
274 Returns a multi font engine in the specified \a script to encapsulate \a fontEngine with the
275 option to fall back to to the fonts given by \a fallbacks if \a fontEngine does not support
278 QFontEngineMulti *QPlatformFontDatabase::fontEngineMulti(QFontEngine *fontEngine,
279 QUnicodeTables::Script script)
281 return new QFontEngineMultiQPA(fontEngine, script);
285 Returns the font engine that can be used to render the font described by
286 the font definition, \a fontDef, in the specified \a script.
288 QFontEngine *QPlatformFontDatabase::fontEngine(const QFontDef &fontDef, QUnicodeTables::Script script, void *handle)
292 QByteArray *fileDataPtr = static_cast<QByteArray *>(handle);
293 QFontEngineQPA *engine = new QFontEngineQPA(fontDef,*fileDataPtr);
294 //qDebug() << fontDef.pixelSize << fontDef.weight << fontDef.style << fontDef.stretch << fontDef.styleHint << fontDef.styleStrategy << fontDef.family << script;
298 QFontEngine *QPlatformFontDatabase::fontEngine(const QByteArray &fontData, qreal pixelSize,
299 QFont::HintingPreference hintingPreference)
303 Q_UNUSED(hintingPreference);
304 qWarning("This plugin does not support font engines created directly from font data");
309 Returns a list of alternative fonts for the specified \a family and
310 \a style and \a script using the \a styleHint given.
312 QStringList QPlatformFontDatabase::fallbacksForFamily(const QString family, const QFont::Style &style, const QFont::StyleHint &styleHint, const QUnicodeTables::Script &script) const
318 return QStringList();
322 Adds an application font described by the font contained supplied \a fontData
323 or using the font contained in the file referenced by \a fileName. Returns
324 a list of family names, or an empty list if the font could not be added.
326 \note The default implementation of this function does not add an application
327 font. Subclasses should reimplement this function to perform the necessary
328 loading and registration of fonts.
330 QStringList QPlatformFontDatabase::addApplicationFont(const QByteArray &fontData, const QString &fileName)
335 qWarning("This plugin does not support application fonts");
336 return QStringList();
340 Releases the specified font \a handle.
342 void QPlatformFontDatabase::releaseHandle(void *handle)
344 QByteArray *fileDataPtr = static_cast<QByteArray *>(handle);
349 Returns the directory containing the fonts used by the database.
351 QString QPlatformFontDatabase::fontDir() const
353 QString fontpath = QString::fromLocal8Bit(qgetenv("QT_QPA_FONTDIR"));
354 if (fontpath.isEmpty()) {
355 #ifndef QT_NO_SETTINGS
356 fontpath = QLibraryInfo::location(QLibraryInfo::LibrariesPath);
357 fontpath += QLatin1String("/fonts");
365 Returns the default system font.
367 \sa QGuiApplication::font()
371 QFont QPlatformFontDatabase::defaultFont() const
373 return QFont(QLatin1String("Helvetica"));
377 Resolve alias to actual font family names.
382 QString QPlatformFontDatabase::resolveFontFamilyAlias(const QString &family) const
388 Return true if all fonts are considered scalable when using this font database.
394 bool QPlatformFontDatabase::fontsAlwaysScalable() const
400 Return list of standard font sizes when using this font database.
405 QList<int> QPlatformFontDatabase::standardSizes() const
408 static const unsigned short standard[] =
409 { 6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72, 0 };
410 ret.reserve(int(sizeof(standard) / sizeof(standard[0])));
411 const unsigned short *sizes = standard;
412 while (*sizes) ret << *sizes++;
417 \class QPlatformFontDatabase
424 \brief The QPlatformFontDatabase class makes it possible to customize how fonts
425 are discovered and how they are rendered
427 QPlatformFontDatabase is the superclass which is intended to let platform implementations use
428 native font handling.
430 Qt has its internal font database which it uses to discover available fonts on the
431 user's system. To be able to populate this database subclass this class, and
432 reimplement populateFontDatabase().
434 Use the function registerFont() to populate the internal font database.
436 Sometimes a specified font does not have the required glyphs; in such a case, the
437 fallbackForFamily() function is called automatically to find alternative font
438 families that can supply alternatives to the missing glyphs.
440 \sa QSupportedWritingSystems