fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "LazyDecodeBitmap.h" |
| 9 | #include "SkCommandLineFlags.h" |
| 10 | #include "SkPicture.h" |
| 11 | #include "SkStream.h" |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame^] | 12 | #include "SkSVGCanvas.h" |
| 13 | #include "SkXMLWriter.h" |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 14 | |
| 15 | DEFINE_string2(input, i, "", "input skp file"); |
| 16 | DEFINE_string2(output, o, "", "output svg file (optional)"); |
| 17 | |
| 18 | // return codes: |
| 19 | static const int kSuccess = 0; |
| 20 | static const int kInvalidArgs = 1; |
| 21 | static const int kIOError = 2; |
| 22 | static const int kNotAnSKP = 3; |
| 23 | |
| 24 | int tool_main(int argc, char** argv); |
| 25 | int tool_main(int argc, char** argv) { |
| 26 | SkCommandLineFlags::SetUsage("Converts an SKP file to SVG."); |
| 27 | SkCommandLineFlags::Parse(argc, argv); |
| 28 | |
| 29 | if (FLAGS_input.count() != 1) { |
| 30 | SkDebugf("Missing input file\n"); |
| 31 | return kInvalidArgs; |
| 32 | } |
| 33 | |
| 34 | SkFILEStream stream(FLAGS_input[0]); |
| 35 | if (!stream.isValid()) { |
| 36 | SkDebugf("Couldn't open file: %s\n", FLAGS_input[0]); |
| 37 | return kIOError; |
| 38 | } |
| 39 | |
| 40 | SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(&stream, &sk_tools::LazyDecodeBitmap)); |
| 41 | if (!SkToBool(pic.get())) { |
| 42 | SkDebugf("Could not load SKP: %s\n", FLAGS_input[0]); |
| 43 | return kNotAnSKP; |
| 44 | } |
| 45 | |
| 46 | SkAutoTDelete<SkWStream> outStream; |
| 47 | if (FLAGS_output.count() > 0) { |
| 48 | SkFILEWStream* fileStream = SkNEW_ARGS(SkFILEWStream, (FLAGS_output[0])); |
| 49 | if (!fileStream->isValid()) { |
| 50 | SkDebugf("Couldn't open output file for writing: %s\n", FLAGS_output[0]); |
| 51 | return kIOError; |
| 52 | } |
| 53 | outStream.reset(fileStream); |
| 54 | } else { |
| 55 | outStream.reset(SkNEW(SkDebugWStream)); |
| 56 | } |
| 57 | |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame^] | 58 | SkAutoTDelete<SkXMLWriter> xmlWriter(SkNEW_ARGS(SkXMLStreamWriter, (outStream.get()))); |
| 59 | SkAutoTUnref<SkCanvas> svgCanvas(SkSVGCanvas::Create(pic->cullRect(), xmlWriter.get())); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 60 | |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame^] | 61 | pic->playback(svgCanvas); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 62 | |
| 63 | return kSuccess; |
| 64 | } |
| 65 | |
| 66 | #if !defined SK_BUILD_FOR_IOS |
| 67 | int main(int argc, char * const argv[]) { |
| 68 | return tool_main(argc, (char**) argv); |
| 69 | } |
| 70 | #endif |