Bug 1797755 - Part 5: Use a single initial mark stack size regardless of whether...
[gecko.git] / gfx / src / nsDeviceContext.h
blobc326cb6bae27329d430b616420c389b3b34dd4f2
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 * Get the ratio of app units to dev pixels that would be used in a top-level
112 * chrome page such as browser.xhtml.
114 int32_t AppUnitsPerDevPixelInTopLevelChromePage() const;
117 * Return the bit depth of the device.
119 uint32_t GetDepth();
122 * Get the size of the displayable area of the output device
123 * in app units.
124 * @param aWidth out parameter for width
125 * @param aHeight out parameter for height
126 * @return error status
128 nsresult GetDeviceSurfaceDimensions(nscoord& aWidth, nscoord& aHeight);
131 * Get the size of the content area of the output device in app
132 * units. This corresponds on a screen device, for instance, to
133 * the entire screen.
134 * @param aRect out parameter for full rect. Position (x,y) will
135 * be (0,0) or relative to the primary monitor if
136 * this is not the primary.
137 * @return error status
139 nsresult GetRect(nsRect& aRect);
142 * Get the size of the content area of the output device in app
143 * units. This corresponds on a screen device, for instance, to
144 * the area reported by GetDeviceSurfaceDimensions, minus the
145 * taskbar (Windows) or menubar (Macintosh).
146 * @param aRect out parameter for client rect. Position (x,y) will
147 * be (0,0) adjusted for any upper/left non-client
148 * space if present or relative to the primary
149 * monitor if this is not the primary.
150 * @return error status
152 nsresult GetClientRect(nsRect& aRect);
155 * Returns true if we're currently between BeginDocument() and
156 * EndDocument() calls.
158 bool IsCurrentlyPrintingDocument() const { return mIsCurrentlyPrintingDoc; }
161 * Inform the output device that output of a document is beginning
162 * Used for print related device contexts. Must be matched 1:1 with
163 * EndDocument() or AbortDocument().
165 * @param aTitle - title of Document
166 * @param aPrintToFileName - name of file to print to, if empty then don't
167 * print to file
168 * @param aStartPage - starting page number (must be greater than zero)
169 * @param aEndPage - ending page number (must be less than or
170 * equal to number of pages)
172 * @return error status
174 nsresult BeginDocument(const nsAString& aTitle,
175 const nsAString& aPrintToFileName, int32_t aStartPage,
176 int32_t aEndPage);
179 * Inform the output device that output of a document is ending.
180 * Used for print related device contexts. Must be matched 1:1 with
181 * BeginDocument()
182 * @return error status
184 nsresult EndDocument();
187 * Inform the output device that output of a document is being aborted.
188 * Must be matched 1:1 with BeginDocument()
189 * @return error status
191 nsresult AbortDocument();
194 * Inform the output device that output of a page is beginning
195 * Used for print related device contexts. Must be matched 1:1 with
196 * EndPage() and within a BeginDocument()/EndDocument() pair.
197 * @return error status
199 nsresult BeginPage();
202 * Inform the output device that output of a page is ending
203 * Used for print related device contexts. Must be matched 1:1 with
204 * BeginPage() and within a BeginDocument()/EndDocument() pair.
205 * @return error status
207 nsresult EndPage();
210 * Check to see if the DPI has changed, or impose a new DPI scale value.
211 * @return whether there was actually a change in the DPI (whether
212 * AppUnitsPerDevPixel() or AppUnitsPerPhysicalInch()
213 * changed)
215 bool CheckDPIChange();
218 * Set the full zoom factor: all lengths are multiplied by this factor
219 * when we convert them to device pixels. Returns whether the ratio of
220 * app units to dev pixels changed because of the zoom factor.
222 bool SetFullZoom(float aScale);
225 * Returns the page full zoom factor applied.
227 float GetFullZoom() const { return mFullZoom; }
230 * True if this device context was created for printing.
232 bool IsPrinterContext() const { return !!mPrintTarget; }
234 mozilla::DesktopToLayoutDeviceScale GetDesktopToDeviceScale();
236 private:
237 // Private destructor, to discourage deletion outside of Release():
238 ~nsDeviceContext();
241 * Implementation shared by CreateRenderingContext and
242 * CreateReferenceRenderingContext.
244 already_AddRefed<gfxContext> CreateRenderingContextCommon(
245 bool aWantReferenceContext);
247 void SetDPI();
248 void ComputeClientRectUsingScreen(nsRect* outRect);
249 void ComputeFullAreaUsingScreen(nsRect* outRect);
250 void FindScreen(nsIScreen** outScreen);
252 // Return false if the surface is not right
253 bool CalcPrintingSize();
254 void UpdateAppUnitsForFullZoom();
256 nscoord mWidth;
257 nscoord mHeight;
258 int32_t mAppUnitsPerDevPixel;
259 int32_t mAppUnitsPerDevPixelAtUnitFullZoom;
260 int32_t mAppUnitsPerPhysicalInch;
261 float mFullZoom;
262 float mPrintingScale;
263 gfxPoint mPrintingTranslate;
265 nsCOMPtr<nsIWidget> mWidget;
266 nsCOMPtr<nsIScreenManager> mScreenManager;
267 nsCOMPtr<nsIDeviceContextSpec> mDeviceContextSpec;
268 RefPtr<PrintTarget> mPrintTarget;
269 bool mIsCurrentlyPrintingDoc;
270 #ifdef DEBUG
271 bool mIsInitialized;
272 #endif
275 #endif /* _NS_DEVICECONTEXT_H_ */