2018-11-08 14:51:51 +00:00
|
|
|
// Copyright (c) 2018 Slack Technologies, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/font_defaults.h"
|
2018-11-08 14:51:51 +00:00
|
|
|
|
2024-01-08 09:31:02 +00:00
|
|
|
#include <array>
|
2018-11-08 14:51:51 +00:00
|
|
|
#include <string>
|
2024-01-08 09:31:02 +00:00
|
|
|
#include <string_view>
|
2018-11-08 14:51:51 +00:00
|
|
|
|
2019-01-21 18:26:33 +00:00
|
|
|
#include "base/stl_util.h"
|
2024-01-08 09:31:02 +00:00
|
|
|
#include "base/strings/strcat.h"
|
2018-11-08 14:51:51 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
|
|
|
#include "chrome/common/pref_names.h"
|
|
|
|
#include "chrome/grit/platform_locale_settings.h"
|
2020-09-21 08:00:36 +00:00
|
|
|
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
|
2018-11-08 14:51:51 +00:00
|
|
|
#include "ui/base/l10n/l10n_util.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// The following list of font defaults was copied from
|
|
|
|
// https://chromium.googlesource.com/chromium/src/+/69.0.3497.106/chrome/browser/ui/prefs/prefs_tab_helper.cc#152
|
|
|
|
//
|
|
|
|
// The only updates that should be made to this list are copying updates that
|
|
|
|
// were made in Chromium.
|
|
|
|
//
|
|
|
|
// vvvvv DO NOT EDIT vvvvv
|
|
|
|
|
|
|
|
struct FontDefault {
|
|
|
|
const char* pref_name;
|
|
|
|
int resource_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Font pref defaults. The prefs that have defaults vary by platform, since not
|
|
|
|
// all platforms have fonts for all scripts for all generic families.
|
|
|
|
// TODO(falken): add proper defaults when possible for all
|
|
|
|
// platforms/scripts/generic families.
|
|
|
|
const FontDefault kFontDefaults[] = {
|
|
|
|
{prefs::kWebKitStandardFontFamily, IDS_STANDARD_FONT_FAMILY},
|
|
|
|
{prefs::kWebKitFixedFontFamily, IDS_FIXED_FONT_FAMILY},
|
|
|
|
{prefs::kWebKitSerifFontFamily, IDS_SERIF_FONT_FAMILY},
|
|
|
|
{prefs::kWebKitSansSerifFontFamily, IDS_SANS_SERIF_FONT_FAMILY},
|
|
|
|
{prefs::kWebKitCursiveFontFamily, IDS_CURSIVE_FONT_FAMILY},
|
|
|
|
{prefs::kWebKitFantasyFontFamily, IDS_FANTASY_FONT_FAMILY},
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
|
2018-11-08 14:51:51 +00:00
|
|
|
{prefs::kWebKitStandardFontFamilyJapanese,
|
|
|
|
IDS_STANDARD_FONT_FAMILY_JAPANESE},
|
|
|
|
{prefs::kWebKitFixedFontFamilyJapanese, IDS_FIXED_FONT_FAMILY_JAPANESE},
|
|
|
|
{prefs::kWebKitSerifFontFamilyJapanese, IDS_SERIF_FONT_FAMILY_JAPANESE},
|
|
|
|
{prefs::kWebKitSansSerifFontFamilyJapanese,
|
|
|
|
IDS_SANS_SERIF_FONT_FAMILY_JAPANESE},
|
|
|
|
{prefs::kWebKitStandardFontFamilyKorean, IDS_STANDARD_FONT_FAMILY_KOREAN},
|
|
|
|
{prefs::kWebKitSerifFontFamilyKorean, IDS_SERIF_FONT_FAMILY_KOREAN},
|
|
|
|
{prefs::kWebKitSansSerifFontFamilyKorean,
|
|
|
|
IDS_SANS_SERIF_FONT_FAMILY_KOREAN},
|
|
|
|
{prefs::kWebKitStandardFontFamilySimplifiedHan,
|
|
|
|
IDS_STANDARD_FONT_FAMILY_SIMPLIFIED_HAN},
|
|
|
|
{prefs::kWebKitSerifFontFamilySimplifiedHan,
|
|
|
|
IDS_SERIF_FONT_FAMILY_SIMPLIFIED_HAN},
|
|
|
|
{prefs::kWebKitSansSerifFontFamilySimplifiedHan,
|
|
|
|
IDS_SANS_SERIF_FONT_FAMILY_SIMPLIFIED_HAN},
|
|
|
|
{prefs::kWebKitStandardFontFamilyTraditionalHan,
|
|
|
|
IDS_STANDARD_FONT_FAMILY_TRADITIONAL_HAN},
|
|
|
|
{prefs::kWebKitSerifFontFamilyTraditionalHan,
|
|
|
|
IDS_SERIF_FONT_FAMILY_TRADITIONAL_HAN},
|
|
|
|
{prefs::kWebKitSansSerifFontFamilyTraditionalHan,
|
|
|
|
IDS_SANS_SERIF_FONT_FAMILY_TRADITIONAL_HAN},
|
|
|
|
#endif
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
|
2018-11-08 14:51:51 +00:00
|
|
|
{prefs::kWebKitCursiveFontFamilySimplifiedHan,
|
|
|
|
IDS_CURSIVE_FONT_FAMILY_SIMPLIFIED_HAN},
|
|
|
|
{prefs::kWebKitCursiveFontFamilyTraditionalHan,
|
|
|
|
IDS_CURSIVE_FONT_FAMILY_TRADITIONAL_HAN},
|
|
|
|
#endif
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_CHROMEOS)
|
2018-11-08 14:51:51 +00:00
|
|
|
{prefs::kWebKitStandardFontFamilyArabic, IDS_STANDARD_FONT_FAMILY_ARABIC},
|
|
|
|
{prefs::kWebKitSerifFontFamilyArabic, IDS_SERIF_FONT_FAMILY_ARABIC},
|
|
|
|
{prefs::kWebKitSansSerifFontFamilyArabic,
|
|
|
|
IDS_SANS_SERIF_FONT_FAMILY_ARABIC},
|
|
|
|
{prefs::kWebKitFixedFontFamilyKorean, IDS_FIXED_FONT_FAMILY_KOREAN},
|
|
|
|
{prefs::kWebKitFixedFontFamilySimplifiedHan,
|
|
|
|
IDS_FIXED_FONT_FAMILY_SIMPLIFIED_HAN},
|
|
|
|
{prefs::kWebKitFixedFontFamilyTraditionalHan,
|
|
|
|
IDS_FIXED_FONT_FAMILY_TRADITIONAL_HAN},
|
2022-02-10 02:58:52 +00:00
|
|
|
#elif BUILDFLAG(IS_WIN)
|
2018-11-08 14:51:51 +00:00
|
|
|
{prefs::kWebKitFixedFontFamilyArabic, IDS_FIXED_FONT_FAMILY_ARABIC},
|
|
|
|
{prefs::kWebKitSansSerifFontFamilyArabic,
|
|
|
|
IDS_SANS_SERIF_FONT_FAMILY_ARABIC},
|
|
|
|
{prefs::kWebKitStandardFontFamilyCyrillic,
|
|
|
|
IDS_STANDARD_FONT_FAMILY_CYRILLIC},
|
|
|
|
{prefs::kWebKitFixedFontFamilyCyrillic, IDS_FIXED_FONT_FAMILY_CYRILLIC},
|
|
|
|
{prefs::kWebKitSerifFontFamilyCyrillic, IDS_SERIF_FONT_FAMILY_CYRILLIC},
|
|
|
|
{prefs::kWebKitSansSerifFontFamilyCyrillic,
|
|
|
|
IDS_SANS_SERIF_FONT_FAMILY_CYRILLIC},
|
|
|
|
{prefs::kWebKitStandardFontFamilyGreek, IDS_STANDARD_FONT_FAMILY_GREEK},
|
|
|
|
{prefs::kWebKitFixedFontFamilyGreek, IDS_FIXED_FONT_FAMILY_GREEK},
|
|
|
|
{prefs::kWebKitSerifFontFamilyGreek, IDS_SERIF_FONT_FAMILY_GREEK},
|
|
|
|
{prefs::kWebKitSansSerifFontFamilyGreek, IDS_SANS_SERIF_FONT_FAMILY_GREEK},
|
|
|
|
{prefs::kWebKitFixedFontFamilyKorean, IDS_FIXED_FONT_FAMILY_KOREAN},
|
|
|
|
{prefs::kWebKitCursiveFontFamilyKorean, IDS_CURSIVE_FONT_FAMILY_KOREAN},
|
|
|
|
{prefs::kWebKitFixedFontFamilySimplifiedHan,
|
|
|
|
IDS_FIXED_FONT_FAMILY_SIMPLIFIED_HAN},
|
|
|
|
{prefs::kWebKitFixedFontFamilyTraditionalHan,
|
|
|
|
IDS_FIXED_FONT_FAMILY_TRADITIONAL_HAN},
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
// ^^^^^ DO NOT EDIT ^^^^^
|
|
|
|
|
2024-01-08 09:31:02 +00:00
|
|
|
// Get `kFontDefault`'s default fontname for [font family, script].
|
|
|
|
// e.g. ("webkit.webprefs.fonts.fixed", "Zyyy") -> "Monospace"
|
|
|
|
std::string GetDefaultFont(const std::string_view family_name,
|
|
|
|
const std::string_view script_name) {
|
|
|
|
const std::string pref_name = base::StrCat({family_name, ".", script_name});
|
|
|
|
|
|
|
|
for (const FontDefault& pref : kFontDefaults) {
|
|
|
|
if (pref_name == pref.pref_name) {
|
2018-11-08 14:51:51 +00:00
|
|
|
return l10n_util::GetStringUTF8(pref.resource_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 09:31:02 +00:00
|
|
|
return std::string{};
|
|
|
|
}
|
2018-11-08 14:51:51 +00:00
|
|
|
|
2024-01-08 09:31:02 +00:00
|
|
|
// Each font family has kWebKitScriptsForFontFamilyMapsLength scripts.
|
|
|
|
// This is a lookup array for script_index -> fontname
|
|
|
|
using PerFamilyFonts =
|
|
|
|
std::array<std::u16string, prefs::kWebKitScriptsForFontFamilyMapsLength>;
|
2018-11-08 14:51:51 +00:00
|
|
|
|
2024-01-08 09:31:02 +00:00
|
|
|
PerFamilyFonts MakeCacheForFamily(const std::string_view family_name) {
|
|
|
|
PerFamilyFonts ret;
|
|
|
|
for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) {
|
|
|
|
const char* script_name = prefs::kWebKitScriptsForFontFamilyMaps[i];
|
|
|
|
ret[i] = base::UTF8ToUTF16(GetDefaultFont(family_name, script_name));
|
2018-11-08 14:51:51 +00:00
|
|
|
}
|
2024-01-08 09:31:02 +00:00
|
|
|
return ret;
|
2018-11-08 14:51:51 +00:00
|
|
|
}
|
|
|
|
|
2024-01-08 09:31:02 +00:00
|
|
|
void FillFontFamilyMap(const PerFamilyFonts& cache,
|
|
|
|
blink::web_pref::ScriptFontFamilyMap& font_family_map) {
|
2018-11-08 14:51:51 +00:00
|
|
|
for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) {
|
2024-01-08 09:31:02 +00:00
|
|
|
if (const std::u16string& fontname = cache[i]; !fontname.empty()) {
|
|
|
|
char const* const script_name = prefs::kWebKitScriptsForFontFamilyMaps[i];
|
|
|
|
font_family_map[script_name] = fontname;
|
2018-11-08 14:51:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 09:31:02 +00:00
|
|
|
struct FamilyCache {
|
|
|
|
std::string_view family_name;
|
|
|
|
|
|
|
|
// where to find the font_family_map in a WebPreferences instance
|
|
|
|
blink::web_pref::ScriptFontFamilyMap blink::web_pref::WebPreferences::*
|
|
|
|
map_offset;
|
|
|
|
|
|
|
|
PerFamilyFonts fonts = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
static auto MakeCache() {
|
|
|
|
// the font families whose defaults we want to cache
|
|
|
|
std::array<FamilyCache, 5> cache{{
|
|
|
|
{prefs::kWebKitStandardFontFamilyMap,
|
|
|
|
&blink::web_pref::WebPreferences::standard_font_family_map},
|
|
|
|
{prefs::kWebKitFixedFontFamilyMap,
|
|
|
|
&blink::web_pref::WebPreferences::fixed_font_family_map},
|
|
|
|
{prefs::kWebKitSerifFontFamilyMap,
|
|
|
|
&blink::web_pref::WebPreferences::serif_font_family_map},
|
|
|
|
{prefs::kWebKitSansSerifFontFamilyMap,
|
|
|
|
&blink::web_pref::WebPreferences::sans_serif_font_family_map},
|
|
|
|
{prefs::kWebKitCursiveFontFamilyMap,
|
|
|
|
&blink::web_pref::WebPreferences::cursive_font_family_map},
|
|
|
|
}};
|
|
|
|
|
|
|
|
// populate the cache
|
|
|
|
for (FamilyCache& row : cache) {
|
|
|
|
row.fonts = MakeCacheForFamily(row.family_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2018-11-08 14:51:51 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
2020-09-21 08:00:36 +00:00
|
|
|
void SetFontDefaults(blink::web_pref::WebPreferences* prefs) {
|
2024-01-08 09:31:02 +00:00
|
|
|
static auto const cache = MakeCache();
|
|
|
|
|
|
|
|
for (FamilyCache const& row : cache) {
|
|
|
|
FillFontFamilyMap(row.fonts, prefs->*row.map_offset);
|
|
|
|
}
|
2018-11-08 14:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace electron
|