1/*
2 * Copyright 2016 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 <iostream>
9
10#include "SkDebugCanvas.h"
11#include "SkNullCanvas.h"
12#include "SkStream.h"
13
14#ifdef SK_BUILD_FOR_WIN
15#include <fcntl.h>
16#include <io.h>
17#endif
18
19/*
20If you execute skp_parser with one argument, it spits out a json representation
21of the skp, but that's incomplete since it's missing many binary blobs (these
22could represent images or typefaces or just anything that doesn't currently
23have a json representation).  Each unique blob is labeled with a string in the
24form "data/%d".  So for example:
25
26    tools/git-sync-deps
27    bin/gn gen out/debug
28    ninja -C out/debug dm skp_parser
29    out/debug/dm -m grayscale -w /tmp/dm --config skp
30    out/debug/skp_parser /tmp/dm/skp/gm/grayscalejpg.skp | less
31    out/debug/skp_parser /tmp/dm/skp/gm/grayscalejpg.skp | grep data
32    out/debug/skp_parser /tmp/dm/skp/gm/grayscalejpg.skp data/0 | file -
33    out/debug/skp_parser /tmp/dm/skp/gm/grayscalejpg.skp data/0 > /tmp/data0.png
34
35"data/0" is an image that the SKP serializer has encoded as PNG.
36*/
37int main(int argc, char** argv) {
38    if (argc < 2) {
39        SkDebugf("Usage:\n  %s SKP_FILE [DATA_URL]\n", argv[0]);
40        return 1;
41    }
42    SkFILEStream input(argv[1]);
43    if (!input.isValid()) {
44        SkDebugf("Bad file: '%s'\n", argv[1]);
45        return 2;
46    }
47    sk_sp<SkPicture> pic = SkPicture::MakeFromStream(&input);
48    if (!pic) {
49        SkDebugf("Bad skp: '%s'\n", argv[1]);
50        return 3;
51    }
52    SkISize size = pic->cullRect().roundOut().size();
53    SkDebugCanvas debugCanvas(size.width(), size.height());
54    pic->playback(&debugCanvas);
55    std::unique_ptr<SkCanvas> nullCanvas = SkMakeNullCanvas();
56    UrlDataManager dataManager(SkString("data"));
57    Json::Value json = debugCanvas.toJSON(
58            dataManager, debugCanvas.getSize(), nullCanvas.get());
59    if (argc > 2) {
60        if (UrlDataManager::UrlData* data =
61            dataManager.getDataFromUrl(SkString(argv[2]))) {
62            SkData* skdata = data->fData.get();
63            SkASSERT(skdata);
64            #ifdef SK_BUILD_FOR_WIN
65            fflush(stdout);
66            (void)_setmode(_fileno(stdout), _O_BINARY);
67            #endif
68            fwrite(skdata->data(), skdata->size(), 1, stdout);
69        } else {
70            SkDebugf("Bad data url.\n");
71            return 4;
72        }
73    } else {
74        Json::StyledStreamWriter("  ").write(std::cout, json);
75    }
76    return 0;
77}
78