[svg] Refactor text rendering context plumbing

Instead of relying on RenderContext to pass text rendering options
downstack, introduce a dedicated virtual (onRenderText) and pass options
explicitly.

Root text nodes bridge from onRender() -> onRenderText().

This removes some complexity from RenderContext and incidentally fixes
xml:space = preserve (the value was being dropped during local ctx
copying).

Bug: skia:10840
Change-Id: Ic5fd9e0f9382f52f65108521574fcb2a422b97aa
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/344559
Reviewed-by: Tyler Denniston <tdenniston@google.com>
Commit-Queue: Florin Malita <fmalita@google.com>
diff --git a/modules/svg/include/SkSVGText.h b/modules/svg/include/SkSVGText.h
index 8fb4721..0710ab8 100644
--- a/modules/svg/include/SkSVGText.h
+++ b/modules/svg/include/SkSVGText.h
@@ -8,11 +8,32 @@
 #ifndef SkSVGText_DEFINED
 #define SkSVGText_DEFINED
 
-#include "modules/svg/include/SkSVGContainer.h"
+#include <vector>
+
+#include "modules/svg/include/SkSVGTransformableNode.h"
 #include "modules/svg/include/SkSVGTypes.h"
 
+class SkSVGTextContext;
+
+// Base class for text-rendering nodes.
+class SkSVGTextFragment : public SkSVGTransformableNode {
+public:
+    void renderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const;
+
+protected:
+    explicit SkSVGTextFragment(SkSVGTag t) : INHERITED(t) {}
+
+    virtual void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*,
+                              SkSVGXmlSpace) const = 0;
+
+private:
+    SkPath onAsPath(const SkSVGRenderContext&) const final;
+
+    using INHERITED = SkSVGTransformableNode;
+};
+
 // Base class for nestable text containers (<text>, <tspan>, etc).
-class SkSVGTextContainer : public SkSVGContainer {
+class SkSVGTextContainer : public SkSVGTextFragment {
 public:
     // TODO: these should be arrays
     SVG_ATTR(X, SkSVGLength, SkSVGLength(0))
@@ -25,11 +46,14 @@
 
 private:
     void appendChild(sk_sp<SkSVGNode>) final;
-    bool onPrepareToRender(SkSVGRenderContext*) const final;
+    void onRender(const SkSVGRenderContext&) const final;
+    void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const final;
 
     bool parseAndSetAttribute(const char*, const char*) override;
 
-    using INHERITED = SkSVGContainer;
+    std::vector<sk_sp<SkSVGTextFragment>> fChildren;
+
+    using INHERITED = SkSVGTextFragment;
 };
 
 class SkSVGText final : public SkSVGTextContainer {
@@ -39,8 +63,6 @@
 private:
     SkSVGText() : INHERITED(SkSVGTag::kText) {}
 
-    void onRender(const SkSVGRenderContext&) const override;
-
     using INHERITED = SkSVGTextContainer;
 };
 
@@ -54,7 +76,7 @@
     using INHERITED = SkSVGTextContainer;
 };
 
-class SkSVGTextLiteral final : public SkSVGNode {
+class SkSVGTextLiteral final : public SkSVGTextFragment {
 public:
     static sk_sp<SkSVGTextLiteral> Make() {
         return sk_sp<SkSVGTextLiteral>(new SkSVGTextLiteral());
@@ -65,12 +87,12 @@
 private:
     SkSVGTextLiteral() : INHERITED(SkSVGTag::kTextLiteral) {}
 
-    void onRender(const SkSVGRenderContext&) const override;
-    SkPath onAsPath(const SkSVGRenderContext&) const override;
+    void onRender(const SkSVGRenderContext&) const override {}
+    void onRenderText(const SkSVGRenderContext&, SkSVGTextContext*, SkSVGXmlSpace) const override;
 
     void appendChild(sk_sp<SkSVGNode>) override {}
 
-    using INHERITED = SkSVGNode;
+    using INHERITED = SkSVGTextFragment;
 };
 
 #endif  // SkSVGText_DEFINED