Bug 1345710 - part2: implement a PDFiumEngineShim class to link to PDFium symbols r=cjku,jwatt
authorFarmer Tseng <farmer.tseng@gmail.com>
Fri, 02 Dec 2016 12:02:56 +0800
changeset 404686 aff3b1f916214ed186a348b23651725e089d6a04
parent 404685 23d0ebe838a6b78df5591001dee6d746b0661be5
child 404687 bf788d19f2d38c09bd08cd0aaadd868bed55df98
push id7391
push usermtabara@mozilla.com
push dateMon, 12 Jun 2017 13:08:53 +0000
treeherdermozilla-beta@2191d7f87e2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscjku, jwatt
bugs1345710
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1345710 - part2: implement a PDFiumEngineShim class to link to PDFium symbols r=cjku,jwatt This class exposes an interface to PDFium library and takes care of loading and linking to the appropriate PDFium symbols. MozReview-Commit-ID: 32jZD6d0KFw
widget/windows/PDFiumEngineShim.cpp
widget/windows/PDFiumEngineShim.h
widget/windows/moz.build
new file mode 100644
--- /dev/null
+++ b/widget/windows/PDFiumEngineShim.cpp
@@ -0,0 +1,181 @@
+/* -*- 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 "PDFiumEngineShim.h"
+
+
+namespace mozilla {
+namespace widget {
+
+PDFiumEngineShim::PDFiumEngineShim(PRLibrary* aLibrary)
+  : mPRLibrary(aLibrary)
+  , mFPDF_InitLibrary(nullptr)
+  , mFPDF_DestroyLibrary(nullptr)
+  , mFPDF_LoadMemDocument(nullptr)
+  , mFPDF_CloseDocument(nullptr)
+  , mFPDF_GetPageCount(nullptr)
+  , mFPDF_GetPageSizeByIndex(nullptr)
+  , mFPDF_LoadPage(nullptr)
+  , mFPDF_ClosePage(nullptr)
+  , mFPDF_RenderPage(nullptr)
+  , mFPDF_RenderPage_Close(nullptr)
+  , mInitialized(false)
+{
+  MOZ_ASSERT(mPRLibrary);
+}
+
+PDFiumEngineShim::~PDFiumEngineShim()
+{
+}
+
+bool
+PDFiumEngineShim::InitSymbolsAndLibrary()
+{
+  if (mInitialized) {
+    return true;
+  }
+
+  mFPDF_InitLibrary = (FPDF_InitLibrary_Pfn)PR_FindFunctionSymbol(
+    mPRLibrary, "FPDF_InitLibrary");
+  if (!mFPDF_InitLibrary) {
+    return false;
+  }
+  mFPDF_DestroyLibrary = (FPDF_DestroyLibrary_Pfn)PR_FindFunctionSymbol(
+    mPRLibrary, "FPDF_DestroyLibrary");
+  if (!mFPDF_DestroyLibrary) {
+    return false;
+  }
+  mFPDF_LoadMemDocument = (FPDF_LoadMemDocument_Pfn)PR_FindFunctionSymbol(
+    mPRLibrary, "FPDF_LoadMemDocument");
+  if (!mFPDF_LoadMemDocument) {
+    return false;
+  }
+  mFPDF_CloseDocument = (FPDF_CloseDocument_Pfn)PR_FindFunctionSymbol(
+    mPRLibrary, "FPDF_CloseDocument");
+  if (!mFPDF_CloseDocument) {
+    return false;
+  }
+  mFPDF_GetPageCount = (FPDF_GetPageCount_Pfn)PR_FindFunctionSymbol(
+    mPRLibrary, "FPDF_GetPageCount");
+  if (!mFPDF_GetPageCount) {
+    return false;
+  }
+  mFPDF_GetPageSizeByIndex = (FPDF_GetPageSizeByIndex_Pfn)PR_FindFunctionSymbol(
+    mPRLibrary, "FPDF_GetPageSizeByIndex");
+  if (!mFPDF_GetPageSizeByIndex) {
+    return false;
+  }
+  mFPDF_LoadPage = (FPDF_LoadPage_Pfn)PR_FindFunctionSymbol(
+    mPRLibrary, "FPDF_LoadPage");
+  if (!mFPDF_LoadPage) {
+    return false;
+  }
+  mFPDF_ClosePage = (FPDF_ClosePage_Pfn)PR_FindFunctionSymbol(
+    mPRLibrary, "FPDF_ClosePage");
+  if (!mFPDF_ClosePage) {
+    return false;
+  }
+  mFPDF_RenderPage = (FPDF_RenderPage_Pfn)PR_FindFunctionSymbol(
+    mPRLibrary, "FPDF_RenderPage");
+  if (!mFPDF_RenderPage) {
+    return false;
+  }
+  mFPDF_RenderPage_Close = (FPDF_RenderPage_Close_Pfn)PR_FindFunctionSymbol(
+    mPRLibrary, "FPDF_RenderPage_Close");
+  if (!mFPDF_RenderPage_Close) {
+    return false;
+  }
+
+  InitLibrary();
+  mInitialized = true;
+  return true;
+}
+
+void
+PDFiumEngineShim::InitLibrary()
+{
+  mFPDF_InitLibrary();
+}
+
+void
+PDFiumEngineShim::DestroyLibrary()
+{
+  mFPDF_DestroyLibrary();
+}
+
+FPDF_DOCUMENT
+PDFiumEngineShim::LoadMemDocument(const void* aDataBuf,
+                                  int aSize,
+                                  FPDF_BYTESTRING aPassword)
+{
+  if (!mInitialized) {
+    if (!InitSymbolsAndLibrary()) {
+      return nullptr;
+    }
+  }
+
+  return mFPDF_LoadMemDocument(aDataBuf, aSize, aPassword);
+}
+
+void
+PDFiumEngineShim::CloseDocument(FPDF_DOCUMENT aDocument)
+{
+  MOZ_ASSERT(mInitialized);
+  mFPDF_CloseDocument(aDocument);
+  DestroyLibrary();
+  mInitialized = false;
+}
+
+int
+PDFiumEngineShim::GetPageCount(FPDF_DOCUMENT aDocument)
+{
+  MOZ_ASSERT(mInitialized);
+  return mFPDF_GetPageCount(aDocument);
+}
+
+int
+PDFiumEngineShim::GetPageSizeByIndex(FPDF_DOCUMENT aDocument,
+                                     int aPageIndex,
+                                     double* aWidth,
+                                     double* aHeight)
+{
+  MOZ_ASSERT(mInitialized);
+  return mFPDF_GetPageSizeByIndex(aDocument, aPageIndex, aWidth, aHeight);
+}
+
+FPDF_PAGE
+PDFiumEngineShim::LoadPage(FPDF_DOCUMENT aDocument, int aPageIndex)
+{
+  MOZ_ASSERT(mInitialized);
+  return mFPDF_LoadPage(aDocument, aPageIndex);
+}
+
+void
+PDFiumEngineShim::ClosePage(FPDF_PAGE aPage)
+{
+  MOZ_ASSERT(mInitialized);
+  mFPDF_ClosePage(aPage);
+}
+
+void
+PDFiumEngineShim::RenderPage(HDC aDC, FPDF_PAGE aPage,
+                             int aStartX, int aStartY,
+                             int aSizeX, int aSizeY,
+                             int aRotate, int aFlags)
+{
+  MOZ_ASSERT(mInitialized);
+  mFPDF_RenderPage(aDC, aPage, aStartX, aStartY,
+                   aSizeX, aSizeY, aRotate, aFlags);
+}
+
+void
+PDFiumEngineShim::RenderPage_Close(FPDF_PAGE aPage)
+{
+  MOZ_ASSERT(mInitialized);
+  mFPDF_RenderPage_Close(aPage);
+}
+
+} // namespace widget
+} // namespace mozilla
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/widget/windows/PDFiumEngineShim.h
@@ -0,0 +1,159 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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/. */
+
+#ifndef PDFIUMENGINESHIM_H
+#define PDFIUMENGINESHIM_H
+
+#include "prlink.h"
+
+/* include windows.h for the HDC definitions that we need. */
+#include <windows.h>
+
+namespace mozilla {
+namespace widget {
+/**
+ * Create Bug 1349139 to replace following defines with an appropriate include file.
+ */
+// Page rendering flags. They can be combined with bit-wise OR.
+//
+// Set if annotations are to be rendered.
+#define FPDF_ANNOT 0x01
+// Set if using text rendering optimized for LCD display.
+#define FPDF_LCD_TEXT 0x02
+// Don't use the native text output available on some platforms
+#define FPDF_NO_NATIVETEXT 0x04
+// Grayscale output.
+#define FPDF_GRAYSCALE 0x08
+// Set if you want to get some debug info.
+#define FPDF_DEBUG_INFO 0x80
+// Set if you don't want to catch exceptions.
+#define FPDF_NO_CATCH 0x100
+// Limit image cache size.
+#define FPDF_RENDER_LIMITEDIMAGECACHE 0x200
+// Always use halftone for image stretching.
+#define FPDF_RENDER_FORCEHALFTONE 0x400
+// Render for printing.
+#define FPDF_PRINTING 0x800
+// Set to disable anti-aliasing on text.
+#define FPDF_RENDER_NO_SMOOTHTEXT 0x1000
+// Set to disable anti-aliasing on images.
+#define FPDF_RENDER_NO_SMOOTHIMAGE 0x2000
+// Set to disable anti-aliasing on paths.
+#define FPDF_RENDER_NO_SMOOTHPATH 0x4000
+// Set whether to render in a reverse Byte order, this flag is only used when
+// rendering to a bitmap.
+#define FPDF_REVERSE_BYTE_ORDER 0x10
+
+// PDF types
+typedef void* FPDF_ACTION;
+typedef void* FPDF_BITMAP;
+typedef void* FPDF_BOOKMARK;
+typedef void* FPDF_CLIPPATH;
+typedef void* FPDF_DEST;
+typedef void* FPDF_DOCSCHHANDLE;
+typedef void* FPDF_DOCUMENT;
+typedef void* FPDF_FONT;
+typedef void* FPDF_HMODULE;
+typedef void* FPDF_LINK;
+typedef void* FPDF_MODULEMGR;
+typedef void* FPDF_PAGE;
+typedef void* FPDF_PAGELINK;
+typedef void* FPDF_PAGEOBJECT;  // Page object(text, path, etc)
+typedef void* FPDF_PAGERANGE;
+typedef void* FPDF_PATH;
+typedef void* FPDF_RECORDER;
+typedef void* FPDF_SCHHANDLE;
+typedef void* FPDF_STRUCTELEMENT;
+typedef void* FPDF_STRUCTTREE;
+typedef void* FPDF_TEXTPAGE;
+
+// Basic data types
+typedef int FPDF_BOOL;
+typedef int FPDF_ERROR;
+typedef unsigned long FPDF_DWORD;
+typedef float FS_FLOAT;
+
+typedef const char* FPDF_BYTESTRING;
+
+typedef void (*FPDF_InitLibrary_Pfn)();
+typedef void (*FPDF_DestroyLibrary_Pfn)();
+typedef FPDF_DOCUMENT (*FPDF_LoadMemDocument_Pfn)(const void* aDataBuf,
+                                                  int aSize,
+                                                  FPDF_BYTESTRING aPassword);
+typedef void(*FPDF_CloseDocument_Pfn)(FPDF_DOCUMENT aDocument);
+
+typedef int (*FPDF_GetPageCount_Pfn)(FPDF_DOCUMENT aDocument);
+typedef int (*FPDF_GetPageSizeByIndex_Pfn)(FPDF_DOCUMENT aDocument,
+                                           int aPageIndex,
+                                           double* aWidth,
+                                           double* aWeight);
+
+typedef FPDF_PAGE (*FPDF_LoadPage_Pfn)(FPDF_DOCUMENT aDocument, int aPageIndex);
+typedef void (*FPDF_ClosePage_Pfn)(FPDF_PAGE aPage);
+typedef void (*FPDF_RenderPage_Pfn)(HDC aDC,
+                                    FPDF_PAGE aPage,
+                                    int aStartX,
+                                    int aStartY,
+                                    int aSizeX,
+                                    int aSizeY,
+                                    int aRotate,
+                                    int aFlags);
+typedef void (*FPDF_RenderPage_Close_Pfn) (FPDF_PAGE aPage);
+
+
+/**
+ * This class exposes an interface to the PDFium library and
+ * takes care of loading and linking to the appropriate PDFium symbols.
+ */
+class PDFiumEngineShim
+{
+public:
+
+  explicit PDFiumEngineShim(PRLibrary* aLibrary);
+  ~PDFiumEngineShim();
+
+  FPDF_DOCUMENT LoadMemDocument(const void* aDataBuf,
+                                int aSize,
+                                FPDF_BYTESTRING aPassword);
+  void CloseDocument(FPDF_DOCUMENT aDocument);
+  int GetPageCount(FPDF_DOCUMENT aDocument);
+  int GetPageSizeByIndex(FPDF_DOCUMENT aDocument, int aPageIndex,
+                         double* aWidth, double* aHeight);
+
+  FPDF_PAGE LoadPage(FPDF_DOCUMENT aDocument, int aPageIndex);
+  void ClosePage(FPDF_PAGE aPage);
+  void RenderPage(HDC aDC, FPDF_PAGE aPage,
+                  int aStartX, int aStartY,
+                  int aSizeX, int aSizeY,
+                  int aRotate, int aFlags);
+
+  void RenderPage_Close(FPDF_PAGE aPage);
+
+private:
+
+  bool InitSymbolsAndLibrary();
+  void InitLibrary();
+  void DestroyLibrary();
+
+  PRLibrary*  mPRLibrary;
+  bool        mInitialized ;
+
+  FPDF_InitLibrary_Pfn        mFPDF_InitLibrary;
+  FPDF_DestroyLibrary_Pfn     mFPDF_DestroyLibrary;
+  FPDF_LoadMemDocument_Pfn    mFPDF_LoadMemDocument;
+  FPDF_CloseDocument_Pfn      mFPDF_CloseDocument;
+  FPDF_GetPageCount_Pfn       mFPDF_GetPageCount;
+  FPDF_GetPageSizeByIndex_Pfn mFPDF_GetPageSizeByIndex;
+  FPDF_LoadPage_Pfn           mFPDF_LoadPage;
+  FPDF_ClosePage_Pfn          mFPDF_ClosePage;
+  FPDF_RenderPage_Pfn         mFPDF_RenderPage;
+  FPDF_RenderPage_Close_Pfn   mFPDF_RenderPage_Close;
+
+};
+
+} // namespace widget
+} // namespace mozilla
+
+#endif /* PDFIUMENGINESHIM_H */
\ No newline at end of file
--- a/widget/windows/moz.build
+++ b/widget/windows/moz.build
@@ -87,16 +87,17 @@ if CONFIG['MOZ_CRASHREPORTER']:
         'LSPAnnotator.cpp',
     ]
 
 if CONFIG['NS_PRINTING']:
     UNIFIED_SOURCES += [
         'nsDeviceContextSpecWin.cpp',
         'nsPrintOptionsWin.cpp',
         'nsPrintSettingsWin.cpp',
+        'PDFiumEngineShim.cpp',
         'WindowsEMF.cpp',
     ]
 
 if CONFIG['NS_ENABLE_TSF']:
     SOURCES += [
         'TSFTextStore.cpp',
     ]