2016-07-27 01:46:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2018-03-22 19:21:12 +00:00
|
|
|
|
|
|
|
#ifdef SK_XML
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkStream.h"
|
2020-10-15 22:10:29 +00:00
|
|
|
#include "modules/svg/include/SkSVGDOM.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/core/SkOSFile.h"
|
|
|
|
#include "src/utils/SkOSPath.h"
|
|
|
|
#include "src/xml/SkDOM.h"
|
2016-07-27 01:46:34 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class SVGFileView : public Sample {
|
2016-07-27 01:46:34 +00:00
|
|
|
public:
|
2016-08-15 14:48:47 +00:00
|
|
|
SVGFileView(const SkString& path)
|
|
|
|
: fPath(path), fLabel(SkStringPrintf("[%s]", SkOSPath::Basename(path.c_str()).c_str())) {}
|
2017-03-22 16:05:03 +00:00
|
|
|
~SVGFileView() override = default;
|
2016-08-15 14:48:47 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
SkFILEStream svgStream(fPath.c_str());
|
2016-07-27 01:46:34 +00:00
|
|
|
if (!svgStream.isValid()) {
|
2021-06-25 15:05:20 +00:00
|
|
|
SkDebugf("file not found: \"%s\"\n", fPath.c_str());
|
2016-07-27 01:46:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-10 20:24:59 +00:00
|
|
|
fDom = SkSVGDOM::MakeFromStream(svgStream);
|
2016-09-14 19:04:30 +00:00
|
|
|
if (fDom) {
|
|
|
|
fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
|
|
|
|
}
|
2016-07-27 01:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
if (fDom) {
|
|
|
|
fDom->render(canvas);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void onSizeChange() override {
|
|
|
|
if (fDom) {
|
|
|
|
fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
|
|
|
|
}
|
|
|
|
|
|
|
|
this->INHERITED::onSizeChange();
|
|
|
|
}
|
|
|
|
|
2019-07-03 14:55:44 +00:00
|
|
|
SkString name() override { return fLabel; }
|
2016-08-04 15:39:41 +00:00
|
|
|
|
2016-07-27 01:46:34 +00:00
|
|
|
private:
|
|
|
|
sk_sp<SkSVGDOM> fDom;
|
2016-08-15 14:48:47 +00:00
|
|
|
SkString fPath;
|
2016-08-04 15:39:41 +00:00
|
|
|
SkString fLabel;
|
2016-07-27 01:46:34 +00:00
|
|
|
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = Sample;
|
2016-07-27 01:46:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
Sample* CreateSampleSVGFileView(const SkString& filename);
|
|
|
|
Sample* CreateSampleSVGFileView(const SkString& filename) {
|
2016-07-27 01:46:34 +00:00
|
|
|
return new SVGFileView(filename);
|
|
|
|
}
|
2018-03-22 19:21:12 +00:00
|
|
|
#endif // SK_XML
|