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