OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GFX_CANVAS_H_ | 5 #ifndef GFX_CANVAS_H_ |
6 #define GFX_CANVAS_H_ | 6 #define GFX_CANVAS_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include "ui/gfx/canvas.h" |
10 | 10 // TODO(sail): remove this file once all includes have been updated. |
11 #include "base/string16.h" | |
12 #include "gfx/native_widget_types.h" | |
13 // TODO(beng): remove this include when we no longer depend on SkTypes. | |
14 #include "skia/ext/platform_canvas.h" | |
15 | |
16 namespace gfx { | |
17 | |
18 class Brush; | |
19 class CanvasSkia; | |
20 class Font; | |
21 class Point; | |
22 class Rect; | |
23 | |
24 // TODO(beng): documentation. | |
25 class Canvas { | |
26 public: | |
27 // Specifies the alignment for text rendered with the DrawStringInt method. | |
28 enum { | |
29 TEXT_ALIGN_LEFT = 1, | |
30 TEXT_ALIGN_CENTER = 2, | |
31 TEXT_ALIGN_RIGHT = 4, | |
32 TEXT_VALIGN_TOP = 8, | |
33 TEXT_VALIGN_MIDDLE = 16, | |
34 TEXT_VALIGN_BOTTOM = 32, | |
35 | |
36 // Specifies the text consists of multiple lines. | |
37 MULTI_LINE = 64, | |
38 | |
39 // By default DrawStringInt does not process the prefix ('&') character | |
40 // specially. That is, the string "&foo" is rendered as "&foo". When | |
41 // rendering text from a resource that uses the prefix character for | |
42 // mnemonics, the prefix should be processed and can be rendered as an | |
43 // underline (SHOW_PREFIX), or not rendered at all (HIDE_PREFIX). | |
44 SHOW_PREFIX = 128, | |
45 HIDE_PREFIX = 256, | |
46 | |
47 // Prevent ellipsizing | |
48 NO_ELLIPSIS = 512, | |
49 | |
50 // Specifies if words can be split by new lines. | |
51 // This only works with MULTI_LINE. | |
52 CHARACTER_BREAK = 1024, | |
53 | |
54 // Instructs DrawStringInt() to render the text using RTL directionality. | |
55 // In most cases, passing this flag is not necessary because information | |
56 // about the text directionality is going to be embedded within the string | |
57 // in the form of special Unicode characters. However, we don't insert | |
58 // directionality characters into strings if the locale is LTR because some | |
59 // platforms (for example, an English Windows XP with no RTL fonts | |
60 // installed) don't support these characters. Thus, this flag should be | |
61 // used to render text using RTL directionality when the locale is LTR. | |
62 FORCE_RTL_DIRECTIONALITY = 2048, | |
63 }; | |
64 | |
65 virtual ~Canvas() {} | |
66 | |
67 // Creates an empty canvas. Must be initialized before it can be used. | |
68 static Canvas* CreateCanvas(); | |
69 | |
70 // Creates a canvas with the specified size. | |
71 static Canvas* CreateCanvas(int width, int height, bool is_opaque); | |
72 | |
73 // Saves a copy of the drawing state onto a stack, operating on this copy | |
74 // until a balanced call to Restore() is made. | |
75 virtual void Save() = 0; | |
76 | |
77 // As with Save(), except draws to a layer that is blended with the canvas | |
78 // at the specified alpha once Restore() is called. | |
79 // |layer_bounds| are the bounds of the layer relative to the current | |
80 // transform. | |
81 virtual void SaveLayerAlpha(uint8 alpha) = 0; | |
82 virtual void SaveLayerAlpha(uint8 alpha, const gfx::Rect& layer_bounds) = 0; | |
83 | |
84 // Restores the drawing state after a call to Save*(). It is an error to | |
85 // call Restore() more times than Save*(). | |
86 virtual void Restore() = 0; | |
87 | |
88 // Wrapper function that takes integer arguments. | |
89 // Returns true if the clip is non-empty. | |
90 // See clipRect for specifics. | |
91 virtual bool ClipRectInt(int x, int y, int w, int h) = 0; | |
92 | |
93 // Wrapper function that takes integer arguments. | |
94 // See translate() for specifics. | |
95 virtual void TranslateInt(int x, int y) = 0; | |
96 | |
97 // Wrapper function that takes integer arguments. | |
98 // See scale() for specifics. | |
99 virtual void ScaleInt(int x, int y) = 0; | |
100 | |
101 // Fills the specified region with the specified color using a transfer | |
102 // mode of SkXfermode::kSrcOver_Mode. | |
103 virtual void FillRectInt(const SkColor& color, | |
104 int x, int y, int w, int h) = 0; | |
105 | |
106 // Fills the specified region with the specified color and mode | |
107 virtual void FillRectInt(const SkColor& color, | |
108 int x, int y, int w, int h, | |
109 SkXfermode::Mode mode) = 0; | |
110 | |
111 // Fills the specified region with the specified brush. | |
112 virtual void FillRectInt(const gfx::Brush* brush, | |
113 int x, int y, int w, int h) = 0; | |
114 | |
115 // Draws a single pixel rect in the specified region with the specified | |
116 // color, using a transfer mode of SkXfermode::kSrcOver_Mode. | |
117 // | |
118 // NOTE: if you need a single pixel line, use DrawLineInt. | |
119 virtual void DrawRectInt(const SkColor& color, | |
120 int x, int y, int w, int h) = 0; | |
121 | |
122 // Draws a single pixel rect in the specified region with the specified | |
123 // color and transfer mode. | |
124 // | |
125 // NOTE: if you need a single pixel line, use DrawLineInt. | |
126 virtual void DrawRectInt(const SkColor& color, | |
127 int x, int y, int w, int h, | |
128 SkXfermode::Mode mode) = 0; | |
129 | |
130 // Draws the given rectangle with the given paint's parameters. | |
131 virtual void DrawRectInt(int x, int y, int w, int h, | |
132 const SkPaint& paint) = 0; | |
133 | |
134 // Draws a single pixel line with the specified color. | |
135 virtual void DrawLineInt(const SkColor& color, | |
136 int x1, int y1, | |
137 int x2, int y2) = 0; | |
138 | |
139 // Draws a bitmap with the origin at the specified location. The upper left | |
140 // corner of the bitmap is rendered at the specified location. | |
141 virtual void DrawBitmapInt(const SkBitmap& bitmap, int x, int y) = 0; | |
142 | |
143 // Draws a bitmap with the origin at the specified location, using the | |
144 // specified paint. The upper left corner of the bitmap is rendered at the | |
145 // specified location. | |
146 virtual void DrawBitmapInt(const SkBitmap& bitmap, | |
147 int x, int y, | |
148 const SkPaint& paint) = 0; | |
149 | |
150 // Draws a portion of a bitmap in the specified location. The src parameters | |
151 // correspond to the region of the bitmap to draw in the region defined | |
152 // by the dest coordinates. | |
153 // | |
154 // If the width or height of the source differs from that of the destination, | |
155 // the bitmap will be scaled. When scaling down, it is highly recommended | |
156 // that you call buildMipMap(false) on your bitmap to ensure that it has | |
157 // a mipmap, which will result in much higher-quality output. Set |filter| | |
158 // to use filtering for bitmaps, otherwise the nearest-neighbor algorithm | |
159 // is used for resampling. | |
160 // | |
161 // An optional custom SkPaint can be provided. | |
162 virtual void DrawBitmapInt(const SkBitmap& bitmap, | |
163 int src_x, int src_y, int src_w, int src_h, | |
164 int dest_x, int dest_y, int dest_w, int dest_h, | |
165 bool filter) = 0; | |
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, | |
170 const SkPaint& paint) = 0; | |
171 | |
172 // Draws text with the specified color, font and location. The text is | |
173 // aligned to the left, vertically centered, clipped to the region. If the | |
174 // text is too big, it is truncated and '...' is added to the end. | |
175 virtual void DrawStringInt(const string16& text, const | |
176 gfx::Font& font, | |
177 const SkColor& color, | |
178 int x, int y, int w, int h) = 0; | |
179 virtual void DrawStringInt(const string16& text, | |
180 const gfx::Font& font, | |
181 const SkColor& color, | |
182 const gfx::Rect& display_rect) = 0; | |
183 | |
184 // Draws text with the specified color, font and location. The last argument | |
185 // specifies flags for how the text should be rendered. It can be one of | |
186 // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT. | |
187 virtual void DrawStringInt(const string16& text, | |
188 const gfx::Font& font, | |
189 const SkColor& color, | |
190 int x, int y, int w, int h, | |
191 int flags) = 0; | |
192 | |
193 // Draws a dotted gray rectangle used for focus purposes. | |
194 virtual void DrawFocusRect(int x, int y, int width, int height) = 0; | |
195 | |
196 // Tiles the image in the specified region. | |
197 virtual void TileImageInt(const SkBitmap& bitmap, | |
198 int x, int y, int w, int h) = 0; | |
199 virtual void TileImageInt(const SkBitmap& bitmap, | |
200 int src_x, int src_y, | |
201 int dest_x, int dest_y, int w, int h) = 0; | |
202 | |
203 // Returns a native drawing context for platform specific drawing routines to | |
204 // use. Must be balanced by a call to EndPlatformPaint(). | |
205 virtual gfx::NativeDrawingContext BeginPlatformPaint() = 0; | |
206 | |
207 // Signifies the end of platform drawing using the native drawing context | |
208 // returned by BeginPlatformPaint(). | |
209 virtual void EndPlatformPaint() = 0; | |
210 | |
211 // TODO(beng): remove this once we don't need to use any skia-specific methods | |
212 // through this interface. | |
213 // A quick and dirty way to obtain the underlying SkCanvas. | |
214 virtual CanvasSkia* AsCanvasSkia(); | |
215 virtual const CanvasSkia* AsCanvasSkia() const; | |
216 }; | |
217 | |
218 class CanvasPaint { | |
219 public: | |
220 virtual ~CanvasPaint() {} | |
221 | |
222 // Creates a canvas that paints to |view| when it is destroyed. The canvas is | |
223 // sized to the client area of |view|. | |
224 static CanvasPaint* CreateCanvasPaint(gfx::NativeView view); | |
225 | |
226 // Returns true if the canvas has an invalid rect that needs to be repainted. | |
227 virtual bool IsValid() const = 0; | |
228 | |
229 // Returns the rectangle that is invalid. | |
230 virtual gfx::Rect GetInvalidRect() const = 0; | |
231 | |
232 // Returns the underlying Canvas. | |
233 virtual Canvas* AsCanvas() = 0; | |
234 }; | |
235 | |
236 } // namespace gfx; | |
237 | 11 |
238 #endif // GFX_CANVAS_H_ | 12 #endif // GFX_CANVAS_H_ |
OLD | NEW |