334a5bb69076a6d55a7998ef26beb747cab62d43
[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 #include "stl_binary_writer.h"
21 #include "subtitle.h"
22 #include "compose.hpp"
23 #include <list>
24 #include <cmath>
25 #include <fstream>
26 #include <iomanip>
27 #include <set>
28
29 using std::list;
30 using std::set;
31 using std::ofstream;
32 using std::string;
33 using std::setw;
34 using std::setfill;
35 using std::max;
36 using std::cout;
37 using namespace sub;
38
39 static void
40 put_string (char* p, string s)
41 {
42         memcpy (p, s.c_str (), s.length ());
43 }
44
45 static void
46 put_string (char* p, unsigned int n, string s)
47 {
48         assert (s.length() <= n);
49         
50         memcpy (p, s.c_str (), s.length ());
51         memset (p + s.length(), ' ', n - s.length ());
52 }
53
54 static void
55 put_int_as_string (char* p, int v, unsigned int n)
56 {
57         std::stringstream s;
58         /* Be careful to ensure we get no thousands separators */
59         s.imbue (std::locale::classic ());
60         s << setw (n) << setfill ('0');
61         s << v;
62         assert (s.str().length() == n);
63         put_string (p, s.str ());
64 }
65
66 static void
67 put_int_as_int (char* p, int v, unsigned int n)
68 {
69         for (unsigned int i = 0; i < n; ++i) {
70                 *p++ = (v & ((1 << ((i + 1) * 8)) - 1)) >> (i * 8);
71         }
72 }
73
74 /** @param language ISO 3-character country code for the language of the subtitles */
75 void
76 sub::write_stl_binary (
77         list<Subtitle> subtitles,
78         float frames_per_second,
79         Language language,
80         string original_programme_title,
81         string original_episode_title,
82         string translated_programme_title,
83         string translated_episode_title,
84         string translator_name,
85         string translator_contact_details,
86         string creation_date,
87         string revision_date,
88         int revision_number,
89         string country_of_origin,
90         string publisher,
91         string editor_name,
92         string editor_contact_details,
93         boost::filesystem::path file_name
94         )
95 {
96         assert (original_programme_title.size() <= 32);
97         assert (original_episode_title.size() <= 32);
98         assert (translated_programme_title.size() <= 32);
99         assert (translated_episode_title.size() <= 32);
100         assert (translator_name.size() <= 32);
101         assert (translator_contact_details.size() <= 32);
102         assert (creation_date.size() == 6);
103         assert (revision_date.size() == 6);
104         assert (revision_number <= 99);
105         assert (country_of_origin.size() == 3);
106         assert (publisher.size() <= 32);
107         assert (editor_name.size() <= 32);
108         assert (editor_contact_details.size() <= 32);
109         
110         char* buffer = new char[1024];
111         ofstream output (file_name.string().c_str ());
112         STLBinaryTables tables;
113
114         /* Find the longest subtitle in characters and the number of rows */
115
116         int longest = 0;
117
118         set<float> check_top;
119         set<float> check_centre;
120         set<float> check_bottom;
121         set<int> check_rows;
122         
123         for (list<Subtitle>::const_iterator i = subtitles.begin(); i != subtitles.end(); ++i) {
124                 int t = 0;
125                 for (list<Line>::const_iterator j = i->lines.begin(); j != i->lines.end(); ++j) {
126                         for (list<Block>::const_iterator k = j->blocks.begin(); k != j->blocks.end(); ++k) {
127                                 t += k->text.size ();
128                         }
129                         longest = max (longest, t);
130                         
131                         if (j->vertical_position.proportional) {
132                                 switch (j->vertical_position.reference.get ()) {
133                                 case TOP:
134                                         check_top.insert (j->vertical_position.proportional.get ());
135                                         break;
136                                 case CENTRE:
137                                         check_centre.insert (j->vertical_position.proportional.get ());
138                                         break;
139                                 case BOTTOM:
140                                         check_bottom.insert (j->vertical_position.proportional.get ());
141                                         break;
142                                 }
143                         } else {
144                                 check_rows.insert (j->vertical_position.line.get ());
145                         }
146                 }
147         }
148
149         int const rows = check_top.size() + check_centre.size() + check_bottom.size() + check_rows.size();
150         
151         /* Code page: 850 */
152         put_string (buffer + 0, "850");
153         /* Disk format code */
154         put_string (buffer + 3, String::compose ("STL%1.01", rint (frames_per_second)));
155         /* Display standard code: open subtitling */
156         put_string (buffer + 11, "0");
157         /* Character code table: Latin (ISO 6937) */
158         put_string (buffer + 12, "00");
159         put_string (buffer + 14, tables.language_enum_to_file (language));
160         put_string (buffer + 16, 32, original_programme_title);
161         put_string (buffer + 48, 32, original_episode_title);
162         put_string (buffer + 80, 32, translated_programme_title);
163         put_string (buffer + 112, 32, translated_episode_title);
164         put_string (buffer + 144, 32, translator_name);
165         put_string (buffer + 176, 32, translator_contact_details);
166         /* Subtitle list reference code */
167         put_string (buffer + 208, "0000000000000000");
168         put_string (buffer + 224, creation_date);
169         put_string (buffer + 230, revision_date);
170         put_int_as_string (buffer + 236, revision_number, 2);
171         /* TTI blocks */
172         put_int_as_string (buffer + 238, subtitles.size (), 5);
173         /* Total number of subtitles */
174         put_int_as_string (buffer + 243, subtitles.size (), 5);
175         /* Total number of subtitle groups */
176         put_string (buffer + 248, "000");
177         /* Maximum number of displayable characters in any text row */
178         put_int_as_string (buffer + 251, 2, longest);
179         /* Maximum number of displayable rows */
180         put_int_as_string (buffer + 253, 2, rows);
181         /* Time code status */
182         put_string (buffer + 255, "1");
183         /* Start-of-programme time code */
184         put_string (buffer + 256, "00000000");
185         /* First-in-cue time code */
186         put_string (buffer + 264, "00000000");
187         /* Total number of disks */
188         put_string (buffer + 272, "1");
189         /* Disk sequence number */
190         put_string (buffer + 273, "1");
191         put_string (buffer + 274, 32, country_of_origin);
192         put_string (buffer + 277, 32, publisher);
193         put_string (buffer + 309, 32, editor_name);
194         put_string (buffer + 341, 32, editor_contact_details);
195
196         output.write (buffer, 1024);
197
198         int N = 0;
199         for (list<Subtitle>::const_iterator i = subtitles.begin(); i != subtitles.end(); ++i) {
200
201                 /* Subtitle group number */
202                 put_int_as_int (buffer + 0, 0, 1);
203                 /* Subtitle number */
204                 put_int_as_int (buffer + 1, N, 2);
205                 /* Extension block number */
206                 put_int_as_int (buffer + 3, 0, 1);
207                 /* Cumulative status */
208                 put_int_as_int (buffer + 4, tables.cumulative_status_enum_to_file (CUMULATIVE_STATUS_NOT_CUMULATIVE), 1);
209                 /* Time code in */
210                 put_int_as_int (buffer + 5, i->from.frame(frames_per_second).hours (), 1);
211                 put_int_as_int (buffer + 6, i->from.frame(frames_per_second).minutes (), 1);
212                 put_int_as_int (buffer + 7, i->from.frame(frames_per_second).seconds (), 1);
213                 put_int_as_int (buffer + 8, i->from.frame(frames_per_second).frames (), 1);
214                 /* Time code out */
215                 put_int_as_int (buffer + 9, i->to.frame(frames_per_second).hours (), 1);
216                 put_int_as_int (buffer + 10, i->to.frame(frames_per_second).minutes (), 1);
217                 put_int_as_int (buffer + 11, i->to.frame(frames_per_second).seconds (), 1);
218                 put_int_as_int (buffer + 12, i->to.frame(frames_per_second).frames (), 1);
219                 /* Vertical position */
220                 /* XXX */
221                 put_int_as_int (buffer + 13, 0, 1);
222                 /* Justification code */
223                 /* XXX */
224                 put_int_as_int (buffer + 14, tables.justification_enum_to_file (JUSTIFICATION_NONE), 1);
225                 /* Comment flag */
226                 put_int_as_int (buffer + 15, tables.comment_enum_to_file (COMMENT_NO), 1);
227
228                 /* Text */
229                 string text;
230                 bool italic = false;
231                 bool underline = false;
232                 
233                 for (list<Line>::const_iterator j = i->lines.begin(); j != i->lines.end(); ++j) {
234                         for (list<Block>::const_iterator k = j->blocks.begin(); k != j->blocks.end(); ++k) {
235                                 if (k->underline && !underline) {
236                                         text += "\x82";
237                                         underline = true;
238                                 } else if (underline && !k->underline) {
239                                         text += "\x83";
240                                         underline = false;
241                                 }
242                                 if (k->italic && !italic) {
243                                         text += "\x80";
244                                         italic = true;
245                                 } else if (italic && !k->italic) {
246                                         text += "\x81";
247                                         italic = false;
248                                 }
249
250                                 text += k->text;
251                         }
252
253                         text += "\x8A";
254                 }
255
256                 if (text.length() > 111) {
257                         text = text.substr (111);
258                 }
259
260                 while (text.length() < 112) {
261                         text += "\x8F";
262                 }
263
264                 put_string (buffer + 16, text);
265                 output.write (buffer, 128);
266         }
267
268         delete[] buffer;
269 }