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