Merge to XFA: Better error from pdfium_test when page too big to render.
[pdfium.git] / samples / pdfium_test.cc
index fc4bbe4..52e0125 100644 (file)
 #include "../fpdfsdk/include/fpdftext.h"
 #include "../fpdfsdk/include/fpdfview.h"
 #include "../core/include/fxcrt/fx_system.h"
-#include "v8/include/v8.h"
 #include "image_diff_png.h"
+#include "v8/include/v8.h"
+#include "v8/include/libplatform/libplatform.h"
+
 
 #ifdef _WIN32
 #define snprintf _snprintf
@@ -260,8 +262,8 @@ void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
 }
 #endif
 
-int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING msg, FPDF_WIDESTRING,
-               int, int) {
+int ExampleAppAlert(IPDF_JSPLATFORM*, FPDF_WIDESTRING msg, FPDF_WIDESTRING,
+                    int, int) {
   // Deal with differences between UTF16LE and wchar_t on this platform.
   size_t characters = 0;
   while (msg[characters]) {
@@ -278,7 +280,11 @@ int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING msg, FPDF_WIDESTRING,
   return 0;
 }
 
-void Unsupported_Handler(UNSUPPORT_INFO*, int type) {
+void ExampleDocGotoPage(IPDF_JSPLATFORM*, int pageNumber) {
+  printf("Goto Page: %d\n", pageNumber);
+}
+
+void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) {
   std::string feature = "Unknown";
   switch (type) {
     case FPDF_UNSP_DOC_XFAFORM:
@@ -418,12 +424,13 @@ void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
 
 void RenderPdf(const std::string& name, const char* pBuf, size_t len,
                const Options& options) {
-  printf("Rendering PDF file %s.\n", name.c_str());
+  fprintf(stderr, "Rendering PDF file %s.\n", name.c_str());
 
   IPDF_JSPLATFORM platform_callbacks;
   memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
   platform_callbacks.version = 1;
-  platform_callbacks.app_alert = Form_Alert;
+  platform_callbacks.app_alert = ExampleAppAlert;
+  platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
 
   FPDF_FORMFILLINFO form_callbacks;
   memset(&form_callbacks, '\0', sizeof(form_callbacks));
@@ -454,19 +461,28 @@ void RenderPdf(const std::string& name, const char* pBuf, size_t len,
   (void) FPDFAvail_IsDocAvail(pdf_avail, &hints);
 
   if (!FPDFAvail_IsLinearized(pdf_avail)) {
-    printf("Non-linearized path...\n");
+    fprintf(stderr, "Non-linearized path...\n");
     doc = FPDF_LoadCustomDocument(&file_access, NULL);
   } else {
-    printf("Linearized path...\n");
+    fprintf(stderr, "Linearized path...\n");
     doc = FPDFAvail_GetDocument(pdf_avail, NULL);
   }
 
+  if (!doc)
+  {
+    fprintf(stderr, "Load pdf docs unsuccessful.\n");
+    return;
+  }
+
   (void) FPDF_GetDocPermissions(doc);
   (void) FPDFAvail_IsFormAvail(pdf_avail, &hints);
 
   FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
-  if (!FPDF_LoadXFA(doc)) {
-    printf("LoadXFA unsuccessful, continuing anyway.\n");
+  int docType = DOCTYPE_PDF;
+  if (FPDF_HasXFAField(doc, docType))
+  {
+      if (docType != DOCTYPE_PDF && !FPDF_LoadXFA(doc))
+          fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
   }
   FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
   FPDF_SetFormFieldHighlightAlpha(form, 100);
@@ -494,16 +510,20 @@ void RenderPdf(const std::string& name, const char* pBuf, size_t len,
     FORM_OnAfterLoadPage(page, form);
     FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
 
-    int width = static_cast<int>(FPDF_GetPageWidth(page));
-    int height = static_cast<int>(FPDF_GetPageHeight(page));
+    double scale = 1.0;
     if (!options.scale_factor_as_string.empty()) {
-      double scale = 1.0;
       std::stringstream(options.scale_factor_as_string) >> scale;
-      width *= scale;
-      height *= scale;
     }
+    int width = static_cast<int>(FPDF_GetPageWidth(page) * scale);
+    int height = static_cast<int>(FPDF_GetPageHeight(page) * scale);
 
     FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
+    if (!bitmap) {
+      fprintf(stderr, "Page was too large to be rendered.\n");
+      bad_pages++;
+      continue;
+    }
+
     FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
     FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
     rendered_pages ++;
@@ -548,28 +568,34 @@ void RenderPdf(const std::string& name, const char* pBuf, size_t len,
   FPDFDOC_ExitFormFillEnvironment(form);
   FPDFAvail_Destroy(pdf_avail);
 
-  printf("Loaded, parsed and rendered %" PRIuS " pages.\n", rendered_pages);
-  printf("Skipped %" PRIuS " bad pages.\n", bad_pages);
+  fprintf(stderr, "Rendered %" PRIuS " pages.\n", rendered_pages);
+  fprintf(stderr, "Skipped %" PRIuS " bad pages.\n", bad_pages);
 }
 
+static const char usage_string[] =
+    "Usage: pdfium_test [OPTION] [FILE]...\n"
+    "  --bin-dir=<path> - override path to v8 external data\n"
+    "  --scale=<number> - scale output size by number (e.g. 0.5)\n"
+#ifdef _WIN32
+    "  --bmp - write page images <pdf-name>.<page-number>.bmp\n"
+    "  --emf - write page meta files <pdf-name>.<page-number>.emf\n"
+#endif
+    "  --png - write page images <pdf-name>.<page-number>.png\n"
+    "  --ppm - write page images <pdf-name>.<page-number>.ppm\n";
+
 int main(int argc, const char* argv[]) {
   std::vector<std::string> args(argv, argv + argc);
   Options options;
   std::list<std::string> files;
   if (!ParseCommandLine(args, &options, &files)) {
-    printf("Usage: pdfium_test [OPTION] [FILE]...\n");
-    printf("--bin-dir=<path> - override path to v8 external data\n");
-    printf("--scale=<number> - scale output size by number (e.g. 0.5)\n");
-    printf("--png - write page images <pdf-name>.<page-number>.png\n");
-    printf("--ppm - write page images <pdf-name>.<page-number>.ppm\n");
-#ifdef _WIN32
-    printf("--bmp - write page images <pdf-name>.<page-number>.bmp\n");
-    printf("--emf - write page meta files <pdf-name>.<page-number>.emf\n");
-#endif
+    fprintf(stderr, "%s", usage_string);
     return 1;
   }
 
   v8::V8::InitializeICU();
+  v8::Platform* platform = v8::platform::CreateDefaultPlatform();
+  v8::V8::InitializePlatform(platform);
+  v8::V8::Initialize();
 
 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
   v8::StartupData natives;
@@ -587,7 +613,7 @@ int main(int argc, const char* argv[]) {
   UNSUPPORT_INFO unsuppored_info;
   memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
   unsuppored_info.version = 1;
-  unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler;
+  unsuppored_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
 
   FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
 
@@ -603,6 +629,8 @@ int main(int argc, const char* argv[]) {
   }
 
   FPDF_DestroyLibrary();
+  v8::V8::ShutdownPlatform();
+  delete platform;
 
   return 0;
 }