|
1 /* -*- Mode: C++; 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 _NS_DEVICECONTEXT_H_ |
|
7 #define _NS_DEVICECONTEXT_H_ |
|
8 |
|
9 #include <stdint.h> // for uint32_t |
|
10 #include <sys/types.h> // for int32_t |
|
11 #include "gfxTypes.h" // for gfxFloat |
|
12 #include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER2 |
|
13 #include "nsAutoPtr.h" // for nsRefPtr |
|
14 #include "nsCOMPtr.h" // for nsCOMPtr |
|
15 #include "nsCoord.h" // for nscoord |
|
16 #include "nsError.h" // for nsresult |
|
17 #include "nsISupports.h" // for NS_INLINE_DECL_REFCOUNTING |
|
18 #include "nsMathUtils.h" // for NS_round |
|
19 #include "nscore.h" // for char16_t, nsAString |
|
20 #include "mozilla/AppUnits.h" // for AppUnits |
|
21 |
|
22 class gfxASurface; |
|
23 class gfxTextPerfMetrics; |
|
24 class gfxUserFontSet; |
|
25 class nsFont; |
|
26 class nsFontCache; |
|
27 class nsFontMetrics; |
|
28 class nsIAtom; |
|
29 class nsIDeviceContextSpec; |
|
30 class nsIScreen; |
|
31 class nsIScreenManager; |
|
32 class nsIWidget; |
|
33 class nsRect; |
|
34 class nsRenderingContext; |
|
35 |
|
36 class nsDeviceContext MOZ_FINAL |
|
37 { |
|
38 public: |
|
39 nsDeviceContext(); |
|
40 |
|
41 NS_INLINE_DECL_REFCOUNTING(nsDeviceContext) |
|
42 |
|
43 /** |
|
44 * Initialize the device context from a widget |
|
45 * @param aWidget a widget to initialize the device context from |
|
46 * @return error status |
|
47 */ |
|
48 nsresult Init(nsIWidget *aWidget); |
|
49 |
|
50 /** |
|
51 * Initialize the device context from a device context spec |
|
52 * @param aDevSpec the specification of the printing device |
|
53 * @return error status |
|
54 */ |
|
55 nsresult InitForPrinting(nsIDeviceContextSpec *aDevSpec); |
|
56 |
|
57 /** |
|
58 * Create a rendering context and initialize it. Only call this |
|
59 * method on device contexts that were initialized for printing. |
|
60 * |
|
61 * @return the new rendering context (guaranteed to be non-null) |
|
62 */ |
|
63 already_AddRefed<nsRenderingContext> CreateRenderingContext(); |
|
64 |
|
65 /** |
|
66 * Gets the number of app units in one CSS pixel; this number is global, |
|
67 * not unique to each device context. |
|
68 */ |
|
69 static int32_t AppUnitsPerCSSPixel() { return mozilla::AppUnitsPerCSSPixel(); } |
|
70 |
|
71 /** |
|
72 * Gets the number of app units in one device pixel; this number |
|
73 * is usually a factor of AppUnitsPerCSSPixel(), although that is |
|
74 * not guaranteed. |
|
75 */ |
|
76 int32_t AppUnitsPerDevPixel() const { return mAppUnitsPerDevPixel; } |
|
77 |
|
78 /** |
|
79 * Convert device pixels which is used for gfx/thebes to nearest |
|
80 * (rounded) app units |
|
81 */ |
|
82 nscoord GfxUnitsToAppUnits(gfxFloat aGfxUnits) const |
|
83 { return nscoord(NS_round(aGfxUnits * AppUnitsPerDevPixel())); } |
|
84 |
|
85 /** |
|
86 * Convert app units to device pixels which is used for gfx/thebes. |
|
87 */ |
|
88 gfxFloat AppUnitsToGfxUnits(nscoord aAppUnits) const |
|
89 { return gfxFloat(aAppUnits) / AppUnitsPerDevPixel(); } |
|
90 |
|
91 /** |
|
92 * Gets the number of app units in one physical inch; this is the |
|
93 * device's DPI times AppUnitsPerDevPixel(). |
|
94 */ |
|
95 int32_t AppUnitsPerPhysicalInch() const |
|
96 { return mAppUnitsPerPhysicalInch; } |
|
97 |
|
98 /** |
|
99 * Gets the number of app units in one CSS inch; this is |
|
100 * 96 times AppUnitsPerCSSPixel. |
|
101 */ |
|
102 static int32_t AppUnitsPerCSSInch() { return mozilla::AppUnitsPerCSSInch(); } |
|
103 |
|
104 /** |
|
105 * Get the unscaled ratio of app units to dev pixels; useful if something |
|
106 * needs to be converted from to unscaled pixels |
|
107 */ |
|
108 int32_t UnscaledAppUnitsPerDevPixel() const |
|
109 { return mAppUnitsPerDevNotScaledPixel; } |
|
110 |
|
111 /** |
|
112 * Get the nsFontMetrics that describe the properties of |
|
113 * an nsFont. |
|
114 * @param aFont font description to obtain metrics for |
|
115 * @param aLanguage the language of the document |
|
116 * @param aMetrics out parameter for font metrics |
|
117 * @param aUserFontSet user font set |
|
118 * @return error status |
|
119 */ |
|
120 nsresult GetMetricsFor(const nsFont& aFont, nsIAtom* aLanguage, |
|
121 gfxUserFontSet* aUserFontSet, |
|
122 gfxTextPerfMetrics* aTextPerf, |
|
123 nsFontMetrics*& aMetrics); |
|
124 |
|
125 /** |
|
126 * Notification when a font metrics instance created for this device is |
|
127 * about to be deleted |
|
128 */ |
|
129 nsresult FontMetricsDeleted(const nsFontMetrics* aFontMetrics); |
|
130 |
|
131 /** |
|
132 * Attempt to free up resources by flushing out any fonts no longer |
|
133 * referenced by anything other than the font cache itself. |
|
134 * @return error status |
|
135 */ |
|
136 nsresult FlushFontCache(); |
|
137 |
|
138 /** |
|
139 * Return the bit depth of the device. |
|
140 */ |
|
141 nsresult GetDepth(uint32_t& aDepth); |
|
142 |
|
143 /** |
|
144 * Get the size of the displayable area of the output device |
|
145 * in app units. |
|
146 * @param aWidth out parameter for width |
|
147 * @param aHeight out parameter for height |
|
148 * @return error status |
|
149 */ |
|
150 nsresult GetDeviceSurfaceDimensions(nscoord& aWidth, nscoord& aHeight); |
|
151 |
|
152 /** |
|
153 * Get the size of the content area of the output device in app |
|
154 * units. This corresponds on a screen device, for instance, to |
|
155 * the entire screen. |
|
156 * @param aRect out parameter for full rect. Position (x,y) will |
|
157 * be (0,0) or relative to the primary monitor if |
|
158 * this is not the primary. |
|
159 * @return error status |
|
160 */ |
|
161 nsresult GetRect(nsRect& aRect); |
|
162 |
|
163 /** |
|
164 * Get the size of the content area of the output device in app |
|
165 * units. This corresponds on a screen device, for instance, to |
|
166 * the area reported by GetDeviceSurfaceDimensions, minus the |
|
167 * taskbar (Windows) or menubar (Macintosh). |
|
168 * @param aRect out parameter for client rect. Position (x,y) will |
|
169 * be (0,0) adjusted for any upper/left non-client |
|
170 * space if present or relative to the primary |
|
171 * monitor if this is not the primary. |
|
172 * @return error status |
|
173 */ |
|
174 nsresult GetClientRect(nsRect& aRect); |
|
175 |
|
176 /** |
|
177 * Inform the output device that output of a document is beginning |
|
178 * Used for print related device contexts. Must be matched 1:1 with |
|
179 * EndDocument() or AbortDocument(). |
|
180 * |
|
181 * @param aTitle - title of Document |
|
182 * @param aPrintToFileName - name of file to print to, if nullptr |
|
183 * then don't print to file |
|
184 * @param aStartPage - starting page number (must be greater than zero) |
|
185 * @param aEndPage - ending page number (must be less than or |
|
186 * equal to number of pages) |
|
187 * |
|
188 * @return error status |
|
189 */ |
|
190 nsresult BeginDocument(const nsAString& aTitle, |
|
191 char16_t* aPrintToFileName, |
|
192 int32_t aStartPage, |
|
193 int32_t aEndPage); |
|
194 |
|
195 /** |
|
196 * Inform the output device that output of a document is ending. |
|
197 * Used for print related device contexts. Must be matched 1:1 with |
|
198 * BeginDocument() |
|
199 * @return error status |
|
200 */ |
|
201 nsresult EndDocument(); |
|
202 |
|
203 /** |
|
204 * Inform the output device that output of a document is being aborted. |
|
205 * Must be matched 1:1 with BeginDocument() |
|
206 * @return error status |
|
207 */ |
|
208 nsresult AbortDocument(); |
|
209 |
|
210 /** |
|
211 * Inform the output device that output of a page is beginning |
|
212 * Used for print related device contexts. Must be matched 1:1 with |
|
213 * EndPage() and within a BeginDocument()/EndDocument() pair. |
|
214 * @return error status |
|
215 */ |
|
216 nsresult BeginPage(); |
|
217 |
|
218 /** |
|
219 * Inform the output device that output of a page is ending |
|
220 * Used for print related device contexts. Must be matched 1:1 with |
|
221 * BeginPage() and within a BeginDocument()/EndDocument() pair. |
|
222 * @return error status |
|
223 */ |
|
224 nsresult EndPage(); |
|
225 |
|
226 /** |
|
227 * Check to see if the DPI has changed |
|
228 * @return whether there was actually a change in the DPI (whether |
|
229 * AppUnitsPerDevPixel() or AppUnitsPerPhysicalInch() |
|
230 * changed) |
|
231 */ |
|
232 bool CheckDPIChange(); |
|
233 |
|
234 /** |
|
235 * Set the pixel scaling factor: all lengths are multiplied by this factor |
|
236 * when we convert them to device pixels. Returns whether the ratio of |
|
237 * app units to dev pixels changed because of the scale factor. |
|
238 */ |
|
239 bool SetPixelScale(float aScale); |
|
240 |
|
241 /** |
|
242 * Returns the pixel scaling factor (page zoom factor) applied. |
|
243 */ |
|
244 float GetPixelScale() const { return mPixelScale; } |
|
245 |
|
246 /** |
|
247 * True if this device context was created for printing. |
|
248 */ |
|
249 bool IsPrinterSurface(); |
|
250 |
|
251 private: |
|
252 // Private destructor, to discourage deletion outside of Release(): |
|
253 ~nsDeviceContext(); |
|
254 |
|
255 void SetDPI(); |
|
256 void ComputeClientRectUsingScreen(nsRect *outRect); |
|
257 void ComputeFullAreaUsingScreen(nsRect *outRect); |
|
258 void FindScreen(nsIScreen **outScreen); |
|
259 void CalcPrintingSize(); |
|
260 void UpdateScaledAppUnits(); |
|
261 |
|
262 nscoord mWidth; |
|
263 nscoord mHeight; |
|
264 uint32_t mDepth; |
|
265 int32_t mAppUnitsPerDevPixel; |
|
266 int32_t mAppUnitsPerDevNotScaledPixel; |
|
267 int32_t mAppUnitsPerPhysicalInch; |
|
268 float mPixelScale; |
|
269 float mPrintingScale; |
|
270 |
|
271 nsFontCache* mFontCache; |
|
272 nsCOMPtr<nsIWidget> mWidget; |
|
273 nsCOMPtr<nsIScreenManager> mScreenManager; |
|
274 nsCOMPtr<nsIDeviceContextSpec> mDeviceContextSpec; |
|
275 nsRefPtr<gfxASurface> mPrintingSurface; |
|
276 #ifdef XP_MACOSX |
|
277 nsRefPtr<gfxASurface> mCachedPrintingSurface; |
|
278 #endif |
|
279 }; |
|
280 |
|
281 #endif /* _NS_DEVICECONTEXT_H_ */ |