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

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

Issue 384953003: RenderText: handle center-aligned text in UpdateCachedBoundsAndOffset() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added explanation for display offset range calculations Created 6 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | ui/gfx/render_text_unittest.cc » ('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 <algorithm> 7 #include <algorithm>
8 #include <climits> 8 #include <climits>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 882 matching lines...) Expand 10 before | Expand all | Expand 10 after
893 sel.is_reversed() ? CURSOR_BACKWARD : CURSOR_FORWARD); 893 sel.is_reversed() ? CURSOR_BACKWARD : CURSOR_FORWARD);
894 } 894 }
895 895
896 const Vector2d& RenderText::GetUpdatedDisplayOffset() { 896 const Vector2d& RenderText::GetUpdatedDisplayOffset() {
897 UpdateCachedBoundsAndOffset(); 897 UpdateCachedBoundsAndOffset();
898 return display_offset_; 898 return display_offset_;
899 } 899 }
900 900
901 void RenderText::SetDisplayOffset(int horizontal_offset) { 901 void RenderText::SetDisplayOffset(int horizontal_offset) {
902 const int extra_content = GetContentWidth() - display_rect_.width(); 902 const int extra_content = GetContentWidth() - display_rect_.width();
903 const int cursor_width = cursor_enabled_ ? 1 : 0;
903 904
904 int min_offset = 0; 905 int min_offset = 0;
905 int max_offset = 0; 906 int max_offset = 0;
906 if (extra_content > 0) { 907 if (extra_content > 0) {
907 switch (horizontal_alignment_) { 908 switch (GetCurrentHorizontalAlignment()) {
908 case ALIGN_LEFT: 909 case ALIGN_LEFT:
909 min_offset = -extra_content; 910 min_offset = -extra_content;
910 break; 911 break;
911 case ALIGN_RIGHT: 912 case ALIGN_RIGHT:
912 max_offset = extra_content; 913 max_offset = extra_content;
913 break; 914 break;
914 case ALIGN_CENTER: 915 case ALIGN_CENTER:
915 min_offset = -extra_content / 2; 916 // The extra space reserved for cursor at the end of the text is ignored
916 max_offset = extra_content / 2; 917 // when centering text. So, to calculate the valid range for offset, we
918 // exclude that extra space, calculate the range, and add it back to the
919 // range (if cursor is enabled).
920 min_offset = -(extra_content - cursor_width + 1) / 2 - cursor_width;
921 max_offset = (extra_content - cursor_width) / 2;
917 break; 922 break;
918 default: 923 default:
919 break; 924 break;
920 } 925 }
921 } 926 }
922 if (horizontal_offset < min_offset) 927 if (horizontal_offset < min_offset)
923 horizontal_offset = min_offset; 928 horizontal_offset = min_offset;
924 else if (horizontal_offset > max_offset) 929 else if (horizontal_offset > max_offset)
925 horizontal_offset = max_offset; 930 horizontal_offset = max_offset;
926 931
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after
1393 username = Elide(username, available_width, ELIDE_TAIL); 1398 username = Elide(username, available_width, ELIDE_TAIL);
1394 return username + kAtSignUTF16 + domain; 1399 return username + kAtSignUTF16 + domain;
1395 } 1400 }
1396 1401
1397 void RenderText::UpdateCachedBoundsAndOffset() { 1402 void RenderText::UpdateCachedBoundsAndOffset() {
1398 if (cached_bounds_and_offset_valid_) 1403 if (cached_bounds_and_offset_valid_)
1399 return; 1404 return;
1400 1405
1401 // TODO(ckocagil): Add support for scrolling multiline text. 1406 // TODO(ckocagil): Add support for scrolling multiline text.
1402 1407
1403 // First, set the valid flag true to calculate the current cursor bounds using 1408 int delta_x = 0;
1404 // the stale |display_offset_|. Applying |delta_offset| at the end of this 1409
1405 // function will set |cursor_bounds_| and |display_offset_| to correct values. 1410 if (cursor_enabled()) {
1406 cached_bounds_and_offset_valid_ = true; 1411 // When cursor is enabled, ensure it is visible. For this, set the valid
1407 if (cursor_enabled()) 1412 // flag true and calculate the current cursor bounds using the stale
1413 // |display_offset_|. Then calculate the change in offset needed to move the
1414 // cursor into the visible area.
1415 cached_bounds_and_offset_valid_ = true;
1408 cursor_bounds_ = GetCursorBounds(selection_model_, insert_mode_); 1416 cursor_bounds_ = GetCursorBounds(selection_model_, insert_mode_);
1409 1417
1410 // Update |display_offset_| to ensure the current cursor is visible. 1418 // TODO(bidi): Show RTL glyphs at the cursor position for ALIGN_LEFT, etc.
1411 const int display_width = display_rect_.width(); 1419 if (cursor_bounds_.right() > display_rect_.right())
1412 const int content_width = GetContentWidth(); 1420 delta_x = display_rect_.right() - cursor_bounds_.right();
1413 1421 else if (cursor_bounds_.x() < display_rect_.x())
1414 int delta_x = 0; 1422 delta_x = display_rect_.x() - cursor_bounds_.x();
1415 if (content_width <= display_width || !cursor_enabled()) {
1416 // Don't pan if the text fits in the display width or when the cursor is
1417 // disabled.
1418 delta_x = -display_offset_.x();
1419 } else if (cursor_bounds_.right() > display_rect_.right()) {
1420 // TODO(xji): when the character overflow is a RTL character, currently, if
1421 // we pan cursor at the rightmost position, the entered RTL character is not
1422 // displayed. Should pan cursor to show the last logical characters.
1423 //
1424 // Pan to show the cursor when it overflows to the right.
1425 delta_x = display_rect_.right() - cursor_bounds_.right();
1426 } else if (cursor_bounds_.x() < display_rect_.x()) {
1427 // TODO(xji): have similar problem as above when overflow character is a
1428 // LTR character.
1429 //
1430 // Pan to show the cursor when it overflows to the left.
1431 delta_x = display_rect_.x() - cursor_bounds_.x();
1432 } else if (display_offset_.x() != 0) {
1433 // Reduce the pan offset to show additional overflow text when the display
1434 // width increases.
1435 HorizontalAlignment horizontal_alignment = GetCurrentHorizontalAlignment();
1436 const int negate_rtl = horizontal_alignment == ALIGN_RIGHT ? -1 : 1;
1437 const int offset = negate_rtl * display_offset_.x();
1438 if (display_width > (content_width + offset)) {
1439 delta_x = negate_rtl * (display_width - (content_width + offset));
1440 }
1441 } 1423 }
1442 1424
1443 Vector2d delta_offset(delta_x, 0); 1425 SetDisplayOffset(display_offset_.x() + delta_x);
1444 display_offset_ += delta_offset;
1445 cursor_bounds_ += delta_offset;
1446 } 1426 }
1447 1427
1448 void RenderText::DrawSelection(Canvas* canvas) { 1428 void RenderText::DrawSelection(Canvas* canvas) {
1449 const std::vector<Rect> sel = GetSubstringBounds(selection()); 1429 const std::vector<Rect> sel = GetSubstringBounds(selection());
1450 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) 1430 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i)
1451 canvas->FillRect(*i, selection_background_focused_color_); 1431 canvas->FillRect(*i, selection_background_focused_color_);
1452 } 1432 }
1453 1433
1454 } // namespace gfx 1434 } // namespace gfx
OLDNEW
« no previous file with comments | « no previous file | ui/gfx/render_text_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698