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

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: Un-shared FirstAvailableOrFirst as per msw review 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) {
msw 2016/10/27 00:04:18 If these functions are file-local, they should go
kojii 2016/10/27 04:50:56 Done.
+#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()));
+ return set && set->count();
+#endif
+}
+
+// Returns the first available font. If there is no available font, returns the
msw 2016/10/27 00:04:18 Ditto nit: say 'font name' or 'font family'.
kojii 2016/10/27 04:50:56 Done.
+// 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) {
msw 2016/10/27 00:04:18 nit: 'const auto&'
kojii 2016/10/27 04:50:56 Done.
+ if (IsFontFamilyAvailable(family, fm.get()))
+ return family;
+ }
+ return families[0];
+}
+
+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