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..3b4c56d77fcd05713fa5b67694ddfe87a3b52916 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)), |
|
Alexei Svitkine (slow)
2013/08/06 15:15:28
I think font_style can go on the previous line and
Yuki
2013/08/07 14:34:03
Done.
|
| + 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,49 +131,38 @@ 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 { |
| @@ -181,16 +193,33 @@ int FontList::GetBaseline() const { |
| return common_baseline_; |
| } |
| -int FontList::GetFontStyle() const { |
| - if (!fonts_.empty()) |
| - return fonts_[0].GetStyle(); |
| +int FontList::GetAverageCharacterWidth() const { |
| + // Rely on the primary font metrics for the time being. |
| + return GetPrimaryFont().GetAverageCharacterWidth(); |
| +} |
| - 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::GetStringWidth(const base::string16& text) const { |
| + // Rely on the primary font metrics for the time being. |
| + // TODO(yukishiino): implements this based on the actual font list. |
|
msw
2013/08/06 05:10:14
nit: Remove this TODO, I think the comment above i
Yuki
2013/08/07 14:34:03
Done.
|
| + return GetPrimaryFont().GetStringWidth(text); |
| +} |
| + |
| +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_ < 0) { |
|
Alexei Svitkine (slow)
2013/08/06 15:15:28
Compare explicitly to -1.
Yuki
2013/08/07 14:34:03
Done.
|
| + if (!fonts_.empty()) |
|
Alexei Svitkine (slow)
2013/08/06 15:15:28
This should have {}'s.
Yuki
2013/08/07 14:34:03
Done.
|
| + font_style_ = fonts_[0].GetStyle(); |
| + else { |
| + std::vector<std::string> font_names; |
| + ParseFontDescriptionString(font_description_string_, &font_names, |
| + &font_style_, &font_size_); |
| + } |
| + } |
| + return font_style_; |
| } |
| const std::string& FontList::GetFontDescriptionString() const { |
| @@ -209,15 +238,16 @@ 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_ < 0) { |
| + if (!fonts_.empty()) |
|
Alexei Svitkine (slow)
2013/08/06 15:15:28
This block is almost identical to the one in GetFo
Yuki
2013/08/07 14:34:03
Thanks for the comment. Applied the same thing to
|
| + font_size_ = fonts_[0].GetFontSize(); |
| + else { |
| + std::vector<std::string> font_names; |
| + ParseFontDescriptionString(font_description_string_, &font_names, |
| + &font_style_, &font_size_); |
| + } |
| + } |
| + return font_size_; |
| } |
| const std::vector<Font>& FontList::GetFonts() const { |