This source file includes following definitions.
- elide_at_beginning_
- CutString
- FindValidBoundaryBefore
- FindValidBoundaryAfter
- ElideEmail
- ElideFilename
- ElideText
- ElideString
- output_
- Init
- AddString
- Finalize
- AddLine
- AddWord
- Append
- NewLine
- insufficient_height_
- Init
- AddString
- Finalize
- AddLine
- WrapWord
- AddWordOverflow
- AddWord
- AddToCurrentLine
- AddToCurrentLineWithWidth
- NewLine
- ElideRectangleString
- ElideRectangleText
- TruncateString
#include "ui/gfx/text_elider.h"
#include <string>
#include <vector>
#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 "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"
using base::ASCIIToUTF16;
using base::UTF8ToUTF16;
using base::WideToUTF16;
namespace gfx {
const char kEllipsis[] = "\xE2\x80\xA6";
const base::char16 kEllipsisUTF16[] = { 0x2026, 0 };
const base::char16 kForwardSlash = '/';
StringSlicer::StringSlicer(const base::string16& text,
const base::string16& ellipsis,
bool elide_in_middle,
bool elide_at_beginning)
: text_(text),
ellipsis_(ellipsis),
elide_in_middle_(elide_in_middle),
elide_at_beginning_(elide_at_beginning) {
}
base::string16 StringSlicer::CutString(size_t length, bool insert_ellipsis) {
const base::string16 ellipsis_text = insert_ellipsis ? ellipsis_
: base::string16();
if (elide_at_beginning_)
return ellipsis_text +
text_.substr(FindValidBoundaryBefore(text_.length() - length));
if (!elide_in_middle_)
return text_.substr(0, FindValidBoundaryBefore(length)) + ellipsis_text;
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);
}
size_t StringSlicer::FindValidBoundaryBefore(size_t index) const {
DCHECK_LE(index, text_.length());
if (index != text_.length())
U16_SET_CP_START(text_.data(), 0, index);
return index;
}
size_t StringSlicer::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;
}
base::string16 ElideEmail(const base::string16& email,
const FontList& font_list,
float available_pixel_width) {
if (GetStringWidthF(email, font_list) <= available_pixel_width)
return email;
const size_t split_index = email.find_last_of('@');
DCHECK_NE(split_index, base::string16::npos);
base::string16 username = email.substr(0, split_index);
base::string16 domain = email.substr(split_index + 1);
DCHECK(!username.empty());
DCHECK(!domain.empty());
const base::string16 kAtSignUTF16 = ASCIIToUTF16("@");
available_pixel_width -= GetStringWidthF(kAtSignUTF16, font_list);
const float full_username_width = GetStringWidthF(username, font_list);
const float available_domain_width =
available_pixel_width -
std::min(full_username_width,
GetStringWidthF(username.substr(0, 1) + kEllipsisUTF16,
font_list));
if (GetStringWidthF(domain, font_list) > available_domain_width) {
const float 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);
if (domain.length() <= 1U)
return base::string16(kEllipsisUTF16);
}
available_pixel_width -= GetStringWidthF(domain, font_list);
username = ElideText(username, font_list, available_pixel_width,
ELIDE_AT_END);
return username + kAtSignUTF16 + domain;
}
base::string16 ElideFilename(const base::FilePath& filename,
const FontList& font_list,
float available_pixel_width) {
#if defined(OS_WIN)
base::string16 filename_utf16 = filename.value();
base::string16 extension = filename.Extension();
base::string16 rootname = filename.BaseName().RemoveExtension().value();
#elif defined(OS_POSIX)
base::string16 filename_utf16 = WideToUTF16(base::SysNativeMBToWide(
filename.value()));
base::string16 extension = WideToUTF16(base::SysNativeMBToWide(
filename.Extension()));
base::string16 rootname = WideToUTF16(base::SysNativeMBToWide(
filename.BaseName().RemoveExtension().value()));
#endif
const float full_width = GetStringWidthF(filename_utf16, font_list);
if (full_width <= available_pixel_width)
return base::i18n::GetDisplayStringInLTRDirectionality(filename_utf16);
if (rootname.empty() || extension.empty()) {
const base::string16 elided_name = ElideText(filename_utf16, font_list,
available_pixel_width, ELIDE_AT_END);
return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
}
const float ext_width = GetStringWidthF(extension, font_list);
const float root_width = GetStringWidthF(rootname, font_list);
if (root_width + ext_width <= available_pixel_width) {
const base::string16 elided_name = rootname + extension;
return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
}
if (ext_width >= available_pixel_width) {
const base::string16 elided_name = ElideText(
rootname + extension, font_list, available_pixel_width,
ELIDE_IN_MIDDLE);
return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
}
float available_root_width = available_pixel_width - ext_width;
base::string16 elided_name =
ElideText(rootname, font_list, available_root_width, ELIDE_AT_END);
elided_name += extension;
return base::i18n::GetDisplayStringInLTRDirectionality(elided_name);
}
base::string16 ElideText(const base::string16& text,
const FontList& font_list,
float available_pixel_width,
ElideBehavior elide_behavior) {
if (text.empty())
return text;
const float current_text_pixel_width = GetStringWidthF(text, font_list);
const bool elide_in_middle = (elide_behavior == ELIDE_IN_MIDDLE);
const bool elide_at_beginning = (elide_behavior == ELIDE_AT_BEGINNING);
const bool insert_ellipsis = (elide_behavior != TRUNCATE_AT_END);
const base::string16 ellipsis = base::string16(kEllipsisUTF16);
StringSlicer slicer(text, ellipsis, elide_in_middle, elide_at_beginning);
if (current_text_pixel_width <= 0 && !text.empty()) {
const base::string16 cut =
slicer.CutString(text.length() / 2, insert_ellipsis);
return ElideText(cut, font_list, available_pixel_width, elide_behavior);
}
if (current_text_pixel_width <= available_pixel_width)
return text;
if (insert_ellipsis &&
GetStringWidthF(ellipsis, font_list) > available_pixel_width)
return base::string16();
size_t lo = 0;
size_t hi = text.length() - 1;
size_t guess;
for (guess = (lo + hi) / 2; lo <= hi; guess = (lo + hi) / 2) {
const base::string16 cut = slicer.CutString(guess, insert_ellipsis);
const float guess_width = GetStringWidthF(cut, font_list);
if (guess_width == available_pixel_width)
break;
if (guess_width > available_pixel_width) {
hi = guess - 1;
if (hi < lo)
lo = hi;
} else {
lo = guess + 1;
}
}
return slicer.CutString(guess, insert_ellipsis);
}
bool ElideString(const base::string16& input, int max_len,
base::string16* 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) + 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 {
class RectangleString {
public:
RectangleString(size_t max_rows, size_t max_cols,
bool strict, base::string16 *output)
: max_rows_(max_rows),
max_cols_(max_cols),
current_row_(0),
current_col_(0),
strict_(strict),
suppressed_(false),
output_(output) {}
void Init() { output_->clear(); }
void AddString(const base::string16& input);
bool Finalize();
private:
void AddLine(const base::string16& line);
void AddWord(const base::string16& word);
void Append(const base::string16& string);
void NewLine(bool output);
size_t max_rows_;
size_t max_cols_;
size_t current_row_;
size_t current_col_;
bool strict_;
bool suppressed_;
base::string16* output_;
DISALLOW_COPY_AND_ASSIGN(RectangleString);
};
void RectangleString::AddString(const base::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 base::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";
}
}
++current_row_;
current_col_ = 0;
}
void RectangleString::AddWord(const base::string16& word) {
if (word.length() < max_cols_) {
if (current_col_ + word.length() >= max_cols_)
NewLine(strict_);
Append(word);
} else {
int array_start = 0;
int char_start = 0;
base::i18n::UTF16CharIterator chars(&word);
while (!chars.end()) {
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();
}
if (array_start != chars.array_pos())
Append(word.substr(array_start, chars.array_pos() - array_start));
}
}
void RectangleString::Append(const base::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;
}
class RectangleText {
public:
RectangleText(const FontList& font_list,
float available_pixel_width,
int available_pixel_height,
WordWrapBehavior wrap_behavior,
std::vector<base::string16>* 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) {}
void Init() { lines_->clear(); }
void AddString(const base::string16& input);
int Finalize();
private:
void AddLine(const base::string16& line);
int WrapWord(const base::string16& word);
int AddWordOverflow(const base::string16& word);
int AddWord(const base::string16& word);
void AddToCurrentLineWithWidth(const base::string16& text, float text_width);
void AddToCurrentLine(const base::string16& text);
bool NewLine();
const FontList& font_list_;
const int line_height_;
const float available_pixel_width_;
const int available_pixel_height_;
const WordWrapBehavior wrap_behavior_;
float current_width_;
int current_height_;
base::string16 current_line_;
bool last_line_ended_in_lf_;
std::vector<base::string16>* lines_;
bool insufficient_width_;
bool insufficient_height_;
DISALLOW_COPY_AND_ASSIGN(RectangleText);
};
void RectangleText::AddString(const base::string16& input) {
base::i18n::BreakIterator lines(input,
base::i18n::BreakIterator::BREAK_NEWLINE);
if (lines.Init()) {
while (!insufficient_height_ && lines.Advance()) {
base::string16 line = lines.GetString();
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() {
if (!insufficient_height_ && !lines_->empty()) {
base::TrimWhitespace(lines_->back(), base::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(base::string16());
return (insufficient_width_ ? INSUFFICIENT_SPACE_HORIZONTAL : 0) |
(insufficient_height_ ? INSUFFICIENT_SPACE_VERTICAL : 0);
}
void RectangleText::AddLine(const base::string16& line) {
const float line_width = GetStringWidthF(line, font_list_);
if (line_width <= available_pixel_width_) {
AddToCurrentLineWithWidth(line, line_width);
} else {
base::i18n::BreakIterator words(line,
base::i18n::BreakIterator::BREAK_LINE);
if (words.Init()) {
while (words.Advance()) {
const bool truncate = !current_line_.empty();
const base::string16& word = words.GetString();
const int lines_added = AddWord(word);
if (lines_added) {
if (truncate) {
const int line = lines_->size() - lines_added;
base::TrimWhitespace(lines_->at(line), base::TRIM_TRAILING,
&lines_->at(line));
}
if (base::ContainsOnlyChars(word, base::kWhitespaceUTF16)) {
current_width_ = 0;
current_line_.clear();
}
}
}
} else {
NOTREACHED() << "BreakIterator (words) init failed";
}
}
NewLine();
}
int RectangleText::WrapWord(const base::string16& word) {
base::string16 text = word;
int lines_added = 0;
bool first_fragment = true;
while (!insufficient_height_ && !text.empty()) {
base::string16 fragment =
ElideText(text, font_list_, available_pixel_width_,
TRUNCATE_AT_END);
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 base::string16& word) {
int lines_added = 0;
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 base::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 base::string16& word) {
int lines_added = 0;
base::string16 trimmed;
base::TrimWhitespace(word, base::TRIM_TRAILING, &trimmed);
const float trimmed_width = GetStringWidthF(trimmed, font_list_);
if (trimmed_width <= available_pixel_width_) {
if ((current_width_ + trimmed_width > available_pixel_width_) && NewLine())
lines_added++;
AddToCurrentLine(word);
} else {
lines_added = AddWordOverflow(wrap_behavior_ == IGNORE_LONG_WORDS ?
trimmed : word);
}
return lines_added;
}
void RectangleText::AddToCurrentLine(const base::string16& text) {
AddToCurrentLineWithWidth(text, GetStringWidthF(text, font_list_));
}
void RectangleText::AddToCurrentLineWithWidth(const base::string16& text,
float 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;
}
}
bool ElideRectangleString(const base::string16& input, size_t max_rows,
size_t max_cols, bool strict,
base::string16* output) {
RectangleString rect(max_rows, max_cols, strict, output);
rect.Init();
rect.AddString(input);
return rect.Finalize();
}
int ElideRectangleText(const base::string16& input,
const FontList& font_list,
float available_pixel_width,
int available_pixel_height,
WordWrapBehavior wrap_behavior,
std::vector<base::string16>* lines) {
RectangleText rect(font_list,
available_pixel_width,
available_pixel_height,
wrap_behavior,
lines);
rect.Init();
rect.AddString(input);
return rect.Finalize();
}
base::string16 TruncateString(const base::string16& string, size_t length) {
if (string.size() <= length)
return string;
if (length == 0)
return base::string16();
size_t max = length - 1;
static const base::char16 kElideString[] = { 0x2026, 0 };
if (max == 0)
return kElideString;
UErrorCode status = U_ZERO_ERROR;
scoped_ptr<icu::RuleBasedBreakIterator> bi(
static_cast<icu::RuleBasedBreakIterator*>(
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<int32_t>(max));
if (index == icu::BreakIterator::DONE) {
index = static_cast<int32_t>(max);
} else {
icu::StringCharacterIterator char_iterator(string.c_str());
if (index == 0) {
index = static_cast<int32_t>(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)) {
char_iterator.next();
break;
}
}
if (char_iterator.hasPrevious()) {
index = char_iterator.getIndex();
} else {
return kElideString;
}
}
return string.substr(0, index) + kElideString;
}
}