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

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

Issue 1013543006: [RenderText] Adding vertical alignment options (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge from master Created 5 years, 9 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 <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 466 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 } 477 }
478 478
479 void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) { 479 void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) {
480 if (horizontal_alignment_ != alignment) { 480 if (horizontal_alignment_ != alignment) {
481 horizontal_alignment_ = alignment; 481 horizontal_alignment_ = alignment;
482 display_offset_ = Vector2d(); 482 display_offset_ = Vector2d();
483 cached_bounds_and_offset_valid_ = false; 483 cached_bounds_and_offset_valid_ = false;
484 } 484 }
485 } 485 }
486 486
487 void RenderText::SetVerticalAlignment(VerticalAlignment alignment) {
488 if (vertical_alignment_ != alignment) {
489 vertical_alignment_ = alignment;
490 display_offset_ = Vector2d();
491 cached_bounds_and_offset_valid_ = false;
492 }
493 }
494
487 void RenderText::SetFontList(const FontList& font_list) { 495 void RenderText::SetFontList(const FontList& font_list) {
488 font_list_ = font_list; 496 font_list_ = font_list;
489 const int font_style = font_list.GetFontStyle(); 497 const int font_style = font_list.GetFontStyle();
490 SetStyle(BOLD, (font_style & gfx::Font::BOLD) != 0); 498 SetStyle(BOLD, (font_style & gfx::Font::BOLD) != 0);
491 SetStyle(ITALIC, (font_style & gfx::Font::ITALIC) != 0); 499 SetStyle(ITALIC, (font_style & gfx::Font::ITALIC) != 0);
492 SetStyle(UNDERLINE, (font_style & gfx::Font::UNDERLINE) != 0); 500 SetStyle(UNDERLINE, (font_style & gfx::Font::UNDERLINE) != 0);
493 baseline_ = kInvalidBaseline; 501 baseline_ = kInvalidBaseline;
494 cached_bounds_and_offset_valid_ = false; 502 cached_bounds_and_offset_valid_ = false;
495 OnLayoutTextAttributeChanged(false); 503 OnLayoutTextAttributeChanged(false);
496 } 504 }
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after
948 if (!multiline()) 956 if (!multiline())
949 offset.Add(GetUpdatedDisplayOffset()); 957 offset.Add(GetUpdatedDisplayOffset());
950 else 958 else
951 offset.Add(Vector2d(0, lines_[line_number].preceding_heights)); 959 offset.Add(Vector2d(0, lines_[line_number].preceding_heights));
952 offset.Add(GetAlignmentOffset(line_number)); 960 offset.Add(GetAlignmentOffset(line_number));
953 return offset; 961 return offset;
954 } 962 }
955 963
956 RenderText::RenderText() 964 RenderText::RenderText()
957 : horizontal_alignment_(base::i18n::IsRTL() ? ALIGN_RIGHT : ALIGN_LEFT), 965 : horizontal_alignment_(base::i18n::IsRTL() ? ALIGN_RIGHT : ALIGN_LEFT),
966 vertical_alignment_(VALIGN_MIDDLE),
958 directionality_mode_(DIRECTIONALITY_FROM_TEXT), 967 directionality_mode_(DIRECTIONALITY_FROM_TEXT),
959 text_direction_(base::i18n::UNKNOWN_DIRECTION), 968 text_direction_(base::i18n::UNKNOWN_DIRECTION),
960 cursor_enabled_(true), 969 cursor_enabled_(true),
961 cursor_visible_(false), 970 cursor_visible_(false),
962 insert_mode_(true), 971 insert_mode_(true),
963 cursor_color_(kDefaultColor), 972 cursor_color_(kDefaultColor),
964 selection_color_(kDefaultColor), 973 selection_color_(kDefaultColor),
965 selection_background_focused_color_(kDefaultSelectionBackgroundColor), 974 selection_background_focused_color_(kDefaultSelectionBackgroundColor),
966 focused_(false), 975 focused_(false),
967 composition_range_(Range::InvalidRange()), 976 composition_range_(Range::InvalidRange()),
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
1148 const int width = multiline_ ? 1157 const int width = multiline_ ?
1149 std::ceil(lines_[line_number].size.width()) + 1158 std::ceil(lines_[line_number].size.width()) +
1150 (cursor_enabled_ ? 1 : 0) : 1159 (cursor_enabled_ ? 1 : 0) :
1151 GetContentWidth(); 1160 GetContentWidth();
1152 offset.set_x(display_rect().width() - width); 1161 offset.set_x(display_rect().width() - width);
1153 // Put any extra margin pixel on the left to match legacy behavior. 1162 // Put any extra margin pixel on the left to match legacy behavior.
1154 if (horizontal_alignment == ALIGN_CENTER) 1163 if (horizontal_alignment == ALIGN_CENTER)
1155 offset.set_x((offset.x() + 1) / 2); 1164 offset.set_x((offset.x() + 1) / 2);
1156 } 1165 }
1157 1166
1158 // Vertically center the text. 1167 // Vertically align the text.
1159 if (multiline_) { 1168 if (multiline_) {
1160 const int text_height = lines_.back().preceding_heights + 1169 const int text_height =
1161 lines_.back().size.height(); 1170 lines_.back().preceding_heights + lines_.back().size.height();
1162 offset.set_y((display_rect_.height() - text_height) / 2); 1171 if (vertical_alignment_ == VALIGN_TOP) {
1172 offset.set_y(lines_.back().preceding_heights);
msw 2015/03/26 22:55:44 I don't think this is correct... Shouldn't it just
dschuyler 2015/03/27 21:20:34 I'm not sure. If it's a single line or if it's do
msw 2015/03/30 19:19:12 This should probably be 0 (as in your latest patch
dschuyler 2015/03/31 18:38:14 Done.
1173 } else if (vertical_alignment_ == VALIGN_BOTTOM) {
1174 offset.set_y(display_rect_.height() - text_height);
1175 } else {
1176 // Vertically center the text.
msw 2015/03/26 22:55:44 nit: change this comment to DCHECK_EQ(VALIGN_MIDDL
dschuyler 2015/03/27 21:20:34 Done.
1177 offset.set_y((display_rect_.height() - text_height) / 2);
1178 }
1163 } else { 1179 } else {
1164 offset.set_y(GetBaseline() - GetDisplayTextBaseline()); 1180 if (vertical_alignment_ == VALIGN_TOP) {
1181 offset.set_y(0);
1182 } else if (vertical_alignment_ == VALIGN_BOTTOM) {
1183 offset.set_y(display_rect_.height() - font_list_.GetHeight());
1184 } else {
1185 // Vertically center the text.
msw 2015/03/26 22:55:45 ditto nit: change this comment to DCHECK_EQ(VALIGN
dschuyler 2015/03/27 21:20:34 Done.
1186 offset.set_y(GetBaseline() - GetDisplayTextBaseline());
1187 }
1165 } 1188 }
1166
1167 return offset; 1189 return offset;
1168 } 1190 }
1169 1191
1170 void RenderText::ApplyFadeEffects(internal::SkiaTextRenderer* renderer) { 1192 void RenderText::ApplyFadeEffects(internal::SkiaTextRenderer* renderer) {
1171 const int width = display_rect().width(); 1193 const int width = display_rect().width();
1172 if (multiline() || elide_behavior_ != FADE_TAIL || GetContentWidth() <= width) 1194 if (multiline() || elide_behavior_ != FADE_TAIL || GetContentWidth() <= width)
1173 return; 1195 return;
1174 1196
1175 const int gradient_width = CalculateFadeGradientWidth(font_list(), width); 1197 const int gradient_width = CalculateFadeGradientWidth(font_list(), width);
1176 if (gradient_width == 0) 1198 if (gradient_width == 0)
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after
1507 1529
1508 SetDisplayOffset(display_offset_.x() + delta_x); 1530 SetDisplayOffset(display_offset_.x() + delta_x);
1509 } 1531 }
1510 1532
1511 void RenderText::DrawSelection(Canvas* canvas) { 1533 void RenderText::DrawSelection(Canvas* canvas) {
1512 for (const Rect& s : GetSubstringBounds(selection())) 1534 for (const Rect& s : GetSubstringBounds(selection()))
1513 canvas->FillRect(s, selection_background_focused_color_); 1535 canvas->FillRect(s, selection_background_focused_color_);
1514 } 1536 }
1515 1537
1516 } // namespace gfx 1538 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698