Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(324)

Side by Side Diff: samples/pdfium_test.cc

Issue 298893007: Create a standalone pdfium build. (Closed) Base URL: https://pdfium.googlesource.com/pdfium@master
Patch Set: fix mac Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "../fpdfsdk/include/fpdf_dataavail.h"
Nico 2014/05/23 21:14:33 nit: Relative includes looks strange; maybe add `'
jam 2014/05/23 23:52:13 Done.
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"
19 #include "../v8/include/v8.h"
20
21 #ifdef _WIN32
22 #define snprintf _snprintf
Nico 2014/05/23 21:14:33 Not sure if it's important, but _snprintf is diffe
jam 2014/05/23 23:52:13 i'm going to leave this since it's all moved code
23 /* in Windows, rb for open and read binary file */
24 #define FOPEN_READ "rb"
25 #else
26 #define FOPEN_READ "r"
27 #endif
28
29 static void write_file(const char* pdf_name, int num,
Nico 2014/05/23 21:14:33 nit: WritePpm?
jam 2014/05/23 23:52:13 Done.
30 const char* buffer, int stride, int width, int height) {
31 if (stride < 0 || width < 0 || height < 0)
32 return;
33 if (height > 0 && width > INT_MAX / height)
34 return;
35 int out_len = width * height;
36 if (out_len > INT_MAX / 3)
37 return;
38 out_len *= 3;
39
40 char filename[256];
41 snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
42 FILE* fp = fopen(filename, "w");
43 if (!fp)
44 return;
45 fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
46 // Source data is B, G, R, unused.
47 // Dest data is R, G, B.
48 char* result = new char[out_len];
49 if (result) {
50 int h, w;
Nico 2014/05/23 21:14:33 declare these in the for loop, you only use them i
jam 2014/05/23 23:52:13 Done.
51 for (h = 0; h < height; ++h) {
52 const char* src_line = buffer + (stride * h);
53 char* dest_line = result + (width * h * 3);
54 for (w = 0; w < width; ++w) {
55 // R
56 dest_line[w * 3] = src_line[(w * 4) + 2];
57 // G
58 dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
59 // B
60 dest_line[(w * 3) + 2] = src_line[w * 4];
61 }
62 }
63 fwrite(result, out_len, 1, fp);
64 delete [] result;
65 }
66 fclose(fp);
67 }
Nico 2014/05/23 21:14:33 (fwiw, writing an inefficiently compressed png isn
jam 2014/05/23 23:52:13 ditto
68
69 int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) {
Nico 2014/05/23 21:14:33 nit: no underscores in function names (?) (if so,
jam 2014/05/23 23:52:13 ditto
70 printf("Form_Alert called.\n");
Nico 2014/05/23 21:14:33 should this print to stderr?
jam 2014/05/23 23:52:13 ditto
jam 2014/05/23 23:52:13 ditto (no idea about these questions, this tool is
71 return 0;
72 }
73
74 void Unsupported_Handler(UNSUPPORT_INFO*, int type) {
75 std::string feature = "Unknown";
Nico 2014/05/23 21:14:33 (nit: i'd make this a const char*, then there's fe
jam 2014/05/23 23:52:13 ditto
76 switch (type) {
77 case FPDF_UNSP_DOC_XFAFORM:
78 feature = "XFA";
79 break;
80 case FPDF_UNSP_DOC_PORTABLECOLLECTION:
81 feature = "Portfolios_Packages";
82 break;
83 case FPDF_UNSP_DOC_ATTACHMENT:
84 case FPDF_UNSP_ANNOT_ATTACHMENT:
85 feature = "Attachment";
86 break;
87 case FPDF_UNSP_DOC_SECURITY:
88 feature = "Rights_Management";
89 break;
90 case FPDF_UNSP_DOC_SHAREDREVIEW:
91 feature = "Shared_Review";
92 break;
93 case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
94 case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
95 case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
96 feature = "Shared_Form";
97 break;
98 case FPDF_UNSP_ANNOT_3DANNOT:
99 feature = "3D";
100 break;
101 case FPDF_UNSP_ANNOT_MOVIE:
102 feature = "Movie";
103 break;
104 case FPDF_UNSP_ANNOT_SOUND:
105 feature = "Sound";
106 break;
107 case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
108 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
109 feature = "Screen";
110 break;
111 case FPDF_UNSP_ANNOT_SIG:
112 feature = "Digital_Signature";
113 break;
114 }
115 printf("Unsupported feature: %s.\n", feature.c_str());
Nico 2014/05/23 21:14:33 stderr?
116 }
117
118 bool ParseCommandLine(int argc, const char* argv[], bool* write_images,
119 std::list<const char*>* files) {
Nico 2014/05/23 21:14:33 nit: use a vector instead of a list (then the main
120 *write_images = false;
121 files->clear();
122
123 int cur_arg = 1;
124 if (cur_arg < argc &&
125 strcmp(argv[cur_arg], "--write_images") == 0) {
126 *write_images = true;
127 cur_arg++;
128 }
129
130 if (cur_arg >= argc)
131 return false;
132
133 for (int i = cur_arg; i < argc; i++)
134 files->push_back(argv[i]);
135
136 return true;
137 }
138
139 class TestLoader {
140 public:
141 TestLoader(const char* pBuf, size_t len);
142
143 const char* m_pBuf;
144 size_t m_Len;
145 };
146
147 TestLoader::TestLoader(const char* pBuf, size_t len)
148 : m_pBuf(pBuf), m_Len(len) {
149 }
150
151 int Get_Block(void* param, unsigned long pos, unsigned char* pBuf,
152 unsigned long size) {
153 TestLoader* pLoader = (TestLoader*) param;
154 if (pos + size < pos || pos + size > pLoader->m_Len) return 0;
155 memcpy(pBuf, pLoader->m_pBuf + pos, size);
156 return 1;
157 }
158
159 bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) {
160 return true;
161 }
162
163 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) {
164 }
165
166 void RenderPdf(const char* name, const char* pBuf, size_t len,
167 bool write_images) {
168 printf("Rendering PDF file %s.\n", name);
169
170 IPDF_JSPLATFORM platform_callbacks;
171 memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
Nico 2014/05/23 21:14:33 nit: 0 is more common than \0 in memset calls (als
172 platform_callbacks.version = 1;
173 platform_callbacks.app_alert = Form_Alert;
174
175 FPDF_FORMFILLINFO form_callbacks;
176 memset(&form_callbacks, '\0', sizeof(form_callbacks));
177 form_callbacks.version = 1;
178 form_callbacks.m_pJsPlatform = &platform_callbacks;
179
180 TestLoader loader(pBuf, len);
181
182 FPDF_FILEACCESS file_access;
183 memset(&file_access, '\0', sizeof(file_access));
184 file_access.m_FileLen = len;
185 file_access.m_GetBlock = Get_Block;
186 file_access.m_Param = &loader;
187
188 FX_FILEAVAIL file_avail;
189 memset(&file_avail, '\0', sizeof(file_avail));
190 file_avail.version = 1;
191 file_avail.IsDataAvail = Is_Data_Avail;
192
193 FX_DOWNLOADHINTS hints;
194 memset(&hints, '\0', sizeof(hints));
195 hints.version = 1;
196 hints.AddSegment = Add_Segment;
197
198 FPDF_DOCUMENT doc;
199 FPDF_AVAIL pdf_avail = FPDFAvail_Create(&file_avail, &file_access);
200
201 (void) FPDFAvail_IsDocAvail(pdf_avail, &hints);
Nico 2014/05/23 21:14:33 Maybe add a comment why this is being called; it's
202
203 if (!FPDFAvail_IsLinearized(pdf_avail)) {
204 printf("Non-linearized path...\n");
205 doc = FPDF_LoadCustomDocument(&file_access, NULL);
206 } else {
207 printf("Linearized path...\n");
208 doc = FPDFAvail_GetDocument(pdf_avail, NULL);
209 }
210
211 (void) FPDF_GetDocPermissions(doc);
212 (void) FPDFAvail_IsFormAvail(pdf_avail, &hints);
213
214 FPDF_FORMHANDLE form = FPDFDOC_InitFormFillEnviroument(doc, &form_callbacks);
215 FPDF_SetFormFieldHighlightColor(form, 0, 0xFFE4DD);
216 FPDF_SetFormFieldHighlightAlpha(form, 100);
Nico 2014/05/23 21:14:33 nit: maybe use a number that isn't 100 here. The a
217
218 int first_page = FPDFAvail_GetFirstPageNum(doc);
219 (void) FPDFAvail_IsPageAvail(pdf_avail, first_page, &hints);
220
221 int page_count = FPDF_GetPageCount(doc);
222 for (int i = 0; i < page_count; ++i) {
223 (void) FPDFAvail_IsPageAvail(pdf_avail, i, &hints);
224 }
225
226 FORM_DoDocumentJSAction(form);
227 FORM_DoDocumentOpenAction(form);
228
229 for (int i = 0; i < page_count; ++i) {
230 FPDF_PAGE page = FPDF_LoadPage(doc, i);
231 FPDF_TEXTPAGE text_page = FPDFText_LoadPage(page);
232 FORM_OnAfterLoadPage(page, form);
233 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN);
234
235 int width = static_cast<int>(FPDF_GetPageWidth(page));
236 int height = static_cast<int>(FPDF_GetPageHeight(page));
237 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0);
238 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 255, 255, 255, 255);
239
240 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0);
241 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0);
242 if (write_images) {
243 const char* buffer = reinterpret_cast<const char*>(
244 FPDFBitmap_GetBuffer(bitmap));
245 int stride = FPDFBitmap_GetStride(bitmap);
246 write_file(name, i, buffer, stride, width, height);
247 }
248
249 FPDFBitmap_Destroy(bitmap);
250
251 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE);
252 FORM_OnBeforeClosePage(page, form);
253 FPDFText_ClosePage(text_page);
254 FPDF_ClosePage(page);
255 }
256
257 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC);
258 FPDFDOC_ExitFormFillEnviroument(form);
259 FPDF_CloseDocument(doc);
260 FPDFAvail_Destroy(pdf_avail);
261
262 printf("Loaded, parsed and rendered %d pages.\n", page_count);
263 }
264
265 int main(int argc, const char* argv[]) {
266 v8::V8::InitializeICU();
267 bool write_images = false;
268 std::list<const char*> files;
269 if (!ParseCommandLine(argc, argv, &write_images, &files)) {
270 printf("Usage is: test [--write_images] /path/to/pdf\n");
271 printf("--write_images - to write page images <pdf-name>.<page-number>.ppm\n ");
272 return 1;
273 }
274
275 FPDF_InitLibrary(NULL);
276
277 UNSUPPORT_INFO unsuppored_info;
278 memset(&unsuppored_info, '\0', sizeof(unsuppored_info));
279 unsuppored_info.version = 1;
280 unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler;
281
282 FSDK_SetUnSpObjProcessHandler(&unsuppored_info);
283
284 while (!files.empty()) {
285 const char* filename = files.front();
286 files.pop_front();
287 FILE* file = fopen(filename, FOPEN_READ);
288 if (!file) {
289 fprintf(stderr, "Failed to open: %s\n", filename);
290 continue;
291 }
292 (void) fseek(file, 0, SEEK_END);
293 size_t len = ftell(file);
294 (void) fseek(file, 0, SEEK_SET);
295 char* pBuf = (char*) malloc(len);
296 size_t ret = fread(pBuf, 1, len, file);
297 (void) fclose(file);
298 if (ret != len) {
299 fprintf(stderr, "Failed to read: %s\n", filename);
300 } else {
301 RenderPdf(filename, pBuf, len, write_images);
302 }
303 free(pBuf);
304 }
305
306 FPDF_DestroyLibrary();
307
308 return 0;
309 }
310
OLDNEW
« build/standalone.gypi ('K') | « build/standalone.gypi ('k') | samples/samples.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698