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
qandroidplatformfontdatabase.cpp
Go to the documentation of this file.
1// Copyright (C) 2012 BogDan Vatra <bogdan@kde.org>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QDir>
5#include <QLocale>
6
8
10
11using namespace Qt::StringLiterals;
12
13QString QAndroidPlatformFontDatabase::fontDir() const
14{
15 return "/system/fonts"_L1;
16}
17
18QFont QAndroidPlatformFontDatabase::defaultFont() const
19{
20 return QFont("Roboto"_L1);
21}
22
23void QAndroidPlatformFontDatabase::populateFontDatabase()
24{
25 QString fontpath = fontDir();
26 QDir dir(fontpath);
27
28 if (Q_UNLIKELY(!dir.exists())) {
29 qFatal("QFontDatabase: Cannot find font directory %s - is Qt installed correctly?",
30 qPrintable(fontpath));
31 }
32
33 QStringList nameFilters;
34 nameFilters << "*.ttf"_L1
35 << "*.otf"_L1
36 << "*.ttc"_L1;
37
38 const auto entries = dir.entryInfoList(nameFilters, QDir::Files);
39 for (const QFileInfo &fi : entries) {
40 const QByteArray file = QFile::encodeName(fi.absoluteFilePath());
41 QFreeTypeFontDatabase::addTTFile(QByteArray(), file);
42 }
43}
44
45QStringList QAndroidPlatformFontDatabase::fallbacksForFamily(const QString &family,
46 QFont::Style style,
47 QFont::StyleHint styleHint,
48 QFontDatabasePrivate::ExtendedScript script) const
49{
50 QStringList result;
51
52 if (script == QFontDatabasePrivate::Script_Emoji) {
53 result.append(QStringLiteral("Noto Color Emoji"));
54 result.append(QStringLiteral("Noto Color Emoji Flags"));
55 }
56
57 // Prepend CJK fonts by the locale.
58 QLocale locale = QLocale::system();
59 switch (locale.language()) {
60 case QLocale::Chinese: {
61 switch (locale.territory()) {
62 case QLocale::China:
63 case QLocale::Singapore:
64 result.append(QStringLiteral("Noto Sans Mono CJK SC"));
65 break;
66 case QLocale::Taiwan:
67 case QLocale::HongKong:
68 case QLocale::Macao:
69 result.append(QStringLiteral("Noto Sans Mono CJK TC"));
70 break;
71 default:
72 // no modifications.
73 break;
74 }
75 break;
76 }
77 case QLocale::Japanese:
78 result.append(QStringLiteral("Noto Sans Mono CJK JP"));
79 break;
80 case QLocale::Korean:
81 result.append(QStringLiteral("Noto Sans Mono CJK KR"));
82 break;
83 default:
84 // no modifications.
85 break;
86 }
87
88 if (styleHint == QFont::Monospace || styleHint == QFont::Courier)
89 result.append(QString(qgetenv("QT_ANDROID_FONTS_MONOSPACE")).split(u';'));
90 else if (styleHint == QFont::Serif)
91 result.append(QString(qgetenv("QT_ANDROID_FONTS_SERIF")).split(u';'));
92 else
93 result.append(QString(qgetenv("QT_ANDROID_FONTS")).split(u';'));
94 result.append(QFreeTypeFontDatabase::fallbacksForFamily(family, style, styleHint, script));
95
96 return result;
97}
98
99QT_END_NAMESPACE