|
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/. */ |
|
5 |
|
6 #ifndef NSRENDERINGCONTEXT__H__ |
|
7 #define NSRENDERINGCONTEXT__H__ |
|
8 |
|
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/2D.h" |
|
14 #include "mozilla/gfx/UserData.h" // for UserData, UserDataKey |
|
15 #include "nsAutoPtr.h" // for nsRefPtr |
|
16 #include "nsBoundingMetrics.h" // for nsBoundingMetrics |
|
17 #include "nsColor.h" // for nscolor |
|
18 #include "nsCoord.h" // for nscoord, NSToIntRound |
|
19 #include "nsDeviceContext.h" // for nsDeviceContext |
|
20 #include "nsFontMetrics.h" // for nsFontMetrics |
|
21 #include "nsISupports.h" // for NS_INLINE_DECL_REFCOUNTING, etc |
|
22 #include "nsString.h" // for nsString |
|
23 #include "nscore.h" // for char16_t |
|
24 |
|
25 class gfxASurface; |
|
26 class nsIntRegion; |
|
27 struct nsPoint; |
|
28 struct nsRect; |
|
29 |
|
30 typedef enum { |
|
31 nsLineStyle_kNone = 0, |
|
32 nsLineStyle_kSolid = 1, |
|
33 nsLineStyle_kDashed = 2, |
|
34 nsLineStyle_kDotted = 3 |
|
35 } nsLineStyle; |
|
36 |
|
37 class nsRenderingContext MOZ_FINAL |
|
38 { |
|
39 typedef mozilla::gfx::UserData UserData; |
|
40 typedef mozilla::gfx::UserDataKey UserDataKey; |
|
41 typedef mozilla::gfx::DrawTarget DrawTarget; |
|
42 |
|
43 public: |
|
44 nsRenderingContext() : mP2A(0.) {} |
|
45 |
|
46 NS_INLINE_DECL_REFCOUNTING(nsRenderingContext) |
|
47 |
|
48 void Init(nsDeviceContext* aContext, gfxASurface* aThebesSurface); |
|
49 void Init(nsDeviceContext* aContext, gfxContext* aThebesContext); |
|
50 void Init(nsDeviceContext* aContext, DrawTarget* aDrawTarget); |
|
51 |
|
52 // These accessors will never return null. |
|
53 gfxContext *ThebesContext() { return mThebes; } |
|
54 DrawTarget *GetDrawTarget() { return mThebes->GetDrawTarget(); } |
|
55 nsDeviceContext *DeviceContext() { return mDeviceContext; } |
|
56 int32_t AppUnitsPerDevPixel() { return NSToIntRound(mP2A); } |
|
57 |
|
58 // Graphics state |
|
59 |
|
60 void PushState(void); |
|
61 void PopState(void); |
|
62 void IntersectClip(const nsRect& aRect); |
|
63 void SetClip(const nsIntRegion& aRegion); |
|
64 void SetLineStyle(nsLineStyle aLineStyle); |
|
65 void SetColor(nscolor aColor); |
|
66 void Translate(const nsPoint& aPt); |
|
67 void Scale(float aSx, float aSy); |
|
68 |
|
69 class AutoPushTranslation { |
|
70 nsRenderingContext* mCtx; |
|
71 public: |
|
72 AutoPushTranslation(nsRenderingContext* aCtx, const nsPoint& aPt) |
|
73 : mCtx(aCtx) { |
|
74 mCtx->PushState(); |
|
75 mCtx->Translate(aPt); |
|
76 } |
|
77 ~AutoPushTranslation() { |
|
78 mCtx->PopState(); |
|
79 } |
|
80 }; |
|
81 |
|
82 // Shapes |
|
83 |
|
84 void DrawLine(const nsPoint& aStartPt, const nsPoint& aEndPt); |
|
85 void DrawLine(nscoord aX0, nscoord aY0, nscoord aX1, nscoord aY1); |
|
86 void DrawRect(const nsRect& aRect); |
|
87 void DrawRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight); |
|
88 void DrawEllipse(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight); |
|
89 |
|
90 void FillRect(const nsRect& aRect); |
|
91 void FillRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight); |
|
92 void FillPolygon(const nsPoint aPoints[], int32_t aNumPoints); |
|
93 |
|
94 void FillEllipse(const nsRect& aRect); |
|
95 void FillEllipse(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight); |
|
96 |
|
97 void InvertRect(const nsRect& aRect); |
|
98 |
|
99 // Text |
|
100 |
|
101 void SetFont(nsFontMetrics *aFontMetrics); |
|
102 nsFontMetrics *FontMetrics() { return mFontMetrics; } // may be null |
|
103 |
|
104 void SetTextRunRTL(bool aIsRTL); |
|
105 |
|
106 nscoord GetWidth(char aC); |
|
107 nscoord GetWidth(char16_t aC); |
|
108 nscoord GetWidth(const nsString& aString); |
|
109 nscoord GetWidth(const char* aString); |
|
110 nscoord GetWidth(const char* aString, uint32_t aLength); |
|
111 nscoord GetWidth(const char16_t *aString, uint32_t aLength); |
|
112 |
|
113 nsBoundingMetrics GetBoundingMetrics(const char16_t *aString, |
|
114 uint32_t aLength); |
|
115 |
|
116 void DrawString(const nsString& aString, nscoord aX, nscoord aY); |
|
117 void DrawString(const char *aString, uint32_t aLength, |
|
118 nscoord aX, nscoord aY); |
|
119 void DrawString(const char16_t *aString, uint32_t aLength, |
|
120 nscoord aX, nscoord aY); |
|
121 |
|
122 void AddUserData(UserDataKey *key, void *userData, void (*destroy)(void*)) { |
|
123 mUserData.Add(key, userData, destroy); |
|
124 } |
|
125 void *GetUserData(UserDataKey *key) { |
|
126 return mUserData.Get(key); |
|
127 } |
|
128 void *RemoveUserData(UserDataKey *key) { |
|
129 return mUserData.Remove(key); |
|
130 } |
|
131 |
|
132 private: |
|
133 // Private destructor, to discourage deletion outside of Release(): |
|
134 ~nsRenderingContext() |
|
135 { |
|
136 } |
|
137 |
|
138 int32_t GetMaxChunkLength(); |
|
139 |
|
140 nsRefPtr<gfxContext> mThebes; |
|
141 nsRefPtr<nsDeviceContext> mDeviceContext; |
|
142 nsRefPtr<nsFontMetrics> mFontMetrics; |
|
143 |
|
144 double mP2A; // cached app units per device pixel value |
|
145 |
|
146 UserData mUserData; |
|
147 }; |
|
148 |
|
149 #endif // NSRENDERINGCONTEXT__H__ |