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/. */
6 #ifndef _NS_DEVICECONTEXT_H_
7 #define _NS_DEVICECONTEXT_H_
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
23 class gfxTextPerfMetrics
;
29 class nsIDeviceContextSpec
;
31 class nsIScreenManager
;
34 class nsRenderingContext
;
36 class nsDeviceContext MOZ_FINAL
41 NS_INLINE_DECL_REFCOUNTING(nsDeviceContext
)
44 * Initialize the device context from a widget
45 * @param aWidget a widget to initialize the device context from
46 * @return error status
48 nsresult
Init(nsIWidget
*aWidget
);
51 * Initialize the device context from a device context spec
52 * @param aDevSpec the specification of the printing device
53 * @return error status
55 nsresult
InitForPrinting(nsIDeviceContextSpec
*aDevSpec
);
58 * Create a rendering context and initialize it. Only call this
59 * method on device contexts that were initialized for printing.
61 * @return the new rendering context (guaranteed to be non-null)
63 already_AddRefed
<nsRenderingContext
> CreateRenderingContext();
66 * Gets the number of app units in one CSS pixel; this number is global,
67 * not unique to each device context.
69 static int32_t AppUnitsPerCSSPixel() { return mozilla::AppUnitsPerCSSPixel(); }
72 * Gets the number of app units in one device pixel; this number
73 * is usually a factor of AppUnitsPerCSSPixel(), although that is
76 int32_t AppUnitsPerDevPixel() const { return mAppUnitsPerDevPixel
; }
79 * Convert device pixels which is used for gfx/thebes to nearest
82 nscoord
GfxUnitsToAppUnits(gfxFloat aGfxUnits
) const
83 { return nscoord(NS_round(aGfxUnits
* AppUnitsPerDevPixel())); }
86 * Convert app units to device pixels which is used for gfx/thebes.
88 gfxFloat
AppUnitsToGfxUnits(nscoord aAppUnits
) const
89 { return gfxFloat(aAppUnits
) / AppUnitsPerDevPixel(); }
92 * Gets the number of app units in one physical inch; this is the
93 * device's DPI times AppUnitsPerDevPixel().
95 int32_t AppUnitsPerPhysicalInch() const
96 { return mAppUnitsPerPhysicalInch
; }
99 * Gets the number of app units in one CSS inch; this is
100 * 96 times AppUnitsPerCSSPixel.
102 static int32_t AppUnitsPerCSSInch() { return mozilla::AppUnitsPerCSSInch(); }
105 * Get the unscaled ratio of app units to dev pixels; useful if something
106 * needs to be converted from to unscaled pixels
108 int32_t UnscaledAppUnitsPerDevPixel() const
109 { return mAppUnitsPerDevNotScaledPixel
; }
112 * Get the nsFontMetrics that describe the properties of
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
120 nsresult
GetMetricsFor(const nsFont
& aFont
, nsIAtom
* aLanguage
,
121 gfxUserFontSet
* aUserFontSet
,
122 gfxTextPerfMetrics
* aTextPerf
,
123 nsFontMetrics
*& aMetrics
);
126 * Notification when a font metrics instance created for this device is
127 * about to be deleted
129 nsresult
FontMetricsDeleted(const nsFontMetrics
* aFontMetrics
);
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
136 nsresult
FlushFontCache();
139 * Return the bit depth of the device.
141 nsresult
GetDepth(uint32_t& aDepth
);
144 * Get the size of the displayable area of the output device
146 * @param aWidth out parameter for width
147 * @param aHeight out parameter for height
148 * @return error status
150 nsresult
GetDeviceSurfaceDimensions(nscoord
& aWidth
, nscoord
& aHeight
);
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
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
161 nsresult
GetRect(nsRect
& aRect
);
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
174 nsresult
GetClientRect(nsRect
& aRect
);
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().
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)
188 * @return error status
190 nsresult
BeginDocument(const nsAString
& aTitle
,
191 char16_t
* aPrintToFileName
,
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
199 * @return error status
201 nsresult
EndDocument();
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
208 nsresult
AbortDocument();
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
216 nsresult
BeginPage();
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
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()
232 bool CheckDPIChange();
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.
239 bool SetPixelScale(float aScale
);
242 * Returns the pixel scaling factor (page zoom factor) applied.
244 float GetPixelScale() const { return mPixelScale
; }
247 * True if this device context was created for printing.
249 bool IsPrinterSurface();
252 // Private destructor, to discourage deletion outside of Release():
256 void ComputeClientRectUsingScreen(nsRect
*outRect
);
257 void ComputeFullAreaUsingScreen(nsRect
*outRect
);
258 void FindScreen(nsIScreen
**outScreen
);
259 void CalcPrintingSize();
260 void UpdateScaledAppUnits();
265 int32_t mAppUnitsPerDevPixel
;
266 int32_t mAppUnitsPerDevNotScaledPixel
;
267 int32_t mAppUnitsPerPhysicalInch
;
269 float mPrintingScale
;
271 nsFontCache
* mFontCache
;
272 nsCOMPtr
<nsIWidget
> mWidget
;
273 nsCOMPtr
<nsIScreenManager
> mScreenManager
;
274 nsCOMPtr
<nsIDeviceContextSpec
> mDeviceContextSpec
;
275 nsRefPtr
<gfxASurface
> mPrintingSurface
;
277 nsRefPtr
<gfxASurface
> mCachedPrintingSurface
;
281 #endif /* _NS_DEVICECONTEXT_H_ */