| 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> | 9 #include "ui/gfx/canvas_skia.h" |
| 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" | |
| 16 #include "ui/gfx/native_widget_types.h" | |
| 17 | |
| 18 class SkCanvas; | |
| 19 | |
| 20 namespace ui { | |
| 21 class Transform; | |
| 22 } | |
| 23 | |
| 24 namespace gfx { | |
| 25 | |
| 26 class Brush; | |
| 27 class CanvasSkia; | |
| 28 class Font; | |
| 29 class Point; | |
| 30 class Rect; | |
| 31 class Size; | |
| 32 | |
| 33 // TODO(beng): documentation. | |
| 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 | |
| 45 // Specifies the text consists of multiple lines. | |
| 46 MULTI_LINE = 64, | |
| 47 | |
| 48 // By default DrawStringInt does not process the prefix ('&') character | |
| 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 // Creates an empty canvas. Must be initialized before it can be used. | |
| 81 static Canvas* CreateCanvas(); | |
| 82 | |
| 83 // Creates a canvas with the specified size. | |
| 84 static Canvas* CreateCanvas(const gfx::Size& size, bool is_opaque); | |
| 85 | |
| 86 // Saves a copy of the drawing state onto a stack, operating on this copy | |
| 87 // until a balanced call to Restore() is made. | |
| 88 virtual void Save() = 0; | |
| 89 | |
| 90 // As with Save(), except draws to a layer that is blended with the canvas | |
| 91 // at the specified alpha once Restore() is called. | |
| 92 // |layer_bounds| are the bounds of the layer relative to the current | |
| 93 // transform. | |
| 94 virtual void SaveLayerAlpha(uint8 alpha) = 0; | |
| 95 virtual void SaveLayerAlpha(uint8 alpha, const gfx::Rect& layer_bounds) = 0; | |
| 96 | |
| 97 // Restores the drawing state after a call to Save*(). It is an error to | |
| 98 // call Restore() more times than Save*(). | |
| 99 virtual void Restore() = 0; | |
| 100 | |
| 101 // Returns true if the clip is non-empty. | |
| 102 virtual bool ClipRect(const gfx::Rect& rect) = 0; | |
| 103 | |
| 104 virtual void Translate(const gfx::Point& point) = 0; | |
| 105 | |
| 106 virtual void Scale(int x_scale, int y_scale) = 0; | |
| 107 | |
| 108 // Fills |rect| with |color| using a transfer mode of | |
| 109 // SkXfermode::kSrcOver_Mode. | |
| 110 virtual void FillRect(const gfx::Rect& rect, const SkColor& color) = 0; | |
| 111 | |
| 112 // Fills |rect| with the specified |color| and |mode|. | |
| 113 virtual void FillRect(const gfx::Rect& rect, | |
| 114 const SkColor& color, | |
| 115 SkXfermode::Mode mode) = 0; | |
| 116 | |
| 117 // Fills |rect| with the specified |brush|. | |
| 118 virtual void FillRect(const gfx::Rect& rect, const gfx::Brush* brush) = 0; | |
| 119 | |
| 120 // Draws a single pixel rect in the specified region with the specified | |
| 121 // color, using a transfer mode of SkXfermode::kSrcOver_Mode. | |
| 122 // | |
| 123 // NOTE: if you need a single pixel line, use DrawLine. | |
| 124 virtual void DrawRect(const gfx::Rect& rect, const SkColor& color) = 0; | |
| 125 | |
| 126 // Draws a single pixel rect in the specified region with the specified | |
| 127 // color and transfer mode. | |
| 128 // | |
| 129 // NOTE: if you need a single pixel line, use DrawLine. | |
| 130 virtual void DrawRect(const gfx::Rect& rect, | |
| 131 const SkColor& color, | |
| 132 SkXfermode::Mode mode) = 0; | |
| 133 | |
| 134 // Draws the given rectangle with the given paint's parameters. | |
| 135 virtual void DrawRect(const gfx::Rect& rect, const SkPaint& paint) = 0; | |
| 136 | |
| 137 // Draws a single pixel line with the specified color. | |
| 138 virtual void DrawLine(const gfx::Point& p1, | |
| 139 const gfx::Point& p2, | |
| 140 const SkColor& color) = 0; | |
| 141 | |
| 142 // Draws a bitmap with the origin at the specified location. The upper left | |
| 143 // corner of the bitmap is rendered at the specified location. | |
| 144 virtual void DrawBitmapInt(const SkBitmap& bitmap, int x, int y) = 0; | |
| 145 | |
| 146 // Draws a bitmap with the origin at the specified location, using the | |
| 147 // specified paint. The upper left corner of the bitmap is rendered at the | |
| 148 // specified location. | |
| 149 virtual void DrawBitmapInt(const SkBitmap& bitmap, | |
| 150 int x, int y, | |
| 151 const SkPaint& paint) = 0; | |
| 152 | |
| 153 // Draws a portion of a bitmap in the specified location. The src parameters | |
| 154 // correspond to the region of the bitmap to draw in the region defined | |
| 155 // by the dest coordinates. | |
| 156 // | |
| 157 // If the width or height of the source differs from that of the destination, | |
| 158 // the bitmap will be scaled. When scaling down, it is highly recommended | |
| 159 // that you call buildMipMap(false) on your bitmap to ensure that it has | |
| 160 // a mipmap, which will result in much higher-quality output. Set |filter| | |
| 161 // to use filtering for bitmaps, otherwise the nearest-neighbor algorithm | |
| 162 // is used for resampling. | |
| 163 // | |
| 164 // An optional custom SkPaint can be provided. | |
| 165 virtual void DrawBitmapInt(const SkBitmap& bitmap, | |
| 166 int src_x, int src_y, int src_w, int src_h, | |
| 167 int dest_x, int dest_y, int dest_w, int dest_h, | |
| 168 bool filter) = 0; | |
| 169 virtual void DrawBitmapInt(const SkBitmap& bitmap, | |
| 170 int src_x, int src_y, int src_w, int src_h, | |
| 171 int dest_x, int dest_y, int dest_w, int dest_h, | |
| 172 bool filter, | |
| 173 const SkPaint& paint) = 0; | |
| 174 | |
| 175 // Draws text with the specified color, font and location. The text is | |
| 176 // aligned to the left, vertically centered, clipped to the region. If the | |
| 177 // text is too big, it is truncated and '...' is added to the end. | |
| 178 virtual void DrawStringInt(const string16& text, | |
| 179 const gfx::Font& font, | |
| 180 const SkColor& color, | |
| 181 int x, int y, int w, int h) = 0; | |
| 182 virtual void DrawStringInt(const string16& text, | |
| 183 const gfx::Font& font, | |
| 184 const SkColor& color, | |
| 185 const gfx::Rect& display_rect) = 0; | |
| 186 | |
| 187 // Draws text with the specified color, font and location. The last argument | |
| 188 // specifies flags for how the text should be rendered. It can be one of | |
| 189 // TEXT_ALIGN_CENTER, TEXT_ALIGN_RIGHT or TEXT_ALIGN_LEFT. | |
| 190 virtual void DrawStringInt(const string16& text, | |
| 191 const gfx::Font& font, | |
| 192 const SkColor& color, | |
| 193 int x, int y, int w, int h, | |
| 194 int flags) = 0; | |
| 195 | |
| 196 // Draws a dotted gray rectangle used for focus purposes. | |
| 197 virtual void DrawFocusRect(const gfx::Rect& rect) = 0; | |
| 198 | |
| 199 // Tiles the image in the specified region. | |
| 200 virtual void TileImageInt(const SkBitmap& bitmap, | |
| 201 int x, int y, int w, int h) = 0; | |
| 202 virtual void TileImageInt(const SkBitmap& bitmap, | |
| 203 int src_x, int src_y, | |
| 204 int dest_x, int dest_y, int w, int h) = 0; | |
| 205 | |
| 206 // Returns a native drawing context for platform specific drawing routines to | |
| 207 // use. Must be balanced by a call to EndPlatformPaint(). | |
| 208 virtual gfx::NativeDrawingContext BeginPlatformPaint() = 0; | |
| 209 | |
| 210 // Signifies the end of platform drawing using the native drawing context | |
| 211 // returned by BeginPlatformPaint(). | |
| 212 virtual void EndPlatformPaint() = 0; | |
| 213 | |
| 214 // Apply transformation on the canvas. | |
| 215 virtual void Transform(const ui::Transform& transform) = 0; | |
| 216 | |
| 217 // TODO(beng): remove this once we don't need to use any skia-specific methods | |
| 218 // through this interface. | |
| 219 // A quick and dirty way to obtain the underlying SkCanvas. | |
| 220 virtual CanvasSkia* AsCanvasSkia(); | |
| 221 virtual const CanvasSkia* AsCanvasSkia() const; | |
| 222 virtual SkCanvas* GetSkCanvas(); | |
| 223 virtual const SkCanvas* GetSkCanvas() const; | |
| 224 }; | |
| 225 | |
| 226 } // namespace gfx | |
| 227 | 10 |
| 228 #endif // UI_GFX_CANVAS_H_ | 11 #endif // UI_GFX_CANVAS_H_ |
| OLD | NEW |