OLD | NEW |
1 #include "SkCanvas.h" | 1 #include "SkCanvas.h" |
2 #include "SkCommandLineFlags.h" | 2 #include "SkCommandLineFlags.h" |
3 #include "SkDevice.h" | 3 #include "SkDevice.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 "SkPdfRenderer.h" | 14 #include "SkPdfRenderer.h" |
15 | 15 |
16 DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to proces
s."); | 16 DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to proces
s."); |
17 DEFINE_string2(writePath, w, "", "Directory to write the rendered pages."); | 17 DEFINE_string2(writePath, w, "", "Directory to write the rendered pages."); |
18 DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one
page."); | 18 DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one
page."); |
19 DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage."); | 19 DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage."); |
| 20 DEFINE_string2(pages, p, "all", "What pages to render and how:\n" |
| 21 "\tall - all pages\n" |
| 22 "\treverse - all pages, in reverse order\n" |
| 23 "\tfirst - first page\n" |
| 24 "\tlast - last page\n" |
| 25 "\tnumber - a specific page number\n" |
| 26 ); |
| 27 |
20 | 28 |
21 /** | 29 /** |
22 * Given list of directories and files to use as input, expects to find .pdf | 30 * Given list of directories and files to use as input, expects to find .pdf |
23 * files and it will convert them to .png files writing them in the same directo
ry | 31 * files and it will convert them to .png files writing them in the same directo
ry |
24 * one file for each page. | 32 * one file for each page. |
25 * | 33 * |
26 * Returns zero exit code if all .pdf files were converted successfully, | 34 * Returns zero exit code if all .pdf files were converted successfully, |
27 * otherwise returns error code 1. | 35 * otherwise returns error code 1. |
28 */ | 36 */ |
29 | 37 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap))); | 123 SkAutoTUnref<SkDevice> device(SkNEW_ARGS(SkDevice, (bitmap))); |
116 SkCanvas canvas(device); | 124 SkCanvas canvas(device); |
117 | 125 |
118 gDumpBitmap = &bitmap; | 126 gDumpBitmap = &bitmap; |
119 | 127 |
120 gDumpCanvas = &canvas; | 128 gDumpCanvas = &canvas; |
121 renderer.renderPage(page < 0 ? 0 : page, &canvas); | 129 renderer.renderPage(page < 0 ? 0 : page, &canvas); |
122 | 130 |
123 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_
Type, 100); | 131 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::kPNG_
Type, 100); |
124 | 132 |
| 133 if (FLAGS_showMemoryUsage) { |
| 134 SkDebugf("Memory usage after page %i rendered: %u\n", page < 0 ? 0 : pag
e, (unsigned int)renderer.bytesUsed()); |
| 135 } |
| 136 |
125 return true; | 137 return true; |
126 } | 138 } |
127 | 139 |
128 /** 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 |
129 * @param inputPath The skp file to be read. | 141 * @param inputPath The skp file to be read. |
130 * @param outputDir Output dir. | 142 * @param outputDir Output dir. |
131 * @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. |
132 */ | 144 */ |
133 static bool process_pdf(const SkString& inputPath, const SkString& outputDir, | 145 static bool process_pdf(const SkString& inputPath, const SkString& outputDir, |
134 SkPdfRenderer& renderer, bool noPageExt, bool showMemory
Usage) { | 146 SkPdfRenderer& renderer) { |
135 SkDebugf("Loading PDF: %s\n", inputPath.c_str()); | 147 SkDebugf("Loading PDF: %s\n", inputPath.c_str()); |
136 | 148 |
137 SkString inputFilename; | 149 SkString inputFilename; |
138 sk_tools::get_basename(&inputFilename, inputPath); | 150 sk_tools::get_basename(&inputFilename, inputPath); |
139 | 151 |
140 SkFILEStream inputStream; | 152 SkFILEStream inputStream; |
141 inputStream.setPath(inputPath.c_str()); | 153 inputStream.setPath(inputPath.c_str()); |
142 if (!inputStream.isValid()) { | 154 if (!inputStream.isValid()) { |
143 SkDebugf("Could not open file %s\n", inputPath.c_str()); | 155 SkDebugf("Could not open file %s\n", inputPath.c_str()); |
144 return false; | 156 return false; |
145 } | 157 } |
146 | 158 |
147 bool success = false; | 159 bool success = false; |
148 | 160 |
149 success = renderer.load(inputPath); | 161 success = renderer.load(inputPath); |
150 | 162 |
151 if (success) { | 163 if (success) { |
152 if (showMemoryUsage) { | 164 if (FLAGS_showMemoryUsage) { |
153 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.byt
esUsed()); | 165 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.byt
esUsed()); |
154 } | 166 } |
155 if (!renderer.pages()) | 167 if (!renderer.pages()) |
156 { | 168 { |
157 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str()); | 169 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str()); |
158 return false; | 170 return false; |
159 } else { | 171 } else { |
160 for (int pn = 0; pn < renderer.pages(); ++pn) { | 172 if (strcmp(FLAGS_pages[0], "all") == 0) { |
161 success = render_page(outputDir, inputFilename, renderer, noPage
Ext && renderer.pages() == 1 ? -1 : pn) && success; | 173 for (int pn = 0; pn < renderer.pages(); ++pn) { |
162 SkDebugf("Memory usage after page %i rendered: %u\n", pn, (unsig
ned int)renderer.bytesUsed()); | 174 success = render_page(outputDir, inputFilename, renderer, FL
AGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success; |
| 175 } |
| 176 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) { |
| 177 for (int pn = renderer.pages() - 1; pn >= 0; --pn) { |
| 178 success = render_page(outputDir, inputFilename, renderer, FL
AGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success; |
| 179 } |
| 180 } else if (strcmp(FLAGS_pages[0], "first") == 0) { |
| 181 success = render_page(outputDir, inputFilename, renderer, FLAGS_
noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : 0) && success; |
| 182 } else if (strcmp(FLAGS_pages[0], "last") == 0) { |
| 183 success = render_page(outputDir, inputFilename, renderer, FLAGS_
noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) &
& success; |
| 184 } else { |
| 185 int pn = atoi(FLAGS_pages[0]); |
| 186 success = render_page(outputDir, inputFilename, renderer, FLAGS_
noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() - 1) &
& pn; |
163 } | 187 } |
164 } | 188 } |
165 } | 189 } |
166 | 190 |
167 return success; | 191 return success; |
168 } | 192 } |
169 | 193 |
170 /** For each file in the directory or for the file passed in input, call | 194 /** For each file in the directory or for the file passed in input, call |
171 * parse_pdf. | 195 * parse_pdf. |
172 * @param input A directory or an pdf file. | 196 * @param input A directory or an pdf file. |
173 * @param outputDir Output dir. | 197 * @param outputDir Output dir. |
174 * @param renderer The object responsible to render the skp object into pdf. | 198 * @param renderer The object responsible to render the skp object into pdf. |
175 */ | 199 */ |
176 static int process_input(const char* input, const SkString& outputDir, | 200 static int process_input(const char* input, const SkString& outputDir, |
177 SkPdfRenderer& renderer, bool noPageExt, bool showMemor
yUsage) { | 201 SkPdfRenderer& renderer) { |
178 int failures = 0; | 202 int failures = 0; |
179 if (sk_isdir(input)) { | 203 if (sk_isdir(input)) { |
180 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION); | 204 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION); |
181 SkString inputFilename; | 205 SkString inputFilename; |
182 while (iter.next(&inputFilename)) { | 206 while (iter.next(&inputFilename)) { |
183 SkString inputPath; | 207 SkString inputPath; |
184 SkString _input; | 208 SkString _input; |
185 _input.append(input); | 209 _input.append(input); |
186 sk_tools::make_filepath(&inputPath, _input, inputFilename); | 210 sk_tools::make_filepath(&inputPath, _input, inputFilename); |
187 if (!process_pdf(inputPath, outputDir, renderer, noPageExt, showMemo
ryUsage)) { | 211 if (!process_pdf(inputPath, outputDir, renderer)) { |
188 ++failures; | 212 ++failures; |
189 } | 213 } |
190 } | 214 } |
191 } else { | 215 } else { |
192 SkString inputPath(input); | 216 SkString inputPath(input); |
193 if (!process_pdf(inputPath, outputDir, renderer, noPageExt, showMemoryUs
age)) { | 217 if (!process_pdf(inputPath, outputDir, renderer)) { |
194 ++failures; | 218 ++failures; |
195 } | 219 } |
196 } | 220 } |
197 return failures; | 221 return failures; |
198 } | 222 } |
199 | 223 |
200 int tool_main(int argc, char** argv); | 224 int tool_main(int argc, char** argv); |
201 int tool_main(int argc, char** argv) { | 225 int tool_main(int argc, char** argv) { |
202 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer)."); | 226 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer)."); |
203 SkCommandLineFlags::Parse(argc, argv); | 227 SkCommandLineFlags::Parse(argc, argv); |
204 | 228 |
205 if (FLAGS_readPath.isEmpty()) { | 229 if (FLAGS_readPath.isEmpty()) { |
206 SkDebugf(".pdf files or directories are required.\n"); | 230 SkDebugf(".pdf files or directories are required.\n"); |
207 exit(-1); | 231 exit(-1); |
208 } | 232 } |
209 | 233 |
210 SkPdfRenderer renderer; | 234 SkPdfRenderer renderer; |
211 | 235 |
212 SkString outputDir; | 236 SkString outputDir; |
213 if (FLAGS_writePath.count() == 1) { | 237 if (FLAGS_writePath.count() == 1) { |
214 outputDir.set(FLAGS_writePath[0]); | 238 outputDir.set(FLAGS_writePath[0]); |
215 } | 239 } |
216 | 240 |
217 int failures = 0; | 241 int failures = 0; |
218 for (int i = 0; i < FLAGS_readPath.count(); i ++) { | 242 for (int i = 0; i < FLAGS_readPath.count(); i ++) { |
219 failures += process_input(FLAGS_readPath[i], outputDir, renderer, | 243 failures += process_input(FLAGS_readPath[i], outputDir, renderer); |
220 FLAGS_noExtensionForOnePagePdf, | |
221 FLAGS_showMemoryUsage); | |
222 renderer.unload(); | 244 renderer.unload(); |
223 } | 245 } |
224 | 246 |
225 reportPdfRenderStats(); | 247 reportPdfRenderStats(); |
226 | 248 |
227 if (failures != 0) { | 249 if (failures != 0) { |
228 SkDebugf("Failed to render %i PDFs.\n", failures); | 250 SkDebugf("Failed to render %i PDFs.\n", failures); |
229 return 1; | 251 return 1; |
230 } | 252 } |
231 | 253 |
232 return 0; | 254 return 0; |
233 } | 255 } |
234 | 256 |
235 #if !defined SK_BUILD_FOR_IOS | 257 #if !defined SK_BUILD_FOR_IOS |
236 int main(int argc, char * const argv[]) { | 258 int main(int argc, char * const argv[]) { |
237 return tool_main(argc, (char**) argv); | 259 return tool_main(argc, (char**) argv); |
238 } | 260 } |
239 #endif | 261 #endif |
OLD | NEW |