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

Side by Side Diff: experimental/PdfViewer/pdf_viewer_main.cpp

Issue 18435007: pdf viewer: refactor and fix a bug (SkPdfobject should not reset on destruct) (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 5 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 | Annotate | Revision Log
OLDNEW
1 #include "SkCanvas.h" 1 #include "SkCanvas.h"
2 #include "SkDevice.h" 2 #include "SkDevice.h"
3 #include "SkForceLinking.h" 3 #include "SkForceLinking.h"
4 #include "SkGraphics.h" 4 #include "SkGraphics.h"
5 #include "SkImageDecoder.h" 5 #include "SkImageDecoder.h"
6 #include "SkImageEncoder.h" 6 #include "SkImageEncoder.h"
7 #include "SkOSFile.h" 7 #include "SkOSFile.h"
8 #include "SkPicture.h" 8 #include "SkPicture.h"
9 #include "SkStream.h" 9 #include "SkStream.h"
10 #include "SkTypeface.h" 10 #include "SkTypeface.h"
11 #include "SkTArray.h" 11 #include "SkTArray.h"
12 #include "picture_utils.h" 12 #include "picture_utils.h"
13 13
14 #include "SkPdfParser.h" 14 #include "SkPdfRenderer.h"
15 15
16 /** 16 /**
17 * Given list of directories and files to use as input, expects to find .pdf 17 * Given list of directories and files to use as input, expects to find .pdf
18 * files and it will convert them to .png files writing them in the same directo ry 18 * files and it will convert them to .png files writing them in the same directo ry
19 * one file for each page. 19 * one file for each page.
20 * 20 *
21 * Returns zero exit code if all .pdf files were converted successfully, 21 * Returns zero exit code if all .pdf files were converted successfully,
22 * otherwise returns error code 1. 22 * otherwise returns error code 1.
23 */ 23 */
24 24
25 static const char PDF_FILE_EXTENSION[] = "pdf"; 25 static const char PDF_FILE_EXTENSION[] = "pdf";
26 static const char PNG_FILE_EXTENSION[] = "png"; 26 static const char PNG_FILE_EXTENSION[] = "png";
27 27
28 // TODO(edisonn): add ability to write to a new directory. 28 // TODO(edisonn): add ability to write to a new directory.
29 static void usage(const char* argv0) { 29 static void usage(const char* argv0) {
30 SkDebugf("PDF to PNG rendering tool\n"); 30 SkDebugf("PDF to PNG rendering tool\n");
31 SkDebugf("\n" 31 SkDebugf("\n"
32 "Usage: \n" 32 "Usage: \n"
33 " %s <input>... -w <outputDir> \n" 33 " %s <input>... [-w <outputDir>] [-n | --no-page-ext] \n"
34 , argv0); 34 , argv0);
35 SkDebugf("\n\n"); 35 SkDebugf("\n\n");
36 SkDebugf( 36 SkDebugf(
37 " input: A list of directories and files to use as input. Files are\n" 37 " input: A list of directories and files to use as input. Files are\n"
38 " expected to have the .skp extension.\n\n"); 38 " expected to have the .skp extension.\n\n");
39 SkDebugf( 39 SkDebugf(
40 " outputDir: directory to write the rendered pdfs.\n\n"); 40 " outputDir: directory to write the rendered pdfs.\n\n");
41 SkDebugf(
42 " -n: no page extension if only one page.\n\n");
41 SkDebugf("\n"); 43 SkDebugf("\n");
42 } 44 }
43 45
44 /** Replaces the extension of a file. 46 /** Replaces the extension of a file.
45 * @param path File name whose extension will be changed. 47 * @param path File name whose extension will be changed.
46 * @param old_extension The old extension. 48 * @param old_extension The old extension.
47 * @param new_extension The new extension. 49 * @param new_extension The new extension.
48 * @returns false if the file did not has the expected extension. 50 * @returns false if the file did not has the expected extension.
49 * if false is returned, contents of path are undefined. 51 * if false is returned, contents of path are undefined.
50 */ 52 */
51 /* 53 static bool add_page_and_replace_filename_extension(SkString* path, int page,
52 static bool replace_filename_extension(SkString* path,
53 const char old_extension[], 54 const char old_extension[],
54 const char new_extension[]) { 55 const char new_extension[]) {
55 if (path->endsWith(old_extension)) { 56 if (path->endsWith(old_extension)) {
56 path->remove(path->size() - strlen(old_extension), 57 path->remove(path->size() - strlen(old_extension),
57 strlen(old_extension)); 58 strlen(old_extension));
58 if (!path->endsWith(".")) { 59 if (!path->endsWith(".")) {
59 return false; 60 return false;
60 } 61 }
62 if (page >= 0) {
63 path->appendf("%i.", page);
64 }
61 path->append(new_extension); 65 path->append(new_extension);
62 return true; 66 return true;
63 } 67 }
64 return false; 68 return false;
65 } 69 }
66 */ 70
67 /** Builds the output filename. path = dir/name, and it replaces expected 71 /** Builds the output filename. path = dir/name, and it replaces expected
68 * .skp extension with .pdf extention. 72 * .skp extension with .pdf extention.
69 * @param path Output filename. 73 * @param path Output filename.
70 * @param name The name of the file. 74 * @param name The name of the file.
71 * @returns false if the file did not has the expected extension. 75 * @returns false if the file did not has the expected extension.
72 * if false is returned, contents of path are undefined. 76 * if false is returned, contents of path are undefined.
73 */ 77 */
74 78
75 /* 79
76 static bool make_output_filepath(SkString* path, const SkString& dir, 80 static bool make_output_filepath(SkString* path, const SkString& dir,
77 const SkString& name) { 81 const SkString& name,
82 int page) {
78 sk_tools::make_filepath(path, dir, name); 83 sk_tools::make_filepath(path, dir, name);
79 return replace_filename_extension(path, 84 return add_page_and_replace_filename_extension(path, page,
80 PDF_FILE_EXTENSION, 85 PDF_FILE_EXTENSION,
81 PNG_FILE_EXTENSION); 86 PNG_FILE_EXTENSION);
82 } 87 }
83 */ 88
89 static void setup_bitmap(SkBitmap* bitmap, int width, int height, SkColor color = SK_ColorWHITE) {
90 bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
91
92 bitmap->allocPixels();
93 bitmap->eraseColor(color);
94 }
95
84 /** Write the output of pdf renderer to a file. 96 /** Write the output of pdf renderer to a file.
85 * @param outputDir Output dir. 97 * @param outputDir Output dir.
86 * @param inputFilename The skp file that was read. 98 * @param inputFilename The skp file that was read.
87 * @param renderer The object responsible to write the pdf file. 99 * @param renderer The object responsible to write the pdf file.
88 */ 100 */
89 /* 101
90 static bool write_output(const SkString& outputDir, 102 static bool render_page(const SkString& outputDir,
91 const SkString& inputFilename, 103 const SkString& inputFilename,
92 const SkPdfViewer& renderer) { 104 const SkPdfRenderer& renderer,
105 int page) {
93 if (outputDir.isEmpty()) { 106 if (outputDir.isEmpty()) {
94 SkDynamicMemoryWStream stream; 107 SkBitmap bitmap;
95 renderer.write(&stream); 108 setup_bitmap(&bitmap, 1, 1);
96 return true; 109 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
110 SkCanvas canvas(device);
111 return renderer.renderPage(page < 0 ? 0 : page, &canvas);
97 } 112 }
98 113
99 SkString outputPath; 114 SkString outputPath;
100 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) { 115 if (!make_output_filepath(&outputPath, outputDir, inputFilename, page)) {
101 return false; 116 return false;
102 } 117 }
103 118
104 SkFILEWStream stream(outputPath.c_str()); 119 SkRect rect = renderer.MediaBox(page);
105 if (!stream.isValid()) { 120
106 SkDebugf("Could not write to file %s\n", outputPath.c_str()); 121 SkBitmap bitmap;
107 return false; 122 #ifdef PDF_DEBUG_3X
108 } 123 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(rect.width()), 3 * (int)SkSc alarToDouble(rect.height()));
109 renderer.write(&stream); 124 #else
125 setup_bitmap(&bitmap, (int)SkScalarToDouble(rect.width()), (int)SkScalarToDo uble(rect.height()));
126 #endif
127 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap)));
128 SkCanvas canvas(device);
129
130 gDumpBitmap = &bitmap;
131
132 gDumpCanvas = &canvas;
133 renderer.renderPage(page, &canvas);
134
135 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_ Type, 100);
110 136
111 return true; 137 return true;
112 } 138 }
113 */ 139
114 /** Reads an skp file, renders it to pdf and writes the output to a pdf file 140 /** Reads an skp file, renders it to pdf and writes the output to a pdf file
115 * @param inputPath The skp file to be read. 141 * @param inputPath The skp file to be read.
116 * @param outputDir Output dir. 142 * @param outputDir Output dir.
117 * @param renderer The object responsible to render the skp object into pdf. 143 * @param renderer The object responsible to render the skp object into pdf.
118 */ 144 */
119 static bool parse_pdf(const SkString& inputPath, const SkString& outputDir, 145 static bool process_pdf(const SkString& inputPath, const SkString& outputDir,
120 SkPdfViewer& renderer) { 146 SkPdfRenderer& renderer, bool noPageExt) {
147 SkDebugf("Loading PDF: %s\n", inputPath.c_str());
148
121 SkString inputFilename; 149 SkString inputFilename;
122 sk_tools::get_basename(&inputFilename, inputPath); 150 sk_tools::get_basename(&inputFilename, inputPath);
123 151
124 SkFILEStream inputStream; 152 SkFILEStream inputStream;
125 inputStream.setPath(inputPath.c_str()); 153 inputStream.setPath(inputPath.c_str());
126 if (!inputStream.isValid()) { 154 if (!inputStream.isValid()) {
127 SkDebugf("Could not open file %s\n", inputPath.c_str()); 155 SkDebugf("Could not open file %s\n", inputPath.c_str());
128 return false; 156 return false;
129 } 157 }
130 158
131 bool success = false; 159 bool success = false;
132 160
133 success = renderer.load(inputPath, NULL); 161 success = renderer.load(inputPath);
134 162
163 if (success) {
164 if (!renderer.pages())
165 {
166 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str());
167 return false;
168 } else {
169 for (int pn = 0; pn < renderer.pages(); ++pn) {
170 success = render_page(outputDir, inputFilename, renderer, noPage Ext && renderer.pages() == 1 ? -1 : pn) && success;
171 }
172 }
173 }
135 174
136 // success = write_output(outputDir, inputFilename, renderer);
137
138 //renderer.end();
139 return success; 175 return success;
140 } 176 }
141 177
142 /** For each file in the directory or for the file passed in input, call 178 /** For each file in the directory or for the file passed in input, call
143 * parse_pdf. 179 * parse_pdf.
144 * @param input A directory or an pdf file. 180 * @param input A directory or an pdf file.
145 * @param outputDir Output dir. 181 * @param outputDir Output dir.
146 * @param renderer The object responsible to render the skp object into pdf. 182 * @param renderer The object responsible to render the skp object into pdf.
147 */ 183 */
148 static int process_input(const SkString& input, const SkString& outputDir, 184 static int process_input(const SkString& input, const SkString& outputDir,
149 SkPdfViewer& renderer) { 185 SkPdfRenderer& renderer, bool noPageExt) {
150 int failures = 0; 186 int failures = 0;
151 if (sk_isdir(input.c_str())) { 187 if (sk_isdir(input.c_str())) {
152 SkOSFile::Iter iter(input.c_str(), PDF_FILE_EXTENSION); 188 SkOSFile::Iter iter(input.c_str(), PDF_FILE_EXTENSION);
153 SkString inputFilename; 189 SkString inputFilename;
154 while (iter.next(&inputFilename)) { 190 while (iter.next(&inputFilename)) {
155 SkString inputPath; 191 SkString inputPath;
156 sk_tools::make_filepath(&inputPath, input, inputFilename); 192 sk_tools::make_filepath(&inputPath, input, inputFilename);
157 if (!parse_pdf(inputPath, outputDir, renderer)) { 193 if (!process_pdf(inputPath, outputDir, renderer, noPageExt)) {
158 ++failures; 194 ++failures;
159 } 195 }
160 } 196 }
161 } else { 197 } else {
162 SkString inputPath(input); 198 SkString inputPath(input);
163 if (!parse_pdf(inputPath, outputDir, renderer)) { 199 if (!process_pdf(inputPath, outputDir, renderer, noPageExt)) {
164 ++failures; 200 ++failures;
165 } 201 }
166 } 202 }
167 return failures; 203 return failures;
168 } 204 }
169 205
170 static void parse_commandline(int argc, char* const argv[], 206 static void parse_commandline(int argc, char* const argv[],
171 SkTArray<SkString>* inputs, 207 SkTArray<SkString>* inputs,
172 SkString* outputDir) { 208 SkString* outputDir, bool* noPageExt) {
173 const char* argv0 = argv[0]; 209 const char* argv0 = argv[0];
174 char* const* stop = argv + argc; 210 char* const* stop = argv + argc;
175 211
176 for (++argv; argv < stop; ++argv) { 212 for (++argv; argv < stop; ++argv) {
177 if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) { 213 if ((0 == strcmp(*argv, "-h")) || (0 == strcmp(*argv, "--help"))) {
178 usage(argv0); 214 usage(argv0);
179 exit(-1); 215 exit(-1);
216 } else if ((0 == strcmp(*argv, "-n")) || (0 == strcmp(*argv, "--no-page- ext"))) {
217 *noPageExt = true;
180 } else if (0 == strcmp(*argv, "-w")) { 218 } else if (0 == strcmp(*argv, "-w")) {
181 ++argv; 219 ++argv;
182 if (argv >= stop) { 220 if (argv >= stop) {
183 SkDebugf("Missing outputDir for -w\n"); 221 SkDebugf("Missing outputDir for -w\n");
184 usage(argv0); 222 usage(argv0);
185 exit(-1); 223 exit(-1);
186 } 224 }
187 *outputDir = SkString(*argv); 225 *outputDir = SkString(*argv);
188 } else { 226 } else {
189 inputs->push_back(SkString(*argv)); 227 inputs->push_back(SkString(*argv));
190 } 228 }
191 } 229 }
192 230
193 if (inputs->count() < 1) { 231 if (inputs->count() < 1) {
194 usage(argv0); 232 usage(argv0);
195 exit(-1); 233 exit(-1);
196 } 234 }
197 } 235 }
198 236
199 int tool_main(int argc, char** argv); 237 int tool_main(int argc, char** argv);
200 int tool_main(int argc, char** argv) { 238 int tool_main(int argc, char** argv) {
201 SkAutoGraphics ag; 239 SkAutoGraphics ag;
202 SkTArray<SkString> inputs; 240 SkTArray<SkString> inputs;
203 241
204 SkAutoTUnref<SkPdfViewer> 242 SkPdfRenderer renderer;
205 renderer(SkNEW(SkPdfViewer));
206 SkASSERT(renderer.get());
207 243
208 SkString outputDir; 244 SkString outputDir;
209 parse_commandline(argc, argv, &inputs, &outputDir); 245 bool noPageExt = false;
246 parse_commandline(argc, argv, &inputs, &outputDir, &noPageExt);
210 247
211 int failures = 0; 248 int failures = 0;
212 for (int i = 0; i < inputs.count(); i ++) { 249 for (int i = 0; i < inputs.count(); i ++) {
213 failures += process_input(inputs[i], outputDir, *renderer); 250 failures += process_input(inputs[i], outputDir, renderer, noPageExt);
251 renderer.unload();
214 } 252 }
215 253
216 reportPdfRenderStats(); 254 reportPdfRenderStats();
217 255
218 if (failures != 0) { 256 if (failures != 0) {
219 SkDebugf("Failed to render %i PDFs.\n", failures); 257 SkDebugf("Failed to render %i PDFs.\n", failures);
220 return 1; 258 return 1;
221 } 259 }
222 260
223 return 0; 261 return 0;
224 } 262 }
225 263
226 #if !defined SK_BUILD_FOR_IOS 264 #if !defined SK_BUILD_FOR_IOS
227 int main(int argc, char * const argv[]) { 265 int main(int argc, char * const argv[]) {
228 return tool_main(argc, (char**) argv); 266 return tool_main(argc, (char**) argv);
229 } 267 }
230 #endif 268 #endif
OLDNEW
« no previous file with comments | « experimental/PdfViewer/SkPdfRenderer.cpp ('k') | experimental/PdfViewer/pdfparser/native/SkNativeParsedPDF.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698