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