OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef GFX_CANVAS_SKIA_H_ | 5 #ifndef GFX_CANVAS_SKIA_H_ |
6 #define GFX_CANVAS_SKIA_H_ | 6 #define GFX_CANVAS_SKIA_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "ui/gfx/canvas_skia.h" |
10 #include "base/string16.h" | 10 // TODO(sail): remove this file once all includes have been updated. |
11 #include "gfx/canvas.h" | |
12 #include "skia/ext/platform_canvas.h" | |
13 | |
14 #if defined(OS_POSIX) && !defined(OS_MACOSX) | |
15 typedef struct _GdkPixbuf GdkPixbuf; | |
16 #endif | |
17 | |
18 namespace gfx { | |
19 | |
20 class Canvas; | |
21 | |
22 // CanvasSkia is a SkCanvas subclass that provides a number of methods for | |
23 // common operations used throughout an application built using base/gfx and | |
24 // app/gfx. | |
25 // | |
26 // All methods that take integer arguments (as is used throughout views) | |
27 // end with Int. If you need to use methods provided by the superclass | |
28 // you'll need to do a conversion. In particular you'll need to use | |
29 // macro SkIntToScalar(xxx), or if converting from a scalar to an integer | |
30 // SkScalarRound. | |
31 // | |
32 // A handful of methods in this class are overloaded providing an additional | |
33 // argument of type SkXfermode::Mode. SkXfermode::Mode specifies how the | |
34 // source and destination colors are combined. Unless otherwise specified, | |
35 // the variant that does not take a SkXfermode::Mode uses a transfer mode | |
36 // of kSrcOver_Mode. | |
37 class CanvasSkia : public skia::PlatformCanvas, | |
38 public Canvas { | |
39 public: | |
40 // Creates an empty Canvas. Callers must use initialize before using the | |
41 // canvas. | |
42 CanvasSkia(); | |
43 | |
44 CanvasSkia(int width, int height, bool is_opaque); | |
45 | |
46 virtual ~CanvasSkia(); | |
47 | |
48 // Compute the size required to draw some text with the provided font. | |
49 // Attempts to fit the text with the provided width and height. Increases | |
50 // height and then width as needed to make the text fit. This method | |
51 // supports multiple lines. | |
52 static void SizeStringInt(const string16& text, | |
53 const gfx::Font& font, | |
54 int* width, int* height, | |
55 int flags); | |
56 | |
57 // Returns the default text alignment to be used when drawing text on a | |
58 // gfx::CanvasSkia based on the directionality of the system locale language. | |
59 // This function is used by gfx::Canvas::DrawStringInt when the text alignment | |
60 // is not specified. | |
61 // | |
62 // This function returns either gfx::Canvas::TEXT_ALIGN_LEFT or | |
63 // gfx::Canvas::TEXT_ALIGN_RIGHT. | |
64 static int DefaultCanvasTextAlignment(); | |
65 | |
66 #if defined(OS_POSIX) && !defined(OS_MACOSX) | |
67 // Draw the pixbuf in its natural size at (x, y). | |
68 void DrawGdkPixbuf(GdkPixbuf* pixbuf, int x, int y); | |
69 #endif | |
70 | |
71 #if defined(OS_WIN) || (defined(OS_POSIX) && !defined(OS_MACOSX)) | |
72 // Draws text with a 1-pixel halo around it of the given color. | |
73 // On Windows, it allows ClearType to be drawn to an otherwise transparenct | |
74 // bitmap for drag images. Drag images have only 1-bit of transparency, so | |
75 // we don't do any fancy blurring. | |
76 // On Linux, text with halo is created by stroking it with 2px |halo_color| | |
77 // then filling it with |text_color|. | |
78 void DrawStringWithHalo(const string16& text, | |
79 const gfx::Font& font, | |
80 const SkColor& text_color, | |
81 const SkColor& halo_color, | |
82 int x, int y, int w, int h, | |
83 int flags); | |
84 #endif | |
85 | |
86 // Extracts a bitmap from the contents of this canvas. | |
87 SkBitmap ExtractBitmap() const; | |
88 | |
89 // Overridden from Canvas: | |
90 virtual void Save(); | |
91 virtual void SaveLayerAlpha(uint8 alpha); | |
92 virtual void SaveLayerAlpha(uint8 alpha, const gfx::Rect& layer_bounds); | |
93 virtual void Restore(); | |
94 virtual bool ClipRectInt(int x, int y, int w, int h); | |
95 virtual void TranslateInt(int x, int y); | |
96 virtual void ScaleInt(int x, int y); | |
97 virtual void FillRectInt(const SkColor& color, int x, int y, int w, int h); | |
98 virtual void FillRectInt(const SkColor& color, int x, int y, int w, int h, | |
99 SkXfermode::Mode mode); | |
100 virtual void FillRectInt(const gfx::Brush* brush, int x, int y, int w, int h); | |
101 virtual void DrawRectInt(const SkColor& color, int x, int y, int w, int h); | |
102 virtual void DrawRectInt(const SkColor& color, | |
103 int x, int y, int w, int h, | |
104 SkXfermode::Mode mode); | |
105 virtual void DrawRectInt(int x, int y, int w, int h, const SkPaint& paint); | |
106 virtual void DrawLineInt(const SkColor& color, | |
107 int x1, int y1, | |
108 int x2, int y2); | |
109 virtual void DrawBitmapInt(const SkBitmap& bitmap, int x, int y); | |
110 virtual void DrawBitmapInt(const SkBitmap& bitmap, | |
111 int x, int y, | |
112 const SkPaint& paint); | |
113 virtual void DrawBitmapInt(const SkBitmap& bitmap, | |
114 int src_x, int src_y, int src_w, int src_h, | |
115 int dest_x, int dest_y, int dest_w, int dest_h, | |
116 bool filter); | |
117 virtual void DrawBitmapInt(const SkBitmap& bitmap, | |
118 int src_x, int src_y, int src_w, int src_h, | |
119 int dest_x, int dest_y, int dest_w, int dest_h, | |
120 bool filter, | |
121 const SkPaint& paint); | |
122 virtual void DrawStringInt(const string16& text, | |
123 const gfx::Font& font, | |
124 const SkColor& color, | |
125 int x, int y, int w, int h); | |
126 virtual void DrawStringInt(const string16& text, | |
127 const gfx::Font& font, | |
128 const SkColor& color, | |
129 const gfx::Rect& display_rect); | |
130 virtual void DrawStringInt(const string16& text, | |
131 const gfx::Font& font, | |
132 const SkColor& color, | |
133 int x, int y, int w, int h, | |
134 int flags); | |
135 virtual void DrawFocusRect(int x, int y, int width, int height); | |
136 virtual void TileImageInt(const SkBitmap& bitmap, int x, int y, int w, int h); | |
137 virtual void TileImageInt(const SkBitmap& bitmap, | |
138 int src_x, int src_y, | |
139 int dest_x, int dest_y, int w, int h); | |
140 virtual gfx::NativeDrawingContext BeginPlatformPaint(); | |
141 virtual void EndPlatformPaint(); | |
142 virtual CanvasSkia* AsCanvasSkia(); | |
143 virtual const CanvasSkia* AsCanvasSkia() const; | |
144 | |
145 private: | |
146 // Test whether the provided rectangle intersects the current clip rect. | |
147 bool IntersectsClipRectInt(int x, int y, int w, int h); | |
148 | |
149 #if defined(OS_WIN) | |
150 // Draws text with the specified color, font and location. The text is | |
151 // aligned to the left, vertically centered, clipped to the region. If the | |
152 // text is too big, it is truncated and '...' is added to the end. | |
153 void DrawStringInt(const string16& text, | |
154 HFONT font, | |
155 const SkColor& color, | |
156 int x, int y, int w, int h, | |
157 int flags); | |
158 #endif | |
159 | |
160 DISALLOW_COPY_AND_ASSIGN(CanvasSkia); | |
161 }; | |
162 | |
163 } // namespace gfx; | |
164 | 11 |
165 #endif // GFX_CANVAS_SKIA_H_ | 12 #endif // GFX_CANVAS_SKIA_H_ |
OLD | NEW |