Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: ui/gfx/render_text.cc

Issue 2164483006: [MacViews] Implemented text context menu (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed tapted's comments and fixed tests Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <limits.h> 7 #include <limits.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <climits> 10 #include <climits>
(...skipping 1010 matching lines...) Expand 10 before | Expand all | Expand 10 after
1021 if (!multiline()) { 1021 if (!multiline()) {
1022 offset.Add(GetUpdatedDisplayOffset()); 1022 offset.Add(GetUpdatedDisplayOffset());
1023 } else { 1023 } else {
1024 DCHECK_LT(line_number, lines().size()); 1024 DCHECK_LT(line_number, lines().size());
1025 offset.Add(Vector2d(0, lines_[line_number].preceding_heights)); 1025 offset.Add(Vector2d(0, lines_[line_number].preceding_heights));
1026 } 1026 }
1027 offset.Add(GetAlignmentOffset(line_number)); 1027 offset.Add(GetAlignmentOffset(line_number));
1028 return offset; 1028 return offset;
1029 } 1029 }
1030 1030
1031 bool RenderText::GetDecoratedWordAtPoint(const Point& point, 1031 bool RenderText::GetDecoratedWordAndBaselineAtPoint(
1032 DecoratedText* decorated_word, 1032 const Point& point,
1033 Point* baseline_point) { 1033 DecoratedText* decorated_word,
1034 Point* baseline_point) {
1034 // FindCursorPosition doesn't currently support multiline. See 1035 // FindCursorPosition doesn't currently support multiline. See
1035 // http://crbug.com/650120. 1036 // http://crbug.com/650120.
1036 if (multiline() || obscured()) 1037 if (multiline() || obscured())
1037 return false; 1038 return false;
1038 1039
1039 // Note: FindCursorPosition will trigger a layout via EnsureLayout. 1040 // Note: FindCursorPosition will trigger a layout via EnsureLayout.
1040 const SelectionModel model_at_point = FindCursorPosition(point); 1041 const SelectionModel model_at_point = FindCursorPosition(point);
1041 const size_t word_index = 1042 const size_t word_index =
1042 GetNearestWordStartBoundary(model_at_point.caret_pos()); 1043 GetNearestWordStartBoundary(model_at_point.caret_pos());
1043 if (word_index >= text().length()) 1044 if (word_index >= text().length())
1044 return false; 1045 return false;
1045 1046
1046 const Range word_range = ExpandRangeToWordBoundary(Range(word_index)); 1047 const Range word_range = ExpandRangeToWordBoundary(Range(word_index));
1047 DCHECK(!word_range.is_reversed()); 1048 DCHECK(!word_range.is_reversed());
1048 DCHECK(!word_range.is_empty()); 1049 DCHECK(!word_range.is_empty());
1049 1050
1050 const std::vector<Rect> word_bounds = GetSubstringBounds(word_range); 1051 return GetDecoratedTextAndBaselineForRange(word_range, decorated_word,
1051 if (word_bounds.empty() || 1052 baseline_point);
1052 !GetDecoratedTextForRange(word_range, decorated_word)) { 1053 }
1054
1055 bool RenderText::GetDecoratedTextAndBaselineForRange(
1056 const Range& range,
1057 DecoratedText* decorated_text,
1058 Point* baseline_point) {
1059 if (range.is_empty() || !GetDecoratedTextForRange(range, decorated_text))
1053 return false; 1060 return false;
1054 }
1055 1061
1056 // Retrieve the baseline origin of the left-most glyph. 1062 // Retrieve the baseline origin of the left-most glyph.
1063 const std::vector<Rect> text_bounds = GetSubstringBounds(range);
1064 if (text_bounds.empty())
1065 return false;
1066
1057 const auto left_rect = std::min_element( 1067 const auto left_rect = std::min_element(
1058 word_bounds.begin(), word_bounds.end(), 1068 text_bounds.begin(), text_bounds.end(),
1059 [](const Rect& lhs, const Rect& rhs) { return lhs.x() < rhs.x(); }); 1069 [](const Rect& lhs, const Rect& rhs) { return lhs.x() < rhs.x(); });
1060 *baseline_point = left_rect->origin() + Vector2d(0, GetDisplayTextBaseline()); 1070 *baseline_point = left_rect->origin() + Vector2d(0, GetDisplayTextBaseline());
1061 return true; 1071 return true;
1062 } 1072 }
1063 1073
1064 base::string16 RenderText::GetTextFromRange(const Range& range) const { 1074 base::string16 RenderText::GetTextFromRange(const Range& range) const {
1065 if (range.IsValid() && range.GetMin() < text().length()) 1075 if (range.IsValid() && range.GetMin() < text().length())
1066 return text().substr(range.GetMin(), range.length()); 1076 return text().substr(range.GetMin(), range.length());
1067 return base::string16(); 1077 return base::string16();
1068 } 1078 }
(...skipping 623 matching lines...) Expand 10 before | Expand all | Expand 10 after
1692 1702
1693 for (; range_max < length; ++range_max) 1703 for (; range_max < length; ++range_max)
1694 if (iter.IsEndOfWord(range_max) || iter.IsStartOfWord(range_max)) 1704 if (iter.IsEndOfWord(range_max) || iter.IsStartOfWord(range_max))
1695 break; 1705 break;
1696 1706
1697 return range.is_reversed() ? Range(range_max, range_min) 1707 return range.is_reversed() ? Range(range_max, range_min)
1698 : Range(range_min, range_max); 1708 : Range(range_min, range_max);
1699 } 1709 }
1700 1710
1701 } // namespace gfx 1711 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698