• 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/SkTemplates.h"
15 #include "modules/skresources/include/SkResources.h"
16 #include "modules/svg/include/SkSVGIDMapper.h"
17 
18 class SkCanvas;
19 class SkDOM;
20 class SkStream;
21 class SkSVGNode;
22 class SkSVGSVG;
23 
24 class SkSVGDOM : public SkRefCnt {
25 public:
26     class Builder final {
27     public:
28         /**
29          * Specify a font manager for loading SVG fonts.
30          */
31         Builder& setFontManager(sk_sp<SkFontMgr>);
32 
33         /**
34          * Specify a resource provider for loading images etc.
35          */
36         Builder& setResourceProvider(sk_sp<skresources::ResourceProvider>);
37 
38         sk_sp<SkSVGDOM> make(SkStream&) const;
39 
40     private:
41         sk_sp<SkFontMgr>                     fFontMgr;
42         sk_sp<skresources::ResourceProvider> fResourceProvider;
43     };
44 
MakeFromStream(SkStream & str)45     static sk_sp<SkSVGDOM> MakeFromStream(SkStream& str) {
46         return Builder().make(str);
47     }
48 
49     /**
50      * Returns the root (outermost) SVG element.
51      */
getRoot()52     SkSVGSVG* getRoot() const { return fRoot.get(); }
53 
54     /**
55      * Specify a "container size" for the SVG dom.
56      *
57      * This is used to resolve the initial viewport when the root SVG width/height are specified
58      * in relative units.
59      *
60      * If the root dimensions are in absolute units, then the container size has no effect since
61      * the initial viewport is fixed.
62      */
63     void setContainerSize(const SkSize&);
64 
65     /**
66      * DEPRECATED: use getRoot()->intrinsicSize() to query the root element intrinsic size.
67      *
68      * Returns the SVG dom container size.
69      *
70      * If the client specified a container size via setContainerSize(), then the same size is
71      * returned.
72      *
73      * When unspecified by clients, this returns the intrinsic size of the root element, as defined
74      * by its width/height attributes.  If either width or height is specified in relative units
75      * (e.g. "100%"), then the corresponding intrinsic size dimension is zero.
76      */
77     const SkSize& containerSize() const;
78 
79     // Returns the node with the given id, or nullptr if not found.
80     sk_sp<SkSVGNode>* findNodeById(const char* id);
81 
82     void render(SkCanvas*) const;
83 
84 private:
85     SkSVGDOM(sk_sp<SkSVGSVG>, sk_sp<SkFontMgr>, sk_sp<skresources::ResourceProvider>,
86              SkSVGIDMapper&&);
87 
88     const sk_sp<SkSVGSVG>                      fRoot;
89     const sk_sp<SkFontMgr>                     fFontMgr;
90     const sk_sp<skresources::ResourceProvider> fResourceProvider;
91     const SkSVGIDMapper                        fIDMapper;
92 
93     SkSize                 fContainerSize;
94 };
95 
96 #endif // SkSVGDOM_DEFINED
97