blob: 1f45fd31a5fe5851c2908a254fb294c739b69eba [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
26 virtual void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*,
27 SkSVGXmlSpace) const = 0;
28
Florin Malita302ea2e2021-01-24 12:04:17 -050029 // Text nodes other than the root <text> element are not rendered directly.
30 void onRender(const SkSVGRenderContext&) const override {}
31
Florin Malitaadc68892020-12-15 10:52:26 -050032private:
33 SkPath onAsPath(const SkSVGRenderContext&) const final;
34
35 using INHERITED = SkSVGTransformableNode;
36};
37
Florin Malita512ff752020-12-06 11:50:52 -050038// Base class for nestable text containers (<text>, <tspan>, etc).
Florin Malitaadc68892020-12-15 10:52:26 -050039class SkSVGTextContainer : public SkSVGTextFragment {
Florin Malita512ff752020-12-06 11:50:52 -050040public:
Florin Malitadec78022020-12-17 16:36:54 -050041 SVG_ATTR(X, std::vector<SkSVGLength>, {})
42 SVG_ATTR(Y, std::vector<SkSVGLength>, {})
Florin Malita735ac972020-12-22 11:23:32 -050043 SVG_ATTR(Dx, std::vector<SkSVGLength>, {})
44 SVG_ATTR(Dy, std::vector<SkSVGLength>, {})
Florin Malita2d059fc2021-01-05 11:53:15 -050045 SVG_ATTR(Rotate, std::vector<SkSVGNumberType>, {})
Florin Malitab3418102020-10-15 18:10:29 -040046
Florin Malita9c1f1be2020-12-09 13:02:50 -050047 SVG_ATTR(XmlSpace, SkSVGXmlSpace, SkSVGXmlSpace::kDefault)
48
Florin Malitadec78022020-12-17 16:36:54 -050049 void appendChild(sk_sp<SkSVGNode>) final;
50
Florin Malita512ff752020-12-06 11:50:52 -050051protected:
52 explicit SkSVGTextContainer(SkSVGTag t) : INHERITED(t) {}
Florin Malitab3418102020-10-15 18:10:29 -040053
Florin Malitafc0ea0a2021-01-12 13:27:01 -050054 void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
Florin Malitab3418102020-10-15 18:10:29 -040055
Florin Malita512ff752020-12-06 11:50:52 -050056 bool parseAndSetAttribute(const char*, const char*) override;
Florin Malitab3418102020-10-15 18:10:29 -040057
Florin Malitafc0ea0a2021-01-12 13:27:01 -050058private:
Florin Malitaadc68892020-12-15 10:52:26 -050059 std::vector<sk_sp<SkSVGTextFragment>> fChildren;
60
61 using INHERITED = SkSVGTextFragment;
Florin Malita512ff752020-12-06 11:50:52 -050062};
Florin Malitab3418102020-10-15 18:10:29 -040063
Florin Malita512ff752020-12-06 11:50:52 -050064class SkSVGText final : public SkSVGTextContainer {
65public:
66 static sk_sp<SkSVGText> Make() { return sk_sp<SkSVGText>(new SkSVGText()); }
Florin Malitab3418102020-10-15 18:10:29 -040067
Florin Malita512ff752020-12-06 11:50:52 -050068private:
69 SkSVGText() : INHERITED(SkSVGTag::kText) {}
Florin Malitab3418102020-10-15 18:10:29 -040070
Florin Malita302ea2e2021-01-24 12:04:17 -050071 void onRender(const SkSVGRenderContext&) const override;
Florin Malitafc0ea0a2021-01-12 13:27:01 -050072
Florin Malita512ff752020-12-06 11:50:52 -050073 using INHERITED = SkSVGTextContainer;
74};
Florin Malita39fe8c82020-10-20 10:43:03 -040075
Florin Malita512ff752020-12-06 11:50:52 -050076class SkSVGTSpan final : public SkSVGTextContainer {
77public:
78 static sk_sp<SkSVGTSpan> Make() { return sk_sp<SkSVGTSpan>(new SkSVGTSpan()); }
79
80private:
81 SkSVGTSpan() : INHERITED(SkSVGTag::kTSpan) {}
82
83 using INHERITED = SkSVGTextContainer;
84};
85
Florin Malitaadc68892020-12-15 10:52:26 -050086class SkSVGTextLiteral final : public SkSVGTextFragment {
Florin Malita512ff752020-12-06 11:50:52 -050087public:
Florin Malita512ff752020-12-06 11:50:52 -050088 static sk_sp<SkSVGTextLiteral> Make() {
89 return sk_sp<SkSVGTextLiteral>(new SkSVGTextLiteral());
90 }
91
92 SVG_ATTR(Text, SkSVGStringType, SkSVGStringType())
93
94private:
95 SkSVGTextLiteral() : INHERITED(SkSVGTag::kTextLiteral) {}
96
Florin Malitaadc68892020-12-15 10:52:26 -050097 void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
Florin Malita512ff752020-12-06 11:50:52 -050098
99 void appendChild(sk_sp<SkSVGNode>) override {}
100
Florin Malitaadc68892020-12-15 10:52:26 -0500101 using INHERITED = SkSVGTextFragment;
Florin Malitab3418102020-10-15 18:10:29 -0400102};
103
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500104class SkSVGTextPath final : public SkSVGTextContainer {
105public:
106 static sk_sp<SkSVGTextPath> Make() { return sk_sp<SkSVGTextPath>(new SkSVGTextPath()); }
107
108 SVG_ATTR(Href , SkSVGIRI , {SkString()} )
109 SVG_ATTR(StartOffset, SkSVGLength, SkSVGLength(0))
110
111private:
112 SkSVGTextPath() : INHERITED(SkSVGTag::kTextPath) {}
113
114 void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
115 bool parseAndSetAttribute(const char*, const char*) override;
116
117 using INHERITED = SkSVGTextContainer;
118};
119
Florin Malitab3418102020-10-15 18:10:29 -0400120#endif // SkSVGText_DEFINED