summaryrefslogtreecommitdiffstats
path: root/app/text_elider.cc
diff options
context:
space:
mode:
authorpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-05-20 18:28:00 +0000
committerpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-05-20 18:28:00 +0000
commit68da8f78cd06ba70144c9e3fb2e1bae724208e6a (patch)
tree695907a99af87ad2c9ff646893e11588a447e41d /app/text_elider.cc
parent6658ca866d0e28950179a65179c1a4d6e8e3f8df (diff)
downloadchromium_src-68da8f78cd06ba70144c9e3fb2e1bae724208e6a.zip
chromium_src-68da8f78cd06ba70144c9e3fb2e1bae724208e6a.tar.gz
chromium_src-68da8f78cd06ba70144c9e3fb2e1bae724208e6a.tar.bz2
Elide the EV bubble when it's extremely long. This limits it to half the location bar width, unless eliding to that would result in a width of less than 150 px.
BUG=42856 TEST=Visit https://www.barbican.org.uk/eticketing/index.asp and make the window smaller. The EV bubble should shrink, eliding in middle, until it hits a minimum size. Review URL: http://codereview.chromium.org/2084012 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@47819 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app/text_elider.cc')
-rw-r--r--app/text_elider.cc73
1 files changed, 45 insertions, 28 deletions
diff --git a/app/text_elider.cc b/app/text_elider.cc
index ccdb3e33..c8ded1e 100644
--- a/app/text_elider.cc
+++ b/app/text_elider.cc
@@ -17,8 +17,30 @@
#include "net/base/net_util.h"
#include "net/base/registry_controlled_domain.h"
+namespace {
+
const wchar_t kEllipsis[] = L"\x2026";
+// Cuts |text| to be |length| characters long. If |cut_in_middle| is true, the
+// middle of the string is removed to leave equal-length pieces from the
+// beginning and end of the string; otherwise, the end of the string is removed
+// and only the beginning remains. If |insert_ellipsis| is true, then an
+// ellipsis character will by inserted at the cut point.
+std::wstring CutString(const std::wstring& text,
+ size_t length,
+ bool cut_in_middle,
+ bool insert_ellipsis) {
+ const std::wstring insert(insert_ellipsis ? kEllipsis : L"");
+ if (!cut_in_middle)
+ return text.substr(0, length) + insert;
+ // We put the extra character, if any, before the cut.
+ const size_t half_length = length / 2;
+ return text.substr(0, length - half_length) + insert +
+ text.substr(text.length() - half_length, half_length);
+}
+
+} // namespace
+
namespace gfx {
// This function takes a GURL object and elides it. It returns a string
@@ -44,7 +66,7 @@ std::wstring ElideUrl(const GURL& url,
// If non-standard or not file type, return plain eliding.
if (!(url.SchemeIsFile() || url.IsStandard()))
- return ElideText(url_string, font, available_pixel_width);
+ return ElideText(url_string, font, available_pixel_width, false);
// Now start eliding url_string to fit within available pixel width.
// Fist pass - check to see whether entire url_string fits.
@@ -62,7 +84,7 @@ std::wstring ElideUrl(const GURL& url,
std::wstring url_minus_query = url_string.substr(0, path_start_index +
path_len);
if (available_pixel_width >= font.GetStringWidth(url_minus_query))
- return ElideText(url_string, font, available_pixel_width);
+ return ElideText(url_string, font, available_pixel_width, false);
// Get Host.
std::wstring url_host = UTF8ToWide(url.host());
@@ -130,7 +152,7 @@ std::wstring ElideUrl(const GURL& url,
pixel_width_url_domain + pixel_width_url_path -
font.GetStringWidth(url_query))) {
return ElideText(url_subdomain + url_domain + url_path_query_etc, font,
- available_pixel_width);
+ available_pixel_width, false);
}
}
@@ -157,7 +179,7 @@ std::wstring ElideUrl(const GURL& url,
// No path to elide, or too long of a path (could overflow in loop below)
// Just elide this as a text string.
return ElideText(url_subdomain + url_domain + url_path_query_etc, font,
- available_pixel_width);
+ available_pixel_width, false);
}
// Start eliding the path and replacing elements by "../".
@@ -199,7 +221,7 @@ std::wstring ElideUrl(const GURL& url,
pixel_width_url_subdomain + pixel_width_url_domain +
pixel_width_elided_path) {
return ElideText(url_subdomain + url_domain + elided_path + url_query,
- font, available_pixel_width);
+ font, available_pixel_width, false);
}
}
@@ -241,7 +263,7 @@ std::wstring ElideUrl(const GURL& url,
if (available_pixel_width >=
pixel_width_url_elided_domain + pixel_width_elided_path) {
return ElideText(url_elided_domain + elided_path + url_query, font,
- available_pixel_width);
+ available_pixel_width, false);
}
}
}
@@ -255,7 +277,7 @@ std::wstring ElideUrl(const GURL& url,
else
final_elided_url_string += url_path;
- return ElideText(final_elided_url_string, font, available_pixel_width);
+ return ElideText(final_elided_url_string, font, available_pixel_width, false);
}
std::wstring ElideFilename(const FilePath& filename,
@@ -277,7 +299,7 @@ std::wstring ElideFilename(const FilePath& filename,
if (rootname.empty() || extension.empty()) {
std::wstring elided_name = ElideText(filename.ToWStringHack(), font,
- available_pixel_width);
+ available_pixel_width, false);
return base::i18n::GetDisplayStringInLTRDirectionality(&elided_name);
}
@@ -291,7 +313,8 @@ std::wstring ElideFilename(const FilePath& filename,
}
int available_root_width = available_pixel_width - ext_width;
- std::wstring elided_name = ElideText(rootname, font, available_root_width);
+ std::wstring elided_name =
+ ElideText(rootname, font, available_root_width, false);
elided_name += extension;
return base::i18n::GetDisplayStringInLTRDirectionality(&elided_name);
}
@@ -300,7 +323,8 @@ std::wstring ElideFilename(const FilePath& filename,
// does not fit the given pixel width.
std::wstring ElideText(const std::wstring& text,
const gfx::Font& font,
- int available_pixel_width) {
+ int available_pixel_width,
+ bool elide_in_middle) {
if (text.empty())
return text;
@@ -315,8 +339,8 @@ std::wstring ElideText(const std::wstring& text,
// (eliding way too much from a ridiculous string is probably still
// ridiculous), but we should check other widths for bogus values as well.
if (current_text_pixel_width <= 0 && !text.empty()) {
- return ElideText(text.substr(0, text.length() / 2), font,
- available_pixel_width);
+ return ElideText(CutString(text, text.length() / 2, elide_in_middle, false),
+ font, available_pixel_width, false);
}
if (current_text_pixel_width <= available_pixel_width)
@@ -328,31 +352,24 @@ std::wstring ElideText(const std::wstring& text,
// Use binary search to compute the elided text.
size_t lo = 0;
size_t hi = text.length() - 1;
- size_t guess = hi / 2;
- while (lo < hi) {
+ for (size_t guess = (lo + hi) / 2; guess != lo; guess = (lo + hi) / 2) {
// We check the length of the whole desired string at once to ensure we
// handle kerning/ligatures/etc. correctly.
- std::wstring guess_str = text.substr(0, guess) + kEllipsis;
- int guess_length = font.GetStringWidth(guess_str);
+ int guess_length =
+ font.GetStringWidth(CutString(text, guess, elide_in_middle, true));
// Check again that we didn't hit a Pango width overflow. If so, cut the
// current string in half and start over.
if (guess_length <= 0) {
- return ElideText(guess_str.substr(0, guess_str.length() / 2), font,
- available_pixel_width);
+ return ElideText(CutString(text, guess / 2, elide_in_middle, false),
+ font, available_pixel_width, elide_in_middle);
}
- if (guess_length > available_pixel_width) {
- if (hi == guess)
- break;
+ if (guess_length > available_pixel_width)
hi = guess;
- } else {
- if (lo == guess)
- break;
+ else
lo = guess;
- }
- guess = (lo + hi) / 2;
}
- return text.substr(0, lo) + kEllipsis;
+ return CutString(text, lo, elide_in_middle, true);
}
SortedDisplayURL::SortedDisplayURL(const GURL& url,
@@ -421,4 +438,4 @@ string16 SortedDisplayURL::AfterHost() const {
return display_url_.substr(slash_index + sort_host_.length());
}
-} // namespace gfx.
+} // namespace gfx