]> git.saurik.com Git - iphone-api.git/blob - WebCore/GraphicsContext.h
Adding the WebCore headers (for Cydget).
[iphone-api.git] / WebCore / GraphicsContext.h
1 /*
2 * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26 #ifndef GraphicsContext_h
27 #define GraphicsContext_h
28
29 #include "DashArray.h"
30 #include "FloatRect.h"
31 #include "Image.h"
32 #include "IntRect.h"
33 #include "Path.h"
34 #include "TextDirection.h"
35 #include <wtf/Noncopyable.h>
36 #include <wtf/Platform.h>
37
38 #if PLATFORM(CG)
39 typedef struct CGContext PlatformGraphicsContext;
40 #elif PLATFORM(CAIRO)
41 typedef struct _cairo PlatformGraphicsContext;
42 #elif PLATFORM(QT)
43 QT_BEGIN_NAMESPACE
44 class QPainter;
45 QT_END_NAMESPACE
46 typedef QPainter PlatformGraphicsContext;
47 #elif PLATFORM(WX)
48 class wxGCDC;
49 class wxWindowDC;
50
51 // wxGraphicsContext allows us to support Path, etc.
52 // but on some platforms, e.g. Linux, it requires fairly
53 // new software.
54 #if USE(WXGC)
55 // On OS X, wxGCDC is just a typedef for wxDC, so use wxDC explicitly to make
56 // the linker happy.
57 #ifdef __APPLE__
58 class wxDC;
59 typedef wxDC PlatformGraphicsContext;
60 #else
61 typedef wxGCDC PlatformGraphicsContext;
62 #endif
63 #else
64 typedef wxWindowDC PlatformGraphicsContext;
65 #endif
66 #elif PLATFORM(SKIA)
67 typedef class PlatformContextSkia PlatformGraphicsContext;
68 #else
69 typedef void PlatformGraphicsContext;
70 #endif
71
72 #if PLATFORM(GTK)
73 typedef struct _GdkDrawable GdkDrawable;
74 typedef struct _GdkEventExpose GdkEventExpose;
75 #endif
76
77 #if PLATFORM(WIN)
78 typedef struct HDC__* HDC;
79 #if !PLATFORM(CG)
80 // UInt8 is defined in CoreFoundation/CFBase.h
81 typedef unsigned char UInt8;
82 #endif
83 #endif
84
85 #if PLATFORM(QT) && defined(Q_WS_WIN)
86 #include <windows.h>
87 #endif
88
89 namespace WebCore {
90
91 const int cMisspellingLineThickness = 3;
92 const int cMisspellingLinePatternWidth = 4;
93 const int cMisspellingLinePatternGapWidth = 1;
94
95 class TransformationMatrix;
96 class Font;
97 class Generator;
98 class Gradient;
99 class GraphicsContextPrivate;
100 class GraphicsContextPlatformPrivate;
101 class ImageBuffer;
102 class KURL;
103 class Path;
104 class Pattern;
105 class TextRun;
106 class BidiStatus;
107
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;
113
114 enum StrokeStyle {
115 NoStroke,
116 SolidStroke,
117 DottedStroke,
118 DashedStroke
119 };
120
121 enum InterpolationQuality {
122 InterpolationDefault,
123 InterpolationNone,
124 InterpolationLow,
125 InterpolationMedium,
126 InterpolationHigh
127 };
128
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 {
132 SpreadMethodPad = 1,
133 SpreadMethodReflect = 2,
134 SpreadMethodRepeat = 3
135 };
136
137 struct IPhoneGradient
138 {
139 float * start;
140 float * end;
141 IPhoneGradient(float* aStart = NULL, float* anEnd = NULL) : start(aStart), end(anEnd) { }
142 };
143
144 typedef IPhoneGradient* IPhoneGradientRef;
145
146 enum Interpolation
147 {
148 LinearInterpolation,
149 ExponentialInterpolation
150 };
151
152 class GraphicsContext : Noncopyable {
153 public:
154 GraphicsContext(PlatformGraphicsContext*, bool setContextColors = true);
155 ~GraphicsContext();
156
157 PlatformGraphicsContext* platformContext() const;
158
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>);
167
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);
177
178 void setShouldAntialias(bool);
179 bool shouldAntialias() const;
180
181 #if PLATFORM(CG)
182 void applyStrokePattern();
183 void applyFillPattern();
184 #endif
185
186 void save();
187 void restore();
188
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);
192
193 void drawJoinedLines(CGPoint points [], unsigned count, bool antialias, CGLineCap lineCap = kCGLineCapButt);
194
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);
199
200 void drawPath();
201 void fillPath();
202 void strokePath();
203
204 // Arc drawing (used by border-radius in CSS) just supports stroking at the moment.
205 void strokeArc(const IntRect&, int startAngle, int angleSpan);
206
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&);
211
212 void clearRect(const FloatRect&);
213
214 void strokeRect(const FloatRect&);
215 void strokeRect(const FloatRect&, float lineWidth);
216
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);
228
229 void setImageInterpolationQuality(InterpolationQuality);
230 InterpolationQuality imageInterpolationQuality() const;
231
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*);
241
242 int textDrawingMode();
243 void setTextDrawingMode(int);
244
245 bool emojiDrawingEnabled();
246 void setEmojiDrawingEnabled(bool emojiDrawingEnabled);
247
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);
251
252 FloatRect roundToDevicePixels(const FloatRect&);
253
254 void drawLineForText(const IntPoint&, int width, bool printing);
255 void drawLineForMisspellingOrBadGrammar(const IntPoint&, int width, bool grammar);
256
257 bool paintingDisabled() const;
258 void setPaintingDisabled(bool);
259
260 bool updatingControlTints() const;
261 void setUpdatingControlTints(bool);
262
263 void beginTransparencyLayer(float opacity);
264 void endTransparencyLayer();
265
266 void setShadow(const IntSize&, int blur, const Color&);
267 bool getShadow(IntSize&, int&, Color&) const;
268 void clearShadow();
269
270 void initFocusRing(int width, int offset);
271 void addFocusRingRect(const IntRect&);
272 void drawFocusRing(const Color&);
273 void clearFocusRing();
274 IntRect focusRingBoundingRect();
275
276 void setLineCap(LineCap);
277 void setLineDash(const DashArray&, float dashOffset);
278 void setLineJoin(LineJoin);
279 void setMiterLimit(float);
280
281 void setAlpha(float);
282 #if PLATFORM(CAIRO)
283 float getAlpha();
284 #endif
285
286 void setCompositeOperation(CompositeOperator);
287
288 void beginPath();
289 void addPath(const Path&);
290
291 void clip(const Path&);
292 void clipOut(const Path&);
293
294 void scale(const FloatSize&);
295 void rotate(float angleInRadians);
296 void translate(float x, float y);
297 IntPoint origin();
298
299 void setURLForRect(const KURL&, const IntRect&);
300
301 void concatCTM(const TransformationMatrix&);
302 TransformationMatrix getCTM() const;
303
304 #if PLATFORM(WIN)
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.
309
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
312 // false.
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;
320
321 class WindowsBitmap : public Noncopyable {
322 public:
323 WindowsBitmap(HDC, IntSize);
324 ~WindowsBitmap();
325
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; }
331
332 private:
333 HDC m_hdc;
334 HBITMAP m_bitmap;
335 UInt8* m_bitmapBuffer;
336 unsigned m_bitmapBufferLength;
337 IntSize m_size;
338 unsigned m_bytesPerRow;
339 };
340
341 WindowsBitmap* createWindowsBitmap(IntSize);
342 // The bitmap should be non-premultiplied.
343 void drawWindowsBitmap(WindowsBitmap*, const IntPoint&);
344 #endif
345
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);
349 #endif
350
351 #if PLATFORM(QT)
352 bool inTransparencyLayer() const;
353 PlatformPath* currentPath();
354 QPen pen();
355 #endif
356
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);
360
361 #if PLATFORM(GTK)
362 void setGdkExposeEvent(GdkEventExpose*);
363 GdkDrawable* gdkDrawable() const;
364 GdkEventExpose* gdkExposeEvent() const;
365 #endif
366
367 private:
368 void savePlatformState();
369 void restorePlatformState();
370
371 void setPlatformTextDrawingMode(int);
372 void setPlatformFont(const Font& font);
373
374 void setPlatformStrokeColor(const Color&);
375 void setPlatformStrokeStyle(const StrokeStyle&);
376 void setPlatformStrokeThickness(float);
377
378 void setPlatformFillColor(const Color&);
379
380 void setPlatformShouldAntialias(bool b);
381
382 void setPlatformShadow(const IntSize&, int blur, const Color&);
383 void clearPlatformShadow();
384
385 int focusRingWidth() const;
386 int focusRingOffset() const;
387 const Vector<IntRect>& focusRingRects() const;
388
389 static GraphicsContextPrivate* createGraphicsContextPrivate();
390 static void destroyGraphicsContextPrivate(GraphicsContextPrivate*);
391
392 GraphicsContextPrivate* m_common;
393 GraphicsContextPlatformPrivate* m_data; // Deprecated; m_commmon can just be downcasted. To be removed.
394 };
395
396 } // namespace WebCore
397
398 #endif // GraphicsContext_h