experimental/skp_to_pdf_md5 optionally also outputs pdf files
authorhalcanary <halcanary@google.com>
Sat, 24 Jan 2015 21:04:57 +0000 (13:04 -0800)
committerCommit bot <commit-bot@chromium.org>
Sat, 24 Jan 2015 21:04:57 +0000 (13:04 -0800)
TBR=mtklein@google.com

Review URL: https://codereview.chromium.org/868333002

experimental/tools/SkDmuxWStream.cpp [new file with mode: 0644]
experimental/tools/SkDmuxWStream.h [new file with mode: 0644]
experimental/tools/skp_to_pdf_md5.cpp
gyp/experimental.gyp

diff --git a/experimental/tools/SkDmuxWStream.cpp b/experimental/tools/SkDmuxWStream.cpp
new file mode 100644 (file)
index 0000000..6dde5bf
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkDmuxWStream.h"
+
+SkDmuxWStream::SkDmuxWStream(SkWStream* const streamArray[], size_t count)
+    : fWStreams(streamArray, static_cast<int>(count)), fBytesWritten(0) {}
+
+SkDmuxWStream::~SkDmuxWStream() {
+    for (int i = 0; i < fWStreams.count(); ++i) {
+        fWStreams[i]->flush();
+    }
+}
+
+bool SkDmuxWStream::write(const void* buffer, size_t size) {
+    for (int i = 0; i < fWStreams.count(); ++i) {
+        if (!fWStreams[i]->write(buffer, size)) {
+            return false;
+        }
+    }
+    fBytesWritten += size;
+    return true;
+}
+
+void SkDmuxWStream::newline() {
+    for (int i = 0; i < fWStreams.count(); ++i) {
+        fWStreams[i]->newline();
+    }
+    fBytesWritten += 1;  // This may be a lie.
+}
+
+void SkDmuxWStream::flush() {
+    for (int i = 0; i < fWStreams.count(); ++i) {
+        fWStreams[i]->flush();
+    }
+}
+
+size_t SkDmuxWStream::bytesWritten() const { return fBytesWritten; }
diff --git a/experimental/tools/SkDmuxWStream.h b/experimental/tools/SkDmuxWStream.h
new file mode 100644 (file)
index 0000000..52376c5
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2015 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+#ifndef SkDmuxWStream_DEFINED
+#define SkDmuxWStream_DEFINED
+
+#include "SkStream.h"
+#include "SkTDArray.h"
+
+/**
+ *  A SkWStream Demultiplexer.  If initialized with
+ *      SkDmuxWStream dmuxWStream(NULL, 0);
+ *  then it becomes a /dev/null.
+ */
+class SkDmuxWStream : public SkWStream {
+public:
+    SkDmuxWStream(SkWStream* const streamArray[], size_t count);
+    ~SkDmuxWStream();
+    virtual bool write(const void* buffer, size_t size) SK_OVERRIDE;
+    virtual void newline() SK_OVERRIDE;
+    virtual void flush() SK_OVERRIDE;
+    virtual size_t bytesWritten() const SK_OVERRIDE;
+
+private:
+    SkTDArray<SkWStream*> fWStreams;
+    size_t fBytesWritten;
+};
+
+#endif  // SkDmuxWStream_DEFINED
index 3232c57ed3dd1023266660dddf04f736eef3ed5b..87f504510f7a110817d339f47c97410fd9d296d3 100644 (file)
@@ -16,6 +16,8 @@
 #include "SkTArray.h"
 #include "SkTSort.h"
 
+#include "SkDmuxWStream.h"
+
 static const char kUsage[] =
     "This program takes a list of Skia Picture (SKP) files and renders\n"
     "each as a multipage PDF, then prints out the MD5 checksum of the\n"
@@ -30,7 +32,10 @@ DEFINE_string2(inputPaths,
                "A list of directories and files to use as input.\n"
                "Files are expected to have the .skp extension.");
 
+DEFINE_string2(outputDirectoryPath, w, "", "TODO: document this");
+
 static const char SKP_FILE_EXTENSION[] = ".skp";
+static const char PDF_FILE_EXTENSION[] = ".pdf";
 
 // Used by SkTQSort<SkString>()
 static bool operator<(const SkString& a, const SkString& b) {
@@ -111,6 +116,24 @@ static bool skp_to_pdf_md5(SkStream* input, SkMD5::Digest* digest) {
     return true;
 }
 
+static bool skp_to_pdf_and_md5(SkStream* input,
+                               const char* path,
+                               SkMD5::Digest* digest) {
+    SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(input));
+    if (NULL == picture.get()) {
+        return false;
+    }
+
+    SkMD5 checksumWStream;
+    SkFILEWStream fileWStream(path);
+    SkWStream* wStreamArray[] = {&checksumWStream, &fileWStream};
+    SkDmuxWStream dmuxWStream(wStreamArray, SK_ARRAY_COUNT(wStreamArray));
+
+    picture_to_pdf(*picture, &dmuxWStream);
+    checksumWStream.finish(*digest);
+    return true;
+}
+
 SkString digest_to_hex(const SkMD5::Digest& digest) {
     static const char kHex[] = "0123456789ABCDEF";
     SkString string(2 * sizeof(digest.data));
@@ -123,9 +146,25 @@ SkString digest_to_hex(const SkMD5::Digest& digest) {
     return string;
 }
 
+static void str_replace_ending(SkString* str,
+                               const char* oldExt,
+                               const char* newExt) {
+    SkASSERT(str->endsWith(oldExt));
+    SkASSERT(str->size() >= strlen(oldExt));
+    str->remove(str->size() - strlen(oldExt), strlen(oldExt));
+    str->append(newExt);
+}
+
 int main(int argc, char** argv) {
     SkCommandLineFlags::SetUsage(kUsage);
     SkCommandLineFlags::Parse(argc, argv);
+    const char* outputDir = FLAGS_outputDirectoryPath.count() > 0
+                                    ? FLAGS_outputDirectoryPath[0]
+                                    : NULL;
+    if (outputDir) {
+        sk_mkdir(outputDir);
+    }
+
     SkAutoGraphics ag;
     int successCount = 0;
     SkTArray<SkString> files;
@@ -141,9 +180,19 @@ int main(int argc, char** argv) {
             continue;
         }
         SkMD5::Digest digest;
-        if (!skp_to_pdf_md5(&inputStream, &digest)) {
-            SkDebugf("invalid_skp %s\n", basename.c_str());
-            continue;
+
+        if (outputDir) {
+            SkString path = SkOSPath::Join(outputDir, basename.c_str());
+            str_replace_ending(&path, SKP_FILE_EXTENSION, PDF_FILE_EXTENSION);
+            if (!skp_to_pdf_and_md5(&inputStream, path.c_str(), &digest)) {
+                SkDebugf("invalid_skp %s\n", basename.c_str());
+                continue;
+            }
+        } else {
+            if (!skp_to_pdf_md5(&inputStream, &digest)) {
+                SkDebugf("invalid_skp %s\n", basename.c_str());
+                continue;
+            }
         }
         SkString hexDigest = digest_to_hex(digest);
         printf("%s %s\n", hexDigest.c_str(), basename.c_str());
@@ -151,3 +200,4 @@ int main(int argc, char** argv) {
     }
     return successCount == files.count() ? 0 : 1;
 }
+
index 8ad3bff860ee76fd499a9f55740882f1f17ee98e..6405e81576d0a13dfab9bca0a38672a49ca961f0 100644 (file)
@@ -93,6 +93,7 @@
       'type': 'executable',
       'sources': [
         '../experimental/tools/skp_to_pdf_md5.cpp',
+        '../experimental/tools/SkDmuxWStream.cpp',
       ],
       'include_dirs': [
         '../src/core',