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

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

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

Powered by Google App Engine
This is Rietveld 408576698