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/
|
|
|
|
|
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"
|
2014-05-22 03:32:42 +02:00
|
|
|
#include "options.h"
|
2014-04-22 19:21:00 +02:00
|
|
|
#include "string_codec.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
|
|
|
|
2014-04-25 16:04:08 +02:00
|
|
|
#include <libaegisub/ass/uuencode.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/fs.h>
|
|
|
|
|
2014-05-29 14:57:27 +02:00
|
|
|
DEFINE_EXCEPTION(AssParseError, SubtitleFormatParseError);
|
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
|
|
|
|
2014-03-26 16:14:08 +01:00
|
|
|
void AssSubtitleFormat::ReadFile(AssFile *target, agi::fs::path const& filename, agi::vfr::Framerate const& fps, std::string const& encoding) const {
|
2013-01-04 16:01:50 +01:00
|
|
|
int version = !agi::fs::HasExtension(filename, "ssa");
|
2006-02-27 01:06:46 +01:00
|
|
|
|
2014-06-11 00:28:45 +02:00
|
|
|
TextFileReader file(filename, encoding);
|
|
|
|
AssParser parser(target, version);
|
|
|
|
while (file.HasMoreLines())
|
|
|
|
parser.AddLine(file.ReadLineFromFile());
|
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 {
|
2014-04-29 18:33:22 +02:00
|
|
|
const char *format(AssEntryGroup group) {
|
|
|
|
if (group == AssEntryGroup::DIALOGUE)
|
|
|
|
return "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text" LINEBREAK;
|
|
|
|
if (group == AssEntryGroup::STYLE)
|
|
|
|
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;
|
2014-04-29 18:02:59 +02:00
|
|
|
return nullptr;
|
2012-11-22 17:14:34 +01:00
|
|
|
}
|
|
|
|
|
2014-03-07 19:58:51 +01:00
|
|
|
struct Writer {
|
|
|
|
TextFileWriter file;
|
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)
|
|
|
|
{
|
|
|
|
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-04-29 18:33:22 +02:00
|
|
|
file.WriteLineToFile(line.GroupHeader());
|
|
|
|
if (const char *str = format(line.Group()))
|
2014-04-29 18:02:59 +02:00
|
|
|
file.WriteLineToFile(str, false);
|
2012-11-22 17:14:34 +01:00
|
|
|
|
2014-03-07 18:02:24 +01:00
|
|
|
group = line.Group();
|
|
|
|
}
|
|
|
|
|
2014-04-29 18:33:22 +02:00
|
|
|
file.WriteLineToFile(line.GetEntryData());
|
2010-10-11 22:06:09 +02:00
|
|
|
}
|
2014-03-07 19:58:51 +01:00
|
|
|
}
|
2014-04-22 19:21:00 +02:00
|
|
|
|
2014-05-22 03:32:42 +02:00
|
|
|
void Write(ProjectProperties const& properties) {
|
|
|
|
file.WriteLineToFile("");
|
|
|
|
file.WriteLineToFile("[Aegisub Project Garbage]");
|
|
|
|
|
|
|
|
WriteIfNotEmpty("Automation Scripts: ", properties.automation_scripts);
|
|
|
|
WriteIfNotEmpty("Export Filters: ", properties.export_filters);
|
|
|
|
WriteIfNotEmpty("Export Encoding: ", properties.export_encoding);
|
|
|
|
WriteIfNotEmpty("Last Style Storage: ", properties.style_storage);
|
|
|
|
WriteIfNotEmpty("Audio File: ", properties.audio_file);
|
|
|
|
WriteIfNotEmpty("Video File: ", properties.video_file);
|
|
|
|
WriteIfNotEmpty("Timecodes File: ", properties.timecodes_file);
|
|
|
|
WriteIfNotEmpty("Keyframes File: ", properties.keyframes_file);
|
|
|
|
|
|
|
|
WriteIfNotZero("Video AR Mode: ", properties.ar_mode);
|
|
|
|
WriteIfNotZero("Video AR Value: ", properties.ar_value);
|
|
|
|
|
|
|
|
if (OPT_GET("App/Save UI State")->GetBool()) {
|
|
|
|
WriteIfNotZero("Video Zoom Percent: ", properties.video_zoom);
|
|
|
|
WriteIfNotZero("Scroll Position: ", properties.scroll_position);
|
|
|
|
WriteIfNotZero("Active Line: ", properties.active_row);
|
|
|
|
WriteIfNotZero("Video Position: ", properties.video_position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteIfNotEmpty(const char *key, std::string const& value) {
|
|
|
|
if (!value.empty())
|
|
|
|
file.WriteLineToFile(key + value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Number>
|
|
|
|
void WriteIfNotZero(const char *key, Number n) {
|
|
|
|
if (n != Number{})
|
|
|
|
file.WriteLineToFile(key + std::to_string(n));
|
|
|
|
}
|
|
|
|
|
2014-09-06 18:16:44 +02:00
|
|
|
void WriteExtradata(std::vector<ExtradataEntry> const& extradata) {
|
|
|
|
if (extradata.empty())
|
2014-04-22 19:21:00 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
group = AssEntryGroup::EXTRADATA;
|
|
|
|
file.WriteLineToFile("");
|
|
|
|
file.WriteLineToFile("[Aegisub Extradata]");
|
|
|
|
for (auto const& edi : extradata) {
|
|
|
|
std::string line = "Data: ";
|
2014-09-06 18:16:44 +02:00
|
|
|
line += std::to_string(edi.id);
|
2014-04-22 19:21:00 +02:00
|
|
|
line += ",";
|
2014-09-06 18:16:44 +02:00
|
|
|
line += inline_string_encode(edi.key);
|
2014-04-22 19:21:00 +02:00
|
|
|
line += ",";
|
2014-09-06 18:16:44 +02:00
|
|
|
std::string encoded_data = inline_string_encode(edi.value);
|
|
|
|
if (4*edi.value.size() < 3*encoded_data.size()) {
|
2014-04-25 16:04:08 +02:00
|
|
|
// the inline_string encoding grew the data by more than uuencoding would
|
|
|
|
// so base64 encode it instead
|
|
|
|
line += "u"; // marker for uuencoding
|
2014-12-28 21:45:55 +01:00
|
|
|
line += agi::ass::UUEncode(edi.value.c_str(), edi.value.c_str() + edi.value.size(), false);
|
2014-04-25 16:04:08 +02:00
|
|
|
} else {
|
|
|
|
line += "e"; // marker for inline_string encoding (escaping)
|
|
|
|
line += encoded_data;
|
|
|
|
}
|
2014-04-22 19:21:00 +02:00
|
|
|
file.WriteLineToFile(line);
|
|
|
|
}
|
|
|
|
}
|
2014-03-07 19:58:51 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-03-26 16:14:08 +01:00
|
|
|
void AssSubtitleFormat::WriteFile(const AssFile *src, agi::fs::path const& filename, agi::vfr::Framerate const& fps, std::string const& encoding) const {
|
2014-03-07 19:58:51 +01:00
|
|
|
Writer writer(filename, encoding);
|
|
|
|
writer.Write(src->Info);
|
2014-05-22 03:32:42 +02:00
|
|
|
writer.Write(src->Properties);
|
2014-03-07 19:58:51 +01:00
|
|
|
writer.Write(src->Styles);
|
|
|
|
writer.Write(src->Attachments);
|
|
|
|
writer.Write(src->Events);
|
2014-04-22 19:21:00 +02:00
|
|
|
writer.WriteExtradata(src->Extradata);
|
2006-02-27 22:57:10 +01:00
|
|
|
}
|
2014-09-19 20:47:34 +02:00
|
|
|
|
|
|
|
void AssSubtitleFormat::ExportFile(const AssFile *src, agi::fs::path const& filename, agi::vfr::Framerate const& fps, std::string const& encoding) const {
|
|
|
|
Writer writer(filename, encoding);
|
|
|
|
writer.Write(src->Info);
|
|
|
|
writer.Write(src->Styles);
|
|
|
|
writer.Write(src->Attachments);
|
|
|
|
writer.Write(src->Events);
|
|
|
|
}
|