| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/font_list.h" | 5 #include "ui/gfx/font_list.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "third_party/skia/include/core/SkTypeface.h" |
| 12 #include "third_party/skia/include/ports/SkFontMgr.h" |
| 11 #include "ui/gfx/font_list_impl.h" | 13 #include "ui/gfx/font_list_impl.h" |
| 12 | 14 |
| 13 namespace { | 15 namespace { |
| 14 | 16 |
| 15 // Font description of the default font set. | 17 // Font description of the default font set. |
| 16 base::LazyInstance<std::string>::Leaky g_default_font_description = | 18 base::LazyInstance<std::string>::Leaky g_default_font_description = |
| 17 LAZY_INSTANCE_INITIALIZER; | 19 LAZY_INSTANCE_INITIALIZER; |
| 18 | 20 |
| 19 // The default instance of gfx::FontListImpl. | 21 // The default instance of gfx::FontListImpl. |
| 20 base::LazyInstance<scoped_refptr<gfx::FontListImpl> >::Leaky g_default_impl = | 22 base::LazyInstance<scoped_refptr<gfx::FontListImpl> >::Leaky g_default_impl = |
| 21 LAZY_INSTANCE_INITIALIZER; | 23 LAZY_INSTANCE_INITIALIZER; |
| 22 bool g_default_impl_initialized = false; | 24 bool g_default_impl_initialized = false; |
| 23 | 25 |
| 26 bool IsFontFamilyAvailable(const std::string& family, SkFontMgr* fontManager) { |
| 27 #if defined(OS_LINUX) |
| 28 sk_sp<SkTypeface> typeface( |
| 29 fontManager->legacyCreateTypeface(family.c_str(), SkFontStyle())); |
| 30 return typeface; |
| 31 #else |
| 32 sk_sp<SkFontStyleSet> set(fontManager->matchFamily(family.c_str())); |
| 33 return set && set->count(); |
| 34 #endif |
| 35 } |
| 36 |
| 24 } // namespace | 37 } // namespace |
| 25 | 38 |
| 26 namespace gfx { | 39 namespace gfx { |
| 27 | 40 |
| 28 // static | 41 // static |
| 29 bool FontList::ParseDescription(const std::string& description, | 42 bool FontList::ParseDescription(const std::string& description, |
| 30 std::vector<std::string>* families_out, | 43 std::vector<std::string>* families_out, |
| 31 int* style_out, | 44 int* style_out, |
| 32 int* size_pixels_out, | 45 int* size_pixels_out, |
| 33 Font::Weight* weight_out) { | 46 Font::Weight* weight_out) { |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 g_default_impl.Get() = | 229 g_default_impl.Get() = |
| 217 g_default_font_description.Get().empty() ? | 230 g_default_font_description.Get().empty() ? |
| 218 new FontListImpl(Font()) : | 231 new FontListImpl(Font()) : |
| 219 new FontListImpl(g_default_font_description.Get()); | 232 new FontListImpl(g_default_font_description.Get()); |
| 220 g_default_impl_initialized = true; | 233 g_default_impl_initialized = true; |
| 221 } | 234 } |
| 222 | 235 |
| 223 return g_default_impl.Get(); | 236 return g_default_impl.Get(); |
| 224 } | 237 } |
| 225 | 238 |
| 239 // static |
| 240 std::string FontList::FirstAvailableOrFirst(const std::string& font_name_list) { |
| 241 std::vector<std::string> families = base::SplitString( |
| 242 font_name_list, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 243 if (families.empty()) |
| 244 return std::string(); |
| 245 if (families.size() == 1) |
| 246 return families[0]; |
| 247 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 248 for (const auto& family : families) { |
| 249 if (IsFontFamilyAvailable(family, fm.get())) |
| 250 return family; |
| 251 } |
| 252 return families[0]; |
| 253 } |
| 254 |
| 226 } // namespace gfx | 255 } // namespace gfx |
| OLD | NEW |