blob: 4470023afebd786fcde10a64d850e42554d4c28e [file] [log] [blame]
[email protected]fa8cfb02012-01-13 00:27:411// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]267c03d2011-02-02 23:03:072// 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]267c03d2011-02-02 23:03:077
[email protected]0834e1b12012-04-11 02:23:568#include <vector>
9
[email protected]3127f6632012-03-17 00:14:0610#include "base/basictypes.h"
11#include "base/memory/scoped_ptr.h"
[email protected]f3652ff92013-06-11 13:54:3112#include "base/strings/string16.h"
[email protected]3127f6632012-03-17 00:14:0613#include "skia/ext/platform_canvas.h"
[email protected]3699ae5a2012-12-05 01:00:5414#include "skia/ext/refptr.h"
[email protected]35d53342012-05-16 21:30:1215#include "ui/gfx/image/image_skia.h"
[email protected]3127f6632012-03-17 00:14:0616#include "ui/gfx/native_widget_types.h"
[email protected]25650682012-05-29 23:09:1917#include "ui/gfx/shadow_value.h"
[email protected]3127f6632012-03-17 00:14:0618
[email protected]267c03d2011-02-02 23:03:0719namespace gfx {
20
[email protected]3127f6632012-03-17 00:14:0621class Rect;
22class Font;
[email protected]ad8d9d52013-08-22 07:20:0523class FontList;
[email protected]3127f6632012-03-17 00:14:0624class Point;
25class Size;
[email protected]0f0453e2012-10-14 18:15:3526class Transform;
[email protected]3127f6632012-03-17 00:14:0627
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.
[email protected]4ffa7892013-09-27 16:56:0641class GFX_EXPORT Canvas {
[email protected]267c03d2011-02-02 23:03:0742 public:
[email protected]3127f6632012-03-17 00:14:0643 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]ff1af582012-11-06 03:34:1253 TEXT_ALIGN_RIGHT = 1 << 2,
[email protected]3127f6632012-03-17 00:14:0654
55 // Specifies the text consists of multiple lines.
[email protected]ff1af582012-11-06 03:34:1256 MULTI_LINE = 1 << 3,
[email protected]3127f6632012-03-17 00:14:0657
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]ff1af582012-11-06 03:34:1263 SHOW_PREFIX = 1 << 4,
64 HIDE_PREFIX = 1 << 5,
[email protected]3127f6632012-03-17 00:14:0665
66 // Prevent ellipsizing
[email protected]ff1af582012-11-06 03:34:1267 NO_ELLIPSIS = 1 << 6,
[email protected]3127f6632012-03-17 00:14:0668
69 // Specifies if words can be split by new lines.
70 // This only works with MULTI_LINE.
[email protected]ff1af582012-11-06 03:34:1271 CHARACTER_BREAK = 1 << 7,
[email protected]3127f6632012-03-17 00:14:0672
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]ff1af582012-11-06 03:34:1281 FORCE_RTL_DIRECTIONALITY = 1 << 8,
[email protected]3127f6632012-03-17 00:14:0682
83 // Similar to FORCE_RTL_DIRECTIONALITY, but left-to-right.
84 // See FORCE_RTL_DIRECTIONALITY for details.
[email protected]ff1af582012-11-06 03:34:1285 FORCE_LTR_DIRECTIONALITY = 1 << 9,
[email protected]3127f6632012-03-17 00:14:0686
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]ff1af582012-11-06 03:34:1290 NO_SUBPIXEL_RENDERING = 1 << 10,
[email protected]3127f6632012-03-17 00:14:0691 };
92
[email protected]50b66262013-09-24 03:25:4893 // Creates an empty canvas with image_scale of 1x.
[email protected]94a0d2582012-03-09 00:30:5994 Canvas();
[email protected]267c03d2011-02-02 23:03:0795
[email protected]50b66262013-09-24 03:25:4896 // Creates canvas with provided DIP |size| and |image_scale|.
[email protected]3b2591a2012-06-27 17:58:4197 // If this canvas is not opaque, it's explicitly cleared to transparent before
98 // being returned.
[email protected]50b66262013-09-24 03:25:4899 Canvas(const Size& size, float image_scale, bool is_opaque);
[email protected]3127f6632012-03-17 00:14:06100
[email protected]50b66262013-09-24 03:25:48101 // 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]ad8d9d52013-08-22 07:20:05103 Canvas(const ImageSkiaRep& image_rep, bool is_opaque);
[email protected]3b2591a2012-06-27 17:58:41104
[email protected]57047da2012-07-23 20:55:08105 virtual ~Canvas();
[email protected]f7fc60212012-07-23 20:27:08106
[email protected]50b66262013-09-24 03:25:48107 // 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]de13f352012-07-24 16:51:16109 // so no additional scaling is applied.
110 static Canvas* CreateCanvasWithoutScaling(SkCanvas* sk_canvas,
[email protected]50b66262013-09-24 03:25:48111 float image_scale);
[email protected]de13f352012-07-24 16:51:16112
[email protected]50b66262013-09-24 03:25:48113 // Recreates the backing platform canvas with DIP |size| and |image_scale_|.
[email protected]3b2591a2012-06-27 17:58:41114 // 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]50b66262013-09-24 03:25:48117 // TODO(pkotwicz): Push the image_scale into skia::PlatformCanvas such that
[email protected]3b2591a2012-06-27 17:58:41118 // this method can be private.
[email protected]ad8d9d52013-08-22 07:20:05119 void RecreateBackingCanvas(const Size& size,
[email protected]50b66262013-09-24 03:25:48120 float image_scale,
[email protected]3b2591a2012-06-27 17:58:41121 bool is_opaque);
122
[email protected]ad8d9d52013-08-22 07:20:05123 // Compute the size required to draw some text with the provided fonts.
[email protected]3127f6632012-03-17 00:14:06124 // 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]561f7482013-04-19 03:39:58126 // 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]031ffed2013-06-09 03:32:36128 static void SizeStringInt(const base::string16& text,
[email protected]ad8d9d52013-08-22 07:20:05129 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]561f7482013-04-19 03:39:58139 int line_height,
[email protected]3127f6632012-03-17 00:14:06140 int flags);
141
[email protected]8ad3c5a2013-10-10 09:57:19142 // This is same as SizeStringInt except that fractional size is returned.
143 // See comment in GetStringWidthF for its usage.
144 static void SizeStringFloat(const base::string16& text,
145 const FontList& font_list,
146 float* width,
147 float* height,
148 int line_height,
149 int flags);
150
[email protected]3127f6632012-03-17 00:14:06151 // Returns the number of horizontal pixels needed to display the specified
[email protected]ad8d9d52013-08-22 07:20:05152 // |text| with |font_list|.
153 static int GetStringWidth(const base::string16& text,
154 const FontList& font_list);
155 // Obsolete version. Use the above version which takes FontList.
156 static int GetStringWidth(const base::string16& text, const Font& font);
[email protected]3127f6632012-03-17 00:14:06157
[email protected]8ad3c5a2013-10-10 09:57:19158 // This is same as GetStringWidth except that fractional width is returned.
159 // Use this method for the scenario that multiple string widths need to be
160 // summed up. This is because GetStringWidth returns the ceiled width and
161 // adding multiple ceiled widths could cause more precision loss for certain
162 // platform like Mac where the fractioal width is used.
163 static float GetStringWidthF(const base::string16& text,
164 const FontList& font_list);
165
[email protected]3127f6632012-03-17 00:14:06166 // Returns the default text alignment to be used when drawing text on a
[email protected]ad8d9d52013-08-22 07:20:05167 // Canvas based on the directionality of the system locale language.
168 // This function is used by Canvas::DrawStringInt when the text alignment
[email protected]3127f6632012-03-17 00:14:06169 // is not specified.
170 //
[email protected]ad8d9d52013-08-22 07:20:05171 // This function returns either Canvas::TEXT_ALIGN_LEFT or
172 // Canvas::TEXT_ALIGN_RIGHT.
[email protected]3127f6632012-03-17 00:14:06173 static int DefaultCanvasTextAlignment();
174
175 // Draws text with a 1-pixel halo around it of the given color.
[email protected]ad8d9d52013-08-22 07:20:05176 // On Windows, it allows ClearType to be drawn to an otherwise transparent
[email protected]3127f6632012-03-17 00:14:06177 // bitmap for drag images. Drag images have only 1-bit of transparency, so
178 // we don't do any fancy blurring.
179 // On Linux, text with halo is created by stroking it with 2px |halo_color|
180 // then filling it with |text_color|.
181 // On Mac, NOTIMPLEMENTED.
182 // TODO(dhollowa): Skia-native implementation is underway. Cut over to
183 // that when ready. http::/crbug.com/109946
[email protected]ad8d9d52013-08-22 07:20:05184 void DrawStringRectWithHalo(const base::string16& text,
185 const FontList& font_list,
186 SkColor text_color,
187 SkColor halo_color,
188 const Rect& display_rect,
189 int flags);
190 // Obsolete version. Use the above version which takes FontList.
[email protected]031ffed2013-06-09 03:32:36191 void DrawStringWithHalo(const base::string16& text,
[email protected]ad8d9d52013-08-22 07:20:05192 const Font& font,
[email protected]1b81ff22012-03-24 22:04:42193 SkColor text_color,
194 SkColor halo_color,
[email protected]ad8d9d52013-08-22 07:20:05195 int x,
196 int y,
197 int w,
198 int h,
[email protected]3127f6632012-03-17 00:14:06199 int flags);
200
[email protected]3b2591a2012-06-27 17:58:41201 // Extracts an ImageSkiaRep from the contents of this canvas.
[email protected]ad8d9d52013-08-22 07:20:05202 ImageSkiaRep ExtractImageRep() const;
[email protected]3b2591a2012-06-27 17:58:41203
[email protected]3127f6632012-03-17 00:14:06204 // Draws a dashed rectangle of the specified color.
[email protected]ad8d9d52013-08-22 07:20:05205 void DrawDashedRect(const Rect& rect, SkColor color);
[email protected]3127f6632012-03-17 00:14:06206
207 // Saves a copy of the drawing state onto a stack, operating on this copy
208 // until a balanced call to Restore() is made.
209 void Save();
210
211 // As with Save(), except draws to a layer that is blended with the canvas
212 // at the specified alpha once Restore() is called.
213 // |layer_bounds| are the bounds of the layer relative to the current
214 // transform.
215 void SaveLayerAlpha(uint8 alpha);
[email protected]ad8d9d52013-08-22 07:20:05216 void SaveLayerAlpha(uint8 alpha, const Rect& layer_bounds);
[email protected]3127f6632012-03-17 00:14:06217
218 // Restores the drawing state after a call to Save*(). It is an error to
219 // call Restore() more times than Save*().
[email protected]964c429d2012-10-25 15:28:45220 void Restore();
[email protected]3127f6632012-03-17 00:14:06221
[email protected]ffdd1fd2012-04-09 23:24:41222 // Adds |rect| to the current clip. Returns true if the resulting clip is
223 // non-empty.
[email protected]ad8d9d52013-08-22 07:20:05224 bool ClipRect(const Rect& rect);
[email protected]3127f6632012-03-17 00:14:06225
[email protected]ffdd1fd2012-04-09 23:24:41226 // Adds |path| to the current clip. Returns true if the resulting clip is
227 // non-empty.
228 bool ClipPath(const SkPath& path);
229
230 // Returns the bounds of the current clip (in local coordinates) in the
231 // |bounds| parameter, and returns true if it is non empty.
[email protected]ad8d9d52013-08-22 07:20:05232 bool GetClipBounds(Rect* bounds);
[email protected]ffdd1fd2012-04-09 23:24:41233
[email protected]ad8d9d52013-08-22 07:20:05234 void Translate(const Vector2d& offset);
[email protected]3127f6632012-03-17 00:14:06235
236 void Scale(int x_scale, int y_scale);
237
[email protected]ffdd1fd2012-04-09 23:24:41238 // Fills the entire canvas' bitmap (restricted to current clip) with
239 // specified |color| using a transfer mode of SkXfermode::kSrcOver_Mode.
240 void DrawColor(SkColor color);
241
242 // Fills the entire canvas' bitmap (restricted to current clip) with
243 // specified |color| and |mode|.
244 void DrawColor(SkColor color, SkXfermode::Mode mode);
245
[email protected]3127f6632012-03-17 00:14:06246 // Fills |rect| with |color| using a transfer mode of
247 // SkXfermode::kSrcOver_Mode.
[email protected]ad8d9d52013-08-22 07:20:05248 void FillRect(const Rect& rect, SkColor color);
[email protected]3127f6632012-03-17 00:14:06249
250 // Fills |rect| with the specified |color| and |mode|.
[email protected]ad8d9d52013-08-22 07:20:05251 void FillRect(const Rect& rect, SkColor color, SkXfermode::Mode mode);
[email protected]3127f6632012-03-17 00:14:06252
[email protected]3127f6632012-03-17 00:14:06253 // Draws a single pixel rect in the specified region with the specified
254 // color, using a transfer mode of SkXfermode::kSrcOver_Mode.
255 //
256 // NOTE: if you need a single pixel line, use DrawLine.
[email protected]ad8d9d52013-08-22 07:20:05257 void DrawRect(const Rect& rect, SkColor color);
[email protected]3127f6632012-03-17 00:14:06258
259 // Draws a single pixel rect in the specified region with the specified
260 // color and transfer mode.
261 //
262 // NOTE: if you need a single pixel line, use DrawLine.
[email protected]ad8d9d52013-08-22 07:20:05263 void DrawRect(const Rect& rect, SkColor color, SkXfermode::Mode mode);
[email protected]3127f6632012-03-17 00:14:06264
[email protected]ffdd1fd2012-04-09 23:24:41265 // Draws the given rectangle with the given |paint| parameters.
[email protected]ad8d9d52013-08-22 07:20:05266 void DrawRect(const Rect& rect, const SkPaint& paint);
[email protected]3127f6632012-03-17 00:14:06267
[email protected]ffdd1fd2012-04-09 23:24:41268 // Draw the given point with the given |paint| parameters.
[email protected]ad8d9d52013-08-22 07:20:05269 void DrawPoint(const Point& p, const SkPaint& paint);
[email protected]ffdd1fd2012-04-09 23:24:41270
[email protected]3127f6632012-03-17 00:14:06271 // Draws a single pixel line with the specified color.
[email protected]ad8d9d52013-08-22 07:20:05272 void DrawLine(const Point& p1, const Point& p2, SkColor color);
[email protected]3127f6632012-03-17 00:14:06273
[email protected]ffdd1fd2012-04-09 23:24:41274 // Draws a line with the given |paint| parameters.
[email protected]ad8d9d52013-08-22 07:20:05275 void DrawLine(const Point& p1, const Point& p2, const SkPaint& paint);
[email protected]ffdd1fd2012-04-09 23:24:41276
277 // Draws a circle with the given |paint| parameters.
[email protected]ad8d9d52013-08-22 07:20:05278 void DrawCircle(const Point& center_point,
[email protected]ffdd1fd2012-04-09 23:24:41279 int radius,
280 const SkPaint& paint);
281
282 // Draws the given rectangle with rounded corners of |radius| using the
283 // given |paint| parameters.
[email protected]ad8d9d52013-08-22 07:20:05284 void DrawRoundRect(const Rect& rect, int radius, const SkPaint& paint);
[email protected]ffdd1fd2012-04-09 23:24:41285
286 // Draws the given path using the given |paint| parameters.
287 void DrawPath(const SkPath& path, const SkPaint& paint);
288
[email protected]35d53342012-05-16 21:30:12289 // Draws an image with the origin at the specified location. The upper left
[email protected]3127f6632012-03-17 00:14:06290 // corner of the bitmap is rendered at the specified location.
[email protected]35d53342012-05-16 21:30:12291 // Parameters are specified relative to current canvas scale not in pixels.
[email protected]8232da6b2012-07-02 19:41:15292 // Thus, x is 2 pixels if canvas scale = 2 & |x| = 1.
[email protected]ad8d9d52013-08-22 07:20:05293 void DrawImageInt(const ImageSkia&, int x, int y);
[email protected]3127f6632012-03-17 00:14:06294
[email protected]2885cb32012-10-03 19:41:11295 // Helper for DrawImageInt(..., paint) that constructs a temporary paint and
296 // calls paint.setAlpha(alpha).
[email protected]ad8d9d52013-08-22 07:20:05297 void DrawImageInt(const ImageSkia&, int x, int y, uint8 alpha);
[email protected]2885cb32012-10-03 19:41:11298
[email protected]35d53342012-05-16 21:30:12299 // Draws an image with the origin at the specified location, using the
[email protected]3127f6632012-03-17 00:14:06300 // specified paint. The upper left corner of the bitmap is rendered at the
301 // specified location.
[email protected]35d53342012-05-16 21:30:12302 // 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]ad8d9d52013-08-22 07:20:05304 void DrawImageInt(const ImageSkia& image,
305 int x,
306 int y,
[email protected]243cac7d2012-06-07 13:29:20307 const SkPaint& paint);
[email protected]3127f6632012-03-17 00:14:06308
[email protected]35d53342012-05-16 21:30:12309 // Draws a portion of an image in the specified location. The src parameters
[email protected]3127f6632012-03-17 00:14:06310 // correspond to the region of the bitmap to draw in the region defined
311 // by the dest coordinates.
312 //
313 // If the width or height of the source differs from that of the destination,
[email protected]35d53342012-05-16 21:30:12314 // the image will be scaled. When scaling down, a mipmap will be generated.
315 // Set |filter| to use filtering for images, otherwise the nearest-neighbor
316 // algorithm is used for resampling.
[email protected]3127f6632012-03-17 00:14:06317 //
318 // An optional custom SkPaint can be provided.
[email protected]35d53342012-05-16 21:30:12319 // Parameters are specified relative to current canvas scale not in pixels.
320 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1.
[email protected]ad8d9d52013-08-22 07:20:05321 void DrawImageInt(const ImageSkia& image,
322 int src_x,
323 int src_y,
324 int src_w,
325 int src_h,
326 int dest_x,
327 int dest_y,
328 int dest_w,
329 int dest_h,
[email protected]243cac7d2012-06-07 13:29:20330 bool filter);
[email protected]ad8d9d52013-08-22 07:20:05331 void DrawImageInt(const ImageSkia& image,
332 int src_x,
333 int src_y,
334 int src_w,
335 int src_h,
336 int dest_x,
337 int dest_y,
338 int dest_w,
339 int dest_h,
[email protected]243cac7d2012-06-07 13:29:20340 bool filter,
341 const SkPaint& paint);
[email protected]3127f6632012-03-17 00:14:06342
[email protected]8232da6b2012-07-02 19:41:15343 // Draws an |image| with the top left corner at |x| and |y|, clipped to
344 // |path|.
345 // Parameters are specified relative to current canvas scale not in pixels.
346 // Thus, x is 2 pixels if canvas scale = 2 & |x| = 1.
[email protected]ad8d9d52013-08-22 07:20:05347 void DrawImageInPath(const ImageSkia& image,
[email protected]8232da6b2012-07-02 19:41:15348 int x,
349 int y,
350 const SkPath& path,
351 const SkPaint& paint);
352
[email protected]ad8d9d52013-08-22 07:20:05353 // Draws text with the specified color, fonts and location. The text is
[email protected]3127f6632012-03-17 00:14:06354 // aligned to the left, vertically centered, clipped to the region. If the
355 // text is too big, it is truncated and '...' is added to the end.
[email protected]ad8d9d52013-08-22 07:20:05356 void DrawStringRect(const base::string16& text,
357 const FontList& font_list,
358 SkColor color,
359 const Rect& display_rect);
360 // Obsolete versions. Use the above versions which take FontList.
[email protected]031ffed2013-06-09 03:32:36361 void DrawStringInt(const base::string16& text,
[email protected]ad8d9d52013-08-22 07:20:05362 const Font& font,
[email protected]1b81ff22012-03-24 22:04:42363 SkColor color,
[email protected]ad8d9d52013-08-22 07:20:05364 int x,
365 int y,
366 int w,
367 int h);
[email protected]031ffed2013-06-09 03:32:36368 void DrawStringInt(const base::string16& text,
[email protected]ad8d9d52013-08-22 07:20:05369 const Font& font,
[email protected]1b81ff22012-03-24 22:04:42370 SkColor color,
[email protected]ad8d9d52013-08-22 07:20:05371 const Rect& display_rect);
[email protected]3127f6632012-03-17 00:14:06372
[email protected]ad8d9d52013-08-22 07:20:05373 // Draws text with the specified color, fonts and location. The last argument
[email protected]3127f6632012-03-17 00:14:06374 // specifies flags for how the text should be rendered. It can be one of
375 // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT.
[email protected]ad8d9d52013-08-22 07:20:05376 void DrawStringRectWithFlags(const base::string16& text,
377 const FontList& font_list,
378 SkColor color,
379 const Rect& display_rect,
380 int flags);
381 // Obsolete version. Use the above version which takes FontList.
[email protected]031ffed2013-06-09 03:32:36382 void DrawStringInt(const base::string16& text,
[email protected]ad8d9d52013-08-22 07:20:05383 const Font& font,
[email protected]1b81ff22012-03-24 22:04:42384 SkColor color,
[email protected]ad8d9d52013-08-22 07:20:05385 int x,
386 int y,
387 int w,
388 int h,
[email protected]3127f6632012-03-17 00:14:06389 int flags);
390
[email protected]0834e1b12012-04-11 02:23:56391 // Similar to above DrawStringInt method but with text shadows support.
[email protected]561f7482013-04-19 03:39:58392 // Currently it's only implemented for canvas skia. Specifying a 0 line_height
393 // will cause the default height to be used.
[email protected]ad8d9d52013-08-22 07:20:05394 void DrawStringRectWithShadows(const base::string16& text,
395 const FontList& font_list,
396 SkColor color,
397 const Rect& text_bounds,
398 int line_height,
399 int flags,
400 const ShadowValues& shadows);
401 // Obsolete version. Use the above version which takes FontList.
[email protected]031ffed2013-06-09 03:32:36402 void DrawStringWithShadows(const base::string16& text,
[email protected]ad8d9d52013-08-22 07:20:05403 const Font& font,
[email protected]0834e1b12012-04-11 02:23:56404 SkColor color,
[email protected]ad8d9d52013-08-22 07:20:05405 const Rect& text_bounds,
[email protected]561f7482013-04-19 03:39:58406 int line_height,
[email protected]0834e1b12012-04-11 02:23:56407 int flags,
[email protected]25650682012-05-29 23:09:19408 const ShadowValues& shadows);
[email protected]0834e1b12012-04-11 02:23:56409
[email protected]3127f6632012-03-17 00:14:06410 // Draws a dotted gray rectangle used for focus purposes.
[email protected]ad8d9d52013-08-22 07:20:05411 void DrawFocusRect(const Rect& rect);
[email protected]3127f6632012-03-17 00:14:06412
413 // Tiles the image in the specified region.
[email protected]35d53342012-05-16 21:30:12414 // Parameters are specified relative to current canvas scale not in pixels.
415 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1.
[email protected]ad8d9d52013-08-22 07:20:05416 void TileImageInt(const ImageSkia& image,
417 int x,
418 int y,
419 int w,
420 int h);
421 void TileImageInt(const ImageSkia& image,
422 int src_x,
423 int src_y,
424 int dest_x,
425 int dest_y,
426 int w,
427 int h);
428 void TileImageInt(const ImageSkia& image,
429 int src_x,
430 int src_y,
431 float tile_scale_x,
432 float tile_scale_y,
433 int dest_x,
434 int dest_y,
435 int w,
436 int h);
[email protected]3127f6632012-03-17 00:14:06437
438 // Returns a native drawing context for platform specific drawing routines to
439 // use. Must be balanced by a call to EndPlatformPaint().
440 NativeDrawingContext BeginPlatformPaint();
441
442 // Signifies the end of platform drawing using the native drawing context
443 // returned by BeginPlatformPaint().
444 void EndPlatformPaint();
445
446 // Apply transformation on the canvas.
[email protected]ad8d9d52013-08-22 07:20:05447 void Transform(const Transform& transform);
[email protected]3127f6632012-03-17 00:14:06448
[email protected]3127f6632012-03-17 00:14:06449 // Draws the given string with the beginning and/or the end using a fade
450 // gradient. When truncating the head
451 // |desired_characters_to_truncate_from_head| specifies the maximum number of
452 // characters that can be truncated.
[email protected]ad8d9d52013-08-22 07:20:05453 void DrawFadeTruncatingStringRect(
454 const base::string16& text,
455 TruncateFadeMode truncate_mode,
456 size_t desired_characters_to_truncate_from_head,
457 const FontList& font_list,
458 SkColor color,
459 const Rect& display_rect);
460 // Obsolete version. Use the above version which takes FontList.
[email protected]3127f6632012-03-17 00:14:06461 void DrawFadeTruncatingString(
[email protected]031ffed2013-06-09 03:32:36462 const base::string16& text,
[email protected]3127f6632012-03-17 00:14:06463 TruncateFadeMode truncate_mode,
464 size_t desired_characters_to_truncate_from_head,
[email protected]ad8d9d52013-08-22 07:20:05465 const Font& font,
[email protected]1b81ff22012-03-24 22:04:42466 SkColor color,
[email protected]ad8d9d52013-08-22 07:20:05467 const Rect& display_rect);
[email protected]3127f6632012-03-17 00:14:06468
469 skia::PlatformCanvas* platform_canvas() const { return owned_canvas_.get(); }
470 SkCanvas* sk_canvas() const { return canvas_; }
[email protected]50b66262013-09-24 03:25:48471 float image_scale() const { return image_scale_; }
[email protected]3127f6632012-03-17 00:14:06472
473 private:
[email protected]50b66262013-09-24 03:25:48474 Canvas(SkCanvas* canvas, float image_scale);
[email protected]de13f352012-07-24 16:51:16475
[email protected]3127f6632012-03-17 00:14:06476 // Test whether the provided rectangle intersects the current clip rect.
477 bool IntersectsClipRectInt(int x, int y, int w, int h);
[email protected]ad8d9d52013-08-22 07:20:05478 bool IntersectsClipRect(const Rect& rect);
[email protected]3127f6632012-03-17 00:14:06479
[email protected]50b66262013-09-24 03:25:48480 // Returns the image rep which best matches the canvas |image_scale_|.
[email protected]8232da6b2012-07-02 19:41:15481 // Returns a null image rep if |image| contains no image reps.
482 // Builds mip map for returned image rep if necessary.
[email protected]35d53342012-05-16 21:30:12483 //
484 // An optional additional user defined scale can be provided.
[email protected]ad8d9d52013-08-22 07:20:05485 const ImageSkiaRep& GetImageRepToPaint(const ImageSkia& image) const;
486 const ImageSkiaRep& GetImageRepToPaint(
487 const ImageSkia& image,
[email protected]8232da6b2012-07-02 19:41:15488 float user_defined_scale_factor_x,
[email protected]ad8d9d52013-08-22 07:20:05489 float user_defined_scale_factor_y) const;
[email protected]35d53342012-05-16 21:30:12490
[email protected]3b2591a2012-06-27 17:58:41491 // The device scale factor at which drawing on this canvas occurs.
492 // An additional scale can be applied via Canvas::Scale(). However,
[email protected]50b66262013-09-24 03:25:48493 // Canvas::Scale() does not affect |image_scale_|.
494 float image_scale_;
[email protected]3b2591a2012-06-27 17:58:41495
[email protected]3699ae5a2012-12-05 01:00:54496 skia::RefPtr<skia::PlatformCanvas> owned_canvas_;
[email protected]de13f352012-07-24 16:51:16497 SkCanvas* canvas_;
498
[email protected]3127f6632012-03-17 00:14:06499 DISALLOW_COPY_AND_ASSIGN(Canvas);
[email protected]267c03d2011-02-02 23:03:07500};
501
[email protected]7fe28392011-10-27 00:16:05502} // namespace gfx
[email protected]267c03d2011-02-02 23:03:07503
504#endif // UI_GFX_CANVAS_H_