OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2014 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 #include "SkCanvas.h" |
| 8 #include "SkDocument.h" |
| 9 #include "SkForceLinking.h" |
| 10 #include "SkGraphics.h" |
| 11 #include "SkNullCanvas.h" |
| 12 #include "SkPicture.h" |
| 13 #include "SkStream.h" |
| 14 #include "SkTemplates.h" |
| 15 #include "ProcStats.h" |
| 16 #include "flags/SkCommandLineFlags.h" |
| 17 |
| 18 #ifdef SK_ENABLE_NEW_SKPDF_BACKEND |
| 19 #include "skpdf.h" |
| 20 #endif |
| 21 |
| 22 DEFINE_string2(readPath, |
| 23 r, |
| 24 "", |
| 25 "(Required) The path to a .skp Skia Picture file."); |
| 26 DEFINE_string2(writePath, w, "", "If set, write PDF output to this file."); |
| 27 DEFINE_bool(newPdf, false, "Use the new PDF backend."); |
| 28 DEFINE_bool(nullCanvas, true, "Render to a SkNullCanvas as a control."); |
| 29 |
| 30 __SK_FORCE_IMAGE_DECODER_LINKING; |
| 31 |
| 32 namespace { |
| 33 class NullWStream : public SkWStream { |
| 34 public: |
| 35 NullWStream() : fBytesWritten(0) { |
| 36 } |
| 37 virtual bool write(const void*, size_t size) SK_OVERRIDE { |
| 38 fBytesWritten += size; |
| 39 return true; |
| 40 } |
| 41 virtual size_t bytesWritten() const SK_OVERRIDE { |
| 42 return fBytesWritten; |
| 43 } |
| 44 size_t fBytesWritten; |
| 45 }; |
| 46 |
| 47 SkDocument* CreatePDFDocument(SkWStream* out) { |
| 48 #ifdef SK_ENABLE_NEW_SKPDF_BACKEND |
| 49 if (FLAGS_newPdf) { |
| 50 return skpdf::CreatePDFDocument(out); |
| 51 } |
| 52 #endif |
| 53 return SkDocument::CreatePDF(out); |
| 54 } |
| 55 } // namespace |
| 56 |
| 57 int main(int argc, char** argv) { |
| 58 SkCommandLineFlags::Parse(argc, argv); |
| 59 if (FLAGS_readPath.isEmpty()) { |
| 60 SkDebugf("Error: missing requires --readPath option\n"); |
| 61 return 1; |
| 62 } |
| 63 const char* path = FLAGS_readPath[0]; |
| 64 SkFILEStream inputStream(path); |
| 65 |
| 66 if (!inputStream.isValid()) { |
| 67 SkDebugf("Could not open file %s\n", path); |
| 68 return 2; |
| 69 } |
| 70 SkAutoGraphics ag; |
| 71 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream)); |
| 72 if (NULL == picture.get()) { |
| 73 SkDebugf("Could not read an SkPicture from %s\n", path); |
| 74 return 3; |
| 75 } |
| 76 |
| 77 int width = picture->cullRect().width(); |
| 78 int height = picture->cullRect().height(); |
| 79 |
| 80 const int kLetterWidth = 612; // 8.5 * 72 |
| 81 const int kLetterHeight = 792; // 11 * 72 |
| 82 SkRect letterRect = SkRect::MakeWH(SkIntToScalar(kLetterWidth), |
| 83 SkIntToScalar(kLetterHeight)); |
| 84 |
| 85 int xPages = ((width - 1) / kLetterWidth) + 1; |
| 86 int yPages = ((height - 1) / kLetterHeight) + 1; |
| 87 |
| 88 SkAutoTDelete<SkWStream> out(SkNEW(NullWStream)); |
| 89 |
| 90 if (!FLAGS_writePath.isEmpty()) { |
| 91 SkAutoTDelete<SkFILEWStream> fileStream( |
| 92 SkNEW_ARGS(SkFILEWStream, (FLAGS_writePath[0]))); |
| 93 if (!fileStream->isValid()) { |
| 94 SkDebugf("Can't open file \"%s\" for writing.", FLAGS_writePath[0]); |
| 95 return 1; |
| 96 } |
| 97 out.reset(fileStream.detach()); |
| 98 } |
| 99 |
| 100 SkCanvas* nullCanvas = SkCreateNullCanvas(); |
| 101 |
| 102 SkAutoTUnref<SkDocument> pdfDocument; |
| 103 if (!FLAGS_nullCanvas) { |
| 104 pdfDocument.reset(CreatePDFDocument(out.get())); |
| 105 } |
| 106 |
| 107 for (int y = 0; y < yPages; ++y) { |
| 108 for (int x = 0; x < xPages; ++x) { |
| 109 SkCanvas* canvas; |
| 110 if (FLAGS_nullCanvas) { |
| 111 canvas = nullCanvas; |
| 112 } else { |
| 113 int w = SkTMin(kLetterWidth, width - (x * kLetterWidth)); |
| 114 int h = SkTMin(kLetterHeight, height - (y * kLetterHeight)); |
| 115 canvas = pdfDocument->beginPage(w, h); |
| 116 } |
| 117 { |
| 118 SkAutoCanvasRestore autoCanvasRestore(canvas, true); |
| 119 canvas->clipRect(letterRect); |
| 120 canvas->translate(SkIntToScalar(-kLetterWidth * x), |
| 121 SkIntToScalar(-kLetterHeight * y)); |
| 122 canvas->drawPicture(picture); |
| 123 } |
| 124 canvas->flush(); |
| 125 if (!FLAGS_nullCanvas) { |
| 126 pdfDocument->endPage(); |
| 127 } |
| 128 } |
| 129 } |
| 130 if (!FLAGS_nullCanvas) { |
| 131 pdfDocument->close(); |
| 132 pdfDocument.reset(NULL); |
| 133 } |
| 134 printf(SK_SIZE_T_SPECIFIER "\t%4d\n", |
| 135 inputStream.getLength(), |
| 136 sk_tools::getMaxResidentSetSizeMB()); |
| 137 return 0; |
| 138 } |
OLD | NEW |