aboutsummaryrefslogtreecommitdiffhomepage
path: root/samplecode/SampleSVGFile.cpp
blob: 12c91a95fe1055f878d79eba41e6fdb2b791f7ae (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
71
72
73
74
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SampleCode.h"
#include "SkCanvas.h"
#include "SkDOM.h"
#include "SkOSFile.h"
#include "SkStream.h"
#include "SkSVGDOM.h"
#include "SkView.h"

namespace {

class SVGFileView : public SampleView {
public:
    SVGFileView(const char path[])
        : fLabel(SkStringPrintf("[%s]", SkOSPath::Basename(path).c_str())) {
        SkFILEStream svgStream(path);
        if (!svgStream.isValid()) {
            SkDebugf("file not found: \"path\"\n", path);
            return;
        }

        SkDOM xmlDom;
        if (!xmlDom.build(svgStream)) {
            SkDebugf("XML parsing failed: \"path\"\n", path);
            return;
        }

        fDom = SkSVGDOM::MakeFromDOM(xmlDom, SkSize::Make(this->width(), this->height()));
    }

    virtual ~SVGFileView() = default;

protected:
    void onDrawContent(SkCanvas* canvas) override {
        if (fDom) {
            fDom->render(canvas);
        }
    }

    void onSizeChange() override {
        if (fDom) {
            fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
        }

        this->INHERITED::onSizeChange();
    }

    bool onQuery(SkEvent* evt) override {
        if (SampleCode::TitleQ(*evt)) {
            SampleCode::TitleR(evt, fLabel.c_str());
            return true;
        }

        return this->INHERITED::onQuery(evt);
    }
private:
    sk_sp<SkSVGDOM> fDom;
    SkString        fLabel;

    typedef SampleView INHERITED;
};

} // anonymous namespace

SampleView* CreateSampleSVGFileView(const char filename[]);
SampleView* CreateSampleSVGFileView(const char filename[]) {
    return new SVGFileView(filename);
}