diff options
author | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-20 18:28:00 +0000 |
---|---|---|
committer | pkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-20 18:28:00 +0000 |
commit | 68da8f78cd06ba70144c9e3fb2e1bae724208e6a (patch) | |
tree | 695907a99af87ad2c9ff646893e11588a447e41d /app | |
parent | 6658ca866d0e28950179a65179c1a4d6e8e3f8df (diff) | |
download | chromium_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')
-rw-r--r-- | app/text_elider.cc | 73 | ||||
-rw-r--r-- | app/text_elider.h | 14 | ||||
-rw-r--r-- | app/text_elider_unittest.cc | 60 |
3 files changed, 96 insertions, 51 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 diff --git a/app/text_elider.h b/app/text_elider.h index f3d0246..1378f3b 100644 --- a/app/text_elider.h +++ b/app/text_elider.h @@ -1,9 +1,9 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef APP_GFX_TEXT_ELIDER_H_ -#define APP_GFX_TEXT_ELIDER_H_ +#ifndef APP_TEXT_ELIDER_H_ +#define APP_TEXT_ELIDER_H_ #include <unicode/coll.h> #include <unicode/uchar.h> @@ -36,9 +36,13 @@ std::wstring ElideUrl(const GURL& url, int available_pixel_width, const std::wstring& languages); +// Elides |text| to fit in |available_pixel_width|. If |elide_in_middle| is +// set the ellipsis is placed in the middle of the string; otherwise it is +// placed at the end. std::wstring ElideText(const std::wstring& text, const gfx::Font& font, - int available_pixel_width); + int available_pixel_width, + bool elide_in_middle); // Elide a filename to fit a given pixel width, with an emphasis on not hiding // the extension unless we have to. If filename contains a path, the path will @@ -84,4 +88,4 @@ class SortedDisplayURL { } // namespace gfx. -#endif // APP_GFX_TEXT_ELIDER_H_ +#endif // APP_TEXT_ELIDER_H_ diff --git a/app/text_elider_unittest.cc b/app/text_elider_unittest.cc index cc421fb..0eacfd2 100644 --- a/app/text_elider_unittest.cc +++ b/app/text_elider_unittest.cc @@ -189,6 +189,7 @@ TEST(TextEliderTest, TestFilenameEliding) { TEST(TextEliderTest, ElideTextLongStrings) { const std::wstring kEllipsisStr(kEllipsis); std::wstring data_scheme(L"data:text/plain,"); + size_t data_scheme_length = data_scheme.length(); std::wstring ten_a(10, L'a'); std::wstring hundred_a(100, L'a'); @@ -197,31 +198,54 @@ TEST(TextEliderTest, ElideTextLongStrings) { std::wstring hundred_thousand_a(100000, L'a'); std::wstring million_a(1000000, L'a'); - WideTestcase testcases[] = { - {data_scheme + ten_a, - data_scheme + ten_a}, - {data_scheme + hundred_a, - data_scheme + hundred_a}, - {data_scheme + thousand_a, - data_scheme + std::wstring(156, L'a') + kEllipsisStr}, - {data_scheme + ten_thousand_a, - data_scheme + std::wstring(156, L'a') + kEllipsisStr}, - {data_scheme + hundred_thousand_a, - data_scheme + std::wstring(156, L'a') + kEllipsisStr}, - {data_scheme + million_a, - data_scheme + std::wstring(156, L'a') + kEllipsisStr}, + size_t number_of_as = 156; + std::wstring long_string_end( + data_scheme + std::wstring(number_of_as, L'a') + kEllipsisStr); + WideTestcase testcases_end[] = { + {data_scheme + ten_a, data_scheme + ten_a}, + {data_scheme + hundred_a, data_scheme + hundred_a}, + {data_scheme + thousand_a, long_string_end}, + {data_scheme + ten_thousand_a, long_string_end}, + {data_scheme + hundred_thousand_a, long_string_end}, + {data_scheme + million_a, long_string_end}, }; const gfx::Font font; int ellipsis_width = font.GetStringWidth(kEllipsisStr); - for (size_t i = 0; i < arraysize(testcases); ++i) { + for (size_t i = 0; i < arraysize(testcases_end); ++i) { + // Compare sizes rather than actual contents because if the test fails, + // output is rather long. + EXPECT_EQ(testcases_end[i].output.size(), + ElideText(testcases_end[i].input, font, + font.GetStringWidth(testcases_end[i].output), + false).size()); + EXPECT_EQ(kEllipsisStr, + ElideText(testcases_end[i].input, font, ellipsis_width, false)); + } + + size_t number_of_trailing_as = (data_scheme_length + number_of_as) / 2; + std::wstring long_string_middle(data_scheme + + std::wstring(number_of_as - number_of_trailing_as, L'a') + kEllipsisStr + + std::wstring(number_of_trailing_as, L'a')); + WideTestcase testcases_middle[] = { + {data_scheme + ten_a, data_scheme + ten_a}, + {data_scheme + hundred_a, data_scheme + hundred_a}, + {data_scheme + thousand_a, long_string_middle}, + {data_scheme + ten_thousand_a, long_string_middle}, + {data_scheme + hundred_thousand_a, long_string_middle}, + {data_scheme + million_a, long_string_middle}, + }; + + for (size_t i = 0; i < arraysize(testcases_middle); ++i) { // Compare sizes rather than actual contents because if the test fails, // output is rather long. - EXPECT_EQ(testcases[i].output.size(), - ElideText(testcases[i].input, font, - font.GetStringWidth(testcases[i].output)).size()); + EXPECT_EQ(testcases_middle[i].output.size(), + ElideText(testcases_middle[i].input, font, + font.GetStringWidth(testcases_middle[i].output), + false).size()); EXPECT_EQ(kEllipsisStr, - ElideText(testcases[i].input, font, ellipsis_width)); + ElideText(testcases_middle[i].input, font, ellipsis_width, + false)); } } |