blob: efffb20d7a00506d1d2ae99487bfe9cbb4dde049 [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"
Ben Wagnerbf111d72016-11-07 18:05:29 -050012#include "SkOSPath.h"
fmalita6ceef3d2016-07-26 18:46:34 -070013#include "SkStream.h"
14#include "SkSVGDOM.h"
15#include "SkView.h"
16
17namespace {
18
19class SVGFileView : public SampleView {
20public:
fmalita851d68a2016-08-15 07:48:47 -070021 SVGFileView(const SkString& path)
22 : fPath(path), fLabel(SkStringPrintf("[%s]", SkOSPath::Basename(path.c_str()).c_str())) {}
23 virtual ~SVGFileView() = default;
24
25protected:
26 void onOnceBeforeDraw() override {
27 SkFILEStream svgStream(fPath.c_str());
fmalita6ceef3d2016-07-26 18:46:34 -070028 if (!svgStream.isValid()) {
fmalita851d68a2016-08-15 07:48:47 -070029 SkDebugf("file not found: \"path\"\n", fPath.c_str());
fmalita6ceef3d2016-07-26 18:46:34 -070030 return;
31 }
32
33 SkDOM xmlDom;
34 if (!xmlDom.build(svgStream)) {
fmalita851d68a2016-08-15 07:48:47 -070035 SkDebugf("XML parsing failed: \"path\"\n", fPath.c_str());
fmalita6ceef3d2016-07-26 18:46:34 -070036 return;
37 }
38
fmalitae1baa7c2016-09-14 12:04:30 -070039 fDom = SkSVGDOM::MakeFromDOM(xmlDom);
40 if (fDom) {
41 fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
42 }
fmalita6ceef3d2016-07-26 18:46:34 -070043 }
44
fmalita6ceef3d2016-07-26 18:46:34 -070045 void onDrawContent(SkCanvas* canvas) override {
46 if (fDom) {
47 fDom->render(canvas);
48 }
49 }
50
51 void onSizeChange() override {
52 if (fDom) {
53 fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
54 }
55
56 this->INHERITED::onSizeChange();
57 }
58
fmalitab83cdbc2016-08-04 08:39:41 -070059 bool onQuery(SkEvent* evt) override {
60 if (SampleCode::TitleQ(*evt)) {
61 SampleCode::TitleR(evt, fLabel.c_str());
62 return true;
63 }
64
65 return this->INHERITED::onQuery(evt);
66 }
fmalita6ceef3d2016-07-26 18:46:34 -070067private:
68 sk_sp<SkSVGDOM> fDom;
fmalita851d68a2016-08-15 07:48:47 -070069 SkString fPath;
fmalitab83cdbc2016-08-04 08:39:41 -070070 SkString fLabel;
fmalita6ceef3d2016-07-26 18:46:34 -070071
72 typedef SampleView INHERITED;
73};
74
75} // anonymous namespace
76
fmalita851d68a2016-08-15 07:48:47 -070077SampleView* CreateSampleSVGFileView(const SkString& filename);
78SampleView* CreateSampleSVGFileView(const SkString& filename) {
fmalita6ceef3d2016-07-26 18:46:34 -070079 return new SVGFileView(filename);
80}