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 <sstream> | 11 #include <sstream> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <utility> | 13 #include <utility> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #if defined PDF_ENABLE_SKIA && !defined _SKIA_SUPPORT_ | |
| 17 #define _SKIA_SUPPORT_ | |
| 18 #endif | |
| 19 | |
| 16 #include "public/fpdf_dataavail.h" | 20 #include "public/fpdf_dataavail.h" |
| 17 #include "public/fpdf_edit.h" | 21 #include "public/fpdf_edit.h" |
| 18 #include "public/fpdf_ext.h" | 22 #include "public/fpdf_ext.h" |
| 19 #include "public/fpdf_formfill.h" | 23 #include "public/fpdf_formfill.h" |
| 20 #include "public/fpdf_text.h" | 24 #include "public/fpdf_text.h" |
| 21 #include "public/fpdfview.h" | 25 #include "public/fpdfview.h" |
| 22 #include "samples/image_diff_png.h" | 26 #include "samples/image_diff_png.h" |
| 23 #include "testing/test_support.h" | 27 #include "testing/test_support.h" |
| 24 | 28 |
| 25 #ifdef PDF_ENABLE_V8 | 29 #ifdef PDF_ENABLE_V8 |
| 26 #include "v8/include/libplatform/libplatform.h" | 30 #include "v8/include/libplatform/libplatform.h" |
| 27 #include "v8/include/v8.h" | 31 #include "v8/include/v8.h" |
| 28 #endif // PDF_ENABLE_V8 | 32 #endif // PDF_ENABLE_V8 |
| 29 | 33 |
| 30 #ifdef _WIN32 | 34 #ifdef _WIN32 |
| 31 #define snprintf _snprintf | 35 #define snprintf _snprintf |
| 32 #endif | 36 #endif |
| 33 | 37 |
| 38 #ifdef PDF_ENABLE_SKIA | |
| 39 #include "third_party/skia/include/core/SkPictureRecorder.h" | |
| 40 #include "third_party/skia/include/core/SkStream.h" | |
| 41 #endif | |
| 42 | |
| 34 enum OutputFormat { | 43 enum OutputFormat { |
| 35 OUTPUT_NONE, | 44 OUTPUT_NONE, |
| 36 OUTPUT_PPM, | 45 OUTPUT_PPM, |
| 37 OUTPUT_PNG, | 46 OUTPUT_PNG, |
| 38 #ifdef _WIN32 | 47 #ifdef _WIN32 |
| 39 OUTPUT_BMP, | 48 OUTPUT_BMP, |
| 40 OUTPUT_EMF, | 49 OUTPUT_EMF, |
| 41 #endif | 50 #endif |
| 51 #ifdef PDF_ENABLE_SKIA | |
| 52 OUTPUT_SKP, | |
| 53 #endif | |
| 42 }; | 54 }; |
| 43 | 55 |
| 44 struct Options { | 56 struct Options { |
| 45 Options() : show_config(false), output_format(OUTPUT_NONE) {} | 57 Options() : show_config(false), output_format(OUTPUT_NONE) {} |
| 46 | 58 |
| 47 bool show_config; | 59 bool show_config; |
| 48 OutputFormat output_format; | 60 OutputFormat output_format; |
| 49 std::string scale_factor_as_string; | 61 std::string scale_factor_as_string; |
| 50 std::string exe_path; | 62 std::string exe_path; |
| 51 std::string bin_directory; | 63 std::string bin_directory; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 buffer, width, height, stride, false, &png_encoding)) { | 121 buffer, width, height, stride, false, &png_encoding)) { |
| 110 fprintf(stderr, "Failed to convert bitmap to PNG\n"); | 122 fprintf(stderr, "Failed to convert bitmap to PNG\n"); |
| 111 return; | 123 return; |
| 112 } | 124 } |
| 113 | 125 |
| 114 char filename[256]; | 126 char filename[256]; |
| 115 int chars_formatted = snprintf( | 127 int chars_formatted = snprintf( |
| 116 filename, sizeof(filename), "%s.%d.png", pdf_name, num); | 128 filename, sizeof(filename), "%s.%d.png", pdf_name, num); |
| 117 if (chars_formatted < 0 || | 129 if (chars_formatted < 0 || |
| 118 static_cast<size_t>(chars_formatted) >= sizeof(filename)) { | 130 static_cast<size_t>(chars_formatted) >= sizeof(filename)) { |
| 119 fprintf(stderr, "Filname %s is too long\n", filename); | 131 fprintf(stderr, "Filename %s is too long\n", filename); |
| 120 return; | 132 return; |
| 121 } | 133 } |
| 122 | 134 |
| 123 FILE* fp = fopen(filename, "wb"); | 135 FILE* fp = fopen(filename, "wb"); |
| 124 if (!fp) { | 136 if (!fp) { |
| 125 fprintf(stderr, "Failed to open %s for output\n", filename); | 137 fprintf(stderr, "Failed to open %s for output\n", filename); |
| 126 return; | 138 return; |
| 127 } | 139 } |
| 128 | 140 |
| 129 size_t bytes_written = fwrite( | 141 size_t bytes_written = fwrite( |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 188 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less. | 200 // If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less. |
| 189 Rectangle(dc, 0, 0, width + 1, height + 1); | 201 Rectangle(dc, 0, 0, width + 1, height + 1); |
| 190 | 202 |
| 191 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, | 203 FPDF_RenderPage(dc, page, 0, 0, width, height, 0, |
| 192 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); | 204 FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH); |
| 193 | 205 |
| 194 DeleteEnhMetaFile(CloseEnhMetaFile(dc)); | 206 DeleteEnhMetaFile(CloseEnhMetaFile(dc)); |
| 195 } | 207 } |
| 196 #endif | 208 #endif |
| 197 | 209 |
| 210 #ifdef PDF_ENABLE_SKIA | |
| 211 void WriteSkp(const char* pdf_name, int num, const void* recorder) { | |
| 212 char filename[256]; | |
| 213 int chars_formatted = | |
| 214 snprintf(filename, sizeof(filename), "%s.%d.skp", pdf_name, num); | |
| 215 | |
| 216 if (chars_formatted < 0 || | |
| 217 static_cast<size_t>(chars_formatted) >= sizeof(filename)) { | |
| 218 fprintf(stderr, "Filename %s is too long\n", filename); | |
| 219 return; | |
| 220 } | |
| 221 | |
| 222 SkPictureRecorder* r = (SkPictureRecorder*)recorder; | |
| 223 SkPicture* picture = r->endRecordingAsPicture(); | |
| 224 SkFILEWStream wStream(filename); | |
| 225 picture->serialize(&wStream); | |
| 226 } | |
| 227 #endif | |
| 228 | |
| 198 // These example JS platform callback handlers are entirely optional, | 229 // These example JS platform callback handlers are entirely optional, |
| 199 // and exist here to show the flow of information from a document back | 230 // and exist here to show the flow of information from a document back |
| 200 // to the embedder. | 231 // to the embedder. |
| 201 int ExampleAppAlert(IPDF_JSPLATFORM*, | 232 int ExampleAppAlert(IPDF_JSPLATFORM*, |
| 202 FPDF_WIDESTRING msg, | 233 FPDF_WIDESTRING msg, |
| 203 FPDF_WIDESTRING title, | 234 FPDF_WIDESTRING title, |
| 204 int nType, | 235 int nType, |
| 205 int nIcon) { | 236 int nIcon) { |
| 206 printf("%ls", GetPlatformWString(title).c_str()); | 237 printf("%ls", GetPlatformWString(title).c_str()); |
| 207 if (nIcon || nType) | 238 if (nIcon || nType) |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 312 fprintf(stderr, "Duplicate or conflicting --ppm argument\n"); | 343 fprintf(stderr, "Duplicate or conflicting --ppm argument\n"); |
| 313 return false; | 344 return false; |
| 314 } | 345 } |
| 315 options->output_format = OUTPUT_PPM; | 346 options->output_format = OUTPUT_PPM; |
| 316 } else if (cur_arg == "--png") { | 347 } else if (cur_arg == "--png") { |
| 317 if (options->output_format != OUTPUT_NONE) { | 348 if (options->output_format != OUTPUT_NONE) { |
| 318 fprintf(stderr, "Duplicate or conflicting --png argument\n"); | 349 fprintf(stderr, "Duplicate or conflicting --png argument\n"); |
| 319 return false; | 350 return false; |
| 320 } | 351 } |
| 321 options->output_format = OUTPUT_PNG; | 352 options->output_format = OUTPUT_PNG; |
| 353 #ifdef PDF_ENABLE_SKIA | |
| 354 } else if (cur_arg == "--skp") { | |
| 355 if (options->output_format != OUTPUT_NONE) { | |
| 356 fprintf(stderr, "Duplicate or conflicting --skp argument\n"); | |
| 357 return false; | |
| 358 } | |
| 359 options->output_format = OUTPUT_SKP; | |
| 360 #endif | |
| 322 } else if (cur_arg.size() > 11 && | 361 } else if (cur_arg.size() > 11 && |
| 323 cur_arg.compare(0, 11, "--font-dir=") == 0) { | 362 cur_arg.compare(0, 11, "--font-dir=") == 0) { |
| 324 if (!options->font_directory.empty()) { | 363 if (!options->font_directory.empty()) { |
| 325 fprintf(stderr, "Duplicate --font-dir argument\n"); | 364 fprintf(stderr, "Duplicate --font-dir argument\n"); |
| 326 return false; | 365 return false; |
| 327 } | 366 } |
| 328 options->font_directory = cur_arg.substr(11); | 367 options->font_directory = cur_arg.substr(11); |
| 329 #ifdef _WIN32 | 368 #ifdef _WIN32 |
| 330 } else if (cur_arg == "--emf") { | 369 } else if (cur_arg == "--emf") { |
| 331 if (options->output_format != OUTPUT_NONE) { | 370 if (options->output_format != OUTPUT_NONE) { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 424 break; | 463 break; |
| 425 #endif | 464 #endif |
| 426 case OUTPUT_PNG: | 465 case OUTPUT_PNG: |
| 427 WritePng(name.c_str(), page_index, buffer, stride, width, height); | 466 WritePng(name.c_str(), page_index, buffer, stride, width, height); |
| 428 break; | 467 break; |
| 429 | 468 |
| 430 case OUTPUT_PPM: | 469 case OUTPUT_PPM: |
| 431 WritePpm(name.c_str(), page_index, buffer, stride, width, height); | 470 WritePpm(name.c_str(), page_index, buffer, stride, width, height); |
| 432 break; | 471 break; |
| 433 | 472 |
| 473 #ifdef PDF_ENABLE_SKIA | |
| 474 case OUTPUT_SKP: { | |
| 475 std::unique_ptr<SkPictureRecorder> recorder( | |
| 476 (SkPictureRecorder*)FPDF_RenderPageSkp(page, width, height)); | |
|
dsinclair
2016/03/14 13:53:40
nit: static_cast<>()
| |
| 477 FPDF_FFLRecord(form, recorder.get(), page, 0, 0, width, height, 0, 0); | |
| 478 WriteSkp(name.c_str(), page_index, recorder.get()); | |
| 479 } break; | |
| 480 #endif | |
| 434 default: | 481 default: |
| 435 break; | 482 break; |
| 436 } | 483 } |
| 437 | 484 |
| 438 FPDFBitmap_Destroy(bitmap); | 485 FPDFBitmap_Destroy(bitmap); |
| 439 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); | 486 FORM_DoPageAAction(page, form, FPDFPAGE_AACTION_CLOSE); |
| 440 FORM_OnBeforeClosePage(page, form); | 487 FORM_OnBeforeClosePage(page, form); |
| 441 FPDFText_ClosePage(text_page); | 488 FPDFText_ClosePage(text_page); |
| 442 FPDF_ClosePage(page); | 489 FPDF_ClosePage(page); |
| 443 return true; | 490 return true; |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 627 "Usage: pdfium_test [OPTION] [FILE]...\n" | 674 "Usage: pdfium_test [OPTION] [FILE]...\n" |
| 628 " --show-config - print build options and exit\n" | 675 " --show-config - print build options and exit\n" |
| 629 " --bin-dir=<path> - override path to v8 external data\n" | 676 " --bin-dir=<path> - override path to v8 external data\n" |
| 630 " --font-dir=<path> - override path to external fonts\n" | 677 " --font-dir=<path> - override path to external fonts\n" |
| 631 " --scale=<number> - scale output size by number (e.g. 0.5)\n" | 678 " --scale=<number> - scale output size by number (e.g. 0.5)\n" |
| 632 #ifdef _WIN32 | 679 #ifdef _WIN32 |
| 633 " --bmp - write page images <pdf-name>.<page-number>.bmp\n" | 680 " --bmp - write page images <pdf-name>.<page-number>.bmp\n" |
| 634 " --emf - write page meta files <pdf-name>.<page-number>.emf\n" | 681 " --emf - write page meta files <pdf-name>.<page-number>.emf\n" |
| 635 #endif // _WIN32 | 682 #endif // _WIN32 |
| 636 " --png - write page images <pdf-name>.<page-number>.png\n" | 683 " --png - write page images <pdf-name>.<page-number>.png\n" |
| 637 " --ppm - write page images <pdf-name>.<page-number>.ppm\n"; | 684 #ifdef PDF_ENABLE_SKIA |
| 685 " --skp - write page images <pdf-name>.<page-number>.skp\n" | |
| 686 #endif | |
| 687 ""; | |
| 638 | 688 |
| 639 int main(int argc, const char* argv[]) { | 689 int main(int argc, const char* argv[]) { |
| 640 std::vector<std::string> args(argv, argv + argc); | 690 std::vector<std::string> args(argv, argv + argc); |
| 641 Options options; | 691 Options options; |
| 642 std::list<std::string> files; | 692 std::list<std::string> files; |
| 643 if (!ParseCommandLine(args, &options, &files)) { | 693 if (!ParseCommandLine(args, &options, &files)) { |
| 644 fprintf(stderr, "%s", usage_string); | 694 fprintf(stderr, "%s", usage_string); |
| 645 return 1; | 695 return 1; |
| 646 } | 696 } |
| 647 | 697 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 699 } | 749 } |
| 700 | 750 |
| 701 FPDF_DestroyLibrary(); | 751 FPDF_DestroyLibrary(); |
| 702 #ifdef PDF_ENABLE_V8 | 752 #ifdef PDF_ENABLE_V8 |
| 703 v8::V8::ShutdownPlatform(); | 753 v8::V8::ShutdownPlatform(); |
| 704 delete platform; | 754 delete platform; |
| 705 #endif // PDF_ENABLE_V8 | 755 #endif // PDF_ENABLE_V8 |
| 706 | 756 |
| 707 return 0; | 757 return 0; |
| 708 } | 758 } |
| OLD | NEW |