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"
18 #include "ui/gfx/text_constants.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
{
44 // Specifies the alignment for text rendered with the DrawStringRect method.
45 TEXT_ALIGN_LEFT
= 1 << 0,
46 TEXT_ALIGN_CENTER
= 1 << 1,
47 TEXT_ALIGN_RIGHT
= 1 << 2,
48 TEXT_ALIGN_TO_HEAD
= 1 << 3,
50 // Specifies the text consists of multiple lines.
53 // By default DrawStringRect does not process the prefix ('&') character
54 // specially. That is, the string "&foo" is rendered as "&foo". When
55 // rendering text from a resource that uses the prefix character for
56 // mnemonics, the prefix should be processed and can be rendered as an
57 // underline (SHOW_PREFIX), or not rendered at all (HIDE_PREFIX).
61 // Prevent ellipsizing
64 // Specifies if words can be split by new lines.
65 // This only works with MULTI_LINE.
66 CHARACTER_BREAK
= 1 << 8,
68 // Instructs DrawStringRect() to not use subpixel rendering. This is useful
69 // when rendering text onto a fully- or partially-transparent background
70 // that will later be blended with another image.
71 NO_SUBPIXEL_RENDERING
= 1 << 9,
74 // Creates an empty canvas with image_scale of 1x.
77 // Creates canvas with provided DIP |size| and |image_scale|.
78 // If this canvas is not opaque, it's explicitly cleared to transparent before
80 Canvas(const Size
& size
, float image_scale
, bool is_opaque
);
82 // Constructs a canvas with the size and the image_scale of the provided
83 // |image_rep|, and draws the |image_rep| into it.
84 Canvas(const ImageSkiaRep
& image_rep
, bool is_opaque
);
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|
88 // so no additional scaling is applied.
89 Canvas(SkCanvas
* sk_canvas
, float image_scale
);
93 // Recreates the backing platform canvas with DIP |size| and |image_scale_|.
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.
97 // TODO(pkotwicz): Push the image_scale into skia::PlatformCanvas such that
98 // this method can be private.
99 void RecreateBackingCanvas(const Size
& size
,
103 // Compute the size required to draw some text with the provided fonts.
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
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.
108 static void SizeStringInt(const base::string16
& text
,
109 const FontList
& font_list
,
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
,
124 // Returns the number of horizontal pixels needed to display the specified
125 // |text| with |font_list|.
126 static int GetStringWidth(const base::string16
& text
,
127 const FontList
& font_list
);
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
);
137 // Returns the default text alignment to be used when drawing text on a
138 // Canvas based on the directionality of the system locale language.
139 // This function is used by Canvas::DrawStringRect when the text alignment
142 // This function returns either Canvas::TEXT_ALIGN_LEFT or
143 // Canvas::TEXT_ALIGN_RIGHT.
144 static int DefaultCanvasTextAlignment();
146 // Draws text with a 1-pixel halo around it of the given color.
147 // On Windows, it allows ClearType to be drawn to an otherwise transparent
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
155 void DrawStringRectWithHalo(const base::string16
& text
,
156 const FontList
& font_list
,
159 const Rect
& display_rect
,
162 // Extracts an ImageSkiaRep from the contents of this canvas.
163 ImageSkiaRep
ExtractImageRep() const;
165 // Draws a dashed rectangle of the specified color.
166 void DrawDashedRect(const Rect
& rect
, SkColor color
);
168 // Saves a copy of the drawing state onto a stack, operating on this copy
169 // until a balanced call to Restore() is made.
172 // As with Save(), except draws to a layer that is blended with the canvas
173 // at the specified alpha once Restore() is called.
174 // |layer_bounds| are the bounds of the layer relative to the current
176 void SaveLayerAlpha(uint8 alpha
);
177 void SaveLayerAlpha(uint8 alpha
, const Rect
& layer_bounds
);
179 // Restores the drawing state after a call to Save*(). It is an error to
180 // call Restore() more times than Save*().
183 // Adds |rect| to the current clip.
184 void ClipRect(const Rect
& rect
);
186 // Adds |path| to the current clip. |do_anti_alias| is true if the clip
187 // should be antialiased.
188 void ClipPath(const SkPath
& path
, bool do_anti_alias
);
190 // Returns true if the current clip is empty.
191 bool IsClipEmpty() const;
193 // Returns the bounds of the current clip (in local coordinates) in the
194 // |bounds| parameter, and returns true if it is non empty.
195 bool GetClipBounds(Rect
* bounds
);
197 void Translate(const Vector2d
& offset
);
199 void Scale(int x_scale
, int y_scale
);
201 // Fills the entire canvas' bitmap (restricted to current clip) with
202 // specified |color| using a transfer mode of SkXfermode::kSrcOver_Mode.
203 void DrawColor(SkColor color
);
205 // Fills the entire canvas' bitmap (restricted to current clip) with
206 // specified |color| and |mode|.
207 void DrawColor(SkColor color
, SkXfermode::Mode mode
);
209 // Fills |rect| with |color| using a transfer mode of
210 // SkXfermode::kSrcOver_Mode.
211 void FillRect(const Rect
& rect
, SkColor color
);
213 // Fills |rect| with the specified |color| and |mode|.
214 void FillRect(const Rect
& rect
, SkColor color
, SkXfermode::Mode mode
);
216 // Draws a single pixel rect in the specified region with the specified
217 // color, using a transfer mode of SkXfermode::kSrcOver_Mode.
219 // NOTE: if you need a single pixel line, use DrawLine.
220 void DrawRect(const Rect
& rect
, SkColor color
);
222 // Draws a single pixel rect in the specified region with the specified
223 // color and transfer mode.
225 // NOTE: if you need a single pixel line, use DrawLine.
226 void DrawRect(const Rect
& rect
, SkColor color
, SkXfermode::Mode mode
);
228 // Draws the given rectangle with the given |paint| parameters.
229 void DrawRect(const Rect
& rect
, const SkPaint
& paint
);
231 // Draw the given point with the given |paint| parameters.
232 void DrawPoint(const Point
& p
, const SkPaint
& paint
);
234 // Draws a single pixel line with the specified color.
235 void DrawLine(const Point
& p1
, const Point
& p2
, SkColor color
);
237 // Draws a line with the given |paint| parameters.
238 void DrawLine(const Point
& p1
, const Point
& p2
, const SkPaint
& paint
);
240 // Draws a circle with the given |paint| parameters.
241 void DrawCircle(const Point
& center_point
,
243 const SkPaint
& paint
);
245 // Draws the given rectangle with rounded corners of |radius| using the
246 // given |paint| parameters.
247 void DrawRoundRect(const Rect
& rect
, int radius
, const SkPaint
& paint
);
249 // Draws the given path using the given |paint| parameters.
250 void DrawPath(const SkPath
& path
, const SkPaint
& paint
);
252 // Draws an image with the origin at the specified location. The upper left
253 // corner of the bitmap is rendered at the specified location.
254 // Parameters are specified relative to current canvas scale not in pixels.
255 // Thus, x is 2 pixels if canvas scale = 2 & |x| = 1.
256 void DrawImageInt(const ImageSkia
&, int x
, int y
);
258 // Helper for DrawImageInt(..., paint) that constructs a temporary paint and
259 // calls paint.setAlpha(alpha).
260 void DrawImageInt(const ImageSkia
&, int x
, int y
, uint8 alpha
);
262 // Draws an image with the origin at the specified location, using the
263 // specified paint. The upper left corner of the bitmap is rendered at the
264 // specified location.
265 // Parameters are specified relative to current canvas scale not in pixels.
266 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1.
267 void DrawImageInt(const ImageSkia
& image
,
270 const SkPaint
& paint
);
272 // Draws a portion of an image in the specified location. The src parameters
273 // correspond to the region of the bitmap to draw in the region defined
274 // by the dest coordinates.
276 // If the width or height of the source differs from that of the destination,
277 // the image will be scaled. When scaling down, a mipmap will be generated.
278 // Set |filter| to use filtering for images, otherwise the nearest-neighbor
279 // algorithm is used for resampling.
281 // An optional custom SkPaint can be provided.
282 // Parameters are specified relative to current canvas scale not in pixels.
283 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1.
284 void DrawImageInt(const ImageSkia
& image
,
294 void DrawImageInt(const ImageSkia
& image
,
304 const SkPaint
& paint
);
306 // Same as the DrawImageInt functions above. Difference being this does not
307 // do any scaling, i.e. it assumes that the source/destination/image, etc are
308 // in pixels. It does translate the destination rectangle to ensure that the
309 // image is displayed at the correct pixel coordinates.
310 void DrawImageIntInPixel(const ImageSkia
& image
,
320 const SkPaint
& paint
);
322 // Draws an |image| with the top left corner at |x| and |y|, clipped to
324 // Parameters are specified relative to current canvas scale not in pixels.
325 // Thus, x is 2 pixels if canvas scale = 2 & |x| = 1.
326 void DrawImageInPath(const ImageSkia
& image
,
330 const SkPaint
& paint
);
332 // Draws text with the specified color, fonts and location. The text is
333 // aligned to the left, vertically centered, clipped to the region. If the
334 // text is too big, it is truncated and '...' is added to the end.
335 void DrawStringRect(const base::string16
& text
,
336 const FontList
& font_list
,
338 const Rect
& display_rect
);
340 // Draws text with the specified color, fonts and location. The last argument
341 // specifies flags for how the text should be rendered. It can be one of
342 // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT.
343 void DrawStringRectWithFlags(const base::string16
& text
,
344 const FontList
& font_list
,
346 const Rect
& display_rect
,
349 // Similar to above DrawStringRect method but with text shadows support.
350 // Currently it's only implemented for canvas skia. Specifying a 0 line_height
351 // will cause the default height to be used.
352 void DrawStringRectWithShadows(const base::string16
& text
,
353 const FontList
& font_list
,
355 const Rect
& text_bounds
,
358 const ShadowValues
& shadows
);
360 // Draws a dotted gray rectangle used for focus purposes.
361 void DrawFocusRect(const Rect
& rect
);
363 // Draws a |rect| in the specified region with the specified |color| with a
364 // with of one logical pixel which might be more device pixels.
365 void DrawSolidFocusRect(const Rect
& rect
, SkColor color
);
367 // Tiles the image in the specified region.
368 // Parameters are specified relative to current canvas scale not in pixels.
369 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1.
370 void TileImageInt(const ImageSkia
& image
,
375 void TileImageInt(const ImageSkia
& image
,
382 void TileImageInt(const ImageSkia
& image
,
392 // Returns a native drawing context for platform specific drawing routines to
393 // use. Must be balanced by a call to EndPlatformPaint().
394 NativeDrawingContext
BeginPlatformPaint();
396 // Signifies the end of platform drawing using the native drawing context
397 // returned by BeginPlatformPaint().
398 void EndPlatformPaint();
400 // Apply transformation on the canvas.
401 void Transform(const Transform
& transform
);
403 // Draws the given string with a fade gradient at the end.
404 void DrawFadedString(const base::string16
& text
,
405 const FontList
& font_list
,
407 const Rect
& display_rect
,
410 skia::PlatformCanvas
* platform_canvas() { return owned_canvas_
.get(); }
411 SkCanvas
* sk_canvas() { return canvas_
; }
412 float image_scale() const { return image_scale_
; }
415 // Test whether the provided rectangle intersects the current clip rect.
416 bool IntersectsClipRectInt(int x
, int y
, int w
, int h
);
417 bool IntersectsClipRect(const Rect
& rect
);
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.
422 void DrawImageIntHelper(const ImageSkia
& image
,
432 const SkPaint
& paint
,
436 // The device scale factor at which drawing on this canvas occurs.
437 // An additional scale can be applied via Canvas::Scale(). However,
438 // Canvas::Scale() does not affect |image_scale_|.
441 skia::RefPtr
<skia::PlatformCanvas
> owned_canvas_
;
444 DISALLOW_COPY_AND_ASSIGN(Canvas
);
449 #endif // UI_GFX_CANVAS_H_