Chromium Code Reviews| Index: ui/gfx/render_text.cc |
| diff --git a/ui/gfx/render_text.cc b/ui/gfx/render_text.cc |
| index 27c9e5ada81279d215723897c8cafe6b79820023..9d2d557369017e7cba9e46d38acde138824c6a91 100644 |
| --- a/ui/gfx/render_text.cc |
| +++ b/ui/gfx/render_text.cc |
| @@ -93,16 +93,20 @@ SkTypeface::Style ConvertFontStyleToSkiaTypefaceStyle(int font_style) { |
| return static_cast<SkTypeface::Style>(skia_style); |
| } |
| +int round(double value) { |
|
Peter Kasting
2015/12/03 17:50:17
Change this to taking a float, then follow my next
Tomasz Moniuszko
2015/12/04 11:08:18
Done.
|
| + return static_cast<int>(floor(value)); |
| +} |
| + |
| // Given |font| and |display_width|, returns the width of the fade gradient. |
| int CalculateFadeGradientWidth(const FontList& font_list, int display_width) { |
| - // Fade in/out about 2.5 characters of the beginning/end of the string. |
| + // Fade in/out about 3 characters of the beginning/end of the string. |
| // The .5 here is helpful if one of the characters is a space. |
|
msw
2015/12/03 17:08:32
nit: remove this line, it's not really applicable
Tomasz Moniuszko
2015/12/04 11:08:18
Done.
|
| - // Use a quarter of the display width if the display width is very short. |
| - const int average_character_width = font_list.GetExpectedTextWidth(1); |
| - const double gradient_width = std::min(average_character_width * 2.5, |
| - display_width / 4.0); |
| + // Use a 1/3 of the display width if the display width is very short. |
| + const double average_character_width = font_list.GetExpectedTextWidth(3); |
|
Peter Kasting
2015/12/03 17:50:17
Avoid unnecessary floating-point math, and fix the
Tomasz Moniuszko
2015/12/04 11:08:18
Done.
|
| + const double gradient_width = |
| + std::min(average_character_width, display_width / 3.0); |
| DCHECK_GE(gradient_width, 0.0); |
| - return static_cast<int>(floor(gradient_width + 0.5)); |
| + return round(gradient_width + 0.5); |
|
msw
2015/12/03 17:08:32
nit: shouldn't |round| add the 0.5 internally? (ot
Peter Kasting
2015/12/03 17:50:17
Yes, round() should be adding the 0.5. And the co
Tomasz Moniuszko
2015/12/04 11:08:18
Done.
|
| } |
| // Appends to |positions| and |colors| values corresponding to the fade over |
| @@ -130,12 +134,22 @@ void AddFadeEffect(const Rect& text_rect, |
| // Creates a SkShader to fade the text, with |left_part| specifying the left |
| // fade effect, if any, and |right_part| specifying the right fade effect. |
| -skia::RefPtr<SkShader> CreateFadeShader(const Rect& text_rect, |
| +skia::RefPtr<SkShader> CreateFadeShader(const FontList& font_list, |
| + const Rect& text_rect, |
| const Rect& left_part, |
| const Rect& right_part, |
| SkColor color) { |
| - // Fade alpha of 51/255 corresponds to a fade of 0.2 of the original color. |
| - const SkColor fade_color = SkColorSetA(color, 51); |
| + // In general, fade down to 0 alpha. But when the available width is less |
| + // than four characters, linearly ramp up the fade target alpha to as high as |
| + // 20% at zero width. This allows the user to see the last faded characters a |
| + // little better when there are only a few characters shown. |
| + const float width_fraction = |
| + text_rect.width() / static_cast<float>(font_list.GetExpectedTextWidth(4)); |
| + const SkAlpha kAlphaAtZeroWidth = 51; |
| + const SkAlpha alpha = (width_fraction < 1) ? |
| + static_cast<SkAlpha>(round((1 - width_fraction) * kAlphaAtZeroWidth)) : 0; |
| + const SkColor fade_color = SkColorSetA(color, alpha); |
| + |
| std::vector<SkScalar> positions; |
| std::vector<SkColor> colors; |
| @@ -1209,8 +1223,9 @@ void RenderText::ApplyFadeEffects(internal::SkiaTextRenderer* renderer) { |
| text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0); |
| // TODO(msw): Use the actual text colors corresponding to each faded part. |
| - skia::RefPtr<SkShader> shader = CreateFadeShader( |
| - text_rect, left_part, right_part, colors_.breaks().front().second); |
| + skia::RefPtr<SkShader> shader = |
| + CreateFadeShader(font_list(), text_rect, left_part, right_part, |
| + colors_.breaks().front().second); |
| if (shader) |
| renderer->SetShader(shader.get()); |
| } |