#include <utility>
#include <vector>
-#include "../fpdfsdk/include/fpdf_dataavail.h"
-#include "../fpdfsdk/include/fpdf_ext.h"
-#include "../fpdfsdk/include/fpdfformfill.h"
-#include "../fpdfsdk/include/fpdftext.h"
-#include "../fpdfsdk/include/fpdfview.h"
-#include "../core/include/fxcrt/fx_system.h"
-#include "v8/include/v8.h"
+#include "../public/fpdf_dataavail.h"
+#include "../public/fpdf_ext.h"
+#include "../public/fpdf_formfill.h"
+#include "../public/fpdf_text.h"
+#include "../public/fpdfview.h"
+#include "image_diff_png.h"
#include "v8/include/libplatform/libplatform.h"
+#include "v8/include/v8.h"
#ifdef _WIN32
#define snprintf _snprintf
enum OutputFormat {
OUTPUT_NONE,
OUTPUT_PPM,
+ OUTPUT_PNG,
#ifdef _WIN32
OUTPUT_BMP,
OUTPUT_EMF,
}
#endif // V8_USE_EXTERNAL_STARTUP_DATA
+static bool CheckDimensions(int stride, int width, int height) {
+ if (stride < 0 || width < 0 || height < 0)
+ return false;
+ if (height > 0 && width > INT_MAX / height)
+ return false;
+ return true;
+}
+
static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
int stride, int width, int height) {
const char* buffer = reinterpret_cast<const char*>(buffer_void);
- if (stride < 0 || width < 0 || height < 0)
- return;
- if (height > 0 && width > INT_MAX / height)
+ if (!CheckDimensions(stride, width, height))
return;
+
int out_len = width * height;
if (out_len > INT_MAX / 3)
return;
fclose(fp);
}
+static void WritePng(const char* pdf_name, int num, const void* buffer_void,
+ int stride, int width, int height) {
+ if (!CheckDimensions(stride, width, height))
+ return;
+
+ std::vector<unsigned char> png_encoding;
+ const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void);
+ if (!image_diff_png::EncodeBGRAPNG(
+ buffer, width, height, stride, false, &png_encoding)) {
+ fprintf(stderr, "Failed to convert bitmap to PNG\n");
+ return;
+ }
+
+ char filename[256];
+ int chars_formatted = snprintf(
+ filename, sizeof(filename), "%s.%d.png", pdf_name, num);
+ if (chars_formatted < 0 ||
+ static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
+ fprintf(stderr, "Filname %s is too long\n", filename);
+ return;
+ }
+
+ FILE* fp = fopen(filename, "wb");
+ if (!fp) {
+ fprintf(stderr, "Failed to open %s for output\n", filename);
+ return;
+ }
+
+ size_t bytes_written = fwrite(
+ &png_encoding.front(), 1, png_encoding.size(), fp);
+ if (bytes_written != png_encoding.size())
+ fprintf(stderr, "Failed to write to %s\n", filename);
+
+ (void) fclose(fp);
+}
+
#ifdef _WIN32
static void WriteBmp(const char* pdf_name, int num, const void* buffer,
int stride, int width, int height) {
snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
HDC dc = CreateEnhMetaFileA(NULL, filename, NULL, NULL);
-
- HRGN rgn = CreateRectRgn(0, 0, width, height);
- SelectClipRgn(dc, rgn);
+
+ HRGN rgn = CreateRectRgn(0, 0, width, height);
+ SelectClipRgn(dc, rgn);
DeleteObject(rgn);
SelectObject(dc, GetStockObject(NULL_PEN));
}
#endif
-int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) {
- printf("Form_Alert called.\n");
+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]) {
+ ++characters;
+ }
+ wchar_t* platform_string =
+ (wchar_t*)malloc((characters + 1) * sizeof(wchar_t));
+ for (size_t i = 0; i < characters + 1; ++i) {
+ unsigned char* ptr = (unsigned char*)&msg[i];
+ platform_string[i] = ptr[0] + 256 * ptr[1];
+ }
+ printf("Alert: %ls\n", platform_string);
+ free(platform_string);
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:
return false;
}
options->output_format = OUTPUT_PPM;
+ } else if (cur_arg == "--png") {
+ if (options->output_format != OUTPUT_NONE) {
+ fprintf(stderr, "Duplicate or conflicting --png argument\n");
+ return false;
+ }
+ options->output_format = OUTPUT_PNG;
}
#ifdef _WIN32
else if (cur_arg == "--emf") {
return 1;
}
-bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
+FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
return true;
}
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));
(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);
}
FORM_DoDocumentJSAction(form);
FORM_DoDocumentOpenAction(form);
- size_t rendered_pages = 0;
- size_t bad_pages = 0;
+ int rendered_pages = 0;
+ int bad_pages = 0;
for (int i = 0; i < page_count; ++i) {
FPDF_PAGE page = FPDF_LoadPage(doc, i);
if (!page) {
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 ++;
WriteEmf(page, name.c_str(), i);
break;
#endif
+ case OUTPUT_PNG:
+ WritePng(name.c_str(), i, buffer, stride, width, height);
+ break;
+
case OUTPUT_PPM:
WritePpm(name.c_str(), i, buffer, stride, width, height);
break;
+
default:
break;
}
}
FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
- FPDFDOC_ExitFormFillEnvironment(form);
FPDF_CloseDocument(doc);
+ 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 %d pages.\n", rendered_pages);
+ fprintf(stderr, "Skipped %d 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("--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;
}
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);