| 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 "SkCanvas.h" | 8 #include "SkCanvas.h" |
| 9 #include "SkCommandLineFlags.h" | 9 #include "SkCommandLineFlags.h" |
| 10 #include "SkDevice.h" | 10 #include "SkDevice.h" |
| 11 #include "SkGraphics.h" | 11 #include "SkGraphics.h" |
| 12 #include "SkImageDecoder.h" | 12 #include "SkImageDecoder.h" |
| 13 #include "SkImageEncoder.h" | 13 #include "SkImageEncoder.h" |
| 14 #include "SkOSFile.h" | 14 #include "SkOSFile.h" |
| 15 #include "SkPdfRenderer.h" |
| 15 #include "SkPicture.h" | 16 #include "SkPicture.h" |
| 16 #include "SkStream.h" | 17 #include "SkStream.h" |
| 17 #include "SkTypeface.h" | 18 #include "SkTypeface.h" |
| 18 #include "SkTArray.h" | 19 #include "SkTArray.h" |
| 19 #include "SkNulCanvas.h" | 20 #include "SkNulCanvas.h" |
| 20 | 21 |
| 21 #if SK_SUPPORT_GPU | 22 #if SK_SUPPORT_GPU |
| 22 #include "GrContextFactory.h" | 23 #include "GrContextFactory.h" |
| 23 #include "GrContext.h" | 24 #include "GrContext.h" |
| 24 #include "SkGpuDevice.h" | 25 #include "SkGpuDevice.h" |
| 25 #endif | 26 #endif |
| 26 | 27 |
| 27 #include "SkPdfRenderer.h" | |
| 28 | |
| 29 DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to proces
s."); | 28 DEFINE_string2(readPath, r, "", "pdf files or directories of pdf files to proces
s."); |
| 30 DEFINE_string2(writePath, w, "", "Directory to write the rendered pages."); | 29 DEFINE_string2(writePath, w, "", "Directory to write the rendered pages."); |
| 31 DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one
page."); | 30 DEFINE_bool2(noExtensionForOnePagePdf, n, false, "No page extension if only one
page."); |
| 32 DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage."); | 31 DEFINE_bool2(showMemoryUsage, m, false, "Show Memory usage."); |
| 33 DEFINE_string2(pages, p, "all", "What pages to render and how:\n" | 32 DEFINE_string2(pages, p, "all", "What pages to render and how:\n" |
| 34 "\tall - all pages\n" | 33 "\tall - all pages\n" |
| 35 "\treverse - all pages, in reverse order\n" | 34 "\treverse - all pages, in reverse order\n" |
| 36 "\tfirst - first page\n" | 35 "\tfirst - first page\n" |
| 37 "\tlast - last page\n" | 36 "\tlast - last page\n" |
| 38 "\tnumber - a specific page number\n" | 37 "\tnumber - a specific page number\n" |
| 39 ); | 38 ); |
| 40 DEFINE_double(DPI, 72, "DPI to be used for rendering (scale)."); | 39 DEFINE_double(DPI, 72, "DPI to be used for rendering (scale)."); |
| 41 DEFINE_int32(benchLoad, 0, "Load the pdf file minimally N times, without any ren
dering and \n" | 40 DEFINE_int32(benchLoad, 0, "Load the pdf file minimally N times, without any ren
dering and \n" |
| 42 "\tminimal parsing to ensure correctness. Default 0 (disabled)."); | 41 "\tminimal parsing to ensure correctness. Default 0 (disabled)."); |
| 43 DEFINE_int32(benchRender, 0, "Render the pdf content N times. Default 0 (disable
d)"); | 42 DEFINE_int32(benchRender, 0, "Render the pdf content N times. Default 0 (disable
d)"); |
| 44 DEFINE_string2(config, c, "8888", "Canvas to render:\n" | 43 DEFINE_string2(config, c, "8888", "Canvas to render:\n" |
| 45 "\t8888 - argb\n" | 44 "\t8888 - argb\n" |
| 46 | |
| 47 #if SK_SUPPORT_GPU | 45 #if SK_SUPPORT_GPU |
| 48 "\tgpu: use the gpu\n" | 46 "\tgpu: use the gpu\n" |
| 49 #endif | 47 #endif |
| 50 "\tnul - render in null canvas, any draw will
just return.\n" | 48 "\tnul - render in null canvas, any draw will
just return.\n" |
| 51 ); | 49 ); |
| 52 DEFINE_bool2(transparentBackground, t, false, "Make background transparent inste
ad of white."); | 50 DEFINE_bool2(transparentBackground, t, false, "Make background transparent inste
ad of white."); |
| 53 | 51 |
| 54 | |
| 55 // TODO(edisonn): add config for device target(gpu, raster, pdf), + ability not
to render at all | |
| 56 | |
| 57 /** | 52 /** |
| 58 * Given list of directories and files to use as input, expects to find .pdf | 53 * Given list of directories and files to use as input, expects to find .pdf |
| 59 * files and it will convert them to .png files writing them in the same directo
ry | 54 * files and it will convert them to .png files writing them in the same directo
ry |
| 60 * one file for each page. | 55 * one file for each page. |
| 61 * | 56 * |
| 62 * Returns zero exit code if all .pdf files were converted successfully, | 57 * Returns zero exit code if all .pdf files were converted successfully, |
| 63 * otherwise returns error code 1. | 58 * otherwise returns error code 1. |
| 64 */ | 59 */ |
| 65 | 60 |
| 66 static const char PDF_FILE_EXTENSION[] = "pdf"; | 61 static const char PDF_FILE_EXTENSION[] = "pdf"; |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 | 191 |
| 197 SkBitmap bitmap; | 192 SkBitmap bitmap; |
| 198 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(FLAGS_
DPI / 72.0))); | 193 SkScalar width = SkScalarMul(rect.width(), SkDoubleToScalar(sqrt(FLAGS_
DPI / 72.0))); |
| 199 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(FLAG
S_DPI / 72.0))); | 194 SkScalar height = SkScalarMul(rect.height(), SkDoubleToScalar(sqrt(FLAG
S_DPI / 72.0))); |
| 200 | 195 |
| 201 rect = SkRect::MakeWH(width, height); | 196 rect = SkRect::MakeWH(width, height); |
| 202 | 197 |
| 203 SkColor background = FLAGS_transparentBackground ? SK_ColorTRANSPARENT :
SK_ColorWHITE; | 198 SkColor background = FLAGS_transparentBackground ? SK_ColorTRANSPARENT :
SK_ColorWHITE; |
| 204 | 199 |
| 205 #ifdef PDF_DEBUG_3X | 200 #ifdef PDF_DEBUG_3X |
| 206 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScala
rToDouble(height), background); | 201 setup_bitmap(&bitmap, 3 * (int)SkScalarToDouble(width), 3 * (int)SkScala
rToDouble(height), |
| 202 background); |
| 207 #else | 203 #else |
| 208 setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDoubl
e(height), background); | 204 setup_bitmap(&bitmap, (int)SkScalarToDouble(width), (int)SkScalarToDoubl
e(height), |
| 205 background); |
| 209 #endif | 206 #endif |
| 210 SkAutoTUnref<SkBaseDevice> device; | 207 SkAutoTUnref<SkBaseDevice> device; |
| 211 if (strcmp(FLAGS_config[0], "8888") == 0) { | 208 if (strcmp(FLAGS_config[0], "8888") == 0) { |
| 212 device.reset(SkNEW_ARGS(SkBitmapDevice, (bitmap))); | 209 device.reset(SkNEW_ARGS(SkBitmapDevice, (bitmap))); |
| 213 } | 210 } |
| 214 #if SK_SUPPORT_GPU | 211 #if SK_SUPPORT_GPU |
| 215 else if (strcmp(FLAGS_config[0], "gpu") == 0) { | 212 else if (strcmp(FLAGS_config[0], "gpu") == 0) { |
| 216 SkAutoTUnref<GrSurface> target; | 213 SkAutoTUnref<GrSurface> target; |
| 217 GrContext* gr = gContextFactory.get(GrContextFactory::kNative_GLCont
extType); | 214 GrContext* gr = gContextFactory.get(GrContextFactory::kNative_GLCont
extType); |
| 218 if (gr) { | 215 if (gr) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 244 gDumpCanvas = &canvas; | 241 gDumpCanvas = &canvas; |
| 245 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect); | 242 renderer.renderPage(page < 0 ? 0 : page, &canvas, rect); |
| 246 | 243 |
| 247 SkString outputPath; | 244 SkString outputPath; |
| 248 if (!make_output_filepath(&outputPath, outputDir, inputFilename, page))
{ | 245 if (!make_output_filepath(&outputPath, outputDir, inputFilename, page))
{ |
| 249 return false; | 246 return false; |
| 250 } | 247 } |
| 251 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::k
PNG_Type, 100); | 248 SkImageEncoder::EncodeFile(outputPath.c_str(), bitmap, SkImageEncoder::k
PNG_Type, 100); |
| 252 | 249 |
| 253 if (FLAGS_showMemoryUsage) { | 250 if (FLAGS_showMemoryUsage) { |
| 254 SkDebugf("Memory usage after page %i rendered: %u\n", page < 0 ? 0 :
page, (unsigned int)renderer.bytesUsed()); | 251 SkDebugf("Memory usage after page %i rendered: %u\n", |
| 252 page < 0 ? 0 : page, (unsigned int)renderer.bytesUsed()); |
| 255 } | 253 } |
| 256 } | 254 } |
| 257 return true; | 255 return true; |
| 258 } | 256 } |
| 259 | 257 |
| 260 /** Reads an skp file, renders it to pdf and writes the output to a pdf file | 258 /** Reads an skp file, renders it to pdf and writes the output to a pdf file |
| 261 * @param inputPath The skp file to be read. | 259 * @param inputPath The skp file to be read. |
| 262 * @param outputDir Output dir. | 260 * @param outputDir Output dir. |
| 263 * @param renderer The object responsible to render the skp object into pdf. | 261 * @param renderer The object responsible to render the skp object into pdf. |
| 264 */ | 262 */ |
| 265 static bool process_pdf(const SkString& inputPath, const SkString& outputDir, | 263 static bool process_pdf(const SkString& inputPath, const SkString& outputDir, |
| 266 SkPdfRenderer& renderer) { | 264 SkPdfRenderer& renderer) { |
| 267 SkDebugf("Loading PDF: %s\n", inputPath.c_str()); | 265 SkDebugf("Loading PDF: %s\n", inputPath.c_str()); |
| 268 | 266 |
| 269 SkString inputFilename; | 267 SkString inputFilename; |
| 270 get_basename(&inputFilename, inputPath); | 268 get_basename(&inputFilename, inputPath); |
| 271 | 269 |
| 272 bool success = true; | 270 bool success = true; |
| 273 | 271 |
| 274 success = renderer.load(inputPath); | 272 success = renderer.load(inputPath); |
| 275 if (FLAGS_showMemoryUsage) { | 273 if (FLAGS_showMemoryUsage) { |
| 276 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUs
ed()); | 274 SkDebugf("Memory usage after load: %u\n", (unsigned int)renderer.bytesUs
ed()); |
| 277 } | 275 } |
| 278 | 276 |
| 279 // TODO(edisonn): bench timers | 277 // TODO(edisonn): bench timers |
| 280 if (FLAGS_benchLoad > 0) { | 278 if (FLAGS_benchLoad > 0) { |
| 281 for (int i = 0 ; i < FLAGS_benchLoad; i++) { | 279 for (int i = 0 ; i < FLAGS_benchLoad; i++) { |
| 282 success = renderer.load(inputPath) && success; | 280 success = renderer.load(inputPath) && success; |
| 283 if (FLAGS_showMemoryUsage) { | 281 if (FLAGS_showMemoryUsage) { |
| 284 SkDebugf("Memory usage after load %i number : %u\n", i, (unsigne
d int)renderer.bytesUsed()); | 282 SkDebugf("Memory usage after load %i number : %u\n", i, |
| 283 (unsigned int)renderer.bytesUsed()); |
| 285 } | 284 } |
| 286 } | 285 } |
| 287 } | 286 } |
| 288 | 287 |
| 289 if (success) { | 288 if (success) { |
| 290 if (!renderer.pages()) | 289 if (!renderer.pages()) |
| 291 { | 290 { |
| 292 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str()); | 291 SkDebugf("ERROR: Empty PDF Document %s\n", inputPath.c_str()); |
| 293 return false; | 292 return false; |
| 294 } else { | 293 } else { |
| 295 for (int i = 0; i < FLAGS_benchRender + 1; i++) { | 294 for (int i = 0; i < FLAGS_benchRender + 1; i++) { |
| 296 // TODO(edisonn) if (i == 1) start timer | 295 // TODO(edisonn) if (i == 1) start timer |
| 297 if (strcmp(FLAGS_pages[0], "all") == 0) { | 296 if (strcmp(FLAGS_pages[0], "all") == 0) { |
| 298 for (int pn = 0; pn < renderer.pages(); ++pn) { | 297 for (int pn = 0; pn < renderer.pages(); ++pn) { |
| 299 success = render_page(outputDir, inputFilename, renderer
, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success; | 298 success = render_page( |
| 299 outputDir, |
| 300 inputFilename, |
| 301 renderer, |
| 302 FLAGS_noExtensionForOnePagePdf && renderer.pages
() == 1 ? -1 : |
| 303
pn) && |
| 304 success; |
| 300 } | 305 } |
| 301 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) { | 306 } else if (strcmp(FLAGS_pages[0], "reverse") == 0) { |
| 302 for (int pn = renderer.pages() - 1; pn >= 0; --pn) { | 307 for (int pn = renderer.pages() - 1; pn >= 0; --pn) { |
| 303 success = render_page(outputDir, inputFilename, renderer
, FLAGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success; | 308 success = render_page( |
| 309 outputDir, |
| 310 inputFilename, |
| 311 renderer, |
| 312 FLAGS_noExtensionForOnePagePdf && renderer.pages
() == 1 ? -1 : |
| 313
pn) && |
| 314 success; |
| 304 } | 315 } |
| 305 } else if (strcmp(FLAGS_pages[0], "first") == 0) { | 316 } else if (strcmp(FLAGS_pages[0], "first") == 0) { |
| 306 success = render_page(outputDir, inputFilename, renderer, FL
AGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : 0) && success; | 317 success = render_page( |
| 318 outputDir, |
| 319 inputFilename, |
| 320 renderer, |
| 321 FLAGS_noExtensionForOnePagePdf && renderer.pages() =
= 1 ? -1 : 0) && |
| 322 success; |
| 307 } else if (strcmp(FLAGS_pages[0], "last") == 0) { | 323 } else if (strcmp(FLAGS_pages[0], "last") == 0) { |
| 308 success = render_page(outputDir, inputFilename, renderer, FL
AGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : renderer.pages() -
1) && success; | 324 success = render_page( |
| 325 outputDir, |
| 326 inputFilename, |
| 327 renderer, |
| 328 FLAGS_noExtensionForOnePagePdf && |
| 329 renderer.pages() == 1 ? -1 : renderer.pages(
) - 1) && success; |
| 309 } else { | 330 } else { |
| 310 int pn = atoi(FLAGS_pages[0]); | 331 int pn = atoi(FLAGS_pages[0]); |
| 311 success = render_page(outputDir, inputFilename, renderer, FL
AGS_noExtensionForOnePagePdf && renderer.pages() == 1 ? -1 : pn) && success; | 332 success = render_page(outputDir, inputFilename, renderer, |
| 333 FLAGS_noExtensionForOnePagePdf && |
| 334 renderer.pages() == 1 ? -1 : pn) &
& success; |
| 312 } | 335 } |
| 313 } | 336 } |
| 314 } | 337 } |
| 315 } | 338 } |
| 316 | 339 |
| 317 if (!success) { | 340 if (!success) { |
| 318 SkDebugf("Failures for file %s\n", inputPath.c_str()); | 341 SkDebugf("Failures for file %s\n", inputPath.c_str()); |
| 319 } | 342 } |
| 320 | 343 |
| 321 return success; | 344 return success; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 } | 405 } |
| 383 | 406 |
| 384 return 0; | 407 return 0; |
| 385 } | 408 } |
| 386 | 409 |
| 387 #if !defined SK_BUILD_FOR_IOS | 410 #if !defined SK_BUILD_FOR_IOS |
| 388 int main(int argc, char * const argv[]) { | 411 int main(int argc, char * const argv[]) { |
| 389 return tool_main(argc, (char**) argv); | 412 return tool_main(argc, (char**) argv); |
| 390 } | 413 } |
| 391 #endif | 414 #endif |
| OLD | NEW |