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