Debian Codegraph @

libqnapi/src/subconvert/formats/tmplayer.cpp

raw symbols SCIP
1 /*****************************************************************************
2 ** QNapi
3 ** Copyright (C) 2008-2017 Piotr KrzemiƄski <[email protected]>
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12 **
13 *****************************************************************************/
14
15 #include "tmplayer.h"
16
17 bool TMPlayerSubtitleFormat::detect(const QStringList &lines) const {
18 foreach (QString line, lines) {
19 if (!line.trimmed().isEmpty()) {
20 QRegExp r("^(0\\d|\\d\\d):(0\\d|\\d\\d):(0\\d|\\d\\d):(.*)");
21 r.setPatternSyntax(QRegExp::RegExp2);
22 return r.exactMatch(line);
23 }
24 }
25
26 return false;
27 }
28
29 SubFile TMPlayerSubtitleFormat::decode(const QStringList &lines) const {
30 SubFile sf;
31
32 foreach (QString line, lines) {
33 if (!line.trimmed().isEmpty()) {
34 QRegExp r("^(0\\d|\\d\\d):(0\\d|\\d\\d):(0\\d|\\d\\d):(.*)");
35 r.setPatternSyntax(QRegExp::RegExp2);
36 if (r.exactMatch(line)) {
37 int h = r.cap(1).toInt();
38 int m = r.cap(2).toInt();
39 int s = r.cap(3).toInt();
40 QString tokenStream = r.cap(4);
41
42 SubEntry se;
43 se.frameStart = 1000L * (3600L * h + 60L * m + s);
44 se.tokens = decodeTokenStream(tokenStream);
45
46 sf.entries.push_back(se);
47 }
48 }
49 }
50
51 for (int i = 0; i < sf.entries.size(); ++i) {
52 long plus5s = sf.entries[i].frameStart + 5000L;
53 sf.entries[i].frameStop = plus5s;
54 if (i < sf.entries.size() - 1) {
55 sf.entries[i].frameStop =
56 std::min(plus5s, sf.entries[i + 1].frameStart - 1);
57 }
58 }
59
60 return sf;
61 }
62
63 QStringList TMPlayerSubtitleFormat::encode(const SubFile &subFile) const {
64 QStringList lines;
65 foreach (const SubEntry &entry, subFile.entries) {
66 lines.append(encodeEntry(entry));
67 }
68 return lines;
69 }
70
71 QString TMPlayerSubtitleFormat::encodeEntry(const SubEntry &entry) const {
72 QString tokensBuff;
73 foreach (const SubToken &token, entry.tokens) {
74 tokensBuff += encodeToken(token);
75 }
76 long ss = entry.frameStart / 1000;
77 int h = ss / 3600;
78 int m = (ss - h * 3600) / 60;
79 int s = ss - h * 3600 - m * 60;
80 return QString("%1:%2:%3: %4")
81 .arg(h, 2, 10, QChar('0'))
82 .arg(m, 2, 10, QChar('0'))
83 .arg(s, 2, 10, QChar('0'))
84 .arg(tokensBuff);
85 }
86
87 QString TMPlayerSubtitleFormat::encodeToken(const SubToken &entry) const {
88 switch (entry.type) {
89 case STT_WS:
90 return " ";
91 case STT_WORD:
92 return entry.payload;
93 case STT_NEWLINE:
94 return "|";
95 case STT_BOLD:
96 return "{y:b}";
97 case STT_ITALIC:
98 return "{y:i}";
99 case STT_UNDERLINE:
100 return "{y:u}";
101 case STT_FONTCOLOR:
102 return QString("{c:$%1}").arg(entry.payload);
103 default:
104 return "";
105 }
106 }