blob: b3e328595250577843702a172cc284ee57733e1e [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
Hal Canary0f666812018-03-22 15:21:12 -04009
10#ifdef SK_XML
11
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "experimental/svg/model/SkSVGDOM.h"
13#include "include/core/SkCanvas.h"
14#include "include/core/SkStream.h"
15#include "samplecode/Sample.h"
16#include "src/core/SkOSFile.h"
17#include "src/utils/SkOSPath.h"
18#include "src/xml/SkDOM.h"
fmalita6ceef3d2016-07-26 18:46:34 -070019
20namespace {
21
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040022class SVGFileView : public Sample {
fmalita6ceef3d2016-07-26 18:46:34 -070023public:
fmalita851d68a2016-08-15 07:48:47 -070024 SVGFileView(const SkString& path)
25 : fPath(path), fLabel(SkStringPrintf("[%s]", SkOSPath::Basename(path.c_str()).c_str())) {}
Brian Salomond3b65972017-03-22 12:05:03 -040026 ~SVGFileView() override = default;
fmalita851d68a2016-08-15 07:48:47 -070027
28protected:
29 void onOnceBeforeDraw() override {
30 SkFILEStream svgStream(fPath.c_str());
fmalita6ceef3d2016-07-26 18:46:34 -070031 if (!svgStream.isValid()) {
fmalita851d68a2016-08-15 07:48:47 -070032 SkDebugf("file not found: \"path\"\n", fPath.c_str());
fmalita6ceef3d2016-07-26 18:46:34 -070033 return;
34 }
35
36 SkDOM xmlDom;
37 if (!xmlDom.build(svgStream)) {
fmalita851d68a2016-08-15 07:48:47 -070038 SkDebugf("XML parsing failed: \"path\"\n", fPath.c_str());
fmalita6ceef3d2016-07-26 18:46:34 -070039 return;
40 }
41
fmalitae1baa7c2016-09-14 12:04:30 -070042 fDom = SkSVGDOM::MakeFromDOM(xmlDom);
43 if (fDom) {
44 fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
45 }
fmalita6ceef3d2016-07-26 18:46:34 -070046 }
47
fmalita6ceef3d2016-07-26 18:46:34 -070048 void onDrawContent(SkCanvas* canvas) override {
49 if (fDom) {
50 fDom->render(canvas);
51 }
52 }
53
54 void onSizeChange() override {
55 if (fDom) {
56 fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
57 }
58
59 this->INHERITED::onSizeChange();
60 }
61
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040062 bool onQuery(Sample::Event* evt) override {
63 if (Sample::TitleQ(*evt)) {
64 Sample::TitleR(evt, fLabel.c_str());
fmalitab83cdbc2016-08-04 08:39:41 -070065 return true;
66 }
67
68 return this->INHERITED::onQuery(evt);
69 }
fmalita6ceef3d2016-07-26 18:46:34 -070070private:
71 sk_sp<SkSVGDOM> fDom;
fmalita851d68a2016-08-15 07:48:47 -070072 SkString fPath;
fmalitab83cdbc2016-08-04 08:39:41 -070073 SkString fLabel;
fmalita6ceef3d2016-07-26 18:46:34 -070074
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040075 typedef Sample INHERITED;
fmalita6ceef3d2016-07-26 18:46:34 -070076};
77
78} // anonymous namespace
79
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040080Sample* CreateSampleSVGFileView(const SkString& filename);
81Sample* CreateSampleSVGFileView(const SkString& filename) {
fmalita6ceef3d2016-07-26 18:46:34 -070082 return new SVGFileView(filename);
83}
Hal Canary0f666812018-03-22 15:21:12 -040084#endif // SK_XML