From dbb97ba9f0608c2ebf6f02f750df9c5e433b770e Mon Sep 17 00:00:00 2001 From: "ben@chromium.org" Date: Mon, 9 Sep 2013 22:15:25 +0000 Subject: [PATCH] Move text_elider to gfx. R=sky@chromium.org http://crbug.com/103304 Review URL: https://codereview.chromium.org/23731010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@222106 0039d316-1c4b-4281-b951-d872f2087c98 --- ash/system/user/tray_user.cc | 15 +- .../chromeos/external_protocol_dialog.cc | 4 +- .../browser/download/download_item_model.cc | 10 +- chrome/browser/extensions/menu_manager.cc | 4 +- chrome/browser/signin/signin_ui_util.cc | 4 +- .../tab_contents/render_view_context_menu.cc | 4 +- .../javascript_app_modal_dialog.cc | 10 +- .../autofill_popup_controller_impl.cc | 10 +- .../ui/cocoa/download/download_item_cell.mm | 8 +- .../download/download_item_controller.mm | 2 +- .../ui/cocoa/external_protocol_dialog.mm | 4 +- .../location_bar/ev_bubble_decoration.mm | 6 +- .../omnibox_popup_view_mac_unittest.mm | 2 +- chrome/browser/ui/cocoa/status_bubble_mac.mm | 6 +- .../ui/external_protocol_dialog_delegate.cc | 6 +- chrome/browser/ui/gtk/avatar_menu_item_gtk.cc | 6 +- .../ui/gtk/bookmarks/bookmark_utils_gtk.cc | 6 +- .../ui/gtk/content_setting_bubble_gtk.cc | 6 +- .../ui/gtk/download/download_item_gtk.cc | 6 +- chrome/browser/ui/gtk/global_history_menu.cc | 4 +- chrome/browser/ui/gtk/gtk_util.cc | 6 +- chrome/browser/ui/gtk/status_bubble_gtk.cc | 6 +- chrome/browser/ui/login/login_prompt.cc | 4 +- .../browser/ui/omnibox/location_bar_util.cc | 4 +- .../ui/toolbar/back_forward_menu_model.cc | 4 +- .../ui/views/bookmarks/bookmark_bar_view.cc | 8 +- .../ui/views/download/download_item_view.cc | 6 +- .../extension_installed_bubble_view.cc | 6 +- .../ui/views/external_protocol_dialog.cc | 6 +- .../ui/views/frame/global_menu_bar_x11.cc | 4 +- .../ui/views/omnibox/omnibox_result_view.cc | 4 +- .../browser/ui/views/status_bubble_views.cc | 6 +- chrome/browser/ui/views/tabs/tab.cc | 2 +- .../render_widget_host_view_gtk.cc | 4 +- .../render_widget_host_view_win.cc | 4 +- .../web_contents/navigation_entry_impl.cc | 4 +- content/shell/browser/shell_login_dialog.cc | 4 +- printing/backend/print_backend.cc | 4 +- printing/print_settings_initializer.cc | 4 +- printing/printed_document.cc | 2 +- ui/base/cocoa/menu_controller.mm | 4 +- ui/base/text/text_elider.cc | 28 +- ui/base/text/text_elider_unittest.cc | 2 +- ui/gfx/canvas_skia.cc | 20 +- ui/gfx/render_text.cc | 4 +- ui/gfx/text_elider.cc | 1172 +++++++++++++++++ ui/gfx/text_elider.h | 232 ++++ ui/gfx/text_elider_unittest.cc | 934 +++++++++++++ .../cocoa/notification_controller.mm | 12 +- ui/message_center/views/bounded_label.cc | 12 +- ui/message_center/views/notification_view.cc | 8 +- ui/ui.gyp | 4 +- ui/ui_unittests.gypi | 2 +- ui/views/controls/label.cc | 13 +- ui/views/controls/styled_label.cc | 6 +- ui/views/corewm/tooltip_controller.cc | 7 +- .../corewm/tooltip_controller_unittest.cc | 6 +- ui/views/widget/tooltip_manager.cc | 4 +- 58 files changed, 2513 insertions(+), 172 deletions(-) create mode 100644 ui/gfx/text_elider.cc create mode 100644 ui/gfx/text_elider.h create mode 100644 ui/gfx/text_elider_unittest.cc diff --git a/ash/system/user/tray_user.cc b/ash/system/user/tray_user.cc index 172b3aac973fff..efae178119debc 100644 --- a/ash/system/user/tray_user.cc +++ b/ash/system/user/tray_user.cc @@ -38,7 +38,6 @@ #include "ui/aura/window.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/canvas.h" #include "ui/gfx/font.h" #include "ui/gfx/image/image.h" @@ -49,6 +48,7 @@ #include "ui/gfx/render_text.h" #include "ui/gfx/size.h" #include "ui/gfx/skia_util.h" +#include "ui/gfx/text_elider.h" #include "ui/views/border.h" #include "ui/views/bubble/tray_bubble_view.h" #include "ui/views/controls/button/button.h" @@ -452,8 +452,9 @@ void PublicAccountUserDetails::Layout() { // Word-wrap the label text. const gfx::Font font; std::vector lines; - ui::ElideRectangleText(text_, font, contents_area.width(), - contents_area.height(), ui::ELIDE_LONG_WORDS, &lines); + gfx::ElideRectangleText(text_, font, contents_area.width(), + contents_area.height(), gfx::ELIDE_LONG_WORDS, + &lines); // Loop through the lines, creating a renderer for each. gfx::Point position = contents_area.origin(); gfx::Range display_name(gfx::Range::InvalidRange()); @@ -550,8 +551,8 @@ void PublicAccountUserDetails::CalculatePreferredSize(SystemTrayItem* owner, while (min_width < max_width) { lines.clear(); const int width = (min_width + max_width) / 2; - const bool too_narrow = ui::ElideRectangleText( - text_, font, width, INT_MAX, ui::TRUNCATE_LONG_WORDS, &lines) != 0; + const bool too_narrow = gfx::ElideRectangleText( + text_, font, width, INT_MAX, gfx::TRUNCATE_LONG_WORDS, &lines) != 0; int line_count = lines.size(); if (!too_narrow && line_count == 3 && width - font.GetStringWidth(lines.back()) <= @@ -566,8 +567,8 @@ void PublicAccountUserDetails::CalculatePreferredSize(SystemTrayItem* owner, // Calculate the corresponding height and set the preferred size. lines.clear(); - ui::ElideRectangleText( - text_, font, min_width, INT_MAX, ui::TRUNCATE_LONG_WORDS, &lines); + gfx::ElideRectangleText( + text_, font, min_width, INT_MAX, gfx::TRUNCATE_LONG_WORDS, &lines); int line_count = lines.size(); if (min_width - font.GetStringWidth(lines.back()) <= space_width + link_size.width()) { diff --git a/chrome/browser/chromeos/external_protocol_dialog.cc b/chrome/browser/chromeos/external_protocol_dialog.cc index 724cd540a33cfa..0cf11852688565 100644 --- a/chrome/browser/chromeos/external_protocol_dialog.cc +++ b/chrome/browser/chromeos/external_protocol_dialog.cc @@ -14,7 +14,7 @@ #include "grit/chromium_strings.h" #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" #include "ui/views/controls/message_box_view.h" #include "ui/views/widget/widget.h" #include "url/gurl.h" @@ -95,7 +95,7 @@ ExternalProtocolDialog::ExternalProtocolDialog(WebContents* web_contents, scheme_(url.scheme()) { const int kMaxUrlWithoutSchemeSize = 256; string16 elided_url_without_scheme; - ui::ElideString(ASCIIToUTF16(url.possibly_invalid_spec()), + gfx::ElideString(ASCIIToUTF16(url.possibly_invalid_spec()), kMaxUrlWithoutSchemeSize, &elided_url_without_scheme); views::MessageBoxView::InitParams params( diff --git a/chrome/browser/download/download_item_model.cc b/chrome/browser/download/download_item_model.cc index 2edd66fa313e81..d7983808ac0b2f 100644 --- a/chrome/browser/download/download_item_model.cc +++ b/chrome/browser/download/download_item_model.cc @@ -23,7 +23,7 @@ #include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/time_format.h" #include "ui/base/text/bytes_formatting.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" using base::TimeDelta; using content::DownloadItem; @@ -325,14 +325,14 @@ string16 DownloadItemModel::GetTabProgressStatusText() const { string16 DownloadItemModel::GetTooltipText(const gfx::FontList& font_list, int max_width) const { - string16 tooltip = ui::ElideFilename( + string16 tooltip = gfx::ElideFilename( download_->GetFileNameToReportUser(), font_list, max_width); content::DownloadInterruptReason reason = download_->GetLastReason(); if (download_->GetState() == DownloadItem::INTERRUPTED && reason != content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED) { tooltip += ASCIIToUTF16("\n"); - tooltip += ui::ElideText(InterruptReasonStatusMessage(reason), - font_list, max_width, ui::ELIDE_AT_END); + tooltip += gfx::ElideText(InterruptReasonStatusMessage(reason), + font_list, max_width, gfx::ELIDE_AT_END); } return tooltip; } @@ -342,7 +342,7 @@ string16 DownloadItemModel::GetWarningText(const gfx::FontList& font_list, // Should only be called if IsDangerous(). DCHECK(IsDangerous()); string16 elided_filename = - ui::ElideFilename(download_->GetFileNameToReportUser(), font_list, + gfx::ElideFilename(download_->GetFileNameToReportUser(), font_list, base_width); switch (download_->GetDangerType()) { case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: { diff --git a/chrome/browser/extensions/menu_manager.cc b/chrome/browser/extensions/menu_manager.cc index 6b0303d139c118..d1ef97589b3b48 100644 --- a/chrome/browser/extensions/menu_manager.cc +++ b/chrome/browser/extensions/menu_manager.cc @@ -28,8 +28,8 @@ #include "content/public/browser/notification_source.h" #include "content/public/browser/web_contents.h" #include "content/public/common/context_menu_params.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/favicon_size.h" +#include "ui/gfx/text_elider.h" using content::WebContents; using extensions::ExtensionSystem; @@ -173,7 +173,7 @@ string16 MenuItem::TitleWithReplacement( ReplaceSubstringsAfterOffset(&result, 0, ASCIIToUTF16("%s"), selection); if (result.length() > max_length) - result = ui::TruncateString(result, max_length); + result = gfx::TruncateString(result, max_length); return result; } diff --git a/chrome/browser/signin/signin_ui_util.cc b/chrome/browser/signin/signin_ui_util.cc index e3bb8ba503dc60..b458b2a0184338 100644 --- a/chrome/browser/signin/signin_ui_util.cc +++ b/chrome/browser/signin/signin_ui_util.cc @@ -16,8 +16,8 @@ #include "grit/chromium_strings.h" #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/font.h" +#include "ui/gfx/text_elider.h" namespace { // Maximum width of a username - we trim emails that are wider than this so @@ -75,7 +75,7 @@ string16 GetSigninMenuLabel(Profile* profile) { if (signin_manager) username = signin_manager->GetAuthenticatedUsername(); if (!username.empty() && !signin_manager->AuthInProgress()) { - string16 elided_username = ui::ElideEmail(UTF8ToUTF16(username), + string16 elided_username = gfx::ElideEmail(UTF8ToUTF16(username), gfx::Font(), kUsernameMaxWidth); return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_SYNCED_LABEL, diff --git a/chrome/browser/tab_contents/render_view_context_menu.cc b/chrome/browser/tab_contents/render_view_context_menu.cc index ab3b312c1e3d35..2be8a7960a577b 100644 --- a/chrome/browser/tab_contents/render_view_context_menu.cc +++ b/chrome/browser/tab_contents/render_view_context_menu.cc @@ -89,10 +89,10 @@ #include "third_party/WebKit/public/web/WebPluginAction.h" #include "ui/base/clipboard/clipboard.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/favicon_size.h" #include "ui/gfx/point.h" #include "ui/gfx/size.h" +#include "ui/gfx/text_elider.h" #if defined(ENABLE_PRINTING) #include "chrome/common/print_messages.h" @@ -2032,7 +2032,7 @@ bool RenderViewContextMenu::IsDevCommandEnabled(int id) const { } string16 RenderViewContextMenu::PrintableSelectionText() { - return ui::TruncateString(params_.selection_text, + return gfx::TruncateString(params_.selection_text, kMaxSelectionTextLength); } diff --git a/chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.cc b/chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.cc index 89fe155bd1e2fb..c65a732ceba0cf 100644 --- a/chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.cc +++ b/chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.cc @@ -10,7 +10,7 @@ #include "chrome/common/chrome_switches.h" #include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents_view.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" #if defined(USE_AURA) #include "ui/aura/root_window.h" @@ -31,11 +31,11 @@ const int kMessageTextMaxCols = 132; const int kDefaultPromptMaxRows = 24; const int kDefaultPromptMaxCols = 132; void EnforceMaxTextSize(const string16& in_string, string16* out_string) { - ui::ElideRectangleString(in_string, kMessageTextMaxRows, + gfx::ElideRectangleString(in_string, kMessageTextMaxRows, kMessageTextMaxCols, false, out_string); } void EnforceMaxPromptSize(const string16& in_string, string16* out_string) { - ui::ElideRectangleString(in_string, kDefaultPromptMaxRows, + gfx::ElideRectangleString(in_string, kDefaultPromptMaxRows, kDefaultPromptMaxCols, false, out_string); } #else @@ -44,10 +44,10 @@ void EnforceMaxPromptSize(const string16& in_string, string16* out_string) { const int kMessageTextMaxSize = 3000; const int kDefaultPromptMaxSize = 2000; void EnforceMaxTextSize(const string16& in_string, string16* out_string) { - ui::ElideString(in_string, kMessageTextMaxSize, out_string); + gfx::ElideString(in_string, kMessageTextMaxSize, out_string); } void EnforceMaxPromptSize(const string16& in_string, string16* out_string) { - ui::ElideString(in_string, kDefaultPromptMaxSize, out_string); + gfx::ElideString(in_string, kDefaultPromptMaxSize, out_string); } #endif diff --git a/chrome/browser/ui/autofill/autofill_popup_controller_impl.cc b/chrome/browser/ui/autofill/autofill_popup_controller_impl.cc index 043e8c15476deb..837025f0aeaed7 100644 --- a/chrome/browser/ui/autofill/autofill_popup_controller_impl.cc +++ b/chrome/browser/ui/autofill/autofill_popup_controller_impl.cc @@ -15,10 +15,10 @@ #include "grit/webkit_resources.h" #include "third_party/WebKit/public/web/WebAutofillClient.h" #include "ui/base/events/event.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/display.h" #include "ui/gfx/rect_conversions.h" #include "ui/gfx/screen.h" +#include "ui/gfx/text_elider.h" #include "ui/gfx/vector2d.h" using base::WeakPtr; @@ -143,16 +143,16 @@ void AutofillPopupControllerImpl::Show( // Each field recieves space in proportion to its length. int name_size = available_width * name_width / total_text_length; - names_[i] = ui::ElideText(names_[i], + names_[i] = gfx::ElideText(names_[i], GetNameFontForRow(i), name_size, - ui::ELIDE_AT_END); + gfx::ELIDE_AT_END); int subtext_size = available_width * subtext_width / total_text_length; - subtexts_[i] = ui::ElideText(subtexts_[i], + subtexts_[i] = gfx::ElideText(subtexts_[i], subtext_font(), subtext_size, - ui::ELIDE_AT_END); + gfx::ELIDE_AT_END); } #endif diff --git a/chrome/browser/ui/cocoa/download/download_item_cell.mm b/chrome/browser/ui/cocoa/download/download_item_cell.mm index c4e5e4d6c98383..4928951da74229 100644 --- a/chrome/browser/ui/cocoa/download/download_item_cell.mm +++ b/chrome/browser/ui/cocoa/download/download_item_cell.mm @@ -16,7 +16,7 @@ #import "third_party/GTM/AppKit/GTMNSAnimation+Duration.h" #import "third_party/GTM/AppKit/GTMNSColor+Luminance.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" #include "ui/gfx/canvas_skia_paint.h" #include "ui/gfx/font.h" #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" @@ -378,7 +378,7 @@ - (NSString*)elideTitle:(int)availableWidth { [font pointSize]); return base::SysUTF16ToNSString( - ui::ElideFilename(downloadPath_, font_chr, availableWidth)); + gfx::ElideFilename(downloadPath_, font_chr, availableWidth)); } - (NSString*)elideStatus:(int)availableWidth { @@ -386,11 +386,11 @@ - (NSString*)elideStatus:(int)availableWidth { gfx::Font font_chr(base::SysNSStringToUTF8([font fontName]), [font pointSize]); - return base::SysUTF16ToNSString(ui::ElideText( + return base::SysUTF16ToNSString(gfx::ElideText( base::SysNSStringToUTF16([self secondaryTitle]), font_chr, availableWidth, - ui::ELIDE_AT_END)); + gfx::ELIDE_AT_END)); } - (ui::ThemeProvider*)backgroundThemeWrappingProvider: diff --git a/chrome/browser/ui/cocoa/download/download_item_controller.mm b/chrome/browser/ui/cocoa/download/download_item_controller.mm index a4037977fb416f..1c7d29561ca2b3 100644 --- a/chrome/browser/ui/cocoa/download/download_item_controller.mm +++ b/chrome/browser/ui/cocoa/download/download_item_controller.mm @@ -29,7 +29,7 @@ #include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" #include "ui/base/l10n/l10n_util_mac.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" #include "ui/gfx/font.h" #include "ui/gfx/image/image.h" diff --git a/chrome/browser/ui/cocoa/external_protocol_dialog.mm b/chrome/browser/ui/cocoa/external_protocol_dialog.mm index 0b66547d1b3891..d4aacba28941e8 100644 --- a/chrome/browser/ui/cocoa/external_protocol_dialog.mm +++ b/chrome/browser/ui/cocoa/external_protocol_dialog.mm @@ -13,7 +13,7 @@ #include "grit/chromium_strings.h" #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util_mac.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" /////////////////////////////////////////////////////////////////////////////// // ExternalProtocolHandler @@ -65,7 +65,7 @@ - (id)initWithGURL:(const GURL*)url { const int kMaxUrlWithoutSchemeSize = 256; string16 elided_url_without_scheme; - ui::ElideString(ASCIIToUTF16(url_.possibly_invalid_spec()), + gfx::ElideString(ASCIIToUTF16(url_.possibly_invalid_spec()), kMaxUrlWithoutSchemeSize, &elided_url_without_scheme); NSString* urlString = l10n_util::GetNSStringFWithFixup( diff --git a/chrome/browser/ui/cocoa/location_bar/ev_bubble_decoration.mm b/chrome/browser/ui/cocoa/location_bar/ev_bubble_decoration.mm index 25b4c710a3f049..1a2abc75afd759 100644 --- a/chrome/browser/ui/cocoa/location_bar/ev_bubble_decoration.mm +++ b/chrome/browser/ui/cocoa/location_bar/ev_bubble_decoration.mm @@ -7,7 +7,7 @@ #import "base/logging.h" #include "base/strings/sys_string_conversions.h" #import "chrome/browser/ui/cocoa/location_bar/location_icon_decoration.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" #include "ui/gfx/font.h" namespace { @@ -88,8 +88,8 @@ // prefix and the trailing country code in place. gfx::Font font(GetFont()); NSString* elided_label = base::SysUTF16ToNSString( - ui::ElideText(base::SysNSStringToUTF16(full_label_), font, width_left, - ui::ELIDE_IN_MIDDLE)); + gfx::ElideText(base::SysNSStringToUTF16(full_label_), font, width_left, + gfx::ELIDE_IN_MIDDLE)); // Use the elided label. SetLabel(elided_label); diff --git a/chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac_unittest.mm b/chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac_unittest.mm index 82e0d18a559e03..f41c36e83eb4f9 100644 --- a/chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac_unittest.mm +++ b/chrome/browser/ui/cocoa/omnibox/omnibox_popup_view_mac_unittest.mm @@ -9,7 +9,7 @@ #include "base/strings/utf_string_conversions.h" #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" #import "chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" namespace { diff --git a/chrome/browser/ui/cocoa/status_bubble_mac.mm b/chrome/browser/ui/cocoa/status_bubble_mac.mm index 05457974ff60fa..b5ec7429aed1f5 100644 --- a/chrome/browser/ui/cocoa/status_bubble_mac.mm +++ b/chrome/browser/ui/cocoa/status_bubble_mac.mm @@ -19,7 +19,7 @@ #import "third_party/GTM/AppKit/GTMNSBezierPath+RoundRect.h" #import "third_party/GTM/AppKit/GTMNSColor+Luminance.h" #include "ui/base/cocoa/window_size_constants.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" #include "ui/gfx/font.h" #include "ui/gfx/point.h" @@ -154,7 +154,7 @@ - (void)animationDidStop:(CAAnimation*)animation finished:(BOOL)finished { [font pointSize]); string16 original_url_text = net::FormatUrl(url, languages); - string16 status = ui::ElideUrl(url, font_chr, text_width, languages); + string16 status = gfx::ElideUrl(url, font_chr, text_width, languages); SetText(status, true); @@ -623,7 +623,7 @@ - (void)animationDidStop:(CAAnimation*)animation finished:(BOOL)finished { NSFont* font = [[window_ contentView] font]; gfx::Font font_chr(base::SysNSStringToUTF8([font fontName]), [font pointSize]); - string16 expanded_url = ui::ElideUrl( + string16 expanded_url = gfx::ElideUrl( url_, font_chr, max_bubble_width, languages_); // Scale width from gfx::Font in view coordinates to window coordinates. diff --git a/chrome/browser/ui/external_protocol_dialog_delegate.cc b/chrome/browser/ui/external_protocol_dialog_delegate.cc index aa368081c1f83e..e0129c2815e9c1 100644 --- a/chrome/browser/ui/external_protocol_dialog_delegate.cc +++ b/chrome/browser/ui/external_protocol_dialog_delegate.cc @@ -13,7 +13,7 @@ #include "grit/chromium_strings.h" #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" ExternalProtocolDialogDelegate::ExternalProtocolDialogDelegate(const GURL& url) : ProtocolDialogDelegate(url) { @@ -32,9 +32,9 @@ string16 ExternalProtocolDialogDelegate::GetMessageText() const { UTF8ToUTF16(ShellIntegration::GetApplicationForProtocol(url())); string16 elided_url_without_scheme; string16 elided_command; - ui::ElideString(ASCIIToUTF16(url().possibly_invalid_spec()), + gfx::ElideString(ASCIIToUTF16(url().possibly_invalid_spec()), kMaxUrlWithoutSchemeSize, &elided_url_without_scheme); - ui::ElideString(command, kMaxCommandSize, &elided_command); + gfx::ElideString(command, kMaxCommandSize, &elided_command); string16 message_text = l10n_util::GetStringFUTF16( IDS_EXTERNAL_PROTOCOL_INFORMATION, diff --git a/chrome/browser/ui/gtk/avatar_menu_item_gtk.cc b/chrome/browser/ui/gtk/avatar_menu_item_gtk.cc index 791cd427a211f5..aabd4908b51583 100644 --- a/chrome/browser/ui/gtk/avatar_menu_item_gtk.cc +++ b/chrome/browser/ui/gtk/avatar_menu_item_gtk.cc @@ -20,10 +20,10 @@ #include "ui/base/gtk/gtk_hig_constants.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/canvas.h" #include "ui/gfx/gtk_util.h" #include "ui/gfx/image/image.h" +#include "ui/gfx/text_elider.h" namespace { @@ -227,10 +227,10 @@ void AvatarMenuItemGtk::Init(GtkThemeService* theme_service) { // The user name label. GtkWidget* item_vbox = gtk_vbox_new(FALSE, 0); GtkWidget* name_label = NULL; - string16 elided_name = ui::ElideText(item_.name, + string16 elided_name = gfx::ElideText(item_.name, gfx::Font(), kUserNameMaxWidth, - ui::ELIDE_AT_END); + gfx::ELIDE_AT_END); name_label = theme_service->BuildLabel(UTF16ToUTF8(elided_name), ui::kGdkBlack); diff --git a/chrome/browser/ui/gtk/bookmarks/bookmark_utils_gtk.cc b/chrome/browser/ui/gtk/bookmarks/bookmark_utils_gtk.cc index 8b9ab4ab8f2cb1..1697fe9f15b24e 100644 --- a/chrome/browser/ui/gtk/bookmarks/bookmark_utils_gtk.cc +++ b/chrome/browser/ui/gtk/bookmarks/bookmark_utils_gtk.cc @@ -25,10 +25,10 @@ #include "ui/base/gtk/gtk_screen_util.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/canvas_skia_paint.h" #include "ui/gfx/font.h" #include "ui/gfx/image/image.h" +#include "ui/gfx/text_elider.h" namespace { @@ -277,10 +277,10 @@ std::string BuildMenuLabelFor(const BookmarkNode* node) { // This breaks on word boundaries. Ideally we would break on character // boundaries. std::string elided_name = UTF16ToUTF8( - ui::TruncateString(node->GetTitle(), kMaxCharsOnAMenuLabel)); + gfx::TruncateString(node->GetTitle(), kMaxCharsOnAMenuLabel)); if (elided_name.empty()) { - elided_name = UTF16ToUTF8(ui::TruncateString( + elided_name = UTF16ToUTF8(gfx::TruncateString( UTF8ToUTF16(node->url().possibly_invalid_spec()), kMaxCharsOnAMenuLabel)); } diff --git a/chrome/browser/ui/gtk/content_setting_bubble_gtk.cc b/chrome/browser/ui/gtk/content_setting_bubble_gtk.cc index aa9e25edecf7bb..6a541e2397c47c 100644 --- a/chrome/browser/ui/gtk/content_setting_bubble_gtk.cc +++ b/chrome/browser/ui/gtk/content_setting_bubble_gtk.cc @@ -31,9 +31,9 @@ #include "ui/base/gtk/gtk_hig_constants.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/models/simple_menu_model.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/font.h" #include "ui/gfx/gtk_util.h" +#include "ui/gfx/text_elider.h" using content::PluginService; using content::WebContents; @@ -49,11 +49,11 @@ const int kMinMediaMenuButtonWidth = 100; const int kMaxMediaMenuButtonWidth = 600; std::string BuildElidedText(const std::string& input) { - return UTF16ToUTF8(ui::ElideText( + return UTF16ToUTF8(gfx::ElideText( UTF8ToUTF16(input), gfx::Font(), kMaxLinkPixelSize, - ui::ELIDE_AT_END)); + gfx::ELIDE_AT_END)); } } // namespace diff --git a/chrome/browser/ui/gtk/download/download_item_gtk.cc b/chrome/browser/ui/gtk/download/download_item_gtk.cc index 144a6b3c6836d5..1ba3895e976d69 100644 --- a/chrome/browser/ui/gtk/download/download_item_gtk.cc +++ b/chrome/browser/ui/gtk/download/download_item_gtk.cc @@ -31,11 +31,11 @@ #include "ui/base/animation/slide_animation.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/canvas_skia_paint.h" #include "ui/gfx/color_utils.h" #include "ui/gfx/image/image.h" #include "ui/gfx/skia_utils_gtk.h" +#include "ui/gfx/text_elider.h" #include "ui/gfx/text_utils.h" namespace { @@ -492,7 +492,7 @@ void DownloadItemGtk::UpdateNameLabel() { ui::ResourceBundle::BaseFont); string16 filename; if (!disabled_while_opening_) { - filename = ui::ElideFilename( + filename = gfx::ElideFilename( download()->GetFileNameToReportUser(), font_list, kTextWidth); } else { // First, Calculate the download status opening string width. @@ -501,7 +501,7 @@ void DownloadItemGtk::UpdateNameLabel() { int status_string_width = gfx::GetStringWidth(status_string, font_list); // Then, elide the file name. string16 filename_string = - ui::ElideFilename(download()->GetFileNameToReportUser(), font_list, + gfx::ElideFilename(download()->GetFileNameToReportUser(), font_list, kTextWidth - status_string_width); // Last, concat the whole string. filename = l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_OPENING, diff --git a/chrome/browser/ui/gtk/global_history_menu.cc b/chrome/browser/ui/gtk/global_history_menu.cc index 005464114d00d2..9235762b8769cb 100644 --- a/chrome/browser/ui/gtk/global_history_menu.cc +++ b/chrome/browser/ui/gtk/global_history_menu.cc @@ -29,9 +29,9 @@ #include "grit/generated_resources.h" #include "ui/base/gtk/owned_widget_gtk.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/codec/png_codec.h" #include "ui/gfx/gtk_util.h" +#include "ui/gfx/text_elider.h" using content::OpenURLParams; @@ -198,7 +198,7 @@ GtkWidget* GlobalHistoryMenu::AddHistoryItemToMenu(HistoryItem* item, if (title.empty()) title = UTF8ToUTF16(url_string); - ui::ElideString(title, kMaximumMenuWidthInChars, &title); + gfx::ElideString(title, kMaximumMenuWidthInChars, &title); GtkWidget* menu_item = gtk_menu_item_new_with_label( UTF16ToUTF8(title).c_str()); diff --git a/chrome/browser/ui/gtk/gtk_util.cc b/chrome/browser/ui/gtk/gtk_util.cc index 2b51a0abf9f68a..36cddb7125b362 100644 --- a/chrome/browser/ui/gtk/gtk_util.cc +++ b/chrome/browser/ui/gtk/gtk_util.cc @@ -40,11 +40,11 @@ #include "ui/base/gtk/gtk_screen_util.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/base/text/text_elider.h" #include "ui/base/x/x11_util.h" #include "ui/gfx/image/cairo_cached_surface.h" #include "ui/gfx/image/image.h" #include "ui/gfx/pango_util.h" +#include "ui/gfx/text_elider.h" #include "url/gurl.h" // These conflict with base/tracked_objects.h, so need to come last. @@ -648,7 +648,7 @@ std::string BuildTooltipTitleFor(string16 title, const GURL& url) { const std::string& url_str = url.possibly_invalid_spec(); const std::string& title_str = UTF16ToUTF8(title); - std::string truncated_url = UTF16ToUTF8(ui::TruncateString( + std::string truncated_url = UTF16ToUTF8(gfx::TruncateString( UTF8ToUTF16(url_str), kMaxTooltipURLLength)); gchar* escaped_url_cstr = g_markup_escape_text(truncated_url.c_str(), truncated_url.size()); @@ -658,7 +658,7 @@ std::string BuildTooltipTitleFor(string16 title, const GURL& url) { if (url_str == title_str || title.empty()) { return escaped_url; } else { - std::string truncated_title = UTF16ToUTF8(ui::TruncateString( + std::string truncated_title = UTF16ToUTF8(gfx::TruncateString( title, kMaxTooltipTitleLength)); gchar* escaped_title_cstr = g_markup_escape_text(truncated_title.c_str(), truncated_title.size()); diff --git a/chrome/browser/ui/gtk/status_bubble_gtk.cc b/chrome/browser/ui/gtk/status_bubble_gtk.cc index 3d4fc79380276e..ad7f7f7550e008 100644 --- a/chrome/browser/ui/gtk/status_bubble_gtk.cc +++ b/chrome/browser/ui/gtk/status_bubble_gtk.cc @@ -20,8 +20,8 @@ #include "ui/base/animation/slide_animation.h" #include "ui/base/gtk/gtk_compat.h" #include "ui/base/gtk/gtk_hig_constants.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/font.h" +#include "ui/gfx/text_elider.h" namespace { @@ -113,9 +113,9 @@ void StatusBubbleGtk::SetStatusTextToURL() { } // TODO(tc): We don't actually use gfx::Font as the font in the status - // bubble. We should extend ui::ElideUrl to take some sort of pango font. + // bubble. We should extend gfx::ElideUrl to take some sort of pango font. url_text_ = UTF16ToUTF8( - ui::ElideUrl(url_, gfx::Font(), desired_width, languages_)); + gfx::ElideUrl(url_, gfx::Font(), desired_width, languages_)); SetStatusTextTo(url_text_); } diff --git a/chrome/browser/ui/login/login_prompt.cc b/chrome/browser/ui/login/login_prompt.cc index a59072029596ef..5c08f33830b8fa 100644 --- a/chrome/browser/ui/login/login_prompt.cc +++ b/chrome/browser/ui/login/login_prompt.cc @@ -27,7 +27,7 @@ #include "net/url_request/url_request.h" #include "net/url_request/url_request_context.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" using autofill::PasswordForm; using content::BrowserThread; @@ -441,7 +441,7 @@ void LoginDialogCallback(const GURL& request_url, // The realm is controlled by the remote server, so there is no reason // to believe it is of a reasonable length. string16 elided_realm; - ui::ElideString(UTF8ToUTF16(auth_info->realm), 120, &elided_realm); + gfx::ElideString(UTF8ToUTF16(auth_info->realm), 120, &elided_realm); string16 host_and_port = ASCIIToUTF16(request_url.scheme() + "://" + auth_info->challenger.ToString()); diff --git a/chrome/browser/ui/omnibox/location_bar_util.cc b/chrome/browser/ui/omnibox/location_bar_util.cc index efcf26b8b9a821..71220d3f05b4cd 100644 --- a/chrome/browser/ui/omnibox/location_bar_util.cc +++ b/chrome/browser/ui/omnibox/location_bar_util.cc @@ -9,10 +9,10 @@ #include "chrome/browser/extensions/extension_action.h" #include "third_party/skia/include/core/SkPaint.h" #include "third_party/skia/include/effects/SkGradientShader.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/canvas.h" #include "ui/gfx/color_utils.h" #include "ui/gfx/rect.h" +#include "ui/gfx/text_elider.h" namespace location_bar_util { @@ -24,7 +24,7 @@ string16 CalculateMinString(const string16& description) { string16 min_string; if (chop_index == string16::npos) { // No dot or whitespace, truncate to at most 3 chars. - min_string = ui::TruncateString(description, 3); + min_string = gfx::TruncateString(description, 3); } else { min_string = description.substr(0, chop_index); } diff --git a/chrome/browser/ui/toolbar/back_forward_menu_model.cc b/chrome/browser/ui/toolbar/back_forward_menu_model.cc index cdce41353819cc..9f26f693795ffa 100644 --- a/chrome/browser/ui/toolbar/back_forward_menu_model.cc +++ b/chrome/browser/ui/toolbar/back_forward_menu_model.cc @@ -28,9 +28,9 @@ #include "net/base/registry_controlled_domains/registry_controlled_domain.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/base/text/text_elider.h" #include "ui/base/window_open_disposition.h" #include "ui/gfx/favicon_size.h" +#include "ui/gfx/text_elider.h" using content::NavigationController; using content::NavigationEntry; @@ -107,7 +107,7 @@ string16 BackForwardMenuModel::GetLabelAt(int index) const { string16 menu_text(entry->GetTitleForDisplay( profile->GetPrefs()->GetString(prefs::kAcceptLanguages))); menu_text = - ui::ElideText(menu_text, gfx::Font(), kMaxWidth, ui::ELIDE_AT_END); + gfx::ElideText(menu_text, gfx::Font(), kMaxWidth, gfx::ELIDE_AT_END); #if !defined(OS_MACOSX) for (size_t i = menu_text.find('&'); i != string16::npos; diff --git a/chrome/browser/ui/views/bookmarks/bookmark_bar_view.cc b/chrome/browser/ui/views/bookmarks/bookmark_bar_view.cc index c8fbf0834fba8b..9bb4a529877066 100644 --- a/chrome/browser/ui/views/bookmarks/bookmark_bar_view.cc +++ b/chrome/browser/ui/views/bookmarks/bookmark_bar_view.cc @@ -68,10 +68,10 @@ #include "ui/base/dragdrop/os_exchange_data.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/base/text/text_elider.h" #include "ui/base/theme_provider.h" #include "ui/base/window_open_disposition.h" #include "ui/gfx/canvas.h" +#include "ui/gfx/text_elider.h" #include "ui/views/button_drag_utils.h" #include "ui/views/controls/button/menu_button.h" #include "ui/views/controls/label.h" @@ -616,8 +616,8 @@ string16 BookmarkBarView::CreateToolTipForURLAndTitle( if (!title.empty()) { string16 localized_title = title; base::i18n::AdjustStringForLocaleDirection(&localized_title); - result.append(ui::ElideText(localized_title, tt_fonts, max_width, - ui::ELIDE_AT_END)); + result.append(gfx::ElideText(localized_title, tt_fonts, max_width, + gfx::ELIDE_AT_END)); } // Only show the URL if the url and title differ. @@ -633,7 +633,7 @@ string16 BookmarkBarView::CreateToolTipForURLAndTitle( // default. std::string languages = profile->GetPrefs()->GetString( prefs::kAcceptLanguages); - string16 elided_url(ui::ElideUrl(url, tt_fonts, max_width, languages)); + string16 elided_url(gfx::ElideUrl(url, tt_fonts, max_width, languages)); elided_url = base::i18n::GetDisplayStringInLTRDirectionality(elided_url); result.append(elided_url); } diff --git a/chrome/browser/ui/views/download/download_item_view.cc b/chrome/browser/ui/views/download/download_item_view.cc index dc53662a2aca43..e9004e3eac0734 100644 --- a/chrome/browser/ui/views/download/download_item_view.cc +++ b/chrome/browser/ui/views/download/download_item_view.cc @@ -36,11 +36,11 @@ #include "ui/base/events/event.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/base/text/text_elider.h" #include "ui/base/theme_provider.h" #include "ui/gfx/canvas.h" #include "ui/gfx/color_utils.h" #include "ui/gfx/image/image.h" +#include "ui/gfx/text_elider.h" #include "ui/views/controls/button/label_button.h" #include "ui/views/controls/label.h" #include "ui/views/widget/root_view.h" @@ -748,7 +748,7 @@ void DownloadItemView::OnPaint(gfx::Canvas* canvas) { if (!IsShowingWarningDialog()) { string16 filename; if (!disabled_while_opening_) { - filename = ui::ElideFilename(download()->GetFileNameToReportUser(), + filename = gfx::ElideFilename(download()->GetFileNameToReportUser(), font_list_, kTextWidth); } else { // First, Calculate the download status opening string width. @@ -757,7 +757,7 @@ void DownloadItemView::OnPaint(gfx::Canvas* canvas) { int status_string_width = font_list_.GetStringWidth(status_string); // Then, elide the file name. string16 filename_string = - ui::ElideFilename(download()->GetFileNameToReportUser(), font_list_, + gfx::ElideFilename(download()->GetFileNameToReportUser(), font_list_, kTextWidth - status_string_width); // Last, concat the whole string. filename = l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_OPENING, diff --git a/chrome/browser/ui/views/extensions/extension_installed_bubble_view.cc b/chrome/browser/ui/views/extensions/extension_installed_bubble_view.cc index 5ec0dc8b0d2461..6418fc04ffb14c 100644 --- a/chrome/browser/ui/views/extensions/extension_installed_bubble_view.cc +++ b/chrome/browser/ui/views/extensions/extension_installed_bubble_view.cc @@ -35,8 +35,8 @@ #include "grit/ui_resources.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/render_text.h" +#include "ui/gfx/text_elider.h" #include "ui/views/controls/button/image_button.h" #include "ui/views/controls/image_view.h" #include "ui/views/controls/label.h" @@ -343,8 +343,8 @@ class InstalledBubbleContent : public views::View, // Word-wrap the full label text. const gfx::Font font; std::vector lines; - ui::ElideRectangleText(full_text, font, contents_area.width(), - contents_area.height(), ui::ELIDE_LONG_WORDS, + gfx::ElideRectangleText(full_text, font, contents_area.width(), + contents_area.height(), gfx::ELIDE_LONG_WORDS, &lines); gfx::Point position = gfx::Point( diff --git a/chrome/browser/ui/views/external_protocol_dialog.cc b/chrome/browser/ui/views/external_protocol_dialog.cc index 1b04dbb0dfb4df..3405c5cb40a9b1 100644 --- a/chrome/browser/ui/views/external_protocol_dialog.cc +++ b/chrome/browser/ui/views/external_protocol_dialog.cc @@ -18,7 +18,7 @@ #include "grit/chromium_strings.h" #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" #include "ui/views/controls/message_box_view.h" #include "ui/views/widget/widget.h" @@ -134,9 +134,9 @@ ExternalProtocolDialog::ExternalProtocolDialog(WebContents* web_contents, const int kMaxCommandSize = 256; string16 elided_url_without_scheme; string16 elided_command; - ui::ElideString(ASCIIToUTF16(url.possibly_invalid_spec()), + gfx::ElideString(ASCIIToUTF16(url.possibly_invalid_spec()), kMaxUrlWithoutSchemeSize, &elided_url_without_scheme); - ui::ElideString(WideToUTF16Hack(command), kMaxCommandSize, &elided_command); + gfx::ElideString(WideToUTF16Hack(command), kMaxCommandSize, &elided_command); string16 message_text = l10n_util::GetStringFUTF16( IDS_EXTERNAL_PROTOCOL_INFORMATION, diff --git a/chrome/browser/ui/views/frame/global_menu_bar_x11.cc b/chrome/browser/ui/views/frame/global_menu_bar_x11.cc index 674c69f6a0eac4..24198aaf8c9165 100644 --- a/chrome/browser/ui/views/frame/global_menu_bar_x11.cc +++ b/chrome/browser/ui/views/frame/global_menu_bar_x11.cc @@ -31,7 +31,7 @@ #include "ui/base/accelerators/menu_label_accelerator_util_linux.h" #include "ui/base/keycodes/keyboard_code_conversion_x.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" // libdbusmenu-glib types typedef struct _DbusmenuMenuitem DbusmenuMenuitem; @@ -524,7 +524,7 @@ void GlobalMenuBarX11::AddHistoryItemToMenu(HistoryItem* item, if (title.empty()) title = UTF8ToUTF16(url_string); - ui::ElideString(title, kMaximumMenuWidthInChars, &title); + gfx::ElideString(title, kMaximumMenuWidthInChars, &title); DbusmenuMenuitem* menu_item = BuildMenuItem(UTF16ToUTF8(title), tag); g_signal_connect(menu_item, "item-activated", diff --git a/chrome/browser/ui/views/omnibox/omnibox_result_view.cc b/chrome/browser/ui/views/omnibox/omnibox_result_view.cc index fe2225ff7ce3b4..8afee9338ceb3a 100644 --- a/chrome/browser/ui/views/omnibox/omnibox_result_view.cc +++ b/chrome/browser/ui/views/omnibox/omnibox_result_view.cc @@ -21,12 +21,12 @@ #include "grit/generated_resources.h" #include "grit/theme_resources.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/base/text/text_elider.h" #include "ui/base/theme_provider.h" #include "ui/gfx/canvas.h" #include "ui/gfx/color_utils.h" #include "ui/gfx/image/image.h" #include "ui/gfx/render_text.h" +#include "ui/gfx/text_elider.h" #include "ui/native_theme/native_theme.h" #if defined(OS_WIN) @@ -517,7 +517,7 @@ void OmniboxResultView::Elide(Runs* runs, int remaining_width) const { (*j)->GetPrimaryFont().DeriveFont(0, gfx::Font::BOLD) : (*j)->GetPrimaryFont()); string16 elided_text( - ui::ElideText((*j)->text(), font, remaining_width, ui::ELIDE_AT_END)); + gfx::ElideText((*j)->text(), font, remaining_width, gfx::ELIDE_AT_END)); Classifications::reverse_iterator prior(j + 1); const bool on_first_classification = (prior == i->classifications.rend()); if (elided_text.empty() && (remaining_width >= ellipsis_width_) && diff --git a/chrome/browser/ui/views/status_bubble_views.cc b/chrome/browser/ui/views/status_bubble_views.cc index 4c290e0f04d547..0fefaee7745a35 100644 --- a/chrome/browser/ui/views/status_bubble_views.cc +++ b/chrome/browser/ui/views/status_bubble_views.cc @@ -21,12 +21,12 @@ #include "ui/base/animation/animation_delegate.h" #include "ui/base/animation/linear_animation.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/base/text/text_elider.h" #include "ui/base/theme_provider.h" #include "ui/gfx/canvas.h" #include "ui/gfx/point.h" #include "ui/gfx/screen.h" #include "ui/gfx/skia_util.h" +#include "ui/gfx/text_elider.h" #include "ui/native_theme/native_theme.h" #include "ui/views/controls/label.h" #include "ui/views/controls/scrollbar/native_scroll_bar.h" @@ -663,7 +663,7 @@ void StatusBubbleViews::SetURL(const GURL& url, const std::string& languages) { gfx::Rect popup_bounds = popup_->GetWindowBoundsInScreen(); int text_width = static_cast(popup_bounds.width() - (kShadowThickness * 2) - kTextPositionX - kTextHorizPadding - 1); - url_text_ = ui::ElideUrl(url, view_->Label::font(), text_width, languages); + url_text_ = gfx::ElideUrl(url, view_->Label::font(), text_width, languages); // An URL is always treated as a left-to-right string. On right-to-left UIs // we need to explicitly mark the URL as LTR to make sure it is displayed @@ -820,7 +820,7 @@ void StatusBubbleViews::ExpandBubble() { // still be too long to fit) before expanding bubble. gfx::Rect popup_bounds = popup_->GetWindowBoundsInScreen(); int max_status_bubble_width = GetMaxStatusBubbleWidth(); - url_text_ = ui::ElideUrl(url_, view_->Label::font(), + url_text_ = gfx::ElideUrl(url_, view_->Label::font(), max_status_bubble_width, languages_); int expanded_bubble_width =std::max(GetStandardStatusBubbleWidth(), std::min(view_->Label::font().GetStringWidth(url_text_) + diff --git a/chrome/browser/ui/views/tabs/tab.cc b/chrome/browser/ui/views/tabs/tab.cc index fde98892ecf4e4..031b2ba734a007 100644 --- a/chrome/browser/ui/views/tabs/tab.cc +++ b/chrome/browser/ui/views/tabs/tab.cc @@ -32,7 +32,6 @@ #include "ui/base/layout.h" #include "ui/base/models/list_selection_model.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/base/text/text_elider.h" #include "ui/base/theme_provider.h" #include "ui/gfx/canvas.h" #include "ui/gfx/color_analysis.h" @@ -40,6 +39,7 @@ #include "ui/gfx/font.h" #include "ui/gfx/image/image_skia_operations.h" #include "ui/gfx/path.h" +#include "ui/gfx/text_elider.h" #include "ui/views/controls/button/image_button.h" #include "ui/views/widget/tooltip_manager.h" #include "ui/views/widget/widget.h" diff --git a/content/browser/renderer_host/render_widget_host_view_gtk.cc b/content/browser/renderer_host/render_widget_host_view_gtk.cc index 2a93cb7c979dd8..91c8ab05a80215 100644 --- a/content/browser/renderer_host/render_widget_host_view_gtk.cc +++ b/content/browser/renderer_host/render_widget_host_view_gtk.cc @@ -43,11 +43,11 @@ #include "third_party/WebKit/public/web/gtk/WebInputEventFactory.h" #include "ui/base/clipboard/scoped_clipboard_writer.h" #include "ui/base/gtk/gtk_compat.h" -#include "ui/base/text/text_elider.h" #include "ui/base/x/active_window_watcher_x.h" #include "ui/base/x/x11_util.h" #include "ui/gfx/gtk_native_view_id_manager.h" #include "ui/gfx/gtk_preserve_window.h" +#include "ui/gfx/text_elider.h" #include "webkit/common/cursors/webcursor_gtk_data.h" using WebKit::WebInputEventFactory; @@ -938,7 +938,7 @@ void RenderWidgetHostViewGtk::SetTooltipText(const string16& tooltip_text) { // this itself). // I filed https://bugzilla.gnome.org/show_bug.cgi?id=604641 upstream. const string16 clamped_tooltip = - ui::TruncateString(tooltip_text, kMaxTooltipLength); + gfx::TruncateString(tooltip_text, kMaxTooltipLength); if (clamped_tooltip.empty()) { gtk_widget_set_has_tooltip(view_.get(), FALSE); diff --git a/content/browser/renderer_host/render_widget_host_view_win.cc b/content/browser/renderer_host/render_widget_host_view_win.cc index 189262a01737d1..6aefe0f622861b 100644 --- a/content/browser/renderer_host/render_widget_host_view_win.cc +++ b/content/browser/renderer_host/render_widget_host_view_win.cc @@ -63,7 +63,6 @@ #include "ui/base/ime/win/imm32_manager.h" #include "ui/base/ime/win/tsf_input_scope.h" #include "ui/base/l10n/l10n_util_win.h" -#include "ui/base/text/text_elider.h" #include "ui/base/touch/touch_device.h" #include "ui/base/touch/touch_enabled.h" #include "ui/base/ui_base_switches.h" @@ -76,6 +75,7 @@ #include "ui/gfx/rect.h" #include "ui/gfx/rect_conversions.h" #include "ui/gfx/screen.h" +#include "ui/gfx/text_elider.h" #include "webkit/common/cursors/webcursor.h" #include "win8/util/win8_util.h" @@ -846,7 +846,7 @@ void RenderWidgetHostViewWin::SetTooltipText(const string16& tooltip_text) { // accidentally DOS the user with a mega tooltip (since Windows doesn't seem // to do this itself). const string16 new_tooltip_text = - ui::TruncateString(tooltip_text, kMaxTooltipLength); + gfx::TruncateString(tooltip_text, kMaxTooltipLength); if (new_tooltip_text != tooltip_text_) { tooltip_text_ = new_tooltip_text; diff --git a/content/browser/web_contents/navigation_entry_impl.cc b/content/browser/web_contents/navigation_entry_impl.cc index 0a8664493d0dd9..10d50ca0df2a54 100644 --- a/content/browser/web_contents/navigation_entry_impl.cc +++ b/content/browser/web_contents/navigation_entry_impl.cc @@ -10,7 +10,7 @@ #include "content/public/common/content_constants.h" #include "content/public/common/url_constants.h" #include "net/base/net_util.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" // Use this to get a new unique ID for a NavigationEntry during construction. // The returned ID is guaranteed to be nonzero (which is the "no ID" indicator). @@ -191,7 +191,7 @@ const string16& NavigationEntryImpl::GetTitleForDisplay( title = title.substr(slashpos + 1); } - ui::ElideString(title, kMaxTitleChars, &cached_display_title_); + gfx::ElideString(title, kMaxTitleChars, &cached_display_title_); return cached_display_title_; } diff --git a/content/shell/browser/shell_login_dialog.cc b/content/shell/browser/shell_login_dialog.cc index f1639e91553162..607fd66ac5aa4a 100644 --- a/content/shell/browser/shell_login_dialog.cc +++ b/content/shell/browser/shell_login_dialog.cc @@ -11,7 +11,7 @@ #include "content/public/browser/resource_dispatcher_host.h" #include "net/base/auth.h" #include "net/url_request/url_request.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" namespace content { @@ -74,7 +74,7 @@ void ShellLoginDialog::PrepDialog(const string16& host, // The realm is controlled by the remote server, so there is no reason to // believe it is of a reasonable length. string16 elided_realm; - ui::ElideString(realm, 120, &elided_realm); + gfx::ElideString(realm, 120, &elided_realm); string16 explanation = ASCIIToUTF16("The server ") + host + diff --git a/printing/backend/print_backend.cc b/printing/backend/print_backend.cc index 958343f4ac8d63..29dc1fd5e9b48f 100644 --- a/printing/backend/print_backend.cc +++ b/printing/backend/print_backend.cc @@ -7,7 +7,7 @@ #include #include "third_party/icu/source/common/unicode/uchar.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" namespace { @@ -44,7 +44,7 @@ string16 PrintBackend::SimplifyDocumentTitle(const string16& title) { std::remove_if(no_controls.begin(), no_controls.end(), &u_iscntrl), no_controls.end()); string16 result; - ui::ElideString(no_controls, kMaxDocumentTitleLength, &result); + gfx::ElideString(no_controls, kMaxDocumentTitleLength, &result); return result; } diff --git a/printing/print_settings_initializer.cc b/printing/print_settings_initializer.cc index 867f4dcace26b5..49a7bd861a406a 100644 --- a/printing/print_settings_initializer.cc +++ b/printing/print_settings_initializer.cc @@ -16,8 +16,8 @@ #include "printing/print_job_constants.h" #include "printing/print_settings.h" #include "printing/units.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/font.h" +#include "ui/gfx/text_elider.h" #include "url/gurl.h" using base::DictionaryValue; @@ -44,7 +44,7 @@ void PrintSettingsInitializer::InitHeaderFooterStrings( print_settings->date = date; print_settings->title = title; - print_settings->url = ui::ElideUrl(GURL(url), gfx::Font(), 0, std::string()); + print_settings->url = gfx::ElideUrl(GURL(url), gfx::Font(), 0, std::string()); } } // namespace printing diff --git a/printing/printed_document.cc b/printing/printed_document.cc index 59cc3856923193..fa40e1f3115ea4 100644 --- a/printing/printed_document.cc +++ b/printing/printed_document.cc @@ -23,8 +23,8 @@ #include "printing/printed_pages_source.h" #include "printing/units.h" #include "skia/ext/platform_device.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/font.h" +#include "ui/gfx/text_elider.h" namespace { diff --git a/ui/base/cocoa/menu_controller.mm b/ui/base/cocoa/menu_controller.mm index ebec346bc0b452..c7b8dff2fa3c8b 100644 --- a/ui/base/cocoa/menu_controller.mm +++ b/ui/base/cocoa/menu_controller.mm @@ -11,9 +11,9 @@ #import "ui/base/cocoa/cocoa_event_utils.h" #include "ui/base/l10n/l10n_util_mac.h" #include "ui/base/models/simple_menu_model.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/font.h" #include "ui/gfx/image/image.h" +#include "ui/gfx/text_elider.h" @interface MenuController (Private) - (void)addSeparatorToMenu:(NSMenu*)menu @@ -30,7 +30,7 @@ + (string16)elideMenuTitle:(const string16&)title NSFont* nsfont = [NSFont menuBarFontOfSize:0]; // 0 means "default" gfx::Font font(base::SysNSStringToUTF8([nsfont fontName]), static_cast([nsfont pointSize])); - return ui::ElideText(title, font, width, ui::ELIDE_AT_END); + return gfx::ElideText(title, font, width, gfx::ELIDE_AT_END); } - (id)init { diff --git a/ui/base/text/text_elider.cc b/ui/base/text/text_elider.cc index 76dbb719946a0e..ed7b4d30cf7b0b 100644 --- a/ui/base/text/text_elider.cc +++ b/ui/base/text/text_elider.cc @@ -7,7 +7,7 @@ // Note that several of the functions declared in text_elider.h are implemented // in this file using helper classes in an unnamed namespace. -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" #include #include @@ -814,7 +814,7 @@ class RectangleText { RectangleText(const gfx::FontList& font_list, int available_pixel_width, int available_pixel_height, - ui::WordWrapBehavior wrap_behavior, + gfx::WordWrapBehavior wrap_behavior, std::vector* lines) : font_list_(font_list), line_height_(font_list.GetHeight()), @@ -882,7 +882,7 @@ class RectangleText { const int available_pixel_height_; // The wrap behavior for words that are too long to fit on a single line. - const ui::WordWrapBehavior wrap_behavior_; + const gfx::WordWrapBehavior wrap_behavior_; // The current running width. int current_width_; @@ -938,8 +938,8 @@ int RectangleText::Finalize() { } if (last_line_ended_in_lf_) lines_->push_back(string16()); - return (insufficient_width_ ? ui::INSUFFICIENT_SPACE_HORIZONTAL : 0) | - (insufficient_height_ ? ui::INSUFFICIENT_SPACE_VERTICAL : 0); + return (insufficient_width_ ? gfx::INSUFFICIENT_SPACE_HORIZONTAL : 0) | + (insufficient_height_ ? gfx::INSUFFICIENT_SPACE_VERTICAL : 0); } void RectangleText::AddLine(const string16& line) { @@ -984,8 +984,8 @@ int RectangleText::WrapWord(const string16& word) { bool first_fragment = true; while (!insufficient_height_ && !text.empty()) { string16 fragment = - ui::ElideText(text, font_list_, available_pixel_width_, - ui::TRUNCATE_AT_END); + gfx::ElideText(text, font_list_, available_pixel_width_, + gfx::TRUNCATE_AT_END); // At least one character has to be added at every line, even if the // available space is too small. if(fragment.empty()) @@ -1009,17 +1009,17 @@ int RectangleText::AddWordOverflow(const string16& word) { lines_added++; } - if (wrap_behavior_ == ui::IGNORE_LONG_WORDS) { + if (wrap_behavior_ == gfx::IGNORE_LONG_WORDS) { current_line_ = word; current_width_ = available_pixel_width_; - } else if (wrap_behavior_ == ui::WRAP_LONG_WORDS) { + } else if (wrap_behavior_ == gfx::WRAP_LONG_WORDS) { lines_added += WrapWord(word); } else { - const ui::ElideBehavior elide_behavior = - (wrap_behavior_ == ui::ELIDE_LONG_WORDS ? ui::ELIDE_AT_END : - ui::TRUNCATE_AT_END); + const gfx::ElideBehavior elide_behavior = + (wrap_behavior_ == gfx::ELIDE_LONG_WORDS ? gfx::ELIDE_AT_END : + gfx::TRUNCATE_AT_END); const string16 elided_word = - ui::ElideText(word, font_list_, available_pixel_width_, elide_behavior); + gfx::ElideText(word, font_list_, available_pixel_width_, elide_behavior); AddToCurrentLine(elided_word); insufficient_width_ = true; } @@ -1039,7 +1039,7 @@ int RectangleText::AddWord(const string16& word) { // Append the non-trimmed word, in case more words are added after. AddToCurrentLine(word); } else { - lines_added = AddWordOverflow(wrap_behavior_ == ui::IGNORE_LONG_WORDS ? + lines_added = AddWordOverflow(wrap_behavior_ == gfx::IGNORE_LONG_WORDS ? trimmed : word); } return lines_added; diff --git a/ui/base/text/text_elider_unittest.cc b/ui/base/text/text_elider_unittest.cc index 3c06cb543cf426..6522c3f3cfde15 100644 --- a/ui/base/text/text_elider_unittest.cc +++ b/ui/base/text/text_elider_unittest.cc @@ -4,7 +4,7 @@ // // Unit tests for eliding and formatting utility functions. -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" #include "base/files/file_path.h" #include "base/i18n/rtl.h" diff --git a/ui/gfx/canvas_skia.cc b/ui/gfx/canvas_skia.cc index fcbe1e613c6017..5d3860662794eb 100644 --- a/ui/gfx/canvas_skia.cc +++ b/ui/gfx/canvas_skia.cc @@ -7,13 +7,13 @@ #include "base/i18n/rtl.h" #include "base/logging.h" #include "base/memory/scoped_ptr.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/font_list.h" #include "ui/gfx/insets.h" #include "ui/gfx/range/range.h" #include "ui/gfx/rect.h" #include "ui/gfx/render_text.h" #include "ui/gfx/shadow_value.h" +#include "ui/gfx/text_elider.h" #include "ui/gfx/text_utils.h" namespace gfx { @@ -103,7 +103,7 @@ void ElideTextAndAdjustRange(const FontList& font_list, gfx::Range* range) { const base::char16 start_char = (range->IsValid() ? text->at(range->start()) : 0); - *text = ui::ElideText(*text, font_list, width, ui::ELIDE_AT_END); + *text = gfx::ElideText(*text, font_list, width, gfx::ELIDE_AT_END); if (!range->IsValid()) return; if (range->start() >= text->length() || @@ -183,15 +183,15 @@ void Canvas::SizeStringInt(const base::string16& text, #endif if ((flags & MULTI_LINE) && *width != 0) { - ui::WordWrapBehavior wrap_behavior = ui::TRUNCATE_LONG_WORDS; + gfx::WordWrapBehavior wrap_behavior = gfx::TRUNCATE_LONG_WORDS; if (flags & CHARACTER_BREAK) - wrap_behavior = ui::WRAP_LONG_WORDS; + wrap_behavior = gfx::WRAP_LONG_WORDS; else if (!(flags & NO_ELLIPSIS)) - wrap_behavior = ui::ELIDE_LONG_WORDS; + wrap_behavior = gfx::ELIDE_LONG_WORDS; Rect rect(*width, INT_MAX); std::vector strings; - ui::ElideRectangleText(adjusted_text, font_list, + gfx::ElideRectangleText(adjusted_text, font_list, rect.width(), rect.height(), wrap_behavior, &strings); scoped_ptr render_text(RenderText::CreateInstance()); @@ -258,14 +258,14 @@ void Canvas::DrawStringRectWithShadows(const base::string16& text, render_text->SetTextShadows(shadows); if (flags & MULTI_LINE) { - ui::WordWrapBehavior wrap_behavior = ui::IGNORE_LONG_WORDS; + gfx::WordWrapBehavior wrap_behavior = gfx::IGNORE_LONG_WORDS; if (flags & CHARACTER_BREAK) - wrap_behavior = ui::WRAP_LONG_WORDS; + wrap_behavior = gfx::WRAP_LONG_WORDS; else if (!(flags & NO_ELLIPSIS)) - wrap_behavior = ui::ELIDE_LONG_WORDS; + wrap_behavior = gfx::ELIDE_LONG_WORDS; std::vector strings; - ui::ElideRectangleText(adjusted_text, + gfx::ElideRectangleText(adjusted_text, font_list, text_bounds.width(), text_bounds.height(), wrap_behavior, diff --git a/ui/gfx/render_text.cc b/ui/gfx/render_text.cc index 25e0027e20ef2d..06640e059454da 100644 --- a/ui/gfx/render_text.cc +++ b/ui/gfx/render_text.cc @@ -13,12 +13,12 @@ #include "third_party/icu/source/common/unicode/utf16.h" #include "third_party/skia/include/core/SkTypeface.h" #include "third_party/skia/include/effects/SkGradientShader.h" -#include "ui/base/text/text_elider.h" #include "ui/base/text/utf16_indexing.h" #include "ui/gfx/canvas.h" #include "ui/gfx/insets.h" #include "ui/gfx/skia_util.h" #include "ui/gfx/text_constants.h" +#include "ui/gfx/text_elider.h" namespace gfx { @@ -977,7 +977,7 @@ void RenderText::UpdateLayoutText() { // Truncate the text at a valid character break and append an ellipsis. icu::StringCharacterIterator iter(text.c_str()); iter.setIndex32(truncate_length_ - 1); - layout_text_.assign(text.substr(0, iter.getIndex()) + ui::kEllipsisUTF16); + layout_text_.assign(text.substr(0, iter.getIndex()) + gfx::kEllipsisUTF16); } } diff --git a/ui/gfx/text_elider.cc b/ui/gfx/text_elider.cc new file mode 100644 index 00000000000000..bc4eb3bb858386 --- /dev/null +++ b/ui/gfx/text_elider.cc @@ -0,0 +1,1172 @@ +// Copyright (c) 2012 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. +// +// This file implements utility functions for eliding and formatting UI text. +// +// Note that several of the functions declared in text_elider.h are implemented +// in this file using helper classes in an unnamed namespace. + +#include "ui/gfx/text_elider.h" + +#include +#include + +#include "base/files/file_path.h" +#include "base/i18n/break_iterator.h" +#include "base/i18n/char_iterator.h" +#include "base/i18n/rtl.h" +#include "base/memory/scoped_ptr.h" +#include "base/strings/string_split.h" +#include "base/strings/string_util.h" +#include "base/strings/sys_string_conversions.h" +#include "base/strings/utf_string_conversions.h" +#include "net/base/escape.h" +#include "net/base/net_util.h" +#include "net/base/registry_controlled_domains/registry_controlled_domain.h" +#include "third_party/icu/source/common/unicode/rbbi.h" +#include "third_party/icu/source/common/unicode/uloc.h" +#include "ui/gfx/font_list.h" +#include "ui/gfx/text_utils.h" +#include "url/gurl.h" + +namespace gfx { + +// U+2026 in utf8 +const char kEllipsis[] = "\xE2\x80\xA6"; +const char16 kEllipsisUTF16[] = { 0x2026, 0 }; +const char16 kForwardSlash = '/'; + +namespace { + +// Helper class to split + elide text, while respecting UTF16 surrogate pairs. +class StringSlicer { + public: + StringSlicer(const string16& text, + const string16& ellipsis, + bool elide_in_middle) + : text_(text), + ellipsis_(ellipsis), + elide_in_middle_(elide_in_middle) { + } + + // Cuts |text_| to be |length| characters long. If |elide_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 be inserted at the cut point. + string16 CutString(size_t length, bool insert_ellipsis) { + const string16 ellipsis_text = insert_ellipsis ? ellipsis_ : string16(); + + if (!elide_in_middle_) + return text_.substr(0, FindValidBoundaryBefore(length)) + ellipsis_text; + + // We put the extra character, if any, before the cut. + const size_t half_length = length / 2; + const size_t prefix_length = FindValidBoundaryBefore(length - half_length); + const size_t suffix_start_guess = text_.length() - half_length; + const size_t suffix_start = FindValidBoundaryAfter(suffix_start_guess); + const size_t suffix_length = + half_length - (suffix_start_guess - suffix_start); + return text_.substr(0, prefix_length) + ellipsis_text + + text_.substr(suffix_start, suffix_length); + } + + private: + // Returns a valid cut boundary at or before |index|. + size_t FindValidBoundaryBefore(size_t index) const { + DCHECK_LE(index, text_.length()); + if (index != text_.length()) + U16_SET_CP_START(text_.data(), 0, index); + return index; + } + + // Returns a valid cut boundary at or after |index|. + size_t FindValidBoundaryAfter(size_t index) const { + DCHECK_LE(index, text_.length()); + if (index != text_.length()) + U16_SET_CP_LIMIT(text_.data(), 0, index, text_.length()); + return index; + } + + // The text to be sliced. + const string16& text_; + + // Ellipsis string to use. + const string16& ellipsis_; + + // If true, the middle of the string will be elided. + bool elide_in_middle_; + + DISALLOW_COPY_AND_ASSIGN(StringSlicer); +}; + +// Build a path from the first |num_components| elements in |path_elements|. +// Prepends |path_prefix|, appends |filename|, inserts ellipsis if appropriate. +string16 BuildPathFromComponents(const string16& path_prefix, + const std::vector& path_elements, + const string16& filename, + size_t num_components) { + // Add the initial elements of the path. + string16 path = path_prefix; + + // Build path from first |num_components| elements. + for (size_t j = 0; j < num_components; ++j) + path += path_elements[j] + kForwardSlash; + + // Add |filename|, ellipsis if necessary. + if (num_components != (path_elements.size() - 1)) + path += string16(kEllipsisUTF16) + kForwardSlash; + path += filename; + + return path; +} + +// Takes a prefix (Domain, or Domain+subdomain) and a collection of path +// components and elides if possible. Returns a string containing the longest +// possible elided path, or an empty string if elision is not possible. +string16 ElideComponentizedPath(const string16& url_path_prefix, + const std::vector& url_path_elements, + const string16& url_filename, + const string16& url_query, + const gfx::FontList& font_list, + int available_pixel_width) { + const size_t url_path_number_of_elements = url_path_elements.size(); + + CHECK(url_path_number_of_elements); + for (size_t i = url_path_number_of_elements - 1; i > 0; --i) { + string16 elided_path = BuildPathFromComponents(url_path_prefix, + url_path_elements, url_filename, i); + if (available_pixel_width >= gfx::GetStringWidth(elided_path, font_list)) + return ElideText(elided_path + url_query, font_list, + available_pixel_width, ELIDE_AT_END); + } + + return string16(); +} + +} // namespace + +string16 ElideEmail(const string16& email, + const gfx::FontList& font_list, + int available_pixel_width) { + if (gfx::GetStringWidth(email, font_list) <= available_pixel_width) + return email; + + // Split the email into its local-part (username) and domain-part. The email + // spec technically allows for @ symbols in the local-part (username) of the + // email under some special requirements. It is guaranteed that there is no @ + // symbol in the domain part of the email however so splitting at the last @ + // symbol is safe. + const size_t split_index = email.find_last_of('@'); + DCHECK_NE(split_index, string16::npos); + string16 username = email.substr(0, split_index); + string16 domain = email.substr(split_index + 1); + DCHECK(!username.empty()); + DCHECK(!domain.empty()); + + // Subtract the @ symbol from the available width as it is mandatory. + const string16 kAtSignUTF16 = ASCIIToUTF16("@"); + available_pixel_width -= gfx::GetStringWidth(kAtSignUTF16, font_list); + + // Check whether eliding the domain is necessary: if eliding the username + // is sufficient, the domain will not be elided. + const int full_username_width = gfx::GetStringWidth(username, font_list); + const int available_domain_width = + available_pixel_width - + std::min(full_username_width, + gfx::GetStringWidth(username.substr(0, 1) + kEllipsisUTF16, + font_list)); + if (gfx::GetStringWidth(domain, font_list) > available_domain_width) { + // Elide the domain so that it only takes half of the available width. + // Should the username not need all the width available in its half, the + // domain will occupy the leftover width. + // If |desired_domain_width| is greater than |available_domain_width|: the + // minimal username elision allowed by the specifications will not fit; thus + // |desired_domain_width| must be <= |available_domain_width| at all cost. + const int desired_domain_width = + std::min(available_domain_width, + std::max(available_pixel_width - full_username_width, + available_pixel_width / 2)); + domain = ElideText(domain, font_list, desired_domain_width, + ELIDE_IN_MIDDLE); + // Failing to elide the domain such that at least one character remains + // (other than the ellipsis itself) remains: return a single ellipsis. + if (domain.length() <= 1U) + return string16(kEllipsisUTF16); + } + + // Fit the username in the remaining width (at this point the elided username + // is guaranteed to fit with at least one character remaining given all the + // precautions taken earlier). + available_pixel_width -= gfx::GetStringWidth(domain, font_list); + username = ElideText(username, font_list, available_pixel_width, + ELIDE_AT_END); + + return username + kAtSignUTF16 + domain; +} + +string16 ElideEmail(const string16& email, + const gfx::Font& font, + int available_pixel_width) { + return ElideEmail(email, gfx::FontList(font), available_pixel_width); +} + +// TODO(pkasting): http://crbug.com/77883 This whole function gets +// kerning/ligatures/etc. issues potentially wrong by assuming that the width of +// a rendered string is always the sum of the widths of its substrings. Also I +// suspect it could be made simpler. +string16 ElideUrl(const GURL& url, + const gfx::FontList& font_list, + int available_pixel_width, + const std::string& languages) { + // Get a formatted string and corresponding parsing of the url. + url_parse::Parsed parsed; + const string16 url_string = + net::FormatUrl(url, languages, net::kFormatUrlOmitAll, + net::UnescapeRule::SPACES, &parsed, NULL, NULL); + if (available_pixel_width <= 0) + return url_string; + + // If non-standard, return plain eliding. + if (!url.IsStandard()) + return ElideText(url_string, font_list, available_pixel_width, + ELIDE_AT_END); + + // Now start eliding url_string to fit within available pixel width. + // Fist pass - check to see whether entire url_string fits. + const int pixel_width_url_string = gfx::GetStringWidth(url_string, font_list); + if (available_pixel_width >= pixel_width_url_string) + return url_string; + + // Get the path substring, including query and reference. + const size_t path_start_index = parsed.path.begin; + const size_t path_len = parsed.path.len; + string16 url_path_query_etc = url_string.substr(path_start_index); + string16 url_path = url_string.substr(path_start_index, path_len); + + // Return general elided text if url minus the query fits. + const string16 url_minus_query = + url_string.substr(0, path_start_index + path_len); + if (available_pixel_width >= gfx::GetStringWidth(url_minus_query, font_list)) + return ElideText(url_string, font_list, available_pixel_width, + ELIDE_AT_END); + + // Get Host. + string16 url_host = UTF8ToUTF16(url.host()); + + // Get domain and registry information from the URL. + string16 url_domain = UTF8ToUTF16( + net::registry_controlled_domains::GetDomainAndRegistry( + url, net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)); + if (url_domain.empty()) + url_domain = url_host; + + // Add port if required. + if (!url.port().empty()) { + url_host += UTF8ToUTF16(":" + url.port()); + url_domain += UTF8ToUTF16(":" + url.port()); + } + + // Get sub domain. + string16 url_subdomain; + const size_t domain_start_index = url_host.find(url_domain); + if (domain_start_index != string16::npos) + url_subdomain = url_host.substr(0, domain_start_index); + const string16 kWwwPrefix = UTF8ToUTF16("www."); + if ((url_subdomain == kWwwPrefix || url_subdomain.empty() || + url.SchemeIsFile())) { + url_subdomain.clear(); + } + + // If this is a file type, the path is now defined as everything after ":". + // For example, "C:/aa/aa/bb", the path is "/aa/bb/cc". Interesting, the + // domain is now C: - this is a nice hack for eliding to work pleasantly. + if (url.SchemeIsFile()) { + // Split the path string using ":" + std::vector file_path_split; + base::SplitString(url_path, ':', &file_path_split); + if (file_path_split.size() > 1) { // File is of type "file:///C:/.." + url_host.clear(); + url_domain.clear(); + url_subdomain.clear(); + + const string16 kColon = UTF8ToUTF16(":"); + url_host = url_domain = file_path_split.at(0).substr(1) + kColon; + url_path_query_etc = url_path = file_path_split.at(1); + } + } + + // Second Pass - remove scheme - the rest fits. + const int pixel_width_url_host = gfx::GetStringWidth(url_host, font_list); + const int pixel_width_url_path = gfx::GetStringWidth(url_path_query_etc, + font_list); + if (available_pixel_width >= + pixel_width_url_host + pixel_width_url_path) + return url_host + url_path_query_etc; + + // Third Pass: Subdomain, domain and entire path fits. + const int pixel_width_url_domain = gfx::GetStringWidth(url_domain, font_list); + const int pixel_width_url_subdomain = gfx::GetStringWidth(url_subdomain, + font_list); + if (available_pixel_width >= + pixel_width_url_subdomain + pixel_width_url_domain + + pixel_width_url_path) + return url_subdomain + url_domain + url_path_query_etc; + + // Query element. + string16 url_query; + const int kPixelWidthDotsTrailer = gfx::GetStringWidth( + string16(kEllipsisUTF16), font_list); + if (parsed.query.is_nonempty()) { + url_query = UTF8ToUTF16("?") + url_string.substr(parsed.query.begin); + if (available_pixel_width >= + (pixel_width_url_subdomain + pixel_width_url_domain + + pixel_width_url_path - gfx::GetStringWidth(url_query, font_list))) { + return ElideText(url_subdomain + url_domain + url_path_query_etc, + font_list, available_pixel_width, ELIDE_AT_END); + } + } + + // Parse url_path using '/'. + std::vector url_path_elements; + base::SplitString(url_path, kForwardSlash, &url_path_elements); + + // Get filename - note that for a path ending with / + // such as www.google.com/intl/ads/, the file name is ads/. + size_t url_path_number_of_elements = url_path_elements.size(); + DCHECK(url_path_number_of_elements != 0); + string16 url_filename; + if ((url_path_elements.at(url_path_number_of_elements - 1)).length() > 0) { + url_filename = *(url_path_elements.end() - 1); + } else if (url_path_number_of_elements > 1) { // Path ends with a '/'. + url_filename = url_path_elements.at(url_path_number_of_elements - 2) + + kForwardSlash; + url_path_number_of_elements--; + } + DCHECK(url_path_number_of_elements != 0); + + const size_t kMaxNumberOfUrlPathElementsAllowed = 1024; + if (url_path_number_of_elements <= 1 || + 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_list, + available_pixel_width, ELIDE_AT_END); + } + + // Start eliding the path and replacing elements by ".../". + const string16 kEllipsisAndSlash = string16(kEllipsisUTF16) + kForwardSlash; + const int pixel_width_ellipsis_slash = gfx::GetStringWidth(kEllipsisAndSlash, + font_list); + + // Check with both subdomain and domain. + string16 elided_path = + ElideComponentizedPath(url_subdomain + url_domain, url_path_elements, + url_filename, url_query, font_list, + available_pixel_width); + if (!elided_path.empty()) + return elided_path; + + // Check with only domain. + // If a subdomain is present, add an ellipsis before domain. + // This is added only if the subdomain pixel width is larger than + // the pixel width of kEllipsis. Otherwise, subdomain remains, + // which means that this case has been resolved earlier. + string16 url_elided_domain = url_subdomain + url_domain; + if (pixel_width_url_subdomain > kPixelWidthDotsTrailer) { + if (!url_subdomain.empty()) + url_elided_domain = kEllipsisAndSlash[0] + url_domain; + else + url_elided_domain = url_domain; + + elided_path = ElideComponentizedPath(url_elided_domain, url_path_elements, + url_filename, url_query, font_list, + available_pixel_width); + + if (!elided_path.empty()) + return elided_path; + } + + // Return elided domain/.../filename anyway. + string16 final_elided_url_string(url_elided_domain); + const int url_elided_domain_width = gfx::GetStringWidth(url_elided_domain, + font_list); + + // A hack to prevent trailing ".../...". + if ((available_pixel_width - url_elided_domain_width) > + pixel_width_ellipsis_slash + kPixelWidthDotsTrailer + + gfx::GetStringWidth(ASCIIToUTF16("UV"), font_list)) { + final_elided_url_string += BuildPathFromComponents(string16(), + url_path_elements, url_filename, 1); + } else { + final_elided_url_string += url_path; + } + + return ElideText(final_elided_url_string, font_list, available_pixel_width, + ELIDE_AT_END); +} + +string16 ElideUrl(const GURL& url, + const gfx::Font& font, + int available_pixel_width, + const std::string& languages) { + return ElideUrl(url, gfx::FontList(font), available_pixel_width, languages); +} + +string16 ElideFilename(const base::FilePath& filename, + const gfx::FontList& font_list, + int available_pixel_width) { +#if defined(OS_WIN) + string16 filename_utf16 = filename.value(); + string16 extension = filename.Extension(); + string16 rootname = filename.BaseName().RemoveExtension().value(); +#elif defined(OS_POSIX) + string16 filename_utf16 = WideToUTF16(base::SysNativeMBToWide( + filename.value())); + string16 extension = WideToUTF16(base::SysNativeMBToWide( + filename.Extension())); + string16 rootname = WideToUTF16(base::SysNativeMBToWide( + filename.BaseName().RemoveExtension().value())); +#endif + + const int full_width = gfx::GetStringWidth(filename_utf16, font_list); + if (full_width <= available_pixel_width) + return base::i18n::GetDisplayStringInLTRDirectionality(filename_utf16); + + if (rootname.empty() || extension.empty()) { + const string16 elided_name = ElideText(filename_utf16, font_list, + available_pixel_width, ELIDE_AT_END); + return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); + } + + const int ext_width = gfx::GetStringWidth(extension, font_list); + const int root_width = gfx::GetStringWidth(rootname, font_list); + + // We may have trimmed the path. + if (root_width + ext_width <= available_pixel_width) { + const string16 elided_name = rootname + extension; + return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); + } + + if (ext_width >= available_pixel_width) { + const string16 elided_name = ElideText(rootname + extension, font_list, + available_pixel_width, + ELIDE_IN_MIDDLE); + return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); + } + + int available_root_width = available_pixel_width - ext_width; + string16 elided_name = + ElideText(rootname, font_list, available_root_width, ELIDE_AT_END); + elided_name += extension; + return base::i18n::GetDisplayStringInLTRDirectionality(elided_name); +} + +string16 ElideFilename(const base::FilePath& filename, + const gfx::Font& font, + int available_pixel_width) { + return ElideFilename(filename, gfx::FontList(font), available_pixel_width); +} + +string16 ElideText(const string16& text, + const gfx::FontList& font_list, + int available_pixel_width, + ElideBehavior elide_behavior) { + if (text.empty()) + return text; + + const int current_text_pixel_width = gfx::GetStringWidth(text, font_list); + const bool elide_in_middle = (elide_behavior == ELIDE_IN_MIDDLE); + const bool insert_ellipsis = (elide_behavior != TRUNCATE_AT_END); + + const string16 ellipsis = string16(kEllipsisUTF16); + StringSlicer slicer(text, ellipsis, elide_in_middle); + + // Pango will return 0 width for absurdly long strings. Cut the string in + // half and try again. + // This is caused by an int overflow in Pango (specifically, in + // pango_glyph_string_extents_range). It's actually more subtle than just + // returning 0, since on super absurdly long strings, the int can wrap and + // return positive numbers again. Detecting that is probably not worth it + // (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()) { + const string16 cut = slicer.CutString(text.length() / 2, false); + return ElideText(cut, font_list, available_pixel_width, elide_behavior); + } + + if (current_text_pixel_width <= available_pixel_width) + return text; + + if (insert_ellipsis && + gfx::GetStringWidth(ellipsis, font_list) > available_pixel_width) + return string16(); + + // Use binary search to compute the elided text. + size_t lo = 0; + size_t hi = text.length() - 1; + size_t guess; + for (guess = (lo + hi) / 2; lo <= hi; guess = (lo + hi) / 2) { + // We check the length of the whole desired string at once to ensure we + // handle kerning/ligatures/etc. correctly. + const string16 cut = slicer.CutString(guess, insert_ellipsis); + const int guess_length = gfx::GetStringWidth(cut, font_list); + // 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(slicer.CutString(guess / 2, false), + font_list, available_pixel_width, elide_behavior); + } + if (guess_length > available_pixel_width) + hi = guess - 1; + else + lo = guess + 1; + } + + return slicer.CutString(guess, insert_ellipsis); +} + +string16 ElideText(const string16& text, + const gfx::Font& font, + int available_pixel_width, + ElideBehavior elide_behavior) { + return ElideText(text, gfx::FontList(font), available_pixel_width, + elide_behavior); +} + +SortedDisplayURL::SortedDisplayURL(const GURL& url, + const std::string& languages) { + net::AppendFormattedHost(url, languages, &sort_host_); + string16 host_minus_www = net::StripWWW(sort_host_); + url_parse::Parsed parsed; + display_url_ = + net::FormatUrl(url, languages, net::kFormatUrlOmitAll, + net::UnescapeRule::SPACES, &parsed, &prefix_end_, NULL); + if (sort_host_.length() > host_minus_www.length()) { + prefix_end_ += sort_host_.length() - host_minus_www.length(); + sort_host_.swap(host_minus_www); + } +} + +SortedDisplayURL::SortedDisplayURL() : prefix_end_(0) { +} + +SortedDisplayURL::~SortedDisplayURL() { +} + +int SortedDisplayURL::Compare(const SortedDisplayURL& other, + icu::Collator* collator) const { + // Compare on hosts first. The host won't contain 'www.'. + UErrorCode compare_status = U_ZERO_ERROR; + UCollationResult host_compare_result = collator->compare( + static_cast(sort_host_.c_str()), + static_cast(sort_host_.length()), + static_cast(other.sort_host_.c_str()), + static_cast(other.sort_host_.length()), + compare_status); + DCHECK(U_SUCCESS(compare_status)); + if (host_compare_result != 0) + return host_compare_result; + + // Hosts match, compare on the portion of the url after the host. + string16 path = this->AfterHost(); + string16 o_path = other.AfterHost(); + compare_status = U_ZERO_ERROR; + UCollationResult path_compare_result = collator->compare( + static_cast(path.c_str()), + static_cast(path.length()), + static_cast(o_path.c_str()), + static_cast(o_path.length()), + compare_status); + DCHECK(U_SUCCESS(compare_status)); + if (path_compare_result != 0) + return path_compare_result; + + // Hosts and paths match, compare on the complete url. This'll push the www. + // ones to the end. + compare_status = U_ZERO_ERROR; + UCollationResult display_url_compare_result = collator->compare( + static_cast(display_url_.c_str()), + static_cast(display_url_.length()), + static_cast(other.display_url_.c_str()), + static_cast(other.display_url_.length()), + compare_status); + DCHECK(U_SUCCESS(compare_status)); + return display_url_compare_result; +} + +string16 SortedDisplayURL::AfterHost() const { + const size_t slash_index = display_url_.find(sort_host_, prefix_end_); + if (slash_index == string16::npos) { + NOTREACHED(); + return string16(); + } + return display_url_.substr(slash_index + sort_host_.length()); +} + +bool ElideString(const string16& input, int max_len, string16* output) { + DCHECK_GE(max_len, 0); + if (static_cast(input.length()) <= max_len) { + output->assign(input); + return false; + } + + switch (max_len) { + case 0: + output->clear(); + break; + case 1: + output->assign(input.substr(0, 1)); + break; + case 2: + output->assign(input.substr(0, 2)); + break; + case 3: + output->assign(input.substr(0, 1) + ASCIIToUTF16(".") + + input.substr(input.length() - 1)); + break; + case 4: + output->assign(input.substr(0, 1) + ASCIIToUTF16("..") + + input.substr(input.length() - 1)); + break; + default: { + int rstr_len = (max_len - 3) / 2; + int lstr_len = rstr_len + ((max_len - 3) % 2); + output->assign(input.substr(0, lstr_len) + ASCIIToUTF16("...") + + input.substr(input.length() - rstr_len)); + break; + } + } + + return true; +} + +namespace { + +// Internal class used to track progress of a rectangular string elide +// operation. Exists so the top-level ElideRectangleString() function +// can be broken into smaller methods sharing this state. +class RectangleString { + public: + RectangleString(size_t max_rows, size_t max_cols, + bool strict, string16 *output) + : max_rows_(max_rows), + max_cols_(max_cols), + current_row_(0), + current_col_(0), + strict_(strict), + suppressed_(false), + output_(output) {} + + // Perform deferred initializations following creation. Must be called + // before any input can be added via AddString(). + void Init() { output_->clear(); } + + // Add an input string, reformatting to fit the desired dimensions. + // AddString() may be called multiple times to concatenate together + // multiple strings into the region (the current caller doesn't do + // this, however). + void AddString(const string16& input); + + // Perform any deferred output processing. Must be called after the + // last AddString() call has occurred. + bool Finalize(); + + private: + // Add a line to the rectangular region at the current position, + // either by itself or by breaking it into words. + void AddLine(const string16& line); + + // Add a word to the rectangular region at the current position, + // either by itself or by breaking it into characters. + void AddWord(const string16& word); + + // Add text to the output string if the rectangular boundaries + // have not been exceeded, advancing the current position. + void Append(const string16& string); + + // Set the current position to the beginning of the next line. If + // |output| is true, add a newline to the output string if the rectangular + // boundaries have not been exceeded. If |output| is false, we assume + // some other mechanism will (likely) do similar breaking after the fact. + void NewLine(bool output); + + // Maximum number of rows allowed in the output string. + size_t max_rows_; + + // Maximum number of characters allowed in the output string. + size_t max_cols_; + + // Current row position, always incremented and may exceed max_rows_ + // when the input can not fit in the region. We stop appending to + // the output string, however, when this condition occurs. In the + // future, we may want to expose this value to allow the caller to + // determine how many rows would actually be required to hold the + // formatted string. + size_t current_row_; + + // Current character position, should never exceed max_cols_. + size_t current_col_; + + // True when we do whitespace to newline conversions ourselves. + bool strict_; + + // True when some of the input has been truncated. + bool suppressed_; + + // String onto which the output is accumulated. + string16* output_; + + DISALLOW_COPY_AND_ASSIGN(RectangleString); +}; + +void RectangleString::AddString(const string16& input) { + base::i18n::BreakIterator lines(input, + base::i18n::BreakIterator::BREAK_NEWLINE); + if (lines.Init()) { + while (lines.Advance()) + AddLine(lines.GetString()); + } else { + NOTREACHED() << "BreakIterator (lines) init failed"; + } +} + +bool RectangleString::Finalize() { + if (suppressed_) { + output_->append(ASCIIToUTF16("...")); + return true; + } + return false; +} + +void RectangleString::AddLine(const string16& line) { + if (line.length() < max_cols_) { + Append(line); + } else { + base::i18n::BreakIterator words(line, + base::i18n::BreakIterator::BREAK_SPACE); + if (words.Init()) { + while (words.Advance()) + AddWord(words.GetString()); + } else { + NOTREACHED() << "BreakIterator (words) init failed"; + } + } + // Account for naturally-occuring newlines. + ++current_row_; + current_col_ = 0; +} + +void RectangleString::AddWord(const string16& word) { + if (word.length() < max_cols_) { + // Word can be made to fit, no need to fragment it. + if (current_col_ + word.length() >= max_cols_) + NewLine(strict_); + Append(word); + } else { + // Word is so big that it must be fragmented. + int array_start = 0; + int char_start = 0; + base::i18n::UTF16CharIterator chars(&word); + while (!chars.end()) { + // When boundary is hit, add as much as will fit on this line. + if (current_col_ + (chars.char_pos() - char_start) >= max_cols_) { + Append(word.substr(array_start, chars.array_pos() - array_start)); + NewLine(true); + array_start = chars.array_pos(); + char_start = chars.char_pos(); + } + chars.Advance(); + } + // Add the last remaining fragment, if any. + if (array_start != chars.array_pos()) + Append(word.substr(array_start, chars.array_pos() - array_start)); + } +} + +void RectangleString::Append(const string16& string) { + if (current_row_ < max_rows_) + output_->append(string); + else + suppressed_ = true; + current_col_ += string.length(); +} + +void RectangleString::NewLine(bool output) { + if (current_row_ < max_rows_) { + if (output) + output_->append(ASCIIToUTF16("\n")); + } else { + suppressed_ = true; + } + ++current_row_; + current_col_ = 0; +} + +// Internal class used to track progress of a rectangular text elide +// operation. Exists so the top-level ElideRectangleText() function +// can be broken into smaller methods sharing this state. +class RectangleText { + public: + RectangleText(const gfx::FontList& font_list, + int available_pixel_width, + int available_pixel_height, + WordWrapBehavior wrap_behavior, + std::vector* lines) + : font_list_(font_list), + line_height_(font_list.GetHeight()), + available_pixel_width_(available_pixel_width), + available_pixel_height_(available_pixel_height), + wrap_behavior_(wrap_behavior), + current_width_(0), + current_height_(0), + last_line_ended_in_lf_(false), + lines_(lines), + insufficient_width_(false), + insufficient_height_(false) {} + + // Perform deferred initializions following creation. Must be called + // before any input can be added via AddString(). + void Init() { lines_->clear(); } + + // Add an input string, reformatting to fit the desired dimensions. + // AddString() may be called multiple times to concatenate together + // multiple strings into the region (the current caller doesn't do + // this, however). + void AddString(const string16& input); + + // Perform any deferred output processing. Must be called after the last + // AddString() call has occured. Returns a combination of + // |ReformattingResultFlags| indicating whether the given width or height was + // insufficient, leading to elision or truncation. + int Finalize(); + + private: + // Add a line to the rectangular region at the current position, + // either by itself or by breaking it into words. + void AddLine(const string16& line); + + // Wrap the specified word across multiple lines. + int WrapWord(const string16& word); + + // Add a long word - wrapping, eliding or truncating per the wrap behavior. + int AddWordOverflow(const string16& word); + + // Add a word to the rectangluar region at the current position. + int AddWord(const string16& word); + + // Append the specified |text| to the current output line, incrementing the + // running width by the specified amount. This is an optimization over + // |AddToCurrentLine()| when |text_width| is already known. + void AddToCurrentLineWithWidth(const string16& text, int text_width); + + // Append the specified |text| to the current output line. + void AddToCurrentLine(const string16& text); + + // Set the current position to the beginning of the next line. + bool NewLine(); + + // The font list used for measuring text width. + const gfx::FontList& font_list_; + + // The height of each line of text. + const int line_height_; + + // The number of pixels of available width in the rectangle. + const int available_pixel_width_; + + // The number of pixels of available height in the rectangle. + const int available_pixel_height_; + + // The wrap behavior for words that are too long to fit on a single line. + const WordWrapBehavior wrap_behavior_; + + // The current running width. + int current_width_; + + // The current running height. + int current_height_; + + // The current line of text. + string16 current_line_; + + // Indicates whether the last line ended with \n. + bool last_line_ended_in_lf_; + + // The output vector of lines. + std::vector* lines_; + + // Indicates whether a word was so long that it had to be truncated or elided + // to fit the available width. + bool insufficient_width_; + + // Indicates whether there were too many lines for the available height. + bool insufficient_height_; + + DISALLOW_COPY_AND_ASSIGN(RectangleText); +}; + +void RectangleText::AddString(const string16& input) { + base::i18n::BreakIterator lines(input, + base::i18n::BreakIterator::BREAK_NEWLINE); + if (lines.Init()) { + while (!insufficient_height_ && lines.Advance()) { + string16 line = lines.GetString(); + // The BREAK_NEWLINE iterator will keep the trailing newline character, + // except in the case of the last line, which may not have one. Remove + // the newline character, if it exists. + last_line_ended_in_lf_ = !line.empty() && line[line.length() - 1] == '\n'; + if (last_line_ended_in_lf_) + line.resize(line.length() - 1); + AddLine(line); + } + } else { + NOTREACHED() << "BreakIterator (lines) init failed"; + } +} + +int RectangleText::Finalize() { + // Remove trailing whitespace from the last line or remove the last line + // completely, if it's just whitespace. + if (!insufficient_height_ && !lines_->empty()) { + TrimWhitespace(lines_->back(), TRIM_TRAILING, &lines_->back()); + if (lines_->back().empty() && !last_line_ended_in_lf_) + lines_->pop_back(); + } + if (last_line_ended_in_lf_) + lines_->push_back(string16()); + return (insufficient_width_ ? INSUFFICIENT_SPACE_HORIZONTAL : 0) | + (insufficient_height_ ? INSUFFICIENT_SPACE_VERTICAL : 0); +} + +void RectangleText::AddLine(const string16& line) { + const int line_width = gfx::GetStringWidth(line, font_list_); + if (line_width <= available_pixel_width_) { + AddToCurrentLineWithWidth(line, line_width); + } else { + // Iterate over positions that are valid to break the line at. In general, + // these are word boundaries but after any punctuation following the word. + base::i18n::BreakIterator words(line, + base::i18n::BreakIterator::BREAK_LINE); + if (words.Init()) { + while (words.Advance()) { + const bool truncate = !current_line_.empty(); + const string16& word = words.GetString(); + const int lines_added = AddWord(word); + if (lines_added) { + if (truncate) { + // Trim trailing whitespace from the line that was added. + const int line = lines_->size() - lines_added; + TrimWhitespace(lines_->at(line), TRIM_TRAILING, &lines_->at(line)); + } + if (ContainsOnlyWhitespace(word)) { + // Skip the first space if the previous line was carried over. + current_width_ = 0; + current_line_.clear(); + } + } + } + } else { + NOTREACHED() << "BreakIterator (words) init failed"; + } + } + // Account for naturally-occuring newlines. + NewLine(); +} + +int RectangleText::WrapWord(const string16& word) { + // Word is so wide that it must be fragmented. + string16 text = word; + int lines_added = 0; + bool first_fragment = true; + while (!insufficient_height_ && !text.empty()) { + string16 fragment = + ElideText(text, font_list_, available_pixel_width_, + TRUNCATE_AT_END); + // At least one character has to be added at every line, even if the + // available space is too small. + if(fragment.empty()) + fragment = text.substr(0, 1); + if (!first_fragment && NewLine()) + lines_added++; + AddToCurrentLine(fragment); + text = text.substr(fragment.length()); + first_fragment = false; + } + return lines_added; +} + +int RectangleText::AddWordOverflow(const string16& word) { + int lines_added = 0; + + // Unless this is the very first word, put it on a new line. + if (!current_line_.empty()) { + if (!NewLine()) + return 0; + lines_added++; + } + + if (wrap_behavior_ == IGNORE_LONG_WORDS) { + current_line_ = word; + current_width_ = available_pixel_width_; + } else if (wrap_behavior_ == WRAP_LONG_WORDS) { + lines_added += WrapWord(word); + } else { + const ElideBehavior elide_behavior = + (wrap_behavior_ == ELIDE_LONG_WORDS ? ELIDE_AT_END : TRUNCATE_AT_END); + const string16 elided_word = + ElideText(word, font_list_, available_pixel_width_, elide_behavior); + AddToCurrentLine(elided_word); + insufficient_width_ = true; + } + + return lines_added; +} + +int RectangleText::AddWord(const string16& word) { + int lines_added = 0; + string16 trimmed; + TrimWhitespace(word, TRIM_TRAILING, &trimmed); + const int trimmed_width = gfx::GetStringWidth(trimmed, font_list_); + if (trimmed_width <= available_pixel_width_) { + // Word can be made to fit, no need to fragment it. + if ((current_width_ + trimmed_width > available_pixel_width_) && NewLine()) + lines_added++; + // Append the non-trimmed word, in case more words are added after. + AddToCurrentLine(word); + } else { + lines_added = AddWordOverflow(wrap_behavior_ == IGNORE_LONG_WORDS ? + trimmed : word); + } + return lines_added; +} + +void RectangleText::AddToCurrentLine(const string16& text) { + AddToCurrentLineWithWidth(text, gfx::GetStringWidth(text, font_list_)); +} + +void RectangleText::AddToCurrentLineWithWidth(const string16& text, + int text_width) { + if (current_height_ >= available_pixel_height_) { + insufficient_height_ = true; + return; + } + current_line_.append(text); + current_width_ += text_width; +} + +bool RectangleText::NewLine() { + bool line_added = false; + if (current_height_ < available_pixel_height_) { + lines_->push_back(current_line_); + current_line_.clear(); + line_added = true; + } else { + insufficient_height_ = true; + } + current_height_ += line_height_; + current_width_ = 0; + return line_added; +} + +} // namespace + +bool ElideRectangleString(const string16& input, size_t max_rows, + size_t max_cols, bool strict, string16* output) { + RectangleString rect(max_rows, max_cols, strict, output); + rect.Init(); + rect.AddString(input); + return rect.Finalize(); +} + +int ElideRectangleText(const string16& input, + const gfx::FontList& font_list, + int available_pixel_width, + int available_pixel_height, + WordWrapBehavior wrap_behavior, + std::vector* lines) { + RectangleText rect(font_list, + available_pixel_width, + available_pixel_height, + wrap_behavior, + lines); + rect.Init(); + rect.AddString(input); + return rect.Finalize(); +} + +int ElideRectangleText(const string16& input, + const gfx::Font& font, + int available_pixel_width, + int available_pixel_height, + WordWrapBehavior wrap_behavior, + std::vector* lines) { + return ElideRectangleText(input, gfx::FontList(font), + available_pixel_width, available_pixel_height, + wrap_behavior, lines); +} + +string16 TruncateString(const string16& string, size_t length) { + if (string.size() <= length) + // String fits, return it. + return string; + + if (length == 0) + // No room for the elide string, return an empty string. + return string16(); + + size_t max = length - 1; + + // Added to the end of strings that are too big. + static const char16 kElideString[] = { 0x2026, 0 }; + + if (max == 0) + // Just enough room for the elide string. + return kElideString; + + // Use a line iterator to find the first boundary. + UErrorCode status = U_ZERO_ERROR; + scoped_ptr bi( + static_cast( + icu::RuleBasedBreakIterator::createLineInstance( + icu::Locale::getDefault(), status))); + if (U_FAILURE(status)) + return string.substr(0, max) + kElideString; + bi->setText(string.c_str()); + int32_t index = bi->preceding(static_cast(max)); + if (index == icu::BreakIterator::DONE) { + index = static_cast(max); + } else { + // Found a valid break (may be the beginning of the string). Now use + // a character iterator to find the previous non-whitespace character. + icu::StringCharacterIterator char_iterator(string.c_str()); + if (index == 0) { + // No valid line breaks. Start at the end again. This ensures we break + // on a valid character boundary. + index = static_cast(max); + } + char_iterator.setIndex(index); + while (char_iterator.hasPrevious()) { + char_iterator.previous(); + if (!(u_isspace(char_iterator.current()) || + u_charType(char_iterator.current()) == U_CONTROL_CHAR || + u_charType(char_iterator.current()) == U_NON_SPACING_MARK)) { + // Not a whitespace character. Advance the iterator so that we + // include the current character in the truncated string. + char_iterator.next(); + break; + } + } + if (char_iterator.hasPrevious()) { + // Found a valid break point. + index = char_iterator.getIndex(); + } else { + // String has leading whitespace, return the elide string. + return kElideString; + } + } + return string.substr(0, index) + kElideString; +} + +} // namespace gfx diff --git a/ui/gfx/text_elider.h b/ui/gfx/text_elider.h new file mode 100644 index 00000000000000..008a1cd1b5b1c6 --- /dev/null +++ b/ui/gfx/text_elider.h @@ -0,0 +1,232 @@ +// Copyright (c) 2012 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. +// +// This file defines utility functions for eliding and formatting UI text. + +#ifndef UI_GFX_TEXT_ELIDER_H_ +#define UI_GFX_TEXT_ELIDER_H_ + +#include +#include + +#include "base/basictypes.h" +#include "base/strings/string16.h" +#include "third_party/icu/source/common/unicode/uchar.h" +#include "third_party/icu/source/i18n/unicode/coll.h" +#include "ui/gfx/gfx_export.h" + +class GURL; + +namespace base { +class FilePath; +} + +namespace gfx { +class Font; +class FontList; + +UI_EXPORT extern const char kEllipsis[]; +UI_EXPORT extern const char16 kEllipsisUTF16[]; + +// Elides a well-formed email address (e.g. username@domain.com) to fit into +// |available_pixel_width| using the specified |font_list|. +// This function guarantees that the string returned will contain at least one +// character, other than the ellipses, on either side of the '@'. If it is +// impossible to achieve these requirements: only an ellipsis will be returned. +// If possible: this elides only the username portion of the |email|. Otherwise, +// the domain is elided in the middle so that it splits the available width +// equally with the elided username (should the username be short enough that it +// doesn't need half the available width: the elided domain will occupy that +// extra width). +UI_EXPORT string16 ElideEmail(const string16& email, + const gfx::FontList& font_list, + int available_pixel_width); +// Obsolete version. Use the above version which takes gfx::FontList. +UI_EXPORT string16 ElideEmail(const string16& email, + const gfx::Font& font, + int available_pixel_width); + +// This function takes a GURL object and elides it. It returns a string +// which composed of parts from subdomain, domain, path, filename and query. +// A "..." is added automatically at the end if the elided string is bigger +// than the |available_pixel_width|. For |available_pixel_width| == 0, a +// formatted, but un-elided, string is returned. |languages| is a comma +// separated list of ISO 639 language codes and is used to determine what +// characters are understood by a user. It should come from +// |prefs::kAcceptLanguages|. +// +// Note: in RTL locales, if the URL returned by this function is going to be +// displayed in the UI, then it is likely that the string needs to be marked +// as an LTR string (using base::i18n::WrapStringWithLTRFormatting()) so that it +// is displayed properly in an RTL context. Please refer to +// http://crbug.com/6487 for more information. +UI_EXPORT string16 ElideUrl(const GURL& url, + const gfx::FontList& font_list, + int available_pixel_width, + const std::string& languages); +// Obsolete version. Use the above version which takes gfx::FontList. +UI_EXPORT string16 ElideUrl(const GURL& url, + const gfx::Font& font, + int available_pixel_width, + const std::string& languages); + +enum ElideBehavior { + // Add ellipsis at the end of the string. + ELIDE_AT_END, + // Add ellipsis in the middle of the string. + ELIDE_IN_MIDDLE, + // Truncate the end of the string. + TRUNCATE_AT_END +}; + +// Elides |text| to fit in |available_pixel_width| according to the specified +// |elide_behavior|. +UI_EXPORT string16 ElideText(const string16& text, + const gfx::FontList& font_list, + int available_pixel_width, + ElideBehavior elide_behavior); +// Obsolete version. Use the above version which takes gfx::FontList. +UI_EXPORT string16 ElideText(const string16& text, + const gfx::Font& font, + int available_pixel_width, + ElideBehavior elide_behavior); + +// 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 +// be removed if filename doesn't fit into available_pixel_width. The elided +// filename is forced to have LTR directionality, which means that in RTL UI +// the elided filename is wrapped with LRE (Left-To-Right Embedding) mark and +// PDF (Pop Directional Formatting) mark. +UI_EXPORT string16 ElideFilename(const base::FilePath& filename, + const gfx::FontList& font_list, + int available_pixel_width); +// Obsolete version. Use the above version which takes gfx::FontList. +UI_EXPORT string16 ElideFilename(const base::FilePath& filename, + const gfx::Font& font, + int available_pixel_width); + +// SortedDisplayURL maintains a string from a URL suitable for display to the +// use. SortedDisplayURL also provides a function used for comparing two +// SortedDisplayURLs for use in visually ordering the SortedDisplayURLs. +// +// SortedDisplayURL is relatively cheap and supports value semantics. +class UI_EXPORT SortedDisplayURL { + public: + SortedDisplayURL(const GURL& url, const std::string& languages); + SortedDisplayURL(); + ~SortedDisplayURL(); + + // Compares this SortedDisplayURL to |url| using |collator|. Returns a value + // < 0, = 1 or > 0 as to whether this url is less then, equal to or greater + // than the supplied url. + int Compare(const SortedDisplayURL& other, icu::Collator* collator) const; + + // Returns the display string for the URL. + const string16& display_url() const { return display_url_; } + + private: + // Returns everything after the host. This is used by Compare if the hosts + // match. + string16 AfterHost() const; + + // Host name minus 'www.'. Used by Compare. + string16 sort_host_; + + // End of the prefix (spec and separator) in display_url_. + size_t prefix_end_; + + string16 display_url_; + + DISALLOW_COPY_AND_ASSIGN(SortedDisplayURL); +}; + +// Functions to elide strings when the font information is unknown. As +// opposed to the above functions, the ElideString() and +// ElideRectangleString() functions operate in terms of character units, +// not pixels. + +// If the size of |input| is more than |max_len|, this function returns +// true and |input| is shortened into |output| by removing chars in the +// middle (they are replaced with up to 3 dots, as size permits). +// Ex: ElideString(ASCIIToUTF16("Hello"), 10, &str) puts Hello in str and +// returns false. ElideString(ASCIIToUTF16("Hello my name is Tom"), 10, &str) +// puts "Hell...Tom" in str and returns true. +// TODO(tsepez): Doesn't handle UTF-16 surrogate pairs properly. +// TODO(tsepez): Doesn't handle bidi properly. +UI_EXPORT bool ElideString(const string16& input, int max_len, + string16* output); + +// Reformat |input| into |output| so that it fits into a |max_rows| by +// |max_cols| rectangle of characters. Input newlines are respected, but +// lines that are too long are broken into pieces. If |strict| is true, +// we break first at naturally occuring whitespace boundaries, otherwise +// we assume some other mechanism will do this in approximately the same +// spot after the fact. If the word itself is too long, we always break +// intra-word (respecting UTF-16 surrogate pairs) as necssary. Truncation +// (indicated by an added 3 dots) occurs if the result is still too long. +// Returns true if the input had to be truncated (and not just reformatted). +UI_EXPORT bool ElideRectangleString(const string16& input, size_t max_rows, + size_t max_cols, bool strict, + string16* output); + +// Specifies the word wrapping behavior of |ElideRectangleText()| when a word +// would exceed the available width. +enum WordWrapBehavior { + // Words that are too wide will be put on a new line, but will not be + // truncated or elided. + IGNORE_LONG_WORDS, + + // Words that are too wide will be put on a new line and will be truncated to + // the available width. + TRUNCATE_LONG_WORDS, + + // Words that are too wide will be put on a new line and will be elided to the + // available width. + ELIDE_LONG_WORDS, + + // Words that are too wide will be put on a new line and will be wrapped over + // multiple lines. + WRAP_LONG_WORDS, +}; + +// Indicates whether the |available_pixel_width| by |available_pixel_height| +// rectangle passed to |ElideRectangleText()| had insufficient space to +// accommodate the given |text|, leading to elision or truncation. +enum ReformattingResultFlags { + INSUFFICIENT_SPACE_HORIZONTAL = 1 << 0, + INSUFFICIENT_SPACE_VERTICAL = 1 << 1, +}; + +// Reformats |text| into output vector |lines| so that the resulting text fits +// into an |available_pixel_width| by |available_pixel_height| rectangle with +// the specified |font_list|. Input newlines are respected, but lines that are +// too long are broken into pieces. For words that are too wide to fit on a +// single line, the wrapping behavior can be specified with the |wrap_behavior| +// param. Returns a combination of |ReformattingResultFlags| that indicate +// whether the given rectangle had insufficient space to accommodate |texŧ|, +// leading to elision or truncation (and not just reformatting). +UI_EXPORT int ElideRectangleText(const string16& text, + const gfx::FontList& font_list, + int available_pixel_width, + int available_pixel_height, + WordWrapBehavior wrap_behavior, + std::vector* lines); +// Obsolete version. Use the above version which takes gfx::FontList. +UI_EXPORT int ElideRectangleText(const string16& text, + const gfx::Font& font, + int available_pixel_width, + int available_pixel_height, + WordWrapBehavior wrap_behavior, + std::vector* lines); + +// Truncates the string to length characters. This breaks the string at +// the first word break before length, adding the horizontal ellipsis +// character (unicode character 0x2026) to render ... +// The supplied string is returned if the string has length characters or +// less. +UI_EXPORT string16 TruncateString(const string16& string, size_t length); + +} // namespace gfx + +#endif // UI_GFX_TEXT_ELIDER_H_ diff --git a/ui/gfx/text_elider_unittest.cc b/ui/gfx/text_elider_unittest.cc new file mode 100644 index 00000000000000..a9e0fb5ed42f58 --- /dev/null +++ b/ui/gfx/text_elider_unittest.cc @@ -0,0 +1,934 @@ +// Copyright (c) 2012 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. +// +// Unit tests for eliding and formatting utility functions. + +#include "ui/gfx/text_elider.h" + +#include "base/files/file_path.h" +#include "base/i18n/rtl.h" +#include "base/memory/scoped_ptr.h" +#include "base/strings/string_util.h" +#include "base/strings/utf_string_conversions.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "ui/gfx/font.h" +#include "url/gurl.h" + +namespace gfx { + +namespace { + +struct Testcase { + const std::string input; + const std::string output; +}; + +struct FileTestcase { + const base::FilePath::StringType input; + const std::string output; +}; + +struct UTF16Testcase { + const string16 input; + const string16 output; +}; + +struct TestData { + const std::string a; + const std::string b; + const int compare_result; +}; + +void RunUrlTest(Testcase* testcases, size_t num_testcases) { + static const gfx::Font font; + for (size_t i = 0; i < num_testcases; ++i) { + const GURL url(testcases[i].input); + // Should we test with non-empty language list? + // That's kinda redundant with net_util_unittests. + EXPECT_EQ(UTF8ToUTF16(testcases[i].output), + ElideUrl(url, font, + font.GetStringWidth(UTF8ToUTF16(testcases[i].output)), + std::string())); + } +} + +} // namespace + +// TODO(ios): Complex eliding is off by one for some of those tests on iOS. +// See crbug.com/154019 +#if defined(OS_IOS) +#define MAYBE_ElideEmail DISABLED_ElideEmail +#else +#define MAYBE_ElideEmail ElideEmail +#endif +TEST(TextEliderTest, MAYBE_ElideEmail) { + const std::string kEllipsisStr(kEllipsis); + + // Test emails and their expected elided forms (from which the available + // widths will be derived). + // For elided forms in which both the username and domain must be elided: + // the result (how many characters are left on each side) can be font + // dependent. To avoid this, the username is prefixed with the characters + // expected to remain in the domain. + Testcase testcases[] = { + {"g@g.c", "g@g.c"}, + {"g@g.c", kEllipsisStr}, + {"ga@co.ca", "ga@c" + kEllipsisStr + "a"}, + {"short@small.com", "s" + kEllipsisStr + "@s" + kEllipsisStr}, + {"short@small.com", "s" + kEllipsisStr + "@small.com"}, + {"short@longbutlotsofspace.com", "short@longbutlotsofspace.com"}, + {"short@longbutnotverymuchspace.com", + "short@long" + kEllipsisStr + ".com"}, + {"la_short@longbutverytightspace.ca", + "la" + kEllipsisStr + "@l" + kEllipsisStr + "a"}, + {"longusername@gmail.com", "long" + kEllipsisStr + "@gmail.com"}, + {"elidetothemax@justfits.com", "e" + kEllipsisStr + "@justfits.com"}, + {"thatom_somelongemail@thatdoesntfit.com", + "thatom" + kEllipsisStr + "@tha" + kEllipsisStr + "om"}, + {"namefits@butthedomaindoesnt.com", + "namefits@butthedo" + kEllipsisStr + "snt.com"}, + {"widthtootight@nospace.com", kEllipsisStr}, + {"nospaceforusername@l", kEllipsisStr}, + {"little@littlespace.com", "l" + kEllipsisStr + "@l" + kEllipsisStr}, + {"l@llllllllllllllllllllllll.com", "l@lllll" + kEllipsisStr + ".com"}, + {"messed\"up@whyanat\"++@notgoogley.com", + "messed\"up@whyanat\"++@notgoogley.com"}, + {"messed\"up@whyanat\"++@notgoogley.com", + "messed\"up@why" + kEllipsisStr + "@notgoogley.com"}, + {"noca_messed\"up@whyanat\"++@notgoogley.ca", + "noca" + kEllipsisStr + "@no" + kEllipsisStr + "ca"}, + {"at\"@@@@@@@@@...@@.@.@.@@@\"@madness.com", + "at\"@@@@@@@@@...@@.@." + kEllipsisStr + "@madness.com"}, + // Special case: "m..." takes more than half of the available width; thus + // the domain must elide to "l..." and not "l...l" as it must allow enough + // space for the minimal username elision although its half of the + // available width would normally allow it to elide to "l...l". + {"mmmmm@llllllllll", "m" + kEllipsisStr + "@l" + kEllipsisStr}, + }; + + const gfx::Font font; + for (size_t i = 0; i < arraysize(testcases); ++i) { + const string16 expected_output = UTF8ToUTF16(testcases[i].output); + EXPECT_EQ(expected_output, + ElideEmail(UTF8ToUTF16(testcases[i].input), + font, + font.GetStringWidth(expected_output))); + } +} + +TEST(TextEliderTest, ElideEmailMoreSpace) { + const int test_width_factors[] = { + 100, + 10000, + 1000000, + }; + const std::string test_emails[] = { + "a@c", + "test@email.com", + "short@verysuperdupperlongdomain.com", + "supermegalongusername@withasuperlonnnggggdomain.gouv.qc.ca", + }; + + const gfx::Font font; + for (size_t i = 0; i < arraysize(test_width_factors); ++i) { + const int test_width = test_width_factors[i] * + font.GetAverageCharacterWidth(); + for (size_t j = 0; j < arraysize(test_emails); ++j) { + // Extra space is available: the email should not be elided. + const string16 test_email = UTF8ToUTF16(test_emails[j]); + EXPECT_EQ(test_email, ElideEmail(test_email, font, test_width)); + } + } +} + +// Test eliding of commonplace URLs. +TEST(TextEliderTest, TestGeneralEliding) { + const std::string kEllipsisStr(kEllipsis); + Testcase testcases[] = { + {"http://www.google.com/intl/en/ads/", + "www.google.com/intl/en/ads/"}, + {"http://www.google.com/intl/en/ads/", "www.google.com/intl/en/ads/"}, + {"http://www.google.com/intl/en/ads/", + "google.com/intl/" + kEllipsisStr + "/ads/"}, + {"http://www.google.com/intl/en/ads/", + "google.com/" + kEllipsisStr + "/ads/"}, + {"http://www.google.com/intl/en/ads/", "google.com/" + kEllipsisStr}, + {"http://www.google.com/intl/en/ads/", "goog" + kEllipsisStr}, + {"https://subdomain.foo.com/bar/filename.html", + "subdomain.foo.com/bar/filename.html"}, + {"https://subdomain.foo.com/bar/filename.html", + "subdomain.foo.com/" + kEllipsisStr + "/filename.html"}, + {"http://subdomain.foo.com/bar/filename.html", + kEllipsisStr + "foo.com/" + kEllipsisStr + "/filename.html"}, + {"http://www.google.com/intl/en/ads/?aLongQueryWhichIsNotRequired", + "www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr}, + }; + + RunUrlTest(testcases, arraysize(testcases)); +} + +// When there is very little space available, the elision code will shorten +// both path AND file name to an ellipsis - ".../...". To avoid this result, +// there is a hack in place that simply treats them as one string in this +// case. +TEST(TextEliderTest, TestTrailingEllipsisSlashEllipsisHack) { + const std::string kEllipsisStr(kEllipsis); + + // Very little space, would cause double ellipsis. + gfx::Font font; + GURL url("http://battersbox.com/directory/foo/peter_paul_and_mary.html"); + int available_width = font.GetStringWidth( + UTF8ToUTF16("battersbox.com/" + kEllipsisStr + "/" + kEllipsisStr)); + + // Create the expected string, after elision. Depending on font size, the + // directory might become /dir... or /di... or/d... - it never should be + // shorter than that. (If it is, the font considers d... to be longer + // than .../... - that should never happen). + ASSERT_GT(font.GetStringWidth(UTF8ToUTF16(kEllipsisStr + "/" + kEllipsisStr)), + font.GetStringWidth(UTF8ToUTF16("d" + kEllipsisStr))); + GURL long_url("http://battersbox.com/directorynameisreallylongtoforcetrunc"); + string16 expected = ElideUrl(long_url, font, available_width, std::string()); + // Ensure that the expected result still contains part of the directory name. + ASSERT_GT(expected.length(), std::string("battersbox.com/d").length()); + EXPECT_EQ(expected, + ElideUrl(url, font, available_width, std::string())); + + // More space available - elide directories, partially elide filename. + Testcase testcases[] = { + {"http://battersbox.com/directory/foo/peter_paul_and_mary.html", + "battersbox.com/" + kEllipsisStr + "/peter" + kEllipsisStr}, + }; + RunUrlTest(testcases, arraysize(testcases)); +} + +// Test eliding of empty strings, URLs with ports, passwords, queries, etc. +TEST(TextEliderTest, TestMoreEliding) { + const std::string kEllipsisStr(kEllipsis); + Testcase testcases[] = { + {"http://www.google.com/foo?bar", "www.google.com/foo?bar"}, + {"http://xyz.google.com/foo?bar", "xyz.google.com/foo?" + kEllipsisStr}, + {"http://xyz.google.com/foo?bar", "xyz.google.com/foo" + kEllipsisStr}, + {"http://xyz.google.com/foo?bar", "xyz.google.com/fo" + kEllipsisStr}, + {"http://a.b.com/pathname/c?d", "a.b.com/" + kEllipsisStr + "/c?d"}, + {"", ""}, + {"http://foo.bar..example.com...hello/test/filename.html", + "foo.bar..example.com...hello/" + kEllipsisStr + "/filename.html"}, + {"http://foo.bar../", "foo.bar.."}, + {"http://xn--1lq90i.cn/foo", "\xe5\x8c\x97\xe4\xba\xac.cn/foo"}, + {"http://me:mypass@secrethost.com:99/foo?bar#baz", + "secrethost.com:99/foo?bar#baz"}, + {"http://me:mypass@ss%xxfdsf.com/foo", "ss%25xxfdsf.com/foo"}, + {"mailto:elgoato@elgoato.com", "mailto:elgoato@elgoato.com"}, + {"javascript:click(0)", "javascript:click(0)"}, + {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename", + "chess.eecs.berkeley.edu:4430/login/arbitfilename"}, + {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename", + kEllipsisStr + "berkeley.edu:4430/" + kEllipsisStr + "/arbitfilename"}, + + // Unescaping. + {"http://www/%E4%BD%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0", + "www/\xe4\xbd\xa0\xe5\xa5\xbd?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"}, + + // Invalid unescaping for path. The ref will always be valid UTF-8. We don't + // bother to do too many edge cases, since these are handled by the escaper + // unittest. + {"http://www/%E4%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0", + "www/%E4%A0%E5%A5%BD?q=\xe4\xbd\xa0\xe5\xa5\xbd#\xe4\xbd\xa0"}, + }; + + RunUrlTest(testcases, arraysize(testcases)); +} + +// Test eliding of file: URLs. +TEST(TextEliderTest, TestFileURLEliding) { + const std::string kEllipsisStr(kEllipsis); + Testcase testcases[] = { + {"file:///C:/path1/path2/path3/filename", + "file:///C:/path1/path2/path3/filename"}, + {"file:///C:/path1/path2/path3/filename", + "C:/path1/path2/path3/filename"}, +// GURL parses "file:///C:path" differently on windows than it does on posix. +#if defined(OS_WIN) + {"file:///C:path1/path2/path3/filename", + "C:/path1/path2/" + kEllipsisStr + "/filename"}, + {"file:///C:path1/path2/path3/filename", + "C:/path1/" + kEllipsisStr + "/filename"}, + {"file:///C:path1/path2/path3/filename", + "C:/" + kEllipsisStr + "/filename"}, +#endif + {"file://filer/foo/bar/file", "filer/foo/bar/file"}, + {"file://filer/foo/bar/file", "filer/foo/" + kEllipsisStr + "/file"}, + {"file://filer/foo/bar/file", "filer/" + kEllipsisStr + "/file"}, + }; + + RunUrlTest(testcases, arraysize(testcases)); +} + +// TODO(ios): Complex eliding is off by one for some of those tests on iOS. +// See crbug.com/154019 +#if defined(OS_IOS) +#define MAYBE_TestFilenameEliding DISABLED_TestFilenameEliding +#else +#define MAYBE_TestFilenameEliding TestFilenameEliding +#endif +TEST(TextEliderTest, MAYBE_TestFilenameEliding) { + const std::string kEllipsisStr(kEllipsis); + const base::FilePath::StringType kPathSeparator = + base::FilePath::StringType().append(1, base::FilePath::kSeparators[0]); + + FileTestcase testcases[] = { + {FILE_PATH_LITERAL(""), ""}, + {FILE_PATH_LITERAL("."), "."}, + {FILE_PATH_LITERAL("filename.exe"), "filename.exe"}, + {FILE_PATH_LITERAL(".longext"), ".longext"}, + {FILE_PATH_LITERAL("pie"), "pie"}, + {FILE_PATH_LITERAL("c:") + kPathSeparator + FILE_PATH_LITERAL("path") + + kPathSeparator + FILE_PATH_LITERAL("filename.pie"), + "filename.pie"}, + {FILE_PATH_LITERAL("c:") + kPathSeparator + FILE_PATH_LITERAL("path") + + kPathSeparator + FILE_PATH_LITERAL("longfilename.pie"), + "long" + kEllipsisStr + ".pie"}, + {FILE_PATH_LITERAL("http://path.com/filename.pie"), "filename.pie"}, + {FILE_PATH_LITERAL("http://path.com/longfilename.pie"), + "long" + kEllipsisStr + ".pie"}, + {FILE_PATH_LITERAL("piesmashingtacularpants"), "pie" + kEllipsisStr}, + {FILE_PATH_LITERAL(".piesmashingtacularpants"), ".pie" + kEllipsisStr}, + {FILE_PATH_LITERAL("cheese."), "cheese."}, + {FILE_PATH_LITERAL("file name.longext"), + "file" + kEllipsisStr + ".longext"}, + {FILE_PATH_LITERAL("fil ename.longext"), + "fil " + kEllipsisStr + ".longext"}, + {FILE_PATH_LITERAL("filename.longext"), + "file" + kEllipsisStr + ".longext"}, + {FILE_PATH_LITERAL("filename.middleext.longext"), + "filename.mid" + kEllipsisStr + ".longext"}, + {FILE_PATH_LITERAL("filename.superduperextremelylongext"), + "filename.sup" + kEllipsisStr + "emelylongext"}, + {FILE_PATH_LITERAL("filenamereallylongtext.superduperextremelylongext"), + "filenamereall" + kEllipsisStr + "emelylongext"}, + {FILE_PATH_LITERAL("file.name.really.long.text.superduperextremelylongext"), + "file.name.re" + kEllipsisStr + "emelylongext"} + }; + + static const gfx::Font font; + for (size_t i = 0; i < arraysize(testcases); ++i) { + base::FilePath filepath(testcases[i].input); + string16 expected = UTF8ToUTF16(testcases[i].output); + expected = base::i18n::GetDisplayStringInLTRDirectionality(expected); + EXPECT_EQ(expected, ElideFilename(filepath, + font, + font.GetStringWidth(UTF8ToUTF16(testcases[i].output)))); + } +} + +TEST(TextEliderTest, ElideTextTruncate) { + const gfx::Font font; + const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test")); + struct TestData { + const char* input; + int width; + const char* output; + } cases[] = { + { "", 0, "" }, + { "Test", 0, "" }, + { "", kTestWidth, "" }, + { "Tes", kTestWidth, "Tes" }, + { "Test", kTestWidth, "Test" }, + { "Tests", kTestWidth, "Test" }, + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { + string16 result = ElideText(UTF8ToUTF16(cases[i].input), font, + cases[i].width, TRUNCATE_AT_END); + EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); + } +} + +TEST(TextEliderTest, ElideTextEllipsis) { + const gfx::Font font; + const int kTestWidth = font.GetStringWidth(ASCIIToUTF16("Test")); + const char* kEllipsis = "\xE2\x80\xA6"; + const int kEllipsisWidth = font.GetStringWidth(UTF8ToUTF16(kEllipsis)); + struct TestData { + const char* input; + int width; + const char* output; + } cases[] = { + { "", 0, "" }, + { "Test", 0, "" }, + { "Test", kEllipsisWidth, kEllipsis }, + { "", kTestWidth, "" }, + { "Tes", kTestWidth, "Tes" }, + { "Test", kTestWidth, "Test" }, + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { + string16 result = ElideText(UTF8ToUTF16(cases[i].input), font, + cases[i].width, ELIDE_AT_END); + EXPECT_EQ(cases[i].output, UTF16ToUTF8(result)); + } +} + +// Checks that all occurrences of |first_char| are followed by |second_char| and +// all occurrences of |second_char| are preceded by |first_char| in |text|. +static void CheckSurrogatePairs(const string16& text, + char16 first_char, + char16 second_char) { + size_t index = text.find_first_of(first_char); + while (index != string16::npos) { + EXPECT_LT(index, text.length() - 1); + EXPECT_EQ(second_char, text[index + 1]); + index = text.find_first_of(first_char, index + 1); + } + index = text.find_first_of(second_char); + while (index != string16::npos) { + EXPECT_GT(index, 0U); + EXPECT_EQ(first_char, text[index - 1]); + index = text.find_first_of(second_char, index + 1); + } +} + +TEST(TextEliderTest, ElideTextSurrogatePairs) { + const gfx::Font font; + // The below is 'MUSICAL SYMBOL G CLEF', which is represented in UTF-16 as + // two characters forming a surrogate pair 0x0001D11E. + const std::string kSurrogate = "\xF0\x9D\x84\x9E"; + const string16 kTestString = + UTF8ToUTF16(kSurrogate + "ab" + kSurrogate + kSurrogate + "cd"); + const int kTestStringWidth = font.GetStringWidth(kTestString); + const char16 kSurrogateFirstChar = kTestString[0]; + const char16 kSurrogateSecondChar = kTestString[1]; + string16 result; + + // Elide |kTextString| to all possible widths and check that no instance of + // |kSurrogate| was split in two. + for (int width = 0; width <= kTestStringWidth; width++) { + result = ElideText(kTestString, font, width, TRUNCATE_AT_END); + CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); + + result = ElideText(kTestString, font, width, ELIDE_AT_END); + CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); + + result = ElideText(kTestString, font, width, ELIDE_IN_MIDDLE); + CheckSurrogatePairs(result, kSurrogateFirstChar, kSurrogateSecondChar); + } +} + +TEST(TextEliderTest, ElideTextLongStrings) { + const string16 kEllipsisStr = UTF8ToUTF16(kEllipsis); + string16 data_scheme(UTF8ToUTF16("data:text/plain,")); + size_t data_scheme_length = data_scheme.length(); + + 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; + 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}, + {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_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), + ELIDE_AT_END).size()); + EXPECT_EQ(kEllipsisStr, + ElideText(testcases_end[i].input, font, ellipsis_width, + ELIDE_AT_END)); + } + + size_t number_of_trailing_as = (data_scheme_length + number_of_as) / 2; + 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}, + {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_middle[i].output.size(), + ElideText(testcases_middle[i].input, font, + font.GetStringWidth(testcases_middle[i].output), + ELIDE_AT_END).size()); + EXPECT_EQ(kEllipsisStr, + ElideText(testcases_middle[i].input, font, ellipsis_width, + ELIDE_AT_END)); + } +} + +// Verifies display_url is set correctly. +TEST(TextEliderTest, SortedDisplayURL) { + SortedDisplayURL d_url(GURL("http://www.google.com"), std::string()); + EXPECT_EQ("www.google.com", UTF16ToASCII(d_url.display_url())); +} + +// Verifies DisplayURL::Compare works correctly. +TEST(TextEliderTest, SortedDisplayURLCompare) { + UErrorCode create_status = U_ZERO_ERROR; + scoped_ptr collator( + icu::Collator::createInstance(create_status)); + if (!U_SUCCESS(create_status)) + return; + + TestData tests[] = { + // IDN comparison. Hosts equal, so compares on path. + { "http://xn--1lq90i.cn/a", "http://xn--1lq90i.cn/b", -1}, + + // Because the host and after host match, this compares the full url. + { "http://www.x/b", "http://x/b", -1 }, + + // Because the host and after host match, this compares the full url. + { "http://www.a:1/b", "http://a:1/b", 1 }, + + // The hosts match, so these end up comparing on the after host portion. + { "http://www.x:0/b", "http://x:1/b", -1 }, + { "http://www.x/a", "http://x/b", -1 }, + { "http://x/b", "http://www.x/a", 1 }, + + // Trivial Equality. + { "http://a/", "http://a/", 0 }, + + // Compares just hosts. + { "http://www.a/", "http://b/", -1 }, + }; + + for (size_t i = 0; i < arraysize(tests); ++i) { + SortedDisplayURL url1(GURL(tests[i].a), std::string()); + SortedDisplayURL url2(GURL(tests[i].b), std::string()); + EXPECT_EQ(tests[i].compare_result, url1.Compare(url2, collator.get())); + EXPECT_EQ(-tests[i].compare_result, url2.Compare(url1, collator.get())); + } +} + +TEST(TextEliderTest, ElideString) { + struct TestData { + const char* input; + int max_len; + bool result; + const char* output; + } cases[] = { + { "Hello", 0, true, "" }, + { "", 0, false, "" }, + { "Hello, my name is Tom", 1, true, "H" }, + { "Hello, my name is Tom", 2, true, "He" }, + { "Hello, my name is Tom", 3, true, "H.m" }, + { "Hello, my name is Tom", 4, true, "H..m" }, + { "Hello, my name is Tom", 5, true, "H...m" }, + { "Hello, my name is Tom", 6, true, "He...m" }, + { "Hello, my name is Tom", 7, true, "He...om" }, + { "Hello, my name is Tom", 10, true, "Hell...Tom" }, + { "Hello, my name is Tom", 100, false, "Hello, my name is Tom" } + }; + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { + string16 output; + EXPECT_EQ(cases[i].result, + ElideString(UTF8ToUTF16(cases[i].input), + cases[i].max_len, &output)); + EXPECT_EQ(cases[i].output, UTF16ToUTF8(output)); + } +} + +TEST(TextEliderTest, ElideRectangleText) { + const gfx::Font font; + const int line_height = font.GetHeight(); + const int test_width = font.GetStringWidth(ASCIIToUTF16("Test")); + + struct TestData { + const char* input; + int available_pixel_width; + int available_pixel_height; + bool truncated_y; + const char* output; + } cases[] = { + { "", 0, 0, false, NULL }, + { "", 1, 1, false, NULL }, + { "Test", test_width, 0, true, NULL }, + { "Test", test_width, 1, false, "Test" }, + { "Test", test_width, line_height, false, "Test" }, + { "Test Test", test_width, line_height, true, "Test" }, + { "Test Test", test_width, line_height + 1, false, "Test|Test" }, + { "Test Test", test_width, line_height * 2, false, "Test|Test" }, + { "Test Test", test_width, line_height * 3, false, "Test|Test" }, + { "Test Test", test_width * 2, line_height * 2, false, "Test|Test" }, + { "Test Test", test_width * 3, line_height, false, "Test Test" }, + { "Test\nTest", test_width * 3, line_height * 2, false, "Test|Test" }, + { "Te\nst Te", test_width, line_height * 3, false, "Te|st|Te" }, + { "\nTest", test_width, line_height * 2, false, "|Test" }, + { "\nTest", test_width, line_height, true, "" }, + { "\n\nTest", test_width, line_height * 3, false, "||Test" }, + { "\n\nTest", test_width, line_height * 2, true, "|" }, + { "Test\n", 2 * test_width, line_height * 5, false, "Test|" }, + { "Test\n\n", 2 * test_width, line_height * 5, false, "Test||" }, + { "Test\n\n\n", 2 * test_width, line_height * 5, false, "Test|||" }, + { "Test\nTest\n\n", 2 * test_width, line_height * 5, false, "Test|Test||" }, + { "Test\n\nTest\n", 2 * test_width, line_height * 5, false, "Test||Test|" }, + { "Test\n\n\nTest", 2 * test_width, line_height * 5, false, "Test|||Test" }, + { "Te ", test_width, line_height, false, "Te" }, + { "Te Te Test", test_width, 3 * line_height, false, "Te|Te|Test" }, + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { + std::vector lines; + EXPECT_EQ(cases[i].truncated_y ? INSUFFICIENT_SPACE_VERTICAL : 0, + ElideRectangleText(UTF8ToUTF16(cases[i].input), + font, + cases[i].available_pixel_width, + cases[i].available_pixel_height, + TRUNCATE_LONG_WORDS, + &lines)); + if (cases[i].output) { + const std::string result = UTF16ToUTF8(JoinString(lines, '|')); + EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!"; + } else { + EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!"; + } + } +} + +TEST(TextEliderTest, ElideRectangleTextPunctuation) { + const gfx::Font font; + const int line_height = font.GetHeight(); + const int test_width = font.GetStringWidth(ASCIIToUTF16("Test")); + const int test_t_width = font.GetStringWidth(ASCIIToUTF16("Test T")); + + struct TestData { + const char* input; + int available_pixel_width; + int available_pixel_height; + bool wrap_words; + bool truncated_x; + const char* output; + } cases[] = { + { "Test T.", test_t_width, line_height * 2, false, false, "Test|T." }, + { "Test T ?", test_t_width, line_height * 2, false, false, "Test|T ?" }, + { "Test. Test", test_width, line_height * 3, false, true, "Test|Test" }, + { "Test. Test", test_width, line_height * 3, true, false, "Test|.|Test" }, + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { + std::vector lines; + const WordWrapBehavior wrap_behavior = + (cases[i].wrap_words ? WRAP_LONG_WORDS : TRUNCATE_LONG_WORDS); + EXPECT_EQ(cases[i].truncated_x ? INSUFFICIENT_SPACE_HORIZONTAL : 0, + ElideRectangleText(UTF8ToUTF16(cases[i].input), + font, + cases[i].available_pixel_width, + cases[i].available_pixel_height, + wrap_behavior, + &lines)); + if (cases[i].output) { + const std::string result = UTF16ToUTF8(JoinString(lines, '|')); + EXPECT_EQ(cases[i].output, result) << "Case " << i << " failed!"; + } else { + EXPECT_TRUE(lines.empty()) << "Case " << i << " failed!"; + } + } +} + +TEST(TextEliderTest, ElideRectangleTextLongWords) { + const gfx::Font font; + const int kAvailableHeight = 1000; + const string16 kElidedTesting = UTF8ToUTF16(std::string("Tes") + kEllipsis); + const int elided_width = font.GetStringWidth(kElidedTesting); + const int test_width = font.GetStringWidth(ASCIIToUTF16("Test")); + + struct TestData { + const char* input; + int available_pixel_width; + WordWrapBehavior wrap_behavior; + bool truncated_x; + const char* output; + } cases[] = { + { "Testing", test_width, IGNORE_LONG_WORDS, false, "Testing" }, + { "X Testing", test_width, IGNORE_LONG_WORDS, false, "X|Testing" }, + { "Test Testing", test_width, IGNORE_LONG_WORDS, false, "Test|Testing" }, + { "Test\nTesting", test_width, IGNORE_LONG_WORDS, false, "Test|Testing" }, + { "Test Tests ", test_width, IGNORE_LONG_WORDS, false, "Test|Tests" }, + { "Test Tests T", test_width, IGNORE_LONG_WORDS, false, "Test|Tests|T" }, + + { "Testing", elided_width, ELIDE_LONG_WORDS, true, "Tes..." }, + { "X Testing", elided_width, ELIDE_LONG_WORDS, true, "X|Tes..." }, + { "Test Testing", elided_width, ELIDE_LONG_WORDS, true, "Test|Tes..." }, + { "Test\nTesting", elided_width, ELIDE_LONG_WORDS, true, "Test|Tes..." }, + + { "Testing", test_width, TRUNCATE_LONG_WORDS, true, "Test" }, + { "X Testing", test_width, TRUNCATE_LONG_WORDS, true, "X|Test" }, + { "Test Testing", test_width, TRUNCATE_LONG_WORDS, true, "Test|Test" }, + { "Test\nTesting", test_width, TRUNCATE_LONG_WORDS, true, "Test|Test" }, + { "Test Tests ", test_width, TRUNCATE_LONG_WORDS, true, "Test|Test" }, + { "Test Tests T", test_width, TRUNCATE_LONG_WORDS, true, "Test|Test|T" }, + + { "Testing", test_width, WRAP_LONG_WORDS, false, "Test|ing" }, + { "X Testing", test_width, WRAP_LONG_WORDS, false, "X|Test|ing" }, + { "Test Testing", test_width, WRAP_LONG_WORDS, false, "Test|Test|ing" }, + { "Test\nTesting", test_width, WRAP_LONG_WORDS, false, "Test|Test|ing" }, + { "Test Tests ", test_width, WRAP_LONG_WORDS, false, "Test|Test|s" }, + { "Test Tests T", test_width, WRAP_LONG_WORDS, false, "Test|Test|s T" }, + { "TestTestTest", test_width, WRAP_LONG_WORDS, false, "Test|Test|Test" }, + { "TestTestTestT", test_width, WRAP_LONG_WORDS, false, "Test|Test|Test|T" }, + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { + std::vector lines; + EXPECT_EQ(cases[i].truncated_x ? INSUFFICIENT_SPACE_HORIZONTAL : 0, + ElideRectangleText(UTF8ToUTF16(cases[i].input), + font, + cases[i].available_pixel_width, + kAvailableHeight, + cases[i].wrap_behavior, + &lines)); + std::string expected_output(cases[i].output); + ReplaceSubstringsAfterOffset(&expected_output, 0, "...", kEllipsis); + const std::string result = UTF16ToUTF8(JoinString(lines, '|')); + EXPECT_EQ(expected_output, result) << "Case " << i << " failed!"; + } +} + +TEST(TextEliderTest, ElideRectangleString) { + struct TestData { + const char* input; + int max_rows; + int max_cols; + bool result; + const char* output; + } cases[] = { + { "", 0, 0, false, "" }, + { "", 1, 1, false, "" }, + { "Hi, my name is\nTom", 0, 0, true, "..." }, + { "Hi, my name is\nTom", 1, 0, true, "\n..." }, + { "Hi, my name is\nTom", 0, 1, true, "..." }, + { "Hi, my name is\nTom", 1, 1, true, "H\n..." }, + { "Hi, my name is\nTom", 2, 1, true, "H\ni\n..." }, + { "Hi, my name is\nTom", 3, 1, true, "H\ni\n,\n..." }, + { "Hi, my name is\nTom", 4, 1, true, "H\ni\n,\n \n..." }, + { "Hi, my name is\nTom", 5, 1, true, "H\ni\n,\n \nm\n..." }, + { "Hi, my name is\nTom", 0, 2, true, "..." }, + { "Hi, my name is\nTom", 1, 2, true, "Hi\n..." }, + { "Hi, my name is\nTom", 2, 2, true, "Hi\n, \n..." }, + { "Hi, my name is\nTom", 3, 2, true, "Hi\n, \nmy\n..." }, + { "Hi, my name is\nTom", 4, 2, true, "Hi\n, \nmy\n n\n..." }, + { "Hi, my name is\nTom", 5, 2, true, "Hi\n, \nmy\n n\nam\n..." }, + { "Hi, my name is\nTom", 0, 3, true, "..." }, + { "Hi, my name is\nTom", 1, 3, true, "Hi,\n..." }, + { "Hi, my name is\nTom", 2, 3, true, "Hi,\n my\n..." }, + { "Hi, my name is\nTom", 3, 3, true, "Hi,\n my\n na\n..." }, + { "Hi, my name is\nTom", 4, 3, true, "Hi,\n my\n na\nme \n..." }, + { "Hi, my name is\nTom", 5, 3, true, "Hi,\n my\n na\nme \nis\n..." }, + { "Hi, my name is\nTom", 1, 4, true, "Hi, \n..." }, + { "Hi, my name is\nTom", 2, 4, true, "Hi, \nmy n\n..." }, + { "Hi, my name is\nTom", 3, 4, true, "Hi, \nmy n\name \n..." }, + { "Hi, my name is\nTom", 4, 4, true, "Hi, \nmy n\name \nis\n..." }, + { "Hi, my name is\nTom", 5, 4, false, "Hi, \nmy n\name \nis\nTom" }, + { "Hi, my name is\nTom", 1, 5, true, "Hi, \n..." }, + { "Hi, my name is\nTom", 2, 5, true, "Hi, \nmy na\n..." }, + { "Hi, my name is\nTom", 3, 5, true, "Hi, \nmy na\nme \n..." }, + { "Hi, my name is\nTom", 4, 5, true, "Hi, \nmy na\nme \nis\n..." }, + { "Hi, my name is\nTom", 5, 5, false, "Hi, \nmy na\nme \nis\nTom" }, + { "Hi, my name is\nTom", 1, 6, true, "Hi, \n..." }, + { "Hi, my name is\nTom", 2, 6, true, "Hi, \nmy \n..." }, + { "Hi, my name is\nTom", 3, 6, true, "Hi, \nmy \nname \n..." }, + { "Hi, my name is\nTom", 4, 6, true, "Hi, \nmy \nname \nis\n..." }, + { "Hi, my name is\nTom", 5, 6, false, "Hi, \nmy \nname \nis\nTom" }, + { "Hi, my name is\nTom", 1, 7, true, "Hi, \n..." }, + { "Hi, my name is\nTom", 2, 7, true, "Hi, \nmy \n..." }, + { "Hi, my name is\nTom", 3, 7, true, "Hi, \nmy \nname \n..." }, + { "Hi, my name is\nTom", 4, 7, true, "Hi, \nmy \nname \nis\n..." }, + { "Hi, my name is\nTom", 5, 7, false, "Hi, \nmy \nname \nis\nTom" }, + { "Hi, my name is\nTom", 1, 8, true, "Hi, my \n..." }, + { "Hi, my name is\nTom", 2, 8, true, "Hi, my \nname \n..." }, + { "Hi, my name is\nTom", 3, 8, true, "Hi, my \nname \nis\n..." }, + { "Hi, my name is\nTom", 4, 8, false, "Hi, my \nname \nis\nTom" }, + { "Hi, my name is\nTom", 1, 9, true, "Hi, my \n..." }, + { "Hi, my name is\nTom", 2, 9, true, "Hi, my \nname is\n..." }, + { "Hi, my name is\nTom", 3, 9, false, "Hi, my \nname is\nTom" }, + { "Hi, my name is\nTom", 1, 10, true, "Hi, my \n..." }, + { "Hi, my name is\nTom", 2, 10, true, "Hi, my \nname is\n..." }, + { "Hi, my name is\nTom", 3, 10, false, "Hi, my \nname is\nTom" }, + { "Hi, my name is\nTom", 1, 11, true, "Hi, my \n..." }, + { "Hi, my name is\nTom", 2, 11, true, "Hi, my \nname is\n..." }, + { "Hi, my name is\nTom", 3, 11, false, "Hi, my \nname is\nTom" }, + { "Hi, my name is\nTom", 1, 12, true, "Hi, my \n..." }, + { "Hi, my name is\nTom", 2, 12, true, "Hi, my \nname is\n..." }, + { "Hi, my name is\nTom", 3, 12, false, "Hi, my \nname is\nTom" }, + { "Hi, my name is\nTom", 1, 13, true, "Hi, my name \n..." }, + { "Hi, my name is\nTom", 2, 13, true, "Hi, my name \nis\n..." }, + { "Hi, my name is\nTom", 3, 13, false, "Hi, my name \nis\nTom" }, + { "Hi, my name is\nTom", 1, 20, true, "Hi, my name is\n..." }, + { "Hi, my name is\nTom", 2, 20, false, "Hi, my name is\nTom" }, + { "Hi, my name is Tom", 1, 40, false, "Hi, my name is Tom" }, + }; + string16 output; + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { + EXPECT_EQ(cases[i].result, + ElideRectangleString(UTF8ToUTF16(cases[i].input), + cases[i].max_rows, cases[i].max_cols, + true, &output)); + EXPECT_EQ(cases[i].output, UTF16ToUTF8(output)); + } +} + +TEST(TextEliderTest, ElideRectangleStringNotStrict) { + struct TestData { + const char* input; + int max_rows; + int max_cols; + bool result; + const char* output; + } cases[] = { + { "", 0, 0, false, "" }, + { "", 1, 1, false, "" }, + { "Hi, my name_is\nDick", 0, 0, true, "..." }, + { "Hi, my name_is\nDick", 1, 0, true, "\n..." }, + { "Hi, my name_is\nDick", 0, 1, true, "..." }, + { "Hi, my name_is\nDick", 1, 1, true, "H\n..." }, + { "Hi, my name_is\nDick", 2, 1, true, "H\ni\n..." }, + { "Hi, my name_is\nDick", 3, 1, true, "H\ni\n,\n..." }, + { "Hi, my name_is\nDick", 4, 1, true, "H\ni\n,\n \n..." }, + { "Hi, my name_is\nDick", 5, 1, true, "H\ni\n,\n \nm\n..." }, + { "Hi, my name_is\nDick", 0, 2, true, "..." }, + { "Hi, my name_is\nDick", 1, 2, true, "Hi\n..." }, + { "Hi, my name_is\nDick", 2, 2, true, "Hi\n, \n..." }, + { "Hi, my name_is\nDick", 3, 2, true, "Hi\n, \nmy\n..." }, + { "Hi, my name_is\nDick", 4, 2, true, "Hi\n, \nmy\n n\n..." }, + { "Hi, my name_is\nDick", 5, 2, true, "Hi\n, \nmy\n n\nam\n..." }, + { "Hi, my name_is\nDick", 0, 3, true, "..." }, + { "Hi, my name_is\nDick", 1, 3, true, "Hi,\n..." }, + { "Hi, my name_is\nDick", 2, 3, true, "Hi,\n my\n..." }, + { "Hi, my name_is\nDick", 3, 3, true, "Hi,\n my\n na\n..." }, + { "Hi, my name_is\nDick", 4, 3, true, "Hi,\n my\n na\nme_\n..." }, + { "Hi, my name_is\nDick", 5, 3, true, "Hi,\n my\n na\nme_\nis\n..." }, + { "Hi, my name_is\nDick", 1, 4, true, "Hi, ..." }, + { "Hi, my name_is\nDick", 2, 4, true, "Hi, my n\n..." }, + { "Hi, my name_is\nDick", 3, 4, true, "Hi, my n\name_\n..." }, + { "Hi, my name_is\nDick", 4, 4, true, "Hi, my n\name_\nis\n..." }, + { "Hi, my name_is\nDick", 5, 4, false, "Hi, my n\name_\nis\nDick" }, + { "Hi, my name_is\nDick", 1, 5, true, "Hi, ..." }, + { "Hi, my name_is\nDick", 2, 5, true, "Hi, my na\n..." }, + { "Hi, my name_is\nDick", 3, 5, true, "Hi, my na\nme_is\n..." }, + { "Hi, my name_is\nDick", 4, 5, true, "Hi, my na\nme_is\n\n..." }, + { "Hi, my name_is\nDick", 5, 5, false, "Hi, my na\nme_is\n\nDick" }, + { "Hi, my name_is\nDick", 1, 6, true, "Hi, ..." }, + { "Hi, my name_is\nDick", 2, 6, true, "Hi, my nam\n..." }, + { "Hi, my name_is\nDick", 3, 6, true, "Hi, my nam\ne_is\n..." }, + { "Hi, my name_is\nDick", 4, 6, false, "Hi, my nam\ne_is\nDick" }, + { "Hi, my name_is\nDick", 5, 6, false, "Hi, my nam\ne_is\nDick" }, + { "Hi, my name_is\nDick", 1, 7, true, "Hi, ..." }, + { "Hi, my name_is\nDick", 2, 7, true, "Hi, my name\n..." }, + { "Hi, my name_is\nDick", 3, 7, true, "Hi, my name\n_is\n..." }, + { "Hi, my name_is\nDick", 4, 7, false, "Hi, my name\n_is\nDick" }, + { "Hi, my name_is\nDick", 5, 7, false, "Hi, my name\n_is\nDick" }, + { "Hi, my name_is\nDick", 1, 8, true, "Hi, my n\n..." }, + { "Hi, my name_is\nDick", 2, 8, true, "Hi, my n\name_is\n..." }, + { "Hi, my name_is\nDick", 3, 8, false, "Hi, my n\name_is\nDick" }, + { "Hi, my name_is\nDick", 1, 9, true, "Hi, my ..." }, + { "Hi, my name_is\nDick", 2, 9, true, "Hi, my name_is\n..." }, + { "Hi, my name_is\nDick", 3, 9, false, "Hi, my name_is\nDick" }, + { "Hi, my name_is\nDick", 1, 10, true, "Hi, my ..." }, + { "Hi, my name_is\nDick", 2, 10, true, "Hi, my name_is\n..." }, + { "Hi, my name_is\nDick", 3, 10, false, "Hi, my name_is\nDick" }, + { "Hi, my name_is\nDick", 1, 11, true, "Hi, my ..." }, + { "Hi, my name_is\nDick", 2, 11, true, "Hi, my name_is\n..." }, + { "Hi, my name_is\nDick", 3, 11, false, "Hi, my name_is\nDick" }, + { "Hi, my name_is\nDick", 1, 12, true, "Hi, my ..." }, + { "Hi, my name_is\nDick", 2, 12, true, "Hi, my name_is\n..." }, + { "Hi, my name_is\nDick", 3, 12, false, "Hi, my name_is\nDick" }, + { "Hi, my name_is\nDick", 1, 13, true, "Hi, my ..." }, + { "Hi, my name_is\nDick", 2, 13, true, "Hi, my name_is\n..." }, + { "Hi, my name_is\nDick", 3, 13, false, "Hi, my name_is\nDick" }, + { "Hi, my name_is\nDick", 1, 20, true, "Hi, my name_is\n..." }, + { "Hi, my name_is\nDick", 2, 20, false, "Hi, my name_is\nDick" }, + { "Hi, my name_is Dick", 1, 40, false, "Hi, my name_is Dick" }, + }; + string16 output; + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { + EXPECT_EQ(cases[i].result, + ElideRectangleString(UTF8ToUTF16(cases[i].input), + cases[i].max_rows, cases[i].max_cols, + false, &output)); + EXPECT_EQ(cases[i].output, UTF16ToUTF8(output)); + } +} + +TEST(TextEliderTest, ElideRectangleWide16) { + // Two greek words separated by space. + const string16 str(WideToUTF16( + L"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9" + L"\x03bf\x03c2\x0020\x0399\x03c3\x03c4\x03cc\x03c2")); + const string16 out1(WideToUTF16( + L"\x03a0\x03b1\x03b3\x03ba\n" + L"\x03cc\x03c3\x03bc\x03b9\n" + L"...")); + const string16 out2(WideToUTF16( + L"\x03a0\x03b1\x03b3\x03ba\x03cc\x03c3\x03bc\x03b9\x03bf\x03c2\x0020\n" + L"\x0399\x03c3\x03c4\x03cc\x03c2")); + string16 output; + EXPECT_TRUE(ElideRectangleString(str, 2, 4, true, &output)); + EXPECT_EQ(out1, output); + EXPECT_FALSE(ElideRectangleString(str, 2, 12, true, &output)); + EXPECT_EQ(out2, output); +} + +TEST(TextEliderTest, ElideRectangleWide32) { + // Four U+1D49C MATHEMATICAL SCRIPT CAPITAL A followed by space "aaaaa". + const string16 str(UTF8ToUTF16( + "\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C" + " aaaaa")); + const string16 out(UTF8ToUTF16( + "\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\xF0\x9D\x92\x9C\n" + "\xF0\x9D\x92\x9C \naaa\n...")); + string16 output; + EXPECT_TRUE(ElideRectangleString(str, 3, 3, true, &output)); + EXPECT_EQ(out, output); +} + +TEST(TextEliderTest, TruncateString) { + string16 string = ASCIIToUTF16("foooooey bxxxar baz"); + + // Make sure it doesn't modify the string if length > string length. + EXPECT_EQ(string, TruncateString(string, 100)); + + // Test no characters. + EXPECT_EQ(L"", UTF16ToWide(TruncateString(string, 0))); + + // Test 1 character. + EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(string, 1))); + + // Test adds ... at right spot when there is enough room to break at a + // word boundary. + EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 14))); + + // Test adds ... at right spot when there is not enough space in first word. + EXPECT_EQ(L"f\x2026", UTF16ToWide(TruncateString(string, 2))); + + // Test adds ... at right spot when there is not enough room to break at a + // word boundary. + EXPECT_EQ(L"foooooey\x2026", UTF16ToWide(TruncateString(string, 11))); + + // Test completely truncates string if break is on initial whitespace. + EXPECT_EQ(L"\x2026", UTF16ToWide(TruncateString(ASCIIToUTF16(" "), 2))); +} + +} // namespace gfx diff --git a/ui/message_center/cocoa/notification_controller.mm b/ui/message_center/cocoa/notification_controller.mm index ab0187859abff6..59af2073259959 100644 --- a/ui/message_center/cocoa/notification_controller.mm +++ b/ui/message_center/cocoa/notification_controller.mm @@ -14,7 +14,7 @@ #import "ui/base/cocoa/hover_image_button.h" #include "ui/base/l10n/l10n_util_mac.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" #include "ui/message_center/message_center.h" #include "ui/message_center/message_center_style.h" #include "ui/message_center/notification.h" @@ -697,15 +697,15 @@ - (string16)wrapText:(const string16&)text int height = (lines + 1) * font.GetHeight(); std::vector wrapped; - ui::ElideRectangleText(text, font, width, height, - ui::WRAP_LONG_WORDS, &wrapped); + gfx::ElideRectangleText(text, font, width, height, + gfx::WRAP_LONG_WORDS, &wrapped); if (wrapped.size() > lines) { // Add an ellipsis to the last line. If this ellipsis makes the last line - // too wide, that line will be further elided by the ui::ElideText below. - string16 last = wrapped[lines - 1] + UTF8ToUTF16(ui::kEllipsis); + // too wide, that line will be further elided by the gfx::ElideText below. + string16 last = wrapped[lines - 1] + UTF8ToUTF16(gfx::kEllipsis); if (font.GetStringWidth(last) > width) - last = ui::ElideText(last, font, width, ui::ELIDE_AT_END); + last = gfx::ElideText(last, font, width, gfx::ELIDE_AT_END); wrapped.resize(lines - 1); wrapped.push_back(last); } diff --git a/ui/message_center/views/bounded_label.cc b/ui/message_center/views/bounded_label.cc index 6752795390e072..1fe428a314f4f5 100644 --- a/ui/message_center/views/bounded_label.cc +++ b/ui/message_center/views/bounded_label.cc @@ -8,8 +8,8 @@ #include "base/strings/string_util.h" #include "base/strings/utf_string_conversions.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/canvas.h" +#include "ui/gfx/text_elider.h" #include "ui/views/controls/label.h" namespace { @@ -143,18 +143,18 @@ std::vector InnerBoundedLabel::GetWrappedText(int width, int lines) { // Wrap, using INT_MAX for -1 widths that indicate no wrapping. std::vector wrapped; - ui::ElideRectangleText(text(), font(), + gfx::ElideRectangleText(text(), font(), (width < 0) ? std::numeric_limits::max() : width, - height, ui::WRAP_LONG_WORDS, &wrapped); + height, gfx::WRAP_LONG_WORDS, &wrapped); // Elide if necessary. if (lines > 0 && wrapped.size() > static_cast(lines)) { // Add an ellipsis to the last line. If this ellipsis makes the last line - // too wide, that line will be further elided by the ui::ElideText below, + // too wide, that line will be further elided by the gfx::ElideText below, // so for example "ABC" could become "ABC..." and then "AB...". - string16 last = wrapped[lines - 1] + UTF8ToUTF16(ui::kEllipsis); + string16 last = wrapped[lines - 1] + UTF8ToUTF16(gfx::kEllipsis); if (width > 0 && font().GetStringWidth(last) > width) - last = ui::ElideText(last, font(), width, ui::ELIDE_AT_END); + last = gfx::ElideText(last, font(), width, gfx::ELIDE_AT_END); wrapped.resize(lines - 1); wrapped.push_back(last); } diff --git a/ui/message_center/views/notification_view.cc b/ui/message_center/views/notification_view.cc index a39b4e4beeed80..5d878a38b8dc5a 100644 --- a/ui/message_center/views/notification_view.cc +++ b/ui/message_center/views/notification_view.cc @@ -10,10 +10,10 @@ #include "grit/ui_resources.h" #include "ui/base/layout.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/canvas.h" #include "ui/gfx/size.h" #include "ui/gfx/skia_util.h" +#include "ui/gfx/text_elider.h" #include "ui/message_center/message_center.h" #include "ui/message_center/message_center_style.h" #include "ui/message_center/message_center_switches.h" @@ -472,7 +472,7 @@ NotificationView::NotificationView(const Notification& notification, gfx::Font font = views::Label().font().DeriveFont(2); int padding = kTitleLineHeight - font.GetHeight(); title_view_ = new BoundedLabel( - ui::TruncateString(notification.title(), kTitleCharacterLimit), font); + gfx::TruncateString(notification.title(), kTitleCharacterLimit), font); title_view_->SetLineHeight(kTitleLineHeight); title_view_->SetLineLimit(message_center::kTitleLineLimit); title_view_->SetColors(message_center::kRegularTextColor, @@ -487,7 +487,7 @@ NotificationView::NotificationView(const Notification& notification, if (!notification.message().empty()) { int padding = kMessageLineHeight - views::Label().font().GetHeight(); message_view_ = new BoundedLabel( - ui::TruncateString(notification.message(), kMessageCharacterLimit)); + gfx::TruncateString(notification.message(), kMessageCharacterLimit)); message_view_->SetLineHeight(kMessageLineHeight); message_view_->SetVisible(!is_expanded() || !notification.items().size()); message_view_->SetColors(message_center::kRegularTextColor, @@ -503,7 +503,7 @@ NotificationView::NotificationView(const Notification& notification, gfx::Font font = views::Label().font(); int padding = kMessageLineHeight - font.GetHeight(); context_message_view_ = - new BoundedLabel(ui::TruncateString(notification.context_message(), + new BoundedLabel(gfx::TruncateString(notification.context_message(), kContextMessageCharacterLimit), font); context_message_view_->SetLineLimit( diff --git a/ui/ui.gyp b/ui/ui.gyp index e495f21da97bb6..505bbd06d8e183 100644 --- a/ui/ui.gyp +++ b/ui/ui.gyp @@ -316,8 +316,6 @@ 'base/resource/resource_handle.h', 'base/text/bytes_formatting.cc', 'base/text/bytes_formatting.h', - 'base/text/text_elider.cc', - 'base/text/text_elider.h', 'base/text/utf16_indexing.cc', 'base/text/utf16_indexing.h', 'base/theme_provider.cc', @@ -583,6 +581,8 @@ 'gfx/sys_color_change_listener.cc', 'gfx/sys_color_change_listener.h', 'gfx/text_constants.h', + 'gfx/text_elider.cc', + 'gfx/text_elider.h', 'gfx/text_utils.cc', 'gfx/text_utils.h', 'gfx/text_utils_android.cc', diff --git a/ui/ui_unittests.gypi b/ui/ui_unittests.gypi index 38c72e161a6443..8c9771b08d6445 100644 --- a/ui/ui_unittests.gypi +++ b/ui/ui_unittests.gypi @@ -104,7 +104,6 @@ 'base/resource/data_pack_literal.cc', 'base/resource/data_pack_unittest.cc', 'base/resource/resource_bundle_unittest.cc', - 'base/text/text_elider_unittest.cc', 'gfx/box_unittest.cc', 'gfx/codec/png_codec_unittest.cc', 'gfx/color_utils_unittest.cc', @@ -131,6 +130,7 @@ 'gfx/shadow_value_unittest.cc', 'gfx/size_unittest.cc', 'gfx/skbitmap_operations_unittest.cc', + 'gfx/text_elider_unittest.cc', 'gfx/text_utils_unittest.cc', 'gfx/vector2d_unittest.cc', 'gfx/vector3d_unittest.cc', diff --git a/ui/views/controls/label.cc b/ui/views/controls/label.cc index 05f9aedb78034f..7d4bc7e84703c0 100644 --- a/ui/views/controls/label.cc +++ b/ui/views/controls/label.cc @@ -16,11 +16,11 @@ #include "base/strings/utf_string_conversions.h" #include "ui/base/accessibility/accessible_view_state.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/canvas.h" #include "ui/gfx/color_utils.h" #include "ui/gfx/insets.h" #include "ui/gfx/shadow_value.h" +#include "ui/gfx/text_elider.h" #include "ui/gfx/text_utils.h" #include "ui/native_theme/native_theme.h" #include "ui/views/background.h" @@ -495,14 +495,15 @@ void Label::CalculateDrawStringParams(string16* paint_text, if (is_multi_line_ || (elide_behavior_ == NO_ELIDE)) { *paint_text = text_; } else if (elide_behavior_ == ELIDE_IN_MIDDLE) { - *paint_text = ui::ElideText(text_, font_list_, GetAvailableRect().width(), - ui::ELIDE_IN_MIDDLE); + *paint_text = gfx::ElideText(text_, font_list_, GetAvailableRect().width(), + gfx::ELIDE_IN_MIDDLE); } else if (elide_behavior_ == ELIDE_AT_END) { - *paint_text = ui::ElideText(text_, font_list_, GetAvailableRect().width(), - ui::ELIDE_AT_END); + *paint_text = gfx::ElideText(text_, font_list_, GetAvailableRect().width(), + gfx::ELIDE_AT_END); } else { DCHECK_EQ(ELIDE_AS_EMAIL, elide_behavior_); - *paint_text = ui::ElideEmail(text_, font_list_, GetAvailableRect().width()); + *paint_text = gfx::ElideEmail(text_, font_list_, + GetAvailableRect().width()); } *text_bounds = GetTextBounds(); diff --git a/ui/views/controls/styled_label.cc b/ui/views/controls/styled_label.cc index 268f3dbcb8abc8..735c8ee1e98972 100644 --- a/ui/views/controls/styled_label.cc +++ b/ui/views/controls/styled_label.cc @@ -7,7 +7,7 @@ #include #include "base/strings/string_util.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" #include "ui/native_theme/native_theme.h" #include "ui/views/controls/label.h" #include "ui/views/controls/link.h" @@ -188,11 +188,11 @@ int StyledLabel::CalculateAndDoLayout(int width, bool dry_run) { text_font = text_font.DeriveFont(0, style_ranges.top().style_info.font_style); } - ui::ElideRectangleText(remaining_string, + gfx::ElideRectangleText(remaining_string, text_font, chunk_bounds.width(), chunk_bounds.height(), - ui::IGNORE_LONG_WORDS, + gfx::IGNORE_LONG_WORDS, &substrings); DCHECK(!substrings.empty()); diff --git a/ui/views/corewm/tooltip_controller.cc b/ui/views/corewm/tooltip_controller.cc index 1c17d916bbaf87..f9943dfa9c5172 100644 --- a/ui/views/corewm/tooltip_controller.cc +++ b/ui/views/corewm/tooltip_controller.cc @@ -17,11 +17,11 @@ #include "ui/aura/window.h" #include "ui/base/events/event.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/font.h" #include "ui/gfx/point.h" #include "ui/gfx/rect.h" #include "ui/gfx/screen.h" +#include "ui/gfx/text_elider.h" #include "ui/views/background.h" #include "ui/views/border.h" #include "ui/views/controls/label.h" @@ -407,7 +407,7 @@ void TooltipController::TrimTooltipToFit(int max_width, if (result_lines.size() > kMaxLines) { result_lines.resize(kMaxLines); // Add ellipses character to last line. - result_lines[kMaxLines - 1] = ui::TruncateString( + result_lines[kMaxLines - 1] = gfx::TruncateString( result_lines.back(), result_lines.back().length() - 1); } *line_count = result_lines.size(); @@ -424,7 +424,8 @@ void TooltipController::TrimTooltipToFit(int max_width, // case, we simply truncate at available_width and add ellipses at the end. if (line_width > available_width) { *width = available_width; - result.append(ui::ElideText(*l, font, available_width, ui::ELIDE_AT_END)); + result.append(gfx::ElideText(*l, font, available_width, + gfx::ELIDE_AT_END)); } else { *width = std::max(*width, line_width); result.append(*l); diff --git a/ui/views/corewm/tooltip_controller_unittest.cc b/ui/views/corewm/tooltip_controller_unittest.cc index 2ea2129f78ff12..61dfb1800d7b22 100644 --- a/ui/views/corewm/tooltip_controller_unittest.cc +++ b/ui/views/corewm/tooltip_controller_unittest.cc @@ -13,9 +13,9 @@ #include "ui/aura/test/event_generator.h" #include "ui/aura/window.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/base/text/text_elider.h" #include "ui/gfx/font.h" #include "ui/gfx/point.h" +#include "ui/gfx/text_elider.h" #include "ui/views/corewm/tooltip_controller_test_helper.h" #include "ui/views/view.h" #include "ui/views/widget/widget.h" @@ -274,8 +274,8 @@ TEST_F(TooltipControllerTest, TrimTooltipToFitTests) { max_width, &tooltip, &width, &line_count); EXPECT_NEAR(max_pixel_width, width, 5); EXPECT_EQ(1, line_count); - EXPECT_EQ(ui::ElideText(UTF8ToUTF16(std::string('a', max_pixel_width)), font, - max_pixel_width, ui::ELIDE_AT_END), tooltip); + EXPECT_EQ(gfx::ElideText(UTF8ToUTF16(std::string('a', max_pixel_width)), font, + max_pixel_width, gfx::ELIDE_AT_END), tooltip); #endif // Normal small tooltip should stay as is. diff --git a/ui/views/widget/tooltip_manager.cc b/ui/views/widget/tooltip_manager.cc index af51f0d0ddf2ac..cbfba8f2993e86 100644 --- a/ui/views/widget/tooltip_manager.cc +++ b/ui/views/widget/tooltip_manager.cc @@ -8,7 +8,7 @@ #include "base/strings/string_split.h" #include "base/strings/utf_string_conversions.h" -#include "ui/base/text/text_elider.h" +#include "ui/gfx/text_elider.h" #include "ui/gfx/text_utils.h" // Maximum number of characters we allow in a tooltip. @@ -50,7 +50,7 @@ void TooltipManager::TrimTooltipToFit(string16* text, for (std::vector::iterator i = lines.begin(); i != lines.end(); ++i) { string16 elided_text = - ui::ElideText(*i, font_list, available_width, ui::ELIDE_AT_END); + gfx::ElideText(*i, font_list, available_width, gfx::ELIDE_AT_END); *max_width = std::max(*max_width, gfx::GetStringWidth(elided_text, font_list)); if (!result.empty())