michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef NSRENDERINGCONTEXT__H__ michael@0: #define NSRENDERINGCONTEXT__H__ michael@0: michael@0: #include // for uint32_t michael@0: #include // for int32_t michael@0: #include "gfxContext.h" // for gfxContext michael@0: #include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER2 michael@0: #include "mozilla/gfx/2D.h" michael@0: #include "mozilla/gfx/UserData.h" // for UserData, UserDataKey michael@0: #include "nsAutoPtr.h" // for nsRefPtr michael@0: #include "nsBoundingMetrics.h" // for nsBoundingMetrics michael@0: #include "nsColor.h" // for nscolor michael@0: #include "nsCoord.h" // for nscoord, NSToIntRound michael@0: #include "nsDeviceContext.h" // for nsDeviceContext michael@0: #include "nsFontMetrics.h" // for nsFontMetrics michael@0: #include "nsISupports.h" // for NS_INLINE_DECL_REFCOUNTING, etc michael@0: #include "nsString.h" // for nsString michael@0: #include "nscore.h" // for char16_t michael@0: michael@0: class gfxASurface; michael@0: class nsIntRegion; michael@0: struct nsPoint; michael@0: struct nsRect; michael@0: michael@0: typedef enum { michael@0: nsLineStyle_kNone = 0, michael@0: nsLineStyle_kSolid = 1, michael@0: nsLineStyle_kDashed = 2, michael@0: nsLineStyle_kDotted = 3 michael@0: } nsLineStyle; michael@0: michael@0: class nsRenderingContext MOZ_FINAL michael@0: { michael@0: typedef mozilla::gfx::UserData UserData; michael@0: typedef mozilla::gfx::UserDataKey UserDataKey; michael@0: typedef mozilla::gfx::DrawTarget DrawTarget; michael@0: michael@0: public: michael@0: nsRenderingContext() : mP2A(0.) {} michael@0: michael@0: NS_INLINE_DECL_REFCOUNTING(nsRenderingContext) michael@0: michael@0: void Init(nsDeviceContext* aContext, gfxASurface* aThebesSurface); michael@0: void Init(nsDeviceContext* aContext, gfxContext* aThebesContext); michael@0: void Init(nsDeviceContext* aContext, DrawTarget* aDrawTarget); michael@0: michael@0: // These accessors will never return null. michael@0: gfxContext *ThebesContext() { return mThebes; } michael@0: DrawTarget *GetDrawTarget() { return mThebes->GetDrawTarget(); } michael@0: nsDeviceContext *DeviceContext() { return mDeviceContext; } michael@0: int32_t AppUnitsPerDevPixel() { return NSToIntRound(mP2A); } michael@0: michael@0: // Graphics state michael@0: michael@0: void PushState(void); michael@0: void PopState(void); michael@0: void IntersectClip(const nsRect& aRect); michael@0: void SetClip(const nsIntRegion& aRegion); michael@0: void SetLineStyle(nsLineStyle aLineStyle); michael@0: void SetColor(nscolor aColor); michael@0: void Translate(const nsPoint& aPt); michael@0: void Scale(float aSx, float aSy); michael@0: michael@0: class AutoPushTranslation { michael@0: nsRenderingContext* mCtx; michael@0: public: michael@0: AutoPushTranslation(nsRenderingContext* aCtx, const nsPoint& aPt) michael@0: : mCtx(aCtx) { michael@0: mCtx->PushState(); michael@0: mCtx->Translate(aPt); michael@0: } michael@0: ~AutoPushTranslation() { michael@0: mCtx->PopState(); michael@0: } michael@0: }; michael@0: michael@0: // Shapes michael@0: michael@0: void DrawLine(const nsPoint& aStartPt, const nsPoint& aEndPt); michael@0: void DrawLine(nscoord aX0, nscoord aY0, nscoord aX1, nscoord aY1); michael@0: void DrawRect(const nsRect& aRect); michael@0: void DrawRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight); michael@0: void DrawEllipse(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight); michael@0: michael@0: void FillRect(const nsRect& aRect); michael@0: void FillRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight); michael@0: void FillPolygon(const nsPoint aPoints[], int32_t aNumPoints); michael@0: michael@0: void FillEllipse(const nsRect& aRect); michael@0: void FillEllipse(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight); michael@0: michael@0: void InvertRect(const nsRect& aRect); michael@0: michael@0: // Text michael@0: michael@0: void SetFont(nsFontMetrics *aFontMetrics); michael@0: nsFontMetrics *FontMetrics() { return mFontMetrics; } // may be null michael@0: michael@0: void SetTextRunRTL(bool aIsRTL); michael@0: michael@0: nscoord GetWidth(char aC); michael@0: nscoord GetWidth(char16_t aC); michael@0: nscoord GetWidth(const nsString& aString); michael@0: nscoord GetWidth(const char* aString); michael@0: nscoord GetWidth(const char* aString, uint32_t aLength); michael@0: nscoord GetWidth(const char16_t *aString, uint32_t aLength); michael@0: michael@0: nsBoundingMetrics GetBoundingMetrics(const char16_t *aString, michael@0: uint32_t aLength); michael@0: michael@0: void DrawString(const nsString& aString, nscoord aX, nscoord aY); michael@0: void DrawString(const char *aString, uint32_t aLength, michael@0: nscoord aX, nscoord aY); michael@0: void DrawString(const char16_t *aString, uint32_t aLength, michael@0: nscoord aX, nscoord aY); michael@0: michael@0: void AddUserData(UserDataKey *key, void *userData, void (*destroy)(void*)) { michael@0: mUserData.Add(key, userData, destroy); michael@0: } michael@0: void *GetUserData(UserDataKey *key) { michael@0: return mUserData.Get(key); michael@0: } michael@0: void *RemoveUserData(UserDataKey *key) { michael@0: return mUserData.Remove(key); michael@0: } michael@0: michael@0: private: michael@0: // Private destructor, to discourage deletion outside of Release(): michael@0: ~nsRenderingContext() michael@0: { michael@0: } michael@0: michael@0: int32_t GetMaxChunkLength(); michael@0: michael@0: nsRefPtr mThebes; michael@0: nsRefPtr mDeviceContext; michael@0: nsRefPtr mFontMetrics; michael@0: michael@0: double mP2A; // cached app units per device pixel value michael@0: michael@0: UserData mUserData; michael@0: }; michael@0: michael@0: #endif // NSRENDERINGCONTEXT__H__