Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qplatformfontdatabase.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <QtGui/private/qfontengine_p.h>
6#include <QtGui/private/qfontdatabase_p.h>
7#include <QtGui/QGuiApplication>
8#include <QtGui/QScreen>
9#include <qpa/qplatformscreen.h>
10#include <QtCore/QLibraryInfo>
11#include <QtCore/QDir>
12#include <QtCore/QMetaEnum>
13#include <QtCore/qendian.h>
14
15#include <algorithm>
16#include <iterator>
17
19
20using namespace Qt::StringLiterals;
21
22Q_LOGGING_CATEGORY(lcQpaFonts, "qt.qpa.fonts")
23
24void qt_registerFont(const QString &familyname, const QString &stylename,
25 const QString &foundryname, int weight,
26 QFont::Style style, int stretch, bool antialiased,
27 bool scalable, int pixelSize, bool fixedPitch,
28 const QSupportedWritingSystems &writingSystems, void *hanlde);
29
30void qt_registerFontFamily(const QString &familyName);
31void qt_registerAliasToFontFamily(const QString &familyName, const QString &alias);
32bool qt_isFontFamilyPopulated(const QString &familyName);
33
55void QPlatformFontDatabase::registerFont(const QString &familyname, const QString &stylename,
56 const QString &foundryname, QFont::Weight weight,
57 QFont::Style style, QFont::Stretch stretch, bool antialiased,
58 bool scalable, int pixelSize, bool fixedPitch,
59 const QSupportedWritingSystems &writingSystems, void *usrPtr)
60{
61 if (scalable)
62 pixelSize = 0;
63
64 qt_registerFont(familyname, stylename, foundryname, weight, style,
65 stretch, antialiased, scalable, pixelSize,
66 fixedPitch, writingSystems, usrPtr);
67}
68
77{
78 qt_registerFontFamily(familyName);
79}
80
82{
83public:
85 : ref(1)
86 , list(QFontDatabase::WritingSystemsCount, false)
87 {
88 }
89
95
97 QList<bool> list;
98};
99
107
116
121{
122 if (d != other.d) {
123 other.d->ref.ref();
124 if (!d->ref.deref())
125 delete d;
126 d = other.d;
127 }
128 return *this;
129}
130
132{
133 return !(lhs != rhs);
134}
135
137{
138 if (lhs.d == rhs.d)
139 return false;
140
141 Q_ASSERT(lhs.d->list.size() == rhs.d->list.size());
143 for (int i = 0; i < QFontDatabase::WritingSystemsCount; ++i) {
144 if (lhs.d->list.at(i) != rhs.d->list.at(i))
145 return true;
146 }
147
148 return false;
149}
150
151#ifndef QT_NO_DEBUG_STREAM
153{
154 const QMetaObject *mo = &QFontDatabase::staticMetaObject;
155 QMetaEnum me = mo->enumerator(mo->indexOfEnumerator("WritingSystem"));
156
157 QDebugStateSaver saver(debug);
158 debug.nospace() << "QSupportedWritingSystems(";
159 int i = sws.d->list.indexOf(true);
160 while (i > 0) {
161 debug << me.valueToKey(i);
162 i = sws.d->list.indexOf(true, i + 1);
163 if (i > 0)
164 debug << ", ";
165 }
166 debug << ")";
167 return debug;
168}
169#endif
170
175{
176 if (!d->ref.deref())
177 delete d;
178}
179
183void QSupportedWritingSystems::detach()
184{
185 if (d->ref.loadRelaxed() != 1) {
187 if (!d->ref.deref())
188 delete d;
189 d = newd;
190 }
191}
192
198{
199 detach();
200 d->list[writingSystem] = support;
201}
202
208{
209 return d->list.at(writingSystem);
210}
211
230
246
255{
256 Q_UNUSED(familyName);
257}
258
268
275{
276 return new QFontEngineMulti(fontEngine, script);
277}
278
295{
296 Q_UNUSED(fontDef);
298 qWarning("This plugin does not support loading system fonts.");
299 return nullptr;
300}
301
310 QFont::HintingPreference hintingPreference)
311{
313 Q_UNUSED(pixelSize);
314 Q_UNUSED(hintingPreference);
315 qWarning("This plugin does not support font engines created directly from font data");
316 return nullptr;
317}
318
335{
338 Q_UNUSED(applicationFont);
339
340 if (applicationFont != nullptr)
341 applicationFont->properties.clear();
342
343 qWarning("This plugin does not support application fonts");
344
345 return QStringList();
346}
347
352{
353 QByteArray *fileDataPtr = static_cast<QByteArray *>(handle);
354 delete fileDataPtr;
355}
356
361{
362 QString fontpath = QString::fromLocal8Bit(qgetenv("QT_QPA_FONTDIR"));
363 if (fontpath.isEmpty())
364 fontpath = QLibraryInfo::path(QLibraryInfo::LibrariesPath) + "/fonts"_L1;
365
366 return fontpath;
367}
368
374{
375 Q_UNUSED(family);
376 return false;
377}
378
387{
388 return QFont("Helvetica"_L1);
389}
390
391
393
403
412{
413 return false;
414}
415
423{
424 QList<int> ret;
425 static const quint8 standard[] =
426 { 6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };
427 static const int num_standards = int(sizeof standard / sizeof *standard);
428 ret.reserve(num_standards);
429 std::copy(standard, standard + num_standards, std::back_inserter(ret));
430 return ret;
431}
432
433// see the Unicode subset bitfields in the MSDN docs
435 { 127, 127 }, // Any
436 { 0, 127 }, // Latin
437 { 7, 127 }, // Greek
438 { 9, 127 }, // Cyrillic
439 { 10, 127 }, // Armenian
440 { 11, 127 }, // Hebrew
441 { 13, 127 }, // Arabic
442 { 71, 127 }, // Syriac
443 { 72, 127 }, // Thaana
444 { 15, 127 }, // Devanagari
445 { 16, 127 }, // Bengali
446 { 17, 127 }, // Gurmukhi
447 { 18, 127 }, // Gujarati
448 { 19, 127 }, // Oriya
449 { 20, 127 }, // Tamil
450 { 21, 127 }, // Telugu
451 { 22, 127 }, // Kannada
452 { 23, 127 }, // Malayalam
453 { 73, 127 }, // Sinhala
454 { 24, 127 }, // Thai
455 { 25, 127 }, // Lao
456 { 70, 127 }, // Tibetan
457 { 74, 127 }, // Myanmar
458 { 26, 127 }, // Georgian
459 { 80, 127 }, // Khmer
460 { 126, 127 }, // SimplifiedChinese
461 { 126, 127 }, // TraditionalChinese
462 { 126, 127 }, // Japanese
463 { 56, 127 }, // Korean
464 { 0, 127 }, // Vietnamese (same as latin1)
465 { 126, 127 }, // Other
466 { 78, 127 }, // Ogham
467 { 79, 127 }, // Runic
468 { 14, 127 }, // Nko
469};
470
489
497{
498 if (length >= 86) {
499 quint32 unicodeRange[4] = {
500 qFromBigEndian<quint32>(os2Table + 42),
501 qFromBigEndian<quint32>(os2Table + 46),
502 qFromBigEndian<quint32>(os2Table + 50),
503 qFromBigEndian<quint32>(os2Table + 54)
504 };
505 quint32 codePageRange[2] = {
506 qFromBigEndian<quint32>(os2Table + 78),
507 qFromBigEndian<quint32>(os2Table + 82)
508 };
509
510 return writingSystemsFromTrueTypeBits(unicodeRange, codePageRange);
511 }
512
514}
515
523{
524 QSupportedWritingSystems writingSystems;
525
526 bool hasScript = false;
527 for (int i = 0; i < QFontDatabase::WritingSystemsCount; ++i) {
528 int bit = requiredUnicodeBits[i][0];
529 int index = bit/32;
530 int flag = 1 << (bit&31);
531 if (bit != 126 && (unicodeRange[index] & flag)) {
532 bit = requiredUnicodeBits[i][1];
533 index = bit/32;
534
535 flag = 1 << (bit&31);
536 if (bit == 127 || (unicodeRange[index] & flag)) {
538 hasScript = true;
539 // qDebug("font %s: index=%d, flag=%8x supports script %d", familyName.latin1(), index, flag, i);
540 }
541 }
542 }
543 if (codePageRange[0] & ((1 << Latin1CsbBit) | (1 << CentralEuropeCsbBit) | (1 << TurkishCsbBit) | (1 << BalticCsbBit))) {
544 writingSystems.setSupported(QFontDatabase::Latin);
545 hasScript = true;
546 //qDebug("font %s supports Latin", familyName.latin1());
547 }
548 if (codePageRange[0] & (1 << CyrillicCsbBit)) {
550 hasScript = true;
551 //qDebug("font %s supports Cyrillic", familyName.latin1());
552 }
553 if (codePageRange[0] & (1 << GreekCsbBit)) {
554 writingSystems.setSupported(QFontDatabase::Greek);
555 hasScript = true;
556 //qDebug("font %s supports Greek", familyName.latin1());
557 }
558 if (codePageRange[0] & (1 << HebrewCsbBit)) {
559 writingSystems.setSupported(QFontDatabase::Hebrew);
560 hasScript = true;
561 //qDebug("font %s supports Hebrew", familyName.latin1());
562 }
563 if (codePageRange[0] & (1 << ArabicCsbBit)) {
564 writingSystems.setSupported(QFontDatabase::Arabic);
565 hasScript = true;
566 //qDebug("font %s supports Arabic", familyName.latin1());
567 }
568 if (codePageRange[0] & (1 << ThaiCsbBit)) {
569 writingSystems.setSupported(QFontDatabase::Thai);
570 hasScript = true;
571 //qDebug("font %s supports Thai", familyName.latin1());
572 }
573 if (codePageRange[0] & (1 << VietnameseCsbBit)) {
575 hasScript = true;
576 //qDebug("font %s supports Vietnamese", familyName.latin1());
577 }
578 if (codePageRange[0] & (1 << SimplifiedChineseCsbBit)) {
580 hasScript = true;
581 //qDebug("font %s supports Simplified Chinese", familyName.latin1());
582 }
583 if (codePageRange[0] & (1 << TraditionalChineseCsbBit)) {
585 hasScript = true;
586 //qDebug("font %s supports Traditional Chinese", familyName.latin1());
587 }
588 if (codePageRange[0] & (1 << JapaneseCsbBit)) {
590 hasScript = true;
591 //qDebug("font %s supports Japanese", familyName.latin1());
592 }
593 if (codePageRange[0] & ((1 << KoreanCsbBit) | (1 << KoreanJohabCsbBit))) {
594 writingSystems.setSupported(QFontDatabase::Korean);
595 hasScript = true;
596 //qDebug("font %s supports Korean", familyName.latin1());
597 }
598 if (codePageRange[0] & (1U << SymbolCsbBit)) {
599 writingSystems = QSupportedWritingSystems();
600 hasScript = false;
601 }
602
603 if (!hasScript)
604 writingSystems.setSupported(QFontDatabase::Symbol);
605
606 return writingSystems;
607}
608
616{
617 qt_registerAliasToFontFamily(familyName, alias);
618}
619
637
644{
645 return qt_isFontFamilyPopulated(familyName);
646}
647
655{
656 return false;
657}
658
659
\inmodule QtCore
Definition qatomic.h:112
bool ref() noexcept
bool deref() noexcept
T loadRelaxed() const noexcept
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
\inmodule QtCore
static QFontDatabasePrivate * instance()
\threadsafe \inmodule QtGui
WritingSystem
\value Any \value Latin \value Greek \value Cyrillic \value Armenian \value Hebrew \value Arabic \val...
\reentrant
Definition qfont.h:22
HintingPreference
Definition qfont.h:55
static QString path(LibraryPath p)
qsizetype size() const noexcept
Definition qlist.h:397
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
\inmodule QtCore
const char * valueToKey(int value) const
Returns the string that is used as the name of the given enumeration value, or \nullptr if value is n...
The QPlatformFontDatabase class makes it possible to customize how fonts are discovered and how they ...
virtual QStringList addApplicationFont(const QByteArray &fontData, const QString &fileName, QFontDatabasePrivate::ApplicationFont *font=nullptr)
Adds an application font described by the font contained supplied fontData or using the font containe...
virtual QFont defaultFont() const
Returns the default system font.
virtual QFontEngine * fontEngine(const QFontDef &fontDef, void *handle)
Returns the font engine that can be used to render the font described by the font definition,...
static void repopulateFontDatabase()
Requests that the platform font database should be repopulated.
virtual QString fontDir() const
Returns the directory containing the fonts used by the database.
static QSupportedWritingSystems writingSystemsFromTrueTypeBits(quint32 unicodeRange[4], quint32 codePageRange[2])
Helper function that determines the writing systems support by a given unicodeRange and codePageRange...
virtual bool fontsAlwaysScalable() const
Return true if all fonts are considered scalable when using this font database.
static QSupportedWritingSystems writingSystemsFromOS2Table(const char *os2Table, size_t length)
Helper function that determines the writing system support based on the contents of the OS/2 table in...
virtual void releaseHandle(void *handle)
Releases the specified font handle.
static bool isFamilyPopulated(const QString &familyName)
Helper function that returns true if the font family has already been registered and populated.
virtual void populateFamily(const QString &familyName)
This function is called whenever a lazily populated family, populated through registerFontFamily(),...
virtual QFontEngineMulti * fontEngineMulti(QFontEngine *fontEngine, QChar::Script script)
Returns a multi font engine in the specified script to encapsulate fontEngine with the option to fall...
virtual bool supportsVariableApplicationFonts() const
Returns true if this font database supports loading named instances from variable application fonts.
virtual bool isPrivateFontFamily(const QString &family) const
Returns true if the font family is private.
virtual QString resolveFontFamilyAlias(const QString &family) const
Resolve alias to actual font family names.
virtual void invalidate()
This function is called whenever the font database is invalidated.
static void registerAliasToFontFamily(const QString &familyName, const QString &alias)
Helper function that register the alias for the familyName.
virtual QList< int > standardSizes() const
Return list of standard font sizes when using this font database.
static void registerFontFamily(const QString &familyName)
Registers a font family with the font database.
virtual void populateFontDatabase()
This function is called once at startup by Qt's internal font database.
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromLocal8Bit(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5949
The QSupportedWritingSystems class is used when registering fonts with the internal Qt fontdatabase.
QSupportedWritingSystems & operator=(const QSupportedWritingSystems &other)
Constructs a copy of the other writing systems object.
QSupportedWritingSystems()
Constructs a new object to handle supported writing systems.
~QSupportedWritingSystems()
Destroys the supported writing systems object.
bool supported(QFontDatabase::WritingSystem) const
Returns true if the writing system specified by writingSystem is supported; otherwise returns false.
void setSupported(QFontDatabase::WritingSystem, bool supported=true)
Sets or clears support for the specified writingSystem based on the value given by support.
QWritingSystemsPrivate(const QWritingSystemsPrivate *other)
auto mo
[7]
Combined button and popup list for selecting options.
QList< QString > QStringList
Constructs a string list that contains the given string, str.
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
bool qt_isFontFamilyPopulated(const QString &familyName)
void qt_registerFontFamily(const QString &familyName)
void qt_registerAliasToFontFamily(const QString &familyName, const QString &alias)
QString qt_resolveFontFamilyAlias(const QString &alias)
#define qWarning
Definition qlogging.h:166
#define Q_LOGGING_CATEGORY(name,...)
return ret
GLuint64 GLenum void * handle
GLuint index
[2]
GLenum GLuint GLenum GLsizei length
GLuint GLuint GLfloat weight
GLint ref
GLenum GLsizeiptr const void * fontData
void qt_registerFont(const QString &familyname, const QString &stylename, const QString &foundryname, int weight, QFont::Style style, int stretch, bool antialiased, bool scalable, int pixelSize, bool fixedPitch, const QSupportedWritingSystems &writingSystems, void *hanlde)
bool operator!=(const QSupportedWritingSystems &lhs, const QSupportedWritingSystems &rhs)
bool qt_isFontFamilyPopulated(const QString &familyName)
QDebug operator<<(QDebug debug, const QSupportedWritingSystems &sws)
bool operator==(const QSupportedWritingSystems &lhs, const QSupportedWritingSystems &rhs)
static const quint8 requiredUnicodeBits[QFontDatabase::WritingSystemsCount][2]
void qt_registerFontFamily(const QString &familyName)
void qt_registerAliasToFontFamily(const QString &familyName, const QString &alias)
@ TraditionalChineseCsbBit
@ SimplifiedChineseCsbBit
@ CentralEuropeCsbBit
QString qt_resolveFontFamilyAlias(const QString &alias)
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
#define Q_UNUSED(x)
unsigned int quint32
Definition qtypes.h:50
double qreal
Definition qtypes.h:187
unsigned char quint8
Definition qtypes.h:46
QSharedPointer< T > other(t)
[5]
\inmodule QtCore