font_list.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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
  3. // found in the LICENSE file.
  4. #include "ui/gfx/font_list.h"
  5. #include <ostream>
  6. #include "base/lazy_instance.h"
  7. #include "base/strings/string_number_conversions.h"
  8. #include "base/strings/string_split.h"
  9. #include "base/strings/string_util.h"
  10. #include "build/build_config.h"
  11. #include "third_party/skia/include/core/SkFontMgr.h"
  12. #include "third_party/skia/include/core/SkTypeface.h"
  13. #include "ui/gfx/font_list_impl.h"
  14. namespace {
  15. // Font description of the default font set.
  16. base::LazyInstance<std::string>::Leaky g_default_font_description =
  17. LAZY_INSTANCE_INITIALIZER;
  18. // The default instance of gfx::FontListImpl.
  19. base::LazyInstance<scoped_refptr<gfx::FontListImpl>>::Leaky g_default_impl =
  20. LAZY_INSTANCE_INITIALIZER;
  21. bool g_default_impl_initialized = false;
  22. bool IsFontFamilyAvailable(const std::string& family, SkFontMgr* fontManager) {
  23. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  24. return !!fontManager->legacyMakeTypeface(family.c_str(), SkFontStyle());
  25. #else
  26. sk_sp<SkFontStyleSet> set(fontManager->matchFamily(family.c_str()));
  27. return set && set->count();
  28. #endif
  29. }
  30. } // namespace
  31. namespace gfx {
  32. // static
  33. bool FontList::ParseDescription(const std::string& description,
  34. std::vector<std::string>* families_out,
  35. int* style_out,
  36. int* size_pixels_out,
  37. Font::Weight* weight_out) {
  38. DCHECK(families_out);
  39. DCHECK(style_out);
  40. DCHECK(size_pixels_out);
  41. DCHECK(weight_out);
  42. *families_out = base::SplitString(
  43. description, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
  44. if (families_out->empty())
  45. return false;
  46. for (auto& family : *families_out)
  47. base::TrimWhitespaceASCII(family, base::TRIM_ALL, &family);
  48. // The last item is "[STYLE1] [STYLE2] [...] SIZE".
  49. std::vector<std::string> styles = base::SplitString(
  50. families_out->back(), base::kWhitespaceASCII,
  51. base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  52. families_out->pop_back();
  53. if (styles.empty())
  54. return false;
  55. // The size takes the form "<INT>px".
  56. std::string size_string = styles.back();
  57. styles.pop_back();
  58. if (!base::EndsWith(size_string, "px", base::CompareCase::SENSITIVE))
  59. return false;
  60. size_string.resize(size_string.size() - 2);
  61. if (!base::StringToInt(size_string, size_pixels_out) ||
  62. *size_pixels_out <= 0)
  63. return false;
  64. // Font supports ITALIC and weights; underline is supported via RenderText.
  65. *style_out = Font::NORMAL;
  66. *weight_out = Font::Weight::NORMAL;
  67. for (const auto& style_string : styles) {
  68. if (style_string == "Italic")
  69. *style_out |= Font::ITALIC;
  70. else if (style_string == "Thin")
  71. *weight_out = Font::Weight::THIN;
  72. else if (style_string == "Ultra-Light")
  73. *weight_out = Font::Weight::EXTRA_LIGHT;
  74. else if (style_string == "Light")
  75. *weight_out = Font::Weight::LIGHT;
  76. else if (style_string == "Normal")
  77. *weight_out = Font::Weight::NORMAL;
  78. else if (style_string == "Medium")
  79. *weight_out = Font::Weight::MEDIUM;
  80. else if (style_string == "Semi-Bold")
  81. *weight_out = Font::Weight::SEMIBOLD;
  82. else if (style_string == "Bold")
  83. *weight_out = Font::Weight::BOLD;
  84. else if (style_string == "Ultra-Bold")
  85. *weight_out = Font::Weight::EXTRA_BOLD;
  86. else if (style_string == "Heavy")
  87. *weight_out = Font::Weight::BLACK;
  88. else
  89. return false;
  90. }
  91. return true;
  92. }
  93. FontList::FontList() : impl_(GetDefaultImpl()) {}
  94. FontList::FontList(const FontList& other) : impl_(other.impl_) {}
  95. FontList::FontList(const std::string& font_description_string)
  96. : impl_(new FontListImpl(font_description_string)) {}
  97. FontList::FontList(const std::vector<std::string>& font_names,
  98. int font_style,
  99. int font_size,
  100. Font::Weight font_weight)
  101. : impl_(new FontListImpl(font_names, font_style, font_size, font_weight)) {}
  102. FontList::FontList(const std::vector<Font>& fonts)
  103. : impl_(new FontListImpl(fonts)) {}
  104. FontList::FontList(const Font& font) : impl_(new FontListImpl(font)) {}
  105. FontList::~FontList() {}
  106. FontList& FontList::operator=(const FontList& other) {
  107. impl_ = other.impl_;
  108. return *this;
  109. }
  110. // static
  111. void FontList::SetDefaultFontDescription(const std::string& font_description) {
  112. // The description string must end with "px" for size in pixel, or must be
  113. // the empty string, which specifies to use a single default font.
  114. DCHECK(font_description.empty() ||
  115. base::EndsWith(font_description, "px", base::CompareCase::SENSITIVE));
  116. g_default_font_description.Get() = font_description;
  117. g_default_impl_initialized = false;
  118. }
  119. FontList FontList::Derive(int size_delta,
  120. int font_style,
  121. Font::Weight weight) const {
  122. return FontList(impl_->Derive(size_delta, font_style, weight));
  123. }
  124. FontList FontList::DeriveWithSizeDelta(int size_delta) const {
  125. return Derive(size_delta, GetFontStyle(), GetFontWeight());
  126. }
  127. FontList FontList::DeriveWithStyle(int font_style) const {
  128. return Derive(0, font_style, GetFontWeight());
  129. }
  130. FontList FontList::DeriveWithWeight(Font::Weight weight) const {
  131. return Derive(0, GetFontStyle(), weight);
  132. }
  133. FontList FontList::DeriveWithHeightUpperBound(int height) const {
  134. FontList font_list(*this);
  135. for (int font_size = font_list.GetFontSize(); font_size > 1; --font_size) {
  136. const int internal_leading =
  137. font_list.GetBaseline() - font_list.GetCapHeight();
  138. // Some platforms don't support getting the cap height, and simply return
  139. // the entire font ascent from GetCapHeight(). Centering the ascent makes
  140. // the font look too low, so if GetCapHeight() returns the ascent, center
  141. // the entire font height instead.
  142. const int space =
  143. height - ((internal_leading != 0) ?
  144. font_list.GetCapHeight() : font_list.GetHeight());
  145. const int y_offset = space / 2 - internal_leading;
  146. const int space_at_bottom = height - (y_offset + font_list.GetHeight());
  147. if ((y_offset >= 0) && (space_at_bottom >= 0))
  148. break;
  149. font_list = font_list.DeriveWithSizeDelta(-1);
  150. }
  151. return font_list;
  152. }
  153. int FontList::GetHeight() const {
  154. return impl_->GetHeight();
  155. }
  156. int FontList::GetBaseline() const {
  157. return impl_->GetBaseline();
  158. }
  159. int FontList::GetCapHeight() const {
  160. return impl_->GetCapHeight();
  161. }
  162. int FontList::GetExpectedTextWidth(int length) const {
  163. return impl_->GetExpectedTextWidth(length);
  164. }
  165. int FontList::GetFontStyle() const {
  166. return impl_->GetFontStyle();
  167. }
  168. int FontList::GetFontSize() const {
  169. return impl_->GetFontSize();
  170. }
  171. Font::Weight FontList::GetFontWeight() const {
  172. return impl_->GetFontWeight();
  173. }
  174. const std::vector<Font>& FontList::GetFonts() const {
  175. return impl_->GetFonts();
  176. }
  177. const Font& FontList::GetPrimaryFont() const {
  178. return impl_->GetPrimaryFont();
  179. }
  180. FontList::FontList(FontListImpl* impl) : impl_(impl) {}
  181. // static
  182. const scoped_refptr<FontListImpl>& FontList::GetDefaultImpl() {
  183. // SetDefaultFontDescription() must be called and the default font description
  184. // must be set earlier than any call of this function.
  185. DCHECK(g_default_font_description.IsCreated())
  186. << "SetDefaultFontDescription has not been called.";
  187. if (!g_default_impl_initialized) {
  188. g_default_impl.Get() =
  189. g_default_font_description.Get().empty() ?
  190. new FontListImpl(Font()) :
  191. new FontListImpl(g_default_font_description.Get());
  192. g_default_impl_initialized = true;
  193. }
  194. return g_default_impl.Get();
  195. }
  196. // static
  197. std::string FontList::FirstAvailableOrFirst(const std::string& font_name_list) {
  198. std::vector<std::string> families = base::SplitString(
  199. font_name_list, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
  200. if (families.empty())
  201. return std::string();
  202. if (families.size() == 1)
  203. return families[0];
  204. sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
  205. for (const auto& family : families) {
  206. if (IsFontFamilyAvailable(family, fm.get()))
  207. return family;
  208. }
  209. return families[0];
  210. }
  211. } // namespace gfx