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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 colors->push_back(c0); | 132 colors->push_back(c0); |
133 } | 133 } |
134 positions->push_back(p0); | 134 positions->push_back(p0); |
135 colors->push_back(c0); | 135 colors->push_back(c0); |
136 positions->push_back(p1); | 136 positions->push_back(p1); |
137 colors->push_back(c1); | 137 colors->push_back(c1); |
138 } | 138 } |
139 | 139 |
140 // Creates a SkShader to fade the text, with |left_part| specifying the left | 140 // Creates a SkShader to fade the text, with |left_part| specifying the left |
141 // fade effect, if any, and |right_part| specifying the right fade effect. | 141 // fade effect, if any, and |right_part| specifying the right fade effect. |
142 skia::RefPtr<SkShader> CreateFadeShader(const FontList& font_list, | 142 sk_sp<SkShader> CreateFadeShader(const FontList& font_list, |
143 const Rect& text_rect, | 143 const Rect& text_rect, |
144 const Rect& left_part, | 144 const Rect& left_part, |
145 const Rect& right_part, | 145 const Rect& right_part, |
146 SkColor color) { | 146 SkColor color) { |
147 // In general, fade down to 0 alpha. But when the available width is less | 147 // In general, fade down to 0 alpha. But when the available width is less |
148 // than four characters, linearly ramp up the fade target alpha to as high as | 148 // than four characters, linearly ramp up the fade target alpha to as high as |
149 // 20% at zero width. This allows the user to see the last faded characters a | 149 // 20% at zero width. This allows the user to see the last faded characters a |
150 // little better when there are only a few characters shown. | 150 // little better when there are only a few characters shown. |
151 const float width_fraction = | 151 const float width_fraction = |
152 text_rect.width() / static_cast<float>(font_list.GetExpectedTextWidth(4)); | 152 text_rect.width() / static_cast<float>(font_list.GetExpectedTextWidth(4)); |
153 const SkAlpha kAlphaAtZeroWidth = 51; | 153 const SkAlpha kAlphaAtZeroWidth = 51; |
154 const SkAlpha alpha = (width_fraction < 1) ? | 154 const SkAlpha alpha = (width_fraction < 1) ? |
155 static_cast<SkAlpha>(round((1 - width_fraction) * kAlphaAtZeroWidth)) : 0; | 155 static_cast<SkAlpha>(round((1 - width_fraction) * kAlphaAtZeroWidth)) : 0; |
156 const SkColor fade_color = SkColorSetA(color, alpha); | 156 const SkColor fade_color = SkColorSetA(color, alpha); |
(...skipping 10 matching lines...) Expand all Loading... |
167 DCHECK(!positions.empty()); | 167 DCHECK(!positions.empty()); |
168 | 168 |
169 // Terminate |positions| with 1.0, as required by Skia. | 169 // Terminate |positions| with 1.0, as required by Skia. |
170 if (positions.back() != 1.0) { | 170 if (positions.back() != 1.0) { |
171 positions.push_back(1.0); | 171 positions.push_back(1.0); |
172 colors.push_back(colors.back()); | 172 colors.push_back(colors.back()); |
173 } | 173 } |
174 | 174 |
175 const SkPoint points[2] = { PointToSkPoint(text_rect.origin()), | 175 const SkPoint points[2] = { PointToSkPoint(text_rect.origin()), |
176 PointToSkPoint(text_rect.top_right()) }; | 176 PointToSkPoint(text_rect.top_right()) }; |
177 return skia::AdoptRef( | 177 return |
178 SkGradientShader::CreateLinear(&points[0], &colors[0], &positions[0], | 178 SkGradientShader::MakeLinear(&points[0], &colors[0], &positions[0], |
179 colors.size(), SkShader::kClamp_TileMode)); | 179 colors.size(), SkShader::kClamp_TileMode); |
180 } | 180 } |
181 | 181 |
182 // Converts a FontRenderParams::Hinting value to the corresponding | 182 // Converts a FontRenderParams::Hinting value to the corresponding |
183 // SkPaint::Hinting value. | 183 // SkPaint::Hinting value. |
184 SkPaint::Hinting FontRenderParamsHintingToSkPaintHinting( | 184 SkPaint::Hinting FontRenderParamsHintingToSkPaintHinting( |
185 FontRenderParams::Hinting params_hinting) { | 185 FontRenderParams::Hinting params_hinting) { |
186 switch (params_hinting) { | 186 switch (params_hinting) { |
187 case FontRenderParams::HINTING_NONE: return SkPaint::kNo_Hinting; | 187 case FontRenderParams::HINTING_NONE: return SkPaint::kNo_Hinting; |
188 case FontRenderParams::HINTING_SLIGHT: return SkPaint::kSlight_Hinting; | 188 case FontRenderParams::HINTING_SLIGHT: return SkPaint::kSlight_Hinting; |
189 case FontRenderParams::HINTING_MEDIUM: return SkPaint::kNormal_Hinting; | 189 case FontRenderParams::HINTING_MEDIUM: return SkPaint::kNormal_Hinting; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 // Enable fake bold text if bold style is needed but new typeface does not | 262 // Enable fake bold text if bold style is needed but new typeface does not |
263 // have it. | 263 // have it. |
264 paint_.setFakeBoldText((style & Font::BOLD) && !typeface->isBold()); | 264 paint_.setFakeBoldText((style & Font::BOLD) && !typeface->isBold()); |
265 } | 265 } |
266 } | 266 } |
267 | 267 |
268 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { | 268 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { |
269 paint_.setColor(foreground); | 269 paint_.setColor(foreground); |
270 } | 270 } |
271 | 271 |
272 void SkiaTextRenderer::SetShader(SkShader* shader) { | 272 void SkiaTextRenderer::SetShader(sk_sp<SkShader> shader) { |
273 paint_.setShader(shader); | 273 paint_.setShader(std::move(shader)); |
274 } | 274 } |
275 | 275 |
276 void SkiaTextRenderer::SetUnderlineMetrics(SkScalar thickness, | 276 void SkiaTextRenderer::SetUnderlineMetrics(SkScalar thickness, |
277 SkScalar position) { | 277 SkScalar position) { |
278 underline_thickness_ = thickness; | 278 underline_thickness_ = thickness; |
279 underline_position_ = position; | 279 underline_position_ = position; |
280 } | 280 } |
281 | 281 |
282 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, | 282 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, |
283 const uint16_t* glyphs, | 283 const uint16_t* glyphs, |
(...skipping 940 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1224 if (horizontal_alignment != ALIGN_RIGHT) { | 1224 if (horizontal_alignment != ALIGN_RIGHT) { |
1225 right_part = solid_part; | 1225 right_part = solid_part; |
1226 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0); | 1226 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0); |
1227 solid_part.Inset(0, 0, gradient_width, 0); | 1227 solid_part.Inset(0, 0, gradient_width, 0); |
1228 } | 1228 } |
1229 | 1229 |
1230 Rect text_rect = display_rect(); | 1230 Rect text_rect = display_rect(); |
1231 text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0); | 1231 text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0); |
1232 | 1232 |
1233 // TODO(msw): Use the actual text colors corresponding to each faded part. | 1233 // TODO(msw): Use the actual text colors corresponding to each faded part. |
1234 skia::RefPtr<SkShader> shader = | 1234 renderer->SetShader( |
1235 CreateFadeShader(font_list(), text_rect, left_part, right_part, | 1235 CreateFadeShader(font_list(), text_rect, left_part, right_part, |
1236 colors_.breaks().front().second); | 1236 colors_.breaks().front().second)); |
1237 if (shader) | |
1238 renderer->SetShader(shader.get()); | |
1239 } | 1237 } |
1240 | 1238 |
1241 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { | 1239 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { |
1242 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_); | 1240 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_); |
1243 renderer->SetDrawLooper(looper.get()); | 1241 renderer->SetDrawLooper(looper.get()); |
1244 } | 1242 } |
1245 | 1243 |
1246 base::i18n::TextDirection RenderText::GetTextDirection( | 1244 base::i18n::TextDirection RenderText::GetTextDirection( |
1247 const base::string16& text) { | 1245 const base::string16& text) { |
1248 if (text_direction_ == base::i18n::UNKNOWN_DIRECTION) { | 1246 if (text_direction_ == base::i18n::UNKNOWN_DIRECTION) { |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1544 | 1542 |
1545 SetDisplayOffset(display_offset_.x() + delta_x); | 1543 SetDisplayOffset(display_offset_.x() + delta_x); |
1546 } | 1544 } |
1547 | 1545 |
1548 void RenderText::DrawSelection(Canvas* canvas) { | 1546 void RenderText::DrawSelection(Canvas* canvas) { |
1549 for (const Rect& s : GetSubstringBounds(selection())) | 1547 for (const Rect& s : GetSubstringBounds(selection())) |
1550 canvas->FillRect(s, selection_background_focused_color_); | 1548 canvas->FillRect(s, selection_background_focused_color_); |
1551 } | 1549 } |
1552 | 1550 |
1553 } // namespace gfx | 1551 } // namespace gfx |
OLD | NEW |