• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef SkSVGDOM_DEFINED
9 #define SkSVGDOM_DEFINED
10 
11 #include "include/core/SkFontMgr.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkSize.h"
14 #include "include/private/base/SkAPI.h"
15 #include "modules/skresources/include/SkResources.h"
16 #include "modules/skshaper/include/SkShaper_factory.h"
17 #include "modules/svg/include/SkSVGIDMapper.h"
18 #include "modules/svg/include/SkSVGSVG.h"
19 
20 class SkCanvas;
21 class SkSVGNode;
22 class SkStream;
23 struct SkSVGPresentationContext;
24 
25 class SK_API SkSVGDOM : public SkRefCnt {
26 public:
27     class Builder final {
28     public:
29         /**
30          * Specify a font manager for loading fonts (e.g. from the system) to render <text>
31          * SVG nodes.
32          * If this is not set, but a font is required as part of rendering, the text will
33          * not be displayed.
34          */
35         Builder& setFontManager(sk_sp<SkFontMgr>);
36 
37         /**
38          * Specify a resource provider for loading images etc.
39          */
40         Builder& setResourceProvider(sk_sp<skresources::ResourceProvider>);
41 
42         /**
43          * Specify the callbacks for dealing with shaping text. See also
44          * modules/skshaper/utils/FactoryHelpers.h
45          */
46         Builder& setTextShapingFactory(sk_sp<SkShapers::Factory>);
47 
48         sk_sp<SkSVGDOM> make(SkStream&) const;
49         sk_sp<SkSVGDOM> make(SkStream&, uint64_t) const;
50 
51     private:
52         sk_sp<SkFontMgr>                             fFontMgr;
53         sk_sp<skresources::ResourceProvider>         fResourceProvider;
54         sk_sp<SkShapers::Factory>                    fTextShapingFactory;
55     };
56 
MakeFromStream(SkStream & str)57     static sk_sp<SkSVGDOM> MakeFromStream(SkStream& str) {
58         return Builder().make(str);
59     }
60 
MakeFromStream(SkStream & str,uint64_t svgColor)61     static sk_sp<SkSVGDOM> MakeFromStream(SkStream&str, uint64_t svgColor) {
62         return Builder().make(str, svgColor);
63     }
64 
65     /**
66      * Returns the root (outermost) SVG element.
67      */
getRoot()68     SkSVGSVG* getRoot() const { return fRoot.get(); }
69 
70     /**
71      * Specify a "container size" for the SVG dom.
72      *
73      * This is used to resolve the initial viewport when the root SVG width/height are specified
74      * in relative units.
75      *
76      * If the root dimensions are in absolute units, then the container size has no effect since
77      * the initial viewport is fixed.
78      */
79     void setContainerSize(const SkSize&);
80 
81     void setResizePercentage(float resizePercentage);
82     /**
83      * DEPRECATED: use getRoot()->intrinsicSize() to query the root element intrinsic size.
84      *
85      * Returns the SVG dom container size.
86      *
87      * If the client specified a container size via setContainerSize(), then the same size is
88      * returned.
89      *
90      * When unspecified by clients, this returns the intrinsic size of the root element, as defined
91      * by its width/height attributes.  If either width or height is specified in relative units
92      * (e.g. "100%"), then the corresponding intrinsic size dimension is zero.
93      */
94     const SkSize& containerSize() const;
95 
96     // Returns the node with the given id, or nullptr if not found.
97     sk_sp<SkSVGNode>* findNodeById(const char* id);
98 
99     void render(SkCanvas*) const;
100 
101     /** Render the node with the given id as if it were the only child of the root. */
102     void renderNode(SkCanvas*, SkSVGPresentationContext&, const char* id) const;
103 
104 private:
105     SkSVGDOM(sk_sp<SkSVGSVG>,
106              sk_sp<SkFontMgr>,
107              sk_sp<skresources::ResourceProvider>,
108              SkSVGIDMapper&&,
109              sk_sp<SkShapers::Factory>);
110 
111     const sk_sp<SkSVGSVG>                       fRoot;
112     const sk_sp<SkFontMgr>                      fFontMgr;
113     const sk_sp<SkShapers::Factory>             fTextShapingFactory;
114     const sk_sp<skresources::ResourceProvider>  fResourceProvider;
115     const SkSVGIDMapper                         fIDMapper;
116     float                                       fSVGResizePercentage;
117     SkSize                                      fContainerSize;
118 };
119 
120 #endif // SkSVGDOM_DEFINED
121