summarylogtreecommitdiffstats
path: root/Use-system-font.patch
blob: 1a88b8ae562cc2f9ea302519ae9da773027e6b86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings
index 34a13bc57..15291cd8c 100644
--- a/Telegram/Resources/langs/lang.strings
+++ b/Telegram/Resources/langs/lang.strings
@@ -2313,6 +2313,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 "ktg_settings_chat_list_compact" = "Compact chat list";
 
 "ktg_fonts_title" = "Fonts";
+"ktg_settings_use_system_font" = "Use system font";
 "ktg_settings_fonts" = "Change application fonts";
 "ktg_fonts_reset" = "Reset";
 "ktg_fonts_about" = "If you want to use \"Bold\" font for your semibold, do not put \"Bold\" in font name, use \"Make semibold bold\" option.\n\nYou will need to restart app to apply and see changes.";
diff --git a/Telegram/Resources/langs/rewrites/ru.json b/Telegram/Resources/langs/rewrites/ru.json
index fc59a598d..3387bf20e 100644
--- a/Telegram/Resources/langs/rewrites/ru.json
+++ b/Telegram/Resources/langs/rewrites/ru.json
@@ -53,6 +53,7 @@
 	"ktg_settings_always_show_scheduled": "Всегда показывать отложенные",
 	"ktg_settings_chat_list_compact": "Компактный список чатов",
 	"ktg_fonts_title": "Шрифты",
+	"ktg_settings_use_system_font": "Использовать системный шрифт",
 	"ktg_settings_fonts": "Изменить шрифты приложения",
 	"ktg_fonts_reset": "Сброс",
 	"ktg_fonts_about": "Если вы хотите использовать жирное начертание для полужирного шрифта, не пишите «Bold» в названии, используйте настройку «Сделать полужирный жирным».\n\nДля применения и просмотра изменений требуется перезапуск.",
diff --git a/Telegram/SourceFiles/core/application.cpp b/Telegram/SourceFiles/core/application.cpp
index 0a71fcda6..cac371c96 100644
--- a/Telegram/SourceFiles/core/application.cpp
+++ b/Telegram/SourceFiles/core/application.cpp
@@ -175,6 +175,9 @@ void Application::run() {
 	if (!cMonospaceFont().isEmpty()) {
 		style::internal::SetMonospaceFont(cMonospaceFont());
 	}
+	if (cUseSystemFont()) {
+		style::internal::SetUseSystemFont(cUseSystemFont());
+	}
 	style::internal::StartFonts();
 
 	ThirdParty::start();
diff --git a/Telegram/SourceFiles/core/kotato_settings.cpp b/Telegram/SourceFiles/core/kotato_settings.cpp
index d5da1f418..fc34589b6 100644
--- a/Telegram/SourceFiles/core/kotato_settings.cpp
+++ b/Telegram/SourceFiles/core/kotato_settings.cpp
@@ -142,6 +142,11 @@ bool Manager::readCustomFile() {
 		if (settingsFontsMonospace != settingsFonts.constEnd() && (*settingsFontsMonospace).isString()) {
 			cSetMonospaceFont((*settingsFontsMonospace).toString());
 		}
+
+		const auto settingsFontsUseSystemFont = settingsFonts.constFind(qsl("use_system_font"));
+		if (settingsFontsUseSystemFont != settingsFonts.constEnd() && (*settingsFontsUseSystemFont).isBool()) {
+			cSetUseSystemFont((*settingsFontsUseSystemFont).toBool());
+		}
 	}
 
 	const auto settingsStickerHeightIt = settings.constFind(qsl("sticker_height"));
@@ -254,6 +259,7 @@ void Manager::writeDefaultFile() {
 	settingsFonts.insert(qsl("semibold"), qsl("Open Sans Semibold"));
 	settingsFonts.insert(qsl("semibold_is_bold"), false);
 	settingsFonts.insert(qsl("monospaced"), qsl("Consolas"));
+	settingsFonts.insert(qsl("use_system_font"), cUseSystemFont());
 
 	settings.insert(qsl("fonts"), settingsFonts);
 
@@ -308,6 +314,7 @@ void Manager::writeCurrentSettings() {
 	}
 
 	settingsFonts.insert(qsl("semibold_is_bold"), cSemiboldFontIsBold());
+	settingsFonts.insert(qsl("use_system_font"), cUseSystemFont());
 
 	settings.insert(qsl("fonts"), settingsFonts);
 
diff --git a/Telegram/SourceFiles/settings.cpp b/Telegram/SourceFiles/settings.cpp
index f841a59f3..a60f78ea4 100644
--- a/Telegram/SourceFiles/settings.cpp
+++ b/Telegram/SourceFiles/settings.cpp
@@ -210,6 +210,12 @@ rpl::producer<> UpdatedRecentEmoji() {
 QString gMainFont, gSemiboldFont, gMonospaceFont;
 bool gSemiboldFontIsBold = false;
 
+#ifdef DESKTOP_APP_USE_PACKAGED_FONTS
+bool gUseSystemFont = true;
+#else
+bool gUseSystemFont = false;
+#endif
+
 rpl::variable<int> gStickerHeight = 128;
 void SetStickerHeight(int height) {
 	gStickerHeight = height;
diff --git a/Telegram/SourceFiles/settings.h b/Telegram/SourceFiles/settings.h
index fd6fa8f2f..ec84b9911 100644
--- a/Telegram/SourceFiles/settings.h
+++ b/Telegram/SourceFiles/settings.h
@@ -183,6 +183,7 @@ DeclareSetting(QString, MainFont);
 DeclareSetting(QString, SemiboldFont);
 DeclareSetting(bool, SemiboldFontIsBold);
 DeclareSetting(QString, MonospaceFont);
+DeclareSetting(bool, UseSystemFont);
 
 void SetBigEmojiOutline(bool enabled);
 [[nodiscard]] bool BigEmojiOutline();
diff --git a/Telegram/SourceFiles/settings/settings_kotato.cpp b/Telegram/SourceFiles/settings/settings_kotato.cpp
index d3773f41d..f4ebc7145 100644
--- a/Telegram/SourceFiles/settings/settings_kotato.cpp
+++ b/Telegram/SourceFiles/settings/settings_kotato.cpp
@@ -139,6 +139,33 @@ void SetupKotatoChats(not_null<Ui::VerticalLayout*> container) {
 		KotatoSettings::Write();
 	}, container->lifetime());
 
+	const auto useSystemFontToggled = Ui::CreateChild<rpl::event_stream<bool>>(
+		container.get());
+	AddButton(
+		container,
+		tr::ktg_settings_use_system_font(),
+		st::settingsButton
+	)->toggleOn(
+		useSystemFontToggled->events_starting_with_copy(cUseSystemFont())
+	)->toggledValue(
+	) | rpl::filter([](bool enabled) {
+		return (enabled != cUseSystemFont());
+	}) | rpl::start_with_next([=](bool enabled) {
+		const auto confirmed = [=] {
+			cSetUseSystemFont(enabled);
+			KotatoSettings::Write();
+			App::restart();
+		};
+		const auto cancelled = [=] {
+			useSystemFontToggled->fire(cUseSystemFont() == true);
+		};
+		Ui::show(Box<ConfirmBox>(
+			tr::lng_settings_need_restart(tr::now),
+			tr::lng_settings_restart_now(tr::now),
+			confirmed,
+			cancelled));
+	}, container->lifetime());
+
 	AddButton(
 		container,
 		tr::ktg_settings_fonts(),
Submodule Telegram/lib_ui contains modified content
diff --git a/Telegram/lib_ui/ui/style/style_core.cpp b/Telegram/lib_ui/ui/style/style_core.cpp
index 3f9b003..6872555 100644
--- a/Telegram/lib_ui/ui/style/style_core.cpp
+++ b/Telegram/lib_ui/ui/style/style_core.cpp
@@ -19,6 +19,9 @@
 
 namespace style {
 namespace internal {
+
+bool GetUseSystemFont();
+
 namespace {
 
 constexpr auto kMinContrastAlpha = 64;
@@ -55,10 +58,12 @@ void ResolveMonospaceFont() {
 	if (!CustomMonospaceFont.isEmpty()) {
 		tryFont(CustomMonospaceFont);
 	}
-	tryFont("Consolas");
-	tryFont("Liberation Mono");
-	tryFont("Menlo");
-	tryFont("Courier");
+	if (!GetUseSystemFont()) {
+		tryFont("Consolas");
+		tryFont("Liberation Mono");
+		tryFont("Menlo");
+		tryFont("Courier");
+	}
 	if (family.isEmpty()) {
 		const auto type = QFontDatabase::FixedFont;
 		family = QFontDatabase::systemFont(type).family();
diff --git a/Telegram/lib_ui/ui/style/style_core_font.cpp b/Telegram/lib_ui/ui/style/style_core_font.cpp
index 72269a4..a36fcfd 100644
--- a/Telegram/lib_ui/ui/style/style_core_font.cpp
+++ b/Telegram/lib_ui/ui/style/style_core_font.cpp
@@ -170,9 +170,14 @@ QString Overrides[FontTypesCount];
 QString CustomMainFont;
 QString CustomSemiboldFont;
 bool CustomSemiboldIsBold = false;
+bool UseSystemFont = false;
 
 } // namespace
 
+bool GetUseSystemFont() {
+	return UseSystemFont;
+}
+
 void SetMainFont(const QString &familyName) {
 	CustomMainFont = familyName;
 }
@@ -185,6 +190,10 @@ void SetSemiboldIsBold(bool isBold) {
 	CustomSemiboldIsBold = isBold;
 }
 
+void SetUseSystemFont(bool ifUse) {
+	UseSystemFont = ifUse;
+}
+
 void StartFonts() {
 	if (Started) {
 		return;
@@ -193,41 +202,43 @@ void StartFonts() {
 
 	style_InitFontsResource();
 
+	if (!UseSystemFont) {
 #ifndef DESKTOP_APP_USE_PACKAGED_FONTS
-	bool areGood[FontTypesCount] = { false };
-	for (auto i = 0; i != FontTypesCount; ++i) {
-		const auto name = FontTypeNames[i];
-		const auto flags = FontTypeFlags[i];
-		areGood[i] = LoadCustomFont(":/gui/fonts/" + name + ".ttf", name, flags);
-		Overrides[i] = name;
+		bool areGood[FontTypesCount] = { false };
+		for (auto i = 0; i != FontTypesCount; ++i) {
+			const auto name = FontTypeNames[i];
+			const auto flags = FontTypeFlags[i];
+			areGood[i] = LoadCustomFont(":/gui/fonts/" + name + ".ttf", name, flags);
+			Overrides[i] = name;
 #ifdef Q_OS_WIN
-		// Attempt to workaround a strange font bug with Open Sans Semibold not loading.
-		// See https://github.com/telegramdesktop/tdesktop/issues/3276 for details.
-		// Crash happens on "options.maxh / _t->_st->font->height" with "division by zero".
-		// In that place "_t->_st->font" is "semiboldFont" is "font(13 "Open Sans Semibold").
-		const auto fallback = FontTypeWindowsFallback[i];
-		if (!areGood[i]) {
-			if (ValidateFont(fallback, flags)) {
-				Overrides[i] = fallback;
-				UI_LOG(("Fonts Info: Using '%1' instead of '%2'.").arg(fallback).arg(name));
+			// Attempt to workaround a strange font bug with Open Sans Semibold not loading.
+			// See https://github.com/telegramdesktop/tdesktop/issues/3276 for details.
+			// Crash happens on "options.maxh / _t->_st->font->height" with "division by zero".
+			// In that place "_t->_st->font" is "semiboldFont" is "font(13 "Open Sans Semibold").
+			const auto fallback = FontTypeWindowsFallback[i];
+			if (!areGood[i]) {
+				if (ValidateFont(fallback, flags)) {
+					Overrides[i] = fallback;
+					UI_LOG(("Fonts Info: Using '%1' instead of '%2'.").arg(fallback).arg(name));
+				}
 			}
-		}
-		// Disable default fallbacks to Segoe UI, see:
-		// https://github.com/telegramdesktop/tdesktop/issues/5368
-		//
-		//QFont::insertSubstitution(name, fallback);
+			// Disable default fallbacks to Segoe UI, see:
+			// https://github.com/telegramdesktop/tdesktop/issues/5368
+			//
+			//QFont::insertSubstitution(name, fallback);
 #endif // Q_OS_WIN
-	}
+		}
 #endif // !DESKTOP_APP_USE_PACKAGED_FONTS
 #ifdef Q_OS_MAC
-	auto list = QStringList();
-	list.append(".SF NS Text");
-	list.append("Helvetica Neue");
-	list.append("Lucida Grande");
-	for (const auto &name : FontTypeNames) {
-		QFont::insertSubstitutions(name, list);
-	}
+		auto list = QStringList();
+		list.append(".SF NS Text");
+		list.append("Helvetica Neue");
+		list.append("Lucida Grande");
+		for (const auto &name : FontTypeNames) {
+			QFont::insertSubstitutions(name, list);
+		}
 #endif // Q_OS_MAC
+	}
 
 	if (!CustomMainFont.isEmpty() && ValidateFont(CustomMainFont)) {
 		Overrides[FontTypeRegular] = CustomMainFont;
@@ -264,7 +275,13 @@ QString GetPossibleEmptyOverride(const QString &familyName, int32 flags) {
 }
 
 QString GetFontOverride(const QString &familyName, int32 flags) {
-	const auto result = GetPossibleEmptyOverride(familyName, flags);
+	auto result = GetPossibleEmptyOverride(familyName, flags);
+
+	if (UseSystemFont && result.isEmpty() &&
+	(familyName == "Open Sans" || familyName == "Open Sans Semibold")) {
+		result = QFontDatabase::systemFont(QFontDatabase::GeneralFont).family();
+	}
+
 	return result.isEmpty() ? familyName : result;
 }
 
@@ -303,6 +320,9 @@ FontData::FontData(int size, uint32 flags, int family, Font *other)
 		f.setBold(true);
 	} else if (fontFamilies[family] == "Open Sans Semibold" && CustomSemiboldIsBold) {
 		f.setBold(true);
+	} else if (fontFamilies[family] == "Open Sans Semibold" && UseSystemFont
+		&& GetPossibleEmptyOverride(fontFamilies[family], flags).isEmpty()) {
+		f.setWeight(QFont::DemiBold);
 #ifdef DESKTOP_APP_USE_PACKAGED_FONTS
 	} else if (fontFamilies[family] == "Open Sans Semibold") {
 		f.setWeight(QFont::DemiBold);
diff --git a/Telegram/lib_ui/ui/style/style_core_font.h b/Telegram/lib_ui/ui/style/style_core_font.h
index 6ea22c0..c684a71 100644
--- a/Telegram/lib_ui/ui/style/style_core_font.h
+++ b/Telegram/lib_ui/ui/style/style_core_font.h
@@ -17,6 +17,7 @@ namespace internal {
 void SetMainFont(const QString &familyName);
 void SetSemiboldFont(const QString &familyName);
 void SetSemiboldIsBold(bool isBold);
+void SetUseSystemFont(bool ifUse);
 
 void StartFonts();
 [[nodiscard]] QString GetFontOverride(const QString &familyName, int32 flags = 0);