2 * Copyright 2014 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkDocument.h"
9 #include "SkForceLinking.h"
10 #include "SkGraphics.h"
11 #include "SkNullCanvas.h"
12 #include "SkPicture.h"
14 #include "SkTemplates.h"
15 #include "PageCachingDocument.h"
16 #include "ProcStats.h"
17 #include "flags/SkCommandLineFlags.h"
19 DEFINE_string2(readPath,
22 "(Required) The path to a .skp Skia Picture file.");
23 DEFINE_string2(writePath, w, "", "If set, write PDF output to this file.");
24 DEFINE_bool(cachePages, false, "Use a PageCachingDocument.");
25 DEFINE_bool(nullCanvas, true, "Render to a SkNullCanvas as a control.");
27 __SK_FORCE_IMAGE_DECODER_LINKING;
30 class NullWStream : public SkWStream {
32 NullWStream() : fBytesWritten(0) {
34 bool write(const void*, size_t size) SK_OVERRIDE {
35 fBytesWritten += size;
38 size_t bytesWritten() const SK_OVERRIDE {
44 SkDocument* CreatePDFDocument(SkWStream* out) {
45 if (FLAGS_cachePages) {
46 return CreatePageCachingDocument(out);
48 return SkDocument::CreatePDF(out);
53 int main(int argc, char** argv) {
54 SkCommandLineFlags::Parse(argc, argv);
55 if (FLAGS_readPath.isEmpty()) {
56 SkDebugf("Error: missing requires --readPath option\n");
59 const char* path = FLAGS_readPath[0];
60 SkFILEStream inputStream(path);
62 if (!inputStream.isValid()) {
63 SkDebugf("Could not open file %s\n", path);
67 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream));
68 if (NULL == picture.get()) {
69 SkDebugf("Could not read an SkPicture from %s\n", path);
73 int width = picture->cullRect().width();
74 int height = picture->cullRect().height();
76 const int kLetterWidth = 612; // 8.5 * 72
77 const int kLetterHeight = 792; // 11 * 72
78 SkRect letterRect = SkRect::MakeWH(SkIntToScalar(kLetterWidth),
79 SkIntToScalar(kLetterHeight));
81 int xPages = ((width - 1) / kLetterWidth) + 1;
82 int yPages = ((height - 1) / kLetterHeight) + 1;
84 SkAutoTDelete<SkWStream> out(SkNEW(NullWStream));
86 if (!FLAGS_writePath.isEmpty()) {
87 SkAutoTDelete<SkFILEWStream> fileStream(
88 SkNEW_ARGS(SkFILEWStream, (FLAGS_writePath[0])));
89 if (!fileStream->isValid()) {
90 SkDebugf("Can't open file \"%s\" for writing.", FLAGS_writePath[0]);
93 out.reset(fileStream.detach());
96 SkCanvas* nullCanvas = SkCreateNullCanvas();
98 SkAutoTUnref<SkDocument> pdfDocument;
99 if (!FLAGS_nullCanvas) {
100 pdfDocument.reset(CreatePDFDocument(out.get()));
103 for (int y = 0; y < yPages; ++y) {
104 for (int x = 0; x < xPages; ++x) {
106 if (FLAGS_nullCanvas) {
109 int w = SkTMin(kLetterWidth, width - (x * kLetterWidth));
110 int h = SkTMin(kLetterHeight, height - (y * kLetterHeight));
111 canvas = pdfDocument->beginPage(w, h);
114 SkAutoCanvasRestore autoCanvasRestore(canvas, true);
115 canvas->clipRect(letterRect);
116 canvas->translate(SkIntToScalar(-kLetterWidth * x),
117 SkIntToScalar(-kLetterHeight * y));
118 picture->playback(canvas);
119 //canvas->drawPicture(picture);
122 if (!FLAGS_nullCanvas) {
123 pdfDocument->endPage();
127 if (!FLAGS_nullCanvas) {
128 pdfDocument->close();
129 pdfDocument.reset(NULL);
131 printf(SK_SIZE_T_SPECIFIER "\t%4d\n",
132 inputStream.getLength(),
133 sk_tools::getMaxResidentSetSizeMB());