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

Unified Diff: ui/gfx/font.cc

Issue 2441343003: Allow the default generic font family settings to find the first available font (Closed)
Patch Set: Code shared in ui/gfx 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
« ui/gfx/font.h ('K') | « ui/gfx/font.h ('k') | ui/gfx/font_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/font.cc
diff --git a/ui/gfx/font.cc b/ui/gfx/font.cc
index ac7b7491a6cdcb98caa8e889900db693f454d9f6..e19ac7dc7aeca181fd01a567f3421066f6f41592 100644
--- a/ui/gfx/font.cc
+++ b/ui/gfx/font.cc
@@ -4,8 +4,11 @@
#include "ui/gfx/font.h"
+#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
+#include "third_party/skia/include/core/SkTypeface.h"
+#include "third_party/skia/include/ports/SkFontMgr.h"
#include "ui/gfx/platform_font.h"
namespace gfx {
@@ -91,6 +94,34 @@ NativeFont Font::GetNativeFont() const {
}
#endif
+namespace {
msw 2016/10/28 07:05:06 nit: put anon namespaces at the top of the file.
kojii 2016/10/28 07:57:30 Done.
+
+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()));
+ return set && set->count();
+#endif
+}
+
+} // namespace
+
+std::string Font::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 (const auto& family : families) {
+ if (IsFontFamilyAvailable(family, fm.get()))
msw 2016/10/28 07:05:06 Is it worth skipping this and early-returning fami
kojii 2016/10/28 07:57:30 Done, unlikely for now but still good to have it.
+ return family;
+ }
+ return families[0];
+}
+
#ifndef NDEBUG
std::ostream& operator<<(std::ostream& stream, const Font::Weight weight) {
return stream << static_cast<int>(weight);
« ui/gfx/font.h ('K') | « ui/gfx/font.h ('k') | ui/gfx/font_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698