From a85073693b48c2581b04022ed79c3ec6ab936c9f Mon Sep 17 00:00:00 2001
From: Elliu <elliu@hashi.re>
Date: Sun, 24 Jul 2022 11:25:59 +0200
Subject: [PATCH] MISC: Support internal Ass saving

---
 src/Lib/Ass/Line.cc                                | 10 ++++++++++
 src/Lib/Ass/Line.hh                                |  1 +
 src/Lib/Ass/StyleProperties.hh                     |  2 ++
 src/Lib/Document/CRTPSubDocument.hh                |  1 +
 src/Lib/Document/CRTPSubDocument/AssSubDocument.cc | 12 ++++++++++++
 src/Lib/Document/VivyDocument.cc                   |  8 +++-----
 6 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/src/Lib/Ass/Line.cc b/src/Lib/Ass/Line.cc
index e8d8476d..5188678d 100644
--- a/src/Lib/Ass/Line.cc
+++ b/src/Lib/Ass/Line.cc
@@ -177,6 +177,16 @@ Line::getContentAsText() const noexcept
     return ___contentAsText;
 }
 
+QString
+Line::getLineAsText() const noexcept
+{
+    return QString::asprintf("%s: %d,%s,%s,", "Dialogue", layer, "0:00:00.00", "0:00:00.00") +
+           styleProperties.name +
+           QString::asprintf(",%s,%d,%d,%d,%s,", "", styleProperties.marginL,
+                             styleProperties.marginR, styleProperties.marginV, "karaoke") +
+           ___contentAsText;
+}
+
 void
 Line::setContent(const QVector<Syl> &syls) noexcept
 {
diff --git a/src/Lib/Ass/Line.hh b/src/Lib/Ass/Line.hh
index 55af6847..48dfd1d8 100644
--- a/src/Lib/Ass/Line.hh
+++ b/src/Lib/Ass/Line.hh
@@ -53,6 +53,7 @@ public:
     quint64 getStart() const noexcept;
     quint64 getEnd() const noexcept;
     QString getContentAsText() const noexcept;
+    QString getLineAsText() const noexcept;
 
 private:
     void initSylFromString(const QString &) noexcept;
diff --git a/src/Lib/Ass/StyleProperties.hh b/src/Lib/Ass/StyleProperties.hh
index eb7c04cd..d01680a2 100644
--- a/src/Lib/Ass/StyleProperties.hh
+++ b/src/Lib/Ass/StyleProperties.hh
@@ -8,6 +8,8 @@ namespace Vivy::Ass
 // Overrides some properties of the Style of an Ass::Line, Ass::Char, Ass::Syl
 
 struct StyleProperties final {
+    QString name{};
+
     // Colors
     QColor primaryColor{ Color::defaultValue }, secondaryColor{ Color::defaultValue },
         outlineColor{ Color::defaultValue }, backColor{ Color::defaultValue };
diff --git a/src/Lib/Document/CRTPSubDocument.hh b/src/Lib/Document/CRTPSubDocument.hh
index 626ea517..7aea6cf7 100644
--- a/src/Lib/Document/CRTPSubDocument.hh
+++ b/src/Lib/Document/CRTPSubDocument.hh
@@ -138,6 +138,7 @@ public:
     QString getElementName() const noexcept;
     QJsonDocument getProperties() const noexcept;
     Ass::AssFactory *getFactory() noexcept;
+    QJsonArray getInternalAss() noexcept;
 
     const QVector<Ass::LinePtr> &getLines() const noexcept;
     const QVector<Ass::StylePtr> &getStyles() const noexcept;
diff --git a/src/Lib/Document/CRTPSubDocument/AssSubDocument.cc b/src/Lib/Document/CRTPSubDocument/AssSubDocument.cc
index 9ad4624a..9f55a6f3 100644
--- a/src/Lib/Document/CRTPSubDocument/AssSubDocument.cc
+++ b/src/Lib/Document/CRTPSubDocument/AssSubDocument.cc
@@ -52,3 +52,15 @@ AssSubDocument::getFactory() noexcept
 {
     return assFactory;
 }
+
+QJsonArray
+AssSubDocument::getInternalAss() noexcept
+{
+    QJsonArray ret;
+
+    for (int i = 0; i < lines.size(); i++) {
+        ret.append(lines[i]->getLineAsText());
+    }
+
+    return ret;
+}
diff --git a/src/Lib/Document/VivyDocument.cc b/src/Lib/Document/VivyDocument.cc
index e3524172..baf0113e 100644
--- a/src/Lib/Document/VivyDocument.cc
+++ b/src/Lib/Document/VivyDocument.cc
@@ -131,12 +131,10 @@ VivyDocument::getSaveJsonDocumentFile() const
 
     if (documentType & Capabilities::AssAble) {
         // ASS is inside Vivy document
-        if ((assDocument->getFilePath() == getName())) {
-            subDocumentJson.insert(KeyInternalAssSource, true);
-            throw std::runtime_error("Unsupported save of internal ASS for now");
-        }
+        if ((assDocument->getFilePath() == getName()))
+            subDocumentJson.insert(KeyInternalAssSource, assDocument->getInternalAss());
 
-        // ASS is ints own ASS file
+        // ASS is in its own ASS file
         else
             subDocumentJson.insert(KeySubAss, assDocument->getFilePath());
     }
-- 
GitLab