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/. */
5 
6 #ifndef PDFVIAEMFPRINTHELPER_H_
7 #define PDFVIAEMFPRINTHELPER_H_
8 
9 #include "nsCOMPtr.h"
10 #include "PDFiumEngineShim.h"
11 #include "mozilla/Vector.h"
12 #include "mozilla/ipc/FileDescriptor.h"
13 #include "mozilla/ipc/Shmem.h"
14 
15 /* include windows.h for the HDC definitions that we need. */
16 #include <windows.h>
17 
18 class nsIFile;
19 class nsFileInputStream;
20 
21 namespace mozilla {
22 
23 namespace ipc {
24 class IShmemAllocator;
25 }
26 
27 namespace widget {
28 
29 /**
30  * This class helps draw a PDF file to a given Windows DC.
31  * To do that it first converts the PDF file to EMF.
32  * Windows EMF:
33  * https://msdn.microsoft.com/en-us/windows/hardware/drivers/print/emf-data-type
34  */
35 class PDFViaEMFPrintHelper {
36  public:
37   typedef mozilla::ipc::FileDescriptor FileDescriptor;
38 
39   PDFViaEMFPrintHelper();
40   virtual ~PDFViaEMFPrintHelper();
41 
42   /** Loads the specified PDF file. */
43   NS_IMETHOD OpenDocument(nsIFile* aFile);
44   NS_IMETHOD OpenDocument(const FileDescriptor& aFD);
45 
46   /** Releases document buffer. */
47   void CloseDocument();
48 
GetPageCount()49   int GetPageCount() const { return mPDFiumEngine->GetPageCount(mPDFDoc); }
50 
51   /**
52    * Convert the specified PDF page to EMF and draw the EMF onto the
53    * given DC.
54    */
55   bool DrawPage(HDC aPrinterDC, unsigned int aPageIndex, int aPageWidth,
56                 int aPageHeight);
57 
58   /** Convert the specified PDF page to EMF and save it to file. */
59   bool SavePageToFile(const wchar_t* aFilePath, unsigned int aPageIndex,
60                       int aPageWidth, int aPageHeight);
61 
62   /** Create a share memory and serialize the EMF content into it. */
63   bool SavePageToBuffer(unsigned int aPageIndex, int aPageWidth,
64                         int aPageHeight, ipc::Shmem& aMem,
65                         mozilla::ipc::IShmemAllocator* aAllocator);
66 
67  protected:
68   virtual bool CreatePDFiumEngineIfNeed();
69   bool RenderPageToDC(HDC aDC, unsigned int aPageIndex, int aPageWidth,
70                       int aPageHeight);
71 
72   RefPtr<PDFiumEngineShim> mPDFiumEngine;
73   FPDF_DOCUMENT mPDFDoc;
74   PRFileDesc* mPrfile;
75 };
76 
77 }  // namespace widget
78 }  // namespace mozilla
79 
80 #endif /* PDFVIAEMFPRINTHELPER_H_ */
81