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