Index: ui/gfx/render_text.cc |
diff --git a/ui/gfx/render_text.cc b/ui/gfx/render_text.cc |
index fc8bad161ba30d97809619e18a56736a8315985c..ec103a4c42b691f1527f27d62bd4c92cd61ea9bf 100644 |
--- a/ui/gfx/render_text.cc |
+++ b/ui/gfx/render_text.cc |
@@ -348,14 +348,6 @@ 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; |
cached_bounds_and_offset_valid_ = false; |
@@ -653,6 +645,11 @@ int RenderText::GetContentWidth() { |
return GetStringSize().width() + (cursor_enabled_ ? 1 : 0); |
} |
+int RenderText::GetBaseline() { |
+ UpdateCachedBoundsAndOffset(); |
+ return baseline_; |
+} |
+ |
void RenderText::Draw(Canvas* canvas) { |
EnsureLayout(); |
@@ -784,7 +781,6 @@ void RenderText::SetTextShadows(const ShadowValues& shadows) { |
RenderText::RenderText() |
: horizontal_alignment_(base::i18n::IsRTL() ? ALIGN_RIGHT : ALIGN_LEFT), |
- vertical_alignment_(ALIGN_VCENTER), |
directionality_mode_(DIRECTIONALITY_FROM_TEXT), |
text_direction_(base::i18n::UNKNOWN_DIRECTION), |
cursor_enabled_(true), |
@@ -969,11 +965,7 @@ Vector2d RenderText::GetAlignmentOffset(size_t line_number) { |
if (horizontal_alignment_ == ALIGN_CENTER) |
offset.set_x(offset.x() / 2); |
} |
- if (vertical_alignment_ != ALIGN_TOP) { |
- offset.set_y(display_rect().height() - GetStringSize().height()); |
- if (vertical_alignment_ == ALIGN_VCENTER) |
- offset.set_y(offset.y() / 2); |
- } |
+ offset.set_y(baseline_ - GetBaselineOfTextLayout()); |
return offset; |
} |
@@ -1130,6 +1122,26 @@ void RenderText::UpdateCachedBoundsAndOffset() { |
Vector2d delta_offset(delta_x, 0); |
display_offset_ += delta_offset; |
cursor_bounds_ += delta_offset; |
+ |
+ // Determine the baseline so the text is placed at vertically center. |
+ const int display_height = display_rect_.height(); |
+ const int font_height = font_list().GetHeight(); |
+ // Lower and upper bound of baseline shift as we try to show as much area of |
+ // text as possible. In particular case of |display_height| == |font_height|, |
+ // we do not want to shift the baseline. |
+ int min_shift; |
+ int max_shift; |
+ if (display_height < font_height) { |
+ min_shift = display_height - font_height; |
+ max_shift = font_height - display_height; |
+ } else { |
+ min_shift = 0; |
+ max_shift = display_height - font_height; |
+ } |
+ const int baseline_shift = |
+ std::max(min_shift, std::min(max_shift, |
+ (display_height - font_list().GetCapHeight()) / 2)); |
+ baseline_ = font_list().GetBaseline() + baseline_shift; |
} |
void RenderText::DrawSelection(Canvas* canvas) { |