220982138aed30906031f0ede3309673f8bb036d
[libdcp.git] / src / subtitle_asset.cc
1 /*
2     Copyright (C) 2012 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 "subtitle_asset.h"
21
22 using namespace std;
23 using namespace boost;
24 using namespace libdcp;
25
26 SubtitleAsset::SubtitleAsset (string directory, string xml)
27         : Asset (directory, xml)
28         , XMLFile (path().string(), "DCSubtitle")
29 {
30         _subtitle_id = string_node ("SubtitleID");
31         _movie_title = string_node ("MovieTitle");
32         _reel_number = int64_node ("ReelNumber");
33         _language = string_node ("Language");
34
35         ignore_node ("LoadFont");
36
37         _fonts = sub_nodes<Font> ("Font");
38 }
39
40 Font::Font (xmlpp::Node const * node)
41         : XMLNode (node)
42 {
43         _subtitles = sub_nodes<Subtitle> ("Subtitle");
44 }
45
46 Subtitle::Subtitle (xmlpp::Node const * node)
47         : XMLNode (node)
48 {
49         _in = time_attribute ("TimeIn");
50         _out = time_attribute ("TimeOut");
51         _texts = sub_nodes<Text> ("Text");
52 }
53
54 Text::Text (xmlpp::Node const * node)
55         : XMLNode (node)
56 {
57         _text = content ();
58         _v_position = float_attribute ("VPosition");
59 }
60
61 list<shared_ptr<Text> >
62 SubtitleAsset::subtitles_at (Time t) const
63 {
64         for (list<shared_ptr<Font> >::const_iterator i = _fonts.begin(); i != _fonts.end(); ++i) {
65                 list<shared_ptr<Text> > s = (*i)->subtitles_at (t);
66                 if (!s.empty ()) {
67                         return s;
68                 }
69         }
70
71         return list<shared_ptr<Text> > ();
72 }
73
74 list<shared_ptr<Text> >
75 Font::subtitles_at (Time t) const
76 {
77         for (list<shared_ptr<Subtitle> >::const_iterator i = _subtitles.begin(); i != _subtitles.end(); ++i) {
78                 if ((*i)->in() <= t && t <= (*i)->out()) {
79                         return (*i)->texts ();
80                 }
81         }
82
83         return list<shared_ptr<Text> > ();
84 }