Bug 1852149 Part 1 - Add margin-rule pref r=firefox-style-system-reviewers,emilio
[gecko.git] / widget / nsIDeviceContextSpec.cpp
blob5ab442415dea963d155dd5a7bc7fc2fc845fabce
1 /* -*- Mode: C++; tab-width: 2; 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 #include "nsIDeviceContextSpec.h"
8 #include "gfxPoint.h"
9 #include "mozilla/gfx/PrintPromise.h"
10 #include "nsError.h"
11 #include "nsIPrintSettings.h"
13 #include "mozilla/Components.h"
14 #include "mozilla/TaskQueue.h"
16 using mozilla::MakeRefPtr;
17 using mozilla::gfx::PrintEndDocumentPromise;
19 // We have some platform specific code here rather than in the appropriate
20 // nsIDeviceContextSpec subclass. We structure the code this way so that
21 // nsIDeviceContextSpecProxy gets the correct behavior without us having to
22 // instantiate a platform specific nsIDeviceContextSpec subclass in content
23 // processes. That is necessary for sandboxing.
25 float nsIDeviceContextSpec::GetPrintingScale() {
26 #ifdef XP_WIN
27 if (mPrintSettings->GetOutputFormat() != nsIPrintSettings::kOutputFormatPDF
28 # ifdef MOZ_ENABLE_SKIA_PDF
29 && !mPrintViaSkPDF
30 # endif
31 ) {
32 // The print settings will have the resolution stored from the real device.
33 int32_t resolution;
34 mPrintSettings->GetResolution(&resolution);
35 return float(resolution) / GetDPI();
37 #endif
39 return 72.0f / GetDPI();
42 gfxPoint nsIDeviceContextSpec::GetPrintingTranslate() {
43 #ifdef XP_WIN
44 // The underlying surface on windows is the size of the printable region. When
45 // the region is smaller than the actual paper size the (0, 0) coordinate
46 // refers top-left of that unwritable region. To instead have (0, 0) become
47 // the top-left of the actual paper, translate it's coordinate system by the
48 // unprintable region's width.
49 double marginTop, marginLeft;
50 mPrintSettings->GetUnwriteableMarginTop(&marginTop);
51 mPrintSettings->GetUnwriteableMarginLeft(&marginLeft);
52 int32_t resolution;
53 mPrintSettings->GetResolution(&resolution);
54 return gfxPoint(-marginLeft * resolution, -marginTop * resolution);
55 #else
56 return gfxPoint(0, 0);
57 #endif
60 RefPtr<PrintEndDocumentPromise>
61 nsIDeviceContextSpec::EndDocumentPromiseFromResult(nsresult aResult,
62 const char* aSite) {
63 return NS_SUCCEEDED(aResult)
64 ? PrintEndDocumentPromise::CreateAndResolve(true, aSite)
65 : PrintEndDocumentPromise::CreateAndReject(aResult, aSite);
68 RefPtr<PrintEndDocumentPromise> nsIDeviceContextSpec::EndDocumentAsync(
69 const char* aCallSite, AsyncEndDocumentFunction aFunction) {
70 auto promise =
71 MakeRefPtr<PrintEndDocumentPromise::Private>("PrintEndDocumentPromise");
73 NS_DispatchBackgroundTask(
74 NS_NewRunnableFunction(
75 "EndDocumentAsync",
76 [promise, function = std::move(aFunction)]() mutable {
77 const auto result = function();
78 if (NS_SUCCEEDED(result)) {
79 promise->Resolve(true, __func__);
80 } else {
81 promise->Reject(result, __func__);
83 }),
84 NS_DISPATCH_EVENT_MAY_BLOCK);
86 return promise;