| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 "SkDevice.h" | 9 #include "SkDevice.h" |
| 10 #include "SkForceLinking.h" | 10 #include "SkForceLinking.h" |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 return replace_filename_extension(path, | 124 return replace_filename_extension(path, |
| 125 SKP_FILE_EXTENSION, | 125 SKP_FILE_EXTENSION, |
| 126 PDF_FILE_EXTENSION); | 126 PDF_FILE_EXTENSION); |
| 127 } | 127 } |
| 128 | 128 |
| 129 /** Write the output of pdf renderer to a file. | 129 /** Write the output of pdf renderer to a file. |
| 130 * @param outputDir Output dir. | 130 * @param outputDir Output dir. |
| 131 * @param inputFilename The skp file that was read. | 131 * @param inputFilename The skp file that was read. |
| 132 * @param renderer The object responsible to write the pdf file. | 132 * @param renderer The object responsible to write the pdf file. |
| 133 */ | 133 */ |
| 134 static bool write_output(const SkString& outputDir, | 134 static SkWStream* open_stream(const SkString& outputDir, |
| 135 const SkString& inputFilename, | 135 const SkString& inputFilename) { |
| 136 const sk_tools::PdfRenderer& renderer) { | |
| 137 if (outputDir.isEmpty()) { | 136 if (outputDir.isEmpty()) { |
| 138 SkDynamicMemoryWStream stream; | 137 return SkNEW(SkDynamicMemoryWStream); |
| 139 renderer.write(&stream); | |
| 140 return true; | |
| 141 } | 138 } |
| 142 | 139 |
| 143 SkString outputPath; | 140 SkString outputPath; |
| 144 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) { | 141 if (!make_output_filepath(&outputPath, outputDir, inputFilename)) { |
| 145 return false; | 142 return NULL; |
| 146 } | 143 } |
| 147 | 144 |
| 148 SkFILEWStream stream(outputPath.c_str()); | 145 SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (outputPath.c_str())); |
| 149 if (!stream.isValid()) { | 146 if (!stream->isValid()) { |
| 150 SkDebugf("Could not write to file %s\n", outputPath.c_str()); | 147 SkDebugf("Could not write to file %s\n", outputPath.c_str()); |
| 151 return false; | 148 return NULL; |
| 152 } | 149 } |
| 153 renderer.write(&stream); | |
| 154 | 150 |
| 155 return true; | 151 return stream; |
| 156 } | 152 } |
| 157 | 153 |
| 158 /** Reads an skp file, renders it to pdf and writes the output to a pdf file | 154 /** Reads an skp file, renders it to pdf and writes the output to a pdf file |
| 159 * @param inputPath The skp file to be read. | 155 * @param inputPath The skp file to be read. |
| 160 * @param outputDir Output dir. | 156 * @param outputDir Output dir. |
| 161 * @param renderer The object responsible to render the skp object into pdf. | 157 * @param renderer The object responsible to render the skp object into pdf. |
| 162 */ | 158 */ |
| 163 static bool render_pdf(const SkString& inputPath, const SkString& outputDir, | 159 static bool render_pdf(const SkString& inputPath, const SkString& outputDir, |
| 164 sk_tools::PdfRenderer& renderer) { | 160 sk_tools::PdfRenderer& renderer) { |
| 165 SkString inputFilename; | 161 SkString inputFilename; |
| 166 sk_tools::get_basename(&inputFilename, inputPath); | 162 sk_tools::get_basename(&inputFilename, inputPath); |
| 167 | 163 |
| 168 SkFILEStream inputStream; | 164 SkFILEStream inputStream; |
| 169 inputStream.setPath(inputPath.c_str()); | 165 inputStream.setPath(inputPath.c_str()); |
| 170 if (!inputStream.isValid()) { | 166 if (!inputStream.isValid()) { |
| 171 SkDebugf("Could not open file %s\n", inputPath.c_str()); | 167 SkDebugf("Could not open file %s\n", inputPath.c_str()); |
| 172 return false; | 168 return false; |
| 173 } | 169 } |
| 174 | 170 |
| 175 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream)); | 171 SkAutoTUnref<SkPicture> picture(SkPicture::CreateFromStream(&inputStream)); |
| 176 | 172 |
| 177 if (NULL == picture.get()) { | 173 if (NULL == picture.get()) { |
| 178 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str()); | 174 SkDebugf("Could not read an SkPicture from %s\n", inputPath.c_str()); |
| 179 return false; | 175 return false; |
| 180 } | 176 } |
| 181 | 177 |
| 182 SkDebugf("exporting... [%i %i] %s\n", picture->width(), picture->height(), | 178 SkDebugf("exporting... [%i %i] %s\n", picture->width(), picture->height(), |
| 183 inputPath.c_str()); | 179 inputPath.c_str()); |
| 184 | 180 |
| 185 renderer.init(picture); | 181 SkWStream* stream(open_stream(outputDir, inputFilename)); |
| 186 | 182 |
| 187 renderer.render(); | 183 if (!stream) { |
| 184 return false; |
| 185 } |
| 188 | 186 |
| 189 bool success = write_output(outputDir, inputFilename, renderer); | 187 renderer.init(picture, stream); |
| 188 |
| 189 bool success = renderer.render(); |
| 190 SkDELETE(stream); |
| 190 | 191 |
| 191 renderer.end(); | 192 renderer.end(); |
| 193 |
| 192 return success; | 194 return success; |
| 193 } | 195 } |
| 194 | 196 |
| 195 /** For each file in the directory or for the file passed in input, call | 197 /** For each file in the directory or for the file passed in input, call |
| 196 * render_pdf. | 198 * render_pdf. |
| 197 * @param input A directory or an skp file. | 199 * @param input A directory or an skp file. |
| 198 * @param outputDir Output dir. | 200 * @param outputDir Output dir. |
| 199 * @param renderer The object responsible to render the skp object into pdf. | 201 * @param renderer The object responsible to render the skp object into pdf. |
| 200 */ | 202 */ |
| 201 static int process_input(const SkString& input, const SkString& outputDir, | 203 static int process_input(const SkString& input, const SkString& outputDir, |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 } | 304 } |
| 303 #endif | 305 #endif |
| 304 return 0; | 306 return 0; |
| 305 } | 307 } |
| 306 | 308 |
| 307 #if !defined SK_BUILD_FOR_IOS | 309 #if !defined SK_BUILD_FOR_IOS |
| 308 int main(int argc, char * const argv[]) { | 310 int main(int argc, char * const argv[]) { |
| 309 return tool_main(argc, (char**) argv); | 311 return tool_main(argc, (char**) argv); |
| 310 } | 312 } |
| 311 #endif | 313 #endif |
| OLD | NEW |