Chromium Code Reviews| 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 // Indicates that underline metrics have not been set explicitly. | |
| 183 const SkScalar kUnderlineMetricsNotSet = -1.0f; | |
|
msw
2012/06/06 01:12:51
nit: technically this applies to a single metric v
Alexei Svitkine (slow)
2012/06/06 14:46:27
Done.
I've also changed the value to be SK_Scalar
| |
| 184 | |
| 182 SkiaTextRenderer::SkiaTextRenderer(Canvas* canvas) | 185 SkiaTextRenderer::SkiaTextRenderer(Canvas* canvas) |
| 183 : canvas_skia_(canvas->sk_canvas()), | 186 : canvas_skia_(canvas->sk_canvas()), |
| 184 started_drawing_(false) { | 187 started_drawing_(false), |
| 188 underline_thickness_(kUnderlineMetricsNotSet), | |
| 189 underline_position_(kUnderlineMetricsNotSet) { | |
| 185 DCHECK(canvas_skia_); | 190 DCHECK(canvas_skia_); |
| 186 paint_.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | 191 paint_.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 187 paint_.setStyle(SkPaint::kFill_Style); | 192 paint_.setStyle(SkPaint::kFill_Style); |
| 188 paint_.setAntiAlias(true); | 193 paint_.setAntiAlias(true); |
| 189 paint_.setSubpixelText(true); | 194 paint_.setSubpixelText(true); |
| 190 paint_.setLCDRenderText(true); | 195 paint_.setLCDRenderText(true); |
| 191 bounds_.setEmpty(); | 196 bounds_.setEmpty(); |
| 192 } | 197 } |
| 193 | 198 |
| 194 SkiaTextRenderer::~SkiaTextRenderer() { | 199 SkiaTextRenderer::~SkiaTextRenderer() { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 214 bool enable_lcd_text) { | 219 bool enable_lcd_text) { |
| 215 paint_.setAntiAlias(enable_smoothing); | 220 paint_.setAntiAlias(enable_smoothing); |
| 216 paint_.setSubpixelText(enable_smoothing); | 221 paint_.setSubpixelText(enable_smoothing); |
| 217 paint_.setLCDRenderText(enable_lcd_text); | 222 paint_.setLCDRenderText(enable_lcd_text); |
| 218 } | 223 } |
| 219 | 224 |
| 220 void SkiaTextRenderer::SetTypeface(SkTypeface* typeface) { | 225 void SkiaTextRenderer::SetTypeface(SkTypeface* typeface) { |
| 221 paint_.setTypeface(typeface); | 226 paint_.setTypeface(typeface); |
| 222 } | 227 } |
| 223 | 228 |
| 224 void SkiaTextRenderer::SetTextSize(int size) { | 229 void SkiaTextRenderer::SetTextSize(SkScalar size) { |
| 225 paint_.setTextSize(size); | 230 paint_.setTextSize(size); |
| 226 } | 231 } |
| 227 | 232 |
| 228 void SkiaTextRenderer::SetFontFamilyWithStyle(const std::string& family, | 233 void SkiaTextRenderer::SetFontFamilyWithStyle(const std::string& family, |
| 229 int style) { | 234 int style) { |
| 230 DCHECK(!family.empty()); | 235 DCHECK(!family.empty()); |
| 231 | 236 |
| 232 SkTypeface::Style skia_style = ConvertFontStyleToSkiaTypefaceStyle(style); | 237 SkTypeface::Style skia_style = ConvertFontStyleToSkiaTypefaceStyle(style); |
| 233 SkTypeface* typeface = SkTypeface::CreateFromName(family.c_str(), skia_style); | 238 SkTypeface* typeface = SkTypeface::CreateFromName(family.c_str(), skia_style); |
| 234 SkAutoUnref auto_unref(typeface); | 239 SkAutoUnref auto_unref(typeface); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 245 | 250 |
| 246 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { | 251 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { |
| 247 paint_.setColor(foreground); | 252 paint_.setColor(foreground); |
| 248 } | 253 } |
| 249 | 254 |
| 250 void SkiaTextRenderer::SetShader(SkShader* shader, const Rect& bounds) { | 255 void SkiaTextRenderer::SetShader(SkShader* shader, const Rect& bounds) { |
| 251 bounds_ = RectToSkRect(bounds); | 256 bounds_ = RectToSkRect(bounds); |
| 252 paint_.setShader(shader); | 257 paint_.setShader(shader); |
| 253 } | 258 } |
| 254 | 259 |
| 260 void SkiaTextRenderer::SetUnderlineMetrics(SkScalar thickness, | |
| 261 SkScalar position) { | |
| 262 underline_thickness_ = thickness; | |
| 263 underline_position_ = position; | |
| 264 } | |
| 265 | |
| 255 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, | 266 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, |
| 256 const uint16* glyphs, | 267 const uint16* glyphs, |
| 257 size_t glyph_count) { | 268 size_t glyph_count) { |
| 258 if (!started_drawing_) { | 269 if (!started_drawing_) { |
| 259 started_drawing_ = true; | 270 started_drawing_ = true; |
| 260 // Work-around for http://crbug.com/122743, where non-ClearType text is | 271 // 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 | 272 // 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. | 273 // to a layer and restore it faded by drawing a rect in kDstIn_Mode mode. |
| 263 // | 274 // |
| 264 // Skip this when there is a looper which seems not working well with | 275 // 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); | 308 const SkScalar kDiagonalStrikeThroughMarginOffset = (SK_Scalar1 / 4); |
| 298 | 309 |
| 299 SkScalar text_size = paint_.getTextSize(); | 310 SkScalar text_size = paint_.getTextSize(); |
| 300 SkScalar height = SkScalarMul(text_size, kLineThickness); | 311 SkScalar height = SkScalarMul(text_size, kLineThickness); |
| 301 SkRect r; | 312 SkRect r; |
| 302 | 313 |
| 303 r.fLeft = x; | 314 r.fLeft = x; |
| 304 r.fRight = x + width; | 315 r.fRight = x + width; |
| 305 | 316 |
| 306 if (style.underline) { | 317 if (style.underline) { |
| 307 SkScalar offset = SkScalarMulAdd(text_size, kUnderlineOffset, y); | 318 if (underline_position_ == kUnderlineMetricsNotSet) { |
|
msw
2012/06/06 01:12:51
Take the 'not set' path if either value isn't set
Alexei Svitkine (slow)
2012/06/06 14:46:27
Made the two fields independent - so that none, on
| |
| 308 r.fTop = offset; | 319 SkScalar offset = SkScalarMulAdd(text_size, kUnderlineOffset, y); |
| 309 r.fBottom = offset + height; | 320 r.fTop = offset; |
| 321 r.fBottom = offset + height; | |
| 322 } else { | |
| 323 r.fTop = 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 |