| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <limits.h> | 5 #include <limits.h> |
| 6 #include <stdio.h> | 6 #include <stdio.h> |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include <list> | 10 #include <list> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <utility> | 12 #include <utility> |
| 13 | 13 |
| 14 #include "../fpdfsdk/include/fpdf_dataavail.h" | 14 #include "../fpdfsdk/include/fpdf_dataavail.h" |
| 15 #include "../fpdfsdk/include/fpdf_ext.h" | 15 #include "../fpdfsdk/include/fpdf_ext.h" |
| 16 #include "../fpdfsdk/include/fpdfformfill.h" | 16 #include "../fpdfsdk/include/fpdfformfill.h" |
| 17 #include "../fpdfsdk/include/fpdftext.h" | 17 #include "../fpdfsdk/include/fpdftext.h" |
| 18 #include "../fpdfsdk/include/fpdfview.h" | 18 #include "../fpdfsdk/include/fpdfview.h" |
| 19 #include "v8/include/v8.h" | 19 #include "v8/include/v8.h" |
| 20 | 20 |
| 21 #ifdef _WIN32 | 21 #ifdef _WIN32 |
| 22 #define snprintf _snprintf | 22 #define snprintf _snprintf |
| 23 #endif | 23 #endif |
| 24 | 24 |
| 25 static void WritePpm(const char* pdf_name, int num, | 25 enum OutputFormat { |
| 26 const char* buffer, int stride, int width, int height) { | 26 OUTPUT_NONE, |
| 27 OUTPUT_PPM, |
| 28 #ifdef _WIN32 |
| 29 OUTPUT_BMP, |
| 30 OUTPUT_EMF, |
| 31 #endif |
| 32 }; |
| 33 |
| 34 static 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 |
| 27 if (stride < 0 || width < 0 || height < 0) | 38 if (stride < 0 || width < 0 || height < 0) |
| 28 return; | 39 return; |
| 29 if (height > 0 && width > INT_MAX / height) | 40 if (height > 0 && width > INT_MAX / height) |
| 30 return; | 41 return; |
| 31 int out_len = width * height; | 42 int out_len = width * height; |
| 32 if (out_len > INT_MAX / 3) | 43 if (out_len > INT_MAX / 3) |
| 33 return; | 44 return; |
| 34 out_len *= 3; | 45 out_len *= 3; |
| 35 | 46 |
| 36 char filename[256]; | 47 char filename[256]; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 54 // B | 65 // B |
| 55 dest_line[(w * 3) + 2] = src_line[w * 4]; | 66 dest_line[(w * 3) + 2] = src_line[w * 4]; |
| 56 } | 67 } |
| 57 } | 68 } |
| 58 fwrite(result, out_len, 1, fp); | 69 fwrite(result, out_len, 1, fp); |
| 59 delete [] result; | 70 delete [] result; |
| 60 } | 71 } |
| 61 fclose(fp); | 72 fclose(fp); |
| 62 } | 73 } |
| 63 | 74 |
| 75 #ifdef _WIN32 |
| 76 static 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 |
| 112 void 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 |
| 64 int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) { | 137 int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) { |
| 65 printf("Form_Alert called.\n"); | 138 printf("Form_Alert called.\n"); |
| 66 return 0; | 139 return 0; |
| 67 } | 140 } |
| 68 | 141 |
| 69 void Unsupported_Handler(UNSUPPORT_INFO*, int type) { | 142 void Unsupported_Handler(UNSUPPORT_INFO*, int type) { |
| 70 std::string feature = "Unknown"; | 143 std::string feature = "Unknown"; |
| 71 switch (type) { | 144 switch (type) { |
| 72 case FPDF_UNSP_DOC_XFAFORM: | 145 case FPDF_UNSP_DOC_XFAFORM: |
| 73 feature = "XFA"; | 146 feature = "XFA"; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 103 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: | 176 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: |
| 104 feature = "Screen"; | 177 feature = "Screen"; |
| 105 break; | 178 break; |
| 106 case FPDF_UNSP_ANNOT_SIG: | 179 case FPDF_UNSP_ANNOT_SIG: |
| 107 feature = "Digital_Signature"; | 180 feature = "Digital_Signature"; |
| 108 break; | 181 break; |
| 109 } | 182 } |
| 110 printf("Unsupported feature: %s.\n", feature.c_str()); | 183 printf("Unsupported feature: %s.\n", feature.c_str()); |
| 111 } | 184 } |
| 112 | 185 |
| 113 bool ParseCommandLine(int argc, const char* argv[], bool* write_images, | 186 bool ParseCommandLine(int argc, const char* argv[], OutputFormat* output_format, |
| 114 std::list<const char*>* files) { | 187 std::list<const char*>* files) { |
| 115 *write_images = false; | 188 *output_format = OUTPUT_NONE; |
| 116 files->clear(); | 189 files->clear(); |
| 117 | 190 |
| 118 int cur_arg = 1; | 191 int cur_arg = 1; |
| 119 if (cur_arg < argc && | 192 if (cur_arg < argc) { |
| 120 strcmp(argv[cur_arg], "--write_images") == 0) { | 193 if (strcmp(argv[cur_arg], "--ppm") == 0) |
| 121 *write_images = true; | 194 *output_format = OUTPUT_PPM; |
| 195 #ifdef _WIN32 |
| 196 if (strcmp(argv[cur_arg], "--emf") == 0) |
| 197 *output_format = OUTPUT_EMF; |
| 198 if (strcmp(argv[cur_arg], "--bmp") == 0) |
| 199 *output_format = OUTPUT_BMP; |
| 200 #endif |
| 122 cur_arg++; | 201 cur_arg++; |
| 123 } | 202 } |
| 124 | 203 |
| 125 if (cur_arg >= argc) | 204 if (cur_arg >= argc) |
| 126 return false; | 205 return false; |
| 127 | 206 |
| 128 for (int i = cur_arg; i < argc; i++) | 207 for (int i = cur_arg; i < argc; i++) |
| 129 files->push_back(argv[i]); | 208 files->push_back(argv[i]); |
| 130 | 209 |
| 131 return true; | 210 return true; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 152 } | 231 } |
| 153 | 232 |
| 154 bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { | 233 bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { |
| 155 return true; | 234 return true; |
| 156 } | 235 } |
| 157 | 236 |
| 158 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { | 237 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { |
| 159 } | 238 } |
| 160 | 239 |
| 161 void RenderPdf(const char* name, const char* pBuf, size_t len, | 240 void RenderPdf(const char* name, const char* pBuf, size_t len, |
| 162 bool write_images) { | 241 OutputFormat format) { |
| 163 printf("Rendering PDF file %s.\n", name); | 242 printf("Rendering PDF file %s.\n", name); |
| 164 | 243 |
| 165 IPDF_JSPLATFORM platform_callbacks; | 244 IPDF_JSPLATFORM platform_callbacks; |
| 166 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); | 245 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); |
| 167 platform_callbacks.version = 1; | 246 platform_callbacks.version = 1; |
| 168 platform_callbacks.app_alert = Form_Alert; | 247 platform_callbacks.app_alert = Form_Alert; |
| 169 | 248 |
| 170 FPDF_FORMFILLINFO form_callbacks; | 249 FPDF_FORMFILLINFO form_callbacks; |
| 171 memset(&form_callbacks, '\0', sizeof(form_callbacks)); | 250 memset(&form_callbacks, '\0', sizeof(form_callbacks)); |
| 172 form_callbacks.version = 1; | 251 form_callbacks.version = 1; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 FORM_OnAfterLoadPage(page, form); | 306 FORM_OnAfterLoadPage(page, form); |
| 228 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); | 307 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); |
| 229 | 308 |
| 230 int width = static_cast<int>(FPDF_GetPageWidth(page)); | 309 int width = static_cast<int>(FPDF_GetPageWidth(page)); |
| 231 int height = static_cast<int>(FPDF_GetPageHeight(page)); | 310 int height = static_cast<int>(FPDF_GetPageHeight(page)); |
| 232 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0); | 311 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0); |
| 233 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); | 312 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
| 234 | 313 |
| 235 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); | 314 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); |
| 236 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); | 315 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); |
| 237 if (write_images) { | 316 int stride = FPDFBitmap_GetStride(bitmap); |
| 238 const char* buffer = reinterpret_cast<const char*>( | 317 const char* buffer = |
| 239 FPDFBitmap_GetBuffer(bitmap)); | 318 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap)); |
| 240 int stride = FPDFBitmap_GetStride(bitmap); | 319 |
| 241 WritePpm(name, i, buffer, stride, width, height); | 320 switch (format) { |
| 321 #ifdef _WIN32 |
| 322 case OUTPUT_BMP: |
| 323 WriteBmp(name, i, buffer, stride, width, height); |
| 324 break; |
| 325 |
| 326 case OUTPUT_EMF: |
| 327 WriteEmf(page, name, i); |
| 328 break; |
| 329 #endif |
| 330 case OUTPUT_PPM: |
| 331 WritePpm(name, i, buffer, stride, width, height); |
| 332 break; |
| 333 default: |
| 334 break; |
| 242 } | 335 } |
| 243 | 336 |
| 244 FPDFBitmap_Destroy(bitmap); | 337 FPDFBitmap_Destroy(bitmap); |
| 245 | 338 |
| 246 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); | 339 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); |
| 247 FORM_OnBeforeClosePage(page, form); | 340 FORM_OnBeforeClosePage(page, form); |
| 248 FPDFText_ClosePage(text_page); | 341 FPDFText_ClosePage(text_page); |
| 249 FPDF_ClosePage(page); | 342 FPDF_ClosePage(page); |
| 250 } | 343 } |
| 251 | 344 |
| 252 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); | 345 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); |
| 253 FPDFDOC_ExitFormFillEnviroument(form); | 346 FPDFDOC_ExitFormFillEnviroument(form); |
| 254 FPDF_CloseDocument(doc); | 347 FPDF_CloseDocument(doc); |
| 255 FPDFAvail_Destroy(pdf_avail); | 348 FPDFAvail_Destroy(pdf_avail); |
| 256 | 349 |
| 257 printf("Loaded, parsed and rendered %d pages.\n", page_count); | 350 printf("Loaded, parsed and rendered %d pages.\n", page_count); |
| 258 } | 351 } |
| 259 | 352 |
| 260 int main(int argc, const char* argv[]) { | 353 int main(int argc, const char* argv[]) { |
| 261 v8::V8::InitializeICU(); | 354 v8::V8::InitializeICU(); |
| 262 bool write_images = false; | 355 OutputFormat format = OUTPUT_NONE; |
| 263 std::list<const char*> files; | 356 std::list<const char*> files; |
| 264 if (!ParseCommandLine(argc, argv, &write_images, &files)) { | 357 if (!ParseCommandLine(argc, argv, &format, &files)) { |
| 265 printf("Usage is: test [--write_images] /path/to/pdf\n"); | 358 printf("Usage: pdfium_test [OPTIONS] [FILE]\n"); |
| 266 printf("--write_images - to write page images <pdf-name>.<page-number>.ppm\n
"); | 359 printf("--ppm write page images <pdf-name>.<page-number>.ppm\n"); |
| 360 #ifdef _WIN32 |
| 361 printf("--bmp write page images <pdf-name>.<page-number>.bmp\n"); |
| 362 printf("--emf write page meta files <pdf-name>.<page-number>.emf\n"); |
| 363 #endif |
| 267 return 1; | 364 return 1; |
| 268 } | 365 } |
| 269 | 366 |
| 270 FPDF_InitLibrary(NULL); | 367 FPDF_InitLibrary(NULL); |
| 271 | 368 |
| 272 UNSUPPORT_INFO unsuppored_info; | 369 UNSUPPORT_INFO unsuppored_info; |
| 273 memset(&unsuppored_info, '\0', sizeof(unsuppored_info)); | 370 memset(&unsuppored_info, '\0', sizeof(unsuppored_info)); |
| 274 unsuppored_info.version = 1; | 371 unsuppored_info.version = 1; |
| 275 unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler; | 372 unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler; |
| 276 | 373 |
| 277 FSDK_SetUnSpObjProcessHandler(&unsuppored_info); | 374 FSDK_SetUnSpObjProcessHandler(&unsuppored_info); |
| 278 | 375 |
| 279 while (!files.empty()) { | 376 while (!files.empty()) { |
| 280 const char* filename = files.front(); | 377 const char* filename = files.front(); |
| 281 files.pop_front(); | 378 files.pop_front(); |
| 282 FILE* file = fopen(filename, "rb"); | 379 FILE* file = fopen(filename, "rb"); |
| 283 if (!file) { | 380 if (!file) { |
| 284 fprintf(stderr, "Failed to open: %s\n", filename); | 381 fprintf(stderr, "Failed to open: %s\n", filename); |
| 285 continue; | 382 continue; |
| 286 } | 383 } |
| 287 (void) fseek(file, 0, SEEK_END); | 384 (void) fseek(file, 0, SEEK_END); |
| 288 size_t len = ftell(file); | 385 size_t len = ftell(file); |
| 289 (void) fseek(file, 0, SEEK_SET); | 386 (void) fseek(file, 0, SEEK_SET); |
| 290 char* pBuf = (char*) malloc(len); | 387 char* pBuf = (char*) malloc(len); |
| 291 size_t ret = fread(pBuf, 1, len, file); | 388 size_t ret = fread(pBuf, 1, len, file); |
| 292 (void) fclose(file); | 389 (void) fclose(file); |
| 293 if (ret != len) { | 390 if (ret != len) { |
| 294 fprintf(stderr, "Failed to read: %s\n", filename); | 391 fprintf(stderr, "Failed to read: %s\n", filename); |
| 295 } else { | 392 } else { |
| 296 RenderPdf(filename, pBuf, len, write_images); | 393 RenderPdf(filename, pBuf, len, format); |
| 297 } | 394 } |
| 298 free(pBuf); | 395 free(pBuf); |
| 299 } | 396 } |
| 300 | 397 |
| 301 FPDF_DestroyLibrary(); | 398 FPDF_DestroyLibrary(); |
| 302 | 399 |
| 303 return 0; | 400 return 0; |
| 304 } | 401 } |
| 305 | |
| OLD | NEW |