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

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

Issue 25039002: Always aligns text at vertically center (Textfield, Label). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Worked on pkasting's minor comment. Created 7 years, 2 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
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 8
9 #include "base/i18n/break_iterator.h" 9 #include "base/i18n/break_iterator.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 } 341 }
342 342
343 void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) { 343 void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) {
344 if (horizontal_alignment_ != alignment) { 344 if (horizontal_alignment_ != alignment) {
345 horizontal_alignment_ = alignment; 345 horizontal_alignment_ = alignment;
346 display_offset_ = Vector2d(); 346 display_offset_ = Vector2d();
347 cached_bounds_and_offset_valid_ = false; 347 cached_bounds_and_offset_valid_ = false;
348 } 348 }
349 } 349 }
350 350
351 void RenderText::SetVerticalAlignment(VerticalAlignment alignment) {
352 if (vertical_alignment_ != alignment) {
353 vertical_alignment_ = alignment;
354 display_offset_ = Vector2d();
355 cached_bounds_and_offset_valid_ = false;
356 }
357 }
358
359 void RenderText::SetFontList(const FontList& font_list) { 351 void RenderText::SetFontList(const FontList& font_list) {
360 font_list_ = font_list; 352 font_list_ = font_list;
361 cached_bounds_and_offset_valid_ = false; 353 cached_bounds_and_offset_valid_ = false;
362 ResetLayout(); 354 ResetLayout();
363 } 355 }
364 356
365 void RenderText::SetFont(const Font& font) { 357 void RenderText::SetFont(const Font& font) {
366 SetFontList(FontList(font)); 358 SetFontList(FontList(font));
367 } 359 }
368 360
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 638
647 SizeF RenderText::GetStringSizeF() { 639 SizeF RenderText::GetStringSizeF() {
648 const Size size = GetStringSize(); 640 const Size size = GetStringSize();
649 return SizeF(size.width(), size.height()); 641 return SizeF(size.width(), size.height());
650 } 642 }
651 643
652 int RenderText::GetContentWidth() { 644 int RenderText::GetContentWidth() {
653 return GetStringSize().width() + (cursor_enabled_ ? 1 : 0); 645 return GetStringSize().width() + (cursor_enabled_ ? 1 : 0);
654 } 646 }
655 647
648 int RenderText::GetBaseline() {
649 UpdateCachedBoundsAndOffset();
650 return baseline_;
651 }
652
656 void RenderText::Draw(Canvas* canvas) { 653 void RenderText::Draw(Canvas* canvas) {
657 EnsureLayout(); 654 EnsureLayout();
658 655
659 if (clip_to_display_rect()) { 656 if (clip_to_display_rect()) {
660 Rect clip_rect(display_rect()); 657 Rect clip_rect(display_rect());
661 clip_rect.Inset(ShadowValue::GetMargin(text_shadows_)); 658 clip_rect.Inset(ShadowValue::GetMargin(text_shadows_));
662 659
663 canvas->Save(); 660 canvas->Save();
664 canvas->ClipRect(clip_rect); 661 canvas->ClipRect(clip_rect);
665 } 662 }
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
777 return SelectionModel(sel.start(), 774 return SelectionModel(sel.start(),
778 sel.is_reversed() ? CURSOR_BACKWARD : CURSOR_FORWARD); 775 sel.is_reversed() ? CURSOR_BACKWARD : CURSOR_FORWARD);
779 } 776 }
780 777
781 void RenderText::SetTextShadows(const ShadowValues& shadows) { 778 void RenderText::SetTextShadows(const ShadowValues& shadows) {
782 text_shadows_ = shadows; 779 text_shadows_ = shadows;
783 } 780 }
784 781
785 RenderText::RenderText() 782 RenderText::RenderText()
786 : horizontal_alignment_(base::i18n::IsRTL() ? ALIGN_RIGHT : ALIGN_LEFT), 783 : horizontal_alignment_(base::i18n::IsRTL() ? ALIGN_RIGHT : ALIGN_LEFT),
787 vertical_alignment_(ALIGN_VCENTER),
788 directionality_mode_(DIRECTIONALITY_FROM_TEXT), 784 directionality_mode_(DIRECTIONALITY_FROM_TEXT),
789 text_direction_(base::i18n::UNKNOWN_DIRECTION), 785 text_direction_(base::i18n::UNKNOWN_DIRECTION),
790 cursor_enabled_(true), 786 cursor_enabled_(true),
791 cursor_visible_(false), 787 cursor_visible_(false),
792 insert_mode_(true), 788 insert_mode_(true),
793 cursor_color_(kDefaultColor), 789 cursor_color_(kDefaultColor),
794 selection_color_(kDefaultColor), 790 selection_color_(kDefaultColor),
795 selection_background_focused_color_(kDefaultSelectionBackgroundColor), 791 selection_background_focused_color_(kDefaultSelectionBackgroundColor),
796 focused_(false), 792 focused_(false),
797 composition_range_(Range::InvalidRange()), 793 composition_range_(Range::InvalidRange()),
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
962 #if defined(OS_WIN) 958 #if defined(OS_WIN)
963 const int width = lines_[line_number].size.width() + 959 const int width = lines_[line_number].size.width() +
964 (cursor_enabled_ ? 1 : 0); 960 (cursor_enabled_ ? 1 : 0);
965 #else 961 #else
966 const int width = GetContentWidth(); 962 const int width = GetContentWidth();
967 #endif 963 #endif
968 offset.set_x(display_rect().width() - width); 964 offset.set_x(display_rect().width() - width);
969 if (horizontal_alignment_ == ALIGN_CENTER) 965 if (horizontal_alignment_ == ALIGN_CENTER)
970 offset.set_x(offset.x() / 2); 966 offset.set_x(offset.x() / 2);
971 } 967 }
972 if (vertical_alignment_ != ALIGN_TOP) { 968 offset.set_y(GetBaseline() - GetBaselineOfTextLayout());
973 offset.set_y(display_rect().height() - GetStringSize().height());
974 if (vertical_alignment_ == ALIGN_VCENTER)
975 offset.set_y(offset.y() / 2);
976 }
977 return offset; 969 return offset;
978 } 970 }
979 971
980 void RenderText::ApplyFadeEffects(internal::SkiaTextRenderer* renderer) { 972 void RenderText::ApplyFadeEffects(internal::SkiaTextRenderer* renderer) {
981 if (multiline() || (!fade_head() && !fade_tail())) 973 if (multiline() || (!fade_head() && !fade_tail()))
982 return; 974 return;
983 975
984 const int display_width = display_rect().width(); 976 const int display_width = display_rect().width();
985 977
986 // If the text fits as-is, no need to fade. 978 // If the text fits as-is, no need to fade.
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
1123 const int negate_rtl = horizontal_alignment_ == ALIGN_RIGHT ? -1 : 1; 1115 const int negate_rtl = horizontal_alignment_ == ALIGN_RIGHT ? -1 : 1;
1124 const int offset = negate_rtl * display_offset_.x(); 1116 const int offset = negate_rtl * display_offset_.x();
1125 if (display_width > (content_width + offset)) { 1117 if (display_width > (content_width + offset)) {
1126 delta_x = negate_rtl * (display_width - (content_width + offset)); 1118 delta_x = negate_rtl * (display_width - (content_width + offset));
1127 } 1119 }
1128 } 1120 }
1129 1121
1130 Vector2d delta_offset(delta_x, 0); 1122 Vector2d delta_offset(delta_x, 0);
1131 display_offset_ += delta_offset; 1123 display_offset_ += delta_offset;
1132 cursor_bounds_ += delta_offset; 1124 cursor_bounds_ += delta_offset;
1125
1126 // Determine the baseline so the text is placed at vertically center.
msw 2013/10/23 01:18:00 nit: s/is placed at vertically center/appears vert
Yuki 2013/10/24 14:32:54 Done.
1127 const int display_height = display_rect_.height();
1128 const int font_height = font_list().GetHeight();
1129 // Lower and upper bound of baseline shift as we try to show as much area of
1130 // text as possible. In particular case of |display_height| == |font_height|,
1131 // we do not want to shift the baseline.
1132 int min_shift;
msw 2013/10/23 01:18:00 nit: explicitly init min_shift and max_shift to 0.
Yuki 2013/10/24 14:32:54 The same reason here. I think it's better to leave
Peter Kasting 2013/10/24 20:12:05 How about this simpler version: int min_shift =
Yuki 2013/10/25 15:21:09 Done.
1133 int max_shift;
1134 if (display_height < font_height) {
1135 min_shift = display_height - font_height;
1136 max_shift = font_height - display_height;
1137 } else {
1138 min_shift = 0;
1139 max_shift = display_height - font_height;
1140 }
1141 const int baseline_shift =
1142 std::max(min_shift, std::min(max_shift,
1143 (display_height - font_list().GetCapHeight()) / 2 -
1144 (font_list().GetBaseline() - font_list().GetCapHeight())));
Alexei Svitkine (slow) 2013/10/22 17:25:30 Nit: Can you cache font_list's GetCapHeight() and
Yuki 2013/10/24 14:32:54 Done.
1145 baseline_ = font_list().GetBaseline() + baseline_shift;
Alexei Svitkine (slow) 2013/10/22 17:25:30 Nit: Can you make this block a free-standing funct
msw 2013/10/23 01:18:00 This only needs to be invalidated on changes to th
Yuki 2013/10/24 14:32:54 Done.
Yuki 2013/10/24 14:32:54 Done.
1133 } 1146 }
1134 1147
1135 void RenderText::DrawSelection(Canvas* canvas) { 1148 void RenderText::DrawSelection(Canvas* canvas) {
1136 const std::vector<Rect> sel = GetSubstringBounds(selection()); 1149 const std::vector<Rect> sel = GetSubstringBounds(selection());
1137 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) 1150 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i)
1138 canvas->FillRect(*i, selection_background_focused_color_); 1151 canvas->FillRect(*i, selection_background_focused_color_);
1139 } 1152 }
1140 1153
1141 } // namespace gfx 1154 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698