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, const gfx::Rect& layer_bounds); |
107 virtual void FillRect(const gfx::Rect& rect, | 158 |
108 const SkColor& color, | 159 // Restores the drawing state after a call to Save*(). It is an error to |
109 SkXfermode::Mode mode) OVERRIDE; | 160 // call Restore() more times than Save*(). |
110 virtual void FillRect(const gfx::Rect& rect, | 161 void Restore() ; |
111 const gfx::Brush* brush) OVERRIDE; | 162 |
112 virtual void DrawRect(const gfx::Rect& rect, const SkColor& color) OVERRIDE; | 163 // Returns true if the clip is non-empty. |
113 virtual void DrawRect(const gfx::Rect& rect, | 164 bool ClipRect(const gfx::Rect& rect); |
114 const SkColor& color, | 165 |
115 SkXfermode::Mode mode) OVERRIDE; | 166 void Translate(const gfx::Point& point); |
116 virtual void DrawRect(const gfx::Rect& rect, const SkPaint& paint) OVERRIDE; | 167 |
117 virtual void DrawLine(const gfx::Point& p1, | 168 void Scale(int x_scale, int y_scale); |
118 const gfx::Point& p2, | 169 |
119 const SkColor& color) OVERRIDE; | 170 // Fills |rect| with |color| using a transfer mode of |
120 virtual void DrawBitmapInt(const SkBitmap& bitmap, int x, int y) OVERRIDE; | 171 // SkXfermode::kSrcOver_Mode. |
121 virtual void DrawBitmapInt(const SkBitmap& bitmap, | 172 void FillRect(const gfx::Rect& rect, const SkColor& color); |
122 int x, int y, | 173 |
123 const SkPaint& paint) OVERRIDE; | 174 // Fills |rect| with the specified |color| and |mode|. |
124 virtual void DrawBitmapInt(const SkBitmap& bitmap, | 175 void FillRect(const gfx::Rect& rect, |
125 int src_x, int src_y, int src_w, int src_h, | 176 const SkColor& color, |
126 int dest_x, int dest_y, int dest_w, int dest_h, | 177 SkXfermode::Mode mode); |
127 bool filter) OVERRIDE; | 178 |
128 virtual void DrawBitmapInt(const SkBitmap& bitmap, | 179 // Fills |rect| with the specified |brush|. |
129 int src_x, int src_y, int src_w, int src_h, | 180 void FillRect(const gfx::Rect& rect, const gfx::Brush* brush); |
130 int dest_x, int dest_y, int dest_w, int dest_h, | 181 |
131 bool filter, | 182 // Draws a single pixel rect in the specified region with the specified |
132 const SkPaint& paint) OVERRIDE; | 183 // color, using a transfer mode of SkXfermode::kSrcOver_Mode. |
133 virtual void DrawStringInt(const string16& text, | 184 // |
134 const gfx::Font& font, | 185 // NOTE: if you need a single pixel line, use DrawLine. |
135 const SkColor& color, | 186 void DrawRect(const gfx::Rect& rect, const SkColor& color); |
136 int x, int y, int w, int h) OVERRIDE; | 187 |
137 virtual void DrawStringInt(const string16& text, | 188 // Draws a single pixel rect in the specified region with the specified |
138 const gfx::Font& font, | 189 // color and transfer mode. |
139 const SkColor& color, | 190 // |
140 const gfx::Rect& display_rect) OVERRIDE; | 191 // NOTE: if you need a single pixel line, use DrawLine. |
141 virtual void DrawStringInt(const string16& text, | 192 void DrawRect(const gfx::Rect& rect, |
142 const gfx::Font& font, | 193 const SkColor& color, |
143 const SkColor& color, | 194 SkXfermode::Mode mode); |
144 int x, int y, int w, int h, | 195 |
145 int flags) OVERRIDE; | 196 // Draws the given rectangle with the given paint's parameters. |
| 197 void DrawRect(const gfx::Rect& rect, const SkPaint& paint); |
| 198 |
| 199 // Draws a single pixel line with the specified color. |
| 200 void DrawLine(const gfx::Point& p1, |
| 201 const gfx::Point& p2, |
| 202 const SkColor& color); |
| 203 |
| 204 // Draws a bitmap with the origin at the specified location. The upper left |
| 205 // corner of the bitmap is rendered at the specified location. |
| 206 void DrawBitmapInt(const SkBitmap& bitmap, int x, int y); |
| 207 |
| 208 // Draws a bitmap with the origin at the specified location, using the |
| 209 // specified paint. The upper left corner of the bitmap is rendered at the |
| 210 // specified location. |
| 211 void DrawBitmapInt(const SkBitmap& bitmap, |
| 212 int x, int y, |
| 213 const SkPaint& paint); |
| 214 |
| 215 // Draws a portion of a bitmap in the specified location. The src parameters |
| 216 // correspond to the region of the bitmap to draw in the region defined |
| 217 // by the dest coordinates. |
| 218 // |
| 219 // If the width or height of the source differs from that of the destination, |
| 220 // the bitmap will be scaled. When scaling down, it is highly recommended |
| 221 // that you call buildMipMap(false) on your bitmap to ensure that it has |
| 222 // a mipmap, which will result in much higher-quality output. Set |filter| |
| 223 // to use filtering for bitmaps, otherwise the nearest-neighbor algorithm |
| 224 // is used for resampling. |
| 225 // |
| 226 // An optional custom SkPaint can be provided. |
| 227 void DrawBitmapInt(const SkBitmap& bitmap, |
| 228 int src_x, int src_y, int src_w, int src_h, |
| 229 int dest_x, int dest_y, int dest_w, int dest_h, |
| 230 bool filter); |
| 231 void DrawBitmapInt(const SkBitmap& bitmap, |
| 232 int src_x, int src_y, int src_w, int src_h, |
| 233 int dest_x, int dest_y, int dest_w, int dest_h, |
| 234 bool filter, |
| 235 const SkPaint& paint); |
| 236 |
| 237 // Draws text with the specified color, font and location. The text is |
| 238 // aligned to the left, vertically centered, clipped to the region. If the |
| 239 // text is too big, it is truncated and '...' is added to the end. |
| 240 void DrawStringInt(const string16& text, |
| 241 const gfx::Font& font, |
| 242 const SkColor& color, |
| 243 int x, int y, int w, int h); |
| 244 void DrawStringInt(const string16& text, |
| 245 const gfx::Font& font, |
| 246 const SkColor& color, |
| 247 const gfx::Rect& display_rect); |
| 248 |
| 249 // Draws text with the specified color, font and location. The last argument |
| 250 // specifies flags for how the text should be rendered. It can be one of |
| 251 // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT. |
| 252 void DrawStringInt(const string16& text, |
| 253 const gfx::Font& font, |
| 254 const SkColor& color, |
| 255 int x, int y, int w, int h, |
| 256 int flags); |
| 257 |
| 258 // Draws a dotted gray rectangle used for focus purposes. |
| 259 void DrawFocusRect(const gfx::Rect& rect); |
| 260 |
| 261 // Tiles the image in the specified region. |
| 262 void TileImageInt(const SkBitmap& bitmap, |
| 263 int x, int y, int w, int h); |
| 264 void TileImageInt(const SkBitmap& bitmap, |
| 265 int src_x, int src_y, |
| 266 int dest_x, int dest_y, int w, int h); |
| 267 |
| 268 // Returns a native drawing context for platform specific drawing routines to |
| 269 // use. Must be balanced by a call to EndPlatformPaint(). |
| 270 NativeDrawingContext BeginPlatformPaint(); |
| 271 |
| 272 // Signifies the end of platform drawing using the native drawing context |
| 273 // returned by BeginPlatformPaint(). |
| 274 void EndPlatformPaint(); |
| 275 |
| 276 // Apply transformation on the canvas. |
| 277 void Transform(const ui::Transform& transform); |
| 278 |
146 #if defined(OS_WIN) | 279 #if defined(OS_WIN) |
147 // Draws the given string with the beginning and/or the end using a fade | 280 // Draws the given string with the beginning and/or the end using a fade |
148 // gradient. When truncating the head | 281 // gradient. When truncating the head |
149 // |desired_characters_to_truncate_from_head| specifies the maximum number of | 282 // |desired_characters_to_truncate_from_head| specifies the maximum number of |
150 // characters that can be truncated. | 283 // characters that can be truncated. |
151 virtual void DrawFadeTruncatingString( | 284 void DrawFadeTruncatingString( |
152 const string16& text, | 285 const string16& text, |
153 TruncateFadeMode truncate_mode, | 286 TruncateFadeMode truncate_mode, |
154 size_t desired_characters_to_truncate_from_head, | 287 size_t desired_characters_to_truncate_from_head, |
155 const gfx::Font& font, | 288 const gfx::Font& font, |
156 const SkColor& color, | 289 const SkColor& color, |
157 const gfx::Rect& display_rect); | 290 const gfx::Rect& display_rect); |
158 #endif | 291 #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 | 292 |
| 293 // TODO(tfarina): Remove these. And stick with sk_canvas() only. |
| 294 CanvasSkia* AsCanvasSkia(); |
| 295 const CanvasSkia* AsCanvasSkia() const; |
| 296 SkCanvas* GetSkCanvas(); |
| 297 const SkCanvas* GetSkCanvas() const; |
173 SkCanvas* sk_canvas() const { return canvas_; } | 298 SkCanvas* sk_canvas() const { return canvas_; } |
| 299 |
174 skia::PlatformCanvas* platform_canvas() const { return owned_canvas_.get(); } | 300 skia::PlatformCanvas* platform_canvas() const { return owned_canvas_.get(); } |
175 | 301 |
176 private: | 302 private: |
177 // Test whether the provided rectangle intersects the current clip rect. | 303 // Test whether the provided rectangle intersects the current clip rect. |
178 bool IntersectsClipRectInt(int x, int y, int w, int h); | 304 bool IntersectsClipRectInt(int x, int y, int w, int h); |
179 | 305 |
180 #if defined(OS_WIN) | 306 #if defined(OS_WIN) |
181 // Draws text with the specified color, font and location. The text is | 307 // 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 | 308 // 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. | 309 // text is too big, it is truncated and '...' is added to the end. |
184 void DrawStringInt(const string16& text, | 310 void DrawStringInt(const string16& text, |
185 HFONT font, | 311 HFONT font, |
186 const SkColor& color, | 312 const SkColor& color, |
187 int x, int y, int w, int h, | 313 int x, int y, int w, int h, |
188 int flags); | 314 int flags); |
189 #endif | 315 #endif |
190 | 316 |
191 scoped_ptr<skia::PlatformCanvas> owned_canvas_; | 317 scoped_ptr<skia::PlatformCanvas> owned_canvas_; |
192 SkCanvas* canvas_; | 318 SkCanvas* canvas_; |
193 | 319 |
194 DISALLOW_COPY_AND_ASSIGN(CanvasSkia); | 320 DISALLOW_COPY_AND_ASSIGN(CanvasSkia); |
195 }; | 321 }; |
196 | 322 |
197 } // namespace gfx | 323 } // namespace gfx |
198 | 324 |
199 #endif // UI_GFX_CANVAS_SKIA_H_ | 325 #endif // UI_GFX_CANVAS_SKIA_H_ |
OLD | NEW |