/* Copyright (C) 2016 Carl Hetherington This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "ssa_reader.h" #include "util.h" #include "sub_assert.h" #include "raw_convert.h" #include "subtitle.h" #include #include #include #include #include #include #include using std::string; using std::stringstream; using std::vector; using std::map; using std::cout; using std::list; using boost::optional; using boost::function; using namespace boost::algorithm; using namespace sub; /** @param s Subtitle string encoded in UTF-8 */ SSAReader::SSAReader (string const & s) { char* buffer = new char[s.length() + 1]; strcpy (buffer, s.c_str()); read (buffer, s.length() + 1); } /** @param f Subtitle file encoded in UTF-8 */ SSAReader::SSAReader (FILE* f) { fseek (f, 0, SEEK_END); long const size = ftell (f); fseek (f, 0, SEEK_SET); char* buffer = new char[size]; fread (buffer, size, 1, f); read (buffer, size); } void SSAReader::read (char* buffer, int size) { ASS_Library* library = ass_library_init (); if (!library) { delete[] buffer; throw std::runtime_error ("Could not initialise libass"); } char encoding[] = "UTF-8"; ASS_Track* track = ass_read_memory (library, buffer, size, encoding); delete[] buffer; for (int i = 0; i < track->n_events; ++i) { sub::RawSubtitle sub; ASS_Event const & event = track->events[i]; ASS_Style const & style = track->styles[event.Style]; sub.text = event.Text; sub.font = style.FontName; sub.font_size.set_proportional (style.FontSize / track->PlayResY); /* XXX: effect, effect_colour */ sub.bold = style.Bold; sub.italic = style.Italic; sub.underline = style.Underline; cout << "align " << style.Alignment << "\n"; int valign = style.Alignment & 12; switch (valign) { case VALIGN_SUB: sub.vertical_position.reference = BOTTOM_OF_SCREEN; break; case VALIGN_CENTER: sub.vertical_position.reference = CENTRE_OF_SCREEN; break; case VALIGN_TOP: sub.vertical_position.reference = TOP_OF_SCREEN; break; } sub.vertical_position.proportional = style.MarginV / track->PlayResY; sub.from = Time::from_milliseconds (event.Start); sub.to = sub.from + Time::from_milliseconds (event.Duration); _subs.push_back (sub); } ass_free_track (track); ass_library_done (library); }