2 Copyright (C) 2016 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "ssa_reader.h"
22 #include "sub_assert.h"
23 #include "raw_convert.h"
25 #include <boost/algorithm/string.hpp>
26 #include <boost/bind.hpp>
27 #include <boost/foreach.hpp>
33 using std::stringstream;
38 using boost::optional;
39 using boost::function;
40 using namespace boost::algorithm;
43 /** @param s Subtitle string encoded in UTF-8 */
44 SSAReader::SSAReader (string const & s)
47 this->read (boost::bind (&get_line_stringstream, &str));
50 /** @param f Subtitle file encoded in UTF-8 */
51 SSAReader::SSAReader (FILE* f)
53 this->read (boost::bind (&get_line_file, f));
61 , primary_colour (255, 255, 255)
64 , vertical_reference (BOTTOM_OF_SCREEN)
68 Style (string format_line, string style_line)
70 , primary_colour (255, 255, 255)
73 , vertical_reference (BOTTOM_OF_SCREEN)
77 split (keys, format_line, is_any_of (","));
79 split (style, style_line, is_any_of (","));
81 SUB_ASSERT (!keys.empty());
82 SUB_ASSERT (!style.empty());
83 SUB_ASSERT (keys.size() == style.size());
85 for (size_t i = 0; i < style.size(); ++i) {
88 if (keys[i] == "Name") {
90 } else if (keys[i] == "Fontname") {
92 } else if (keys[i] == "Fontsize") {
93 font_size = raw_convert<int> (style[i]);
94 } else if (keys[i] == "PrimaryColour") {
95 primary_colour = colour (raw_convert<int> (style[i]));
96 } else if (keys[i] == "BackColour") {
97 back_colour = colour (raw_convert<int> (style[i]));
98 } else if (keys[i] == "Bold") {
99 bold = style[i] == "-1";
100 } else if (keys[i] == "Italic") {
101 italic = style[i] == "-1";
102 } else if (keys[i] == "BorderStyle") {
103 if (style[i] == "1") {
106 } else if (keys[i] == "Alignment") {
107 /* These values from libass' source code */
108 switch (raw_convert<int> (style[i]) & 12) {
110 vertical_reference = TOP_OF_SCREEN;
113 vertical_reference = CENTRE_OF_SCREEN;
116 vertical_reference = BOTTOM_OF_SCREEN;
119 } else if (keys[i] == "MarginV") {
120 vertical_margin = raw_convert<int> (style[i]);
126 optional<string> font_name;
128 Colour primary_colour;
129 /** outline colour */
130 optional<Colour> back_colour;
133 optional<Effect> effect;
134 VerticalReference vertical_reference;
138 Colour colour (int c) const
141 ((c & 0x0000ff) >> 0) / 255.0,
142 ((c & 0x00ff00) >> 8) / 255.0,
143 ((c & 0xff0000) >> 16) / 255.0
149 SSAReader::parse_time (string t) const
152 split (bits, t, is_any_of (":."));
153 SUB_ASSERT (bits.size() == 4);
154 return Time::from_hms (
155 raw_convert<int> (bits[0]),
156 raw_convert<int> (bits[1]),
157 raw_convert<int> (bits[2]),
158 raw_convert<int> (bits[3]) * 10
162 /** @param base RawSubtitle filled in with any required common values.
163 * @param line SSA line string (i.e. just the subtitle, possibly with embedded stuff)
164 * @return List of RawSubtitles to represent line with vertical reference TOP_OF_SUBTITLE.
167 SSAReader::parse_line (RawSubtitle base, string line)
175 list<RawSubtitle> subs;
176 RawSubtitle current = base;
179 if (!current.vertical_position.reference) {
180 current.vertical_position.reference = BOTTOM_OF_SCREEN;
183 if (!current.vertical_position.proportional) {
184 current.vertical_position.proportional = 0;
187 /* We must have a font size, as there could be a margin specified
188 in pixels and in that case we must know how big the subtitle
189 lines are to work out the position on screen.
191 if (!current.font_size.points()) {
192 current.font_size.set_points (72);
195 /* Count the number of line breaks */
197 for (size_t i = 0; i < line.length() - 1; ++i) {
198 if (line[i] == '\\' && (line[i+1] == 'n' || line[i+1] == 'N')) {
203 /* Imagine that the screen is 792 points (i.e. 11 inches) high (as with DCP) */
204 double const line_size = current.font_size.proportional(792) * 1.2;
206 /* Tweak vertical_position accordingly */
207 switch (current.vertical_position.reference.get()) {
209 case TOP_OF_SUBTITLE:
212 case CENTRE_OF_SCREEN:
213 current.vertical_position.proportional = current.vertical_position.proportional.get() - ((line_breaks + 1) * line_size) / 2;
215 case BOTTOM_OF_SCREEN:
216 current.vertical_position.proportional = current.vertical_position.proportional.get() + line_breaks * line_size;
220 for (size_t i = 0; i < line.length(); ++i) {
221 char const c = line[i];
226 } else if (c == '\\') {
228 } else if (c != '\r' && c != '\n') {
234 if (!current.text.empty ()) {
235 subs.push_back (current);
238 if (style == "\\i1") {
239 current.italic = true;
240 } else if (style == "\\i0" || style == "\\i") {
241 current.italic = false;
242 } else if (style == "\\b1") {
244 } else if (style == "\\b0") {
245 current.bold = false;
246 } else if (style == "\\an1" || style == "\\an2" || style == "\\an3") {
247 current.vertical_position.reference = sub::BOTTOM_OF_SCREEN;
248 } else if (style == "\\an4" || style == "\\an5" || style == "\\an6") {
249 current.vertical_position.reference = sub::CENTRE_OF_SCREEN;
250 } else if (style == "\\an7" || style == "\\an8" || style == "\\an9") {
251 current.vertical_position.reference = sub::TOP_OF_SCREEN;
260 if (c == 'n' || c == 'N') {
261 if (!current.text.empty ()) {
262 subs.push_back (current);
265 /* Move down one line (1.2 times the font size) */
266 if (current.vertical_position.reference.get() == BOTTOM_OF_SCREEN) {
267 current.vertical_position.proportional = current.vertical_position.proportional.get() - line_size;
269 current.vertical_position.proportional = current.vertical_position.proportional.get() + line_size;
277 if (!current.text.empty ()) {
278 subs.push_back (current);
285 SSAReader::read (function<optional<string> ()> get_line)
293 int play_res_y = 288;
294 map<string, Style> styles;
295 string style_format_line;
296 vector<string> event_format;
299 optional<string> line = get_line ();
305 remove_unicode_bom (line);
307 if (starts_with (*line, ";") || line->empty ()) {
311 if (starts_with (*line, "[")) {
312 /* Section heading */
313 if (line.get() == "[Script Info]") {
315 } else if (line.get() == "[V4 Styles]" || line.get() == "[V4+ Styles]") {
317 } else if (line.get() == "[Events]") {
323 size_t const colon = line->find (":");
324 SUB_ASSERT (colon != string::npos);
325 string const type = line->substr (0, colon);
326 string body = line->substr (colon + 1);
331 if (type == "PlayResY") {
332 play_res_y = raw_convert<int> (body);
336 if (type == "Format") {
337 style_format_line = body;
338 } else if (type == "Style") {
339 SUB_ASSERT (!style_format_line.empty ());
340 Style s (style_format_line, body);
345 if (type == "Format") {
346 split (event_format, body, is_any_of (","));
347 BOOST_FOREACH (string& i, event_format) {
350 } else if (type == "Dialogue") {
351 SUB_ASSERT (!event_format.empty ());
352 vector<string> event;
353 split (event, body, is_any_of (","));
355 /* There may be commas in the subtitle part; reassemble any extra parts
356 from when we just split it.
358 while (event.size() > event_format.size()) {
359 string const ex = event.back ();
361 event.back() += "," + ex;
364 SUB_ASSERT (!event.empty());
365 SUB_ASSERT (event_format.size() == event.size());
369 for (size_t i = 0; i < event.size(); ++i) {
371 if (event_format[i] == "Start") {
372 sub.from = parse_time (event[i]);
373 } else if (event_format[i] == "End") {
374 sub.to = parse_time (event[i]);
375 } else if (event_format[i] == "Style") {
376 /* libass trims leading '*'s from style names, commenting that
377 "they seem to mean literally nothing". Go figure...
379 trim_left_if (event[i], boost::is_any_of ("*"));
380 SUB_ASSERT (styles.find(event[i]) != styles.end());
381 Style style = styles[event[i]];
382 sub.font = style.font_name;
383 sub.font_size = FontSize::from_points (style.font_size);
384 sub.colour = style.primary_colour;
385 sub.effect_colour = style.back_colour;
386 sub.bold = style.bold;
387 sub.italic = style.italic;
388 sub.effect = style.effect;
389 sub.vertical_position.reference = style.vertical_reference;
390 sub.vertical_position.proportional = float(style.vertical_margin) / play_res_y;
391 } else if (event_format[i] == "MarginV") {
392 sub.vertical_position.proportional = raw_convert<float>(event[i]) / play_res_y;
393 } else if (event_format[i] == "Text") {
394 BOOST_FOREACH (sub::RawSubtitle j, parse_line (sub, event[i])) {