2 * Copyright (C) 2003, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2008-2009 Torch Mobile, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef GraphicsContext_h
28 #define GraphicsContext_h
30 #include "ColorSpace.h"
31 #include "DashArray.h"
32 #include "FloatRect.h"
36 #include "TextDirection.h"
37 #include <wtf/Noncopyable.h>
38 #include <wtf/PassOwnPtr.h>
41 typedef struct CGContext PlatformGraphicsContext;
46 typedef struct _cairo PlatformGraphicsContext;
47 #elif PLATFORM(OPENVG)
51 typedef class WebCore::SurfaceOpenVG PlatformGraphicsContext;
57 typedef QPainter PlatformGraphicsContext;
62 // wxGraphicsContext allows us to support Path, etc.
63 // but on some platforms, e.g. Linux, it requires fairly
66 // On OS X, wxGCDC is just a typedef for wxDC, so use wxDC explicitly to make
70 typedef wxDC PlatformGraphicsContext;
72 typedef wxGCDC PlatformGraphicsContext;
75 typedef wxWindowDC PlatformGraphicsContext;
80 class PlatformGraphicsContext;
86 class PlatformContextSkia;
88 typedef WebCore::PlatformContextSkia PlatformGraphicsContext;
92 typedef BView PlatformGraphicsContext;
95 typedef struct HDC__ PlatformGraphicsContext;
97 typedef void PlatformGraphicsContext;
101 typedef struct _GdkDrawable GdkDrawable;
102 typedef struct _GdkEventExpose GdkEventExpose;
106 typedef struct HDC__* HDC;
108 // UInt8 is defined in CoreFoundation/CFBase.h
109 typedef unsigned char UInt8;
113 #if PLATFORM(QT) && defined(Q_WS_WIN)
119 #if OS(WINCE) && !PLATFORM(QT)
121 class SimpleFontData;
125 const int cMisspellingLineThickness = 3;
126 const int cMisspellingLinePatternWidth = 4;
127 const int cMisspellingLinePatternGapWidth = 1;
129 class AffineTransform;
134 class GraphicsContextPlatformPrivate;
135 class GraphicsContextPrivate;
139 class SharedGraphicsContext3D;
142 // These bits can be ORed together for a total of 8 possible text drawing modes.
143 const int cTextInvisible = 0;
144 const int cTextFill = 1;
145 const int cTextStroke = 2;
146 const int cTextClip = 4;
155 enum InterpolationQuality {
156 InterpolationDefault,
163 class GraphicsContext : public Noncopyable {
165 GraphicsContext(PlatformGraphicsContext*);
168 #if !OS(WINCE) || PLATFORM(QT)
169 PlatformGraphicsContext* platformContext() const;
172 float strokeThickness() const;
173 void setStrokeThickness(float);
174 StrokeStyle strokeStyle() const;
175 void setStrokeStyle(const StrokeStyle& style);
176 Color strokeColor() const;
177 ColorSpace strokeColorSpace() const;
178 void setStrokeColor(const Color&, ColorSpace);
180 void setStrokePattern(PassRefPtr<Pattern>);
181 Pattern* strokePattern() const;
183 void setStrokeGradient(PassRefPtr<Gradient>);
184 Gradient* strokeGradient() const;
186 WindRule fillRule() const;
187 void setFillRule(WindRule);
188 Color fillColor() const;
189 ColorSpace fillColorSpace() const;
190 void setFillColor(const Color&, ColorSpace);
192 void setFillPattern(PassRefPtr<Pattern>);
193 Pattern* fillPattern() const;
195 void setFillGradient(PassRefPtr<Gradient>);
196 Gradient* fillGradient() const;
198 void setShadowsIgnoreTransforms(bool);
200 void setShouldAntialias(bool);
201 bool shouldAntialias() const;
204 void applyStrokePattern();
205 void applyFillPattern();
206 void drawPath(const Path&);
209 #if PLATFORM(ANDROID)
210 // initialize a paint for bitmaps
211 void setupBitmapPaint(SkPaint*);
212 // initialize a paint for filling
213 void setupFillPaint(SkPaint*);
214 // initialize a paint for stroking
215 void setupStrokePaint(SkPaint*);
216 // initialize a paint for a shadow, or if false is returned, the
217 // parameters are left untouched
218 bool setupShadowPaint(SkPaint* paint, SkPoint* offset);
219 // returns true if there is a valid (non-transparent) fill color
220 bool willFill() const;
221 // returns true if there is a valid (non-transparent) stroke color
222 bool willStroke() const;
224 // may return NULL, since we lazily allocate the path. This is the path
225 // that is drawn by drawPath()
226 const SkPath* getCurrPath() const;
228 /** platform-specific factory method to return a bitmap graphicscontext,
229 called by <canvas> when we need to draw offscreen. Caller is responsible for
230 deleting the context. Use drawOffscreenContext() to draw the context's image
231 onto another graphics context.
233 static GraphicsContext* createOffscreenContext(int width, int height);
239 // These draw methods will do both stroking and filling.
240 // FIXME: ...except drawRect(), which fills properly but always strokes
241 // using a 1-pixel stroke inset from the rect borders (of the correct
243 void drawRect(const IntRect&);
244 void drawLine(const IntPoint&, const IntPoint&);
245 void drawEllipse(const IntRect&);
246 void drawConvexPolygon(size_t numPoints, const FloatPoint*, bool shouldAntialias = false);
248 void fillPath(const Path&);
249 void strokePath(const Path&);
251 // Arc drawing (used by border-radius in CSS) just supports stroking at the moment.
252 void strokeArc(const IntRect&, int startAngle, int angleSpan);
254 void fillRect(const FloatRect&);
255 void fillRect(const FloatRect&, const Color&, ColorSpace);
256 void fillRect(const FloatRect&, Generator&);
257 void fillRoundedRect(const IntRect&, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color&, ColorSpace);
259 void clearRect(const FloatRect&);
261 void strokeRect(const FloatRect&, float lineWidth);
263 void drawImage(Image*, ColorSpace styleColorSpace, const IntPoint&, CompositeOperator = CompositeSourceOver);
264 void drawImage(Image*, ColorSpace styleColorSpace, const IntRect&, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
265 void drawImage(Image*, ColorSpace styleColorSpace, const IntPoint& destPoint, const IntRect& srcRect, CompositeOperator = CompositeSourceOver);
266 void drawImage(Image*, ColorSpace styleColorSpace, const IntRect& destRect, const IntRect& srcRect, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
267 void drawImage(Image*, ColorSpace styleColorSpace, const FloatRect& destRect, const FloatRect& srcRect = FloatRect(0, 0, -1, -1),
268 CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
269 void drawTiledImage(Image*, ColorSpace styleColorSpace, const IntRect& destRect, const IntPoint& srcPoint, const IntSize& tileSize,
270 CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
271 void drawTiledImage(Image*, ColorSpace styleColorSpace, const IntRect& destRect, const IntRect& srcRect,
272 Image::TileRule hRule = Image::StretchTile, Image::TileRule vRule = Image::StretchTile,
273 CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
275 void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const IntPoint&, CompositeOperator = CompositeSourceOver);
276 void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const IntRect&, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
277 void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const IntPoint& destPoint, const IntRect& srcRect, CompositeOperator = CompositeSourceOver);
278 void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const IntRect& destRect, const IntRect& srcRect, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
279 void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const FloatRect& destRect, const FloatRect& srcRect = FloatRect(0, 0, -1, -1),
280 CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
282 void setImageInterpolationQuality(InterpolationQuality);
283 InterpolationQuality imageInterpolationQuality() const;
285 void clip(const FloatRect&);
286 void addRoundedRectClip(const IntRect&, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight);
287 void addInnerRoundedRectClip(const IntRect&, int thickness);
288 void clipOut(const IntRect&);
289 void clipOutRoundedRect(const IntRect&, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight);
290 void clipPath(const Path&, WindRule);
291 void clipConvexPolygon(size_t numPoints, const FloatPoint*, bool antialias = true);
292 void clipToImageBuffer(ImageBuffer*, const FloatRect&);
294 int textDrawingMode();
295 void setTextDrawingMode(int);
297 void drawText(const Font&, const TextRun&, const IntPoint&, int from = 0, int to = -1);
298 void drawBidiText(const Font&, const TextRun&, const FloatPoint&);
299 void drawHighlightForText(const Font&, const TextRun&, const IntPoint&, int h, const Color& backgroundColor, ColorSpace, int from = 0, int to = -1);
301 FloatRect roundToDevicePixels(const FloatRect&);
303 void drawLineForText(const IntPoint&, int width, bool printing);
304 enum TextCheckingLineStyle {
305 TextCheckingSpellingLineStyle,
306 TextCheckingGrammarLineStyle,
307 TextCheckingReplacementLineStyle
309 void drawLineForTextChecking(const IntPoint&, int width, TextCheckingLineStyle);
311 bool paintingDisabled() const;
312 void setPaintingDisabled(bool);
314 bool updatingControlTints() const;
315 void setUpdatingControlTints(bool);
317 void beginTransparencyLayer(float opacity);
318 void endTransparencyLayer();
320 void setShadow(const FloatSize&, float blur, const Color&, ColorSpace);
321 bool getShadow(FloatSize&, float&, Color&) const;
324 void drawFocusRing(const Vector<IntRect>&, int width, int offset, const Color&);
325 void drawFocusRing(const Path&, int width, int offset, const Color&);
327 void setLineCap(LineCap);
328 void setLineDash(const DashArray&, float dashOffset);
329 void setLineJoin(LineJoin);
330 void setMiterLimit(float);
332 void setAlpha(float);
337 void setCompositeOperation(CompositeOperator);
339 #if PLATFORM(SKIA) || PLATFORM(WX) || PLATFORM(OPENVG) || OS(WINCE)
341 void addPath(const Path&);
344 void clip(const Path&);
346 // This clip function is used only by <canvas> code. It allows
347 // implementations to handle clipping on the canvas differently since
348 // the disipline is different.
349 void canvasClip(const Path&);
350 void clipOut(const Path&);
352 void scale(const FloatSize&);
353 void rotate(float angleInRadians);
354 void translate(const FloatSize& size) { translate(size.width(), size.height()); }
355 void translate(float x, float y);
357 void setURLForRect(const KURL&, const IntRect&);
359 void concatCTM(const AffineTransform&);
360 AffineTransform getCTM() const;
362 #if OS(WINCE) && !PLATFORM(QT)
363 void setBitmap(PassRefPtr<SharedBitmap>);
364 const AffineTransform& affineTransform() const;
365 AffineTransform& affineTransform();
366 void resetAffineTransform();
367 void fillRect(const FloatRect&, const Gradient*);
368 void drawText(const SimpleFontData* fontData, const GlyphBuffer& glyphBuffer, int from, int numGlyphs, const FloatPoint& point);
369 void drawFrameControl(const IntRect& rect, unsigned type, unsigned state);
370 void drawFocusRect(const IntRect& rect);
371 void paintTextField(const IntRect& rect, unsigned state);
372 void drawBitmap(SharedBitmap*, const IntRect& dstRect, const IntRect& srcRect, ColorSpace styleColorSpace, CompositeOperator compositeOp);
373 void drawBitmapPattern(SharedBitmap*, const FloatRect& tileRectIn, const AffineTransform& patternTransform, const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator op, const FloatRect& destRect, const IntSize& origSourceSize);
374 void drawIcon(HICON icon, const IntRect& dstRect, UINT flags);
375 HDC getWindowsContext(const IntRect&, bool supportAlphaBlend = false, bool mayCreateBitmap = true); // The passed in rect is used to create a bitmap for compositing inside transparency layers.
376 void releaseWindowsContext(HDC, const IntRect&, bool supportAlphaBlend = false, bool mayCreateBitmap = true); // The passed in HDC should be the one handed back by getWindowsContext.
377 void drawRoundCorner(bool newClip, RECT clipRect, RECT rectWin, HDC dc, int width, int height);
379 GraphicsContext(HDC, bool hasAlpha = false); // FIXME: To be removed.
380 bool inTransparencyLayer() const;
381 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.
382 void releaseWindowsContext(HDC, const IntRect&, bool supportAlphaBlend = true, bool mayCreateBitmap = true); // The passed in HDC should be the one handed back by getWindowsContext.
384 // When set to true, child windows should be rendered into this context
385 // rather than allowing them just to render to the screen. Defaults to
387 // FIXME: This is a layering violation. GraphicsContext shouldn't know
388 // what a "window" is. It would be much more appropriate for this flag
389 // to be passed as a parameter alongside the GraphicsContext, but doing
390 // that would require lots of changes in cross-platform code that we
391 // aren't sure we want to make.
392 void setShouldIncludeChildWindows(bool);
393 bool shouldIncludeChildWindows() const;
395 class WindowsBitmap : public Noncopyable {
397 WindowsBitmap(HDC, IntSize);
400 HDC hdc() const { return m_hdc; }
401 UInt8* buffer() const { return m_bitmapBuffer; }
402 unsigned bufferLength() const { return m_bitmapBufferLength; }
403 IntSize size() const { return m_size; }
404 unsigned bytesPerRow() const { return m_bytesPerRow; }
409 UInt8* m_bitmapBuffer;
410 unsigned m_bitmapBufferLength;
412 unsigned m_bytesPerRow;
415 WindowsBitmap* createWindowsBitmap(IntSize);
416 // The bitmap should be non-premultiplied.
417 void drawWindowsBitmap(WindowsBitmap*, const IntPoint&);
420 #if (PLATFORM(QT) && defined(Q_WS_WIN)) || (PLATFORM(WX) && OS(WINDOWS))
421 HDC getWindowsContext(const IntRect&, bool supportAlphaBlend = true, bool mayCreateBitmap = true);
422 void releaseWindowsContext(HDC, const IntRect&, bool supportAlphaBlend = true, bool mayCreateBitmap = true);
423 bool shouldIncludeChildWindows() const { return false; }
427 bool inTransparencyLayer() const { return false; }
431 bool inTransparencyLayer() const;
432 void pushTransparencyLayerInternal(const QRect &rect, qreal opacity, QPixmap& alphaMask);
433 void takeOwnershipOfPlatformContext();
434 static QPainter::CompositionMode toQtCompositionMode(CompositeOperator op);
437 #if PLATFORM(QT) || PLATFORM(CAIRO)
438 ContextShadow* contextShadow();
442 void setGdkExposeEvent(GdkEventExpose*);
443 GdkDrawable* gdkDrawable() const;
444 GdkEventExpose* gdkExposeEvent() const;
448 pattern getHaikuStrokeStyle();
451 void setSharedGraphicsContext3D(SharedGraphicsContext3D*, DrawingBuffer*, const IntSize&);
452 void syncSoftwareCanvas();
453 void markDirtyRect(const IntRect&); // Hints that a portion of the backing store is dirty.
456 void savePlatformState();
457 void restorePlatformState();
459 void setPlatformTextDrawingMode(int);
460 void setPlatformFont(const Font& font);
462 void setPlatformStrokeColor(const Color&, ColorSpace);
463 void setPlatformStrokeStyle(const StrokeStyle&);
464 void setPlatformStrokeThickness(float);
465 void setPlatformStrokeGradient(Gradient*);
466 void setPlatformStrokePattern(Pattern*);
468 void setPlatformFillColor(const Color&, ColorSpace);
469 void setPlatformFillGradient(Gradient*);
470 void setPlatformFillPattern(Pattern*);
472 void setPlatformShouldAntialias(bool b);
474 void setPlatformShadow(const FloatSize&, float blur, const Color&, ColorSpace);
475 void clearPlatformShadow();
477 static void adjustLineToPixelBoundaries(FloatPoint& p1, FloatPoint& p2, float strokeWidth, const StrokeStyle&);
479 static GraphicsContextPrivate* createGraphicsContextPrivate();
480 static void destroyGraphicsContextPrivate(GraphicsContextPrivate*);
482 GraphicsContextPrivate* m_common;
483 GraphicsContextPlatformPrivate* m_data; // Deprecated; m_commmon can just be downcasted. To be removed.
486 } // namespace WebCore
488 #endif // GraphicsContext_h