blob: c3af1e32299bf63ca47ac0e80396b2cea0857cbb [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_
7#pragma once
8
9#include <string>
10
11#include "base/string16.h"
[email protected]91ef7b02011-08-02 15:26:0312// TODO(beng): remove these includes when we no longer depend on SkTypes.
13#include "third_party/skia/include/core/SkColor.h"
14#include "third_party/skia/include/core/SkXfermode.h"
[email protected]507bad52011-08-06 04:51:0715#include "ui/base/ui_export.h"
[email protected]08397d52011-02-05 01:53:3816#include "ui/gfx/native_widget_types.h"
[email protected]267c03d2011-02-02 23:03:0717
[email protected]be172ba82011-10-05 19:05:0718class SkCanvas;
19
[email protected]36df22b2011-02-24 21:47:5620namespace ui {
21class Transform;
[email protected]c797cd42011-03-15 02:18:3622typedef unsigned int TextureID;
[email protected]36df22b2011-02-24 21:47:5623}
24
[email protected]267c03d2011-02-02 23:03:0725namespace gfx {
26
27class Brush;
28class CanvasSkia;
29class Font;
30class Point;
31class Rect;
[email protected]7e82ed012011-11-01 01:40:0732class Size;
[email protected]267c03d2011-02-02 23:03:0733
34// TODO(beng): documentation.
[email protected]507bad52011-08-06 04:51:0735class UI_EXPORT Canvas {
[email protected]267c03d2011-02-02 23:03:0736 public:
37 // Specifies the alignment for text rendered with the DrawStringInt method.
38 enum {
39 TEXT_ALIGN_LEFT = 1,
40 TEXT_ALIGN_CENTER = 2,
41 TEXT_ALIGN_RIGHT = 4,
42 TEXT_VALIGN_TOP = 8,
43 TEXT_VALIGN_MIDDLE = 16,
44 TEXT_VALIGN_BOTTOM = 32,
45
46 // Specifies the text consists of multiple lines.
47 MULTI_LINE = 64,
48
49 // By default DrawStringInt does not process the prefix ('&') character
50 // specially. That is, the string "&foo" is rendered as "&foo". When
51 // rendering text from a resource that uses the prefix character for
52 // mnemonics, the prefix should be processed and can be rendered as an
53 // underline (SHOW_PREFIX), or not rendered at all (HIDE_PREFIX).
54 SHOW_PREFIX = 128,
55 HIDE_PREFIX = 256,
56
57 // Prevent ellipsizing
58 NO_ELLIPSIS = 512,
59
60 // Specifies if words can be split by new lines.
61 // This only works with MULTI_LINE.
62 CHARACTER_BREAK = 1024,
63
64 // Instructs DrawStringInt() to render the text using RTL directionality.
65 // In most cases, passing this flag is not necessary because information
66 // about the text directionality is going to be embedded within the string
67 // in the form of special Unicode characters. However, we don't insert
68 // directionality characters into strings if the locale is LTR because some
69 // platforms (for example, an English Windows XP with no RTL fonts
70 // installed) don't support these characters. Thus, this flag should be
71 // used to render text using RTL directionality when the locale is LTR.
72 FORCE_RTL_DIRECTIONALITY = 2048,
[email protected]3f49b812011-08-18 23:32:3473
74 // Similar to FORCE_RTL_DIRECTIONALITY, but left-to-right.
75 // See FORCE_RTL_DIRECTIONALITY for details.
76 FORCE_LTR_DIRECTIONALITY = 4096,
[email protected]267c03d2011-02-02 23:03:0777 };
78
79 virtual ~Canvas() {}
80
81 // Creates an empty canvas. Must be initialized before it can be used.
82 static Canvas* CreateCanvas();
83
84 // Creates a canvas with the specified size.
[email protected]7e82ed012011-11-01 01:40:0785 static Canvas* CreateCanvas(const gfx::Size& size, bool is_opaque);
[email protected]267c03d2011-02-02 23:03:0786
87 // Saves a copy of the drawing state onto a stack, operating on this copy
88 // until a balanced call to Restore() is made.
89 virtual void Save() = 0;
90
91 // As with Save(), except draws to a layer that is blended with the canvas
92 // at the specified alpha once Restore() is called.
93 // |layer_bounds| are the bounds of the layer relative to the current
94 // transform.
95 virtual void SaveLayerAlpha(uint8 alpha) = 0;
96 virtual void SaveLayerAlpha(uint8 alpha, const gfx::Rect& layer_bounds) = 0;
97
98 // Restores the drawing state after a call to Save*(). It is an error to
99 // call Restore() more times than Save*().
100 virtual void Restore() = 0;
101
[email protected]267c03d2011-02-02 23:03:07102 // Returns true if the clip is non-empty.
[email protected]7fe28392011-10-27 00:16:05103 virtual bool ClipRect(const gfx::Rect& rect) = 0;
104
105 virtual void Translate(const gfx::Point& point) = 0;
[email protected]267c03d2011-02-02 23:03:07106
[email protected]80277112011-10-27 12:29:21107 virtual void Scale(int x_scale, int y_scale) = 0;
[email protected]267c03d2011-02-02 23:03:07108
109 // Fills the specified region with the specified color using a transfer
110 // mode of SkXfermode::kSrcOver_Mode.
[email protected]e728d1d2011-10-31 00:11:05111 virtual void FillRect(const SkColor& color, const gfx::Rect& rect) = 0;
[email protected]267c03d2011-02-02 23:03:07112
[email protected]e728d1d2011-10-31 00:11:05113 // Fills the specified region with the specified color and mode.
114 virtual void FillRect(const SkColor& color,
115 const gfx::Rect& rect,
116 SkXfermode::Mode mode) = 0;
[email protected]267c03d2011-02-02 23:03:07117
118 // Fills the specified region with the specified brush.
[email protected]e728d1d2011-10-31 00:11:05119 virtual void FillRect(const gfx::Brush* brush, const gfx::Rect& rect) = 0;
[email protected]267c03d2011-02-02 23:03:07120
121 // Draws a single pixel rect in the specified region with the specified
122 // color, using a transfer mode of SkXfermode::kSrcOver_Mode.
123 //
124 // NOTE: if you need a single pixel line, use DrawLineInt.
[email protected]3acc6422011-12-17 16:31:31125 virtual void DrawRect(const gfx::Rect& rect, const SkColor& color) = 0;
[email protected]267c03d2011-02-02 23:03:07126
127 // Draws a single pixel rect in the specified region with the specified
128 // color and transfer mode.
129 //
130 // NOTE: if you need a single pixel line, use DrawLineInt.
[email protected]3acc6422011-12-17 16:31:31131 virtual void DrawRect(const gfx::Rect& rect,
132 const SkColor& color,
133 SkXfermode::Mode mode) = 0;
[email protected]267c03d2011-02-02 23:03:07134
135 // Draws the given rectangle with the given paint's parameters.
[email protected]3acc6422011-12-17 16:31:31136 virtual void DrawRect(const gfx::Rect& rect, const SkPaint& paint) = 0;
[email protected]267c03d2011-02-02 23:03:07137
138 // Draws a single pixel line with the specified color.
139 virtual void DrawLineInt(const SkColor& color,
140 int x1, int y1,
141 int x2, int y2) = 0;
142
143 // Draws a bitmap with the origin at the specified location. The upper left
144 // corner of the bitmap is rendered at the specified location.
145 virtual void DrawBitmapInt(const SkBitmap& bitmap, int x, int y) = 0;
146
147 // Draws a bitmap with the origin at the specified location, using the
148 // specified paint. The upper left corner of the bitmap is rendered at the
149 // specified location.
150 virtual void DrawBitmapInt(const SkBitmap& bitmap,
151 int x, int y,
152 const SkPaint& paint) = 0;
153
154 // Draws a portion of a bitmap in the specified location. The src parameters
155 // correspond to the region of the bitmap to draw in the region defined
156 // by the dest coordinates.
157 //
158 // If the width or height of the source differs from that of the destination,
159 // the bitmap will be scaled. When scaling down, it is highly recommended
160 // that you call buildMipMap(false) on your bitmap to ensure that it has
161 // a mipmap, which will result in much higher-quality output. Set |filter|
162 // to use filtering for bitmaps, otherwise the nearest-neighbor algorithm
163 // is used for resampling.
164 //
165 // An optional custom SkPaint can be provided.
166 virtual void DrawBitmapInt(const SkBitmap& bitmap,
167 int src_x, int src_y, int src_w, int src_h,
168 int dest_x, int dest_y, int dest_w, int dest_h,
169 bool filter) = 0;
170 virtual void DrawBitmapInt(const SkBitmap& bitmap,
171 int src_x, int src_y, int src_w, int src_h,
172 int dest_x, int dest_y, int dest_w, int dest_h,
173 bool filter,
174 const SkPaint& paint) = 0;
175
176 // Draws text with the specified color, font and location. The text is
177 // aligned to the left, vertically centered, clipped to the region. If the
178 // text is too big, it is truncated and '...' is added to the end.
[email protected]4e8655d2011-06-16 17:20:17179 virtual void DrawStringInt(const string16& text,
180 const gfx::Font& font,
[email protected]267c03d2011-02-02 23:03:07181 const SkColor& color,
182 int x, int y, int w, int h) = 0;
183 virtual void DrawStringInt(const string16& text,
184 const gfx::Font& font,
185 const SkColor& color,
186 const gfx::Rect& display_rect) = 0;
187
188 // Draws text with the specified color, font and location. The last argument
189 // specifies flags for how the text should be rendered. It can be one of
190 // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT.
191 virtual void DrawStringInt(const string16& text,
192 const gfx::Font& font,
193 const SkColor& color,
194 int x, int y, int w, int h,
195 int flags) = 0;
196
197 // Draws a dotted gray rectangle used for focus purposes.
[email protected]27488c22011-10-25 02:45:21198 virtual void DrawFocusRect(const gfx::Rect& rect) = 0;
[email protected]267c03d2011-02-02 23:03:07199
200 // Tiles the image in the specified region.
201 virtual void TileImageInt(const SkBitmap& bitmap,
202 int x, int y, int w, int h) = 0;
203 virtual void TileImageInt(const SkBitmap& bitmap,
204 int src_x, int src_y,
205 int dest_x, int dest_y, int w, int h) = 0;
206
207 // Returns a native drawing context for platform specific drawing routines to
208 // use. Must be balanced by a call to EndPlatformPaint().
209 virtual gfx::NativeDrawingContext BeginPlatformPaint() = 0;
210
211 // Signifies the end of platform drawing using the native drawing context
212 // returned by BeginPlatformPaint().
213 virtual void EndPlatformPaint() = 0;
214
[email protected]36df22b2011-02-24 21:47:56215 // Apply transformation on the canvas.
216 virtual void Transform(const ui::Transform& transform) = 0;
217
[email protected]c797cd42011-03-15 02:18:36218 // Create a texture ID that can be used for accelerated drawing.
219 virtual ui::TextureID GetTextureID() = 0;
220
[email protected]267c03d2011-02-02 23:03:07221 // TODO(beng): remove this once we don't need to use any skia-specific methods
222 // through this interface.
223 // A quick and dirty way to obtain the underlying SkCanvas.
224 virtual CanvasSkia* AsCanvasSkia();
225 virtual const CanvasSkia* AsCanvasSkia() const;
[email protected]be172ba82011-10-05 19:05:07226 virtual SkCanvas* GetSkCanvas();
227 virtual const SkCanvas* GetSkCanvas() const;
[email protected]267c03d2011-02-02 23:03:07228};
229
[email protected]507bad52011-08-06 04:51:07230class UI_EXPORT CanvasPaint {
[email protected]267c03d2011-02-02 23:03:07231 public:
232 virtual ~CanvasPaint() {}
233
234 // Creates a canvas that paints to |view| when it is destroyed. The canvas is
235 // sized to the client area of |view|.
236 static CanvasPaint* CreateCanvasPaint(gfx::NativeView view);
237
238 // Returns true if the canvas has an invalid rect that needs to be repainted.
239 virtual bool IsValid() const = 0;
240
241 // Returns the rectangle that is invalid.
242 virtual gfx::Rect GetInvalidRect() const = 0;
243
244 // Returns the underlying Canvas.
245 virtual Canvas* AsCanvas() = 0;
246};
247
[email protected]7fe28392011-10-27 00:16:05248} // namespace gfx
[email protected]267c03d2011-02-02 23:03:07249
250#endif // UI_GFX_CANVAS_H_