1 /* 2 * Copyright 2019 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 SkSVGText_DEFINED 9 #define SkSVGText_DEFINED 10 11 #include "include/core/SkPath.h" 12 #include "include/core/SkRect.h" 13 #include "include/core/SkRefCnt.h" 14 #include "include/private/base/SkAPI.h" 15 #include "modules/svg/include/SkSVGNode.h" 16 #include "modules/svg/include/SkSVGTransformableNode.h" 17 #include "modules/svg/include/SkSVGTypes.h" 18 19 #include <vector> 20 21 class SkSVGRenderContext; 22 class SK_API SkSVGTextContext; 23 24 // Base class for text-rendering nodes. 25 class SkSVGTextFragment : public SkSVGTransformableNode { 26 public: 27 void renderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const; 28 29 protected: SkSVGTextFragment(SkSVGTag t)30 explicit SkSVGTextFragment(SkSVGTag t) : INHERITED(t) {} 31 32 virtual void onShapeText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const = 0; 33 34 // Text nodes other than the root <text> element are not rendered directly. onRender(const SkSVGRenderContext &)35 void onRender(const SkSVGRenderContext&) const override {} 36 37 private: 38 SkPath onAsPath(const SkSVGRenderContext&) const override; 39 40 using INHERITED = SkSVGTransformableNode; 41 }; 42 43 // Base class for nestable text containers (<text>, <tspan>, etc). 44 class SkSVGTextContainer : public SkSVGTextFragment { 45 public: 46 SVG_ATTR(X, std::vector<SkSVGLength>, {}) 47 SVG_ATTR(Y, std::vector<SkSVGLength>, {}) 48 SVG_ATTR(Dx, std::vector<SkSVGLength>, {}) 49 SVG_ATTR(Dy, std::vector<SkSVGLength>, {}) 50 SVG_ATTR(Rotate, std::vector<SkSVGNumberType>, {}) 51 52 SVG_ATTR(XmlSpace, SkSVGXmlSpace, SkSVGXmlSpace::kDefault) 53 54 void appendChild(sk_sp<SkSVGNode>) final; 55 std::vector<sk_sp<SkSVGNode>> getChild() final; 56 57 protected: SkSVGTextContainer(SkSVGTag t)58 explicit SkSVGTextContainer(SkSVGTag t) : INHERITED(t) {} 59 60 void onShapeText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override; 61 62 bool parseAndSetAttribute(const char*, const char*) override; 63 64 private: 65 std::vector<sk_sp<SkSVGTextFragment>> fChildren; 66 67 using INHERITED = SkSVGTextFragment; 68 }; 69 70 class SkSVGText final : public SkSVGTextContainer { 71 public: Make()72 static sk_sp<SkSVGText> Make() { return sk_sp<SkSVGText>(new SkSVGText()); } 73 74 private: SkSVGText()75 SkSVGText() : INHERITED(SkSVGTag::kText) {} 76 77 void onRender(const SkSVGRenderContext&) const override; 78 79 SkRect onTransformableObjectBoundingBox(const SkSVGRenderContext&) const override; 80 SkPath onAsPath(const SkSVGRenderContext&) const override; 81 82 using INHERITED = SkSVGTextContainer; 83 }; 84 85 class SkSVGTSpan final : public SkSVGTextContainer { 86 public: Make()87 static sk_sp<SkSVGTSpan> Make() { return sk_sp<SkSVGTSpan>(new SkSVGTSpan()); } 88 89 private: SkSVGTSpan()90 SkSVGTSpan() : INHERITED(SkSVGTag::kTSpan) {} 91 92 using INHERITED = SkSVGTextContainer; 93 }; 94 95 class SkSVGTextLiteral final : public SkSVGTextFragment { 96 public: Make()97 static sk_sp<SkSVGTextLiteral> Make() { 98 return sk_sp<SkSVGTextLiteral>(new SkSVGTextLiteral()); 99 } 100 SVG_ATTR(Text,SkSVGStringType,SkSVGStringType ())101 SVG_ATTR(Text, SkSVGStringType, SkSVGStringType()) 102 103 private: 104 SkSVGTextLiteral() : INHERITED(SkSVGTag::kTextLiteral) {} 105 106 void onShapeText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override; 107 appendChild(sk_sp<SkSVGNode>)108 void appendChild(sk_sp<SkSVGNode>) override {} 109 110 using INHERITED = SkSVGTextFragment; 111 }; 112 113 class SkSVGTextPath final : public SkSVGTextContainer { 114 public: Make()115 static sk_sp<SkSVGTextPath> Make() { return sk_sp<SkSVGTextPath>(new SkSVGTextPath()); } 116 117 SVG_ATTR(Href , SkSVGIRI , {} ) 118 SVG_ATTR(StartOffset, SkSVGLength, SkSVGLength(0)) 119 120 private: SkSVGTextPath()121 SkSVGTextPath() : INHERITED(SkSVGTag::kTextPath) {} 122 123 void onShapeText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override; 124 bool parseAndSetAttribute(const char*, const char*) override; 125 126 using INHERITED = SkSVGTextContainer; 127 }; 128 129 #endif // SkSVGText_DEFINED 130