1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
15 #include "../fpdfsdk/include/fpdf_dataavail.h"
16 #include "../fpdfsdk/include/fpdf_ext.h"
17 #include "../fpdfsdk/include/fpdfformfill.h"
18 #include "../fpdfsdk/include/fpdftext.h"
19 #include "../fpdfsdk/include/fpdfview.h"
20 #include "../core/include/fxcrt/fx_system.h"
21 #include "v8/include/v8.h"
24 #define snprintf _snprintf
25 #define PATH_SEPARATOR '\\'
27 #define PATH_SEPARATOR '/'
40 Options() : output_format(OUTPUT_NONE) { }
42 OutputFormat output_format;
44 std::string bin_directory;
47 // Reads the entire contents of a file into a newly malloc'd buffer.
48 static char* GetFileContents(const char* filename, size_t* retlen) {
49 FILE* file = fopen(filename, "rb");
51 fprintf(stderr, "Failed to open: %s\n", filename);
54 (void) fseek(file, 0, SEEK_END);
55 size_t file_length = ftell(file);
59 (void) fseek(file, 0, SEEK_SET);
60 char* buffer = (char*) malloc(file_length);
64 size_t bytes_read = fread(buffer, 1, file_length, file);
66 if (bytes_read != file_length) {
67 fprintf(stderr, "Failed to read: %s\n", filename);
75 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
76 // Returns the full path for an external V8 data file based on either
77 // the currect exectuable path or an explicit override.
78 static std::string GetFullPathForSnapshotFile(const Options& options,
79 const std::string& filename) {
81 if (!options.bin_directory.empty()) {
82 result = options.bin_directory;
83 if (*options.bin_directory.rbegin() != PATH_SEPARATOR) {
84 result += PATH_SEPARATOR;
86 } else if (!options.exe_path.empty()) {
87 size_t last_separator = options.exe_path.rfind(PATH_SEPARATOR);
88 if (last_separator != std::string::npos) {
89 result = options.exe_path.substr(0, last_separator + 1);
96 // Reads an extenal V8 data file from the |options|-indicated location,
97 // returing true on success and false on error.
98 static bool GetExternalData(const Options& options,
99 const std::string& bin_filename,
100 v8::StartupData* result_data) {
101 std::string full_path = GetFullPathForSnapshotFile(options, bin_filename);
102 size_t data_length = 0;
103 char* data_buffer = GetFileContents(full_path.c_str(), &data_length);
107 result_data->data = const_cast<const char*>(data_buffer);
108 result_data->raw_size = data_length;
111 #endif // V8_USE_EXTERNAL_STARTUP_DATA
113 static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
114 int stride, int width, int height) {
115 const char* buffer = reinterpret_cast<const char*>(buffer_void);
117 if (stride < 0 || width < 0 || height < 0)
119 if (height > 0 && width > INT_MAX / height)
121 int out_len = width * height;
122 if (out_len > INT_MAX / 3)
127 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
128 FILE* fp = fopen(filename, "wb");
131 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
132 // Source data is B, G, R, unused.
133 // Dest data is R, G, B.
134 char* result = new char[out_len];
136 for (int h = 0; h < height; ++h) {
137 const char* src_line = buffer + (stride * h);
138 char* dest_line = result + (width * h * 3);
139 for (int w = 0; w < width; ++w) {
141 dest_line[w * 3] = src_line[(w * 4) + 2];
143 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
145 dest_line[(w * 3) + 2] = src_line[w * 4];
148 fwrite(result, out_len, 1, fp);
155 static void WriteBmp(const char* pdf_name, int num, const void* buffer,
156 int stride, int width, int height) {
157 if (stride < 0 || width < 0 || height < 0)
159 if (height > 0 && width > INT_MAX / height)
161 int out_len = stride * height;
162 if (out_len > INT_MAX / 3)
166 snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
167 FILE* fp = fopen(filename, "wb");
171 BITMAPINFO bmi = {0};
172 bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
173 bmi.bmiHeader.biWidth = width;
174 bmi.bmiHeader.biHeight = -height; // top-down image
175 bmi.bmiHeader.biPlanes = 1;
176 bmi.bmiHeader.biBitCount = 32;
177 bmi.bmiHeader.biCompression = BI_RGB;
178 bmi.bmiHeader.biSizeImage = 0;
180 BITMAPFILEHEADER file_header = {0};
181 file_header.bfType = 0x4d42;
182 file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
183 file_header.bfOffBits = file_header.bfSize - out_len;
185 fwrite(&file_header, sizeof(file_header), 1, fp);
186 fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
187 fwrite(buffer, out_len, 1, fp);
191 void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
192 int width = static_cast<int>(FPDF_GetPageWidth(page));
193 int height = static_cast<int>(FPDF_GetPageHeight(page));
196 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
198 HDC dc = CreateEnhMetaFileA(NULL, filename, NULL, NULL);
200 HRGN rgn = CreateRectRgn(0, 0, width, height);
201 SelectClipRgn(dc, rgn);
204 SelectObject(dc, GetStockObject(NULL_PEN));
205 SelectObject(dc, GetStockObject(WHITE_BRUSH));
206 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
207 Rectangle(dc, 0, 0, width + 1, height + 1);
209 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
210 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
212 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
216 int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) {
217 printf("Form_Alert called.\n");
221 void Unsupported_Handler(UNSUPPORT_INFO*, int type) {
222 std::string feature = "Unknown";
224 case FPDF_UNSP_DOC_XFAFORM:
227 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
228 feature = "Portfolios_Packages";
230 case FPDF_UNSP_DOC_ATTACHMENT:
231 case FPDF_UNSP_ANNOT_ATTACHMENT:
232 feature = "Attachment";
234 case FPDF_UNSP_DOC_SECURITY:
235 feature = "Rights_Management";
237 case FPDF_UNSP_DOC_SHAREDREVIEW:
238 feature = "Shared_Review";
240 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
241 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
242 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
243 feature = "Shared_Form";
245 case FPDF_UNSP_ANNOT_3DANNOT:
248 case FPDF_UNSP_ANNOT_MOVIE:
251 case FPDF_UNSP_ANNOT_SOUND:
254 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
255 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
258 case FPDF_UNSP_ANNOT_SIG:
259 feature = "Digital_Signature";
262 printf("Unsupported feature: %s.\n", feature.c_str());
265 bool ParseCommandLine(const std::vector<std::string>& args,
266 Options* options, std::list<std::string>* files) {
270 options->exe_path = args[0];
272 for (; cur_idx < args.size(); ++cur_idx) {
273 const std::string& cur_arg = args[cur_idx];
274 if (cur_arg == "--ppm") {
275 if (options->output_format != OUTPUT_NONE) {
276 fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
279 options->output_format = OUTPUT_PPM;
282 else if (cur_arg == "--emf") {
283 if (options->output_format != OUTPUT_NONE) {
284 fprintf(stderr, "Duplicate or conflicting --emf argument\n");
287 options->output_format = OUTPUT_EMF;
289 else if (cur_arg == "--bmp") {
290 if (options->output_format != OUTPUT_NONE) {
291 fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
294 options->output_format = OUTPUT_BMP;
297 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
298 else if (cur_arg.size() > 10 && cur_arg.compare(0, 10, "--bin-dir=") == 0) {
299 if (!options->bin_directory.empty()) {
300 fprintf(stderr, "Duplicate --bin-dir argument\n");
303 options->bin_directory = cur_arg.substr(10);
305 #endif // V8_USE_EXTERNAL_STARTUP_DATA
309 if (cur_idx >= args.size()) {
310 fprintf(stderr, "No input files.\n");
313 for (size_t i = cur_idx; i < args.size(); i++) {
314 files->push_back(args[i]);
321 TestLoader(const char* pBuf, size_t len);
327 TestLoader::TestLoader(const char* pBuf, size_t len)
328 : m_pBuf(pBuf), m_Len(len) {
331 int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
332 unsigned long size) {
333 TestLoader* pLoader = (TestLoader*) param;
334 if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
335 memcpy(pBuf, pLoader->m_pBuf + pos, size);
339 bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
343 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
346 void RenderPdf(const std::string& name, const char* pBuf, size_t len,
347 OutputFormat format) {
348 printf("Rendering PDF file %s.\n", name.c_str());
350 IPDF_JSPLATFORM platform_callbacks;
351 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
352 platform_callbacks.version = 1;
353 platform_callbacks.app_alert = Form_Alert;
355 FPDF_FORMFILLINFO form_callbacks;
356 memset(&form_callbacks, '\0', sizeof(form_callbacks));
357 form_callbacks.version = 1;
358 form_callbacks.m_pJsPlatform = &platform_callbacks;
360 TestLoader loader(pBuf, len);
362 FPDF_FILEACCESS file_access;
363 memset(&file_access, '\0', sizeof(file_access));
364 file_access.m_FileLen = static_cast<unsigned long>(len);
365 file_access.m_GetBlock = Get_Block;
366 file_access.m_Param = &loader;
368 FX_FILEAVAIL file_avail;
369 memset(&file_avail, '\0', sizeof(file_avail));
370 file_avail.version = 1;
371 file_avail.IsDataAvail = Is_Data_Avail;
373 FX_DOWNLOADHINTS hints;
374 memset(&hints, '\0', sizeof(hints));
376 hints.AddSegment = Add_Segment;
379 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
381 (void) FPDFAvail_IsDocAvail(pdf_avail, &hints);
383 if (!FPDFAvail_IsLinearized(pdf_avail)) {
384 printf("Non-linearized path...\n");
385 doc = FPDF_LoadCustomDocument(&file_access, NULL);
387 printf("Linearized path...\n");
388 doc = FPDFAvail_GetDocument(pdf_avail, NULL);
391 (void) FPDF_GetDocPermissions(doc);
392 (void) FPDFAvail_IsFormAvail(pdf_avail, &hints);
394 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnvironment(doc, &form_callbacks);
395 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
396 FPDF_SetFormFieldHighlightAlpha(form, 100);
398 int first_page = FPDFAvail_GetFirstPageNum(doc);
399 (void) FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);
401 int page_count = FPDF_GetPageCount(doc);
402 for (int i = 0; i < page_count; ++i) {
403 (void) FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
406 FORM_DoDocumentJSAction(form);
407 FORM_DoDocumentOpenAction(form);
409 size_t rendered_pages = 0;
410 size_t bad_pages = 0;
411 for (int i = 0; i < page_count; ++i) {
412 FPDF_PAGE page = FPDF_LoadPage(doc, i);
417 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
418 FORM_OnAfterLoadPage(page, form);
419 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
421 int width = static_cast<int>(FPDF_GetPageWidth(page));
422 int height = static_cast<int>(FPDF_GetPageHeight(page));
423 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
424 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
426 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
429 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
430 int stride = FPDFBitmap_GetStride(bitmap);
432 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
437 WriteBmp(name.c_str(), i, buffer, stride, width, height);
441 WriteEmf(page, name.c_str(), i);
445 WritePpm(name.c_str(), i, buffer, stride, width, height);
451 FPDFBitmap_Destroy(bitmap);
453 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
454 FORM_OnBeforeClosePage(page, form);
455 FPDFText_ClosePage(text_page);
456 FPDF_ClosePage(page);
459 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
460 FPDFDOC_ExitFormFillEnvironment(form);
461 FPDF_CloseDocument(doc);
462 FPDFAvail_Destroy(pdf_avail);
464 printf("Loaded, parsed and rendered %" PRIuS " pages.\n", rendered_pages);
465 printf("Skipped %" PRIuS " bad pages.\n", bad_pages);
468 int main(int argc, const char* argv[]) {
469 std::vector<std::string> args(argv, argv + argc);
471 std::list<std::string> files;
472 if (!ParseCommandLine(args, &options, &files)) {
473 printf("Usage: pdfium_test [OPTION] [FILE]...\n");
474 printf("--bin-dir=<path> - override path to v8 external data\n");
475 printf("--ppm - write page images <pdf-name>.<page-number>.ppm\n");
477 printf("--bmp - write page images <pdf-name>.<page-number>.bmp\n");
478 printf("--emf - write page meta files <pdf-name>.<page-number>.emf\n");
483 v8::V8::InitializeICU();
485 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
486 v8::StartupData natives;
487 v8::StartupData snapshot;
488 if (!GetExternalData(options, "natives_blob.bin", &natives) ||
489 !GetExternalData(options, "snapshot_blob.bin", &snapshot)) {
492 v8::V8::SetNativesDataBlob(&natives);
493 v8::V8::SetSnapshotDataBlob(&snapshot);
494 #endif // V8_USE_EXTERNAL_STARTUP_DATA
498 UNSUPPORT_INFO unsuppored_info;
499 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
500 unsuppored_info.version = 1;
501 unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler;
503 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
505 while (!files.empty()) {
506 std::string filename = files.front();
508 size_t file_length = 0;
509 char* file_contents = GetFileContents(filename.c_str(), &file_length);
512 RenderPdf(filename, file_contents, file_length, options.output_format);
516 FPDF_DestroyLibrary();