Qt 4.8
Public Functions | Static Public Functions | List of all members
QPlatformFontDatabase Class Reference

The QPlatformFontDatabase class makes it possible to customize how fonts are discovered and how they are rendered. More...

#include <qplatformfontdatabase_qpa.h>

Inheritance diagram for QPlatformFontDatabase:
QBasicUnixFontDatabase QCoreTextFontDatabase QFontconfigDatabase

Public Functions

virtual QStringList addApplicationFont (const QByteArray &fontData, const QString &fileName)
 Adds an application font described by the font contained supplied fontData or using the font contained in the file referenced by fileName. More...
 
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 given. More...
 
virtual QString fontDir () const
 Returns the path to the font directory. More...
 
virtual QFontEnginefontEngine (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, fontDef, in the specified script. More...
 
virtual QFontEnginefontEngine (const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference)
 
virtual void populateFontDatabase ()
 This function is called once at startup by Qts internal fontdatabase. More...
 
virtual void releaseHandle (void *handle)
 Releases the font handle and deletes any associated data loaded from a file. More...
 

Static Public Functions

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's foundry, family name, style and stretch information, pixel size, and supported writing systems. More...
 
static void registerQPF2Font (const QByteArray &dataArray, void *handle)
 Registers the pre-rendered QPF2 font contained in the given dataArray. More...
 

Detailed Description

The QPlatformFontDatabase class makes it possible to customize how fonts are discovered and how they are rendered.

Since
4.8

QPlatformFontDatabase is the superclass which is intended to let platform implementations use native font handling.

Qt has its internal font database which it uses to discover available fonts on the user's system. To be able to populate this database subclass this class, and reimplement populateFontDatabase().

Use the function registerFont() to populate the internal font database.

Sometimes a specified font does not have the required glyphs; in such a case, the fallbackForFamily() function is called automatically to find alternative font families that can supply alternatives to the missing glyphs.

See also
QSupportedWritingSystems

Definition at line 86 of file qplatformfontdatabase_qpa.h.

Functions

◆ addApplicationFont()

QStringList QPlatformFontDatabase::addApplicationFont ( const QByteArray fontData,
const QString fileName 
)
virtual

Adds an application font described by the font contained supplied fontData or using the font contained in the file referenced by fileName.

Returns a list of family names, or an empty list if the font could not be added.

Note
The default implementation of this function does not add an application font. Subclasses should reimplement this function to perform the necessary loading and registration of fonts.

Reimplemented in QBasicUnixFontDatabase.

Definition at line 311 of file qplatformfontdatabase_qpa.cpp.

312 {
313  Q_UNUSED(fontData);
314  Q_UNUSED(fileName);
315 
316  qWarning("This plugin does not support application fonts");
317  return QStringList();
318 }
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
Q_CORE_EXPORT void qWarning(const char *,...)
#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

◆ fallbacksForFamily()

QStringList QPlatformFontDatabase::fallbacksForFamily ( const QString  family,
const QFont::Style style,
const QFont::StyleHint styleHint,
const QUnicodeTables::Script script 
) const
virtual

Returns a list of alternative fonts for the specified family and style and script using the styleHint given.

Reimplemented in QBasicUnixFontDatabase, QCoreTextFontDatabase, and QFontconfigDatabase.

Definition at line 293 of file qplatformfontdatabase_qpa.cpp.

294 {
295  Q_UNUSED(family);
296  Q_UNUSED(style);
298  Q_UNUSED(script);
299  return QStringList();
300 }
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
const char * styleHint(const QFontDef &request)
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
#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

◆ fontDir()

QString QPlatformFontDatabase::fontDir ( ) const
virtual

Returns the path to the font directory.

The font directory is stored in the general Qt settings unless it has been overridden by the QT_QPA_FONTDIR environment variable.

When using builds of Qt that do not support settings, the QT_QPA_FONTDIR environment variable is the only way to specify the font directory.

Definition at line 338 of file qplatformfontdatabase_qpa.cpp.

Referenced by QBasicUnixFontDatabase::populateFontDatabase(), and populateFontDatabase().

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 }
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
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
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static QString location(LibraryLocation)
Returns the location specified by loc.

◆ fontEngine() [1/2]

QFontEngine * QPlatformFontDatabase::fontEngine ( const QFontDef fontDef,
QUnicodeTables::Script  script,
void *  handle 
)
virtual

Returns the font engine that can be used to render the font described by the font definition, fontDef, in the specified script.

Reimplemented in QBasicUnixFontDatabase, QCoreTextFontDatabase, and QFontconfigDatabase.

Definition at line 269 of file qplatformfontdatabase_qpa.cpp.

Referenced by loadSingleEngine().

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 }
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
#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

◆ fontEngine() [2/2]

QFontEngine * QPlatformFontDatabase::fontEngine ( const QByteArray fontData,
qreal  pixelSize,
QFont::HintingPreference  hintingPreference 
)
virtual

Definition at line 279 of file qplatformfontdatabase_qpa.cpp.

281 {
282  Q_UNUSED(fontData);
284  Q_UNUSED(hintingPreference);
285  qWarning("This plugin does not support font engines created directly from font data");
286  return 0;
287 }
static float pixelSize(const QFontDef &request, int dpi)
Definition: qfont_win.cpp:80
Q_CORE_EXPORT void qWarning(const char *,...)
#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

◆ populateFontDatabase()

void QPlatformFontDatabase::populateFontDatabase ( )
virtual

This function is called once at startup by Qts internal fontdatabase.

Reimplement this function in a subclass for a convenient place to initialise the internal fontdatabase.

The default implementation looks in the fontDir() location and registers all qpf2 fonts.

Reimplemented in QBasicUnixFontDatabase, QCoreTextFontDatabase, and QFontconfigDatabase.

Definition at line 242 of file qplatformfontdatabase_qpa.cpp.

Referenced by QBasicUnixFontDatabase::populateFontDatabase().

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) {
255  const QByteArray fileName = QFile::encodeName(dir.absoluteFilePath(dir[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 }
The QDir class provides access to directory structures and their contents.
Definition: qdir.h:58
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
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool exists() const
Returns true if the file specified by fileName() exists; otherwise returns false. ...
Definition: qfile.cpp:626
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
virtual QString fontDir() const
Returns the path to the font directory.
static void registerQPF2Font(const QByteArray &dataArray, void *handle)
Registers the pre-rendered QPF2 font contained in the given dataArray.
Q_CORE_EXPORT void qFatal(const char *,...)
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
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
#define qPrintable(string)
Definition: qglobal.h:1750
static QString fileName(const QString &fileUrl)

◆ registerFont()

void QPlatformFontDatabase::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 *  usrPtr 
)
static

Registers a font with the given set of attributes describing the font's foundry, family name, style and stretch information, pixel size, and supported writing systems.

Additional information about whether the font can be scaled and antialiased can also be provided.

The foundry name and font family are described by foundryName and familyName. The font weight (light, normal, bold, etc.), style (normal, oblique, italic) and stretch information (condensed, expanded, unstretched, etc.) are specified by weight, style and stretch.

Some fonts can be antialiased and scaled; scalable and antialiased can be set to true for fonts with these attributes. The intended pixel size of non-scalable fonts is specified by pixelSize; this value will be ignored for scalable fonts.

The writing systems supported by the font are specified by the writingSystems argument.

See also
registerQPF2Font()

Definition at line 124 of file qplatformfontdatabase_qpa.cpp.

Referenced by QBasicUnixFontDatabase::addTTFile(), QFontconfigDatabase::populateFontDatabase(), QCoreTextFontDatabase::populateFontDatabase(), and registerQPF2Font().

127 {
128  if (scalable)
129  pixelSize = 0;
130  qt_registerFont(familyname,foundryname,weight,style,stretch,antialiased,scalable,pixelSize,writingSystems,usrPtr);
131 }
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)
static float pixelSize(const QFontDef &request, int dpi)
Definition: qfont_win.cpp:80

◆ registerQPF2Font()

void QPlatformFontDatabase::registerQPF2Font ( const QByteArray dataArray,
void *  handle 
)
static

Registers the pre-rendered QPF2 font contained in the given dataArray.

See also
registerFont()

Definition at line 61 of file qplatformfontdatabase_qpa.cpp.

Referenced by populateFontDatabase().

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 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
void setSupported(QFontDatabase::WritingSystem, bool supported=true)
Sets the supported state of the writing system given by writingSystem to the value specified by suppo...
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
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
The QString class provides a Unicode character string.
Definition: qstring.h:83
Stretch
Predefined stretch values that follow the CSS naming convention.
Definition: qfont.h:117
static QVariant extractHeaderField(const uchar *data, HeaderTag tag)
QByteArray toByteArray() const
Returns the variant as a QByteArray if the variant has type() ByteArray or String (converted using QS...
Definition: qvariant.cpp:2383
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
Q_CORE_EXPORT void qDebug(const char *,...)
unsigned char uchar
Definition: qglobal.h:994
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 QSupportedWritingSystems class is used when registering fonts with the internal Qt fontdatabase...
static const char * data(const QByteArray &arr)
Style
This enum describes the different styles of glyphs that are used to display text. ...
Definition: qfont.h:111
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
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
int size() const
Returns the number of bytes in this byte array.
Definition: qbytearray.h:402
char at(int i) const
Returns the character at index position i in the byte array.
Definition: qbytearray.h:413

◆ releaseHandle()

void QPlatformFontDatabase::releaseHandle ( void *  handle)
virtual

Releases the font handle and deletes any associated data loaded from a file.

Reimplemented in QBasicUnixFontDatabase.

Definition at line 323 of file qplatformfontdatabase_qpa.cpp.

Referenced by QtFontStyle::~QtFontStyle().

324 {
325  QByteArray *fileDataPtr = static_cast<QByteArray *>(handle);
326  delete fileDataPtr;
327 }
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135

The documentation for this class was generated from the following files: