1
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2
* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is Oracle Corporation code.
17
* The Initial Developer of the Original Code is Oracle Corporation.
18
* Portions created by the Initial Developer are Copyright (C) 2005
19
* the Initial Developer. All Rights Reserved.
22
* Stuart Parmenter <pavlov@pavlov.net>
23
* Vladimir Vukicevic <vladimir@pobox.com>
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the MPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the MPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
44
#include "gfxASurface.h"
48
#include "gfxMatrix.h"
49
#include "gfxPattern.h"
52
typedef struct _cairo cairo_t;
55
* This is the main class for doing actual drawing. It is initialized using
56
* a surface and can be drawn on. It manages various state information like
57
* a current transformation matrix (CTM), a current path, current color,
60
* All drawing happens by creating a path and then stroking or filling it.
61
* The functions like Rectangle and Arc do not do any drawing themselves.
62
* When a path is drawn (stroked or filled), it is filled/stroked with a
63
* pattern set by SetPattern, SetColor or SetSource.
65
* Note that the gfxContext takes coordinates in device pixels,
66
* as opposed to app units.
68
class THEBES_API gfxContext {
69
THEBES_INLINE_DECL_REFCOUNTING(gfxContext)
73
* Initialize this context from a surface.
75
gfxContext(gfxASurface *surface);
79
* Return the surface that this gfxContext was created with
81
gfxASurface *OriginalSurface();
84
* Return the current transparency group target, if any, along
85
* with its device offsets from the top. If no group is
86
* active, returns the surface the gfxContext was created with,
89
already_AddRefed<gfxASurface> CurrentSurface(gfxFloat *dx, gfxFloat *dy);
90
already_AddRefed<gfxASurface> CurrentSurface() {
91
return CurrentSurface(NULL, NULL);
95
* Return the raw cairo_t object.
96
* XXX this should go away at some point.
98
cairo_t *GetCairo() { return mCairo; }
103
// XXX document exactly what bits are saved
112
* Stroke the current path using the current settings (such as line
114
* A path is set up using functions such as Line, Rectangle and Arc.
116
* Does not consume the current path.
120
* Fill the current path according to the current settings.
122
* Does not consume the current path.
127
* Forgets the current path.
132
* Closes the path, i.e. connects the last drawn point to the first one.
134
* Filling a path will implicitly close it.
139
* Moves the pen to a new point without drawing a line.
141
void MoveTo(const gfxPoint& pt);
144
* Creates a new subpath starting at the current point.
145
* Equivalent to MoveTo(CurrentPoint()).
150
* Returns the current point in the current path.
152
gfxPoint CurrentPoint() const;
155
* Draws a line from the current point to pt.
159
void LineTo(const gfxPoint& pt);
162
* Draws a quadratic Bézier curve with control points pt1, pt2 and pt3.
164
void CurveTo(const gfxPoint& pt1, const gfxPoint& pt2, const gfxPoint& pt3);
167
* Draws a clockwise arc (i.e. a circle segment).
168
* @param center The center of the circle
169
* @param radius The radius of the circle
170
* @param angle1 Starting angle for the segment
171
* @param angle2 Ending angle
173
void Arc(const gfxPoint& center, gfxFloat radius,
174
gfxFloat angle1, gfxFloat angle2);
177
* Draws a counter-clockwise arc (i.e. a circle segment).
178
* @param center The center of the circle
179
* @param radius The radius of the circle
180
* @param angle1 Starting angle for the segment
181
* @param angle2 Ending angle
184
void NegativeArc(const gfxPoint& center, gfxFloat radius,
185
gfxFloat angle1, gfxFloat angle2);
189
* Draws a line from start to end.
191
void Line(const gfxPoint& start, const gfxPoint& end); // XXX snapToPixels option?
194
* Draws the rectangle given by rect.
195
* @param snapToPixels ?
197
void Rectangle(const gfxRect& rect, PRBool snapToPixels = PR_FALSE);
198
void Ellipse(const gfxPoint& center, const gfxSize& dimensions);
199
void Polygon(const gfxPoint *points, PRUint32 numPoints);
202
** Transformation Matrix manipulation
206
* Adds a translation to the current matrix. This translation takes place
207
* before the previously set transformations.
209
void Translate(const gfxPoint& pt);
212
* Adds a scale to the current matrix. This scaling takes place before the
213
* previously set transformations.
215
void Scale(gfxFloat x, gfxFloat y);
218
* Adds a rotation around the origin to the current matrix. This rotation
219
* takes place before the previously set transformations.
221
* @param angle The angle in radians.
223
void Rotate(gfxFloat angle);
226
* Post-multiplies 'other' onto the current CTM, i.e. this
227
* matrix's transformation will take place before the previously set
230
void Multiply(const gfxMatrix& other);
233
* Replaces the current transformation matrix with matrix.
235
void SetMatrix(const gfxMatrix& matrix);
238
* Sets the transformation matrix to the identity matrix.
240
void IdentityMatrix();
243
* Returns the current transformation matrix.
245
gfxMatrix CurrentMatrix() const;
248
* Converts a point from device to user coordinates using the inverse
249
* transformation matrix.
251
gfxPoint DeviceToUser(const gfxPoint& point) const;
254
* Converts a size from device to user coordinates. This does not apply
255
* translation components of the matrix.
257
gfxSize DeviceToUser(const gfxSize& size) const;
260
* Converts a rectangle from device to user coordinates; this has the
261
* same effect as using DeviceToUser on both the rectangle's point and
264
gfxRect DeviceToUser(const gfxRect& rect) const;
267
* Converts a point from user to device coordinates using the inverse
268
* transformation matrix.
270
gfxPoint UserToDevice(const gfxPoint& point) const;
273
* Converts a size from user to device coordinates. This does not apply
274
* translation components of the matrix.
276
gfxSize UserToDevice(const gfxSize& size) const;
279
* Converts a rectangle from user to device coordinates; this has the
280
* same effect as using UserToDevice on both the rectangle's point and
283
gfxRect UserToDevice(const gfxRect& rect) const;
286
* Takes the given rect and tries to align it to device pixels. If
287
* this succeeds, the method will return PR_TRUE, and the rect will
288
* be in device coordinates (already transformed by the CTM). If it
289
* fails, the method will return PR_FALSE, and the rect will not be
292
* If ignoreScale is PR_TRUE, then snapping will take place even if
293
* the CTM has a scale applied. Snapping never takes place if
294
* there is a rotation in the CTM.
296
PRBool UserToDevicePixelSnapped(gfxRect& rect, PRBool ignoreScale = PR_FALSE) const;
299
* Attempts to pixel snap the rectangle, add it to the current
300
* path, and to set pattern as the current painting source. This
301
* should be used for drawing filled pixel-snapped rectangles (like
302
* images), because the CTM at the time of the SetPattern call needs
303
* to have a snapped translation, or you get smeared images.
305
void PixelSnappedRectangleAndSetPattern(const gfxRect& rect, gfxPattern *pattern);
312
* Uses a solid color for drawing.
314
void SetColor(const gfxRGBA& c);
317
* Gets the current color.
318
* returns PR_FALSE if there is something other than a color
319
* set as the current source (pattern, surface, etc)
321
PRBool GetColor(gfxRGBA& c);
324
* Uses a surface for drawing. This is a shorthand for creating a
325
* pattern and setting it.
329
void SetSource(gfxASurface *surface, const gfxPoint& offset = gfxPoint(0.0, 0.0));
332
* Uses a pattern for drawing.
334
void SetPattern(gfxPattern *pattern);
337
* Get the source pattern (solid color, normal pattern, surface, etc)
339
already_AddRefed<gfxPattern> GetPattern();
345
* Paints the current source surface/pattern everywhere in the current
348
void Paint(gfxFloat alpha = 1.0);
351
** Painting with a Mask
354
* Like Paint, except that it only draws the source where pattern is
357
void Mask(gfxPattern *pattern);
360
* Shorthand for creating a pattern and calling the pattern-taking
363
void Mask(gfxASurface *surface, const gfxPoint& offset = gfxPoint(0.0, 0.0));
370
* Creates a new path with a rectangle from 0,0 to size.w,size.h
371
* and calls cairo_fill.
373
void DrawSurface(gfxASurface *surface, const gfxSize& size);
385
void SetDash(gfxLineType ltype);
386
void SetDash(gfxFloat *dashes, int ndash, gfxFloat offset);
387
//void getDash() const;
390
* Sets the line width that's used for line drawing.
392
void SetLineWidth(gfxFloat width);
395
* Returns the currently set line width.
399
gfxFloat CurrentLineWidth() const;
401
enum GraphicsLineCap {
407
* Sets the line caps, i.e. how line endings are drawn.
409
void SetLineCap(GraphicsLineCap cap);
410
GraphicsLineCap CurrentLineCap() const;
412
enum GraphicsLineJoin {
418
* Sets the line join, i.e. how the connection between two lines is
421
void SetLineJoin(GraphicsLineJoin join);
422
GraphicsLineJoin CurrentLineJoin() const;
424
void SetMiterLimit(gfxFloat limit);
425
gfxFloat CurrentMiterLimit() const;
435
void SetFillRule(FillRule rule);
436
FillRule CurrentFillRule() const;
439
** Operators and Rendering control
442
// define enum for operators (clear, src, dst, etc)
443
enum GraphicsOperator {
463
* Sets the operator used for all further drawing. The operator affects
464
* how drawing something will modify the destination. For example, the
465
* OVER operator will do alpha blending of source and destination, while
466
* SOURCE will replace the destination with the source.
468
* Note that if the flag FLAG_SIMPLIFY_OPERATORS is set on this
469
* gfxContext, the actual operator set might change for optimization
470
* purposes. Check the comments below around that flag.
472
void SetOperator(GraphicsOperator op);
473
GraphicsOperator CurrentOperator() const;
476
* MODE_ALIASED means that only pixels whose centers are in the drawn area
477
* should be modified, and they should be modified to take the value drawn
478
* at the pixel center.
484
void SetAntialiasMode(AntialiasMode mode);
485
AntialiasMode CurrentAntialiasMode() const;
492
* Clips all further drawing to the current path.
493
* This does not consume the current path.
498
* Undoes any clipping. Further drawings will only be restricted by the
499
* surface dimensions.
504
* Helper functions that will create a rect path and call Clip().
505
* Any current path will be destroyed by these functions!
507
void Clip(const gfxRect& rect); // will clip to a rect
510
* This will ensure that the surface actually has its clip set.
511
* Useful if you are doing native drawing.
513
void UpdateSurfaceClip();
516
* This will return the current bounds of the clip region.
518
gfxRect GetClipExtents();
523
void PushGroup(gfxASurface::gfxContentType content = gfxASurface::CONTENT_COLOR);
524
already_AddRefed<gfxPattern> PopGroup();
525
void PopGroupToSource();
528
** Hit Testing - check if given point is in the current path
530
PRBool PointInFill(const gfxPoint& pt);
531
PRBool PointInStroke(const gfxPoint& pt);
534
** Extents - returns user space extent of current path
536
gfxRect GetUserFillExtent();
537
gfxRect GetUserStrokeExtent();
540
** Obtaining a "flattened" path - path converted to all line segments
542
already_AddRefed<gfxFlattenedPath> GetFlattenedPath();
549
/* If this flag is set, operators other than CLEAR, SOURCE, or
550
* OVER will be converted to OVER before being sent to cairo.
552
* This is most useful with a printing surface, where
553
* operators such as ADD are used to avoid seams for on-screen
554
* display, but where such errors aren't noticable in print.
555
* This approach is currently used in border rendering.
557
* However, when printing complex renderings such as SVG,
558
* care should be taken to clear this flag.
560
FLAG_SIMPLIFY_OPERATORS = (1 << 0)
563
void SetFlag(PRInt32 aFlag) { mFlags |= aFlag; }
564
void ClearFlag(PRInt32 aFlag) { mFlags &= ~aFlag; }
565
PRInt32 GetFlags() { return mFlags; }
569
nsRefPtr<gfxASurface> mSurface;
573
#endif /* GFX_CONTEXT_H */