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>
32 using std::shared_ptr;
33 using boost::optional;
34 using std::dynamic_pointer_cast;
38 dcp_to_sub_time (dcp::Time t)
40 return Time::from_hms (t.h, t.m, t.s, t.e * 1000.0 / t.tcr);
44 dcp_to_sub_colour (dcp::Colour c)
46 return Colour (c.r / 255.0, c.g / 255.0, c.b / 255.0);
49 DCPReader::DCPReader (boost::filesystem::path file)
51 shared_ptr<dcp::SubtitleAsset> sc;
56 sc.reset (new dcp::InteropSubtitleAsset (file));
57 } catch (exception& e) {
58 interop_error = e.what ();
63 sc.reset (new dcp::SMPTESubtitleAsset (file));
64 } catch (exception& e) {
65 smpte_error = e.what();
70 throw DCPError (String::compose ("Could not read subtitles (%1 / %2)", interop_error, smpte_error));
74 for (auto i: sc->subtitles()) {
76 /* We don't deal with image subs */
77 auto is = dynamic_pointer_cast<dcp::SubtitleString>(i);
83 rs.text = is->text ();
84 rs.font = is->font ();
85 rs.font_size = FontSize::from_proportional (is->size() / (72.0 * 11.0));
87 switch (is->effect ()) {
98 rs.effect_colour = dcp_to_sub_colour (is->effect_colour());
100 rs.colour = dcp_to_sub_colour (is->colour());
101 rs.bold = is->bold ();
102 rs.italic = is->italic ();
103 rs.underline = is->underline ();
105 switch (is->h_align()) {
106 case dcp::HALIGN_LEFT:
107 rs.horizontal_position.reference = LEFT_OF_SCREEN;
109 case dcp::HALIGN_CENTER:
110 rs.horizontal_position.reference = HORIZONTAL_CENTRE_OF_SCREEN;
112 case dcp::HALIGN_RIGHT:
113 rs.horizontal_position.reference = RIGHT_OF_SCREEN;
117 rs.vertical_position.proportional = is->v_position();
118 switch (is->v_align()) {
119 case dcp::VALIGN_TOP:
120 rs.vertical_position.reference = TOP_OF_SCREEN;
122 case dcp::VALIGN_CENTER:
123 rs.vertical_position.reference = VERTICAL_CENTRE_OF_SCREEN;
125 case dcp::VALIGN_BOTTOM:
126 rs.vertical_position.reference = BOTTOM_OF_SCREEN;
130 rs.from = dcp_to_sub_time (is->in ());
131 rs.to = dcp_to_sub_time (is->out ());
133 rs.fade_up = dcp_to_sub_time (is->fade_up_time ());
134 rs.fade_down = dcp_to_sub_time (is->fade_down_time ());
136 _subs.push_back (rs);