Line data Source code
1 : /* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/. */
5 :
6 : #ifndef _NS_DEVICECONTEXT_H_
7 : #define _NS_DEVICECONTEXT_H_
8 :
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 :
24 : class gfxContext;
25 : class gfxTextPerfMetrics;
26 : class gfxUserFontSet;
27 : struct nsFont;
28 : class nsFontCache;
29 : class nsIAtom;
30 : class nsIDeviceContextSpec;
31 : class nsIScreen;
32 : class nsIScreenManager;
33 : class nsIWidget;
34 : struct nsRect;
35 :
36 : namespace mozilla {
37 : namespace gfx {
38 : class PrintTarget;
39 : }
40 : }
41 :
42 : class nsDeviceContext final
43 : {
44 : public:
45 : typedef mozilla::gfx::PrintTarget PrintTarget;
46 :
47 : nsDeviceContext();
48 :
49 101 : NS_INLINE_DECL_REFCOUNTING(nsDeviceContext)
50 :
51 : /**
52 : * Initialize the device context from a widget
53 : * @param aWidget a widget to initialize the device context from
54 : * @return error status
55 : */
56 : nsresult Init(nsIWidget *aWidget);
57 :
58 : /*
59 : * Initialize the font cache if it hasn't been initialized yet.
60 : * (Needed for stylo)
61 : */
62 : void InitFontCache();
63 :
64 : void UpdateFontCacheUserFonts(gfxUserFontSet* aUserFontSet);
65 :
66 : /**
67 : * Initialize the device context from a device context spec
68 : * @param aDevSpec the specification of the printing device
69 : * @return error status
70 : */
71 : nsresult InitForPrinting(nsIDeviceContextSpec *aDevSpec);
72 :
73 : /**
74 : * Create a rendering context and initialize it. Only call this
75 : * method on device contexts that were initialized for printing.
76 : *
77 : * @return the new rendering context (guaranteed to be non-null)
78 : */
79 : already_AddRefed<gfxContext> CreateRenderingContext();
80 :
81 : /**
82 : * Create a reference rendering context and initialize it. Only call this
83 : * method on device contexts that were initialized for printing.
84 : *
85 : * @return the new rendering context.
86 : */
87 : already_AddRefed<gfxContext> CreateReferenceRenderingContext();
88 :
89 : /**
90 : * Gets the number of app units in one CSS pixel; this number is global,
91 : * not unique to each device context.
92 : */
93 160 : static int32_t AppUnitsPerCSSPixel() { return mozilla::AppUnitsPerCSSPixel(); }
94 :
95 : /**
96 : * Gets the number of app units in one device pixel; this number
97 : * is usually a factor of AppUnitsPerCSSPixel(), although that is
98 : * not guaranteed.
99 : */
100 19771 : int32_t AppUnitsPerDevPixel() const { return mAppUnitsPerDevPixel; }
101 :
102 : /**
103 : * Convert device pixels which is used for gfx/thebes to nearest
104 : * (rounded) app units
105 : */
106 0 : nscoord GfxUnitsToAppUnits(gfxFloat aGfxUnits) const
107 0 : { return nscoord(NS_round(aGfxUnits * AppUnitsPerDevPixel())); }
108 :
109 : /**
110 : * Convert app units to device pixels which is used for gfx/thebes.
111 : */
112 172 : gfxFloat AppUnitsToGfxUnits(nscoord aAppUnits) const
113 172 : { return gfxFloat(aAppUnits) / AppUnitsPerDevPixel(); }
114 :
115 : /**
116 : * Gets the number of app units in one physical inch; this is the
117 : * device's DPI times AppUnitsPerDevPixel().
118 : */
119 0 : int32_t AppUnitsPerPhysicalInch() const
120 0 : { return mAppUnitsPerPhysicalInch; }
121 :
122 : /**
123 : * Gets the number of app units in one CSS inch; this is
124 : * 96 times AppUnitsPerCSSPixel.
125 : */
126 0 : static int32_t AppUnitsPerCSSInch() { return mozilla::AppUnitsPerCSSInch(); }
127 :
128 : /**
129 : * Get the ratio of app units to dev pixels that would be used at unit
130 : * (100%) full zoom.
131 : */
132 100 : int32_t AppUnitsPerDevPixelAtUnitFullZoom() const
133 100 : { return mAppUnitsPerDevPixelAtUnitFullZoom; }
134 :
135 : /**
136 : * Get the nsFontMetrics that describe the properties of
137 : * an nsFont.
138 : * @param aFont font description to obtain metrics for
139 : */
140 : already_AddRefed<nsFontMetrics> GetMetricsFor(
141 : const nsFont& aFont, const nsFontMetrics::Params& aParams);
142 :
143 : /**
144 : * Notification when a font metrics instance created for this device is
145 : * about to be deleted
146 : */
147 : nsresult FontMetricsDeleted(const nsFontMetrics* aFontMetrics);
148 :
149 : /**
150 : * Attempt to free up resources by flushing out any fonts no longer
151 : * referenced by anything other than the font cache itself.
152 : * @return error status
153 : */
154 : nsresult FlushFontCache();
155 :
156 : /**
157 : * Return the bit depth of the device.
158 : */
159 : nsresult GetDepth(uint32_t& aDepth);
160 :
161 : /**
162 : * Get the size of the displayable area of the output device
163 : * in app units.
164 : * @param aWidth out parameter for width
165 : * @param aHeight out parameter for height
166 : * @return error status
167 : */
168 : nsresult GetDeviceSurfaceDimensions(nscoord& aWidth, nscoord& aHeight);
169 :
170 : /**
171 : * Get the size of the content area of the output device in app
172 : * units. This corresponds on a screen device, for instance, to
173 : * the entire screen.
174 : * @param aRect out parameter for full rect. Position (x,y) will
175 : * be (0,0) or relative to the primary monitor if
176 : * this is not the primary.
177 : * @return error status
178 : */
179 : nsresult GetRect(nsRect& aRect);
180 :
181 : /**
182 : * Get the size of the content area of the output device in app
183 : * units. This corresponds on a screen device, for instance, to
184 : * the area reported by GetDeviceSurfaceDimensions, minus the
185 : * taskbar (Windows) or menubar (Macintosh).
186 : * @param aRect out parameter for client rect. Position (x,y) will
187 : * be (0,0) adjusted for any upper/left non-client
188 : * space if present or relative to the primary
189 : * monitor if this is not the primary.
190 : * @return error status
191 : */
192 : nsresult GetClientRect(nsRect& aRect);
193 :
194 : /**
195 : * Returns true if we're currently between BeginDocument() and
196 : * EndDocument() calls.
197 : */
198 0 : bool IsCurrentlyPrintingDocument() const { return mIsCurrentlyPrintingDoc; }
199 :
200 : /**
201 : * Inform the output device that output of a document is beginning
202 : * Used for print related device contexts. Must be matched 1:1 with
203 : * EndDocument() or AbortDocument().
204 : *
205 : * @param aTitle - title of Document
206 : * @param aPrintToFileName - name of file to print to, if empty then don't
207 : * print to file
208 : * @param aStartPage - starting page number (must be greater than zero)
209 : * @param aEndPage - ending page number (must be less than or
210 : * equal to number of pages)
211 : *
212 : * @return error status
213 : */
214 : nsresult BeginDocument(const nsAString& aTitle,
215 : const nsAString& aPrintToFileName,
216 : int32_t aStartPage,
217 : int32_t aEndPage);
218 :
219 : /**
220 : * Inform the output device that output of a document is ending.
221 : * Used for print related device contexts. Must be matched 1:1 with
222 : * BeginDocument()
223 : * @return error status
224 : */
225 : nsresult EndDocument();
226 :
227 : /**
228 : * Inform the output device that output of a document is being aborted.
229 : * Must be matched 1:1 with BeginDocument()
230 : * @return error status
231 : */
232 : nsresult AbortDocument();
233 :
234 : /**
235 : * Inform the output device that output of a page is beginning
236 : * Used for print related device contexts. Must be matched 1:1 with
237 : * EndPage() and within a BeginDocument()/EndDocument() pair.
238 : * @return error status
239 : */
240 : nsresult BeginPage();
241 :
242 : /**
243 : * Inform the output device that output of a page is ending
244 : * Used for print related device contexts. Must be matched 1:1 with
245 : * BeginPage() and within a BeginDocument()/EndDocument() pair.
246 : * @return error status
247 : */
248 : nsresult EndPage();
249 :
250 : /**
251 : * Check to see if the DPI has changed, or impose a new DPI scale value.
252 : * @param aScale - If non-null, the default (unzoomed) CSS to device pixel
253 : * scale factor will be returned here; and if it is > 0.0
254 : * on input, the given value will be used instead of
255 : * getting it from the widget (if any). This is used to
256 : * allow subdocument contexts to inherit the resolution
257 : * setting of their parent.
258 : * @return whether there was actually a change in the DPI (whether
259 : * AppUnitsPerDevPixel() or AppUnitsPerPhysicalInch()
260 : * changed)
261 : */
262 : bool CheckDPIChange(double* aScale = nullptr);
263 :
264 : /**
265 : * Set the full zoom factor: all lengths are multiplied by this factor
266 : * when we convert them to device pixels. Returns whether the ratio of
267 : * app units to dev pixels changed because of the zoom factor.
268 : */
269 : bool SetFullZoom(float aScale);
270 :
271 : /**
272 : * Returns the page full zoom factor applied.
273 : */
274 5 : float GetFullZoom() const { return mFullZoom; }
275 :
276 : /**
277 : * True if this device context was created for printing.
278 : */
279 : bool IsPrinterContext();
280 :
281 : mozilla::DesktopToLayoutDeviceScale GetDesktopToDeviceScale();
282 :
283 : private:
284 : // Private destructor, to discourage deletion outside of Release():
285 : ~nsDeviceContext();
286 :
287 : /**
288 : * Implementation shared by CreateRenderingContext and
289 : * CreateReferenceRenderingContext.
290 : */
291 : already_AddRefed<gfxContext>
292 : CreateRenderingContextCommon(bool aWantReferenceContext);
293 :
294 : void SetDPI(double* aScale = nullptr);
295 : void ComputeClientRectUsingScreen(nsRect *outRect);
296 : void ComputeFullAreaUsingScreen(nsRect *outRect);
297 : void FindScreen(nsIScreen **outScreen);
298 :
299 : // Return false if the surface is not right
300 : bool CalcPrintingSize();
301 : void UpdateAppUnitsForFullZoom();
302 :
303 : nscoord mWidth;
304 : nscoord mHeight;
305 : int32_t mAppUnitsPerDevPixel;
306 : int32_t mAppUnitsPerDevPixelAtUnitFullZoom;
307 : int32_t mAppUnitsPerPhysicalInch;
308 : float mFullZoom;
309 : float mPrintingScale;
310 :
311 : RefPtr<nsFontCache> mFontCache;
312 : nsCOMPtr<nsIWidget> mWidget;
313 : nsCOMPtr<nsIScreenManager> mScreenManager;
314 : nsCOMPtr<nsIDeviceContextSpec> mDeviceContextSpec;
315 : RefPtr<PrintTarget> mPrintTarget;
316 : bool mIsCurrentlyPrintingDoc;
317 : #ifdef DEBUG
318 : bool mIsInitialized;
319 : #endif
320 : };
321 :
322 : #endif /* _NS_DEVICECONTEXT_H_ */
|