summaryrefslogtreecommitdiff
path: root/src/ssa_reader.cc
blob: a4daf24474453f4270d30beeba5ff3b1bba92735 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
    Copyright (C) 2016 Carl Hetherington <cth@carlh.net>

    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 <ass/ass.h>
#include <boost/algorithm/string.hpp>
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include <sstream>
#include <iostream>
#include <vector>

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);
}