1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 "gfxFont.h" // for gfxFont::Orientation
13 #include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER2
14 #include "mozilla/RefPtr.h" // for RefPtr
15 #include "nsCOMPtr.h" // for nsCOMPtr
16 #include "nsCoord.h" // for nscoord
17 #include "nsError.h" // for nsresult
18 #include "nsISupports.h" // for NS_INLINE_DECL_REFCOUNTING
19 #include "nsMathUtils.h" // for NS_round
20 #include "nscore.h" // for char16_t, nsAString
21 #include "mozilla/AppUnits.h" // for AppUnits
22 #include "nsFontMetrics.h" // for nsFontMetrics::Params
23 #include "mozilla/gfx/Point.h" // for IntSize
24 #include "mozilla/gfx/PrintTarget.h" // for PrintTarget::PageDoneCallback
25 #include "mozilla/gfx/PrintPromise.h"
28 class gfxTextPerfMetrics
;
32 class nsIDeviceContextSpec
;
34 class nsIScreenManager
;
40 enum class ScreenColorGamut
: uint8_t;
43 enum class ScreenOrientation
: uint32_t;
48 } // namespace mozilla
50 class nsDeviceContext final
{
52 using IntSize
= mozilla::gfx::IntSize
;
53 using PrintTarget
= mozilla::gfx::PrintTarget
;
57 NS_INLINE_DECL_REFCOUNTING(nsDeviceContext
)
60 * Initialize the device context from a widget
61 * @param aWidget a widget to initialize the device context from
63 void Init(nsIWidget
* aWidget
);
66 * Initialize the device context from a device context spec
67 * @param aDevSpec the specification of the printing device
68 * @return error status
70 nsresult
InitForPrinting(nsIDeviceContextSpec
* aDevSpec
);
73 * Create a rendering context and initialize it. Only call this
74 * method on device contexts that were initialized for printing.
76 * @return the new rendering context (guaranteed to be non-null)
78 mozilla::UniquePtr
<gfxContext
> CreateRenderingContext();
81 * Create a reference rendering context and initialize it. Only call this
82 * method on device contexts that were initialized for printing.
84 * @return the new rendering context.
86 mozilla::UniquePtr
<gfxContext
> CreateReferenceRenderingContext();
89 * Gets the number of app units in one device pixel; this number
90 * is usually a factor of AppUnitsPerCSSPixel(), although that is
93 int32_t AppUnitsPerDevPixel() const { return mAppUnitsPerDevPixel
; }
96 * Convert device pixels which is used for gfx/thebes to nearest
99 nscoord
GfxUnitsToAppUnits(gfxFloat aGfxUnits
) const {
100 return nscoord(NS_round(aGfxUnits
* AppUnitsPerDevPixel()));
104 * Convert app units to device pixels which is used for gfx/thebes.
106 gfxFloat
AppUnitsToGfxUnits(nscoord aAppUnits
) const {
107 return gfxFloat(aAppUnits
) / AppUnitsPerDevPixel();
111 * Gets the number of app units in one physical inch; this is the
112 * device's DPI times AppUnitsPerDevPixel().
114 int32_t AppUnitsPerPhysicalInch() const { return mAppUnitsPerPhysicalInch
; }
117 * Get the ratio of app units to dev pixels that would be used at unit
120 int32_t AppUnitsPerDevPixelAtUnitFullZoom() const {
121 return mAppUnitsPerDevPixelAtUnitFullZoom
;
125 * Get the ratio of app units to dev pixels that would be used in a top-level
126 * chrome page such as browser.xhtml.
128 int32_t AppUnitsPerDevPixelInTopLevelChromePage() const;
131 * Return the bit depth of the device.
136 * Return the color gamut of the device.
138 mozilla::dom::ScreenColorGamut
GetColorGamut();
141 * Return the orientation type of the device.
142 * If not screen device, return primary screen's value
144 mozilla::hal::ScreenOrientation
GetScreenOrientationType();
147 * Return the orientation angle of the device.
148 * If not screen device, return primary screen's value
150 uint16_t GetScreenOrientationAngle();
153 * Get the status of HDR support of the associated screen.
155 bool GetScreenIsHDR();
158 * Get the size of the displayable area of the output device
160 * @param aWidth out parameter for width
161 * @param aHeight out parameter for height
162 * @return error status
164 nsresult
GetDeviceSurfaceDimensions(nscoord
& aWidth
, nscoord
& aHeight
);
167 * Get the size of the content area of the output device in app
168 * units. This corresponds on a screen device, for instance, to
170 * @param aRect out parameter for full rect. Position (x,y) will
171 * be (0,0) or relative to the primary monitor if
172 * this is not the primary.
173 * @return error status
175 nsresult
GetRect(nsRect
& aRect
);
178 * Get the size of the content area of the output device in app
179 * units. This corresponds on a screen device, for instance, to
180 * the area reported by GetDeviceSurfaceDimensions, minus the
181 * taskbar (Windows) or menubar (Macintosh).
182 * @param aRect out parameter for client rect. Position (x,y) will
183 * be (0,0) adjusted for any upper/left non-client
184 * space if present or relative to the primary
185 * monitor if this is not the primary.
186 * @return error status
188 nsresult
GetClientRect(nsRect
& aRect
);
191 * Returns true if we're currently between BeginDocument() and
192 * EndDocument() calls.
194 bool IsCurrentlyPrintingDocument() const { return mIsCurrentlyPrintingDoc
; }
197 * Inform the output device that output of a document is beginning
198 * Used for print related device contexts. Must be matched 1:1 with
199 * EndDocument() or AbortDocument().
201 * @param aTitle - title of Document
202 * @param aPrintToFileName - name of file to print to, if empty then don't
204 * @param aStartPage - starting page number (must be greater than zero)
205 * @param aEndPage - ending page number (must be less than or
206 * equal to number of pages)
208 * @return error status
210 nsresult
BeginDocument(const nsAString
& aTitle
,
211 const nsAString
& aPrintToFileName
, int32_t aStartPage
,
215 * Inform the output device that output of a document is ending.
216 * Used for print related device contexts. Must be matched 1:1 with
218 * @return Promise that can be chained once the operation is complete.
220 RefPtr
<mozilla::gfx::PrintEndDocumentPromise
> EndDocument();
223 * Inform the output device that output of a document is being aborted.
224 * Must be matched 1:1 with BeginDocument()
225 * @return error status
227 nsresult
AbortDocument();
230 * Inform the output device that output of a page is beginning
231 * Used for print related device contexts. Must be matched 1:1 with
232 * EndPage() and within a BeginDocument()/EndDocument() pair.
234 * @param aSizeInPoints - The physical dimensions of the page in points.
235 * Currently only supported (used) by print-to-PDF
236 * print targets, and then only to switch the
237 * orientation for a specific page (arbitrary page
238 * sizes are not supported by the Core Graphics print-
239 * to-PDF APIs, for example).
241 * @return error status
243 nsresult
BeginPage(const IntSize
& aSizeInPoints
);
246 * Inform the output device that output of a page is ending
247 * Used for print related device contexts. Must be matched 1:1 with
248 * BeginPage() and within a BeginDocument()/EndDocument() pair.
249 * @return error status
254 * Check to see if the DPI has changed, or impose a new DPI scale value.
255 * @return whether there was actually a change in the DPI (whether
256 * AppUnitsPerDevPixel() or AppUnitsPerPhysicalInch()
259 bool CheckDPIChange();
262 * Set the full zoom factor: all lengths are multiplied by this factor
263 * when we convert them to device pixels. Returns whether the ratio of
264 * app units to dev pixels changed because of the zoom factor.
266 bool SetFullZoom(float aScale
);
269 * Returns the page full zoom factor applied.
271 float GetFullZoom() const { return mFullZoom
; }
274 * True if this device context was created for printing.
276 bool IsPrinterContext() const { return !!mPrintTarget
; }
278 mozilla::DesktopToLayoutDeviceScale
GetDesktopToDeviceScale();
281 // Private destructor, to discourage deletion outside of Release():
285 * Implementation shared by CreateRenderingContext and
286 * CreateReferenceRenderingContext.
288 mozilla::UniquePtr
<gfxContext
> CreateRenderingContextCommon(
289 bool aWantReferenceContext
);
292 void ComputeClientRectUsingScreen(nsRect
* outRect
);
293 void ComputeFullAreaUsingScreen(nsRect
* outRect
);
294 already_AddRefed
<mozilla::widget::Screen
> FindScreen();
296 // Return false if the surface is not right
297 bool CalcPrintingSize();
298 void UpdateAppUnitsForFullZoom();
302 int32_t mAppUnitsPerDevPixel
;
303 int32_t mAppUnitsPerDevPixelAtUnitFullZoom
;
304 int32_t mAppUnitsPerPhysicalInch
;
306 float mPrintingScale
;
307 gfxPoint mPrintingTranslate
;
309 nsCOMPtr
<nsIWidget
> mWidget
;
310 nsCOMPtr
<nsIDeviceContextSpec
> mDeviceContextSpec
;
311 RefPtr
<PrintTarget
> mPrintTarget
;
312 bool mIsCurrentlyPrintingDoc
;
313 bool mIsInitialized
= false;
316 #endif /* _NS_DEVICECONTEXT_H_ */