2 Copyright (C) 2018-2019 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "closed_captions_dialog.h"
23 #include "film_viewer.h"
25 #include "lib/butler.h"
26 #include "lib/compose.hpp"
27 #include "lib/constants.h"
28 #include "lib/string_text.h"
29 #include "lib/text_content.h"
30 #include <boost/bind/bind.hpp>
38 using std::shared_ptr;
40 using boost::optional;
41 #if BOOST_VERSION >= 106100
42 using namespace boost::placeholders;
44 using namespace dcpomatic;
47 ClosedCaptionsDialog::ClosedCaptionsDialog (wxWindow* parent, FilmViewer* viewer)
48 : wxDialog (parent, wxID_ANY, _("Closed captions"), wxDefaultPosition, wxDefaultSize,
50 /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
51 the window above all others (and not just our own) it's better than nothing for now.
53 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
55 wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
59 /* XXX: empirical and probably unhelpful default size here; needs to be related to font metrics */
60 , _display (new wxPanel(this, wxID_ANY, wxDefaultPosition, wxSize(640, (640 / 10) + 64)))
61 , _track (new wxChoice(this, wxID_ANY))
62 , _current_in_lines (false)
65 _lines.resize (MAX_CLOSED_CAPTION_LINES);
67 wxBoxSizer* sizer = new wxBoxSizer (wxVERTICAL);
69 wxBoxSizer* track_sizer = new wxBoxSizer (wxHORIZONTAL);
70 add_label_to_sizer (track_sizer, this, _("Track"), true, 0, wxLEFT | wxRIGHT | wxALIGN_CENTRE_VERTICAL);
71 track_sizer->Add (_track, 0, wxEXPAND | wxLEFT, DCPOMATIC_SIZER_X_GAP);
73 sizer->Add (track_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
74 sizer->Add (_display, 1, wxEXPAND);
76 Bind (wxEVT_SHOW, boost::bind(&ClosedCaptionsDialog::shown, this, _1));
77 Bind (wxEVT_TIMER, boost::bind(&ClosedCaptionsDialog::update, this));
78 _display->Bind (wxEVT_PAINT, boost::bind(&ClosedCaptionsDialog::paint, this));
79 _track->Bind (wxEVT_CHOICE, boost::bind(&ClosedCaptionsDialog::track_selected, this));
81 SetSizerAndFit (sizer);
85 ClosedCaptionsDialog::shown (wxShowEvent ev)
95 ClosedCaptionsDialog::track_selected ()
97 _current = optional<TextRingBuffers::Data> ();
98 _viewer->slow_refresh ();
103 ClosedCaptionsDialog::paint ()
105 wxPaintDC dc (_display);
106 dc.SetBackground (*wxBLACK_BRUSH);
108 dc.SetTextForeground (*wxWHITE);
110 /* Choose a font which fits vertically */
111 int const line_height = max (8, dc.GetSize().GetHeight() / MAX_CLOSED_CAPTION_LINES);
112 wxFont font (*wxNORMAL_FONT);
113 font.SetPixelSize (wxSize (0, line_height * 0.8));
116 for (int i = 0; i < MAX_CLOSED_CAPTION_LINES; ++i) {
117 wxString const good = _lines[i].Left (MAX_CLOSED_CAPTION_LENGTH);
118 dc.DrawText (good, 8, line_height * i);
119 if (_lines[i].Length() > MAX_CLOSED_CAPTION_LENGTH) {
120 wxString const bad = _lines[i].Right (_lines[i].Length() - MAX_CLOSED_CAPTION_LENGTH);
121 wxSize size = dc.GetTextExtent (good);
122 dc.SetTextForeground (*wxRED);
123 dc.DrawText (bad, 8 + size.GetWidth(), line_height * i);
124 dc.SetTextForeground (*wxWHITE);
129 class ClosedCaptionSorter
132 bool operator() (StringText const & a, StringText const & b)
134 return from_top(a) < from_top(b);
138 float from_top (StringText const & c) const
140 switch (c.v_align()) {
141 case dcp::VAlign::TOP:
142 return c.v_position();
143 case dcp::VAlign::CENTER:
144 return c.v_position() + 0.5;
145 case dcp::VAlign::BOTTOM:
146 return 1.0 - c.v_position();
148 DCPOMATIC_ASSERT (false);
154 ClosedCaptionsDialog::update ()
156 auto const time = _viewer->time ();
158 if (_current_in_lines && _current && _current->period.to > time) {
159 /* Current one is fine */
163 if (_current && _current->period.to < time) {
164 /* Current one has finished; clear out */
165 for (int j = 0; j < MAX_CLOSED_CAPTION_LINES; ++j) {
169 _current = optional<TextRingBuffers::Data>();
172 if (!_current && !_tracks.empty()) {
173 /* We have no current one: get another */
174 auto butler = _butler.lock ();
175 DCPOMATIC_ASSERT (_track->GetSelection() >= 0);
176 DCPOMATIC_ASSERT (_track->GetSelection() < int(_tracks.size()));
177 auto track = _tracks[_track->GetSelection()];
180 optional<TextRingBuffers::Data> d = butler->get_closed_caption ();
184 if (d->track == track) {
190 _current_in_lines = false;
194 if (_current && _current->period.contains(time)) {
195 /* We need to set this new one up */
197 auto to_show = _current->text.string;
199 for (int j = 0; j < MAX_CLOSED_CAPTION_LINES; ++j) {
203 to_show.sort (ClosedCaptionSorter());
205 auto j = to_show.begin();
207 while (j != to_show.end() && k < MAX_CLOSED_CAPTION_LINES) {
208 _lines[k] = std_to_wx (j->text());
214 _current_in_lines = true;
217 if (!_current && _tracks.empty()) {
218 for (int i = 0; i < MAX_CLOSED_CAPTION_LINES; ++i) {
219 _lines[i] = wxString();
225 ClosedCaptionsDialog::clear ()
227 _current = optional<TextRingBuffers::Data>();
228 _current_in_lines = false;
234 ClosedCaptionsDialog::set_butler (weak_ptr<Butler> butler)
240 ClosedCaptionsDialog::update_tracks (shared_ptr<const Film> film)
244 for (auto i: film->content()) {
245 for (auto j: i->text) {
246 if (j->use() && j->type() == TextType::CLOSED_CAPTION && j->dcp_track()) {
247 if (find(_tracks.begin(), _tracks.end(), j->dcp_track()) == _tracks.end()) {
248 _tracks.push_back (*j->dcp_track());
255 for (auto const& i: _tracks) {
256 _track->Append (std_to_wx(String::compose("%1 (%2)", i.name, i.language ? i.language->to_string() : wx_to_std(_("Unknown")))));
259 if (_track->GetCount() > 0) {
260 _track->SetSelection (0);