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 size of the displayable area of the output device
155 * @param aWidth out parameter for width
156 * @param aHeight out parameter for height
157 * @return error status
159 nsresult
GetDeviceSurfaceDimensions(nscoord
& aWidth
, nscoord
& aHeight
);
162 * Get the size of the content area of the output device in app
163 * units. This corresponds on a screen device, for instance, to
165 * @param aRect out parameter for full rect. Position (x,y) will
166 * be (0,0) or relative to the primary monitor if
167 * this is not the primary.
168 * @return error status
170 nsresult
GetRect(nsRect
& aRect
);
173 * Get the size of the content area of the output device in app
174 * units. This corresponds on a screen device, for instance, to
175 * the area reported by GetDeviceSurfaceDimensions, minus the
176 * taskbar (Windows) or menubar (Macintosh).
177 * @param aRect out parameter for client rect. Position (x,y) will
178 * be (0,0) adjusted for any upper/left non-client
179 * space if present or relative to the primary
180 * monitor if this is not the primary.
181 * @return error status
183 nsresult
GetClientRect(nsRect
& aRect
);
186 * Returns true if we're currently between BeginDocument() and
187 * EndDocument() calls.
189 bool IsCurrentlyPrintingDocument() const { return mIsCurrentlyPrintingDoc
; }
192 * Inform the output device that output of a document is beginning
193 * Used for print related device contexts. Must be matched 1:1 with
194 * EndDocument() or AbortDocument().
196 * @param aTitle - title of Document
197 * @param aPrintToFileName - name of file to print to, if empty then don't
199 * @param aStartPage - starting page number (must be greater than zero)
200 * @param aEndPage - ending page number (must be less than or
201 * equal to number of pages)
203 * @return error status
205 nsresult
BeginDocument(const nsAString
& aTitle
,
206 const nsAString
& aPrintToFileName
, int32_t aStartPage
,
210 * Inform the output device that output of a document is ending.
211 * Used for print related device contexts. Must be matched 1:1 with
213 * @return Promise that can be chained once the operation is complete.
215 RefPtr
<mozilla::gfx::PrintEndDocumentPromise
> EndDocument();
218 * Inform the output device that output of a document is being aborted.
219 * Must be matched 1:1 with BeginDocument()
220 * @return error status
222 nsresult
AbortDocument();
225 * Inform the output device that output of a page is beginning
226 * Used for print related device contexts. Must be matched 1:1 with
227 * EndPage() and within a BeginDocument()/EndDocument() pair.
229 * @param aSizeInPoints - The physical dimensions of the page in points.
230 * Currently only supported (used) by print-to-PDF
231 * print targets, and then only to switch the
232 * orientation for a specific page (arbitrary page
233 * sizes are not supported by the Core Graphics print-
234 * to-PDF APIs, for example).
236 * @return error status
238 nsresult
BeginPage(const IntSize
& aSizeInPoints
);
241 * Inform the output device that output of a page is ending
242 * Used for print related device contexts. Must be matched 1:1 with
243 * BeginPage() and within a BeginDocument()/EndDocument() pair.
244 * @return error status
249 * Check to see if the DPI has changed, or impose a new DPI scale value.
250 * @return whether there was actually a change in the DPI (whether
251 * AppUnitsPerDevPixel() or AppUnitsPerPhysicalInch()
254 bool CheckDPIChange();
257 * Set the full zoom factor: all lengths are multiplied by this factor
258 * when we convert them to device pixels. Returns whether the ratio of
259 * app units to dev pixels changed because of the zoom factor.
261 bool SetFullZoom(float aScale
);
264 * Returns the page full zoom factor applied.
266 float GetFullZoom() const { return mFullZoom
; }
269 * True if this device context was created for printing.
271 bool IsPrinterContext() const { return !!mPrintTarget
; }
273 mozilla::DesktopToLayoutDeviceScale
GetDesktopToDeviceScale();
276 // Private destructor, to discourage deletion outside of Release():
280 * Implementation shared by CreateRenderingContext and
281 * CreateReferenceRenderingContext.
283 mozilla::UniquePtr
<gfxContext
> CreateRenderingContextCommon(
284 bool aWantReferenceContext
);
287 void ComputeClientRectUsingScreen(nsRect
* outRect
);
288 void ComputeFullAreaUsingScreen(nsRect
* outRect
);
289 already_AddRefed
<mozilla::widget::Screen
> FindScreen();
291 // Return false if the surface is not right
292 bool CalcPrintingSize();
293 void UpdateAppUnitsForFullZoom();
297 int32_t mAppUnitsPerDevPixel
;
298 int32_t mAppUnitsPerDevPixelAtUnitFullZoom
;
299 int32_t mAppUnitsPerPhysicalInch
;
301 float mPrintingScale
;
302 gfxPoint mPrintingTranslate
;
304 nsCOMPtr
<nsIWidget
> mWidget
;
305 nsCOMPtr
<nsIDeviceContextSpec
> mDeviceContextSpec
;
306 RefPtr
<PrintTarget
> mPrintTarget
;
307 bool mIsCurrentlyPrintingDoc
;
308 bool mIsInitialized
= false;
311 #endif /* _NS_DEVICECONTEXT_H_ */