OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ui/gfx/render_text.h" | 5 #include "ui/gfx/render_text.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <climits> | 8 #include <climits> |
9 | 9 |
10 #include "base/i18n/break_iterator.h" | 10 #include "base/i18n/break_iterator.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
13 #include "third_party/icu/source/common/unicode/rbbi.h" | 13 #include "third_party/icu/source/common/unicode/rbbi.h" |
14 #include "third_party/icu/source/common/unicode/utf16.h" | 14 #include "third_party/icu/source/common/unicode/utf16.h" |
15 #include "third_party/skia/include/core/SkTypeface.h" | 15 #include "third_party/skia/include/core/SkTypeface.h" |
16 #include "third_party/skia/include/effects/SkGradientShader.h" | 16 #include "third_party/skia/include/effects/SkGradientShader.h" |
17 #include "ui/gfx/canvas.h" | 17 #include "ui/gfx/canvas.h" |
18 #include "ui/gfx/insets.h" | 18 #include "ui/gfx/insets.h" |
19 #include "ui/gfx/skia_util.h" | 19 #include "ui/gfx/skia_util.h" |
20 #include "ui/gfx/text_constants.h" | 20 #include "ui/gfx/text_constants.h" |
21 #include "ui/gfx/text_elider.h" | 21 #include "ui/gfx/text_elider.h" |
22 #include "ui/gfx/text_utils.h" | |
22 #include "ui/gfx/utf16_indexing.h" | 23 #include "ui/gfx/utf16_indexing.h" |
23 | 24 |
24 namespace gfx { | 25 namespace gfx { |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
28 // All chars are replaced by this char when the password style is set. | 29 // All chars are replaced by this char when the password style is set. |
29 // TODO(benrg): GTK uses the first of U+25CF, U+2022, U+2731, U+273A, '*' | 30 // TODO(benrg): GTK uses the first of U+25CF, U+2022, U+2731, U+273A, '*' |
30 // that's available in the font (find_invisible_char() in gtkentry.c). | 31 // that's available in the font (find_invisible_char() in gtkentry.c). |
31 const base::char16 kPasswordReplacementChar = '*'; | 32 const base::char16 kPasswordReplacementChar = '*'; |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
358 // Reset selection model. SetText should always followed by SetSelectionModel | 359 // Reset selection model. SetText should always followed by SetSelectionModel |
359 // or SetCursorPosition in upper layer. | 360 // or SetCursorPosition in upper layer. |
360 SetSelectionModel(SelectionModel()); | 361 SetSelectionModel(SelectionModel()); |
361 | 362 |
362 // Invalidate the cached text direction if it depends on the text contents. | 363 // Invalidate the cached text direction if it depends on the text contents. |
363 if (directionality_mode_ == DIRECTIONALITY_FROM_TEXT) | 364 if (directionality_mode_ == DIRECTIONALITY_FROM_TEXT) |
364 text_direction_ = base::i18n::UNKNOWN_DIRECTION; | 365 text_direction_ = base::i18n::UNKNOWN_DIRECTION; |
365 | 366 |
366 obscured_reveal_index_ = -1; | 367 obscured_reveal_index_ = -1; |
367 UpdateLayoutText(); | 368 UpdateLayoutText(); |
368 ResetLayout(); | |
369 } | 369 } |
370 | 370 |
371 void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) { | 371 void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) { |
372 if (horizontal_alignment_ != alignment) { | 372 if (horizontal_alignment_ != alignment) { |
373 horizontal_alignment_ = alignment; | 373 horizontal_alignment_ = alignment; |
374 display_offset_ = Vector2d(); | 374 display_offset_ = Vector2d(); |
375 cached_bounds_and_offset_valid_ = false; | 375 cached_bounds_and_offset_valid_ = false; |
376 } | 376 } |
377 } | 377 } |
378 | 378 |
(...skipping 25 matching lines...) Expand all Loading... | |
404 insert_mode_ = !insert_mode_; | 404 insert_mode_ = !insert_mode_; |
405 cached_bounds_and_offset_valid_ = false; | 405 cached_bounds_and_offset_valid_ = false; |
406 } | 406 } |
407 | 407 |
408 void RenderText::SetObscured(bool obscured) { | 408 void RenderText::SetObscured(bool obscured) { |
409 if (obscured != obscured_) { | 409 if (obscured != obscured_) { |
410 obscured_ = obscured; | 410 obscured_ = obscured; |
411 obscured_reveal_index_ = -1; | 411 obscured_reveal_index_ = -1; |
412 cached_bounds_and_offset_valid_ = false; | 412 cached_bounds_and_offset_valid_ = false; |
413 UpdateLayoutText(); | 413 UpdateLayoutText(); |
414 ResetLayout(); | |
415 } | 414 } |
416 } | 415 } |
417 | 416 |
418 void RenderText::SetObscuredRevealIndex(int index) { | 417 void RenderText::SetObscuredRevealIndex(int index) { |
419 if (obscured_reveal_index_ == index) | 418 if (obscured_reveal_index_ == index) |
420 return; | 419 return; |
421 | 420 |
422 obscured_reveal_index_ = index; | 421 obscured_reveal_index_ = index; |
423 cached_bounds_and_offset_valid_ = false; | 422 cached_bounds_and_offset_valid_ = false; |
424 UpdateLayoutText(); | 423 UpdateLayoutText(); |
425 ResetLayout(); | |
426 } | 424 } |
427 | 425 |
428 void RenderText::SetMultiline(bool multiline) { | 426 void RenderText::SetMultiline(bool multiline) { |
429 if (multiline != multiline_) { | 427 if (multiline != multiline_) { |
430 multiline_ = multiline; | 428 multiline_ = multiline; |
431 cached_bounds_and_offset_valid_ = false; | 429 cached_bounds_and_offset_valid_ = false; |
432 lines_.clear(); | 430 lines_.clear(); |
433 } | 431 } |
434 } | 432 } |
435 | 433 |
434 void RenderText::SetElideBehavior(ElideBehavior elide_behavior) { | |
435 // TODO(skanuj) : Add a test for triggering layout change. | |
436 if (elide_behavior_ != elide_behavior) { | |
437 elide_behavior_ = elide_behavior; | |
438 UpdateLayoutText(); | |
439 } | |
440 } | |
441 | |
436 void RenderText::SetDisplayRect(const Rect& r) { | 442 void RenderText::SetDisplayRect(const Rect& r) { |
437 display_rect_ = r; | 443 if (r != display_rect_) { |
438 baseline_ = kInvalidBaseline; | 444 display_rect_ = r; |
439 cached_bounds_and_offset_valid_ = false; | 445 baseline_ = kInvalidBaseline; |
440 lines_.clear(); | 446 cached_bounds_and_offset_valid_ = false; |
447 lines_.clear(); | |
448 if (elide_behavior_ != gfx::NO_ELIDE) | |
449 UpdateLayoutText(); | |
450 } | |
441 } | 451 } |
442 | 452 |
443 void RenderText::SetCursorPosition(size_t position) { | 453 void RenderText::SetCursorPosition(size_t position) { |
444 MoveCursorTo(position, false); | 454 MoveCursorTo(position, false); |
445 } | 455 } |
446 | 456 |
447 void RenderText::MoveCursor(BreakType break_type, | 457 void RenderText::MoveCursor(BreakType break_type, |
448 VisualCursorDirection direction, | 458 VisualCursorDirection direction, |
449 bool select) { | 459 bool select) { |
450 SelectionModel position(cursor_position(), selection_model_.caret_affinity()); | 460 SelectionModel position(cursor_position(), selection_model_.caret_affinity()); |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
823 selection_color_(kDefaultColor), | 833 selection_color_(kDefaultColor), |
824 selection_background_focused_color_(kDefaultSelectionBackgroundColor), | 834 selection_background_focused_color_(kDefaultSelectionBackgroundColor), |
825 focused_(false), | 835 focused_(false), |
826 composition_range_(Range::InvalidRange()), | 836 composition_range_(Range::InvalidRange()), |
827 colors_(kDefaultColor), | 837 colors_(kDefaultColor), |
828 styles_(NUM_TEXT_STYLES), | 838 styles_(NUM_TEXT_STYLES), |
829 composition_and_selection_styles_applied_(false), | 839 composition_and_selection_styles_applied_(false), |
830 obscured_(false), | 840 obscured_(false), |
831 obscured_reveal_index_(-1), | 841 obscured_reveal_index_(-1), |
832 truncate_length_(0), | 842 truncate_length_(0), |
843 elide_behavior_(NO_ELIDE), | |
833 multiline_(false), | 844 multiline_(false), |
834 fade_head_(false), | 845 fade_head_(false), |
835 fade_tail_(false), | 846 fade_tail_(false), |
836 background_is_transparent_(false), | 847 background_is_transparent_(false), |
837 clip_to_display_rect_(true), | 848 clip_to_display_rect_(true), |
838 baseline_(kInvalidBaseline), | 849 baseline_(kInvalidBaseline), |
839 cached_bounds_and_offset_valid_(false) { | 850 cached_bounds_and_offset_valid_(false) { |
840 } | 851 } |
841 | 852 |
842 const Vector2d& RenderText::GetUpdatedDisplayOffset() { | 853 const Vector2d& RenderText::GetUpdatedDisplayOffset() { |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1102 U16_NEXT(text_.data(), end, text_.length(), unused_char); | 1113 U16_NEXT(text_.data(), end, text_.length(), unused_char); |
1103 | 1114 |
1104 // Gets the index in |layout_text_| to be replaced. | 1115 // Gets the index in |layout_text_| to be replaced. |
1105 const size_t cp_start = | 1116 const size_t cp_start = |
1106 static_cast<size_t>(gfx::UTF16IndexToOffset(text_, 0, start)); | 1117 static_cast<size_t>(gfx::UTF16IndexToOffset(text_, 0, start)); |
1107 if (layout_text_.length() > cp_start) | 1118 if (layout_text_.length() > cp_start) |
1108 layout_text_.replace(cp_start, 1, text_.substr(start, end - start)); | 1119 layout_text_.replace(cp_start, 1, text_.substr(start, end - start)); |
1109 } | 1120 } |
1110 } | 1121 } |
1111 | 1122 |
1112 const base::string16& text = obscured_ ? layout_text_ : text_; | 1123 const base::string16& text = GetLayoutText(); |
1113 if (truncate_length_ > 0 && truncate_length_ < text.length()) { | 1124 if (truncate_length_ > 0 && truncate_length_ < text.length()) { |
1114 // Truncate the text at a valid character break and append an ellipsis. | 1125 // Truncate the text at a valid character break and append an ellipsis. |
1115 icu::StringCharacterIterator iter(text.c_str()); | 1126 icu::StringCharacterIterator iter(text.c_str()); |
1116 iter.setIndex32(truncate_length_ - 1); | 1127 iter.setIndex32(truncate_length_ - 1); |
1117 layout_text_.assign(text.substr(0, iter.getIndex()) + gfx::kEllipsisUTF16); | 1128 layout_text_.assign(text.substr(0, iter.getIndex()) + gfx::kEllipsisUTF16); |
1118 } | 1129 } |
1130 | |
1131 if (elide_behavior_ != NO_ELIDE && display_rect_.width() > 0 && | |
1132 GetContentWidth() > display_rect_.width()) { | |
1133 base::string16 elided_text = ElideText(GetLayoutText()); | |
1134 | |
1135 // This doesn't trim styles so ellipsis may get rendered as a different | |
1136 // style than the preceding text. See crbug.com/327850. | |
1137 layout_text_.assign(elided_text); | |
1138 } | |
1139 ResetLayout(); | |
1140 } | |
1141 | |
1142 // TODO(skanuj): Fix code duplication with ElideText in ui/gfx/text_elider.cc | |
1143 // See crbug.com/327846 | |
1144 base::string16 RenderText::ElideText(const base::string16& text) { | |
1145 if (text.empty()) | |
1146 return text; | |
1147 | |
1148 const bool insert_ellipsis = (elide_behavior_ != TRUNCATE_AT_END); | |
1149 // Create a RenderText copy with attributes that affect the rendering width. | |
1150 scoped_ptr<RenderText> render_text(CreateInstance()); | |
1151 render_text->SetFontList(font_list_); | |
1152 render_text->SetDirectionalityMode(directionality_mode_); | |
1153 render_text->SetCursorEnabled(cursor_enabled_); | |
1154 render_text->styles_ = styles_; | |
1155 render_text->colors_ = colors_; | |
1156 render_text->SetText(text); | |
1157 const float current_text_pixel_width = render_text->GetContentWidth(); | |
1158 | |
1159 const base::string16 ellipsis = base::string16(kEllipsisUTF16); | |
1160 const bool elide_in_middle = false; | |
1161 StringSlicer slicer(text, ellipsis, elide_in_middle); | |
1162 | |
1163 // Pango will return 0 width for absurdly long strings. Cut the string in | |
1164 // half and try again. | |
1165 // This is caused by an int overflow in Pango (specifically, in | |
1166 // pango_glyph_string_extents_range). It's actually more subtle than just | |
1167 // returning 0, since on super absurdly long strings, the int can wrap and | |
1168 // return positive numbers again. Detecting that is probably not worth it | |
1169 // (eliding way too much from a ridiculous string is probably still | |
1170 // ridiculous), but we should check other widths for bogus values as well. | |
1171 if (current_text_pixel_width <= 0 && !text.empty()) | |
1172 return ElideText(slicer.CutString(text.length() / 2, insert_ellipsis)); | |
1173 | |
1174 if (current_text_pixel_width <= display_rect_.width()) | |
1175 return text; | |
1176 | |
1177 if (insert_ellipsis && | |
1178 GetStringWidthF(ellipsis, font_list_) > display_rect_.width()) | |
1179 return base::string16(); | |
1180 | |
1181 // Use binary search to compute the elided text. | |
1182 size_t lo = 0; | |
1183 size_t hi = text.length() - 1; | |
1184 | |
1185 for (size_t guess = (lo + hi) / 2; lo <= hi; guess = (lo + hi) / 2) { | |
1186 // Restore styles and colors. They will be truncated to size by SetText. | |
1187 render_text->styles_ = styles_; | |
1188 render_text->colors_ = colors_; | |
1189 base::string16 new_text = slicer.CutString(guess, false); | |
1190 render_text->SetText(new_text); | |
1191 | |
1192 // This has to be an additional step so that the ellipsis is rendered with | |
1193 // same style as trailing part of the text. | |
1194 if (insert_ellipsis) { | |
1195 // When ellipsis follows text whose directionality is not the same as that | |
1196 // of the whole text, it will be rendered with the directionality of the | |
1197 // whole text. Since we want ellipsis to indicate continuation of the | |
1198 // preceding text, we force the directionality of ellipsis to be same as | |
1199 // the preceding text using LTR or RTL markers. | |
1200 base::i18n::TextDirection leading_text_direction = | |
1201 base::i18n::GetFirstStrongCharacterDirection(new_text); | |
1202 base::i18n::TextDirection trailing_text_direction = | |
1203 base::i18n::GetLastStrongCharacterDirection(new_text); | |
1204 new_text += ellipsis; | |
1205 if (trailing_text_direction != leading_text_direction) { | |
1206 if (trailing_text_direction == base::i18n::LEFT_TO_RIGHT) | |
1207 new_text += base::i18n::kLeftToRightMark; | |
1208 else | |
1209 new_text += base::i18n::kRightToLeftMark; | |
1210 } | |
1211 render_text->SetText(new_text); | |
1212 } | |
1213 | |
1214 // We check the width of the whole desired string at once to ensure we | |
1215 // handle kerning/ligatures/etc. correctly. | |
1216 const int guess_width = render_text->GetContentWidth(); | |
1217 if (guess_width == display_rect_.width()) | |
1218 break; | |
1219 if (guess_width > display_rect_.width()) { | |
1220 hi = guess - 1; | |
msw
2013/12/17 02:14:33
I hit a weird bug while testing your code locally
Anuj
2013/12/20 07:02:42
Done.
| |
1221 // Move back if we are on loop terminating condition, and guess is wider | |
1222 // than available. | |
1223 if (hi < lo) | |
1224 lo = hi; | |
1225 } else { | |
1226 lo = guess + 1; | |
1227 } | |
1228 } | |
1229 | |
1230 return render_text->text(); | |
1119 } | 1231 } |
1120 | 1232 |
1121 void RenderText::UpdateCachedBoundsAndOffset() { | 1233 void RenderText::UpdateCachedBoundsAndOffset() { |
1122 if (cached_bounds_and_offset_valid_) | 1234 if (cached_bounds_and_offset_valid_) |
1123 return; | 1235 return; |
1124 | 1236 |
1125 // TODO(ckocagil): Add support for scrolling multiline text. | 1237 // TODO(ckocagil): Add support for scrolling multiline text. |
1126 | 1238 |
1127 // First, set the valid flag true to calculate the current cursor bounds using | 1239 // First, set the valid flag true to calculate the current cursor bounds using |
1128 // the stale |display_offset_|. Applying |delta_offset| at the end of this | 1240 // the stale |display_offset_|. Applying |delta_offset| at the end of this |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1167 cursor_bounds_ += delta_offset; | 1279 cursor_bounds_ += delta_offset; |
1168 } | 1280 } |
1169 | 1281 |
1170 void RenderText::DrawSelection(Canvas* canvas) { | 1282 void RenderText::DrawSelection(Canvas* canvas) { |
1171 const std::vector<Rect> sel = GetSubstringBounds(selection()); | 1283 const std::vector<Rect> sel = GetSubstringBounds(selection()); |
1172 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) | 1284 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) |
1173 canvas->FillRect(*i, selection_background_focused_color_); | 1285 canvas->FillRect(*i, selection_background_focused_color_); |
1174 } | 1286 } |
1175 | 1287 |
1176 } // namespace gfx | 1288 } // namespace gfx |
OLD | NEW |