Skip to content

Commit 87bdd97

Browse files
committed
core: add DropExpensiveFonts pragma disabling woff and woff2 fonts
1 parent 7c5a6c4 commit 87bdd97

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

changelog/next.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ set shell id.
4343
- Added `QS_DISABLE_CRASH_HANDLER` environment variable to disable crash handling.
4444
- Added `QS_CRASHREPORT_URL` environment variable to allow overriding the crash reporter link.
4545
- Added `AppId` pragma and `QS_APP_ID` environment variable to allow overriding the desktop application ID.
46+
- Added `DropExpensiveFonts` pragma which avoids loading fonts which may cause lag and excessive memory usage if many variants are used.
47+
- Unrecognized pragmas are no longer a hard error for future backward compatibility.
4648

4749
## Bug Fixes
4850

src/launch/launch.cpp

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ int launch(const LaunchArgs& args, char** argv, QCoreApplication* coreApplicatio
7777
QString iconTheme = qEnvironmentVariable("QS_ICON_THEME");
7878
QHash<QString, QString> envOverrides;
7979
QString appId = qEnvironmentVariable("QS_APP_ID");
80+
bool dropExpensiveFonts = false;
8081
QString dataDir;
8182
QString stateDir;
8283
QString cacheDir;
@@ -92,6 +93,7 @@ int launch(const LaunchArgs& args, char** argv, QCoreApplication* coreApplicatio
9293
else if (pragma == "NativeTextRendering") pragmas.nativeTextRendering = true;
9394
else if (pragma == "IgnoreSystemSettings") pragmas.desktopSettingsAware = false;
9495
else if (pragma == "RespectSystemStyle") pragmas.useSystemStyle = true;
96+
else if (pragma == "DropExpensiveFonts") pragmas.dropExpensiveFonts = true;
9597
else if (pragma.startsWith("IconTheme ")) pragmas.iconTheme = pragma.sliced(10);
9698
else if (pragma.startsWith("Env ")) {
9799
auto envPragma = pragma.sliced(4);
@@ -116,8 +118,7 @@ int launch(const LaunchArgs& args, char** argv, QCoreApplication* coreApplicatio
116118
} else if (pragma.startsWith("CacheDir ")) {
117119
pragmas.cacheDir = pragma.sliced(9).trimmed();
118120
} else {
119-
qCritical() << "Unrecognized pragma" << pragma;
120-
return -1;
121+
qWarning() << "Unrecognized pragma" << pragma;
121122
}
122123
} else if (line.startsWith("import")) break;
123124
}
@@ -168,6 +169,46 @@ int launch(const LaunchArgs& args, char** argv, QCoreApplication* coreApplicatio
168169

169170
Common::INITIAL_ENVIRONMENT = QProcessEnvironment::systemEnvironment();
170171

172+
if (pragmas.dropExpensiveFonts) {
173+
if (auto* runDir = QsPaths::instance()->instanceRunDir()) {
174+
auto baseConfigPath = qEnvironmentVariable("FONTCONFIG_FILE");
175+
if (baseConfigPath.isEmpty()) baseConfigPath = "/etc/fonts/fonts.conf";
176+
177+
auto wrapperPath = runDir->filePath("fonts-override.conf");
178+
auto filterFile = QFile(wrapperPath);
179+
if (filterFile.open(QFile::WriteOnly | QFile::Truncate | QFile::Text)) {
180+
auto filterTemplate = QStringLiteral(R"(<?xml version="1.0"?>
181+
<!DOCTYPE fontconfig SYSTEM "urn:fontconfig:fonts.dtd">
182+
<fontconfig>
183+
<include ignore_missing="no">%1</include>
184+
<selectfont>
185+
<rejectfont>
186+
<pattern>
187+
<patelt name="fontwrapper">
188+
<string>woff</string>
189+
</patelt>
190+
</pattern>
191+
<pattern>
192+
<patelt name="fontwrapper">
193+
<string>woff2</string>
194+
</patelt>
195+
</pattern>
196+
</rejectfont>
197+
</selectfont>
198+
</fontconfig>
199+
)");
200+
201+
QTextStream(&filterFile) << filterTemplate.arg(baseConfigPath);
202+
filterFile.close();
203+
qputenv("FONTCONFIG_FILE", wrapperPath.toUtf8());
204+
} else {
205+
qCritical() << "Could not write fontconfig filter to" << wrapperPath;
206+
}
207+
} else {
208+
qCritical() << "Could not create fontconfig filter: instance run directory unavailable";
209+
}
210+
}
211+
171212
if (!pragmas.useSystemStyle) {
172213
qunsetenv("QT_STYLE_OVERRIDE");
173214
qputenv("QT_QUICK_CONTROLS_STYLE", "Fusion");

0 commit comments

Comments
 (0)