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