Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1085)

Unified Diff: chrome/browser/ui/webui/options/font_settings_utils.cc

Issue 2441343003: Allow the default generic font family settings to find the first available font (Closed)
Patch Set: Add font_settings_utils_unittest Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/webui/options/font_settings_utils.cc
diff --git a/chrome/browser/ui/webui/options/font_settings_utils.cc b/chrome/browser/ui/webui/options/font_settings_utils.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ca2465958767faeaaa0dd5cda1cb1ae4721bee87
--- /dev/null
+++ b/chrome/browser/ui/webui/options/font_settings_utils.cc
@@ -0,0 +1,53 @@
+// Copyright (c) 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/ui/webui/options/font_settings_utils.h"
+
+#include "base/strings/string_split.h"
+#include "third_party/skia/include/core/SkTypeface.h"
+#include "third_party/skia/include/ports/SkFontMgr.h"
+
+namespace options {
+
+bool IsFontFamilyAvailable(const std::string& family, SkFontMgr* fontManager) {
+#if defined(OS_LINUX)
+ sk_sp<SkTypeface> typeface(
+ fontManager->legacyCreateTypeface(family.c_str(), SkFontStyle()));
+ return typeface;
+#else
+ sk_sp<SkFontStyleSet> set(fontManager->matchFamily(family.c_str()));
msw 2016/10/27 00:04:18 How does this non-linux code align with Mac and Wi
kojii 2016/10/27 04:50:56 First, this function needs to return available fon
msw 2016/10/27 17:18:11 Sorry for not being clear, "GenericFont" was meant
+ return set && set->count();
+#endif
+}
+
+// Returns the first available font. If there is no available font, returns the
+// first font. Empty entries are ignored.
+std::string FirstAvailableOrFirst(const std::string& font_name_list) {
+ std::vector<std::string> families = base::SplitString(
+ font_name_list, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
+ if (families.empty())
+ return std::string();
+ sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
+ for (auto& family : families) {
+ if (IsFontFamilyAvailable(family, fm.get()))
+ return family;
+ }
+ return families[0];
+}
Dan Beam 2016/10/27 02:07:24 ^ why are we duplicating this with the blink code?
kojii 2016/10/27 04:50:56 Two reasons: 1. No good place to share the code. T
msw 2016/10/27 17:18:11 I'm not sure if ui/gfx makes sense, because this i
Dan Beam 2016/10/27 17:29:41 ui/base?
msw 2016/10/27 18:24:02 If ui/ is the right top-level dir, then gfx/ would
kojii 2016/10/28 04:59:41 Blink can't use ui/base (yet?); only ui/gfx and se
+
+std::string FontSettingsUtilities::ResolveFontList(
+ const std::string& font_name_or_list) {
+ if (!font_name_or_list.empty() && font_name_or_list[0] == ',')
+ return FirstAvailableOrFirst(font_name_or_list);
+ return font_name_or_list;
+}
+
+#if !defined(OS_WIN)
+std::string FontSettingsUtilities::MaybeGetLocalizedFontName(
+ const std::string& font_name_or_list) {
+ return ResolveFontList(font_name_or_list);
+}
+#endif
+
+} // namespace options

Powered by Google App Engine
This is Rietveld 408576698