Add a nice note for general MXF errors.
[libdcp.git] / src / subtitle_string.cc
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_string.cc
36  *  @brief SubtitleString class
37  */
38
39
40 #include "compose.hpp"
41 #include "subtitle_string.h"
42 #include "xml.h"
43 #include <cmath>
44
45
46 using std::dynamic_pointer_cast;
47 using std::max;
48 using std::min;
49 using std::ostream;
50 using std::shared_ptr;
51 using std::string;
52 using boost::optional;
53 using namespace dcp;
54
55
56 SubtitleString::SubtitleString (
57         optional<string> font,
58         bool italic,
59         bool bold,
60         bool underline,
61         Colour colour,
62         int size,
63         float aspect_adjust,
64         Time in,
65         Time out,
66         float h_position,
67         HAlign h_align,
68         float v_position,
69         VAlign v_align,
70         float z_position,
71         Direction direction,
72         string text,
73         Effect effect,
74         Colour effect_colour,
75         Time fade_up_time,
76         Time fade_down_time,
77         float space_before
78         )
79         : Subtitle(in, out, h_position, h_align, v_position, v_align, z_position, fade_up_time, fade_down_time)
80         , _font (font)
81         , _italic (italic)
82         , _bold (bold)
83         , _underline (underline)
84         , _colour (colour)
85         , _size (size)
86         , _aspect_adjust (aspect_adjust)
87         , _direction (direction)
88         , _text (text)
89         , _effect (effect)
90         , _effect_colour (effect_colour)
91         , _space_before (space_before)
92 {
93         _aspect_adjust = max(min(_aspect_adjust, 4.0f), 0.25f);
94 }
95
96
97 float
98 SubtitleString::size_in_pixels (int screen_height) const
99 {
100         /* Size in the subtitle file is given in points as if the screen
101            height is 11 inches, so a 72pt font would be 1/11th of the screen
102            height.
103         */
104
105         return _size * static_cast<float>(screen_height) / (11.0f * 72.0f);
106 }
107
108
109 bool
110 dcp::operator== (SubtitleString const & a, SubtitleString const & b)
111 {
112         return (
113                 a.font() == b.font() &&
114                 a.italic() == b.italic() &&
115                 a.bold() == b.bold() &&
116                 a.underline() == b.underline() &&
117                 a.colour() == b.colour() &&
118                 a.size() == b.size() &&
119                 fabs (a.aspect_adjust() - b.aspect_adjust()) < ASPECT_ADJUST_EPSILON &&
120                 a.in() == b.in() &&
121                 a.out() == b.out() &&
122                 a.h_position() == b.h_position() &&
123                 a.h_align() == b.h_align() &&
124                 a.v_position() == b.v_position() &&
125                 a.v_align() == b.v_align() &&
126                 a.z_position() == b.z_position() &&
127                 a.direction() == b.direction() &&
128                 a.text() == b.text() &&
129                 a.effect() == b.effect() &&
130                 a.effect_colour() == b.effect_colour() &&
131                 a.fade_up_time() == b.fade_up_time() &&
132                 a.fade_down_time() == b.fade_down_time() &&
133                 fabs (a.space_before() - b.space_before()) < SPACE_BEFORE_EPSILON
134                 );
135 }
136
137
138 bool
139 dcp::operator!= (SubtitleString const & a, SubtitleString const & b)
140 {
141         return !(a == b);
142 }
143
144
145 ostream&
146 dcp::operator<< (ostream& s, SubtitleString const & sub)
147 {
148         s << "\n`" << sub.text() << "' from " << sub.in() << " to " << sub.out() << ";\n"
149           << "fade up " << sub.fade_up_time() << ", fade down " << sub.fade_down_time() << ";\n"
150           << "font " << sub.font().get_value_or ("[default]") << ", ";
151
152         if (sub.italic()) {
153                 s << "italic, ";
154         } else {
155                 s << "non-italic, ";
156         }
157
158         if (sub.bold()) {
159                 s << "bold, ";
160         } else {
161                 s << "normal, ";
162         }
163
164         if (sub.underline()) {
165                 s << "underlined, ";
166         }
167
168         s << "size " << sub.size() << ", aspect " << sub.aspect_adjust()
169           << ", colour (" << sub.colour().r << ", " << sub.colour().g << ", " << sub.colour().b << ")"
170           << ", vpos " << sub.v_position() << ", valign " << ((int) sub.v_align())
171           << ", hpos " << sub.h_position() << ", halign " << ((int) sub.h_align())
172           << ", zpos " << sub.z_position()
173           << ", direction " << ((int) sub.direction())
174           << ", effect " << ((int) sub.effect())
175           << ", effect colour (" << sub.effect_colour().r << ", " << sub.effect_colour().g << ", " << sub.effect_colour().b << ")"
176           << ", space before " << sub.space_before();
177
178         return s;
179 }
180
181
182 bool
183 SubtitleString::equals(shared_ptr<const Subtitle> other_sub, EqualityOptions options, NoteHandler note) const
184 {
185         if (!Subtitle::equals(other_sub, options, note)) {
186                 return false;
187         }
188
189         auto other = dynamic_pointer_cast<const SubtitleString>(other_sub);
190         if (!other) {
191                 note(NoteType::ERROR, "Subtitle types differ: string vs image");
192                 return false;
193         }
194
195         bool same = true;
196
197         if (_font != other->_font) {
198                 note(NoteType::ERROR, String::compose("subtitle font differs: %1 vs %2", _font.get_value_or("[none]"), other->_font.get_value_or("[none]")));
199                 same = false;
200         }
201
202         if (_italic != other->_italic) {
203                 note(NoteType::ERROR, String::compose("subtitle italic flag differs: %1 vs %2", _italic ? "true" : "false", other->_italic ? "true" : "false"));
204                 same = false;
205         }
206
207         if (_bold != other->_bold) {
208                 note(NoteType::ERROR, String::compose("subtitle bold flag differs: %1 vs %2", _bold ? "true" : "false", other->_bold ? "true" : "false"));
209                 same = false;
210         }
211
212         if (_underline != other->_underline) {
213                 note(NoteType::ERROR, String::compose("subtitle underline flag differs: %1 vs %2", _underline ? "true" : "false", other->_underline ? "true" : "false"));
214                 same = false;
215         }
216
217         if (_colour != other->_colour) {
218                 note(NoteType::ERROR, String::compose("subtitle colour differs: %1 vs %2", _colour.to_rgb_string(), other->_colour.to_rgb_string()));
219                 same = false;
220         }
221
222         if (_size != other->_size) {
223                 note(NoteType::ERROR, String::compose("subtitle size differs: %1 vs %2", _size, other->_size));
224                 same = false;
225         }
226
227         if (_aspect_adjust != other->_aspect_adjust) {
228                 note(NoteType::ERROR, String::compose("subtitle aspect_adjust differs: %1 vs %2", _aspect_adjust, other->_aspect_adjust));
229                 same = false;
230         }
231
232         if (_direction != other->_direction) {
233                 note(NoteType::ERROR, String::compose("subtitle direction differs: %1 vs %2", direction_to_string(_direction), direction_to_string(other->_direction)));
234                 same = false;
235         }
236
237         if (_text != other->_text) {
238                 note(NoteType::ERROR, String::compose("subtitle text differs: %1 vs %2", _text, other->_text));
239                 same = false;
240         }
241
242         if (_effect != other->_effect) {
243                 note(NoteType::ERROR, String::compose("subtitle effect differs: %1 vs %2", effect_to_string(_effect), effect_to_string(other->_effect)));
244                 same = false;
245         }
246
247         if (_effect_colour != other->_effect_colour) {
248                 note(NoteType::ERROR, String::compose("subtitle effect colour differs: %1 vs %2", _effect_colour.to_rgb_string(), other->_effect_colour.to_rgb_string()));
249                 same = false;
250         }
251
252         if (_space_before != other->_space_before) {
253                 note(NoteType::ERROR, String::compose("subtitle space before differs: %1 vs %2", _space_before, other->_space_before));
254                 same = false;
255         }
256
257         return same;
258 }
259