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.
34 using std::shared_ptr;
36 using boost::optional;
39 /** @param s A string.
40 * @return true if the string contains only space, newline or tab characters, or is empty.
43 sub::empty_or_white_space (string s)
45 for (size_t i = 0; i < s.length(); ++i) {
46 if (s[i] != ' ' && s[i] != '\n' && s[i] != '\t') {
55 sub::get_line_string (string* s)
57 if (s->length() == 0) {
58 return optional<string>();
61 size_t pos = s->find ("\n");
62 if (pos == string::npos) {
68 string const c = s->substr (0, pos);
69 s->erase (0, pos + 1);
74 sub::get_line_file (FILE* f)
77 char* r = fgets (buffer, sizeof (buffer), f);
79 return optional<string> ();
82 return string (buffer);
86 sub::remove_unicode_bom (optional<string>& line)
89 line->length() >= 3 &&
90 static_cast<unsigned char> (line.get()[0]) == 0xef &&
91 static_cast<unsigned char> (line.get()[1]) == 0xbb &&
92 static_cast<unsigned char> (line.get()[2]) == 0xbf
95 /* Skip Unicode byte order mark */
96 line = line->substr (3);
101 sub::dump (shared_ptr<const Reader> reader, ostream& os)
103 auto metadata = reader->metadata ();
104 for (auto const& i: metadata) {
105 os << i.first << ": " << i.second << "\n";
108 auto subs = collect<vector<sub::Subtitle>> (reader->subtitles());
110 for (auto const& i: subs) {
111 os << "Subtitle " << n << " at " << i.from << " -> " << i.to << "\n";
112 for (auto const& j: i.lines) {
116 if (j.vertical_position.proportional) {
117 os << j.vertical_position.proportional.get() << " of screen";
118 } else if (j.vertical_position.line && j.vertical_position.lines) {
119 os << j.vertical_position.line.get() << " lines of " << j.vertical_position.lines.get();
121 if (j.vertical_position.reference) {
123 switch (j.vertical_position.reference.get()) {
127 case VERTICAL_CENTRE_OF_SCREEN:
130 case BOTTOM_OF_SCREEN:
133 case TOP_OF_SUBTITLE:
134 os << "top of subtitle";
141 bool underline = false;
142 for (auto const& k: j.blocks) {
143 if (k.italic && !italic) {
145 } else if (italic && !k.italic) {
148 if (k.underline && !underline) {
150 } else if (underline && !k.underline) {
155 underline = k.underline;