Skip to content
Extraits de code Groupes Projets
Sélectionner une révision Git
  • b51e05bae727bfd2b4d2e7d9d6f387fe03a9fb87
  • master par défaut
  • script
  • new-devel
  • devel
  • timingView-edit
  • fix-mpv
7 résultats

Line.cc

  • Line.cc 3,46 Kio
    #include "Line.hh"
    #include "AssFactory.hh"
    
    using namespace Vivy::Ass;
    
    Line::Line(AssFactory *const factory, const QString &lineString)
        : assFactory(factory)
    {
        enum LineIndex : int {
            Layer   = 0, // int
            Start   = 1, // time
            End     = 2, // time
            Style   = 3, // text
            Name    = 4, // text
            MarginL = 5, // int
            MarginR = 6, // int
            MarginV = 7, // int
            Effect  = 8, // text
            Text    = 9, // text
            PastLastCode
    
            // NOTE: time is of the form: `h:mm:ss.cc`
        };
    
        static const QString lineHeader = "Dialogue: ";
        if (!lineString.startsWith(lineHeader))
            throw std::runtime_error(("invalid event line header: " + lineString).toStdString());
    
        const QString lineContent = lineString.mid(lineString.indexOf(": ") + 2 /* strlen ": " */);
        QStringList contentList   = lineContent.split(",", Qt::KeepEmptyParts, Qt::CaseInsensitive);
    
        if (contentList.size() < LineIndex::PastLastCode)
            throw std::runtime_error(("invalid number of items " + QString::number(contentList.size()) +
                                      " instead of something superiror or equal to " +
                                      QString::number(PastLastCode) + " for line: " + lineContent)
                                         .toStdString());
    
        layer  = Utils::decodeLineToInteger(contentList[LineIndex::Layer], "Layer is not an integer");
        effect = contentList[LineIndex::Effect];
        nameOrActor = contentList[LineIndex::Name];
        start       = Utils::Time::fromString(contentList[LineIndex::Start]).toUInt();
        end         = Utils::Time::fromString(contentList[LineIndex::End]).toUInt();
    
        styleProperties.marginL =
            Utils::decodeLineToInteger(contentList[LineIndex::MarginL], "MarginL is not an integer");
        styleProperties.marginR =
            Utils::decodeLineToInteger(contentList[LineIndex::MarginR], "MarginR is not an integer");
        styleProperties.marginV =
            Utils::decodeLineToInteger(contentList[LineIndex::MarginV], "MarginV is not an integer");
    
        const QString style = contentList[LineIndex::Style];
        if (!assFactory->hasStyle(style))
            throw std::runtime_error(("Invalid or not declared style name: " + style).toStdString());
        lineStyle = assFactory->getStyle(style);
    
        // Pop all but the text, it may contains `,` characters
        for (int i = 0; i < LineIndex::Text; ++i)
            contentList.removeFirst();
        ___contentAsText = contentList.join("");
    }
    
    void
    Line::initSylFromString(const QString &line) noexcept
    {
        // Matches syllabes like: `{\toto}{\alpha&HFF}content`
        QRegExp re("(?:{[^}]*})+[^{]*", Qt::CaseInsensitive, QRegExp::RegExp);
    
        int pos = 0;
        try {
            while ((pos = re.indexIn(line, pos)) != -1) {
                content.append(Syl(this, re.cap(1)));
                pos += re.matchedLength();
            }
        } catch (const std::runtime_error &e) {
            qCritical() << "Failed to init syllabes with line:" << line;
            qCritical() << "Fallback to all line is one syllabe";
            pos = 0;
        }
    
        if (pos == 0) {
            content.clear();
            content.append(Syl(this, line, Syl::ConstructMode::Raw));
        }
    }
    
    void
    Line::setStart(quint64 s) noexcept
    {
        start = s;
        if (s > end)
            end = s;
    }
    
    void
    Line::setEnd(quint64 s) noexcept
    {
        end = s;
        if (start > s)
            start = s;
    }
    
    quint64
    Line::getDuration() const noexcept
    {
        return end - start;
    }
    
    StyleProperties
    Line::getStyleProperties() const noexcept
    {
        return styleProperties;
    }