OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 #include "SkCanvas.h" | 7 #include "SkCanvas.h" |
8 #include "SkCommandLineFlags.h" | 8 #include "SkCommandLineFlags.h" |
9 #include "SkDocument.h" | 9 #include "SkDocument.h" |
10 #include "SkForceLinking.h" | 10 #include "SkForceLinking.h" |
11 #include "SkGraphics.h" | 11 #include "SkGraphics.h" |
12 #include "SkMD5.h" | 12 #include "SkMD5.h" |
13 #include "SkOSFile.h" | 13 #include "SkOSFile.h" |
14 #include "SkPicture.h" | 14 #include "SkPicture.h" |
15 #include "SkStream.h" | 15 #include "SkStream.h" |
16 #include "SkTArray.h" | 16 #include "SkTArray.h" |
17 #include "SkTSort.h" | 17 #include "SkTSort.h" |
18 | 18 |
| 19 #include "SkDmuxWStream.h" |
| 20 |
19 static const char kUsage[] = | 21 static const char kUsage[] = |
20 "This program takes a list of Skia Picture (SKP) files and renders\n" | 22 "This program takes a list of Skia Picture (SKP) files and renders\n" |
21 "each as a multipage PDF, then prints out the MD5 checksum of the\n" | 23 "each as a multipage PDF, then prints out the MD5 checksum of the\n" |
22 "PDF file. This can be used to verify that changes to the PDF\n" | 24 "PDF file. This can be used to verify that changes to the PDF\n" |
23 "backend will not change PDF output.\n"; | 25 "backend will not change PDF output.\n"; |
24 | 26 |
25 __SK_FORCE_IMAGE_DECODER_LINKING; | 27 __SK_FORCE_IMAGE_DECODER_LINKING; |
26 | 28 |
27 DEFINE_string2(inputPaths, | 29 DEFINE_string2(inputPaths, |
28 r, | 30 r, |
29 "", | 31 "", |
30 "A list of directories and files to use as input.\n" | 32 "A list of directories and files to use as input.\n" |
31 "Files are expected to have the .skp extension."); | 33 "Files are expected to have the .skp extension."); |
32 | 34 |
| 35 DEFINE_string2(outputDirectoryPath, w, "", "TODO: document this"); |
| 36 |
33 static const char SKP_FILE_EXTENSION[] = ".skp"; | 37 static const char SKP_FILE_EXTENSION[] = ".skp"; |
| 38 static const char PDF_FILE_EXTENSION[] = ".pdf"; |
34 | 39 |
35 // Used by SkTQSort<SkString>() | 40 // Used by SkTQSort<SkString>() |
36 static bool operator<(const SkString& a, const SkString& b) { | 41 static bool operator<(const SkString& a, const SkString& b) { |
37 return strcmp(a.c_str(), b.c_str()) < 0; | 42 return strcmp(a.c_str(), b.c_str()) < 0; |
38 } | 43 } |
39 | 44 |
40 // Process --inputPaths defined on the command line. Return false if | 45 // Process --inputPaths defined on the command line. Return false if |
41 // no files found. | 46 // no files found. |
42 static bool process_input_files(SkTArray<SkString>* files) { | 47 static bool process_input_files(SkTArray<SkString>* files) { |
43 for (int i = 0; i < FLAGS_inputPaths.count(); i++) { | 48 for (int i = 0; i < FLAGS_inputPaths.count(); i++) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 if (NULL == picture.get()) { | 109 if (NULL == picture.get()) { |
105 return false; | 110 return false; |
106 } | 111 } |
107 | 112 |
108 SkMD5 checksumWStream; | 113 SkMD5 checksumWStream; |
109 picture_to_pdf(*picture, &checksumWStream); | 114 picture_to_pdf(*picture, &checksumWStream); |
110 checksumWStream.finish(*digest); | 115 checksumWStream.finish(*digest); |
111 return true; | 116 return true; |
112 } | 117 } |
113 | 118 |
| 119 static bool skp_to_pdf_and_md5(SkStream* input, |
| 120 const char* path, |
| 121 SkMD5::Digest* digest) { |
| 122 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(input)); |
| 123 if (NULL == picture.get()) { |
| 124 return false; |
| 125 } |
| 126 |
| 127 SkMD5 checksumWStream; |
| 128 SkFILEWStream fileWStream(path); |
| 129 SkWStream* wStreamArray[] = {&checksumWStream, &fileWStream}; |
| 130 SkDmuxWStream dmuxWStream(wStreamArray, SK_ARRAY_COUNT(wStreamArray)); |
| 131 |
| 132 picture_to_pdf(*picture, &dmuxWStream); |
| 133 checksumWStream.finish(*digest); |
| 134 return true; |
| 135 } |
| 136 |
114 SkString digest_to_hex(const SkMD5::Digest& digest) { | 137 SkString digest_to_hex(const SkMD5::Digest& digest) { |
115 static const char kHex[] = "0123456789ABCDEF"; | 138 static const char kHex[] = "0123456789ABCDEF"; |
116 SkString string(2 * sizeof(digest.data)); | 139 SkString string(2 * sizeof(digest.data)); |
117 char* p = string.writable_str(); | 140 char* p = string.writable_str(); |
118 for (size_t i = 0; i < sizeof(digest.data); ++i) { | 141 for (size_t i = 0; i < sizeof(digest.data); ++i) { |
119 uint8_t c = digest.data[i]; | 142 uint8_t c = digest.data[i]; |
120 *(p++) = kHex[c >> 4]; | 143 *(p++) = kHex[c >> 4]; |
121 *(p++) = kHex[c & 0xF]; | 144 *(p++) = kHex[c & 0xF]; |
122 } | 145 } |
123 return string; | 146 return string; |
124 } | 147 } |
125 | 148 |
| 149 static void str_replace_ending(SkString* str, |
| 150 const char* oldExt, |
| 151 const char* newExt) { |
| 152 SkASSERT(str->endsWith(oldExt)); |
| 153 SkASSERT(str->size() >= strlen(oldExt)); |
| 154 str->remove(str->size() - strlen(oldExt), strlen(oldExt)); |
| 155 str->append(newExt); |
| 156 } |
| 157 |
126 int main(int argc, char** argv) { | 158 int main(int argc, char** argv) { |
127 SkCommandLineFlags::SetUsage(kUsage); | 159 SkCommandLineFlags::SetUsage(kUsage); |
128 SkCommandLineFlags::Parse(argc, argv); | 160 SkCommandLineFlags::Parse(argc, argv); |
| 161 const char* outputDir = FLAGS_outputDirectoryPath.count() > 0 |
| 162 ? FLAGS_outputDirectoryPath[0] |
| 163 : NULL; |
| 164 if (outputDir) { |
| 165 sk_mkdir(outputDir); |
| 166 } |
| 167 |
129 SkAutoGraphics ag; | 168 SkAutoGraphics ag; |
130 int successCount = 0; | 169 int successCount = 0; |
131 SkTArray<SkString> files; | 170 SkTArray<SkString> files; |
132 if (!process_input_files(&files)) { | 171 if (!process_input_files(&files)) { |
133 SkDebugf("You need to specify a --inputPaths option.\n"); | 172 SkDebugf("You need to specify a --inputPaths option.\n"); |
134 return 1; | 173 return 1; |
135 } | 174 } |
136 for (int i = 0; i < files.count(); ++i) { | 175 for (int i = 0; i < files.count(); ++i) { |
137 SkString basename = SkOSPath::Basename(files[i].c_str()); | 176 SkString basename = SkOSPath::Basename(files[i].c_str()); |
138 SkFILEStream inputStream(files[i].c_str()); | 177 SkFILEStream inputStream(files[i].c_str()); |
139 if (!inputStream.isValid()) { | 178 if (!inputStream.isValid()) { |
140 SkDebugf("could_not_open %s\n", basename.c_str()); | 179 SkDebugf("could_not_open %s\n", basename.c_str()); |
141 continue; | 180 continue; |
142 } | 181 } |
143 SkMD5::Digest digest; | 182 SkMD5::Digest digest; |
144 if (!skp_to_pdf_md5(&inputStream, &digest)) { | 183 |
145 SkDebugf("invalid_skp %s\n", basename.c_str()); | 184 if (outputDir) { |
146 continue; | 185 SkString path = SkOSPath::Join(outputDir, basename.c_str()); |
| 186 str_replace_ending(&path, SKP_FILE_EXTENSION, PDF_FILE_EXTENSION); |
| 187 if (!skp_to_pdf_and_md5(&inputStream, path.c_str(), &digest)) { |
| 188 SkDebugf("invalid_skp %s\n", basename.c_str()); |
| 189 continue; |
| 190 } |
| 191 } else { |
| 192 if (!skp_to_pdf_md5(&inputStream, &digest)) { |
| 193 SkDebugf("invalid_skp %s\n", basename.c_str()); |
| 194 continue; |
| 195 } |
147 } | 196 } |
148 SkString hexDigest = digest_to_hex(digest); | 197 SkString hexDigest = digest_to_hex(digest); |
149 printf("%s %s\n", hexDigest.c_str(), basename.c_str()); | 198 printf("%s %s\n", hexDigest.c_str(), basename.c_str()); |
150 ++successCount; | 199 ++successCount; |
151 } | 200 } |
152 return successCount == files.count() ? 0 : 1; | 201 return successCount == files.count() ? 0 : 1; |
153 } | 202 } |
| 203 |
OLD | NEW |