blob: 7436e9d86cccbd00e834b2b9f79deae908da832a [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
4
5#include <limits.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9
10#include <list>
11#include <string>
12#include <utility>
13
John Abd-El-Malek197fd8d2014-05-23 19:27:48 -070014#include "../fpdfsdk/include/fpdf_dataavail.h"
15#include "../fpdfsdk/include/fpdf_ext.h"
16#include "../fpdfsdk/include/fpdfformfill.h"
17#include "../fpdfsdk/include/fpdftext.h"
18#include "../fpdfsdk/include/fpdfview.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070019#include "v8/include/v8.h"
20
21#ifdef _WIN32
22 #define snprintf _snprintf
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023#endif
24
Vitaly Buka9e0177a2014-07-22 18:15:42 -070025enum OutputFormat {
26 OUTPUT_NONE,
27 OUTPUT_PPM,
28#ifdef _WIN32
29 OUTPUT_BMP,
30 OUTPUT_EMF,
31#endif
32};
33
34static void WritePpm(const char* pdf_name, int num, const void* buffer_void,
35 int stride, int width, int height) {
36 const char* buffer = reinterpret_cast<const char*>(buffer_void);
37
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070038 if (stride < 0 || width < 0 || height < 0)
39 return;
40 if (height > 0 && width > INT_MAX / height)
41 return;
42 int out_len = width * height;
43 if (out_len > INT_MAX / 3)
44 return;
45 out_len *= 3;
46
47 char filename[256];
48 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
John Abd-El-Maleka548d302014-06-26 10:18:11 -070049 FILE* fp = fopen(filename, "wb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050 if (!fp)
51 return;
52 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
53 // Source data is B, G, R, unused.
54 // Dest data is R, G, B.
55 char* result = new char[out_len];
56 if (result) {
57 for (int h = 0; h < height; ++h) {
58 const char* src_line = buffer + (stride * h);
59 char* dest_line = result + (width * h * 3);
60 for (int w = 0; w < width; ++w) {
61 // R
62 dest_line[w * 3] = src_line[(w * 4) + 2];
63 // G
64 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
65 // B
66 dest_line[(w * 3) + 2] = src_line[w * 4];
67 }
68 }
69 fwrite(result, out_len, 1, fp);
70 delete [] result;
71 }
72 fclose(fp);
73}
74
Vitaly Buka9e0177a2014-07-22 18:15:42 -070075#ifdef _WIN32
76static void WriteBmp(const char* pdf_name, int num, const void* buffer,
77 int stride, int width, int height) {
78 if (stride < 0 || width < 0 || height < 0)
79 return;
80 if (height > 0 && width > INT_MAX / height)
81 return;
82 int out_len = stride * height;
83 if (out_len > INT_MAX / 3)
84 return;
85
86 char filename[256];
87 snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
88 FILE* fp = fopen(filename, "wb");
89 if (!fp)
90 return;
91
92 BITMAPINFO bmi = {0};
93 bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
94 bmi.bmiHeader.biWidth = width;
95 bmi.bmiHeader.biHeight = -height; // top-down image
96 bmi.bmiHeader.biPlanes = 1;
97 bmi.bmiHeader.biBitCount = 32;
98 bmi.bmiHeader.biCompression = BI_RGB;
99 bmi.bmiHeader.biSizeImage = 0;
100
101 BITMAPFILEHEADER file_header = {0};
102 file_header.bfType = 0x4d42;
103 file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
104 file_header.bfOffBits = file_header.bfSize - out_len;
105
106 fwrite(&file_header, sizeof(file_header), 1, fp);
107 fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
108 fwrite(buffer, out_len, 1, fp);
109 fclose(fp);
110}
111
112void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
113 int width = static_cast<int>(FPDF_GetPageWidth(page));
114 int height = static_cast<int>(FPDF_GetPageHeight(page));
115
116 char filename[256];
117 snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
118
119 HDC dc = CreateEnhMetaFileA(NULL, filename, NULL, NULL);
120
121 HRGN rgn = CreateRectRgn(0, 0, width, height);
122 SelectClipRgn(dc, rgn);
123 DeleteObject(rgn);
124
125 SelectObject(dc, GetStockObject(NULL_PEN));
126 SelectObject(dc, GetStockObject(WHITE_BRUSH));
127 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
128 Rectangle(dc, 0, 0, width + 1, height + 1);
129
130 FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
131 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
132
133 DeleteEnhMetaFile(CloseEnhMetaFile(dc));
134}
135#endif
136
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) {
138 printf("Form_Alert called.\n");
139 return 0;
140}
141
142void Unsupported_Handler(UNSUPPORT_INFO*, int type) {
143 std::string feature = "Unknown";
144 switch (type) {
145 case FPDF_UNSP_DOC_XFAFORM:
146 feature = "XFA";
147 break;
148 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
149 feature = "Portfolios_Packages";
150 break;
151 case FPDF_UNSP_DOC_ATTACHMENT:
152 case FPDF_UNSP_ANNOT_ATTACHMENT:
153 feature = "Attachment";
154 break;
155 case FPDF_UNSP_DOC_SECURITY:
156 feature = "Rights_Management";
157 break;
158 case FPDF_UNSP_DOC_SHAREDREVIEW:
159 feature = "Shared_Review";
160 break;
161 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
162 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
163 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
164 feature = "Shared_Form";
165 break;
166 case FPDF_UNSP_ANNOT_3DANNOT:
167 feature = "3D";
168 break;
169 case FPDF_UNSP_ANNOT_MOVIE:
170 feature = "Movie";
171 break;
172 case FPDF_UNSP_ANNOT_SOUND:
173 feature = "Sound";
174 break;
175 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
176 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
177 feature = "Screen";
178 break;
179 case FPDF_UNSP_ANNOT_SIG:
180 feature = "Digital_Signature";
181 break;
182 }
183 printf("Unsupported feature: %s.\n", feature.c_str());
184}
185
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700186bool ParseCommandLine(int argc, const char* argv[], OutputFormat* output_format,
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700187 std::list<const char*>* files) {
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700188 *output_format = OUTPUT_NONE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700189 files->clear();
190
191 int cur_arg = 1;
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700192 for (; cur_arg < argc; ++cur_arg) {
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700193 if (strcmp(argv[cur_arg], "--ppm") == 0)
194 *output_format = OUTPUT_PPM;
195#ifdef _WIN32
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700196 else if (strcmp(argv[cur_arg], "--emf") == 0)
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700197 *output_format = OUTPUT_EMF;
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700198 else if (strcmp(argv[cur_arg], "--bmp") == 0)
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700199 *output_format = OUTPUT_BMP;
200#endif
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700201 else
202 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700203 }
204
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700205 if (cur_arg > 2) // Multiple options.
206 return false;
207
208 if (cur_arg >= argc) // No input files.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700209 return false;
210
211 for (int i = cur_arg; i < argc; i++)
212 files->push_back(argv[i]);
213
214 return true;
215}
216
217class TestLoader {
218 public:
219 TestLoader(const char* pBuf, size_t len);
220
221 const char* m_pBuf;
222 size_t m_Len;
223};
224
225TestLoader::TestLoader(const char* pBuf, size_t len)
226 : m_pBuf(pBuf), m_Len(len) {
227}
228
229int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
230 unsigned long size) {
231 TestLoader* pLoader = (TestLoader*) param;
232 if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
233 memcpy(pBuf, pLoader->m_pBuf + pos, size);
234 return 1;
235}
236
237bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
238 return true;
239}
240
241void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
242}
243
244void RenderPdf(const char* name, const char* pBuf, size_t len,
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700245 OutputFormat format) {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700246 printf("Rendering PDF file %s.\n", name);
247
248 IPDF_JSPLATFORM platform_callbacks;
249 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
250 platform_callbacks.version = 1;
251 platform_callbacks.app_alert = Form_Alert;
252
253 FPDF_FORMFILLINFO form_callbacks;
254 memset(&form_callbacks, '\0', sizeof(form_callbacks));
255 form_callbacks.version = 1;
256 form_callbacks.m_pJsPlatform = &platform_callbacks;
257
258 TestLoader loader(pBuf, len);
259
260 FPDF_FILEACCESS file_access;
261 memset(&file_access, '\0', sizeof(file_access));
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700262 file_access.m_FileLen = static_cast<unsigned long>(len);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700263 file_access.m_GetBlock = Get_Block;
264 file_access.m_Param = &loader;
265
266 FX_FILEAVAIL file_avail;
267 memset(&file_avail, '\0', sizeof(file_avail));
268 file_avail.version = 1;
269 file_avail.IsDataAvail = Is_Data_Avail;
270
271 FX_DOWNLOADHINTS hints;
272 memset(&hints, '\0', sizeof(hints));
273 hints.version = 1;
274 hints.AddSegment = Add_Segment;
275
276 FPDF_DOCUMENT doc;
277 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
278
279 (void) FPDFAvail_IsDocAvail(pdf_avail, &hints);
280
281 if (!FPDFAvail_IsLinearized(pdf_avail)) {
282 printf("Non-linearized path...\n");
283 doc = FPDF_LoadCustomDocument(&file_access, NULL);
284 } else {
285 printf("Linearized path...\n");
286 doc = FPDFAvail_GetDocument(pdf_avail, NULL);
287 }
288
289 (void) FPDF_GetDocPermissions(doc);
290 (void) FPDFAvail_IsFormAvail(pdf_avail, &hints);
291
292 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnviroument(doc, &form_callbacks);
293 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
294 FPDF_SetFormFieldHighlightAlpha(form, 100);
295
296 int first_page = FPDFAvail_GetFirstPageNum(doc);
297 (void) FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);
298
299 int page_count = FPDF_GetPageCount(doc);
300 for (int i = 0; i < page_count; ++i) {
301 (void) FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
302 }
303
304 FORM_DoDocumentJSAction(form);
305 FORM_DoDocumentOpenAction(form);
306
Jun Fangaeacba42014-08-22 17:04:29 -0700307 size_t rendered_pages = 0;
308 size_t bad_pages = 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700309 for (int i = 0; i < page_count; ++i) {
310 FPDF_PAGE page = FPDF_LoadPage(doc, i);
Jun Fangaeacba42014-08-22 17:04:29 -0700311 if (!page) {
312 bad_pages ++;
313 continue;
314 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700315 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
316 FORM_OnAfterLoadPage(page, form);
317 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
318
319 int width = static_cast<int>(FPDF_GetPageWidth(page));
320 int height = static_cast<int>(FPDF_GetPageHeight(page));
321 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
Lei Zhang532a6a72014-07-09 11:47:15 -0700322 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700323
324 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
Jun Fangaeacba42014-08-22 17:04:29 -0700325 rendered_pages ++;
326
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700327 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700328 int stride = FPDFBitmap_GetStride(bitmap);
329 const char* buffer =
330 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap));
331
332 switch (format) {
333#ifdef _WIN32
334 case OUTPUT_BMP:
335 WriteBmp(name, i, buffer, stride, width, height);
336 break;
337
338 case OUTPUT_EMF:
339 WriteEmf(page, name, i);
340 break;
341#endif
342 case OUTPUT_PPM:
343 WritePpm(name, i, buffer, stride, width, height);
344 break;
345 default:
346 break;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700347 }
348
349 FPDFBitmap_Destroy(bitmap);
350
351 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
352 FORM_OnBeforeClosePage(page, form);
353 FPDFText_ClosePage(text_page);
354 FPDF_ClosePage(page);
355 }
356
357 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
358 FPDFDOC_ExitFormFillEnviroument(form);
359 FPDF_CloseDocument(doc);
360 FPDFAvail_Destroy(pdf_avail);
361
Jun Fangaeacba42014-08-22 17:04:29 -0700362 printf("Loaded, parsed and rendered %d pages.\n", rendered_pages);
363 printf("Skipped %d bad pages.\n", bad_pages);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700364}
365
366int main(int argc, const char* argv[]) {
367 v8::V8::InitializeICU();
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700368 OutputFormat format = OUTPUT_NONE;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700369 std::list<const char*> files;
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700370 if (!ParseCommandLine(argc, argv, &format, &files)) {
Vitaly Buka8f2c3dc2014-08-20 10:32:36 -0700371 printf("Usage: pdfium_test [OPTION] [FILE]...\n");
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700372 printf("--ppm write page images <pdf-name>.<page-number>.ppm\n");
373#ifdef _WIN32
374 printf("--bmp write page images <pdf-name>.<page-number>.bmp\n");
375 printf("--emf write page meta files <pdf-name>.<page-number>.emf\n");
376#endif
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700377 return 1;
378 }
379
380 FPDF_InitLibrary(NULL);
381
382 UNSUPPORT_INFO unsuppored_info;
383 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
384 unsuppored_info.version = 1;
385 unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler;
386
387 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
388
389 while (!files.empty()) {
390 const char* filename = files.front();
391 files.pop_front();
John Abd-El-Maleka548d302014-06-26 10:18:11 -0700392 FILE* file = fopen(filename, "rb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700393 if (!file) {
394 fprintf(stderr, "Failed to open: %s\n", filename);
395 continue;
396 }
397 (void) fseek(file, 0, SEEK_END);
398 size_t len = ftell(file);
399 (void) fseek(file, 0, SEEK_SET);
400 char* pBuf = (char*) malloc(len);
401 size_t ret = fread(pBuf, 1, len, file);
402 (void) fclose(file);
403 if (ret != len) {
404 fprintf(stderr, "Failed to read: %s\n", filename);
405 } else {
Vitaly Buka9e0177a2014-07-22 18:15:42 -0700406 RenderPdf(filename, pBuf, len, format);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700407 }
408 free(pBuf);
409 }
410
411 FPDF_DestroyLibrary();
412
413 return 0;
414}