blob: 61a6be73d60e2c82a6dbe72b2b66238209af9491 [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
fmalitae1baa7c2016-09-14 12:04:30 -070038 fDom = SkSVGDOM::MakeFromDOM(xmlDom);
39 if (fDom) {
40 fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
41 }
fmalita6ceef3d2016-07-26 18:46:34 -070042 }
43
fmalita6ceef3d2016-07-26 18:46:34 -070044 void onDrawContent(SkCanvas* canvas) override {
45 if (fDom) {
46 fDom->render(canvas);
47 }
48 }
49
50 void onSizeChange() override {
51 if (fDom) {
52 fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
53 }
54
55 this->INHERITED::onSizeChange();
56 }
57
fmalitab83cdbc2016-08-04 08:39:41 -070058 bool onQuery(SkEvent* evt) override {
59 if (SampleCode::TitleQ(*evt)) {
60 SampleCode::TitleR(evt, fLabel.c_str());
61 return true;
62 }
63
64 return this->INHERITED::onQuery(evt);
65 }
fmalita6ceef3d2016-07-26 18:46:34 -070066private:
67 sk_sp<SkSVGDOM> fDom;
fmalita851d68a2016-08-15 07:48:47 -070068 SkString fPath;
fmalitab83cdbc2016-08-04 08:39:41 -070069 SkString fLabel;
fmalita6ceef3d2016-07-26 18:46:34 -070070
71 typedef SampleView INHERITED;
72};
73
74} // anonymous namespace
75
fmalita851d68a2016-08-15 07:48:47 -070076SampleView* CreateSampleSVGFileView(const SkString& filename);
77SampleView* CreateSampleSVGFileView(const SkString& filename) {
fmalita6ceef3d2016-07-26 18:46:34 -070078 return new SVGFileView(filename);
79}