Chromium Code Reviews| Index: ui/gfx/render_text.cc |
| diff --git a/ui/gfx/render_text.cc b/ui/gfx/render_text.cc |
| index fc8bad161ba30d97809619e18a56736a8315985c..37438ab44314e6be07a4fdbd52e2fb3054c37476 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(GetBaseline() - GetBaselineOfTextLayout()); |
| return offset; |
| } |
| @@ -1130,6 +1122,27 @@ 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. |
|
msw
2013/10/23 01:18:00
nit: s/is placed at vertically center/appears vert
Yuki
2013/10/24 14:32:54
Done.
|
| + 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; |
|
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.
|
| + 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 - |
| + (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.
|
| + 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.
|
| } |
| void RenderText::DrawSelection(Canvas* canvas) { |