OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_impl.h" | 5 #include "ui/gfx/font_list_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "ui/gfx/font.h" | |
15 #include "ui/gfx/font_list.h" | 14 #include "ui/gfx/font_list.h" |
16 | 15 |
17 namespace { | 16 namespace { |
18 | 17 |
19 // Returns a font description from |families|, |style|, and |size_pixels|. | 18 // Returns a font description from |families|, |style|, and |size_pixels|. |
20 std::string BuildDescription(const std::vector<std::string>& families, | 19 std::string BuildDescription(const std::vector<std::string>& families, |
21 int style, | 20 int style, |
22 int size_pixels) { | 21 int size_pixels, |
22 int weight) { | |
sky
2016/03/21 17:13:22
Can this takes a Font::FontWeight?
| |
23 std::string description = base::JoinString(families, ","); | 23 std::string description = base::JoinString(families, ","); |
24 description += ","; | 24 description += ","; |
25 | 25 |
26 if (style & gfx::Font::BOLD) | |
27 description += "Bold "; | |
28 if (style & gfx::Font::ITALIC) | 26 if (style & gfx::Font::ITALIC) |
29 description += "Italic "; | 27 description += "Italic "; |
28 switch (static_cast<gfx::Font::FontWeight>(weight)) { | |
29 case gfx::Font::FontWeight::WEIGHT_THIN: | |
30 description += "Thin "; | |
31 break; | |
32 case gfx::Font::FontWeight::WEIGHT_EXTRA_LIGHT: | |
33 description += "Ultra-Light "; | |
34 break; | |
35 case gfx::Font::FontWeight::WEIGHT_LIGHT: | |
36 description += "Light "; | |
37 break; | |
38 case gfx::Font::FontWeight::WEIGHT_NORMAL: | |
39 description += "Normal "; | |
msw
2016/03/22 01:53:43
Can/should we omit normal here?
Mikus
2016/03/22 14:19:51
I guess there'd be nothing wrong with that.
| |
40 break; | |
41 case gfx::Font::FontWeight::WEIGHT_MEDIUM: | |
42 description += "Medium "; | |
43 break; | |
44 case gfx::Font::FontWeight::WEIGHT_SEMIBOLD: | |
45 description += "Semi-Bold "; | |
46 break; | |
47 case gfx::Font::FontWeight::WEIGHT_BOLD: | |
48 description += "Bold "; | |
49 break; | |
50 case gfx::Font::FontWeight::WEIGHT_EXTRA_BOLD: | |
51 description += "Ultra-Bold "; | |
52 break; | |
53 case gfx::Font::FontWeight::WEIGHT_BLACK: | |
54 description += "Heavy "; | |
55 break; | |
56 default: | |
57 break; | |
58 } | |
30 | 59 |
31 description += base::IntToString(size_pixels); | 60 description += base::IntToString(size_pixels); |
32 description += "px"; | 61 description += "px "; |
62 | |
msw
2016/03/22 01:53:43
nit: remove blank line
| |
63 description += base::IntToString(weight); | |
33 | 64 |
34 return description; | 65 return description; |
35 } | 66 } |
36 | 67 |
37 } // namespace | 68 } // namespace |
38 | 69 |
39 namespace gfx { | 70 namespace gfx { |
40 | 71 |
41 FontListImpl::FontListImpl(const std::string& font_description_string) | 72 FontListImpl::FontListImpl(const std::string& font_description_string) |
42 : font_description_string_(font_description_string), | 73 : font_description_string_(font_description_string), |
43 common_height_(-1), | 74 common_height_(-1), |
44 common_baseline_(-1), | 75 common_baseline_(-1), |
45 font_style_(-1), | 76 font_style_(-1), |
46 font_size_(-1) { | 77 font_size_(-1), |
78 font_weight_(gfx::Font::WEIGHT_INVALID) { | |
47 DCHECK(!font_description_string.empty()); | 79 DCHECK(!font_description_string.empty()); |
48 // DCHECK description string ends with "px" for size in pixel. | 80 // DCHECK description string ends with "px" for size in pixel. |
49 DCHECK(base::EndsWith(font_description_string, "px", | 81 DCHECK(base::EndsWith(font_description_string, "px", |
50 base::CompareCase::SENSITIVE)); | 82 base::CompareCase::SENSITIVE)); |
51 } | 83 } |
52 | 84 |
53 FontListImpl::FontListImpl(const std::vector<std::string>& font_names, | 85 FontListImpl::FontListImpl(const std::vector<std::string>& font_names, |
54 int font_style, | 86 int font_style, |
55 int font_size) | 87 int font_size, |
56 : font_description_string_(BuildDescription(font_names, font_style, | 88 int font_weight) |
57 font_size)), | 89 : font_description_string_( |
90 BuildDescription(font_names, font_style, font_size, font_weight)), | |
58 common_height_(-1), | 91 common_height_(-1), |
59 common_baseline_(-1), | 92 common_baseline_(-1), |
60 font_style_(font_style), | 93 font_style_(font_style), |
61 font_size_(font_size) { | 94 font_size_(font_size), |
95 font_weight_(gfx::Font::WEIGHT_INVALID) { | |
msw
2016/03/22 01:53:43
Shouldn't this init to |font_weight|?
Mikus
2016/03/22 14:19:51
Done.
| |
62 DCHECK(!font_names.empty()); | 96 DCHECK(!font_names.empty()); |
63 DCHECK(!font_names[0].empty()); | 97 DCHECK(!font_names[0].empty()); |
64 } | 98 } |
65 | 99 |
66 FontListImpl::FontListImpl(const std::vector<Font>& fonts) | 100 FontListImpl::FontListImpl(const std::vector<Font>& fonts) |
67 : fonts_(fonts), | 101 : fonts_(fonts), |
68 common_height_(-1), | 102 common_height_(-1), |
69 common_baseline_(-1), | 103 common_baseline_(-1), |
70 font_style_(-1), | 104 font_style_(-1), |
71 font_size_(-1) { | 105 font_size_(-1), |
106 font_weight_(gfx::Font::WEIGHT_INVALID) { | |
72 DCHECK(!fonts.empty()); | 107 DCHECK(!fonts.empty()); |
73 font_style_ = fonts[0].GetStyle(); | 108 font_style_ = fonts[0].GetStyle(); |
74 font_size_ = fonts[0].GetFontSize(); | 109 font_size_ = fonts[0].GetFontSize(); |
110 font_weight_ = fonts[0].GetWeight(); | |
75 #if DCHECK_IS_ON() | 111 #if DCHECK_IS_ON() |
76 for (size_t i = 1; i < fonts.size(); ++i) { | 112 for (size_t i = 1; i < fonts.size(); ++i) { |
77 DCHECK_EQ(fonts[i].GetStyle(), font_style_); | 113 DCHECK_EQ(fonts[i].GetStyle(), font_style_); |
78 DCHECK_EQ(fonts[i].GetFontSize(), font_size_); | 114 DCHECK_EQ(fonts[i].GetFontSize(), font_size_); |
79 } | 115 } |
80 #endif | 116 #endif |
81 } | 117 } |
82 | 118 |
83 FontListImpl::FontListImpl(const Font& font) | 119 FontListImpl::FontListImpl(const Font& font) |
84 : common_height_(-1), | 120 : common_height_(-1), |
85 common_baseline_(-1), | 121 common_baseline_(-1), |
86 font_style_(-1), | 122 font_style_(-1), |
87 font_size_(-1) { | 123 font_size_(-1), |
124 font_weight_(gfx::Font::WEIGHT_INVALID) { | |
88 fonts_.push_back(font); | 125 fonts_.push_back(font); |
89 } | 126 } |
90 | 127 |
91 FontListImpl* FontListImpl::Derive(int size_delta, int font_style) const { | 128 FontListImpl* FontListImpl::Derive(int size_delta, |
129 int font_style, | |
130 gfx::Font::FontWeight weight) const { | |
92 // If there is a font vector, derive from that. | 131 // If there is a font vector, derive from that. |
93 if (!fonts_.empty()) { | 132 if (!fonts_.empty()) { |
94 std::vector<Font> fonts = fonts_; | 133 std::vector<Font> fonts = fonts_; |
95 for (size_t i = 0; i < fonts.size(); ++i) | 134 for (size_t i = 0; i < fonts.size(); ++i) |
96 fonts[i] = fonts[i].Derive(size_delta, font_style); | 135 fonts[i] = fonts[i].Derive(size_delta, font_style, weight); |
97 return new FontListImpl(fonts); | 136 return new FontListImpl(fonts); |
98 } | 137 } |
99 | 138 |
100 // Otherwise, parse the font description string to derive from it. | 139 // Otherwise, parse the font description string to derive from it. |
101 std::vector<std::string> font_names; | 140 std::vector<std::string> font_names; |
102 int old_size; | 141 int old_size; |
103 int old_style; | 142 int old_style; |
msw
2016/03/22 01:53:43
I find it weird that this code ignores the descrip
Mikus
2016/03/22 14:19:51
It's just like with old_style, it's ignored as wel
msw
2016/03/22 18:24:10
Acknowledged; the current style and weight don't m
| |
143 gfx::Font::FontWeight old_weight; | |
104 CHECK(FontList::ParseDescription(font_description_string_, &font_names, | 144 CHECK(FontList::ParseDescription(font_description_string_, &font_names, |
105 &old_style, &old_size)); | 145 &old_style, &old_size, &old_weight)); |
106 const int size = std::max(1, old_size + size_delta); | 146 const int size = std::max(1, old_size + size_delta); |
107 return new FontListImpl(font_names, font_style, size); | 147 return new FontListImpl(font_names, font_style, size, weight); |
108 } | 148 } |
109 | 149 |
110 int FontListImpl::GetHeight() const { | 150 int FontListImpl::GetHeight() const { |
111 if (common_height_ == -1) | 151 if (common_height_ == -1) |
112 CacheCommonFontHeightAndBaseline(); | 152 CacheCommonFontHeightAndBaseline(); |
113 return common_height_; | 153 return common_height_; |
114 } | 154 } |
115 | 155 |
116 int FontListImpl::GetBaseline() const { | 156 int FontListImpl::GetBaseline() const { |
117 if (common_baseline_ == -1) | 157 if (common_baseline_ == -1) |
(...skipping 16 matching lines...) Expand all Loading... | |
134 CacheFontStyleAndSize(); | 174 CacheFontStyleAndSize(); |
135 return font_style_; | 175 return font_style_; |
136 } | 176 } |
137 | 177 |
138 int FontListImpl::GetFontSize() const { | 178 int FontListImpl::GetFontSize() const { |
139 if (font_size_ == -1) | 179 if (font_size_ == -1) |
140 CacheFontStyleAndSize(); | 180 CacheFontStyleAndSize(); |
141 return font_size_; | 181 return font_size_; |
142 } | 182 } |
143 | 183 |
184 gfx::Font::FontWeight FontListImpl::GetFontWeight() const { | |
185 if (font_weight_ == gfx::Font::WEIGHT_INVALID) | |
186 CacheFontStyleAndSize(); | |
187 return font_weight_; | |
188 } | |
189 | |
144 const std::vector<Font>& FontListImpl::GetFonts() const { | 190 const std::vector<Font>& FontListImpl::GetFonts() const { |
145 if (fonts_.empty()) { | 191 if (fonts_.empty()) { |
146 DCHECK(!font_description_string_.empty()); | 192 DCHECK(!font_description_string_.empty()); |
147 | 193 |
148 std::vector<std::string> font_names; | 194 std::vector<std::string> font_names; |
149 // It's possible that gfx::Font::UNDERLINE is specified and it's already | 195 // It's possible that gfx::Font::UNDERLINE is specified and it's already |
150 // stored in |font_style_| but |font_description_string_| doesn't have the | 196 // stored in |font_style_| but |font_description_string_| doesn't have the |
151 // underline info. So we should respect |font_style_| as long as it's | 197 // underline info. So we should respect |font_style_| as long as it's |
152 // valid. | 198 // valid. |
153 int style = 0; | 199 int style = 0; |
154 CHECK(FontList::ParseDescription(font_description_string_, &font_names, | 200 CHECK(FontList::ParseDescription(font_description_string_, &font_names, |
155 &style, &font_size_)); | 201 &style, &font_size_, &font_weight_)); |
156 if (font_style_ == -1) | 202 if (font_style_ == -1) |
157 font_style_ = style; | 203 font_style_ = style; |
158 for (size_t i = 0; i < font_names.size(); ++i) { | 204 for (size_t i = 0; i < font_names.size(); ++i) { |
159 DCHECK(!font_names[i].empty()); | 205 DCHECK(!font_names[i].empty()); |
160 | 206 |
161 Font font(font_names[i], font_size_); | 207 Font font(font_names[i], font_size_); |
162 if (font_style_ == Font::NORMAL) | 208 if (font_style_ == Font::NORMAL) |
163 fonts_.push_back(font); | 209 fonts_.push_back(font); |
164 else | 210 else |
165 fonts_.push_back(font.Derive(0, font_style_)); | 211 fonts_.push_back(font.Derive(0, font_style_, font_weight_)); |
166 } | 212 } |
167 } | 213 } |
168 return fonts_; | 214 return fonts_; |
169 } | 215 } |
170 | 216 |
171 const Font& FontListImpl::GetPrimaryFont() const { | 217 const Font& FontListImpl::GetPrimaryFont() const { |
172 return GetFonts()[0]; | 218 return GetFonts()[0]; |
173 } | 219 } |
174 | 220 |
175 FontListImpl::~FontListImpl() {} | 221 FontListImpl::~FontListImpl() {} |
176 | 222 |
177 void FontListImpl::CacheCommonFontHeightAndBaseline() const { | 223 void FontListImpl::CacheCommonFontHeightAndBaseline() const { |
178 int ascent = 0; | 224 int ascent = 0; |
179 int descent = 0; | 225 int descent = 0; |
180 const std::vector<Font>& fonts = GetFonts(); | 226 const std::vector<Font>& fonts = GetFonts(); |
181 for (std::vector<Font>::const_iterator i = fonts.begin(); | 227 for (std::vector<Font>::const_iterator i = fonts.begin(); |
182 i != fonts.end(); ++i) { | 228 i != fonts.end(); ++i) { |
183 ascent = std::max(ascent, i->GetBaseline()); | 229 ascent = std::max(ascent, i->GetBaseline()); |
184 descent = std::max(descent, i->GetHeight() - i->GetBaseline()); | 230 descent = std::max(descent, i->GetHeight() - i->GetBaseline()); |
185 } | 231 } |
186 common_height_ = ascent + descent; | 232 common_height_ = ascent + descent; |
187 common_baseline_ = ascent; | 233 common_baseline_ = ascent; |
188 } | 234 } |
189 | 235 |
190 void FontListImpl::CacheFontStyleAndSize() const { | 236 void FontListImpl::CacheFontStyleAndSize() const { |
191 if (!fonts_.empty()) { | 237 if (!fonts_.empty()) { |
192 font_style_ = fonts_[0].GetStyle(); | 238 font_style_ = fonts_[0].GetStyle(); |
193 font_size_ = fonts_[0].GetFontSize(); | 239 font_size_ = fonts_[0].GetFontSize(); |
240 font_weight_ = fonts_[0].GetWeight(); | |
194 } else { | 241 } else { |
195 std::vector<std::string> font_names; | 242 std::vector<std::string> font_names; |
196 CHECK(FontList::ParseDescription(font_description_string_, &font_names, | 243 CHECK(FontList::ParseDescription(font_description_string_, &font_names, |
197 &font_style_, &font_size_)); | 244 &font_style_, &font_size_, &font_weight_)); |
198 } | 245 } |
199 } | 246 } |
200 | 247 |
201 } // namespace gfx | 248 } // namespace gfx |
OLD | NEW |