Bug 1771374 - Fix lint warnings. r=gfx-reviewers,aosmond
[gecko.git] / gfx / src / nsDeviceContext.h
blob5c78df2557dc7e34562892639d63f5225d054a8d
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/PrintTarget.h" // for PrintTarget::PageDoneCallback
25 class gfxContext;
26 class gfxTextPerfMetrics;
27 class gfxUserFontSet;
28 struct nsFont;
29 class nsAtom;
30 class nsIDeviceContextSpec;
31 class nsIScreen;
32 class nsIScreenManager;
33 class nsIWidget;
34 struct nsRect;
36 class nsDeviceContext final {
37 public:
38 typedef mozilla::gfx::PrintTarget PrintTarget;
40 nsDeviceContext();
42 NS_INLINE_DECL_REFCOUNTING(nsDeviceContext)
44 /**
45 * Initialize the device context from a widget
46 * @param aWidget a widget to initialize the device context from
47 * @return error status
49 nsresult Init(nsIWidget* aWidget);
51 /**
52 * Initialize the device context from a device context spec
53 * @param aDevSpec the specification of the printing device
54 * @return error status
56 nsresult InitForPrinting(nsIDeviceContextSpec* aDevSpec);
58 /**
59 * Create a rendering context and initialize it. Only call this
60 * method on device contexts that were initialized for printing.
62 * @return the new rendering context (guaranteed to be non-null)
64 already_AddRefed<gfxContext> CreateRenderingContext();
66 /**
67 * Create a reference rendering context and initialize it. Only call this
68 * method on device contexts that were initialized for printing.
70 * @return the new rendering context.
72 already_AddRefed<gfxContext> CreateReferenceRenderingContext();
74 /**
75 * Gets the number of app units in one device pixel; this number
76 * is usually a factor of AppUnitsPerCSSPixel(), although that is
77 * not guaranteed.
79 int32_t AppUnitsPerDevPixel() const { return mAppUnitsPerDevPixel; }
81 /**
82 * Convert device pixels which is used for gfx/thebes to nearest
83 * (rounded) app units
85 nscoord GfxUnitsToAppUnits(gfxFloat aGfxUnits) const {
86 return nscoord(NS_round(aGfxUnits * AppUnitsPerDevPixel()));
89 /**
90 * Convert app units to device pixels which is used for gfx/thebes.
92 gfxFloat AppUnitsToGfxUnits(nscoord aAppUnits) const {
93 return gfxFloat(aAppUnits) / AppUnitsPerDevPixel();
96 /**
97 * Gets the number of app units in one physical inch; this is the
98 * device's DPI times AppUnitsPerDevPixel().
100 int32_t AppUnitsPerPhysicalInch() const { return mAppUnitsPerPhysicalInch; }
103 * Get the ratio of app units to dev pixels that would be used at unit
104 * (100%) full zoom.
106 int32_t AppUnitsPerDevPixelAtUnitFullZoom() const {
107 return mAppUnitsPerDevPixelAtUnitFullZoom;
111 * Return the bit depth of the device.
113 uint32_t GetDepth();
116 * Get the size of the displayable area of the output device
117 * in app units.
118 * @param aWidth out parameter for width
119 * @param aHeight out parameter for height
120 * @return error status
122 nsresult GetDeviceSurfaceDimensions(nscoord& aWidth, nscoord& aHeight);
125 * Get the size of the content area of the output device in app
126 * units. This corresponds on a screen device, for instance, to
127 * the entire screen.
128 * @param aRect out parameter for full rect. Position (x,y) will
129 * be (0,0) or relative to the primary monitor if
130 * this is not the primary.
131 * @return error status
133 nsresult GetRect(nsRect& aRect);
136 * Get the size of the content area of the output device in app
137 * units. This corresponds on a screen device, for instance, to
138 * the area reported by GetDeviceSurfaceDimensions, minus the
139 * taskbar (Windows) or menubar (Macintosh).
140 * @param aRect out parameter for client rect. Position (x,y) will
141 * be (0,0) adjusted for any upper/left non-client
142 * space if present or relative to the primary
143 * monitor if this is not the primary.
144 * @return error status
146 nsresult GetClientRect(nsRect& aRect);
149 * Returns true if we're currently between BeginDocument() and
150 * EndDocument() calls.
152 bool IsCurrentlyPrintingDocument() const { return mIsCurrentlyPrintingDoc; }
155 * Inform the output device that output of a document is beginning
156 * Used for print related device contexts. Must be matched 1:1 with
157 * EndDocument() or AbortDocument().
159 * @param aTitle - title of Document
160 * @param aPrintToFileName - name of file to print to, if empty then don't
161 * print to file
162 * @param aStartPage - starting page number (must be greater than zero)
163 * @param aEndPage - ending page number (must be less than or
164 * equal to number of pages)
166 * @return error status
168 nsresult BeginDocument(const nsAString& aTitle,
169 const nsAString& aPrintToFileName, int32_t aStartPage,
170 int32_t aEndPage);
173 * Inform the output device that output of a document is ending.
174 * Used for print related device contexts. Must be matched 1:1 with
175 * BeginDocument()
176 * @return error status
178 nsresult EndDocument();
181 * Inform the output device that output of a document is being aborted.
182 * Must be matched 1:1 with BeginDocument()
183 * @return error status
185 nsresult AbortDocument();
188 * Inform the output device that output of a page is beginning
189 * Used for print related device contexts. Must be matched 1:1 with
190 * EndPage() and within a BeginDocument()/EndDocument() pair.
191 * @return error status
193 nsresult BeginPage();
196 * Inform the output device that output of a page is ending
197 * Used for print related device contexts. Must be matched 1:1 with
198 * BeginPage() and within a BeginDocument()/EndDocument() pair.
199 * @return error status
201 nsresult EndPage();
204 * Check to see if the DPI has changed, or impose a new DPI scale value.
205 * @return whether there was actually a change in the DPI (whether
206 * AppUnitsPerDevPixel() or AppUnitsPerPhysicalInch()
207 * changed)
209 bool CheckDPIChange();
212 * Set the full zoom factor: all lengths are multiplied by this factor
213 * when we convert them to device pixels. Returns whether the ratio of
214 * app units to dev pixels changed because of the zoom factor.
216 bool SetFullZoom(float aScale);
219 * Returns the page full zoom factor applied.
221 float GetFullZoom() const { return mFullZoom; }
224 * True if this device context was created for printing.
226 bool IsPrinterContext() const { return !!mPrintTarget; }
228 mozilla::DesktopToLayoutDeviceScale GetDesktopToDeviceScale();
230 private:
231 // Private destructor, to discourage deletion outside of Release():
232 ~nsDeviceContext();
235 * Implementation shared by CreateRenderingContext and
236 * CreateReferenceRenderingContext.
238 already_AddRefed<gfxContext> CreateRenderingContextCommon(
239 bool aWantReferenceContext);
241 void SetDPI();
242 void ComputeClientRectUsingScreen(nsRect* outRect);
243 void ComputeFullAreaUsingScreen(nsRect* outRect);
244 void FindScreen(nsIScreen** outScreen);
246 // Return false if the surface is not right
247 bool CalcPrintingSize();
248 void UpdateAppUnitsForFullZoom();
250 nscoord mWidth;
251 nscoord mHeight;
252 int32_t mAppUnitsPerDevPixel;
253 int32_t mAppUnitsPerDevPixelAtUnitFullZoom;
254 int32_t mAppUnitsPerPhysicalInch;
255 float mFullZoom;
256 float mPrintingScale;
257 gfxPoint mPrintingTranslate;
259 nsCOMPtr<nsIWidget> mWidget;
260 nsCOMPtr<nsIScreenManager> mScreenManager;
261 nsCOMPtr<nsIDeviceContextSpec> mDeviceContextSpec;
262 RefPtr<PrintTarget> mPrintTarget;
263 bool mIsCurrentlyPrintingDoc;
264 #ifdef DEBUG
265 bool mIsInitialized;
266 #endif
269 #endif /* _NS_DEVICECONTEXT_H_ */