2 Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file src/stl_binary_writer.cc
21 * @brief Writer for STL binary files.
24 #include "stl_binary_writer.h"
28 #include "compose.hpp"
29 #include "sub_assert.h"
30 #include <boost/locale.hpp>
31 #include <boost/algorithm/string.hpp>
46 using boost::locale::conv::utf_to_utf;
47 using boost::optional;
50 /** Arbitrary number which to divide the screen into rows; e.g.
51 * 64 here would mean that there are 64 addressable vertical positions
52 * on the screen, each 1/64th of the screen height tall.
54 * The magic 23 makes our output agree more closely with
55 * AnnotationEdit, which makes life easier when testing.
57 static int const ROWS = 23;
60 put_string (char* p, string s)
62 memcpy (p, s.c_str (), s.length ());
66 put_string (char* p, unsigned int n, string s)
68 SUB_ASSERT (s.length() <= n);
70 memcpy (p, s.c_str (), s.length ());
71 memset (p + s.length(), ' ', n - s.length ());
75 * @param n Width to zero-pad v to.
78 put_int_as_string (char* p, int v, unsigned int n)
84 snprintf (buffer, sizeof(buffer), "%02d", v);
87 snprintf (buffer, sizeof(buffer), "%05d", v);
95 struct lconv* lc = localeconv ();
96 boost::algorithm::replace_all (s, lc->thousands_sep, "");
97 boost::algorithm::replace_all (s, lc->decimal_point, ".");
103 put_int_as_int (char* p, int v, unsigned int n)
105 for (unsigned int i = 0; i < n; ++i) {
106 *p++ = (v & ((1 << ((i + 1) * 8)) - 1)) >> (i * 8);
111 vertical_position (sub::Line const & line)
114 if (line.vertical_position.proportional) {
115 switch (line.vertical_position.reference.get_value_or (TOP_OF_SCREEN)) {
117 vp = rint (line.vertical_position.proportional.get() * ROWS);
119 case VERTICAL_CENTRE_OF_SCREEN:
120 vp = rint (line.vertical_position.proportional.get() * ROWS + (ROWS / 2.0));
122 case BOTTOM_OF_SCREEN:
123 vp = rint (ROWS - (line.vertical_position.proportional.get() * ROWS));
128 } else if (line.vertical_position.line) {
129 float const prop = float (line.vertical_position.line.get()) / line.vertical_position.lines.get ();
130 switch (line.vertical_position.reference.get_value_or (TOP_OF_SCREEN)) {
134 case VERTICAL_CENTRE_OF_SCREEN:
135 vp = (prop + 0.5) * ROWS;
137 case BOTTOM_OF_SCREEN:
138 vp = (1 - prop) * ROWS;
148 /** @param language ISO 3-character country code for the language of the subtitles */
150 sub::write_stl_binary (
151 list<Subtitle> subtitles,
152 float frames_per_second,
154 string original_programme_title,
155 string original_episode_title,
156 string translated_programme_title,
157 string translated_episode_title,
158 string translator_name,
159 string translator_contact_details,
160 string creation_date,
161 string revision_date,
163 string country_of_origin,
166 string editor_contact_details,
167 boost::filesystem::path file_name
170 SUB_ASSERT (original_programme_title.size() <= 32);
171 SUB_ASSERT (original_episode_title.size() <= 32);
172 SUB_ASSERT (translated_programme_title.size() <= 32);
173 SUB_ASSERT (translated_episode_title.size() <= 32);
174 SUB_ASSERT (translator_name.size() <= 32);
175 SUB_ASSERT (translator_contact_details.size() <= 32);
176 SUB_ASSERT (creation_date.size() == 6);
177 SUB_ASSERT (revision_date.size() == 6);
178 SUB_ASSERT (revision_number <= 99);
179 SUB_ASSERT (country_of_origin.size() == 3);
180 SUB_ASSERT (publisher.size() <= 32);
181 SUB_ASSERT (editor_name.size() <= 32);
182 SUB_ASSERT (editor_contact_details.size() <= 32);
184 char* buffer = new char[1024];
185 memset (buffer, 0, 1024);
186 ofstream output (file_name.string().c_str ());
187 STLBinaryTables tables;
189 /* Find the longest subtitle in characters */
193 for (list<Subtitle>::const_iterator i = subtitles.begin(); i != subtitles.end(); ++i) {
194 for (list<Line>::const_iterator j = i->lines.begin(); j != i->lines.end(); ++j) {
196 for (list<Block>::const_iterator k = j->blocks.begin(); k != j->blocks.end(); ++k) {
197 t += k->text.size ();
199 longest = std::max (longest, t);
204 put_string (buffer + 0, "850");
205 /* Disk format code */
206 put_string (buffer + 3, stl_frame_rate_to_dfc (frames_per_second));
207 /* Display standard code: open subtitling */
208 put_string (buffer + 11, "0");
209 /* Character code table: Latin (ISO 6937) */
210 put_string (buffer + 12, "00");
211 put_string (buffer + 14, tables.language_enum_to_file (language));
212 put_string (buffer + 16, 32, original_programme_title);
213 put_string (buffer + 48, 32, original_episode_title);
214 put_string (buffer + 80, 32, translated_programme_title);
215 put_string (buffer + 112, 32, translated_episode_title);
216 put_string (buffer + 144, 32, translator_name);
217 put_string (buffer + 176, 32, translator_contact_details);
218 /* Subtitle list reference code */
219 put_string (buffer + 208, "0000000000000000");
220 put_string (buffer + 224, creation_date);
221 put_string (buffer + 230, revision_date);
222 put_int_as_string (buffer + 236, revision_number, 2);
224 put_int_as_string (buffer + 238, subtitles.size(), 5);
225 /* Total number of subtitles */
226 put_int_as_string (buffer + 243, subtitles.size(), 5);
227 /* Total number of subtitle groups */
228 put_string (buffer + 248, "001");
229 /* Maximum number of displayable characters in any text row */
230 put_int_as_string (buffer + 251, longest, 2);
231 /* Maximum number of displayable rows */
232 put_int_as_string (buffer + 253, ROWS, 2);
233 /* Time code status */
234 put_string (buffer + 255, "1");
235 /* Start-of-programme time code */
236 put_string (buffer + 256, "00000000");
237 /* First-in-cue time code */
238 put_string (buffer + 264, "00000000");
239 /* Total number of disks */
240 put_string (buffer + 272, "1");
241 /* Disk sequence number */
242 put_string (buffer + 273, "1");
243 put_string (buffer + 274, 3, country_of_origin);
244 put_string (buffer + 277, 32, publisher);
245 put_string (buffer + 309, 32, editor_name);
246 put_string (buffer + 341, 32, editor_contact_details);
248 output.write (buffer, 1024);
250 for (list<Subtitle>::const_iterator i = subtitles.begin(); i != subtitles.end(); ++i) {
252 /* Find the top vertical position of this subtitle */
254 for (list<Line>::const_iterator j = i->lines.begin(); j != i->lines.end(); ++j) {
255 int const vp = vertical_position (*j);
256 if (!top || vp < top.get ()) {
261 memset (buffer, 0, 1024);
263 /* Subtitle group number */
264 put_int_as_int (buffer + 0, 1, 1);
265 /* Subtitle number */
266 put_int_as_int (buffer + 1, 0, 2);
267 /* Extension block number. Use 0xff here to indicate that it is the last TTI
268 block in this subtitle "set", as we only ever use one.
270 put_int_as_int (buffer + 3, 255, 1);
271 /* Cumulative status */
272 put_int_as_int (buffer + 4, tables.cumulative_status_enum_to_file (CUMULATIVE_STATUS_NOT_CUMULATIVE), 1);
274 put_int_as_int (buffer + 5, i->from.hours(), 1);
275 put_int_as_int (buffer + 6, i->from.minutes(), 1);
276 put_int_as_int (buffer + 7, i->from.seconds(), 1);
277 put_int_as_int (buffer + 8, i->from.frames_at(sub::Rational (frames_per_second * 1000, 1000)), 1);
279 put_int_as_int (buffer + 9, i->to.hours(), 1);
280 put_int_as_int (buffer + 10, i->to.minutes(), 1);
281 put_int_as_int (buffer + 11, i->to.seconds(), 1);
282 put_int_as_int (buffer + 12, i->to.frames_at(sub::Rational (frames_per_second * 1000, 1000)), 1);
283 /* Vertical position */
284 put_int_as_int (buffer + 13, top.get(), 1);
286 /* Justification code */
287 /* XXX: this assumes the first line has the right value */
288 switch (i->lines.front().horizontal_position.reference) {
290 put_int_as_int (buffer + 14, tables.justification_enum_to_file (JUSTIFICATION_LEFT), 1);
292 case HORIZONTAL_CENTRE_OF_SCREEN:
293 put_int_as_int (buffer + 14, tables.justification_enum_to_file (JUSTIFICATION_CENTRE), 1);
295 case RIGHT_OF_SCREEN:
296 put_int_as_int (buffer + 14, tables.justification_enum_to_file (JUSTIFICATION_RIGHT), 1);
301 put_int_as_int (buffer + 15, tables.comment_enum_to_file (COMMENT_NO), 1);
306 bool underline = false;
307 optional<int> last_vp;
309 for (list<Line>::const_iterator j = i->lines.begin(); j != i->lines.end(); ++j) {
311 /* CR/LF down to this line */
312 int const vp = vertical_position (*j);
315 for (int i = last_vp.get(); i < vp; ++i) {
322 for (list<Block>::const_iterator k = j->blocks.begin(); k != j->blocks.end(); ++k) {
323 if (k->underline && !underline) {
326 } else if (underline && !k->underline) {
330 if (k->italic && !italic) {
333 } else if (italic && !k->italic) {
338 text += utf16_to_iso6937 (utf_to_utf<wchar_t> (k->text));
342 /* Turn italic/underline off before the end of this subtitle */
352 if (text.length() > 111) {
353 text = text.substr (111);
356 while (text.length() < 112) {
360 put_string (buffer + 16, text);
361 output.write (buffer, 128);