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/canvas.h" | 5 #include "ui/gfx/canvas.h" |
6 | 6 |
7 #include <limits.h> | 7 #include <limits.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 Rect clip_rect(text_bounds); | 193 Rect clip_rect(text_bounds); |
194 clip_rect.Inset(ShadowValue::GetMargin(shadows)); | 194 clip_rect.Inset(ShadowValue::GetMargin(shadows)); |
195 | 195 |
196 canvas_->save(); | 196 canvas_->save(); |
197 ClipRect(clip_rect); | 197 ClipRect(clip_rect); |
198 | 198 |
199 Rect rect(text_bounds); | 199 Rect rect(text_bounds); |
200 | 200 |
201 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); | 201 scoped_ptr<RenderText> render_text(RenderText::CreateInstance()); |
202 render_text->set_shadows(shadows); | 202 render_text->set_shadows(shadows); |
| 203 render_text->set_halo_effect(!!(flags & HALO_EFFECT)); |
203 | 204 |
204 if (flags & MULTI_LINE) { | 205 if (flags & MULTI_LINE) { |
205 WordWrapBehavior wrap_behavior = IGNORE_LONG_WORDS; | 206 WordWrapBehavior wrap_behavior = IGNORE_LONG_WORDS; |
206 if (flags & CHARACTER_BREAK) | 207 if (flags & CHARACTER_BREAK) |
207 wrap_behavior = WRAP_LONG_WORDS; | 208 wrap_behavior = WRAP_LONG_WORDS; |
208 else if (!(flags & NO_ELLIPSIS)) | 209 else if (!(flags & NO_ELLIPSIS)) |
209 wrap_behavior = ELIDE_LONG_WORDS; | 210 wrap_behavior = ELIDE_LONG_WORDS; |
210 | 211 |
211 std::vector<base::string16> strings; | 212 std::vector<base::string16> strings; |
212 ElideRectangleText(text, font_list, | 213 ElideRectangleText(text, font_list, |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 void Canvas::DrawStringRectWithHalo(const base::string16& text, | 277 void Canvas::DrawStringRectWithHalo(const base::string16& text, |
277 const FontList& font_list, | 278 const FontList& font_list, |
278 SkColor text_color, | 279 SkColor text_color, |
279 SkColor halo_color_in, | 280 SkColor halo_color_in, |
280 const Rect& display_rect, | 281 const Rect& display_rect, |
281 int flags) { | 282 int flags) { |
282 // Some callers will have semitransparent halo colors, which we don't handle | 283 // Some callers will have semitransparent halo colors, which we don't handle |
283 // (since the resulting image can have 1-bit transparency only). | 284 // (since the resulting image can have 1-bit transparency only). |
284 SkColor halo_color = SkColorSetA(halo_color_in, 0xFF); | 285 SkColor halo_color = SkColorSetA(halo_color_in, 0xFF); |
285 | 286 |
286 // Create a temporary buffer filled with the halo color. It must leave room | 287 // Draw the halo. |
287 // for the 1-pixel border around the text. | 288 DrawStringRectWithFlags(text, font_list, halo_color, display_rect, |
288 Size size(display_rect.width() + 2, display_rect.height() + 2); | 289 flags | HALO_EFFECT | NO_SUBPIXEL_RENDERING); |
289 Canvas text_canvas(size, image_scale(), false); | 290 // Draw the text. |
290 SkPaint bkgnd_paint; | 291 DrawStringRectWithFlags(text, font_list, text_color, display_rect, |
291 bkgnd_paint.setColor(halo_color); | 292 flags | NO_SUBPIXEL_RENDERING); |
292 text_canvas.DrawRect(Rect(size), bkgnd_paint); | |
293 | |
294 // Draw the text into the temporary buffer. This will have correct | |
295 // ClearType since the background color is the same as the halo color. | |
296 text_canvas.DrawStringRectWithFlags( | |
297 text, font_list, text_color, | |
298 Rect(1, 1, display_rect.width(), display_rect.height()), flags); | |
299 | |
300 uint32_t halo_premul = SkPreMultiplyColor(halo_color); | |
301 SkPixmap pixmap; | |
302 skia::GetWritablePixels(text_canvas.sk_canvas(), &pixmap); | |
303 | |
304 for (int cur_y = 0; cur_y < pixmap.height(); cur_y++) { | |
305 uint32_t* text_row = pixmap.writable_addr32(0, cur_y); | |
306 for (int cur_x = 0; cur_x < pixmap.width(); cur_x++) { | |
307 if (text_row[cur_x] == halo_premul) { | |
308 // This pixel was not touched by the text routines. See if it borders | |
309 // a touched pixel in any of the 4 directions (not diagonally). | |
310 if (!PixelShouldGetHalo(pixmap, cur_x, cur_y, halo_premul)) | |
311 text_row[cur_x] = 0; // Make transparent. | |
312 } else { | |
313 text_row[cur_x] |= 0xff << SK_A32_SHIFT; // Make opaque. | |
314 } | |
315 } | |
316 } | |
317 | |
318 // Draw the halo bitmap with blur. | |
319 SkBitmap bitmap; | |
320 bitmap.installPixels(pixmap.info(), pixmap.writable_addr(), | |
321 pixmap.rowBytes()); | |
322 ImageSkia text_image = ImageSkia(ImageSkiaRep(bitmap, | |
323 text_canvas.image_scale())); | |
324 DrawImageInt(text_image, display_rect.x() - 1, display_rect.y() - 1); | |
325 } | 293 } |
326 | 294 |
327 void Canvas::DrawFadedString(const base::string16& text, | 295 void Canvas::DrawFadedString(const base::string16& text, |
328 const FontList& font_list, | 296 const FontList& font_list, |
329 SkColor color, | 297 SkColor color, |
330 const Rect& display_rect, | 298 const Rect& display_rect, |
331 int flags) { | 299 int flags) { |
332 // If the whole string fits in the destination then just draw it directly. | 300 // If the whole string fits in the destination then just draw it directly. |
333 if (GetStringWidth(text, font_list) <= display_rect.width()) { | 301 if (GetStringWidth(text, font_list) <= display_rect.width()) { |
334 DrawStringRectWithFlags(text, font_list, color, display_rect, flags); | 302 DrawStringRectWithFlags(text, font_list, color, display_rect, flags); |
(...skipping 10 matching lines...) Expand all Loading... |
345 UpdateRenderText(rect, text, font_list, flags, color, render_text.get()); | 313 UpdateRenderText(rect, text, font_list, flags, color, render_text.get()); |
346 render_text->SetElideBehavior(FADE_TAIL); | 314 render_text->SetElideBehavior(FADE_TAIL); |
347 | 315 |
348 canvas_->save(); | 316 canvas_->save(); |
349 ClipRect(display_rect); | 317 ClipRect(display_rect); |
350 render_text->Draw(this); | 318 render_text->Draw(this); |
351 canvas_->restore(); | 319 canvas_->restore(); |
352 } | 320 } |
353 | 321 |
354 } // namespace gfx | 322 } // namespace gfx |
OLD | NEW |