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 QStringList paths = QLibraryInfo::paths(QLibraryInfo::LibrariesPath);
366 if (!paths.isEmpty())
367 fontpath = paths.takeFirst() + "/fonts"_L1;
368 }
369
370 return fontpath;
371}
372
373/*!
374 Returns true if the font family is private. For any given family name,
375 the result is platform dependent.
376*/
377bool QPlatformFontDatabase::isPrivateFontFamily(const QString &family) const
378{
379 Q_UNUSED(family);
380 return false;
381}
382
383/*!
384 Returns the default system font.
385
386 \sa QGuiApplication::font()
387 \since 5.0
388*/
389
390QFont QPlatformFontDatabase::defaultFont() const
391{
392 return QFont("Helvetica"_L1);
393}
394
395
396QString qt_resolveFontFamilyAlias(const QString &alias);
397
398/*!
399 Resolve alias to actual font family names.
400
401 \since 5.0
402 */
403QString QPlatformFontDatabase::resolveFontFamilyAlias(const QString &family) const
404{
405 return qt_resolveFontFamilyAlias(family);
406}
407
408/*!
409 Return true if all fonts are considered scalable when using this font database.
410 Defaults to false.
411
412 \since 5.0
413 */
414
415bool QPlatformFontDatabase::fontsAlwaysScalable() const
416{
417 return false;
418}
419
420/*!
421 Return list of standard font sizes when using this font database.
422
423 \since 5.0
424 */
425
426 QList<int> QPlatformFontDatabase::standardSizes() const
427{
428 QList<int> ret;
429 static const quint8 standard[] =
430 { 6, 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };
431 static const int num_standards = int(sizeof standard / sizeof *standard);
432 ret.reserve(num_standards);
433 std::copy(standard, standard + num_standards, std::back_inserter(ret));
434 return ret;
435}
436
437// see the Unicode subset bitfields in the MSDN docs
439 { 127, 127 }, // Any
440 { 0, 127 }, // Latin
441 { 7, 127 }, // Greek
442 { 9, 127 }, // Cyrillic
443 { 10, 127 }, // Armenian
444 { 11, 127 }, // Hebrew
445 { 13, 127 }, // Arabic
446 { 71, 127 }, // Syriac
447 { 72, 127 }, // Thaana
448 { 15, 127 }, // Devanagari
449 { 16, 127 }, // Bengali
450 { 17, 127 }, // Gurmukhi
451 { 18, 127 }, // Gujarati
452 { 19, 127 }, // Oriya
453 { 20, 127 }, // Tamil
454 { 21, 127 }, // Telugu
455 { 22, 127 }, // Kannada
456 { 23, 127 }, // Malayalam
457 { 73, 127 }, // Sinhala
458 { 24, 127 }, // Thai
459 { 25, 127 }, // Lao
460 { 70, 127 }, // Tibetan
461 { 74, 127 }, // Myanmar
462 { 26, 127 }, // Georgian
463 { 80, 127 }, // Khmer
464 { 126, 127 }, // SimplifiedChinese
465 { 126, 127 }, // TraditionalChinese
466 { 126, 127 }, // Japanese
467 { 56, 127 }, // Korean
468 { 0, 127 }, // Vietnamese (same as latin1)
469 { 126, 127 }, // Other
470 { 78, 127 }, // Ogham
471 { 79, 127 }, // Runic
472 { 14, 127 }, // Nko
473};
474
493
494/*!
495 Helper function that determines the writing system support based on the contents of the OS/2 table
496 in the font.
497
498 \since 6.0
499*/
500QSupportedWritingSystems QPlatformFontDatabase::writingSystemsFromOS2Table(const char *os2Table, size_t length)
501{
502 if (length >= 86) {
503 quint32 unicodeRange[4] = {
504 qFromBigEndian<quint32>(os2Table + 42),
505 qFromBigEndian<quint32>(os2Table + 46),
506 qFromBigEndian<quint32>(os2Table + 50),
507 qFromBigEndian<quint32>(os2Table + 54)
508 };
509 quint32 codePageRange[2] = {
510 qFromBigEndian<quint32>(os2Table + 78),
511 qFromBigEndian<quint32>(os2Table + 82)
512 };
513
514 return writingSystemsFromTrueTypeBits(unicodeRange, codePageRange);
515 }
516
517 return QSupportedWritingSystems();
518}
519
520/*!
521 Helper function that determines the writing systems support by a given
522 \a unicodeRange and \a codePageRange.
523
524 \since 5.1
525*/
526QSupportedWritingSystems QPlatformFontDatabase::writingSystemsFromTrueTypeBits(quint32 unicodeRange[4], quint32 codePageRange[2])
527{
528 QSupportedWritingSystems writingSystems;
529
530 bool hasScript = false;
531 for (int i = 0; i < QFontDatabase::WritingSystemsCount; ++i) {
532 int bit = requiredUnicodeBits[i][0];
533 int index = bit/32;
534 int flag = 1 << (bit&31);
535 if (bit != 126 && (unicodeRange[index] & flag)) {
536 bit = requiredUnicodeBits[i][1];
537 index = bit/32;
538
539 flag = 1 << (bit&31);
540 if (bit == 127 || (unicodeRange[index] & flag)) {
541 writingSystems.setSupported(QFontDatabase::WritingSystem(i));
542 hasScript = true;
543 // qDebug("font %s: index=%d, flag=%8x supports script %d", familyName.latin1(), index, flag, i);
544 }
545 }
546 }
547 if (codePageRange[0] & ((1 << Latin1CsbBit) | (1 << CentralEuropeCsbBit) | (1 << TurkishCsbBit) | (1 << BalticCsbBit))) {
548 writingSystems.setSupported(QFontDatabase::Latin);
549 hasScript = true;
550 //qDebug("font %s supports Latin", familyName.latin1());
551 }
552 if (codePageRange[0] & (1 << CyrillicCsbBit)) {
553 writingSystems.setSupported(QFontDatabase::Cyrillic);
554 hasScript = true;
555 //qDebug("font %s supports Cyrillic", familyName.latin1());
556 }
557 if (codePageRange[0] & (1 << GreekCsbBit)) {
558 writingSystems.setSupported(QFontDatabase::Greek);
559 hasScript = true;
560 //qDebug("font %s supports Greek", familyName.latin1());
561 }
562 if (codePageRange[0] & (1 << HebrewCsbBit)) {
563 writingSystems.setSupported(QFontDatabase::Hebrew);
564 hasScript = true;
565 //qDebug("font %s supports Hebrew", familyName.latin1());
566 }
567 if (codePageRange[0] & (1 << ArabicCsbBit)) {
568 writingSystems.setSupported(QFontDatabase::Arabic);
569 hasScript = true;
570 //qDebug("font %s supports Arabic", familyName.latin1());
571 }
572 if (codePageRange[0] & (1 << ThaiCsbBit)) {
573 writingSystems.setSupported(QFontDatabase::Thai);
574 hasScript = true;
575 //qDebug("font %s supports Thai", familyName.latin1());
576 }
577 if (codePageRange[0] & (1 << VietnameseCsbBit)) {
578 writingSystems.setSupported(QFontDatabase::Vietnamese);
579 hasScript = true;
580 //qDebug("font %s supports Vietnamese", familyName.latin1());
581 }
582 if (codePageRange[0] & (1 << SimplifiedChineseCsbBit)) {
583 writingSystems.setSupported(QFontDatabase::SimplifiedChinese);
584 hasScript = true;
585 //qDebug("font %s supports Simplified Chinese", familyName.latin1());
586 }
587 if (codePageRange[0] & (1 << TraditionalChineseCsbBit)) {
588 writingSystems.setSupported(QFontDatabase::TraditionalChinese);
589 hasScript = true;
590 //qDebug("font %s supports Traditional Chinese", familyName.latin1());
591 }
592 if (codePageRange[0] & (1 << JapaneseCsbBit)) {
593 writingSystems.setSupported(QFontDatabase::Japanese);
594 hasScript = true;
595 //qDebug("font %s supports Japanese", familyName.latin1());
596 }
597 if (codePageRange[0] & ((1 << KoreanCsbBit) | (1 << KoreanJohabCsbBit))) {
598 writingSystems.setSupported(QFontDatabase::Korean);
599 hasScript = true;
600 //qDebug("font %s supports Korean", familyName.latin1());
601 }
602 if (codePageRange[0] & (1U << SymbolCsbBit)) {
603 writingSystems = QSupportedWritingSystems();
604 hasScript = false;
605 }
606
607 if (!hasScript)
608 writingSystems.setSupported(QFontDatabase::Symbol);
609
610 return writingSystems;
611}
612
613/*!
614 Helper function that register the \a alias for the \a familyName.
615
616 \since 5.2
617*/
618
619void QPlatformFontDatabase::registerAliasToFontFamily(const QString &familyName, const QString &alias)
620{
621 qt_registerAliasToFontFamily(familyName, alias);
622}
623
624/*!
625 Requests that the platform font database should be repopulated.
626
627 This will result in invalidating the entire font database.
628
629 The next time the font database is accessed it will be repopulated
630 via a call to QPlatformFontDatabase::populate().
631
632 Application fonts will not be removed, and will be automatically
633 populated when the font database is repopulated.
634
635 \since 6.4
636*/
637void QPlatformFontDatabase::repopulateFontDatabase()
638{
639 QFontDatabasePrivate::instance()->invalidate();
640}
641
642/*!
643 Helper function that returns true if the font family has already been registered and populated.
644
645 \since 5.14
646*/
647bool QPlatformFontDatabase::isFamilyPopulated(const QString &familyName)
648{
649 return qt_isFontFamilyPopulated(familyName);
650}
651
652/*!
653 Returns true if this font database supports loading named instances from variable application
654 fonts.
655
656 \since 6.7
657*/
658bool QPlatformFontDatabase::supportsVariableApplicationFonts() const
659{
660 return false;
661}
662
663/*!
664 Returns true if this font database supports loading color fonts in the COLRv0 format.
665
666 \since 6.9
667*/
668bool QPlatformFontDatabase::supportsColrv0Fonts() const
669{
670 return false;
671}
672
673/*!
674 \class QPlatformFontDatabase
675 \since 5.0
676 \internal
677 \preliminary
678 \ingroup qpa
679 \ingroup painting
680
681 \brief The QPlatformFontDatabase class makes it possible to customize how fonts
682 are discovered and how they are rendered
683
684 QPlatformFontDatabase is the superclass which is intended to let platform implementations use
685 native font handling.
686
687 Qt has its internal font database which it uses to discover available fonts on the
688 user's system. To be able to populate this database subclass this class, and
689 reimplement populateFontDatabase().
690
691 Use the function registerFont() to populate the internal font database.
692
693 Sometimes a specified font does not have the required glyphs; in such a case, the
694 fallbackForFamily() function is called automatically to find alternative font
695 families that can supply alternatives to the missing glyphs.
696
697 \sa QSupportedWritingSystems
698*/
699QT_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:815
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:826
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