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
qohosplatformfontdatabase.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 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
4#include <QtCore/private/qcore_ohos_p.h>
5#include <QtCore/qfileinfo.h>
6
9#include <QtCore/qset.h>
10#include <QtCore/private/qnapi_p.h>
11#include <fontconfig/fontconfig.h>
12
13using namespace std::string_literals;
14
16
17// Defined in gui/text/qfontdatabase.cpp
18Q_GUI_EXPORT QFontDatabase::WritingSystem qt_writing_system_for_script(int script);
19
20namespace {
21
22using SystemFontType = QtOhosQpa::enums::ohos::graphics::text::SystemFontType;
23
24bool ohosNoUiChildMode = false;
25
26QStringList getAllFontPaths()
27{
28 if (ohosNoUiChildMode)
29 return {};
30
31 auto fontsPaths = QOhosJsThreadGateway::evalWithPromise<std::vector<std::string>>(
32 [](QOhosJsState &jsState, QOhosTaskPromise<std::vector<std::string>> evalPromise) {
33 auto thenCatchPromises = std::move(evalPromise).makeThenCatchBranches(Q_FUNC_INFO);
34 jsState.evalToPromiseOrRejectOnThrow(
35 "@ohos.graphics.text.getSystemFontFullNamesByType(*)",
36 {jsState.mapOhosEnumToJs(SystemFontType::ALL)})
37 .onThen(
38 [thenPromise = std::move(thenCatchPromises.first)](const QOhosCallbackInfo &cbInfo) mutable {
39 auto fontsNamesArray = cbInfo.getFirstArg<QNapi::Array>(Q_FUNC_INFO);
40
41 if (fontsNamesArray.Length() == 0) {
42 thenPromise({});
43 return;
44 }
45
46 auto fontsNames = QNapi::getArrayElements<std::vector<std::string>, QNapi::String>(fontsNamesArray);
47 auto sharedThenPromise = QtOhos::moveToSharedPtr(std::move(thenPromise).makeChained(Q_FUNC_INFO));
48 auto pathsCollector = std::make_shared<QOhosConsumer<std::string>>(
49 [sharedThenPromise, pushSize = fontsNames.size(), result = std::vector<std::string>()](auto element) mutable {
50 result.push_back(std::move(element));
51 if (result.size() == pushSize)
52 (*sharedThenPromise)(std::move(result));
53 });
54
55 for (const auto &fontName : fontsNames) {
56 cbInfo.jsState().evalToPromiseOrRejectOnThrow(
57 "@ohos.graphics.text.getFontDescriptorByFullName(*)",
58 {fontName, cbInfo.jsState().mapOhosEnumToJs(SystemFontType::ALL)})
59 .onThen(
60 [pathsCollector](const QOhosCallbackInfo &cbInfo) {
61 auto fontDescriptor = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
62 auto optFontPath = QNapi::getOptionalPropOrEmpty<QNapi::String>(fontDescriptor, "path");
63 (*pathsCollector)(!optFontPath.IsEmpty() ? optFontPath.Utf8Value() : ""s);
64 })
65 .onCatch(
66 [pathsCollector, fontName](const QOhosCallbackInfo &cbInfo) {
67 QtOhos::logJsCallbackError(
68 cbInfo, ("getFontDescriptorByFullName("s + fontName + ") failed"s).c_str());
69 (*pathsCollector)(""s);
70 });
71 }
72 })
73 .onCatch(
74 [catchPromise = std::move(thenCatchPromises.second)](const QOhosCallbackInfo &cbInfo) {
75 QtOhos::logJsCallbackError(cbInfo, "getSystemFontFullNamesByType() failed");
76 catchPromise({});
77 });
78 },
79 Q_FUNC_INFO);
80
81 QStringList result;
82 std::transform(
83 fontsPaths.begin(), fontsPaths.end(),
84 std::back_inserter(result), QString::fromStdString);
85 result.removeAll(QString());
86 result.sort();
87
88 return result;
89}
90
91std::string getDefaultFontFamily(QOhosJsState &jsState)
92{
93 if (ohosNoUiChildMode)
94 return "";
95
96 std::string defaultFontFamily;
97 auto genericFonts = jsState.eval<QNapi::Array>("@ohos.font.getUIFontConfig().generic");
98 if (genericFonts.Length() > 0) {
99 auto firstFont = QNapi::checkedCast<QNapi::Object>(genericFonts.Get(0U));
100 defaultFontFamily = firstFont.get<QNapi::String>("family");
101 } else {
102 qFatal("Failed to get system default font family name."
103 " The reason is: empty `@ohos.font.getUIFontConfig().generic` array.");
104 }
105
106 return defaultFontFamily;
107}
108
109void registerSystemFonts()
110{
111 QStringList fontPaths = getAllFontPaths();
112 fontPaths.removeDuplicates();
113
114 QSet<QString> uniqueFontDirs;
115 for (const QString &fontPath : fontPaths)
116 uniqueFontDirs.insert(QFileInfo(fontPath).absolutePath());
117
118 FcConfig *config = FcConfigGetCurrent();
119 if (config != nullptr) {
120 for (const QString &dir : uniqueFontDirs)
121 FcConfigAppFontAddDir(config, reinterpret_cast<const FcChar8 *>(dir.toUtf8().constData()));
122 FcConfigBuildFonts(config);
123 } else {
124 qOhosPrintfError("Failed to get fontconfig current configuration.");
125 std::abort();
126 }
127}
128
129}
130
132{
133 ohosNoUiChildMode = true;
134}
135
137{
138 FcInit();
139
140 registerSystemFonts();
141
142 QFontconfigDatabase::populateFontDatabase();
143}
144
146 QFont::Style style,
147 QFont::StyleHint styleHint,
148 QFontDatabasePrivate::ExtendedScript script) const
149{
150 QStringList result;
151
152 const QFontDatabase::WritingSystem ws = qt_writing_system_for_script(script);
153 const bool defaultFontSupportsScript =
154 ws == QFontDatabase::Any ||
155 QFontDatabase::writingSystems(defaultFont().family()).contains(ws);
156
157 if (defaultFontSupportsScript)
158 result.append(defaultFont().family());
159
160 if (styleHint == QFont::Monospace || styleHint == QFont::Courier)
161 result.append(QString::fromUtf8(qgetenv("Droid Sans Mono;Droid Sans;Noto Sans")).split(QLatin1Char(';')));
162 else if (styleHint == QFont::Serif)
163 result.append(QString::fromUtf8(qgetenv("Noto Serif")).split(QLatin1Char(';')));
164 else
165 result.append(QString::fromUtf8(qgetenv("Roboto;Droid Sans")).split(QLatin1Char(';')));
166 result.append(QFontconfigDatabase::fallbacksForFamily(family, style, styleHint, script));
167
168 return result;
169}
170
172{
173 static const char * const preferredDefaultFontFamily = "HarmonyOS Sans SC";
174 static const QString defaultFontFamily =
175 QFontDatabase::families().contains(QLatin1String(preferredDefaultFontFamily))
176 ? QString::fromStdString(preferredDefaultFontFamily)
177 : QOhosJsThreadGateway::eval(
178 [&](auto &jsState) {
179 return QString::fromStdString(getDefaultFontFamily(jsState));
180 },
181 Q_FUNC_INFO);
182 return QFont(defaultFontFamily);
183}
184
185QT_END_NAMESPACE
QStringList fallbacksForFamily(const QString &family, QFont::Style style, QFont::StyleHint styleHint, QFontDatabasePrivate::ExtendedScript script) const override
Returns a list of alternative fonts for the specified family and style and script using the styleHint...
QFont defaultFont() const override
Returns the default system font.
void populateFontDatabase() override
This function is called once at startup by Qt's internal font database.
QDebug Q_GUI_EXPORT & operator<<(QDebug &s, const QVectorPath &path)