Support font color attributes with alpha channel (even if it's ignored). v1.6.43
authorCarl Hetherington <cth@carlh.net>
Fri, 24 Feb 2023 21:32:13 +0000 (22:32 +0100)
committerCarl Hetherington <cth@carlh.net>
Fri, 24 Feb 2023 21:32:13 +0000 (22:32 +0100)
src/subrip_reader.cc
test/subrip_reader_test.cc

index a59b43fbedfe8642f836d3e95160696ef60dd864..268582dc66f37027a8965dc3cd7d1c110c5013d2 100644 (file)
@@ -269,9 +269,16 @@ SubripReader::convert_line (string t, RawSubtitle& p)
                                ++i;
                        }
                        ++i;
-                       if (boost::regex_search (tag, match, re) && string (match[1]).size() == 6) {
-                               p.colour = Colour::from_rgb_hex (match[1]);
-                               colours.push_back (p.colour);
+                       if (boost::regex_search(tag, match, re)) {
+                               if (string(match[1]).size() == 6) {
+                                       p.colour = Colour::from_rgb_hex(match[1]);
+                                       colours.push_back(p.colour);
+                               } else if (string(match[1]).size() == 8) {
+                                       p.colour = Colour::from_rgba_hex(match[1]);
+                                       colours.push_back(p.colour);
+                               } else {
+                                       throw SubripError(tag, "a colour in the format #rrggbb #rrggbbaa or rgba(rr,gg,bb,aa)", _context);
+                               }
                        } else {
                                re = boost::regex (
                                        ".*color=\"rgba\\("
@@ -287,7 +294,7 @@ SubripReader::convert_line (string t, RawSubtitle& p)
                                        p.colour.b = raw_convert<int>(string(match[3])) / 255.0;
                                        colours.push_back (p.colour);
                                } else {
-                                       throw SubripError (tag, "a colour in the format #rrggbb or rgba(rr,gg,bb,aa)", _context);
+                                       throw SubripError (tag, "a colour in the format #rrggbb #rrggbbaa or rgba(rr,gg,bb,aa)", _context);
                                }
                        }
                } else if (has_next(t, i, "</font>")) {
index aa3760236215cb8e21e2d8739fc28f703857cc31..dc9698d9332497431866daf4b6992f4c47c37e59 100644 (file)
@@ -396,6 +396,17 @@ BOOST_AUTO_TEST_CASE (subrip_reader_convert_line_test)
        BOOST_CHECK_CLOSE (i->colour.b, 1, 0.1);
        r._subs.clear ();
 
+       rs = sub::RawSubtitle();
+       r.convert_line("<font color=\"#0000ffdd\">some blue text with alpha</font>", rs);
+       BOOST_CHECK_EQUAL(r._subs.size(), 1);
+       i = r._subs.begin();
+       BOOST_CHECK_EQUAL(i->text, "some blue text with alpha");
+       BOOST_CHECK_EQUAL(i->bold, false);
+       BOOST_CHECK(fabs(i->colour.r) < 0.01);
+       BOOST_CHECK(fabs(i->colour.g) < 0.01);
+       BOOST_CHECK_CLOSE(i->colour.b, 1, 0.1);
+       r._subs.clear();
+
        rs = sub::RawSubtitle();
        r.convert_line ("<< angle brackets but no HTML >>", rs);
        BOOST_CHECK_EQUAL (r._subs.size(), 1);