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