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