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 "ImageOrientation.h"
39
#include <wtf/Noncopyable.h>
40
#include <wtf/PassOwnPtr.h>
43
typedef struct CGContext PlatformGraphicsContext;
46
class PlatformContextCairo;
48
typedef WebCore::PlatformContextCairo PlatformGraphicsContext;
49
#elif PLATFORM(OPENVG)
53
typedef class WebCore::SurfaceOpenVG PlatformGraphicsContext;
59
typedef QPainter PlatformGraphicsContext;
64
// wxGraphicsContext allows us to support Path, etc.
65
// but on some platforms, e.g. Linux, it requires fairly
68
// On OS X, wxGCDC is just a typedef for wxDC, so use wxDC explicitly to make
72
typedef wxDC PlatformGraphicsContext;
74
typedef wxGCDC PlatformGraphicsContext;
77
typedef wxWindowDC PlatformGraphicsContext;
81
class PlatformContextSkia;
82
typedef PlatformContextSkia GraphicsContextPlatformPrivate;
84
typedef WebCore::PlatformContextSkia PlatformGraphicsContext;
86
typedef struct HDC__ PlatformGraphicsContext;
88
typedef void PlatformGraphicsContext;
92
#include "DIBPixelData.h"
93
typedef struct HDC__* HDC;
95
// UInt8 is defined in CoreFoundation/CFBase.h
96
typedef unsigned char UInt8;
100
#if PLATFORM(QT) && OS(WINDOWS)
106
#if OS(WINCE) && !PLATFORM(QT)
108
class SimpleFontData;
112
const int cMisspellingLineThickness = 3;
113
const int cMisspellingLinePatternWidth = 4;
114
const int cMisspellingLinePatternGapWidth = 1;
116
class AffineTransform;
120
class GraphicsContextPlatformPrivate;
126
class GraphicsContext3D;
128
class TransformationMatrix;
130
enum TextDrawingMode {
131
TextModeFill = 1 << 0,
132
TextModeStroke = 1 << 1,
134
typedef unsigned TextDrawingModeFlags;
141
#if ENABLE(CSS3_TEXT)
147
enum InterpolationQuality {
148
InterpolationDefault,
155
struct GraphicsContextState {
156
GraphicsContextState()
159
, textDrawingMode(TextModeFill)
160
, strokeColor(Color::black)
161
, fillColor(Color::black)
162
, strokeStyle(SolidStroke)
163
, fillRule(RULE_NONZERO)
164
, strokeColorSpace(ColorSpaceDeviceRGB)
165
, fillColorSpace(ColorSpaceDeviceRGB)
166
, shadowColorSpace(ColorSpaceDeviceRGB)
167
, compositeOperator(CompositeSourceOver)
168
, shouldAntialias(true)
169
, shouldSmoothFonts(true)
170
, shouldSubpixelQuantizeFonts(true)
171
, paintingDisabled(false)
172
, shadowsIgnoreTransforms(false)
174
// Core Graphics incorrectly renders shadows with radius > 8px (<rdar://problem/8103442>),
175
// but we need to preserve this buggy behavior for canvas and -webkit-box-shadow.
176
, shadowsUseLegacyRadius(false)
181
RefPtr<Gradient> strokeGradient;
182
RefPtr<Pattern> strokePattern;
184
RefPtr<Gradient> fillGradient;
185
RefPtr<Pattern> fillPattern;
187
FloatSize shadowOffset;
189
float strokeThickness;
192
TextDrawingModeFlags textDrawingMode;
198
StrokeStyle strokeStyle;
201
ColorSpace strokeColorSpace;
202
ColorSpace fillColorSpace;
203
ColorSpace shadowColorSpace;
205
CompositeOperator compositeOperator;
207
bool shouldAntialias : 1;
208
bool shouldSmoothFonts : 1;
209
bool shouldSubpixelQuantizeFonts : 1;
210
bool paintingDisabled : 1;
211
bool shadowsIgnoreTransforms : 1;
213
bool shadowsUseLegacyRadius : 1;
217
class GraphicsContext {
218
WTF_MAKE_NONCOPYABLE(GraphicsContext); WTF_MAKE_FAST_ALLOCATED;
220
GraphicsContext(PlatformGraphicsContext*);
223
#if !OS(WINCE) || PLATFORM(QT)
224
PlatformGraphicsContext* platformContext() const;
227
float strokeThickness() const;
228
void setStrokeThickness(float);
229
StrokeStyle strokeStyle() const;
230
void setStrokeStyle(StrokeStyle);
231
Color strokeColor() const;
232
ColorSpace strokeColorSpace() const;
233
void setStrokeColor(const Color&, ColorSpace);
235
void setStrokePattern(PassRefPtr<Pattern>);
236
Pattern* strokePattern() const;
238
void setStrokeGradient(PassRefPtr<Gradient>);
239
Gradient* strokeGradient() const;
241
WindRule fillRule() const;
242
void setFillRule(WindRule);
243
Color fillColor() const;
244
ColorSpace fillColorSpace() const;
245
void setFillColor(const Color&, ColorSpace);
247
void setFillPattern(PassRefPtr<Pattern>);
248
Pattern* fillPattern() const;
250
void setFillGradient(PassRefPtr<Gradient>);
251
Gradient* fillGradient() const;
253
void setShadowsIgnoreTransforms(bool);
254
bool shadowsIgnoreTransforms() const;
256
void setShouldAntialias(bool);
257
bool shouldAntialias() const;
259
void setShouldSmoothFonts(bool);
260
bool shouldSmoothFonts() const;
262
// Normally CG enables subpixel-quantization because it improves the performance of aligning glyphs.
263
// In some cases we have to disable to to ensure a high-quality output of the glyphs.
264
void setShouldSubpixelQuantizeFonts(bool);
265
bool shouldSubpixelQuantizeFonts() const;
267
const GraphicsContextState& state() const;
270
void applyStrokePattern();
271
void applyFillPattern();
272
void drawPath(const Path&);
274
void drawNativeImage(NativeImagePtr, const FloatSize& selfSize, ColorSpace styleColorSpace, const FloatRect& destRect, const FloatRect& srcRect, CompositeOperator = CompositeSourceOver, ImageOrientation = DefaultImageOrientation);
276
// Allow font smoothing (LCD antialiasing). Not part of the graphics state.
277
void setAllowsFontSmoothing(bool);
279
void setIsCALayerContext(bool);
280
bool isCALayerContext() const;
282
void setIsAcceleratedContext(bool);
284
bool isAcceleratedContext() const;
289
// These draw methods will do both stroking and filling.
290
// FIXME: ...except drawRect(), which fills properly but always strokes
291
// using a 1-pixel stroke inset from the rect borders (of the correct
293
void drawRect(const IntRect&);
294
void drawLine(const IntPoint&, const IntPoint&);
295
void drawEllipse(const IntRect&);
296
void drawConvexPolygon(size_t numPoints, const FloatPoint*, bool shouldAntialias = false);
298
void fillPath(const Path&);
299
void strokePath(const Path&);
301
void fillEllipse(const FloatRect&);
302
void strokeEllipse(const FloatRect&);
304
// Arc drawing (used by border-radius in CSS) just supports stroking at the moment.
305
void strokeArc(const IntRect&, int startAngle, int angleSpan);
307
void fillRect(const FloatRect&);
308
void fillRect(const FloatRect&, const Color&, ColorSpace);
309
void fillRect(const FloatRect&, Generator&);
310
void fillRect(const FloatRect&, const Color&, ColorSpace, CompositeOperator);
311
void fillRoundedRect(const IntRect&, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color&, ColorSpace);
312
void fillRoundedRect(const RoundedRect&, const Color&, ColorSpace);
313
void fillRectWithRoundedHole(const IntRect&, const RoundedRect& roundedHoleRect, const Color&, ColorSpace);
315
void clearRect(const FloatRect&);
317
void strokeRect(const FloatRect&, float lineWidth);
319
void drawImage(Image*, ColorSpace styleColorSpace, const IntPoint&, CompositeOperator = CompositeSourceOver, RespectImageOrientationEnum = DoNotRespectImageOrientation);
320
void drawImage(Image*, ColorSpace styleColorSpace, const IntRect&, CompositeOperator = CompositeSourceOver, RespectImageOrientationEnum = DoNotRespectImageOrientation, bool useLowQualityScale = false);
321
void drawImage(Image*, ColorSpace styleColorSpace, const IntPoint& destPoint, const IntRect& srcRect, CompositeOperator = CompositeSourceOver, RespectImageOrientationEnum = DoNotRespectImageOrientation);
322
void drawImage(Image*, ColorSpace styleColorSpace, const IntRect& destRect, const IntRect& srcRect, CompositeOperator = CompositeSourceOver, RespectImageOrientationEnum = DoNotRespectImageOrientation, bool useLowQualityScale = false);
323
void drawImage(Image*, ColorSpace styleColorSpace, const FloatRect& destRect, const FloatRect& srcRect = FloatRect(0, 0, -1, -1),
324
CompositeOperator = CompositeSourceOver, RespectImageOrientationEnum = DoNotRespectImageOrientation, bool useLowQualityScale = false);
325
void drawTiledImage(Image*, ColorSpace styleColorSpace, const IntRect& destRect, const IntPoint& srcPoint, const IntSize& tileSize,
326
CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
327
void drawTiledImage(Image*, ColorSpace styleColorSpace, const IntRect& destRect, const IntRect& srcRect,
328
const FloatSize& tileScaleFactor, Image::TileRule hRule = Image::StretchTile, Image::TileRule vRule = Image::StretchTile,
329
CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
331
void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const IntPoint&, CompositeOperator = CompositeSourceOver);
332
void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const IntRect&, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
333
void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const IntPoint& destPoint, const IntRect& srcRect, CompositeOperator = CompositeSourceOver);
334
void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const IntRect& destRect, const IntRect& srcRect, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
335
void drawImageBuffer(ImageBuffer*, ColorSpace styleColorSpace, const FloatRect& destRect, const FloatRect& srcRect = FloatRect(0, 0, -1, -1),
336
CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
338
void setImageInterpolationQuality(InterpolationQuality);
339
InterpolationQuality imageInterpolationQuality() const;
341
void clip(const IntRect&);
342
void clip(const FloatRect&);
343
void addRoundedRectClip(const RoundedRect&);
344
void addInnerRoundedRectClip(const IntRect&, int thickness);
345
void clipOut(const IntRect&);
346
void clipOutRoundedRect(const RoundedRect&);
347
void clipPath(const Path&, WindRule);
348
void clipConvexPolygon(size_t numPoints, const FloatPoint*, bool antialias = true);
349
void clipToImageBuffer(ImageBuffer*, const FloatRect&);
351
IntRect clipBounds() const;
353
TextDrawingModeFlags textDrawingMode() const;
354
void setTextDrawingMode(TextDrawingModeFlags);
356
void drawText(const Font&, const TextRun&, const FloatPoint&, int from = 0, int to = -1);
357
void drawEmphasisMarks(const Font&, const TextRun& , const AtomicString& mark, const FloatPoint&, int from = 0, int to = -1);
358
void drawBidiText(const Font&, const TextRun&, const FloatPoint&, Font::CustomFontNotReadyAction = Font::DoNotPaintIfFontNotReady);
359
void drawHighlightForText(const Font&, const TextRun&, const FloatPoint&, int h, const Color& backgroundColor, ColorSpace, int from = 0, int to = -1);
363
RoundOriginAndDimensions
365
FloatRect roundToDevicePixels(const FloatRect&, RoundingMode = RoundAllSides);
367
void drawLineForText(const FloatPoint&, float width, bool printing);
368
enum DocumentMarkerLineStyle {
369
DocumentMarkerSpellingLineStyle,
370
DocumentMarkerGrammarLineStyle,
371
DocumentMarkerAutocorrectionReplacementLineStyle,
372
DocumentMarkerDictationAlternativesLineStyle
374
void drawLineForDocumentMarker(const FloatPoint&, float width, DocumentMarkerLineStyle);
376
bool paintingDisabled() const;
377
void setPaintingDisabled(bool);
379
bool updatingControlTints() const;
380
void setUpdatingControlTints(bool);
382
void beginTransparencyLayer(float opacity);
383
void endTransparencyLayer();
384
bool isInTransparencyLayer() const;
386
bool hasShadow() const;
387
void setShadow(const FloatSize&, float blur, const Color&, ColorSpace);
388
// Legacy shadow blur radius is used for canvas, and -webkit-box-shadow.
389
// It has different treatment of radii > 8px.
390
void setLegacyShadow(const FloatSize&, float blur, const Color&, ColorSpace);
392
bool getShadow(FloatSize&, float&, Color&, ColorSpace&) const;
395
void drawFocusRing(const Vector<IntRect>&, int width, int offset, const Color&);
396
void drawFocusRing(const Path&, int width, int offset, const Color&);
398
void setLineCap(LineCap);
399
void setLineDash(const DashArray&, float dashOffset);
400
void setLineJoin(LineJoin);
401
void setMiterLimit(float);
403
void setAlpha(float);
405
void setCompositeOperation(CompositeOperator);
406
CompositeOperator compositeOperation() const;
408
void clip(const Path&);
410
// This clip function is used only by <canvas> code. It allows
411
// implementations to handle clipping on the canvas differently since
412
// the discipline is different.
413
void canvasClip(const Path&);
414
void clipOut(const Path&);
416
void scale(const FloatSize&);
417
void rotate(float angleInRadians);
418
void translate(const FloatSize& size) { translate(size.width(), size.height()); }
419
void translate(float x, float y);
421
void setURLForRect(const KURL&, const IntRect&);
423
void concatCTM(const AffineTransform&);
424
void setCTM(const AffineTransform&);
426
enum IncludeDeviceScale { DefinitelyIncludeDeviceScale, PossiblyIncludeDeviceScale };
427
AffineTransform getCTM(IncludeDeviceScale includeScale = PossiblyIncludeDeviceScale) const;
429
#if ENABLE(3D_RENDERING) && USE(TEXTURE_MAPPER)
430
// This is needed when using accelerated-compositing in software mode, like in TextureMapper.
431
void concat3DTransform(const TransformationMatrix&);
432
void set3DTransform(const TransformationMatrix&);
433
TransformationMatrix get3DTransform() const;
435
// Create an image buffer compatible with this context, with suitable resolution
436
// for drawing into the buffer and then into this context.
437
PassOwnPtr<ImageBuffer> createCompatibleBuffer(const IntSize&) const;
438
bool isCompatibleWithBuffer(ImageBuffer*) const;
440
// This function applies the device scale factor to the context, making the context capable of
441
// acting as a base-level context for a HiDPI environment.
442
void applyDeviceScaleFactor(float);
443
void platformApplyDeviceScaleFactor(float);
446
HDC getWindowsContext(const IntRect&, bool supportAlphaBlend, bool mayCreateBitmap); // The passed in rect is used to create a bitmap for compositing inside transparency layers.
447
void releaseWindowsContext(HDC, const IntRect&, bool supportAlphaBlend, bool mayCreateBitmap); // The passed in HDC should be the one handed back by getWindowsContext.
450
void setBitmap(PassRefPtr<SharedBitmap>);
451
const AffineTransform& affineTransform() const;
452
AffineTransform& affineTransform();
453
void resetAffineTransform();
454
void fillRect(const FloatRect&, const Gradient*);
455
void drawText(const SimpleFontData* fontData, const GlyphBuffer& glyphBuffer, int from, int numGlyphs, const FloatPoint& point);
456
void drawFrameControl(const IntRect& rect, unsigned type, unsigned state);
457
void drawFocusRect(const IntRect& rect);
458
void paintTextField(const IntRect& rect, unsigned state);
459
void drawBitmap(SharedBitmap*, const IntRect& dstRect, const IntRect& srcRect, ColorSpace styleColorSpace, CompositeOperator compositeOp);
460
void drawBitmapPattern(SharedBitmap*, const FloatRect& tileRectIn, const AffineTransform& patternTransform, const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator op, const FloatRect& destRect, const IntSize& origSourceSize);
461
void drawIcon(HICON icon, const IntRect& dstRect, UINT flags);
462
void drawRoundCorner(bool newClip, RECT clipRect, RECT rectWin, HDC dc, int width, int height);
464
GraphicsContext(HDC, bool hasAlpha = false); // FIXME: To be removed.
466
// When set to true, child windows should be rendered into this context
467
// rather than allowing them just to render to the screen. Defaults to
469
// FIXME: This is a layering violation. GraphicsContext shouldn't know
470
// what a "window" is. It would be much more appropriate for this flag
471
// to be passed as a parameter alongside the GraphicsContext, but doing
472
// that would require lots of changes in cross-platform code that we
473
// aren't sure we want to make.
474
void setShouldIncludeChildWindows(bool);
475
bool shouldIncludeChildWindows() const;
477
class WindowsBitmap {
478
WTF_MAKE_NONCOPYABLE(WindowsBitmap);
480
WindowsBitmap(HDC, const IntSize&);
483
HDC hdc() const { return m_hdc; }
484
UInt8* buffer() const { return m_pixelData.buffer(); }
485
unsigned bufferLength() const { return m_pixelData.bufferLength(); }
486
const IntSize& size() const { return m_pixelData.size(); }
487
unsigned bytesPerRow() const { return m_pixelData.bytesPerRow(); }
488
unsigned short bitsPerPixel() const { return m_pixelData.bitsPerPixel(); }
489
const DIBPixelData& windowsDIB() const { return m_pixelData; }
494
DIBPixelData m_pixelData;
497
PassOwnPtr<WindowsBitmap> createWindowsBitmap(const IntSize&);
498
// The bitmap should be non-premultiplied.
499
void drawWindowsBitmap(WindowsBitmap*, const IntPoint&);
501
#else // PLATFORM(WIN)
502
bool shouldIncludeChildWindows() const { return false; }
503
#endif // PLATFORM(WIN)
504
#endif // OS(WINDOWS)
507
// This is needed because of a bug whereby getting an HDC from a GDI+ context
508
// loses the scale operations applied to the context.
509
FloatSize currentScale();
513
void pushTransparencyLayerInternal(const QRect&, qreal, QPixmap&);
514
void takeOwnershipOfPlatformContext();
518
ShadowBlur* shadowBlur();
522
GraphicsContext(cairo_t*);
526
void setGdkExposeEvent(GdkEventExpose*);
527
GdkWindow* gdkWindow() const;
528
GdkEventExpose* gdkExposeEvent() const;
531
static void adjustLineToPixelBoundaries(FloatPoint& p1, FloatPoint& p2, float strokeWidth, StrokeStyle);
534
void platformInit(PlatformGraphicsContext*);
535
void platformDestroy();
537
#if PLATFORM(WIN) && !OS(WINCE)
538
void platformInit(HDC, bool hasAlpha = false);
541
void savePlatformState();
542
void restorePlatformState();
544
void setPlatformTextDrawingMode(TextDrawingModeFlags);
545
void setPlatformFont(const Font& font);
547
void setPlatformStrokeColor(const Color&, ColorSpace);
548
void setPlatformStrokeStyle(StrokeStyle);
549
void setPlatformStrokeThickness(float);
551
void setPlatformFillColor(const Color&, ColorSpace);
553
void setPlatformShouldAntialias(bool);
554
void setPlatformShouldSmoothFonts(bool);
556
void setPlatformShadow(const FloatSize&, float blur, const Color&, ColorSpace);
557
void clearPlatformShadow();
559
void setPlatformCompositeOperation(CompositeOperator);
561
void beginPlatformTransparencyLayer(float opacity);
562
void endPlatformTransparencyLayer();
563
static bool supportsTransparencyLayers();
565
void fillEllipseAsPath(const FloatRect&);
566
void strokeEllipseAsPath(const FloatRect&);
568
void platformFillEllipse(const FloatRect&);
569
void platformStrokeEllipse(const FloatRect&);
571
GraphicsContextPlatformPrivate* m_data;
573
GraphicsContextState m_state;
574
Vector<GraphicsContextState> m_stack;
575
bool m_updatingControlTints;
576
unsigned m_transparencyCount;
579
class GraphicsContextStateSaver {
580
WTF_MAKE_FAST_ALLOCATED;
582
GraphicsContextStateSaver(GraphicsContext& context, bool saveAndRestore = true)
584
, m_saveAndRestore(saveAndRestore)
586
if (m_saveAndRestore)
590
~GraphicsContextStateSaver()
592
if (m_saveAndRestore)
598
ASSERT(!m_saveAndRestore);
600
m_saveAndRestore = true;
605
ASSERT(m_saveAndRestore);
607
m_saveAndRestore = false;
610
GraphicsContext* context() const { return &m_context; }
613
GraphicsContext& m_context;
614
bool m_saveAndRestore;
617
} // namespace WebCore
619
#endif // GraphicsContext_h