X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Fssa_reader.cc;h=dad0f2617215c99b8ef47a0b6656adf5199011e8;hb=b02d08547dd5739ede56cf73190b23f0bcd990a8;hp=f592b9e9371e86109bb374d60717014a45760982;hpb=5038e68ad9eb66e007211c8f1b707612a0c01e29;p=libsub.git diff --git a/src/ssa_reader.cc b/src/ssa_reader.cc index f592b9e..dad0f26 100644 --- a/src/ssa_reader.cc +++ b/src/ssa_reader.cc @@ -22,9 +22,10 @@ #include "sub_assert.h" #include "raw_convert.h" #include "subtitle.h" +#include "compose.hpp" #include -#include -#include +#include +#include #include #include @@ -32,10 +33,12 @@ using std::string; using std::vector; using std::map; using std::cout; -using std::list; using boost::optional; using boost::function; using namespace boost::algorithm; +#if BOOST_VERSION >= 106100 +using namespace boost::placeholders; +#endif using namespace sub; /** @param s Subtitle string encoded in UTF-8 */ @@ -50,6 +53,29 @@ SSAReader::SSAReader (FILE* f) this->read (boost::bind (&get_line_file, f)); } +Colour +h_colour (string s) +{ + if (s.empty() || s[0] != '&' || s[1] != 'H') { + throw SSAError(String::compose("Badly formatted colour tag %1", s)); + } + + auto start = s.c_str(); + auto const end = start + s.length(); + while (start < end && (*start == '&' || *start == 'H')) { + ++start; + } + + auto const colour = strtoll(start, nullptr, 16); + + /* XXX: ignoring alpha channel here; note that 00 is opaque and FF is transparent */ + return sub::Colour( + ((colour & 0x000000ff) >> 0) / 255.0, + ((colour & 0x0000ff00) >> 8) / 255.0, + ((colour & 0x00ff0000) >> 16) / 255.0 + ); +} + class Style { public: @@ -93,9 +119,9 @@ public: } else if (keys[i] == "Fontsize") { font_size = raw_convert (style[i]); } else if (keys[i] == "PrimaryColour") { - primary_colour = colour (raw_convert (style[i])); + primary_colour = colour (style[i]); } else if (keys[i] == "BackColour") { - back_colour = colour (raw_convert (style[i])); + back_colour = colour (style[i]); } else if (keys[i] == "Bold") { bold = style[i] == "-1"; } else if (keys[i] == "Italic") { @@ -107,28 +133,19 @@ public: effect = SHADOW; } } else if (keys[i] == "Alignment") { - /* These values from libass' source code */ - switch ((raw_convert (style[i]) - 1) % 3) { - case 0: - horizontal_reference = LEFT_OF_SCREEN; - break; - case 1: - horizontal_reference = HORIZONTAL_CENTRE_OF_SCREEN; - break; - case 2: - horizontal_reference = RIGHT_OF_SCREEN; - break; - } - switch (raw_convert (style[i]) & 12) { - case 4: + if (style[i] == "7" || style[i] == "8" || style[i] == "9") { vertical_reference = TOP_OF_SCREEN; - break; - case 8: + } else if (style[i] == "4" || style[i] == "5" || style[i] == "6") { vertical_reference = VERTICAL_CENTRE_OF_SCREEN; - break; - case 0: + } else { vertical_reference = BOTTOM_OF_SCREEN; - break; + } + if (style[i] == "1" || style[i] == "4" || style[i] == "7") { + horizontal_reference = LEFT_OF_SCREEN; + } else if (style[i] == "3" || style[i] == "6" || style[i] == "9") { + horizontal_reference = RIGHT_OF_SCREEN; + } else { + horizontal_reference = HORIZONTAL_CENTRE_OF_SCREEN; } } else if (keys[i] == "MarginV") { vertical_margin = raw_convert (style[i]); @@ -138,7 +155,7 @@ public: string name; optional font_name; - int font_size; + int font_size; ///< points Colour primary_colour; /** outline colour */ optional back_colour; @@ -151,13 +168,20 @@ public: int vertical_margin; private: - Colour colour (int c) const + Colour colour (string c) const { - return Colour ( - ((c & 0x0000ff) >> 0) / 255.0, - ((c & 0x00ff00) >> 8) / 255.0, - ((c & 0xff0000) >> 16) / 255.0 - ); + if (c.length() > 0 && c[0] == '&') { + /* &Hbbggrr or &Haabbggrr */ + return h_colour (c); + } else { + /* integer */ + int i = raw_convert(c); + return Colour ( + ((i & 0x0000ff) >> 0) / 255.0, + ((i & 0x00ff00) >> 8) / 255.0, + ((i & 0xff0000) >> 16) / 255.0 + ); + } } }; @@ -175,12 +199,77 @@ SSAReader::parse_time (string t) const ); } +void +SSAReader::parse_style(RawSubtitle& sub, string style, int play_res_x, int play_res_y, Colour primary_colour) +{ + if (style == "\\i1") { + sub.italic = true; + } else if (style == "\\i0" || style == "\\i") { + sub.italic = false; + } else if (style == "\\b1") { + sub.bold = true; + } else if (style == "\\b0") { + sub.bold = false; + } else if (style == "\\u1") { + sub.underline = true; + } else if (style == "\\u0") { + sub.underline = false; + } else if (style == "\\an1") { + sub.horizontal_position.reference = sub::LEFT_OF_SCREEN; + sub.vertical_position.reference = sub::BOTTOM_OF_SCREEN; + } else if (style == "\\an2") { + sub.horizontal_position.reference = sub::HORIZONTAL_CENTRE_OF_SCREEN; + sub.vertical_position.reference = sub::BOTTOM_OF_SCREEN; + } else if (style == "\\an3") { + sub.horizontal_position.reference = sub::RIGHT_OF_SCREEN; + sub.vertical_position.reference = sub::BOTTOM_OF_SCREEN; + } else if (style == "\\an4") { + sub.horizontal_position.reference = sub::LEFT_OF_SCREEN; + sub.vertical_position.reference = sub::VERTICAL_CENTRE_OF_SCREEN; + } else if (style == "\\an5") { + sub.horizontal_position.reference = sub::HORIZONTAL_CENTRE_OF_SCREEN; + sub.vertical_position.reference = sub::VERTICAL_CENTRE_OF_SCREEN; + } else if (style == "\\an6") { + sub.horizontal_position.reference = sub::RIGHT_OF_SCREEN; + sub.vertical_position.reference = sub::VERTICAL_CENTRE_OF_SCREEN; + } else if (style == "\\an7") { + sub.horizontal_position.reference = sub::LEFT_OF_SCREEN; + sub.vertical_position.reference = sub::TOP_OF_SCREEN; + } else if (style == "\\an8") { + sub.horizontal_position.reference = sub::HORIZONTAL_CENTRE_OF_SCREEN; + sub.vertical_position.reference = sub::TOP_OF_SCREEN; + } else if (style == "\\an9") { + sub.horizontal_position.reference = sub::RIGHT_OF_SCREEN; + sub.vertical_position.reference = sub::TOP_OF_SCREEN; + } else if (boost::starts_with(style, "\\pos")) { + vector bits; + boost::algorithm::split (bits, style, boost::is_any_of("(,")); + SUB_ASSERT (bits.size() == 3); + sub.horizontal_position.reference = sub::LEFT_OF_SCREEN; + sub.horizontal_position.proportional = raw_convert(bits[1]) / play_res_x; + sub.vertical_position.reference = sub::TOP_OF_SCREEN; + sub.vertical_position.proportional = raw_convert(bits[2]) / play_res_y; + } else if (boost::starts_with(style, "\\fs")) { + SUB_ASSERT (style.length() > 3); + sub.font_size.set_proportional(raw_convert(style.substr(3)) / play_res_y); + } else if (boost::starts_with(style, "\\c")) { + /* \c&Hbbggrr& */ + if (style.length() > 2) { + sub.colour = h_colour(style.substr(2, style.length() - 3)); + } else if (style.length() == 2) { + sub.colour = primary_colour; + } else { + throw SSAError(String::compose("Badly formatted colour tag %1", style)); + } + } +} + /** @param base RawSubtitle filled in with any required common values. * @param line SSA line string (i.e. just the subtitle, possibly with embedded stuff) * @return List of RawSubtitles to represent line with vertical reference TOP_OF_SUBTITLE. */ -list -SSAReader::parse_line (RawSubtitle base, string line, int play_res_x, int play_res_y) +vector +SSAReader::parse_line(RawSubtitle base, string line, int play_res_x, int play_res_y, Colour primary_colour) { enum { TEXT, @@ -188,7 +277,7 @@ SSAReader::parse_line (RawSubtitle base, string line, int play_res_x, int play_r BACKSLASH } state = TEXT; - list subs; + vector subs; RawSubtitle current = base; string style; @@ -196,16 +285,19 @@ SSAReader::parse_line (RawSubtitle base, string line, int play_res_x, int play_r current.vertical_position.reference = BOTTOM_OF_SCREEN; } - if (!current.vertical_position.proportional) { - current.vertical_position.proportional = 0; - } + /* Any vertical_position that is set in base (and therefore current) is a margin, which + * we need to ignore if we end up vertically centering this subtitle. + * Clear out vertical_position from current; we'll re-add it from base later + * if required. + */ + current.vertical_position.proportional = 0; /* We must have a font size, as there could be a margin specified in pixels and in that case we must know how big the subtitle lines are to work out the position on screen. */ - if (!current.font_size.points()) { - current.font_size.set_points (72); + if (!current.font_size.proportional()) { + current.font_size.set_proportional(72.0 / play_res_y); } /* Count the number of line breaks */ @@ -218,22 +310,8 @@ SSAReader::parse_line (RawSubtitle base, string line, int play_res_x, int play_r } } - /* Imagine that the screen is 792 points (i.e. 11 inches) high (as with DCP) */ - double const line_size = current.font_size.proportional(792) * 1.2; - - /* Tweak vertical_position accordingly */ - switch (current.vertical_position.reference.get()) { - case TOP_OF_SCREEN: - case TOP_OF_SUBTITLE: - /* Nothing to do */ - break; - case VERTICAL_CENTRE_OF_SCREEN: - current.vertical_position.proportional = current.vertical_position.proportional.get() - ((line_breaks + 1) * line_size) / 2; - break; - case BOTTOM_OF_SCREEN: - current.vertical_position.proportional = current.vertical_position.proportional.get() + line_breaks * line_size; - break; - } + /* There are vague indications that with ASS 1 point should equal 1 pixel */ + double const line_size = current.font_size.proportional(play_res_y) * 1.2; for (size_t i = 0; i < line.length(); ++i) { char const c = line[i]; @@ -253,36 +331,7 @@ SSAReader::parse_line (RawSubtitle base, string line, int play_res_x, int play_r subs.push_back (current); current.text = ""; } - if (style == "\\i1") { - current.italic = true; - } else if (style == "\\i0" || style == "\\i") { - current.italic = false; - } else if (style == "\\b1") { - current.bold = true; - } else if (style == "\\b0") { - current.bold = false; - } else if (style == "\\u1") { - current.underline = true; - } else if (style == "\\u0") { - current.underline = false; - } else if (style == "\\an1" || style == "\\an2" || style == "\\an3") { - current.vertical_position.reference = sub::BOTTOM_OF_SCREEN; - } else if (style == "\\an4" || style == "\\an5" || style == "\\an6") { - current.vertical_position.reference = sub::VERTICAL_CENTRE_OF_SCREEN; - } else if (style == "\\an7" || style == "\\an8" || style == "\\an9") { - current.vertical_position.reference = sub::TOP_OF_SCREEN; - } else if (boost::starts_with(style, "\\pos")) { - vector bits; - boost::algorithm::split (bits, style, boost::is_any_of("(,")); - SUB_ASSERT (bits.size() == 3); - current.horizontal_position.reference = sub::LEFT_OF_SCREEN; - current.horizontal_position.proportional = raw_convert(bits[1]) / play_res_x; - current.vertical_position.reference = sub::TOP_OF_SCREEN; - current.vertical_position.proportional = raw_convert(bits[2]) / play_res_y; - } else if (boost::starts_with(style, "\\fs")) { - SUB_ASSERT (style.length() > 3); - current.font_size.set_points (raw_convert(style.substr(3))); - } + parse_style(current, style, play_res_x, play_res_y, primary_colour); style = ""; } @@ -314,6 +363,28 @@ SSAReader::parse_line (RawSubtitle base, string line, int play_res_x, int play_r subs.push_back (current); } + /* Now we definitely know the vertical position reference we can finish off the position */ + for (auto& sub: subs) { + switch (sub.vertical_position.reference.get()) { + case TOP_OF_SCREEN: + case TOP_OF_SUBTITLE: + /* Just re-add any margins we came in with */ + sub.vertical_position.proportional = sub.vertical_position.proportional.get() + base.vertical_position.proportional.get_value_or(0); + break; + case VERTICAL_CENTRE_OF_SCREEN: + /* Margins are ignored, but we need to centre */ + sub.vertical_position.proportional = sub.vertical_position.proportional.get() - ((line_breaks + 1) * line_size) / 2; + break; + case BOTTOM_OF_SCREEN: + /* Re-add margins and account for each line */ + sub.vertical_position.proportional = + sub.vertical_position.proportional.get() + + base.vertical_position.proportional.get_value_or(0) + + line_breaks * line_size; + break; + } + } + return subs; } @@ -383,7 +454,7 @@ SSAReader::read (function ()> get_line) case EVENTS: if (type == "Format") { split (event_format, body, is_any_of (",")); - BOOST_FOREACH (string& i, event_format) { + for (auto& i: event_format) { trim (i); } } else if (type == "Dialogue") { @@ -404,6 +475,7 @@ SSAReader::read (function ()> get_line) SUB_ASSERT (event_format.size() == event.size()); RawSubtitle sub; + optional