Merge to XFA: Better error from pdfium_test when page too big to render.
[pdfium.git] / samples / pdfium_test.cc
index 1051594..52e0125 100644 (file)
@@ -468,12 +468,21 @@ void RenderPdf(const std::string& name, const char* pBuf, size_t len,
     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)) {
-    fprintf(stderr, "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);
@@ -501,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 ++;