blob: f8e567349beb0ba8c98450dbaef88894fb7ee014 [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
25static void WritePpm(const char* pdf_name, int num,
26 const char* buffer, int stride, int width, int height) {
27 if (stride < 0 || width < 0 || height < 0)
28 return;
29 if (height > 0 && width > INT_MAX / height)
30 return;
31 int out_len = width * height;
32 if (out_len > INT_MAX / 3)
33 return;
34 out_len *= 3;
35
36 char filename[256];
37 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
John Abd-El-Maleka548d302014-06-26 10:18:11 -070038 FILE* fp = fopen(filename, "wb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039 if (!fp)
40 return;
41 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
42 // Source data is B, G, R, unused.
43 // Dest data is R, G, B.
44 char* result = new char[out_len];
45 if (result) {
46 for (int h = 0; h < height; ++h) {
47 const char* src_line = buffer + (stride * h);
48 char* dest_line = result + (width * h * 3);
49 for (int w = 0; w < width; ++w) {
50 // R
51 dest_line[w * 3] = src_line[(w * 4) + 2];
52 // G
53 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
54 // B
55 dest_line[(w * 3) + 2] = src_line[w * 4];
56 }
57 }
58 fwrite(result, out_len, 1, fp);
59 delete [] result;
60 }
61 fclose(fp);
62}
63
64int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) {
65 printf("Form_Alert called.\n");
66 return 0;
67}
68
69void Unsupported_Handler(UNSUPPORT_INFO*, int type) {
70 std::string feature = "Unknown";
71 switch (type) {
72 case FPDF_UNSP_DOC_XFAFORM:
73 feature = "XFA";
74 break;
75 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
76 feature = "Portfolios_Packages";
77 break;
78 case FPDF_UNSP_DOC_ATTACHMENT:
79 case FPDF_UNSP_ANNOT_ATTACHMENT:
80 feature = "Attachment";
81 break;
82 case FPDF_UNSP_DOC_SECURITY:
83 feature = "Rights_Management";
84 break;
85 case FPDF_UNSP_DOC_SHAREDREVIEW:
86 feature = "Shared_Review";
87 break;
88 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
89 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
90 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
91 feature = "Shared_Form";
92 break;
93 case FPDF_UNSP_ANNOT_3DANNOT:
94 feature = "3D";
95 break;
96 case FPDF_UNSP_ANNOT_MOVIE:
97 feature = "Movie";
98 break;
99 case FPDF_UNSP_ANNOT_SOUND:
100 feature = "Sound";
101 break;
102 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
103 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
104 feature = "Screen";
105 break;
106 case FPDF_UNSP_ANNOT_SIG:
107 feature = "Digital_Signature";
108 break;
109 }
110 printf("Unsupported feature: %s.\n", feature.c_str());
111}
112
113bool ParseCommandLine(int argc, const char* argv[], bool* write_images,
114 std::list<const char*>* files) {
115 *write_images = false;
116 files->clear();
117
118 int cur_arg = 1;
119 if (cur_arg < argc &&
120 strcmp(argv[cur_arg], "--write_images") == 0) {
121 *write_images = true;
122 cur_arg++;
123 }
124
125 if (cur_arg >= argc)
126 return false;
127
128 for (int i = cur_arg; i < argc; i++)
129 files->push_back(argv[i]);
130
131 return true;
132}
133
134class TestLoader {
135 public:
136 TestLoader(const char* pBuf, size_t len);
137
138 const char* m_pBuf;
139 size_t m_Len;
140};
141
142TestLoader::TestLoader(const char* pBuf, size_t len)
143 : m_pBuf(pBuf), m_Len(len) {
144}
145
146int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
147 unsigned long size) {
148 TestLoader* pLoader = (TestLoader*) param;
149 if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
150 memcpy(pBuf, pLoader->m_pBuf + pos, size);
151 return 1;
152}
153
154bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
155 return true;
156}
157
158void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
159}
160
161void RenderPdf(const char* name, const char* pBuf, size_t len,
162 bool write_images) {
163 printf("Rendering PDF file %s.\n", name);
164
165 IPDF_JSPLATFORM platform_callbacks;
166 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
167 platform_callbacks.version = 1;
168 platform_callbacks.app_alert = Form_Alert;
169
170 FPDF_FORMFILLINFO form_callbacks;
171 memset(&form_callbacks, '\0', sizeof(form_callbacks));
172 form_callbacks.version = 1;
173 form_callbacks.m_pJsPlatform = &platform_callbacks;
174
175 TestLoader loader(pBuf, len);
176
177 FPDF_FILEACCESS file_access;
178 memset(&file_access, '\0', sizeof(file_access));
John Abd-El-Malek7dc51722014-05-26 12:54:31 -0700179 file_access.m_FileLen = static_cast<unsigned long>(len);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700180 file_access.m_GetBlock = Get_Block;
181 file_access.m_Param = &loader;
182
183 FX_FILEAVAIL file_avail;
184 memset(&file_avail, '\0', sizeof(file_avail));
185 file_avail.version = 1;
186 file_avail.IsDataAvail = Is_Data_Avail;
187
188 FX_DOWNLOADHINTS hints;
189 memset(&hints, '\0', sizeof(hints));
190 hints.version = 1;
191 hints.AddSegment = Add_Segment;
192
193 FPDF_DOCUMENT doc;
194 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
195
196 (void) FPDFAvail_IsDocAvail(pdf_avail, &hints);
197
198 if (!FPDFAvail_IsLinearized(pdf_avail)) {
199 printf("Non-linearized path...\n");
200 doc = FPDF_LoadCustomDocument(&file_access, NULL);
201 } else {
202 printf("Linearized path...\n");
203 doc = FPDFAvail_GetDocument(pdf_avail, NULL);
204 }
205
206 (void) FPDF_GetDocPermissions(doc);
207 (void) FPDFAvail_IsFormAvail(pdf_avail, &hints);
208
209 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnviroument(doc, &form_callbacks);
210 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
211 FPDF_SetFormFieldHighlightAlpha(form, 100);
212
213 int first_page = FPDFAvail_GetFirstPageNum(doc);
214 (void) FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);
215
216 int page_count = FPDF_GetPageCount(doc);
217 for (int i = 0; i < page_count; ++i) {
218 (void) FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
219 }
220
221 FORM_DoDocumentJSAction(form);
222 FORM_DoDocumentOpenAction(form);
223
224 for (int i = 0; i < page_count; ++i) {
225 FPDF_PAGE page = FPDF_LoadPage(doc, i);
226 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
227 FORM_OnAfterLoadPage(page, form);
228 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
229
230 int width = static_cast<int>(FPDF_GetPageWidth(page));
231 int height = static_cast<int>(FPDF_GetPageHeight(page));
232 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
Lei Zhang95c018e2014-07-08 16:42:00 -0700233 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700234
235 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
236 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
237 if (write_images) {
238 const char* buffer = reinterpret_cast<const char*>(
239 FPDFBitmap_GetBuffer(bitmap));
240 int stride = FPDFBitmap_GetStride(bitmap);
241 WritePpm(name, i, buffer, stride, width, height);
242 }
243
244 FPDFBitmap_Destroy(bitmap);
245
246 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
247 FORM_OnBeforeClosePage(page, form);
248 FPDFText_ClosePage(text_page);
249 FPDF_ClosePage(page);
250 }
251
252 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
253 FPDFDOC_ExitFormFillEnviroument(form);
254 FPDF_CloseDocument(doc);
255 FPDFAvail_Destroy(pdf_avail);
256
257 printf("Loaded, parsed and rendered %d pages.\n", page_count);
258}
259
260int main(int argc, const char* argv[]) {
261 v8::V8::InitializeICU();
262 bool write_images = false;
263 std::list<const char*> files;
264 if (!ParseCommandLine(argc, argv, &write_images, &files)) {
265 printf("Usage is: test [--write_images] /path/to/pdf\n");
266 printf("--write_images - to write page images <pdf-name>.<page-number>.ppm\n");
267 return 1;
268 }
269
270 FPDF_InitLibrary(NULL);
271
272 UNSUPPORT_INFO unsuppored_info;
273 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
274 unsuppored_info.version = 1;
275 unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler;
276
277 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
278
279 while (!files.empty()) {
280 const char* filename = files.front();
281 files.pop_front();
John Abd-El-Maleka548d302014-06-26 10:18:11 -0700282 FILE* file = fopen(filename, "rb");
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700283 if (!file) {
284 fprintf(stderr, "Failed to open: %s\n", filename);
285 continue;
286 }
287 (void) fseek(file, 0, SEEK_END);
288 size_t len = ftell(file);
289 (void) fseek(file, 0, SEEK_SET);
290 char* pBuf = (char*) malloc(len);
291 size_t ret = fread(pBuf, 1, len, file);
292 (void) fclose(file);
293 if (ret != len) {
294 fprintf(stderr, "Failed to read: %s\n", filename);
295 } else {
296 RenderPdf(filename, pBuf, len, write_images);
297 }
298 free(pBuf);
299 }
300
301 FPDF_DestroyLibrary();
302
303 return 0;
304}
305