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

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: Removed pixel tests on vertical alignment Created 5 years, 8 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 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
955 if (!multiline()) 963 if (!multiline())
956 offset.Add(GetUpdatedDisplayOffset()); 964 offset.Add(GetUpdatedDisplayOffset());
957 else 965 else
958 offset.Add(Vector2d(0, lines_[line_number].preceding_heights)); 966 offset.Add(Vector2d(0, lines_[line_number].preceding_heights));
959 offset.Add(GetAlignmentOffset(line_number)); 967 offset.Add(GetAlignmentOffset(line_number));
960 return offset; 968 return offset;
961 } 969 }
962 970
963 RenderText::RenderText() 971 RenderText::RenderText()
964 : horizontal_alignment_(base::i18n::IsRTL() ? ALIGN_RIGHT : ALIGN_LEFT), 972 : horizontal_alignment_(base::i18n::IsRTL() ? ALIGN_RIGHT : ALIGN_LEFT),
973 vertical_alignment_(VALIGN_MIDDLE),
965 directionality_mode_(DIRECTIONALITY_FROM_TEXT), 974 directionality_mode_(DIRECTIONALITY_FROM_TEXT),
966 text_direction_(base::i18n::UNKNOWN_DIRECTION), 975 text_direction_(base::i18n::UNKNOWN_DIRECTION),
967 cursor_enabled_(true), 976 cursor_enabled_(true),
968 cursor_visible_(false), 977 cursor_visible_(false),
969 insert_mode_(true), 978 insert_mode_(true),
970 cursor_color_(kDefaultColor), 979 cursor_color_(kDefaultColor),
971 selection_color_(kDefaultColor), 980 selection_color_(kDefaultColor),
972 selection_background_focused_color_(kDefaultSelectionBackgroundColor), 981 selection_background_focused_color_(kDefaultSelectionBackgroundColor),
973 focused_(false), 982 focused_(false),
974 composition_range_(Range::InvalidRange()), 983 composition_range_(Range::InvalidRange()),
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
1156 const int width = multiline_ ? 1165 const int width = multiline_ ?
1157 std::ceil(lines_[line_number].size.width()) + 1166 std::ceil(lines_[line_number].size.width()) +
1158 (cursor_enabled_ ? 1 : 0) : 1167 (cursor_enabled_ ? 1 : 0) :
1159 GetContentWidth(); 1168 GetContentWidth();
1160 offset.set_x(display_rect().width() - width); 1169 offset.set_x(display_rect().width() - width);
1161 // Put any extra margin pixel on the left to match legacy behavior. 1170 // Put any extra margin pixel on the left to match legacy behavior.
1162 if (horizontal_alignment == ALIGN_CENTER) 1171 if (horizontal_alignment == ALIGN_CENTER)
1163 offset.set_x((offset.x() + 1) / 2); 1172 offset.set_x((offset.x() + 1) / 2);
1164 } 1173 }
1165 1174
1166 // Vertically center the text. 1175 // Vertically align the text.
1167 if (multiline_) { 1176 if (multiline_) {
1168 const int text_height = lines_.back().preceding_heights + 1177 const int text_height = lines_.back().preceding_heights +
1169 lines_.back().size.height(); 1178 std::ceil(lines_.back().size.height());
1170 offset.set_y((display_rect_.height() - text_height) / 2); 1179 if (vertical_alignment_ == VALIGN_TOP) {
1180 offset.set_y(0);
1181 } else if (vertical_alignment_ == VALIGN_BOTTOM) {
1182 offset.set_y(display_rect_.height() - text_height);
1183 } else {
1184 DCHECK_EQ(VALIGN_MIDDLE, vertical_alignment_);
1185 offset.set_y((display_rect_.height() - text_height) / 2);
1186 }
1171 } else { 1187 } else {
1172 offset.set_y(GetBaseline() - GetDisplayTextBaseline()); 1188 if (vertical_alignment_ == VALIGN_TOP) {
1189 offset.set_y(0);
1190 } else if (vertical_alignment_ == VALIGN_BOTTOM) {
1191 offset.set_y(display_rect_.height() - font_list_.GetHeight());
1192 } else {
1193 DCHECK_EQ(VALIGN_MIDDLE, vertical_alignment_);
1194 offset.set_y(GetBaseline() - GetDisplayTextBaseline());
1195 }
1173 } 1196 }
1174
1175 return offset; 1197 return offset;
1176 } 1198 }
1177 1199
1178 void RenderText::ApplyFadeEffects(internal::SkiaTextRenderer* renderer) { 1200 void RenderText::ApplyFadeEffects(internal::SkiaTextRenderer* renderer) {
1179 const int width = display_rect().width(); 1201 const int width = display_rect().width();
1180 if (multiline() || elide_behavior_ != FADE_TAIL || GetContentWidth() <= width) 1202 if (multiline() || elide_behavior_ != FADE_TAIL || GetContentWidth() <= width)
1181 return; 1203 return;
1182 1204
1183 const int gradient_width = CalculateFadeGradientWidth(font_list(), width); 1205 const int gradient_width = CalculateFadeGradientWidth(font_list(), width);
1184 if (gradient_width == 0) 1206 if (gradient_width == 0)
1185 return; 1207 return;
1186 1208
1187 HorizontalAlignment horizontal_alignment = GetCurrentHorizontalAlignment(); 1209 HorizontalAlignment horizontal_alignment = GetCurrentHorizontalAlignment();
1188 Rect solid_part = display_rect(); 1210 Rect solid_part = display_rect();
1189 Rect left_part; 1211 Rect left_part;
1190 Rect right_part; 1212 Rect right_part;
1191 if (horizontal_alignment != ALIGN_LEFT) { 1213 if (horizontal_alignment != ALIGN_LEFT) {
1192 left_part = solid_part; 1214 left_part = solid_part;
1193 left_part.Inset(0, 0, solid_part.width() - gradient_width, 0); 1215 left_part.Inset(0, 0, solid_part.width() - gradient_width, 0);
1194 solid_part.Inset(gradient_width, 0, 0, 0); 1216 solid_part.Inset(gradient_width, 0, 0, 0);
1195 } 1217 }
1196 if (horizontal_alignment != ALIGN_RIGHT) { 1218 if (horizontal_alignment != ALIGN_RIGHT) {
1197 right_part = solid_part; 1219 right_part = solid_part;
1198 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0); 1220 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0);
1199 solid_part.Inset(0, 0, gradient_width, 0); 1221 solid_part.Inset(0, 0, gradient_width, 0);
1200 } 1222 }
1201 1223
1202 Rect text_rect = display_rect(); 1224 Rect text_rect = display_rect();
1203 text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0); 1225 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
1204 1226
1205 // TODO(msw): Use the actual text colors corresponding to each faded part. 1227 // TODO(msw): Use the actual text colors corresponding to each faded part.
1206 skia::RefPtr<SkShader> shader = CreateFadeShader( 1228 skia::RefPtr<SkShader> shader = CreateFadeShader(
1207 text_rect, left_part, right_part, colors_.breaks().front().second); 1229 text_rect, left_part, right_part, colors_.breaks().front().second);
1208 if (shader) 1230 if (shader)
1209 renderer->SetShader(shader.get()); 1231 renderer->SetShader(shader.get());
1210 } 1232 }
1211 1233
1212 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { 1234 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) {
1213 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_); 1235 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_);
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
1515 1537
1516 SetDisplayOffset(display_offset_.x() + delta_x); 1538 SetDisplayOffset(display_offset_.x() + delta_x);
1517 } 1539 }
1518 1540
1519 void RenderText::DrawSelection(Canvas* canvas) { 1541 void RenderText::DrawSelection(Canvas* canvas) {
1520 for (const Rect& s : GetSubstringBounds(selection())) 1542 for (const Rect& s : GetSubstringBounds(selection()))
1521 canvas->FillRect(s, selection_background_focused_color_); 1543 canvas->FillRect(s, selection_background_focused_color_);
1522 } 1544 }
1523 1545
1524 } // namespace gfx 1546 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698