OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_skia.h" | 5 #include "ui/gfx/canvas_skia.h" |
6 | 6 |
7 #include <cairo/cairo.h> | 7 #include <cairo/cairo.h> |
8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
9 #include <pango/pango.h> | 9 #include <pango/pango.h> |
10 #include <pango/pangocairo.h> | 10 #include <pango/pangocairo.h> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "ui/gfx/blit.h" |
14 #include "ui/gfx/font.h" | 15 #include "ui/gfx/font.h" |
15 #include "ui/gfx/gtk_util.h" | 16 #include "ui/gfx/gtk_util.h" |
16 #include "ui/gfx/platform_font_gtk.h" | 17 #include "ui/gfx/platform_font_gtk.h" |
17 #include "ui/gfx/rect.h" | 18 #include "ui/gfx/rect.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 const gunichar kAcceleratorChar = '&'; | 22 const gunichar kAcceleratorChar = '&'; |
22 | 23 |
23 // Font settings that we initialize once and then use when drawing text in | 24 // Font settings that we initialize once and then use when drawing text in |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 font_(font), | 202 font_(font), |
202 canvas_(canvas), | 203 canvas_(canvas), |
203 cr_(NULL), | 204 cr_(NULL), |
204 layout_(NULL), | 205 layout_(NULL), |
205 text_x_(bounds.x()), | 206 text_x_(bounds.x()), |
206 text_y_(bounds.y()), | 207 text_y_(bounds.y()), |
207 text_width_(0), | 208 text_width_(0), |
208 text_height_(0) { | 209 text_height_(0) { |
209 DCHECK(!bounds_.IsEmpty()); | 210 DCHECK(!bounds_.IsEmpty()); |
210 | 211 |
211 cr_ = canvas_->beginPlatformPaint(); | 212 cr_ = canvas_->BeginPlatformPaint(); |
212 layout_ = pango_cairo_create_layout(cr_); | 213 layout_ = pango_cairo_create_layout(cr_); |
213 | 214 |
214 SetupPangoLayout(layout_, text, font, bounds_.width(), flags_); | 215 SetupPangoLayout(layout_, text, font, bounds_.width(), flags_); |
215 | 216 |
216 pango_layout_set_height(layout_, bounds_.height() * PANGO_SCALE); | 217 pango_layout_set_height(layout_, bounds_.height() * PANGO_SCALE); |
217 | 218 |
218 cairo_save(cr_); | 219 cairo_save(cr_); |
219 | 220 |
220 cairo_rectangle(cr_, clip.x(), clip.y(), clip.width(), clip.height()); | 221 cairo_rectangle(cr_, clip.x(), clip.y(), clip.width(), clip.height()); |
221 cairo_clip(cr_); | 222 cairo_clip(cr_); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 SkColorGetR(text_color) / 255.0, | 256 SkColorGetR(text_color) / 255.0, |
256 SkColorGetG(text_color) / 255.0, | 257 SkColorGetG(text_color) / 255.0, |
257 SkColorGetB(text_color) / 255.0, | 258 SkColorGetB(text_color) / 255.0, |
258 SkColorGetA(text_color) / 255.0); | 259 SkColorGetA(text_color) / 255.0); |
259 cairo_move_to(cr_, text_x_, text_y_); | 260 cairo_move_to(cr_, text_x_, text_y_); |
260 pango_cairo_show_layout(cr_, layout_); | 261 pango_cairo_show_layout(cr_, layout_); |
261 } | 262 } |
262 | 263 |
263 void DrawStringContext::DrawWithHalo(const SkColor& text_color, | 264 void DrawStringContext::DrawWithHalo(const SkColor& text_color, |
264 const SkColor& halo_color) { | 265 const SkColor& halo_color) { |
265 gfx::CanvasSkia text_canvas(bounds_.width() + 2, bounds_.height() + 2, false); | 266 gfx::CanvasSkia text_canvas; |
| 267 text_canvas.Init(bounds_.width() + 2, bounds_.height() + 2, false); |
266 text_canvas.FillRectInt(static_cast<SkColor>(0), | 268 text_canvas.FillRectInt(static_cast<SkColor>(0), |
267 0, 0, bounds_.width() + 2, bounds_.height() + 2); | 269 0, 0, bounds_.width() + 2, bounds_.height() + 2); |
268 | 270 |
269 cairo_t* text_cr = text_canvas.beginPlatformPaint(); | 271 cairo_t* text_cr = text_canvas.BeginPlatformPaint(); |
270 | 272 |
271 cairo_move_to(text_cr, 2, 1); | 273 cairo_move_to(text_cr, 2, 1); |
272 pango_cairo_layout_path(text_cr, layout_); | 274 pango_cairo_layout_path(text_cr, layout_); |
273 | 275 |
274 cairo_set_source_rgba(text_cr, | 276 cairo_set_source_rgba(text_cr, |
275 SkColorGetR(halo_color) / 255.0, | 277 SkColorGetR(halo_color) / 255.0, |
276 SkColorGetG(halo_color) / 255.0, | 278 SkColorGetG(halo_color) / 255.0, |
277 SkColorGetB(halo_color) / 255.0, | 279 SkColorGetB(halo_color) / 255.0, |
278 SkColorGetA(halo_color) / 255.0); | 280 SkColorGetA(halo_color) / 255.0); |
279 cairo_set_line_width(text_cr, 2.0); | 281 cairo_set_line_width(text_cr, 2.0); |
280 cairo_set_line_join(text_cr, CAIRO_LINE_JOIN_ROUND); | 282 cairo_set_line_join(text_cr, CAIRO_LINE_JOIN_ROUND); |
281 cairo_stroke_preserve(text_cr); | 283 cairo_stroke_preserve(text_cr); |
282 | 284 |
283 cairo_set_operator(text_cr, CAIRO_OPERATOR_SOURCE); | 285 cairo_set_operator(text_cr, CAIRO_OPERATOR_SOURCE); |
284 cairo_set_source_rgba(text_cr, | 286 cairo_set_source_rgba(text_cr, |
285 SkColorGetR(text_color) / 255.0, | 287 SkColorGetR(text_color) / 255.0, |
286 SkColorGetG(text_color) / 255.0, | 288 SkColorGetG(text_color) / 255.0, |
287 SkColorGetB(text_color) / 255.0, | 289 SkColorGetB(text_color) / 255.0, |
288 SkColorGetA(text_color) / 255.0); | 290 SkColorGetA(text_color) / 255.0); |
289 cairo_fill(text_cr); | 291 cairo_fill(text_cr); |
290 | 292 |
291 text_canvas.endPlatformPaint(); | 293 text_canvas.EndPlatformPaint(); |
292 | 294 |
293 const SkBitmap& text_bitmap = const_cast<SkBitmap&>( | 295 const SkBitmap& text_bitmap = const_cast<SkBitmap&>( |
294 text_canvas.getTopPlatformDevice().accessBitmap(false)); | 296 text_canvas.skia_canvas()->getTopDevice()->accessBitmap(false)); |
295 canvas_->DrawBitmapInt(text_bitmap, text_x_ - 1, text_y_ - 1); | 297 canvas_->DrawBitmapInt(text_bitmap, text_x_ - 1, text_y_ - 1); |
296 } | 298 } |
297 | 299 |
298 } // namespace | 300 } // namespace |
299 | 301 |
300 namespace gfx { | 302 namespace gfx { |
301 | 303 |
302 CanvasSkia::CanvasSkia(int width, int height, bool is_opaque) | |
303 : skia::PlatformCanvas(width, height, is_opaque) { | |
304 } | |
305 | |
306 CanvasSkia::CanvasSkia() : skia::PlatformCanvas() { | |
307 } | |
308 | |
309 CanvasSkia::~CanvasSkia() { | |
310 } | |
311 | |
312 // static | 304 // static |
313 void CanvasSkia::SizeStringInt(const string16& text, | 305 void CanvasSkia::SizeStringInt(const string16& text, |
314 const gfx::Font& font, | 306 const gfx::Font& font, |
315 int* width, int* height, | 307 int* width, int* height, |
316 int flags) { | 308 int flags) { |
317 int org_width = *width; | 309 int org_width = *width; |
318 cairo_surface_t* surface = | 310 cairo_surface_t* surface = |
319 cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0); | 311 cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 0, 0); |
320 cairo_t* cr = cairo_create(surface); | 312 cairo_t* cr = cairo_create(surface); |
321 PangoLayout* layout = pango_cairo_create_layout(cr); | 313 PangoLayout* layout = pango_cairo_create_layout(cr); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 DrawStringContext context(this, text, font, bounds, bounds, flags); | 364 DrawStringContext context(this, text, font, bounds, bounds, flags); |
373 context.Draw(color); | 365 context.Draw(color); |
374 } | 366 } |
375 | 367 |
376 void CanvasSkia::DrawGdkPixbuf(GdkPixbuf* pixbuf, int x, int y) { | 368 void CanvasSkia::DrawGdkPixbuf(GdkPixbuf* pixbuf, int x, int y) { |
377 if (!pixbuf) { | 369 if (!pixbuf) { |
378 NOTREACHED(); | 370 NOTREACHED(); |
379 return; | 371 return; |
380 } | 372 } |
381 | 373 |
382 cairo_t* cr = beginPlatformPaint(); | 374 cairo_t* cr = BeginPlatformPaint(); |
383 gdk_cairo_set_source_pixbuf(cr, pixbuf, x, y); | 375 gdk_cairo_set_source_pixbuf(cr, pixbuf, x, y); |
384 cairo_paint(cr); | 376 cairo_paint(cr); |
385 } | 377 } |
386 | 378 |
387 ui::TextureID CanvasSkia::GetTextureID() { | 379 ui::TextureID CanvasSkia::GetTextureID() { |
388 // TODO(wjmaclean) | 380 // TODO(wjmaclean) |
389 return 0; | 381 return 0; |
390 } | 382 } |
391 | 383 |
| 384 void CanvasSkia::BlitToNativeContext(const Rect& src_rect, |
| 385 const Point& dst_origin, |
| 386 NativeDrawingContext dst_context) { |
| 387 BlitContextToContext( |
| 388 dst_context, |
| 389 Rect(dst_origin, src_rect.size()), |
| 390 BeginPlatformPaint(), |
| 391 src_rect.origin()); |
| 392 EndPlatformPaint(); |
| 393 } |
| 394 |
392 } // namespace gfx | 395 } // namespace gfx |
OLD | NEW |