[email protected] | fa8cfb0 | 2012-01-13 00:27:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef UI_GFX_CANVAS_H_ |
| 6 | #define UI_GFX_CANVAS_H_ |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 7 | |
[email protected] | 0834e1b1 | 2012-04-11 02:23:56 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 10 | #include "base/basictypes.h" |
| 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | f3652ff9 | 2013-06-11 13:54:31 | [diff] [blame] | 12 | #include "base/strings/string16.h" |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 13 | #include "skia/ext/platform_canvas.h" |
[email protected] | 3699ae5a | 2012-12-05 01:00:54 | [diff] [blame] | 14 | #include "skia/ext/refptr.h" |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 15 | #include "ui/gfx/image/image_skia.h" |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 16 | #include "ui/gfx/native_widget_types.h" |
[email protected] | 2565068 | 2012-05-29 23:09:19 | [diff] [blame] | 17 | #include "ui/gfx/shadow_value.h" |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 18 | |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 19 | namespace gfx { |
| 20 | |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 21 | class Rect; |
| 22 | class Font; |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 23 | class FontList; |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 24 | class Point; |
| 25 | class Size; |
[email protected] | 0f0453e | 2012-10-14 18:15:35 | [diff] [blame] | 26 | class Transform; |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 27 | |
| 28 | // Canvas is a SkCanvas wrapper that provides a number of methods for |
| 29 | // common operations used throughout an application built using ui/gfx. |
| 30 | // |
| 31 | // All methods that take integer arguments (as is used throughout views) |
| 32 | // end with Int. If you need to use methods provided by SkCanvas, you'll |
| 33 | // need to do a conversion. In particular you'll need to use |SkIntToScalar()|, |
| 34 | // or if converting from a scalar to an integer |SkScalarRound()|. |
| 35 | // |
| 36 | // A handful of methods in this class are overloaded providing an additional |
| 37 | // argument of type SkXfermode::Mode. SkXfermode::Mode specifies how the |
| 38 | // source and destination colors are combined. Unless otherwise specified, |
| 39 | // the variant that does not take a SkXfermode::Mode uses a transfer mode |
| 40 | // of kSrcOver_Mode. |
| 41 | class UI_EXPORT Canvas { |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 42 | public: |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 43 | enum TruncateFadeMode { |
| 44 | TruncateFadeTail, |
| 45 | TruncateFadeHead, |
| 46 | TruncateFadeHeadAndTail, |
| 47 | }; |
| 48 | |
| 49 | // Specifies the alignment for text rendered with the DrawStringInt method. |
| 50 | enum { |
| 51 | TEXT_ALIGN_LEFT = 1 << 0, |
| 52 | TEXT_ALIGN_CENTER = 1 << 1, |
[email protected] | ff1af58 | 2012-11-06 03:34:12 | [diff] [blame] | 53 | TEXT_ALIGN_RIGHT = 1 << 2, |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 54 | |
| 55 | // Specifies the text consists of multiple lines. |
[email protected] | ff1af58 | 2012-11-06 03:34:12 | [diff] [blame] | 56 | MULTI_LINE = 1 << 3, |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 57 | |
| 58 | // By default DrawStringInt does not process the prefix ('&') character |
| 59 | // specially. That is, the string "&foo" is rendered as "&foo". When |
| 60 | // rendering text from a resource that uses the prefix character for |
| 61 | // mnemonics, the prefix should be processed and can be rendered as an |
| 62 | // underline (SHOW_PREFIX), or not rendered at all (HIDE_PREFIX). |
[email protected] | ff1af58 | 2012-11-06 03:34:12 | [diff] [blame] | 63 | SHOW_PREFIX = 1 << 4, |
| 64 | HIDE_PREFIX = 1 << 5, |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 65 | |
| 66 | // Prevent ellipsizing |
[email protected] | ff1af58 | 2012-11-06 03:34:12 | [diff] [blame] | 67 | NO_ELLIPSIS = 1 << 6, |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 68 | |
| 69 | // Specifies if words can be split by new lines. |
| 70 | // This only works with MULTI_LINE. |
[email protected] | ff1af58 | 2012-11-06 03:34:12 | [diff] [blame] | 71 | CHARACTER_BREAK = 1 << 7, |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 72 | |
| 73 | // Instructs DrawStringInt() to render the text using RTL directionality. |
| 74 | // In most cases, passing this flag is not necessary because information |
| 75 | // about the text directionality is going to be embedded within the string |
| 76 | // in the form of special Unicode characters. However, we don't insert |
| 77 | // directionality characters into strings if the locale is LTR because some |
| 78 | // platforms (for example, an English Windows XP with no RTL fonts |
| 79 | // installed) don't support these characters. Thus, this flag should be |
| 80 | // used to render text using RTL directionality when the locale is LTR. |
[email protected] | ff1af58 | 2012-11-06 03:34:12 | [diff] [blame] | 81 | FORCE_RTL_DIRECTIONALITY = 1 << 8, |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 82 | |
| 83 | // Similar to FORCE_RTL_DIRECTIONALITY, but left-to-right. |
| 84 | // See FORCE_RTL_DIRECTIONALITY for details. |
[email protected] | ff1af58 | 2012-11-06 03:34:12 | [diff] [blame] | 85 | FORCE_LTR_DIRECTIONALITY = 1 << 9, |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 86 | |
| 87 | // Instructs DrawStringInt() to not use subpixel rendering. This is useful |
| 88 | // when rendering text onto a fully- or partially-transparent background |
| 89 | // that will later be blended with another image. |
[email protected] | ff1af58 | 2012-11-06 03:34:12 | [diff] [blame] | 90 | NO_SUBPIXEL_RENDERING = 1 << 10, |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 91 | }; |
| 92 | |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame^] | 93 | // Creates an empty canvas with image_scale of 1x. |
[email protected] | 94a0d258 | 2012-03-09 00:30:59 | [diff] [blame] | 94 | Canvas(); |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 95 | |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame^] | 96 | // Creates canvas with provided DIP |size| and |image_scale|. |
[email protected] | 3b2591a | 2012-06-27 17:58:41 | [diff] [blame] | 97 | // If this canvas is not opaque, it's explicitly cleared to transparent before |
| 98 | // being returned. |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame^] | 99 | Canvas(const Size& size, float image_scale, bool is_opaque); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 100 | |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame^] | 101 | // Constructs a canvas with the size and the image_scale of the provided |
| 102 | // |image_rep|, and draws the |image_rep| into it. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 103 | Canvas(const ImageSkiaRep& image_rep, bool is_opaque); |
[email protected] | 3b2591a | 2012-06-27 17:58:41 | [diff] [blame] | 104 | |
[email protected] | 57047da | 2012-07-23 20:55:08 | [diff] [blame] | 105 | virtual ~Canvas(); |
[email protected] | f7fc6021 | 2012-07-23 20:27:08 | [diff] [blame] | 106 | |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame^] | 107 | // Creates a Canvas backed by an |sk_canvas| with |image_scale_|. |
| 108 | // |sk_canvas| is assumed to be already scaled based on |image_scale| |
[email protected] | de13f35 | 2012-07-24 16:51:16 | [diff] [blame] | 109 | // so no additional scaling is applied. |
| 110 | static Canvas* CreateCanvasWithoutScaling(SkCanvas* sk_canvas, |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame^] | 111 | float image_scale); |
[email protected] | de13f35 | 2012-07-24 16:51:16 | [diff] [blame] | 112 | |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame^] | 113 | // Recreates the backing platform canvas with DIP |size| and |image_scale_|. |
[email protected] | 3b2591a | 2012-06-27 17:58:41 | [diff] [blame] | 114 | // If the canvas is not opaque, it is explicitly cleared. |
| 115 | // This method is public so that canvas_skia_paint can recreate the platform |
| 116 | // canvas after having initialized the canvas. |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame^] | 117 | // TODO(pkotwicz): Push the image_scale into skia::PlatformCanvas such that |
[email protected] | 3b2591a | 2012-06-27 17:58:41 | [diff] [blame] | 118 | // this method can be private. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 119 | void RecreateBackingCanvas(const Size& size, |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame^] | 120 | float image_scale, |
[email protected] | 3b2591a | 2012-06-27 17:58:41 | [diff] [blame] | 121 | bool is_opaque); |
| 122 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 123 | // Compute the size required to draw some text with the provided fonts. |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 124 | // Attempts to fit the text with the provided width and height. Increases |
| 125 | // height and then width as needed to make the text fit. This method |
[email protected] | 561f748 | 2013-04-19 03:39:58 | [diff] [blame] | 126 | // supports multiple lines. On Skia only a line_height can be specified and |
| 127 | // specifying a 0 value for it will cause the default height to be used. |
[email protected] | 031ffed | 2013-06-09 03:32:36 | [diff] [blame] | 128 | static void SizeStringInt(const base::string16& text, |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 129 | const FontList& font_list, |
| 130 | int* width, |
| 131 | int* height, |
| 132 | int line_height, |
| 133 | int flags); |
| 134 | // Obsolete version. Use the above version which takes FontList. |
| 135 | static void SizeStringInt(const base::string16& text, |
| 136 | const Font& font, |
| 137 | int* width, |
| 138 | int* height, |
[email protected] | 561f748 | 2013-04-19 03:39:58 | [diff] [blame] | 139 | int line_height, |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 140 | int flags); |
| 141 | |
| 142 | // Returns the number of horizontal pixels needed to display the specified |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 143 | // |text| with |font_list|. |
| 144 | static int GetStringWidth(const base::string16& text, |
| 145 | const FontList& font_list); |
| 146 | // Obsolete version. Use the above version which takes FontList. |
| 147 | static int GetStringWidth(const base::string16& text, const Font& font); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 148 | |
| 149 | // Returns the default text alignment to be used when drawing text on a |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 150 | // Canvas based on the directionality of the system locale language. |
| 151 | // This function is used by Canvas::DrawStringInt when the text alignment |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 152 | // is not specified. |
| 153 | // |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 154 | // This function returns either Canvas::TEXT_ALIGN_LEFT or |
| 155 | // Canvas::TEXT_ALIGN_RIGHT. |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 156 | static int DefaultCanvasTextAlignment(); |
| 157 | |
| 158 | // Draws text with a 1-pixel halo around it of the given color. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 159 | // On Windows, it allows ClearType to be drawn to an otherwise transparent |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 160 | // bitmap for drag images. Drag images have only 1-bit of transparency, so |
| 161 | // we don't do any fancy blurring. |
| 162 | // On Linux, text with halo is created by stroking it with 2px |halo_color| |
| 163 | // then filling it with |text_color|. |
| 164 | // On Mac, NOTIMPLEMENTED. |
| 165 | // TODO(dhollowa): Skia-native implementation is underway. Cut over to |
| 166 | // that when ready. http::/crbug.com/109946 |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 167 | void DrawStringRectWithHalo(const base::string16& text, |
| 168 | const FontList& font_list, |
| 169 | SkColor text_color, |
| 170 | SkColor halo_color, |
| 171 | const Rect& display_rect, |
| 172 | int flags); |
| 173 | // Obsolete version. Use the above version which takes FontList. |
[email protected] | 031ffed | 2013-06-09 03:32:36 | [diff] [blame] | 174 | void DrawStringWithHalo(const base::string16& text, |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 175 | const Font& font, |
[email protected] | 1b81ff2 | 2012-03-24 22:04:42 | [diff] [blame] | 176 | SkColor text_color, |
| 177 | SkColor halo_color, |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 178 | int x, |
| 179 | int y, |
| 180 | int w, |
| 181 | int h, |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 182 | int flags); |
| 183 | |
[email protected] | 3b2591a | 2012-06-27 17:58:41 | [diff] [blame] | 184 | // Extracts an ImageSkiaRep from the contents of this canvas. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 185 | ImageSkiaRep ExtractImageRep() const; |
[email protected] | 3b2591a | 2012-06-27 17:58:41 | [diff] [blame] | 186 | |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 187 | // Draws a dashed rectangle of the specified color. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 188 | void DrawDashedRect(const Rect& rect, SkColor color); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 189 | |
| 190 | // Saves a copy of the drawing state onto a stack, operating on this copy |
| 191 | // until a balanced call to Restore() is made. |
| 192 | void Save(); |
| 193 | |
| 194 | // As with Save(), except draws to a layer that is blended with the canvas |
| 195 | // at the specified alpha once Restore() is called. |
| 196 | // |layer_bounds| are the bounds of the layer relative to the current |
| 197 | // transform. |
| 198 | void SaveLayerAlpha(uint8 alpha); |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 199 | void SaveLayerAlpha(uint8 alpha, const Rect& layer_bounds); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 200 | |
| 201 | // Restores the drawing state after a call to Save*(). It is an error to |
| 202 | // call Restore() more times than Save*(). |
[email protected] | 964c429d | 2012-10-25 15:28:45 | [diff] [blame] | 203 | void Restore(); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 204 | |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 205 | // Adds |rect| to the current clip. Returns true if the resulting clip is |
| 206 | // non-empty. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 207 | bool ClipRect(const Rect& rect); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 208 | |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 209 | // Adds |path| to the current clip. Returns true if the resulting clip is |
| 210 | // non-empty. |
| 211 | bool ClipPath(const SkPath& path); |
| 212 | |
| 213 | // Returns the bounds of the current clip (in local coordinates) in the |
| 214 | // |bounds| parameter, and returns true if it is non empty. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 215 | bool GetClipBounds(Rect* bounds); |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 216 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 217 | void Translate(const Vector2d& offset); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 218 | |
| 219 | void Scale(int x_scale, int y_scale); |
| 220 | |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 221 | // Fills the entire canvas' bitmap (restricted to current clip) with |
| 222 | // specified |color| using a transfer mode of SkXfermode::kSrcOver_Mode. |
| 223 | void DrawColor(SkColor color); |
| 224 | |
| 225 | // Fills the entire canvas' bitmap (restricted to current clip) with |
| 226 | // specified |color| and |mode|. |
| 227 | void DrawColor(SkColor color, SkXfermode::Mode mode); |
| 228 | |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 229 | // Fills |rect| with |color| using a transfer mode of |
| 230 | // SkXfermode::kSrcOver_Mode. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 231 | void FillRect(const Rect& rect, SkColor color); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 232 | |
| 233 | // Fills |rect| with the specified |color| and |mode|. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 234 | void FillRect(const Rect& rect, SkColor color, SkXfermode::Mode mode); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 235 | |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 236 | // Draws a single pixel rect in the specified region with the specified |
| 237 | // color, using a transfer mode of SkXfermode::kSrcOver_Mode. |
| 238 | // |
| 239 | // NOTE: if you need a single pixel line, use DrawLine. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 240 | void DrawRect(const Rect& rect, SkColor color); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 241 | |
| 242 | // Draws a single pixel rect in the specified region with the specified |
| 243 | // color and transfer mode. |
| 244 | // |
| 245 | // NOTE: if you need a single pixel line, use DrawLine. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 246 | void DrawRect(const Rect& rect, SkColor color, SkXfermode::Mode mode); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 247 | |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 248 | // Draws the given rectangle with the given |paint| parameters. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 249 | void DrawRect(const Rect& rect, const SkPaint& paint); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 250 | |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 251 | // Draw the given point with the given |paint| parameters. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 252 | void DrawPoint(const Point& p, const SkPaint& paint); |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 253 | |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 254 | // Draws a single pixel line with the specified color. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 255 | void DrawLine(const Point& p1, const Point& p2, SkColor color); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 256 | |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 257 | // Draws a line with the given |paint| parameters. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 258 | void DrawLine(const Point& p1, const Point& p2, const SkPaint& paint); |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 259 | |
| 260 | // Draws a circle with the given |paint| parameters. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 261 | void DrawCircle(const Point& center_point, |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 262 | int radius, |
| 263 | const SkPaint& paint); |
| 264 | |
| 265 | // Draws the given rectangle with rounded corners of |radius| using the |
| 266 | // given |paint| parameters. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 267 | void DrawRoundRect(const Rect& rect, int radius, const SkPaint& paint); |
[email protected] | ffdd1fd | 2012-04-09 23:24:41 | [diff] [blame] | 268 | |
| 269 | // Draws the given path using the given |paint| parameters. |
| 270 | void DrawPath(const SkPath& path, const SkPaint& paint); |
| 271 | |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 272 | // Draws an image with the origin at the specified location. The upper left |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 273 | // corner of the bitmap is rendered at the specified location. |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 274 | // Parameters are specified relative to current canvas scale not in pixels. |
[email protected] | 8232da6b | 2012-07-02 19:41:15 | [diff] [blame] | 275 | // Thus, x is 2 pixels if canvas scale = 2 & |x| = 1. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 276 | void DrawImageInt(const ImageSkia&, int x, int y); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 277 | |
[email protected] | 2885cb3 | 2012-10-03 19:41:11 | [diff] [blame] | 278 | // Helper for DrawImageInt(..., paint) that constructs a temporary paint and |
| 279 | // calls paint.setAlpha(alpha). |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 280 | void DrawImageInt(const ImageSkia&, int x, int y, uint8 alpha); |
[email protected] | 2885cb3 | 2012-10-03 19:41:11 | [diff] [blame] | 281 | |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 282 | // Draws an image with the origin at the specified location, using the |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 283 | // specified paint. The upper left corner of the bitmap is rendered at the |
| 284 | // specified location. |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 285 | // Parameters are specified relative to current canvas scale not in pixels. |
| 286 | // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 287 | void DrawImageInt(const ImageSkia& image, |
| 288 | int x, |
| 289 | int y, |
[email protected] | 243cac7d | 2012-06-07 13:29:20 | [diff] [blame] | 290 | const SkPaint& paint); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 291 | |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 292 | // Draws a portion of an image in the specified location. The src parameters |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 293 | // correspond to the region of the bitmap to draw in the region defined |
| 294 | // by the dest coordinates. |
| 295 | // |
| 296 | // If the width or height of the source differs from that of the destination, |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 297 | // the image will be scaled. When scaling down, a mipmap will be generated. |
| 298 | // Set |filter| to use filtering for images, otherwise the nearest-neighbor |
| 299 | // algorithm is used for resampling. |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 300 | // |
| 301 | // An optional custom SkPaint can be provided. |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 302 | // Parameters are specified relative to current canvas scale not in pixels. |
| 303 | // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 304 | void DrawImageInt(const ImageSkia& image, |
| 305 | int src_x, |
| 306 | int src_y, |
| 307 | int src_w, |
| 308 | int src_h, |
| 309 | int dest_x, |
| 310 | int dest_y, |
| 311 | int dest_w, |
| 312 | int dest_h, |
[email protected] | 243cac7d | 2012-06-07 13:29:20 | [diff] [blame] | 313 | bool filter); |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 314 | void DrawImageInt(const ImageSkia& image, |
| 315 | int src_x, |
| 316 | int src_y, |
| 317 | int src_w, |
| 318 | int src_h, |
| 319 | int dest_x, |
| 320 | int dest_y, |
| 321 | int dest_w, |
| 322 | int dest_h, |
[email protected] | 243cac7d | 2012-06-07 13:29:20 | [diff] [blame] | 323 | bool filter, |
| 324 | const SkPaint& paint); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 325 | |
[email protected] | 8232da6b | 2012-07-02 19:41:15 | [diff] [blame] | 326 | // Draws an |image| with the top left corner at |x| and |y|, clipped to |
| 327 | // |path|. |
| 328 | // Parameters are specified relative to current canvas scale not in pixels. |
| 329 | // Thus, x is 2 pixels if canvas scale = 2 & |x| = 1. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 330 | void DrawImageInPath(const ImageSkia& image, |
[email protected] | 8232da6b | 2012-07-02 19:41:15 | [diff] [blame] | 331 | int x, |
| 332 | int y, |
| 333 | const SkPath& path, |
| 334 | const SkPaint& paint); |
| 335 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 336 | // Draws text with the specified color, fonts and location. The text is |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 337 | // aligned to the left, vertically centered, clipped to the region. If the |
| 338 | // text is too big, it is truncated and '...' is added to the end. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 339 | void DrawStringRect(const base::string16& text, |
| 340 | const FontList& font_list, |
| 341 | SkColor color, |
| 342 | const Rect& display_rect); |
| 343 | // Obsolete versions. Use the above versions which take FontList. |
[email protected] | 031ffed | 2013-06-09 03:32:36 | [diff] [blame] | 344 | void DrawStringInt(const base::string16& text, |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 345 | const Font& font, |
[email protected] | 1b81ff2 | 2012-03-24 22:04:42 | [diff] [blame] | 346 | SkColor color, |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 347 | int x, |
| 348 | int y, |
| 349 | int w, |
| 350 | int h); |
[email protected] | 031ffed | 2013-06-09 03:32:36 | [diff] [blame] | 351 | void DrawStringInt(const base::string16& text, |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 352 | const Font& font, |
[email protected] | 1b81ff2 | 2012-03-24 22:04:42 | [diff] [blame] | 353 | SkColor color, |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 354 | const Rect& display_rect); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 355 | |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 356 | // Draws text with the specified color, fonts and location. The last argument |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 357 | // specifies flags for how the text should be rendered. It can be one of |
| 358 | // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 359 | void DrawStringRectWithFlags(const base::string16& text, |
| 360 | const FontList& font_list, |
| 361 | SkColor color, |
| 362 | const Rect& display_rect, |
| 363 | int flags); |
| 364 | // Obsolete version. Use the above version which takes FontList. |
[email protected] | 031ffed | 2013-06-09 03:32:36 | [diff] [blame] | 365 | void DrawStringInt(const base::string16& text, |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 366 | const Font& font, |
[email protected] | 1b81ff2 | 2012-03-24 22:04:42 | [diff] [blame] | 367 | SkColor color, |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 368 | int x, |
| 369 | int y, |
| 370 | int w, |
| 371 | int h, |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 372 | int flags); |
| 373 | |
[email protected] | 0834e1b1 | 2012-04-11 02:23:56 | [diff] [blame] | 374 | // Similar to above DrawStringInt method but with text shadows support. |
[email protected] | 561f748 | 2013-04-19 03:39:58 | [diff] [blame] | 375 | // Currently it's only implemented for canvas skia. Specifying a 0 line_height |
| 376 | // will cause the default height to be used. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 377 | void DrawStringRectWithShadows(const base::string16& text, |
| 378 | const FontList& font_list, |
| 379 | SkColor color, |
| 380 | const Rect& text_bounds, |
| 381 | int line_height, |
| 382 | int flags, |
| 383 | const ShadowValues& shadows); |
| 384 | // Obsolete version. Use the above version which takes FontList. |
[email protected] | 031ffed | 2013-06-09 03:32:36 | [diff] [blame] | 385 | void DrawStringWithShadows(const base::string16& text, |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 386 | const Font& font, |
[email protected] | 0834e1b1 | 2012-04-11 02:23:56 | [diff] [blame] | 387 | SkColor color, |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 388 | const Rect& text_bounds, |
[email protected] | 561f748 | 2013-04-19 03:39:58 | [diff] [blame] | 389 | int line_height, |
[email protected] | 0834e1b1 | 2012-04-11 02:23:56 | [diff] [blame] | 390 | int flags, |
[email protected] | 2565068 | 2012-05-29 23:09:19 | [diff] [blame] | 391 | const ShadowValues& shadows); |
[email protected] | 0834e1b1 | 2012-04-11 02:23:56 | [diff] [blame] | 392 | |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 393 | // Draws a dotted gray rectangle used for focus purposes. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 394 | void DrawFocusRect(const Rect& rect); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 395 | |
| 396 | // Tiles the image in the specified region. |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 397 | // Parameters are specified relative to current canvas scale not in pixels. |
| 398 | // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 399 | void TileImageInt(const ImageSkia& image, |
| 400 | int x, |
| 401 | int y, |
| 402 | int w, |
| 403 | int h); |
| 404 | void TileImageInt(const ImageSkia& image, |
| 405 | int src_x, |
| 406 | int src_y, |
| 407 | int dest_x, |
| 408 | int dest_y, |
| 409 | int w, |
| 410 | int h); |
| 411 | void TileImageInt(const ImageSkia& image, |
| 412 | int src_x, |
| 413 | int src_y, |
| 414 | float tile_scale_x, |
| 415 | float tile_scale_y, |
| 416 | int dest_x, |
| 417 | int dest_y, |
| 418 | int w, |
| 419 | int h); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 420 | |
| 421 | // Returns a native drawing context for platform specific drawing routines to |
| 422 | // use. Must be balanced by a call to EndPlatformPaint(). |
| 423 | NativeDrawingContext BeginPlatformPaint(); |
| 424 | |
| 425 | // Signifies the end of platform drawing using the native drawing context |
| 426 | // returned by BeginPlatformPaint(). |
| 427 | void EndPlatformPaint(); |
| 428 | |
| 429 | // Apply transformation on the canvas. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 430 | void Transform(const Transform& transform); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 431 | |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 432 | // Draws the given string with the beginning and/or the end using a fade |
| 433 | // gradient. When truncating the head |
| 434 | // |desired_characters_to_truncate_from_head| specifies the maximum number of |
| 435 | // characters that can be truncated. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 436 | void DrawFadeTruncatingStringRect( |
| 437 | const base::string16& text, |
| 438 | TruncateFadeMode truncate_mode, |
| 439 | size_t desired_characters_to_truncate_from_head, |
| 440 | const FontList& font_list, |
| 441 | SkColor color, |
| 442 | const Rect& display_rect); |
| 443 | // Obsolete version. Use the above version which takes FontList. |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 444 | void DrawFadeTruncatingString( |
[email protected] | 031ffed | 2013-06-09 03:32:36 | [diff] [blame] | 445 | const base::string16& text, |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 446 | TruncateFadeMode truncate_mode, |
| 447 | size_t desired_characters_to_truncate_from_head, |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 448 | const Font& font, |
[email protected] | 1b81ff2 | 2012-03-24 22:04:42 | [diff] [blame] | 449 | SkColor color, |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 450 | const Rect& display_rect); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 451 | |
| 452 | skia::PlatformCanvas* platform_canvas() const { return owned_canvas_.get(); } |
| 453 | SkCanvas* sk_canvas() const { return canvas_; } |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame^] | 454 | float image_scale() const { return image_scale_; } |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 455 | |
| 456 | private: |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame^] | 457 | Canvas(SkCanvas* canvas, float image_scale); |
[email protected] | de13f35 | 2012-07-24 16:51:16 | [diff] [blame] | 458 | |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 459 | // Test whether the provided rectangle intersects the current clip rect. |
| 460 | bool IntersectsClipRectInt(int x, int y, int w, int h); |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 461 | bool IntersectsClipRect(const Rect& rect); |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 462 | |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame^] | 463 | // Returns the image rep which best matches the canvas |image_scale_|. |
[email protected] | 8232da6b | 2012-07-02 19:41:15 | [diff] [blame] | 464 | // Returns a null image rep if |image| contains no image reps. |
| 465 | // Builds mip map for returned image rep if necessary. |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 466 | // |
| 467 | // An optional additional user defined scale can be provided. |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 468 | const ImageSkiaRep& GetImageRepToPaint(const ImageSkia& image) const; |
| 469 | const ImageSkiaRep& GetImageRepToPaint( |
| 470 | const ImageSkia& image, |
[email protected] | 8232da6b | 2012-07-02 19:41:15 | [diff] [blame] | 471 | float user_defined_scale_factor_x, |
[email protected] | ad8d9d5 | 2013-08-22 07:20:05 | [diff] [blame] | 472 | float user_defined_scale_factor_y) const; |
[email protected] | 35d5334 | 2012-05-16 21:30:12 | [diff] [blame] | 473 | |
[email protected] | 3b2591a | 2012-06-27 17:58:41 | [diff] [blame] | 474 | // The device scale factor at which drawing on this canvas occurs. |
| 475 | // An additional scale can be applied via Canvas::Scale(). However, |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame^] | 476 | // Canvas::Scale() does not affect |image_scale_|. |
| 477 | float image_scale_; |
[email protected] | 3b2591a | 2012-06-27 17:58:41 | [diff] [blame] | 478 | |
[email protected] | 3699ae5a | 2012-12-05 01:00:54 | [diff] [blame] | 479 | skia::RefPtr<skia::PlatformCanvas> owned_canvas_; |
[email protected] | de13f35 | 2012-07-24 16:51:16 | [diff] [blame] | 480 | SkCanvas* canvas_; |
| 481 | |
[email protected] | 3127f663 | 2012-03-17 00:14:06 | [diff] [blame] | 482 | DISALLOW_COPY_AND_ASSIGN(Canvas); |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 483 | }; |
| 484 | |
[email protected] | 7fe2839 | 2011-10-27 00:16:05 | [diff] [blame] | 485 | } // namespace gfx |
[email protected] | 267c03d | 2011-02-02 23:03:07 | [diff] [blame] | 486 | |
| 487 | #endif // UI_GFX_CANVAS_H_ |