Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 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 | |
Florin Malita | adc6889 | 2020-12-15 10:52:26 -0500 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
| 13 | #include "modules/svg/include/SkSVGTransformableNode.h" |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 14 | #include "modules/svg/include/SkSVGTypes.h" |
| 15 | |
Florin Malita | adc6889 | 2020-12-15 10:52:26 -0500 | [diff] [blame] | 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: |
| 24 | explicit SkSVGTextFragment(SkSVGTag t) : INHERITED(t) {} |
| 25 | |
| 26 | virtual void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*, |
| 27 | SkSVGXmlSpace) const = 0; |
| 28 | |
Florin Malita | 302ea2e | 2021-01-24 12:04:17 -0500 | [diff] [blame] | 29 | // Text nodes other than the root <text> element are not rendered directly. |
| 30 | void onRender(const SkSVGRenderContext&) const override {} |
| 31 | |
Florin Malita | adc6889 | 2020-12-15 10:52:26 -0500 | [diff] [blame] | 32 | private: |
| 33 | SkPath onAsPath(const SkSVGRenderContext&) const final; |
| 34 | |
| 35 | using INHERITED = SkSVGTransformableNode; |
| 36 | }; |
| 37 | |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 38 | // Base class for nestable text containers (<text>, <tspan>, etc). |
Florin Malita | adc6889 | 2020-12-15 10:52:26 -0500 | [diff] [blame] | 39 | class SkSVGTextContainer : public SkSVGTextFragment { |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 40 | public: |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame] | 41 | SVG_ATTR(X, std::vector<SkSVGLength>, {}) |
| 42 | SVG_ATTR(Y, std::vector<SkSVGLength>, {}) |
Florin Malita | 735ac97 | 2020-12-22 11:23:32 -0500 | [diff] [blame] | 43 | SVG_ATTR(Dx, std::vector<SkSVGLength>, {}) |
| 44 | SVG_ATTR(Dy, std::vector<SkSVGLength>, {}) |
Florin Malita | 2d059fc | 2021-01-05 11:53:15 -0500 | [diff] [blame] | 45 | SVG_ATTR(Rotate, std::vector<SkSVGNumberType>, {}) |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 46 | |
Florin Malita | 9c1f1be | 2020-12-09 13:02:50 -0500 | [diff] [blame] | 47 | SVG_ATTR(XmlSpace, SkSVGXmlSpace, SkSVGXmlSpace::kDefault) |
| 48 | |
Florin Malita | dec7802 | 2020-12-17 16:36:54 -0500 | [diff] [blame] | 49 | void appendChild(sk_sp<SkSVGNode>) final; |
| 50 | |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 51 | protected: |
| 52 | explicit SkSVGTextContainer(SkSVGTag t) : INHERITED(t) {} |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 53 | |
Florin Malita | fc0ea0a | 2021-01-12 13:27:01 -0500 | [diff] [blame] | 54 | void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override; |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 55 | |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 56 | bool parseAndSetAttribute(const char*, const char*) override; |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 57 | |
Florin Malita | fc0ea0a | 2021-01-12 13:27:01 -0500 | [diff] [blame] | 58 | private: |
Florin Malita | adc6889 | 2020-12-15 10:52:26 -0500 | [diff] [blame] | 59 | std::vector<sk_sp<SkSVGTextFragment>> fChildren; |
| 60 | |
| 61 | using INHERITED = SkSVGTextFragment; |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 62 | }; |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 63 | |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 64 | class SkSVGText final : public SkSVGTextContainer { |
| 65 | public: |
| 66 | static sk_sp<SkSVGText> Make() { return sk_sp<SkSVGText>(new SkSVGText()); } |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 67 | |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 68 | private: |
| 69 | SkSVGText() : INHERITED(SkSVGTag::kText) {} |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 70 | |
Florin Malita | 302ea2e | 2021-01-24 12:04:17 -0500 | [diff] [blame] | 71 | void onRender(const SkSVGRenderContext&) const override; |
Florin Malita | fc0ea0a | 2021-01-12 13:27:01 -0500 | [diff] [blame] | 72 | |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 73 | using INHERITED = SkSVGTextContainer; |
| 74 | }; |
Florin Malita | 39fe8c8 | 2020-10-20 10:43:03 -0400 | [diff] [blame] | 75 | |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 76 | class SkSVGTSpan final : public SkSVGTextContainer { |
| 77 | public: |
| 78 | static sk_sp<SkSVGTSpan> Make() { return sk_sp<SkSVGTSpan>(new SkSVGTSpan()); } |
| 79 | |
| 80 | private: |
| 81 | SkSVGTSpan() : INHERITED(SkSVGTag::kTSpan) {} |
| 82 | |
| 83 | using INHERITED = SkSVGTextContainer; |
| 84 | }; |
| 85 | |
Florin Malita | adc6889 | 2020-12-15 10:52:26 -0500 | [diff] [blame] | 86 | class SkSVGTextLiteral final : public SkSVGTextFragment { |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 87 | public: |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 88 | static sk_sp<SkSVGTextLiteral> Make() { |
| 89 | return sk_sp<SkSVGTextLiteral>(new SkSVGTextLiteral()); |
| 90 | } |
| 91 | |
| 92 | SVG_ATTR(Text, SkSVGStringType, SkSVGStringType()) |
| 93 | |
| 94 | private: |
| 95 | SkSVGTextLiteral() : INHERITED(SkSVGTag::kTextLiteral) {} |
| 96 | |
Florin Malita | adc6889 | 2020-12-15 10:52:26 -0500 | [diff] [blame] | 97 | void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override; |
Florin Malita | 512ff75 | 2020-12-06 11:50:52 -0500 | [diff] [blame] | 98 | |
| 99 | void appendChild(sk_sp<SkSVGNode>) override {} |
| 100 | |
Florin Malita | adc6889 | 2020-12-15 10:52:26 -0500 | [diff] [blame] | 101 | using INHERITED = SkSVGTextFragment; |
Florin Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 102 | }; |
| 103 | |
Florin Malita | fc0ea0a | 2021-01-12 13:27:01 -0500 | [diff] [blame] | 104 | class SkSVGTextPath final : public SkSVGTextContainer { |
| 105 | public: |
| 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 | |
| 111 | private: |
| 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 Malita | b341810 | 2020-10-15 18:10:29 -0400 | [diff] [blame] | 120 | #endif // SkSVGText_DEFINED |