Debian Codegraph @

libqnapi/src/subconvert/formats/microdvd.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 "microdvd.h"
16
17 bool MicroDVDSubtitleFormat::detect(const QStringList &lines) const {
18 foreach (QString line, lines) {
19 if (!line.trimmed().isEmpty()) {
20 QRegExp r("^\\{(\\d+)\\}\\{(\\d+)\\}(.*)");
21 r.setPatternSyntax(QRegExp::RegExp2);
22 return r.exactMatch(line);
23 }
24 }
25
26 return false;
27 }
28
29 SubFile MicroDVDSubtitleFormat::decode(const QStringList &lines) const {
30 SubFile sf;
31
32 foreach (QString line, lines) {
33 if (!line.trimmed().isEmpty()) {
34 QRegExp r("^\\{(\\d+)\\}\\{(\\d+)\\}(.*)");
35 r.setPatternSyntax(QRegExp::RegExp2);
36 if (r.exactMatch(line)) {
37 SubEntry se;
38 se.frameStart = r.cap(1).toLong();
39 se.frameStop = r.cap(2).toLong();
40 QString tokenStream = r.cap(3);
41
42 se.tokens = decodeTokenStream(tokenStream);
43
44 sf.entries.push_back(se);
45 }
46 }
47 }
48
49 return sf;
50 }
51
52 QStringList MicroDVDSubtitleFormat::encode(const SubFile &subFile) const {
53 QStringList lines;
54 foreach (const SubEntry &entry, subFile.entries) {
55 lines.append(encodeEntry(entry));
56 }
57 return lines;
58 }
59
60 QString MicroDVDSubtitleFormat::encodeEntry(const SubEntry &entry) const {
61 QString tokensBuff;
62 foreach (const SubToken &token, entry.tokens) {
63 tokensBuff += encodeToken(token);
64 }
65 return QString("{%1}{%2}%3")
66 .arg(entry.frameStart)
67 .arg(entry.frameStop)
68 .arg(tokensBuff);
69 }
70
71 QString MicroDVDSubtitleFormat::encodeToken(const SubToken &entry) const {
72 switch (entry.type) {
73 case STT_WS:
74 return " ";
75 case STT_WORD:
76 return entry.payload;
77 case STT_NEWLINE:
78 return "|";
79 case STT_BOLD:
80 return "{y:b}";
81 case STT_ITALIC:
82 return "{y:i}";
83 case STT_UNDERLINE:
84 return "{y:u}";
85 case STT_FONTCOLOR:
86 return QString("{c:$%1}").arg(entry.payload);
87 default:
88 return "";
89 }
90 }