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