Chromium Code Reviews| 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" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 bool g_default_impl_initialized = false; | 22 bool g_default_impl_initialized = false; |
| 23 | 23 |
| 24 } // namespace | 24 } // namespace |
| 25 | 25 |
| 26 namespace gfx { | 26 namespace gfx { |
| 27 | 27 |
| 28 // static | 28 // static |
| 29 bool FontList::ParseDescription(const std::string& description, | 29 bool FontList::ParseDescription(const std::string& description, |
| 30 std::vector<std::string>* families_out, | 30 std::vector<std::string>* families_out, |
| 31 int* style_out, | 31 int* style_out, |
| 32 int* size_pixels_out) { | 32 int* size_pixels_out, |
| 33 gfx::Font::Weight* weight_out) { | |
| 33 DCHECK(families_out); | 34 DCHECK(families_out); |
| 34 DCHECK(style_out); | 35 DCHECK(style_out); |
| 35 DCHECK(size_pixels_out); | 36 DCHECK(size_pixels_out); |
| 37 DCHECK(weight_out); | |
| 36 | 38 |
| 37 *families_out = base::SplitString( | 39 *families_out = base::SplitString( |
| 38 description, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 40 description, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 39 if (families_out->empty()) | 41 if (families_out->empty()) |
| 40 return false; | 42 return false; |
| 41 for (auto& family : *families_out) | 43 for (auto& family : *families_out) |
| 42 base::TrimWhitespaceASCII(family, base::TRIM_ALL, &family); | 44 base::TrimWhitespaceASCII(family, base::TRIM_ALL, &family); |
| 43 | 45 |
| 44 // The last item is "[STYLE1] [STYLE2] [...] SIZE". | 46 // The last item is "[STYLE1] [STYLE2] [...] SIZE". |
| 45 std::vector<std::string> styles = base::SplitString( | 47 std::vector<std::string> styles = base::SplitString( |
| 46 families_out->back(), base::kWhitespaceASCII, | 48 families_out->back(), base::kWhitespaceASCII, |
| 47 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 49 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 48 families_out->pop_back(); | 50 families_out->pop_back(); |
| 49 if (styles.empty()) | 51 if (styles.empty()) |
| 50 return false; | 52 return false; |
| 51 | 53 |
| 52 // The size takes the form "<INT>px". | 54 // The size takes the form "<INT>px". |
| 53 std::string size_string = styles.back(); | 55 std::string size_string = styles.back(); |
| 54 styles.pop_back(); | 56 styles.pop_back(); |
| 55 if (!base::EndsWith(size_string, "px", base::CompareCase::SENSITIVE)) | 57 if (!base::EndsWith(size_string, "px", base::CompareCase::SENSITIVE)) |
| 56 return false; | 58 return false; |
| 57 size_string.resize(size_string.size() - 2); | 59 size_string.resize(size_string.size() - 2); |
| 58 if (!base::StringToInt(size_string, size_pixels_out) || | 60 if (!base::StringToInt(size_string, size_pixels_out) || |
| 59 *size_pixels_out <= 0) | 61 *size_pixels_out <= 0) |
| 60 return false; | 62 return false; |
| 61 | 63 |
| 62 // Font supports BOLD and ITALIC; underline is supported via RenderText. | 64 // Font supports ITALIC and weights; underline is supported via RenderText. |
| 63 *style_out = gfx::Font::NORMAL; | 65 *style_out = gfx::Font::NORMAL; |
|
Alexei Svitkine (slow)
2016/04/05 16:38:50
Remove gfx:: here and on the lines below.
| |
| 66 *weight_out = gfx::Font::Weight::NORMAL; | |
| 64 for (const auto& style_string : styles) { | 67 for (const auto& style_string : styles) { |
| 65 if (style_string == "Bold") | 68 if (style_string == "Italic") |
| 66 *style_out |= gfx::Font::BOLD; | |
| 67 else if (style_string == "Italic") | |
| 68 *style_out |= gfx::Font::ITALIC; | 69 *style_out |= gfx::Font::ITALIC; |
| 70 else if (style_string == "Thin") | |
| 71 *weight_out = gfx::Font::Weight::THIN; | |
| 72 else if (style_string == "Ultra-Light") | |
| 73 *weight_out = gfx::Font::Weight::EXTRA_LIGHT; | |
| 74 else if (style_string == "Light") | |
| 75 *weight_out = gfx::Font::Weight::LIGHT; | |
| 76 else if (style_string == "Normal") | |
| 77 *weight_out = gfx::Font::Weight::NORMAL; | |
| 78 else if (style_string == "Medium") | |
| 79 *weight_out = gfx::Font::Weight::MEDIUM; | |
| 80 else if (style_string == "Semi-Bold") | |
| 81 *weight_out = gfx::Font::Weight::SEMIBOLD; | |
| 82 else if (style_string == "Bold") | |
| 83 *weight_out = gfx::Font::Weight::BOLD; | |
| 84 else if (style_string == "Ultra-Bold") | |
| 85 *weight_out = gfx::Font::Weight::EXTRA_BOLD; | |
| 86 else if (style_string == "Heavy") | |
| 87 *weight_out = gfx::Font::Weight::BLACK; | |
| 69 else | 88 else |
| 70 return false; | 89 return false; |
| 71 } | 90 } |
| 72 | 91 |
| 73 return true; | 92 return true; |
| 74 } | 93 } |
| 75 | 94 |
| 76 FontList::FontList() : impl_(GetDefaultImpl()) {} | 95 FontList::FontList() : impl_(GetDefaultImpl()) {} |
| 77 | 96 |
| 78 FontList::FontList(const FontList& other) : impl_(other.impl_) {} | 97 FontList::FontList(const FontList& other) : impl_(other.impl_) {} |
| 79 | 98 |
| 80 FontList::FontList(const std::string& font_description_string) | 99 FontList::FontList(const std::string& font_description_string) |
| 81 : impl_(new FontListImpl(font_description_string)) {} | 100 : impl_(new FontListImpl(font_description_string)) {} |
| 82 | 101 |
| 83 FontList::FontList(const std::vector<std::string>& font_names, | 102 FontList::FontList(const std::vector<std::string>& font_names, |
| 84 int font_style, | 103 int font_style, |
| 85 int font_size) | 104 int font_size, |
| 86 : impl_(new FontListImpl(font_names, font_style, font_size)) {} | 105 Font::Weight font_weight) |
| 106 : impl_(new FontListImpl(font_names, font_style, font_size, font_weight)) {} | |
| 87 | 107 |
| 88 FontList::FontList(const std::vector<Font>& fonts) | 108 FontList::FontList(const std::vector<Font>& fonts) |
| 89 : impl_(new FontListImpl(fonts)) {} | 109 : impl_(new FontListImpl(fonts)) {} |
| 90 | 110 |
| 91 FontList::FontList(const Font& font) : impl_(new FontListImpl(font)) {} | 111 FontList::FontList(const Font& font) : impl_(new FontListImpl(font)) {} |
| 92 | 112 |
| 93 FontList::~FontList() {} | 113 FontList::~FontList() {} |
| 94 | 114 |
| 95 FontList& FontList::operator=(const FontList& other) { | 115 FontList& FontList::operator=(const FontList& other) { |
| 96 impl_ = other.impl_; | 116 impl_ = other.impl_; |
| 97 return *this; | 117 return *this; |
| 98 } | 118 } |
| 99 | 119 |
| 100 // static | 120 // static |
| 101 void FontList::SetDefaultFontDescription(const std::string& font_description) { | 121 void FontList::SetDefaultFontDescription(const std::string& font_description) { |
| 102 // The description string must end with "px" for size in pixel, or must be | 122 // The description string must end with "px" for size in pixel, or must be |
| 103 // the empty string, which specifies to use a single default font. | 123 // the empty string, which specifies to use a single default font. |
| 104 DCHECK(font_description.empty() || | 124 DCHECK(font_description.empty() || |
| 105 base::EndsWith(font_description, "px", base::CompareCase::SENSITIVE)); | 125 base::EndsWith(font_description, "px", base::CompareCase::SENSITIVE)); |
| 106 | 126 |
| 107 g_default_font_description.Get() = font_description; | 127 g_default_font_description.Get() = font_description; |
| 108 g_default_impl_initialized = false; | 128 g_default_impl_initialized = false; |
| 109 } | 129 } |
| 110 | 130 |
| 111 FontList FontList::Derive(int size_delta, int font_style) const { | 131 FontList FontList::Derive(int size_delta, |
| 112 return FontList(impl_->Derive(size_delta, font_style)); | 132 int font_style, |
| 133 gfx::Font::Weight weight) const { | |
|
Alexei Svitkine (slow)
2016/04/05 16:38:50
Remove gfx::
| |
| 134 return FontList(impl_->Derive(size_delta, font_style, weight)); | |
| 113 } | 135 } |
| 114 | 136 |
| 115 FontList FontList::DeriveWithSizeDelta(int size_delta) const { | 137 FontList FontList::DeriveWithSizeDelta(int size_delta) const { |
| 116 return Derive(size_delta, GetFontStyle()); | 138 return Derive(size_delta, GetFontStyle(), GetFontWeight()); |
| 117 } | 139 } |
| 118 | 140 |
| 119 FontList FontList::DeriveWithStyle(int font_style) const { | 141 FontList FontList::DeriveWithStyle(int font_style) const { |
| 120 return Derive(0, font_style); | 142 return Derive(0, font_style, GetFontWeight()); |
| 143 } | |
| 144 | |
| 145 FontList FontList::DeriveWithWeight(gfx::Font::Weight weight) const { | |
|
Alexei Svitkine (slow)
2016/04/05 16:38:50
Remove gfx::
| |
| 146 return Derive(0, GetFontStyle(), weight); | |
| 121 } | 147 } |
| 122 | 148 |
| 123 gfx::FontList FontList::DeriveWithHeightUpperBound(int height) const { | 149 gfx::FontList FontList::DeriveWithHeightUpperBound(int height) const { |
| 124 gfx::FontList font_list(*this); | 150 gfx::FontList font_list(*this); |
| 125 for (int font_size = font_list.GetFontSize(); font_size > 1; --font_size) { | 151 for (int font_size = font_list.GetFontSize(); font_size > 1; --font_size) { |
| 126 const int internal_leading = | 152 const int internal_leading = |
| 127 font_list.GetBaseline() - font_list.GetCapHeight(); | 153 font_list.GetBaseline() - font_list.GetCapHeight(); |
| 128 // Some platforms don't support getting the cap height, and simply return | 154 // Some platforms don't support getting the cap height, and simply return |
| 129 // the entire font ascent from GetCapHeight(). Centering the ascent makes | 155 // the entire font ascent from GetCapHeight(). Centering the ascent makes |
| 130 // the font look too low, so if GetCapHeight() returns the ascent, center | 156 // the font look too low, so if GetCapHeight() returns the ascent, center |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 158 } | 184 } |
| 159 | 185 |
| 160 int FontList::GetFontStyle() const { | 186 int FontList::GetFontStyle() const { |
| 161 return impl_->GetFontStyle(); | 187 return impl_->GetFontStyle(); |
| 162 } | 188 } |
| 163 | 189 |
| 164 int FontList::GetFontSize() const { | 190 int FontList::GetFontSize() const { |
| 165 return impl_->GetFontSize(); | 191 return impl_->GetFontSize(); |
| 166 } | 192 } |
| 167 | 193 |
| 194 gfx::Font::Weight FontList::GetFontWeight() const { | |
|
Alexei Svitkine (slow)
2016/04/05 16:38:50
Remove gfx::
| |
| 195 return impl_->GetFontWeight(); | |
| 196 } | |
| 197 | |
| 168 const std::vector<Font>& FontList::GetFonts() const { | 198 const std::vector<Font>& FontList::GetFonts() const { |
| 169 return impl_->GetFonts(); | 199 return impl_->GetFonts(); |
| 170 } | 200 } |
| 171 | 201 |
| 172 const Font& FontList::GetPrimaryFont() const { | 202 const Font& FontList::GetPrimaryFont() const { |
| 173 return impl_->GetPrimaryFont(); | 203 return impl_->GetPrimaryFont(); |
| 174 } | 204 } |
| 175 | 205 |
| 176 FontList::FontList(FontListImpl* impl) : impl_(impl) {} | 206 FontList::FontList(FontListImpl* impl) : impl_(impl) {} |
| 177 | 207 |
| 178 // static | 208 // static |
| 179 const scoped_refptr<FontListImpl>& FontList::GetDefaultImpl() { | 209 const scoped_refptr<FontListImpl>& FontList::GetDefaultImpl() { |
| 180 // SetDefaultFontDescription() must be called and the default font description | 210 // SetDefaultFontDescription() must be called and the default font description |
| 181 // must be set earlier than any call of this function. | 211 // must be set earlier than any call of this function. |
| 182 DCHECK(!(g_default_font_description == NULL)) // != is not overloaded. | 212 DCHECK(!(g_default_font_description == NULL)) // != is not overloaded. |
| 183 << "SetDefaultFontDescription has not been called."; | 213 << "SetDefaultFontDescription has not been called."; |
| 184 | 214 |
| 185 if (!g_default_impl_initialized) { | 215 if (!g_default_impl_initialized) { |
| 186 g_default_impl.Get() = | 216 g_default_impl.Get() = |
| 187 g_default_font_description.Get().empty() ? | 217 g_default_font_description.Get().empty() ? |
| 188 new FontListImpl(Font()) : | 218 new FontListImpl(Font()) : |
| 189 new FontListImpl(g_default_font_description.Get()); | 219 new FontListImpl(g_default_font_description.Get()); |
| 190 g_default_impl_initialized = true; | 220 g_default_impl_initialized = true; |
| 191 } | 221 } |
| 192 | 222 |
| 193 return g_default_impl.Get(); | 223 return g_default_impl.Get(); |
| 194 } | 224 } |
| 195 | 225 |
| 196 } // namespace gfx | 226 } // namespace gfx |
| OLD | NEW |