From 759a2e6150e2a611db90cd81c0b2f81699eb22bd Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Mon, 24 Jun 2019 12:31:56 +0100 Subject: Add support for \c in SSA. Backported from 2e4089247c02f1f1bf43ab880594ce0218dfbc11 in master. --- src/exceptions.h | 8 ++++++++ src/ssa_reader.cc | 11 +++++++++++ test/ssa_reader_test.cc | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 1 deletion(-) 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 _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 #include #include @@ -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(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 + Copyright (C) 2016-2019 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 @@ -21,6 +21,7 @@ #include "ssa_reader.h" #include "collect.h" #include "subtitle.h" +#include "exceptions.h" #include #include #include @@ -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 r = sub::SSAReader::parse_line ( + base, + "{\\c&H00FFFF&}Dieser Untertitel ist gelb", + 1920, 1080 + ); + + list::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 + ); +} -- cgit v1.2.3