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