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

Unified Diff: ui/gfx/font.cc

Issue 2441343003: Allow the default generic font family settings to find the first available font (Closed)
Patch Set: Add more tests 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..b657cde7bb0253b4b024d0ecedb06940fcd33a02 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,30 @@ NativeFont Font::GetNativeFont() const {
}
#endif
+bool IsFontFamilyAvailable(const std::string& family, SkFontMgr* fontManager) {
+#if defined(OS_LINUX)
+ sk_sp<SkTypeface> typeface(
+ fontManager->legacyCreateTypeface(family.c_str(), SkFontStyle()));
msw 2016/10/26 20:33:49 Why are we using a legacy skia codepath here and a
kojii 2016/10/26 22:20:03 Because if I create gfx::Font("that does not exist
msw 2016/10/27 00:04:18 Since FontList::ParseDescription requires a size,
+ return typeface;
+#else
+ sk_sp<SkFontStyleSet> sset(fontManager->matchFamily(family.c_str()));
msw 2016/10/26 20:33:49 nit: |style_set| or just |set|
kojii 2016/10/26 22:20:03 Done.
+ return sset && sset->count();
+#endif
+}
+
+std::string Font::FirstAvailableOrFirst(const std::string& font_name_list) {
+ std::vector<std::string> families = base::SplitString(
msw 2016/10/26 20:33:49 Could we use gfx::FontList's constructor that take
kojii 2016/10/26 22:20:03 Hmm...FontList::ParseDescription() needs size, and
msw 2016/10/27 00:04:18 Hmm, perhaps derat or asvitkine would have a bette
+ 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];
+}
+
#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