2 Copyright (C) 2012-2020 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 using std::shared_ptr;
37 using boost::optional;
40 /** @param s A string.
41 * @return true if the string contains only space, newline or tab characters, or is empty.
44 sub::empty_or_white_space (string s)
46 for (size_t i = 0; i < s.length(); ++i) {
47 if (s[i] != ' ' && s[i] != '\n' && s[i] != '\t') {
56 sub::get_line_string (string* s)
58 if (s->length() == 0) {
59 return optional<string>();
62 size_t pos = s->find ("\n");
63 if (pos == string::npos) {
69 string const c = s->substr (0, pos);
70 s->erase (0, pos + 1);
75 sub::get_line_file (FILE* f)
78 char* r = fgets (buffer, sizeof (buffer), f);
80 return optional<string> ();
83 return string (buffer);
87 sub::remove_unicode_bom (optional<string>& line)
90 line->length() >= 3 &&
91 static_cast<unsigned char> (line.get()[0]) == 0xef &&
92 static_cast<unsigned char> (line.get()[1]) == 0xbb &&
93 static_cast<unsigned char> (line.get()[2]) == 0xbf
96 /* Skip Unicode byte order mark */
97 line = line->substr (3);
102 sub::dump (shared_ptr<const Reader> reader, ostream& os)
104 auto metadata = reader->metadata ();
105 for (auto const& i: metadata) {
106 os << i.first << ": " << i.second << "\n";
109 auto subs = collect<vector<sub::Subtitle>> (reader->subtitles());
111 for (auto const& i: subs) {
112 os << "Subtitle " << n << " at " << i.from << " -> " << i.to << "\n";
113 for (auto const& j: i.lines) {
117 if (j.vertical_position.proportional) {
118 os << j.vertical_position.proportional.get() << " of screen";
119 } else if (j.vertical_position.line && j.vertical_position.lines) {
120 os << j.vertical_position.line.get() << " lines of " << j.vertical_position.lines.get();
122 if (j.vertical_position.reference) {
124 switch (j.vertical_position.reference.get()) {
128 case VERTICAL_CENTRE_OF_SCREEN:
131 case BOTTOM_OF_SCREEN:
134 case TOP_OF_SUBTITLE:
135 os << "top of subtitle";
142 bool underline = false;
143 for (auto const& k: j.blocks) {
144 if (k.italic && !italic) {
146 } else if (italic && !k.italic) {
149 if (k.underline && !underline) {
151 } else if (underline && !k.underline) {
156 underline = k.underline;