2 Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "dcp_reader.h"
21 #include "compose.hpp"
22 #include "exceptions.h"
23 #include <dcp/subtitle_string.h>
24 #include <dcp/interop_subtitle_asset.h>
25 #include <dcp/smpte_subtitle_asset.h>
26 #include <boost/filesystem.hpp>
28 using std::dynamic_pointer_cast;
30 using std::make_shared;
31 using std::shared_ptr;
33 using boost::optional;
37 dcp_to_sub_time (dcp::Time t)
39 return Time::from_hms (t.h, t.m, t.s, t.e * 1000.0 / t.tcr);
43 dcp_to_sub_colour (dcp::Colour c)
45 return Colour (c.r / 255.0, c.g / 255.0, c.b / 255.0);
48 DCPReader::DCPReader (boost::filesystem::path file)
50 shared_ptr<dcp::SubtitleAsset> sc;
55 sc = make_shared<dcp::InteropSubtitleAsset>(file);
56 } catch (exception& e) {
57 interop_error = e.what ();
62 sc = make_shared<dcp::SMPTESubtitleAsset>(file);
63 } catch (exception& e) {
64 smpte_error = e.what();
69 throw DCPError(String::compose("Could not read subtitles (%1 / %2)", interop_error, smpte_error));
73 for (auto i: sc->subtitles()) {
75 /* We don't deal with image subs */
76 auto is = dynamic_pointer_cast<const dcp::SubtitleString>(i);
82 rs.text = is->text ();
83 rs.font = is->font ();
84 rs.font_size = FontSize::from_proportional (is->size() / (72.0 * 11.0));
86 switch (is->effect ()) {
87 case dcp::Effect::BORDER:
90 case dcp::Effect::SHADOW:
97 rs.effect_colour = dcp_to_sub_colour (is->effect_colour());
99 rs.colour = dcp_to_sub_colour (is->colour());
100 rs.bold = is->bold ();
101 rs.italic = is->italic ();
102 rs.underline = is->underline ();
104 switch (is->h_align()) {
105 case dcp::HAlign::LEFT:
106 rs.horizontal_position.reference = LEFT_OF_SCREEN;
108 case dcp::HAlign::CENTER:
109 rs.horizontal_position.reference = HORIZONTAL_CENTRE_OF_SCREEN;
111 case dcp::HAlign::RIGHT:
112 rs.horizontal_position.reference = RIGHT_OF_SCREEN;
116 rs.vertical_position.proportional = is->v_position();
117 switch (is->v_align()) {
118 case dcp::VAlign::TOP:
119 rs.vertical_position.reference = TOP_OF_SCREEN;
121 case dcp::VAlign::CENTER:
122 rs.vertical_position.reference = VERTICAL_CENTRE_OF_SCREEN;
124 case dcp::VAlign::BOTTOM:
125 rs.vertical_position.reference = BOTTOM_OF_SCREEN;
129 rs.from = dcp_to_sub_time (is->in ());
130 rs.to = dcp_to_sub_time (is->out ());
132 rs.fade_up = dcp_to_sub_time (is->fade_up_time ());
133 rs.fade_down = dcp_to_sub_time (is->fade_down_time ());
135 _subs.push_back (rs);