diff options
author | yukishiino@chromium.org <yukishiino@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-08-22 11:41:30 +0000 |
---|---|---|
committer | yukishiino@chromium.org <yukishiino@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-08-22 11:41:30 +0000 |
commit | 372416bb6add4562000c213504090cc2f9333623 (patch) | |
tree | 8268a7b806e0fae645df298e6643c07f920276c3 /ui | |
parent | a8c8ce11833e91ff6491b67077346980b7380012 (diff) | |
download | chromium_src-372416bb6add4562000c213504090cc2f9333623.zip chromium_src-372416bb6add4562000c213504090cc2f9333623.tar.gz chromium_src-372416bb6add4562000c213504090cc2f9333623.tar.bz2 |
Adds gfx::FontList version of APIs to views::Label.
Changes in this CL are:
- views::Label holds gfx::FontList instead of gfx::Font.
- Adds new methods to support gfx::FontList.
- Redirects old methods (font(), SetFont(font)) to new methods.
This is a part of the plan to support gfx::FontList in views::Label.
https://docs.google.com/a/chromium.org/document/d/1D_25fp9B8b9aZJORfAjDIFq61NWvUquZ5xmKH-VcC4k/edit
After this CL, I'll do following things:
- Make client code of Label call new methods.
- Remove old methods in Label.
- Improve rendering code using FontList.
BUG=265485
Review URL: https://chromiumcodereview.appspot.com/22546005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@218996 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/views/controls/label.cc | 60 | ||||
-rw-r--r-- | ui/views/controls/label.h | 22 | ||||
-rw-r--r-- | ui/views/controls/label_unittest.cc | 16 |
3 files changed, 54 insertions, 44 deletions
diff --git a/ui/views/controls/label.cc b/ui/views/controls/label.cc index 3e81ce5..05f9aed 100644 --- a/ui/views/controls/label.cc +++ b/ui/views/controls/label.cc @@ -19,9 +19,9 @@ #include "ui/base/text/text_elider.h" #include "ui/gfx/canvas.h" #include "ui/gfx/color_utils.h" -#include "ui/gfx/font.h" #include "ui/gfx/insets.h" #include "ui/gfx/shadow_value.h" +#include "ui/gfx/text_utils.h" #include "ui/native_theme/native_theme.h" #include "ui/views/background.h" @@ -31,6 +31,11 @@ namespace { const int kFocusBorderPadding = 1; const int kCachedSizeLimit = 10; +gfx::FontList GetDefaultFontList() { + return ui::ResourceBundle::GetSharedInstance().GetFontList( + ui::ResourceBundle::BaseFont); +} + } // namespace namespace views { @@ -39,27 +44,39 @@ namespace views { const char Label::kViewClassName[] = "Label"; Label::Label() { - Init(string16(), GetDefaultFont()); + Init(string16(), GetDefaultFontList()); } Label::Label(const string16& text) { - Init(text, GetDefaultFont()); + Init(text, GetDefaultFontList()); +} + +Label::Label(const string16& text, const gfx::FontList& font_list) { + Init(text, font_list); } Label::Label(const string16& text, const gfx::Font& font) { - Init(text, font); + Init(text, gfx::FontList(font)); } Label::~Label() { } -void Label::SetFont(const gfx::Font& font) { - font_ = font; +void Label::SetFontList(const gfx::FontList& font_list) { + font_list_ = font_list; ResetCachedSize(); PreferredSizeChanged(); SchedulePaint(); } +const gfx::Font& Label::font() const { + return font_list_.GetPrimaryFont(); +} + +void Label::SetFont(const gfx::Font& font) { + SetFontList(gfx::FontList(font)); +} + void Label::SetText(const string16& text) { if (text == text_) return; @@ -172,7 +189,7 @@ void Label::SizeToFit(int max_width) { int label_width = 0; for (std::vector<string16>::const_iterator iter = lines.begin(); iter != lines.end(); ++iter) { - label_width = std::max(label_width, font_.GetStringWidth(*iter)); + label_width = std::max(label_width, gfx::GetStringWidth(*iter, font_list_)); } label_width += GetInsets().width(); @@ -202,7 +219,7 @@ gfx::Insets Label::GetInsets() const { } int Label::GetBaseline() const { - return GetInsets().top() + font_.GetBaseline(); + return GetInsets().top() + font_list_.GetBaseline(); } gfx::Size Label::GetPreferredSize() { @@ -234,9 +251,9 @@ int Label::GetHeightForWidth(int w) { int cache_width = w; - int h = font_.GetHeight(); + int h = font_list_.GetHeight(); const int flags = ComputeDrawStringFlags(); - gfx::Canvas::SizeStringInt(text_, font_, &w, &h, line_height_, flags); + gfx::Canvas::SizeStringInt(text_, font_list_, &w, &h, line_height_, flags); cached_heights_[cached_heights_cursor_] = gfx::Size(cache_width, h); cached_heights_cursor_ = (cached_heights_cursor_ + 1) % kCachedSizeLimit; return h + GetInsets().height(); @@ -297,7 +314,7 @@ void Label::PaintText(gfx::Canvas* canvas, if (has_shadow_) shadows.push_back(gfx::ShadowValue(shadow_offset_, 0, enabled() ? enabled_shadow_color_ : disabled_shadow_color_)); - canvas->DrawStringWithShadows(text, font_, + canvas->DrawStringRectWithShadows(text, font_list_, enabled() ? actual_enabled_color_ : actual_disabled_color_, text_bounds, line_height_, flags, shadows); @@ -316,13 +333,13 @@ gfx::Size Label::GetTextSize() const { // on Linux. int w = is_multi_line_ ? GetAvailableRect().width() : std::numeric_limits<int>::max(); - int h = font_.GetHeight(); + int h = font_list_.GetHeight(); // For single-line strings, ignore the available width and calculate how // wide the text wants to be. int flags = ComputeDrawStringFlags(); if (!is_multi_line_) flags |= gfx::Canvas::NO_ELLIPSIS; - gfx::Canvas::SizeStringInt(text_, font_, &w, &h, line_height_, flags); + gfx::Canvas::SizeStringInt(text_, font_list_, &w, &h, line_height_, flags); text_size_.SetSize(w, h); text_size_valid_ = true; } @@ -352,13 +369,8 @@ void Label::OnNativeThemeChanged(const ui::NativeTheme* theme) { UpdateColorsFromTheme(theme); } -// static -gfx::Font Label::GetDefaultFont() { - return ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont); -} - -void Label::Init(const string16& text, const gfx::Font& font) { - font_ = font; +void Label::Init(const string16& text, const gfx::FontList& font_list) { + font_list_ = font_list; enabled_color_set_ = disabled_color_set_ = background_color_set_ = false; auto_color_readability_ = true; UpdateColorsFromTheme(ui::NativeTheme::instance()); @@ -483,14 +495,14 @@ void Label::CalculateDrawStringParams(string16* paint_text, if (is_multi_line_ || (elide_behavior_ == NO_ELIDE)) { *paint_text = text_; } else if (elide_behavior_ == ELIDE_IN_MIDDLE) { - *paint_text = ui::ElideText(text_, font_, GetAvailableRect().width(), + *paint_text = ui::ElideText(text_, font_list_, GetAvailableRect().width(), ui::ELIDE_IN_MIDDLE); } else if (elide_behavior_ == ELIDE_AT_END) { - *paint_text = ui::ElideText(text_, font_, GetAvailableRect().width(), + *paint_text = ui::ElideText(text_, font_list_, GetAvailableRect().width(), ui::ELIDE_AT_END); } else { DCHECK_EQ(ELIDE_AS_EMAIL, elide_behavior_); - *paint_text = ui::ElideEmail(text_, font_, GetAvailableRect().width()); + *paint_text = ui::ElideEmail(text_, font_list_, GetAvailableRect().width()); } *text_bounds = GetTextBounds(); @@ -524,7 +536,7 @@ void Label::ResetCachedSize() { bool Label::ShouldShowDefaultTooltip() const { return !is_multi_line_ && - font_.GetStringWidth(text_) > GetAvailableRect().width(); + gfx::GetStringWidth(text_, font_list_) > GetAvailableRect().width(); } } // namespace views diff --git a/ui/views/controls/label.h b/ui/views/controls/label.h index d637d7f..40534ff 100644 --- a/ui/views/controls/label.h +++ b/ui/views/controls/label.h @@ -12,7 +12,7 @@ #include "base/gtest_prod_util.h" #include "base/strings/string16.h" #include "third_party/skia/include/core/SkColor.h" -#include "ui/gfx/font.h" +#include "ui/gfx/font_list.h" #include "ui/gfx/text_constants.h" #include "ui/views/view.h" @@ -54,12 +54,16 @@ class VIEWS_EXPORT Label : public View { Label(); explicit Label(const string16& text); - Label(const string16& text, const gfx::Font& font); + Label(const string16& text, const gfx::FontList& font_list); + Label(const string16& text, const gfx::Font& font); // OBSOLETE virtual ~Label(); - // Get or set the font used by this label. - const gfx::Font& font() const { return font_; } - virtual void SetFont(const gfx::Font& font); + // Gets or sets the fonts used by this label. + const gfx::FontList& font_list() const { return font_list_; } + virtual void SetFontList(const gfx::FontList& font_list); + // Obsolete gfx::Font version. Should use gfx::FontList version instead. + const gfx::Font& font() const; // OBSOLETE + virtual void SetFont(const gfx::Font& font); // OBSOLETE // Get or set the label text. const string16& text() const { return text_; } @@ -119,7 +123,7 @@ class VIEWS_EXPORT Label : public View { // Get or set the distance in pixels between baselines of multi-line text. // Default is 0, indicating the distance between lines should be the standard - // one for the label's text, font, and platform. + // one for the label's text, font list, and platform. int line_height() const { return line_height_; } void SetLineHeight(int height); @@ -208,9 +212,7 @@ class VIEWS_EXPORT Label : public View { // Calls ComputeDrawStringFlags(). FRIEND_TEST_ALL_PREFIXES(LabelTest, DisableSubpixelRendering); - static gfx::Font GetDefaultFont(); - - void Init(const string16& text, const gfx::Font& font); + void Init(const string16& text, const gfx::FontList& font_list); void RecalculateColors(); @@ -236,7 +238,7 @@ class VIEWS_EXPORT Label : public View { bool ShouldShowDefaultTooltip() const; string16 text_; - gfx::Font font_; + gfx::FontList font_list_; SkColor requested_enabled_color_; SkColor actual_enabled_color_; SkColor requested_disabled_color_; diff --git a/ui/views/controls/label_unittest.cc b/ui/views/controls/label_unittest.cc index 134a05d..be75281 100644 --- a/ui/views/controls/label_unittest.cc +++ b/ui/views/controls/label_unittest.cc @@ -16,17 +16,13 @@ namespace views { // All text sizing measurements (width and height) should be greater than this. const int kMinTextDimension = 4; -TEST(LabelTest, FontPropertyCourier) { +TEST(LabelTest, FontPropertySymbol) { Label label; - std::string font_name("courier"); - // Note: This test is size dependent since Courier does not support all sizes. + std::string font_name("symbol"); gfx::Font font(font_name, 26); - label.SetFont(font); - gfx::Font font_used = label.font(); -#if defined(OS_WIN) - // On Linux, this results in "Sans" instead of "courier". + label.SetFontList(gfx::FontList(font)); + gfx::Font font_used = label.font_list().GetPrimaryFont(); EXPECT_EQ(font_name, font_used.GetFontName()); -#endif EXPECT_EQ(26, font_used.GetFontSize()); } @@ -34,8 +30,8 @@ TEST(LabelTest, FontPropertyArial) { Label label; std::string font_name("arial"); gfx::Font font(font_name, 30); - label.SetFont(font); - gfx::Font font_used = label.font(); + label.SetFontList(gfx::FontList(font)); + gfx::Font font_used = label.font_list().GetPrimaryFont(); EXPECT_EQ(font_name, font_used.GetFontName()); EXPECT_EQ(30, font_used.GetFontSize()); } |