summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorCarl Hetherington <cth@carlh.net>2024-08-13 17:43:18 +0200
committerCarl Hetherington <cth@carlh.net>2024-08-13 18:19:32 +0200
commitf3da0b18b9949913c8e2de725d12c71802713ed1 (patch)
treec2f1a5e8f206a175863e920f316bc1d102665f0a /src/lib
parentc9f7228cf9b0b48744676efb87edbec0bf561856 (diff)
Extract and fix closed text display logic (#2857).
This was buggy, but here it is extracted, fixed and gains a basic test.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/closed_text_display.cc128
-rw-r--r--src/lib/closed_text_display.h60
-rw-r--r--src/lib/wscript1
3 files changed, 189 insertions, 0 deletions
diff --git a/src/lib/closed_text_display.cc b/src/lib/closed_text_display.cc
new file mode 100644
index 000000000..7ec8d5d8a
--- /dev/null
+++ b/src/lib/closed_text_display.cc
@@ -0,0 +1,128 @@
+/*
+ Copyright (C) 2024 Carl Hetherington <cth@carlh.net>
+
+ This file is part of DCP-o-matic.
+
+ DCP-o-matic is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ DCP-o-matic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+#include "butler.h"
+#include "closed_text_display.h"
+#include "constants.h"
+#include "dcp_text_track.h"
+#include "dcpomatic_time.h"
+#include "string_text.h"
+#include "text_ring_buffers.h"
+
+
+using std::weak_ptr;
+using boost::optional;
+
+
+void
+ClosedTextDisplay::set_butler(weak_ptr<Butler> butler)
+{
+ _butler = butler;
+}
+
+
+class ClosedTextSorter
+{
+public:
+ bool operator() (StringText const & a, StringText const & b)
+ {
+ return from_top(a) < from_top(b);
+ }
+
+private:
+ float from_top (StringText const & c) const
+ {
+ switch (c.v_align()) {
+ case dcp::VAlign::TOP:
+ return c.v_position();
+ case dcp::VAlign::CENTER:
+ return c.v_position() + 0.5;
+ case dcp::VAlign::BOTTOM:
+ return 1.0 - c.v_position();
+ }
+ DCPOMATIC_ASSERT (false);
+ return 0;
+ }
+};
+
+
+bool
+ClosedTextDisplay::update(dcpomatic::DCPTime time, optional<DCPTextTrack> track)
+{
+ bool changed = false;
+
+ /* Clear _current if it should not be visible */
+ if (!_current.empty() && _current_period && !_current_period->contains(time)) {
+ _current = {};
+ _current_period = {};
+ changed = true;
+ }
+
+ /* Fill next if necessary and possible */
+ if (_next.empty() && track) {
+ if (auto butler = _butler.lock()) {
+ optional<TextRingBuffers::Data> data;
+ while (auto d = butler->get_closed_caption()) {
+ if (d->track == *track && d->period.to > time) {
+ data = d;
+ break;
+ }
+ }
+
+ if (data) {
+ auto to_show = data->text.string;
+ std::sort(to_show.begin(), to_show.end(), ClosedTextSorter());
+
+ auto j = to_show.begin();
+ int k = 0;
+ while (j != to_show.end() && k < MAX_CLOSED_CAPTION_LINES) {
+ _next.push_back(j->text());
+ ++j;
+ ++k;
+ }
+
+ _next_period = data->period;
+ }
+ }
+ }
+
+ /* Swap next in if it's time */
+ if (_next_period && _next_period->contains(time)) {
+ _current = _next;
+ _current_period = _next_period;
+ _next = {};
+ _next_period = {};
+ changed = true;
+ }
+
+ return changed;
+}
+
+
+void
+ClosedTextDisplay::clear()
+{
+ _current = {};
+ _current_period = {};
+ _next = {};
+ _next_period = {};
+}
+
diff --git a/src/lib/closed_text_display.h b/src/lib/closed_text_display.h
new file mode 100644
index 000000000..f2cf7dd11
--- /dev/null
+++ b/src/lib/closed_text_display.h
@@ -0,0 +1,60 @@
+/*
+ Copyright (C) 2024 Carl Hetherington <cth@carlh.net>
+
+ This file is part of DCP-o-matic.
+
+ DCP-o-matic is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ DCP-o-matic is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+#include "dcpomatic_time.h"
+#include <memory>
+#include <string>
+#include <vector>
+
+
+class Butler;
+class DCPTextTrack;
+class Film;
+
+
+/** @class ClosedTextDisplay
+ * @brief Helper for displaying closed text from a butler.
+ *
+ * This class extracts the logic used by the closed text dialogue box,
+ * mostly to make it more easily testable.
+ */
+class ClosedTextDisplay
+{
+public:
+ void clear();
+ void set_butler(std::weak_ptr<Butler> butler);
+
+ /** Set up the display for a particular time and text track */
+ bool update(dcpomatic::DCPTime time, boost::optional<DCPTextTrack> track);
+
+ /** @return the current closed text lines that should be shown */
+ std::vector<std::string> const& current() const {
+ return _current;
+ }
+
+private:
+ std::weak_ptr<Butler> _butler;
+ std::vector<std::string> _current;
+ boost::optional<dcpomatic::DCPTimePeriod> _current_period;
+ std::vector<std::string> _next;
+ boost::optional<dcpomatic::DCPTimePeriod> _next_period;
+};
+
diff --git a/src/lib/wscript b/src/lib/wscript
index 87a1ca787..7b00468e8 100644
--- a/src/lib/wscript
+++ b/src/lib/wscript
@@ -52,6 +52,7 @@ sources = """
cinema.cc
cinema_sound_processor.cc
change_signaller.cc
+ closed_text_display.cc
collator.cc
colour_conversion.cc
config.cc