| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 namespace gfx { | 47 namespace gfx { |
| 48 | 48 |
| 49 namespace { | 49 namespace { |
| 50 | 50 |
| 51 // Default color used for the text and cursor. | 51 // Default color used for the text and cursor. |
| 52 const SkColor kDefaultColor = SK_ColorBLACK; | 52 const SkColor kDefaultColor = SK_ColorBLACK; |
| 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 raise the center of a strike-through line above |
| 58 const SkScalar kStrikeThroughOffset = (-SK_Scalar1 * 6 / 21); | 58 // the baseline. |
| 59 const SkScalar kStrikeThroughOffset = (SK_Scalar1 * 65 / 252); |
| 59 // Fraction of the text size to lower an underline below the baseline. | 60 // Fraction of the text size to lower an underline below the baseline. |
| 60 const SkScalar kUnderlineOffset = (SK_Scalar1 / 9); | 61 const SkScalar kUnderlineOffset = (SK_Scalar1 / 9); |
| 61 // Fraction of the text size to use for a strike through or under-line. | 62 // Default fraction of the text size to use for a strike-through or underline. |
| 62 const SkScalar kLineThickness = (SK_Scalar1 / 18); | 63 const SkScalar kLineThicknessFactor = (SK_Scalar1 / 18); |
| 63 | 64 |
| 64 // Invalid value of baseline. Assigning this value to |baseline_| causes | 65 // Invalid value of baseline. Assigning this value to |baseline_| causes |
| 65 // re-calculation of baseline. | 66 // re-calculation of baseline. |
| 66 const int kInvalidBaseline = INT_MAX; | 67 const int kInvalidBaseline = INT_MAX; |
| 67 | 68 |
| 68 int round(float value) { | 69 int round(float value) { |
| 69 return static_cast<int>(floor(value + 0.5f)); | 70 return static_cast<int>(floor(value + 0.5f)); |
| 70 } | 71 } |
| 71 | 72 |
| 72 // Given |font| and |display_width|, returns the width of the fade gradient. | 73 // Given |font| and |display_width|, returns the width of the fade gradient. |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 render_text->IndexOfAdjacentGrapheme(range.end(), CURSOR_FORWARD)); | 182 render_text->IndexOfAdjacentGrapheme(range.end(), CURSOR_FORWARD)); |
| 182 break_list->ApplyValue(current_break->second, range); | 183 break_list->ApplyValue(current_break->second, range); |
| 183 } | 184 } |
| 184 } | 185 } |
| 185 } | 186 } |
| 186 | 187 |
| 187 } // namespace | 188 } // namespace |
| 188 | 189 |
| 189 namespace internal { | 190 namespace internal { |
| 190 | 191 |
| 191 // Value of |underline_thickness_| that indicates that underline metrics have | |
| 192 // not been set explicitly. | |
| 193 const SkScalar kUnderlineMetricsNotSet = -1.0f; | |
| 194 | |
| 195 SkiaTextRenderer::SkiaTextRenderer(Canvas* canvas) | 192 SkiaTextRenderer::SkiaTextRenderer(Canvas* canvas) |
| 196 : canvas_(canvas), | 193 : canvas_(canvas), canvas_skia_(canvas->sk_canvas()) { |
| 197 canvas_skia_(canvas->sk_canvas()), | |
| 198 underline_thickness_(kUnderlineMetricsNotSet), | |
| 199 underline_position_(0.0f) { | |
| 200 DCHECK(canvas_skia_); | 194 DCHECK(canvas_skia_); |
| 201 flags_.setTextEncoding(cc::PaintFlags::kGlyphID_TextEncoding); | 195 flags_.setTextEncoding(cc::PaintFlags::kGlyphID_TextEncoding); |
| 202 flags_.setStyle(cc::PaintFlags::kFill_Style); | 196 flags_.setStyle(cc::PaintFlags::kFill_Style); |
| 203 flags_.setAntiAlias(true); | 197 flags_.setAntiAlias(true); |
| 204 flags_.setSubpixelText(true); | 198 flags_.setSubpixelText(true); |
| 205 flags_.setLCDRenderText(true); | 199 flags_.setLCDRenderText(true); |
| 206 flags_.setHinting(cc::PaintFlags::kNormal_Hinting); | 200 flags_.setHinting(cc::PaintFlags::kNormal_Hinting); |
| 207 } | 201 } |
| 208 | 202 |
| 209 SkiaTextRenderer::~SkiaTextRenderer() { | 203 SkiaTextRenderer::~SkiaTextRenderer() { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 227 } | 221 } |
| 228 | 222 |
| 229 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { | 223 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { |
| 230 flags_.setColor(foreground); | 224 flags_.setColor(foreground); |
| 231 } | 225 } |
| 232 | 226 |
| 233 void SkiaTextRenderer::SetShader(std::unique_ptr<cc::PaintShader> shader) { | 227 void SkiaTextRenderer::SetShader(std::unique_ptr<cc::PaintShader> shader) { |
| 234 flags_.setShader(std::move(shader)); | 228 flags_.setShader(std::move(shader)); |
| 235 } | 229 } |
| 236 | 230 |
| 237 void SkiaTextRenderer::SetUnderlineMetrics(SkScalar thickness, | |
| 238 SkScalar position) { | |
| 239 underline_thickness_ = thickness; | |
| 240 underline_position_ = position; | |
| 241 } | |
| 242 | |
| 243 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, | 231 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, |
| 244 const uint16_t* glyphs, | 232 const uint16_t* glyphs, |
| 245 size_t glyph_count) { | 233 size_t glyph_count) { |
| 246 const size_t byte_length = glyph_count * sizeof(glyphs[0]); | 234 const size_t byte_length = glyph_count * sizeof(glyphs[0]); |
| 247 canvas_skia_->drawPosText(&glyphs[0], byte_length, &pos[0], flags_); | 235 canvas_skia_->drawPosText(&glyphs[0], byte_length, &pos[0], flags_); |
| 248 } | 236 } |
| 249 | 237 |
| 250 void SkiaTextRenderer::DrawDecorations(int x, | |
| 251 int y, | |
| 252 int width, | |
| 253 bool underline, | |
| 254 bool strike) { | |
| 255 if (underline) | |
| 256 DrawUnderline(x, y, width); | |
| 257 if (strike) | |
| 258 DrawStrike(x, y, width); | |
| 259 } | |
| 260 | |
| 261 void SkiaTextRenderer::DrawUnderline(int x, int y, int width) { | 238 void SkiaTextRenderer::DrawUnderline(int x, int y, int width) { |
| 262 SkScalar x_scalar = SkIntToScalar(x); | 239 SkScalar x_scalar = SkIntToScalar(x); |
| 240 const SkScalar text_size = flags_.getTextSize(); |
| 263 SkRect r = SkRect::MakeLTRB( | 241 SkRect r = SkRect::MakeLTRB( |
| 264 x_scalar, y + underline_position_, x_scalar + width, | 242 x_scalar, y + text_size * kUnderlineOffset, x_scalar + width, |
| 265 y + underline_position_ + underline_thickness_); | 243 y + (text_size * (kUnderlineOffset + kLineThicknessFactor))); |
| 266 if (underline_thickness_ == kUnderlineMetricsNotSet) { | |
| 267 const SkScalar text_size = flags_.getTextSize(); | |
| 268 r.fTop = text_size * kUnderlineOffset + y; | |
| 269 r.fBottom = r.fTop + text_size * kLineThickness; | |
| 270 } | |
| 271 canvas_skia_->drawRect(r, flags_); | 244 canvas_skia_->drawRect(r, flags_); |
| 272 } | 245 } |
| 273 | 246 |
| 274 void SkiaTextRenderer::DrawStrike(int x, int y, int width) const { | 247 void SkiaTextRenderer::DrawStrike(int x, |
| 248 int y, |
| 249 int width, |
| 250 SkScalar thickness_factor) { |
| 275 const SkScalar text_size = flags_.getTextSize(); | 251 const SkScalar text_size = flags_.getTextSize(); |
| 276 const SkScalar height = text_size * kLineThickness; | 252 const SkScalar height = text_size * thickness_factor; |
| 277 const SkScalar offset = text_size * kStrikeThroughOffset + y; | 253 const SkScalar top = y - text_size * kStrikeThroughOffset - height / 2; |
| 278 SkScalar x_scalar = SkIntToScalar(x); | 254 SkScalar x_scalar = SkIntToScalar(x); |
| 279 const SkRect r = | 255 const SkRect r = |
| 280 SkRect::MakeLTRB(x_scalar, offset, x_scalar + width, offset + height); | 256 SkRect::MakeLTRB(x_scalar, top, x_scalar + width, top + height); |
| 281 canvas_skia_->drawRect(r, flags_); | 257 canvas_skia_->drawRect(r, flags_); |
| 282 } | 258 } |
| 283 | 259 |
| 284 StyleIterator::StyleIterator(const BreakList<SkColor>& colors, | 260 StyleIterator::StyleIterator(const BreakList<SkColor>& colors, |
| 285 const BreakList<BaselineStyle>& baselines, | 261 const BreakList<BaselineStyle>& baselines, |
| 286 const BreakList<Font::Weight>& weights, | 262 const BreakList<Font::Weight>& weights, |
| 287 const std::vector<BreakList<bool>>& styles) | 263 const std::vector<BreakList<bool>>& styles) |
| 288 : colors_(colors), | 264 : colors_(colors), |
| 289 baselines_(baselines), | 265 baselines_(baselines), |
| 290 weights_(weights), | 266 weights_(weights), |
| (...skipping 708 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 999 elide_behavior_(NO_ELIDE), | 975 elide_behavior_(NO_ELIDE), |
| 1000 text_elided_(false), | 976 text_elided_(false), |
| 1001 min_line_height_(0), | 977 min_line_height_(0), |
| 1002 multiline_(false), | 978 multiline_(false), |
| 1003 max_lines_(0), | 979 max_lines_(0), |
| 1004 word_wrap_behavior_(IGNORE_LONG_WORDS), | 980 word_wrap_behavior_(IGNORE_LONG_WORDS), |
| 1005 replace_newline_chars_with_symbols_(true), | 981 replace_newline_chars_with_symbols_(true), |
| 1006 subpixel_rendering_suppressed_(false), | 982 subpixel_rendering_suppressed_(false), |
| 1007 clip_to_display_rect_(true), | 983 clip_to_display_rect_(true), |
| 1008 baseline_(kInvalidBaseline), | 984 baseline_(kInvalidBaseline), |
| 1009 cached_bounds_and_offset_valid_(false) {} | 985 cached_bounds_and_offset_valid_(false), |
| 986 strike_thickness_factor_(kLineThicknessFactor) {} |
| 1010 | 987 |
| 1011 SelectionModel RenderText::GetAdjacentSelectionModel( | 988 SelectionModel RenderText::GetAdjacentSelectionModel( |
| 1012 const SelectionModel& current, | 989 const SelectionModel& current, |
| 1013 BreakType break_type, | 990 BreakType break_type, |
| 1014 VisualCursorDirection direction) { | 991 VisualCursorDirection direction) { |
| 1015 EnsureLayout(); | 992 EnsureLayout(); |
| 1016 | 993 |
| 1017 if (break_type == LINE_BREAK || text().empty()) | 994 if (break_type == LINE_BREAK || text().empty()) |
| 1018 return EdgeSelectionModel(direction); | 995 return EdgeSelectionModel(direction); |
| 1019 if (break_type == CHARACTER_BREAK) | 996 if (break_type == CHARACTER_BREAK) |
| (...skipping 619 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1639 | 1616 |
| 1640 for (; range_max < length; ++range_max) | 1617 for (; range_max < length; ++range_max) |
| 1641 if (iter.IsEndOfWord(range_max) || iter.IsStartOfWord(range_max)) | 1618 if (iter.IsEndOfWord(range_max) || iter.IsStartOfWord(range_max)) |
| 1642 break; | 1619 break; |
| 1643 | 1620 |
| 1644 return range.is_reversed() ? Range(range_max, range_min) | 1621 return range.is_reversed() ? Range(range_max, range_min) |
| 1645 : Range(range_min, range_max); | 1622 : Range(range_min, range_max); |
| 1646 } | 1623 } |
| 1647 | 1624 |
| 1648 } // namespace gfx | 1625 } // namespace gfx |
| OLD | NEW |