xref: /aosp_15_r20/external/skia/tools/viewer/SVGFileSlide.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/skshaper/utils/FactoryHelpers.h"
15 #include "modules/svg/include/SkSVGDOM.h"
16 #include "modules/svg/include/SkSVGNode.h"
17 #include "src/core/SkOSFile.h"
18 #include "src/utils/SkOSPath.h"
19 #include "src/xml/SkDOM.h"
20 #include "tools/fonts/FontToolUtils.h"
21 #include "tools/viewer/Slide.h"
22 
23 namespace {
24 
25 class SVGFileSlide : public Slide {
26 public:
SVGFileSlide(const SkString & path)27     SVGFileSlide(const SkString& path) : fPath(path) {
28         fName = SkStringPrintf("[%s]", SkOSPath::Basename(path.c_str()).c_str());
29     }
30     ~SVGFileSlide() override = default;
31 
load(SkScalar w,SkScalar h)32     void load(SkScalar w, SkScalar h) override {
33         SkFILEStream svgStream(fPath.c_str());
34         if (!svgStream.isValid()) {
35             SkDebugf("file not found: \"%s\"\n", fPath.c_str());
36             return;
37         }
38 
39         fDom = SkSVGDOM::Builder()
40                        .setFontManager(ToolUtils::TestFontMgr())
41                        .setTextShapingFactory(SkShapers::BestAvailable())
42                        .make(svgStream);
43         if (fDom) {
44             fDom->setContainerSize(SkSize{w, h});
45         }
46     }
47 
draw(SkCanvas * canvas)48     void draw(SkCanvas* canvas) override {
49         if (fDom) {
50             fDom->render(canvas);
51         }
52     }
53 
resize(SkScalar w,SkScalar h)54     void resize(SkScalar w, SkScalar h) override {
55         if (fDom) {
56             fDom->setContainerSize({w, h});
57         }
58     }
59 
60 private:
61     sk_sp<SkSVGDOM> fDom;
62     SkString        fPath;
63 };
64 
65 } // anonymous namespace
66 
CreateSampleSVGFileSlide(const SkString & filename)67 Slide* CreateSampleSVGFileSlide(const SkString& filename) {
68     return new SVGFileSlide(filename);
69 }
70 #endif  // defined(SK_ENABLE_SVG)
71