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

Side by Side Diff: trunk/src/ui/gfx/font_list.cc

Issue 19270002: This caused failures in ui_unittests FontListTest.Fonts_GetHeight_GetBaseline (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 7 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « trunk/src/ui/gfx/font_list.h ('k') | trunk/src/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 (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 <stdlib.h> 5 #include <stdlib.h>
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h" 8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_split.h" 9 #include "base/strings/string_split.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 int font_size) { 62 int font_size) {
63 std::string description = JoinString(font_names, ','); 63 std::string description = JoinString(font_names, ',');
64 description += "," + FontStyleAndSizeToString(font_style, font_size); 64 description += "," + FontStyleAndSizeToString(font_style, font_size);
65 return description; 65 return description;
66 } 66 }
67 67
68 } // namespace 68 } // namespace
69 69
70 namespace gfx { 70 namespace gfx {
71 71
72 FontList::FontList() : common_height_(-1), common_baseline_(-1) { 72 FontList::FontList() {
73 fonts_.push_back(Font()); 73 fonts_.push_back(Font());
74 } 74 }
75 75
76 FontList::FontList(const std::string& font_description_string) 76 FontList::FontList(const std::string& font_description_string)
77 : font_description_string_(font_description_string), 77 : font_description_string_(font_description_string) {
78 common_height_(-1),
79 common_baseline_(-1) {
80 DCHECK(!font_description_string.empty()); 78 DCHECK(!font_description_string.empty());
81 // DCHECK description string ends with "px" for size in pixel. 79 // DCHECK description string ends with "px" for size in pixel.
82 DCHECK(EndsWith(font_description_string, "px", true)); 80 DCHECK(EndsWith(font_description_string, "px", true));
83 } 81 }
84 82
85 FontList::FontList(const std::vector<Font>& fonts) 83 FontList::FontList(const std::vector<Font>& fonts)
86 : fonts_(fonts), common_height_(-1), common_baseline_(-1) { 84 : fonts_(fonts) {
87 DCHECK(!fonts.empty()); 85 DCHECK(!fonts.empty());
88 if (DCHECK_IS_ON()) { 86 if (DCHECK_IS_ON()) {
89 int style = fonts[0].GetStyle(); 87 int style = fonts[0].GetStyle();
90 int size = fonts[0].GetFontSize(); 88 int size = fonts[0].GetFontSize();
91 for (size_t i = 1; i < fonts.size(); ++i) { 89 for (size_t i = 1; i < fonts.size(); ++i) {
92 DCHECK_EQ(fonts[i].GetStyle(), style); 90 DCHECK_EQ(fonts[i].GetStyle(), style);
93 DCHECK_EQ(fonts[i].GetFontSize(), size); 91 DCHECK_EQ(fonts[i].GetFontSize(), size);
94 } 92 }
95 } 93 }
96 } 94 }
97 95
98 FontList::FontList(const Font& font) 96 FontList::FontList(const Font& font) {
99 : common_height_(-1), common_baseline_(-1) {
100 fonts_.push_back(font); 97 fonts_.push_back(font);
101 } 98 }
102 99
103 FontList::~FontList() { 100 FontList::~FontList() {
104 } 101 }
105 102
106 FontList FontList::DeriveFontList(int font_style) const { 103 FontList FontList::DeriveFontList(int font_style) const {
107 // If there is a font vector, derive from that. 104 // If there is a font vector, derive from that.
108 if (!fonts_.empty()) { 105 if (!fonts_.empty()) {
109 std::vector<Font> fonts = fonts_; 106 std::vector<Font> fonts = fonts_;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 int font_style = 0; 139 int font_style = 0;
143 ParseFontDescriptionString(font_description_string_, &font_names, 140 ParseFontDescriptionString(font_description_string_, &font_names,
144 &font_style, &old_size); 141 &font_style, &old_size);
145 142
146 if (old_size == size) 143 if (old_size == size)
147 return FontList(font_description_string_); 144 return FontList(font_description_string_);
148 145
149 return FontList(BuildFontDescription(font_names, font_style, size)); 146 return FontList(BuildFontDescription(font_names, font_style, size));
150 } 147 }
151 148
152 int FontList::GetHeight() const {
153 if (common_height_ < 0) {
154 int ascent = 0;
155 int descent = 0;
156 const std::vector<Font>& fonts = GetFonts();
157 for (std::vector<Font>::const_iterator i = fonts.begin();
158 i != fonts.end(); ++i) {
159 ascent = std::max(ascent, i->GetBaseline());
160 descent = std::max(descent, i->GetHeight() - i->GetBaseline());
161 }
162 common_height_ = ascent + descent;
163 }
164 return common_height_;
165 }
166
167 int FontList::GetBaseline() const {
168 if (common_baseline_ < 0) {
169 int baseline = 0;
170 const std::vector<Font>& fonts = GetFonts();
171 for (std::vector<Font>::const_iterator i = fonts.begin();
172 i != fonts.end(); ++i) {
173 baseline = std::max(baseline, i->GetBaseline());
174 }
175 common_baseline_ = baseline;
176 }
177 return common_baseline_;
178 }
179
180 int FontList::GetFontStyle() const { 149 int FontList::GetFontStyle() const {
181 if (!fonts_.empty()) 150 if (!fonts_.empty())
182 return fonts_[0].GetStyle(); 151 return fonts_[0].GetStyle();
183 152
184 std::vector<std::string> font_names; 153 std::vector<std::string> font_names;
185 int font_style; 154 int font_style;
186 int font_size; 155 int font_size;
187 ParseFontDescriptionString(font_description_string_, &font_names, 156 ParseFontDescriptionString(font_description_string_, &font_names,
188 &font_style, &font_size); 157 &font_style, &font_size);
189 return font_style; 158 return font_style;
(...skipping 30 matching lines...) Expand all
220 if (font_style == Font::NORMAL) 189 if (font_style == Font::NORMAL)
221 fonts_.push_back(font); 190 fonts_.push_back(font);
222 else 191 else
223 fonts_.push_back(font.DeriveFont(0, font_style)); 192 fonts_.push_back(font.DeriveFont(0, font_style));
224 } 193 }
225 } 194 }
226 return fonts_; 195 return fonts_;
227 } 196 }
228 197
229 } // namespace gfx 198 } // namespace gfx
OLDNEW
« no previous file with comments | « trunk/src/ui/gfx/font_list.h ('k') | trunk/src/ui/gfx/font_list_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698