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

Unified Diff: ui/views/controls/label.cc

Issue 24883002: Uses and returns the fractional width in text eliding (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix round-down problems Created 7 years, 2 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 side-by-side diff with in-line comments
Download patch
Index: ui/views/controls/label.cc
diff --git a/ui/views/controls/label.cc b/ui/views/controls/label.cc
index 7d4bc7e84703c03dfcbb50ed1f231e991e3b7590..550d2b95dd3af11dd82429900b97e00b786a3adb 100644
--- a/ui/views/controls/label.cc
+++ b/ui/views/controls/label.cc
@@ -186,7 +186,7 @@ void Label::SizeToFit(int max_width) {
std::vector<string16> lines;
base::SplitString(text_, '\n', &lines);
- int label_width = 0;
+ float label_width = 0;
for (std::vector<string16>::const_iterator iter = lines.begin();
iter != lines.end(); ++iter) {
label_width = std::max(label_width, gfx::GetStringWidth(*iter, font_list_));
@@ -195,9 +195,9 @@ void Label::SizeToFit(int max_width) {
label_width += GetInsets().width();
if (max_width > 0)
- label_width = std::min(label_width, max_width);
+ label_width = std::min<float>(label_width, max_width);
- SetBounds(x(), y(), label_width, 0);
+ SetBounds(x(), y(), std::ceil(label_width), 0);
SizeToPreferredSize();
}
@@ -249,12 +249,11 @@ int Label::GetHeightForWidth(int w) {
return s.height() + GetInsets().height();
}
- int cache_width = w;
-
- int h = font_list_.GetHeight();
+ float fw = w;
+ float h = font_list_.GetHeight();
const int flags = ComputeDrawStringFlags();
- gfx::Canvas::SizeStringInt(text_, font_list_, &w, &h, line_height_, flags);
- cached_heights_[cached_heights_cursor_] = gfx::Size(cache_width, h);
+ gfx::Canvas::SizeStringToFit(text_, font_list_, &fw, &h, line_height_, flags);
+ cached_heights_[cached_heights_cursor_] = gfx::Size(w, h);
cached_heights_cursor_ = (cached_heights_cursor_ + 1) % kCachedSizeLimit;
return h + GetInsets().height();
}
@@ -329,17 +328,18 @@ gfx::Size Label::GetTextSize() const {
if (!text_size_valid_) {
// For single-line strings, we supply the largest possible width, because
// while adding NO_ELLIPSIS to the flags works on Windows for forcing
- // SizeStringInt() to calculate the desired width, it doesn't seem to work
+ // SizeStringToFit() to calculate the desired width, it doesn't seem to work
// on Linux.
- int w = is_multi_line_ ?
+ float w = is_multi_line_ ?
GetAvailableRect().width() : std::numeric_limits<int>::max();
- int h = font_list_.GetHeight();
+ float 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_list_, &w, &h, line_height_, flags);
+ gfx::Canvas::SizeStringToFit(
+ text_, font_list_, &w, &h, line_height_, flags);
text_size_.SetSize(w, h);
text_size_valid_ = true;
}

Powered by Google App Engine
This is Rietveld 408576698