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 #ifdef SK_XML 11 12 #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" 19 20 namespace { 21 22 class SVGFileView : public Sample { 23 public: SVGFileView(const SkString & path)24 SVGFileView(const SkString& path) 25 : fPath(path), fLabel(SkStringPrintf("[%s]", SkOSPath::Basename(path.c_str()).c_str())) {} 26 ~SVGFileView() override = default; 27 28 protected: onOnceBeforeDraw()29 void onOnceBeforeDraw() override { 30 SkFILEStream svgStream(fPath.c_str()); 31 if (!svgStream.isValid()) { 32 SkDebugf("file not found: \"path\"\n", fPath.c_str()); 33 return; 34 } 35 36 SkDOM xmlDom; 37 if (!xmlDom.build(svgStream)) { 38 SkDebugf("XML parsing failed: \"path\"\n", fPath.c_str()); 39 return; 40 } 41 42 fDom = SkSVGDOM::MakeFromDOM(xmlDom); 43 if (fDom) { 44 fDom->setContainerSize(SkSize::Make(this->width(), this->height())); 45 } 46 } 47 onDrawContent(SkCanvas * canvas)48 void onDrawContent(SkCanvas* canvas) override { 49 if (fDom) { 50 fDom->render(canvas); 51 } 52 } 53 onSizeChange()54 void onSizeChange() override { 55 if (fDom) { 56 fDom->setContainerSize(SkSize::Make(this->width(), this->height())); 57 } 58 59 this->INHERITED::onSizeChange(); 60 } 61 name()62 SkString name() override { return fLabel; } 63 64 private: 65 sk_sp<SkSVGDOM> fDom; 66 SkString fPath; 67 SkString fLabel; 68 69 typedef Sample INHERITED; 70 }; 71 72 } // anonymous namespace 73 74 Sample* CreateSampleSVGFileView(const SkString& filename); CreateSampleSVGFileView(const SkString & filename)75Sample* CreateSampleSVGFileView(const SkString& filename) { 76 return new SVGFileView(filename); 77 } 78 #endif // SK_XML 79