2014-03-07 19:58:51 +01:00
|
|
|
// Copyright (c) 2014, Thomas Goyne <plorkyeran@aegisub.org>
|
2006-02-27 01:06:46 +01:00
|
|
|
//
|
2014-03-07 19:58:51 +01:00
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
2006-02-27 01:06:46 +01:00
|
|
|
//
|
2014-03-07 19:58:51 +01:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2006-02-27 01:06:46 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2006-02-27 01:06:46 +01:00
|
|
|
#include "subtitle_format_ass.h"
|
2011-09-28 21:44:53 +02:00
|
|
|
|
2014-03-07 19:58:51 +01:00
|
|
|
#include "ass_attachment.h"
|
|
|
|
#include "ass_dialogue.h"
|
|
|
|
#include "ass_info.h"
|
2012-01-26 21:08:38 +01:00
|
|
|
#include "ass_file.h"
|
2014-03-07 19:58:51 +01:00
|
|
|
#include "ass_style.h"
|
2012-10-12 04:12:42 +02:00
|
|
|
#include "ass_parser.h"
|
2006-02-27 01:06:46 +01:00
|
|
|
#include "text_file_reader.h"
|
2006-02-27 22:57:10 +01:00
|
|
|
#include "text_file_writer.h"
|
2012-10-12 05:03:00 +02:00
|
|
|
#include "version.h"
|
2006-02-27 01:06:46 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/fs.h>
|
|
|
|
|
2011-09-28 21:44:53 +02:00
|
|
|
DEFINE_SIMPLE_EXCEPTION(AssParseError, SubtitleFormatParseError, "subtitle_io/parse/ass")
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2012-10-12 03:52:36 +02:00
|
|
|
AssSubtitleFormat::AssSubtitleFormat()
|
2011-09-28 21:44:53 +02:00
|
|
|
: SubtitleFormat("Advanced Substation Alpha")
|
|
|
|
{
|
2006-12-26 19:26:13 +01:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::vector<std::string> AssSubtitleFormat::GetReadWildcards() const {
|
2014-03-07 19:58:51 +01:00
|
|
|
return {"ass", "ssa"};
|
2006-12-26 19:26:13 +01:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::vector<std::string> AssSubtitleFormat::GetWriteWildcards() const {
|
2014-03-07 19:58:51 +01:00
|
|
|
return {"ass", "ssa"};
|
2006-12-26 19:26:13 +01:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void AssSubtitleFormat::ReadFile(AssFile *target, agi::fs::path const& filename, std::string const& encoding) const {
|
2011-09-28 21:44:53 +02:00
|
|
|
TextFileReader file(filename, encoding);
|
2013-01-04 16:01:50 +01:00
|
|
|
int version = !agi::fs::HasExtension(filename, "ssa");
|
2012-10-12 04:12:42 +02:00
|
|
|
AssParser parser(target, version);
|
2006-02-27 01:06:46 +01:00
|
|
|
|
|
|
|
while (file.HasMoreLines()) {
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string line = file.ReadLineFromFile();
|
2006-02-27 01:06:46 +01:00
|
|
|
try {
|
2012-10-12 04:12:42 +02:00
|
|
|
parser.AddLine(line);
|
2006-02-27 01:06:46 +01:00
|
|
|
}
|
2011-09-28 21:43:11 +02:00
|
|
|
catch (const char *err) {
|
2013-11-21 18:13:36 +01:00
|
|
|
throw AssParseError("Error processing line: " + line + ": " + err, nullptr);
|
2006-02-27 01:06:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-02-27 22:57:10 +01:00
|
|
|
|
2012-11-22 17:14:34 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define LINEBREAK "\r\n"
|
|
|
|
#else
|
|
|
|
#define LINEBREAK "\n"
|
|
|
|
#endif
|
|
|
|
|
2014-03-07 19:58:51 +01:00
|
|
|
namespace {
|
|
|
|
std::string format(AssEntryGroup group, bool ssa) {
|
2013-06-13 00:54:19 +02:00
|
|
|
if (group == AssEntryGroup::DIALOGUE) {
|
2012-11-22 17:14:34 +01:00
|
|
|
if (ssa)
|
|
|
|
return "Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text" LINEBREAK;
|
|
|
|
else
|
|
|
|
return "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text" LINEBREAK;
|
|
|
|
}
|
|
|
|
|
2013-06-13 00:54:19 +02:00
|
|
|
if (group == AssEntryGroup::STYLE) {
|
2012-11-22 17:14:34 +01:00
|
|
|
if (ssa)
|
|
|
|
return "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding" LINEBREAK;
|
|
|
|
else
|
|
|
|
return "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding" LINEBREAK;
|
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
return "";
|
2012-11-22 17:14:34 +01:00
|
|
|
}
|
|
|
|
|
2014-03-07 19:58:51 +01:00
|
|
|
struct Writer {
|
|
|
|
TextFileWriter file;
|
|
|
|
bool ssa;
|
2013-06-13 00:54:19 +02:00
|
|
|
AssEntryGroup group = AssEntryGroup::INFO;
|
2006-02-27 22:57:10 +01:00
|
|
|
|
2014-03-07 19:58:51 +01:00
|
|
|
Writer(agi::fs::path const& filename, std::string const& encoding)
|
|
|
|
: file(filename, encoding)
|
|
|
|
, ssa(agi::fs::HasExtension(filename, "ssa"))
|
|
|
|
{
|
|
|
|
file.WriteLineToFile("[Script Info]");
|
|
|
|
file.WriteLineToFile(std::string("; Script generated by Aegisub ") + GetAegisubLongVersionString());
|
|
|
|
file.WriteLineToFile("; http://www.aegisub.org/");
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void Write(T const& list) {
|
2014-03-07 18:02:24 +01:00
|
|
|
for (auto const& line : list) {
|
|
|
|
if (line.Group() != group) {
|
|
|
|
// Add a blank line between each group
|
|
|
|
file.WriteLineToFile("");
|
2012-11-22 17:14:34 +01:00
|
|
|
|
2014-03-07 18:02:24 +01:00
|
|
|
file.WriteLineToFile(line.GroupHeader(ssa));
|
|
|
|
file.WriteLineToFile(format(line.Group(), ssa), false);
|
2012-11-22 17:14:34 +01:00
|
|
|
|
2014-03-07 18:02:24 +01:00
|
|
|
group = line.Group();
|
|
|
|
}
|
|
|
|
|
|
|
|
file.WriteLineToFile(ssa ? line.GetSSAText() : line.GetEntryData());
|
2010-10-11 22:06:09 +02:00
|
|
|
}
|
2014-03-07 19:58:51 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssSubtitleFormat::WriteFile(const AssFile *src, agi::fs::path const& filename, std::string const& encoding) const {
|
|
|
|
Writer writer(filename, encoding);
|
2006-03-13 23:25:09 +01:00
|
|
|
|
2014-03-07 19:58:51 +01:00
|
|
|
writer.Write(src->Info);
|
|
|
|
writer.Write(src->Styles);
|
|
|
|
writer.Write(src->Attachments);
|
|
|
|
writer.Write(src->Events);
|
2006-02-27 22:57:10 +01:00
|
|
|
}
|