font_defaults.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // Copyright (c) 2018 Slack Technologies, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/font_defaults.h"
  5. #include <string>
  6. #include <string_view>
  7. #include "base/containers/fixed_flat_map.h"
  8. #include "base/containers/map_util.h"
  9. #include "base/strings/cstring_view.h"
  10. #include "base/strings/string_split.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. #include "chrome/browser/browser_process.h"
  13. #include "chrome/common/pref_names.h"
  14. #include "chrome/grit/platform_locale_settings.h"
  15. #include "third_party/blink/public/common/web_preferences/web_preferences.h"
  16. #include "third_party/icu/source/common/unicode/uchar.h"
  17. #include "third_party/icu/source/common/unicode/uscript.h"
  18. #include "ui/base/l10n/l10n_util.h"
  19. #if BUILDFLAG(IS_WIN)
  20. #include <windows.h>
  21. #endif
  22. #if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
  23. // If a font name in prefs default values starts with a comma, consider it's a
  24. // comma-separated font list and resolve it to the first available font.
  25. #define PREFS_FONT_LIST 1
  26. #include "ui/gfx/font_list.h"
  27. #else
  28. #define PREFS_FONT_LIST 0
  29. #endif
  30. namespace {
  31. #if BUILDFLAG(IS_WIN)
  32. // On Windows with antialiasing we want to use an alternate fixed font like
  33. // Consolas, which looks much better than Courier New.
  34. bool ShouldUseAlternateDefaultFixedFont(const std::string& script) {
  35. if (!base::StartsWith(script, "courier",
  36. base::CompareCase::INSENSITIVE_ASCII)) {
  37. return false;
  38. }
  39. UINT smooth_type = 0;
  40. SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE, 0, &smooth_type, 0);
  41. return smooth_type == FE_FONTSMOOTHINGCLEARTYPE;
  42. }
  43. #endif
  44. // Returns the script of the font pref |pref_name|. For example, suppose
  45. // |pref_name| is "webkit.webprefs.fonts.serif.Hant". Since the script code for
  46. // the script name "Hant" is USCRIPT_TRADITIONAL_HAN, the function returns
  47. // USCRIPT_TRADITIONAL_HAN. |pref_name| must be a valid font pref name.
  48. UScriptCode GetScriptOfFontPref(const base::cstring_view pref_name) {
  49. // ICU script names are four letters.
  50. static const size_t kScriptNameLength = 4;
  51. size_t len = pref_name.size();
  52. DCHECK_GT(len, kScriptNameLength);
  53. const char* scriptName = pref_name.substr(len - kScriptNameLength).data();
  54. int32_t code = u_getPropertyValueEnum(UCHAR_SCRIPT, scriptName);
  55. DCHECK(code >= 0 && code < USCRIPT_CODE_LIMIT);
  56. return static_cast<UScriptCode>(code);
  57. }
  58. // Returns the primary script used by the browser's UI locale. For example, if
  59. // the locale is "ru", the function returns USCRIPT_CYRILLIC, and if the locale
  60. // is "en", the function returns USCRIPT_LATIN.
  61. UScriptCode GetScriptOfBrowserLocale(const std::string& locale) {
  62. // For Chinese locales, uscript_getCode() just returns USCRIPT_HAN but our
  63. // per-script fonts are for USCRIPT_SIMPLIFIED_HAN and
  64. // USCRIPT_TRADITIONAL_HAN.
  65. if (locale == "zh-CN") {
  66. return USCRIPT_SIMPLIFIED_HAN;
  67. }
  68. if (locale == "zh-TW") {
  69. return USCRIPT_TRADITIONAL_HAN;
  70. }
  71. // For Korean and Japanese, multiple scripts are returned by
  72. // |uscript_getCode|, but we're passing a one entry buffer leading
  73. // the buffer to be filled by USCRIPT_INVALID_CODE. We need to
  74. // hard-code the results for them.
  75. if (locale == "ko") {
  76. return USCRIPT_HANGUL;
  77. }
  78. if (locale == "ja") {
  79. return USCRIPT_JAPANESE;
  80. }
  81. UScriptCode code = USCRIPT_INVALID_CODE;
  82. UErrorCode err = U_ZERO_ERROR;
  83. uscript_getCode(locale.c_str(), &code, 1, &err);
  84. if (U_FAILURE(err)) {
  85. code = USCRIPT_INVALID_CODE;
  86. }
  87. return code;
  88. }
  89. struct FontDefault {
  90. const char* pref_name;
  91. int resource_id;
  92. };
  93. // The following list of font defaults was copied from
  94. // https://chromium.googlesource.com/chromium/src/+/69.0.3497.106/chrome/browser/ui/prefs/prefs_tab_helper.cc#152
  95. //
  96. // The only updates that should be made to this list are copying updates that
  97. // were made in Chromium.
  98. //
  99. // vvvvv DO NOT EDIT vvvvv
  100. // Font pref defaults. The prefs that have defaults vary by platform, since not
  101. // all platforms have fonts for all scripts for all generic families.
  102. // TODO(falken): add proper defaults when possible for all
  103. // platforms/scripts/generic families.
  104. constexpr auto kFontDefaults = std::to_array<FontDefault>({
  105. {prefs::kWebKitStandardFontFamily, IDS_STANDARD_FONT_FAMILY},
  106. {prefs::kWebKitFixedFontFamily, IDS_FIXED_FONT_FAMILY},
  107. {prefs::kWebKitSerifFontFamily, IDS_SERIF_FONT_FAMILY},
  108. {prefs::kWebKitSansSerifFontFamily, IDS_SANS_SERIF_FONT_FAMILY},
  109. {prefs::kWebKitCursiveFontFamily, IDS_CURSIVE_FONT_FAMILY},
  110. {prefs::kWebKitFantasyFontFamily, IDS_FANTASY_FONT_FAMILY},
  111. {prefs::kWebKitMathFontFamily, IDS_MATH_FONT_FAMILY},
  112. #if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
  113. {prefs::kWebKitStandardFontFamilyJapanese,
  114. IDS_STANDARD_FONT_FAMILY_JAPANESE},
  115. {prefs::kWebKitFixedFontFamilyJapanese, IDS_FIXED_FONT_FAMILY_JAPANESE},
  116. {prefs::kWebKitSerifFontFamilyJapanese, IDS_SERIF_FONT_FAMILY_JAPANESE},
  117. {prefs::kWebKitSansSerifFontFamilyJapanese,
  118. IDS_SANS_SERIF_FONT_FAMILY_JAPANESE},
  119. {prefs::kWebKitStandardFontFamilyKorean, IDS_STANDARD_FONT_FAMILY_KOREAN},
  120. {prefs::kWebKitSerifFontFamilyKorean, IDS_SERIF_FONT_FAMILY_KOREAN},
  121. {prefs::kWebKitSansSerifFontFamilyKorean,
  122. IDS_SANS_SERIF_FONT_FAMILY_KOREAN},
  123. {prefs::kWebKitStandardFontFamilySimplifiedHan,
  124. IDS_STANDARD_FONT_FAMILY_SIMPLIFIED_HAN},
  125. {prefs::kWebKitSerifFontFamilySimplifiedHan,
  126. IDS_SERIF_FONT_FAMILY_SIMPLIFIED_HAN},
  127. {prefs::kWebKitSansSerifFontFamilySimplifiedHan,
  128. IDS_SANS_SERIF_FONT_FAMILY_SIMPLIFIED_HAN},
  129. {prefs::kWebKitStandardFontFamilyTraditionalHan,
  130. IDS_STANDARD_FONT_FAMILY_TRADITIONAL_HAN},
  131. {prefs::kWebKitSerifFontFamilyTraditionalHan,
  132. IDS_SERIF_FONT_FAMILY_TRADITIONAL_HAN},
  133. {prefs::kWebKitSansSerifFontFamilyTraditionalHan,
  134. IDS_SANS_SERIF_FONT_FAMILY_TRADITIONAL_HAN},
  135. #endif
  136. #if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
  137. {prefs::kWebKitCursiveFontFamilySimplifiedHan,
  138. IDS_CURSIVE_FONT_FAMILY_SIMPLIFIED_HAN},
  139. {prefs::kWebKitCursiveFontFamilyTraditionalHan,
  140. IDS_CURSIVE_FONT_FAMILY_TRADITIONAL_HAN},
  141. #endif
  142. #if BUILDFLAG(IS_CHROMEOS)
  143. {prefs::kWebKitStandardFontFamilyArabic, IDS_STANDARD_FONT_FAMILY_ARABIC},
  144. {prefs::kWebKitSerifFontFamilyArabic, IDS_SERIF_FONT_FAMILY_ARABIC},
  145. {prefs::kWebKitSansSerifFontFamilyArabic,
  146. IDS_SANS_SERIF_FONT_FAMILY_ARABIC},
  147. {prefs::kWebKitFixedFontFamilyKorean, IDS_FIXED_FONT_FAMILY_KOREAN},
  148. {prefs::kWebKitFixedFontFamilySimplifiedHan,
  149. IDS_FIXED_FONT_FAMILY_SIMPLIFIED_HAN},
  150. {prefs::kWebKitFixedFontFamilyTraditionalHan,
  151. IDS_FIXED_FONT_FAMILY_TRADITIONAL_HAN},
  152. #elif BUILDFLAG(IS_WIN)
  153. {prefs::kWebKitFixedFontFamilyArabic, IDS_FIXED_FONT_FAMILY_ARABIC},
  154. {prefs::kWebKitSansSerifFontFamilyArabic,
  155. IDS_SANS_SERIF_FONT_FAMILY_ARABIC},
  156. {prefs::kWebKitStandardFontFamilyCyrillic,
  157. IDS_STANDARD_FONT_FAMILY_CYRILLIC},
  158. {prefs::kWebKitFixedFontFamilyCyrillic, IDS_FIXED_FONT_FAMILY_CYRILLIC},
  159. {prefs::kWebKitSerifFontFamilyCyrillic, IDS_SERIF_FONT_FAMILY_CYRILLIC},
  160. {prefs::kWebKitSansSerifFontFamilyCyrillic,
  161. IDS_SANS_SERIF_FONT_FAMILY_CYRILLIC},
  162. {prefs::kWebKitStandardFontFamilyGreek, IDS_STANDARD_FONT_FAMILY_GREEK},
  163. {prefs::kWebKitFixedFontFamilyGreek, IDS_FIXED_FONT_FAMILY_GREEK},
  164. {prefs::kWebKitSerifFontFamilyGreek, IDS_SERIF_FONT_FAMILY_GREEK},
  165. {prefs::kWebKitSansSerifFontFamilyGreek, IDS_SANS_SERIF_FONT_FAMILY_GREEK},
  166. {prefs::kWebKitFixedFontFamilyKorean, IDS_FIXED_FONT_FAMILY_KOREAN},
  167. {prefs::kWebKitCursiveFontFamilyKorean, IDS_CURSIVE_FONT_FAMILY_KOREAN},
  168. {prefs::kWebKitFixedFontFamilySimplifiedHan,
  169. IDS_FIXED_FONT_FAMILY_SIMPLIFIED_HAN},
  170. {prefs::kWebKitFixedFontFamilyTraditionalHan,
  171. IDS_FIXED_FONT_FAMILY_TRADITIONAL_HAN},
  172. #endif
  173. });
  174. // ^^^^^ DO NOT EDIT ^^^^^
  175. auto MakeDefaultFontCopier() {
  176. using namespace prefs;
  177. using WP = blink::web_pref::WebPreferences;
  178. using FamilyMap = blink::web_pref::ScriptFontFamilyMap;
  179. // Map from a family name (e.g. "webkit.webprefs.fonts.fixed") to a
  180. // Pointer-to-Member of the location in WebPreferences of its
  181. // ScriptFontFamilyMap (e.g. &WebPreferences::fixed_font_family_map)
  182. static constexpr auto FamilyMapByName =
  183. base::MakeFixedFlatMap<std::string_view, FamilyMap WP::*>({
  184. {kWebKitStandardFontFamilyMap, &WP::standard_font_family_map},
  185. {kWebKitFixedFontFamilyMap, &WP::fixed_font_family_map},
  186. {kWebKitSerifFontFamilyMap, &WP::serif_font_family_map},
  187. {kWebKitSansSerifFontFamilyMap, &WP::sans_serif_font_family_map},
  188. {kWebKitCursiveFontFamilyMap, &WP::cursive_font_family_map},
  189. });
  190. WP defaults;
  191. std::set<std::string> fonts_with_defaults;
  192. UScriptCode browser_script =
  193. GetScriptOfBrowserLocale(g_browser_process->GetApplicationLocale());
  194. // Populate `defaults`'s ScriptFontFamilyMaps with the values from
  195. // the kFontDefaults array in the "DO NOT EDIT" section of this file.
  196. //
  197. // The kFontDefaults's `pref_name` field is built as `${family}.${script}`,
  198. // so splitting on the last '.' gives the family and script: a pref key of
  199. // "webkit.webprefs.fonts.fixed.Zyyy" splits into family name
  200. // "webkit.webprefs.fonts.fixed" and script "Zyyy". (Yes, "Zyyy" is real.
  201. // See pref_font_script_names-inl.h for the full list :)
  202. for (auto [pref_name, resource_id] : kFontDefaults) {
  203. #if BUILDFLAG(IS_WIN)
  204. if (pref_name == prefs::kWebKitFixedFontFamily) {
  205. if (ShouldUseAlternateDefaultFixedFont(
  206. l10n_util::GetStringUTF8(resource_id))) {
  207. resource_id = IDS_FIXED_FONT_FAMILY_ALT_WIN;
  208. }
  209. }
  210. #endif
  211. UScriptCode pref_script =
  212. GetScriptOfFontPref(UNSAFE_BUFFERS(base::cstring_view(pref_name)));
  213. // Suppress this default font pref value if it is for the primary script of
  214. // the browser's UI locale. For example, if the pref is for the sans-serif
  215. // font for the Cyrillic script, and the browser locale is "ru" (Russian),
  216. // the default is suppressed. Otherwise, the default would override the
  217. // user's font preferences when viewing pages in their native language.
  218. // This is because users have no way yet of customizing their per-script
  219. // font preferences. The font prefs accessible in the options UI are for
  220. // the default, unknown script; these prefs have less priority than the
  221. // per-script font prefs when the script of the content is known. This code
  222. // can possibly be removed later if users can easily access per-script font
  223. // prefs (e.g., via the extensions workflow), or the problem turns out to
  224. // not be really critical after all.
  225. if (browser_script != pref_script) {
  226. std::string value = l10n_util::GetStringUTF8(resource_id);
  227. #if PREFS_FONT_LIST
  228. if (value.starts_with(',')) {
  229. value = gfx::FontList::FirstAvailableOrFirst(value);
  230. }
  231. #else // !PREFS_FONT_LIST
  232. DCHECK(!value.starts_with(','))
  233. << "This platform doesn't support default font lists. " << pref_name
  234. << "=" << value;
  235. #endif // PREFS_FONT_LIST
  236. const auto [family, script] = *base::RSplitStringOnce(pref_name, '.');
  237. if (auto* family_map_ptr = base::FindOrNull(FamilyMapByName, family)) {
  238. FamilyMap& family_map = defaults.**family_map_ptr;
  239. family_map[std::string{script}] = base::UTF8ToUTF16(value);
  240. }
  241. fonts_with_defaults.insert(pref_name);
  242. }
  243. }
  244. // Expand the font concatenated with script name so this stays at RO memory
  245. // rather than allocated in heap.
  246. // clang-format off
  247. static const auto kFontFamilyMap = std::to_array<const char *>({
  248. #define EXPAND_SCRIPT_FONT(map_name, script_name) map_name "." script_name,
  249. #include "chrome/common/pref_font_script_names-inl.h"
  250. ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_CURSIVE)
  251. ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_FIXED)
  252. ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_SANSERIF)
  253. ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_SERIF)
  254. ALL_FONT_SCRIPTS(WEBKIT_WEBPREFS_FONTS_STANDARD)
  255. #undef EXPAND_SCRIPT_FONT
  256. });
  257. // clang-format on
  258. for (const char* const pref_name : kFontFamilyMap) {
  259. if (fonts_with_defaults.find(pref_name) == fonts_with_defaults.end()) {
  260. // We haven't already set a default value for this font preference, so set
  261. // an empty string as the default.
  262. const auto [family, script] = *base::RSplitStringOnce(pref_name, '.');
  263. if (auto* family_map_ptr = base::FindOrNull(FamilyMapByName, family)) {
  264. FamilyMap& family_map = defaults.**family_map_ptr;
  265. family_map[std::string{script}] = std::u16string();
  266. }
  267. }
  268. }
  269. // Lambda that copies all of `default`'s fonts into `prefs`
  270. auto copy_default_fonts_to_web_prefs = [defaults](WP* prefs) {
  271. for (const auto [_, family_map_ptr] : FamilyMapByName) {
  272. const FamilyMap& src = defaults.*family_map_ptr;
  273. FamilyMap& tgt = prefs->*family_map_ptr;
  274. for (const auto& [key, val] : src)
  275. tgt[key] = val;
  276. }
  277. };
  278. return copy_default_fonts_to_web_prefs;
  279. }
  280. } // namespace
  281. namespace electron {
  282. void SetFontDefaults(blink::web_pref::WebPreferences* prefs) {
  283. static const auto copy_default_fonts_to_web_prefs = MakeDefaultFontCopier();
  284. copy_default_fonts_to_web_prefs(prefs);
  285. }
  286. } // namespace electron