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

Side by Side Diff: samples/pdfium_test.cc

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

Powered by Google App Engine
This is Rietveld 408576698