Bug 1891710: part 2) Enable <Element-outerHTML.html> WPT for Trusted Types. r=smaug
[gecko.git] / gfx / src / nsDeviceContext.h
blob7bf7a6aee260e9ed013850a8b684c132fd8714ae
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"
27 class gfxContext;
28 class gfxTextPerfMetrics;
29 class gfxUserFontSet;
30 struct nsFont;
31 class nsAtom;
32 class nsIDeviceContextSpec;
33 class nsIScreen;
34 class nsIScreenManager;
35 class nsIWidget;
36 struct nsRect;
38 namespace mozilla {
39 namespace dom {
40 enum class ScreenColorGamut : uint8_t;
41 } // namespace dom
42 namespace hal {
43 enum class ScreenOrientation : uint32_t;
44 } // namespace hal
45 namespace widget {
46 class Screen;
47 } // namespace widget
48 } // namespace mozilla
50 class nsDeviceContext final {
51 public:
52 using IntSize = mozilla::gfx::IntSize;
53 using PrintTarget = mozilla::gfx::PrintTarget;
55 nsDeviceContext();
57 NS_INLINE_DECL_REFCOUNTING(nsDeviceContext)
59 /**
60 * Initialize the device context from a widget
61 * @param aWidget a widget to initialize the device context from
63 void Init(nsIWidget* aWidget);
65 /**
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);
72 /**
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();
80 /**
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();
88 /**
89 * Gets the number of app units in one device pixel; this number
90 * is usually a factor of AppUnitsPerCSSPixel(), although that is
91 * not guaranteed.
93 int32_t AppUnitsPerDevPixel() const { return mAppUnitsPerDevPixel; }
95 /**
96 * Convert device pixels which is used for gfx/thebes to nearest
97 * (rounded) app units
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
118 * (100%) full zoom.
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.
133 uint32_t GetDepth();
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
159 * in app units.
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
169 * the entire screen.
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
203 * print to file
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,
212 int32_t aEndPage);
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
217 * BeginDocument()
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
251 nsresult EndPage();
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()
257 * changed)
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();
280 private:
281 // Private destructor, to discourage deletion outside of Release():
282 ~nsDeviceContext();
285 * Implementation shared by CreateRenderingContext and
286 * CreateReferenceRenderingContext.
288 mozilla::UniquePtr<gfxContext> CreateRenderingContextCommon(
289 bool aWantReferenceContext);
291 void SetDPI();
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();
300 nscoord mWidth;
301 nscoord mHeight;
302 int32_t mAppUnitsPerDevPixel;
303 int32_t mAppUnitsPerDevPixelAtUnitFullZoom;
304 int32_t mAppUnitsPerPhysicalInch;
305 float mFullZoom;
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_ */