1 /* 2 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #ifndef GraphicsContext_h 27 #define GraphicsContext_h 28 29 #include "DashArray.h" 30 #include "FloatRect.h" 31 #include "Image.h" 32 #include "IntRect.h" 33 #include "Path.h" 34 #include "TextDirection.h" 35 #include <wtf/Noncopyable.h> 36 #include <wtf/Platform.h> 37 38 #if PLATFORM(CG) 39 typedef struct CGContext PlatformGraphicsContext; 40 #elif PLATFORM(CAIRO) 41 typedef struct _cairo PlatformGraphicsContext; 42 #elif PLATFORM(QT) 43 QT_BEGIN_NAMESPACE 44 class QPainter; 45 QT_END_NAMESPACE 46 typedef QPainter PlatformGraphicsContext; 47 #elif PLATFORM(SGL) 48 namespace WebCore { 49 class PlatformGraphicsContext; 50 } 51 class SkPaint; 52 struct SkPoint; 53 #elif PLATFORM(WX) 54 class wxGCDC; 55 class wxWindowDC; 56 57 // wxGraphicsContext allows us to support Path, etc. 58 // but on some platforms, e.g. Linux, it requires fairly 59 // new software. 60 #if USE(WXGC) 61 // On OS X, wxGCDC is just a typedef for wxDC, so use wxDC explicitly to make 62 // the linker happy. 63 #ifdef __APPLE__ 64 class wxDC; 65 typedef wxDC PlatformGraphicsContext; 66 #else 67 typedef wxGCDC PlatformGraphicsContext; 68 #endif 69 #else 70 typedef wxWindowDC PlatformGraphicsContext; 71 #endif 72 #elif PLATFORM(SKIA) 73 typedef class PlatformContextSkia PlatformGraphicsContext; 74 #else 75 typedef void PlatformGraphicsContext; 76 #endif 77 78 #if PLATFORM(GTK) 79 typedef struct _GdkDrawable GdkDrawable; 80 typedef struct _GdkEventExpose GdkEventExpose; 81 #endif 82 83 #if PLATFORM(WIN) 84 typedef struct HDC__* HDC; 85 #if !PLATFORM(CG) 86 // UInt8 is defined in CoreFoundation/CFBase.h 87 typedef unsigned char UInt8; 88 #endif 89 #endif 90 91 #if PLATFORM(QT) && defined(Q_WS_WIN) 92 #include <windows.h> 93 #endif 94 95 namespace WebCore { 96 97 const int cMisspellingLineThickness = 3; 98 const int cMisspellingLinePatternWidth = 4; 99 const int cMisspellingLinePatternGapWidth = 1; 100 101 class TransformationMatrix; 102 class Font; 103 class Generator; 104 class Gradient; 105 class GraphicsContextPrivate; 106 class GraphicsContextPlatformPrivate; 107 class ImageBuffer; 108 class KURL; 109 class Path; 110 class Pattern; 111 class TextRun; 112 113 // These bits can be ORed together for a total of 8 possible text drawing modes. 114 const int cTextInvisible = 0; 115 const int cTextFill = 1; 116 const int cTextStroke = 2; 117 const int cTextClip = 4; 118 119 enum StrokeStyle { 120 NoStroke, 121 SolidStroke, 122 DottedStroke, 123 DashedStroke 124 }; 125 126 enum InterpolationQuality { 127 InterpolationDefault, 128 InterpolationNone, 129 InterpolationLow, 130 InterpolationMedium, 131 InterpolationHigh 132 }; 133 134 // FIXME: Currently these constants have to match the values used in the SVG 135 // DOM API. That's a mistake. We need to make cut that dependency. 136 enum GradientSpreadMethod { 137 SpreadMethodPad = 1, 138 SpreadMethodReflect = 2, 139 SpreadMethodRepeat = 3 140 }; 141 142 class GraphicsContext : Noncopyable { 143 public: 144 GraphicsContext(PlatformGraphicsContext*); 145 ~GraphicsContext(); 146 147 PlatformGraphicsContext* platformContext() const; 148 149 float strokeThickness() const; 150 void setStrokeThickness(float); 151 StrokeStyle strokeStyle() const; 152 void setStrokeStyle(const StrokeStyle& style); 153 Color strokeColor() const; 154 void setStrokeColor(const Color&); 155 void setStrokePattern(PassRefPtr<Pattern>); 156 void setStrokeGradient(PassRefPtr<Gradient>); 157 158 WindRule fillRule() const; 159 void setFillRule(WindRule); 160 GradientSpreadMethod spreadMethod() const; 161 void setSpreadMethod(GradientSpreadMethod); 162 Color fillColor() const; 163 void setFillColor(const Color&); 164 void setFillPattern(PassRefPtr<Pattern>); 165 void setFillGradient(PassRefPtr<Gradient>); 166 void setShadowsIgnoreTransforms(bool); 167 168 void setShouldAntialias(bool); 169 bool shouldAntialias() const; 170 171 #if PLATFORM(CG) 172 void applyStrokePattern(); 173 void applyFillPattern(); 174 #endif 175 176 #if PLATFORM(SGL) 177 /* these should be pused to apple. needed for CanvasStyle.cpp */ 178 void setCMYKAFillColor(float c, float m, float y, float k, float a); 179 void setCMYKAStrokeColor(float c, float m, float y, float k, float a); 180 181 // initialize a paint for filling 182 void setupFillPaint(SkPaint*); 183 // initialize a paint for stroking 184 void setupStrokePaint(SkPaint*); 185 // initialize a paint for a shadow, or if false is returned, the 186 // parameters are left untouched 187 bool setupShadowPaint(SkPaint* paint, SkPoint* offset); 188 // returns true if there is a valid (non-transparent) fill color 189 bool willFill() const; 190 // returns true if there is a valid (non-transparent) stroke color 191 bool willStroke() const; 192 193 // may return NULL, since we lazily allocate the path. This is the path 194 // that is drawn by drawPath() 195 const SkPath* getCurrPath() const; 196 197 /** platform-specific factory method to return a bitmap graphicscontext, 198 called by <canvas> when we need to draw offscreen. Caller is responsible for 199 deleting the context. Use drawOffscreenContext() to draw the context's image 200 onto another graphics context. 201 */ 202 static GraphicsContext* createOffscreenContext(int width, int height); 203 #endif 204 205 void save(); 206 void restore(); 207 208 // These draw methods will do both stroking and filling. 209 void drawRect(const IntRect&); 210 void drawLine(const IntPoint&, const IntPoint&); 211 void drawEllipse(const IntRect&); 212 void drawConvexPolygon(size_t numPoints, const FloatPoint*, bool shouldAntialias = false); 213 214 void drawPath(); 215 void fillPath(); 216 void strokePath(); 217 218 // Arc drawing (used by border-radius in CSS) just supports stroking at the moment. 219 void strokeArc(const IntRect&, int startAngle, int angleSpan); 220 221 void fillRect(const FloatRect&); 222 void fillRect(const FloatRect&, const Color&); 223 void fillRect(const FloatRect&, Generator&); 224 void fillRoundedRect(const IntRect&, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color&); 225 226 void clearRect(const FloatRect&); 227 228 void strokeRect(const FloatRect&); 229 void strokeRect(const FloatRect&, float lineWidth); 230 231 void drawImage(Image*, const IntPoint&, CompositeOperator = CompositeSourceOver); 232 void drawImage(Image*, const IntRect&, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false); 233 void drawImage(Image*, const IntPoint& destPoint, const IntRect& srcRect, CompositeOperator = CompositeSourceOver); 234 void drawImage(Image*, const IntRect& destRect, const IntRect& srcRect, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false); 235 void drawImage(Image*, const FloatRect& destRect, const FloatRect& srcRect = FloatRect(0, 0, -1, -1), 236 CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false); 237 void drawTiledImage(Image*, const IntRect& destRect, const IntPoint& srcPoint, const IntSize& tileSize, 238 CompositeOperator = CompositeSourceOver); 239 void drawTiledImage(Image*, const IntRect& destRect, const IntRect& srcRect, 240 Image::TileRule hRule = Image::StretchTile, Image::TileRule vRule = Image::StretchTile, 241 CompositeOperator = CompositeSourceOver); 242 243 void setImageInterpolationQuality(InterpolationQuality); 244 InterpolationQuality imageInterpolationQuality() const; 245 246 void clip(const FloatRect&); 247 void addRoundedRectClip(const IntRect&, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight); 248 void addInnerRoundedRectClip(const IntRect&, int thickness); 249 void clipOut(const IntRect&); 250 void clipOutEllipseInRect(const IntRect&); 251 void clipOutRoundedRect(const IntRect&, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight); 252 void clipPath(WindRule); 253 void clipToImageBuffer(const FloatRect&, const ImageBuffer*); 254 255 int textDrawingMode(); 256 void setTextDrawingMode(int); 257 258 void drawText(const Font&, const TextRun&, const IntPoint&, int from = 0, int to = -1); 259 void drawBidiText(const Font&, const TextRun&, const FloatPoint&); 260 void drawHighlightForText(const Font&, const TextRun&, const IntPoint&, int h, const Color& backgroundColor, int from = 0, int to = -1); 261 262 FloatRect roundToDevicePixels(const FloatRect&); 263 264 void drawLineForText(const IntPoint&, int width, bool printing); 265 void drawLineForMisspellingOrBadGrammar(const IntPoint&, int width, bool grammar); 266 267 bool paintingDisabled() const; 268 void setPaintingDisabled(bool); 269 270 bool updatingControlTints() const; 271 void setUpdatingControlTints(bool); 272 273 void beginTransparencyLayer(float opacity); 274 void endTransparencyLayer(); 275 276 void setShadow(const IntSize&, int blur, const Color&); 277 bool getShadow(IntSize&, int&, Color&) const; 278 void clearShadow(); 279 280 void initFocusRing(int width, int offset); 281 void addFocusRingRect(const IntRect&); 282 void drawFocusRing(const Color&); 283 void clearFocusRing(); 284 IntRect focusRingBoundingRect(); 285 286 void setLineCap(LineCap); 287 void setLineDash(const DashArray&, float dashOffset); 288 void setLineJoin(LineJoin); 289 void setMiterLimit(float); 290 291 void setAlpha(float); 292 #if PLATFORM(CAIRO) 293 float getAlpha(); 294 #endif 295 296 void setCompositeOperation(CompositeOperator); 297 298 void beginPath(); 299 void addPath(const Path&); 300 301 void clip(const Path&); 302 void clipOut(const Path&); 303 304 void scale(const FloatSize&); 305 void rotate(float angleInRadians); 306 void translate(float x, float y); 307 IntPoint origin(); 308 309 void setURLForRect(const KURL&, const IntRect&); 310 311 void concatCTM(const TransformationMatrix&); 312 TransformationMatrix getCTM() const; 313 314 #if PLATFORM(WIN) 315 GraphicsContext(HDC, bool hasAlpha = false); // FIXME: To be removed. 316 bool inTransparencyLayer() const; 317 HDC getWindowsContext(const IntRect&, bool supportAlphaBlend = true, bool mayCreateBitmap = true); // The passed in rect is used to create a bitmap for compositing inside transparency layers. 318 void releaseWindowsContext(HDC, const IntRect&, bool supportAlphaBlend = true, bool mayCreateBitmap = true); // The passed in HDC should be the one handed back by getWindowsContext. 319 320 // When set to true, child windows should be rendered into this context 321 // rather than allowing them just to render to the screen. Defaults to 322 // false. 323 // FIXME: This is a layering violation. GraphicsContext shouldn't know 324 // what a "window" is. It would be much more appropriate for this flag 325 // to be passed as a parameter alongside the GraphicsContext, but doing 326 // that would require lots of changes in cross-platform code that we 327 // aren't sure we want to make. 328 void setShouldIncludeChildWindows(bool); 329 bool shouldIncludeChildWindows() const; 330 331 class WindowsBitmap : public Noncopyable { 332 public: 333 WindowsBitmap(HDC, IntSize); 334 ~WindowsBitmap(); 335 hdc()336 HDC hdc() const { return m_hdc; } buffer()337 UInt8* buffer() const { return m_bitmapBuffer; } bufferLength()338 unsigned bufferLength() const { return m_bitmapBufferLength; } size()339 IntSize size() const { return m_size; } bytesPerRow()340 unsigned bytesPerRow() const { return m_bytesPerRow; } 341 342 private: 343 HDC m_hdc; 344 HBITMAP m_bitmap; 345 UInt8* m_bitmapBuffer; 346 unsigned m_bitmapBufferLength; 347 IntSize m_size; 348 unsigned m_bytesPerRow; 349 }; 350 351 WindowsBitmap* createWindowsBitmap(IntSize); 352 // The bitmap should be non-premultiplied. 353 void drawWindowsBitmap(WindowsBitmap*, const IntPoint&); 354 #endif 355 356 #if PLATFORM(QT) && defined(Q_WS_WIN) 357 HDC getWindowsContext(const IntRect&, bool supportAlphaBlend = true, bool mayCreateBitmap = true); 358 void releaseWindowsContext(HDC, const IntRect&, bool supportAlphaBlend = true, bool mayCreateBitmap = true); 359 #endif 360 361 #if PLATFORM(QT) 362 bool inTransparencyLayer() const; 363 PlatformPath* currentPath(); 364 QPen pen(); 365 #endif 366 367 #if PLATFORM(GTK) 368 void setGdkExposeEvent(GdkEventExpose*); 369 GdkDrawable* gdkDrawable() const; 370 GdkEventExpose* gdkExposeEvent() const; 371 #endif 372 373 private: 374 void savePlatformState(); 375 void restorePlatformState(); 376 377 void setPlatformTextDrawingMode(int); 378 void setPlatformFont(const Font& font); 379 380 void setPlatformStrokeColor(const Color&); 381 void setPlatformStrokeStyle(const StrokeStyle&); 382 void setPlatformStrokeThickness(float); 383 384 void setPlatformFillColor(const Color&); 385 386 void setPlatformShouldAntialias(bool b); 387 388 void setPlatformShadow(const IntSize&, int blur, const Color&); 389 void clearPlatformShadow(); 390 391 int focusRingWidth() const; 392 int focusRingOffset() const; 393 const Vector<IntRect>& focusRingRects() const; 394 395 static GraphicsContextPrivate* createGraphicsContextPrivate(); 396 static void destroyGraphicsContextPrivate(GraphicsContextPrivate*); 397 398 GraphicsContextPrivate* m_common; 399 GraphicsContextPlatformPrivate* m_data; // Deprecated; m_commmon can just be downcasted. To be removed. 400 }; 401 402 } // namespace WebCore 403 404 #endif // GraphicsContext_h 405