diff options
| -rw-r--r-- | src/exceptions.h | 8 | ||||
| -rw-r--r-- | src/ssa_reader.cc | 11 | ||||
| -rw-r--r-- | test/ssa_reader_test.cc | 34 |
3 files changed, 52 insertions, 1 deletions
diff --git a/src/exceptions.h b/src/exceptions.h index 56b58aa..2bb7018 100644 --- a/src/exceptions.h +++ b/src/exceptions.h @@ -65,6 +65,14 @@ private: std::list<std::string> _context; }; +class SSAError : public std::runtime_error +{ +public: + SSAError (std::string message) + : std::runtime_error(message) + {} +}; + class MXFError : public std::runtime_error { public: diff --git a/src/ssa_reader.cc b/src/ssa_reader.cc index a1672d8..bfcd0de 100644 --- a/src/ssa_reader.cc +++ b/src/ssa_reader.cc @@ -22,6 +22,7 @@ #include "sub_assert.h" #include "raw_convert.h" #include "subtitle.h" +#include "compose.hpp" #include <locked_sstream.h> #include <boost/algorithm/string.hpp> #include <boost/bind.hpp> @@ -284,6 +285,16 @@ SSAReader::parse_line (RawSubtitle base, string line, int play_res_x, int play_r } else if (boost::starts_with(style, "\\fs")) { SUB_ASSERT (style.length() > 3); current.font_size.set_points (raw_convert<int>(style.substr(3))); + } else if (boost::starts_with(style, "\\c")) { + /* \c&Hbbggrr& */ + if (style.length() != 11 || style[2] != '&' || style[3] != 'H' || style[10] != '&') { + throw SSAError(String::compose("Badly formatted colour tag %1", style)); + } + int ir, ig, ib; + if (sscanf(style.c_str() + 4, "%2x%2x%2x", &ib, &ig, &ir) < 3) { + throw SSAError(String::compose("Badly formatted colour tag %1", style)); + } + current.colour = sub::Colour(ir / 255.0, ig / 255.0, ib / 255.0); } style = ""; } diff --git a/test/ssa_reader_test.cc b/test/ssa_reader_test.cc index 1b4d596..4aa8ff5 100644 --- a/test/ssa_reader_test.cc +++ b/test/ssa_reader_test.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2016 Carl Hetherington <cth@carlh.net> + Copyright (C) 2016-2019 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 @@ -21,6 +21,7 @@ #include "ssa_reader.h" #include "collect.h" #include "subtitle.h" +#include "exceptions.h" #include <boost/test/unit_test.hpp> #include <boost/filesystem.hpp> #include <boost/foreach.hpp> @@ -494,3 +495,34 @@ BOOST_AUTO_TEST_CASE (ssa_reader_fs) ++i; BOOST_REQUIRE (i == r.end ()); } + +/** Test a valid \c */ +BOOST_AUTO_TEST_CASE (ssa_reader_c) +{ + sub::RawSubtitle base; + list<sub::RawSubtitle> r = sub::SSAReader::parse_line ( + base, + "{\\c&H00FFFF&}Dieser Untertitel ist gelb", + 1920, 1080 + ); + + list<sub::RawSubtitle>::const_iterator i = r.begin (); + BOOST_CHECK_EQUAL (i->text, "Dieser Untertitel ist gelb"); + BOOST_CHECK (i->colour == sub::Colour::from_rgb_hex("ffff00")); + ++i; + BOOST_REQUIRE (i == r.end ()); +} + +/** Test invalid \c */ +BOOST_AUTO_TEST_CASE (ssa_reader_c_bad) +{ + sub::RawSubtitle base; + BOOST_CHECK_THROW( + sub::SSAReader::parse_line( + base, + "{\\c&H0}Dieser Untertitel ist gelb", + 1920, 1080 + ), + sub::SSAError + ); +} |
