| 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 | 8 |
| 9 #include "base/i18n/break_iterator.h" | 9 #include "base/i18n/break_iterator.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "third_party/skia/include/core/SkTypeface.h" | 12 #include "third_party/skia/include/core/SkTypeface.h" |
| 13 #include "third_party/skia/include/effects/SkGradientShader.h" | 13 #include "third_party/skia/include/effects/SkGradientShader.h" |
| 14 #include "ui/base/text/utf16_indexing.h" | 14 #include "ui/base/text/utf16_indexing.h" |
| 15 #include "ui/gfx/canvas.h" | 15 #include "ui/gfx/canvas.h" |
| 16 #include "ui/gfx/insets.h" | 16 #include "ui/gfx/insets.h" |
| 17 #include "ui/gfx/skia_util.h" | 17 #include "ui/gfx/skia_util.h" |
| 18 #include "ui/gfx/text_constants.h" | 18 #include "ui/gfx/text_constants.h" |
| 19 | 19 |
| 20 namespace gfx { |
| 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 // All chars are replaced by this char when the password style is set. | 24 // All chars are replaced by this char when the password style is set. |
| 23 // TODO(benrg): GTK uses the first of U+25CF, U+2022, U+2731, U+273A, '*' | 25 // TODO(benrg): GTK uses the first of U+25CF, U+2022, U+2731, U+273A, '*' |
| 24 // that's available in the font (find_invisible_char() in gtkentry.c). | 26 // that's available in the font (find_invisible_char() in gtkentry.c). |
| 25 const char16 kPasswordReplacementChar = '*'; | 27 const char16 kPasswordReplacementChar = '*'; |
| 26 | 28 |
| 27 // Default color used for the cursor. | 29 // Default color used for the text and cursor. |
| 28 const SkColor kDefaultCursorColor = SK_ColorBLACK; | 30 const SkColor kDefaultColor = SK_ColorBLACK; |
| 29 | |
| 30 // Default color used for drawing selection text. | |
| 31 const SkColor kDefaultSelectionColor = SK_ColorBLACK; | |
| 32 | 31 |
| 33 // Default color used for drawing selection background. | 32 // Default color used for drawing selection background. |
| 34 const SkColor kDefaultSelectionBackgroundColor = SK_ColorGRAY; | 33 const SkColor kDefaultSelectionBackgroundColor = SK_ColorGRAY; |
| 35 | 34 |
| 36 #ifndef NDEBUG | 35 // Fraction of the text size to lower a strike through below the baseline. |
| 37 // Check StyleRanges invariant conditions: sorted and non-overlapping ranges. | 36 const SkScalar kStrikeThroughOffset = (-SK_Scalar1 * 6 / 21); |
| 38 void CheckStyleRanges(const gfx::StyleRanges& style_ranges, size_t length) { | 37 // Fraction of the text size to lower an underline below the baseline. |
| 39 if (length == 0) { | 38 const SkScalar kUnderlineOffset = (SK_Scalar1 / 9); |
| 40 DCHECK(style_ranges.empty()) << "Style ranges exist for empty text."; | 39 // Fraction of the text size to use for a strike through or under-line. |
| 41 return; | 40 const SkScalar kLineThickness = (SK_Scalar1 / 18); |
| 42 } | 41 // Fraction of the text size to use for a top margin of a diagonal strike. |
| 43 for (gfx::StyleRanges::size_type i = 0; i < style_ranges.size() - 1; i++) { | 42 const SkScalar kDiagonalStrikeMarginOffset = (SK_Scalar1 / 4); |
| 44 const ui::Range& former = style_ranges[i].range; | |
| 45 const ui::Range& latter = style_ranges[i + 1].range; | |
| 46 DCHECK(!former.is_empty()) << "Empty range at " << i << ":" << | |
| 47 former.ToString(); | |
| 48 DCHECK(former.IsValid()) << "Invalid range at " << i << ":" << | |
| 49 former.ToString(); | |
| 50 DCHECK(!former.is_reversed()) << "Reversed range at " << i << ":" << | |
| 51 former.ToString(); | |
| 52 DCHECK(former.end() == latter.start()) << "Ranges gap/overlap/unsorted." << | |
| 53 "former:" << former.ToString() << ", latter:" << latter.ToString(); | |
| 54 } | |
| 55 const gfx::StyleRange& end_style = *style_ranges.rbegin(); | |
| 56 DCHECK(!end_style.range.is_empty()) << "Empty range at end."; | |
| 57 DCHECK(end_style.range.IsValid()) << "Invalid range at end."; | |
| 58 DCHECK(!end_style.range.is_reversed()) << "Reversed range at end."; | |
| 59 DCHECK(end_style.range.end() == length) << "Style and text length mismatch."; | |
| 60 } | |
| 61 #endif | |
| 62 | |
| 63 void ApplyStyleRangeImpl(gfx::StyleRanges* style_ranges, | |
| 64 const gfx::StyleRange& style_range) { | |
| 65 const ui::Range& new_range = style_range.range; | |
| 66 // Follow StyleRanges invariant conditions: sorted and non-overlapping ranges. | |
| 67 gfx::StyleRanges::iterator i; | |
| 68 for (i = style_ranges->begin(); i != style_ranges->end();) { | |
| 69 if (i->range.end() < new_range.start()) { | |
| 70 i++; | |
| 71 } else if (i->range.start() == new_range.end()) { | |
| 72 break; | |
| 73 } else if (new_range.Contains(i->range)) { | |
| 74 i = style_ranges->erase(i); | |
| 75 if (i == style_ranges->end()) | |
| 76 break; | |
| 77 } else if (i->range.start() < new_range.start() && | |
| 78 i->range.end() > new_range.end()) { | |
| 79 // Split the current style into two styles. | |
| 80 gfx::StyleRange split_style = gfx::StyleRange(*i); | |
| 81 split_style.range.set_end(new_range.start()); | |
| 82 i = style_ranges->insert(i, split_style) + 1; | |
| 83 i->range.set_start(new_range.end()); | |
| 84 break; | |
| 85 } else if (i->range.start() < new_range.start()) { | |
| 86 i->range.set_end(new_range.start()); | |
| 87 i++; | |
| 88 } else if (i->range.end() > new_range.end()) { | |
| 89 i->range.set_start(new_range.end()); | |
| 90 break; | |
| 91 } else { | |
| 92 NOTREACHED(); | |
| 93 } | |
| 94 } | |
| 95 // Add the new range in its sorted location. | |
| 96 style_ranges->insert(i, style_range); | |
| 97 } | |
| 98 | 43 |
| 99 // Converts |gfx::Font::FontStyle| flags to |SkTypeface::Style| flags. | 44 // Converts |gfx::Font::FontStyle| flags to |SkTypeface::Style| flags. |
| 100 SkTypeface::Style ConvertFontStyleToSkiaTypefaceStyle(int font_style) { | 45 SkTypeface::Style ConvertFontStyleToSkiaTypefaceStyle(int font_style) { |
| 101 int skia_style = SkTypeface::kNormal; | 46 int skia_style = SkTypeface::kNormal; |
| 102 if (font_style & gfx::Font::BOLD) | 47 skia_style |= (font_style & gfx::Font::BOLD) ? SkTypeface::kBold : 0; |
| 103 skia_style |= SkTypeface::kBold; | 48 skia_style |= (font_style & gfx::Font::ITALIC) ? SkTypeface::kItalic : 0; |
| 104 if (font_style & gfx::Font::ITALIC) | |
| 105 skia_style |= SkTypeface::kItalic; | |
| 106 return static_cast<SkTypeface::Style>(skia_style); | 49 return static_cast<SkTypeface::Style>(skia_style); |
| 107 } | 50 } |
| 108 | 51 |
| 109 // Given |font| and |display_width|, returns the width of the fade gradient. | 52 // Given |font| and |display_width|, returns the width of the fade gradient. |
| 110 int CalculateFadeGradientWidth(const gfx::Font& font, int display_width) { | 53 int CalculateFadeGradientWidth(const Font& font, int display_width) { |
| 111 // Fade in/out about 2.5 characters of the beginning/end of the string. | 54 // Fade in/out about 2.5 characters of the beginning/end of the string. |
| 112 // The .5 here is helpful if one of the characters is a space. | 55 // The .5 here is helpful if one of the characters is a space. |
| 113 // Use a quarter of the display width if the display width is very short. | 56 // Use a quarter of the display width if the display width is very short. |
| 114 const int average_character_width = font.GetAverageCharacterWidth(); | 57 const int average_character_width = font.GetAverageCharacterWidth(); |
| 115 const double gradient_width = std::min(average_character_width * 2.5, | 58 const double gradient_width = std::min(average_character_width * 2.5, |
| 116 display_width / 4.0); | 59 display_width / 4.0); |
| 117 DCHECK_GE(gradient_width, 0.0); | 60 DCHECK_GE(gradient_width, 0.0); |
| 118 return static_cast<int>(floor(gradient_width + 0.5)); | 61 return static_cast<int>(floor(gradient_width + 0.5)); |
| 119 } | 62 } |
| 120 | 63 |
| 121 // Appends to |positions| and |colors| values corresponding to the fade over | 64 // Appends to |positions| and |colors| values corresponding to the fade over |
| 122 // |fade_rect| from color |c0| to color |c1|. | 65 // |fade_rect| from color |c0| to color |c1|. |
| 123 void AddFadeEffect(const gfx::Rect& text_rect, | 66 void AddFadeEffect(const Rect& text_rect, |
| 124 const gfx::Rect& fade_rect, | 67 const Rect& fade_rect, |
| 125 SkColor c0, | 68 SkColor c0, |
| 126 SkColor c1, | 69 SkColor c1, |
| 127 std::vector<SkScalar>* positions, | 70 std::vector<SkScalar>* positions, |
| 128 std::vector<SkColor>* colors) { | 71 std::vector<SkColor>* colors) { |
| 129 const SkScalar left = static_cast<SkScalar>(fade_rect.x() - text_rect.x()); | 72 const SkScalar left = static_cast<SkScalar>(fade_rect.x() - text_rect.x()); |
| 130 const SkScalar width = static_cast<SkScalar>(fade_rect.width()); | 73 const SkScalar width = static_cast<SkScalar>(fade_rect.width()); |
| 131 const SkScalar p0 = left / text_rect.width(); | 74 const SkScalar p0 = left / text_rect.width(); |
| 132 const SkScalar p1 = (left + width) / text_rect.width(); | 75 const SkScalar p1 = (left + width) / text_rect.width(); |
| 133 // Prepend 0.0 to |positions|, as required by Skia. | 76 // Prepend 0.0 to |positions|, as required by Skia. |
| 134 if (positions->empty() && p0 != 0.0) { | 77 if (positions->empty() && p0 != 0.0) { |
| 135 positions->push_back(0.0); | 78 positions->push_back(0.0); |
| 136 colors->push_back(c0); | 79 colors->push_back(c0); |
| 137 } | 80 } |
| 138 positions->push_back(p0); | 81 positions->push_back(p0); |
| 139 colors->push_back(c0); | 82 colors->push_back(c0); |
| 140 positions->push_back(p1); | 83 positions->push_back(p1); |
| 141 colors->push_back(c1); | 84 colors->push_back(c1); |
| 142 } | 85 } |
| 143 | 86 |
| 144 // Creates a SkShader to fade the text, with |left_part| specifying the left | 87 // Creates a SkShader to fade the text, with |left_part| specifying the left |
| 145 // fade effect, if any, and |right_part| specifying the right fade effect. | 88 // fade effect, if any, and |right_part| specifying the right fade effect. |
| 146 skia::RefPtr<SkShader> CreateFadeShader(const gfx::Rect& text_rect, | 89 skia::RefPtr<SkShader> CreateFadeShader(const Rect& text_rect, |
| 147 const gfx::Rect& left_part, | 90 const Rect& left_part, |
| 148 const gfx::Rect& right_part, | 91 const Rect& right_part, |
| 149 SkColor color) { | 92 SkColor color) { |
| 150 // Fade alpha of 51/255 corresponds to a fade of 0.2 of the original color. | 93 // Fade alpha of 51/255 corresponds to a fade of 0.2 of the original color. |
| 151 const SkColor fade_color = SkColorSetA(color, 51); | 94 const SkColor fade_color = SkColorSetA(color, 51); |
| 152 std::vector<SkScalar> positions; | 95 std::vector<SkScalar> positions; |
| 153 std::vector<SkColor> colors; | 96 std::vector<SkColor> colors; |
| 154 | 97 |
| 155 if (!left_part.IsEmpty()) | 98 if (!left_part.IsEmpty()) |
| 156 AddFadeEffect(text_rect, left_part, fade_color, color, | 99 AddFadeEffect(text_rect, left_part, fade_color, color, |
| 157 &positions, &colors); | 100 &positions, &colors); |
| 158 if (!right_part.IsEmpty()) | 101 if (!right_part.IsEmpty()) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 170 points[0].iset(text_rect.x(), text_rect.y()); | 113 points[0].iset(text_rect.x(), text_rect.y()); |
| 171 points[1].iset(text_rect.right(), text_rect.y()); | 114 points[1].iset(text_rect.right(), text_rect.y()); |
| 172 | 115 |
| 173 return skia::AdoptRef( | 116 return skia::AdoptRef( |
| 174 SkGradientShader::CreateLinear(&points[0], &colors[0], &positions[0], | 117 SkGradientShader::CreateLinear(&points[0], &colors[0], &positions[0], |
| 175 colors.size(), SkShader::kClamp_TileMode)); | 118 colors.size(), SkShader::kClamp_TileMode)); |
| 176 } | 119 } |
| 177 | 120 |
| 178 } // namespace | 121 } // namespace |
| 179 | 122 |
| 180 namespace gfx { | |
| 181 | |
| 182 namespace internal { | 123 namespace internal { |
| 183 | 124 |
| 184 // Value of |underline_thickness_| that indicates that underline metrics have | 125 // Value of |underline_thickness_| that indicates that underline metrics have |
| 185 // not been set explicitly. | 126 // not been set explicitly. |
| 186 const SkScalar kUnderlineMetricsNotSet = -1.0f; | 127 const SkScalar kUnderlineMetricsNotSet = -1.0f; |
| 187 | 128 |
| 188 SkiaTextRenderer::SkiaTextRenderer(Canvas* canvas) | 129 SkiaTextRenderer::SkiaTextRenderer(Canvas* canvas) |
| 189 : canvas_skia_(canvas->sk_canvas()), | 130 : canvas_skia_(canvas->sk_canvas()), |
| 190 started_drawing_(false), | 131 started_drawing_(false), |
| 191 underline_thickness_(kUnderlineMetricsNotSet), | 132 underline_thickness_(kUnderlineMetricsNotSet), |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 deferred_fade_shader_ = paint_.getShader(); | 227 deferred_fade_shader_ = paint_.getShader(); |
| 287 paint_.setShader(NULL); | 228 paint_.setShader(NULL); |
| 288 canvas_skia_->saveLayer(&bounds_, NULL); | 229 canvas_skia_->saveLayer(&bounds_, NULL); |
| 289 } | 230 } |
| 290 } | 231 } |
| 291 | 232 |
| 292 const size_t byte_length = glyph_count * sizeof(glyphs[0]); | 233 const size_t byte_length = glyph_count * sizeof(glyphs[0]); |
| 293 canvas_skia_->drawPosText(&glyphs[0], byte_length, &pos[0], paint_); | 234 canvas_skia_->drawPosText(&glyphs[0], byte_length, &pos[0], paint_); |
| 294 } | 235 } |
| 295 | 236 |
| 296 // Draw underline and strike through text decorations. | 237 void SkiaTextRenderer::DrawDecorations(int x, int y, int width, bool underline, |
| 297 // Based on |SkCanvas::DrawTextDecorations()| and constants from: | 238 bool strike, bool diagonal_strike) { |
| 298 // third_party/skia/src/core/SkTextFormatParams.h | 239 if (underline) |
| 299 void SkiaTextRenderer::DrawDecorations(int x, int y, int width, | 240 DrawUnderline(x, y, width); |
| 300 const StyleRange& style) { | 241 if (strike) |
| 301 if (!style.underline && !style.strike && !style.diagonal_strike) | 242 DrawStrike(x, y, width); |
| 302 return; | 243 if (diagonal_strike) |
| 244 DrawDiagonalStrike(x, y, width); |
| 245 } |
| 303 | 246 |
| 304 // Fraction of the text size to lower a strike through below the baseline. | 247 void SkiaTextRenderer::DrawUnderline(int x, int y, int width) { |
| 305 const SkScalar kStrikeThroughOffset = (-SK_Scalar1 * 6 / 21); | 248 SkRect r = SkRect::MakeLTRB(x, y + underline_position_, x + width, |
| 306 // Fraction of the text size to lower an underline below the baseline. | 249 y + underline_position_ + underline_thickness_); |
| 307 const SkScalar kUnderlineOffset = (SK_Scalar1 / 9); | 250 if (underline_thickness_ == kUnderlineMetricsNotSet) { |
| 308 // Fraction of the text size to use for a strike through or under-line. | 251 const SkScalar text_size = paint_.getTextSize(); |
| 309 const SkScalar kLineThickness = (SK_Scalar1 / 18); | 252 r.fTop = SkScalarMulAdd(text_size, kUnderlineOffset, y); |
| 310 // Fraction of the text size to use for a top margin of a diagonal strike. | 253 r.fBottom = r.fTop + SkScalarMul(text_size, kLineThickness); |
| 311 const SkScalar kDiagonalStrikeThroughMarginOffset = (SK_Scalar1 / 4); | 254 } |
| 255 canvas_skia_->drawRect(r, paint_); |
| 256 } |
| 312 | 257 |
| 313 SkScalar text_size = paint_.getTextSize(); | 258 void SkiaTextRenderer::DrawStrike(int x, int y, int width) const { |
| 314 SkScalar height = SkScalarMul(text_size, kLineThickness); | 259 const SkScalar text_size = paint_.getTextSize(); |
| 315 SkRect r; | 260 const SkScalar height = SkScalarMul(text_size, kLineThickness); |
| 261 const SkScalar offset = SkScalarMulAdd(text_size, kStrikeThroughOffset, y); |
| 262 const SkRect r = SkRect::MakeLTRB(x, offset, x + width, offset + height); |
| 263 canvas_skia_->drawRect(r, paint_); |
| 264 } |
| 316 | 265 |
| 317 r.fLeft = x; | 266 void SkiaTextRenderer::DrawDiagonalStrike(int x, int y, int width) const { |
| 318 r.fRight = x + width; | 267 const SkScalar text_size = paint_.getTextSize(); |
| 268 const SkScalar offset = SkScalarMul(text_size, kDiagonalStrikeMarginOffset); |
| 319 | 269 |
| 320 if (style.underline) { | 270 SkPaint paint(paint_); |
| 321 if (underline_thickness_ == kUnderlineMetricsNotSet) { | 271 paint.setAntiAlias(true); |
| 322 r.fTop = SkScalarMulAdd(text_size, kUnderlineOffset, y); | 272 paint.setStyle(SkPaint::kFill_Style); |
| 323 r.fBottom = r.fTop + height; | 273 paint.setStrokeWidth(SkScalarMul(text_size, kLineThickness) * 2); |
| 324 } else { | 274 canvas_skia_->drawLine(x, y, x + width, y - text_size + offset, paint); |
| 325 r.fTop = y + underline_position_; | 275 } |
| 326 r.fBottom = r.fTop + underline_thickness_; | 276 |
| 327 } | 277 StyleIterator::StyleIterator(const BreakList<SkColor>& colors, |
| 328 canvas_skia_->drawRect(r, paint_); | 278 const std::vector<BreakList<bool> >& styles) |
| 329 } | 279 : colors_(colors), |
| 330 if (style.strike) { | 280 styles_(styles) { |
| 331 SkScalar offset = SkScalarMulAdd(text_size, kStrikeThroughOffset, y); | 281 color_ = colors_.breaks().begin(); |
| 332 r.fTop = offset; | 282 for (size_t i = 0; i < styles_.size(); ++i) |
| 333 r.fBottom = offset + height; | 283 style_.push_back(styles_[i].breaks().begin()); |
| 334 canvas_skia_->drawRect(r, paint_); | 284 } |
| 335 } | 285 |
| 336 if (style.diagonal_strike) { | 286 StyleIterator::~StyleIterator() {} |
| 337 SkScalar offset = | 287 |
| 338 SkScalarMul(text_size, kDiagonalStrikeThroughMarginOffset); | 288 size_t StyleIterator::GetNextBreak() const { |
| 339 SkPaint paint(paint_); | 289 size_t next_break = colors_.GetBreakEnd(color_); |
| 340 paint.setAntiAlias(true); | 290 for (size_t i = 0; i < NUM_TEXT_STYLES; ++i) |
| 341 paint.setStyle(SkPaint::kFill_Style); | 291 next_break = std::min(next_break, styles_[i].GetBreakEnd(style_[i])); |
| 342 paint.setStrokeWidth(height * 2); | 292 return next_break; |
| 343 canvas_skia_->drawLine( | 293 } |
| 344 SkIntToScalar(x), SkIntToScalar(y), | 294 |
| 345 SkIntToScalar(x + width), SkIntToScalar(y) - text_size + offset, | 295 void StyleIterator::UpdatePosition(size_t position) { |
| 346 paint); | 296 color_ = colors_.GetBreak(position); |
| 347 } | 297 for (size_t i = 0; i < NUM_TEXT_STYLES; ++i) |
| 298 style_[i] = styles_[i].GetBreak(position); |
| 348 } | 299 } |
| 349 | 300 |
| 350 } // namespace internal | 301 } // namespace internal |
| 351 | 302 |
| 352 | |
| 353 StyleRange::StyleRange() | |
| 354 : foreground(SK_ColorBLACK), | |
| 355 font_style(gfx::Font::NORMAL), | |
| 356 strike(false), | |
| 357 diagonal_strike(false), | |
| 358 underline(false) { | |
| 359 } | |
| 360 | |
| 361 RenderText::~RenderText() { | 303 RenderText::~RenderText() { |
| 362 } | 304 } |
| 363 | 305 |
| 364 void RenderText::SetText(const string16& text) { | 306 void RenderText::SetText(const string16& text) { |
| 365 DCHECK(!composition_range_.IsValid()); | 307 DCHECK(!composition_range_.IsValid()); |
| 366 size_t old_text_length = text_.length(); | 308 const size_t old_length = text_.length(); |
| 367 text_ = text; | 309 text_ = text; |
| 368 | 310 |
| 369 // Update the style ranges as needed. | 311 // Adjust any ranged styles and colors to accommodate a new text length. |
| 370 if (text_.empty()) { | 312 const size_t new_length = text_.length(); |
| 371 style_ranges_.clear(); | 313 if (new_length < old_length) { |
| 372 } else if (style_ranges_.empty()) { | 314 colors_.TrimBreaks(new_length); |
| 373 ApplyDefaultStyle(); | 315 for (size_t style = 0; style < NUM_TEXT_STYLES; ++style) |
| 374 } else if (text_.length() > old_text_length) { | 316 styles_[style].TrimBreaks(new_length); |
| 375 style_ranges_.back().range.set_end(text_.length()); | |
| 376 } else if (text_.length() < old_text_length) { | |
| 377 StyleRanges::iterator i; | |
| 378 for (i = style_ranges_.begin(); i != style_ranges_.end(); i++) { | |
| 379 if (i->range.start() >= text_.length()) { | |
| 380 // Style ranges are sorted and non-overlapping, so all the subsequent | |
| 381 // style ranges should be out of text_.length() as well. | |
| 382 style_ranges_.erase(i, style_ranges_.end()); | |
| 383 break; | |
| 384 } | |
| 385 } | |
| 386 // Since style ranges are sorted and non-overlapping, if there is a style | |
| 387 // range ends beyond text_.length, it must be the last one. | |
| 388 style_ranges_.back().range.set_end(text_.length()); | |
| 389 } | 317 } |
| 390 #ifndef NDEBUG | |
| 391 CheckStyleRanges(style_ranges_, text_.length()); | |
| 392 #endif | |
| 393 cached_bounds_and_offset_valid_ = false; | 318 cached_bounds_and_offset_valid_ = false; |
| 394 | 319 |
| 395 // Reset selection model. SetText should always followed by SetSelectionModel | 320 // Reset selection model. SetText should always followed by SetSelectionModel |
| 396 // or SetCursorPosition in upper layer. | 321 // or SetCursorPosition in upper layer. |
| 397 SetSelectionModel(SelectionModel()); | 322 SetSelectionModel(SelectionModel()); |
| 398 | 323 |
| 399 // Invalidate the cached text direction if it depends on the text contents. | 324 // Invalidate the cached text direction if it depends on the text contents. |
| 400 if (directionality_mode_ == DIRECTIONALITY_FROM_TEXT) | 325 if (directionality_mode_ == DIRECTIONALITY_FROM_TEXT) |
| 401 text_direction_ = base::i18n::UNKNOWN_DIRECTION; | 326 text_direction_ = base::i18n::UNKNOWN_DIRECTION; |
| 402 | 327 |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 575 } | 500 } |
| 576 | 501 |
| 577 void RenderText::SetCompositionRange(const ui::Range& composition_range) { | 502 void RenderText::SetCompositionRange(const ui::Range& composition_range) { |
| 578 CHECK(!composition_range.IsValid() || | 503 CHECK(!composition_range.IsValid() || |
| 579 ui::Range(0, text_.length()).Contains(composition_range)); | 504 ui::Range(0, text_.length()).Contains(composition_range)); |
| 580 composition_range_.set_end(composition_range.end()); | 505 composition_range_.set_end(composition_range.end()); |
| 581 composition_range_.set_start(composition_range.start()); | 506 composition_range_.set_start(composition_range.start()); |
| 582 ResetLayout(); | 507 ResetLayout(); |
| 583 } | 508 } |
| 584 | 509 |
| 585 void RenderText::ApplyStyleRange(const StyleRange& style_range) { | 510 void RenderText::SetColor(SkColor value) { |
| 586 const ui::Range& new_range = style_range.range; | 511 // TODO(msw): Fix Windows invalidation and use colors_.SetValue(value). |
| 587 if (!new_range.IsValid() || new_range.is_empty()) | 512 ApplyColor(value, ui::Range(0, text().length())); |
| 588 return; | 513 } |
| 589 CHECK(!new_range.is_reversed()); | 514 |
| 590 CHECK(ui::Range(0, text_.length()).Contains(new_range)); | 515 void RenderText::ApplyColor(SkColor value, const ui::Range& range) { |
| 591 ApplyStyleRangeImpl(&style_ranges_, style_range); | 516 DCHECK(ui::Range(0, text().length()).Contains(range)); |
| 592 #ifndef NDEBUG | 517 colors_.ApplyValue(value, range.end() >= text().length() ? |
| 593 CheckStyleRanges(style_ranges_, text_.length()); | 518 ui::Range(range.start(), INT_MAX) : range); |
| 594 #endif | 519 |
| 595 // TODO(xji): only invalidate if font or underline changes. | 520 #if defined(OS_WIN) |
| 521 // TODO(msw): Windows applies colors and decorations in the layout process. |
| 596 cached_bounds_and_offset_valid_ = false; | 522 cached_bounds_and_offset_valid_ = false; |
| 597 ResetLayout(); | 523 ResetLayout(); |
| 524 #endif |
| 598 } | 525 } |
| 599 | 526 |
| 600 void RenderText::ApplyDefaultStyle() { | 527 void RenderText::SetStyle(TextStyle style, bool value) { |
| 601 style_ranges_.clear(); | 528 // TODO(msw): Fix Windows invalidation and use styles_[style].SetValue(value). |
| 602 StyleRange style = StyleRange(default_style_); | 529 ApplyStyle(style, value, ui::Range(0, text().length())); |
| 603 style.range.set_end(text_.length()); | 530 } |
| 604 style_ranges_.push_back(style); | 531 |
| 605 cached_bounds_and_offset_valid_ = false; | 532 void RenderText::ApplyStyle(TextStyle style, |
| 606 ResetLayout(); | 533 bool value, |
| 534 const ui::Range& range) { |
| 535 DCHECK(ui::Range(0, text().length()).Contains(range)); |
| 536 styles_[style].ApplyValue(value, range.end() >= text().length() ? |
| 537 ui::Range(range.start(), INT_MAX) : range); |
| 538 |
| 539 // Only invalidate the layout on font changes; not for colors or decorations. |
| 540 bool invalidate = (style == BOLD) || (style == ITALIC); |
| 541 #if defined(OS_WIN) |
| 542 // TODO(msw): Windows applies colors and decorations in the layout process. |
| 543 invalidate = true; |
| 544 #endif |
| 545 if (invalidate) { |
| 546 cached_bounds_and_offset_valid_ = false; |
| 547 ResetLayout(); |
| 548 } |
| 607 } | 549 } |
| 608 | 550 |
| 609 void RenderText::SetDirectionalityMode(DirectionalityMode mode) { | 551 void RenderText::SetDirectionalityMode(DirectionalityMode mode) { |
| 610 if (mode == directionality_mode_) | 552 if (mode == directionality_mode_) |
| 611 return; | 553 return; |
| 612 | 554 |
| 613 directionality_mode_ = mode; | 555 directionality_mode_ = mode; |
| 614 text_direction_ = base::i18n::UNKNOWN_DIRECTION; | 556 text_direction_ = base::i18n::UNKNOWN_DIRECTION; |
| 615 ResetLayout(); | 557 ResetLayout(); |
| 616 } | 558 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 644 | 586 |
| 645 VisualCursorDirection RenderText::GetVisualDirectionOfLogicalEnd() { | 587 VisualCursorDirection RenderText::GetVisualDirectionOfLogicalEnd() { |
| 646 return GetTextDirection() == base::i18n::LEFT_TO_RIGHT ? | 588 return GetTextDirection() == base::i18n::LEFT_TO_RIGHT ? |
| 647 CURSOR_RIGHT : CURSOR_LEFT; | 589 CURSOR_RIGHT : CURSOR_LEFT; |
| 648 } | 590 } |
| 649 | 591 |
| 650 void RenderText::Draw(Canvas* canvas) { | 592 void RenderText::Draw(Canvas* canvas) { |
| 651 EnsureLayout(); | 593 EnsureLayout(); |
| 652 | 594 |
| 653 if (clip_to_display_rect()) { | 595 if (clip_to_display_rect()) { |
| 654 gfx::Rect clip_rect(display_rect()); | 596 Rect clip_rect(display_rect()); |
| 655 clip_rect.Inset(ShadowValue::GetMargin(text_shadows_)); | 597 clip_rect.Inset(ShadowValue::GetMargin(text_shadows_)); |
| 656 | 598 |
| 657 canvas->Save(); | 599 canvas->Save(); |
| 658 canvas->ClipRect(clip_rect); | 600 canvas->ClipRect(clip_rect); |
| 659 } | 601 } |
| 660 | 602 |
| 661 if (!text().empty()) | 603 if (!text().empty()) |
| 662 DrawSelection(canvas); | 604 DrawSelection(canvas); |
| 663 | 605 |
| 664 DrawCursor(canvas); | 606 DrawCursor(canvas); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 745 text_shadows_ = shadows; | 687 text_shadows_ = shadows; |
| 746 } | 688 } |
| 747 | 689 |
| 748 RenderText::RenderText() | 690 RenderText::RenderText() |
| 749 : horizontal_alignment_(base::i18n::IsRTL() ? ALIGN_RIGHT : ALIGN_LEFT), | 691 : horizontal_alignment_(base::i18n::IsRTL() ? ALIGN_RIGHT : ALIGN_LEFT), |
| 750 directionality_mode_(DIRECTIONALITY_FROM_TEXT), | 692 directionality_mode_(DIRECTIONALITY_FROM_TEXT), |
| 751 text_direction_(base::i18n::UNKNOWN_DIRECTION), | 693 text_direction_(base::i18n::UNKNOWN_DIRECTION), |
| 752 cursor_enabled_(true), | 694 cursor_enabled_(true), |
| 753 cursor_visible_(false), | 695 cursor_visible_(false), |
| 754 insert_mode_(true), | 696 insert_mode_(true), |
| 755 cursor_color_(kDefaultCursorColor), | 697 cursor_color_(kDefaultColor), |
| 756 selection_color_(kDefaultSelectionColor), | 698 selection_color_(kDefaultColor), |
| 757 selection_background_focused_color_(kDefaultSelectionBackgroundColor), | 699 selection_background_focused_color_(kDefaultSelectionBackgroundColor), |
| 758 selection_background_unfocused_color_(kDefaultSelectionBackgroundColor), | 700 selection_background_unfocused_color_(kDefaultSelectionBackgroundColor), |
| 759 focused_(false), | 701 focused_(false), |
| 760 composition_range_(ui::Range::InvalidRange()), | 702 composition_range_(ui::Range::InvalidRange()), |
| 703 colors_(kDefaultColor), |
| 704 styles_(NUM_TEXT_STYLES), |
| 705 composition_and_selection_styles_applied_(false), |
| 761 obscured_(false), | 706 obscured_(false), |
| 762 fade_head_(false), | 707 fade_head_(false), |
| 763 fade_tail_(false), | 708 fade_tail_(false), |
| 764 background_is_transparent_(false), | 709 background_is_transparent_(false), |
| 765 clip_to_display_rect_(true), | 710 clip_to_display_rect_(true), |
| 766 cached_bounds_and_offset_valid_(false) { | 711 cached_bounds_and_offset_valid_(false) { |
| 767 } | 712 } |
| 768 | 713 |
| 769 const Vector2d& RenderText::GetUpdatedDisplayOffset() { | 714 const Vector2d& RenderText::GetUpdatedDisplayOffset() { |
| 770 UpdateCachedBoundsAndOffset(); | 715 UpdateCachedBoundsAndOffset(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 795 void RenderText::SetSelectionModel(const SelectionModel& model) { | 740 void RenderText::SetSelectionModel(const SelectionModel& model) { |
| 796 DCHECK_LE(model.selection().GetMax(), text().length()); | 741 DCHECK_LE(model.selection().GetMax(), text().length()); |
| 797 selection_model_ = model; | 742 selection_model_ = model; |
| 798 cached_bounds_and_offset_valid_ = false; | 743 cached_bounds_and_offset_valid_ = false; |
| 799 } | 744 } |
| 800 | 745 |
| 801 const string16& RenderText::GetLayoutText() const { | 746 const string16& RenderText::GetLayoutText() const { |
| 802 return obscured() ? obscured_text_ : text(); | 747 return obscured() ? obscured_text_ : text(); |
| 803 } | 748 } |
| 804 | 749 |
| 805 void RenderText::ApplyCompositionAndSelectionStyles( | 750 void RenderText::ApplyCompositionAndSelectionStyles() { |
| 806 StyleRanges* style_ranges) { | 751 // Save the underline and color breaks to undo the temporary styles later. |
| 807 // TODO(msw): This pattern ought to be reconsidered; what about composition | 752 DCHECK(!composition_and_selection_styles_applied_); |
| 808 // and selection overlaps, retain existing local style features? | 753 saved_colors_ = colors_; |
| 809 // Apply a composition style override to a copy of the style ranges. | 754 saved_underlines_ = styles_[UNDERLINE]; |
| 810 if (composition_range_.IsValid() && !composition_range_.is_empty()) { | 755 |
| 811 StyleRange composition_style(default_style_); | 756 // Apply an underline to the composition range in |underlines|. |
| 812 composition_style.underline = true; | 757 if (composition_range_.IsValid() && !composition_range_.is_empty()) |
| 813 composition_style.range = composition_range_; | 758 styles_[UNDERLINE].ApplyValue(true, composition_range_); |
| 814 ApplyStyleRangeImpl(style_ranges, composition_style); | 759 |
| 760 // Apply the selected text color to the selection range. |
| 761 if (!selection().is_empty()) { |
| 762 const ui::Range range(selection().GetMin(), selection().GetMax()); |
| 763 colors_.ApplyValue(selection_color_, range); |
| 815 } | 764 } |
| 816 // Apply a selection style override to a copy of the style ranges. | 765 // Apply the selected text color to the cursor range in overtype mode. |
| 817 if (!selection().is_empty()) { | 766 if (!insert_mode_ && cursor_visible() && focused()) { |
| 818 StyleRange selection_style(default_style_); | 767 const size_t cursor = cursor_position(); |
| 819 selection_style.foreground = selection_color_; | 768 const size_t next = IndexOfAdjacentGrapheme(cursor, CURSOR_FORWARD); |
| 820 selection_style.range = ui::Range(selection().GetMin(), | 769 colors_.ApplyValue(selection_color_, ui::Range(cursor, next)); |
| 821 selection().GetMax()); | |
| 822 ApplyStyleRangeImpl(style_ranges, selection_style); | |
| 823 } | 770 } |
| 824 // Apply replacement-mode style override to a copy of the style ranges. | 771 composition_and_selection_styles_applied_ = true; |
| 825 // | 772 } |
| 826 // TODO(xji): NEED TO FIX FOR WINDOWS ASAP. Windows call this function (to | 773 |
| 827 // apply styles) in ItemizeLogicalText(). In order for the cursor's underline | 774 void RenderText::UndoCompositionAndSelectionStyles() { |
| 828 // character to be drawn correctly, we will need to re-layout the text. It's | 775 // Restore the underline and color breaks to undo the temporary styles. |
| 829 // not practical to do layout on every cursor blink. We need to fix Windows | 776 DCHECK(composition_and_selection_styles_applied_); |
| 830 // port to apply styles during drawing phase like Linux port does. | 777 colors_ = saved_colors_; |
| 831 // http://crbug.com/110109 | 778 styles_[UNDERLINE] = saved_underlines_; |
| 832 if (!insert_mode_ && cursor_visible() && focused()) { | 779 composition_and_selection_styles_applied_ = false; |
| 833 StyleRange replacement_mode_style(default_style_); | |
| 834 replacement_mode_style.foreground = selection_color_; | |
| 835 size_t cursor = cursor_position(); | |
| 836 replacement_mode_style.range.set_start(cursor); | |
| 837 replacement_mode_style.range.set_end( | |
| 838 IndexOfAdjacentGrapheme(cursor, CURSOR_FORWARD)); | |
| 839 ApplyStyleRangeImpl(style_ranges, replacement_mode_style); | |
| 840 } | |
| 841 } | 780 } |
| 842 | 781 |
| 843 Vector2d RenderText::GetTextOffset() { | 782 Vector2d RenderText::GetTextOffset() { |
| 844 Vector2d offset = display_rect().OffsetFromOrigin(); | 783 Vector2d offset = display_rect().OffsetFromOrigin(); |
| 845 offset.Add(GetUpdatedDisplayOffset()); | 784 offset.Add(GetUpdatedDisplayOffset()); |
| 846 offset.Add(GetAlignmentOffset()); | 785 offset.Add(GetAlignmentOffset()); |
| 847 return offset; | 786 return offset; |
| 848 } | 787 } |
| 849 | 788 |
| 850 Point RenderText::ToTextPoint(const Point& point) { | 789 Point RenderText::ToTextPoint(const Point& point) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 892 | 831 |
| 893 bool fade_left = fade_head(); | 832 bool fade_left = fade_head(); |
| 894 bool fade_right = fade_tail(); | 833 bool fade_right = fade_tail(); |
| 895 // Under RTL, |fade_right| == |fade_head|. | 834 // Under RTL, |fade_right| == |fade_head|. |
| 896 // TODO(asvitkine): This is currently not based on GetTextDirection() because | 835 // TODO(asvitkine): This is currently not based on GetTextDirection() because |
| 897 // RenderTextWin does not return a direction that's based on | 836 // RenderTextWin does not return a direction that's based on |
| 898 // the text content. | 837 // the text content. |
| 899 if (horizontal_alignment() == ALIGN_RIGHT) | 838 if (horizontal_alignment() == ALIGN_RIGHT) |
| 900 std::swap(fade_left, fade_right); | 839 std::swap(fade_left, fade_right); |
| 901 | 840 |
| 902 gfx::Rect solid_part = display_rect(); | 841 Rect solid_part = display_rect(); |
| 903 gfx::Rect left_part; | 842 Rect left_part; |
| 904 gfx::Rect right_part; | 843 Rect right_part; |
| 905 if (fade_left) { | 844 if (fade_left) { |
| 906 left_part = solid_part; | 845 left_part = solid_part; |
| 907 left_part.Inset(0, 0, solid_part.width() - gradient_width, 0); | 846 left_part.Inset(0, 0, solid_part.width() - gradient_width, 0); |
| 908 solid_part.Inset(gradient_width, 0, 0, 0); | 847 solid_part.Inset(gradient_width, 0, 0, 0); |
| 909 } | 848 } |
| 910 if (fade_right) { | 849 if (fade_right) { |
| 911 right_part = solid_part; | 850 right_part = solid_part; |
| 912 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0); | 851 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0); |
| 913 solid_part.Inset(0, 0, gradient_width, 0); | 852 solid_part.Inset(0, 0, gradient_width, 0); |
| 914 } | 853 } |
| 915 | 854 |
| 916 gfx::Rect text_rect = display_rect(); | 855 Rect text_rect = display_rect(); |
| 917 text_rect.Inset(GetAlignmentOffset().x(), 0, 0, 0); | 856 text_rect.Inset(GetAlignmentOffset().x(), 0, 0, 0); |
| 918 | 857 |
| 919 const SkColor color = default_style().foreground; | 858 skia::RefPtr<SkShader> shader = CreateFadeShader( |
| 920 skia::RefPtr<SkShader> shader = | 859 text_rect, left_part, right_part, colors_.breaks().front().second); |
| 921 CreateFadeShader(text_rect, left_part, right_part, color); | |
| 922 if (shader) | 860 if (shader) |
| 923 renderer->SetShader(shader.get(), display_rect()); | 861 renderer->SetShader(shader.get(), display_rect()); |
| 924 } | 862 } |
| 925 | 863 |
| 926 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { | 864 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { |
| 927 skia::RefPtr<SkDrawLooper> looper = | 865 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(text_shadows_); |
| 928 gfx::CreateShadowDrawLooper(text_shadows_); | |
| 929 renderer->SetDrawLooper(looper.get()); | 866 renderer->SetDrawLooper(looper.get()); |
| 930 } | 867 } |
| 931 | 868 |
| 932 // static | 869 // static |
| 933 bool RenderText::RangeContainsCaret(const ui::Range& range, | 870 bool RenderText::RangeContainsCaret(const ui::Range& range, |
| 934 size_t caret_pos, | 871 size_t caret_pos, |
| 935 LogicalCursorDirection caret_affinity) { | 872 LogicalCursorDirection caret_affinity) { |
| 936 // NB: exploits unsigned wraparound (WG14/N1124 section 6.2.5 paragraph 9). | 873 // NB: exploits unsigned wraparound (WG14/N1124 section 6.2.5 paragraph 9). |
| 937 size_t adjacent = (caret_affinity == CURSOR_BACKWARD) ? | 874 size_t adjacent = (caret_affinity == CURSOR_BACKWARD) ? |
| 938 caret_pos - 1 : caret_pos + 1; | 875 caret_pos - 1 : caret_pos + 1; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 992 } else if (display_offset_.x() != 0) { | 929 } else if (display_offset_.x() != 0) { |
| 993 // Reduce the pan offset to show additional overflow text when the display | 930 // Reduce the pan offset to show additional overflow text when the display |
| 994 // width increases. | 931 // width increases. |
| 995 const int negate_rtl = horizontal_alignment_ == ALIGN_RIGHT ? -1 : 1; | 932 const int negate_rtl = horizontal_alignment_ == ALIGN_RIGHT ? -1 : 1; |
| 996 const int offset = negate_rtl * display_offset_.x(); | 933 const int offset = negate_rtl * display_offset_.x(); |
| 997 if (display_width > (content_width + offset)) { | 934 if (display_width > (content_width + offset)) { |
| 998 delta_x = negate_rtl * (display_width - (content_width + offset)); | 935 delta_x = negate_rtl * (display_width - (content_width + offset)); |
| 999 } | 936 } |
| 1000 } | 937 } |
| 1001 | 938 |
| 1002 gfx::Vector2d delta_offset(delta_x, 0); | 939 Vector2d delta_offset(delta_x, 0); |
| 1003 display_offset_ += delta_offset; | 940 display_offset_ += delta_offset; |
| 1004 cursor_bounds_ += delta_offset; | 941 cursor_bounds_ += delta_offset; |
| 1005 } | 942 } |
| 1006 | 943 |
| 1007 void RenderText::DrawSelection(Canvas* canvas) { | 944 void RenderText::DrawSelection(Canvas* canvas) { |
| 1008 const SkColor color = focused() ? selection_background_focused_color_ : | 945 const SkColor color = focused() ? selection_background_focused_color_ : |
| 1009 selection_background_unfocused_color_; | 946 selection_background_unfocused_color_; |
| 1010 const std::vector<Rect> sel = GetSubstringBounds(selection()); | 947 const std::vector<Rect> sel = GetSubstringBounds(selection()); |
| 1011 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) | 948 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) |
| 1012 canvas->FillRect(*i, color); | 949 canvas->FillRect(*i, color); |
| 1013 } | 950 } |
| 1014 | 951 |
| 1015 void RenderText::DrawCursor(Canvas* canvas) { | 952 void RenderText::DrawCursor(Canvas* canvas) { |
| 1016 // Paint cursor. Replace cursor is drawn as rectangle for now. | 953 // Paint cursor. Replace cursor is drawn as rectangle for now. |
| 1017 // TODO(msw): Draw a better cursor with a better indication of association. | 954 // TODO(msw): Draw a better cursor with a better indication of association. |
| 1018 if (cursor_enabled() && cursor_visible() && focused()) { | 955 if (cursor_enabled() && cursor_visible() && focused()) { |
| 1019 const Rect& bounds = GetUpdatedCursorBounds(); | 956 const Rect& bounds = GetUpdatedCursorBounds(); |
| 1020 DCHECK(bounds.width()); | 957 DCHECK(bounds.width()); |
| 1021 canvas->FillRect(bounds, cursor_color_); | 958 canvas->FillRect(bounds, cursor_color_); |
| 1022 } | 959 } |
| 1023 } | 960 } |
| 1024 | 961 |
| 1025 } // namespace gfx | 962 } // namespace gfx |
| OLD | NEW |