Simplify time representation; better in-tree DCP subtitle parser.
[libsub.git] / src / stl_text_reader.cc
1 /*
2     Copyright (C) 2014 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
26 using std::list;
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         _subtitle.vertical_position.line = 0;
42         /* XXX: no idea what this should be */
43         _subtitle.vertical_position.lines = 32;
44         _subtitle.vertical_position.reference = TOP_OF_SCREEN;
45         
46         while (in.good ()) {
47                 string line;
48                 getline (in, line);
49                 if (!in.good ()) {
50                         return;
51                 }
52
53                 trim (line);
54
55                 if (starts_with (line, "//")) {
56                         continue;
57                 }
58
59                 if (line.size() > 0 && line[0] == '$') {
60                         /* $ variables */
61                         vector<string> bits;
62                         split (bits, line, is_any_of ("="));
63                         if (bits.size() == 2) {
64                                 string name = bits[0];
65                                 trim (name);
66                                 string value = bits[1];
67                                 trim (value);
68
69                                 set (name, value);
70                         } else {
71                                 warn (String::compose ("Unrecognised line %1", line));
72                         }
73                 } else {
74                         /* "Normal" lines */
75                         size_t divider[2];
76                         divider[0] = line.find_first_of (",");
77                         if (divider[0] != string::npos) {
78                                 divider[1] = line.find_first_of (",", divider[0] + 1);
79                         }
80                         
81                         if (divider[0] == string::npos || divider[1] == string::npos || divider[0] <= 1 || divider[1] >= line.length() - 1) {
82                                 warn (String::compose ("Unrecognised line %1", line));
83                                 continue;
84                         }
85
86                         string from_string = line.substr (0, divider[0] - 1);
87                         trim (from_string);
88                         string to_string = line.substr (divider[0] + 1, divider[1] - divider[0] - 1);
89                         trim (to_string);
90
91                         optional<Time> from = time (from_string);
92                         optional<Time> to = time (to_string);
93
94                         if (!from || !to) {
95                                 warn (String::compose ("Unrecognised line %1", line));
96                                 continue;
97                         }
98
99                         _subtitle.from = from.get ();
100                         _subtitle.to = to.get ();
101
102                         /* Parse ^B/^I/^U */
103                         string text = line.substr (divider[1] + 1);
104                         for (size_t i = 0; i < text.length(); ++i) {
105                                 if (text[i] == '|') {
106                                         maybe_push ();
107                                         _subtitle.vertical_position.line = _subtitle.vertical_position.line.get() + 1;
108                                 } else if (text[i] == '^') {
109                                         maybe_push ();
110                                         if ((i + 1) < text.length()) {
111                                                 switch (text[i + 1]) {
112                                                 case 'B':
113                                                         _subtitle.bold = !_subtitle.bold;
114                                                         break;
115                                                 case 'I':
116                                                         _subtitle.italic = !_subtitle.italic;
117                                                         break;
118                                                 case 'U':
119                                                         _subtitle.underline = !_subtitle.underline;
120                                                         break;
121                                                 }
122                                         }
123                                         ++i;
124                                 } else {
125                                         _subtitle.text += text[i];
126                                 }
127                         }
128
129                         maybe_push ();
130                 }
131         }
132 }
133
134 optional<Time>
135 STLTextReader::time (string t) const
136 {
137         vector<string> b;
138         split (b, t, is_any_of (":"));
139         if (b.size() != 4) {
140                 warn (String::compose ("Unrecognised time %1", t));
141                 return optional<Time> ();
142         }
143
144         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]));
145 }
146
147 void
148 STLTextReader::set (string name, string value)
149 {
150         if (name == "$FontName") {
151                 _subtitle.font = value;
152         } else if (name == "$Bold") {
153                 _subtitle.bold = value == "True";
154         } else if (name == "$Italic") {
155                 _subtitle.italic = value == "True";
156         } else if (name == "$Underlined") {
157                 _subtitle.underline = value == "True";
158         } else if (name == "$FontSize") {
159                 _subtitle.font_size.set_points (lexical_cast<int> (value));
160         }
161 }
162
163 void
164 STLTextReader::maybe_push ()
165 {
166         if (!_subtitle.text.empty ()) {
167                 _subs.push_back (_subtitle);
168                 _subtitle.text.clear ();
169                 _subtitle.vertical_position.line = 0;
170         }
171 }