UpdateProcThreadAttribute has a restriction that its lpValue parameter
[chromium-blink-merge.git] / ui / gfx / canvas.h
blob88882fa2f765d1c754655df1e688588615e4124c
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_
8 #include <vector>
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"
20 namespace gfx {
22 class Rect;
23 class FontList;
24 class Point;
25 class Size;
26 class Transform;
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
40 // of kSrcOver_Mode.
41 class GFX_EXPORT Canvas {
42 public:
43 enum {
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.
51 MULTI_LINE = 1 << 4,
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).
58 SHOW_PREFIX = 1 << 5,
59 HIDE_PREFIX = 1 << 6,
61 // Prevent ellipsizing
62 NO_ELLIPSIS = 1 << 7,
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.
75 Canvas();
77 // Creates canvas with provided DIP |size| and |image_scale|.
78 // If this canvas is not opaque, it's explicitly cleared to transparent before
79 // being returned.
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 virtual ~Canvas();
88 // Creates a Canvas backed by an |sk_canvas| with |image_scale_|.
89 // |sk_canvas| is assumed to be already scaled based on |image_scale|
90 // so no additional scaling is applied.
91 static Canvas* CreateCanvasWithoutScaling(SkCanvas* sk_canvas,
92 float image_scale);
94 // Recreates the backing platform canvas with DIP |size| and |image_scale_|.
95 // If the canvas is not opaque, it is explicitly cleared.
96 // This method is public so that canvas_skia_paint can recreate the platform
97 // canvas after having initialized the canvas.
98 // TODO(pkotwicz): Push the image_scale into skia::PlatformCanvas such that
99 // this method can be private.
100 void RecreateBackingCanvas(const Size& size,
101 float image_scale,
102 bool is_opaque);
104 // Compute the size required to draw some text with the provided fonts.
105 // Attempts to fit the text with the provided width and height. Increases
106 // height and then width as needed to make the text fit. This method
107 // supports multiple lines. On Skia only a line_height can be specified and
108 // specifying a 0 value for it will cause the default height to be used.
109 static void SizeStringInt(const base::string16& text,
110 const FontList& font_list,
111 int* width,
112 int* height,
113 int line_height,
114 int flags);
116 // This is same as SizeStringInt except that fractional size is returned.
117 // See comment in GetStringWidthF for its usage.
118 static void SizeStringFloat(const base::string16& text,
119 const FontList& font_list,
120 float* width,
121 float* height,
122 int line_height,
123 int flags);
125 // Returns the number of horizontal pixels needed to display the specified
126 // |text| with |font_list|.
127 static int GetStringWidth(const base::string16& text,
128 const FontList& font_list);
130 // This is same as GetStringWidth except that fractional width is returned.
131 // Use this method for the scenario that multiple string widths need to be
132 // summed up. This is because GetStringWidth returns the ceiled width and
133 // adding multiple ceiled widths could cause more precision loss for certain
134 // platform like Mac where the fractioal width is used.
135 static float GetStringWidthF(const base::string16& text,
136 const FontList& font_list);
138 // Returns the default text alignment to be used when drawing text on a
139 // Canvas based on the directionality of the system locale language.
140 // This function is used by Canvas::DrawStringRect when the text alignment
141 // is not specified.
143 // This function returns either Canvas::TEXT_ALIGN_LEFT or
144 // Canvas::TEXT_ALIGN_RIGHT.
145 static int DefaultCanvasTextAlignment();
147 // Draws text with a 1-pixel halo around it of the given color.
148 // On Windows, it allows ClearType to be drawn to an otherwise transparent
149 // bitmap for drag images. Drag images have only 1-bit of transparency, so
150 // we don't do any fancy blurring.
151 // On Linux, text with halo is created by stroking it with 2px |halo_color|
152 // then filling it with |text_color|.
153 // On Mac, NOTIMPLEMENTED.
154 // TODO(dhollowa): Skia-native implementation is underway. Cut over to
155 // that when ready. http::/crbug.com/109946
156 void DrawStringRectWithHalo(const base::string16& text,
157 const FontList& font_list,
158 SkColor text_color,
159 SkColor halo_color,
160 const Rect& display_rect,
161 int flags);
163 // Extracts an ImageSkiaRep from the contents of this canvas.
164 ImageSkiaRep ExtractImageRep() const;
166 // Draws a dashed rectangle of the specified color.
167 void DrawDashedRect(const Rect& rect, SkColor color);
169 // Saves a copy of the drawing state onto a stack, operating on this copy
170 // until a balanced call to Restore() is made.
171 void Save();
173 // As with Save(), except draws to a layer that is blended with the canvas
174 // at the specified alpha once Restore() is called.
175 // |layer_bounds| are the bounds of the layer relative to the current
176 // transform.
177 void SaveLayerAlpha(uint8 alpha);
178 void SaveLayerAlpha(uint8 alpha, const Rect& layer_bounds);
180 // Restores the drawing state after a call to Save*(). It is an error to
181 // call Restore() more times than Save*().
182 void Restore();
184 // Adds |rect| to the current clip.
185 void ClipRect(const Rect& rect);
187 // Adds |path| to the current clip. |do_anti_alias| is true if the clip
188 // should be antialiased.
189 void ClipPath(const SkPath& path, bool do_anti_alias);
191 // Returns true if the current clip is empty.
192 bool IsClipEmpty() const;
194 // Returns the bounds of the current clip (in local coordinates) in the
195 // |bounds| parameter, and returns true if it is non empty.
196 bool GetClipBounds(Rect* bounds);
198 void Translate(const Vector2d& offset);
200 void Scale(int x_scale, int y_scale);
202 // Fills the entire canvas' bitmap (restricted to current clip) with
203 // specified |color| using a transfer mode of SkXfermode::kSrcOver_Mode.
204 void DrawColor(SkColor color);
206 // Fills the entire canvas' bitmap (restricted to current clip) with
207 // specified |color| and |mode|.
208 void DrawColor(SkColor color, SkXfermode::Mode mode);
210 // Fills |rect| with |color| using a transfer mode of
211 // SkXfermode::kSrcOver_Mode.
212 void FillRect(const Rect& rect, SkColor color);
214 // Fills |rect| with the specified |color| and |mode|.
215 void FillRect(const Rect& rect, SkColor color, SkXfermode::Mode mode);
217 // Draws a single pixel rect in the specified region with the specified
218 // color, using a transfer mode of SkXfermode::kSrcOver_Mode.
220 // NOTE: if you need a single pixel line, use DrawLine.
221 void DrawRect(const Rect& rect, SkColor color);
223 // Draws a single pixel rect in the specified region with the specified
224 // color and transfer mode.
226 // NOTE: if you need a single pixel line, use DrawLine.
227 void DrawRect(const Rect& rect, SkColor color, SkXfermode::Mode mode);
229 // Draws the given rectangle with the given |paint| parameters.
230 void DrawRect(const Rect& rect, const SkPaint& paint);
232 // Draw the given point with the given |paint| parameters.
233 void DrawPoint(const Point& p, const SkPaint& paint);
235 // Draws a single pixel line with the specified color.
236 void DrawLine(const Point& p1, const Point& p2, SkColor color);
238 // Draws a line with the given |paint| parameters.
239 void DrawLine(const Point& p1, const Point& p2, const SkPaint& paint);
241 // Draws a circle with the given |paint| parameters.
242 void DrawCircle(const Point& center_point,
243 int radius,
244 const SkPaint& paint);
246 // Draws the given rectangle with rounded corners of |radius| using the
247 // given |paint| parameters.
248 void DrawRoundRect(const Rect& rect, int radius, const SkPaint& paint);
250 // Draws the given path using the given |paint| parameters.
251 void DrawPath(const SkPath& path, const SkPaint& paint);
253 // Draws an image with the origin at the specified location. The upper left
254 // corner of the bitmap is rendered at the specified location.
255 // Parameters are specified relative to current canvas scale not in pixels.
256 // Thus, x is 2 pixels if canvas scale = 2 & |x| = 1.
257 void DrawImageInt(const ImageSkia&, int x, int y);
259 // Helper for DrawImageInt(..., paint) that constructs a temporary paint and
260 // calls paint.setAlpha(alpha).
261 void DrawImageInt(const ImageSkia&, int x, int y, uint8 alpha);
263 // Draws an image with the origin at the specified location, using the
264 // specified paint. The upper left corner of the bitmap is rendered at the
265 // specified location.
266 // Parameters are specified relative to current canvas scale not in pixels.
267 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1.
268 void DrawImageInt(const ImageSkia& image,
269 int x,
270 int y,
271 const SkPaint& paint);
273 // Draws a portion of an image in the specified location. The src parameters
274 // correspond to the region of the bitmap to draw in the region defined
275 // by the dest coordinates.
277 // If the width or height of the source differs from that of the destination,
278 // the image will be scaled. When scaling down, a mipmap will be generated.
279 // Set |filter| to use filtering for images, otherwise the nearest-neighbor
280 // algorithm is used for resampling.
282 // An optional custom SkPaint can be provided.
283 // Parameters are specified relative to current canvas scale not in pixels.
284 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1.
285 void DrawImageInt(const ImageSkia& image,
286 int src_x,
287 int src_y,
288 int src_w,
289 int src_h,
290 int dest_x,
291 int dest_y,
292 int dest_w,
293 int dest_h,
294 bool filter);
295 void DrawImageInt(const ImageSkia& image,
296 int src_x,
297 int src_y,
298 int src_w,
299 int src_h,
300 int dest_x,
301 int dest_y,
302 int dest_w,
303 int dest_h,
304 bool filter,
305 const SkPaint& paint);
307 // Same as the DrawImageInt functions above. Difference being this does not
308 // do any scaling, i.e. it assumes that the source/destination/image, etc are
309 // in pixels. It does translate the destination rectangle to ensure that the
310 // image is displayed at the correct pixel coordinates.
311 void DrawImageIntInPixel(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,
320 bool filter,
321 const SkPaint& paint);
323 // Draws an |image| with the top left corner at |x| and |y|, clipped to
324 // |path|.
325 // Parameters are specified relative to current canvas scale not in pixels.
326 // Thus, x is 2 pixels if canvas scale = 2 & |x| = 1.
327 void DrawImageInPath(const ImageSkia& image,
328 int x,
329 int y,
330 const SkPath& path,
331 const SkPaint& paint);
333 // Draws text with the specified color, fonts and location. The text is
334 // aligned to the left, vertically centered, clipped to the region. If the
335 // text is too big, it is truncated and '...' is added to the end.
336 void DrawStringRect(const base::string16& text,
337 const FontList& font_list,
338 SkColor color,
339 const Rect& display_rect);
341 // Draws text with the specified color, fonts and location. The last argument
342 // specifies flags for how the text should be rendered. It can be one of
343 // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT.
344 void DrawStringRectWithFlags(const base::string16& text,
345 const FontList& font_list,
346 SkColor color,
347 const Rect& display_rect,
348 int flags);
350 // Similar to above DrawStringRect method but with text shadows support.
351 // Currently it's only implemented for canvas skia. Specifying a 0 line_height
352 // will cause the default height to be used.
353 void DrawStringRectWithShadows(const base::string16& text,
354 const FontList& font_list,
355 SkColor color,
356 const Rect& text_bounds,
357 int line_height,
358 int flags,
359 const ShadowValues& shadows);
361 // Draws a dotted gray rectangle used for focus purposes.
362 void DrawFocusRect(const Rect& rect);
364 // Draws a |rect| in the specified region with the specified |color| with a
365 // with of one logical pixel which might be more device pixels.
366 void DrawSolidFocusRect(const Rect& rect, SkColor color);
368 // Tiles the image in the specified region.
369 // Parameters are specified relative to current canvas scale not in pixels.
370 // Thus, |x| is 2 pixels if canvas scale = 2 & |x| = 1.
371 void TileImageInt(const ImageSkia& image,
372 int x,
373 int y,
374 int w,
375 int h);
376 void TileImageInt(const ImageSkia& image,
377 int src_x,
378 int src_y,
379 int dest_x,
380 int dest_y,
381 int w,
382 int h);
383 void TileImageInt(const ImageSkia& image,
384 int src_x,
385 int src_y,
386 float tile_scale_x,
387 float tile_scale_y,
388 int dest_x,
389 int dest_y,
390 int w,
391 int h);
393 // Returns a native drawing context for platform specific drawing routines to
394 // use. Must be balanced by a call to EndPlatformPaint().
395 NativeDrawingContext BeginPlatformPaint();
397 // Signifies the end of platform drawing using the native drawing context
398 // returned by BeginPlatformPaint().
399 void EndPlatformPaint();
401 // Apply transformation on the canvas.
402 void Transform(const Transform& transform);
404 // Draws the given string with a fade gradient at the end.
405 void DrawFadedString(const base::string16& text,
406 const FontList& font_list,
407 SkColor color,
408 const Rect& display_rect,
409 int flags);
411 skia::PlatformCanvas* platform_canvas() { return owned_canvas_.get(); }
412 SkCanvas* sk_canvas() { return canvas_; }
413 float image_scale() const { return image_scale_; }
415 private:
416 Canvas(SkCanvas* canvas, float image_scale);
418 // Test whether the provided rectangle intersects the current clip rect.
419 bool IntersectsClipRectInt(int x, int y, int w, int h);
420 bool IntersectsClipRect(const Rect& rect);
422 // Helper for the DrawImageInt functions declared above. The |pixel|
423 // parameter if true indicates that the bounds and the image are to
424 // be assumed to be in pixels, i.e. no scaling needs to be performed.
425 void DrawImageIntHelper(const ImageSkia& image,
426 int src_x,
427 int src_y,
428 int src_w,
429 int src_h,
430 int dest_x,
431 int dest_y,
432 int dest_w,
433 int dest_h,
434 bool filter,
435 const SkPaint& paint,
436 float image_scale,
437 bool pixel);
439 // The device scale factor at which drawing on this canvas occurs.
440 // An additional scale can be applied via Canvas::Scale(). However,
441 // Canvas::Scale() does not affect |image_scale_|.
442 float image_scale_;
444 skia::RefPtr<skia::PlatformCanvas> owned_canvas_;
445 SkCanvas* canvas_;
447 DISALLOW_COPY_AND_ASSIGN(Canvas);
450 } // namespace gfx
452 #endif // UI_GFX_CANVAS_H_