Back to index

lightning-sunbird  0.9+nobinonly
nsCairoRenderingContext.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 mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * mozilla.org.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Stuart Parmenter <pavlov@pavlov.net>
00024  *   Joe Hewitt <hewitt@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef NSCAIRORENDERINGCONTEXT__H__
00041 #define NSCAIRORENDERINGCONTEXT__H__
00042 
00043 #include <cairo.h>
00044 
00045 #include "nsCOMPtr.h"
00046 #include "nsIRenderingContext.h"
00047 #include "nsIDeviceContext.h"
00048 #include "nsIFontMetrics.h"
00049 #include "nsIWidget.h"
00050 #include "nsPoint.h"
00051 #include "nsSize.h"
00052 #include "nsColor.h"
00053 #include "nsRect.h"
00054 #include "nsIRegion.h"
00055 #include "nsTransform2D.h"
00056 #include "nsVoidArray.h"
00057 #include "nsICairoFontMetrics.h"
00058 
00059 class nsIImage;
00060 
00061 class nsCairoDrawingSurface;
00062 
00063 class nsCairoRenderingContext : public nsIRenderingContext
00064 {
00065 public:
00066     nsCairoRenderingContext();
00067     virtual ~nsCairoRenderingContext();
00068 
00069     NS_DECL_ISUPPORTS
00070 
00071     NS_IMETHOD Init(nsIDeviceContext* aContext, nsIWidget *aWidget);
00072     NS_IMETHOD Init(nsIDeviceContext* aContext, nsIDrawingSurface *aSurface);
00073     NS_IMETHOD CommonInit(void);
00074     NS_IMETHOD Reset(void);
00075     NS_IMETHOD GetDeviceContext(nsIDeviceContext *& aDeviceContext);
00076     NS_IMETHOD LockDrawingSurface(PRInt32 aX, PRInt32 aY,
00077                                   PRUint32 aWidth, PRUint32 aHeight,
00078                                   void **aBits, PRInt32 *aStride,
00079                                   PRInt32 *aWidthBytes,
00080                                   PRUint32 aFlags);
00081     NS_IMETHOD UnlockDrawingSurface(void);
00082     NS_IMETHOD SelectOffScreenDrawingSurface(nsIDrawingSurface *aSurface);
00083     NS_IMETHOD GetDrawingSurface(nsIDrawingSurface **aSurface);
00084     NS_IMETHOD GetHints(PRUint32& aResult);
00085     NS_IMETHOD PushState(void);
00086     NS_IMETHOD PopState(void);
00087     NS_IMETHOD IsVisibleRect(const nsRect& aRect, PRBool &aIsVisible);
00088     NS_IMETHOD SetClipRect(const nsRect& aRect, nsClipCombine aCombine);
00089     NS_IMETHOD GetClipRect(nsRect &aRect, PRBool &aHasLocalClip);
00090     NS_IMETHOD SetLineStyle(nsLineStyle aLineStyle);
00091     NS_IMETHOD GetLineStyle(nsLineStyle &aLineStyle);
00092     NS_IMETHOD GetPenMode(nsPenMode &aPenMode);
00093     NS_IMETHOD SetPenMode(nsPenMode aPenMode);
00094     NS_IMETHOD SetClipRegion(const nsIRegion& aRegion, nsClipCombine aCombine);
00095     NS_IMETHOD CopyClipRegion(nsIRegion &aRegion);
00096     NS_IMETHOD GetClipRegion(nsIRegion **aRegion);
00097     NS_IMETHOD SetColor(nscolor aColor);
00098     NS_IMETHOD GetColor(nscolor &aColor) const;
00099     NS_IMETHOD SetFont(const nsFont& aFont, nsIAtom* aLangGroup);
00100     NS_IMETHOD SetFont(nsIFontMetrics *aFontMetrics);
00101     NS_IMETHOD GetFontMetrics(nsIFontMetrics *&aFontMetrics);
00102     NS_IMETHOD Translate(nscoord aX, nscoord aY);
00103     NS_IMETHOD Scale(float aSx, float aSy);
00104     NS_IMETHOD GetCurrentTransform(nsTransform2D *&aTransform);
00105     NS_IMETHOD CreateDrawingSurface(const nsRect &aBounds, PRUint32 aSurfFlags, nsIDrawingSurface* &aSurface);
00106     NS_IMETHOD DestroyDrawingSurface(nsIDrawingSurface *aDS);
00107     NS_IMETHOD DrawLine(nscoord aX0, nscoord aY0, nscoord aX1, nscoord aY1);
00108     NS_IMETHOD DrawPolyline(const nsPoint aPoints[], PRInt32 aNumPoints);
00109     NS_IMETHOD DrawRect(const nsRect& aRect);
00110     NS_IMETHOD DrawRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
00111     NS_IMETHOD FillRect(const nsRect& aRect);
00112     NS_IMETHOD FillRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
00113     NS_IMETHOD InvertRect(const nsRect& aRect);
00114     NS_IMETHOD InvertRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
00115     NS_IMETHOD FlushRect(const nsRect& aRect);
00116     NS_IMETHOD FlushRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
00117     NS_IMETHOD DrawPolygon(const nsPoint aPoints[], PRInt32 aNumPoints);
00118     NS_IMETHOD FillPolygon(const nsPoint aPoints[], PRInt32 aNumPoints);
00119     NS_IMETHOD DrawEllipse(const nsRect& aRect);
00120     NS_IMETHOD DrawEllipse(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
00121     NS_IMETHOD FillEllipse(const nsRect& aRect);
00122     NS_IMETHOD FillEllipse(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
00123     NS_IMETHOD DrawArc(const nsRect& aRect,
00124                        float aStartAngle, float aEndAngle);
00125     NS_IMETHOD DrawArc(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight,
00126                        float aStartAngle, float aEndAngle);
00127     NS_IMETHOD FillArc(const nsRect& aRect,
00128                        float aStartAngle, float aEndAngle);
00129     NS_IMETHOD FillArc(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight,
00130                        float aStartAngle, float aEndAngle);
00131     NS_IMETHOD GetWidth(char aC, nscoord &aWidth);
00132     NS_IMETHOD GetWidth(PRUnichar aC, nscoord &aWidth,
00133                         PRInt32 *aFontID = nsnull);
00134     NS_IMETHOD GetWidth(const nsString& aString, nscoord &aWidth,
00135                         PRInt32 *aFontID = nsnull);
00136     NS_IMETHOD GetWidth(const char* aString, nscoord& aWidth);
00137     NS_IMETHOD GetWidth(const char* aString, PRUint32 aLength,
00138                         nscoord& aWidth);
00139     NS_IMETHOD GetWidth(const PRUnichar *aString, PRUint32 aLength,
00140                         nscoord &aWidth, PRInt32 *aFontID = nsnull);
00141     NS_IMETHOD GetTextDimensions(const char* aString, PRUint32 aLength,
00142                                  nsTextDimensions& aDimensions);
00143     NS_IMETHOD GetTextDimensions(const PRUnichar* aString, PRUint32 aLength,
00144                                  nsTextDimensions& aDimensions, PRInt32* aFontID = nsnull);
00145 
00146 #if defined(_WIN32) || defined(XP_OS2) || defined(MOZ_X11) || defined(XP_BEOS)
00147     NS_IMETHOD GetTextDimensions(const char*     aString,
00148                                  PRInt32           aLength,
00149                                  PRInt32           aAvailWidth,
00150                                  PRInt32*          aBreaks,
00151                                  PRInt32           aNumBreaks,
00152                                  nsTextDimensions& aDimensions,
00153                                  PRInt32&          aNumCharsFit,
00154                                  nsTextDimensions& aLastWordDimensions,
00155                                  PRInt32*          aFontID = nsnull);
00156 
00157     NS_IMETHOD GetTextDimensions(const PRUnichar*  aString,
00158                                  PRInt32           aLength,
00159                                  PRInt32           aAvailWidth,
00160                                  PRInt32*          aBreaks,
00161                                  PRInt32           aNumBreaks,
00162                                  nsTextDimensions& aDimensions,
00163                                  PRInt32&          aNumCharsFit,
00164                                  nsTextDimensions& aLastWordDimensions,
00165                                  PRInt32*          aFontID = nsnull);
00166 #endif
00167 
00168     NS_IMETHOD DrawString(const char *aString, PRUint32 aLength,
00169                           nscoord aX, nscoord aY,
00170                           const nscoord* aSpacing = nsnull);
00171     NS_IMETHOD DrawString(const PRUnichar *aString, PRUint32 aLength,
00172                           nscoord aX, nscoord aY,
00173                           PRInt32 aFontID = -1,
00174                           const nscoord* aSpacing = nsnull);
00175     NS_IMETHOD DrawString(const nsString& aString, nscoord aX, nscoord aY,
00176                           PRInt32 aFontID = -1,
00177                           const nscoord* aSpacing = nsnull);
00178     NS_IMETHOD CopyOffScreenBits(nsIDrawingSurface *aSrcSurf,
00179                                  PRInt32 aSrcX, PRInt32 aSrcY,
00180                                  const nsRect &aDestBounds,
00181                                  PRUint32 aCopyFlags);
00182     NS_IMETHOD RetrieveCurrentNativeGraphicData(PRUint32 * ngd);
00183     NS_IMETHOD GetBackbuffer(const nsRect &aRequestedSize,
00184                              const nsRect &aMaxSize,
00185                              PRBool aForBlending,
00186                              nsIDrawingSurface* &aBackbuffer);
00187     NS_IMETHOD ReleaseBackbuffer(void);
00188     NS_IMETHOD DestroyCachedBackbuffer(void);
00189     NS_IMETHOD UseBackbuffer(PRBool* aUseBackbuffer);
00190 
00191     NS_IMETHOD PushTranslation(PushedTranslation* aState);
00192     NS_IMETHOD PopTranslation(PushedTranslation* aState);
00193 
00194 #ifdef MOZ_MATHML
00195     NS_IMETHOD GetBoundingMetrics(const char* aString, PRUint32 aLength, nsBoundingMetrics& aBoundingMetrics);
00196     NS_IMETHOD GetBoundingMetrics(const PRUnichar* aString, PRUint32 aLength,
00197                                   nsBoundingMetrics& aBoundingMetrics,
00198                                   PRInt32* aFontID);
00199 
00200 #endif // MOZ_MATHML
00201 
00202 
00203     NS_IMETHOD DrawImage(imgIContainer *aImage,
00204                          const nsRect &aSrcRect,
00205                          const nsRect &aDestRect);
00206     NS_IMETHOD DrawTile(imgIContainer *aImage, nscoord aXOffset, nscoord aYOffset,
00207                         const nsRect * aTargetRect);
00208 
00209     NS_IMETHOD SetRightToLeftText(PRBool aIsRTL) { return NS_OK; }
00210 
00211     NS_IMETHOD GetClusterInfo(const PRUnichar *aText,
00212                               PRUint32 aLength,
00213                               PRUint8 *aClusterStarts);
00214     virtual PRInt32 GetPosition(const PRUnichar *aText,
00215                                 PRUint32 aLength,
00216                                 nsPoint aPt);
00217     NS_IMETHOD GetRangeWidth(const PRUnichar *aText,
00218                              PRUint32 aLength,
00219                              PRUint32 aStart,
00220                              PRUint32 aEnd,
00221                              PRUint32 &aWidth);
00222     NS_IMETHOD GetRangeWidth(const char *aText,
00223                              PRUint32 aLength,
00224                              PRUint32 aStart,
00225                              PRUint32 aEnd,
00226                              PRUint32 &aWidth);
00227 
00228     NS_IMETHOD RenderEPS(const nsRect& aRect, FILE *aDataFile);
00229 
00230     // Cairo specific stuff
00231 
00232     cairo_t *GetCairo() { return mCairo; }
00233 
00234     nsTransform2D& CurrentTransform();
00235 
00236     void TransformCoord (nscoord *aX, nscoord *aY);
00237 
00238 protected:
00239     PRBool DoCairoDrawPolygon(const nsPoint aPoints[], PRInt32 aNumPoints);
00240     void DoCairoDrawEllipse (double aX, double aY, double aWidth, double aHeight);
00241     void DoCairoDrawArc (double aX, double aY, double aWidth, double aHeight,
00242                          double aStartAngle, double aEndAngle);
00243     void DoCairoClip ();
00244 
00245     nsCOMPtr<nsIDeviceContext> mDeviceContext;
00246     nsCOMPtr<nsIWidget> mWidget;
00247 
00248     // we need to manage our own clip region (since we can't get at
00249     // the old one from cairo)
00250     nsCOMPtr<nsIRegion> mClipRegion;
00251     nsCOMPtr<nsICairoFontMetrics> mFontMetrics;
00252 
00253     nsLineStyle mLineStyle;
00254     nscolor mColor;
00255 
00256     cairo_t *mCairo;
00257 
00258     nsCOMPtr<nsCairoDrawingSurface> mDrawingSurface;
00259     nsCOMPtr<nsCairoDrawingSurface> mOffscreenSurface;
00260     nsCOMPtr<nsCairoDrawingSurface> mBackBufferSurface;
00261 
00262     // for handing out to people
00263     void UpdateTempTransformMatrix();
00264     nsTransform2D mTempTransform;
00265 };
00266 
00267 #endif  // NSCAIRORENDERINGCONTEXT__H__