1
// Copyright (c) 2006, Rodrigo Braz Monteiro
2
// All rights reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are met:
7
// * Redistributions of source code must retain the above copyright notice,
8
// this list of conditions and the following disclaimer.
9
// * Redistributions in binary form must reproduce the above copyright notice,
10
// this list of conditions and the following disclaimer in the documentation
11
// and/or other materials provided with the distribution.
12
// * Neither the name of the Aegisub Group nor the names of its contributors
13
// may be used to endorse or promote products derived from this software
14
// without specific prior written permission.
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26
// POSSIBILITY OF SUCH DAMAGE.
28
// -----------------------------------------------------------------------------
32
// Website: http://aegisub.cellosoft.com
33
// Contact: mailto:zeratul@cellosoft.com
41
#include "subtitle_format_microdvd.h"
42
#include "ass_dialogue.h"
44
#include "text_file_reader.h"
45
#include "text_file_writer.h"
52
wxString MicroDVDSubtitleFormat::GetName() {
53
return _T("MicroDVD");
57
//////////////////////
59
wxArrayString MicroDVDSubtitleFormat::GetReadWildcards() {
60
wxArrayString formats;
61
formats.Add(_T("sub"));
66
///////////////////////
67
// Get write wildcards
68
wxArrayString MicroDVDSubtitleFormat::GetWriteWildcards() {
69
return GetReadWildcards();
75
bool MicroDVDSubtitleFormat::CanReadFile(wxString filename) {
76
// Return false immediately if extension is wrong
77
if (filename.Right(4).Lower() != _T(".sub")) return false;
79
// Since there is an infinity of .sub formats, load first line and check if it's valid
80
TextFileReader file(filename);
81
if (file.HasMoreLines() && !file.SmellsBinary()) {
82
wxRegEx exp(_T("^[\\{\\[]([0-9]+)[\\}\\]][\\{\\[]([0-9]+)[\\}\\]](.*)$"),wxRE_ADVANCED);
83
return exp.Matches(file.ReadLineFromFile());
92
bool MicroDVDSubtitleFormat::CanWriteFile(wxString filename) {
93
return (filename.Right(4).Lower() == _T(".sub"));
97
DEFINE_SIMPLE_EXCEPTION(MicrodvdFormatParseError, SubtitleFormatParseError, "subtitle_io/parse/microdvd")
101
void MicroDVDSubtitleFormat::ReadFile(wxString filename,wxString forceEncoding) {
102
// Load and prepare regexp
103
TextFileReader file(filename, forceEncoding);
104
wxRegEx exp(_T("^[\\{\\[]([0-9]+)[\\}\\]][\\{\\[]([0-9]+)[\\}\\]](.*)$"),wxRE_ADVANCED);
106
if (file.SmellsBinary())
107
throw new MicrodvdFormatParseError(_T("File seems to be binary. Maybe it's a VobSub format file? (Aegisub does not support VobSub format.)"), 0);
112
// Prepare conversion
114
FrameRate *rate = 𝔠
120
while (file.HasMoreLines()) {
121
wxString line = file.ReadLineFromFile();
122
if (exp.Matches(line)) {
125
exp.GetMatch(line,1).ToLong(&f1);
126
exp.GetMatch(line,2).ToLong(&f2);
127
wxString text = exp.GetMatch(line,3);
129
// If it's the first, check if it contains fps information
131
if (f1 == 1 && f2 == 1) {
140
// If it wasn't an fps line, ask the user for it
142
fps_rat = AskForFPS();
143
if (fps_rat.num == 0) return;
144
else if (fps_rat.num > 0) cfr.SetCFR(double(fps_rat.num)/double(fps_rat.den));
145
else rate = &VFR_Output;
153
// Start and end times
155
start = rate->GetTimeAtFrame(f1,true);
156
end = rate->GetTimeAtFrame(f2,false);
159
text.Replace(_T("|"),_T("\\N"));
161
// Create and insert line
162
AssDialogue *line = new AssDialogue();
163
line->group = _T("[Events]");
164
line->Style = _T("Default");
165
line->SetStartMS(start);
168
Line->push_back(line);
176
void MicroDVDSubtitleFormat::WriteFile(wxString filename,wxString encoding) {
179
FrameRate *rate = 𝔠
180
FPSRational fps_rat = AskForFPS();
181
if (fps_rat.num == 0 || fps_rat.den == 0) return;
182
double fps = double(fps_rat.num) / double(fps_rat.den);
183
if (fps > 0.0) cfr.SetCFR(fps);
184
else rate = &VFR_Output;
192
ConvertTags(1,_T("|"));
195
TextFileWriter file(filename,encoding);
198
if (rate->GetFrameRateType() != VFR) {
199
file.WriteLineToFile(wxString::Format(_T("{1}{1}%.6f"),rate->GetAverage()));
204
for (list<AssEntry*>::iterator cur=Line->begin();cur!=Line->end();cur++) {
205
AssDialogue *current = AssEntry::GetAsDialogue(*cur);
206
if (current && !current->Comment) {
208
int start = rate->GetFrameAtTime(current->Start.GetMS(),true);
209
int end = rate->GetFrameAtTime(current->End.GetMS(),false);
212
file.WriteLineToFile(wxString::Format(_T("{%i}{%i}%s"),start,end,current->Text.c_str()));