Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: ui/gfx/font_list_impl.cc

Issue 1819753003: Allow various font weights in gfx. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use the std::tie in resource_bundle Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 gfx::Font::Weight weight) {
Alexei Svitkine (slow) 2016/04/04 16:34:50 Nit: Move the start of "namespace gfx" above the a
Mikus 2016/04/05 16:18:34 Done.
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 (weight) {
29 case gfx::Font::Weight::THIN:
30 description += "Thin ";
31 break;
32 case gfx::Font::Weight::EXTRA_LIGHT:
33 description += "Ultra-Light ";
34 break;
35 case gfx::Font::Weight::LIGHT:
36 description += "Light ";
37 break;
38 case gfx::Font::Weight::MEDIUM:
39 description += "Medium ";
40 break;
41 case gfx::Font::Weight::SEMIBOLD:
42 description += "Semi-Bold ";
43 break;
44 case gfx::Font::Weight::BOLD:
45 description += "Bold ";
46 break;
47 case gfx::Font::Weight::EXTRA_BOLD:
48 description += "Ultra-Bold ";
49 break;
50 case gfx::Font::Weight::BLACK:
51 description += "Heavy ";
52 break;
53 default:
Alexei Svitkine (slow) 2016/04/04 16:34:50 If the switch covers all the entries in the enum,
Mikus 2016/04/05 16:18:34 Done.
54 break;
55 }
30 56
31 description += base::IntToString(size_pixels); 57 description += base::IntToString(size_pixels);
32 description += "px"; 58 description += "px ";
59 description += base::IntToString(static_cast<int>(weight));
33 60
34 return description; 61 return description;
35 } 62 }
36 63
37 } // namespace 64 } // namespace
38 65
39 namespace gfx { 66 namespace gfx {
40 67
41 FontListImpl::FontListImpl(const std::string& font_description_string) 68 FontListImpl::FontListImpl(const std::string& font_description_string)
42 : font_description_string_(font_description_string), 69 : font_description_string_(font_description_string),
43 common_height_(-1), 70 common_height_(-1),
44 common_baseline_(-1), 71 common_baseline_(-1),
45 font_style_(-1), 72 font_style_(-1),
46 font_size_(-1) { 73 font_size_(-1),
74 font_weight_(gfx::Font::Weight::INVALID) {
47 DCHECK(!font_description_string.empty()); 75 DCHECK(!font_description_string.empty());
48 // DCHECK description string ends with "px" for size in pixel. 76 // DCHECK description string ends with "px" for size in pixel.
49 DCHECK(base::EndsWith(font_description_string, "px", 77 DCHECK(base::EndsWith(font_description_string, "px",
50 base::CompareCase::SENSITIVE)); 78 base::CompareCase::SENSITIVE));
51 } 79 }
52 80
53 FontListImpl::FontListImpl(const std::vector<std::string>& font_names, 81 FontListImpl::FontListImpl(const std::vector<std::string>& font_names,
54 int font_style, 82 int font_style,
55 int font_size) 83 int font_size,
56 : font_description_string_(BuildDescription(font_names, font_style, 84 gfx::Font::Weight font_weight)
57 font_size)), 85 : font_description_string_(
86 BuildDescription(font_names, font_style, font_size, font_weight)),
58 common_height_(-1), 87 common_height_(-1),
59 common_baseline_(-1), 88 common_baseline_(-1),
60 font_style_(font_style), 89 font_style_(font_style),
61 font_size_(font_size) { 90 font_size_(font_size),
91 font_weight_(font_weight) {
62 DCHECK(!font_names.empty()); 92 DCHECK(!font_names.empty());
63 DCHECK(!font_names[0].empty()); 93 DCHECK(!font_names[0].empty());
64 } 94 }
65 95
66 FontListImpl::FontListImpl(const std::vector<Font>& fonts) 96 FontListImpl::FontListImpl(const std::vector<Font>& fonts)
67 : fonts_(fonts), 97 : fonts_(fonts),
68 common_height_(-1), 98 common_height_(-1),
69 common_baseline_(-1), 99 common_baseline_(-1),
70 font_style_(-1), 100 font_style_(-1),
71 font_size_(-1) { 101 font_size_(-1),
102 font_weight_(gfx::Font::Weight::INVALID) {
72 DCHECK(!fonts.empty()); 103 DCHECK(!fonts.empty());
73 font_style_ = fonts[0].GetStyle(); 104 font_style_ = fonts[0].GetStyle();
74 font_size_ = fonts[0].GetFontSize(); 105 font_size_ = fonts[0].GetFontSize();
106 font_weight_ = fonts[0].GetWeight();
75 #if DCHECK_IS_ON() 107 #if DCHECK_IS_ON()
76 for (size_t i = 1; i < fonts.size(); ++i) { 108 for (size_t i = 1; i < fonts.size(); ++i) {
77 DCHECK_EQ(fonts[i].GetStyle(), font_style_); 109 DCHECK_EQ(fonts[i].GetStyle(), font_style_);
78 DCHECK_EQ(fonts[i].GetFontSize(), font_size_); 110 DCHECK_EQ(fonts[i].GetFontSize(), font_size_);
79 } 111 }
80 #endif 112 #endif
81 } 113 }
82 114
83 FontListImpl::FontListImpl(const Font& font) 115 FontListImpl::FontListImpl(const Font& font)
84 : common_height_(-1), 116 : common_height_(-1),
85 common_baseline_(-1), 117 common_baseline_(-1),
86 font_style_(-1), 118 font_style_(-1),
87 font_size_(-1) { 119 font_size_(-1),
120 font_weight_(gfx::Font::Weight::INVALID) {
88 fonts_.push_back(font); 121 fonts_.push_back(font);
89 } 122 }
90 123
91 FontListImpl* FontListImpl::Derive(int size_delta, int font_style) const { 124 FontListImpl* FontListImpl::Derive(int size_delta,
125 int font_style,
126 gfx::Font::Weight weight) const {
92 // If there is a font vector, derive from that. 127 // If there is a font vector, derive from that.
93 if (!fonts_.empty()) { 128 if (!fonts_.empty()) {
94 std::vector<Font> fonts = fonts_; 129 std::vector<Font> fonts = fonts_;
95 for (size_t i = 0; i < fonts.size(); ++i) 130 for (size_t i = 0; i < fonts.size(); ++i)
96 fonts[i] = fonts[i].Derive(size_delta, font_style); 131 fonts[i] = fonts[i].Derive(size_delta, font_style, weight);
97 return new FontListImpl(fonts); 132 return new FontListImpl(fonts);
98 } 133 }
99 134
100 // Otherwise, parse the font description string to derive from it. 135 // Otherwise, parse the font description string to derive from it.
101 std::vector<std::string> font_names; 136 std::vector<std::string> font_names;
102 int old_size; 137 int old_size;
103 int old_style; 138 int old_style;
139 gfx::Font::Weight old_weight;
104 CHECK(FontList::ParseDescription(font_description_string_, &font_names, 140 CHECK(FontList::ParseDescription(font_description_string_, &font_names,
105 &old_style, &old_size)); 141 &old_style, &old_size, &old_weight));
106 const int size = std::max(1, old_size + size_delta); 142 const int size = std::max(1, old_size + size_delta);
107 return new FontListImpl(font_names, font_style, size); 143 return new FontListImpl(font_names, font_style, size, weight);
108 } 144 }
109 145
110 int FontListImpl::GetHeight() const { 146 int FontListImpl::GetHeight() const {
111 if (common_height_ == -1) 147 if (common_height_ == -1)
112 CacheCommonFontHeightAndBaseline(); 148 CacheCommonFontHeightAndBaseline();
113 return common_height_; 149 return common_height_;
114 } 150 }
115 151
116 int FontListImpl::GetBaseline() const { 152 int FontListImpl::GetBaseline() const {
117 if (common_baseline_ == -1) 153 if (common_baseline_ == -1)
(...skipping 16 matching lines...) Expand all
134 CacheFontStyleAndSize(); 170 CacheFontStyleAndSize();
135 return font_style_; 171 return font_style_;
136 } 172 }
137 173
138 int FontListImpl::GetFontSize() const { 174 int FontListImpl::GetFontSize() const {
139 if (font_size_ == -1) 175 if (font_size_ == -1)
140 CacheFontStyleAndSize(); 176 CacheFontStyleAndSize();
141 return font_size_; 177 return font_size_;
142 } 178 }
143 179
180 gfx::Font::Weight FontListImpl::GetFontWeight() const {
181 if (font_weight_ == gfx::Font::Weight::INVALID)
182 CacheFontStyleAndSize();
183 return font_weight_;
184 }
185
144 const std::vector<Font>& FontListImpl::GetFonts() const { 186 const std::vector<Font>& FontListImpl::GetFonts() const {
145 if (fonts_.empty()) { 187 if (fonts_.empty()) {
146 DCHECK(!font_description_string_.empty()); 188 DCHECK(!font_description_string_.empty());
147 189
148 std::vector<std::string> font_names; 190 std::vector<std::string> font_names;
149 // It's possible that gfx::Font::UNDERLINE is specified and it's already 191 // 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 192 // 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 193 // underline info. So we should respect |font_style_| as long as it's
152 // valid. 194 // valid.
153 int style = 0; 195 int style = 0;
154 CHECK(FontList::ParseDescription(font_description_string_, &font_names, 196 CHECK(FontList::ParseDescription(font_description_string_, &font_names,
155 &style, &font_size_)); 197 &style, &font_size_, &font_weight_));
156 if (font_style_ == -1) 198 if (font_style_ == -1)
157 font_style_ = style; 199 font_style_ = style;
158 for (size_t i = 0; i < font_names.size(); ++i) { 200 for (size_t i = 0; i < font_names.size(); ++i) {
159 DCHECK(!font_names[i].empty()); 201 DCHECK(!font_names[i].empty());
160 202
161 Font font(font_names[i], font_size_); 203 Font font(font_names[i], font_size_);
162 if (font_style_ == Font::NORMAL) 204 if (font_style_ == Font::NORMAL)
163 fonts_.push_back(font); 205 fonts_.push_back(font);
164 else 206 else
165 fonts_.push_back(font.Derive(0, font_style_)); 207 fonts_.push_back(font.Derive(0, font_style_, font_weight_));
166 } 208 }
167 } 209 }
168 return fonts_; 210 return fonts_;
169 } 211 }
170 212
171 const Font& FontListImpl::GetPrimaryFont() const { 213 const Font& FontListImpl::GetPrimaryFont() const {
172 return GetFonts()[0]; 214 return GetFonts()[0];
173 } 215 }
174 216
175 FontListImpl::~FontListImpl() {} 217 FontListImpl::~FontListImpl() {}
176 218
177 void FontListImpl::CacheCommonFontHeightAndBaseline() const { 219 void FontListImpl::CacheCommonFontHeightAndBaseline() const {
178 int ascent = 0; 220 int ascent = 0;
179 int descent = 0; 221 int descent = 0;
180 const std::vector<Font>& fonts = GetFonts(); 222 const std::vector<Font>& fonts = GetFonts();
181 for (std::vector<Font>::const_iterator i = fonts.begin(); 223 for (std::vector<Font>::const_iterator i = fonts.begin();
182 i != fonts.end(); ++i) { 224 i != fonts.end(); ++i) {
183 ascent = std::max(ascent, i->GetBaseline()); 225 ascent = std::max(ascent, i->GetBaseline());
184 descent = std::max(descent, i->GetHeight() - i->GetBaseline()); 226 descent = std::max(descent, i->GetHeight() - i->GetBaseline());
185 } 227 }
186 common_height_ = ascent + descent; 228 common_height_ = ascent + descent;
187 common_baseline_ = ascent; 229 common_baseline_ = ascent;
188 } 230 }
189 231
190 void FontListImpl::CacheFontStyleAndSize() const { 232 void FontListImpl::CacheFontStyleAndSize() const {
191 if (!fonts_.empty()) { 233 if (!fonts_.empty()) {
192 font_style_ = fonts_[0].GetStyle(); 234 font_style_ = fonts_[0].GetStyle();
193 font_size_ = fonts_[0].GetFontSize(); 235 font_size_ = fonts_[0].GetFontSize();
236 font_weight_ = fonts_[0].GetWeight();
194 } else { 237 } else {
195 std::vector<std::string> font_names; 238 std::vector<std::string> font_names;
196 CHECK(FontList::ParseDescription(font_description_string_, &font_names, 239 CHECK(FontList::ParseDescription(font_description_string_, &font_names,
197 &font_style_, &font_size_)); 240 &font_style_, &font_size_, &font_weight_));
198 } 241 }
199 } 242 }
200 243
201 } // namespace gfx 244 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698