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