Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: ui/gfx/render_text.cc

Issue 1493713002: Adjust text fade width and alpha (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Apply review suggestions Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include <climits> 8 #include <climits>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 } 86 }
87 87
88 // Converts |Font::FontStyle| flags to |SkTypeface::Style| flags. 88 // Converts |Font::FontStyle| flags to |SkTypeface::Style| flags.
89 SkTypeface::Style ConvertFontStyleToSkiaTypefaceStyle(int font_style) { 89 SkTypeface::Style ConvertFontStyleToSkiaTypefaceStyle(int font_style) {
90 int skia_style = SkTypeface::kNormal; 90 int skia_style = SkTypeface::kNormal;
91 skia_style |= (font_style & Font::BOLD) ? SkTypeface::kBold : 0; 91 skia_style |= (font_style & Font::BOLD) ? SkTypeface::kBold : 0;
92 skia_style |= (font_style & Font::ITALIC) ? SkTypeface::kItalic : 0; 92 skia_style |= (font_style & Font::ITALIC) ? SkTypeface::kItalic : 0;
93 return static_cast<SkTypeface::Style>(skia_style); 93 return static_cast<SkTypeface::Style>(skia_style);
94 } 94 }
95 95
96 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.
97 return static_cast<int>(floor(value));
98 }
99
96 // Given |font| and |display_width|, returns the width of the fade gradient. 100 // Given |font| and |display_width|, returns the width of the fade gradient.
97 int CalculateFadeGradientWidth(const FontList& font_list, int display_width) { 101 int CalculateFadeGradientWidth(const FontList& font_list, int display_width) {
98 // Fade in/out about 2.5 characters of the beginning/end of the string. 102 // Fade in/out about 3 characters of the beginning/end of the string.
99 // The .5 here is helpful if one of the characters is a space. 103 // 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.
100 // Use a quarter of the display width if the display width is very short. 104 // Use a 1/3 of the display width if the display width is very short.
101 const int average_character_width = font_list.GetExpectedTextWidth(1); 105 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.
102 const double gradient_width = std::min(average_character_width * 2.5, 106 const double gradient_width =
103 display_width / 4.0); 107 std::min(average_character_width, display_width / 3.0);
104 DCHECK_GE(gradient_width, 0.0); 108 DCHECK_GE(gradient_width, 0.0);
105 return static_cast<int>(floor(gradient_width + 0.5)); 109 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.
106 } 110 }
107 111
108 // Appends to |positions| and |colors| values corresponding to the fade over 112 // Appends to |positions| and |colors| values corresponding to the fade over
109 // |fade_rect| from color |c0| to color |c1|. 113 // |fade_rect| from color |c0| to color |c1|.
110 void AddFadeEffect(const Rect& text_rect, 114 void AddFadeEffect(const Rect& text_rect,
111 const Rect& fade_rect, 115 const Rect& fade_rect,
112 SkColor c0, 116 SkColor c0,
113 SkColor c1, 117 SkColor c1,
114 std::vector<SkScalar>* positions, 118 std::vector<SkScalar>* positions,
115 std::vector<SkColor>* colors) { 119 std::vector<SkColor>* colors) {
116 const SkScalar left = static_cast<SkScalar>(fade_rect.x() - text_rect.x()); 120 const SkScalar left = static_cast<SkScalar>(fade_rect.x() - text_rect.x());
117 const SkScalar width = static_cast<SkScalar>(fade_rect.width()); 121 const SkScalar width = static_cast<SkScalar>(fade_rect.width());
118 const SkScalar p0 = left / text_rect.width(); 122 const SkScalar p0 = left / text_rect.width();
119 const SkScalar p1 = (left + width) / text_rect.width(); 123 const SkScalar p1 = (left + width) / text_rect.width();
120 // Prepend 0.0 to |positions|, as required by Skia. 124 // Prepend 0.0 to |positions|, as required by Skia.
121 if (positions->empty() && p0 != 0.0) { 125 if (positions->empty() && p0 != 0.0) {
122 positions->push_back(0.0); 126 positions->push_back(0.0);
123 colors->push_back(c0); 127 colors->push_back(c0);
124 } 128 }
125 positions->push_back(p0); 129 positions->push_back(p0);
126 colors->push_back(c0); 130 colors->push_back(c0);
127 positions->push_back(p1); 131 positions->push_back(p1);
128 colors->push_back(c1); 132 colors->push_back(c1);
129 } 133 }
130 134
131 // Creates a SkShader to fade the text, with |left_part| specifying the left 135 // Creates a SkShader to fade the text, with |left_part| specifying the left
132 // fade effect, if any, and |right_part| specifying the right fade effect. 136 // fade effect, if any, and |right_part| specifying the right fade effect.
133 skia::RefPtr<SkShader> CreateFadeShader(const Rect& text_rect, 137 skia::RefPtr<SkShader> CreateFadeShader(const FontList& font_list,
138 const Rect& text_rect,
134 const Rect& left_part, 139 const Rect& left_part,
135 const Rect& right_part, 140 const Rect& right_part,
136 SkColor color) { 141 SkColor color) {
137 // Fade alpha of 51/255 corresponds to a fade of 0.2 of the original color. 142 // In general, fade down to 0 alpha. But when the available width is less
138 const SkColor fade_color = SkColorSetA(color, 51); 143 // than four characters, linearly ramp up the fade target alpha to as high as
144 // 20% at zero width. This allows the user to see the last faded characters a
145 // little better when there are only a few characters shown.
146 const float width_fraction =
147 text_rect.width() / static_cast<float>(font_list.GetExpectedTextWidth(4));
148 const SkAlpha kAlphaAtZeroWidth = 51;
149 const SkAlpha alpha = (width_fraction < 1) ?
150 static_cast<SkAlpha>(round((1 - width_fraction) * kAlphaAtZeroWidth)) : 0;
151 const SkColor fade_color = SkColorSetA(color, alpha);
152
139 std::vector<SkScalar> positions; 153 std::vector<SkScalar> positions;
140 std::vector<SkColor> colors; 154 std::vector<SkColor> colors;
141 155
142 if (!left_part.IsEmpty()) 156 if (!left_part.IsEmpty())
143 AddFadeEffect(text_rect, left_part, fade_color, color, 157 AddFadeEffect(text_rect, left_part, fade_color, color,
144 &positions, &colors); 158 &positions, &colors);
145 if (!right_part.IsEmpty()) 159 if (!right_part.IsEmpty())
146 AddFadeEffect(text_rect, right_part, color, fade_color, 160 AddFadeEffect(text_rect, right_part, color, fade_color,
147 &positions, &colors); 161 &positions, &colors);
148 DCHECK(!positions.empty()); 162 DCHECK(!positions.empty());
(...skipping 1053 matching lines...) Expand 10 before | Expand all | Expand 10 after
1202 if (horizontal_alignment != ALIGN_RIGHT) { 1216 if (horizontal_alignment != ALIGN_RIGHT) {
1203 right_part = solid_part; 1217 right_part = solid_part;
1204 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0); 1218 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0);
1205 solid_part.Inset(0, 0, gradient_width, 0); 1219 solid_part.Inset(0, 0, gradient_width, 0);
1206 } 1220 }
1207 1221
1208 Rect text_rect = display_rect(); 1222 Rect text_rect = display_rect();
1209 text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0); 1223 text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0);
1210 1224
1211 // TODO(msw): Use the actual text colors corresponding to each faded part. 1225 // TODO(msw): Use the actual text colors corresponding to each faded part.
1212 skia::RefPtr<SkShader> shader = CreateFadeShader( 1226 skia::RefPtr<SkShader> shader =
1213 text_rect, left_part, right_part, colors_.breaks().front().second); 1227 CreateFadeShader(font_list(), text_rect, left_part, right_part,
1228 colors_.breaks().front().second);
1214 if (shader) 1229 if (shader)
1215 renderer->SetShader(shader.get()); 1230 renderer->SetShader(shader.get());
1216 } 1231 }
1217 1232
1218 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { 1233 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) {
1219 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_); 1234 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_);
1220 renderer->SetDrawLooper(looper.get()); 1235 renderer->SetDrawLooper(looper.get());
1221 } 1236 }
1222 1237
1223 base::i18n::TextDirection RenderText::GetTextDirection( 1238 base::i18n::TextDirection RenderText::GetTextDirection(
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
1521 1536
1522 SetDisplayOffset(display_offset_.x() + delta_x); 1537 SetDisplayOffset(display_offset_.x() + delta_x);
1523 } 1538 }
1524 1539
1525 void RenderText::DrawSelection(Canvas* canvas) { 1540 void RenderText::DrawSelection(Canvas* canvas) {
1526 for (const Rect& s : GetSubstringBounds(selection())) 1541 for (const Rect& s : GetSubstringBounds(selection()))
1527 canvas->FillRect(s, selection_background_focused_color_); 1542 canvas->FillRect(s, selection_background_focused_color_);
1528 } 1543 }
1529 1544
1530 } // namespace gfx 1545 } // namespace gfx
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698