| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmapDevice.h" | 8 #include "SkBitmapDevice.h" |
| 9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkCommandLineFlags.h" | 10 #include "SkCommandLineFlags.h" |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 SkDebugf("Memory usage after page %i rendered: %u\n", | 207 SkDebugf("Memory usage after page %i rendered: %u\n", |
| 208 page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed()); | 208 page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed()); |
| 209 } | 209 } |
| 210 } | 210 } |
| 211 return true; | 211 return true; |
| 212 } | 212 } |
| 213 | 213 |
| 214 /** Reads an skp file, renders it to pdf and writes the output to a pdf file | 214 /** Reads an skp file, renders it to pdf and writes the output to a pdf file |
| 215 * @param inputPath The skp file to be read. | 215 * @param inputPath The skp file to be read. |
| 216 * @param outputDir Output dir. | 216 * @param outputDir Output dir. |
| 217 * @param renderer The object responsible to render the skp object into pdf. | |
| 218 */ | 217 */ |
| 219 static bool process_pdf(const SkString& inputPath, const SkString& outputDir, | 218 static bool process_pdf(const SkString& inputPath, const SkString& outputDir) { |
| 220 SkPdfRenderer& renderer) { | |
| 221 SkDebugf("Loading PDF: %s\n", inputPath.c_str()); | 219 SkDebugf("Loading PDF: %s\n", inputPath.c_str()); |
| 222 | 220 |
| 223 SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str()); | 221 SkString inputFilename = SkOSPath::SkBasename(inputPath.c_str()); |
| 224 | 222 |
| 225 bool success = true; | 223 SkAutoTDelete<SkPdfRenderer> renderer(SkPdfRenderer::CreateFromFile(inputPat
h.c_str())); |
| 224 if (NULL == renderer.get()) { |
| 225 SkDebugf("Failure loading file %s\n", inputPath.c_str()); |
| 226 return false; |
| 227 } |
| 226 | 228 |
| 227 success = renderer.load(inputPath); | |
| 228 if (FLAGS_showMemoryUsage) { | 229 if (FLAGS_showMemoryUsage) { |
| 229 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUs
ed()); | 230 SkDebugf("Memory usage after load: %u\n", (unsigned int) renderer->bytes
Used()); |
| 230 } | 231 } |
| 231 | 232 |
| 232 // TODO(edisonn): bench timers | 233 // TODO(edisonn): bench timers |
| 233 if (FLAGS_benchLoad > 0) { | 234 if (FLAGS_benchLoad > 0) { |
| 234 for (int i = 0 ; i < FLAGS_benchLoad; i++) { | 235 for (int i = 0 ; i < FLAGS_benchLoad; i++) { |
| 235 success = renderer.load(inputPath) && success; | 236 SkAutoTDelete<SkPdfRenderer> benchRenderer( |
| 236 if (FLAGS_showMemoryUsage) { | 237 SkPdfRenderer::CreateFromFile(inputPath.c_str())); |
| 238 if (NULL == benchRenderer.get()) { |
| 239 SkDebugf("Failed to load on %ith attempt\n", i); |
| 240 } else if (FLAGS_showMemoryUsage) { |
| 237 SkDebugf("Memory usage after load %i number : %u\n", i, | 241 SkDebugf("Memory usage after load %i number : %u\n", i, |
| 238 (unsigned int)renderer.bytesUsed()); | 242 (unsigned int) benchRenderer->bytesUsed()); |
| 239 } | 243 } |
| 240 } | 244 } |
| 241 } | 245 } |
| 242 | 246 |
| 243 if (success) { | 247 if (!renderer->pages()) { |
| 244 if (!renderer.pages()) | 248 // This should never happen, since CreateFromFile will return NULL if th
ere are no pages. |
| 245 { | 249 SkASSERT(false); |
| 246 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str()); | 250 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str()); |
| 247 return false; | 251 return false; |
| 252 } |
| 253 |
| 254 bool success = true; |
| 255 for (int i = 0; i < FLAGS_benchRender + 1; i++) { |
| 256 // TODO(edisonn) if (i == 1) start timer |
| 257 if (strcmp(FLAGS_pages[0], "all") == 0) { |
| 258 for (int pn = 0; pn < renderer->pages(); ++pn) { |
| 259 success &= render_page(outputDir, inputFilename, *renderer, |
| 260 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1
? -1 : pn); |
| 261 } |
| 262 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) { |
| 263 for (int pn = renderer->pages() - 1; pn >= 0; --pn) { |
| 264 success &= render_page(outputDir, inputFilename, *renderer, |
| 265 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1
? -1 : pn); |
| 266 } |
| 267 } else if (strcmp(FLAGS_pages[0], "first") == 0) { |
| 268 success &= render_page(outputDir, inputFilename, *renderer, |
| 269 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -
1 : 0); |
| 270 } else if (strcmp(FLAGS_pages[0], "last") == 0) { |
| 271 success &= render_page(outputDir, inputFilename, *renderer, |
| 272 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -
1 |
| 273 : renderer->pages() - 1); |
| 248 } else { | 274 } else { |
| 249 for (int i = 0; i < FLAGS_benchRender + 1; i++) { | 275 int pn = atoi(FLAGS_pages[0]); |
| 250 // TODO(edisonn) if (i == 1) start timer | 276 success &= render_page(outputDir, inputFilename, *renderer, |
| 251 if (strcmp(FLAGS_pages[0], "all") == 0) { | 277 FLAGS_noExtensionForOnePagePdf && renderer->pages() == 1 ? -
1 : pn); |
| 252 for (int pn = 0; pn < renderer.pages(); ++pn) { | |
| 253 success = render_page( | |
| 254 outputDir, | |
| 255 inputFilename, | |
| 256 renderer, | |
| 257 FLAGS_noExtensionForOnePagePdf && renderer.pages
() == 1 ? -1 : | |
| 258
pn) && | |
| 259 success; | |
| 260 } | |
| 261 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) { | |
| 262 for (int pn = renderer.pages() - 1; pn >= 0; --pn) { | |
| 263 success = render_page( | |
| 264 outputDir, | |
| 265 inputFilename, | |
| 266 renderer, | |
| 267 FLAGS_noExtensionForOnePagePdf && renderer.pages
() == 1 ? -1 : | |
| 268
pn) && | |
| 269 success; | |
| 270 } | |
| 271 } else if (strcmp(FLAGS_pages[0], "first") == 0) { | |
| 272 success = render_page( | |
| 273 outputDir, | |
| 274 inputFilename, | |
| 275 renderer, | |
| 276 FLAGS_noExtensionForOnePagePdf && renderer.pages() =
= 1 ? -1 : 0) && | |
| 277 success; | |
| 278 } else if (strcmp(FLAGS_pages[0], "last") == 0) { | |
| 279 success = render_page( | |
| 280 outputDir, | |
| 281 inputFilename, | |
| 282 renderer, | |
| 283 FLAGS_noExtensionForOnePagePdf && | |
| 284 renderer.pages() == 1 ? -1 : renderer.pages(
) - 1) && success; | |
| 285 } else { | |
| 286 int pn = atoi(FLAGS_pages[0]); | |
| 287 success = render_page(outputDir, inputFilename, renderer, | |
| 288 FLAGS_noExtensionForOnePagePdf && | |
| 289 renderer.pages() == 1 ? -1 : pn) &
& success; | |
| 290 } | |
| 291 } | |
| 292 } | 278 } |
| 293 } | 279 } |
| 294 | 280 |
| 295 if (!success) { | 281 if (!success) { |
| 296 SkDebugf("Failures for file %s\n", inputPath.c_str()); | 282 SkDebugf("Failures for file %s\n", inputPath.c_str()); |
| 297 } | 283 } |
| 298 | 284 |
| 299 return success; | 285 return success; |
| 300 } | 286 } |
| 301 | 287 |
| 302 /** For each file in the directory or for the file passed in input, call | 288 /** For each file in the directory or for the file passed in input, call |
| 303 * parse_pdf. | 289 * parse_pdf. |
| 304 * @param input A directory or an pdf file. | 290 * @param input A directory or an pdf file. |
| 305 * @param outputDir Output dir. | 291 * @param outputDir Output dir. |
| 306 * @param renderer The object responsible to render the skp object into pdf. | |
| 307 */ | 292 */ |
| 308 static int process_input(const char* input, const SkString& outputDir, | 293 static int process_input(const char* input, const SkString& outputDir) { |
| 309 SkPdfRenderer& renderer) { | |
| 310 int failures = 0; | 294 int failures = 0; |
| 311 if (sk_isdir(input)) { | 295 if (sk_isdir(input)) { |
| 312 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION); | 296 SkOSFile::Iter iter(input, PDF_FILE_EXTENSION); |
| 313 SkString inputFilename; | 297 SkString inputFilename; |
| 314 while (iter.next(&inputFilename)) { | 298 while (iter.next(&inputFilename)) { |
| 315 SkString inputPath = SkOSPath::SkPathJoin(input, inputFilename.c_str
()); | 299 SkString inputPath = SkOSPath::SkPathJoin(input, inputFilename.c_str
()); |
| 316 if (!process_pdf(inputPath, outputDir, renderer)) { | 300 if (!process_pdf(inputPath, outputDir)) { |
| 317 ++failures; | 301 ++failures; |
| 318 } | 302 } |
| 319 } | 303 } |
| 320 } else { | 304 } else { |
| 321 SkString inputPath(input); | 305 SkString inputPath(input); |
| 322 if (!process_pdf(inputPath, outputDir, renderer)) { | 306 if (!process_pdf(inputPath, outputDir)) { |
| 323 ++failures; | 307 ++failures; |
| 324 } | 308 } |
| 325 } | 309 } |
| 326 return failures; | 310 return failures; |
| 327 } | 311 } |
| 328 | 312 |
| 329 int tool_main(int argc, char** argv); | 313 int tool_main(int argc, char** argv); |
| 330 int tool_main(int argc, char** argv) { | 314 int tool_main(int argc, char** argv) { |
| 331 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer)."); | 315 SkCommandLineFlags::SetUsage("Parse and Render .pdf files (pdf viewer)."); |
| 332 SkCommandLineFlags::Parse(argc, argv); | 316 SkCommandLineFlags::Parse(argc, argv); |
| 333 | 317 |
| 334 if (FLAGS_readPath.isEmpty()) { | 318 if (FLAGS_readPath.isEmpty()) { |
| 335 SkDebugf(".pdf files or directories are required.\n"); | 319 SkDebugf(".pdf files or directories are required.\n"); |
| 336 exit(-1); | 320 exit(-1); |
| 337 } | 321 } |
| 338 | 322 |
| 339 SkPdfRenderer renderer; | |
| 340 | |
| 341 SkString outputDir; | 323 SkString outputDir; |
| 342 if (FLAGS_writePath.count() == 1) { | 324 if (FLAGS_writePath.count() == 1) { |
| 343 outputDir.set(FLAGS_writePath[0]); | 325 outputDir.set(FLAGS_writePath[0]); |
| 344 } | 326 } |
| 345 | 327 |
| 346 int failures = 0; | 328 int failures = 0; |
| 347 for (int i = 0; i < FLAGS_readPath.count(); i ++) { | 329 for (int i = 0; i < FLAGS_readPath.count(); i ++) { |
| 348 failures += process_input(FLAGS_readPath[i], outputDir, renderer); | 330 failures += process_input(FLAGS_readPath[i], outputDir); |
| 349 renderer.unload(); | |
| 350 } | 331 } |
| 351 | 332 |
| 352 reportPdfRenderStats(); | 333 reportPdfRenderStats(); |
| 353 | 334 |
| 354 if (failures != 0) { | 335 if (failures != 0) { |
| 355 SkDebugf("Failed to render %i PDFs.\n", failures); | 336 SkDebugf("Failed to render %i PDFs.\n", failures); |
| 356 return 1; | 337 return 1; |
| 357 } | 338 } |
| 358 | 339 |
| 359 return 0; | 340 return 0; |
| 360 } | 341 } |
| 361 | 342 |
| 362 #if !defined SK_BUILD_FOR_IOS | 343 #if !defined SK_BUILD_FOR_IOS |
| 363 int main(int argc, char * const argv[]) { | 344 int main(int argc, char * const argv[]) { |
| 364 return tool_main(argc, (char**) argv); | 345 return tool_main(argc, (char**) argv); |
| 365 } | 346 } |
| 366 #endif | 347 #endif |
| OLD | NEW |