blob: 4c2c9d98de24325446ac573d8b429764c982f24c [file] [log] [blame]
Florin Malitab3418102020-10-15 18:10:29 -04001/*
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
Florin Malita7006e152020-11-10 15:24:59 -050011#include "include/core/SkFontMgr.h"
Florin Malitab3418102020-10-15 18:10:29 -040012#include "include/core/SkRefCnt.h"
13#include "include/core/SkSize.h"
14#include "include/private/SkTemplates.h"
Florin Malita24df67d2021-01-26 18:45:34 -050015#include "modules/skresources/include/SkResources.h"
Florin Malitab3418102020-10-15 18:10:29 -040016#include "modules/svg/include/SkSVGIDMapper.h"
17
18class SkCanvas;
19class SkDOM;
20class SkStream;
21class SkSVGNode;
Florin Malita7006e152020-11-10 15:24:59 -050022class SkSVGSVG;
Florin Malitab3418102020-10-15 18:10:29 -040023
24class SkSVGDOM : public SkRefCnt {
25public:
Florin Malita7006e152020-11-10 15:24:59 -050026 class Builder final {
27 public:
28 /**
29 * Specify a font manager for loading SVG fonts.
30 */
31 Builder& setFontManager(sk_sp<SkFontMgr>);
Florin Malitab3418102020-10-15 18:10:29 -040032
Florin Malita24df67d2021-01-26 18:45:34 -050033 /**
34 * Specify a resource provider for loading images etc.
35 */
36 Builder& setResourceProvider(sk_sp<skresources::ResourceProvider>);
37
Florin Malita7006e152020-11-10 15:24:59 -050038 sk_sp<SkSVGDOM> make(SkStream&) const;
39
40 private:
Florin Malita24df67d2021-01-26 18:45:34 -050041 sk_sp<SkFontMgr> fFontMgr;
42 sk_sp<skresources::ResourceProvider> fResourceProvider;
Florin Malita7006e152020-11-10 15:24:59 -050043 };
44
45 static sk_sp<SkSVGDOM> MakeFromStream(SkStream& str) {
46 return Builder().make(str);
47 }
Florin Malitab3418102020-10-15 18:10:29 -040048
Florin Malitabbaf6302021-06-04 14:31:06 -040049 /**
50 * Returns the root (outermost) SVG element.
51 */
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 */
Florin Malitab3418102020-10-15 18:10:29 -040063 void setContainerSize(const SkSize&);
64
Florin Malitabbaf6302021-06-04 14:31:06 -040065 /**
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
Florin Malitab3418102020-10-15 18:10:29 -040079 // 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
84private:
Florin Malita24df67d2021-01-26 18:45:34 -050085 SkSVGDOM(sk_sp<SkSVGSVG>, sk_sp<SkFontMgr>, sk_sp<skresources::ResourceProvider>,
86 SkSVGIDMapper&&);
Florin Malitab3418102020-10-15 18:10:29 -040087
Florin Malita24df67d2021-01-26 18:45:34 -050088 const sk_sp<SkSVGSVG> fRoot;
89 const sk_sp<SkFontMgr> fFontMgr;
90 const sk_sp<skresources::ResourceProvider> fResourceProvider;
91 const SkSVGIDMapper fIDMapper;
Florin Malitab3418102020-10-15 18:10:29 -040092
Florin Malita7006e152020-11-10 15:24:59 -050093 SkSize fContainerSize;
Florin Malitab3418102020-10-15 18:10:29 -040094};
95
96#endif // SkSVGDOM_DEFINED