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 boost::shared_ptr;
35 using boost::weak_ptr;
37 AnalyseSubtitlesJob::AnalyseSubtitlesJob (shared_ptr<const Film> film, shared_ptr<Content> content)
40 , _path (_film->subtitle_analysis_path(content))
46 AnalyseSubtitlesJob::name () const
48 return _("Analysing subtitles");
53 AnalyseSubtitlesJob::json_name () const
55 return N_("analyse_subtitles");
60 AnalyseSubtitlesJob::run ()
62 shared_ptr<Playlist> playlist (new Playlist());
63 shared_ptr<Content> content = _content.lock ();
64 DCPOMATIC_ASSERT (content);
65 playlist->add (_film, content);
67 shared_ptr<Player> player (new Player(_film, playlist));
68 player->set_ignore_audio ();
70 player->set_play_referenced ();
71 player->Text.connect (bind(&AnalyseSubtitlesJob::analyse, this, _1, _2));
73 set_progress_unknown ();
75 if (!content->text.empty()) {
76 while (!player->pass ()) {}
79 SubtitleAnalysis analysis (_bounding_box, content->text.front()->x_offset(), content->text.front()->y_offset());
80 analysis.write (_path);
83 set_state (FINISHED_OK);
88 AnalyseSubtitlesJob::analyse (PlayerText text, TextType type)
90 if (type != TEXT_OPEN_SUBTITLE) {
94 BOOST_FOREACH (BitmapText const& i, text.bitmap) {
96 _bounding_box = i.rectangle;
98 _bounding_box->extend (i.rectangle);
102 if (!text.string.empty()) {
103 /* We can provide dummy values for time and frame rate here as they are only used to calculate fades */
104 dcp::Size const frame = _film->frame_size();
105 BOOST_FOREACH (PositionImage i, render_text(text.string, text.fonts, frame, dcpomatic::DCPTime(), 24)) {
106 dcpomatic::Rect<double> rect (
107 double(i.position.x) / frame.width, double(i.position.y) / frame.height,
108 double(i.image->size().width) / frame.width, double(i.image->size().height) / frame.height
110 if (!_bounding_box) {
111 _bounding_box = rect;
113 _bounding_box->extend (rect);