OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <vector> | 5 #include <vector> |
6 | 6 |
7 #include "app/text_elider.h" | 7 #include "app/text_elider.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/i18n/break_iterator.h" | |
10 #include "base/i18n/char_iterator.h" | |
9 #include "base/i18n/rtl.h" | 11 #include "base/i18n/rtl.h" |
10 #include "base/string_split.h" | 12 #include "base/string_split.h" |
11 #include "base/string_util.h" | 13 #include "base/string_util.h" |
12 #include "base/sys_string_conversions.h" | 14 #include "base/sys_string_conversions.h" |
13 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
14 #include "gfx/font.h" | 16 #include "gfx/font.h" |
15 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
16 #include "net/base/escape.h" | 18 #include "net/base/escape.h" |
17 #include "net/base/net_util.h" | 19 #include "net/base/net_util.h" |
18 #include "net/base/registry_controlled_domain.h" | 20 #include "net/base/registry_controlled_domain.h" |
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
491 output->assign(input.substr(0, lstr_len) + L"..." + | 493 output->assign(input.substr(0, lstr_len) + L"..." + |
492 input.substr(input.length() - rstr_len)); | 494 input.substr(input.length() - rstr_len)); |
493 break; | 495 break; |
494 } | 496 } |
495 } | 497 } |
496 | 498 |
497 return true; | 499 return true; |
498 } | 500 } |
499 | 501 |
500 } // namespace gfx | 502 } // namespace gfx |
503 | |
504 namespace { | |
505 | |
506 class RectangleString { | |
Evan Martin
2010/12/21 23:39:18
Can you add a comment describing this class?
| |
507 public: | |
508 RectangleString(size_t max_rows, size_t max_cols, string16 *output) | |
509 : max_rows_(max_rows), | |
510 max_cols_(max_cols), | |
511 current_row_(0), | |
512 current_col_(0), | |
513 suppressed_(false), | |
514 output_(output) {} | |
515 | |
516 void Init() { output_->clear(); } | |
517 void AddString(const string16& input); | |
Evan Martin
2010/12/21 23:39:18
typo: double space
| |
518 bool Finalize(); | |
Evan Martin
2010/12/21 23:39:18
Please add comment docs to all functions and membe
| |
519 | |
520 private: | |
521 void AddLine(const string16& line); | |
522 void AddWord(const string16& word); | |
523 void Append(const string16& string); | |
524 void NewLine(); | |
525 | |
526 size_t max_rows_; | |
527 size_t max_cols_; | |
528 size_t current_row_; | |
529 size_t current_col_; | |
530 bool suppressed_; | |
531 string16 *output_; | |
532 }; | |
533 | |
534 void RectangleString::AddString(const string16& input) { | |
535 base::BreakIterator lines(&input, base::BreakIterator::BREAK_NEWLINE); | |
536 if (lines.Init()) { | |
537 while (lines.Advance()) | |
538 AddLine(lines.GetString()); | |
539 } else { | |
540 NOTREACHED() << "BreakIterator (lines) init failed"; | |
541 } | |
542 } | |
543 | |
544 bool RectangleString::Finalize() { | |
545 if (suppressed_) { | |
546 output_->append(WideToUTF16(L"...")); | |
Evan Martin
2010/12/21 23:39:18
ASCIIToUTF16("...")
Elsewhere we use a Unicode el
| |
547 return true; | |
548 } | |
549 return false; | |
550 } | |
551 | |
552 void RectangleString::AddLine(const string16& line) { | |
553 if (line.length() < max_cols_) { | |
554 Append(line); | |
555 } else { | |
556 base::BreakIterator words(&line, base::BreakIterator::BREAK_SPACE); | |
557 if (words.Init()) { | |
558 while (words.Advance()) | |
559 AddWord(words.GetString()); | |
560 } else { | |
561 NOTREACHED() << "BreakIterator (words) init failed"; | |
562 } | |
563 } | |
564 // Account for naturally-occuring newlines. | |
565 ++current_row_; | |
566 current_col_ = 0; | |
567 } | |
568 | |
569 void RectangleString::AddWord(const string16& word) { | |
570 if (word.length() < max_cols_) { | |
571 // Word can be made to fit, no need to fragment it. | |
572 if (current_col_ + word.length() >= max_cols_) | |
573 NewLine(); | |
574 Append(word); | |
575 } else { | |
576 // Word is so big that it must be fragmented. | |
577 int array_start = 0; | |
578 int char_start = 0; | |
579 base::UTF16CharIterator chars(&word); | |
580 while (!chars.end()) { | |
581 // When boundary is hit, add as much as will fit on this line. | |
582 if (current_col_ + (chars.char_pos() - char_start) >= max_cols_) { | |
583 Append(word.substr(array_start, chars.array_pos() - array_start)); | |
584 NewLine(); | |
585 array_start = chars.array_pos(); | |
586 char_start = chars.char_pos(); | |
587 } | |
588 chars.Advance(); | |
589 } | |
590 // add last remaining fragment, if any. | |
591 if (array_start != chars.array_pos()) | |
592 Append(word.substr(array_start, chars.array_pos() - array_start)); | |
593 } | |
594 } | |
595 | |
596 void RectangleString::Append(const string16& string) { | |
597 if (current_row_ < max_rows_) | |
598 output_->append(string); | |
599 else | |
600 suppressed_ = true; | |
601 current_col_ += string.length(); | |
602 } | |
603 | |
604 void RectangleString::NewLine() { | |
605 if (current_row_ < max_rows_) | |
606 output_->append(WideToUTF16(L"\n")); | |
Evan Martin
2010/12/21 23:39:18
ASCIIToUTF16("\n")
| |
607 else | |
608 suppressed_ = true; | |
609 ++current_row_; | |
610 current_col_ = 0; | |
611 } | |
612 | |
613 } // namespace | |
614 | |
615 namespace gfx { | |
616 | |
617 bool ElideRectangleString(const string16& input, size_t max_rows, | |
618 size_t max_cols, string16* output) { | |
619 RectangleString rect(max_rows, max_cols, output); | |
620 rect.Init(); | |
621 rect.AddString(input); | |
622 return rect.Finalize(); | |
623 } | |
624 | |
625 } // namespace gfx | |
626 | |
OLD | NEW |