blob: f1fa4e7eeb745be32ca40794e11824578cf36abb [file] [log] [blame]
Florin Malitab3418102020-10-15 18:10:29 -04001/*
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
Florin Malitaadc68892020-12-15 10:52:26 -050011#include <vector>
12
13#include "modules/svg/include/SkSVGTransformableNode.h"
Florin Malitab3418102020-10-15 18:10:29 -040014#include "modules/svg/include/SkSVGTypes.h"
15
Florin Malitaadc68892020-12-15 10:52:26 -050016class SkSVGTextContext;
17
18// Base class for text-rendering nodes.
19class SkSVGTextFragment : public SkSVGTransformableNode {
20public:
21 void renderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const;
22
23protected:
24 explicit SkSVGTextFragment(SkSVGTag t) : INHERITED(t) {}
25
Florin Malitac55c8c12021-01-25 10:37:22 -050026 virtual void onShapeText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const = 0;
Florin Malitaadc68892020-12-15 10:52:26 -050027
Florin Malita302ea2e2021-01-24 12:04:17 -050028 // Text nodes other than the root <text> element are not rendered directly.
29 void onRender(const SkSVGRenderContext&) const override {}
30
Florin Malitaadc68892020-12-15 10:52:26 -050031private:
Florin Malitaf661ec72021-01-26 08:49:10 -050032 SkPath onAsPath(const SkSVGRenderContext&) const override;
Florin Malitaadc68892020-12-15 10:52:26 -050033
34 using INHERITED = SkSVGTransformableNode;
35};
36
Florin Malita512ff752020-12-06 11:50:52 -050037// Base class for nestable text containers (<text>, <tspan>, etc).
Florin Malitaadc68892020-12-15 10:52:26 -050038class SkSVGTextContainer : public SkSVGTextFragment {
Florin Malita512ff752020-12-06 11:50:52 -050039public:
Florin Malitadec78022020-12-17 16:36:54 -050040 SVG_ATTR(X, std::vector<SkSVGLength>, {})
41 SVG_ATTR(Y, std::vector<SkSVGLength>, {})
Florin Malita735ac972020-12-22 11:23:32 -050042 SVG_ATTR(Dx, std::vector<SkSVGLength>, {})
43 SVG_ATTR(Dy, std::vector<SkSVGLength>, {})
Florin Malita2d059fc2021-01-05 11:53:15 -050044 SVG_ATTR(Rotate, std::vector<SkSVGNumberType>, {})
Florin Malitab3418102020-10-15 18:10:29 -040045
Florin Malita9c1f1be2020-12-09 13:02:50 -050046 SVG_ATTR(XmlSpace, SkSVGXmlSpace, SkSVGXmlSpace::kDefault)
47
Florin Malitadec78022020-12-17 16:36:54 -050048 void appendChild(sk_sp<SkSVGNode>) final;
49
Florin Malita512ff752020-12-06 11:50:52 -050050protected:
51 explicit SkSVGTextContainer(SkSVGTag t) : INHERITED(t) {}
Florin Malitab3418102020-10-15 18:10:29 -040052
Florin Malitac55c8c12021-01-25 10:37:22 -050053 void onShapeText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
Florin Malitab3418102020-10-15 18:10:29 -040054
Florin Malita512ff752020-12-06 11:50:52 -050055 bool parseAndSetAttribute(const char*, const char*) override;
Florin Malitab3418102020-10-15 18:10:29 -040056
Florin Malitafc0ea0a2021-01-12 13:27:01 -050057private:
Florin Malitaadc68892020-12-15 10:52:26 -050058 std::vector<sk_sp<SkSVGTextFragment>> fChildren;
59
60 using INHERITED = SkSVGTextFragment;
Florin Malita512ff752020-12-06 11:50:52 -050061};
Florin Malitab3418102020-10-15 18:10:29 -040062
Florin Malita512ff752020-12-06 11:50:52 -050063class SkSVGText final : public SkSVGTextContainer {
64public:
65 static sk_sp<SkSVGText> Make() { return sk_sp<SkSVGText>(new SkSVGText()); }
Florin Malitab3418102020-10-15 18:10:29 -040066
Florin Malita512ff752020-12-06 11:50:52 -050067private:
68 SkSVGText() : INHERITED(SkSVGTag::kText) {}
Florin Malitab3418102020-10-15 18:10:29 -040069
Florin Malita302ea2e2021-01-24 12:04:17 -050070 void onRender(const SkSVGRenderContext&) const override;
Florin Malitafc0ea0a2021-01-12 13:27:01 -050071
Florin Malitaf9652242021-01-25 14:12:26 -050072 SkRect onObjectBoundingBox(const SkSVGRenderContext&) const override;
Florin Malitaf661ec72021-01-26 08:49:10 -050073 SkPath onAsPath(const SkSVGRenderContext&) const override;
Florin Malitaf9652242021-01-25 14:12:26 -050074
Florin Malita512ff752020-12-06 11:50:52 -050075 using INHERITED = SkSVGTextContainer;
76};
Florin Malita39fe8c82020-10-20 10:43:03 -040077
Florin Malita512ff752020-12-06 11:50:52 -050078class SkSVGTSpan final : public SkSVGTextContainer {
79public:
80 static sk_sp<SkSVGTSpan> Make() { return sk_sp<SkSVGTSpan>(new SkSVGTSpan()); }
81
82private:
83 SkSVGTSpan() : INHERITED(SkSVGTag::kTSpan) {}
84
85 using INHERITED = SkSVGTextContainer;
86};
87
Florin Malitaadc68892020-12-15 10:52:26 -050088class SkSVGTextLiteral final : public SkSVGTextFragment {
Florin Malita512ff752020-12-06 11:50:52 -050089public:
Florin Malita512ff752020-12-06 11:50:52 -050090 static sk_sp<SkSVGTextLiteral> Make() {
91 return sk_sp<SkSVGTextLiteral>(new SkSVGTextLiteral());
92 }
93
94 SVG_ATTR(Text, SkSVGStringType, SkSVGStringType())
95
96private:
97 SkSVGTextLiteral() : INHERITED(SkSVGTag::kTextLiteral) {}
98
Florin Malitac55c8c12021-01-25 10:37:22 -050099 void onShapeText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
Florin Malita512ff752020-12-06 11:50:52 -0500100
101 void appendChild(sk_sp<SkSVGNode>) override {}
102
Florin Malitaadc68892020-12-15 10:52:26 -0500103 using INHERITED = SkSVGTextFragment;
Florin Malitab3418102020-10-15 18:10:29 -0400104};
105
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500106class SkSVGTextPath final : public SkSVGTextContainer {
107public:
108 static sk_sp<SkSVGTextPath> Make() { return sk_sp<SkSVGTextPath>(new SkSVGTextPath()); }
109
Tyler Dennistone71f5472021-01-27 13:30:59 -0500110 SVG_ATTR(Href , SkSVGIRI , {} )
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500111 SVG_ATTR(StartOffset, SkSVGLength, SkSVGLength(0))
112
113private:
114 SkSVGTextPath() : INHERITED(SkSVGTag::kTextPath) {}
115
Florin Malitac55c8c12021-01-25 10:37:22 -0500116 void onShapeText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500117 bool parseAndSetAttribute(const char*, const char*) override;
118
119 using INHERITED = SkSVGTextContainer;
120};
121
Florin Malitab3418102020-10-15 18:10:29 -0400122#endif // SkSVGText_DEFINED