diff options
-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 | ||||
-rw-r--r-- | chrome/browser/autocomplete/autocomplete_popup_view_mac.mm | 4 | ||||
-rw-r--r-- | chrome/browser/autocomplete/autocomplete_popup_view_mac_unittest.mm | 7 | ||||
-rw-r--r-- | chrome/browser/cocoa/bookmarks/bookmark_menu_cocoa_controller.mm | 10 | ||||
-rw-r--r-- | chrome/browser/cocoa/download/download_item_cell.mm | 4 | ||||
-rw-r--r-- | chrome/browser/cocoa/location_bar/ev_bubble_decoration.mm | 4 | ||||
-rw-r--r-- | chrome/browser/views/autocomplete/autocomplete_popup_contents_view.cc | 12 | ||||
-rw-r--r-- | chrome/browser/views/bookmark_bar_view.cc | 7 | ||||
-rw-r--r-- | printing/printed_document.cc | 3 | ||||
-rw-r--r-- | views/controls/label.cc | 3 | ||||
-rw-r--r-- | views/widget/tooltip_manager.cc | 4 |
13 files changed, 98 insertions, 74 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, diff --git a/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm b/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm index 56b1854..1e4f42f 100644 --- a/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm +++ b/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm @@ -10,6 +10,7 @@ #include "app/text_elider.h" #include "base/stl_util-inl.h" #include "base/sys_string_conversions.h" +#include "base/utf_string_conversions.h" #include "chrome/browser/autocomplete/autocomplete_edit.h" #include "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" #include "chrome/browser/autocomplete/autocomplete_popup_model.h" @@ -154,7 +155,8 @@ NSMutableAttributedString* AutocompletePopupViewMac::ElideString( } // If ElideText() decides to do nothing, nothing to be done. - const std::wstring elided(ElideText(originalString, font, width, false)); + const std::wstring elided(UTF16ToWideHack(ElideText( + WideToUTF16Hack(originalString), font, width, false))); if (0 == elided.compare(originalString)) { return aString; } diff --git a/chrome/browser/autocomplete/autocomplete_popup_view_mac_unittest.mm b/chrome/browser/autocomplete/autocomplete_popup_view_mac_unittest.mm index 3ad4b33..82d30e8 100644 --- a/chrome/browser/autocomplete/autocomplete_popup_view_mac_unittest.mm +++ b/chrome/browser/autocomplete/autocomplete_popup_view_mac_unittest.mm @@ -7,6 +7,7 @@ #include "app/text_elider.h" #include "base/scoped_ptr.h" #include "base/sys_string_conversions.h" +#include "base/utf_string_conversions.h" #include "chrome/browser/autocomplete/autocomplete.h" #include "testing/platform_test.h" @@ -460,14 +461,16 @@ TEST_F(AutocompletePopupViewMacTest, ElideString) { // When elided, result is the same as ElideText(). ret = AutocompletePopupViewMac::ElideString(as, wideContents, font_, kNarrow); - std::wstring elided(ElideText(wideContents, font_, kNarrow, false)); + std::wstring elided(UTF16ToWideHack(ElideText(WideToUTF16Hack( + wideContents), font_, kNarrow, false))); EXPECT_TRUE(ret == as); EXPECT_FALSE([[as string] isEqualToString:contents]); EXPECT_TRUE([[as string] isEqualToString:base::SysWideToNSString(elided)]); // When elided, result is the same as ElideText(). ret = AutocompletePopupViewMac::ElideString(as, wideContents, font_, 0.0); - elided = ElideText(wideContents, font_, 0.0, false); + elided = UTF16ToWideHack(ElideText(WideToUTF16Hack(wideContents), font_, + 0.0, false)); EXPECT_TRUE(ret == as); EXPECT_FALSE([[as string] isEqualToString:contents]); EXPECT_TRUE([[as string] isEqualToString:base::SysWideToNSString(elided)]); diff --git a/chrome/browser/cocoa/bookmarks/bookmark_menu_cocoa_controller.mm b/chrome/browser/cocoa/bookmarks/bookmark_menu_cocoa_controller.mm index 5696fe8..534ae66 100644 --- a/chrome/browser/cocoa/bookmarks/bookmark_menu_cocoa_controller.mm +++ b/chrome/browser/cocoa/bookmarks/bookmark_menu_cocoa_controller.mm @@ -31,11 +31,11 @@ const NSUInteger kMaximumMenuPixelsWide = 300; NSFont* nsfont = [NSFont menuBarFontOfSize:0]; // 0 means "default" gfx::Font font(base::SysNSStringToWide([nsfont fontName]), static_cast<int>([nsfont pointSize])); - std::wstring title = gfx::ElideText(UTF16ToWideHack(node->GetTitle()), - font, - kMaximumMenuPixelsWide, - false); - return base::SysWideToNSString(title); + string16 title = gfx::ElideText(node->GetTitle(), + font, + kMaximumMenuPixelsWide, + false); + return base::SysUTF16ToNSString(title); } - (id)initWithBridge:(BookmarkMenuBridge *)bridge { diff --git a/chrome/browser/cocoa/download/download_item_cell.mm b/chrome/browser/cocoa/download/download_item_cell.mm index 997821a..c286669 100644 --- a/chrome/browser/cocoa/download/download_item_cell.mm +++ b/chrome/browser/cocoa/download/download_item_cell.mm @@ -407,8 +407,8 @@ NSGradient* BackgroundTheme::GetNSGradient(int id) const { gfx::Font font_chr(base::SysNSStringToWide([font fontName]), [font pointSize]); - return base::SysWideToNSString(ElideText( - base::SysNSStringToWide([self secondaryTitle]), + return base::SysUTF16ToNSString(ElideText( + base::SysNSStringToUTF16([self secondaryTitle]), font_chr, availableWidth, false)); diff --git a/chrome/browser/cocoa/location_bar/ev_bubble_decoration.mm b/chrome/browser/cocoa/location_bar/ev_bubble_decoration.mm index d2bfe63..1f54500 100644 --- a/chrome/browser/cocoa/location_bar/ev_bubble_decoration.mm +++ b/chrome/browser/cocoa/location_bar/ev_bubble_decoration.mm @@ -81,8 +81,8 @@ CGFloat EVBubbleDecoration::GetWidthForSpace(CGFloat width) { // prefix and the trailing country code in place. gfx::Font font(base::SysNSStringToWide([font_ fontName]), [font_ pointSize]); - NSString* elided_label = base::SysWideToNSString( - ElideText(base::SysNSStringToWide(full_label_), font, width_left, true)); + NSString* elided_label = base::SysUTF16ToNSString( + ElideText(base::SysNSStringToUTF16(full_label_), font, width_left, true)); // Use the elided label. SetLabel(elided_label); diff --git a/chrome/browser/views/autocomplete/autocomplete_popup_contents_view.cc b/chrome/browser/views/autocomplete/autocomplete_popup_contents_view.cc index 84abbc5..807ee00 100644 --- a/chrome/browser/views/autocomplete/autocomplete_popup_contents_view.cc +++ b/chrome/browser/views/autocomplete/autocomplete_popup_contents_view.cc @@ -11,6 +11,7 @@ #include "app/text_elider.h" #include "base/compiler_specific.h" #include "base/i18n/rtl.h" +#include "base/utf_string_conversions.h" #include "chrome/browser/autocomplete/autocomplete_edit_view.h" #include "chrome/browser/autocomplete/autocomplete_popup_model.h" #include "chrome/browser/instant/instant_opt_in.h" @@ -183,9 +184,9 @@ class OptInButtonBorder : public views::Border { } // namespace -class AutocompletePopupContentsView::InstantOptInView : - public views::View, - public views::ButtonListener { +class AutocompletePopupContentsView::InstantOptInView + : public views::View, + public views::ButtonListener { public: InstantOptInView(AutocompletePopupContentsView* contents_view, const gfx::Font& label_font, @@ -696,8 +697,9 @@ void AutocompleteResultView::Elide(Runs* runs, int remaining_width) const { first_classification = false; // Can we fit at least an ellipsis? - std::wstring elided_text( - gfx::ElideText(j->text, *j->font, remaining_width, false)); + std::wstring elided_text(UTF16ToWideHack( + gfx::ElideText(WideToUTF16Hack(j->text), *j->font, remaining_width, + false))); Classifications::reverse_iterator prior_classification(j); ++prior_classification; const bool on_first_classification = diff --git a/chrome/browser/views/bookmark_bar_view.cc b/chrome/browser/views/bookmark_bar_view.cc index cd03a56..8ce71e7 100644 --- a/chrome/browser/views/bookmark_bar_view.cc +++ b/chrome/browser/views/bookmark_bar_view.cc @@ -143,9 +143,10 @@ static std::wstring CreateToolTipForURLAndTitle(const gfx::Point& screen_loc, // First the title. if (!title.empty()) { std::wstring localized_title; - result.append(gfx::ElideText( - base::i18n::AdjustStringForLocaleDirection(title, &localized_title) ? - localized_title : title, tt_font, max_width, false)); + if (!base::i18n::AdjustStringForLocaleDirection(title, &localized_title)) + localized_title = title; + result.append(UTF16ToWideHack(gfx::ElideText(WideToUTF16Hack( + localized_title), tt_font, max_width, false))); } // Only show the URL if the url and title differ. diff --git a/printing/printed_document.cc b/printing/printed_document.cc index 2dad1a0..04d8d3d 100644 --- a/printing/printed_document.cc +++ b/printing/printed_document.cc @@ -234,7 +234,8 @@ void PrintedDocument::PrintHeaderFooter(gfx::NativeDrawingContext context, if (line == PageOverlays::kUrl) { output = gfx::ElideUrl(url(), font, bounding.width(), std::wstring()); } else { - output = gfx::ElideText(output, font, bounding.width(), false); + output = UTF16ToWideHack(gfx::ElideText(WideToUTF16Hack(output), + font, bounding.width(), false)); } } diff --git a/views/controls/label.cc b/views/controls/label.cc index 6d979df..dcff7c8 100644 --- a/views/controls/label.cc +++ b/views/controls/label.cc @@ -437,7 +437,8 @@ void Label::CalculateDrawStringParams(std::wstring* paint_text, *paint_text = UTF16ToWide(base::i18n::GetDisplayStringInLTRDirectionality( WideToUTF16(*paint_text))); } else if (elide_in_middle_) { - *paint_text = gfx::ElideText(text_, font_, width(), true); + *paint_text = UTF16ToWideHack(gfx::ElideText(WideToUTF16Hack(text_), + font_, width(), true)); } else { *paint_text = text_; } diff --git a/views/widget/tooltip_manager.cc b/views/widget/tooltip_manager.cc index 39ccab8..5c400eb9 100644 --- a/views/widget/tooltip_manager.cc +++ b/views/widget/tooltip_manager.cc @@ -7,6 +7,7 @@ #include <vector> #include "app/text_elider.h" +#include "base/utf_string_conversions.h" namespace views { @@ -57,7 +58,8 @@ void TooltipManager::TrimTooltipToFit(std::wstring* text, std::wstring result; for (std::vector<std::wstring>::iterator i = lines.begin(); i != lines.end(); ++i) { - std::wstring elided_text = gfx::ElideText(*i, font, available_width, false); + std::wstring elided_text = UTF16ToWideHack(gfx::ElideText(WideToUTF16Hack( + *i), font, available_width, false)); *max_width = std::max(*max_width, font.GetStringWidth(elided_text)); if (i == lines.begin() && i + 1 == lines.end()) { *text = elided_text; |