Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "ui/gfx/font_list.h" | 5 #include "ui/gfx/font_list.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 int font_size) { | 63 int font_size) { |
| 64 std::string description = JoinString(font_names, ','); | 64 std::string description = JoinString(font_names, ','); |
| 65 description += "," + FontStyleAndSizeToString(font_style, font_size); | 65 description += "," + FontStyleAndSizeToString(font_style, font_size); |
| 66 return description; | 66 return description; |
| 67 } | 67 } |
| 68 | 68 |
| 69 } // namespace | 69 } // namespace |
| 70 | 70 |
| 71 namespace gfx { | 71 namespace gfx { |
| 72 | 72 |
| 73 FontList::FontList() : common_height_(-1), common_baseline_(-1) { | 73 FontList::FontList() |
| 74 : common_height_(-1), | |
| 75 common_baseline_(-1), | |
| 76 font_style_(-1), | |
| 77 font_size_(-1) { | |
| 74 fonts_.push_back(Font()); | 78 fonts_.push_back(Font()); |
| 75 } | 79 } |
| 76 | 80 |
| 77 FontList::FontList(const std::string& font_description_string) | 81 FontList::FontList(const std::string& font_description_string) |
| 78 : font_description_string_(font_description_string), | 82 : font_description_string_(font_description_string), |
| 79 common_height_(-1), | 83 common_height_(-1), |
| 80 common_baseline_(-1) { | 84 common_baseline_(-1), |
| 85 font_style_(-1), | |
| 86 font_size_(-1) { | |
| 81 DCHECK(!font_description_string.empty()); | 87 DCHECK(!font_description_string.empty()); |
| 82 // DCHECK description string ends with "px" for size in pixel. | 88 // DCHECK description string ends with "px" for size in pixel. |
| 83 DCHECK(EndsWith(font_description_string, "px", true)); | 89 DCHECK(EndsWith(font_description_string, "px", true)); |
| 84 } | 90 } |
| 85 | 91 |
| 92 FontList::FontList(const std::vector<std::string>& font_names, | |
| 93 int font_style, | |
| 94 int font_size) | |
| 95 : font_description_string_(BuildFontDescription(font_names, font_style, | |
| 96 font_size)), | |
| 97 common_height_(-1), | |
| 98 common_baseline_(-1), | |
| 99 font_style_(font_style), | |
| 100 font_size_(font_size) { | |
| 101 DCHECK(!font_names.empty()); | |
| 102 DCHECK(!font_names[0].empty()); | |
| 103 } | |
| 104 | |
| 86 FontList::FontList(const std::vector<Font>& fonts) | 105 FontList::FontList(const std::vector<Font>& fonts) |
| 87 : fonts_(fonts), | 106 : fonts_(fonts), |
| 88 common_height_(-1), | 107 common_height_(-1), |
| 89 common_baseline_(-1) { | 108 common_baseline_(-1), |
| 109 font_style_(-1), | |
| 110 font_size_(-1) { | |
| 90 DCHECK(!fonts.empty()); | 111 DCHECK(!fonts.empty()); |
| 112 font_style_ = fonts[0].GetStyle(); | |
| 113 font_size_ = fonts[0].GetFontSize(); | |
| 91 if (DCHECK_IS_ON()) { | 114 if (DCHECK_IS_ON()) { |
| 92 int style = fonts[0].GetStyle(); | |
| 93 int size = fonts[0].GetFontSize(); | |
| 94 for (size_t i = 1; i < fonts.size(); ++i) { | 115 for (size_t i = 1; i < fonts.size(); ++i) { |
| 95 DCHECK_EQ(fonts[i].GetStyle(), style); | 116 DCHECK_EQ(fonts[i].GetStyle(), font_style_); |
| 96 DCHECK_EQ(fonts[i].GetFontSize(), size); | 117 DCHECK_EQ(fonts[i].GetFontSize(), font_size_); |
| 97 } | 118 } |
| 98 } | 119 } |
| 99 } | 120 } |
| 100 | 121 |
| 101 FontList::FontList(const Font& font) | 122 FontList::FontList(const Font& font) |
| 102 : common_height_(-1), | 123 : common_height_(-1), |
| 103 common_baseline_(-1) { | 124 common_baseline_(-1), |
| 125 font_style_(-1), | |
| 126 font_size_(-1) { | |
| 104 fonts_.push_back(font); | 127 fonts_.push_back(font); |
| 105 } | 128 } |
| 106 | 129 |
| 107 FontList::~FontList() { | 130 FontList::~FontList() { |
| 108 } | 131 } |
| 109 | 132 |
| 110 FontList FontList::DeriveFontList(int font_style) const { | 133 FontList FontList::DeriveFontList(int font_style) const { |
| 134 return DeriveFontListWithSizeDeltaAndStyle(0, font_style); | |
| 135 } | |
| 136 | |
| 137 FontList FontList::DeriveFontListWithSize(int size) const { | |
| 138 DCHECK_GT(size, 0); | |
| 139 return DeriveFontListWithSizeDeltaAndStyle(size - GetFontSize(), | |
| 140 GetFontStyle()); | |
| 141 } | |
| 142 | |
| 143 FontList FontList::DeriveFontListWithSizeDelta(int size_delta) const { | |
| 144 return DeriveFontListWithSizeDeltaAndStyle(size_delta, GetFontStyle()); | |
| 145 } | |
| 146 | |
| 147 FontList FontList::DeriveFontListWithSizeDeltaAndStyle(int size_delta, | |
| 148 int style) const { | |
| 111 // If there is a font vector, derive from that. | 149 // If there is a font vector, derive from that. |
| 112 if (!fonts_.empty()) { | 150 if (!fonts_.empty()) { |
| 113 std::vector<Font> fonts = fonts_; | 151 std::vector<Font> fonts = fonts_; |
| 114 for (size_t i = 0; i < fonts.size(); ++i) | 152 for (size_t i = 0; i < fonts.size(); ++i) |
| 115 fonts[i] = fonts[i].DeriveFont(0, font_style); | 153 fonts[i] = fonts[i].DeriveFont(size_delta, style); |
| 116 return FontList(fonts); | 154 return FontList(fonts); |
| 117 } | 155 } |
| 118 | 156 |
| 119 // Otherwise, parse the font description string to derive from it. | 157 // Otherwise, parse the font description string to derive from it. |
| 120 std::vector<std::string> font_names; | 158 std::vector<std::string> font_names; |
| 159 int old_size; | |
| 121 int old_style; | 160 int old_style; |
| 122 int font_size; | |
| 123 ParseFontDescriptionString(font_description_string_, &font_names, | 161 ParseFontDescriptionString(font_description_string_, &font_names, |
| 124 &old_style, &font_size); | 162 &old_style, &old_size); |
| 125 return FontList(BuildFontDescription(font_names, font_style, font_size)); | 163 int size = old_size + size_delta; |
| 126 } | |
| 127 | |
| 128 FontList FontList::DeriveFontListWithSize(int size) const { | |
| 129 DCHECK_GT(size, 0); | 164 DCHECK_GT(size, 0); |
| 130 | 165 return FontList(font_names, style, size); |
| 131 // If there is a font vector, derive from that. | |
| 132 int old_size = 0; | |
| 133 if (!fonts_.empty()) { | |
| 134 old_size = fonts_[0].GetFontSize(); | |
| 135 if (old_size == size) | |
| 136 return FontList(fonts_); | |
| 137 | |
| 138 std::vector<Font> fonts = fonts_; | |
| 139 for (size_t i = 0; i < fonts.size(); ++i) | |
| 140 fonts[i] = fonts[i].DeriveFont(size - old_size); | |
| 141 return FontList(fonts); | |
| 142 } | |
| 143 | |
| 144 // Otherwise, parse the font description string to derive from it. | |
| 145 std::vector<std::string> font_names; | |
| 146 int font_style = 0; | |
| 147 ParseFontDescriptionString(font_description_string_, &font_names, | |
| 148 &font_style, &old_size); | |
| 149 | |
| 150 if (old_size == size) | |
| 151 return FontList(font_description_string_); | |
| 152 | |
| 153 return FontList(BuildFontDescription(font_names, font_style, size)); | |
| 154 } | 166 } |
| 155 | 167 |
| 156 int FontList::GetHeight() const { | 168 int FontList::GetHeight() const { |
| 157 if (common_height_ < 0) { | 169 if (common_height_ == -1) |
| 158 int ascent = 0; | 170 CacheCommonFontHeightAndBaseline(); |
| 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_; | 171 return common_height_; |
| 169 } | 172 } |
| 170 | 173 |
| 171 int FontList::GetBaseline() const { | 174 int FontList::GetBaseline() const { |
| 172 if (common_baseline_ < 0) { | 175 if (common_baseline_ == -1) |
| 173 int baseline = 0; | 176 CacheCommonFontHeightAndBaseline(); |
| 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_; | 177 return common_baseline_; |
| 182 } | 178 } |
| 183 | 179 |
| 180 int FontList::GetStringWidth(const base::string16& text) const { | |
| 181 // Rely on the primary font metrics for the time being. | |
| 182 return GetPrimaryFont().GetStringWidth(text); | |
|
Alexei Svitkine (slow)
2013/08/07 19:30:44
This function is pretty bad, since GetStringWidth(
Yuki
2013/08/08 06:37:36
Let me work on it in a separate CL.
Added a TODO c
| |
| 183 } | |
| 184 | |
| 185 int FontList::GetExpectedTextWidth(int length) const { | |
| 186 // Rely on the primary font metrics for the time being. | |
| 187 return GetPrimaryFont().GetExpectedTextWidth(length); | |
| 188 } | |
| 189 | |
| 184 int FontList::GetFontStyle() const { | 190 int FontList::GetFontStyle() const { |
| 185 if (!fonts_.empty()) | 191 if (font_style_ == -1) |
| 186 return fonts_[0].GetStyle(); | 192 CacheFontStyleAndSize(); |
| 187 | 193 return font_style_; |
| 188 std::vector<std::string> font_names; | |
| 189 int font_style; | |
| 190 int font_size; | |
| 191 ParseFontDescriptionString(font_description_string_, &font_names, | |
| 192 &font_style, &font_size); | |
| 193 return font_style; | |
| 194 } | 194 } |
| 195 | 195 |
| 196 const std::string& FontList::GetFontDescriptionString() const { | 196 const std::string& FontList::GetFontDescriptionString() const { |
| 197 if (font_description_string_.empty()) { | 197 if (font_description_string_.empty()) { |
| 198 DCHECK(!fonts_.empty()); | 198 DCHECK(!fonts_.empty()); |
| 199 for (size_t i = 0; i < fonts_.size(); ++i) { | 199 for (size_t i = 0; i < fonts_.size(); ++i) { |
| 200 std::string name = fonts_[i].GetFontName(); | 200 std::string name = fonts_[i].GetFontName(); |
| 201 font_description_string_ += name; | 201 font_description_string_ += name; |
| 202 font_description_string_ += ','; | 202 font_description_string_ += ','; |
| 203 } | 203 } |
| 204 // All fonts have the same style and size. | 204 // All fonts have the same style and size. |
| 205 font_description_string_ += | 205 font_description_string_ += |
| 206 FontStyleAndSizeToString(fonts_[0].GetStyle(), fonts_[0].GetFontSize()); | 206 FontStyleAndSizeToString(fonts_[0].GetStyle(), fonts_[0].GetFontSize()); |
| 207 } | 207 } |
| 208 return font_description_string_; | 208 return font_description_string_; |
| 209 } | 209 } |
| 210 | 210 |
| 211 int FontList::GetFontSize() const { | 211 int FontList::GetFontSize() const { |
| 212 if (!fonts_.empty()) | 212 if (font_size_ == -1) |
| 213 return fonts_[0].GetFontSize(); | 213 CacheFontStyleAndSize(); |
| 214 | 214 return font_size_; |
| 215 std::vector<std::string> font_names; | |
| 216 int font_style; | |
| 217 int font_size; | |
| 218 ParseFontDescriptionString(font_description_string_, &font_names, | |
| 219 &font_style, &font_size); | |
| 220 return font_size; | |
| 221 } | 215 } |
| 222 | 216 |
| 223 const std::vector<Font>& FontList::GetFonts() const { | 217 const std::vector<Font>& FontList::GetFonts() const { |
| 224 if (fonts_.empty()) { | 218 if (fonts_.empty()) { |
| 225 DCHECK(!font_description_string_.empty()); | 219 DCHECK(!font_description_string_.empty()); |
| 226 | 220 |
| 227 std::vector<std::string> font_names; | 221 std::vector<std::string> font_names; |
| 228 int font_style; | |
| 229 int font_size; | |
| 230 ParseFontDescriptionString(font_description_string_, &font_names, | 222 ParseFontDescriptionString(font_description_string_, &font_names, |
| 231 &font_style, &font_size); | 223 &font_style_, &font_size_); |
| 232 for (size_t i = 0; i < font_names.size(); ++i) { | 224 for (size_t i = 0; i < font_names.size(); ++i) { |
| 233 DCHECK(!font_names[i].empty()); | 225 DCHECK(!font_names[i].empty()); |
| 234 | 226 |
| 235 Font font(font_names[i], font_size); | 227 Font font(font_names[i], font_size_); |
| 236 if (font_style == Font::NORMAL) | 228 if (font_style_ == Font::NORMAL) |
| 237 fonts_.push_back(font); | 229 fonts_.push_back(font); |
| 238 else | 230 else |
| 239 fonts_.push_back(font.DeriveFont(0, font_style)); | 231 fonts_.push_back(font.DeriveFont(0, font_style_)); |
| 240 } | 232 } |
| 241 } | 233 } |
| 242 return fonts_; | 234 return fonts_; |
| 243 } | 235 } |
| 244 | 236 |
| 245 const Font& FontList::GetPrimaryFont() const { | 237 const Font& FontList::GetPrimaryFont() const { |
| 246 return GetFonts()[0]; | 238 return GetFonts()[0]; |
| 247 } | 239 } |
| 248 | 240 |
| 241 void FontList::CacheCommonFontHeightAndBaseline() const { | |
| 242 int ascent = 0; | |
| 243 int descent = 0; | |
| 244 const std::vector<Font>& fonts = GetFonts(); | |
| 245 for (std::vector<Font>::const_iterator i = fonts.begin(); | |
| 246 i != fonts.end(); ++i) { | |
| 247 ascent = std::max(ascent, i->GetBaseline()); | |
| 248 descent = std::max(descent, i->GetHeight() - i->GetBaseline()); | |
| 249 } | |
| 250 common_height_ = ascent + descent; | |
| 251 common_baseline_ = ascent; | |
| 252 } | |
| 253 | |
| 254 void FontList::CacheFontStyleAndSize() const { | |
| 255 if (!fonts_.empty()) { | |
| 256 font_style_ = fonts_[0].GetStyle(); | |
| 257 font_size_ = fonts_[0].GetFontSize(); | |
| 258 } else { | |
| 259 std::vector<std::string> font_names; | |
| 260 ParseFontDescriptionString(font_description_string_, &font_names, | |
| 261 &font_style_, &font_size_); | |
| 262 } | |
| 263 } | |
| 264 | |
| 249 } // namespace gfx | 265 } // namespace gfx |
| OLD | NEW |