| 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 <limits.h> | 7 #include <limits.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <climits> | 10 #include <climits> |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 // Value of |underline_thickness_| that indicates that underline metrics have | 216 // Value of |underline_thickness_| that indicates that underline metrics have |
| 217 // not been set explicitly. | 217 // not been set explicitly. |
| 218 const SkScalar kUnderlineMetricsNotSet = -1.0f; | 218 const SkScalar kUnderlineMetricsNotSet = -1.0f; |
| 219 | 219 |
| 220 SkiaTextRenderer::SkiaTextRenderer(Canvas* canvas) | 220 SkiaTextRenderer::SkiaTextRenderer(Canvas* canvas) |
| 221 : canvas_(canvas), | 221 : canvas_(canvas), |
| 222 canvas_skia_(canvas->sk_canvas()), | 222 canvas_skia_(canvas->sk_canvas()), |
| 223 underline_thickness_(kUnderlineMetricsNotSet), | 223 underline_thickness_(kUnderlineMetricsNotSet), |
| 224 underline_position_(0.0f) { | 224 underline_position_(0.0f) { |
| 225 DCHECK(canvas_skia_); | 225 DCHECK(canvas_skia_); |
| 226 paint_.setTextEncoding(cc::PaintFlags::kGlyphID_TextEncoding); | 226 flags_.setTextEncoding(cc::PaintFlags::kGlyphID_TextEncoding); |
| 227 paint_.setStyle(cc::PaintFlags::kFill_Style); | 227 flags_.setStyle(cc::PaintFlags::kFill_Style); |
| 228 paint_.setAntiAlias(true); | 228 flags_.setAntiAlias(true); |
| 229 paint_.setSubpixelText(true); | 229 flags_.setSubpixelText(true); |
| 230 paint_.setLCDRenderText(true); | 230 flags_.setLCDRenderText(true); |
| 231 paint_.setHinting(cc::PaintFlags::kNormal_Hinting); | 231 flags_.setHinting(cc::PaintFlags::kNormal_Hinting); |
| 232 } | 232 } |
| 233 | 233 |
| 234 SkiaTextRenderer::~SkiaTextRenderer() { | 234 SkiaTextRenderer::~SkiaTextRenderer() { |
| 235 } | 235 } |
| 236 | 236 |
| 237 void SkiaTextRenderer::SetDrawLooper(sk_sp<SkDrawLooper> draw_looper) { | 237 void SkiaTextRenderer::SetDrawLooper(sk_sp<SkDrawLooper> draw_looper) { |
| 238 paint_.setLooper(std::move(draw_looper)); | 238 flags_.setLooper(std::move(draw_looper)); |
| 239 } | 239 } |
| 240 | 240 |
| 241 void SkiaTextRenderer::SetFontRenderParams(const FontRenderParams& params, | 241 void SkiaTextRenderer::SetFontRenderParams(const FontRenderParams& params, |
| 242 bool subpixel_rendering_suppressed) { | 242 bool subpixel_rendering_suppressed) { |
| 243 ApplyRenderParams(params, subpixel_rendering_suppressed, &paint_); | 243 ApplyRenderParams(params, subpixel_rendering_suppressed, &flags_); |
| 244 } | 244 } |
| 245 | 245 |
| 246 void SkiaTextRenderer::SetTypeface(sk_sp<SkTypeface> typeface) { | 246 void SkiaTextRenderer::SetTypeface(sk_sp<SkTypeface> typeface) { |
| 247 paint_.setTypeface(std::move(typeface)); | 247 flags_.setTypeface(std::move(typeface)); |
| 248 } | 248 } |
| 249 | 249 |
| 250 void SkiaTextRenderer::SetTextSize(SkScalar size) { | 250 void SkiaTextRenderer::SetTextSize(SkScalar size) { |
| 251 paint_.setTextSize(size); | 251 flags_.setTextSize(size); |
| 252 } | 252 } |
| 253 | 253 |
| 254 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { | 254 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { |
| 255 paint_.setColor(foreground); | 255 flags_.setColor(foreground); |
| 256 } | 256 } |
| 257 | 257 |
| 258 void SkiaTextRenderer::SetShader(sk_sp<SkShader> shader) { | 258 void SkiaTextRenderer::SetShader(sk_sp<SkShader> shader) { |
| 259 paint_.setShader(cc::WrapSkShader(std::move(shader))); | 259 flags_.setShader(cc::WrapSkShader(std::move(shader))); |
| 260 } | 260 } |
| 261 | 261 |
| 262 void SkiaTextRenderer::SetHaloEffect() { | 262 void SkiaTextRenderer::SetHaloEffect() { |
| 263 paint_.setImageFilter(SkDilateImageFilter::Make(1, 1, nullptr)); | 263 flags_.setImageFilter(SkDilateImageFilter::Make(1, 1, nullptr)); |
| 264 } | 264 } |
| 265 | 265 |
| 266 void SkiaTextRenderer::SetUnderlineMetrics(SkScalar thickness, | 266 void SkiaTextRenderer::SetUnderlineMetrics(SkScalar thickness, |
| 267 SkScalar position) { | 267 SkScalar position) { |
| 268 underline_thickness_ = thickness; | 268 underline_thickness_ = thickness; |
| 269 underline_position_ = position; | 269 underline_position_ = position; |
| 270 } | 270 } |
| 271 | 271 |
| 272 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, | 272 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, |
| 273 const uint16_t* glyphs, | 273 const uint16_t* glyphs, |
| 274 size_t glyph_count) { | 274 size_t glyph_count) { |
| 275 const size_t byte_length = glyph_count * sizeof(glyphs[0]); | 275 const size_t byte_length = glyph_count * sizeof(glyphs[0]); |
| 276 canvas_skia_->drawPosText(&glyphs[0], byte_length, &pos[0], paint_); | 276 canvas_skia_->drawPosText(&glyphs[0], byte_length, &pos[0], flags_); |
| 277 } | 277 } |
| 278 | 278 |
| 279 void SkiaTextRenderer::DrawDecorations(int x, int y, int width, bool underline, | 279 void SkiaTextRenderer::DrawDecorations(int x, int y, int width, bool underline, |
| 280 bool strike, bool diagonal_strike) { | 280 bool strike, bool diagonal_strike) { |
| 281 if (underline) | 281 if (underline) |
| 282 DrawUnderline(x, y, width); | 282 DrawUnderline(x, y, width); |
| 283 if (strike) | 283 if (strike) |
| 284 DrawStrike(x, y, width); | 284 DrawStrike(x, y, width); |
| 285 if (diagonal_strike) { | 285 if (diagonal_strike) { |
| 286 if (!diagonal_) | 286 if (!diagonal_) |
| 287 diagonal_.reset(new DiagonalStrike(canvas_, Point(x, y), paint_)); | 287 diagonal_.reset(new DiagonalStrike(canvas_, Point(x, y), flags_)); |
| 288 diagonal_->AddPiece(width, paint_.getColor()); | 288 diagonal_->AddPiece(width, flags_.getColor()); |
| 289 } else if (diagonal_) { | 289 } else if (diagonal_) { |
| 290 EndDiagonalStrike(); | 290 EndDiagonalStrike(); |
| 291 } | 291 } |
| 292 } | 292 } |
| 293 | 293 |
| 294 void SkiaTextRenderer::EndDiagonalStrike() { | 294 void SkiaTextRenderer::EndDiagonalStrike() { |
| 295 if (diagonal_) { | 295 if (diagonal_) { |
| 296 diagonal_->Draw(); | 296 diagonal_->Draw(); |
| 297 diagonal_.reset(); | 297 diagonal_.reset(); |
| 298 } | 298 } |
| 299 } | 299 } |
| 300 | 300 |
| 301 void SkiaTextRenderer::DrawUnderline(int x, int y, int width) { | 301 void SkiaTextRenderer::DrawUnderline(int x, int y, int width) { |
| 302 SkScalar x_scalar = SkIntToScalar(x); | 302 SkScalar x_scalar = SkIntToScalar(x); |
| 303 SkRect r = SkRect::MakeLTRB( | 303 SkRect r = SkRect::MakeLTRB( |
| 304 x_scalar, y + underline_position_, x_scalar + width, | 304 x_scalar, y + underline_position_, x_scalar + width, |
| 305 y + underline_position_ + underline_thickness_); | 305 y + underline_position_ + underline_thickness_); |
| 306 if (underline_thickness_ == kUnderlineMetricsNotSet) { | 306 if (underline_thickness_ == kUnderlineMetricsNotSet) { |
| 307 const SkScalar text_size = paint_.getTextSize(); | 307 const SkScalar text_size = flags_.getTextSize(); |
| 308 r.fTop = SkScalarMulAdd(text_size, kUnderlineOffset, y); | 308 r.fTop = SkScalarMulAdd(text_size, kUnderlineOffset, y); |
| 309 r.fBottom = r.fTop + SkScalarMul(text_size, kLineThickness); | 309 r.fBottom = r.fTop + SkScalarMul(text_size, kLineThickness); |
| 310 } | 310 } |
| 311 canvas_skia_->drawRect(r, paint_); | 311 canvas_skia_->drawRect(r, flags_); |
| 312 } | 312 } |
| 313 | 313 |
| 314 void SkiaTextRenderer::DrawStrike(int x, int y, int width) const { | 314 void SkiaTextRenderer::DrawStrike(int x, int y, int width) const { |
| 315 const SkScalar text_size = paint_.getTextSize(); | 315 const SkScalar text_size = flags_.getTextSize(); |
| 316 const SkScalar height = SkScalarMul(text_size, kLineThickness); | 316 const SkScalar height = SkScalarMul(text_size, kLineThickness); |
| 317 const SkScalar offset = SkScalarMulAdd(text_size, kStrikeThroughOffset, y); | 317 const SkScalar offset = SkScalarMulAdd(text_size, kStrikeThroughOffset, y); |
| 318 SkScalar x_scalar = SkIntToScalar(x); | 318 SkScalar x_scalar = SkIntToScalar(x); |
| 319 const SkRect r = | 319 const SkRect r = |
| 320 SkRect::MakeLTRB(x_scalar, offset, x_scalar + width, offset + height); | 320 SkRect::MakeLTRB(x_scalar, offset, x_scalar + width, offset + height); |
| 321 canvas_skia_->drawRect(r, paint_); | 321 canvas_skia_->drawRect(r, flags_); |
| 322 } | 322 } |
| 323 | 323 |
| 324 SkiaTextRenderer::DiagonalStrike::DiagonalStrike(Canvas* canvas, | 324 SkiaTextRenderer::DiagonalStrike::DiagonalStrike(Canvas* canvas, |
| 325 Point start, | 325 Point start, |
| 326 const cc::PaintFlags& paint) | 326 const cc::PaintFlags& flags) |
| 327 : canvas_(canvas), start_(start), paint_(paint), total_length_(0) {} | 327 : canvas_(canvas), start_(start), flags_(flags), total_length_(0) {} |
| 328 | 328 |
| 329 SkiaTextRenderer::DiagonalStrike::~DiagonalStrike() { | 329 SkiaTextRenderer::DiagonalStrike::~DiagonalStrike() { |
| 330 } | 330 } |
| 331 | 331 |
| 332 void SkiaTextRenderer::DiagonalStrike::AddPiece(int length, SkColor color) { | 332 void SkiaTextRenderer::DiagonalStrike::AddPiece(int length, SkColor color) { |
| 333 pieces_.push_back(Piece(length, color)); | 333 pieces_.push_back(Piece(length, color)); |
| 334 total_length_ += length; | 334 total_length_ += length; |
| 335 } | 335 } |
| 336 | 336 |
| 337 void SkiaTextRenderer::DiagonalStrike::Draw() { | 337 void SkiaTextRenderer::DiagonalStrike::Draw() { |
| 338 const SkScalar text_size = paint_.getTextSize(); | 338 const SkScalar text_size = flags_.getTextSize(); |
| 339 const SkScalar offset = SkScalarMul(text_size, kDiagonalStrikeMarginOffset); | 339 const SkScalar offset = SkScalarMul(text_size, kDiagonalStrikeMarginOffset); |
| 340 const int thickness = | 340 const int thickness = |
| 341 SkScalarCeilToInt(SkScalarMul(text_size, kLineThickness) * 2); | 341 SkScalarCeilToInt(SkScalarMul(text_size, kLineThickness) * 2); |
| 342 const int height = SkScalarCeilToInt(text_size - offset); | 342 const int height = SkScalarCeilToInt(text_size - offset); |
| 343 const Point end = start_ + Vector2d(total_length_, -height); | 343 const Point end = start_ + Vector2d(total_length_, -height); |
| 344 const int clip_height = height + 2 * thickness; | 344 const int clip_height = height + 2 * thickness; |
| 345 | 345 |
| 346 paint_.setAntiAlias(true); | 346 flags_.setAntiAlias(true); |
| 347 paint_.setStrokeWidth(SkIntToScalar(thickness)); | 347 flags_.setStrokeWidth(SkIntToScalar(thickness)); |
| 348 | 348 |
| 349 const bool clipped = pieces_.size() > 1; | 349 const bool clipped = pieces_.size() > 1; |
| 350 int x = start_.x(); | 350 int x = start_.x(); |
| 351 | 351 |
| 352 for (size_t i = 0; i < pieces_.size(); ++i) { | 352 for (size_t i = 0; i < pieces_.size(); ++i) { |
| 353 paint_.setColor(pieces_[i].second); | 353 flags_.setColor(pieces_[i].second); |
| 354 | 354 |
| 355 if (clipped) { | 355 if (clipped) { |
| 356 canvas_->Save(); | 356 canvas_->Save(); |
| 357 canvas_->ClipRect( | 357 canvas_->ClipRect( |
| 358 Rect(x, end.y() - thickness, pieces_[i].first, clip_height)); | 358 Rect(x, end.y() - thickness, pieces_[i].first, clip_height)); |
| 359 } | 359 } |
| 360 | 360 |
| 361 canvas_->DrawLine(start_, end, paint_); | 361 canvas_->DrawLine(start_, end, flags_); |
| 362 | 362 |
| 363 if (clipped) | 363 if (clipped) |
| 364 canvas_->Restore(); | 364 canvas_->Restore(); |
| 365 | 365 |
| 366 x += pieces_[i].first; | 366 x += pieces_[i].first; |
| 367 } | 367 } |
| 368 } | 368 } |
| 369 | 369 |
| 370 StyleIterator::StyleIterator(const BreakList<SkColor>& colors, | 370 StyleIterator::StyleIterator(const BreakList<SkColor>& colors, |
| 371 const BreakList<BaselineStyle>& baselines, | 371 const BreakList<BaselineStyle>& baselines, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 LineSegment::~LineSegment() {} | 406 LineSegment::~LineSegment() {} |
| 407 | 407 |
| 408 Line::Line() : preceding_heights(0), baseline(0) {} | 408 Line::Line() : preceding_heights(0), baseline(0) {} |
| 409 | 409 |
| 410 Line::Line(const Line& other) = default; | 410 Line::Line(const Line& other) = default; |
| 411 | 411 |
| 412 Line::~Line() {} | 412 Line::~Line() {} |
| 413 | 413 |
| 414 void ApplyRenderParams(const FontRenderParams& params, | 414 void ApplyRenderParams(const FontRenderParams& params, |
| 415 bool subpixel_rendering_suppressed, | 415 bool subpixel_rendering_suppressed, |
| 416 cc::PaintFlags* paint) { | 416 cc::PaintFlags* flags) { |
| 417 paint->setAntiAlias(params.antialiasing); | 417 flags->setAntiAlias(params.antialiasing); |
| 418 paint->setLCDRenderText(!subpixel_rendering_suppressed && | 418 flags->setLCDRenderText(!subpixel_rendering_suppressed && |
| 419 params.subpixel_rendering != FontRenderParams::SUBPIXEL_RENDERING_NONE); | 419 params.subpixel_rendering != |
| 420 paint->setSubpixelText(params.subpixel_positioning); | 420 FontRenderParams::SUBPIXEL_RENDERING_NONE); |
| 421 paint->setAutohinted(params.autohinter); | 421 flags->setSubpixelText(params.subpixel_positioning); |
| 422 paint->setHinting(FontRenderParamsHintingToPaintFlagsHinting(params.hinting)); | 422 flags->setAutohinted(params.autohinter); |
| 423 flags->setHinting(FontRenderParamsHintingToPaintFlagsHinting(params.hinting)); |
| 423 } | 424 } |
| 424 | 425 |
| 425 } // namespace internal | 426 } // namespace internal |
| 426 | 427 |
| 427 // static | 428 // static |
| 428 constexpr base::char16 RenderText::kPasswordReplacementChar; | 429 constexpr base::char16 RenderText::kPasswordReplacementChar; |
| 429 constexpr bool RenderText::kDragToEndIfOutsideVerticalBounds; | 430 constexpr bool RenderText::kDragToEndIfOutsideVerticalBounds; |
| 430 | 431 |
| 431 RenderText::~RenderText() { | 432 RenderText::~RenderText() { |
| 432 } | 433 } |
| (...skipping 1291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1724 | 1725 |
| 1725 for (; range_max < length; ++range_max) | 1726 for (; range_max < length; ++range_max) |
| 1726 if (iter.IsEndOfWord(range_max) || iter.IsStartOfWord(range_max)) | 1727 if (iter.IsEndOfWord(range_max) || iter.IsStartOfWord(range_max)) |
| 1727 break; | 1728 break; |
| 1728 | 1729 |
| 1729 return range.is_reversed() ? Range(range_max, range_min) | 1730 return range.is_reversed() ? Range(range_max, range_min) |
| 1730 : Range(range_min, range_max); | 1731 : Range(range_min, range_max); |
| 1731 } | 1732 } |
| 1732 | 1733 |
| 1733 } // namespace gfx | 1734 } // namespace gfx |
| OLD | NEW |