forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPDFViaEMFPrintHelper.cpp
189 lines (155 loc) · 5.62 KB
/
PDFViaEMFPrintHelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "PDFViaEMFPrintHelper.h"
#include "nsIFileStreams.h"
#include "WindowsEMF.h"
#include "nsFileStreams.h"
#include "mozilla/UniquePtrExtensions.h"
#include "mozilla/dom/File.h"
#include "mozilla/Unused.h"
/* Scale DC by keeping aspect ratio */
static
float ComputeScaleFactor(int aDCWidth, int aDCHeight,
int aPageWidth, int aPageHeight)
{
MOZ_ASSERT(aPageWidth !=0 && aPageWidth != 0);
return (aDCWidth >= aPageWidth && aDCHeight >= aPageHeight)
? 1.0 /* If page fits DC - no scaling needed. */
: std::min(static_cast<float>(aDCWidth) / static_cast<float>(aPageWidth), static_cast<float>(aDCHeight) / static_cast<float>(aPageHeight));
}
PDFViaEMFPrintHelper::PDFViaEMFPrintHelper()
: mPDFDoc(nullptr)
{
}
PDFViaEMFPrintHelper::~PDFViaEMFPrintHelper()
{
CloseDocument();
}
nsresult
PDFViaEMFPrintHelper::OpenDocument(nsIFile *aFile)
{
MOZ_ASSERT(aFile);
if (mPDFDoc) {
MOZ_ASSERT_UNREACHABLE("We can only open one PDF at a time,"
"Use CloseDocument() to close the opened file"
"before calling OpenDocument()");
return NS_ERROR_FAILURE;
}
nsAutoCString nativePath;
nsresult rv = aFile->GetNativePath(nativePath);
if (NS_FAILED(rv)) {
return rv;
}
return OpenDocument(nativePath.get());
}
nsresult
PDFViaEMFPrintHelper::OpenDocument(const char* aFileName)
{
MOZ_ASSERT(aFileName);
if (mPDFDoc) {
MOZ_ASSERT_UNREACHABLE("We can only open one PDF at a time,"
"Use CloseDocument() to close the opened file"
"before calling OpenDocument()");
return NS_ERROR_FAILURE;
}
NS_ENSURE_TRUE(CreatePDFiumEngineIfNeed(), NS_ERROR_FAILURE);
mPDFDoc = mPDFiumEngine->LoadDocument(aFileName, nullptr);
NS_ENSURE_TRUE(mPDFDoc, NS_ERROR_FAILURE);
return NS_OK;
}
bool
PDFViaEMFPrintHelper::RenderPageToDC(HDC aDC, unsigned int aPageIndex,
int aPageWidth, int aPageHeight)
{
MOZ_ASSERT(aDC && mPDFDoc);
MOZ_ASSERT(static_cast<int>(aPageIndex) <
mPDFiumEngine->GetPageCount(mPDFDoc));
if (aPageWidth <= 0 || aPageHeight <= 0) {
return false;
}
FPDF_PAGE pdfPage = mPDFiumEngine->LoadPage(mPDFDoc, aPageIndex);
NS_ENSURE_TRUE(pdfPage, false);
float scaleFactor = ComputeScaleFactor(::GetDeviceCaps(aDC, HORZRES),
::GetDeviceCaps(aDC, VERTRES),
aPageWidth, aPageHeight);
int savedState = ::SaveDC(aDC);
::SetGraphicsMode(aDC, GM_ADVANCED);
XFORM xform = { 0 };
xform.eM11 = xform.eM22 = scaleFactor;
::ModifyWorldTransform(aDC, &xform, MWT_LEFTMULTIPLY);
// The caller wanted all drawing to happen within the bounds specified.
// Based on scale calculations, our destination rect might be larger
// than the bounds. Set the clip rect to the bounds.
::IntersectClipRect(aDC, 0, 0, aPageWidth, aPageHeight);
mPDFiumEngine->RenderPage(aDC, pdfPage,
0, 0, aPageWidth, aPageHeight,
0, FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
mPDFiumEngine->ClosePage(pdfPage);
::RestoreDC(aDC, savedState);
return true;
}
bool
PDFViaEMFPrintHelper::DrawPage(HDC aPrinterDC, unsigned int aPageIndex,
int aPageWidth, int aPageHeight)
{
MOZ_ASSERT(aPrinterDC);
// OpenDocument might fail.
if (!mPDFDoc) {
MOZ_ASSERT_UNREACHABLE("Make sure OpenDocument return true before"
"using DrawPage.");
return false;
}
// There is a comment in Chromium.
// https://cs.chromium.org/chromium/src/pdf/pdfium/pdfium_engine.cc?rcl=9ad9f6860b4d6a4ec7f7f975b2c99672e02d5d49&l=4008
// Some PDFs seems to render very slowly if RenderPageToDC is directly used
// on a printer DC.
// The way Chromium works around the issue at the code linked above is to
// print to a bitmap and send that to a printer. Instead of doing that we
// render to an EMF file and replay that on the printer DC. It is unclear
// whether our approach will avoid the performance issues though. Bug
// 1359298 covers investigating that.
WindowsEMF emf;
bool result = emf.InitForDrawing();
NS_ENSURE_TRUE(result, false);
result = RenderPageToDC(emf.GetDC(), aPageIndex, aPageWidth, aPageHeight);
NS_ENSURE_TRUE(result, false);
RECT printRect = {0, 0, aPageWidth, aPageHeight};
result = emf.Playback(aPrinterDC, printRect);
return result;
}
bool
PDFViaEMFPrintHelper::DrawPageToFile(const wchar_t* aFilePath,
unsigned int aPageIndex,
int aPageWidth, int aPageHeight)
{
// OpenDocument might fail.
if (!mPDFDoc) {
MOZ_ASSERT_UNREACHABLE("Make sure OpenDocument return true before"
"using DrawPageToFile.");
return false;
}
WindowsEMF emf;
bool result = emf.InitForDrawing(aFilePath);
NS_ENSURE_TRUE(result, false);
result = RenderPageToDC(emf.GetDC(), aPageIndex, aPageWidth, aPageHeight);
NS_ENSURE_TRUE(result, false);
return emf.SaveToFile();
}
void
PDFViaEMFPrintHelper::CloseDocument()
{
if (mPDFDoc) {
mPDFiumEngine->CloseDocument(mPDFDoc);
mPDFDoc = nullptr;
}
}
bool
PDFViaEMFPrintHelper::CreatePDFiumEngineIfNeed()
{
if (!mPDFiumEngine) {
mPDFiumEngine = PDFiumEngineShim::GetInstanceOrNull();
}
return !!mPDFiumEngine;
}