e54b33c31b32b10f9200f9063e28ca8bb2336953
[libdcp.git] / src / smpte_subtitle_content.cc
1 /*
2     Copyright (C) 2012-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 "smpte_subtitle_content.h"
21 #include "smpte_load_font.h"
22 #include "font.h"
23 #include "exceptions.h"
24 #include "xml.h"
25 #include "AS_DCP.h"
26 #include "KM_util.h"
27
28 using std::string;
29 using std::list;
30 using std::stringstream;
31 using boost::shared_ptr;
32 using namespace dcp;
33
34 SMPTESubtitleContent::SMPTESubtitleContent (boost::filesystem::path file, bool mxf)
35         : SubtitleContent (file)
36 {
37         shared_ptr<cxml::Document> xml (new cxml::Document ("SubtitleReel"));
38         
39         if (mxf) {
40                 ASDCP::TimedText::MXFReader reader;
41                 Kumu::Result_t r = reader.OpenRead (file.string().c_str ());
42                 if (ASDCP_FAILURE (r)) {
43                         boost::throw_exception (MXFFileError ("could not open MXF file for reading", file, r));
44                 }
45         
46                 string s;
47                 reader.ReadTimedTextResource (s, 0, 0);
48                 stringstream t;
49                 t << s;
50                 xml->read_stream (t);
51
52                 ASDCP::WriterInfo info;
53                 reader.FillWriterInfo (info);
54                 
55                 char buffer[64];
56                 Kumu::bin2UUIDhex (info.AssetUUID, ASDCP::UUIDlen, buffer, sizeof (buffer));
57                 _id = buffer;
58         } else {
59                 xml->read_file (file);
60                 _id = xml->string_child("Id").substr (9);
61         }
62         
63         _load_font_nodes = type_children<dcp::SMPTELoadFont> (xml, "LoadFont");
64
65         shared_ptr<cxml::Node> subtitle_list = xml->optional_node_child ("SubtitleList");
66         list<shared_ptr<dcp::Font> > font_nodes = type_children<dcp::Font> (subtitle_list, "Font");
67
68         parse_common (xml, font_nodes);
69 }
70
71 list<shared_ptr<LoadFont> >
72 SMPTESubtitleContent::load_font_nodes () const
73 {
74         list<shared_ptr<LoadFont> > lf;
75         copy (_load_font_nodes.begin(), _load_font_nodes.end(), back_inserter (lf));
76         return lf;
77 }