| 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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 | 53 |
| 54 // Default color used for drawing selection background. | 54 // Default color used for drawing selection background. |
| 55 const SkColor kDefaultSelectionBackgroundColor = SK_ColorGRAY; | 55 const SkColor kDefaultSelectionBackgroundColor = SK_ColorGRAY; |
| 56 | 56 |
| 57 // Fraction of the text size to lower a strike through below the baseline. | 57 // Fraction of the text size to lower a strike through below the baseline. |
| 58 const SkScalar kStrikeThroughOffset = (-SK_Scalar1 * 6 / 21); | 58 const SkScalar kStrikeThroughOffset = (-SK_Scalar1 * 6 / 21); |
| 59 // Fraction of the text size to lower an underline below the baseline. | 59 // Fraction of the text size to lower an underline below the baseline. |
| 60 const SkScalar kUnderlineOffset = (SK_Scalar1 / 9); | 60 const SkScalar kUnderlineOffset = (SK_Scalar1 / 9); |
| 61 // Fraction of the text size to use for a strike through or under-line. | 61 // Fraction of the text size to use for a strike through or under-line. |
| 62 const SkScalar kLineThickness = (SK_Scalar1 / 18); | 62 const SkScalar kLineThickness = (SK_Scalar1 / 18); |
| 63 // Fraction of the text size to use for a top margin of a diagonal strike. | |
| 64 const SkScalar kDiagonalStrikeMarginOffset = (SK_Scalar1 / 4); | |
| 65 | 63 |
| 66 // Invalid value of baseline. Assigning this value to |baseline_| causes | 64 // Invalid value of baseline. Assigning this value to |baseline_| causes |
| 67 // re-calculation of baseline. | 65 // re-calculation of baseline. |
| 68 const int kInvalidBaseline = INT_MAX; | 66 const int kInvalidBaseline = INT_MAX; |
| 69 | 67 |
| 70 int round(float value) { | 68 int round(float value) { |
| 71 return static_cast<int>(floor(value + 0.5f)); | 69 return static_cast<int>(floor(value + 0.5f)); |
| 72 } | 70 } |
| 73 | 71 |
| 74 // Given |font| and |display_width|, returns the width of the fade gradient. | 72 // Given |font| and |display_width|, returns the width of the fade gradient. |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 underline_position_ = position; | 240 underline_position_ = position; |
| 243 } | 241 } |
| 244 | 242 |
| 245 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, | 243 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, |
| 246 const uint16_t* glyphs, | 244 const uint16_t* glyphs, |
| 247 size_t glyph_count) { | 245 size_t glyph_count) { |
| 248 const size_t byte_length = glyph_count * sizeof(glyphs[0]); | 246 const size_t byte_length = glyph_count * sizeof(glyphs[0]); |
| 249 canvas_skia_->drawPosText(&glyphs[0], byte_length, &pos[0], flags_); | 247 canvas_skia_->drawPosText(&glyphs[0], byte_length, &pos[0], flags_); |
| 250 } | 248 } |
| 251 | 249 |
| 252 void SkiaTextRenderer::DrawDecorations(int x, int y, int width, bool underline, | 250 void SkiaTextRenderer::DrawDecorations(int x, |
| 253 bool strike, bool diagonal_strike) { | 251 int y, |
| 252 int width, |
| 253 bool underline, |
| 254 bool strike) { |
| 254 if (underline) | 255 if (underline) |
| 255 DrawUnderline(x, y, width); | 256 DrawUnderline(x, y, width); |
| 256 if (strike) | 257 if (strike) |
| 257 DrawStrike(x, y, width); | 258 DrawStrike(x, y, width); |
| 258 if (diagonal_strike) { | |
| 259 if (!diagonal_) | |
| 260 diagonal_.reset(new DiagonalStrike(canvas_, Point(x, y), flags_)); | |
| 261 diagonal_->AddPiece(width, flags_.getColor()); | |
| 262 } else if (diagonal_) { | |
| 263 EndDiagonalStrike(); | |
| 264 } | |
| 265 } | |
| 266 | |
| 267 void SkiaTextRenderer::EndDiagonalStrike() { | |
| 268 if (diagonal_) { | |
| 269 diagonal_->Draw(); | |
| 270 diagonal_.reset(); | |
| 271 } | |
| 272 } | 259 } |
| 273 | 260 |
| 274 void SkiaTextRenderer::DrawUnderline(int x, int y, int width) { | 261 void SkiaTextRenderer::DrawUnderline(int x, int y, int width) { |
| 275 SkScalar x_scalar = SkIntToScalar(x); | 262 SkScalar x_scalar = SkIntToScalar(x); |
| 276 SkRect r = SkRect::MakeLTRB( | 263 SkRect r = SkRect::MakeLTRB( |
| 277 x_scalar, y + underline_position_, x_scalar + width, | 264 x_scalar, y + underline_position_, x_scalar + width, |
| 278 y + underline_position_ + underline_thickness_); | 265 y + underline_position_ + underline_thickness_); |
| 279 if (underline_thickness_ == kUnderlineMetricsNotSet) { | 266 if (underline_thickness_ == kUnderlineMetricsNotSet) { |
| 280 const SkScalar text_size = flags_.getTextSize(); | 267 const SkScalar text_size = flags_.getTextSize(); |
| 281 r.fTop = text_size * kUnderlineOffset + y; | 268 r.fTop = text_size * kUnderlineOffset + y; |
| 282 r.fBottom = r.fTop + text_size * kLineThickness; | 269 r.fBottom = r.fTop + text_size * kLineThickness; |
| 283 } | 270 } |
| 284 canvas_skia_->drawRect(r, flags_); | 271 canvas_skia_->drawRect(r, flags_); |
| 285 } | 272 } |
| 286 | 273 |
| 287 void SkiaTextRenderer::DrawStrike(int x, int y, int width) const { | 274 void SkiaTextRenderer::DrawStrike(int x, int y, int width) const { |
| 288 const SkScalar text_size = flags_.getTextSize(); | 275 const SkScalar text_size = flags_.getTextSize(); |
| 289 const SkScalar height = text_size * kLineThickness; | 276 const SkScalar height = text_size * kLineThickness; |
| 290 const SkScalar offset = text_size * kStrikeThroughOffset + y; | 277 const SkScalar offset = text_size * kStrikeThroughOffset + y; |
| 291 SkScalar x_scalar = SkIntToScalar(x); | 278 SkScalar x_scalar = SkIntToScalar(x); |
| 292 const SkRect r = | 279 const SkRect r = |
| 293 SkRect::MakeLTRB(x_scalar, offset, x_scalar + width, offset + height); | 280 SkRect::MakeLTRB(x_scalar, offset, x_scalar + width, offset + height); |
| 294 canvas_skia_->drawRect(r, flags_); | 281 canvas_skia_->drawRect(r, flags_); |
| 295 } | 282 } |
| 296 | 283 |
| 297 SkiaTextRenderer::DiagonalStrike::DiagonalStrike(Canvas* canvas, | |
| 298 Point start, | |
| 299 const cc::PaintFlags& flags) | |
| 300 : canvas_(canvas), start_(start), flags_(flags), total_length_(0) {} | |
| 301 | |
| 302 SkiaTextRenderer::DiagonalStrike::~DiagonalStrike() { | |
| 303 } | |
| 304 | |
| 305 void SkiaTextRenderer::DiagonalStrike::AddPiece(int length, SkColor color) { | |
| 306 pieces_.push_back(Piece(length, color)); | |
| 307 total_length_ += length; | |
| 308 } | |
| 309 | |
| 310 void SkiaTextRenderer::DiagonalStrike::Draw() { | |
| 311 const SkScalar text_size = flags_.getTextSize(); | |
| 312 const SkScalar offset = text_size * kDiagonalStrikeMarginOffset; | |
| 313 const int thickness = SkScalarCeilToInt(text_size * kLineThickness * 2); | |
| 314 const int height = SkScalarCeilToInt(text_size - offset); | |
| 315 const Point end = start_ + Vector2d(total_length_, -height); | |
| 316 const int clip_height = height + 2 * thickness; | |
| 317 | |
| 318 flags_.setAntiAlias(true); | |
| 319 flags_.setStrokeWidth(SkIntToScalar(thickness)); | |
| 320 | |
| 321 const bool clipped = pieces_.size() > 1; | |
| 322 int x = start_.x(); | |
| 323 | |
| 324 for (size_t i = 0; i < pieces_.size(); ++i) { | |
| 325 flags_.setColor(pieces_[i].second); | |
| 326 | |
| 327 if (clipped) { | |
| 328 canvas_->Save(); | |
| 329 canvas_->ClipRect( | |
| 330 Rect(x, end.y() - thickness, pieces_[i].first, clip_height)); | |
| 331 } | |
| 332 | |
| 333 canvas_->DrawLine(start_, end, flags_); | |
| 334 | |
| 335 if (clipped) | |
| 336 canvas_->Restore(); | |
| 337 | |
| 338 x += pieces_[i].first; | |
| 339 } | |
| 340 } | |
| 341 | |
| 342 StyleIterator::StyleIterator(const BreakList<SkColor>& colors, | 284 StyleIterator::StyleIterator(const BreakList<SkColor>& colors, |
| 343 const BreakList<BaselineStyle>& baselines, | 285 const BreakList<BaselineStyle>& baselines, |
| 344 const BreakList<Font::Weight>& weights, | 286 const BreakList<Font::Weight>& weights, |
| 345 const std::vector<BreakList<bool>>& styles) | 287 const std::vector<BreakList<bool>>& styles) |
| 346 : colors_(colors), | 288 : colors_(colors), |
| 347 baselines_(baselines), | 289 baselines_(baselines), |
| 348 weights_(weights), | 290 weights_(weights), |
| 349 styles_(styles) { | 291 styles_(styles) { |
| 350 color_ = colors_.breaks().begin(); | 292 color_ = colors_.breaks().begin(); |
| 351 baseline_ = baselines_.breaks().begin(); | 293 baseline_ = baselines_.breaks().begin(); |
| (...skipping 1345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1697 | 1639 |
| 1698 for (; range_max < length; ++range_max) | 1640 for (; range_max < length; ++range_max) |
| 1699 if (iter.IsEndOfWord(range_max) || iter.IsStartOfWord(range_max)) | 1641 if (iter.IsEndOfWord(range_max) || iter.IsStartOfWord(range_max)) |
| 1700 break; | 1642 break; |
| 1701 | 1643 |
| 1702 return range.is_reversed() ? Range(range_max, range_min) | 1644 return range.is_reversed() ? Range(range_max, range_min) |
| 1703 : Range(range_min, range_max); | 1645 : Range(range_min, range_max); |
| 1704 } | 1646 } |
| 1705 | 1647 |
| 1706 } // namespace gfx | 1648 } // namespace gfx |
| OLD | NEW |