summaryrefslogtreecommitdiffstats
path: root/ui/base/text
diff options
context:
space:
mode:
authorben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-01-20 18:27:06 +0000
committerben@chromium.org <ben@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-01-20 18:27:06 +0000
commit9dd7e3d78c14f67c5c3d78868a3a63bbc4f90634 (patch)
tree3b7332926a05a1c8382eb27422c385b56b29cb24 /ui/base/text
parenta12f7fbe12afffb4b1b31ec0d6b0988f1f9a6554 (diff)
downloadchromium_src-9dd7e3d78c14f67c5c3d78868a3a63bbc4f90634.zip
chromium_src-9dd7e3d78c14f67c5c3d78868a3a63bbc4f90634.tar.gz
chromium_src-9dd7e3d78c14f67c5c3d78868a3a63bbc4f90634.tar.bz2
Move a bunch of random other files to src/ui/base
BUG=none TEST=none TBR=brettw Review URL: http://codereview.chromium.org/6257006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@71970 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base/text')
-rw-r--r--ui/base/text/text_elider.cc665
-rw-r--r--ui/base/text/text_elider.h119
-rw-r--r--ui/base/text/text_elider_unittest.cc442
3 files changed, 1226 insertions, 0 deletions
diff --git a/ui/base/text/text_elider.cc b/ui/base/text/text_elider.cc
new file mode 100644
index 0000000..8a262fd
--- /dev/null
+++ b/ui/base/text/text_elider.cc
@@ -0,0 +1,665 @@
+// Copyright (c) 2011 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.
+
+#include <vector>
+
+#include "ui/base/text/text_elider.h"
+
+#include "base/file_path.h"
+#include "base/i18n/break_iterator.h"
+#include "base/i18n/char_iterator.h"
+#include "base/i18n/rtl.h"
+#include "base/string_split.h"
+#include "base/string_util.h"
+#include "base/sys_string_conversions.h"
+#include "base/utf_string_conversions.h"
+#include "gfx/font.h"
+#include "googleurl/src/gurl.h"
+#include "net/base/escape.h"
+#include "net/base/net_util.h"
+#include "net/base/registry_controlled_domain.h"
+
+namespace ui {
+
+namespace {
+
+const char* kEllipsis = "\xE2\x80\xA6";
+
+// Cuts |text| to be |length| characters long. If |cut_in_middle| is true, the
+// middle of the string is removed to leave equal-length pieces from the
+// beginning and end of the string; otherwise, the end of the string is removed
+// and only the beginning remains. If |insert_ellipsis| is true, then an
+// ellipsis character will by inserted at the cut point.
+string16 CutString(const string16& text,
+ size_t length,
+ bool cut_in_middle,
+ bool insert_ellipsis) {
+ // TODO(tony): This is wrong, it might split the string in the middle of a
+ // surrogate pair.
+ const string16 kInsert = insert_ellipsis ? UTF8ToUTF16(kEllipsis) :
+ ASCIIToUTF16("");
+ if (!cut_in_middle)
+ return text.substr(0, length) + kInsert;
+ // We put the extra character, if any, before the cut.
+ const size_t half_length = length / 2;
+ return text.substr(0, length - half_length) + kInsert +
+ text.substr(text.length() - half_length, half_length);
+}
+
+} // namespace
+
+// 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.
+//
+// TODO(pkasting): http://b/119635 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::Font& font,
+ int available_pixel_width,
+ const std::wstring& languages) {
+ // Get a formatted string and corresponding parsing of the url.
+ url_parse::Parsed parsed;
+ string16 url_string = net::FormatUrl(url, WideToUTF8(languages),
+ net::kFormatUrlOmitAll, UnescapeRule::SPACES, &parsed, NULL, NULL);
+ if (available_pixel_width <= 0)
+ return url_string;
+
+ // If non-standard or not file type, return plain eliding.
+ if (!(url.SchemeIsFile() || url.IsStandard()))
+ return ElideText(url_string, font, available_pixel_width, false);
+
+ // Now start eliding url_string to fit within available pixel width.
+ // Fist pass - check to see whether entire url_string fits.
+ int pixel_width_url_string = font.GetStringWidth(url_string);
+ if (available_pixel_width >= pixel_width_url_string)
+ return url_string;
+
+ // Get the path substring, including query and reference.
+ size_t path_start_index = parsed.path.begin;
+ 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.
+ string16 url_minus_query = url_string.substr(0, path_start_index + path_len);
+ if (available_pixel_width >= font.GetStringWidth(url_minus_query))
+ return ElideText(url_string, font, available_pixel_width, false);
+
+ // Get Host.
+ string16 url_host = UTF8ToUTF16(url.host());
+
+ // Get domain and registry information from the URL.
+ string16 url_domain = UTF8ToUTF16(
+ net::RegistryControlledDomainService::GetDomainAndRegistry(url));
+ 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;
+ size_t domain_start_index = url_host.find(url_domain);
+ if (domain_start_index > 0)
+ url_subdomain = url_host.substr(0, domain_start_index);
+ static 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<string16> 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();
+
+ static 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.
+ int pixel_width_url_host = font.GetStringWidth(url_host);
+ int pixel_width_url_path = font.GetStringWidth(url_path_query_etc);
+ 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.
+ int pixel_width_url_domain = font.GetStringWidth(url_domain);
+ int pixel_width_url_subdomain = font.GetStringWidth(url_subdomain);
+ 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 =
+ font.GetStringWidth(UTF8ToUTF16(kEllipsis));
+ 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 -
+ font.GetStringWidth(url_query))) {
+ return ElideText(url_subdomain + url_domain + url_path_query_etc,
+ font, available_pixel_width, false);
+ }
+ }
+
+ // Parse url_path using '/'.
+ static const string16 kForwardSlash = UTF8ToUTF16("/");
+ std::vector<string16> url_path_elements;
+ base::SplitString(url_path, kForwardSlash[0], &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,
+ available_pixel_width, false);
+ }
+
+ // Start eliding the path and replacing elements by "../".
+ const string16 kEllipsisAndSlash = UTF8ToUTF16(kEllipsis) + kForwardSlash;
+ int pixel_width_url_filename = font.GetStringWidth(url_filename);
+ int pixel_width_dot_dot_slash = font.GetStringWidth(kEllipsisAndSlash);
+ int pixel_width_slash = font.GetStringWidth(ASCIIToUTF16("/"));
+ int pixel_width_url_path_elements[kMaxNumberOfUrlPathElementsAllowed];
+ for (size_t i = 0; i < url_path_number_of_elements; ++i) {
+ pixel_width_url_path_elements[i] =
+ font.GetStringWidth(url_path_elements.at(i));
+ }
+
+ // Check with both subdomain and domain.
+ string16 elided_path;
+ int pixel_width_elided_path;
+ for (size_t i = url_path_number_of_elements - 1; i >= 1; --i) {
+ // Add the initial elements of the path.
+ elided_path.clear();
+ pixel_width_elided_path = 0;
+ for (size_t j = 0; j < i; ++j) {
+ elided_path += url_path_elements.at(j) + kForwardSlash;
+ pixel_width_elided_path += pixel_width_url_path_elements[j] +
+ pixel_width_slash;
+ }
+
+ // Add url_file_name.
+ if (i == (url_path_number_of_elements - 1)) {
+ elided_path += url_filename;
+ pixel_width_elided_path += pixel_width_url_filename;
+ } else {
+ elided_path += kEllipsisAndSlash + url_filename;
+ pixel_width_elided_path += pixel_width_dot_dot_slash +
+ pixel_width_url_filename;
+ }
+
+ if (available_pixel_width >=
+ pixel_width_url_subdomain + pixel_width_url_domain +
+ pixel_width_elided_path) {
+ return ElideText(url_subdomain + url_domain + elided_path + url_query,
+ font, available_pixel_width, false);
+ }
+ }
+
+ // 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;
+ int pixel_width_url_elided_domain = pixel_width_url_domain;
+ if (pixel_width_url_subdomain > kPixelWidthDotsTrailer) {
+ if (!url_subdomain.empty()) {
+ url_elided_domain = kEllipsisAndSlash[0] + url_domain;
+ pixel_width_url_elided_domain += kPixelWidthDotsTrailer;
+ } else {
+ url_elided_domain = url_domain;
+ }
+
+ for (size_t i = url_path_number_of_elements - 1; i >= 1; --i) {
+ // Add the initial elements of the path.
+ elided_path.clear();
+ pixel_width_elided_path = 0;
+ for (size_t j = 0; j < i; ++j) {
+ elided_path += url_path_elements.at(j) + kForwardSlash;
+ pixel_width_elided_path += pixel_width_url_path_elements[j] +
+ pixel_width_slash;
+ }
+
+ // Add url_file_name.
+ if (i == (url_path_number_of_elements - 1)) {
+ elided_path += url_filename;
+ pixel_width_elided_path += pixel_width_url_filename;
+ } else {
+ elided_path += kEllipsisAndSlash + url_filename;
+ pixel_width_elided_path += pixel_width_dot_dot_slash +
+ pixel_width_url_filename;
+ }
+
+ if (available_pixel_width >=
+ pixel_width_url_elided_domain + pixel_width_elided_path) {
+ return ElideText(url_elided_domain + elided_path + url_query, font,
+ available_pixel_width, false);
+ }
+ }
+ }
+
+ // Return elided domain/../filename anyway.
+ string16 final_elided_url_string(url_elided_domain);
+ int url_elided_domain_width = font.GetStringWidth(url_elided_domain);
+
+ // A hack to prevent trailing "../...".
+ if ((available_pixel_width - url_elided_domain_width) >
+ pixel_width_dot_dot_slash + kPixelWidthDotsTrailer +
+ font.GetStringWidth(ASCIIToUTF16("UV"))) {
+ final_elided_url_string += elided_path;
+ } else {
+ final_elided_url_string += url_path;
+ }
+
+ return ElideText(final_elided_url_string, font, available_pixel_width, false);
+}
+
+string16 ElideFilename(const FilePath& filename,
+ const gfx::Font& font,
+ 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
+
+ int full_width = font.GetStringWidth(filename_utf16);
+ if (full_width <= available_pixel_width)
+ return base::i18n::GetDisplayStringInLTRDirectionality(filename_utf16);
+
+ if (rootname.empty() || extension.empty()) {
+ string16 elided_name = ElideText(filename_utf16, font,
+ available_pixel_width, false);
+ return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
+ }
+
+ int ext_width = font.GetStringWidth(extension);
+ int root_width = font.GetStringWidth(rootname);
+
+ // We may have trimmed the path.
+ if (root_width + ext_width <= available_pixel_width) {
+ string16 elided_name = rootname + extension;
+ return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
+ }
+
+ int available_root_width = available_pixel_width - ext_width;
+ string16 elided_name =
+ ElideText(rootname, font, available_root_width, false);
+ elided_name += extension;
+ return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
+}
+
+// This function adds an ellipsis at the end of the text if the text
+// does not fit the given pixel width.
+string16 ElideText(const string16& text,
+ const gfx::Font& font,
+ int available_pixel_width,
+ bool elide_in_middle) {
+ if (text.empty())
+ return text;
+
+ int current_text_pixel_width = font.GetStringWidth(text);
+
+ // 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()) {
+ return ElideText(CutString(text, text.length() / 2, elide_in_middle, false),
+ font, available_pixel_width, false);
+ }
+
+ if (current_text_pixel_width <= available_pixel_width)
+ return text;
+
+ if (font.GetStringWidth(UTF8ToUTF16(kEllipsis)) > available_pixel_width)
+ return string16();
+
+ // Use binary search to compute the elided text.
+ size_t lo = 0;
+ size_t hi = text.length() - 1;
+ for (size_t guess = (lo + hi) / 2; guess != lo; guess = (lo + hi) / 2) {
+ // We check the length of the whole desired string at once to ensure we
+ // handle kerning/ligatures/etc. correctly.
+ int guess_length = font.GetStringWidth(
+ CutString(text, guess, elide_in_middle, true));
+ // 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(CutString(text, guess / 2, elide_in_middle, false),
+ font, available_pixel_width, elide_in_middle);
+ }
+ if (guess_length > available_pixel_width)
+ hi = guess;
+ else
+ lo = guess;
+ }
+
+ return CutString(text, lo, elide_in_middle, true);
+}
+
+// TODO(viettrungluu): convert |languages| to an |std::string|.
+SortedDisplayURL::SortedDisplayURL(const GURL& url,
+ const std::wstring& languages) {
+ std::wstring host;
+ net::AppendFormattedHost(url, languages, &host, NULL, NULL);
+ sort_host_ = WideToUTF16Hack(host);
+ string16 host_minus_www = net::StripWWW(WideToUTF16Hack(host));
+ url_parse::Parsed parsed;
+ display_url_ = net::FormatUrl(url, WideToUTF8(languages),
+ net::kFormatUrlOmitAll, 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() {
+}
+
+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<const UChar*>(sort_host_.c_str()),
+ static_cast<int>(sort_host_.length()),
+ static_cast<const UChar*>(other.sort_host_.c_str()),
+ static_cast<int>(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<const UChar*>(path.c_str()),
+ static_cast<int>(path.length()),
+ static_cast<const UChar*>(o_path.c_str()),
+ static_cast<int>(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<const UChar*>(display_url_.c_str()),
+ static_cast<int>(display_url_.length()),
+ static_cast<const UChar*>(other.display_url_.c_str()),
+ static_cast<int>(other.display_url_.length()),
+ compare_status);
+ DCHECK(U_SUCCESS(compare_status));
+ return display_url_compare_result;
+}
+
+string16 SortedDisplayURL::AfterHost() 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 std::wstring& input, int max_len, std::wstring* output) {
+ DCHECK_GE(max_len, 0);
+ if (static_cast<int>(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) + L"." +
+ input.substr(input.length() - 1));
+ break;
+ case 4:
+ output->assign(input.substr(0, 1) + L".." +
+ 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) + L"..." +
+ input.substr(input.length() - rstr_len));
+ break;
+ }
+ }
+
+ return true;
+}
+
+} // namespace ui
+
+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, string16 *output)
+ : max_rows_(max_rows),
+ max_cols_(max_cols),
+ current_row_(0),
+ current_col_(0),
+ suppressed_(false),
+ output_(output) {}
+
+ // Perform deferred initializions 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 occured.
+ 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 rectangluar region at the current position,
+ // either by itelf 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);
+
+ // Add a newline to the output string if the rectangular boundaries
+ // have not been exceeded, resetting the current position to the
+ // beginning of the next line.
+ void NewLine();
+
+ // 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 some of the input has been truncated.
+ bool suppressed_;
+
+ // String onto which the output is accumulated.
+ string16 *output_;
+};
+
+void RectangleString::AddString(const string16& input) {
+ base::BreakIterator lines(&input, base::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::BreakIterator words(&line, base::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();
+ 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();
+ array_start = chars.array_pos();
+ char_start = chars.char_pos();
+ }
+ chars.Advance();
+ }
+ // add 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() {
+ if (current_row_ < max_rows_)
+ output_->append(ASCIIToUTF16("\n"));
+ else
+ suppressed_ = true;
+ ++current_row_;
+ current_col_ = 0;
+}
+
+} // namespace
+
+namespace ui {
+
+bool ElideRectangleString(const string16& input, size_t max_rows,
+ size_t max_cols, string16* output) {
+ RectangleString rect(max_rows, max_cols, output);
+ rect.Init();
+ rect.AddString(input);
+ return rect.Finalize();
+}
+
+} // namespace ui
diff --git a/ui/base/text/text_elider.h b/ui/base/text/text_elider.h
new file mode 100644
index 0000000..97b9542
--- /dev/null
+++ b/ui/base/text/text_elider.h
@@ -0,0 +1,119 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef UI_BASE_TEXT_TEXT_ELIDER_H_
+#define UI_BASE_TEXT_TEXT_ELIDER_H_
+#pragma once
+
+#include <unicode/coll.h>
+#include <unicode/uchar.h>
+
+#include "base/basictypes.h"
+#include "base/string16.h"
+#include "gfx/font.h"
+
+class FilePath;
+class GURL;
+
+// TODO(port): this file should deal in string16s rather than wstrings.
+namespace ui {
+
+// 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, empty
+// string is returned. |languages| is a comma separted 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.
+string16 ElideUrl(const GURL& url,
+ const gfx::Font& font,
+ int available_pixel_width,
+ const std::wstring& languages);
+
+// Elides |text| to fit in |available_pixel_width|. If |elide_in_middle| is
+// set the ellipsis is placed in the middle of the string; otherwise it is
+// placed at the end.
+string16 ElideText(const string16& text,
+ const gfx::Font& font,
+ int available_pixel_width,
+ bool elide_in_middle);
+
+// Elide a filename to fit a given pixel width, with an emphasis on not hiding
+// the extension unless we have to. If filename contains a path, the path will
+// 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.
+string16 ElideFilename(const 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 SortedDisplayURL {
+ public:
+ SortedDisplayURL(const GURL& url, const std::wstring& 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_;
+};
+
+// 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(L"Hello", 10, &str) puts Hello in str and returns false.
+// ElideString(L"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
+bool ElideString(const std::wstring& input, int max_len, std::wstring* 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, first at naturally
+// occuring whitespace boundaries, and then 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).
+bool ElideRectangleString(const string16& input, size_t max_rows,
+ size_t max_cols, string16* output);
+
+
+} // namespace ui
+
+#endif // UI_BASE_TEXT_TEXT_ELIDER_H_
diff --git a/ui/base/text/text_elider_unittest.cc b/ui/base/text/text_elider_unittest.cc
new file mode 100644
index 0000000..0889d27
--- /dev/null
+++ b/ui/base/text/text_elider_unittest.cc
@@ -0,0 +1,442 @@
+// Copyright (c) 2011 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.
+
+#include "base/file_path.h"
+#include "base/i18n/rtl.h"
+#include "base/scoped_ptr.h"
+#include "base/string_util.h"
+#include "base/utf_string_conversions.h"
+#include "gfx/font.h"
+#include "googleurl/src/gurl.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "ui/base/text/text_elider.h"
+
+namespace ui {
+
+namespace {
+
+const wchar_t kEllipsis[] = L"\x2026";
+
+struct Testcase {
+ const std::string input;
+ const std::wstring output;
+};
+
+struct FileTestcase {
+ const FilePath::StringType input;
+ const std::wstring output;
+};
+
+struct UTF16Testcase {
+ const string16 input;
+ const string16 output;
+};
+
+struct TestData {
+ const std::string a;
+ const std::string b;
+ const int compare_result;
+};
+
+void RunTest(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(WideToUTF16(testcases[i].output),
+ ElideUrl(url, font,
+ font.GetStringWidth(WideToUTF16(testcases[i].output)),
+ std::wstring()));
+ }
+}
+
+} // namespace
+
+// Test eliding of commonplace URLs.
+TEST(TextEliderTest, TestGeneralEliding) {
+ const std::wstring kEllipsisStr(kEllipsis);
+ Testcase testcases[] = {
+ {"http://www.google.com/intl/en/ads/",
+ L"www.google.com/intl/en/ads/"},
+ {"http://www.google.com/intl/en/ads/", L"www.google.com/intl/en/ads/"},
+// TODO(port): make this test case work on mac.
+#if !defined(OS_MACOSX)
+ {"http://www.google.com/intl/en/ads/",
+ L"google.com/intl/" + kEllipsisStr + L"/ads/"},
+#endif
+ {"http://www.google.com/intl/en/ads/",
+ L"google.com/" + kEllipsisStr + L"/ads/"},
+ {"http://www.google.com/intl/en/ads/", L"google.com/" + kEllipsisStr},
+ {"http://www.google.com/intl/en/ads/", L"goog" + kEllipsisStr},
+ {"https://subdomain.foo.com/bar/filename.html",
+ L"subdomain.foo.com/bar/filename.html"},
+ {"https://subdomain.foo.com/bar/filename.html",
+ L"subdomain.foo.com/" + kEllipsisStr + L"/filename.html"},
+ {"http://subdomain.foo.com/bar/filename.html",
+ kEllipsisStr + L"foo.com/" + kEllipsisStr + L"/filename.html"},
+ {"http://www.google.com/intl/en/ads/?aLongQueryWhichIsNotRequired",
+ L"www.google.com/intl/en/ads/?aLongQ" + kEllipsisStr},
+ };
+
+ RunTest(testcases, arraysize(testcases));
+}
+
+// Test eliding of empty strings, URLs with ports, passwords, queries, etc.
+TEST(TextEliderTest, TestMoreEliding) {
+ const std::wstring kEllipsisStr(kEllipsis);
+ Testcase testcases[] = {
+ {"http://www.google.com/foo?bar", L"www.google.com/foo?bar"},
+ {"http://xyz.google.com/foo?bar", L"xyz.google.com/foo?" + kEllipsisStr},
+ {"http://xyz.google.com/foo?bar", L"xyz.google.com/foo" + kEllipsisStr},
+ {"http://xyz.google.com/foo?bar", L"xyz.google.com/fo" + kEllipsisStr},
+ {"http://a.b.com/pathname/c?d", L"a.b.com/" + kEllipsisStr + L"/c?d"},
+ {"", L""},
+ {"http://foo.bar..example.com...hello/test/filename.html",
+ L"foo.bar..example.com...hello/" + kEllipsisStr + L"/filename.html"},
+ {"http://foo.bar../", L"foo.bar.."},
+ {"http://xn--1lq90i.cn/foo", L"\x5317\x4eac.cn/foo"},
+ {"http://me:mypass@secrethost.com:99/foo?bar#baz",
+ L"secrethost.com:99/foo?bar#baz"},
+ {"http://me:mypass@ss%xxfdsf.com/foo", L"ss%25xxfdsf.com/foo"},
+ {"mailto:elgoato@elgoato.com", L"mailto:elgoato@elgoato.com"},
+ {"javascript:click(0)", L"javascript:click(0)"},
+ {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
+ L"chess.eecs.berkeley.edu:4430/login/arbitfilename"},
+ {"https://chess.eecs.berkeley.edu:4430/login/arbitfilename",
+ kEllipsisStr + L"berkeley.edu:4430/" + kEllipsisStr + L"/arbitfilename"},
+
+ // Unescaping.
+ {"http://www/%E4%BD%A0%E5%A5%BD?q=%E4%BD%A0%E5%A5%BD#\xe4\xbd\xa0",
+ L"www/\x4f60\x597d?q=\x4f60\x597d#\x4f60"},
+
+ // 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",
+ L"www/%E4%A0%E5%A5%BD?q=\x4f60\x597d#\x4f60"},
+ };
+
+ RunTest(testcases, arraysize(testcases));
+}
+
+// Test eliding of file: URLs.
+TEST(TextEliderTest, TestFileURLEliding) {
+ const std::wstring kEllipsisStr(kEllipsis);
+ Testcase testcases[] = {
+ {"file:///C:/path1/path2/path3/filename",
+ L"file:///C:/path1/path2/path3/filename"},
+ {"file:///C:/path1/path2/path3/filename",
+ L"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",
+ L"C:/path1/path2/" + kEllipsisStr + L"/filename"},
+ {"file:///C:path1/path2/path3/filename",
+ L"C:/path1/" + kEllipsisStr + L"/filename"},
+ {"file:///C:path1/path2/path3/filename",
+ L"C:/" + kEllipsisStr + L"/filename"},
+#endif
+ {"file://filer/foo/bar/file", L"filer/foo/bar/file"},
+ {"file://filer/foo/bar/file", L"filer/foo/" + kEllipsisStr + L"/file"},
+ {"file://filer/foo/bar/file", L"filer/" + kEllipsisStr + L"/file"},
+ };
+
+ RunTest(testcases, arraysize(testcases));
+}
+
+TEST(TextEliderTest, TestFilenameEliding) {
+ const std::wstring kEllipsisStr(kEllipsis);
+ const FilePath::StringType kPathSeparator =
+ FilePath::StringType().append(1, FilePath::kSeparators[0]);
+
+ FileTestcase testcases[] = {
+ {FILE_PATH_LITERAL(""), L""},
+ {FILE_PATH_LITERAL("."), L"."},
+ {FILE_PATH_LITERAL("filename.exe"), L"filename.exe"},
+ {FILE_PATH_LITERAL(".longext"), L".longext"},
+ {FILE_PATH_LITERAL("pie"), L"pie"},
+ {FILE_PATH_LITERAL("c:") + kPathSeparator + FILE_PATH_LITERAL("path") +
+ kPathSeparator + FILE_PATH_LITERAL("filename.pie"),
+ L"filename.pie"},
+ {FILE_PATH_LITERAL("c:") + kPathSeparator + FILE_PATH_LITERAL("path") +
+ kPathSeparator + FILE_PATH_LITERAL("longfilename.pie"),
+ L"long" + kEllipsisStr + L".pie"},
+ {FILE_PATH_LITERAL("http://path.com/filename.pie"), L"filename.pie"},
+ {FILE_PATH_LITERAL("http://path.com/longfilename.pie"),
+ L"long" + kEllipsisStr + L".pie"},
+ {FILE_PATH_LITERAL("piesmashingtacularpants"), L"pie" + kEllipsisStr},
+ {FILE_PATH_LITERAL(".piesmashingtacularpants"), L".pie" + kEllipsisStr},
+ {FILE_PATH_LITERAL("cheese."), L"cheese."},
+ {FILE_PATH_LITERAL("file name.longext"),
+ L"file" + kEllipsisStr + L".longext"},
+ {FILE_PATH_LITERAL("fil ename.longext"),
+ L"fil " + kEllipsisStr + L".longext"},
+ {FILE_PATH_LITERAL("filename.longext"),
+ L"file" + kEllipsisStr + L".longext"},
+ {FILE_PATH_LITERAL("filename.middleext.longext"),
+ L"filename.mid" + kEllipsisStr + L".longext"}
+ };
+
+ static const gfx::Font font;
+ for (size_t i = 0; i < arraysize(testcases); ++i) {
+ FilePath filepath(testcases[i].input);
+ string16 expected = WideToUTF16(testcases[i].output);
+ expected = base::i18n::GetDisplayStringInLTRDirectionality(expected);
+ EXPECT_EQ(expected, ElideFilename(filepath,
+ font,
+ font.GetStringWidth(WideToUTF16(testcases[i].output))));
+ }
+}
+
+TEST(TextEliderTest, ElideTextLongStrings) {
+ const string16 kEllipsisStr(WideToUTF16(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),
+ false).size());
+ EXPECT_EQ(kEllipsisStr,
+ ElideText(testcases_end[i].input, font, ellipsis_width, false));
+ }
+
+ size_t number_of_trailing_as = (data_scheme_length + number_of_as) / 2;
+ 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),
+ false).size());
+ EXPECT_EQ(kEllipsisStr,
+ ElideText(testcases_middle[i].input, font, ellipsis_width,
+ false));
+ }
+}
+
+// Verifies display_url is set correctly.
+TEST(TextEliderTest, SortedDisplayURL) {
+ ui::SortedDisplayURL d_url(GURL("http://www.google.com"), std::wstring());
+ 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<icu::Collator> 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) {
+ ui::SortedDisplayURL url1(GURL(tests[i].a), std::wstring());
+ ui::SortedDisplayURL url2(GURL(tests[i].b), std::wstring());
+ 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 wchar_t* input;
+ int max_len;
+ bool result;
+ const wchar_t* output;
+ } cases[] = {
+ { L"Hello", 0, true, L"" },
+ { L"", 0, false, L"" },
+ { L"Hello, my name is Tom", 1, true, L"H" },
+ { L"Hello, my name is Tom", 2, true, L"He" },
+ { L"Hello, my name is Tom", 3, true, L"H.m" },
+ { L"Hello, my name is Tom", 4, true, L"H..m" },
+ { L"Hello, my name is Tom", 5, true, L"H...m" },
+ { L"Hello, my name is Tom", 6, true, L"He...m" },
+ { L"Hello, my name is Tom", 7, true, L"He...om" },
+ { L"Hello, my name is Tom", 10, true, L"Hell...Tom" },
+ { L"Hello, my name is Tom", 100, false, L"Hello, my name is Tom" }
+ };
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
+ std::wstring output;
+ EXPECT_EQ(cases[i].result,
+ ui::ElideString(cases[i].input, cases[i].max_len, &output));
+ EXPECT_EQ(cases[i].output, output);
+ }
+}
+
+TEST(TextEliderTest, ElideRectangleString) {
+ struct TestData {
+ const wchar_t* input;
+ int max_rows;
+ int max_cols;
+ bool result;
+ const wchar_t* output;
+ } cases[] = {
+ { L"", 0, 0, false, L"" },
+ { L"", 1, 1, false, L"" },
+ { L"Hi, my name is\nTom", 0, 0, true, L"..." },
+ { L"Hi, my name is\nTom", 1, 0, true, L"\n..." },
+ { L"Hi, my name is\nTom", 0, 1, true, L"..." },
+ { L"Hi, my name is\nTom", 1, 1, true, L"H\n..." },
+ { L"Hi, my name is\nTom", 2, 1, true, L"H\ni\n..." },
+ { L"Hi, my name is\nTom", 3, 1, true, L"H\ni\n,\n..." },
+ { L"Hi, my name is\nTom", 4, 1, true, L"H\ni\n,\n \n..." },
+ { L"Hi, my name is\nTom", 5, 1, true, L"H\ni\n,\n \nm\n..." },
+ { L"Hi, my name is\nTom", 0, 2, true, L"..." },
+ { L"Hi, my name is\nTom", 1, 2, true, L"Hi\n..." },
+ { L"Hi, my name is\nTom", 2, 2, true, L"Hi\n, \n..." },
+ { L"Hi, my name is\nTom", 3, 2, true, L"Hi\n, \nmy\n..." },
+ { L"Hi, my name is\nTom", 4, 2, true, L"Hi\n, \nmy\n n\n..." },
+ { L"Hi, my name is\nTom", 5, 2, true, L"Hi\n, \nmy\n n\nam\n..." },
+ { L"Hi, my name is\nTom", 0, 3, true, L"..." },
+ { L"Hi, my name is\nTom", 1, 3, true, L"Hi,\n..." },
+ { L"Hi, my name is\nTom", 2, 3, true, L"Hi,\n my\n..." },
+ { L"Hi, my name is\nTom", 3, 3, true, L"Hi,\n my\n na\n..." },
+ { L"Hi, my name is\nTom", 4, 3, true, L"Hi,\n my\n na\nme \n..." },
+ { L"Hi, my name is\nTom", 5, 3, true, L"Hi,\n my\n na\nme \nis\n..." },
+ { L"Hi, my name is\nTom", 1, 4, true, L"Hi, \n..." },
+ { L"Hi, my name is\nTom", 2, 4, true, L"Hi, \nmy n\n..." },
+ { L"Hi, my name is\nTom", 3, 4, true, L"Hi, \nmy n\name \n..." },
+ { L"Hi, my name is\nTom", 4, 4, true, L"Hi, \nmy n\name \nis\n..." },
+ { L"Hi, my name is\nTom", 5, 4, false, L"Hi, \nmy n\name \nis\nTom" },
+ { L"Hi, my name is\nTom", 1, 5, true, L"Hi, \n..." },
+ { L"Hi, my name is\nTom", 2, 5, true, L"Hi, \nmy na\n..." },
+ { L"Hi, my name is\nTom", 3, 5, true, L"Hi, \nmy na\nme \n..." },
+ { L"Hi, my name is\nTom", 4, 5, true, L"Hi, \nmy na\nme \nis\n..." },
+ { L"Hi, my name is\nTom", 5, 5, false, L"Hi, \nmy na\nme \nis\nTom" },
+ { L"Hi, my name is\nTom", 1, 6, true, L"Hi, \n..." },
+ { L"Hi, my name is\nTom", 2, 6, true, L"Hi, \nmy \n..." },
+ { L"Hi, my name is\nTom", 3, 6, true, L"Hi, \nmy \nname \n..." },
+ { L"Hi, my name is\nTom", 4, 6, true, L"Hi, \nmy \nname \nis\n..." },
+ { L"Hi, my name is\nTom", 5, 6, false, L"Hi, \nmy \nname \nis\nTom" },
+ { L"Hi, my name is\nTom", 1, 7, true, L"Hi, \n..." },
+ { L"Hi, my name is\nTom", 2, 7, true, L"Hi, \nmy \n..." },
+ { L"Hi, my name is\nTom", 3, 7, true, L"Hi, \nmy \nname \n..." },
+ { L"Hi, my name is\nTom", 4, 7, true, L"Hi, \nmy \nname \nis\n..." },
+ { L"Hi, my name is\nTom", 5, 7, false, L"Hi, \nmy \nname \nis\nTom" },
+ { L"Hi, my name is\nTom", 1, 8, true, L"Hi, my \n..." },
+ { L"Hi, my name is\nTom", 2, 8, true, L"Hi, my \nname \n..." },
+ { L"Hi, my name is\nTom", 3, 8, true, L"Hi, my \nname \nis\n..." },
+ { L"Hi, my name is\nTom", 4, 8, false, L"Hi, my \nname \nis\nTom" },
+ { L"Hi, my name is\nTom", 1, 9, true, L"Hi, my \n..." },
+ { L"Hi, my name is\nTom", 2, 9, true, L"Hi, my \nname is\n..." },
+ { L"Hi, my name is\nTom", 3, 9, false, L"Hi, my \nname is\nTom" },
+ { L"Hi, my name is\nTom", 1, 10, true, L"Hi, my \n..." },
+ { L"Hi, my name is\nTom", 2, 10, true, L"Hi, my \nname is\n..." },
+ { L"Hi, my name is\nTom", 3, 10, false, L"Hi, my \nname is\nTom" },
+ { L"Hi, my name is\nTom", 1, 11, true, L"Hi, my \n..." },
+ { L"Hi, my name is\nTom", 2, 11, true, L"Hi, my \nname is\n..." },
+ { L"Hi, my name is\nTom", 3, 11, false, L"Hi, my \nname is\nTom" },
+ { L"Hi, my name is\nTom", 1, 12, true, L"Hi, my \n..." },
+ { L"Hi, my name is\nTom", 2, 12, true, L"Hi, my \nname is\n..." },
+ { L"Hi, my name is\nTom", 3, 12, false, L"Hi, my \nname is\nTom" },
+ { L"Hi, my name is\nTom", 1, 13, true, L"Hi, my name \n..." },
+ { L"Hi, my name is\nTom", 2, 13, true, L"Hi, my name \nis\n..." },
+ { L"Hi, my name is\nTom", 3, 13, false, L"Hi, my name \nis\nTom" },
+ { L"Hi, my name is\nTom", 1, 20, true, L"Hi, my name is\n..." },
+ { L"Hi, my name is\nTom", 2, 20, false, L"Hi, my name is\nTom" },
+ { L"Hi, my name is Tom", 1, 40, false, L"Hi, my name is Tom" },
+ };
+ string16 output;
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
+ EXPECT_EQ(cases[i].result,
+ ui::ElideRectangleString(WideToUTF16(cases[i].input),
+ cases[i].max_rows, cases[i].max_cols,
+ &output));
+ EXPECT_EQ(cases[i].output, UTF16ToWide(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(ui::ElideRectangleString(str, 2, 4, &output));
+ EXPECT_EQ(out1, output);
+ EXPECT_FALSE(ui::ElideRectangleString(str, 2, 12, &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(ui::ElideRectangleString(str, 3, 3, &output));
+ EXPECT_EQ(out, output);
+}
+
+} // namespace ui