blob: c3adcfa3cb74ef6a279efdfaeee7d35db29c99b6 [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:
32 SkPath onAsPath(const SkSVGRenderContext&) const final;
33
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;
73
Florin Malita512ff752020-12-06 11:50:52 -050074 using INHERITED = SkSVGTextContainer;
75};
Florin Malita39fe8c82020-10-20 10:43:03 -040076
Florin Malita512ff752020-12-06 11:50:52 -050077class SkSVGTSpan final : public SkSVGTextContainer {
78public:
79 static sk_sp<SkSVGTSpan> Make() { return sk_sp<SkSVGTSpan>(new SkSVGTSpan()); }
80
81private:
82 SkSVGTSpan() : INHERITED(SkSVGTag::kTSpan) {}
83
84 using INHERITED = SkSVGTextContainer;
85};
86
Florin Malitaadc68892020-12-15 10:52:26 -050087class SkSVGTextLiteral final : public SkSVGTextFragment {
Florin Malita512ff752020-12-06 11:50:52 -050088public:
Florin Malita512ff752020-12-06 11:50:52 -050089 static sk_sp<SkSVGTextLiteral> Make() {
90 return sk_sp<SkSVGTextLiteral>(new SkSVGTextLiteral());
91 }
92
93 SVG_ATTR(Text, SkSVGStringType, SkSVGStringType())
94
95private:
96 SkSVGTextLiteral() : INHERITED(SkSVGTag::kTextLiteral) {}
97
Florin Malitac55c8c12021-01-25 10:37:22 -050098 void onShapeText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
Florin Malita512ff752020-12-06 11:50:52 -050099
100 void appendChild(sk_sp<SkSVGNode>) override {}
101
Florin Malitaadc68892020-12-15 10:52:26 -0500102 using INHERITED = SkSVGTextFragment;
Florin Malitab3418102020-10-15 18:10:29 -0400103};
104
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500105class SkSVGTextPath final : public SkSVGTextContainer {
106public:
107 static sk_sp<SkSVGTextPath> Make() { return sk_sp<SkSVGTextPath>(new SkSVGTextPath()); }
108
109 SVG_ATTR(Href , SkSVGIRI , {SkString()} )
110 SVG_ATTR(StartOffset, SkSVGLength, SkSVGLength(0))
111
112private:
113 SkSVGTextPath() : INHERITED(SkSVGTag::kTextPath) {}
114
Florin Malitac55c8c12021-01-25 10:37:22 -0500115 void onShapeText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
Florin Malitafc0ea0a2021-01-12 13:27:01 -0500116 bool parseAndSetAttribute(const char*, const char*) override;
117
118 using INHERITED = SkSVGTextContainer;
119};
120
Florin Malitab3418102020-10-15 18:10:29 -0400121#endif // SkSVGText_DEFINED