summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/i18n/break_iterator.cc6
-rw-r--r--base/i18n/break_iterator.h31
-rw-r--r--chrome/browser/ui/views/download_item_view.cc43
3 files changed, 58 insertions, 22 deletions
diff --git a/base/i18n/break_iterator.cc b/base/i18n/break_iterator.cc
index e1b5e29..edc8950 100644
--- a/base/i18n/break_iterator.cc
+++ b/base/i18n/break_iterator.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// 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.
@@ -33,7 +33,7 @@ bool BreakIterator::Init() {
case BREAK_WORD:
break_type = UBRK_WORD;
break;
- case BREAK_SPACE:
+ case BREAK_LINE:
case BREAK_NEWLINE:
break_type = UBRK_LINE;
break;
@@ -59,7 +59,7 @@ bool BreakIterator::Advance() {
prev_ = pos_;
switch (break_type_) {
case BREAK_WORD:
- case BREAK_SPACE:
+ case BREAK_LINE:
pos = ubrk_next(static_cast<UBreakIterator*>(iter_));
if (pos == UBRK_DONE) {
pos_ = npos;
diff --git a/base/i18n/break_iterator.h b/base/i18n/break_iterator.h
index 9de7ac7..f64a1e1 100644
--- a/base/i18n/break_iterator.h
+++ b/base/i18n/break_iterator.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// 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.
@@ -12,19 +12,28 @@
// The BreakIterator class iterates through the words, word breaks, and
// line breaks in a UTF-16 string.
//
-// It provides several modes, BREAK_WORD, BREAK_SPACE, and BREAK_NEWLINE,
+// It provides several modes, BREAK_WORD, BREAK_LINE, and BREAK_NEWLINE,
// which modify how characters are aggregated into the returned string.
//
// Under BREAK_WORD mode, once a word is encountered any non-word
// characters are not included in the returned string (e.g. in the
// UTF-16 equivalent of the string " foo bar! ", the word breaks are at
// the periods in ". .foo. .bar.!. .").
+// Note that Chinese/Japanese/Thai do not use spaces between words so that
+// boundaries can fall in the middle of a continuous run of non-space /
+// non-punctuation characters.
//
-// Under BREAK_SPACE mode, once a word is encountered, any non-word
-// characters are included in the returned string, breaking only when a
-// space-equivalent character is encountered (e.g. in the
-// UTF16-equivalent of the string " foo bar! ", the word breaks are at
-// the periods in ". .foo .bar! .").
+// Under BREAK_LINE mode, once a line breaking opportunity is encountered,
+// any non-word characters are included in the returned string, breaking
+// only when a space-equivalent character or a line breaking opportunity
+// is encountered (e.g. in the UTF16-equivalent of the string " foo bar! ",
+// the breaks are at the periods in ". .foo .bar! .").
+//
+// Note that lines can be broken at any character/syllable/grapheme cluster
+// boundary in Chinese/Japanese/Korean and at word boundaries in Thai
+// (Thai does not use spaces between words). Therefore, this is NOT the same
+// as breaking only at space-equivalent characters where its former
+// name (BREAK_SPACE) implied.
//
// Under BREAK_NEWLINE mode, all characters are included in the returned
// string, breking only when a newline-equivalent character is encountered
@@ -48,7 +57,11 @@ class BreakIterator {
public:
enum BreakType {
BREAK_WORD,
- BREAK_SPACE,
+ BREAK_LINE,
+ // TODO(jshin): Remove this after reviewing call sites.
+ // If call sites really need break only on space-like characters
+ // implement it separately.
+ BREAK_SPACE = BREAK_LINE,
BREAK_NEWLINE,
};
@@ -75,7 +88,7 @@ class BreakIterator {
// Under BREAK_WORD mode, returns true if the break we just hit is the
// end of a word. (Otherwise, the break iterator just skipped over e.g.
- // whitespace or punctuation.) Under BREAK_SPACE and BREAK_NEWLINE modes,
+ // whitespace or punctuation.) Under BREAK_LINE and BREAK_NEWLINE modes,
// this distinction doesn't apply and it always retuns false.
bool IsWord() const;
diff --git a/chrome/browser/ui/views/download_item_view.cc b/chrome/browser/ui/views/download_item_view.cc
index b644718..d119264 100644
--- a/chrome/browser/ui/views/download_item_view.cc
+++ b/chrome/browser/ui/views/download_item_view.cc
@@ -8,6 +8,7 @@
#include "base/callback.h"
#include "base/file_path.h"
+#include "base/i18n/break_iterator.h"
#include "base/i18n/rtl.h"
#include "base/metrics/histogram.h"
#include "base/string_util.h"
@@ -28,6 +29,7 @@
#include "ui/gfx/canvas_skia.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/image.h"
+#include "unicode/uchar.h"
#include "views/controls/button/native_button.h"
#include "views/controls/menu/menu_2.h"
#include "views/widget/root_view.h"
@@ -1047,28 +1049,49 @@ void DownloadItemView::SizeLabelToMinWidth() {
gfx::Size size;
int min_width = -1;
- size_t sp_index = text.find(L" ");
- while (sp_index != std::wstring::npos) {
- text.replace(sp_index, 1, L"\n");
- dangerous_download_label_->SetText(text);
+ string16 text16 = WideToUTF16(text);
+ // Using BREAK_WORD can work in most cases, but it can also break
+ // lines where it should not. Using BREAK_LINE is safer although
+ // slower for Chinese/Japanese. This is not perf-critical at all, though.
+ base::BreakIterator iter(&text16, base::BreakIterator::BREAK_LINE);
+ bool status = iter.Init();
+ DCHECK(status);
+
+ string16 current_text = text16;
+ string16 prev_text = text16;
+ while (iter.Advance()) {
+ size_t pos = iter.pos();
+ if (pos >= text16.length())
+ break;
+ // This can be a low surrogate codepoint, but u_isUWhiteSpace will
+ // return false and inserting a new line after a surrogate pair
+ // is perfectly ok.
+ char16 line_end_char = text16[pos - 1];
+ if (u_isUWhiteSpace(line_end_char))
+ current_text.replace(pos - 1, 1, 1, char16('\n'));
+ else
+ current_text.insert(pos, 1, char16('\n'));
+ dangerous_download_label_->SetText(UTF16ToWide(current_text));
size = dangerous_download_label_->GetPreferredSize();
if (min_width == -1)
min_width = size.width();
// If the width is growing again, it means we passed the optimal width spot.
- if (size.width() > min_width)
+ if (size.width() > min_width) {
+ dangerous_download_label_->SetText(UTF16ToWide(prev_text));
break;
- else
+ } else {
min_width = size.width();
+ }
// Restore the string.
- text.replace(sp_index, 1, L" ");
-
- sp_index = text.find(L" ", sp_index + 1);
+ prev_text = current_text;
+ current_text = text16;
}
- // If we have a line with no space, we won't cut it.
+ // If we have a line with no line breaking opportunity (which is very
+ // unlikely), we won't cut it.
if (min_width == -1)
size = dangerous_download_label_->GetPreferredSize();