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 SUBRIPINPUTFORMAT_H
0009 #define SUBRIPINPUTFORMAT_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 class SubRipInputFormat : public InputFormat
0019 {
0020     friend class FormatManager;
0021 
0022 protected:
0023     SubRipInputFormat()
0024         : InputFormat($("SubRip"), QStringList($("srt")))
0025 
0026     {}
0027 
0028     bool parseSubtitles(Subtitle &subtitle, const QString &data) const override
0029     {
0030         staticRE$(reTime, "[\\d]+\n([0-2][0-9]):([0-5][0-9]):([0-5][0-9])[,\\.]([0-9]+) --> ([0-2][0-9]):([0-5][0-9]):([0-5][0-9])[,\\.]([0-9]+)\n", REu);
0031 
0032         QRegularExpressionMatchIterator itTime = reTime.globalMatch(data);
0033         if(!itTime.hasNext())
0034             return false;
0035 
0036         do {
0037             QRegularExpressionMatch mTime = itTime.next();
0038 
0039             Time showTime(mTime.captured(1).toInt(), mTime.captured(2).toInt(), mTime.captured(3).toInt(), mTime.captured(4).toInt());
0040             Time hideTime(mTime.captured(5).toInt(), mTime.captured(6).toInt(), mTime.captured(7).toInt(), mTime.captured(8).toInt());
0041 
0042             const int off = mTime.capturedEnd();
0043             const QString text = data.mid(off, itTime.hasNext() ? itTime.peekNext().capturedStart() - off : -1).trimmed();
0044 
0045             RichString stext;
0046             stext.setRichString(text);
0047 
0048             SubtitleLine *line = new SubtitleLine(showTime, hideTime);
0049             line->primaryDoc()->setRichText(stext, true);
0050             subtitle.insertLine(line);
0051         } while(itTime.hasNext());
0052 
0053         return true;
0054     }
0055 };
0056 }
0057 
0058 #endif