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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SampleCode.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkDOM.h"
|
2016-08-04 15:39:41 +00:00
|
|
|
#include "SkOSFile.h"
|
2016-07-27 01:46:34 +00:00
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkSVGDOM.h"
|
|
|
|
#include "SkView.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class SVGFileView : public SampleView {
|
|
|
|
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())) {}
|
|
|
|
virtual ~SVGFileView() = default;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void onOnceBeforeDraw() override {
|
|
|
|
SkFILEStream svgStream(fPath.c_str());
|
2016-07-27 01:46:34 +00:00
|
|
|
if (!svgStream.isValid()) {
|
2016-08-15 14:48:47 +00:00
|
|
|
SkDebugf("file not found: \"path\"\n", fPath.c_str());
|
2016-07-27 01:46:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkDOM xmlDom;
|
|
|
|
if (!xmlDom.build(svgStream)) {
|
2016-08-15 14:48:47 +00:00
|
|
|
SkDebugf("XML parsing failed: \"path\"\n", fPath.c_str());
|
2016-07-27 01:46:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fDom = SkSVGDOM::MakeFromDOM(xmlDom, SkSize::Make(this->width(), this->height()));
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2016-08-04 15:39:41 +00:00
|
|
|
bool onQuery(SkEvent* evt) override {
|
|
|
|
if (SampleCode::TitleQ(*evt)) {
|
|
|
|
SampleCode::TitleR(evt, fLabel.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
typedef SampleView INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-08-15 14:48:47 +00:00
|
|
|
SampleView* CreateSampleSVGFileView(const SkString& filename);
|
|
|
|
SampleView* CreateSampleSVGFileView(const SkString& filename) {
|
2016-07-27 01:46:34 +00:00
|
|
|
return new SVGFileView(filename);
|
|
|
|
}
|