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