blob: a4b5b34d7d0d52a6fbb28628d886b7e97d1de6e3 [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:
fmalita851d68a2016-08-15 07:48:47 -070020 SVGFileView(const SkString& path)
21 : fPath(path), fLabel(SkStringPrintf("[%s]", SkOSPath::Basename(path.c_str()).c_str())) {}
22 virtual ~SVGFileView() = default;
23
24protected:
25 void onOnceBeforeDraw() override {
26 SkFILEStream svgStream(fPath.c_str());
fmalita6ceef3d2016-07-26 18:46:34 -070027 if (!svgStream.isValid()) {
fmalita851d68a2016-08-15 07:48:47 -070028 SkDebugf("file not found: \"path\"\n", fPath.c_str());
fmalita6ceef3d2016-07-26 18:46:34 -070029 return;
30 }
31
32 SkDOM xmlDom;
33 if (!xmlDom.build(svgStream)) {
fmalita851d68a2016-08-15 07:48:47 -070034 SkDebugf("XML parsing failed: \"path\"\n", fPath.c_str());
fmalita6ceef3d2016-07-26 18:46:34 -070035 return;
36 }
37
38 fDom = SkSVGDOM::MakeFromDOM(xmlDom, SkSize::Make(this->width(), this->height()));
39 }
40
fmalita6ceef3d2016-07-26 18:46:34 -070041 void onDrawContent(SkCanvas* canvas) override {
42 if (fDom) {
43 fDom->render(canvas);
44 }
45 }
46
47 void onSizeChange() override {
48 if (fDom) {
49 fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
50 }
51
52 this->INHERITED::onSizeChange();
53 }
54
fmalitab83cdbc2016-08-04 08:39:41 -070055 bool onQuery(SkEvent* evt) override {
56 if (SampleCode::TitleQ(*evt)) {
57 SampleCode::TitleR(evt, fLabel.c_str());
58 return true;
59 }
60
61 return this->INHERITED::onQuery(evt);
62 }
fmalita6ceef3d2016-07-26 18:46:34 -070063private:
64 sk_sp<SkSVGDOM> fDom;
fmalita851d68a2016-08-15 07:48:47 -070065 SkString fPath;
fmalitab83cdbc2016-08-04 08:39:41 -070066 SkString fLabel;
fmalita6ceef3d2016-07-26 18:46:34 -070067
68 typedef SampleView INHERITED;
69};
70
71} // anonymous namespace
72
fmalita851d68a2016-08-15 07:48:47 -070073SampleView* CreateSampleSVGFileView(const SkString& filename);
74SampleView* CreateSampleSVGFileView(const SkString& filename) {
fmalita6ceef3d2016-07-26 18:46:34 -070075 return new SVGFileView(filename);
76}