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" |
| 9 #include "base/strings/string_split.h" |
8 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
9 #include "ui/gfx/font_list_impl.h" | 11 #include "ui/gfx/font_list_impl.h" |
10 | 12 |
11 namespace { | 13 namespace { |
12 | 14 |
13 // Font description of the default font set. | 15 // Font description of the default font set. |
14 base::LazyInstance<std::string>::Leaky g_default_font_description = | 16 base::LazyInstance<std::string>::Leaky g_default_font_description = |
15 LAZY_INSTANCE_INITIALIZER; | 17 LAZY_INSTANCE_INITIALIZER; |
16 | 18 |
17 // The default instance of gfx::FontListImpl. | 19 // The default instance of gfx::FontListImpl. |
18 base::LazyInstance<scoped_refptr<gfx::FontListImpl> >::Leaky g_default_impl = | 20 base::LazyInstance<scoped_refptr<gfx::FontListImpl> >::Leaky g_default_impl = |
19 LAZY_INSTANCE_INITIALIZER; | 21 LAZY_INSTANCE_INITIALIZER; |
20 bool g_default_impl_initialized = false; | 22 bool g_default_impl_initialized = false; |
21 | 23 |
22 } // namespace | 24 } // namespace |
23 | 25 |
24 namespace gfx { | 26 namespace gfx { |
25 | 27 |
| 28 // static |
| 29 bool FontList::ParseDescription(const std::string& description, |
| 30 std::vector<std::string>* families_out, |
| 31 int* style_out, |
| 32 int* size_pixels_out) { |
| 33 DCHECK(families_out); |
| 34 DCHECK(style_out); |
| 35 DCHECK(size_pixels_out); |
| 36 |
| 37 base::SplitString(description, ',', families_out); |
| 38 if (families_out->empty()) |
| 39 return false; |
| 40 for (auto& family : *families_out) |
| 41 base::TrimWhitespaceASCII(family, base::TRIM_ALL, &family); |
| 42 |
| 43 // The last item is "[STYLE1] [STYLE2] [...] SIZE". |
| 44 std::vector<std::string> styles; |
| 45 base::SplitStringAlongWhitespace(families_out->back(), &styles); |
| 46 families_out->pop_back(); |
| 47 if (styles.empty()) |
| 48 return false; |
| 49 |
| 50 // The size takes the form "<INT>px". |
| 51 std::string size_string = styles.back(); |
| 52 styles.pop_back(); |
| 53 if (!EndsWith(size_string, "px", true /* case_sensitive */)) |
| 54 return false; |
| 55 size_string.resize(size_string.size() - 2); |
| 56 if (!base::StringToInt(size_string, size_pixels_out) || |
| 57 *size_pixels_out <= 0) |
| 58 return false; |
| 59 |
| 60 // Font supports BOLD and ITALIC; underline is supported via RenderText. |
| 61 *style_out = gfx::Font::NORMAL; |
| 62 for (const auto& style_string : styles) { |
| 63 if (style_string == "Bold") |
| 64 *style_out |= gfx::Font::BOLD; |
| 65 else if (style_string == "Italic") |
| 66 *style_out |= gfx::Font::ITALIC; |
| 67 else |
| 68 return false; |
| 69 } |
| 70 |
| 71 return true; |
| 72 } |
| 73 |
26 FontList::FontList() : impl_(GetDefaultImpl()) {} | 74 FontList::FontList() : impl_(GetDefaultImpl()) {} |
27 | 75 |
28 FontList::FontList(const FontList& other) : impl_(other.impl_) {} | 76 FontList::FontList(const FontList& other) : impl_(other.impl_) {} |
29 | 77 |
30 FontList::FontList(const std::string& font_description_string) | 78 FontList::FontList(const std::string& font_description_string) |
31 : impl_(new FontListImpl(font_description_string)) {} | 79 : impl_(new FontListImpl(font_description_string)) {} |
32 | 80 |
33 FontList::FontList(const std::vector<std::string>& font_names, | 81 FontList::FontList(const std::vector<std::string>& font_names, |
34 int font_style, | 82 int font_style, |
35 int font_size) | 83 int font_size) |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 } | 152 } |
105 | 153 |
106 int FontList::GetExpectedTextWidth(int length) const { | 154 int FontList::GetExpectedTextWidth(int length) const { |
107 return impl_->GetExpectedTextWidth(length); | 155 return impl_->GetExpectedTextWidth(length); |
108 } | 156 } |
109 | 157 |
110 int FontList::GetFontStyle() const { | 158 int FontList::GetFontStyle() const { |
111 return impl_->GetFontStyle(); | 159 return impl_->GetFontStyle(); |
112 } | 160 } |
113 | 161 |
114 const std::string& FontList::GetFontDescriptionString() const { | |
115 return impl_->GetFontDescriptionString(); | |
116 } | |
117 | |
118 int FontList::GetFontSize() const { | 162 int FontList::GetFontSize() const { |
119 return impl_->GetFontSize(); | 163 return impl_->GetFontSize(); |
120 } | 164 } |
121 | 165 |
122 const std::vector<Font>& FontList::GetFonts() const { | 166 const std::vector<Font>& FontList::GetFonts() const { |
123 return impl_->GetFonts(); | 167 return impl_->GetFonts(); |
124 } | 168 } |
125 | 169 |
126 const Font& FontList::GetPrimaryFont() const { | 170 const Font& FontList::GetPrimaryFont() const { |
127 return impl_->GetPrimaryFont(); | 171 return impl_->GetPrimaryFont(); |
(...skipping 13 matching lines...) Expand all Loading... |
141 g_default_font_description.Get().empty() ? | 185 g_default_font_description.Get().empty() ? |
142 new FontListImpl(Font()) : | 186 new FontListImpl(Font()) : |
143 new FontListImpl(g_default_font_description.Get()); | 187 new FontListImpl(g_default_font_description.Get()); |
144 g_default_impl_initialized = true; | 188 g_default_impl_initialized = true; |
145 } | 189 } |
146 | 190 |
147 return g_default_impl.Get(); | 191 return g_default_impl.Get(); |
148 } | 192 } |
149 | 193 |
150 } // namespace gfx | 194 } // namespace gfx |
OLD | NEW |