476f8fe480c07c515aa5a74ee1a1295db1be938a
[libdcp.git] / src / subtitle_content.h
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 #ifndef LIBDCP_SUBTITLE_CONTENT_H
21 #define LIBDCP_SUBTITLE_CONTENT_H
22
23 #include "content.h"
24 #include "dcp_time.h"
25 #include <libcxml/cxml.h>
26
27 namespace dcp
28 {
29
30 class SubtitleString;   
31 class Font;
32 class Text;
33 class Subtitle;
34 class LoadFont;
35
36 /** @class SubtitleContent
37  *  @brief A representation of an XML file containing subtitles.
38  */
39 class SubtitleContent : public Content
40 {
41 public:
42         SubtitleContent (boost::filesystem::path file);
43         SubtitleContent (Fraction edit_rate, std::string movie_title, std::string language);
44
45         std::string pkl_type () const {
46                 return "text/xml";
47         }
48         
49         bool equals (
50                 boost::shared_ptr<const Content>,
51                 EqualityOptions,
52                 boost::function<void (NoteType, std::string)> note
53                 ) const {
54                 /* XXX */
55                 note (ERROR, "subtitle content not compared yet");
56                 return true;
57         }
58
59         std::string language () const {
60                 return _language;
61         }
62
63         std::list<boost::shared_ptr<SubtitleString> > subtitles_at (Time t) const;
64         std::list<boost::shared_ptr<SubtitleString> > const & subtitles () const {
65                 return _subtitles;
66         }
67
68         void add (boost::shared_ptr<SubtitleString>);
69
70         void write_xml () const;
71         Glib::ustring xml_as_string () const;
72
73 private:
74         std::string font_id_to_name (std::string id) const;
75
76         struct ParseState {
77                 std::list<boost::shared_ptr<Font> > font_nodes;
78                 std::list<boost::shared_ptr<Text> > text_nodes;
79                 std::list<boost::shared_ptr<Subtitle> > subtitle_nodes;
80         };
81
82         void maybe_add_subtitle (std::string text, ParseState const & parse_state);
83         
84         void examine_font_nodes (
85                 boost::shared_ptr<const cxml::Node> xml,
86                 std::list<boost::shared_ptr<Font> > const & font_nodes,
87                 ParseState& parse_state
88                 );
89         
90         void examine_text_nodes (
91                 boost::shared_ptr<const cxml::Node> xml,
92                 std::list<boost::shared_ptr<Text> > const & text_nodes,
93                 ParseState& parse_state
94                 );
95
96         std::string _movie_title;
97         /* strangely, this is sometimes a string */
98         std::string _reel_number;
99         std::string _language;
100         std::list<boost::shared_ptr<LoadFont> > _load_font_nodes;
101
102         std::list<boost::shared_ptr<SubtitleString> > _subtitles;
103         bool _need_sort;
104 };
105
106 }
107
108 #endif