Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 } | 262 } |
| 263 | 263 |
| 264 void SkiaTextRenderer::EndDiagonalStrike() { | 264 void SkiaTextRenderer::EndDiagonalStrike() { |
| 265 if (diagonal_) { | 265 if (diagonal_) { |
| 266 diagonal_->Draw(); | 266 diagonal_->Draw(); |
| 267 diagonal_.reset(); | 267 diagonal_.reset(); |
| 268 } | 268 } |
| 269 } | 269 } |
| 270 | 270 |
| 271 void SkiaTextRenderer::DrawUnderline(int x, int y, int width) { | 271 void SkiaTextRenderer::DrawUnderline(int x, int y, int width) { |
| 272 SkRect r = SkRect::MakeLTRB(x, y + underline_position_, x + width, | 272 SkScalar x_scalar = SkIntToScalar(x); |
| 273 y + underline_position_ + underline_thickness_); | 273 SkRect r = SkRect::MakeLTRB( |
| 274 x_scalar, y + underline_position_, x_scalar + width, | |
| 275 y + underline_position_ + underline_thickness_); | |
| 274 if (underline_thickness_ == kUnderlineMetricsNotSet) { | 276 if (underline_thickness_ == kUnderlineMetricsNotSet) { |
| 275 const SkScalar text_size = paint_.getTextSize(); | 277 const SkScalar text_size = paint_.getTextSize(); |
| 276 r.fTop = SkScalarMulAdd(text_size, kUnderlineOffset, y); | 278 r.fTop = SkScalarMulAdd(text_size, kUnderlineOffset, y); |
| 277 r.fBottom = r.fTop + SkScalarMul(text_size, kLineThickness); | 279 r.fBottom = r.fTop + SkScalarMul(text_size, kLineThickness); |
| 278 } | 280 } |
| 279 canvas_skia_->drawRect(r, paint_); | 281 canvas_skia_->drawRect(r, paint_); |
| 280 } | 282 } |
| 281 | 283 |
| 282 void SkiaTextRenderer::DrawStrike(int x, int y, int width) const { | 284 void SkiaTextRenderer::DrawStrike(int x, int y, int width) const { |
| 283 const SkScalar text_size = paint_.getTextSize(); | 285 const SkScalar text_size = paint_.getTextSize(); |
| 284 const SkScalar height = SkScalarMul(text_size, kLineThickness); | 286 const SkScalar height = SkScalarMul(text_size, kLineThickness); |
| 285 const SkScalar offset = SkScalarMulAdd(text_size, kStrikeThroughOffset, y); | 287 const SkScalar offset = SkScalarMulAdd(text_size, kStrikeThroughOffset, y); |
| 286 const SkRect r = SkRect::MakeLTRB(x, offset, x + width, offset + height); | 288 SkScalar x_scalar = SkIntToScalar(x); |
| 289 const SkRect r = | |
| 290 SkRect::MakeLTRB(x_scalar, offset, x_scalar + width, offset + height); | |
| 287 canvas_skia_->drawRect(r, paint_); | 291 canvas_skia_->drawRect(r, paint_); |
| 288 } | 292 } |
| 289 | 293 |
| 290 SkiaTextRenderer::DiagonalStrike::DiagonalStrike(Canvas* canvas, | 294 SkiaTextRenderer::DiagonalStrike::DiagonalStrike(Canvas* canvas, |
| 291 Point start, | 295 Point start, |
| 292 const SkPaint& paint) | 296 const SkPaint& paint) |
| 293 : canvas_(canvas), | 297 : canvas_(canvas), |
| 294 start_(start), | 298 start_(start), |
| 295 paint_(paint), | 299 paint_(paint), |
| 296 total_length_(0) { | 300 total_length_(0) { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 307 void SkiaTextRenderer::DiagonalStrike::Draw() { | 311 void SkiaTextRenderer::DiagonalStrike::Draw() { |
| 308 const SkScalar text_size = paint_.getTextSize(); | 312 const SkScalar text_size = paint_.getTextSize(); |
| 309 const SkScalar offset = SkScalarMul(text_size, kDiagonalStrikeMarginOffset); | 313 const SkScalar offset = SkScalarMul(text_size, kDiagonalStrikeMarginOffset); |
| 310 const int thickness = | 314 const int thickness = |
| 311 SkScalarCeilToInt(SkScalarMul(text_size, kLineThickness) * 2); | 315 SkScalarCeilToInt(SkScalarMul(text_size, kLineThickness) * 2); |
| 312 const int height = SkScalarCeilToInt(text_size - offset); | 316 const int height = SkScalarCeilToInt(text_size - offset); |
| 313 const Point end = start_ + Vector2d(total_length_, -height); | 317 const Point end = start_ + Vector2d(total_length_, -height); |
| 314 const int clip_height = height + 2 * thickness; | 318 const int clip_height = height + 2 * thickness; |
| 315 | 319 |
| 316 paint_.setAntiAlias(true); | 320 paint_.setAntiAlias(true); |
| 317 paint_.setStrokeWidth(thickness); | 321 paint_.setStrokeWidth(SkIntToScalar(thickness)); |
| 318 | 322 |
| 319 const bool clipped = pieces_.size() > 1; | 323 const bool clipped = pieces_.size() > 1; |
| 320 SkCanvas* sk_canvas = canvas_->sk_canvas(); | 324 SkCanvas* sk_canvas = canvas_->sk_canvas(); |
| 321 int x = start_.x(); | 325 int x = start_.x(); |
| 322 | 326 |
| 323 for (size_t i = 0; i < pieces_.size(); ++i) { | 327 for (size_t i = 0; i < pieces_.size(); ++i) { |
| 324 paint_.setColor(pieces_[i].second); | 328 paint_.setColor(pieces_[i].second); |
| 325 | 329 |
| 326 if (clipped) { | 330 if (clipped) { |
| 327 canvas_->Save(); | 331 canvas_->Save(); |
| (...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 713 | 717 |
| 714 return text_direction_; | 718 return text_direction_; |
| 715 } | 719 } |
| 716 | 720 |
| 717 VisualCursorDirection RenderText::GetVisualDirectionOfLogicalEnd() { | 721 VisualCursorDirection RenderText::GetVisualDirectionOfLogicalEnd() { |
| 718 return GetTextDirection() == base::i18n::LEFT_TO_RIGHT ? | 722 return GetTextDirection() == base::i18n::LEFT_TO_RIGHT ? |
| 719 CURSOR_RIGHT : CURSOR_LEFT; | 723 CURSOR_RIGHT : CURSOR_LEFT; |
| 720 } | 724 } |
| 721 | 725 |
| 722 SizeF RenderText::GetStringSizeF() { | 726 SizeF RenderText::GetStringSizeF() { |
| 723 const Size size = GetStringSize(); | 727 return GetStringSize(); |
|
msw
2014/10/17 22:13:23
Cool, I hadn't seen the gfx::Size operator SizeF()
| |
| 724 return SizeF(size.width(), size.height()); | |
| 725 } | 728 } |
| 726 | 729 |
| 727 float RenderText::GetContentWidth() { | 730 float RenderText::GetContentWidth() { |
| 728 return GetStringSizeF().width() + (cursor_enabled_ ? 1 : 0); | 731 return GetStringSizeF().width() + (cursor_enabled_ ? 1 : 0); |
| 729 } | 732 } |
| 730 | 733 |
| 731 int RenderText::GetBaseline() { | 734 int RenderText::GetBaseline() { |
| 732 if (baseline_ == kInvalidBaseline) | 735 if (baseline_ == kInvalidBaseline) |
| 733 baseline_ = DetermineBaselineCenteringText(display_rect(), font_list()); | 736 baseline_ = DetermineBaselineCenteringText(display_rect(), font_list()); |
| 734 DCHECK_NE(kInvalidBaseline, baseline_); | 737 DCHECK_NE(kInvalidBaseline, baseline_); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 850 return SelectionModel(sel.start(), | 853 return SelectionModel(sel.start(), |
| 851 sel.is_reversed() ? CURSOR_BACKWARD : CURSOR_FORWARD); | 854 sel.is_reversed() ? CURSOR_BACKWARD : CURSOR_FORWARD); |
| 852 } | 855 } |
| 853 | 856 |
| 854 const Vector2d& RenderText::GetUpdatedDisplayOffset() { | 857 const Vector2d& RenderText::GetUpdatedDisplayOffset() { |
| 855 UpdateCachedBoundsAndOffset(); | 858 UpdateCachedBoundsAndOffset(); |
| 856 return display_offset_; | 859 return display_offset_; |
| 857 } | 860 } |
| 858 | 861 |
| 859 void RenderText::SetDisplayOffset(int horizontal_offset) { | 862 void RenderText::SetDisplayOffset(int horizontal_offset) { |
| 860 const int extra_content = GetContentWidth() - display_rect_.width(); | 863 // !!! Should this round or ceil? |
|
msw
2014/10/17 22:13:23
Dan or Cem may have opinions here; we ultimately w
Daniel Erat
2014/10/17 23:20:05
i have no opinion here.
Peter Kasting
2014/10/21 01:20:45
It seems like ceiling here is a different behavior
| |
| 864 const int extra_content = | |
| 865 static_cast<int>(GetContentWidth()) - display_rect_.width(); | |
| 861 const int cursor_width = cursor_enabled_ ? 1 : 0; | 866 const int cursor_width = cursor_enabled_ ? 1 : 0; |
| 862 | 867 |
| 863 int min_offset = 0; | 868 int min_offset = 0; |
| 864 int max_offset = 0; | 869 int max_offset = 0; |
| 865 if (extra_content > 0) { | 870 if (extra_content > 0) { |
| 866 switch (GetCurrentHorizontalAlignment()) { | 871 switch (GetCurrentHorizontalAlignment()) { |
| 867 case ALIGN_LEFT: | 872 case ALIGN_LEFT: |
| 868 min_offset = -extra_content; | 873 min_offset = -extra_content; |
| 869 break; | 874 break; |
| 870 case ALIGN_RIGHT: | 875 case ALIGN_RIGHT: |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1203 layout_text_.assign(text.substr(0, iter.getIndex()) + kEllipsisUTF16); | 1208 layout_text_.assign(text.substr(0, iter.getIndex()) + kEllipsisUTF16); |
| 1204 } | 1209 } |
| 1205 } | 1210 } |
| 1206 | 1211 |
| 1207 if (elide_behavior_ != NO_ELIDE && | 1212 if (elide_behavior_ != NO_ELIDE && |
| 1208 elide_behavior_ != FADE_TAIL && | 1213 elide_behavior_ != FADE_TAIL && |
| 1209 !layout_text_.empty() && | 1214 !layout_text_.empty() && |
| 1210 static_cast<int>(GetContentWidth()) > display_rect_.width()) { | 1215 static_cast<int>(GetContentWidth()) > display_rect_.width()) { |
| 1211 // This doesn't trim styles so ellipsis may get rendered as a different | 1216 // This doesn't trim styles so ellipsis may get rendered as a different |
| 1212 // style than the preceding text. See crbug.com/327850. | 1217 // style than the preceding text. See crbug.com/327850. |
| 1213 layout_text_.assign( | 1218 layout_text_.assign(Elide(layout_text_, |
| 1214 Elide(layout_text_, display_rect_.width(), elide_behavior_)); | 1219 static_cast<float>(display_rect_.width()), |
| 1220 elide_behavior_)); | |
| 1215 } | 1221 } |
| 1216 | 1222 |
| 1217 // Replace the newline character with a newline symbol in single line mode. | 1223 // Replace the newline character with a newline symbol in single line mode. |
| 1218 static const base::char16 kNewline[] = { '\n', 0 }; | 1224 static const base::char16 kNewline[] = { '\n', 0 }; |
| 1219 static const base::char16 kNewlineSymbol[] = { 0x2424, 0 }; | 1225 static const base::char16 kNewlineSymbol[] = { 0x2424, 0 }; |
| 1220 if (!multiline_ && replace_newline_chars_with_symbols_) | 1226 if (!multiline_ && replace_newline_chars_with_symbols_) |
| 1221 base::ReplaceChars(layout_text_, kNewline, kNewlineSymbol, &layout_text_); | 1227 base::ReplaceChars(layout_text_, kNewline, kNewlineSymbol, &layout_text_); |
| 1222 | 1228 |
| 1223 ResetLayout(); | 1229 ResetLayout(); |
| 1224 } | 1230 } |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1404 SetDisplayOffset(display_offset_.x() + delta_x); | 1410 SetDisplayOffset(display_offset_.x() + delta_x); |
| 1405 } | 1411 } |
| 1406 | 1412 |
| 1407 void RenderText::DrawSelection(Canvas* canvas) { | 1413 void RenderText::DrawSelection(Canvas* canvas) { |
| 1408 const std::vector<Rect> sel = GetSubstringBounds(selection()); | 1414 const std::vector<Rect> sel = GetSubstringBounds(selection()); |
| 1409 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) | 1415 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) |
| 1410 canvas->FillRect(*i, selection_background_focused_color_); | 1416 canvas->FillRect(*i, selection_background_focused_color_); |
| 1411 } | 1417 } |
| 1412 | 1418 |
| 1413 } // namespace gfx | 1419 } // namespace gfx |
| OLD | NEW |