OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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. | |
4 | |
5 #ifndef APP_GFX_CANVAS_H_ | |
6 #define APP_GFX_CANVAS_H_ | |
7 | |
8 #if defined(OS_WIN) | |
9 #include <windows.h> | |
10 #endif | |
11 | |
12 #include <string> | |
13 | |
14 #include "base/basictypes.h" | |
15 #include "skia/ext/platform_canvas.h" | |
16 | |
17 #if defined(OS_POSIX) && !defined(OS_MACOSX) | |
18 typedef struct _cairo cairo_t; | |
19 typedef struct _GdkPixbuf GdkPixbuf; | |
20 #endif | |
21 | |
22 namespace gfx { | |
23 | |
24 class Font; | |
25 class Rect; | |
26 | |
27 // Canvas is a SkCanvas subclass that provides a number of methods for common | |
28 // operations used throughout an application built using base/gfx and app/gfx. | |
29 // | |
30 // All methods that take integer arguments (as is used throughout views) | |
31 // end with Int. If you need to use methods provided by the superclass | |
32 // you'll need to do a conversion. In particular you'll need to use | |
33 // macro SkIntToScalar(xxx), or if converting from a scalar to an integer | |
34 // SkScalarRound. | |
35 // | |
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 Canvas : public skia::PlatformCanvas { | |
42 public: | |
43 // Specifies the alignment for text rendered with the DrawStringInt method. | |
44 enum { | |
45 TEXT_ALIGN_LEFT = 1, | |
46 TEXT_ALIGN_CENTER = 2, | |
47 TEXT_ALIGN_RIGHT = 4, | |
48 TEXT_VALIGN_TOP = 8, | |
49 TEXT_VALIGN_MIDDLE = 16, | |
50 TEXT_VALIGN_BOTTOM = 32, | |
51 | |
52 // Specifies the text consists of multiple lines. | |
53 MULTI_LINE = 64, | |
54 | |
55 // By default DrawStringInt does not process the prefix ('&') character | |
56 // specially. That is, the string "&foo" is rendered as "&foo". When | |
57 // rendering text from a resource that uses the prefix character for | |
58 // mnemonics, the prefix should be processed and can be rendered as an | |
59 // underline (SHOW_PREFIX), or not rendered at all (HIDE_PREFIX). | |
60 SHOW_PREFIX = 128, | |
61 HIDE_PREFIX = 256, | |
62 | |
63 // Prevent ellipsizing | |
64 NO_ELLIPSIS = 512, | |
65 | |
66 // Specifies if words can be split by new lines. | |
67 // This only works with MULTI_LINE. | |
68 CHARACTER_BREAK = 1024, | |
69 | |
70 // Instructs DrawStringInt() to render the text using RTL directionality. | |
71 // In most cases, passing this flag is not necessary because information | |
72 // about the text directionality is going to be embedded within the string | |
73 // in the form of special Unicode characters. However, we don't insert | |
74 // directionality characters into strings if the locale is LTR because some | |
75 // platforms (for example, an English Windows XP with no RTL fonts | |
76 // installed) don't support these characters. Thus, this flag should be | |
77 // used to render text using RTL directionality when the locale is LTR. | |
78 FORCE_RTL_DIRECTIONALITY = 2048, | |
79 }; | |
80 | |
81 // Creates an empty Canvas. Callers must use initialize before using the | |
82 // canvas. | |
83 Canvas(); | |
84 | |
85 Canvas(int width, int height, bool is_opaque); | |
86 | |
87 virtual ~Canvas(); | |
88 | |
89 // Retrieves the clip rectangle and sets it in the specified rectangle if any. | |
90 // Returns true if the clip rect is non-empty. | |
91 bool GetClipRect(gfx::Rect* clip_rect); | |
92 | |
93 // Wrapper function that takes integer arguments. | |
94 // Returns true if the clip is non-empty. | |
95 // See clipRect for specifics. | |
96 bool ClipRectInt(int x, int y, int w, int h); | |
97 | |
98 // Test whether the provided rectangle intersects the current clip rect. | |
99 bool IntersectsClipRectInt(int x, int y, int w, int h); | |
100 | |
101 // Wrapper function that takes integer arguments. | |
102 // See translate() for specifics. | |
103 void TranslateInt(int x, int y); | |
104 | |
105 // Wrapper function that takes integer arguments. | |
106 // See scale() for specifics. | |
107 void ScaleInt(int x, int y); | |
108 | |
109 // Fills the given rectangle with the given paint's parameters. | |
110 void FillRectInt(int x, int y, int w, int h, const SkPaint& paint); | |
111 | |
112 // Fills the specified region with the specified color using a transfer | |
113 // mode of SkXfermode::kSrcOver_Mode. | |
114 void FillRectInt(const SkColor& color, int x, int y, int w, int h); | |
115 | |
116 // Draws a single pixel rect in the specified region with the specified | |
117 // color, using a transfer mode of SkXfermode::kSrcOver_Mode. | |
118 // | |
119 // NOTE: if you need a single pixel line, use DraLineInt. | |
120 void DrawRectInt(const SkColor& color, int x, int y, int w, int h); | |
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 DraLineInt. | |
126 void DrawRectInt(const SkColor& color, int x, int y, int w, int h, | |
127 SkXfermode::Mode mode); | |
128 | |
129 // Draws a single pixel line with the specified color. | |
130 void DrawLineInt(const SkColor& color, int x1, int y1, int x2, int y2); | |
131 | |
132 // Draws a bitmap with the origin at the specified location. The upper left | |
133 // corner of the bitmap is rendered at the specified location. | |
134 void DrawBitmapInt(const SkBitmap& bitmap, int x, int y); | |
135 | |
136 // Draws a bitmap with the origin at the specified location, using the | |
137 // specified paint. The upper left corner of the bitmap is rendered at the | |
138 // specified location. | |
139 void DrawBitmapInt(const SkBitmap& bitmap, int x, int y, | |
140 const SkPaint& paint); | |
141 | |
142 // Draws a portion of a bitmap in the specified location. The src parameters | |
143 // correspond to the region of the bitmap to draw in the region defined | |
144 // by the dest coordinates. | |
145 // | |
146 // If the width or height of the source differs from that of the destination, | |
147 // the bitmap will be scaled. When scaling down, it is highly recommended | |
148 // that you call buildMipMap(false) on your bitmap to ensure that it has | |
149 // a mipmap, which will result in much higher-quality output. Set |filter| | |
150 // to use filtering for bitmaps, otherwise the nearest-neighbor algorithm | |
151 // is used for resampling. | |
152 // | |
153 // An optional custom SkPaint can be provided. | |
154 void DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, int src_w, | |
155 int src_h, int dest_x, int dest_y, int dest_w, int dest_h, | |
156 bool filter); | |
157 void DrawBitmapInt(const SkBitmap& bitmap, int src_x, int src_y, int src_w, | |
158 int src_h, int dest_x, int dest_y, int dest_w, int dest_h, | |
159 bool filter, const SkPaint& paint); | |
160 | |
161 // Draws text with the specified color, font and location. The text is | |
162 // aligned to the left, vertically centered, clipped to the region. If the | |
163 // text is too big, it is truncated and '...' is added to the end. | |
164 void DrawStringInt(const std::wstring& text, const gfx::Font& font, | |
165 const SkColor& color, int x, int y, int w, int h); | |
166 void DrawStringInt(const std::wstring& text, const gfx::Font& font, | |
167 const SkColor& color, const gfx::Rect& display_rect); | |
168 | |
169 // Draws text with the specified color, font and location. The last argument | |
170 // specifies flags for how the text should be rendered. It can be one of | |
171 // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT. | |
172 void DrawStringInt(const std::wstring& text, const gfx::Font& font, | |
173 const SkColor& color, int x, int y, int w, int h, | |
174 int flags); | |
175 | |
176 #ifdef OS_WIN // Only implemented on Windows for now. | |
177 // Draws text with a 1-pixel halo around it of the given color. It allows | |
178 // ClearType to be drawn to an otherwise transparenct bitmap for drag images. | |
179 // Drag images have only 1-bit of transparency, so we don't do any fancy | |
180 // blurring. | |
181 void DrawStringWithHalo(const std::wstring& text, const gfx::Font& font, | |
182 const SkColor& text_color, const SkColor& halo_color, | |
183 int x, int y, int w, int h, int flags); | |
184 #endif | |
185 | |
186 // Draws a dotted gray rectangle used for focus purposes. | |
187 void DrawFocusRect(int x, int y, int width, int height); | |
188 | |
189 // Tiles the image in the specified region. | |
190 void TileImageInt(const SkBitmap& bitmap, int x, int y, int w, int h); | |
191 void TileImageInt(const SkBitmap& bitmap, int src_x, int src_y, | |
192 int dest_x, int dest_y, int w, int h); | |
193 | |
194 // Extracts a bitmap from the contents of this canvas. | |
195 SkBitmap ExtractBitmap() const; | |
196 | |
197 #if defined(OS_POSIX) && !defined(OS_MACOSX) | |
198 // Applies current matrix on the canvas to the cairo context. This should be | |
199 // invoked anytime you plan on drawing directly to the cairo context. Be | |
200 // sure and set the matrix back to the identity when done. | |
201 void ApplySkiaMatrixToCairoContext(cairo_t* cr); | |
202 | |
203 // Draw the pixbuf in its natural size at (x, y). | |
204 void DrawGdkPixbuf(GdkPixbuf* pixbuf, int x, int y); | |
205 #endif | |
206 | |
207 // Compute the size required to draw some text with the provided font. | |
208 // Attempts to fit the text with the provided width and height. Increases | |
209 // height and then width as needed to make the text fit. This method | |
210 // supports multiple lines. | |
211 static void SizeStringInt(const std::wstring& test, const gfx::Font& font, | |
212 int *width, int* height, int flags); | |
213 | |
214 // Returns the default text alignment to be used when drawing text on a | |
215 // gfx::Canvas based on the directionality of the system locale language. This | |
216 // function is used by gfx::Canvas::DrawStringInt when the text alignment is | |
217 // not specified. | |
218 // | |
219 // This function returns either gfx::Canvas::TEXT_ALIGN_LEFT or | |
220 // gfx::Canvas::TEXT_ALIGN_RIGHT. | |
221 static int DefaultCanvasTextAlignment(); | |
222 | |
223 private: | |
224 #if defined(OS_WIN) | |
225 // Draws text with the specified color, font and location. The text is | |
226 // aligned to the left, vertically centered, clipped to the region. If the | |
227 // text is too big, it is truncated and '...' is added to the end. | |
228 void DrawStringInt(const std::wstring& text, HFONT font, | |
229 const SkColor& color, int x, int y, int w, int h, | |
230 int flags); | |
231 #endif | |
232 | |
233 DISALLOW_COPY_AND_ASSIGN(Canvas); | |
234 }; | |
235 | |
236 } // namespace gfx; | |
237 | |
238 #endif // APP_GFX_CANVAS_H_ | |
OLD | NEW |