1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef UI_GFX_CANVAS_H_
6 #define UI_GFX_CANVAS_H_
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string16.h"
13 #include "skia/ext/platform_canvas.h"
14 #include "skia/ext/refptr.h"
15 #include "ui/gfx/image/image_skia.h"
16 #include "ui/gfx/native_widget_types.h"
17 #include "ui/gfx/shadow_value.h"
28 // Canvas is a SkCanvas wrapper that provides a number of methods for
29 // common operations used throughout an application built using ui/gfx.
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()|.
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
41 class GFX_EXPORT Canvas {
43 enum TruncateFadeMode {
48 // Specifies the alignment for text rendered with the DrawStringRect method.
50 TEXT_ALIGN_LEFT = 1 << 0,
51 TEXT_ALIGN_CENTER = 1 << 1,
52 TEXT_ALIGN_RIGHT = 1 << 2,
54 // Specifies the text consists of multiple lines.
57 // By default DrawStringRect does not process the prefix ('&') character
58 // specially. That is, the string "&foo" is rendered as "&foo". When
59 // rendering text from a resource that uses the prefix character for
60 // mnemonics, the prefix should be processed and can be rendered as an
61 // underline (SHOW_PREFIX), or not rendered at all (HIDE_PREFIX).
65 // Prevent ellipsizing
68 // Specifies if words can be split by new lines.
69 // This only works with MULTI_LINE.
70 CHARACTER_BREAK = 1 << 7,
72 // Instructs DrawStringRect() to render the text using RTL directionality.
73 // In most cases, passing this flag is not necessary because information
74 // about the text directionality is going to be embedded within the string
75 // in the form of special Unicode characters. However, we don't insert
76 // directionality characters into strings if the locale is LTR because some
77 // platforms (for example, an English Windows XP with no RTL fonts
78 // installed) don't support these characters. Thus, this flag should be
79 // used to render text using RTL directionality when the locale is LTR.
80 FORCE_RTL_DIRECTIONALITY = 1 << 8,
82 // Similar to FORCE_RTL_DIRECTIONALITY, but left-to-right.
83 // See FORCE_RTL_DIRECTIONALITY for details.
84 FORCE_LTR_DIRECTIONALITY = 1 << 9,
86 // Instructs DrawStringRect() to not use subpixel rendering. This is useful
87 // when rendering text onto a fully- or partially-transparent background
88 // that will later be blended with another image.
89 NO_SUBPIXEL_RENDERING = 1 << 10,
92 // Creates an empty canvas with image_scale of 1x.
95 // Creates canvas with provided DIP |size| and |image_scale|.
96 // If this canvas is not opaque, it's explicitly cleared to transparent before
98 Canvas(const Size& size, float image_scale, bool is_opaque);
100 // Constructs a canvas with the size and the image_scale of the provided
101 // |image_rep|, and draws the |image_rep| into it.
102 Canvas(const ImageSkiaRep& image_rep, bool is_opaque);
106 // Creates a Canvas backed by an |sk_canvas| with |image_scale_|.
107 // |sk_canvas| is assumed to be already scaled based on |image_scale|
108 // so no additional scaling is applied.
109 static Canvas* CreateCanvasWithoutScaling(SkCanvas* sk_canvas,
112 // Recreates the backing platform canvas with DIP |size| and |image_scale_|.
113 // If the canvas is not opaque, it is explicitly cleared.
114 // This method is public so that canvas_skia_paint can recreate the platform
115 // canvas after having initialized the canvas.
116 // TODO(pkotwicz): Push the image_scale into skia::PlatformCanvas such that
117 // this method can be private.
118 void RecreateBackingCanvas(const Size& size,
122 // Compute the size required to draw some text with the provided fonts.
123 // Attempts to fit the text with the provided width and height. Increases
124 // height and then width as needed to make the text fit. This method
125 // supports multiple lines. On Skia only a line_height can be specified and
126 // specifying a 0 value for it will cause the default height to be used.
127 static void SizeStringInt(const base::string16& text,
128 const FontList& font_list,
134 // This is same as SizeStringInt except that fractional size is returned.
135 // See comment in GetStringWidthF for its usage.
136 static void SizeStringFloat(const base::string16& text,
137 const FontList& font_list,
143 // Returns the number of horizontal pixels needed to display the specified
144 // |text| with |font_list|.
145 static int GetStringWidth(const base::string16& text,
146 const FontList& font_list);
147 // Obsolete version. Use the above version which takes FontList.
148 static int GetStringWidth(const base::string16& text, const Font& font);
150 // This is same as GetStringWidth except that fractional width is returned.
151 // Use this method for the scenario that multiple string widths need to be
152 // summed up. This is because GetStringWidth returns the ceiled width and
153 // adding multiple ceiled widths could cause more precision loss for certain
154 // platform like Mac where the fractioal width is used.
155 static float GetStringWidthF(const base::string16& text,
156 const FontList& font_list);
158 // Returns the default text alignment to be used when drawing text on a
159 // Canvas based on the directionality of the system locale language.
160 // This function is used by Canvas::DrawStringRect when the text alignment
163 // This function returns either Canvas::TEXT_ALIGN_LEFT or
164 // Canvas::TEXT_ALIGN_RIGHT.
165 static int DefaultCanvasTextAlignment();
167 // Draws text with a 1-pixel halo around it of the given color.
168 // On Windows, it allows ClearType to be drawn to an otherwise transparent
169 // bitmap for drag images. Drag images have only 1-bit of transparency, so
170 // we don't do any fancy blurring.
171 // On Linux, text with halo is created by stroking it with 2px |halo_color|
172 // then filling it with |text_color|.
173 // On Mac, NOTIMPLEMENTED.
174 // TODO(dhollowa): Skia-native implementation is underway. Cut over to
175 // that when ready. http::/crbug.com/109946
176 void DrawStringRectWithHalo(const base::string16& text,
177 const FontList& font_list,
180 const Rect& display_rect,
183 // Extracts an ImageSkiaRep from the contents of this canvas.
184 ImageSkiaRep ExtractImageRep() const;
186 // Draws a dashed rectangle of the specified color.
187 void DrawDashedRect(const Rect& rect, SkColor color);
189 // Saves a copy of the drawing state onto a stack, operating on this copy
190 // until a balanced call to Restore() is made.
193 // As with Save(), except draws to a layer that is blended with the canvas
194 // at the specified alpha once Restore() is called.
195 // |layer_bounds| are the bounds of the layer relative to the current
197 void SaveLayerAlpha(uint8 alpha);
198 void SaveLayerAlpha(uint8 alpha, const Rect& layer_bounds);
200 // Restores the drawing state after a call to Save*(). It is an error to
201 // call Restore() more times than Save*().
204 // Adds |rect| to the current clip. Returns true if the resulting clip is
206 bool ClipRect(const Rect& rect);
208 // Adds |path| to the current clip. Returns true if the resulting clip is
210 bool ClipPath(const SkPath& path);
212 // Returns the bounds of the current clip (in local coordinates) in the
213 // |bounds| parameter, and returns true if it is non empty.
214 bool GetClipBounds(Rect* bounds);
216 void Translate(const Vector2d& offset);
218 void Scale(int x_scale, int y_scale);
220 // Fills the entire canvas' bitmap (restricted to current clip) with
221 // specified |color| using a transfer mode of SkXfermode::kSrcOver_Mode.
222 void DrawColor(SkColor color);
224 // Fills the entire canvas' bitmap (restricted to current clip) with
225 // specified |color| and |mode|.
226 void DrawColor(SkColor color, SkXfermode::Mode mode);
228 // Fills |rect| with |color| using a transfer mode of
229 // SkXfermode::kSrcOver_Mode.
230 void FillRect(const Rect& rect, SkColor color);
232 // Fills |rect| with the specified |color| and |mode|.
233 void FillRect(const Rect& rect, SkColor color, SkXfermode::Mode mode);
235 // Draws a single pixel rect in the specified region with the specified
236 // color, using a transfer mode of SkXfermode::kSrcOver_Mode.
238 // NOTE: if you need a single pixel line, use DrawLine.
239 void DrawRect(const Rect& rect, SkColor color);
241 // Draws a single pixel rect in the specified region with the specified
242 // color and transfer mode.
244 // NOTE: if you need a single pixel line, use DrawLine.
245 void DrawRect(const Rect& rect, SkColor color, SkXfermode::Mode mode);
247 // Draws the given rectangle with the given |paint| parameters.
248 void DrawRect(const Rect& rect, const SkPaint& paint);
250 // Draw the given point with the given |paint| parameters.
251 void DrawPoint(const Point& p, const SkPaint& paint);
253 // Draws a single pixel line with the specified color.
254 void DrawLine(const Point& p1, const Point& p2, SkColor color);
256 // Draws a line with the given |paint| parameters.
257 void DrawLine(const Point& p1, const Point& p2, const SkPaint& paint);
259 // Draws a circle with the given |paint| parameters.
260 void DrawCircle(const Point& center_point,
262 const SkPaint& paint);
264 // Draws the given rectangle with rounded corners of |radius| using the
265 // given |paint| parameters.
266 void DrawRoundRect(const Rect& rect, int radius, const SkPaint& paint);
268 // Draws the given path using the given |paint| parameters.
269 void DrawPath(const SkPath& path, const SkPaint& paint);
271 // Draws an image with the origin at the specified location. The upper left
272 // corner of the bitmap is rendered at the specified location.
273 // Parameters are specified relative to current canvas scale not in pixels.
274 // Thus, x is 2 pixels if canvas scale = 2 & |x| = 1.
275 void DrawImageInt(const ImageSkia&, int x, int y);
277 // Helper for DrawImageInt(..., paint) that constructs a temporary paint and
278 // calls paint.setAlpha(alpha).
279 void DrawImageInt(const ImageSkia&, int x, int y, uint8 alpha);
281 // Draws an image with the origin at the specified location, using the
282 // specified paint. The upper left corner of the bitmap is rendered at the
283 // specified location.
284 // Parameters are specified relative to current canvas scale not in pixels.
285 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1.
286 void DrawImageInt(const ImageSkia& image,
289 const SkPaint& paint);
291 // Draws a portion of an image in the specified location. The src parameters
292 // correspond to the region of the bitmap to draw in the region defined
293 // by the dest coordinates.
295 // If the width or height of the source differs from that of the destination,
296 // the image will be scaled. When scaling down, a mipmap will be generated.
297 // Set |filter| to use filtering for images, otherwise the nearest-neighbor
298 // algorithm is used for resampling.
300 // An optional custom SkPaint can be provided.
301 // Parameters are specified relative to current canvas scale not in pixels.
302 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1.
303 void DrawImageInt(const ImageSkia& image,
313 void DrawImageInt(const ImageSkia& image,
323 const SkPaint& paint);
325 // Draws an |image| with the top left corner at |x| and |y|, clipped to
327 // Parameters are specified relative to current canvas scale not in pixels.
328 // Thus, x is 2 pixels if canvas scale = 2 & |x| = 1.
329 void DrawImageInPath(const ImageSkia& image,
333 const SkPaint& paint);
335 // Draws text with the specified color, fonts and location. The text is
336 // aligned to the left, vertically centered, clipped to the region. If the
337 // text is too big, it is truncated and '...' is added to the end.
338 void DrawStringRect(const base::string16& text,
339 const FontList& font_list,
341 const Rect& display_rect);
343 // Draws text with the specified color, fonts and location. The last argument
344 // specifies flags for how the text should be rendered. It can be one of
345 // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT.
346 void DrawStringRectWithFlags(const base::string16& text,
347 const FontList& font_list,
349 const Rect& display_rect,
352 // Similar to above DrawStringRect method but with text shadows support.
353 // Currently it's only implemented for canvas skia. Specifying a 0 line_height
354 // will cause the default height to be used.
355 void DrawStringRectWithShadows(const base::string16& text,
356 const FontList& font_list,
358 const Rect& text_bounds,
361 const ShadowValues& shadows);
363 // Draws a dotted gray rectangle used for focus purposes.
364 void DrawFocusRect(const Rect& rect);
366 // Draws a |rect| in the specified region with the specified |color| with a
367 // with of one logical pixel which might be more device pixels.
368 void DrawSolidFocusRect(const Rect& rect, SkColor color);
370 // Tiles the image in the specified region.
371 // Parameters are specified relative to current canvas scale not in pixels.
372 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1.
373 void TileImageInt(const ImageSkia& image,
378 void TileImageInt(const ImageSkia& image,
385 void TileImageInt(const ImageSkia& image,
395 // Returns a native drawing context for platform specific drawing routines to
396 // use. Must be balanced by a call to EndPlatformPaint().
397 NativeDrawingContext BeginPlatformPaint();
399 // Signifies the end of platform drawing using the native drawing context
400 // returned by BeginPlatformPaint().
401 void EndPlatformPaint();
403 // Apply transformation on the canvas.
404 void Transform(const Transform& transform);
406 // Draws the given string with the beginning or the end using a fade gradient.
407 void DrawFadeTruncatingStringRect(
408 const base::string16& text,
409 TruncateFadeMode truncate_mode,
410 const FontList& font_list,
412 const Rect& display_rect);
413 void DrawFadeTruncatingStringRectWithFlags(
414 const base::string16& text,
415 TruncateFadeMode truncate_mode,
416 const FontList& font_list,
418 const Rect& display_rect,
421 skia::PlatformCanvas* platform_canvas() const { return owned_canvas_.get(); }
422 SkCanvas* sk_canvas() const { return canvas_; }
423 float image_scale() const { return image_scale_; }
426 Canvas(SkCanvas* canvas, float image_scale);
428 // Test whether the provided rectangle intersects the current clip rect.
429 bool IntersectsClipRectInt(int x, int y, int w, int h);
430 bool IntersectsClipRect(const Rect& rect);
432 // Returns the image rep which best matches the canvas |image_scale_|.
433 // Returns a null image rep if |image| contains no image reps.
434 // Builds mip map for returned image rep if necessary.
436 // An optional additional user defined scale can be provided.
437 const ImageSkiaRep& GetImageRepToPaint(const ImageSkia& image) const;
438 const ImageSkiaRep& GetImageRepToPaint(
439 const ImageSkia& image,
440 float user_defined_scale_factor_x,
441 float user_defined_scale_factor_y) const;
443 // The device scale factor at which drawing on this canvas occurs.
444 // An additional scale can be applied via Canvas::Scale(). However,
445 // Canvas::Scale() does not affect |image_scale_|.
448 skia::RefPtr<skia::PlatformCanvas> owned_canvas_;
451 DISALLOW_COPY_AND_ASSIGN(Canvas);
456 #endif // UI_GFX_CANVAS_H_