[svgcanvas] Add flag to suppress newlines and tabs in XML

Bug: skia:9343
Change-Id: Id82f998ac57fc787aeb4435a6907047ea680d2fc
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/236157
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Florin Malita <fmalita@chromium.org>
diff --git a/include/svg/SkSVGCanvas.h b/include/svg/SkSVGCanvas.h
index fb87be6..86e8975 100644
--- a/include/svg/SkSVGCanvas.h
+++ b/include/svg/SkSVGCanvas.h
@@ -15,7 +15,8 @@
 class SK_API SkSVGCanvas {
 public:
     enum {
-        kConvertTextToPaths_Flag = 0x01,
+        kConvertTextToPaths_Flag = 0x01, // emit text as <path>s
+        kNoPrettyXML_Flag        = 0x02, // suppress newlines and tabs in output
     };
 
     /**
diff --git a/src/svg/SkSVGCanvas.cpp b/src/svg/SkSVGCanvas.cpp
index 82b493f..30c9198 100644
--- a/src/svg/SkSVGCanvas.cpp
+++ b/src/svg/SkSVGCanvas.cpp
@@ -13,10 +13,14 @@
 std::unique_ptr<SkCanvas> SkSVGCanvas::Make(const SkRect& bounds, SkWStream* writer,
                                             uint32_t flags) {
     // TODO: pass full bounds to the device
-    SkISize size = bounds.roundOut().size();
+    const auto size = bounds.roundOut().size();
+    const auto xml_flags = (flags & kNoPrettyXML_Flag) ? SkToU32(SkXMLStreamWriter::kNoPretty_Flag)
+                                                       : 0;
 
-    auto svgDevice = SkSVGDevice::Make(size, skstd::make_unique<SkXMLStreamWriter>(writer), flags);
+    auto svgDevice = SkSVGDevice::Make(size,
+                                       skstd::make_unique<SkXMLStreamWriter>(writer, xml_flags),
+                                       flags);
 
-    return svgDevice ? skstd::make_unique<SkCanvas>(svgDevice)
+    return svgDevice ? skstd::make_unique<SkCanvas>(std::move(svgDevice))
                      : nullptr;
 }
diff --git a/src/xml/SkXMLWriter.cpp b/src/xml/SkXMLWriter.cpp
index 25d59b4..4640d73 100644
--- a/src/xml/SkXMLWriter.cpp
+++ b/src/xml/SkXMLWriter.cpp
@@ -190,14 +190,9 @@
 
 // SkXMLStreamWriter
 
-static void tab(SkWStream& stream, int level) {
-    for (int i = 0; i < level; i++) {
-        stream.writeText("\t");
-    }
-}
-
-SkXMLStreamWriter::SkXMLStreamWriter(SkWStream* stream) : fStream(*stream)
-{}
+SkXMLStreamWriter::SkXMLStreamWriter(SkWStream* stream, uint32_t flags)
+    : fStream(*stream)
+    , fFlags(flags) {}
 
 SkXMLStreamWriter::~SkXMLStreamWriter() {
     this->flush();
@@ -217,25 +212,25 @@
 
     if (!elem->fHasChildren && !elem->fHasText) {
         fStream.writeText(">");
-        fStream.newline();
+        this->newline();
     }
 
-    tab(fStream, fElems.count() + 1);
+    this->tab(fElems.count() + 1);
     fStream.write(text, length);
-    fStream.newline();
+    this->newline();
 }
 
 void SkXMLStreamWriter::onEndElement() {
     Elem* elem = getEnd();
     if (elem->fHasChildren || elem->fHasText) {
-        tab(fStream, fElems.count());
+        this->tab(fElems.count());
         fStream.writeText("</");
         fStream.writeText(elem->fName.c_str());
         fStream.writeText(">");
     } else {
         fStream.writeText("/>");
     }
-    fStream.newline();
+    this->newline();
     doEnd(elem);
 }
 
@@ -244,10 +239,10 @@
     if (this->doStart(name, length)) {
         // the first child, need to close with >
         fStream.writeText(">");
-        fStream.newline();
+        this->newline();
     }
 
-    tab(fStream, level);
+    this->tab(level);
     fStream.writeText("<");
     fStream.write(name, length);
 }
@@ -255,7 +250,21 @@
 void SkXMLStreamWriter::writeHeader() {
     const char* header = getHeader();
     fStream.write(header, strlen(header));
-    fStream.newline();
+    this->newline();
+}
+
+void SkXMLStreamWriter::newline() {
+    if (!(fFlags & kNoPretty_Flag)) {
+        fStream.newline();
+    }
+}
+
+void SkXMLStreamWriter::tab(int level) {
+    if (!(fFlags & kNoPretty_Flag)) {
+        for (int i = 0; i < level; i++) {
+            fStream.writeText("\t");
+        }
+    }
 }
 
 ////////////////////////////////////////////////////////////////////////////////////////////////
diff --git a/src/xml/SkXMLWriter.h b/src/xml/SkXMLWriter.h
index 49fb4be..a189169 100644
--- a/src/xml/SkXMLWriter.h
+++ b/src/xml/SkXMLWriter.h
@@ -63,7 +63,11 @@
 
 class SkXMLStreamWriter : public SkXMLWriter {
 public:
-    SkXMLStreamWriter(SkWStream*);
+    enum : uint32_t {
+        kNoPretty_Flag = 0x01,
+    };
+
+    SkXMLStreamWriter(SkWStream*, uint32_t flags = 0);
     ~SkXMLStreamWriter() override;
     void writeHeader() override;
 
@@ -74,7 +78,11 @@
     void onAddText(const char text[], size_t length) override;
 
 private:
+    void newline();
+    void tab(int lvl);
+
     SkWStream&      fStream;
+    const uint32_t  fFlags;
 };
 
 class SkXMLParserWriter : public SkXMLWriter {