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 #include <climits> | 8 #include <climits> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 // Given |font| and |display_width|, returns the width of the fade gradient. | 96 // Given |font| and |display_width|, returns the width of the fade gradient. |
97 int CalculateFadeGradientWidth(const FontList& font_list, int display_width) { | 97 int CalculateFadeGradientWidth(const FontList& font_list, int display_width) { |
98 // Fade in/out about 2.5 characters of the beginning/end of the string. | 98 // 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. | 99 // The .5 here is helpful if one of the characters is a space. |
100 // Use a quarter of the display width if the display width is very short. | 100 // 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); | 101 const int average_character_width = font_list.GetExpectedTextWidth(1); |
Peter Kasting
2015/12/03 05:29:25
While here: Instead of getting the width of 1 char
Tomasz Moniuszko
2015/12/03 11:18:42
Done.
| |
102 const double gradient_width = std::min(average_character_width * 2.5, | 102 const double gradient_width = |
103 display_width / 4.0); | 103 std::min(average_character_width * 3.0, display_width / 3.0); |
104 DCHECK_GE(gradient_width, 0.0); | 104 DCHECK_GE(gradient_width, 0.0); |
105 return static_cast<int>(floor(gradient_width + 0.5)); | 105 return static_cast<int>(floor(gradient_width + 0.5)); |
Peter Kasting
2015/12/03 05:29:25
Nit: For now, define a round() helper in this file
Tomasz Moniuszko
2015/12/03 11:18:42
Done.
| |
106 } | 106 } |
107 | 107 |
108 // Appends to |positions| and |colors| values corresponding to the fade over | 108 // Appends to |positions| and |colors| values corresponding to the fade over |
109 // |fade_rect| from color |c0| to color |c1|. | 109 // |fade_rect| from color |c0| to color |c1|. |
110 void AddFadeEffect(const Rect& text_rect, | 110 void AddFadeEffect(const Rect& text_rect, |
111 const Rect& fade_rect, | 111 const Rect& fade_rect, |
112 SkColor c0, | 112 SkColor c0, |
113 SkColor c1, | 113 SkColor c1, |
114 std::vector<SkScalar>* positions, | 114 std::vector<SkScalar>* positions, |
115 std::vector<SkColor>* colors) { | 115 std::vector<SkColor>* colors) { |
116 const SkScalar left = static_cast<SkScalar>(fade_rect.x() - text_rect.x()); | 116 const SkScalar left = static_cast<SkScalar>(fade_rect.x() - text_rect.x()); |
117 const SkScalar width = static_cast<SkScalar>(fade_rect.width()); | 117 const SkScalar width = static_cast<SkScalar>(fade_rect.width()); |
118 const SkScalar p0 = left / text_rect.width(); | 118 const SkScalar p0 = left / text_rect.width(); |
119 const SkScalar p1 = (left + width) / text_rect.width(); | 119 const SkScalar p1 = (left + width) / text_rect.width(); |
120 // Prepend 0.0 to |positions|, as required by Skia. | 120 // Prepend 0.0 to |positions|, as required by Skia. |
121 if (positions->empty() && p0 != 0.0) { | 121 if (positions->empty() && p0 != 0.0) { |
122 positions->push_back(0.0); | 122 positions->push_back(0.0); |
123 colors->push_back(c0); | 123 colors->push_back(c0); |
124 } | 124 } |
125 positions->push_back(p0); | 125 positions->push_back(p0); |
126 colors->push_back(c0); | 126 colors->push_back(c0); |
127 positions->push_back(p1); | 127 positions->push_back(p1); |
128 colors->push_back(c1); | 128 colors->push_back(c1); |
129 } | 129 } |
130 | 130 |
131 // Creates a SkShader to fade the text, with |left_part| specifying the left | 131 // 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. | 132 // fade effect, if any, and |right_part| specifying the right fade effect. |
133 skia::RefPtr<SkShader> CreateFadeShader(const Rect& text_rect, | 133 skia::RefPtr<SkShader> CreateFadeShader(const FontList& font_list, |
134 const Rect& text_rect, | |
134 const Rect& left_part, | 135 const Rect& left_part, |
135 const Rect& right_part, | 136 const Rect& right_part, |
136 SkColor color) { | 137 SkColor color) { |
137 // Fade alpha of 51/255 corresponds to a fade of 0.2 of the original color. | 138 // Use 0 fade target alpha. But if text is narrow (less than 4 average |
138 const SkColor fade_color = SkColorSetA(color, 51); | 139 // characters) linearly increase fade target alpha up to 51/255 what |
140 // corresponds to a fade of 0.2 of the original color. | |
141 const int average_character_width = font_list.GetExpectedTextWidth(1); | |
142 int alpha = | |
143 text_rect.width() > 4 * average_character_width | |
144 ? 0 | |
145 : static_cast<int>( | |
146 51 * (1 - text_rect.width() / (4.0 * average_character_width))); | |
147 const SkColor fade_color = SkColorSetA(color, alpha); | |
Peter Kasting
2015/12/03 05:29:25
How about this:
// In general, fade down to 0 a
Tomasz Moniuszko
2015/12/03 11:18:42
Done.
| |
139 std::vector<SkScalar> positions; | 148 std::vector<SkScalar> positions; |
140 std::vector<SkColor> colors; | 149 std::vector<SkColor> colors; |
141 | 150 |
142 if (!left_part.IsEmpty()) | 151 if (!left_part.IsEmpty()) |
143 AddFadeEffect(text_rect, left_part, fade_color, color, | 152 AddFadeEffect(text_rect, left_part, fade_color, color, |
144 &positions, &colors); | 153 &positions, &colors); |
145 if (!right_part.IsEmpty()) | 154 if (!right_part.IsEmpty()) |
146 AddFadeEffect(text_rect, right_part, color, fade_color, | 155 AddFadeEffect(text_rect, right_part, color, fade_color, |
147 &positions, &colors); | 156 &positions, &colors); |
148 DCHECK(!positions.empty()); | 157 DCHECK(!positions.empty()); |
(...skipping 1053 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1202 if (horizontal_alignment != ALIGN_RIGHT) { | 1211 if (horizontal_alignment != ALIGN_RIGHT) { |
1203 right_part = solid_part; | 1212 right_part = solid_part; |
1204 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0); | 1213 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0); |
1205 solid_part.Inset(0, 0, gradient_width, 0); | 1214 solid_part.Inset(0, 0, gradient_width, 0); |
1206 } | 1215 } |
1207 | 1216 |
1208 Rect text_rect = display_rect(); | 1217 Rect text_rect = display_rect(); |
1209 text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0); | 1218 text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0); |
1210 | 1219 |
1211 // TODO(msw): Use the actual text colors corresponding to each faded part. | 1220 // TODO(msw): Use the actual text colors corresponding to each faded part. |
1212 skia::RefPtr<SkShader> shader = CreateFadeShader( | 1221 skia::RefPtr<SkShader> shader = |
1213 text_rect, left_part, right_part, colors_.breaks().front().second); | 1222 CreateFadeShader(font_list(), text_rect, left_part, right_part, |
1223 colors_.breaks().front().second); | |
1214 if (shader) | 1224 if (shader) |
1215 renderer->SetShader(shader.get()); | 1225 renderer->SetShader(shader.get()); |
1216 } | 1226 } |
1217 | 1227 |
1218 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { | 1228 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { |
1219 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_); | 1229 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_); |
1220 renderer->SetDrawLooper(looper.get()); | 1230 renderer->SetDrawLooper(looper.get()); |
1221 } | 1231 } |
1222 | 1232 |
1223 base::i18n::TextDirection RenderText::GetTextDirection( | 1233 base::i18n::TextDirection RenderText::GetTextDirection( |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1521 | 1531 |
1522 SetDisplayOffset(display_offset_.x() + delta_x); | 1532 SetDisplayOffset(display_offset_.x() + delta_x); |
1523 } | 1533 } |
1524 | 1534 |
1525 void RenderText::DrawSelection(Canvas* canvas) { | 1535 void RenderText::DrawSelection(Canvas* canvas) { |
1526 for (const Rect& s : GetSubstringBounds(selection())) | 1536 for (const Rect& s : GetSubstringBounds(selection())) |
1527 canvas->FillRect(s, selection_background_focused_color_); | 1537 canvas->FillRect(s, selection_background_focused_color_); |
1528 } | 1538 } |
1529 | 1539 |
1530 } // namespace gfx | 1540 } // namespace gfx |
OLD | NEW |