Replace list with vector in most of the API.
[libsub.git] / src / stl_text_reader.cc
1 /*
2     Copyright (C) 2014-2016 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "stl_text_reader.h"
21 #include "compose.hpp"
22 #include <boost/algorithm/string.hpp>
23 #include <boost/lexical_cast.hpp>
24 #include <vector>
25 #include <iostream>
26
27 using std::ostream;
28 using std::istream;
29 using std::string;
30 using std::vector;
31 using std::cout;
32 using boost::algorithm::trim;
33 using boost::algorithm::starts_with;
34 using boost::is_any_of;
35 using boost::optional;
36 using boost::lexical_cast;
37 using namespace sub;
38
39 STLTextReader::STLTextReader (istream& in)
40 {
41         /* This reader extracts no information about where the subtitle
42            should be on screen, so its reference is TOP_OF_SUBTITLE.
43         */
44         _subtitle.vertical_position.line = 0;
45         _subtitle.vertical_position.reference = TOP_OF_SUBTITLE;
46
47         while (in.good ()) {
48                 string line;
49                 getline (in, line);
50                 if (!in.good ()) {
51                         return;
52                 }
53
54                 trim (line);
55
56                 if (starts_with (line, "//")) {
57                         continue;
58                 }
59
60                 if (line.size() > 0 && line[0] == '$') {
61                         /* $ variables */
62                         vector<string> bits;
63                         split (bits, line, is_any_of ("="));
64                         if (bits.size() == 2) {
65                                 string name = bits[0];
66                                 trim (name);
67                                 string value = bits[1];
68                                 trim (value);
69
70                                 set (name, value);
71                         } else {
72                                 warn (String::compose ("Unrecognised line %1", line));
73                         }
74                 } else {
75                         /* "Normal" lines */
76                         size_t divider[2];
77                         divider[0] = line.find_first_of (",");
78                         if (divider[0] != string::npos) {
79                                 divider[1] = line.find_first_of (",", divider[0] + 1);
80                         }
81
82                         if (divider[0] == string::npos || divider[1] == string::npos || divider[0] <= 1 || divider[1] >= line.length() - 1) {
83                                 warn (String::compose ("Unrecognised line %1", line));
84                                 continue;
85                         }
86
87                         string from_string = line.substr (0, divider[0] - 1);
88                         trim (from_string);
89                         string to_string = line.substr (divider[0] + 1, divider[1] - divider[0] - 1);
90                         trim (to_string);
91
92                         optional<Time> from = time (from_string);
93                         optional<Time> to = time (to_string);
94
95                         if (!from || !to) {
96                                 warn (String::compose ("Unrecognised line %1", line));
97                                 continue;
98                         }
99
100                         _subtitle.from = from.get ();
101                         _subtitle.to = to.get ();
102
103                         /* Parse ^B/^I/^U */
104                         string text = line.substr (divider[1] + 1);
105                         for (size_t i = 0; i < text.length(); ++i) {
106                                 if (text[i] == '|') {
107                                         maybe_push ();
108                                         _subtitle.vertical_position.line = _subtitle.vertical_position.line.get() + 1;
109                                 } else if (text[i] == '^') {
110                                         maybe_push ();
111                                         if ((i + 1) < text.length()) {
112                                                 switch (text[i + 1]) {
113                                                 case 'B':
114                                                         _subtitle.bold = !_subtitle.bold;
115                                                         break;
116                                                 case 'I':
117                                                         _subtitle.italic = !_subtitle.italic;
118                                                         break;
119                                                 case 'U':
120                                                         _subtitle.underline = !_subtitle.underline;
121                                                         break;
122                                                 }
123                                         }
124                                         ++i;
125                                 } else {
126                                         _subtitle.text += text[i];
127                                 }
128                         }
129
130                         maybe_push ();
131                 }
132         }
133 }
134
135 optional<Time>
136 STLTextReader::time (string t) const
137 {
138         vector<string> b;
139         split (b, t, is_any_of (":"));
140         if (b.size() != 4) {
141                 warn (String::compose ("Unrecognised time %1", t));
142                 return optional<Time> ();
143         }
144
145         return sub::Time::from_hmsf (lexical_cast<int> (b[0]), lexical_cast<int> (b[1]), lexical_cast<int> (b[2]), lexical_cast<int> (b[3]));
146 }
147
148 void
149 STLTextReader::set (string name, string value)
150 {
151         if (name == "$FontName") {
152                 _subtitle.font = value;
153         } else if (name == "$Bold") {
154                 _subtitle.bold = value == "True";
155         } else if (name == "$Italic") {
156                 _subtitle.italic = value == "True";
157         } else if (name == "$Underlined") {
158                 _subtitle.underline = value == "True";
159         } else if (name == "$FontSize") {
160                 _subtitle.font_size.set_points (lexical_cast<int> (value));
161         }
162 }
163
164 void
165 STLTextReader::maybe_push ()
166 {
167         if (!_subtitle.text.empty ()) {
168                 _subs.push_back (_subtitle);
169                 _subtitle.text.clear ();
170                 _subtitle.vertical_position.line = 0;
171         }
172 }