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

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

Issue 2639493002: MacViews: Enable word lookup for selectable views::Labels and multi-line text. (Closed)
Patch Set: Correct baseline point logic. Add test. 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
« no previous file with comments | « ui/gfx/render_text.h ('k') | ui/gfx/render_text_harfbuzz.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 1013 matching lines...) Expand 10 before | Expand all | Expand 10 after
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::GetDecoratedWordAtPoint(const Point& point,
1032 DecoratedText* decorated_word, 1032 DecoratedText* decorated_word,
1033 Point* baseline_point) { 1033 Point* baseline_point) {
1034 // FindCursorPosition doesn't currently support multiline. See 1034 if (obscured())
1035 // http://crbug.com/650120.
1036 if (multiline() || obscured())
1037 return false; 1035 return false;
1038 1036
1039 // Note: FindCursorPosition will trigger a layout via EnsureLayout. 1037 EnsureLayout();
1040 const SelectionModel model_at_point = FindCursorPosition(point); 1038 const SelectionModel model_at_point = FindCursorPosition(point);
1041 const size_t word_index = 1039 const size_t word_index =
1042 GetNearestWordStartBoundary(model_at_point.caret_pos()); 1040 GetNearestWordStartBoundary(model_at_point.caret_pos());
1043 if (word_index >= text().length()) 1041 if (word_index >= text().length())
1044 return false; 1042 return false;
1045 1043
1046 const Range word_range = ExpandRangeToWordBoundary(Range(word_index)); 1044 const Range word_range = ExpandRangeToWordBoundary(Range(word_index));
1047 DCHECK(!word_range.is_reversed()); 1045 DCHECK(!word_range.is_reversed());
1048 DCHECK(!word_range.is_empty()); 1046 DCHECK(!word_range.is_empty());
1049 1047
1050 const std::vector<Rect> word_bounds = GetSubstringBounds(word_range); 1048 const std::vector<Rect> word_bounds = GetSubstringBounds(word_range);
1051 if (word_bounds.empty() || 1049 if (word_bounds.empty() ||
1052 !GetDecoratedTextForRange(word_range, decorated_word)) { 1050 !GetDecoratedTextForRange(word_range, decorated_word)) {
1053 return false; 1051 return false;
1054 } 1052 }
1055 1053
1056 // Retrieve the baseline origin of the left-most glyph. 1054 // Retrieve the baseline origin of the left-most glyph.
1057 const auto left_rect = std::min_element( 1055 const auto left_rect = std::min_element(
1058 word_bounds.begin(), word_bounds.end(), 1056 word_bounds.begin(), word_bounds.end(),
1059 [](const Rect& lhs, const Rect& rhs) { return lhs.x() < rhs.x(); }); 1057 [](const Rect& lhs, const Rect& rhs) { return lhs.x() < rhs.x(); });
1060 *baseline_point = left_rect->origin() + Vector2d(0, GetDisplayTextBaseline()); 1058 const int line_index = GetLineContainingYCoord(left_rect->CenterPoint().y() -
1059 GetLineOffset(0).y());
karandeepb 2017/01/19 04:38:36 The logic in the previous patchset was indeed inco
1060 if (line_index == -1 || line_index == static_cast<int>(lines().size()))
1061 return false;
1062 *baseline_point =
1063 left_rect->origin() + Vector2d(0, lines()[line_index].baseline);
1061 return true; 1064 return true;
1062 } 1065 }
1063 1066
1064 base::string16 RenderText::GetTextFromRange(const Range& range) const { 1067 base::string16 RenderText::GetTextFromRange(const Range& range) const {
1065 if (range.IsValid() && range.GetMin() < text().length()) 1068 if (range.IsValid() && range.GetMin() < text().length())
1066 return text().substr(range.GetMin(), range.length()); 1069 return text().substr(range.GetMin(), range.length());
1067 return base::string16(); 1070 return base::string16();
1068 } 1071 }
1069 1072
1070 RenderText::RenderText() 1073 RenderText::RenderText()
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
1380 1383
1381 void RenderText::UpdateStyleLengths() { 1384 void RenderText::UpdateStyleLengths() {
1382 const size_t text_length = text_.length(); 1385 const size_t text_length = text_.length();
1383 colors_.SetMax(text_length); 1386 colors_.SetMax(text_length);
1384 baselines_.SetMax(text_length); 1387 baselines_.SetMax(text_length);
1385 weights_.SetMax(text_length); 1388 weights_.SetMax(text_length);
1386 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) 1389 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style)
1387 styles_[style].SetMax(text_length); 1390 styles_[style].SetMax(text_length);
1388 } 1391 }
1389 1392
1393 int RenderText::GetLineContainingYCoord(float text_y) {
1394 if (text_y < 0)
1395 return -1;
1396
1397 for (size_t i = 0; i < lines().size(); i++) {
1398 const internal::Line& line = lines()[i];
1399
1400 if (text_y <= line.size.height())
1401 return i;
1402 text_y -= line.size.height();
1403 }
1404
1405 return lines().size();
1406 }
1407
1408 int RenderText::GetLineSegmentContainingXCoord(const internal::Line& line,
1409 float line_x,
1410 float* offset_relative_segment) {
1411 DCHECK(offset_relative_segment);
1412
1413 *offset_relative_segment = 0;
1414 if (line_x < 0)
1415 return -1;
1416 for (size_t i = 0; i < line.segments.size(); i++) {
1417 const internal::LineSegment& segment = line.segments[i];
1418
1419 // segment.x_range is not used because it is in text space.
1420 if (line_x < segment.width()) {
1421 *offset_relative_segment = line_x;
1422 return i;
1423 }
1424 line_x -= segment.width();
1425 }
1426 return line.segments.size();
1427 }
1428
1390 // static 1429 // static
1391 bool RenderText::RangeContainsCaret(const Range& range, 1430 bool RenderText::RangeContainsCaret(const Range& range,
1392 size_t caret_pos, 1431 size_t caret_pos,
1393 LogicalCursorDirection caret_affinity) { 1432 LogicalCursorDirection caret_affinity) {
1394 // NB: exploits unsigned wraparound (WG14/N1124 section 6.2.5 paragraph 9). 1433 // NB: exploits unsigned wraparound (WG14/N1124 section 6.2.5 paragraph 9).
1395 size_t adjacent = (caret_affinity == CURSOR_BACKWARD) ? 1434 size_t adjacent = (caret_affinity == CURSOR_BACKWARD) ?
1396 caret_pos - 1 : caret_pos + 1; 1435 caret_pos - 1 : caret_pos + 1;
1397 return range.Contains(Range(caret_pos, adjacent)); 1436 return range.Contains(Range(caret_pos, adjacent));
1398 } 1437 }
1399 1438
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
1692 1731
1693 for (; range_max < length; ++range_max) 1732 for (; range_max < length; ++range_max)
1694 if (iter.IsEndOfWord(range_max) || iter.IsStartOfWord(range_max)) 1733 if (iter.IsEndOfWord(range_max) || iter.IsStartOfWord(range_max))
1695 break; 1734 break;
1696 1735
1697 return range.is_reversed() ? Range(range_max, range_min) 1736 return range.is_reversed() ? Range(range_max, range_min)
1698 : Range(range_min, range_max); 1737 : Range(range_min, range_max);
1699 } 1738 }
1700 1739
1701 } // namespace gfx 1740 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/render_text.h ('k') | ui/gfx/render_text_harfbuzz.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698