Small C++11 tweaks.
[libsub.git] / src / dcp_reader.cc
1 /*
2     Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
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>
27
28 using std::dynamic_pointer_cast;
29 using std::exception;
30 using std::make_shared;
31 using std::shared_ptr;
32 using std::string;
33 using boost::optional;
34 using namespace sub;
35
36 static Time
37 dcp_to_sub_time (dcp::Time t)
38 {
39         return Time::from_hms (t.h, t.m, t.s, t.e * 1000.0 / t.tcr);
40 }
41
42 static Colour
43 dcp_to_sub_colour (dcp::Colour c)
44 {
45         return Colour (c.r / 255.0, c.g / 255.0, c.b / 255.0);
46 }
47
48 DCPReader::DCPReader (boost::filesystem::path file)
49 {
50         shared_ptr<dcp::SubtitleAsset> sc;
51         string interop_error;
52         string smpte_error;
53
54         try {
55                 sc = make_shared<dcp::InteropSubtitleAsset>(file);
56         } catch (exception& e) {
57                 interop_error = e.what ();
58         }
59
60         if (!sc) {
61                 try {
62                         sc = make_shared<dcp::SMPTESubtitleAsset>(file);
63                 } catch (exception& e) {
64                         smpte_error = e.what();
65                 }
66         }
67
68         if (!sc) {
69                 throw DCPError(String::compose("Could not read subtitles (%1 / %2)", interop_error, smpte_error));
70         }
71
72
73         for (auto i: sc->subtitles()) {
74
75                 /* We don't deal with image subs */
76                 auto is = dynamic_pointer_cast<const dcp::SubtitleString>(i);
77                 if (!is) {
78                         continue;
79                 }
80
81                 RawSubtitle rs;
82                 rs.text = is->text ();
83                 rs.font = is->font ();
84                 rs.font_size = FontSize::from_proportional (is->size() / (72.0 * 11.0));
85
86                 switch (is->effect ()) {
87                 case dcp::Effect::BORDER:
88                         rs.effect = BORDER;
89                         break;
90                 case dcp::Effect::SHADOW:
91                         rs.effect = SHADOW;
92                         break;
93                 default:
94                         break;
95                 }
96
97                 rs.effect_colour = dcp_to_sub_colour (is->effect_colour());
98
99                 rs.colour = dcp_to_sub_colour (is->colour());
100                 rs.bold = is->bold ();
101                 rs.italic = is->italic ();
102                 rs.underline = is->underline ();
103
104                 switch (is->h_align()) {
105                 case dcp::HAlign::LEFT:
106                         rs.horizontal_position.reference = LEFT_OF_SCREEN;
107                         break;
108                 case dcp::HAlign::CENTER:
109                         rs.horizontal_position.reference = HORIZONTAL_CENTRE_OF_SCREEN;
110                         break;
111                 case dcp::HAlign::RIGHT:
112                         rs.horizontal_position.reference = RIGHT_OF_SCREEN;
113                         break;
114                 }
115
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;
120                         break;
121                 case dcp::VAlign::CENTER:
122                         rs.vertical_position.reference = VERTICAL_CENTRE_OF_SCREEN;
123                         break;
124                 case dcp::VAlign::BOTTOM:
125                         rs.vertical_position.reference = BOTTOM_OF_SCREEN;
126                         break;
127                 }
128
129                 rs.from = dcp_to_sub_time (is->in ());
130                 rs.to = dcp_to_sub_time (is->out ());
131
132                 rs.fade_up = dcp_to_sub_time (is->fade_up_time ());
133                 rs.fade_down = dcp_to_sub_time (is->fade_down_time ());
134
135                 _subs.push_back (rs);
136         }
137 }