Coverage Report

Created: 2018-09-25 14:53

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