Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 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 | |
Florin Malita | 7006e15 | 2020-11-10 15:24:59 -0500 | [diff] [blame] | 11 | #include "include/core/SkFontMgr.h" |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 12 | #include "include/core/SkRefCnt.h" |
| 13 | #include "include/core/SkSize.h" |
| 14 | #include "include/private/SkTemplates.h" |
Florin Malita | 24df67d | 2021-01-26 18:45:34 -0500 | [diff] [blame] | 15 | #include "modules/skresources/include/SkResources.h" |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 16 | #include "modules/svg/include/SkSVGIDMapper.h" |
| 17 | |
| 18 | class SkCanvas; |
| 19 | class SkDOM; |
| 20 | class SkStream; |
| 21 | class SkSVGNode; |
Florin Malita | 7006e15 | 2020-11-10 15:24:59 -0500 | [diff] [blame] | 22 | class SkSVGSVG; |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 23 | |
| 24 | class SkSVGDOM : public SkRefCnt { |
| 25 | public: |
Florin Malita | 7006e15 | 2020-11-10 15:24:59 -0500 | [diff] [blame] | 26 | class Builder final { |
| 27 | public: |
| 28 | /** |
| 29 | * Specify a font manager for loading SVG fonts. |
| 30 | */ |
| 31 | Builder& setFontManager(sk_sp<SkFontMgr>); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 32 | |
Florin Malita | 24df67d | 2021-01-26 18:45:34 -0500 | [diff] [blame] | 33 | /** |
| 34 | * Specify a resource provider for loading images etc. |
| 35 | */ |
| 36 | Builder& setResourceProvider(sk_sp<skresources::ResourceProvider>); |
| 37 | |
Florin Malita | 7006e15 | 2020-11-10 15:24:59 -0500 | [diff] [blame] | 38 | sk_sp<SkSVGDOM> make(SkStream&) const; |
| 39 | |
| 40 | private: |
Florin Malita | 24df67d | 2021-01-26 18:45:34 -0500 | [diff] [blame] | 41 | sk_sp<SkFontMgr> fFontMgr; |
| 42 | sk_sp<skresources::ResourceProvider> fResourceProvider; |
Florin Malita | 7006e15 | 2020-11-10 15:24:59 -0500 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | static sk_sp<SkSVGDOM> MakeFromStream(SkStream& str) { |
| 46 | return Builder().make(str); |
| 47 | } |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 48 | |
Florin Malita | bbaf630 | 2021-06-04 14:31:06 -0400 | [diff] [blame] | 49 | /** |
| 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 Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 63 | void setContainerSize(const SkSize&); |
| 64 | |
Florin Malita | bbaf630 | 2021-06-04 14:31:06 -0400 | [diff] [blame] | 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 | |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 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: |
Florin Malita | 24df67d | 2021-01-26 18:45:34 -0500 | [diff] [blame] | 85 | SkSVGDOM(sk_sp<SkSVGSVG>, sk_sp<SkFontMgr>, sk_sp<skresources::ResourceProvider>, |
| 86 | SkSVGIDMapper&&); |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 87 | |
Florin Malita | 24df67d | 2021-01-26 18:45:34 -0500 | [diff] [blame] | 88 | const sk_sp<SkSVGSVG> fRoot; |
| 89 | const sk_sp<SkFontMgr> fFontMgr; |
| 90 | const sk_sp<skresources::ResourceProvider> fResourceProvider; |
| 91 | const SkSVGIDMapper fIDMapper; |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 92 | |
Florin Malita | 7006e15 | 2020-11-10 15:24:59 -0500 | [diff] [blame] | 93 | SkSize fContainerSize; |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | #endif // SkSVGDOM_DEFINED |