diff options
author | tony@chromium.org <tony@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-21 22:02:55 +0000 |
---|---|---|
committer | tony@chromium.org <tony@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-21 22:02:55 +0000 |
commit | d6b671d0968013c3205c50ebaf45f356e34a58c5 (patch) | |
tree | 52b215053c36e30e97de5ff97edb309700735ef0 /app | |
parent | c5af1c1af7e02b4998b085d29ecf8249f679e50a (diff) | |
download | chromium_src-d6b671d0968013c3205c50ebaf45f356e34a58c5.zip chromium_src-d6b671d0968013c3205c50ebaf45f356e34a58c5.tar.gz chromium_src-d6b671d0968013c3205c50ebaf45f356e34a58c5.tar.bz2 |
Convert gfx::ElideText from using wstrings to string16.
BUG=23581
Review URL: http://codereview.chromium.org/3885003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@63433 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app')
-rw-r--r-- | app/text_elider.cc | 62 | ||||
-rw-r--r-- | app/text_elider.h | 8 | ||||
-rw-r--r-- | app/text_elider_unittest.cc | 44 |
3 files changed, 63 insertions, 51 deletions
diff --git a/app/text_elider.cc b/app/text_elider.cc index 4f7ddfb..96a7593 100644 --- a/app/text_elider.cc +++ b/app/text_elider.cc @@ -26,16 +26,18 @@ const wchar_t kEllipsis[] = L"\x2026"; // 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, +string16 CutString(const string16& text, size_t length, bool cut_in_middle, bool insert_ellipsis) { - const std::wstring insert(insert_ellipsis ? kEllipsis : L""); + // TODO(tony): This is wrong, it might split the string in the middle of a + // surrogate pair. + const string16 kInsert = WideToUTF16(insert_ellipsis ? kEllipsis : L""); if (!cut_in_middle) - return text.substr(0, length) + insert; + return text.substr(0, length) + kInsert; // 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 + + return text.substr(0, length - half_length) + kInsert + text.substr(text.length() - half_length, half_length); } @@ -44,6 +46,13 @@ string16 GetDisplayStringInLTRDirectionality(const std::wstring& text) { return base::i18n::GetDisplayStringInLTRDirectionality(WideToUTF16(text)); } +// TODO(tony): This is just a crutch until we convert ElideUrl to string16. +std::wstring ElideTextWide(const std::wstring& text, const gfx::Font& font, + int available_pixel_width, bool elide_in_middle) { + return UTF16ToWideHack(ElideText(WideToUTF16Hack(text), font, + available_pixel_width, elide_in_middle)); +} + } // namespace namespace gfx { @@ -72,7 +81,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, false); + return ElideTextWide(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. @@ -90,7 +99,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, false); + return ElideTextWide(url_string, font, available_pixel_width, false); // Get Host. std::wstring url_host = UTF8ToWide(url.host()); @@ -157,8 +166,8 @@ std::wstring ElideUrl(const GURL& url, if (available_pixel_width >= (pixel_width_url_subdomain + 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, false); + return ElideTextWide(url_subdomain + url_domain + url_path_query_etc, + font, available_pixel_width, false); } } @@ -184,8 +193,8 @@ std::wstring ElideUrl(const GURL& url, url_path_number_of_elements > kMaxNumberOfUrlPathElementsAllowed) { // 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, false); + return ElideTextWide(url_subdomain + url_domain + url_path_query_etc, font, + available_pixel_width, false); } // Start eliding the path and replacing elements by "../". @@ -226,8 +235,8 @@ std::wstring ElideUrl(const GURL& url, if (available_pixel_width >= 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, false); + return ElideTextWide(url_subdomain + url_domain + elided_path + url_query, + font, available_pixel_width, false); } } @@ -268,8 +277,8 @@ 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, false); + return ElideTextWide(url_elided_domain + elided_path + url_query, font, + available_pixel_width, false); } } } @@ -283,7 +292,8 @@ std::wstring ElideUrl(const GURL& url, else final_elided_url_string += url_path; - return ElideText(final_elided_url_string, font, available_pixel_width, false); + return ElideTextWide(final_elided_url_string, font, available_pixel_width, + false); } string16 ElideFilename(const FilePath& filename, @@ -304,8 +314,8 @@ string16 ElideFilename(const FilePath& filename, filename.BaseName().RemoveExtension().ToWStringHack(); if (rootname.empty() || extension.empty()) { - std::wstring elided_name = ElideText(filename.ToWStringHack(), font, - available_pixel_width, false); + std::wstring elided_name = ElideTextWide(filename.ToWStringHack(), font, + available_pixel_width, false); return GetDisplayStringInLTRDirectionality(elided_name); } @@ -320,21 +330,21 @@ string16 ElideFilename(const FilePath& filename, int available_root_width = available_pixel_width - ext_width; std::wstring elided_name = - ElideText(rootname, font, available_root_width, false); + ElideTextWide(rootname, font, available_root_width, false); elided_name += extension; return GetDisplayStringInLTRDirectionality(elided_name); } // This function adds an ellipsis at the end of the text if the text // does not fit the given pixel width. -std::wstring ElideText(const std::wstring& text, - const gfx::Font& font, - int available_pixel_width, - bool elide_in_middle) { +string16 ElideText(const string16& text, + const gfx::Font& font, + int available_pixel_width, + bool elide_in_middle) { if (text.empty()) return text; - int current_text_pixel_width = font.GetStringWidth(text); + int current_text_pixel_width = font.GetStringWidth(UTF16ToWideHack(text)); // Pango will return 0 width for absurdly long strings. Cut the string in // half and try again. @@ -353,7 +363,7 @@ std::wstring ElideText(const std::wstring& text, return text; if (font.GetStringWidth(kEllipsis) > available_pixel_width) - return std::wstring(); + return string16(); // Use binary search to compute the elided text. size_t lo = 0; @@ -361,8 +371,8 @@ std::wstring ElideText(const std::wstring& text, 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. - int guess_length = - font.GetStringWidth(CutString(text, guess, elide_in_middle, true)); + int guess_length = font.GetStringWidth(UTF16ToWide( + 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) { diff --git a/app/text_elider.h b/app/text_elider.h index 6e86b05..8a5653d 100644 --- a/app/text_elider.h +++ b/app/text_elider.h @@ -40,10 +40,10 @@ std::wstring ElideUrl(const GURL& url, // 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, - bool elide_in_middle); +string16 ElideText(const string16& text, + const gfx::Font& font, + 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 diff --git a/app/text_elider_unittest.cc b/app/text_elider_unittest.cc index 174be59..cc65165 100644 --- a/app/text_elider_unittest.cc +++ b/app/text_elider_unittest.cc @@ -26,9 +26,9 @@ struct FileTestcase { const std::wstring output; }; -struct WideTestcase { - const std::wstring input; - const std::wstring output; +struct UTF16Testcase { + const string16 input; + const string16 output; }; struct TestData { @@ -188,21 +188,21 @@ TEST(TextEliderTest, TestFilenameEliding) { } TEST(TextEliderTest, ElideTextLongStrings) { - const std::wstring kEllipsisStr(kEllipsis); - std::wstring data_scheme(L"data:text/plain,"); + const string16 kEllipsisStr(WideToUTF16(kEllipsis)); + string16 data_scheme(UTF8ToUTF16("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'); - std::wstring thousand_a(1000, L'a'); - std::wstring ten_thousand_a(10000, L'a'); - std::wstring hundred_thousand_a(100000, L'a'); - std::wstring million_a(1000000, L'a'); + string16 ten_a(10, 'a'); + string16 hundred_a(100, 'a'); + string16 thousand_a(1000, 'a'); + string16 ten_thousand_a(10000, 'a'); + string16 hundred_thousand_a(100000, 'a'); + string16 million_a(1000000, 'a'); size_t number_of_as = 156; - std::wstring long_string_end( - data_scheme + std::wstring(number_of_as, L'a') + kEllipsisStr); - WideTestcase testcases_end[] = { + string16 long_string_end( + data_scheme + string16(number_of_as, 'a') + kEllipsisStr); + UTF16Testcase 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}, @@ -212,23 +212,24 @@ TEST(TextEliderTest, ElideTextLongStrings) { }; const gfx::Font font; - int ellipsis_width = font.GetStringWidth(kEllipsisStr); + int ellipsis_width = font.GetStringWidth(UTF16ToWideHack(kEllipsisStr)); 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), + font.GetStringWidth(UTF16ToWideHack( + 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[] = { + string16 long_string_middle(data_scheme + + string16(number_of_as - number_of_trailing_as, 'a') + kEllipsisStr + + string16(number_of_trailing_as, 'a')); + UTF16Testcase 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}, @@ -242,7 +243,8 @@ TEST(TextEliderTest, ElideTextLongStrings) { // output is rather long. EXPECT_EQ(testcases_middle[i].output.size(), ElideText(testcases_middle[i].input, font, - font.GetStringWidth(testcases_middle[i].output), + font.GetStringWidth(UTF16ToWideHack( + testcases_middle[i].output)), false).size()); EXPECT_EQ(kEllipsisStr, ElideText(testcases_middle[i].input, font, ellipsis_width, |