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"
37 #include <wtf/Noncopyable.h>
38 #include <wtf/PassOwnPtr.h>
41 typedef struct CGContext PlatformGraphicsContext;
45 class PlatformContextCairo;
47 typedef WebCore::PlatformContextCairo PlatformGraphicsContext;
48 #elif PLATFORM(OPENVG)
52 typedef class WebCore::SurfaceOpenVG PlatformGraphicsContext;
58 typedef QPainter PlatformGraphicsContext;
63 // wxGraphicsContext allows us to support Path, etc.
64 // but on some platforms, e.g. Linux, it requires fairly
67 // On OS X, wxGCDC is just a typedef for wxDC, so use wxDC explicitly to make
71 typedef wxDC PlatformGraphicsContext;
73 typedef wxGCDC PlatformGraphicsContext;
76 typedef wxWindowDC PlatformGraphicsContext;
81 class PlatformGraphicsContext;
87 class PlatformContextSkia;
89 typedef WebCore::PlatformContextSkia PlatformGraphicsContext;
93 typedef BView PlatformGraphicsContext;
96 typedef struct HDC__ PlatformGraphicsContext;
98 typedef void PlatformGraphicsContext;
102 #include "DIBPixelData.h"
103 typedef struct HDC__* HDC;
105 // UInt8 is defined in CoreFoundation/CFBase.h
106 typedef unsigned char UInt8;
110 #if PLATFORM(QT) && defined(Q_WS_WIN)
116 #if OS(WINCE) && !PLATFORM(QT)
118 class SimpleFontData;
122 const int cMisspellingLineThickness = 3;
123 const int cMisspellingLinePatternWidth = 4;
124 const int cMisspellingLinePatternGapWidth = 1;
126 class AffineTransform;
130 class GraphicsContextPlatformPrivate;
133 class RoundedIntRect;
135 class SharedGraphicsContext3D;
138 enum TextDrawingMode {
139 TextModeInvisible = 0,
140 TextModeFill = 1 << 0,
141 TextModeStroke = 1 << 1,
142 TextModeClip = 1 << 2
144 typedef unsigned TextDrawingModeFlags;
153 enum InterpolationQuality {
154 InterpolationDefault,
161 struct GraphicsContextState {
162 GraphicsContextState()
168 , textDrawingMode(TextModeFill)
169 , strokeColor(Color::black)
170 , fillColor(Color::black)
171 , strokeStyle(SolidStroke)
172 , fillRule(RULE_NONZERO)
173 , strokeColorSpace(ColorSpaceDeviceRGB)
174 , fillColorSpace(ColorSpaceDeviceRGB)
175 , shadowColorSpace(ColorSpaceDeviceRGB)
176 , compositeOperator(CompositeSourceOver)
177 , shouldAntialias(true)
178 , shouldSmoothFonts(true)
179 , paintingDisabled(false)
180 , shadowsIgnoreTransforms(false)
182 // Core Graphics incorrectly renders shadows with radius > 8px (<rdar://problem/8103442>),
183 // but we need to preserve this buggy behavior for canvas and -webkit-box-shadow.
184 , shadowsUseLegacyRadius(false)
189 RefPtr<Gradient> strokeGradient;
190 RefPtr<Pattern> strokePattern;
192 RefPtr<Gradient> fillGradient;
193 RefPtr<Pattern> fillPattern;
195 FloatSize shadowOffset;
197 float strokeThickness;
203 TextDrawingModeFlags textDrawingMode;
209 StrokeStyle strokeStyle;
212 ColorSpace strokeColorSpace;
213 ColorSpace fillColorSpace;
214 ColorSpace shadowColorSpace;
216 CompositeOperator compositeOperator;
218 bool shouldAntialias : 1;
219 bool shouldSmoothFonts : 1;
220 bool paintingDisabled : 1;
221 bool shadowsIgnoreTransforms : 1;
223 bool shadowsUseLegacyRadius : 1;
227 class GraphicsContext {
228 WTF_MAKE_NONCOPYABLE(GraphicsContext); WTF_MAKE_FAST_ALLOCATED;
230 GraphicsContext(PlatformGraphicsContext*);
233 #if !OS(WINCE) || PLATFORM(QT)
234 PlatformGraphicsContext* platformContext() const;
237 float strokeThickness() const;
238 void setStrokeThickness(float);
239 StrokeStyle strokeStyle() const;
240 void setStrokeStyle(StrokeStyle);
241 Color strokeColor() const;
242 ColorSpace strokeColorSpace() const;
243 void setStrokeColor(const Color&, ColorSpace);
245 void setStrokePattern(PassRefPtr<Pattern>);
246 Pattern* strokePattern() const;
248 void setStrokeGradient(PassRefPtr<Gradient>);
249 Gradient* strokeGradient() const;
251 WindRule fillRule() const;
252 void setFillRule(WindRule);
253 Color fillColor() const;
254 ColorSpace fillColorSpace() const;
255 void setFillColor(const Color&, ColorSpace);
257 void setFillPattern(PassRefPtr<Pattern>);
258 Pattern* fillPattern() const;
260 void setFillGradient(PassRefPtr<Gradient>);
261 Gradient* fillGradient() const;
263 void setShadowsIgnoreTransforms(bool);
264 bool shadowsIgnoreTransforms() const;
266 void setShouldAntialias(bool);
267 bool shouldAntialias() const;
269 void setShouldSmoothFonts(bool);
270 bool shouldSmoothFonts() const;
272 const GraphicsContextState& state() const;
275 void applyStrokePattern();
276 void applyFillPattern();
277 void drawPath(const Path&);
279 // Allow font smoothing (LCD antialiasing). Not part of the graphics state.
280 void setAllowsFontSmoothing(bool);
282 void setIsCALayerContext(bool);
283 bool isCALayerContext() const;
285 void setIsAcceleratedContext(bool);
286 bool isAcceleratedContext() const;
289 #if PLATFORM(ANDROID)
290 // initialize a paint for bitmaps
291 void setupBitmapPaint(SkPaint*);
292 // initialize a paint for filling
293 void setupFillPaint(SkPaint*);
294 // initialize a paint for stroking
295 void setupStrokePaint(SkPaint*);
296 // initialize a paint for a shadow, or if false is returned, the
297 // parameters are left untouched
298 bool setupShadowPaint(SkPaint* paint, SkPoint* offset);
299 // returns true if there is a valid (non-transparent) fill color
300 bool willFill() const;
301 // returns true if there is a valid (non-transparent) stroke color
302 bool willStroke() const;
304 // may return NULL, since we lazily allocate the path. This is the path
305 // that is drawn by drawPath()
306 const SkPath* getCurrPath() const;
308 /** platform-specific factory method to return a bitmap graphicscontext,
309 called by <canvas> when we need to draw offscreen. Caller is responsible for
310 deleting the context. Use drawOffscreenContext() to draw the context's image
311 onto another graphics context.
313 static GraphicsContext* createOffscreenContext(int width, int height);
319 // These draw methods will do both stroking and filling.
320 // FIXME: ...except drawRect(), which fills properly but always strokes
321 // using a 1-pixel stroke inset from the rect borders (of the correct
323 void drawRect(const IntRect&);
324 void drawLine(const IntPoint&, const IntPoint&);
325 void drawEllipse(const IntRect&);
326 void drawConvexPolygon(size_t numPoints, const FloatPoint*, bool shouldAntialias = false);
328 void fillPath(const Path&);
329 void strokePath(const Path&);
331 // Arc drawing (used by border-radius in CSS) just supports stroking at the moment.
332 void strokeArc(const IntRect&, int startAngle, int angleSpan);
334 void fillRect(const FloatRect&);
335 void fillRect(const FloatRect&, const Color&, ColorSpace);
336 void fillRect(const FloatRect&, Generator&);
337 void fillRoundedRect(const IntRect&, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color&, ColorSpace);
338 void fillRoundedRect(const RoundedIntRect&, const Color&, ColorSpace);
339 void fillRectWithRoundedHole(const IntRect&, const RoundedIntRect& roundedHoleRect, const Color&, ColorSpace);
341 void clearRect(const FloatRect&);
343 void strokeRect(const FloatRect&, float lineWidth);
345 void drawImage(Image*, ColorSpace styleColorSpace, const IntPoint&, CompositeOperator = CompositeSourceOver);
346 void drawImage(Image*, ColorSpace styleColorSpace, const IntRect&, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
347 void drawImage(Image*, ColorSpace styleColorSpace, const IntPoint& destPoint, const IntRect& srcRect, CompositeOperator = CompositeSourceOver);
348 void drawImage(Image*, ColorSpace styleColorSpace, const IntRect& destRect, const IntRect& srcRect, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
349 void drawImage(Image*, ColorSpace styleColorSpace, const FloatRect& destRect, const FloatRect& srcRect = FloatRect(0, 0, -1, -1),
350 CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
351 void drawTiledImage(Image*, ColorSpace styleColorSpace, const IntRect& destRect, const IntPoint& srcPoint, const IntSize& tileSize,
352 CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
353 void drawTiledImage(Image*, ColorSpace styleColorSpace, const IntRect& destRect, const IntRect& srcRect,
354 Image::TileRule hRule = Image::StretchTile, Image::TileRule vRule = Image::StretchTile,
355 CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
357 void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const IntPoint&, CompositeOperator = CompositeSourceOver);
358 void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const IntRect&, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
359 void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const IntPoint& destPoint, const IntRect& srcRect, CompositeOperator = CompositeSourceOver);
360 void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const IntRect& destRect, const IntRect& srcRect, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
361 void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const FloatRect& destRect, const FloatRect& srcRect = FloatRect(0, 0, -1, -1),
362 CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
364 void setImageInterpolationQuality(InterpolationQuality);
365 InterpolationQuality imageInterpolationQuality() const;
367 void clip(const IntRect&);
368 void clip(const FloatRect&);
369 void addRoundedRectClip(const RoundedIntRect&);
370 void addInnerRoundedRectClip(const IntRect&, int thickness);
371 void clipOut(const IntRect&);
372 void clipOutRoundedRect(const RoundedIntRect&);
373 void clipPath(const Path&, WindRule);
374 void clipConvexPolygon(size_t numPoints, const FloatPoint*, bool antialias = true);
375 void clipToImageBuffer(ImageBuffer*, const FloatRect&);
377 IntRect clipBounds() const;
379 TextDrawingModeFlags textDrawingMode() const;
380 void setTextDrawingMode(TextDrawingModeFlags);
382 void drawText(const Font&, const TextRun&, const FloatPoint&, int from = 0, int to = -1);
383 void drawEmphasisMarks(const Font&, const TextRun& , const AtomicString& mark, const FloatPoint&, int from = 0, int to = -1);
384 void drawBidiText(const Font&, const TextRun&, const FloatPoint&);
385 void drawHighlightForText(const Font&, const TextRun&, const FloatPoint&, int h, const Color& backgroundColor, ColorSpace, int from = 0, int to = -1);
389 RoundOriginAndDimensions
391 FloatRect roundToDevicePixels(const FloatRect&, RoundingMode = RoundAllSides);
393 void drawLineForText(const FloatPoint&, float width, bool printing);
394 enum TextCheckingLineStyle {
395 TextCheckingSpellingLineStyle,
396 TextCheckingGrammarLineStyle,
397 TextCheckingReplacementLineStyle
399 void drawLineForTextChecking(const FloatPoint&, float width, TextCheckingLineStyle);
401 bool paintingDisabled() const;
402 void setPaintingDisabled(bool);
404 bool updatingControlTints() const;
405 void setUpdatingControlTints(bool);
407 void beginTransparencyLayer(float opacity);
408 void endTransparencyLayer();
410 bool hasShadow() const;
411 void setShadow(const FloatSize&, float blur, const Color&, ColorSpace);
412 // Legacy shadow blur radius is used for canvas, and -webkit-box-shadow.
413 // It has different treatment of radii > 8px.
414 void setLegacyShadow(const FloatSize&, float blur, const Color&, ColorSpace);
416 bool getShadow(FloatSize&, float&, Color&, ColorSpace&) const;
419 void drawFocusRing(const Vector<IntRect>&, int width, int offset, const Color&);
420 void drawFocusRing(const Path&, int width, int offset, const Color&);
422 void setLineCap(LineCap);
423 void setLineDash(const DashArray&, float dashOffset);
424 void setLineJoin(LineJoin);
425 void setMiterLimit(float);
427 void setAlpha(float);
432 void setCompositeOperation(CompositeOperator);
433 CompositeOperator compositeOperation() const;
435 void clip(const Path&);
437 // This clip function is used only by <canvas> code. It allows
438 // implementations to handle clipping on the canvas differently since
439 // the discipline is different.
440 void canvasClip(const Path&);
441 void clipOut(const Path&);
443 void scale(const FloatSize&);
444 void rotate(float angleInRadians);
445 void translate(const FloatSize& size) { translate(size.width(), size.height()); }
446 void translate(float x, float y);
448 void setURLForRect(const KURL&, const IntRect&);
450 void concatCTM(const AffineTransform&);
451 void setCTM(const AffineTransform&);
452 AffineTransform getCTM() const;
454 #if OS(WINCE) && !PLATFORM(QT)
455 void setBitmap(PassRefPtr<SharedBitmap>);
456 const AffineTransform& affineTransform() const;
457 AffineTransform& affineTransform();
458 void resetAffineTransform();
459 void fillRect(const FloatRect&, const Gradient*);
460 void drawText(const SimpleFontData* fontData, const GlyphBuffer& glyphBuffer, int from, int numGlyphs, const FloatPoint& point);
461 void drawFrameControl(const IntRect& rect, unsigned type, unsigned state);
462 void drawFocusRect(const IntRect& rect);
463 void paintTextField(const IntRect& rect, unsigned state);
464 void drawBitmap(SharedBitmap*, const IntRect& dstRect, const IntRect& srcRect, ColorSpace styleColorSpace, CompositeOperator compositeOp);
465 void drawBitmapPattern(SharedBitmap*, const FloatRect& tileRectIn, const AffineTransform& patternTransform, const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator op, const FloatRect& destRect, const IntSize& origSourceSize);
466 void drawIcon(HICON icon, const IntRect& dstRect, UINT flags);
467 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.
468 void releaseWindowsContext(HDC, const IntRect&, bool supportAlphaBlend = false, bool mayCreateBitmap = true); // The passed in HDC should be the one handed back by getWindowsContext.
469 void drawRoundCorner(bool newClip, RECT clipRect, RECT rectWin, HDC dc, int width, int height);
471 GraphicsContext(HDC, bool hasAlpha = false); // FIXME: To be removed.
472 bool inTransparencyLayer() const;
473 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.
474 void releaseWindowsContext(HDC, const IntRect&, bool supportAlphaBlend = true, bool mayCreateBitmap = true); // The passed in HDC should be the one handed back by getWindowsContext.
476 // When set to true, child windows should be rendered into this context
477 // rather than allowing them just to render to the screen. Defaults to
479 // FIXME: This is a layering violation. GraphicsContext shouldn't know
480 // what a "window" is. It would be much more appropriate for this flag
481 // to be passed as a parameter alongside the GraphicsContext, but doing
482 // that would require lots of changes in cross-platform code that we
483 // aren't sure we want to make.
484 void setShouldIncludeChildWindows(bool);
485 bool shouldIncludeChildWindows() const;
487 class WindowsBitmap {
488 WTF_MAKE_NONCOPYABLE(WindowsBitmap);
490 WindowsBitmap(HDC, IntSize);
493 HDC hdc() const { return m_hdc; }
494 UInt8* buffer() const { return m_pixelData.buffer(); }
495 unsigned bufferLength() const { return m_pixelData.bufferLength(); }
496 const IntSize& size() const { return m_pixelData.size(); }
497 unsigned bytesPerRow() const { return m_pixelData.bytesPerRow(); }
498 unsigned short bitsPerPixel() const { return m_pixelData.bitsPerPixel(); }
499 const DIBPixelData& windowsDIB() const { return m_pixelData; }
504 DIBPixelData m_pixelData;
507 WindowsBitmap* createWindowsBitmap(IntSize);
508 // The bitmap should be non-premultiplied.
509 void drawWindowsBitmap(WindowsBitmap*, const IntPoint&);
512 #if (PLATFORM(QT) && defined(Q_WS_WIN)) || (PLATFORM(WX) && OS(WINDOWS))
513 HDC getWindowsContext(const IntRect&, bool supportAlphaBlend = true, bool mayCreateBitmap = true);
514 void releaseWindowsContext(HDC, const IntRect&, bool supportAlphaBlend = true, bool mayCreateBitmap = true);
515 bool shouldIncludeChildWindows() const { return false; }
519 bool inTransparencyLayer() const { return false; }
523 bool inTransparencyLayer() const;
524 void pushTransparencyLayerInternal(const QRect &rect, qreal opacity, QPixmap& alphaMask);
525 void takeOwnershipOfPlatformContext();
528 #if PLATFORM(QT) || USE(CAIRO)
529 ContextShadow* contextShadow();
533 GraphicsContext(cairo_t*);
537 void setGdkExposeEvent(GdkEventExpose*);
538 GdkWindow* gdkWindow() const;
539 GdkEventExpose* gdkExposeEvent() const;
543 pattern getHaikuStrokeStyle();
546 void setSharedGraphicsContext3D(SharedGraphicsContext3D*, DrawingBuffer*, const IntSize&);
547 void syncSoftwareCanvas();
548 void markDirtyRect(const IntRect&); // Hints that a portion of the backing store is dirty.
551 void platformInit(PlatformGraphicsContext*);
552 void platformDestroy();
554 #if PLATFORM(WIN) && !OS(WINCE)
555 void platformInit(HDC, bool hasAlpha = false);
558 void savePlatformState();
559 void restorePlatformState();
561 void setPlatformTextDrawingMode(TextDrawingModeFlags);
562 void setPlatformFont(const Font& font);
564 void setPlatformStrokeColor(const Color&, ColorSpace);
565 void setPlatformStrokeStyle(StrokeStyle);
566 void setPlatformStrokeThickness(float);
567 void setPlatformStrokeGradient(Gradient*);
568 void setPlatformStrokePattern(Pattern*);
570 void setPlatformFillColor(const Color&, ColorSpace);
571 void setPlatformFillGradient(Gradient*);
572 void setPlatformFillPattern(Pattern*);
574 void setPlatformShouldAntialias(bool);
575 void setPlatformShouldSmoothFonts(bool);
577 void setPlatformShadow(const FloatSize&, float blur, const Color&, ColorSpace);
578 void clearPlatformShadow();
580 void setPlatformCompositeOperation(CompositeOperator);
582 static void adjustLineToPixelBoundaries(FloatPoint& p1, FloatPoint& p2, float strokeWidth, StrokeStyle);
584 GraphicsContextPlatformPrivate* m_data;
586 GraphicsContextState m_state;
587 Vector<GraphicsContextState> m_stack;
588 bool m_updatingControlTints;
591 } // namespace WebCore
593 #endif // GraphicsContext_h