| 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 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 started_drawing_(false), | 182 started_drawing_(false), |
| 183 underline_thickness_(kUnderlineMetricsNotSet), | 183 underline_thickness_(kUnderlineMetricsNotSet), |
| 184 underline_position_(0.0f) { | 184 underline_position_(0.0f) { |
| 185 DCHECK(canvas_skia_); | 185 DCHECK(canvas_skia_); |
| 186 paint_.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | 186 paint_.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
| 187 paint_.setStyle(SkPaint::kFill_Style); | 187 paint_.setStyle(SkPaint::kFill_Style); |
| 188 paint_.setAntiAlias(true); | 188 paint_.setAntiAlias(true); |
| 189 paint_.setSubpixelText(true); | 189 paint_.setSubpixelText(true); |
| 190 paint_.setLCDRenderText(true); | 190 paint_.setLCDRenderText(true); |
| 191 paint_.setHinting(SkPaint::kNormal_Hinting); | 191 paint_.setHinting(SkPaint::kNormal_Hinting); |
| 192 bounds_.setEmpty(); | |
| 193 } | 192 } |
| 194 | 193 |
| 195 SkiaTextRenderer::~SkiaTextRenderer() { | 194 SkiaTextRenderer::~SkiaTextRenderer() { |
| 196 // Work-around for http://crbug.com/122743, where non-ClearType text is | |
| 197 // rendered with incorrect gamma when using the fade shader. Draw the text | |
| 198 // to a layer and restore it faded by drawing a rect in kDstIn_Mode mode. | |
| 199 // | |
| 200 // TODO(asvitkine): Remove this work-around once the Skia bug is fixed. | |
| 201 // http://code.google.com/p/skia/issues/detail?id=590 | |
| 202 if (deferred_fade_shader_.get()) { | |
| 203 paint_.setShader(deferred_fade_shader_.get()); | |
| 204 paint_.setXfermodeMode(SkXfermode::kDstIn_Mode); | |
| 205 canvas_skia_->drawRect(bounds_, paint_); | |
| 206 canvas_skia_->restore(); | |
| 207 } | |
| 208 } | 195 } |
| 209 | 196 |
| 210 void SkiaTextRenderer::SetDrawLooper(SkDrawLooper* draw_looper) { | 197 void SkiaTextRenderer::SetDrawLooper(SkDrawLooper* draw_looper) { |
| 211 paint_.setLooper(draw_looper); | 198 paint_.setLooper(draw_looper); |
| 212 } | 199 } |
| 213 | 200 |
| 214 void SkiaTextRenderer::SetFontRenderParams(const FontRenderParams& params, | 201 void SkiaTextRenderer::SetFontRenderParams(const FontRenderParams& params, |
| 215 bool background_is_transparent) { | 202 bool background_is_transparent) { |
| 216 ApplyRenderParams(params, background_is_transparent, &paint_); | 203 ApplyRenderParams(params, background_is_transparent, &paint_); |
| 217 } | 204 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 236 // Enable fake bold text if bold style is needed but new typeface does not | 223 // Enable fake bold text if bold style is needed but new typeface does not |
| 237 // have it. | 224 // have it. |
| 238 paint_.setFakeBoldText((style & Font::BOLD) && !typeface->isBold()); | 225 paint_.setFakeBoldText((style & Font::BOLD) && !typeface->isBold()); |
| 239 } | 226 } |
| 240 } | 227 } |
| 241 | 228 |
| 242 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { | 229 void SkiaTextRenderer::SetForegroundColor(SkColor foreground) { |
| 243 paint_.setColor(foreground); | 230 paint_.setColor(foreground); |
| 244 } | 231 } |
| 245 | 232 |
| 246 void SkiaTextRenderer::SetShader(SkShader* shader, const Rect& bounds) { | 233 void SkiaTextRenderer::SetShader(SkShader* shader) { |
| 247 bounds_ = RectToSkRect(bounds); | |
| 248 paint_.setShader(shader); | 234 paint_.setShader(shader); |
| 249 } | 235 } |
| 250 | 236 |
| 251 void SkiaTextRenderer::SetUnderlineMetrics(SkScalar thickness, | 237 void SkiaTextRenderer::SetUnderlineMetrics(SkScalar thickness, |
| 252 SkScalar position) { | 238 SkScalar position) { |
| 253 underline_thickness_ = thickness; | 239 underline_thickness_ = thickness; |
| 254 underline_position_ = position; | 240 underline_position_ = position; |
| 255 } | 241 } |
| 256 | 242 |
| 257 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, | 243 void SkiaTextRenderer::DrawPosText(const SkPoint* pos, |
| 258 const uint16* glyphs, | 244 const uint16* glyphs, |
| 259 size_t glyph_count) { | 245 size_t glyph_count) { |
| 260 if (!started_drawing_) { | |
| 261 started_drawing_ = true; | |
| 262 // Work-around for http://crbug.com/122743, where non-ClearType text is | |
| 263 // rendered with incorrect gamma when using the fade shader. Draw the text | |
| 264 // to a layer and restore it faded by drawing a rect in kDstIn_Mode mode. | |
| 265 // | |
| 266 // Skip this when there is a looper which seems not working well with | |
| 267 // deferred paint. Currently a looper is only used for text shadows. | |
| 268 // | |
| 269 // TODO(asvitkine): Remove this work-around once the Skia bug is fixed. | |
| 270 // http://code.google.com/p/skia/issues/detail?id=590 | |
| 271 if (!paint_.isLCDRenderText() && | |
| 272 paint_.getShader() && | |
| 273 !paint_.getLooper()) { | |
| 274 deferred_fade_shader_ = skia::SharePtr(paint_.getShader()); | |
| 275 paint_.setShader(NULL); | |
| 276 canvas_skia_->saveLayer(&bounds_, NULL); | |
| 277 } | |
| 278 } | |
| 279 | |
| 280 const size_t byte_length = glyph_count * sizeof(glyphs[0]); | 246 const size_t byte_length = glyph_count * sizeof(glyphs[0]); |
| 281 canvas_skia_->drawPosText(&glyphs[0], byte_length, &pos[0], paint_); | 247 canvas_skia_->drawPosText(&glyphs[0], byte_length, &pos[0], paint_); |
| 282 } | 248 } |
| 283 | 249 |
| 284 void SkiaTextRenderer::DrawDecorations(int x, int y, int width, bool underline, | 250 void SkiaTextRenderer::DrawDecorations(int x, int y, int width, bool underline, |
| 285 bool strike, bool diagonal_strike) { | 251 bool strike, bool diagonal_strike) { |
| 286 if (underline) | 252 if (underline) |
| 287 DrawUnderline(x, y, width); | 253 DrawUnderline(x, y, width); |
| 288 if (strike) | 254 if (strike) |
| 289 DrawStrike(x, y, width); | 255 DrawStrike(x, y, width); |
| (...skipping 878 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1168 solid_part.Inset(0, 0, gradient_width, 0); | 1134 solid_part.Inset(0, 0, gradient_width, 0); |
| 1169 } | 1135 } |
| 1170 | 1136 |
| 1171 Rect text_rect = display_rect(); | 1137 Rect text_rect = display_rect(); |
| 1172 text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0); | 1138 text_rect.Inset(GetAlignmentOffset(0).x(), 0, 0, 0); |
| 1173 | 1139 |
| 1174 // TODO(msw): Use the actual text colors corresponding to each faded part. | 1140 // TODO(msw): Use the actual text colors corresponding to each faded part. |
| 1175 skia::RefPtr<SkShader> shader = CreateFadeShader( | 1141 skia::RefPtr<SkShader> shader = CreateFadeShader( |
| 1176 text_rect, left_part, right_part, colors_.breaks().front().second); | 1142 text_rect, left_part, right_part, colors_.breaks().front().second); |
| 1177 if (shader) | 1143 if (shader) |
| 1178 renderer->SetShader(shader.get(), display_rect()); | 1144 renderer->SetShader(shader.get()); |
| 1179 } | 1145 } |
| 1180 | 1146 |
| 1181 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { | 1147 void RenderText::ApplyTextShadows(internal::SkiaTextRenderer* renderer) { |
| 1182 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_); | 1148 skia::RefPtr<SkDrawLooper> looper = CreateShadowDrawLooper(shadows_); |
| 1183 renderer->SetDrawLooper(looper.get()); | 1149 renderer->SetDrawLooper(looper.get()); |
| 1184 } | 1150 } |
| 1185 | 1151 |
| 1186 // static | 1152 // static |
| 1187 bool RenderText::RangeContainsCaret(const Range& range, | 1153 bool RenderText::RangeContainsCaret(const Range& range, |
| 1188 size_t caret_pos, | 1154 size_t caret_pos, |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1431 SetDisplayOffset(display_offset_.x() + delta_x); | 1397 SetDisplayOffset(display_offset_.x() + delta_x); |
| 1432 } | 1398 } |
| 1433 | 1399 |
| 1434 void RenderText::DrawSelection(Canvas* canvas) { | 1400 void RenderText::DrawSelection(Canvas* canvas) { |
| 1435 const std::vector<Rect> sel = GetSubstringBounds(selection()); | 1401 const std::vector<Rect> sel = GetSubstringBounds(selection()); |
| 1436 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) | 1402 for (std::vector<Rect>::const_iterator i = sel.begin(); i < sel.end(); ++i) |
| 1437 canvas->FillRect(*i, selection_background_focused_color_); | 1403 canvas->FillRect(*i, selection_background_focused_color_); |
| 1438 } | 1404 } |
| 1439 | 1405 |
| 1440 } // namespace gfx | 1406 } // namespace gfx |
| OLD | NEW |