Back to index

lightning-sunbird  0.9+nobinonly
gfxContext.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  * ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Oracle Corporation code.
00016  *
00017  * The Initial Developer of the Original Code is Oracle Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2005
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Stuart Parmenter <pavlov@pavlov.net>
00023  *   Vladimir Vukicevic <vladimir@pobox.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #ifndef GFX_CONTEXT_H
00040 #define GFX_CONTEXT_H
00041 
00042 #include <cairo.h>
00043 
00044 #include "gfxASurface.h"
00045 #include "gfxColor.h"
00046 #include "gfxPoint.h"
00047 #include "gfxRect.h"
00048 #include "gfxTypes.h"
00049 #include "gfxMatrix.h"
00050 #include "gfxPattern.h"
00051 
00052 class gfxRegion;
00053 class gfxFilter;
00054 class gfxTextRun;
00055 
00056 class gfxContext {
00057     THEBES_DECL_REFCOUNTING
00058 
00059 public:
00060     gfxContext(gfxASurface *surface);
00061     ~gfxContext();
00062 
00063     // this does not addref
00064     gfxASurface* CurrentSurface();
00065     cairo_t* GetCairo() { return mCairo; }
00066 
00070     // XXX document exactly what bits are saved
00071     void Save();
00072     void Restore();
00073 
00078     // these do not consume the current path
00079     void Stroke();
00080     void Fill();
00081 
00082     void NewPath();
00083     void ClosePath();
00084 
00085     void MoveTo(gfxPoint pt);
00086     void LineTo(gfxPoint pt);
00087     void CurveTo(gfxPoint pt1, gfxPoint pt2, gfxPoint pt3);
00088 
00089     // clockwise arc
00090     void Arc(gfxPoint center, gfxFloat radius,
00091              gfxFloat angle1, gfxFloat angle2);
00092 
00093     // counterclockwise arc
00094     void NegativeArc(gfxPoint center, gfxFloat radius,
00095                      gfxFloat angle1, gfxFloat angle2);
00096 
00097     // path helpers
00098     void Line(gfxPoint start, gfxPoint end); // XXX snapToPixels option?
00099     void Rectangle(gfxRect rect, PRBool snapToPixels = PR_FALSE);
00100     void Ellipse(gfxPoint center, gfxSize dimensions);
00101     void Polygon(const gfxPoint *points, PRUint32 numPoints);
00102 
00107     // Add the text outline to the current path
00108     void AddStringToPath(gfxTextRun& text, int pos, int len);
00109 
00110     // Draw a substring of the text run at the current point
00111     void DrawString(gfxTextRun& text, int pos, int len);
00112 
00117     void Translate(gfxPoint pt);
00118     void Scale(gfxFloat x, gfxFloat y);
00119     void Rotate(gfxFloat angle); // radians
00120 
00121     // post-multiplies 'other' onto the current CTM
00122     void Multiply(const gfxMatrix& other);
00123 
00124     void SetMatrix(const gfxMatrix& matrix);
00125     void IdentityMatrix();
00126     gfxMatrix CurrentMatrix() const;
00127 
00128     gfxPoint DeviceToUser(gfxPoint point) const;
00129     gfxSize DeviceToUser(gfxSize size) const;
00130     gfxRect DeviceToUser(gfxRect rect) const;
00131     gfxPoint UserToDevice(gfxPoint point) const;
00132     gfxSize UserToDevice(gfxSize size) const;
00133     gfxRect UserToDevice(gfxRect rect) const;
00134 
00139     void SetColor(const gfxRGBA& c);
00140     void SetPattern(gfxPattern *pattern);
00141     void SetSource(gfxASurface *surface) {
00142         SetSource(surface, gfxPoint(0, 0));
00143     }
00144     void SetSource(gfxASurface* surface, gfxPoint offset);
00145 
00149     void Paint(gfxFloat alpha = 1.0);
00150 
00155     // Creates a new path with a rectangle from 0,0 to size.w,size.h
00156     // and calls cairo_fill
00157     void DrawSurface(gfxASurface *surface, gfxSize size);
00158 
00163     typedef enum {
00164         gfxLineSolid,
00165         gfxLineDashed,
00166         gfxLineDotted
00167     } gfxLineType;
00168 
00169     void SetDash(gfxLineType ltype);
00170     void SetDash(gfxFloat *dashes, int ndash, gfxFloat offset);
00171     //void getDash() const;
00172 
00173     void SetLineWidth(gfxFloat width);
00174     gfxFloat CurrentLineWidth() const;
00175 
00176     enum GraphicsLineCap {
00177         LINE_CAP_BUTT,
00178         LINE_CAP_ROUND,
00179         LINE_CAP_SQUARE
00180     };
00181     void SetLineCap(GraphicsLineCap cap);
00182     GraphicsLineCap CurrentLineCap() const;
00183 
00184     enum GraphicsLineJoin {
00185         LINE_JOIN_MITER,
00186         LINE_JOIN_ROUND,
00187         LINE_JOIN_BEVEL
00188     };
00189     void SetLineJoin(GraphicsLineJoin join);
00190     GraphicsLineJoin CurrentLineJoin() const;
00191 
00192     void SetMiterLimit(gfxFloat limit);
00193     gfxFloat CurrentMiterLimit() const;
00194 
00199     // define enum for operators (clear, src, dst, etc)
00200     enum GraphicsOperator {
00201         OPERATOR_CLEAR,
00202         OPERATOR_SRC,
00203         OPERATOR_DST,
00204         OPERATOR_OVER,
00205         OPERATOR_OVER_REVERSE,
00206         OPERATOR_IN,
00207         OPERATOR_IN_REVERSE,
00208         OPERATOR_OUT,
00209         OPERATOR_OUT_REVERSE,
00210         OPERATOR_ATOP,
00211         OPERATOR_ATOP_REVERSE,
00212         OPERATOR_XOR,
00213         OPERATOR_ADD,
00214         OPERATOR_SATURATE
00215     };
00216     void SetOperator(GraphicsOperator op);
00217     GraphicsOperator CurrentOperator() const;
00218 
00224     enum AntialiasMode {
00225         MODE_ALIASED,
00226         MODE_COVERAGE
00227     };
00228     void SetAntialiasMode(AntialiasMode mode);
00229     AntialiasMode CurrentAntialiasMode();
00230 
00235     void Clip(); // will clip the last path you've drawn
00236     void ResetClip(); // will remove any clip set
00237     // Helper functions that will create a rect path and call Clip().
00238     // Any current path will be destroyed by these functions!
00239     //
00240     void Clip(gfxRect rect); // will clip to a rect
00241     void Clip(const gfxRegion& region); // will clip to a region
00242 
00247     enum FilterHints {
00248         // Future drawing will completely cover the specified maxArea
00249         FILTER_OPAQUE_DRAW
00250     };
00251 
00252     // Start rendering under the filter. We guarantee not to draw outside 'maxArea'.
00253     void PushFilter(gfxFilter& filter, FilterHints hints, gfxRect& maxArea);
00254 
00255     // Completed rendering under the filter, composite what we rendered back to the
00256     // underlying surface using the filter.
00257     void PopFilter();
00258 
00259 private:
00260     cairo_t *mCairo;
00261     nsRefPtr<gfxASurface> mSurface;
00262 };
00263 
00264 #endif /* GFX_CONTEXT_H */