Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
18QT_BEGIN_NAMESPACE
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, bool colorFont,
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
34/*!
35 Registers a font with the given set of attributes describing the font's
36 foundry, family name, style and stretch information, pixel size, and
37 supported writing systems. Additional information about whether the font
38 can be scaled and antialiased can also be provided.
39
40 The foundry name and font family are described by \a foundryName and
41 \a familyName. The font weight (light, normal, bold, etc.), style (normal,
42 oblique, italic) and stretch information (condensed, expanded, unstretched,
43 etc.) are specified by \a weight, \a style and \a stretch.
44
45 Some fonts can be antialiased and scaled; \a scalable and \a antialiased
46 can be set to true for fonts with these attributes. The intended pixel
47 size of non-scalable fonts is specified by \a pixelSize; this value will be
48 ignored for scalable fonts.
50 The writing systems supported by the font are specified by the
51 \a writingSystems argument.
52
53 \sa registerFontFamily()
54*/
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, bool colorFont,
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, colorFont, writingSystems, usrPtr);
67}
68
69/*!
70 Registers a font family with the font database. The font will be
71 lazily populated by a callback to populateFamily() when the font
72 database determines that the family needs population.
73
74 \sa populateFamily(), registerFont()
75*/
76void QPlatformFontDatabase::registerFontFamily(const QString &familyName)
77{
78 qt_registerFontFamily(familyName);
79}
80
82{
83public:
89
91 : ref(1)
92 , list(other->list)
93 {
94 }
95
97 QList<bool> list;
98};
99
100/*!
101 Constructs a new object to handle supported writing systems.
102*/
103QSupportedWritingSystems::QSupportedWritingSystems()
104{
105 d = new QWritingSystemsPrivate;
106}
107
108/*!
109 Constructs a copy of the \a other writing systems object.
110*/
111QSupportedWritingSystems::QSupportedWritingSystems(const QSupportedWritingSystems &other)
112{
113 d = other.d;
114 d->ref.ref();
115}
116
117/*!
118 Constructs a copy of the \a other writing systems object.
119*/
120QSupportedWritingSystems &QSupportedWritingSystems::operator=(const QSupportedWritingSystems &other)
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
131bool operator==(const QSupportedWritingSystems &lhs, const QSupportedWritingSystems &rhs)
132{
133 return !(lhs != rhs);
134}
135
136bool operator!=(const QSupportedWritingSystems &lhs, const QSupportedWritingSystems &rhs)
137{
138 if (lhs.d == rhs.d)
139 return false;
140
141 Q_ASSERT(lhs.d->list.size() == rhs.d->list.size());
142 Q_ASSERT(lhs.d->list.size() == QFontDatabase::WritingSystemsCount);
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
152QDebug operator<<(QDebug debug, const QSupportedWritingSystems &sws)
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
171/*!
172 Destroys the supported writing systems object.
173*/
174QSupportedWritingSystems::~QSupportedWritingSystems()
175{
176 if (!d->ref.deref())
177 delete d;
178}
179
180/*!
181 \internal
182*/
183void QSupportedWritingSystems::detach()
184{
185 if (d->ref.loadRelaxed() != 1) {
186 QWritingSystemsPrivate *newd = new QWritingSystemsPrivate(d);
187 if (!d->ref.deref())
188 delete d;
189 d = newd;
190 }
191}
192
193/*!
194 Sets or clears support for the specified \a writingSystem based on the
195 value given by \a support.
196*/
197void QSupportedWritingSystems::setSupported(QFontDatabase::WritingSystem writingSystem, bool support)
198{
199 detach();
200 d->list[writingSystem] = support;
201}
202
203/*!
204 Returns \c true if the writing system specified by \a writingSystem is
205 supported; otherwise returns \c false.
206*/
207bool QSupportedWritingSystems::supported(QFontDatabase::WritingSystem writingSystem) const
208{
209 return d->list.at(writingSystem);
210}
211
212/*!
213 \class QSupportedWritingSystems
214 \brief The QSupportedWritingSystems class is used when registering fonts with the internal Qt
215 fontdatabase.
216 \ingroup painting
217 \inmodule QtGui
218
219 Its to provide an easy to use interface for indicating what writing systems a specific font
220 supports.
221
222*/
223
224/*!
225 \internal
226 */
227QPlatformFontDatabase::~QPlatformFontDatabase()
228{
229}
230
231/*!
232 This function is called once at startup by Qt's internal font database.
233 Reimplement this function in a subclass for a convenient place to initialize
234 the internal font database.
235
236 You may lazily populate the database by calling registerFontFamily() instead
237 of registerFont(), in which case you'll get a callback to populateFamily()
238 when the required family needs population. You then call registerFont() to
239 finish population of the family.
240
241 The default implementation does nothing.
242*/
243void QPlatformFontDatabase::populateFontDatabase()
244{
245}
246
247/*!
248 This function is called whenever a lazily populated family, populated
249 through registerFontFamily(), needs full population.
250
251 You are expected to fully populate the family by calling registerFont()
252 for each font that matches the family name.
253*/
254void QPlatformFontDatabase::populateFamily(const QString &familyName)
255{
256 Q_UNUSED(familyName);
257}
258
259/*!
260 This function is called whenever the font database is invalidated.
261
262 Reimplement this function to clear any internal data structures that
263 will need to be rebuilt at the next call to populateFontDatabase().
264*/
265void QPlatformFontDatabase::invalidate()
266{
267}
268
269/*!
270 Returns a multi font engine in the specified \a script to encapsulate \a fontEngine with the
271 option to fall back to the fonts given by \a fallbacks if \a fontEngine does not support
272 a certain character.
273*/
274QFontEngineMulti *QPlatformFontDatabase::fontEngineMulti(QFontEngine *fontEngine,
275 QFontDatabasePrivate::ExtendedScript script)
276{
277 return new QFontEngineMulti(fontEngine, script);
278}
279
280/*!
281 Returns the font engine that can be used to render the font described by
282 the font definition, \a fontDef, in the specified \a script.
283
284 This function is called by QFontDatabase both for system fonts provided
285 by the platform font database, as well as for application fonts added by
286 the application developer.
287
288 The handle is the QPlatformFontDatabase specific handle passed when
289 registering the font family via QPlatformFontDatabase::registerFont.
290
291 The function is called for both fonts added via a filename as well
292 as fonts added from QByteArray data. Subclasses will need to handle
293 both cases via its platform specific handle.
294*/
295QFontEngine *QPlatformFontDatabase::fontEngine(const QFontDef &fontDef, void *handle)
296{
297 Q_UNUSED(fontDef);
298 Q_UNUSED(handle);
299 qWarning("This plugin does not support loading system fonts.");
300 return nullptr;
301}
302
303/*!
304 Returns the font engine that will be used to back a QRawFont,
305 based on the given \fontData, \a pixelSize, and \a hintingPreference.
306
307 This function is called by QRawFont, and does not play a part in
308 the normal operations of QFontDatabase.
309*/
310QFontEngine *QPlatformFontDatabase::fontEngine(const QByteArray &fontData, qreal pixelSize,
311 QFont::HintingPreference hintingPreference)
312{
313 Q_UNUSED(fontData);
314 Q_UNUSED(pixelSize);
315 Q_UNUSED(hintingPreference);
316 qWarning("This plugin does not support font engines created directly from font data");
317 return nullptr;
318}
319
320/*!
321 Adds an application font described by the font contained supplied \a fontData
322 or using the font contained in the file referenced by \a fileName. Returns
323 a list of family names, or an empty list if the font could not be added.
324
325 If \a applicationFont is non-null, its \c properties list should be filled
326 with information from the loaded fonts. This is exposed through FontLoader in
327 Qt Quick where it is needed for disambiguating fonts in the same family. When
328 the function exits, the \a applicationFont should contain an entry of properties
329 per font in the file, or it should be empty if no font was loaded.
330
331 \note The default implementation of this function does not add an application
332 font. Subclasses should reimplement this function to perform the necessary
333 loading and registration of fonts.
334*/
335QStringList QPlatformFontDatabase::addApplicationFont(const QByteArray &fontData, const QString &fileName, QFontDatabasePrivate::ApplicationFont *applicationFont)
336{
337 Q_UNUSED(fontData);
338 Q_UNUSED(fileName);
339 Q_UNUSED(applicationFont);
340
341 if (applicationFont != nullptr)
342 applicationFont->properties.clear();
343
344 qWarning("This plugin does not support application fonts");
345
346 return QStringList();
347}
348
349/*!
350 Releases the specified font \a handle.
351*/
352void QPlatformFontDatabase::releaseHandle(void *handle)
353{
354 QByteArray *fileDataPtr = static_cast<QByteArray *>(handle);
355 delete fileDataPtr;
356}
357
358/*!
359 Returns the directory containing the fonts used by the database.
360*/
361QString QPlatformFontDatabase::fontDir() const
362{
363 QString fontpath = qEnvironmentVariable("QT_QPA_FONTDIR");
364 if (fontpath.isEmpty())
365 fontpath = QLibraryInfo::path(QLibraryInfo::LibrariesPath) + "/fonts"_L1;
366
367 return fontpath;
368}
369
370/*!
371 Returns true if the font family is private. For any given family name,
372 the result is platform dependent.
373*/
374bool QPlatformFontDatabase::isPrivateFontFamily(const QString &family) const
375{
376 Q_UNUSED(family);
377 return false;
378}
379
380/*!
381 Returns the default system font.
382
383 \sa QGuiApplication::font()
384 \since 5.0
385*/
386
387QFont QPlatformFontDatabase::defaultFont() const
388{
389 return QFont("Helvetica"_L1);
390}
391
392
393QString qt_resolveFontFamilyAlias(const QString &alias);
394
395/*!
396 Resolve alias to actual font family names.
397
398 \since 5.0
399 */
400QString QPlatformFontDatabase::resolveFontFamilyAlias(const QString &family) const
401{
402 return qt_resolveFontFamilyAlias(family);
403}
404
405/*!
406 Return true if all fonts are considered scalable when using this font database.
407 Defaults to false.
408
409 \since 5.0
410 */
411
412bool QPlatformFontDatabase::fontsAlwaysScalable() const
413{
414 return false;
415}
416
417/*!
418 Return list of standard font sizes when using this font database.
419
420 \since 5.0
421 */
422
423 QList<int> QPlatformFontDatabase::standardSizes() const
424{
425 QList<int> ret;
426 static const quint8 standard[] =
427 { 6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };
428 static const int num_standards = int(sizeof standard / sizeof *standard);
429 ret.reserve(num_standards);
430 std::copy(standard, standard + num_standards, std::back_inserter(ret));
431 return ret;
432}
433
434// see the Unicode subset bitfields in the MSDN docs
436 { 127, 127 }, // Any
437 { 0, 127 }, // Latin
438 { 7, 127 }, // Greek
439 { 9, 127 }, // Cyrillic
440 { 10, 127 }, // Armenian
441 { 11, 127 }, // Hebrew
442 { 13, 127 }, // Arabic
443 { 71, 127 }, // Syriac
444 { 72, 127 }, // Thaana
445 { 15, 127 }, // Devanagari
446 { 16, 127 }, // Bengali
447 { 17, 127 }, // Gurmukhi
448 { 18, 127 }, // Gujarati
449 { 19, 127 }, // Oriya
450 { 20, 127 }, // Tamil
451 { 21, 127 }, // Telugu
452 { 22, 127 }, // Kannada
453 { 23, 127 }, // Malayalam
454 { 73, 127 }, // Sinhala
455 { 24, 127 }, // Thai
456 { 25, 127 }, // Lao
457 { 70, 127 }, // Tibetan
458 { 74, 127 }, // Myanmar
459 { 26, 127 }, // Georgian
460 { 80, 127 }, // Khmer
461 { 126, 127 }, // SimplifiedChinese
462 { 126, 127 }, // TraditionalChinese
463 { 126, 127 }, // Japanese
464 { 56, 127 }, // Korean
465 { 0, 127 }, // Vietnamese (same as latin1)
466 { 126, 127 }, // Other
467 { 78, 127 }, // Ogham
468 { 79, 127 }, // Runic
469 { 14, 127 }, // Nko
470};
471
490
491/*!
492 Helper function that determines the writing system support based on the contents of the OS/2 table
493 in the font.
494
495 \since 6.0
496*/
497QSupportedWritingSystems QPlatformFontDatabase::writingSystemsFromOS2Table(const char *os2Table, size_t length)
498{
499 if (length >= 86) {
500 quint32 unicodeRange[4] = {
501 qFromBigEndian<quint32>(os2Table + 42),
502 qFromBigEndian<quint32>(os2Table + 46),
503 qFromBigEndian<quint32>(os2Table + 50),
504 qFromBigEndian<quint32>(os2Table + 54)
505 };
506 quint32 codePageRange[2] = {
507 qFromBigEndian<quint32>(os2Table + 78),
508 qFromBigEndian<quint32>(os2Table + 82)
509 };
510
511 return writingSystemsFromTrueTypeBits(unicodeRange, codePageRange);
512 }
513
514 return QSupportedWritingSystems();
515}
516
517/*!
518 Helper function that determines the writing systems support by a given
519 \a unicodeRange and \a codePageRange.
520
521 \since 5.1
522*/
523QSupportedWritingSystems QPlatformFontDatabase::writingSystemsFromTrueTypeBits(quint32 unicodeRange[4], quint32 codePageRange[2])
524{
525 QSupportedWritingSystems writingSystems;
526
527 bool hasScript = false;
528 for (int i = 0; i < QFontDatabase::WritingSystemsCount; ++i) {
529 int bit = requiredUnicodeBits[i][0];
530 int index = bit/32;
531 int flag = 1 << (bit&31);
532 if (bit != 126 && (unicodeRange[index] & flag)) {
533 bit = requiredUnicodeBits[i][1];
534 index = bit/32;
535
536 flag = 1 << (bit&31);
537 if (bit == 127 || (unicodeRange[index] & flag)) {
538 writingSystems.setSupported(QFontDatabase::WritingSystem(i));
539 hasScript = true;
540 // qDebug("font %s: index=%d, flag=%8x supports script %d", familyName.latin1(), index, flag, i);
541 }
542 }
543 }
544 if (codePageRange[0] & ((1 << Latin1CsbBit) | (1 << CentralEuropeCsbBit) | (1 << TurkishCsbBit) | (1 << BalticCsbBit))) {
545 writingSystems.setSupported(QFontDatabase::Latin);
546 hasScript = true;
547 //qDebug("font %s supports Latin", familyName.latin1());
548 }
549 if (codePageRange[0] & (1 << CyrillicCsbBit)) {
550 writingSystems.setSupported(QFontDatabase::Cyrillic);
551 hasScript = true;
552 //qDebug("font %s supports Cyrillic", familyName.latin1());
553 }
554 if (codePageRange[0] & (1 << GreekCsbBit)) {
555 writingSystems.setSupported(QFontDatabase::Greek);
556 hasScript = true;
557 //qDebug("font %s supports Greek", familyName.latin1());
558 }
559 if (codePageRange[0] & (1 << HebrewCsbBit)) {
560 writingSystems.setSupported(QFontDatabase::Hebrew);
561 hasScript = true;
562 //qDebug("font %s supports Hebrew", familyName.latin1());
563 }
564 if (codePageRange[0] & (1 << ArabicCsbBit)) {
565 writingSystems.setSupported(QFontDatabase::Arabic);
566 hasScript = true;
567 //qDebug("font %s supports Arabic", familyName.latin1());
568 }
569 if (codePageRange[0] & (1 << ThaiCsbBit)) {
570 writingSystems.setSupported(QFontDatabase::Thai);
571 hasScript = true;
572 //qDebug("font %s supports Thai", familyName.latin1());
573 }
574 if (codePageRange[0] & (1 << VietnameseCsbBit)) {
575 writingSystems.setSupported(QFontDatabase::Vietnamese);
576 hasScript = true;
577 //qDebug("font %s supports Vietnamese", familyName.latin1());
578 }
579 if (codePageRange[0] & (1 << SimplifiedChineseCsbBit)) {
580 writingSystems.setSupported(QFontDatabase::SimplifiedChinese);
581 hasScript = true;
582 //qDebug("font %s supports Simplified Chinese", familyName.latin1());
583 }
584 if (codePageRange[0] & (1 << TraditionalChineseCsbBit)) {
585 writingSystems.setSupported(QFontDatabase::TraditionalChinese);
586 hasScript = true;
587 //qDebug("font %s supports Traditional Chinese", familyName.latin1());
588 }
589 if (codePageRange[0] & (1 << JapaneseCsbBit)) {
590 writingSystems.setSupported(QFontDatabase::Japanese);
591 hasScript = true;
592 //qDebug("font %s supports Japanese", familyName.latin1());
593 }
594 if (codePageRange[0] & ((1 << KoreanCsbBit) | (1 << KoreanJohabCsbBit))) {
595 writingSystems.setSupported(QFontDatabase::Korean);
596 hasScript = true;
597 //qDebug("font %s supports Korean", familyName.latin1());
598 }
599 if (codePageRange[0] & (1U << SymbolCsbBit)) {
600 writingSystems = QSupportedWritingSystems();
601 hasScript = false;
602 }
603
604 if (!hasScript)
605 writingSystems.setSupported(QFontDatabase::Symbol);
606
607 return writingSystems;
608}
609
610/*!
611 Helper function that register the \a alias for the \a familyName.
612
613 \since 5.2
614*/
615
616void QPlatformFontDatabase::registerAliasToFontFamily(const QString &familyName, const QString &alias)
617{
618 qt_registerAliasToFontFamily(familyName, alias);
619}
620
621/*!
622 Requests that the platform font database should be repopulated.
623
624 This will result in invalidating the entire font database.
625
626 The next time the font database is accessed it will be repopulated
627 via a call to QPlatformFontDatabase::populate().
628
629 Application fonts will not be removed, and will be automatically
630 populated when the font database is repopulated.
631
632 \since 6.4
633*/
634void QPlatformFontDatabase::repopulateFontDatabase()
635{
636 QFontDatabasePrivate::instance()->invalidate();
637}
638
639/*!
640 Helper function that returns true if the font family has already been registered and populated.
641
642 \since 5.14
643*/
644bool QPlatformFontDatabase::isFamilyPopulated(const QString &familyName)
645{
646 return qt_isFontFamilyPopulated(familyName);
647}
648
649/*!
650 Returns true if this font database supports loading named instances from variable application
651 fonts.
652
653 \since 6.7
654*/
655bool QPlatformFontDatabase::supportsVariableApplicationFonts() const
656{
657 return false;
658}
659
660/*!
661 Returns true if this font database supports loading color fonts in the COLRv0 format.
662
663 \since 6.9
664*/
665bool QPlatformFontDatabase::supportsColrv0Fonts() const
666{
667 return false;
668}
669
670/*!
671 \class QPlatformFontDatabase
672 \since 5.0
673 \internal
674 \preliminary
675 \ingroup qpa
676 \ingroup painting
677
678 \brief The QPlatformFontDatabase class makes it possible to customize how fonts
679 are discovered and how they are rendered
680
681 QPlatformFontDatabase is the superclass which is intended to let platform implementations use
682 native font handling.
683
684 Qt has its internal font database which it uses to discover available fonts on the
685 user's system. To be able to populate this database subclass this class, and
686 reimplement populateFontDatabase().
687
688 Use the function registerFont() to populate the internal font database.
689
690 Sometimes a specified font does not have the required glyphs; in such a case, the
691 fallbackForFamily() function is called automatically to find alternative font
692 families that can supply alternatives to the missing glyphs.
693
694 \sa QSupportedWritingSystems
695*/
696QT_END_NAMESPACE
friend bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are equal, otherwise returns false.
Definition qbytearray.h:801
friend bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are different, otherwise returns false.
Definition qbytearray.h:812
QWritingSystemsPrivate(const QWritingSystemsPrivate *other)
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
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, bool colorFont, const QSupportedWritingSystems &writingSystems, void *handle)
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)
static const quint8 requiredUnicodeBits[QFontDatabase::WritingSystemsCount][2]
@ SimplifiedChineseCsbBit
@ CentralEuropeCsbBit
@ TraditionalChineseCsbBit