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