1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/*
Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
This file is part of libdcp.
libdcp is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
libdcp is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with libdcp. If not, see <http://www.gnu.org/licenses/>.
In addition, as a special exception, the copyright holders give
permission to link the code of portions of this program with the
OpenSSL library under certain conditions as described in each
individual source file, and distribute linked combinations
including the two.
You must obey the GNU General Public License in all respects
for all of the code used other than OpenSSL. If you modify
file(s) with this exception, you may extend this exception to your
version of the file(s), but you are not obligated to do so. If you
do not wish to do so, delete this exception statement from your
version. If you delete this exception statement from all source
files in the program, then also delete it here.
*/
/** @file src/text_string.cc
* @brief TextString class
*/
#include "compose.hpp"
#include "text_string.h"
#include "xml.h"
#include <fmt/format.h>
#include <cmath>
using std::dynamic_pointer_cast;
using std::max;
using std::min;
using std::ostream;
using std::shared_ptr;
using std::string;
using std::vector;
using boost::optional;
using boost::variant;
using namespace dcp;
TextString::TextString(
optional<string> font,
bool italic,
bool bold,
bool underline,
Colour colour,
int size,
float aspect_adjust,
Time in,
Time out,
float h_position,
HAlign h_align,
float v_position,
VAlign v_align,
float z_position,
vector<VariableZPosition> variable_z_positions,
Direction direction,
string text,
Effect effect,
Colour effect_colour,
Time fade_up_time,
Time fade_down_time,
float space_before
)
: Text(in, out, h_position, h_align, v_position, v_align, z_position, variable_z_positions, fade_up_time, fade_down_time)
, _font (font)
, _italic (italic)
, _bold (bold)
, _underline (underline)
, _colour (colour)
, _size (size)
, _aspect_adjust (aspect_adjust)
, _direction (direction)
, _text (text)
, _effect (effect)
, _effect_colour (effect_colour)
, _space_before (space_before)
{
_aspect_adjust = max(min(_aspect_adjust, 4.0f), 0.25f);
}
TextString::TextString(
optional<string> font,
bool italic,
bool bold,
bool underline,
Colour colour,
int size,
float aspect_adjust,
Time in,
Time out,
float h_position,
HAlign h_align,
float v_position,
VAlign v_align,
float z_position,
vector<VariableZPosition> variable_z_positions,
Direction direction,
Ruby text,
Effect effect,
Colour effect_colour,
Time fade_up_time,
Time fade_down_time,
float space_before
)
: Text(in, out, h_position, h_align, v_position, v_align, z_position, variable_z_positions, fade_up_time, fade_down_time)
, _font(font)
, _italic(italic)
, _bold(bold)
, _underline(underline)
, _colour(colour)
, _size(size)
, _aspect_adjust(aspect_adjust)
, _direction(direction)
, _text(text)
, _effect(effect)
, _effect_colour(effect_colour)
, _space_before(space_before)
{
_aspect_adjust = max(min(_aspect_adjust, 4.0f), 0.25f);
}
TextString::TextString(
optional<string> font,
bool italic,
bool bold,
bool underline,
Colour colour,
int size,
float aspect_adjust,
Time in,
Time out,
float h_position,
HAlign h_align,
float v_position,
VAlign v_align,
float z_position,
vector<VariableZPosition> variable_z_positions,
Direction direction,
variant<string, Ruby> text,
Effect effect,
Colour effect_colour,
Time fade_up_time,
Time fade_down_time,
float space_before
)
: Text(in, out, h_position, h_align, v_position, v_align, z_position, variable_z_positions, fade_up_time, fade_down_time)
, _font(font)
, _italic(italic)
, _bold(bold)
, _underline(underline)
, _colour(colour)
, _size(size)
, _aspect_adjust(aspect_adjust)
, _direction(direction)
, _text(text)
, _effect(effect)
, _effect_colour(effect_colour)
, _space_before(space_before)
{
_aspect_adjust = max(min(_aspect_adjust, 4.0f), 0.25f);
}
float
TextString::size_in_pixels(int screen_height) const
{
/* Size in the subtitle file is given in points as if the screen
height is 11 inches, so a 72pt font would be 1/11th of the screen
height.
*/
return _size * static_cast<float>(screen_height) / (11.0f * 72.0f);
}
bool
dcp::operator==(TextString const & a, TextString const & b)
{
return (
a.font() == b.font() &&
a.italic() == b.italic() &&
a.bold() == b.bold() &&
a.underline() == b.underline() &&
a.colour() == b.colour() &&
a.size() == b.size() &&
fabs (a.aspect_adjust() - b.aspect_adjust()) < ASPECT_ADJUST_EPSILON &&
a.in() == b.in() &&
a.out() == b.out() &&
a.h_position() == b.h_position() &&
a.h_align() == b.h_align() &&
a.v_position() == b.v_position() &&
a.v_align() == b.v_align() &&
a.z_position() == b.z_position() &&
a.variable_z_positions() == b.variable_z_positions() &&
a.direction() == b.direction() &&
a.text() == b.text() &&
a.effect() == b.effect() &&
a.effect_colour() == b.effect_colour() &&
a.fade_up_time() == b.fade_up_time() &&
a.fade_down_time() == b.fade_down_time() &&
fabs (a.space_before() - b.space_before()) < SPACE_BEFORE_EPSILON
);
}
bool
dcp::operator!=(TextString const & a, TextString const & b)
{
return !(a == b);
}
ostream&
dcp::operator<<(ostream& s, TextString const & sub)
{
s << "\n`";
if (sub.string_text()) {
s << *sub.string_text();
} else {
s << "[Ruby]";
}
s << "' from " << sub.in() << " to " << sub.out() << ";\n"
<< "fade up " << sub.fade_up_time() << ", fade down " << sub.fade_down_time() << ";\n"
<< "font " << sub.font().get_value_or ("[default]") << ", ";
if (sub.italic()) {
s << "italic, ";
} else {
s << "non-italic, ";
}
if (sub.bold()) {
s << "bold, ";
} else {
s << "normal, ";
}
if (sub.underline()) {
s << "underlined, ";
}
s << "size " << sub.size() << ", aspect " << sub.aspect_adjust()
<< ", colour (" << sub.colour().r << ", " << sub.colour().g << ", " << sub.colour().b << ")"
<< ", vpos " << sub.v_position() << ", valign " << ((int) sub.v_align())
<< ", hpos " << sub.h_position() << ", halign " << ((int) sub.h_align())
<< ", zpos " << sub.z_position();
s << ", direction " << ((int) sub.direction())
<< ", effect " << ((int) sub.effect())
<< ", effect colour (" << sub.effect_colour().r << ", " << sub.effect_colour().g << ", " << sub.effect_colour().b << ")"
<< ", space before " << sub.space_before();
return s;
}
bool
TextString::equals(shared_ptr<const Text> other_sub, EqualityOptions const& options, NoteHandler note) const
{
if (!Text::equals(other_sub, options, note)) {
return false;
}
auto other = dynamic_pointer_cast<const TextString>(other_sub);
if (!other) {
note(NoteType::ERROR, "Text types differ: string vs image");
return false;
}
bool same = true;
if (_font != other->_font) {
note(NoteType::ERROR, String::compose("text font differs: %1 vs %2", _font.get_value_or("[none]"), other->_font.get_value_or("[none]")));
same = false;
}
if (_italic != other->_italic) {
note(NoteType::ERROR, String::compose("text italic flag differs: %1 vs %2", _italic ? "true" : "false", other->_italic ? "true" : "false"));
same = false;
}
if (_bold != other->_bold) {
note(NoteType::ERROR, String::compose("text bold flag differs: %1 vs %2", _bold ? "true" : "false", other->_bold ? "true" : "false"));
same = false;
}
if (_underline != other->_underline) {
note(NoteType::ERROR, String::compose("text underline flag differs: %1 vs %2", _underline ? "true" : "false", other->_underline ? "true" : "false"));
same = false;
}
if (_colour != other->_colour) {
note(NoteType::ERROR, String::compose("text colour differs: %1 vs %2", _colour.to_rgb_string(), other->_colour.to_rgb_string()));
same = false;
}
if (_size != other->_size) {
note(NoteType::ERROR, String::compose("text size differs: %1 vs %2", _size, other->_size));
same = false;
}
if (_aspect_adjust != other->_aspect_adjust) {
note(NoteType::ERROR, String::compose("text aspect_adjust differs: %1 vs %2", _aspect_adjust, other->_aspect_adjust));
same = false;
}
if (_direction != other->_direction) {
note(NoteType::ERROR, String::compose("text direction differs: %1 vs %2", direction_to_string(_direction), direction_to_string(other->_direction)));
same = false;
}
if (_text != other->_text) {
if ((!string_text() && ruby_text()) || (string_text() && !ruby_text())) {
note(NoteType::ERROR, "text text differs: one is a ruby and the other a string");
} else if (string_text()) {
note(NoteType::ERROR, fmt::format("text text differs: {} vs {}", *string_text(), *other->string_text()));
} else {
note(NoteType::ERROR, "text text differs: rubies differ");
}
same = false;
}
if (_effect != other->_effect) {
note(NoteType::ERROR, String::compose("text effect differs: %1 vs %2", effect_to_string(_effect), effect_to_string(other->_effect)));
same = false;
}
if (_effect_colour != other->_effect_colour) {
note(NoteType::ERROR, String::compose("text effect colour differs: %1 vs %2", _effect_colour.to_rgb_string(), other->_effect_colour.to_rgb_string()));
same = false;
}
if (_space_before != other->_space_before) {
note(NoteType::ERROR, String::compose("text space before differs: %1 vs %2", _space_before, other->_space_before));
same = false;
}
return same;
}
|