2013-02-07 09:56:07 -08:00
|
|
|
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
|
2006-06-30 13:56:16 -07:00
|
|
|
//
|
2013-02-07 09:56:07 -08: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-06-30 13:56:16 -07:00
|
|
|
//
|
2013-02-07 09:56:07 -08: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-06-30 13:56:16 -07:00
|
|
|
//
|
2009-07-28 22:43:02 -07:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
2006-06-30 13:56:16 -07:00
|
|
|
#include "ass_attachment.h"
|
|
|
|
|
2013-02-07 09:36:47 -08:00
|
|
|
#include <libaegisub/ass/uuencode.h>
|
2014-03-21 07:44:22 -07:00
|
|
|
#include <libaegisub/file_mapping.h>
|
2011-12-22 13:15:37 -08:00
|
|
|
#include <libaegisub/io.h>
|
|
|
|
|
2013-01-04 07:01:50 -08:00
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
|
2015-09-15 14:35:07 -07:00
|
|
|
// Out-of-line to anchor vtable
|
2014-12-28 20:58:21 -08:00
|
|
|
AssEntryGroup AssAttachment::Group() const { return group; }
|
|
|
|
|
2013-12-09 13:45:28 -08:00
|
|
|
AssAttachment::AssAttachment(std::string const& header, AssEntryGroup group)
|
|
|
|
: entry_data(header + "\r\n")
|
|
|
|
, filename(header.substr(10))
|
2012-11-24 16:08:29 -08:00
|
|
|
, group(group)
|
2011-12-22 13:15:28 -08:00
|
|
|
{
|
2013-01-04 07:01:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
AssAttachment::AssAttachment(agi::fs::path const& name, AssEntryGroup group)
|
2013-02-07 09:56:07 -08:00
|
|
|
: filename(name.filename().string())
|
2013-01-04 07:01:50 -08:00
|
|
|
, group(group)
|
|
|
|
{
|
2013-12-09 13:45:28 -08:00
|
|
|
// SSA stuffs some information about the font in the embedded filename, but
|
2013-02-07 09:56:07 -08:00
|
|
|
// nothing else uses it so just do the absolute minimum (0 is the encoding)
|
|
|
|
if (boost::iends_with(filename.get(), ".ttf"))
|
|
|
|
filename = filename.get().substr(0, filename.get().size() - 4) + "_0" + filename.get().substr(filename.get().size() - 4);
|
2013-01-04 07:01:50 -08:00
|
|
|
|
2014-03-21 07:44:22 -07:00
|
|
|
agi::read_file_mapping file(name);
|
2014-03-21 11:08:26 -07:00
|
|
|
auto buff = file.read();
|
2013-06-12 15:54:19 -07:00
|
|
|
entry_data = (group == AssEntryGroup::FONT ? "fontname: " : "filename: ") + filename.get() + "\r\n";
|
2014-12-28 12:45:55 -08:00
|
|
|
entry_data = entry_data.get() + agi::ass::UUEncode(buff, buff + file.size());
|
2006-06-30 13:56:16 -07:00
|
|
|
}
|
|
|
|
|
2013-02-07 09:56:07 -08:00
|
|
|
size_t AssAttachment::GetSize() const {
|
|
|
|
auto header_end = entry_data.get().find('\n');
|
|
|
|
return entry_data.get().size() - header_end - 1;
|
2006-06-30 16:37:30 -07:00
|
|
|
}
|
|
|
|
|
2013-01-04 07:01:50 -08:00
|
|
|
void AssAttachment::Extract(agi::fs::path const& filename) const {
|
2013-02-07 09:56:07 -08:00
|
|
|
auto header_end = entry_data.get().find('\n');
|
2014-12-28 12:45:55 -08:00
|
|
|
auto decoded = agi::ass::UUDecode(entry_data.get().c_str() + header_end + 1, &entry_data.get().back() + 1);
|
2013-10-24 08:53:33 -07:00
|
|
|
agi::io::Save(filename, true).Get().write(&decoded[0], decoded.size());
|
2006-06-30 20:06:21 -07:00
|
|
|
}
|
|
|
|
|
2013-01-04 07:01:50 -08:00
|
|
|
std::string AssAttachment::GetFileName(bool raw) const {
|
2013-02-07 09:56:07 -08:00
|
|
|
if (raw || !boost::iends_with(filename.get(), ".ttf")) return filename;
|
2006-07-07 19:30:18 -07:00
|
|
|
|
|
|
|
// Remove stuff after last underscore if it's a font
|
2013-02-07 09:56:07 -08:00
|
|
|
std::string::size_type last_under = filename.get().rfind('_');
|
2013-01-04 07:01:50 -08:00
|
|
|
if (last_under == std::string::npos)
|
2011-12-22 13:15:28 -08:00
|
|
|
return filename;
|
2006-06-30 20:06:21 -07:00
|
|
|
|
2013-02-07 09:56:07 -08:00
|
|
|
return filename.get().substr(0, last_under) + ".ttf";
|
2006-06-30 14:21:30 -07:00
|
|
|
}
|