blob: e03d9ff6cbdc3755f14b6f365539cd783e4ec1b5 [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
Hal Canary0f666812018-03-22 15:21:12 -04008#include "SkTypes.h"
9
10#ifdef SK_XML
11
fmalita6ceef3d2016-07-26 18:46:34 -070012#include "SampleCode.h"
13#include "SkCanvas.h"
14#include "SkDOM.h"
fmalitab83cdbc2016-08-04 08:39:41 -070015#include "SkOSFile.h"
Ben Wagnerbf111d72016-11-07 18:05:29 -050016#include "SkOSPath.h"
fmalita6ceef3d2016-07-26 18:46:34 -070017#include "SkStream.h"
18#include "SkSVGDOM.h"
19#include "SkView.h"
20
21namespace {
22
23class SVGFileView : public SampleView {
24public:
fmalita851d68a2016-08-15 07:48:47 -070025 SVGFileView(const SkString& path)
26 : fPath(path), fLabel(SkStringPrintf("[%s]", SkOSPath::Basename(path.c_str()).c_str())) {}
Brian Salomond3b65972017-03-22 12:05:03 -040027 ~SVGFileView() override = default;
fmalita851d68a2016-08-15 07:48:47 -070028
29protected:
30 void onOnceBeforeDraw() override {
31 SkFILEStream svgStream(fPath.c_str());
fmalita6ceef3d2016-07-26 18:46:34 -070032 if (!svgStream.isValid()) {
fmalita851d68a2016-08-15 07:48:47 -070033 SkDebugf("file not found: \"path\"\n", fPath.c_str());
fmalita6ceef3d2016-07-26 18:46:34 -070034 return;
35 }
36
37 SkDOM xmlDom;
38 if (!xmlDom.build(svgStream)) {
fmalita851d68a2016-08-15 07:48:47 -070039 SkDebugf("XML parsing failed: \"path\"\n", fPath.c_str());
fmalita6ceef3d2016-07-26 18:46:34 -070040 return;
41 }
42
fmalitae1baa7c2016-09-14 12:04:30 -070043 fDom = SkSVGDOM::MakeFromDOM(xmlDom);
44 if (fDom) {
45 fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
46 }
fmalita6ceef3d2016-07-26 18:46:34 -070047 }
48
fmalita6ceef3d2016-07-26 18:46:34 -070049 void onDrawContent(SkCanvas* canvas) override {
50 if (fDom) {
51 fDom->render(canvas);
52 }
53 }
54
55 void onSizeChange() override {
56 if (fDom) {
57 fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
58 }
59
60 this->INHERITED::onSizeChange();
61 }
62
fmalitab83cdbc2016-08-04 08:39:41 -070063 bool onQuery(SkEvent* evt) override {
64 if (SampleCode::TitleQ(*evt)) {
65 SampleCode::TitleR(evt, fLabel.c_str());
66 return true;
67 }
68
69 return this->INHERITED::onQuery(evt);
70 }
fmalita6ceef3d2016-07-26 18:46:34 -070071private:
72 sk_sp<SkSVGDOM> fDom;
fmalita851d68a2016-08-15 07:48:47 -070073 SkString fPath;
fmalitab83cdbc2016-08-04 08:39:41 -070074 SkString fLabel;
fmalita6ceef3d2016-07-26 18:46:34 -070075
76 typedef SampleView INHERITED;
77};
78
79} // anonymous namespace
80
fmalita851d68a2016-08-15 07:48:47 -070081SampleView* CreateSampleSVGFileView(const SkString& filename);
82SampleView* CreateSampleSVGFileView(const SkString& filename) {
fmalita6ceef3d2016-07-26 18:46:34 -070083 return new SVGFileView(filename);
84}
Hal Canary0f666812018-03-22 15:21:12 -040085#endif // SK_XML