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