backout 29799f914cab, Bug 917642 - [Helix] Please update the helix blobs
[gecko.git] / gfx / src / nsRenderingContext.h
bloba9f81ef978ded01ff40e5c5c11f8c0042eea64c7
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef NSRENDERINGCONTEXT__H__
7 #define NSRENDERINGCONTEXT__H__
9 #include <stdint.h> // for uint32_t
10 #include <sys/types.h> // for int32_t
11 #include "gfxContext.h" // for gfxContext
12 #include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER2
13 #include "mozilla/gfx/UserData.h" // for UserData, UserDataKey
14 #include "nsAutoPtr.h" // for nsRefPtr
15 #include "nsBoundingMetrics.h" // for nsBoundingMetrics
16 #include "nsColor.h" // for nscolor
17 #include "nsCoord.h" // for nscoord, NSToIntRound
18 #include "nsDeviceContext.h" // for nsDeviceContext
19 #include "nsFontMetrics.h" // for nsFontMetrics
20 #include "nsISupports.h" // for NS_INLINE_DECL_REFCOUNTING, etc
21 #include "nsString.h" // for nsString
22 #include "nscore.h" // for PRUnichar
24 class gfxASurface;
25 class nsIntRegion;
26 struct nsPoint;
27 struct nsRect;
29 typedef enum {
30 nsLineStyle_kNone = 0,
31 nsLineStyle_kSolid = 1,
32 nsLineStyle_kDashed = 2,
33 nsLineStyle_kDotted = 3
34 } nsLineStyle;
36 class nsRenderingContext
38 typedef mozilla::gfx::UserData UserData;
39 typedef mozilla::gfx::UserDataKey UserDataKey;
41 public:
42 nsRenderingContext() : mP2A(0.) {}
43 // ~nsRenderingContext() {}
45 NS_INLINE_DECL_REFCOUNTING(nsRenderingContext)
47 void Init(nsDeviceContext* aContext, gfxASurface* aThebesSurface);
48 void Init(nsDeviceContext* aContext, gfxContext* aThebesContext);
50 // These accessors will never return null.
51 gfxContext *ThebesContext() { return mThebes; }
52 nsDeviceContext *DeviceContext() { return mDeviceContext; }
53 int32_t AppUnitsPerDevPixel() { return NSToIntRound(mP2A); }
55 // Graphics state
57 void PushState(void);
58 void PopState(void);
59 void IntersectClip(const nsRect& aRect);
60 void SetClip(const nsIntRegion& aRegion);
61 void SetLineStyle(nsLineStyle aLineStyle);
62 void SetColor(nscolor aColor);
63 void Translate(const nsPoint& aPt);
64 void Scale(float aSx, float aSy);
66 class AutoPushTranslation {
67 nsRenderingContext* mCtx;
68 public:
69 AutoPushTranslation(nsRenderingContext* aCtx, const nsPoint& aPt)
70 : mCtx(aCtx) {
71 mCtx->PushState();
72 mCtx->Translate(aPt);
74 ~AutoPushTranslation() {
75 mCtx->PopState();
79 // Shapes
81 void DrawLine(const nsPoint& aStartPt, const nsPoint& aEndPt);
82 void DrawLine(nscoord aX0, nscoord aY0, nscoord aX1, nscoord aY1);
83 void DrawRect(const nsRect& aRect);
84 void DrawRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
85 void DrawEllipse(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
87 void FillRect(const nsRect& aRect);
88 void FillRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
89 void FillPolygon(const nsPoint aPoints[], int32_t aNumPoints);
91 void FillEllipse(const nsRect& aRect);
92 void FillEllipse(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
94 void InvertRect(const nsRect& aRect);
96 // Text
98 void SetFont(nsFontMetrics *aFontMetrics);
99 nsFontMetrics *FontMetrics() { return mFontMetrics; } // may be null
101 void SetTextRunRTL(bool aIsRTL);
103 nscoord GetWidth(char aC);
104 nscoord GetWidth(PRUnichar aC);
105 nscoord GetWidth(const nsString& aString);
106 nscoord GetWidth(const char* aString);
107 nscoord GetWidth(const char* aString, uint32_t aLength);
108 nscoord GetWidth(const PRUnichar *aString, uint32_t aLength);
110 nsBoundingMetrics GetBoundingMetrics(const PRUnichar *aString,
111 uint32_t aLength);
113 void DrawString(const nsString& aString, nscoord aX, nscoord aY);
114 void DrawString(const char *aString, uint32_t aLength,
115 nscoord aX, nscoord aY);
116 void DrawString(const PRUnichar *aString, uint32_t aLength,
117 nscoord aX, nscoord aY);
119 void AddUserData(UserDataKey *key, void *userData, void (*destroy)(void*)) {
120 mUserData.Add(key, userData, destroy);
122 void *GetUserData(UserDataKey *key) {
123 return mUserData.Get(key);
125 void *RemoveUserData(UserDataKey *key) {
126 return mUserData.Remove(key);
129 protected:
130 int32_t GetMaxChunkLength();
132 nsRefPtr<gfxContext> mThebes;
133 nsRefPtr<nsDeviceContext> mDeviceContext;
134 nsRefPtr<nsFontMetrics> mFontMetrics;
136 double mP2A; // cached app units per device pixel value
138 UserData mUserData;
141 #endif // NSRENDERINGCONTEXT__H__