blob: 12c91a95fe1055f878d79eba41e6fdb2b791f7ae [file] [log] [blame]
fmalita6ceef3d2016-07-26 18:46:34 -07001/*
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 "SampleCode.h"
9#include "SkCanvas.h"
10#include "SkDOM.h"
fmalitab83cdbc2016-08-04 08:39:41 -070011#include "SkOSFile.h"
fmalita6ceef3d2016-07-26 18:46:34 -070012#include "SkStream.h"
13#include "SkSVGDOM.h"
14#include "SkView.h"
15
16namespace {
17
18class SVGFileView : public SampleView {
19public:
fmalitab83cdbc2016-08-04 08:39:41 -070020 SVGFileView(const char path[])
21 : fLabel(SkStringPrintf("[%s]", SkOSPath::Basename(path).c_str())) {
fmalita6ceef3d2016-07-26 18:46:34 -070022 SkFILEStream svgStream(path);
23 if (!svgStream.isValid()) {
24 SkDebugf("file not found: \"path\"\n", path);
25 return;
26 }
27
28 SkDOM xmlDom;
29 if (!xmlDom.build(svgStream)) {
30 SkDebugf("XML parsing failed: \"path\"\n", path);
31 return;
32 }
33
34 fDom = SkSVGDOM::MakeFromDOM(xmlDom, SkSize::Make(this->width(), this->height()));
35 }
36
37 virtual ~SVGFileView() = default;
38
39protected:
40 void onDrawContent(SkCanvas* canvas) override {
41 if (fDom) {
42 fDom->render(canvas);
43 }
44 }
45
46 void onSizeChange() override {
47 if (fDom) {
48 fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
49 }
50
51 this->INHERITED::onSizeChange();
52 }
53
fmalitab83cdbc2016-08-04 08:39:41 -070054 bool onQuery(SkEvent* evt) override {
55 if (SampleCode::TitleQ(*evt)) {
56 SampleCode::TitleR(evt, fLabel.c_str());
57 return true;
58 }
59
60 return this->INHERITED::onQuery(evt);
61 }
fmalita6ceef3d2016-07-26 18:46:34 -070062private:
63 sk_sp<SkSVGDOM> fDom;
fmalitab83cdbc2016-08-04 08:39:41 -070064 SkString fLabel;
fmalita6ceef3d2016-07-26 18:46:34 -070065
66 typedef SampleView INHERITED;
67};
68
69} // anonymous namespace
70
71SampleView* CreateSampleSVGFileView(const char filename[]);
72SampleView* CreateSampleSVGFileView(const char filename[]) {
73 return new SVGFileView(filename);
74}