This repository was archived by the owner on May 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.h
More file actions
322 lines (272 loc) · 8.46 KB
/
script.h
File metadata and controls
322 lines (272 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* This file is part of Re_Sync Next.
* Copyright (C) 2011-2019 Andrey Efremov <duxus@yandex.ru>
*
* Re_Sync Next is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Re_Sync Next is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Re_Sync Next. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef SCRIPT_H
#define SCRIPT_H
#include <QVector>
#include <QList>
#include <QString>
#include <QStringList>
#include <QTextStream>
namespace Script
{
enum ScriptType {SCR_UNKNOWN, SCR_ASS, SCR_SSA, SCR_SRT};
enum SectionType {SEC_UNKNOWN, SEC_HEADER, SEC_STYLES, SEC_EVENTS, SEC_FONTS, SEC_GRAPHICS};
namespace Sections
{
const QString header = "Script Info";
const QString stylesSSA = "V4 Styles";
const QString stylesASS = "V4+ Styles";
const QString events = "Events";
const QString fonts = "Fonts";
const QString graphics = "Graphics";
}
namespace Line
{
const QVector<ushort> AlignmentSSA = {0, 1, 2, 3, 0, 7, 8, 9, 0, 4, 5, 6};
const QVector<ushort> AlignmentASS = {0, 1, 2, 3, 9, 10, 11, 5, 6, 7};
const QString defaultStyle = "Default";
const QString defaultFont = "Arial";
uint StrToTime(const QString& str, const ScriptType type);
QString TimeToStr(const uint time, const ScriptType type);
// Базовая строка
class Base
{
public:
Base();
Base(const QString& value);
QString generate(const ScriptType type) const;
private:
QString _value;
};
// Простейшая строка с двоеточием
class Named : public Base
{
public:
QString text;
Named(const QString& name);
Named(const QString& name, const QStringList& before);
void clearBefore();
QString name() const;
QString generate(const ScriptType type) const;
protected:
QString _name;
QStringList _before;
QString generate(const ScriptType type, const QString& value) const;
};
// Строка стиля
class Style : public Named
{
public:
QString styleName;
QString fontName;
double fontSize;
quint32 primaryColour;
quint32 secondaryColour;
quint32 outlineColour;
quint32 backColour;
bool bold;
bool italic;
bool underline;
bool strikeOut;
double scaleX;
double scaleY;
double spacing;
double angle;
ushort borderStyle;
double outline;
double shadow;
ushort alignment;
ushort marginL;
ushort marginR;
ushort marginV;
ushort encoding;
Style();
Style(const QStringList& before);
QString generate(const ScriptType type) const;
private:
void init();
};
// Строка события
class Event : public Named
{
public:
uint layer;
uint start;
uint end;
QString style;
QString actorName;
ushort marginL;
ushort marginR;
ushort marginV;
QString effect;
Event();
Event(const QStringList& before);
QString generate(const ScriptType type) const;
private:
void init();
};
}
// Секция в файле
template <class T>
class Section
{
public:
QList<T*> content;
Section(const Section<T> &original) :
_sectionType(original._sectionType),
_after(original._after)
{
for (const T* const e : original.content) content.append(new T(*e));
}
Section(const SectionType sectionType) :
_sectionType(sectionType)
{}
~Section()
{
qDeleteAll(content);
}
void clearAfter()
{
_after.clear();
}
void clear()
{
qDeleteAll(content);
content.clear();
clearAfter();
}
bool isEmpty() const
{
return content.isEmpty() && _after.isEmpty();
}
void appendAfter(const QStringList& after)
{
_after.append(after);
}
void append(T* ptr)
{
content.append(ptr);
}
QString generate(const ScriptType type) const
{
QString result;
if (SCR_ASS == type || SCR_SSA == type)
{
switch (_sectionType)
{
case SEC_HEADER:
result.append( QString("[%1]\n").arg(Sections::header) );
break;
case SEC_STYLES:
if (SCR_ASS == type)
{
result.append( QString("[%1]\n").arg(Sections::stylesASS) );
result.append("Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\n");
}
else
{
result.append( QString("[%1]\n").arg(Sections::stylesSSA) );
result.append("Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\n");
}
break;
case SEC_EVENTS:
result.append( QString("[%1]\n").arg(Sections::events) );
if (SCR_ASS == type)
{
result.append("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n");
}
else
{
result.append("Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\n");
}
break;
case SEC_FONTS:
result.append( QString("[%1]\n").arg(Sections::fonts) );
break;
case SEC_GRAPHICS:
result.append( QString("[%1]\n").arg(Sections::graphics) );
break;
default:
break;
}
for (const T* const e : qAsConst(content))
{
result.append( e->generate(type) );
result.append("\n");
}
// Уродливый костыль
if (SEC_HEADER == _sectionType)
{
if (SCR_ASS == type)
{
result.append("ScriptType: v4.00+\n");
}
else
{
result.append("ScriptType: v4.00\n");
}
}
if (_after.length())
{
result.append( _after.join("\n") );
result.append("\n");
}
}
else if (SCR_SRT == type && SEC_EVENTS == _sectionType)
{
for (typename QList<T*>::size_type i = 0, len = content.length(); i < len; ++i)
{
result.append( QString("%1\n").arg(i + 1) );
result.append( content.at(i)->generate(type) );
result.append("\n\n");
}
}
return result;
}
private:
SectionType _sectionType;
QStringList _after;
};
// Скрипт
class Script
{
public:
Script();
Section<Line::Named> header;
Section<Line::Style> styles;
Section<Line::Event> events;
Section<Line::Base> fonts;
Section<Line::Base> graphics;
void clearBefore();
void clearAfter();
void clear();
void appendBefore(const QStringList& before);
void appendAfter(const QStringList& after);
QString generate(const ScriptType type) const;
private:
QStringList _before;
QStringList _after;
};
ScriptType DetectFormat(QTextStream& in);
bool ParseSSA(QTextStream& in, Script& script);
bool ParseSRT(QTextStream& in, Script& script);
void GenerateSSA(QTextStream& out, const Script& script);
void GenerateASS(QTextStream& out, const Script& script);
void GenerateSRT(QTextStream& out, const Script& script);
}
#endif // SCRIPT_H