8ecd9fd5d1781f3afc0b3b94818b62275285ce5a
[libdcp.git] / src / subtitle_asset_internal.h
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 /** @file  src/subtitle_asset_internal.h
36  *  @brief Internal SubtitleAsset helpers
37  */
38
39
40 #ifndef LIBDCP_SUBTITLE_ASSET_INTERNAL_H
41 #define LIBDCP_SUBTITLE_ASSET_INTERNAL_H
42
43
44 #include "array_data.h"
45 #include "dcp_time.h"
46 #include "raw_convert.h"
47 #include "warnings.h"
48 LIBDCP_DISABLE_WARNINGS
49 #include <libxml++/libxml++.h>
50 LIBDCP_ENABLE_WARNINGS
51
52
53 struct take_intersection_test;
54 struct take_difference_test;
55 struct pull_fonts_test1;
56 struct pull_fonts_test2;
57 struct pull_fonts_test3;
58
59
60 namespace dcp {
61
62
63 class SubtitleString;
64
65
66 namespace order {
67
68
69 struct Context
70 {
71         int time_code_rate;
72         Standard standard;
73         int spot_number;
74 };
75
76
77 class Font
78 {
79 public:
80         Font () {}
81
82         Font (std::shared_ptr<SubtitleString> s, Standard standard);
83
84         xmlpp::Element* as_xml (xmlpp::Element* parent, Context& context) const;
85
86         void take_intersection (Font other);
87         void take_difference (Font other);
88         bool empty () const;
89         void clear ();
90         bool operator== (Font const & other) const;
91
92 private:
93         friend struct ::take_intersection_test;
94         friend struct ::take_difference_test;
95         friend struct ::pull_fonts_test1;
96         friend struct ::pull_fonts_test2;
97         friend struct ::pull_fonts_test3;
98
99         std::map<std::string, std::string> _values;
100 };
101
102
103 class Part
104 {
105 public:
106         Part (std::shared_ptr<Part> parent_)
107                 : parent (parent_)
108         {}
109
110         Part (std::shared_ptr<Part> parent_, Font font_)
111                 : parent (parent_)
112                 , font (font_)
113         {}
114
115         virtual ~Part () {}
116
117         virtual xmlpp::Element* as_xml (xmlpp::Element* parent, Context &) const;
118         void write_xml (xmlpp::Element* parent, order::Context& context) const;
119
120         std::shared_ptr<Part> parent;
121         Font font;
122         std::vector<std::shared_ptr<Part>> children;
123 };
124
125
126 class String : public Part
127 {
128 public:
129         String (std::shared_ptr<Part> parent, Font font, std::string text, float space_before)
130                 : Part (parent, font)
131                 , _text (text)
132                 , _space_before (space_before)
133         {}
134
135         virtual xmlpp::Element* as_xml (xmlpp::Element* parent, Context &) const override;
136
137 private:
138         std::string _text;
139         float _space_before;
140 };
141
142
143 class Text : public Part
144 {
145 public:
146         Text (std::shared_ptr<Part> parent, HAlign h_align, float h_position, VAlign v_align, float v_position, float z_position, Direction direction)
147                 : Part (parent)
148                 , _h_align (h_align)
149                 , _h_position (h_position)
150                 , _v_align (v_align)
151                 , _v_position (v_position)
152                 , _z_position(z_position)
153                 , _direction (direction)
154         {}
155
156         xmlpp::Element* as_xml (xmlpp::Element* parent, Context& context) const override;
157
158 private:
159         HAlign _h_align;
160         float _h_position;
161         VAlign _v_align;
162         float _v_position;
163         float _z_position;
164         Direction _direction;
165 };
166
167
168 class Subtitle : public Part
169 {
170 public:
171         Subtitle (std::shared_ptr<Part> parent, Time in, Time out, Time fade_up, Time fade_down)
172                 : Part (parent)
173                 , _in (in)
174                 , _out (out)
175                 , _fade_up (fade_up)
176                 , _fade_down (fade_down)
177         {}
178
179         xmlpp::Element* as_xml (xmlpp::Element* parent, Context& context) const override;
180
181 private:
182         Time _in;
183         Time _out;
184         Time _fade_up;
185         Time _fade_down;
186 };
187
188
189 class Image : public Part
190 {
191 public:
192         Image (std::shared_ptr<Part> parent, std::string id, ArrayData png_data, HAlign h_align, float h_position, VAlign v_align, float v_position, float z_position)
193                 : Part (parent)
194                 , _png_data (png_data)
195                 , _id (id)
196                 , _h_align (h_align)
197                 , _h_position (h_position)
198                 , _v_align (v_align)
199                 , _v_position (v_position)
200                 , _z_position(z_position)
201         {}
202
203         xmlpp::Element* as_xml (xmlpp::Element* parent, Context& context) const override;
204
205 private:
206         ArrayData _png_data;
207         std::string _id; ///< the ID of this image
208         HAlign _h_align;
209         float _h_position;
210         VAlign _v_align;
211         float _v_position;
212         float _z_position;
213 };
214
215
216 }
217 }
218
219
220 #endif