2 Copyright (C) 2020 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/>.
21 #include "analyse_subtitles_job.h"
24 #include "subtitle_analysis.h"
25 #include "bitmap_text.h"
26 #include "render_text.h"
27 #include "text_content.h"
34 using std::shared_ptr;
36 #if BOOST_VERSION >= 106100
37 using namespace boost::placeholders;
40 AnalyseSubtitlesJob::AnalyseSubtitlesJob (shared_ptr<const Film> film, shared_ptr<Content> content)
43 , _path (_film->subtitle_analysis_path(content))
49 AnalyseSubtitlesJob::name () const
51 return _("Analysing subtitles");
56 AnalyseSubtitlesJob::json_name () const
58 return N_("analyse_subtitles");
63 AnalyseSubtitlesJob::run ()
65 shared_ptr<Playlist> playlist (new Playlist());
66 shared_ptr<Content> content = _content.lock ();
67 DCPOMATIC_ASSERT (content);
68 playlist->add (_film, content);
70 shared_ptr<Player> player (new Player(_film, playlist));
71 player->set_ignore_audio ();
73 player->set_play_referenced ();
74 player->Text.connect (bind(&AnalyseSubtitlesJob::analyse, this, _1, _2));
76 set_progress_unknown ();
78 if (!content->text.empty()) {
79 while (!player->pass ()) {}
82 SubtitleAnalysis analysis (_bounding_box, content->text.front()->x_offset(), content->text.front()->y_offset());
83 analysis.write (_path);
86 set_state (FINISHED_OK);
91 AnalyseSubtitlesJob::analyse (PlayerText text, TextType type)
93 if (type != TEXT_OPEN_SUBTITLE) {
97 for (auto const& i: text.bitmap) {
99 _bounding_box = i.rectangle;
101 _bounding_box->extend (i.rectangle);
105 if (!text.string.empty()) {
106 /* We can provide dummy values for time and frame rate here as they are only used to calculate fades */
107 dcp::Size const frame = _film->frame_size();
108 for (auto i: render_text(text.string, text.fonts, frame, dcpomatic::DCPTime(), 24)) {
109 dcpomatic::Rect<double> rect (
110 double(i.position.x) / frame.width, double(i.position.y) / frame.height,
111 double(i.image->size().width) / frame.width, double(i.image->size().height) / frame.height
113 if (!_bounding_box) {
114 _bounding_box = rect;
116 _bounding_box->extend (rect);