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 // The shader should only specify transparency of the fade itself, not the | 147 // The shader should only specify transparency of the fade itself, not the |
148 // original transparency, which will be applied by the actual renderer. | 148 // original transparency, which will be applied by the actual renderer. |
149 DCHECK_EQ(SkColorGetA(color), static_cast<uint8_t>(0xff)); | 149 DCHECK_EQ(SkColorGetA(color), static_cast<uint8_t>(0xff)); |
150 | 150 |
151 // In general, fade down to 0 alpha. But when the available width is less | 151 // In general, fade down to 0 alpha. But when the available width is less |
152 // than four characters, linearly ramp up the fade target alpha to as high as | 152 // than four characters, linearly ramp up the fade target alpha to as high as |
153 // 20% at zero width. This allows the user to see the last faded characters a | 153 // 20% at zero width. This allows the user to see the last faded characters a |
154 // little better when there are only a few characters shown. | 154 // little better when there are only a few characters shown. |
155 const float width_fraction = | 155 const float width_fraction = |
156 text_rect.width() / static_cast<float>(font_list.GetExpectedTextWidth(4)); | 156 text_rect.width() / static_cast<float>(font_list.GetExpectedTextWidth(4)); |
(...skipping 14 matching lines...) Expand all Loading... |
171 DCHECK(!positions.empty()); | 171 DCHECK(!positions.empty()); |
172 | 172 |
173 // Terminate |positions| with 1.0, as required by Skia. | 173 // Terminate |positions| with 1.0, as required by Skia. |
174 if (positions.back() != 1.0) { | 174 if (positions.back() != 1.0) { |
175 positions.push_back(1.0); | 175 positions.push_back(1.0); |
176 colors.push_back(colors.back()); | 176 colors.push_back(colors.back()); |
177 } | 177 } |
178 | 178 |
179 const SkPoint points[2] = { PointToSkPoint(text_rect.origin()), | 179 const SkPoint points[2] = { PointToSkPoint(text_rect.origin()), |
180 PointToSkPoint(text_rect.top_right()) }; | 180 PointToSkPoint(text_rect.top_right()) }; |
181 return skia::AdoptRef( | 181 return |
182 SkGradientShader::CreateLinear(&points[0], &colors[0], &positions[0], | 182 SkGradientShader::MakeLinear(&points[0], &colors[0], &positions[0], |
183 colors.size(), SkShader::kClamp_TileMode)); | 183 colors.size(), SkShader::kClamp_TileMode); |
184 } | 184 } |
185 | 185 |
186 // Converts a FontRenderParams::Hinting value to the corresponding | 186 // Converts a FontRenderParams::Hinting value to the corresponding |
187 // SkPaint::Hinting value. | 187 // SkPaint::Hinting value. |
188 SkPaint::Hinting FontRenderParamsHintingToSkPaintHinting( | 188 SkPaint::Hinting FontRenderParamsHintingToSkPaintHinting( |
189 FontRenderParams::Hinting params_hinting) { | 189 FontRenderParams::Hinting params_hinting) { |
190 switch (params_hinting) { | 190 switch (params_hinting) { |
191 case FontRenderParams::HINTING_NONE: return SkPaint::kNo_Hinting; | 191 case FontRenderParams::HINTING_NONE: return SkPaint::kNo_Hinting; |
192 case FontRenderParams::HINTING_SLIGHT: return SkPaint::kSlight_Hinting; | 192 case FontRenderParams::HINTING_SLIGHT: return SkPaint::kSlight_Hinting; |
193 case FontRenderParams::HINTING_MEDIUM: return SkPaint::kNormal_Hinting; | 193 case FontRenderParams::HINTING_MEDIUM: return SkPaint::kNormal_Hinting; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 // Enable fake bold text if bold style is needed but new typeface does not | 266 // Enable fake bold text if bold style is needed but new typeface does not |
267 // have it. | 267 // have it. |
268 paint_.setFakeBoldText((style & Font::BOLD) && !typeface->isBold()); | 268 paint_.setFakeBoldText((style & Font::BOLD) && !typeface->isBold()); |
269 } | 269 } |
270 } | 270 } |
271 | 271 |
272 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { | 272 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { |
273 paint_.setColor(foreground); | 273 paint_.setColor(foreground); |
274 } | 274 } |
275 | 275 |
276 void SkiaTextRenderer::SetShader(SkShader* shader) { | 276 void SkiaTextRenderer::SetShader(sk_sp<SkShader> shader) { |
277 paint_.setShader(shader); | 277 paint_.setShader(std::move(shader)); |
278 } | 278 } |
279 | 279 |
280 void SkiaTextRenderer::SetUnderlineMetrics(SkScalar thickness, | 280 void SkiaTextRenderer::SetUnderlineMetrics(SkScalar thickness, |
281 SkScalar position) { | 281 SkScalar position) { |
282 underline_thickness_ = thickness; | 282 underline_thickness_ = thickness; |
283 underline_position_ = position; | 283 underline_position_ = position; |
284 } | 284 } |
285 | 285 |
286 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, | 286 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, |
287 const uint16_t* glyphs, | 287 const uint16_t* glyphs, |
(...skipping 940 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1228 if (horizontal_alignment != ALIGN_RIGHT) { | 1228 if (horizontal_alignment != ALIGN_RIGHT) { |
1229 right_part = solid_part; | 1229 right_part = solid_part; |
1230 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0); | 1230 right_part.Inset(solid_part.width() - gradient_width, 0, 0, 0); |
1231 solid_part.Inset(0, 0, gradient_width, 0); | 1231 solid_part.Inset(0, 0, gradient_width, 0); |
1232 } | 1232 } |
1233 | 1233 |
1234 Rect text_rect = display_rect(); | 1234 Rect text_rect = display_rect(); |
1235 text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0); | 1235 text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0); |
1236 | 1236 |
1237 // TODO(msw): Use the actual text colors corresponding to each faded part. | 1237 // TODO(msw): Use the actual text colors corresponding to each faded part. |
1238 skia::RefPtr<SkShader> shader = | 1238 renderer->SetShader( |
1239 CreateFadeShader(font_list(), text_rect, left_part, right_part, | 1239 CreateFadeShader(font_list(), text_rect, left_part, right_part, |
1240 SkColorSetA(colors_.breaks().front().second, 0xff)); | 1240 SkColorSetA(colors_.breaks().front().second, 0xff))); |
1241 if (shader) | |
1242 renderer->SetShader(shader.get()); | |
1243 } | 1241 } |
1244 | 1242 |
1245 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { | 1243 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { |
1246 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_); | 1244 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_); |
1247 renderer->SetDrawLooper(looper.get()); | 1245 renderer->SetDrawLooper(looper.get()); |
1248 } | 1246 } |
1249 | 1247 |
1250 base::i18n::TextDirection RenderText::GetTextDirection( | 1248 base::i18n::TextDirection RenderText::GetTextDirection( |
1251 const base::string16& text) { | 1249 const base::string16& text) { |
1252 if (text_direction_ == base::i18n::UNKNOWN_DIRECTION) { | 1250 if (text_direction_ == base::i18n::UNKNOWN_DIRECTION) { |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1548 | 1546 |
1549 SetDisplayOffset(display_offset_.x() + delta_x); | 1547 SetDisplayOffset(display_offset_.x() + delta_x); |
1550 } | 1548 } |
1551 | 1549 |
1552 void RenderText::DrawSelection(Canvas* canvas) { | 1550 void RenderText::DrawSelection(Canvas* canvas) { |
1553 for (const Rect& s : GetSubstringBounds(selection())) | 1551 for (const Rect& s : GetSubstringBounds(selection())) |
1554 canvas->FillRect(s, selection_background_focused_color_); | 1552 canvas->FillRect(s, selection_background_focused_color_); |
1555 } | 1553 } |
1556 | 1554 |
1557 } // namespace gfx | 1555 } // namespace gfx |
OLD | NEW |