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 | 8 |
9 #include "base/i18n/break_iterator.h" | 9 #include "base/i18n/break_iterator.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 return SkGradientShader::CreateLinear(&points[0], &colors[0], &positions[0], | 172 return SkGradientShader::CreateLinear(&points[0], &colors[0], &positions[0], |
173 colors.size(), SkShader::kClamp_TileMode); | 173 colors.size(), SkShader::kClamp_TileMode); |
174 } | 174 } |
175 | 175 |
176 } // namespace | 176 } // namespace |
177 | 177 |
178 namespace gfx { | 178 namespace gfx { |
179 | 179 |
180 namespace internal { | 180 namespace internal { |
181 | 181 |
| 182 // Value of |underline_thickness_| that indicates that underline metrics have |
| 183 // not been set explicitly. |
| 184 const SkScalar kUnderlineMetricsNotSet = -1.0f; |
| 185 |
182 SkiaTextRenderer::SkiaTextRenderer(Canvas* canvas) | 186 SkiaTextRenderer::SkiaTextRenderer(Canvas* canvas) |
183 : canvas_skia_(canvas->sk_canvas()), | 187 : canvas_skia_(canvas->sk_canvas()), |
184 started_drawing_(false) { | 188 started_drawing_(false), |
| 189 underline_thickness_(kUnderlineMetricsNotSet), |
| 190 underline_position_(0.0f) { |
185 DCHECK(canvas_skia_); | 191 DCHECK(canvas_skia_); |
186 paint_.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | 192 paint_.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
187 paint_.setStyle(SkPaint::kFill_Style); | 193 paint_.setStyle(SkPaint::kFill_Style); |
188 paint_.setAntiAlias(true); | 194 paint_.setAntiAlias(true); |
189 paint_.setSubpixelText(true); | 195 paint_.setSubpixelText(true); |
190 paint_.setLCDRenderText(true); | 196 paint_.setLCDRenderText(true); |
191 bounds_.setEmpty(); | 197 bounds_.setEmpty(); |
192 } | 198 } |
193 | 199 |
194 SkiaTextRenderer::~SkiaTextRenderer() { | 200 SkiaTextRenderer::~SkiaTextRenderer() { |
(...skipping 19 matching lines...) Expand all Loading... |
214 bool enable_lcd_text) { | 220 bool enable_lcd_text) { |
215 paint_.setAntiAlias(enable_smoothing); | 221 paint_.setAntiAlias(enable_smoothing); |
216 paint_.setSubpixelText(enable_smoothing); | 222 paint_.setSubpixelText(enable_smoothing); |
217 paint_.setLCDRenderText(enable_lcd_text); | 223 paint_.setLCDRenderText(enable_lcd_text); |
218 } | 224 } |
219 | 225 |
220 void SkiaTextRenderer::SetTypeface(SkTypeface* typeface) { | 226 void SkiaTextRenderer::SetTypeface(SkTypeface* typeface) { |
221 paint_.setTypeface(typeface); | 227 paint_.setTypeface(typeface); |
222 } | 228 } |
223 | 229 |
224 void SkiaTextRenderer::SetTextSize(int size) { | 230 void SkiaTextRenderer::SetTextSize(SkScalar size) { |
225 paint_.setTextSize(size); | 231 paint_.setTextSize(size); |
226 } | 232 } |
227 | 233 |
228 void SkiaTextRenderer::SetFontFamilyWithStyle(const std::string& family, | 234 void SkiaTextRenderer::SetFontFamilyWithStyle(const std::string& family, |
229 int style) { | 235 int style) { |
230 DCHECK(!family.empty()); | 236 DCHECK(!family.empty()); |
231 | 237 |
232 SkTypeface::Style skia_style = ConvertFontStyleToSkiaTypefaceStyle(style); | 238 SkTypeface::Style skia_style = ConvertFontStyleToSkiaTypefaceStyle(style); |
233 SkTypeface* typeface = SkTypeface::CreateFromName(family.c_str(), skia_style); | 239 SkTypeface* typeface = SkTypeface::CreateFromName(family.c_str(), skia_style); |
234 SkAutoUnref auto_unref(typeface); | 240 SkAutoUnref auto_unref(typeface); |
(...skipping 10 matching lines...) Expand all Loading... |
245 | 251 |
246 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { | 252 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { |
247 paint_.setColor(foreground); | 253 paint_.setColor(foreground); |
248 } | 254 } |
249 | 255 |
250 void SkiaTextRenderer::SetShader(SkShader* shader, const Rect& bounds) { | 256 void SkiaTextRenderer::SetShader(SkShader* shader, const Rect& bounds) { |
251 bounds_ = RectToSkRect(bounds); | 257 bounds_ = RectToSkRect(bounds); |
252 paint_.setShader(shader); | 258 paint_.setShader(shader); |
253 } | 259 } |
254 | 260 |
| 261 void SkiaTextRenderer::SetUnderlineMetrics(SkScalar thickness, |
| 262 SkScalar position) { |
| 263 underline_thickness_ = thickness; |
| 264 underline_position_ = position; |
| 265 } |
| 266 |
255 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, | 267 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, |
256 const uint16* glyphs, | 268 const uint16* glyphs, |
257 size_t glyph_count) { | 269 size_t glyph_count) { |
258 if (!started_drawing_) { | 270 if (!started_drawing_) { |
259 started_drawing_ = true; | 271 started_drawing_ = true; |
260 // Work-around for http://crbug.com/122743, where non-ClearType text is | 272 // Work-around for http://crbug.com/122743, where non-ClearType text is |
261 // rendered with incorrect gamma when using the fade shader. Draw the text | 273 // rendered with incorrect gamma when using the fade shader. Draw the text |
262 // to a layer and restore it faded by drawing a rect in kDstIn_Mode mode. | 274 // to a layer and restore it faded by drawing a rect in kDstIn_Mode mode. |
263 // | 275 // |
264 // Skip this when there is a looper which seems not working well with | 276 // Skip this when there is a looper which seems not working well with |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 const SkScalar kDiagonalStrikeThroughMarginOffset = (SK_Scalar1 / 4); | 309 const SkScalar kDiagonalStrikeThroughMarginOffset = (SK_Scalar1 / 4); |
298 | 310 |
299 SkScalar text_size = paint_.getTextSize(); | 311 SkScalar text_size = paint_.getTextSize(); |
300 SkScalar height = SkScalarMul(text_size, kLineThickness); | 312 SkScalar height = SkScalarMul(text_size, kLineThickness); |
301 SkRect r; | 313 SkRect r; |
302 | 314 |
303 r.fLeft = x; | 315 r.fLeft = x; |
304 r.fRight = x + width; | 316 r.fRight = x + width; |
305 | 317 |
306 if (style.underline) { | 318 if (style.underline) { |
307 SkScalar offset = SkScalarMulAdd(text_size, kUnderlineOffset, y); | 319 if (underline_thickness_ == kUnderlineMetricsNotSet) { |
308 r.fTop = offset; | 320 r.fTop = SkScalarMulAdd(text_size, kUnderlineOffset, y); |
309 r.fBottom = offset + height; | 321 r.fBottom = r.fTop + height; |
| 322 } else { |
| 323 r.fTop = y + underline_position_; |
| 324 r.fBottom = r.fTop + underline_thickness_; |
| 325 } |
310 canvas_skia_->drawRect(r, paint_); | 326 canvas_skia_->drawRect(r, paint_); |
311 } | 327 } |
312 if (style.strike) { | 328 if (style.strike) { |
313 SkScalar offset = SkScalarMulAdd(text_size, kStrikeThroughOffset, y); | 329 SkScalar offset = SkScalarMulAdd(text_size, kStrikeThroughOffset, y); |
314 r.fTop = offset; | 330 r.fTop = offset; |
315 r.fBottom = offset + height; | 331 r.fBottom = offset + height; |
316 canvas_skia_->drawRect(r, paint_); | 332 canvas_skia_->drawRect(r, paint_); |
317 } | 333 } |
318 if (style.diagonal_strike) { | 334 if (style.diagonal_strike) { |
319 SkScalar offset = | 335 SkScalar offset = |
(...skipping 625 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
945 if (cursor_enabled() && cursor_visible() && focused()) { | 961 if (cursor_enabled() && cursor_visible() && focused()) { |
946 const Rect& bounds = GetUpdatedCursorBounds(); | 962 const Rect& bounds = GetUpdatedCursorBounds(); |
947 if (bounds.width() != 0) | 963 if (bounds.width() != 0) |
948 canvas->FillRect(bounds, cursor_color_); | 964 canvas->FillRect(bounds, cursor_color_); |
949 else | 965 else |
950 canvas->DrawRect(bounds, cursor_color_); | 966 canvas->DrawRect(bounds, cursor_color_); |
951 } | 967 } |
952 } | 968 } |
953 | 969 |
954 } // namespace gfx | 970 } // namespace gfx |
OLD | NEW |