Chromium Code Reviews| 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 SelectObject(dc, GetStockObject(NULL_PEN)); | |
| 122 SelectObject(dc, GetStockObject(WHITE_BRUSH)); | |
| 123 Rectangle(dc, 0, 0, width, height); | |
| 124 | |
| 125 int saved_dc = SaveDC(dc); | |
| 126 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, | |
| 127 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); | |
| 128 RestoreDC(dc, saved_dc); | |
| 129 | |
| 130 // Mark page boundaries for convenience. | |
| 131 HPEN pen = CreatePen(PS_DASH, 5, RGB(255, 0, 0)); | |
|
Bo Xu
2014/07/23 00:31:47
Not sure if we want to mark the boundary here, or
Vitaly Buka (NO REVIEWS)
2014/07/23 00:35:41
I had impression is just test application and no o
| |
| 132 SelectObject(dc, pen); | |
| 133 SelectObject(dc, GetStockObject(NULL_BRUSH)); | |
| 134 Rectangle(dc, 0, 0, width, height); | |
| 135 DeleteObject(pen); | |
| 136 | |
| 137 DeleteEnhMetaFile(CloseEnhMetaFile(dc)); | |
| 138 } | |
| 139 #endif | |
| 140 | |
| 64 int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) { | 141 int Form_Alert(IPDF_JSPLATFORM*, FPDF_WIDESTRING, FPDF_WIDESTRING, int, int) { |
| 65 printf("Form_Alert called.\n"); | 142 printf("Form_Alert called.\n"); |
| 66 return 0; | 143 return 0; |
| 67 } | 144 } |
| 68 | 145 |
| 69 void Unsupported_Handler(UNSUPPORT_INFO*, int type) { | 146 void Unsupported_Handler(UNSUPPORT_INFO*, int type) { |
| 70 std::string feature = "Unknown"; | 147 std::string feature = "Unknown"; |
| 71 switch (type) { | 148 switch (type) { |
| 72 case FPDF_UNSP_DOC_XFAFORM: | 149 case FPDF_UNSP_DOC_XFAFORM: |
| 73 feature = "XFA"; | 150 feature = "XFA"; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 103 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: | 180 case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: |
| 104 feature = "Screen"; | 181 feature = "Screen"; |
| 105 break; | 182 break; |
| 106 case FPDF_UNSP_ANNOT_SIG: | 183 case FPDF_UNSP_ANNOT_SIG: |
| 107 feature = "Digital_Signature"; | 184 feature = "Digital_Signature"; |
| 108 break; | 185 break; |
| 109 } | 186 } |
| 110 printf("Unsupported feature: %s.\n", feature.c_str()); | 187 printf("Unsupported feature: %s.\n", feature.c_str()); |
| 111 } | 188 } |
| 112 | 189 |
| 113 bool ParseCommandLine(int argc, const char* argv[], bool* write_images, | 190 bool ParseCommandLine(int argc, const char* argv[], OutputFormat* output_format, |
| 114 std::list<const char*>* files) { | 191 std::list<const char*>* files) { |
| 115 *write_images = false; | 192 *output_format = OUTPUT_NONE; |
| 116 files->clear(); | 193 files->clear(); |
| 117 | 194 |
| 118 int cur_arg = 1; | 195 int cur_arg = 1; |
| 119 if (cur_arg < argc && | 196 if (cur_arg < argc) { |
| 120 strcmp(argv[cur_arg], "--write_images") == 0) { | 197 if (strcmp(argv[cur_arg], "--ppm") == 0) |
| 121 *write_images = true; | 198 *output_format = OUTPUT_PPM; |
| 199 #ifdef _WIN32 | |
| 200 if (strcmp(argv[cur_arg], "--emf") == 0) | |
| 201 *output_format = OUTPUT_EMF; | |
| 202 if (strcmp(argv[cur_arg], "--bmp") == 0) | |
| 203 *output_format = OUTPUT_BMP; | |
| 204 #endif | |
| 122 cur_arg++; | 205 cur_arg++; |
| 123 } | 206 } |
| 124 | 207 |
| 125 if (cur_arg >= argc) | 208 if (cur_arg >= argc) |
| 126 return false; | 209 return false; |
| 127 | 210 |
| 128 for (int i = cur_arg; i < argc; i++) | 211 for (int i = cur_arg; i < argc; i++) |
| 129 files->push_back(argv[i]); | 212 files->push_back(argv[i]); |
| 130 | 213 |
| 131 return true; | 214 return true; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 152 } | 235 } |
| 153 | 236 |
| 154 bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { | 237 bool Is_Data_Avail(FX_FILEAVAIL* pThis, size_t offset, size_t size) { |
| 155 return true; | 238 return true; |
| 156 } | 239 } |
| 157 | 240 |
| 158 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { | 241 void Add_Segment(FX_DOWNLOADHINTS* pThis, size_t offset, size_t size) { |
| 159 } | 242 } |
| 160 | 243 |
| 161 void RenderPdf(const char* name, const char* pBuf, size_t len, | 244 void RenderPdf(const char* name, const char* pBuf, size_t len, |
| 162 bool write_images) { | 245 OutputFormat format) { |
| 163 printf("Rendering PDF file %s.\n", name); | 246 printf("Rendering PDF file %s.\n", name); |
| 164 | 247 |
| 165 IPDF_JSPLATFORM platform_callbacks; | 248 IPDF_JSPLATFORM platform_callbacks; |
| 166 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); | 249 memset(&platform_callbacks, '\0', sizeof(platform_callbacks)); |
| 167 platform_callbacks.version = 1; | 250 platform_callbacks.version = 1; |
| 168 platform_callbacks.app_alert = Form_Alert; | 251 platform_callbacks.app_alert = Form_Alert; |
| 169 | 252 |
| 170 FPDF_FORMFILLINFO form_callbacks; | 253 FPDF_FORMFILLINFO form_callbacks; |
| 171 memset(&form_callbacks, '\0', sizeof(form_callbacks)); | 254 memset(&form_callbacks, '\0', sizeof(form_callbacks)); |
| 172 form_callbacks.version = 1; | 255 form_callbacks.version = 1; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 FORM_OnAfterLoadPage(page, form); | 310 FORM_OnAfterLoadPage(page, form); |
| 228 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); | 311 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_OPEN); |
| 229 | 312 |
| 230 int width = static_cast<int>(FPDF_GetPageWidth(page)); | 313 int width = static_cast<int>(FPDF_GetPageWidth(page)); |
| 231 int height = static_cast<int>(FPDF_GetPageHeight(page)); | 314 int height = static_cast<int>(FPDF_GetPageHeight(page)); |
| 232 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0); | 315 FPDF_BITMAP bitmap = FPDFBitmap_Create(width, height, 0); |
| 233 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); | 316 FPDFBitmap_FillRect(bitmap, 0, 0, width, height, 0xFFFFFFFF); |
| 234 | 317 |
| 235 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); | 318 FPDF_RenderPageBitmap(bitmap, page, 0, 0, width, height, 0, 0); |
| 236 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); | 319 FPDF_FFLDraw(form, bitmap, page, 0, 0, width, height, 0, 0); |
| 237 if (write_images) { | 320 int stride = FPDFBitmap_GetStride(bitmap); |
| 238 const char* buffer = reinterpret_cast<const char*>( | 321 const char* buffer = |
| 239 FPDFBitmap_GetBuffer(bitmap)); | 322 reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap)); |
| 240 int stride = FPDFBitmap_GetStride(bitmap); | 323 |
| 241 WritePpm(name, i, buffer, stride, width, height); | 324 switch (format) { |
| 325 #ifdef _WIN32 | |
| 326 case OUTPUT_BMP: | |
| 327 WriteBmp(name, i, buffer, stride, width, height); | |
| 328 break; | |
| 329 | |
| 330 case OUTPUT_EMF: | |
| 331 WriteEmf(page, name, i); | |
| 332 break; | |
| 333 #endif | |
| 334 case OUTPUT_PPM: | |
| 335 WritePpm(name, i, buffer, stride, width, height); | |
| 336 break; | |
| 337 default: | |
| 338 break; | |
| 242 } | 339 } |
| 243 | 340 |
| 244 FPDFBitmap_Destroy(bitmap); | 341 FPDFBitmap_Destroy(bitmap); |
| 245 | 342 |
| 246 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); | 343 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); |
| 247 FORM_OnBeforeClosePage(page, form); | 344 FORM_OnBeforeClosePage(page, form); |
| 248 FPDFText_ClosePage(text_page); | 345 FPDFText_ClosePage(text_page); |
| 249 FPDF_ClosePage(page); | 346 FPDF_ClosePage(page); |
| 250 } | 347 } |
| 251 | 348 |
| 252 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); | 349 FORM_DoDocumentAAction(form, FPDFDOC_AACTION_WC); |
| 253 FPDFDOC_ExitFormFillEnviroument(form); | 350 FPDFDOC_ExitFormFillEnviroument(form); |
| 254 FPDF_CloseDocument(doc); | 351 FPDF_CloseDocument(doc); |
| 255 FPDFAvail_Destroy(pdf_avail); | 352 FPDFAvail_Destroy(pdf_avail); |
| 256 | 353 |
| 257 printf("Loaded, parsed and rendered %d pages.\n", page_count); | 354 printf("Loaded, parsed and rendered %d pages.\n", page_count); |
| 258 } | 355 } |
| 259 | 356 |
| 260 int main(int argc, const char* argv[]) { | 357 int main(int argc, const char* argv[]) { |
| 261 v8::V8::InitializeICU(); | 358 v8::V8::InitializeICU(); |
| 262 bool write_images = false; | 359 OutputFormat format = OUTPUT_NONE; |
| 263 std::list<const char*> files; | 360 std::list<const char*> files; |
| 264 if (!ParseCommandLine(argc, argv, &write_images, &files)) { | 361 if (!ParseCommandLine(argc, argv, &format, &files)) { |
| 265 printf("Usage is: test [--write_images] /path/to/pdf\n"); | 362 printf("Usage: pdfium_test [OPTIONS] [FILE]\n"); |
| 266 printf("--write_images - to write page images <pdf-name>.<page-number>.ppm\n "); | 363 printf("--ppm write page images <pdf-name>.<page-number>.ppm\n"); |
| 364 #ifdef _WIN32 | |
| 365 printf("--bmp write page images <pdf-name>.<page-number>.bmp\n"); | |
| 366 printf("--emf write page meta files <pdf-name>.<page-number>.emf\n"); | |
| 367 #endif | |
| 267 return 1; | 368 return 1; |
| 268 } | 369 } |
| 269 | 370 |
| 270 FPDF_InitLibrary(NULL); | 371 FPDF_InitLibrary(NULL); |
| 271 | 372 |
| 272 UNSUPPORT_INFO unsuppored_info; | 373 UNSUPPORT_INFO unsuppored_info; |
| 273 memset(&unsuppored_info, '\0', sizeof(unsuppored_info)); | 374 memset(&unsuppored_info, '\0', sizeof(unsuppored_info)); |
| 274 unsuppored_info.version = 1; | 375 unsuppored_info.version = 1; |
| 275 unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler; | 376 unsuppored_info.FSDK_UnSupport_Handler = Unsupported_Handler; |
| 276 | 377 |
| 277 FSDK_SetUnSpObjProcessHandler(&unsuppored_info); | 378 FSDK_SetUnSpObjProcessHandler(&unsuppored_info); |
| 278 | 379 |
| 279 while (!files.empty()) { | 380 while (!files.empty()) { |
| 280 const char* filename = files.front(); | 381 const char* filename = files.front(); |
| 281 files.pop_front(); | 382 files.pop_front(); |
| 282 FILE* file = fopen(filename, "rb"); | 383 FILE* file = fopen(filename, "rb"); |
| 283 if (!file) { | 384 if (!file) { |
| 284 fprintf(stderr, "Failed to open: %s\n", filename); | 385 fprintf(stderr, "Failed to open: %s\n", filename); |
| 285 continue; | 386 continue; |
| 286 } | 387 } |
| 287 (void) fseek(file, 0, SEEK_END); | 388 (void) fseek(file, 0, SEEK_END); |
| 288 size_t len = ftell(file); | 389 size_t len = ftell(file); |
| 289 (void) fseek(file, 0, SEEK_SET); | 390 (void) fseek(file, 0, SEEK_SET); |
| 290 char* pBuf = (char*) malloc(len); | 391 char* pBuf = (char*) malloc(len); |
| 291 size_t ret = fread(pBuf, 1, len, file); | 392 size_t ret = fread(pBuf, 1, len, file); |
| 292 (void) fclose(file); | 393 (void) fclose(file); |
| 293 if (ret != len) { | 394 if (ret != len) { |
| 294 fprintf(stderr, "Failed to read: %s\n", filename); | 395 fprintf(stderr, "Failed to read: %s\n", filename); |
| 295 } else { | 396 } else { |
| 296 RenderPdf(filename, pBuf, len, write_images); | 397 RenderPdf(filename, pBuf, len, format); |
| 297 } | 398 } |
| 298 free(pBuf); | 399 free(pBuf); |
| 299 } | 400 } |
| 300 | 401 |
| 301 FPDF_DestroyLibrary(); | 402 FPDF_DestroyLibrary(); |
| 302 | 403 |
| 303 return 0; | 404 return 0; |
| 304 } | 405 } |
| 305 | |
| OLD | NEW |