2 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #ifndef GraphicsContext_h
27 #define GraphicsContext_h
29 #include "DashArray.h"
30 #include "FloatRect.h"
34 #include "TextDirection.h"
35 #include <wtf/Noncopyable.h>
36 #include <wtf/Platform.h>
39 typedef struct CGContext PlatformGraphicsContext
;
41 typedef struct _cairo PlatformGraphicsContext
;
46 typedef QPainter PlatformGraphicsContext
;
51 // wxGraphicsContext allows us to support Path, etc.
52 // but on some platforms, e.g. Linux, it requires fairly
55 // On OS X, wxGCDC is just a typedef for wxDC, so use wxDC explicitly to make
59 typedef wxDC PlatformGraphicsContext
;
61 typedef wxGCDC PlatformGraphicsContext
;
64 typedef wxWindowDC PlatformGraphicsContext
;
67 typedef class PlatformContextSkia PlatformGraphicsContext
;
69 typedef void PlatformGraphicsContext
;
73 typedef struct _GdkDrawable GdkDrawable
;
74 typedef struct _GdkEventExpose GdkEventExpose
;
78 typedef struct HDC__
* HDC
;
80 // UInt8 is defined in CoreFoundation/CFBase.h
81 typedef unsigned char UInt8
;
85 #if PLATFORM(QT) && defined(Q_WS_WIN)
91 const int cMisspellingLineThickness
= 3;
92 const int cMisspellingLinePatternWidth
= 4;
93 const int cMisspellingLinePatternGapWidth
= 1;
95 class TransformationMatrix
;
99 class GraphicsContextPrivate
;
100 class GraphicsContextPlatformPrivate
;
108 // These bits can be ORed together for a total of 8 possible text drawing modes.
109 const int cTextInvisible
= 0;
110 const int cTextFill
= 1;
111 const int cTextStroke
= 2;
112 const int cTextClip
= 4;
121 enum InterpolationQuality
{
122 InterpolationDefault
,
129 // FIXME: Currently these constants have to match the values used in the SVG
130 // DOM API. That's a mistake. We need to make cut that dependency.
131 enum GradientSpreadMethod
{
133 SpreadMethodReflect
= 2,
134 SpreadMethodRepeat
= 3
137 struct IPhoneGradient
141 IPhoneGradient(float* aStart
= NULL
, float* anEnd
= NULL
) : start(aStart
), end(anEnd
) { }
144 typedef IPhoneGradient
* IPhoneGradientRef
;
149 ExponentialInterpolation
152 class GraphicsContext
: Noncopyable
{
154 GraphicsContext(PlatformGraphicsContext
*, bool setContextColors
= true);
157 PlatformGraphicsContext
* platformContext() const;
159 float strokeThickness() const;
160 void setStrokeThickness(float);
161 StrokeStyle
strokeStyle() const;
162 void setStrokeStyle(const StrokeStyle
& style
);
163 Color
strokeColor() const;
164 void setStrokeColor(const Color
&);
165 void setStrokePattern(PassRefPtr
<Pattern
>);
166 void setStrokeGradient(PassRefPtr
<Gradient
>);
168 WindRule
fillRule() const;
169 void setFillRule(WindRule
);
170 GradientSpreadMethod
spreadMethod() const;
171 void setSpreadMethod(GradientSpreadMethod
);
172 Color
fillColor() const;
173 void setFillColor(const Color
&);
174 void setFillPattern(PassRefPtr
<Pattern
>);
175 void setFillGradient(PassRefPtr
<Gradient
>);
176 void setShadowsIgnoreTransforms(bool);
178 void setShouldAntialias(bool);
179 bool shouldAntialias() const;
182 void applyStrokePattern();
183 void applyFillPattern();
189 // These draw methods will do both stroking and filling.
190 void drawRect(const IntRect
&);
191 void drawLine(const IntPoint
&, const IntPoint
&, bool antialias
= false);
193 void drawJoinedLines(CGPoint points
[], unsigned count
, bool antialias
, CGLineCap lineCap
= kCGLineCapButt
);
195 void drawEllipse(const IntRect
&);
196 void drawEllipse(const FloatRect
&);
197 void drawRaisedEllipse(const FloatRect
&, Color ellipseColor
, Color shadowColor
);
198 void drawConvexPolygon(size_t numPoints
, const FloatPoint
*, bool shouldAntialias
= false);
204 // Arc drawing (used by border-radius in CSS) just supports stroking at the moment.
205 void strokeArc(const IntRect
&, int startAngle
, int angleSpan
);
207 void fillRect(const FloatRect
&);
208 void fillRect(const FloatRect
&, const Color
&);
209 void fillRect(const FloatRect
&, Generator
&);
210 void fillRoundedRect(const IntRect
&, const IntSize
& topLeft
, const IntSize
& topRight
, const IntSize
& bottomLeft
, const IntSize
& bottomRight
, const Color
&);
212 void clearRect(const FloatRect
&);
214 void strokeRect(const FloatRect
&);
215 void strokeRect(const FloatRect
&, float lineWidth
);
217 void drawImage(Image
*, const IntPoint
&, CompositeOperator
= CompositeSourceOver
);
218 void drawImage(Image
*, const IntRect
&, CompositeOperator
= CompositeSourceOver
, bool useLowQualityScale
= false);
219 void drawImage(Image
*, const IntPoint
& destPoint
, const IntRect
& srcRect
, CompositeOperator
= CompositeSourceOver
);
220 void drawImage(Image
*, const IntRect
& destRect
, const IntRect
& srcRect
, CompositeOperator
= CompositeSourceOver
, bool useLowQualityScale
= false);
221 void drawImage(Image
*, const FloatRect
& destRect
, const FloatRect
& srcRect
= FloatRect(0, 0, -1, -1),
222 CompositeOperator
= CompositeSourceOver
, bool useLowQualityScale
= false);
223 void drawTiledImage(Image
*, const IntRect
& destRect
, const IntPoint
& srcPoint
, const IntSize
& tileSize
,
224 CompositeOperator
= CompositeSourceOver
);
225 void drawTiledImage(Image
*, const IntRect
& destRect
, const IntRect
& srcRect
,
226 Image::TileRule hRule
= Image::StretchTile
, Image::TileRule vRule
= Image::StretchTile
,
227 CompositeOperator
= CompositeSourceOver
);
229 void setImageInterpolationQuality(InterpolationQuality
);
230 InterpolationQuality
imageInterpolationQuality() const;
232 void clip(const FloatRect
&);
233 void addRoundedRectClip(const IntRect
&, const IntSize
& topLeft
, const IntSize
& topRight
, const IntSize
& bottomLeft
, const IntSize
& bottomRight
);
234 void addRoundedRectClip(const FloatRect
&, const FloatSize
& topLeft
, const FloatSize
& topRight
, const FloatSize
& bottomLeft
, const FloatSize
& bottomRight
);
235 void addInnerRoundedRectClip(const IntRect
&, int thickness
);
236 void clipOut(const IntRect
&);
237 void clipOutEllipseInRect(const IntRect
&);
238 void clipOutRoundedRect(const IntRect
&, const IntSize
& topLeft
, const IntSize
& topRight
, const IntSize
& bottomLeft
, const IntSize
& bottomRight
);
239 void clipPath(WindRule
);
240 void clipToImageBuffer(const FloatRect
&, const ImageBuffer
*);
242 int textDrawingMode();
243 void setTextDrawingMode(int);
245 bool emojiDrawingEnabled();
246 void setEmojiDrawingEnabled(bool emojiDrawingEnabled
);
248 float drawText(const Font
&, const TextRun
&, const IntPoint
&, int from
= 0, int to
= -1);
249 float drawBidiText(const Font
&, const TextRun
&, const FloatPoint
&, BidiStatus
* = 0, int length
= -1);
250 void drawHighlightForText(const Font
&, const TextRun
&, const IntPoint
&, int h
, const Color
& backgroundColor
, int from
= 0, int to
= -1);
252 FloatRect
roundToDevicePixels(const FloatRect
&);
254 void drawLineForText(const IntPoint
&, int width
, bool printing
);
255 void drawLineForMisspellingOrBadGrammar(const IntPoint
&, int width
, bool grammar
);
257 bool paintingDisabled() const;
258 void setPaintingDisabled(bool);
260 bool updatingControlTints() const;
261 void setUpdatingControlTints(bool);
263 void beginTransparencyLayer(float opacity
);
264 void endTransparencyLayer();
266 void setShadow(const IntSize
&, int blur
, const Color
&);
267 bool getShadow(IntSize
&, int&, Color
&) const;
270 void initFocusRing(int width
, int offset
);
271 void addFocusRingRect(const IntRect
&);
272 void drawFocusRing(const Color
&);
273 void clearFocusRing();
274 IntRect
focusRingBoundingRect();
276 void setLineCap(LineCap
);
277 void setLineDash(const DashArray
&, float dashOffset
);
278 void setLineJoin(LineJoin
);
279 void setMiterLimit(float);
281 void setAlpha(float);
286 void setCompositeOperation(CompositeOperator
);
289 void addPath(const Path
&);
291 void clip(const Path
&);
292 void clipOut(const Path
&);
294 void scale(const FloatSize
&);
295 void rotate(float angleInRadians
);
296 void translate(float x
, float y
);
299 void setURLForRect(const KURL
&, const IntRect
&);
301 void concatCTM(const TransformationMatrix
&);
302 TransformationMatrix
getCTM() const;
305 GraphicsContext(HDC
, bool hasAlpha
= false); // FIXME: To be removed.
306 bool inTransparencyLayer() const;
307 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.
308 void releaseWindowsContext(HDC
, const IntRect
&, bool supportAlphaBlend
= true, bool mayCreateBitmap
= true); // The passed in HDC should be the one handed back by getWindowsContext.
310 // When set to true, child windows should be rendered into this context
311 // rather than allowing them just to render to the screen. Defaults to
313 // FIXME: This is a layering violation. GraphicsContext shouldn't know
314 // what a "window" is. It would be much more appropriate for this flag
315 // to be passed as a parameter alongside the GraphicsContext, but doing
316 // that would require lots of changes in cross-platform code that we
317 // aren't sure we want to make.
318 void setShouldIncludeChildWindows(bool);
319 bool shouldIncludeChildWindows() const;
321 class WindowsBitmap
: public Noncopyable
{
323 WindowsBitmap(HDC
, IntSize
);
326 HDC
hdc() const { return m_hdc
; }
327 UInt8
* buffer() const { return m_bitmapBuffer
; }
328 unsigned bufferLength() const { return m_bitmapBufferLength
; }
329 IntSize
size() const { return m_size
; }
330 unsigned bytesPerRow() const { return m_bytesPerRow
; }
335 UInt8
* m_bitmapBuffer
;
336 unsigned m_bitmapBufferLength
;
338 unsigned m_bytesPerRow
;
341 WindowsBitmap
* createWindowsBitmap(IntSize
);
342 // The bitmap should be non-premultiplied.
343 void drawWindowsBitmap(WindowsBitmap
*, const IntPoint
&);
346 #if PLATFORM(QT) && defined(Q_WS_WIN)
347 HDC
getWindowsContext(const IntRect
&, bool supportAlphaBlend
= true, bool mayCreateBitmap
= true);
348 void releaseWindowsContext(HDC
, const IntRect
&, bool supportAlphaBlend
= true, bool mayCreateBitmap
= true);
352 bool inTransparencyLayer() const;
353 PlatformPath
* currentPath();
357 void setPenFromCGColor (CGColorRef color
);
358 void drawAxialGradient(IPhoneGradientRef aGradient
, FloatPoint startPoint
, FloatPoint stopPoint
, Interpolation anInterpolation
);
359 void drawRadialGradient(IPhoneGradientRef aGradient
, FloatPoint startPoint
, float startRadius
, FloatPoint stopPoint
, float stopRadius
, Interpolation anInterpolation
);
362 void setGdkExposeEvent(GdkEventExpose
*);
363 GdkDrawable
* gdkDrawable() const;
364 GdkEventExpose
* gdkExposeEvent() const;
368 void savePlatformState();
369 void restorePlatformState();
371 void setPlatformTextDrawingMode(int);
372 void setPlatformFont(const Font
& font
);
374 void setPlatformStrokeColor(const Color
&);
375 void setPlatformStrokeStyle(const StrokeStyle
&);
376 void setPlatformStrokeThickness(float);
378 void setPlatformFillColor(const Color
&);
380 void setPlatformShouldAntialias(bool b
);
382 void setPlatformShadow(const IntSize
&, int blur
, const Color
&);
383 void clearPlatformShadow();
385 int focusRingWidth() const;
386 int focusRingOffset() const;
387 const Vector
<IntRect
>& focusRingRects() const;
389 static GraphicsContextPrivate
* createGraphicsContextPrivate();
390 static void destroyGraphicsContextPrivate(GraphicsContextPrivate
*);
392 GraphicsContextPrivate
* m_common
;
393 GraphicsContextPlatformPrivate
* m_data
; // Deprecated; m_commmon can just be downcasted. To be removed.
396 } // namespace WebCore
398 #endif // GraphicsContext_h