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 std::vector<sk_sp<SkSVGNode>> getChild() final; 50 51 protected: SkSVGTextContainer(SkSVGTag t)52 explicit SkSVGTextContainer(SkSVGTag t) : INHERITED(t) {} 53 54 void onShapeText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override; 55 56 bool parseAndSetAttribute(const char*, const char*) override; 57 58 private: 59 std::vector<sk_sp<SkSVGTextFragment>> fChildren; 60 61 using INHERITED = SkSVGTextFragment; 62 }; 63 64 class SkSVGText final : public SkSVGTextContainer { 65 public: Make()66 static sk_sp<SkSVGText> Make() { return sk_sp<SkSVGText>(new SkSVGText()); } 67 68 private: SkSVGText()69 SkSVGText() : INHERITED(SkSVGTag::kText) {} 70 71 void onRender(const SkSVGRenderContext&) const override; 72 73 SkRect onObjectBoundingBox(const SkSVGRenderContext&) const override; 74 SkPath onAsPath(const SkSVGRenderContext&) const override; 75 76 using INHERITED = SkSVGTextContainer; 77 }; 78 79 class SkSVGTSpan final : public SkSVGTextContainer { 80 public: Make()81 static sk_sp<SkSVGTSpan> Make() { return sk_sp<SkSVGTSpan>(new SkSVGTSpan()); } 82 83 private: SkSVGTSpan()84 SkSVGTSpan() : INHERITED(SkSVGTag::kTSpan) {} 85 86 using INHERITED = SkSVGTextContainer; 87 }; 88 89 class SkSVGTextLiteral final : public SkSVGTextFragment { 90 public: Make()91 static sk_sp<SkSVGTextLiteral> Make() { 92 return sk_sp<SkSVGTextLiteral>(new SkSVGTextLiteral()); 93 } 94 SVG_ATTR(Text,SkSVGStringType,SkSVGStringType ())95 SVG_ATTR(Text, SkSVGStringType, SkSVGStringType()) 96 97 private: 98 SkSVGTextLiteral() : INHERITED(SkSVGTag::kTextLiteral) {} 99 100 void onShapeText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override; 101 appendChild(sk_sp<SkSVGNode>)102 void appendChild(sk_sp<SkSVGNode>) override {} 103 104 using INHERITED = SkSVGTextFragment; 105 }; 106 107 class SkSVGTextPath final : public SkSVGTextContainer { 108 public: Make()109 static sk_sp<SkSVGTextPath> Make() { return sk_sp<SkSVGTextPath>(new SkSVGTextPath()); } 110 111 SVG_ATTR(Href , SkSVGIRI , {} ) 112 SVG_ATTR(StartOffset, SkSVGLength, SkSVGLength(0)) 113 114 private: SkSVGTextPath()115 SkSVGTextPath() : INHERITED(SkSVGTag::kTextPath) {} 116 117 void onShapeText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override; 118 bool parseAndSetAttribute(const char*, const char*) override; 119 120 using INHERITED = SkSVGTextContainer; 121 }; 122 123 #endif // SkSVGText_DEFINED 124