1 /* 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 "include/core/SkTypes.h" 9 10 #if defined(SK_ENABLE_SVG) 11 12 #include "include/core/SkCanvas.h" 13 #include "include/core/SkStream.h" 14 #include "modules/svg/include/SkSVGDOM.h" 15 #include "modules/svg/include/SkSVGNode.h" 16 #include "samplecode/Sample.h" 17 #include "src/core/SkOSFile.h" 18 #include "src/utils/SkOSPath.h" 19 #include "src/xml/SkDOM.h" 20 21 namespace { 22 23 class SVGFileView : public Sample { 24 public: SVGFileView(const SkString & path)25 SVGFileView(const SkString& path) 26 : fPath(path), fLabel(SkStringPrintf("[%s]", SkOSPath::Basename(path.c_str()).c_str())) {} 27 ~SVGFileView() override = default; 28 29 protected: onOnceBeforeDraw()30 void onOnceBeforeDraw() override { 31 SkFILEStream svgStream(fPath.c_str()); 32 if (!svgStream.isValid()) { 33 SkDebugf("file not found: \"%s\"\n", fPath.c_str()); 34 return; 35 } 36 37 fDom = SkSVGDOM::MakeFromStream(svgStream); 38 if (fDom) { 39 fDom->setContainerSize(SkSize::Make(this->width(), this->height())); 40 } 41 } 42 onDrawContent(SkCanvas * canvas)43 void onDrawContent(SkCanvas* canvas) override { 44 if (fDom) { 45 fDom->render(canvas); 46 } 47 } 48 onSizeChange()49 void onSizeChange() override { 50 if (fDom) { 51 fDom->setContainerSize(SkSize::Make(this->width(), this->height())); 52 } 53 54 this->INHERITED::onSizeChange(); 55 } 56 name()57 SkString name() override { return fLabel; } 58 59 private: 60 sk_sp<SkSVGDOM> fDom; 61 SkString fPath; 62 SkString fLabel; 63 64 using INHERITED = Sample; 65 }; 66 67 } // anonymous namespace 68 69 Sample* CreateSampleSVGFileView(const SkString& filename); CreateSampleSVGFileView(const SkString & filename)70Sample* CreateSampleSVGFileView(const SkString& filename) { 71 return new SVGFileView(filename); 72 } 73 #endif // defined(SK_ENABLE_SVG) 74