File indexing completed on 2024-12-22 04:40:08

0001 /*
0002     SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar>
0003     SPDX-FileCopyrightText: 2010-2022 Mladen Milinkovic <max@smoothware.net>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef TMPLAYERINPUTFORMAT_H
0009 #define TMPLAYERINPUTFORMAT_H
0010 
0011 #include "core/richtext/richdocument.h"
0012 #include "helpers/common.h"
0013 #include "formats/inputformat.h"
0014 
0015 #include <QRegularExpression>
0016 
0017 namespace SubtitleComposer {
0018 // FIXME TMPlayer Multiline variant
0019 
0020 class TMPlayerInputFormat : public InputFormat
0021 {
0022     friend class FormatManager;
0023 
0024 protected:
0025     TMPlayerInputFormat(const QString &name, const QStringList &extensions, const QString &re)
0026         : InputFormat(name, extensions),
0027           m_reTime(re)
0028     {}
0029 
0030     TMPlayerInputFormat()
0031         : TMPlayerInputFormat($("TMPlayer"), QStringList() << $("sub") << $("txt"),
0032                               $("([0-2]?[0-9]):([0-5][0-9]):([0-5][0-9]):([^\n]*)\n?"))
0033     {}
0034 
0035     bool parseSubtitles(Subtitle &subtitle, const QString &data) const override
0036     {
0037         QRegularExpressionMatchIterator itTime = m_reTime.globalMatch(data);
0038         if(!itTime.hasNext())
0039             return false;
0040 
0041         do {
0042             QRegularExpressionMatch mTime = itTime.next();
0043 
0044             const QString text = mTime.captured(4).replace('|', '\n').trimmed();
0045 
0046             // To compensate for the format deficiencies, Subtitle Workshop writes empty lines
0047             // indicating that way the line hide time. We do the same.
0048             if(text.isEmpty())
0049                 continue;
0050 
0051             const Time showTime(mTime.captured(1).toInt(), mTime.captured(2).toInt(), mTime.captured(3).toInt(), 0);
0052             Time hideTime;
0053             if(itTime.hasNext()) {
0054                 mTime = itTime.peekNext();
0055                 hideTime = Time(mTime.captured(1).toInt(), mTime.captured(2).toInt(), mTime.captured(3).toInt(), 0);
0056             } else {
0057                 hideTime = showTime + 2000;
0058             }
0059 
0060             SubtitleLine *l = new SubtitleLine(showTime, hideTime);
0061             l->primaryDoc()->setPlainText(text);
0062             subtitle.insertLine(l);
0063         } while(itTime.hasNext());
0064 
0065         return subtitle.count() > 0;
0066     }
0067 
0068     QRegularExpression m_reTime;
0069 };
0070 
0071 class TMPlayerPlusInputFormat : public TMPlayerInputFormat
0072 {
0073     friend class FormatManager;
0074 
0075 protected:
0076     TMPlayerPlusInputFormat()
0077         : TMPlayerInputFormat($("TMPlayer+"), QStringList() << $("sub") << $("txt"),
0078                               $("([0-2]?[0-9]):([0-5][0-9]):([0-5][0-9])=([^\n]*)\n?"))
0079     {}
0080 };
0081 }
0082 
0083 #endif