electron/shell/browser/font_defaults.cc
Charles Kerr 60c4c9fec6
chore: remove unused #includes (#42971)
* chore: iwyu buildflags.h

* chore: iwyu dictionary.h

* chore: iwyu arguments.h

* chore: iwyu values.h

* chore: iwyu compiler_specific.h

* chore: iwyu binder_map.h

* chore: iwyu <vector>

* chore: iwyu <set>

* chore: iwyu raw_ptr

* chore: iwyu gfx/canvas.h

* chore: iwyu gfx/color_utils.h

* chore: iwyu base/strings/stringprintf.h

* chore: iwyu base/task/thread_pool.h

* chore: iwyu base/no_destructor.h

* chore: iwyu base/path_service.h

* chore: iwyu base/files/file_pathh

* chore: iwyu base/strings/sys_string_conversions.h

* chore: iwyu base/logging.h

* chore: iwyu base/command_line.h

* chore: iwyu base/files/file_util.h

* chore: iwyu base/files/scoped_file.h

* chore: iwyu base/strings/utf_string_conversions.h

* chore: iwyu base/environment.h

* chore: iwyu base/scoped_observation.h

* chore: iwyu base/strings/string_split.h

* chore: iwyu base/strings/pattern.h

* chore: iwyu base/json/string_escape.h

* chore: iwyu base/json/json_reader.h

* chore: iwyu base/memory/singleton.h

* chore: iwyu base/observer_list.h

* chore: iwyu base/timer/timer.h

* fixup! chore: iwyu values.h

* chore: iwyu shell/browser/browser.h

* chore: iwyu base/stl_util.h

* chore: iwyu base/strings/string_util.h

* chore: iwyu shell/browser/javascript_environment.h

* chore: iwyu base/memory/ref_counted.h

* chore: iwyu base/environment.h

* chore: iwyu content/public/browser/browser_thread.h

* chore: remove unused typedef gin_helper::EventEmitter::ValueArray

* chore: iwyu gin/wrappable.h

* chore: iwyu shell/common/gin_helper/function_template_extensions.h

* chore: iwyu shell/common/gin_converters/login_item_settings_converter.h

* chore: iwyu shell/common/gin_helper/arguments.h

* chore: iwyu ui/gfx/skia_util.h

* chore: iwyu ui/gfx/geometry/rect.h

* chore: iwyu ui/gfx/image/image.h

* chore: iwyu base/strings/strcat.h

* chore: iwyu ui/native_theme/native_theme.h

* fixup! chore: iwyu shell/browser/javascript_environment.h

* fixup! chore: iwyu gfx/canvas.h

* fixup! chore: iwyu content/public/browser/browser_thread.h

* fixup! chore: iwyu ui/native_theme/native_theme.h

* fixup! chore: iwyu ui/native_theme/native_theme.h
2024-07-22 11:31:32 +02:00

193 lines
7.5 KiB
C++

// 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.
#include "shell/browser/font_defaults.h"
#include <array>
#include <string>
#include <string_view>
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/platform_locale_settings.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
#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},
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
{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
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
{prefs::kWebKitCursiveFontFamilySimplifiedHan,
IDS_CURSIVE_FONT_FAMILY_SIMPLIFIED_HAN},
{prefs::kWebKitCursiveFontFamilyTraditionalHan,
IDS_CURSIVE_FONT_FAMILY_TRADITIONAL_HAN},
#endif
#if BUILDFLAG(IS_CHROMEOS)
{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},
#elif BUILDFLAG(IS_WIN)
{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 ^^^^^
// 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) {
return l10n_util::GetStringUTF8(pref.resource_id);
}
}
return std::string{};
}
// Each font family has kWebKitScriptsForFontFamilyMapsLength scripts.
// This is a lookup array for script_index -> fontname
using PerFamilyFonts =
std::array<std::u16string, prefs::kWebKitScriptsForFontFamilyMapsLength>;
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));
}
return ret;
}
void FillFontFamilyMap(const PerFamilyFonts& cache,
blink::web_pref::ScriptFontFamilyMap& font_family_map) {
for (size_t i = 0; i < prefs::kWebKitScriptsForFontFamilyMapsLength; ++i) {
if (const std::u16string& fontname = cache[i]; !fontname.empty()) {
char const* const script_name = prefs::kWebKitScriptsForFontFamilyMaps[i];
font_family_map[script_name] = fontname;
}
}
}
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;
}
} // namespace
namespace electron {
void SetFontDefaults(blink::web_pref::WebPreferences* prefs) {
static auto const cache = MakeCache();
for (FamilyCache const& row : cache) {
FillFontFamilyMap(row.fonts, prefs->*row.map_offset);
}
}
} // namespace electron