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