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