From 100f8225e027608b5da7b4a6734167f17b443fd9 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Fri, 9 Jul 2021 17:20:18 +0200 Subject: [PATCH] ASS: Generate the styles and display them in the vivy file property --- src/Lib/Ass/AssFactory.cc | 15 ++++++--- src/Lib/Ass/AssFactory.hh | 2 +- src/Lib/Ass/Style.cc | 47 ++++++++++++++++++++++++++++- src/Lib/Ass/Style.hh | 4 +++ src/Lib/Document/CRTPSubDocument.cc | 5 +++ src/Lib/Document/CRTPSubDocument.hh | 2 +- 6 files changed, 68 insertions(+), 7 deletions(-) diff --git a/src/Lib/Ass/AssFactory.cc b/src/Lib/Ass/AssFactory.cc index d034e0d9..db96607f 100644 --- a/src/Lib/Ass/AssFactory.cc +++ b/src/Lib/Ass/AssFactory.cc @@ -74,24 +74,31 @@ AssFactory::initFromStorage() noexcept bool AssFactory::checkValidity() const noexcept { - if (assInfo.isEmpty()) + if (assInfo.isEmpty()) { + qCritical() << "Empty info section"; return false; + } // Check for fields that must be integers - bool ok = false; SectionContent::const_iterator it = assInfo.begin(); const SectionContent::const_iterator end = assInfo.end(); while (it != end) { - if (intTypeFields.contains(it.key()) && (it.value().toInt(&ok), !ok)) + bool ok = false; + if (intTypeFields.contains(it.key()) && (it.value().toInt(&ok), !ok)) { + qCritical() << it.key() << "is not an integer:" << it.value(); return false; + } ++it; } // Check for fixed values fields for (const auto &fixedValues : checkedValues) { if (const auto &first = fixedValues.first; - assInfo.contains(first) && assInfo[first] != fixedValues.second) + assInfo.contains(first) && assInfo[first] != fixedValues.second) { + qCritical() << "Invalid" << first << "as it should be equal to" << fixedValues.second + << "but was" << assInfo[first]; return false; + } } return true; diff --git a/src/Lib/Ass/AssFactory.hh b/src/Lib/Ass/AssFactory.hh index b30189c8..62c0941d 100644 --- a/src/Lib/Ass/AssFactory.hh +++ b/src/Lib/Ass/AssFactory.hh @@ -48,7 +48,7 @@ private: static inline const QList<QPair<QString, QString>> checkedValues = { { "ScriptType", "v4.00+" }, { "WrapStyle", "0" }, - { "YCbCr Matrix", "TV.601" }, + // { "YCbCr Matrix", "TV.601" }, { "ScaledBorderAndShadow", "yes" } }; diff --git a/src/Lib/Ass/Style.cc b/src/Lib/Ass/Style.cc index 2652769b..a5169635 100644 --- a/src/Lib/Ass/Style.cc +++ b/src/Lib/Ass/Style.cc @@ -1,6 +1,8 @@ #include "Style.hh" #include <stdexcept> +#include <QJsonDocument> +#include <QJsonObject> using namespace Vivy::Ass; @@ -50,7 +52,7 @@ Style::Style(const QString &styleString) if (!styleString.startsWith(lineHeader)) throw std::runtime_error(("invalid style line header: " + styleString).toStdString()); - const QString lineContent = styleString.mid(styleString.indexOf(": ")); + const QString lineContent = styleString.mid(styleString.indexOf(": ") + 2 /* strlen ": " */); const QStringList content = lineContent.split(",", Qt::KeepEmptyParts, Qt::CaseInsensitive); if (content.size() != StyleIndex::PastLastCode) @@ -166,3 +168,46 @@ Color::fromString(const QString &colorString) noexcept return QColor(red, green, blue, alpha); } + +QString +Style::getElementName() const noexcept +{ + return styleName; +} + +QJsonDocument +Style::getProperties() const noexcept +{ + QJsonDocument ret; + + QJsonObject styleFont{ + { "Font name", fontName }, { "Font size", fontSize }, { "Bold", bold }, + { "Italic", italic }, { "Underline", underline }, { "Strike Out", strikeOut }, + }; + + QJsonObject styleFontProperties{ + { "Scale X", static_cast<const double>(scaleX) }, + { "Scale Y", static_cast<const double>(scaleY) }, + { "Spacing", static_cast<const double>(spacing) }, + { "Angle", static_cast<const double>(angle) }, + { "Border Style", static_cast<const double>(borderStyle) }, + { "Outline", static_cast<const double>(outline) }, + { "Shadow", static_cast<const double>(shadow) }, + }; + + QJsonObject styleMargins{ + { "Left", marginL }, + { "Right", marginR }, + { "Vertical", marginV }, + }; + + QJsonObject object{ { "Name", styleName }, + { "Font", styleFont }, + { "Font properties", styleFontProperties }, + { "Margin", styleMargins }, + { "Alignement", alignment }, + { "Encoding", encoding } }; + + ret.setObject(object); + return ret; +} diff --git a/src/Lib/Ass/Style.hh b/src/Lib/Ass/Style.hh index 21ca04d2..c8803692 100644 --- a/src/Lib/Ass/Style.hh +++ b/src/Lib/Ass/Style.hh @@ -5,6 +5,7 @@ #include <QVector> #include <QtGlobal> #include <QColor> +#include <QObject> namespace Vivy::Ass { @@ -41,6 +42,9 @@ public: Style &operator=(const Style &) = delete; ~Style() noexcept = default; + QString getElementName() const noexcept; + QJsonDocument getProperties() const noexcept; + private: static bool decodeLineToBoolean(const QString &item, const QString &error); static int decodeLineToInteger(const QString &item, const QString &error); diff --git a/src/Lib/Document/CRTPSubDocument.cc b/src/Lib/Document/CRTPSubDocument.cc index d6ac414b..a5493d26 100644 --- a/src/Lib/Document/CRTPSubDocument.cc +++ b/src/Lib/Document/CRTPSubDocument.cc @@ -119,6 +119,11 @@ AssSubDocument::getProperties() const noexcept { QJsonDocument ret; QJsonObject object; + + for (const Ass::StylePtr &style : styles) { + object.insert(style->getElementName(), style->getProperties().object()); + } + ret.setObject(object); return ret; } diff --git a/src/Lib/Document/CRTPSubDocument.hh b/src/Lib/Document/CRTPSubDocument.hh index f89b26f6..f8be95a0 100644 --- a/src/Lib/Document/CRTPSubDocument.hh +++ b/src/Lib/Document/CRTPSubDocument.hh @@ -41,7 +41,7 @@ public: try { ret->initFromPath(path); // May throw } catch (const std::runtime_error &e) { - qDebug() << "Failed to init document from file" << path; + qDebug().nospace() << "Failed to init document from file " << path << ": " << e.what(); ret.reset(); } -- GitLab