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