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