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

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

Powered by Google App Engine
This is Rietveld 408576698