+static int
+vertical_position (sub::Line const & line)
+{
+ int vp = 0;
+ if (line.vertical_position.proportional) {
+ switch (line.vertical_position.reference.get_value_or (TOP_OF_SCREEN)) {
+ case TOP_OF_SCREEN:
+ vp = rint (line.vertical_position.proportional.get() * ROWS);
+ break;
+ case VERTICAL_CENTRE_OF_SCREEN:
+ vp = rint (line.vertical_position.proportional.get() * ROWS + (ROWS / 2.0));
+ break;
+ case BOTTOM_OF_SCREEN:
+ vp = rint (ROWS - (line.vertical_position.proportional.get() * ROWS));
+ break;
+ default:
+ break;
+ }
+ } else if (line.vertical_position.line) {
+ float const prop = float (line.vertical_position.line.get()) / line.vertical_position.lines.get ();
+ switch (line.vertical_position.reference.get_value_or (TOP_OF_SCREEN)) {
+ case TOP_OF_SCREEN:
+ vp = prop * ROWS;
+ break;
+ case VERTICAL_CENTRE_OF_SCREEN:
+ vp = (prop + 0.5) * ROWS;
+ break;
+ case BOTTOM_OF_SCREEN:
+ vp = (1 - prop) * ROWS;
+ break;
+ default:
+ break;
+ }
+ }
+
+ return vp;
+}
+
+vector<char*>
+make_tti_blocks (list<Subtitle> const& subtitles, STLBinaryTables const& tables, float frames_per_second)
+{
+ static int const tti_size = 128;
+ vector<char*> tti;
+
+ /* Buffer to build the TTI blocks in */
+ char buffer[tti_size];
+
+ BOOST_FOREACH (Subtitle const& i, subtitles) {
+
+ /* Find the top vertical position of this subtitle */
+ optional<int> top;
+ BOOST_FOREACH (Line const& j, i.lines) {
+ int const vp = vertical_position (j);
+ if (!top || vp < top.get ()) {
+ top = vp;
+ }
+ }
+
+ /* Work out the text */
+ string text;
+ bool italic = false;
+ bool underline = false;
+ optional<int> last_vp;
+
+ BOOST_FOREACH (Line const& j, i.lines) {
+
+ /* CR/LF down to this line */
+ int const vp = vertical_position (j);
+
+ if (last_vp) {
+ for (int k = last_vp.get(); k < vp; ++k) {
+ text += "\x8A";
+ }
+ }
+
+ last_vp = vp;
+
+ BOOST_FOREACH (Block const& k, j.blocks) {
+ if (k.underline && !underline) {
+ text += "\x82";
+ underline = true;
+ } else if (underline && !k.underline) {
+ text += "\x83";
+ underline = false;
+ }
+ if (k.italic && !italic) {
+ text += "\x80";
+ italic = true;
+ } else if (italic && !k.italic) {
+ text += "\x81";
+ italic = false;
+ }
+
+ text += utf16_to_iso6937 (utf_to_utf<wchar_t> (k.text));
+ }
+ }
+
+ /* Turn italic/underline off before the end of this subtitle */
+ if (underline) {
+ text += "\x83";
+ }
+ if (italic) {
+ text += "\x81";
+ }
+
+ /* Make sure there's at least one end-of-line */
+ text += "\x8F";
+
+ /* Now write this text in 112 byte chunks (TTI blocks). Only the first TTI
+ block's cumulative status, timecodes, vertical position, justification code
+ and comment flag are taken into account by the reader.
+ */
+
+ /* Set up the first part of the block */
+
+ /* XXX: these should increment, surely! */
+ /* Subtitle group number */
+ put_int_as_int (buffer + 0, 1, 1);
+ /* Subtitle number */
+ put_int_as_int (buffer + 1, 0, 2);
+ /* Cumulative status */
+ put_int_as_int (buffer + 4, tables.cumulative_status_enum_to_file (CUMULATIVE_STATUS_NOT_CUMULATIVE), 1);
+ /* Time code in */
+ put_int_as_int (buffer + 5, i.from.hours(), 1);
+ put_int_as_int (buffer + 6, i.from.minutes(), 1);
+ put_int_as_int (buffer + 7, i.from.seconds(), 1);
+ put_int_as_int (buffer + 8, i.from.frames_at(sub::Rational(frames_per_second * 1000, 1000)), 1);
+ /* Time code out */
+ put_int_as_int (buffer + 9, i.to.hours(), 1);
+ put_int_as_int (buffer + 10, i.to.minutes(), 1);
+ put_int_as_int (buffer + 11, i.to.seconds(), 1);
+ put_int_as_int (buffer + 12, i.to.frames_at(sub::Rational(frames_per_second * 1000, 1000)), 1);
+ /* Vertical position */
+ put_int_as_int (buffer + 13, top.get(), 1);
+
+ /* Justification code */
+ /* XXX: this assumes the first line has the right value */
+ switch (i.lines.front().horizontal_position.reference) {
+ case LEFT_OF_SCREEN:
+ put_int_as_int (buffer + 14, tables.justification_enum_to_file (JUSTIFICATION_LEFT), 1);
+ break;
+ case HORIZONTAL_CENTRE_OF_SCREEN:
+ put_int_as_int (buffer + 14, tables.justification_enum_to_file (JUSTIFICATION_CENTRE), 1);
+ break;
+ case RIGHT_OF_SCREEN:
+ put_int_as_int (buffer + 14, tables.justification_enum_to_file (JUSTIFICATION_RIGHT), 1);
+ break;
+ }
+
+ /* Comment flag */
+ put_int_as_int (buffer + 15, tables.comment_enum_to_file (COMMENT_NO), 1);
+
+ /* Now make as many blocks as are needed to add all the text */
+ size_t const block_size = 112;
+ size_t offset = 0;
+ int block_number = 0;
+ while (offset < text.length()) {
+ size_t this_time = std::min(block_size, text.length() - offset);
+ put_string (buffer + 16, text.substr(offset, this_time) + string(block_size - this_time, '\x8f'));
+ offset += this_time;
+
+ /* Extension block number. Count up from 0 but use 0xff for the last one */
+ put_int_as_int (buffer + 3, offset == text.length() ? 0xff : block_number, 1);
+ ++block_number;
+
+ char* finished = new char[tti_size];
+ memcpy (finished, buffer, tti_size);
+ tti.push_back (finished);
+ }
+ }
+
+ return tti;
+}
+
+
+