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