Chromium Code Reviews| Index: ui/gfx/font_list.cc |
| diff --git a/ui/gfx/font_list.cc b/ui/gfx/font_list.cc |
| index cb6cc9763de556591cf561610d23db741866fa8f..e832719f1a94899a082bfc2056c61fcf9f80afb7 100644 |
| --- a/ui/gfx/font_list.cc |
| +++ b/ui/gfx/font_list.cc |
| @@ -70,37 +70,60 @@ std::string BuildFontDescription(const std::vector<std::string>& font_names, |
| namespace gfx { |
| -FontList::FontList() : common_height_(-1), common_baseline_(-1) { |
| +FontList::FontList() |
| + : common_height_(-1), |
| + common_baseline_(-1), |
| + font_style_(-1), |
| + font_size_(-1) { |
| fonts_.push_back(Font()); |
| } |
| FontList::FontList(const std::string& font_description_string) |
| : font_description_string_(font_description_string), |
| common_height_(-1), |
| - common_baseline_(-1) { |
| + common_baseline_(-1), |
| + font_style_(-1), |
| + font_size_(-1) { |
| DCHECK(!font_description_string.empty()); |
| // DCHECK description string ends with "px" for size in pixel. |
| DCHECK(EndsWith(font_description_string, "px", true)); |
| } |
| +FontList::FontList(const std::vector<std::string>& font_names, |
| + int font_style, |
| + int font_size) |
| + : font_description_string_(BuildFontDescription(font_names, font_style, |
| + font_size)), |
| + common_height_(-1), |
| + common_baseline_(-1), |
| + font_style_(font_style), |
| + font_size_(font_size) { |
| + DCHECK(!font_names.empty()); |
| + DCHECK(!font_names[0].empty()); |
| +} |
| + |
| FontList::FontList(const std::vector<Font>& fonts) |
| : fonts_(fonts), |
| common_height_(-1), |
| - common_baseline_(-1) { |
| + common_baseline_(-1), |
| + font_style_(-1), |
| + font_size_(-1) { |
| DCHECK(!fonts.empty()); |
| + font_style_ = fonts[0].GetStyle(); |
| + font_size_ = fonts[0].GetFontSize(); |
| if (DCHECK_IS_ON()) { |
| - int style = fonts[0].GetStyle(); |
| - int size = fonts[0].GetFontSize(); |
| for (size_t i = 1; i < fonts.size(); ++i) { |
| - DCHECK_EQ(fonts[i].GetStyle(), style); |
| - DCHECK_EQ(fonts[i].GetFontSize(), size); |
| + DCHECK_EQ(fonts[i].GetStyle(), font_style_); |
| + DCHECK_EQ(fonts[i].GetFontSize(), font_size_); |
| } |
| } |
| } |
| FontList::FontList(const Font& font) |
| : common_height_(-1), |
| - common_baseline_(-1) { |
| + common_baseline_(-1), |
| + font_style_(-1), |
| + font_size_(-1) { |
| fonts_.push_back(font); |
| } |
| @@ -108,89 +131,66 @@ FontList::~FontList() { |
| } |
| FontList FontList::DeriveFontList(int font_style) const { |
| - // If there is a font vector, derive from that. |
| - if (!fonts_.empty()) { |
| - std::vector<Font> fonts = fonts_; |
| - for (size_t i = 0; i < fonts.size(); ++i) |
| - fonts[i] = fonts[i].DeriveFont(0, font_style); |
| - return FontList(fonts); |
| - } |
| - |
| - // Otherwise, parse the font description string to derive from it. |
| - std::vector<std::string> font_names; |
| - int old_style; |
| - int font_size; |
| - ParseFontDescriptionString(font_description_string_, &font_names, |
| - &old_style, &font_size); |
| - return FontList(BuildFontDescription(font_names, font_style, font_size)); |
| + return DeriveFontListWithSizeDeltaAndStyle(0, font_style); |
| } |
| FontList FontList::DeriveFontListWithSize(int size) const { |
| DCHECK_GT(size, 0); |
| + return DeriveFontListWithSizeDeltaAndStyle(size - GetFontSize(), |
| + GetFontStyle()); |
| +} |
| + |
| +FontList FontList::DeriveFontListWithSizeDelta(int size_delta) const { |
| + return DeriveFontListWithSizeDeltaAndStyle(size_delta, GetFontStyle()); |
| +} |
| +FontList FontList::DeriveFontListWithSizeDeltaAndStyle(int size_delta, |
| + int style) const { |
| // If there is a font vector, derive from that. |
| - int old_size = 0; |
| if (!fonts_.empty()) { |
| - old_size = fonts_[0].GetFontSize(); |
| - if (old_size == size) |
| - return FontList(fonts_); |
| - |
| std::vector<Font> fonts = fonts_; |
| for (size_t i = 0; i < fonts.size(); ++i) |
| - fonts[i] = fonts[i].DeriveFont(size - old_size); |
| + fonts[i] = fonts[i].DeriveFont(size_delta, style); |
| return FontList(fonts); |
| } |
| // Otherwise, parse the font description string to derive from it. |
| std::vector<std::string> font_names; |
| - int font_style = 0; |
| + int old_size; |
| + int old_style; |
| ParseFontDescriptionString(font_description_string_, &font_names, |
| - &font_style, &old_size); |
| - |
| - if (old_size == size) |
| - return FontList(font_description_string_); |
| - |
| - return FontList(BuildFontDescription(font_names, font_style, size)); |
| + &old_style, &old_size); |
| + int size = old_size + size_delta; |
| + DCHECK_GT(size, 0); |
| + return FontList(font_names, style, size); |
| } |
| int FontList::GetHeight() const { |
| - if (common_height_ < 0) { |
| - int ascent = 0; |
| - int descent = 0; |
| - const std::vector<Font>& fonts = GetFonts(); |
| - for (std::vector<Font>::const_iterator i = fonts.begin(); |
| - i != fonts.end(); ++i) { |
| - ascent = std::max(ascent, i->GetBaseline()); |
| - descent = std::max(descent, i->GetHeight() - i->GetBaseline()); |
| - } |
| - common_height_ = ascent + descent; |
| - } |
| + if (common_height_ == -1) |
| + CacheCommonFontHeightAndBaseline(); |
| return common_height_; |
| } |
| int FontList::GetBaseline() const { |
| - if (common_baseline_ < 0) { |
| - int baseline = 0; |
| - const std::vector<Font>& fonts = GetFonts(); |
| - for (std::vector<Font>::const_iterator i = fonts.begin(); |
| - i != fonts.end(); ++i) { |
| - baseline = std::max(baseline, i->GetBaseline()); |
| - } |
| - common_baseline_ = baseline; |
| - } |
| + if (common_baseline_ == -1) |
| + CacheCommonFontHeightAndBaseline(); |
| return common_baseline_; |
| } |
| -int FontList::GetFontStyle() const { |
| - if (!fonts_.empty()) |
| - return fonts_[0].GetStyle(); |
| +int FontList::GetStringWidth(const base::string16& text) const { |
| + // Rely on the primary font metrics for the time being. |
| + 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
|
| +} |
| - std::vector<std::string> font_names; |
| - int font_style; |
| - int font_size; |
| - ParseFontDescriptionString(font_description_string_, &font_names, |
| - &font_style, &font_size); |
| - return font_style; |
| +int FontList::GetExpectedTextWidth(int length) const { |
| + // Rely on the primary font metrics for the time being. |
| + return GetPrimaryFont().GetExpectedTextWidth(length); |
| +} |
| + |
| +int FontList::GetFontStyle() const { |
| + if (font_style_ == -1) |
| + CacheFontStyleAndSize(); |
| + return font_style_; |
| } |
| const std::string& FontList::GetFontDescriptionString() const { |
| @@ -209,15 +209,9 @@ const std::string& FontList::GetFontDescriptionString() const { |
| } |
| int FontList::GetFontSize() const { |
| - if (!fonts_.empty()) |
| - return fonts_[0].GetFontSize(); |
| - |
| - std::vector<std::string> font_names; |
| - int font_style; |
| - int font_size; |
| - ParseFontDescriptionString(font_description_string_, &font_names, |
| - &font_style, &font_size); |
| - return font_size; |
| + if (font_size_ == -1) |
| + CacheFontStyleAndSize(); |
| + return font_size_; |
| } |
| const std::vector<Font>& FontList::GetFonts() const { |
| @@ -225,18 +219,16 @@ const std::vector<Font>& FontList::GetFonts() const { |
| DCHECK(!font_description_string_.empty()); |
| std::vector<std::string> font_names; |
| - int font_style; |
| - int font_size; |
| ParseFontDescriptionString(font_description_string_, &font_names, |
| - &font_style, &font_size); |
| + &font_style_, &font_size_); |
| for (size_t i = 0; i < font_names.size(); ++i) { |
| DCHECK(!font_names[i].empty()); |
| - Font font(font_names[i], font_size); |
| - if (font_style == Font::NORMAL) |
| + Font font(font_names[i], font_size_); |
| + if (font_style_ == Font::NORMAL) |
| fonts_.push_back(font); |
| else |
| - fonts_.push_back(font.DeriveFont(0, font_style)); |
| + fonts_.push_back(font.DeriveFont(0, font_style_)); |
| } |
| } |
| return fonts_; |
| @@ -246,4 +238,28 @@ const Font& FontList::GetPrimaryFont() const { |
| return GetFonts()[0]; |
| } |
| +void FontList::CacheCommonFontHeightAndBaseline() const { |
| + int ascent = 0; |
| + int descent = 0; |
| + const std::vector<Font>& fonts = GetFonts(); |
| + for (std::vector<Font>::const_iterator i = fonts.begin(); |
| + i != fonts.end(); ++i) { |
| + ascent = std::max(ascent, i->GetBaseline()); |
| + descent = std::max(descent, i->GetHeight() - i->GetBaseline()); |
| + } |
| + common_height_ = ascent + descent; |
| + common_baseline_ = ascent; |
| +} |
| + |
| +void FontList::CacheFontStyleAndSize() const { |
| + if (!fonts_.empty()) { |
| + font_style_ = fonts_[0].GetStyle(); |
| + font_size_ = fonts_[0].GetFontSize(); |
| + } else { |
| + std::vector<std::string> font_names; |
| + ParseFontDescriptionString(font_description_string_, &font_names, |
| + &font_style_, &font_size_); |
| + } |
| +} |
| + |
| } // namespace gfx |