Add experimental support for svg text tag.

Change-Id: Ied72850ec98d5fe6d4263d91cb1dd89bd58b5ad1
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/274796
Commit-Queue: Florin Malita <fmalita@chromium.org>
Reviewed-by: Florin Malita <fmalita@chromium.org>
Reviewed-by: Mike Reed <reed@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index 9112d09..ca08a29 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1723,6 +1723,7 @@
         "experimental/svg/model/SkSVGSVG.cpp",
         "experimental/svg/model/SkSVGShape.cpp",
         "experimental/svg/model/SkSVGStop.cpp",
+        "experimental/svg/model/SkSVGText.cpp",
         "experimental/svg/model/SkSVGTransformableNode.cpp",
         "experimental/svg/model/SkSVGUse.cpp",
         "experimental/svg/model/SkSVGValue.cpp",
diff --git a/experimental/svg/model/SkSVGAttribute.h b/experimental/svg/model/SkSVGAttribute.h
index 3242f59..23a8081 100644
--- a/experimental/svg/model/SkSVGAttribute.h
+++ b/experimental/svg/model/SkSVGAttribute.h
@@ -22,6 +22,8 @@
     kFill,
     kFillOpacity,
     kFillRule,
+    kFontFamily,
+    kFontSize,
     kFx, // <radialGradient>: focal point x position
     kFy, // <radialGradient>: focal point y position
     kGradientTransform,
@@ -46,6 +48,8 @@
     kStrokeMiterLimit,
     kStrokeWidth,
     kTransform,
+    kText,
+    kTextAnchor,
     kViewBox,
     kVisibility,
     kWidth,
diff --git a/experimental/svg/model/SkSVGDOM.cpp b/experimental/svg/model/SkSVGDOM.cpp
index 79d5899..23a3355 100644
--- a/experimental/svg/model/SkSVGDOM.cpp
+++ b/experimental/svg/model/SkSVGDOM.cpp
@@ -23,6 +23,7 @@
 #include "experimental/svg/model/SkSVGRenderContext.h"
 #include "experimental/svg/model/SkSVGSVG.h"
 #include "experimental/svg/model/SkSVGStop.h"
+#include "experimental/svg/model/SkSVGText.h"
 #include "experimental/svg/model/SkSVGTypes.h"
 #include "experimental/svg/model/SkSVGUse.h"
 #include "experimental/svg/model/SkSVGValue.h"
@@ -95,6 +96,14 @@
     return true;
 }
 
+bool SetStringAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
+                           const char* stringValue) {
+    SkString str(stringValue, strlen(stringValue));
+    SkSVGStringType strType = SkSVGStringType(str);
+    node->setAttribute(attr, SkSVGStringValue(strType));
+    return true;
+}
+
 bool SetTransformAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
                            const char* stringValue) {
     SkSVGTransformType transform;
@@ -313,6 +322,8 @@
     { "fill"             , { SkSVGAttribute::kFill             , SetPaintAttribute        }},
     { "fill-opacity"     , { SkSVGAttribute::kFillOpacity      , SetNumberAttribute       }},
     { "fill-rule"        , { SkSVGAttribute::kFillRule         , SetFillRuleAttribute     }},
+    { "font-family"      , { SkSVGAttribute::kFontFamily       , SetStringAttribute       }},
+    { "font-size"        , { SkSVGAttribute::kFontSize         , SetLengthAttribute       }},
     // focal point x & y
     { "fx"               , { SkSVGAttribute::kFx               , SetLengthAttribute       }},
     { "fy"               , { SkSVGAttribute::kFy               , SetLengthAttribute       }},
@@ -337,6 +348,8 @@
     { "stroke-opacity"   , { SkSVGAttribute::kStrokeOpacity    , SetNumberAttribute       }},
     { "stroke-width"     , { SkSVGAttribute::kStrokeWidth      , SetLengthAttribute       }},
     { "style"            , { SkSVGAttribute::kUnknown          , SetStyleAttributes       }},
+    { "text"             , { SkSVGAttribute::kText             , SetStringAttribute       }},
+    { "text-anchor"      , { SkSVGAttribute::kTextAnchor       , SetStringAttribute       }},
     { "transform"        , { SkSVGAttribute::kTransform        , SetTransformAttribute    }},
     { "viewBox"          , { SkSVGAttribute::kViewBox          , SetViewBoxAttribute      }},
     { "visibility"       , { SkSVGAttribute::kVisibility       , SetVisibilityAttribute   }},
@@ -367,6 +380,7 @@
     { "rect"          , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make();           }},
     { "stop"          , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make();           }},
     { "svg"           , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make();            }},
+    { "text"          , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make();           }},
     { "use"           , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make();            }},
 };
 
diff --git a/experimental/svg/model/SkSVGNode.h b/experimental/svg/model/SkSVGNode.h
index 0a20a67..10c8d30 100644
--- a/experimental/svg/model/SkSVGNode.h
+++ b/experimental/svg/model/SkSVGNode.h
@@ -34,6 +34,7 @@
     kRect,
     kStop,
     kSvg,
+    kText,
     kUse
 };
 
diff --git a/experimental/svg/model/SkSVGText.cpp b/experimental/svg/model/SkSVGText.cpp
new file mode 100644
index 0000000..f98a16f
--- /dev/null
+++ b/experimental/svg/model/SkSVGText.cpp
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2019 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "experimental/svg/model/SkSVGText.h"
+
+#include "experimental/svg/model/SkSVGRenderContext.h"
+#include "experimental/svg/model/SkSVGValue.h"
+#include "include/core/SkCanvas.h"
+
+SkSVGText::SkSVGText() : INHERITED(SkSVGTag::kText) {}
+
+void SkSVGText::setX(const SkSVGLength& x) { fX = x; }
+
+void SkSVGText::setY(const SkSVGLength& y) { fY = y; }
+
+void SkSVGText::setFontFamily(const SkSVGStringType& font_family) {
+  fTypeface =
+      SkTypeface::MakeFromName(font_family.value().c_str(), SkFontStyle());
+}
+
+void SkSVGText::setFontSize(const SkSVGLength& size) { fFontSize = size; }
+
+void SkSVGText::setText(const SkSVGStringType& text) { fText = text; }
+
+void SkSVGText::setTextAnchor(const SkSVGStringType& text_anchor) {
+  if (strcmp(text_anchor.value().c_str(), "start") == 0) {
+    fTextAlign = SkTextUtils::Align::kLeft_Align;
+  } else if (strcmp(text_anchor.value().c_str(), "middle") == 0) {
+    fTextAlign = SkTextUtils::Align::kCenter_Align;
+  } else if (strcmp(text_anchor.value().c_str(), "end") == 0) {
+    fTextAlign = SkTextUtils::Align::kRight_Align;
+  }
+}
+
+void SkSVGText::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx,
+                       const SkPaint& paint, SkPathFillType) const {
+  SkFont font(fTypeface, fFontSize.value());
+  SkTextUtils::DrawString(canvas, fText.value().c_str(), fX.value(), fY.value(),
+                          font, paint, fTextAlign);
+}
+
+SkPath SkSVGText::onAsPath(const SkSVGRenderContext& ctx) const {
+  SkPath path;
+  return path;
+}
+
+void SkSVGText::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
+  switch (attr) {
+    case SkSVGAttribute::kX:
+      if (const auto* x = v.as<SkSVGLengthValue>()) {
+        this->setX(*x);
+      }
+      break;
+    case SkSVGAttribute::kY:
+      if (const auto* y = v.as<SkSVGLengthValue>()) {
+        this->setY(*y);
+      }
+      break;
+    case SkSVGAttribute::kText:
+      if (const auto* text = v.as<SkSVGStringValue>()) {
+        this->setText(*text);
+      }
+      break;
+    case SkSVGAttribute::kTextAnchor:
+      if (const auto* text_anchor = v.as<SkSVGStringValue>()) {
+        this->setTextAnchor(*text_anchor);
+      }
+      break;
+    case SkSVGAttribute::kFontFamily:
+      if (const auto* font_family = v.as<SkSVGStringValue>()) {
+        this->setFontFamily(*font_family);
+      }
+      break;
+    case SkSVGAttribute::kFontSize:
+      if (const auto* font_size = v.as<SkSVGLengthValue>()) {
+        this->setFontSize(*font_size);
+      }
+      break;
+    default:
+      this->INHERITED::onSetAttribute(attr, v);
+  }
+}
diff --git a/experimental/svg/model/SkSVGText.h b/experimental/svg/model/SkSVGText.h
new file mode 100644
index 0000000..0cc3fca
--- /dev/null
+++ b/experimental/svg/model/SkSVGText.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2019 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkSVGText_DEFINED
+#define SkSVGText_DEFINED
+
+#include "experimental/svg/model/SkSVGShape.h"
+#include "experimental/svg/model/SkSVGTypes.h"
+#include "include/core/SkFont.h"
+#include "include/utils/SkTextUtils.h"
+
+class SkRRect;
+
+class SkSVGText final : public SkSVGShape {
+ public:
+  ~SkSVGText() override = default;
+  static sk_sp<SkSVGText> Make() {
+    return sk_sp<SkSVGText>(new SkSVGText()); }
+
+  void setX(const SkSVGLength&);
+  void setY(const SkSVGLength&);
+  void setFontFamily(const SkSVGStringType&);
+  void setFontSize(const SkSVGLength&);
+  void setText(const SkSVGStringType&);
+  void setTextAnchor(const SkSVGStringType&);
+
+ protected:
+  void onSetAttribute(SkSVGAttribute, const SkSVGValue&) override;
+
+  void onDraw(SkCanvas*, const SkSVGLengthContext&, const SkPaint&,
+              SkPathFillType) const override;
+
+  SkPath onAsPath(const SkSVGRenderContext&) const override;
+
+ private:
+  SkSVGText();
+  SkSVGLength fX = SkSVGLength(0);
+  SkSVGLength fY = SkSVGLength(0);
+  SkSVGStringType fText;
+  sk_sp<SkTypeface> fTypeface;
+  SkSVGLength fFontSize;
+  SkTextUtils::Align fTextAlign = SkTextUtils::Align::kLeft_Align;
+  typedef SkSVGShape INHERITED;
+};
+
+#endif  // SkSVGText_DEFINED