Add FIXME.
[libsub.git] / src / stl_binary_writer.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 /** @file  src/stl_binary_writer.cc
21  *  @brief Writer for STL binary files.
22  */
23
24 #include "stl_binary_writer.h"
25 #include "subtitle.h"
26 #include "iso6937.h"
27 #include "stl_util.h"
28 #include "compose.hpp"
29 #include "sub_assert.h"
30 #include <boost/locale.hpp>
31 #include <list>
32 #include <cmath>
33 #include <fstream>
34 #include <iomanip>
35 #include <set>
36
37 using std::list;
38 using std::set;
39 using std::ofstream;
40 using std::string;
41 using std::setw;
42 using std::setfill;
43 using std::max;
44 using std::cout;
45 using boost::locale::conv::utf_to_utf;
46 using boost::optional;
47 using namespace sub;
48
49 /** Arbitrary number which to divide the screen into rows; e.g.
50  *  64 here would mean that there are 64 addressable vertical positions
51  *  on the screen, each 1/64th of the screen height tall.
52  *
53  *  The magic 23 makes our output agree more closely with
54  *  AnnotationEdit, which makes life easier when testing.
55  */
56 static int const ROWS = 23;
57
58 static void
59 put_string (char* p, string s)
60 {
61         memcpy (p, s.c_str (), s.length ());
62 }
63
64 static void
65 put_string (char* p, unsigned int n, string s)
66 {
67         SUB_ASSERT (s.length() <= n);
68
69         memcpy (p, s.c_str (), s.length ());
70         memset (p + s.length(), ' ', n - s.length ());
71 }
72
73 static void
74 put_int_as_string (char* p, int v, unsigned int n)
75 {
76         locked_stringstream s;
77         /* Be careful to ensure we get no thousands separators */
78         s.imbue (std::locale::classic ());
79         s << setw (n) << setfill ('0');
80         s << v;
81         SUB_ASSERT (s.str().length() == n);
82         put_string (p, s.str ());
83 }
84
85 static void
86 put_int_as_int (char* p, int v, unsigned int n)
87 {
88         for (unsigned int i = 0; i < n; ++i) {
89                 *p++ = (v & ((1 << ((i + 1) * 8)) - 1)) >> (i * 8);
90         }
91 }
92
93 static int
94 vertical_position (sub::Line const & line)
95 {
96         int vp = 0;
97         if (line.vertical_position.proportional) {
98                 switch (line.vertical_position.reference.get_value_or (TOP_OF_SCREEN)) {
99                 case TOP_OF_SCREEN:
100                         vp = rint (line.vertical_position.proportional.get() * ROWS);
101                         break;
102                 case VERTICAL_CENTRE_OF_SCREEN:
103                         vp = rint (line.vertical_position.proportional.get() * ROWS + (ROWS / 2.0));
104                         break;
105                 case BOTTOM_OF_SCREEN:
106                         vp = rint (ROWS - (line.vertical_position.proportional.get() * ROWS));
107                         break;
108                 default:
109                         break;
110                 }
111         } else if (line.vertical_position.line) {
112                 float const prop = float (line.vertical_position.line.get()) / line.vertical_position.lines.get ();
113                 switch (line.vertical_position.reference.get_value_or (TOP_OF_SCREEN)) {
114                 case TOP_OF_SCREEN:
115                         vp = prop * ROWS;
116                         break;
117                 case VERTICAL_CENTRE_OF_SCREEN:
118                         vp = (prop + 0.5) * ROWS;
119                         break;
120                 case BOTTOM_OF_SCREEN:
121                         vp = (1 - prop) * ROWS;
122                         break;
123                 default:
124                         break;
125                 }
126         }
127
128         return vp;
129 }
130
131 /** @param language ISO 3-character country code for the language of the subtitles */
132 void
133 sub::write_stl_binary (
134         list<Subtitle> subtitles,
135         float frames_per_second,
136         Language language,
137         string original_programme_title,
138         string original_episode_title,
139         string translated_programme_title,
140         string translated_episode_title,
141         string translator_name,
142         string translator_contact_details,
143         string creation_date,
144         string revision_date,
145         int revision_number,
146         string country_of_origin,
147         string publisher,
148         string editor_name,
149         string editor_contact_details,
150         boost::filesystem::path file_name
151         )
152 {
153         SUB_ASSERT (original_programme_title.size() <= 32);
154         SUB_ASSERT (original_episode_title.size() <= 32);
155         SUB_ASSERT (translated_programme_title.size() <= 32);
156         SUB_ASSERT (translated_episode_title.size() <= 32);
157         SUB_ASSERT (translator_name.size() <= 32);
158         SUB_ASSERT (translator_contact_details.size() <= 32);
159         SUB_ASSERT (creation_date.size() == 6);
160         SUB_ASSERT (revision_date.size() == 6);
161         SUB_ASSERT (revision_number <= 99);
162         SUB_ASSERT (country_of_origin.size() == 3);
163         SUB_ASSERT (publisher.size() <= 32);
164         SUB_ASSERT (editor_name.size() <= 32);
165         SUB_ASSERT (editor_contact_details.size() <= 32);
166
167         char* buffer = new char[1024];
168         memset (buffer, 0, 1024);
169         ofstream output (file_name.string().c_str ());
170         STLBinaryTables tables;
171
172         /* Find the longest subtitle in characters */
173
174         int longest = 0;
175
176         for (list<Subtitle>::const_iterator i = subtitles.begin(); i != subtitles.end(); ++i) {
177                 for (list<Line>::const_iterator j = i->lines.begin(); j != i->lines.end(); ++j) {
178                         int t = 0;
179                         for (list<Block>::const_iterator k = j->blocks.begin(); k != j->blocks.end(); ++k) {
180                                 t += k->text.size ();
181                         }
182                         longest = std::max (longest, t);
183                 }
184         }
185
186         /* Code page: 850 */
187         put_string (buffer + 0, "850");
188         /* Disk format code */
189         put_string (buffer + 3, stl_frame_rate_to_dfc (frames_per_second));
190         /* Display standard code: open subtitling */
191         put_string (buffer + 11, "0");
192         /* Character code table: Latin (ISO 6937) */
193         put_string (buffer + 12, "00");
194         put_string (buffer + 14, tables.language_enum_to_file (language));
195         put_string (buffer + 16, 32, original_programme_title);
196         put_string (buffer + 48, 32, original_episode_title);
197         put_string (buffer + 80, 32, translated_programme_title);
198         put_string (buffer + 112, 32, translated_episode_title);
199         put_string (buffer + 144, 32, translator_name);
200         put_string (buffer + 176, 32, translator_contact_details);
201         /* Subtitle list reference code */
202         put_string (buffer + 208, "0000000000000000");
203         put_string (buffer + 224, creation_date);
204         put_string (buffer + 230, revision_date);
205         put_int_as_string (buffer + 236, revision_number, 2);
206         /* TTI blocks */
207         put_int_as_string (buffer + 238, subtitles.size(), 5);
208         /* Total number of subtitles */
209         put_int_as_string (buffer + 243, subtitles.size(), 5);
210         /* Total number of subtitle groups */
211         put_string (buffer + 248, "001");
212         /* Maximum number of displayable characters in any text row */
213         put_int_as_string (buffer + 251, longest, 2);
214         /* Maximum number of displayable rows */
215         put_int_as_string (buffer + 253, ROWS, 2);
216         /* Time code status */
217         put_string (buffer + 255, "1");
218         /* Start-of-programme time code */
219         put_string (buffer + 256, "00000000");
220         /* First-in-cue time code */
221         put_string (buffer + 264, "00000000");
222         /* Total number of disks */
223         put_string (buffer + 272, "1");
224         /* Disk sequence number */
225         put_string (buffer + 273, "1");
226         put_string (buffer + 274, 3, country_of_origin);
227         put_string (buffer + 277, 32, publisher);
228         put_string (buffer + 309, 32, editor_name);
229         put_string (buffer + 341, 32, editor_contact_details);
230
231         output.write (buffer, 1024);
232
233         for (list<Subtitle>::const_iterator i = subtitles.begin(); i != subtitles.end(); ++i) {
234
235                 /* Find the top vertical position of this subtitle */
236                 optional<int> top;
237                 for (list<Line>::const_iterator j = i->lines.begin(); j != i->lines.end(); ++j) {
238                         int const vp = vertical_position (*j);
239                         if (!top || vp < top.get ()) {
240                                 top = vp;
241                         }
242                 }
243
244                 memset (buffer, 0, 128);
245
246                 /* XXX: these should increment, surely! */
247                 /* Subtitle group number */
248                 put_int_as_int (buffer + 0, 1, 1);
249                 /* Subtitle number */
250                 put_int_as_int (buffer + 1, 0, 2);
251                 /* Extension block number.  Use 0xff here to indicate that it is the last TTI
252                    block in this subtitle "set", as we only ever use one.
253                 */
254                 put_int_as_int (buffer + 3, 255, 1);
255                 /* Cumulative status */
256                 put_int_as_int (buffer + 4, tables.cumulative_status_enum_to_file (CUMULATIVE_STATUS_NOT_CUMULATIVE), 1);
257                 /* Time code in */
258                 put_int_as_int (buffer + 5, i->from.hours(), 1);
259                 put_int_as_int (buffer + 6, i->from.minutes(), 1);
260                 put_int_as_int (buffer + 7, i->from.seconds(), 1);
261                 put_int_as_int (buffer + 8, i->from.frames_at(sub::Rational (frames_per_second * 1000, 1000)), 1);
262                 /* Time code out */
263                 put_int_as_int (buffer + 9, i->to.hours(), 1);
264                 put_int_as_int (buffer + 10, i->to.minutes(), 1);
265                 put_int_as_int (buffer + 11, i->to.seconds(), 1);
266                 put_int_as_int (buffer + 12, i->to.frames_at(sub::Rational (frames_per_second * 1000, 1000)), 1);
267                 /* Vertical position */
268                 put_int_as_int (buffer + 13, top.get(), 1);
269
270                 /* Justification code */
271                 /* XXX: this assumes the first line has the right value */
272                 switch (i->lines.front().horizontal_position.reference) {
273                 case LEFT_OF_SCREEN:
274                         put_int_as_int (buffer + 14, tables.justification_enum_to_file (JUSTIFICATION_LEFT), 1);
275                         break;
276                 case HORIZONTAL_CENTRE_OF_SCREEN:
277                         put_int_as_int (buffer + 14, tables.justification_enum_to_file (JUSTIFICATION_CENTRE), 1);
278                         break;
279                 case RIGHT_OF_SCREEN:
280                         put_int_as_int (buffer + 14, tables.justification_enum_to_file (JUSTIFICATION_RIGHT), 1);
281                         break;
282                 }
283
284                 /* Comment flag */
285                 put_int_as_int (buffer + 15, tables.comment_enum_to_file (COMMENT_NO), 1);
286
287                 /* Text */
288                 string text;
289                 bool italic = false;
290                 bool underline = false;
291                 optional<int> last_vp;
292
293                 for (list<Line>::const_iterator j = i->lines.begin(); j != i->lines.end(); ++j) {
294
295                         /* CR/LF down to this line */
296                         int const vp = vertical_position (*j);
297
298                         if (last_vp) {
299                                 for (int i = last_vp.get(); i < vp; ++i) {
300                                         text += "\x8A";
301                                 }
302                         }
303
304                         last_vp = vp;
305
306                         for (list<Block>::const_iterator k = j->blocks.begin(); k != j->blocks.end(); ++k) {
307                                 if (k->underline && !underline) {
308                                         text += "\x82";
309                                         underline = true;
310                                 } else if (underline && !k->underline) {
311                                         text += "\x83";
312                                         underline = false;
313                                 }
314                                 if (k->italic && !italic) {
315                                         text += "\x80";
316                                         italic = true;
317                                 } else if (italic && !k->italic) {
318                                         text += "\x81";
319                                         italic = false;
320                                 }
321
322                                 text += utf16_to_iso6937 (utf_to_utf<wchar_t> (k->text));
323                         }
324                 }
325
326                 /* Turn italic/underline off before the end of this subtitle */
327
328                 if (underline) {
329                         text += "\x83";
330                 }
331
332                 if (italic) {
333                         text += "\x81";
334                 }
335
336                 if (text.length() > 111) {
337                         text = text.substr (111);
338                 }
339
340                 while (text.length() < 112) {
341                         text += "\x8F";
342                 }
343
344                 put_string (buffer + 16, text);
345                 output.write (buffer, 128);
346         }
347
348         delete[] buffer;
349 }