SampleSVGFile.cpp 1.9 KB

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