Qt 4.8
qplatformfontdatabase_qpa.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
43 #include <QtGui/private/qfontengine_p.h>
44 #include <QtGui/private/qfontengine_qpa_p.h>
45 #include <QtCore/QLibraryInfo>
46 #include <QtCore/QDir>
47 
49 
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);
53 
61 void QPlatformFontDatabase::registerQPF2Font(const QByteArray &dataArray, void *handle)
62 {
63  if (dataArray.size() == 0)
64  return;
65 
66  const uchar *data = reinterpret_cast<const uchar *>(dataArray.constData());
67  if (QFontEngineQPA::verifyHeader(data, dataArray.size())) {
73 
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());
78 
79  QFont::Style fontStyle = static_cast<QFont::Style>(style.toInt());
80 
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) {
85  if (currentByte & 1)
86  writingSystems.setSupported(QFontDatabase::WritingSystem(i * 8 + j));
87  currentByte >>= 1;
88  }
89  }
91  registerFont(fontName,QString(),fontWeight,fontStyle,stretch,true,false,pixelSize,writingSystems,handle);
92  }
93  } else {
94  qDebug() << "header verification of QPF2 font failed. maybe it is corrupt?";
95  }
96 }
97 
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)
127 {
128  if (scalable)
129  pixelSize = 0;
130  qt_registerFont(familyname,foundryname,weight,style,stretch,antialiased,scalable,pixelSize,writingSystems,usrPtr);
131 }
132 
134 {
135 public:
137  : ref(1)
138  , vector(QFontDatabase::WritingSystemsCount,false)
139  {
140  }
141 
143  : ref(1)
144  , vector(other->vector)
145  {
146  }
147 
150 };
151 
153 {
155 }
156 
158 {
159  d = other.d;
160  d->ref.ref();
161 }
162 
167 {
168  if (d != other.d) {
169  other.d->ref.ref();
170  if (!d->ref.deref())
171  delete d;
172  d = other.d;
173  }
174  return *this;
175 }
176 
181 {
182  if (!d->ref.deref())
183  delete d;
184 }
185 
187 {
188  if (d->ref != 1) {
190  if (!d->ref.deref())
191  delete d;
192  d = newd;
193  }
194 }
195 
205 {
206  detach();
207  d->vector[writingSystem] = support;
208 }
209 
217 {
218  return d->vector.at(writingSystem);
219 }
220 
243 {
244  QString fontpath = fontDir();
245 
246  if(!QFile::exists(fontpath)) {
247  qFatal("QFontDatabase: Cannot find font directory %s - is Qt installed correctly?",
248  qPrintable(fontpath));
249  }
250 
251  QDir dir(fontpath);
252  dir.setNameFilters(QStringList() << QLatin1String("*.qpf2"));
253  dir.refresh();
254  for (int i = 0; i < int(dir.count()); ++i) {
256  QFile file(QString::fromLocal8Bit(fileName));
257  if (file.open(QFile::ReadOnly)) {
258  const QByteArray fileData = file.readAll();
259  QByteArray *fileDataPtr = new QByteArray(fileData);
260  registerQPF2Font(fileData, fileDataPtr);
261  }
262  }
263 }
264 
270 {
271  Q_UNUSED(script);
272  Q_UNUSED(handle);
273  QByteArray *fileDataPtr = static_cast<QByteArray *>(handle);
274  QFontEngineQPA *engine = new QFontEngineQPA(fontDef,*fileDataPtr);
275  //qDebug() << fontDef.pixelSize << fontDef.weight << fontDef.style << fontDef.stretch << fontDef.styleHint << fontDef.styleStrategy << fontDef.family << script;
276  return engine;
277 }
278 
280  QFont::HintingPreference hintingPreference)
281 {
282  Q_UNUSED(fontData);
283  Q_UNUSED(pixelSize);
284  Q_UNUSED(hintingPreference);
285  qWarning("This plugin does not support font engines created directly from font data");
286  return 0;
287 }
288 
294 {
295  Q_UNUSED(family);
296  Q_UNUSED(style);
297  Q_UNUSED(styleHint);
298  Q_UNUSED(script);
299  return QStringList();
300 }
301 
312 {
313  Q_UNUSED(fontData);
314  Q_UNUSED(fileName);
315 
316  qWarning("This plugin does not support application fonts");
317  return QStringList();
318 }
319 
324 {
325  QByteArray *fileDataPtr = static_cast<QByteArray *>(handle);
326  delete fileDataPtr;
327 }
328 
339 {
340  QString fontpath = QString::fromLocal8Bit(qgetenv("QT_QPA_FONTDIR"));
341  if (fontpath.isEmpty()) {
342 #ifndef QT_NO_SETTINGS
344  fontpath += QLatin1String("/fonts");
345 #endif
346  }
347 
348  return fontpath;
349 }
350 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
double d
Definition: qnumeric_p.h:62
void setSupported(QFontDatabase::WritingSystem, bool supported=true)
Sets the supported state of the writing system given by writingSystem to the value specified by suppo...
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
HintingPreference
This enum describes the different levels of hinting that can be applied to glyphs to improve legibili...
Definition: qfont.h:96
static QString fromLocal8Bit(const char *, int size=-1)
Returns a QString initialized with the first size characters of the 8-bit string str.
Definition: qstring.cpp:4245
double qreal
Definition: qglobal.h:1193
QSupportedWritingSystems & operator=(const QSupportedWritingSystems &other)
Assigns the other supported writing systems object to this object.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QString absoluteFilePath(const QString &fileName) const
Returns the absolute path name of a file in the directory.
Definition: qdir.cpp:701
void qt_registerFont(const QString &familyname, const QString &foundryname, int weight, QFont::Style style, int stretch, bool antialiased, bool scalable, int pixelSize, const QSupportedWritingSystems &writingSystems, void *hanlde)
The QAtomicInt class provides platform-independent atomic operations on integers. ...
Definition: qatomic.h:55
QWritingSystemsPrivate(const QWritingSystemsPrivate *other)
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
Weight
Qt uses a weighting scale from 0 to 99 similar to, but not the same as, the scales used in Windows or...
Definition: qfont.h:103
bool ref()
Atomically increments the value of this QAtomicInt.
The QString class provides a Unicode character string.
Definition: qstring.h:83
uint count() const
Returns the total number of directories and files in the directory.
Definition: qdir.cpp:1247
virtual QStringList addApplicationFont(const QByteArray &fontData, const QString &fileName)
Adds an application font described by the font contained supplied fontData or using the font containe...
Stretch
Predefined stretch values that follow the CSS naming convention.
Definition: qfont.h:117
static QVariant extractHeaderField(const uchar *data, HeaderTag tag)
bool supported(QFontDatabase::WritingSystem) const
Returns true if the writing system given by writingSystem is supported; otherwise returns false...
QByteArray toByteArray() const
Returns the variant as a QByteArray if the variant has type() ByteArray or String (converted using QS...
Definition: qvariant.cpp:2383
void setNameFilters(const QStringList &nameFilters)
Sets the name filters used by entryList() and entryInfoList() to the list of filters specified by nam...
Definition: qdir.cpp:966
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
bool exists() const
Returns true if the file specified by fileName() exists; otherwise returns false. ...
Definition: qfile.cpp:626
void refresh() const
Refreshes the directory information.
Definition: qdir.cpp:2218
Q_CORE_EXPORT void qDebug(const char *,...)
unsigned char uchar
Definition: qglobal.h:994
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QFontDatabase class provides information about the fonts available in the underlying window syste...
Definition: qfontdatabase.h:66
static float pixelSize(const QFontDef &request, int dpi)
Definition: qfont_win.cpp:80
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
The QSupportedWritingSystems class is used when registering fonts with the internal Qt fontdatabase...
virtual QString fontDir() const
Returns the path to the font directory.
const char * styleHint(const QFontDef &request)
Q_CORE_EXPORT void qWarning(const char *,...)
virtual QFontEngine * fontEngine(const QFontDef &fontDef, QUnicodeTables::Script script, void *handle)
Returns the font engine that can be used to render the font described by the font definition...
static const char * data(const QByteArray &arr)
static void registerQPF2Font(const QByteArray &dataArray, void *handle)
Registers the pre-rendered QPF2 font contained in the given dataArray.
Style
This enum describes the different styles of glyphs that are used to display text. ...
Definition: qfont.h:111
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
virtual void releaseHandle(void *handle)
Releases the font handle and deletes any associated data loaded from a file.
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
Q_CORE_EXPORT void qFatal(const char *,...)
StyleHint
Style hints are used by the font matching algorithm to find an appropriate default family if a select...
Definition: qfont.h:69
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
int count(char c) const
Returns the number of occurrences of character ch in the byte array.
static bool verifyHeader(const uchar *data, int size)
static void registerFont(const QString &familyname, const QString &foundryname, QFont::Weight weight, QFont::Style style, QFont::Stretch stretch, bool antialiased, bool scalable, int pixelSize, const QSupportedWritingSystems &writingSystems, void *handle)
Registers a font with the given set of attributes describing the font&#39;s foundry, family name...
Type type() const
Returns the storage type of the value stored in the variant.
Definition: qvariant.cpp:1901
static QString location(LibraryLocation)
Returns the location specified by loc.
QWritingSystemsPrivate * d
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
virtual void populateFontDatabase()
This function is called once at startup by Qts internal fontdatabase.
static QByteArray encodeName(const QString &fileName)
By default, this function converts fileName to the local 8-bit encoding determined by the user&#39;s loca...
Definition: qfile.cpp:528
char at(int i) const
Returns the character at index position i in the byte array.
Definition: qbytearray.h:413
virtual QStringList fallbacksForFamily(const QString family, const QFont::Style &style, const QFont::StyleHint &styleHint, const QUnicodeTables::Script &script) const
Returns a list of alternative fonts for the specified family and style and script using the styleHint...
#define qPrintable(string)
Definition: qglobal.h:1750
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
static QString fileName(const QString &fileUrl)
~QSupportedWritingSystems()
Destroys the object.