aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/svg/skp2svg.cpp
blob: ae6e54c8854ac2a209e27893a1ab4968f6cde14e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 * 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 "LazyDecodeBitmap.h"
#include "SkCommandLineFlags.h"
#include "SkPicture.h"
#include "SkStream.h"
#include "SkSVGCanvas.h"
#include "SkXMLWriter.h"

DEFINE_string2(input, i, "", "input skp file");
DEFINE_string2(output, o, "", "output svg file (optional)");

// return codes:
static const int kSuccess     = 0;
static const int kInvalidArgs = 1;
static const int kIOError     = 2;
static const int kNotAnSKP    = 3;

int tool_main(int argc, char** argv);
int tool_main(int argc, char** argv) {
    SkCommandLineFlags::SetUsage("Converts an SKP file to SVG.");
    SkCommandLineFlags::Parse(argc, argv);

    if (FLAGS_input.count() != 1) {
        SkDebugf("Missing input file\n");
        return kInvalidArgs;
    }

    SkFILEStream stream(FLAGS_input[0]);
    if (!stream.isValid()) {
        SkDebugf("Couldn't open file: %s\n", FLAGS_input[0]);
        return kIOError;
    }

    SkAutoTUnref<SkPicture> pic(SkPicture::CreateFromStream(&stream, &sk_tools::LazyDecodeBitmap));
    if (!SkToBool(pic.get())) {
        SkDebugf("Could not load SKP: %s\n", FLAGS_input[0]);
        return kNotAnSKP;
    }

    SkAutoTDelete<SkWStream> outStream;
    if (FLAGS_output.count() > 0) {
        SkFILEWStream* fileStream = SkNEW_ARGS(SkFILEWStream, (FLAGS_output[0]));
        if (!fileStream->isValid()) {
            SkDebugf("Couldn't open output file for writing: %s\n", FLAGS_output[0]);
            return kIOError;
        }
        outStream.reset(fileStream);
    } else {
        outStream.reset(SkNEW(SkDebugWStream));
    }

    SkAutoTDelete<SkXMLWriter> xmlWriter(SkNEW_ARGS(SkXMLStreamWriter, (outStream.get())));
    SkAutoTUnref<SkCanvas> svgCanvas(SkSVGCanvas::Create(pic->cullRect(), xmlWriter.get()));

    pic->playback(svgCanvas);

    return kSuccess;
}

#if !defined SK_BUILD_FOR_IOS
int main(int argc, char * const argv[]) {
    return tool_main(argc, (char**) argv);
}
#endif