Build against libdcp1.
[libsub.git] / src / dcp_reader.cc
1 /*
2     Copyright (C) 2014 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 "vertical_reference.h"
22 #include "xml.h"
23 #include <libcxml/cxml.h>
24 #include <dcp/interop_subtitle_content.h>
25 #include <dcp/smpte_subtitle_content.h>
26 #include <dcp/subtitle.h>
27
28 using std::list;
29 using std::cout;
30 using boost::shared_ptr;
31 using namespace sub;
32
33 static MetricTime
34 dcp_to_metric (dcp::Time t)
35 {
36         return MetricTime (t.h, t.m, t.s, t.t * 4);
37 }
38
39 static Colour
40 dcp_to_colour (dcp::Color c)
41 {
42         return Colour (float (c.r) / 255, float (c.g) / 255, float (c.b) / 255);
43 }
44
45 /** @class DCPReader
46  *  @brief A class to read DCP subtitles.
47  */
48 DCPReader::DCPReader (boost::filesystem::path file, bool interop)
49 {
50         shared_ptr<dcp::SubtitleContent> content;
51         if (interop) {
52                 content.reset (new dcp::InteropSubtitleContent (file));
53         } else {
54                 content.reset (new dcp::SMPTESubtitleContent (file));
55         }
56         
57         list<dcp::SubtitleString> subs = content->subtitles ();
58         for (list<dcp::SubtitleString>::const_iterator i = subs.begin(); i != subs.end(); ++i) {
59                 RawSubtitle sub;
60
61                 sub.vertical_position.proportional = float (i->v_position ()) / 100;
62                 switch (i->v_align ()) {
63                 case dcp::TOP:
64                         sub.vertical_position.reference = TOP_OF_SCREEN;
65                         break;
66                 case dcp::CENTER:
67                         sub.vertical_position.reference = CENTRE_OF_SCREEN;
68                         break;
69                 case dcp::BOTTOM:
70                         sub.vertical_position.reference = BOTTOM_OF_SCREEN;
71                         break;
72                 }
73                         
74                 sub.from.set_metric (dcp_to_metric (i->in ()));
75                 sub.to.set_metric (dcp_to_metric (i->out ()));
76                 sub.fade_up = dcp_to_metric (i->fade_up_time ());
77                 sub.fade_down = dcp_to_metric (i->fade_down_time ());
78                 
79                 sub.text = i->text ();
80                 /* XXX: should sub.font be optional? */
81                 sub.font = i->font().get_value_or ("");
82                 sub.font_size.set_proportional (float (i->size ()) / (72 * 11));
83                 switch (i->effect ()) {
84                 case dcp::NONE:
85                         break;
86                 case dcp::BORDER:
87                         sub.effect = BORDER;
88                         break;
89                 case dcp::SHADOW:
90                         sub.effect = SHADOW;
91                         break;
92                 }
93
94                 sub.effect_colour = dcp_to_colour (i->effect_color ());
95                 sub.colour = dcp_to_colour (i->color ());
96                 sub.italic = i->italic ();
97                 
98                 _subs.push_back (sub);
99         }
100 }