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

Unified 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: Removed pixel tests on vertical alignment 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 side-by-side diff with in-line comments
Download patch
Index: ui/gfx/render_text.cc
diff --git a/ui/gfx/render_text.cc b/ui/gfx/render_text.cc
index 1dc111d474bc1cfa16b82821da62e8578d429870..f145debe2aa9c85ef4776e2249a784d92340b41a 100644
--- a/ui/gfx/render_text.cc
+++ b/ui/gfx/render_text.cc
@@ -484,6 +484,14 @@ void RenderText::SetHorizontalAlignment(HorizontalAlignment alignment) {
}
}
+void RenderText::SetVerticalAlignment(VerticalAlignment alignment) {
+ if (vertical_alignment_ != alignment) {
+ vertical_alignment_ = alignment;
+ display_offset_ = Vector2d();
+ cached_bounds_and_offset_valid_ = false;
+ }
+}
+
void RenderText::SetFontList(const FontList& font_list) {
font_list_ = font_list;
const int font_style = font_list.GetFontStyle();
@@ -962,6 +970,7 @@ Vector2d RenderText::GetLineOffset(size_t line_number) {
RenderText::RenderText()
: horizontal_alignment_(base::i18n::IsRTL() ? ALIGN_RIGHT : ALIGN_LEFT),
+ vertical_alignment_(VALIGN_MIDDLE),
directionality_mode_(DIRECTIONALITY_FROM_TEXT),
text_direction_(base::i18n::UNKNOWN_DIRECTION),
cursor_enabled_(true),
@@ -1163,15 +1172,28 @@ Vector2d RenderText::GetAlignmentOffset(size_t line_number) {
offset.set_x((offset.x() + 1) / 2);
}
- // Vertically center the text.
+ // Vertically align the text.
if (multiline_) {
const int text_height = lines_.back().preceding_heights +
- lines_.back().size.height();
- offset.set_y((display_rect_.height() - text_height) / 2);
+ std::ceil(lines_.back().size.height());
+ if (vertical_alignment_ == VALIGN_TOP) {
+ offset.set_y(0);
+ } else if (vertical_alignment_ == VALIGN_BOTTOM) {
+ offset.set_y(display_rect_.height() - text_height);
+ } else {
+ DCHECK_EQ(VALIGN_MIDDLE, vertical_alignment_);
+ offset.set_y((display_rect_.height() - text_height) / 2);
+ }
} else {
- offset.set_y(GetBaseline() - GetDisplayTextBaseline());
+ if (vertical_alignment_ == VALIGN_TOP) {
+ offset.set_y(0);
+ } else if (vertical_alignment_ == VALIGN_BOTTOM) {
+ offset.set_y(display_rect_.height() - font_list_.GetHeight());
+ } else {
+ DCHECK_EQ(VALIGN_MIDDLE, vertical_alignment_);
+ offset.set_y(GetBaseline() - GetDisplayTextBaseline());
+ }
}
-
return offset;
}
@@ -1200,7 +1222,7 @@ void RenderText::ApplyFadeEffects(internal::SkiaTextRenderer* renderer) {
}
Rect text_rect = display_rect();
- text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0);
+ text_rect.Inset(GetAlignmentOffset(0).x(), GetAlignmentOffset(0).y(), 0, 0);
msw 2015/04/01 15:07:17 nit: cache the alignment offset instead of calling
// TODO(msw): Use the actual text colors corresponding to each faded part.
skia::RefPtr<SkShader> shader = CreateFadeShader(

Powered by Google App Engine
This is Rietveld 408576698