Strip Unicode U+202B (right-to-left-embedding) code; it looks like DoM does RTL ...
authorCarl Hetherington <cth@carlh.net>
Wed, 23 Jan 2019 21:44:20 +0000 (21:44 +0000)
committerCarl Hetherington <cth@carlh.net>
Wed, 23 Jan 2019 21:44:20 +0000 (21:44 +0000)
src/subrip_reader.cc
test/subrip_reader_test.cc

index 02ee20acddfcb4ca7b7a99ebeac6c81d27f7931c..b5d04466bc7bb3e1f9cac167f75f204735b2642b 100644 (file)
@@ -42,6 +42,7 @@ using boost::lexical_cast;
 using boost::to_upper;
 using boost::optional;
 using boost::function;
+using boost::algorithm::replace_all;
 using namespace sub;
 
 /** @param s Subtitle string encoded in UTF-8 */
@@ -233,6 +234,11 @@ SubripReader::convert_line (string t, RawSubtitle& p)
                }
        }
 
+       /* Strip Unicode U+202B (right-to-left embedding) as sometimes it is rendered
+          as a missing character.  This may be a hack.
+       */
+       replace_all (p.text, "\xe2\x80\xab", "");
+
        maybe_content (p);
 }
 
index f323f6a24e67e099161a73227a1270b0156666ea..c2702c48401c657d0747febb2828f0c1afd8ed1f 100644 (file)
@@ -501,3 +501,27 @@ BOOST_AUTO_TEST_CASE (subrip_reader_test3)
        BLOCK ("Both lines are bold AND italic", "Arial", 30, true, true, false);
        SUB_END ();
 }
+
+/** Test reading of a .srt file with RTL text */
+BOOST_AUTO_TEST_CASE (subrip_reader_test4)
+{
+       boost::filesystem::path p = private_test / "rtl.srt";
+       FILE* f = fopen (p.string().c_str(), "r");
+       sub::SubripReader reader (f);
+       fclose (f);
+       list<sub::Subtitle> subs = sub::collect<std::list<sub::Subtitle> >(reader.subtitles());
+
+       list<sub::Subtitle>::iterator i = subs.begin ();
+       std::cout << i->lines.front().blocks.front().text << "\n";
+
+       std::string const t = i->lines.front().blocks.front().text;
+       for (size_t i = 0; i < t.length() - 2; ++i) {
+               /* Check that unicode U+202B (right-to-left embedding) has been stripped */
+               unsigned char const a = t[i];
+               unsigned char const b = t[i+1];
+               unsigned char const c = t[i+2];
+               BOOST_CHECK ((a != 0xe2 || b != 0x80 || c != 0xab));
+       }
+
+       BOOST_CHECK (t == "- \"(دريه فابينار)\"");
+}