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 #ifndef UI_GFX_CANVAS_SKIA_H_ | 5 #ifndef UI_GFX_CANVAS_SKIA_H_ |
6 #define UI_GFX_CANVAS_SKIA_H_ | 6 #define UI_GFX_CANVAS_SKIA_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/compiler_specific.h" | |
11 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
12 #include "base/string16.h" | 11 #include "base/string16.h" |
13 #include "skia/ext/platform_canvas.h" | 12 #include "skia/ext/platform_canvas.h" |
14 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/native_widget_types.h" |
15 | 14 |
16 class SkBitmap; | 15 class SkBitmap; |
17 | 16 |
17 namespace ui { | |
18 class Transform; | |
19 } | |
20 | |
18 namespace gfx { | 21 namespace gfx { |
19 | 22 |
23 class Brush; | |
24 class Rect; | |
25 class Font; | |
26 class Point; | |
27 class Size; | |
28 | |
20 // CanvasSkia is a SkCanvas wrapper that provides a number of methods for | 29 // CanvasSkia is a SkCanvas wrapper that provides a number of methods for |
21 // common operations used throughout an application built using base/gfx. | 30 // common operations used throughout an application built using ui/gfx. |
22 // | 31 // |
23 // All methods that take integer arguments (as is used throughout views) | 32 // All methods that take integer arguments (as is used throughout views) |
24 // end with Int. If you need to use methods provided by SkCanvas, you'll | 33 // end with Int. If you need to use methods provided by SkCanvas, you'll |
25 // need to do a conversion. In particular you'll need to use |SkIntToScalar()|, | 34 // need to do a conversion. In particular you'll need to use |SkIntToScalar()|, |
26 // or if converting from a scalar to an integer |SkScalarRound()|. | 35 // or if converting from a scalar to an integer |SkScalarRound()|. |
27 // | 36 // |
28 // A handful of methods in this class are overloaded providing an additional | 37 // A handful of methods in this class are overloaded providing an additional |
29 // argument of type SkXfermode::Mode. SkXfermode::Mode specifies how the | 38 // argument of type SkXfermode::Mode. SkXfermode::Mode specifies how the |
30 // source and destination colors are combined. Unless otherwise specified, | 39 // source and destination colors are combined. Unless otherwise specified, |
31 // the variant that does not take a SkXfermode::Mode uses a transfer mode | 40 // the variant that does not take a SkXfermode::Mode uses a transfer mode |
32 // of kSrcOver_Mode. | 41 // of kSrcOver_Mode. |
33 class UI_EXPORT CanvasSkia : public Canvas { | 42 class UI_EXPORT CanvasSkia { |
34 public: | 43 public: |
35 enum TruncateFadeMode { | 44 enum TruncateFadeMode { |
36 TruncateFadeTail, | 45 TruncateFadeTail, |
37 TruncateFadeHead, | 46 TruncateFadeHead, |
38 TruncateFadeHeadAndTail, | 47 TruncateFadeHeadAndTail, |
39 }; | 48 }; |
40 | 49 |
50 // Specifies the alignment for text rendered with the DrawStringInt method. | |
51 enum { | |
52 TEXT_ALIGN_LEFT = 1, | |
53 TEXT_ALIGN_CENTER = 2, | |
54 TEXT_ALIGN_RIGHT = 4, | |
55 TEXT_VALIGN_TOP = 8, | |
56 TEXT_VALIGN_MIDDLE = 16, | |
57 TEXT_VALIGN_BOTTOM = 32, | |
58 | |
59 // Specifies the text consists of multiple lines. | |
60 MULTI_LINE = 64, | |
61 | |
62 // By default DrawStringInt does not process the prefix ('&') character | |
63 // specially. That is, the string "&foo" is rendered as "&foo". When | |
64 // rendering text from a resource that uses the prefix character for | |
65 // mnemonics, the prefix should be processed and can be rendered as an | |
66 // underline (SHOW_PREFIX), or not rendered at all (HIDE_PREFIX). | |
67 SHOW_PREFIX = 128, | |
68 HIDE_PREFIX = 256, | |
69 | |
70 // Prevent ellipsizing | |
71 NO_ELLIPSIS = 512, | |
72 | |
73 // Specifies if words can be split by new lines. | |
74 // This only works with MULTI_LINE. | |
75 CHARACTER_BREAK = 1024, | |
76 | |
77 // Instructs DrawStringInt() to render the text using RTL directionality. | |
78 // In most cases, passing this flag is not necessary because information | |
79 // about the text directionality is going to be embedded within the string | |
80 // in the form of special Unicode characters. However, we don't insert | |
81 // directionality characters into strings if the locale is LTR because some | |
82 // platforms (for example, an English Windows XP with no RTL fonts | |
83 // installed) don't support these characters. Thus, this flag should be | |
84 // used to render text using RTL directionality when the locale is LTR. | |
85 FORCE_RTL_DIRECTIONALITY = 2048, | |
86 | |
87 // Similar to FORCE_RTL_DIRECTIONALITY, but left-to-right. | |
88 // See FORCE_RTL_DIRECTIONALITY for details. | |
89 FORCE_LTR_DIRECTIONALITY = 4096, | |
90 }; | |
91 | |
41 // Creates an empty canvas. | 92 // Creates an empty canvas. |
42 CanvasSkia(); | 93 CanvasSkia(); |
43 | 94 |
44 // If this canvas is not opaque, it's explicitly cleared to transparent before | 95 // If this canvas is not opaque, it's explicitly cleared to transparent before |
45 // being returned. | 96 // being returned. |
46 CanvasSkia(const gfx::Size& size, bool is_opaque); | 97 CanvasSkia(const gfx::Size& size, bool is_opaque); |
47 | 98 |
48 // Constructs a canvas the size of the provided |bitmap|, and draws the | 99 // Constructs a canvas the size of the provided |bitmap|, and draws the |
49 // bitmap into it. | 100 // bitmap into it. |
50 CanvasSkia(const SkBitmap& bitmap, bool is_opaque); | 101 CanvasSkia(const SkBitmap& bitmap, bool is_opaque); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 void DrawStringWithHalo(const string16& text, | 138 void DrawStringWithHalo(const string16& text, |
88 const gfx::Font& font, | 139 const gfx::Font& font, |
89 const SkColor& text_color, | 140 const SkColor& text_color, |
90 const SkColor& halo_color, | 141 const SkColor& halo_color, |
91 int x, int y, int w, int h, | 142 int x, int y, int w, int h, |
92 int flags); | 143 int flags); |
93 | 144 |
94 // Extracts a bitmap from the contents of this canvas. | 145 // Extracts a bitmap from the contents of this canvas. |
95 SkBitmap ExtractBitmap() const; | 146 SkBitmap ExtractBitmap() const; |
96 | 147 |
97 // Overridden from Canvas: | 148 // Saves a copy of the drawing state onto a stack, operating on this copy |
98 virtual void Save() OVERRIDE; | 149 // until a balanced call to Restore() is made. |
99 virtual void SaveLayerAlpha(uint8 alpha) OVERRIDE; | 150 void Save(); |
100 virtual void SaveLayerAlpha(uint8 alpha, | 151 |
101 const gfx::Rect& layer_bounds) OVERRIDE; | 152 // As with Save(), except draws to a layer that is blended with the canvas |
102 virtual void Restore() OVERRIDE; | 153 // at the specified alpha once Restore() is called. |
103 virtual bool ClipRect(const gfx::Rect& rect) OVERRIDE; | 154 // |layer_bounds| are the bounds of the layer relative to the current |
104 virtual void Translate(const gfx::Point& point) OVERRIDE; | 155 // transform. |
105 virtual void Scale(int x_scale, int y_scale) OVERRIDE; | 156 void SaveLayerAlpha(uint8 alpha); |
106 virtual void FillRect(const gfx::Rect& rect, const SkColor& color) OVERRIDE; | 157 void SaveLayerAlpha(uint8 alpha, |
107 virtual void FillRect(const gfx::Rect& rect, | 158 const gfx::Rect& layer_bounds); |
159 | |
160 // Restores the drawing state after a call to Save*(). It is an error to | |
161 // call Restore() more times than Save*(). | |
162 void Restore() ; | |
Alexei Svitkine (slow)
2012/03/06 17:44:51
You have a space between ")" and ";" here and in m
tfarina
2012/03/06 18:05:30
Done.
| |
163 | |
164 // Returns true if the clip is non-empty. | |
165 bool ClipRect(const gfx::Rect& rect) ; | |
166 | |
167 void Translate(const gfx::Point& point) ; | |
168 | |
169 void Scale(int x_scale, int y_scale) ; | |
170 | |
171 // Fills |rect| with |color| using a transfer mode of | |
172 // SkXfermode::kSrcOver_Mode. | |
173 void FillRect(const gfx::Rect& rect, const SkColor& color) ; | |
174 | |
175 // Fills |rect| with the specified |color| and |mode|. | |
176 void FillRect(const gfx::Rect& rect, | |
108 const SkColor& color, | 177 const SkColor& color, |
109 SkXfermode::Mode mode) OVERRIDE; | 178 SkXfermode::Mode mode) ; |
110 virtual void FillRect(const gfx::Rect& rect, | 179 |
111 const gfx::Brush* brush) OVERRIDE; | 180 // Fills |rect| with the specified |brush|. |
112 virtual void DrawRect(const gfx::Rect& rect, const SkColor& color) OVERRIDE; | 181 void FillRect(const gfx::Rect& rect, |
113 virtual void DrawRect(const gfx::Rect& rect, | 182 const gfx::Brush* brush) ; |
183 | |
184 // Draws a single pixel rect in the specified region with the specified | |
185 // color, using a transfer mode of SkXfermode::kSrcOver_Mode. | |
186 // | |
187 // NOTE: if you need a single pixel line, use DrawLine. | |
188 void DrawRect(const gfx::Rect& rect, const SkColor& color) ; | |
189 | |
190 // Draws a single pixel rect in the specified region with the specified | |
191 // color and transfer mode. | |
192 // | |
193 // NOTE: if you need a single pixel line, use DrawLine. | |
194 void DrawRect(const gfx::Rect& rect, | |
114 const SkColor& color, | 195 const SkColor& color, |
115 SkXfermode::Mode mode) OVERRIDE; | 196 SkXfermode::Mode mode) ; |
116 virtual void DrawRect(const gfx::Rect& rect, const SkPaint& paint) OVERRIDE; | 197 |
117 virtual void DrawLine(const gfx::Point& p1, | 198 // Draws the given rectangle with the given paint's parameters. |
199 void DrawRect(const gfx::Rect& rect, const SkPaint& paint) ; | |
200 | |
201 // Draws a single pixel line with the specified color. | |
202 void DrawLine(const gfx::Point& p1, | |
118 const gfx::Point& p2, | 203 const gfx::Point& p2, |
119 const SkColor& color) OVERRIDE; | 204 const SkColor& color) ; |
120 virtual void DrawBitmapInt(const SkBitmap& bitmap, int x, int y) OVERRIDE; | 205 |
121 virtual void DrawBitmapInt(const SkBitmap& bitmap, | 206 // Draws a bitmap with the origin at the specified location. The upper left |
207 // corner of the bitmap is rendered at the specified location. | |
208 void DrawBitmapInt(const SkBitmap& bitmap, int x, int y) ; | |
209 | |
210 // Draws a bitmap with the origin at the specified location, using the | |
211 // specified paint. The upper left corner of the bitmap is rendered at the | |
212 // specified location. | |
213 void DrawBitmapInt(const SkBitmap& bitmap, | |
122 int x, int y, | 214 int x, int y, |
123 const SkPaint& paint) OVERRIDE; | 215 const SkPaint& paint) ; |
124 virtual void DrawBitmapInt(const SkBitmap& bitmap, | 216 |
217 // Draws a portion of a bitmap in the specified location. The src parameters | |
218 // correspond to the region of the bitmap to draw in the region defined | |
219 // by the dest coordinates. | |
220 // | |
221 // If the width or height of the source differs from that of the destination, | |
222 // the bitmap will be scaled. When scaling down, it is highly recommended | |
223 // that you call buildMipMap(false) on your bitmap to ensure that it has | |
224 // a mipmap, which will result in much higher-quality output. Set |filter| | |
225 // to use filtering for bitmaps, otherwise the nearest-neighbor algorithm | |
226 // is used for resampling. | |
227 // | |
228 // An optional custom SkPaint can be provided. | |
229 void DrawBitmapInt(const SkBitmap& bitmap, | |
125 int src_x, int src_y, int src_w, int src_h, | 230 int src_x, int src_y, int src_w, int src_h, |
126 int dest_x, int dest_y, int dest_w, int dest_h, | 231 int dest_x, int dest_y, int dest_w, int dest_h, |
127 bool filter) OVERRIDE; | 232 bool filter) ; |
128 virtual void DrawBitmapInt(const SkBitmap& bitmap, | 233 void DrawBitmapInt(const SkBitmap& bitmap, |
129 int src_x, int src_y, int src_w, int src_h, | 234 int src_x, int src_y, int src_w, int src_h, |
130 int dest_x, int dest_y, int dest_w, int dest_h, | 235 int dest_x, int dest_y, int dest_w, int dest_h, |
131 bool filter, | 236 bool filter, |
132 const SkPaint& paint) OVERRIDE; | 237 const SkPaint& paint) ; |
133 virtual void DrawStringInt(const string16& text, | 238 |
239 // Draws text with the specified color, font and location. The text is | |
240 // aligned to the left, vertically centered, clipped to the region. If the | |
241 // text is too big, it is truncated and '...' is added to the end. | |
242 void DrawStringInt(const string16& text, | |
134 const gfx::Font& font, | 243 const gfx::Font& font, |
135 const SkColor& color, | 244 const SkColor& color, |
136 int x, int y, int w, int h) OVERRIDE; | 245 int x, int y, int w, int h) ; |
137 virtual void DrawStringInt(const string16& text, | 246 void DrawStringInt(const string16& text, |
138 const gfx::Font& font, | 247 const gfx::Font& font, |
139 const SkColor& color, | 248 const SkColor& color, |
140 const gfx::Rect& display_rect) OVERRIDE; | 249 const gfx::Rect& display_rect) ; |
141 virtual void DrawStringInt(const string16& text, | 250 |
251 // Draws text with the specified color, font and location. The last argument | |
252 // specifies flags for how the text should be rendered. It can be one of | |
253 // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT. | |
254 void DrawStringInt(const string16& text, | |
142 const gfx::Font& font, | 255 const gfx::Font& font, |
143 const SkColor& color, | 256 const SkColor& color, |
144 int x, int y, int w, int h, | 257 int x, int y, int w, int h, |
145 int flags) OVERRIDE; | 258 int flags) ; |
259 | |
260 // Draws a dotted gray rectangle used for focus purposes. | |
261 void DrawFocusRect(const gfx::Rect& rect) ; | |
262 | |
263 // Tiles the image in the specified region. | |
264 void TileImageInt(const SkBitmap& bitmap, | |
265 int x, int y, int w, int h) ; | |
266 void TileImageInt(const SkBitmap& bitmap, | |
267 int src_x, int src_y, | |
268 int dest_x, int dest_y, int w, int h) ; | |
269 | |
270 // Returns a native drawing context for platform specific drawing routines to | |
271 // use. Must be balanced by a call to EndPlatformPaint(). | |
272 NativeDrawingContext BeginPlatformPaint() ; | |
273 | |
274 // Signifies the end of platform drawing using the native drawing context | |
275 // returned by BeginPlatformPaint(). | |
276 void EndPlatformPaint() ; | |
277 | |
278 // Apply transformation on the canvas. | |
279 void Transform(const ui::Transform& transform) ; | |
280 | |
281 CanvasSkia* AsCanvasSkia() ; | |
282 const CanvasSkia* AsCanvasSkia() const ; | |
283 SkCanvas* GetSkCanvas() ; | |
Alexei Svitkine (slow)
2012/03/06 17:44:51
Can you move these next to sk_canvas() below and a
tfarina
2012/03/06 18:05:30
Done.
| |
284 const SkCanvas* GetSkCanvas() const ; | |
285 | |
146 #if defined(OS_WIN) | 286 #if defined(OS_WIN) |
147 // Draws the given string with the beginning and/or the end using a fade | 287 // Draws the given string with the beginning and/or the end using a fade |
148 // gradient. When truncating the head | 288 // gradient. When truncating the head |
149 // |desired_characters_to_truncate_from_head| specifies the maximum number of | 289 // |desired_characters_to_truncate_from_head| specifies the maximum number of |
150 // characters that can be truncated. | 290 // characters that can be truncated. |
151 virtual void DrawFadeTruncatingString( | 291 void DrawFadeTruncatingString( |
152 const string16& text, | 292 const string16& text, |
153 TruncateFadeMode truncate_mode, | 293 TruncateFadeMode truncate_mode, |
154 size_t desired_characters_to_truncate_from_head, | 294 size_t desired_characters_to_truncate_from_head, |
155 const gfx::Font& font, | 295 const gfx::Font& font, |
156 const SkColor& color, | 296 const SkColor& color, |
157 const gfx::Rect& display_rect); | 297 const gfx::Rect& display_rect); |
158 #endif | 298 #endif |
159 virtual void DrawFocusRect(const gfx::Rect& rect) OVERRIDE; | |
160 virtual void TileImageInt(const SkBitmap& bitmap, | |
161 int x, int y, int w, int h) OVERRIDE; | |
162 virtual void TileImageInt(const SkBitmap& bitmap, | |
163 int src_x, int src_y, | |
164 int dest_x, int dest_y, int w, int h) OVERRIDE; | |
165 virtual gfx::NativeDrawingContext BeginPlatformPaint() OVERRIDE; | |
166 virtual void EndPlatformPaint() OVERRIDE; | |
167 virtual void Transform(const ui::Transform& transform) OVERRIDE; | |
168 virtual CanvasSkia* AsCanvasSkia() OVERRIDE; | |
169 virtual const CanvasSkia* AsCanvasSkia() const OVERRIDE; | |
170 virtual SkCanvas* GetSkCanvas() OVERRIDE; | |
171 virtual const SkCanvas* GetSkCanvas() const OVERRIDE; | |
172 | 299 |
173 SkCanvas* sk_canvas() const { return canvas_; } | 300 SkCanvas* sk_canvas() const { return canvas_; } |
174 skia::PlatformCanvas* platform_canvas() const { return owned_canvas_.get(); } | 301 skia::PlatformCanvas* platform_canvas() const { return owned_canvas_.get(); } |
175 | 302 |
176 private: | 303 private: |
177 // Test whether the provided rectangle intersects the current clip rect. | 304 // Test whether the provided rectangle intersects the current clip rect. |
178 bool IntersectsClipRectInt(int x, int y, int w, int h); | 305 bool IntersectsClipRectInt(int x, int y, int w, int h); |
179 | 306 |
180 #if defined(OS_WIN) | 307 #if defined(OS_WIN) |
181 // Draws text with the specified color, font and location. The text is | 308 // Draws text with the specified color, font and location. The text is |
182 // aligned to the left, vertically centered, clipped to the region. If the | 309 // aligned to the left, vertically centered, clipped to the region. If the |
183 // text is too big, it is truncated and '...' is added to the end. | 310 // text is too big, it is truncated and '...' is added to the end. |
184 void DrawStringInt(const string16& text, | 311 void DrawStringInt(const string16& text, |
185 HFONT font, | 312 HFONT font, |
186 const SkColor& color, | 313 const SkColor& color, |
187 int x, int y, int w, int h, | 314 int x, int y, int w, int h, |
188 int flags); | 315 int flags); |
189 #endif | 316 #endif |
190 | 317 |
191 scoped_ptr<skia::PlatformCanvas> owned_canvas_; | 318 scoped_ptr<skia::PlatformCanvas> owned_canvas_; |
192 SkCanvas* canvas_; | 319 SkCanvas* canvas_; |
193 | 320 |
194 DISALLOW_COPY_AND_ASSIGN(CanvasSkia); | 321 DISALLOW_COPY_AND_ASSIGN(CanvasSkia); |
195 }; | 322 }; |
196 | 323 |
197 } // namespace gfx | 324 } // namespace gfx |
198 | 325 |
199 #endif // UI_GFX_CANVAS_SKIA_H_ | 326 #endif // UI_GFX_CANVAS_SKIA_H_ |
OLD | NEW |