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

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: Add a lost comment and modify a render text unittest to not test black because of test env font con… Created 4 years, 6 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
« no previous file with comments | « ui/gfx/font_list_impl.h ('k') | ui/gfx/font_list_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
16 namespace gfx {
17 namespace { 17 namespace {
18 18
19 // Returns a font description from |families|, |style|, and |size_pixels|. 19 // Returns a font description from |families|, |style|, and |size_pixels|.
20 std::string BuildDescription(const std::vector<std::string>& families, 20 std::string BuildDescription(const std::vector<std::string>& families,
21 int style, 21 int style,
22 int size_pixels) { 22 int size_pixels,
23 Font::Weight weight) {
23 std::string description = base::JoinString(families, ","); 24 std::string description = base::JoinString(families, ",");
24 description += ","; 25 description += ",";
25 26
26 if (style & gfx::Font::BOLD) 27 if (style & Font::ITALIC)
27 description += "Bold ";
28 if (style & gfx::Font::ITALIC)
29 description += "Italic "; 28 description += "Italic ";
29 switch (weight) {
30 case Font::Weight::THIN:
31 description += "Thin ";
32 break;
33 case Font::Weight::EXTRA_LIGHT:
34 description += "Ultra-Light ";
35 break;
36 case Font::Weight::LIGHT:
37 description += "Light ";
38 break;
39 case Font::Weight::MEDIUM:
40 description += "Medium ";
41 break;
42 case Font::Weight::SEMIBOLD:
43 description += "Semi-Bold ";
44 break;
45 case Font::Weight::BOLD:
46 description += "Bold ";
47 break;
48 case Font::Weight::EXTRA_BOLD:
49 description += "Ultra-Bold ";
50 break;
51 case Font::Weight::BLACK:
52 description += "Heavy ";
53 break;
54 case Font::Weight::NORMAL:
55 case Font::Weight::INVALID:
56 break;
57 }
30 58
31 description += base::IntToString(size_pixels); 59 description += base::IntToString(size_pixels);
32 description += "px"; 60 description += "px";
33 61
34 return description; 62 return description;
35 } 63 }
36 64
37 } // namespace 65 } // namespace
38 66
39 namespace gfx {
40
41 FontListImpl::FontListImpl(const std::string& font_description_string) 67 FontListImpl::FontListImpl(const std::string& font_description_string)
42 : font_description_string_(font_description_string), 68 : font_description_string_(font_description_string),
43 common_height_(-1), 69 common_height_(-1),
44 common_baseline_(-1), 70 common_baseline_(-1),
45 font_style_(-1), 71 font_style_(-1),
46 font_size_(-1) { 72 font_size_(-1),
73 font_weight_(Font::Weight::INVALID) {
47 DCHECK(!font_description_string.empty()); 74 DCHECK(!font_description_string.empty());
48 // DCHECK description string ends with "px" for size in pixel. 75 // DCHECK description string ends with "px" for size in pixel.
49 DCHECK(base::EndsWith(font_description_string, "px", 76 DCHECK(base::EndsWith(font_description_string, "px",
50 base::CompareCase::SENSITIVE)); 77 base::CompareCase::SENSITIVE));
51 } 78 }
52 79
53 FontListImpl::FontListImpl(const std::vector<std::string>& font_names, 80 FontListImpl::FontListImpl(const std::vector<std::string>& font_names,
54 int font_style, 81 int font_style,
55 int font_size) 82 int font_size,
56 : font_description_string_(BuildDescription(font_names, font_style, 83 Font::Weight font_weight)
57 font_size)), 84 : font_description_string_(
85 BuildDescription(font_names, font_style, font_size, font_weight)),
58 common_height_(-1), 86 common_height_(-1),
59 common_baseline_(-1), 87 common_baseline_(-1),
60 font_style_(font_style), 88 font_style_(font_style),
61 font_size_(font_size) { 89 font_size_(font_size),
90 font_weight_(font_weight) {
62 DCHECK(!font_names.empty()); 91 DCHECK(!font_names.empty());
63 DCHECK(!font_names[0].empty()); 92 DCHECK(!font_names[0].empty());
64 } 93 }
65 94
66 FontListImpl::FontListImpl(const std::vector<Font>& fonts) 95 FontListImpl::FontListImpl(const std::vector<Font>& fonts)
67 : fonts_(fonts), 96 : fonts_(fonts),
68 common_height_(-1), 97 common_height_(-1),
69 common_baseline_(-1), 98 common_baseline_(-1),
70 font_style_(-1), 99 font_style_(-1),
71 font_size_(-1) { 100 font_size_(-1),
101 font_weight_(Font::Weight::INVALID) {
72 DCHECK(!fonts.empty()); 102 DCHECK(!fonts.empty());
73 font_style_ = fonts[0].GetStyle(); 103 font_style_ = fonts[0].GetStyle();
74 font_size_ = fonts[0].GetFontSize(); 104 font_size_ = fonts[0].GetFontSize();
105 font_weight_ = fonts[0].GetWeight();
75 #if DCHECK_IS_ON() 106 #if DCHECK_IS_ON()
76 for (size_t i = 1; i < fonts.size(); ++i) { 107 for (size_t i = 1; i < fonts.size(); ++i) {
77 DCHECK_EQ(fonts[i].GetStyle(), font_style_); 108 DCHECK_EQ(fonts[i].GetStyle(), font_style_);
78 DCHECK_EQ(fonts[i].GetFontSize(), font_size_); 109 DCHECK_EQ(fonts[i].GetFontSize(), font_size_);
79 } 110 }
80 #endif 111 #endif
81 } 112 }
82 113
83 FontListImpl::FontListImpl(const Font& font) 114 FontListImpl::FontListImpl(const Font& font)
84 : common_height_(-1), 115 : common_height_(-1),
85 common_baseline_(-1), 116 common_baseline_(-1),
86 font_style_(-1), 117 font_style_(-1),
87 font_size_(-1) { 118 font_size_(-1),
119 font_weight_(Font::Weight::INVALID) {
88 fonts_.push_back(font); 120 fonts_.push_back(font);
89 } 121 }
90 122
91 FontListImpl* FontListImpl::Derive(int size_delta, int font_style) const { 123 FontListImpl* FontListImpl::Derive(int size_delta,
124 int font_style,
125 Font::Weight weight) const {
92 // If there is a font vector, derive from that. 126 // If there is a font vector, derive from that.
93 if (!fonts_.empty()) { 127 if (!fonts_.empty()) {
94 std::vector<Font> fonts = fonts_; 128 std::vector<Font> fonts = fonts_;
95 for (size_t i = 0; i < fonts.size(); ++i) 129 for (size_t i = 0; i < fonts.size(); ++i)
96 fonts[i] = fonts[i].Derive(size_delta, font_style); 130 fonts[i] = fonts[i].Derive(size_delta, font_style, weight);
97 return new FontListImpl(fonts); 131 return new FontListImpl(fonts);
98 } 132 }
99 133
100 // Otherwise, parse the font description string to derive from it. 134 // Otherwise, parse the font description string to derive from it.
101 std::vector<std::string> font_names; 135 std::vector<std::string> font_names;
102 int old_size; 136 int old_size;
103 int old_style; 137 int old_style;
138 Font::Weight old_weight;
104 CHECK(FontList::ParseDescription(font_description_string_, &font_names, 139 CHECK(FontList::ParseDescription(font_description_string_, &font_names,
105 &old_style, &old_size)); 140 &old_style, &old_size, &old_weight));
106 const int size = std::max(1, old_size + size_delta); 141 const int size = std::max(1, old_size + size_delta);
107 return new FontListImpl(font_names, font_style, size); 142 return new FontListImpl(font_names, font_style, size, weight);
108 } 143 }
109 144
110 int FontListImpl::GetHeight() const { 145 int FontListImpl::GetHeight() const {
111 if (common_height_ == -1) 146 if (common_height_ == -1)
112 CacheCommonFontHeightAndBaseline(); 147 CacheCommonFontHeightAndBaseline();
113 return common_height_; 148 return common_height_;
114 } 149 }
115 150
116 int FontListImpl::GetBaseline() const { 151 int FontListImpl::GetBaseline() const {
117 if (common_baseline_ == -1) 152 if (common_baseline_ == -1)
(...skipping 16 matching lines...) Expand all
134 CacheFontStyleAndSize(); 169 CacheFontStyleAndSize();
135 return font_style_; 170 return font_style_;
136 } 171 }
137 172
138 int FontListImpl::GetFontSize() const { 173 int FontListImpl::GetFontSize() const {
139 if (font_size_ == -1) 174 if (font_size_ == -1)
140 CacheFontStyleAndSize(); 175 CacheFontStyleAndSize();
141 return font_size_; 176 return font_size_;
142 } 177 }
143 178
179 Font::Weight FontListImpl::GetFontWeight() const {
180 if (font_weight_ == Font::Weight::INVALID)
181 CacheFontStyleAndSize();
182 return font_weight_;
183 }
184
144 const std::vector<Font>& FontListImpl::GetFonts() const { 185 const std::vector<Font>& FontListImpl::GetFonts() const {
145 if (fonts_.empty()) { 186 if (fonts_.empty()) {
146 DCHECK(!font_description_string_.empty()); 187 DCHECK(!font_description_string_.empty());
147 188
148 std::vector<std::string> font_names; 189 std::vector<std::string> font_names;
149 // It's possible that gfx::Font::UNDERLINE is specified and it's already 190 // It's possible that Font::UNDERLINE is specified and it's already
150 // stored in |font_style_| but |font_description_string_| doesn't have the 191 // 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 192 // underline info. So we should respect |font_style_| as long as it's
152 // valid. 193 // valid.
153 int style = 0; 194 int style = 0;
154 CHECK(FontList::ParseDescription(font_description_string_, &font_names, 195 CHECK(FontList::ParseDescription(font_description_string_, &font_names,
155 &style, &font_size_)); 196 &style, &font_size_, &font_weight_));
156 if (font_style_ == -1) 197 if (font_style_ == -1)
157 font_style_ = style; 198 font_style_ = style;
158 for (size_t i = 0; i < font_names.size(); ++i) { 199 for (size_t i = 0; i < font_names.size(); ++i) {
159 DCHECK(!font_names[i].empty()); 200 DCHECK(!font_names[i].empty());
160 201
161 Font font(font_names[i], font_size_); 202 Font font(font_names[i], font_size_);
162 if (font_style_ == Font::NORMAL) 203 if (font_style_ == Font::NORMAL && font_weight_ == Font::Weight::NORMAL)
163 fonts_.push_back(font); 204 fonts_.push_back(font);
164 else 205 else
165 fonts_.push_back(font.Derive(0, font_style_)); 206 fonts_.push_back(font.Derive(0, font_style_, font_weight_));
166 } 207 }
167 } 208 }
168 return fonts_; 209 return fonts_;
169 } 210 }
170 211
171 const Font& FontListImpl::GetPrimaryFont() const { 212 const Font& FontListImpl::GetPrimaryFont() const {
172 return GetFonts()[0]; 213 return GetFonts()[0];
173 } 214 }
174 215
175 FontListImpl::~FontListImpl() {} 216 FontListImpl::~FontListImpl() {}
176 217
177 void FontListImpl::CacheCommonFontHeightAndBaseline() const { 218 void FontListImpl::CacheCommonFontHeightAndBaseline() const {
178 int ascent = 0; 219 int ascent = 0;
179 int descent = 0; 220 int descent = 0;
180 const std::vector<Font>& fonts = GetFonts(); 221 const std::vector<Font>& fonts = GetFonts();
181 for (std::vector<Font>::const_iterator i = fonts.begin(); 222 for (std::vector<Font>::const_iterator i = fonts.begin();
182 i != fonts.end(); ++i) { 223 i != fonts.end(); ++i) {
183 ascent = std::max(ascent, i->GetBaseline()); 224 ascent = std::max(ascent, i->GetBaseline());
184 descent = std::max(descent, i->GetHeight() - i->GetBaseline()); 225 descent = std::max(descent, i->GetHeight() - i->GetBaseline());
185 } 226 }
186 common_height_ = ascent + descent; 227 common_height_ = ascent + descent;
187 common_baseline_ = ascent; 228 common_baseline_ = ascent;
188 } 229 }
189 230
190 void FontListImpl::CacheFontStyleAndSize() const { 231 void FontListImpl::CacheFontStyleAndSize() const {
191 if (!fonts_.empty()) { 232 if (!fonts_.empty()) {
192 font_style_ = fonts_[0].GetStyle(); 233 font_style_ = fonts_[0].GetStyle();
193 font_size_ = fonts_[0].GetFontSize(); 234 font_size_ = fonts_[0].GetFontSize();
235 font_weight_ = fonts_[0].GetWeight();
194 } else { 236 } else {
195 std::vector<std::string> font_names; 237 std::vector<std::string> font_names;
196 CHECK(FontList::ParseDescription(font_description_string_, &font_names, 238 CHECK(FontList::ParseDescription(font_description_string_, &font_names,
197 &font_style_, &font_size_)); 239 &font_style_, &font_size_, &font_weight_));
198 } 240 }
199 } 241 }
200 242
201 } // namespace gfx 243 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/font_list_impl.h ('k') | ui/gfx/font_list_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698