• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2003, 2006, 2007 Apple Inc.  All rights reserved.
3  * Copyright (C) 2008-2009 Torch Mobile, Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
25  */
26 
27 #ifndef GraphicsContext_h
28 #define GraphicsContext_h
29 
30 #include "DashArray.h"
31 #include "FloatRect.h"
32 #include "Image.h"
33 #include "IntRect.h"
34 #include "Path.h"
35 #include "TextDirection.h"
36 #include <wtf/Noncopyable.h>
37 #include <wtf/Platform.h>
38 
39 #if PLATFORM(CG)
40 typedef struct CGContext PlatformGraphicsContext;
41 #elif PLATFORM(CAIRO)
42 typedef struct _cairo PlatformGraphicsContext;
43 #elif PLATFORM(QT)
44 QT_BEGIN_NAMESPACE
45 class QPainter;
46 QT_END_NAMESPACE
47 typedef QPainter PlatformGraphicsContext;
48 #elif PLATFORM(SGL)
49 namespace WebCore {
50 class PlatformGraphicsContext;
51 }
52 class SkPaint;
53 struct SkPoint;
54 #elif PLATFORM(WX)
55 class wxGCDC;
56 class wxWindowDC;
57 
58 // wxGraphicsContext allows us to support Path, etc.
59 // but on some platforms, e.g. Linux, it requires fairly
60 // new software.
61 #if USE(WXGC)
62 // On OS X, wxGCDC is just a typedef for wxDC, so use wxDC explicitly to make
63 // the linker happy.
64 #ifdef __APPLE__
65     class wxDC;
66     typedef wxDC PlatformGraphicsContext;
67 #else
68     typedef wxGCDC PlatformGraphicsContext;
69 #endif
70 #else
71     typedef wxWindowDC PlatformGraphicsContext;
72 #endif
73 #elif PLATFORM(SKIA)
74 typedef class PlatformContextSkia PlatformGraphicsContext;
75 #elif PLATFORM(WINCE)
76 typedef struct HDC__ PlatformGraphicsContext;
77 #else
78 typedef void PlatformGraphicsContext;
79 #endif
80 
81 #if PLATFORM(GTK)
82 typedef struct _GdkDrawable GdkDrawable;
83 typedef struct _GdkEventExpose GdkEventExpose;
84 #endif
85 
86 #if PLATFORM(WIN)
87 typedef struct HDC__* HDC;
88 #if !PLATFORM(CG)
89 // UInt8 is defined in CoreFoundation/CFBase.h
90 typedef unsigned char UInt8;
91 #endif
92 #endif
93 
94 #if PLATFORM(QT) && defined(Q_WS_WIN)
95 #include <windows.h>
96 #endif
97 
98 namespace WebCore {
99 
100 #if PLATFORM(WINCE) && !PLATFORM(QT)
101     class SharedBitmap;
102     class SimpleFontData;
103     class GlyphBuffer;
104 #endif
105 
106     const int cMisspellingLineThickness = 3;
107     const int cMisspellingLinePatternWidth = 4;
108     const int cMisspellingLinePatternGapWidth = 1;
109 
110     class TransformationMatrix;
111     class Font;
112     class Generator;
113     class Gradient;
114     class GraphicsContextPrivate;
115     class GraphicsContextPlatformPrivate;
116     class ImageBuffer;
117     class KURL;
118     class Path;
119     class Pattern;
120     class TextRun;
121 
122     // These bits can be ORed together for a total of 8 possible text drawing modes.
123     const int cTextInvisible = 0;
124     const int cTextFill = 1;
125     const int cTextStroke = 2;
126     const int cTextClip = 4;
127 
128     enum StrokeStyle {
129         NoStroke,
130         SolidStroke,
131         DottedStroke,
132         DashedStroke
133     };
134 
135 // FIXME: This is a place-holder until we decide to add
136 // real color space support to WebCore.  At that time, ColorSpace will be a
137 // class and instances will be held  off of Colors.   There will be
138 // special singleton Gradient and Pattern color spaces to mark when
139 // a fill or stroke is using a gradient or pattern instead of a solid color.
140 // https://bugs.webkit.org/show_bug.cgi?id=20558
141     enum ColorSpace {
142         SolidColorSpace,
143         PatternColorSpace,
144         GradientColorSpace
145     };
146 
147     enum InterpolationQuality {
148         InterpolationDefault,
149         InterpolationNone,
150         InterpolationLow,
151         InterpolationMedium,
152         InterpolationHigh
153     };
154 
155     class GraphicsContext : public Noncopyable {
156     public:
157         GraphicsContext(PlatformGraphicsContext*);
158         ~GraphicsContext();
159 
160 #if !PLATFORM(WINCE) || PLATFORM(QT)
161         PlatformGraphicsContext* platformContext() const;
162 #endif
163 
164         float strokeThickness() const;
165         void setStrokeThickness(float);
166         StrokeStyle strokeStyle() const;
167         void setStrokeStyle(const StrokeStyle& style);
168         Color strokeColor() const;
169         void setStrokeColor(const Color&);
170 
171         ColorSpace strokeColorSpace() const;
172 
173         void setStrokePattern(PassRefPtr<Pattern>);
174         Pattern* strokePattern() const;
175 
176         void setStrokeGradient(PassRefPtr<Gradient>);
177         Gradient* strokeGradient() const;
178 
179         WindRule fillRule() const;
180         void setFillRule(WindRule);
181         Color fillColor() const;
182         void setFillColor(const Color&);
183 
184         void setFillPattern(PassRefPtr<Pattern>);
185         Pattern* fillPattern() const;
186 
187         void setFillGradient(PassRefPtr<Gradient>);
188         Gradient* fillGradient() const;
189 
190         ColorSpace fillColorSpace() const;
191 
192         void setShadowsIgnoreTransforms(bool);
193 
194         void setShouldAntialias(bool);
195         bool shouldAntialias() const;
196 
197 #if PLATFORM(CG)
198         void applyStrokePattern();
199         void applyFillPattern();
200 #endif
201 
202 #if PLATFORM(SGL)
203         /* these should be pused to apple. needed for CanvasStyle.cpp */
204         void setCMYKAFillColor(float c, float m, float y, float k, float a);
205         void setCMYKAStrokeColor(float c, float m, float y, float k, float a);
206 
207         // initialize a paint for filling
208         void setupFillPaint(SkPaint*);
209         // initialize a paint for stroking
210         void setupStrokePaint(SkPaint*);
211         // initialize a paint for a shadow, or if false is returned, the
212         // parameters are left untouched
213         bool setupShadowPaint(SkPaint* paint, SkPoint* offset);
214         // returns true if there is a valid (non-transparent) fill color
215         bool willFill() const;
216         // returns true if there is a valid (non-transparent) stroke color
217         bool willStroke() const;
218 
219         // may return NULL, since we lazily allocate the path. This is the path
220         // that is drawn by drawPath()
221         const SkPath* getCurrPath() const;
222 
223         /** platform-specific factory method to return a bitmap graphicscontext,
224          called by <canvas> when we need to draw offscreen. Caller is responsible for
225          deleting the context. Use drawOffscreenContext() to draw the context's image
226          onto another graphics context.
227          */
228         static GraphicsContext* createOffscreenContext(int width, int height);
229 #endif
230 
231         void save();
232         void restore();
233 
234         // These draw methods will do both stroking and filling.
235         // FIXME: ...except drawRect(), which fills properly but always strokes
236         // using a 1-pixel stroke inset from the rect borders (of the correct
237         // stroke color).
238         void drawRect(const IntRect&);
239         void drawLine(const IntPoint&, const IntPoint&);
240         void drawEllipse(const IntRect&);
241         void drawConvexPolygon(size_t numPoints, const FloatPoint*, bool shouldAntialias = false);
242 
243         void drawPath();
244         void fillPath();
245         void strokePath();
246 
247         // Arc drawing (used by border-radius in CSS) just supports stroking at the moment.
248         void strokeArc(const IntRect&, int startAngle, int angleSpan);
249 
250         void fillRect(const FloatRect&);
251         void fillRect(const FloatRect&, const Color&);
252         void fillRect(const FloatRect&, Generator&);
253         void fillRoundedRect(const IntRect&, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color&);
254 
255         void clearRect(const FloatRect&);
256 
257         void strokeRect(const FloatRect&);
258         void strokeRect(const FloatRect&, float lineWidth);
259 
260         void drawImage(Image*, const IntPoint&, CompositeOperator = CompositeSourceOver);
261         void drawImage(Image*, const IntRect&, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
262         void drawImage(Image*, const IntPoint& destPoint, const IntRect& srcRect, CompositeOperator = CompositeSourceOver);
263         void drawImage(Image*, const IntRect& destRect, const IntRect& srcRect, CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
264         void drawImage(Image*, const FloatRect& destRect, const FloatRect& srcRect = FloatRect(0, 0, -1, -1),
265                        CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
266         void drawTiledImage(Image*, const IntRect& destRect, const IntPoint& srcPoint, const IntSize& tileSize,
267                        CompositeOperator = CompositeSourceOver);
268         void drawTiledImage(Image*, const IntRect& destRect, const IntRect& srcRect,
269                             Image::TileRule hRule = Image::StretchTile, Image::TileRule vRule = Image::StretchTile,
270                             CompositeOperator = CompositeSourceOver);
271 
272         void setImageInterpolationQuality(InterpolationQuality);
273         InterpolationQuality imageInterpolationQuality() const;
274 
275         void clip(const FloatRect&);
276         void addRoundedRectClip(const IntRect&, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight);
277         void addInnerRoundedRectClip(const IntRect&, int thickness);
278         void clipOut(const IntRect&);
279         void clipOutEllipseInRect(const IntRect&);
280         void clipOutRoundedRect(const IntRect&, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight);
281         void clipPath(WindRule);
282         void clipToImageBuffer(const FloatRect&, const ImageBuffer*);
283 
284         int textDrawingMode();
285         void setTextDrawingMode(int);
286 
287         void drawText(const Font&, const TextRun&, const IntPoint&, int from = 0, int to = -1);
288         void drawBidiText(const Font&, const TextRun&, const FloatPoint&);
289         void drawHighlightForText(const Font&, const TextRun&, const IntPoint&, int h, const Color& backgroundColor, int from = 0, int to = -1);
290 
291         FloatRect roundToDevicePixels(const FloatRect&);
292 
293         void drawLineForText(const IntPoint&, int width, bool printing);
294         void drawLineForMisspellingOrBadGrammar(const IntPoint&, int width, bool grammar);
295 
296         bool paintingDisabled() const;
297         void setPaintingDisabled(bool);
298 
299         bool updatingControlTints() const;
300         void setUpdatingControlTints(bool);
301 
302         void beginTransparencyLayer(float opacity);
303         void endTransparencyLayer();
304 
305         void setShadow(const IntSize&, int blur, const Color&);
306         bool getShadow(IntSize&, int&, Color&) const;
307         void clearShadow();
308 
309         void initFocusRing(int width, int offset);
310         void addFocusRingRect(const IntRect&);
311         void drawFocusRing(const Color&);
312         void clearFocusRing();
313         IntRect focusRingBoundingRect();
314 
315         void setLineCap(LineCap);
316         void setLineDash(const DashArray&, float dashOffset);
317         void setLineJoin(LineJoin);
318         void setMiterLimit(float);
319 
320         void setAlpha(float);
321 #if PLATFORM(CAIRO)
322         float getAlpha();
323 #endif
324 
325         void setCompositeOperation(CompositeOperator);
326 
327         void beginPath();
328         void addPath(const Path&);
329 
330         void clip(const Path&);
331         void clipOut(const Path&);
332 
333         void scale(const FloatSize&);
334         void rotate(float angleInRadians);
335         void translate(float x, float y);
336         IntPoint origin();
337 
338         void setURLForRect(const KURL&, const IntRect&);
339 
340         void concatCTM(const TransformationMatrix&);
341         TransformationMatrix getCTM() const;
342 
343 #if PLATFORM(WINCE) && !PLATFORM(QT)
344         void setBitmap(PassRefPtr<SharedBitmap>);
345         const TransformationMatrix& affineTransform() const;
346         TransformationMatrix& affineTransform();
347         void resetAffineTransform();
348         void fillRect(const FloatRect&, const Gradient*);
349         void drawText(const SimpleFontData* fontData, const GlyphBuffer& glyphBuffer, int from, int numGlyphs, const FloatPoint& point);
350         void drawFrameControl(const IntRect& rect, unsigned type, unsigned state);
351         void drawFocusRect(const IntRect& rect);
352         void paintTextField(const IntRect& rect, unsigned state);
353         void drawBitmap(SharedBitmap*, const IntRect& dstRect, const IntRect& srcRect, CompositeOperator compositeOp);
354         void drawBitmapPattern(SharedBitmap*, const FloatRect& tileRectIn, const TransformationMatrix& patternTransform, const FloatPoint& phase, CompositeOperator op, const FloatRect& destRect, const IntSize& origSourceSize);
355         void drawIcon(HICON icon, const IntRect& dstRect, UINT flags);
356         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.
357         void releaseWindowsContext(HDC, const IntRect&, bool supportAlphaBlend = false, bool mayCreateBitmap = true);    // The passed in HDC should be the one handed back by getWindowsContext.
358         void drawRoundCorner(bool newClip, RECT clipRect, RECT rectWin, HDC dc, int width, int height);
359 #elif PLATFORM(WIN)
360         GraphicsContext(HDC, bool hasAlpha = false); // FIXME: To be removed.
361         bool inTransparencyLayer() const;
362         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.
363         void releaseWindowsContext(HDC, const IntRect&, bool supportAlphaBlend = true, bool mayCreateBitmap = true);    // The passed in HDC should be the one handed back by getWindowsContext.
364 
365         // When set to true, child windows should be rendered into this context
366         // rather than allowing them just to render to the screen. Defaults to
367         // false.
368         // FIXME: This is a layering violation. GraphicsContext shouldn't know
369         // what a "window" is. It would be much more appropriate for this flag
370         // to be passed as a parameter alongside the GraphicsContext, but doing
371         // that would require lots of changes in cross-platform code that we
372         // aren't sure we want to make.
373         void setShouldIncludeChildWindows(bool);
374         bool shouldIncludeChildWindows() const;
375 
376         class WindowsBitmap : public Noncopyable {
377         public:
378             WindowsBitmap(HDC, IntSize);
379             ~WindowsBitmap();
380 
hdc()381             HDC hdc() const { return m_hdc; }
buffer()382             UInt8* buffer() const { return m_bitmapBuffer; }
bufferLength()383             unsigned bufferLength() const { return m_bitmapBufferLength; }
size()384             IntSize size() const { return m_size; }
bytesPerRow()385             unsigned bytesPerRow() const { return m_bytesPerRow; }
386 
387         private:
388             HDC m_hdc;
389             HBITMAP m_bitmap;
390             UInt8* m_bitmapBuffer;
391             unsigned m_bitmapBufferLength;
392             IntSize m_size;
393             unsigned m_bytesPerRow;
394         };
395 
396         WindowsBitmap* createWindowsBitmap(IntSize);
397         // The bitmap should be non-premultiplied.
398         void drawWindowsBitmap(WindowsBitmap*, const IntPoint&);
399 #endif
400 
401 #if PLATFORM(QT) && defined(Q_WS_WIN)
402         HDC getWindowsContext(const IntRect&, bool supportAlphaBlend = true, bool mayCreateBitmap = true);
403         void releaseWindowsContext(HDC, const IntRect&, bool supportAlphaBlend = true, bool mayCreateBitmap = true);
shouldIncludeChildWindows()404         bool shouldIncludeChildWindows() const { return false; }
405 #endif
406 
407 #if PLATFORM(QT)
408         bool inTransparencyLayer() const;
409         PlatformPath* currentPath();
410         QPen pen();
411 #endif
412 
413 #if PLATFORM(GTK)
414         void setGdkExposeEvent(GdkEventExpose*);
415         GdkDrawable* gdkDrawable() const;
416         GdkEventExpose* gdkExposeEvent() const;
417 #endif
418 
419     private:
420         void savePlatformState();
421         void restorePlatformState();
422 
423         void setPlatformTextDrawingMode(int);
424         void setPlatformFont(const Font& font);
425 
426         void setPlatformStrokeColor(const Color&);
427         void setPlatformStrokeStyle(const StrokeStyle&);
428         void setPlatformStrokeThickness(float);
429         void setPlatformStrokeGradient(Gradient*);
430         void setPlatformStrokePattern(Pattern*);
431 
432         void setPlatformFillColor(const Color&);
433         void setPlatformFillGradient(Gradient*);
434         void setPlatformFillPattern(Pattern*);
435 
436         void setPlatformShouldAntialias(bool b);
437 
438         void setPlatformShadow(const IntSize&, int blur, const Color&);
439         void clearPlatformShadow();
440 
441         int focusRingWidth() const;
442         int focusRingOffset() const;
443         const Vector<IntRect>& focusRingRects() const;
444 
445         static GraphicsContextPrivate* createGraphicsContextPrivate();
446         static void destroyGraphicsContextPrivate(GraphicsContextPrivate*);
447 
448         GraphicsContextPrivate* m_common;
449         GraphicsContextPlatformPrivate* m_data; // Deprecated; m_commmon can just be downcasted. To be removed.
450     };
451 
452 } // namespace WebCore
453 
454 #endif // GraphicsContext_h
455 
456