summaryrefslogtreecommitdiff
path: root/src/text_string.cc
blob: b7786edf2bd39098566ae0cd9e88f4020c78275b (plain)
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
/*
    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 "text_string.h"
#include "xml.h"
#include <fmt/core.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 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,
	Direction direction,
	string text,
	Effect effect,
	Colour effect_colour,
	Time fade_up_time,
	Time fade_down_time,
	float space_before,
	vector<Ruby> rubies
	)
	: Text(in, out, h_position, h_align, v_position, v_align, z_position, 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)
	, _rubies(rubies)
{
	_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.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 &&
		a.rubies() == b.rubies()
		);
}


bool
dcp::operator!=(TextString const & a, TextString const & b)
{
	return !(a == b);
}


ostream&
dcp::operator<<(ostream& s, TextString const & sub)
{
	s << "\n`" << sub.text() << "' 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()
	  << ", 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();

	for (auto ruby: sub.rubies()) {
		s << ", ruby " << ruby.base << " " << ruby.annotation;
	}

	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, fmt::format("text font differs: {} vs {}", _font.get_value_or("[none]"), other->_font.get_value_or("[none]")));
		same = false;
	}

	if (_italic != other->_italic) {
		note(NoteType::ERROR, fmt::format("text italic flag differs: {} vs {}", _italic ? "true" : "false", other->_italic ? "true" : "false"));
		same = false;
	}

	if (_bold != other->_bold) {
		note(NoteType::ERROR, fmt::format("text bold flag differs: {} vs {}", _bold ? "true" : "false", other->_bold ? "true" : "false"));
		same = false;
	}

	if (_underline != other->_underline) {
		note(NoteType::ERROR, fmt::format("text underline flag differs: {} vs {}", _underline ? "true" : "false", other->_underline ? "true" : "false"));
		same = false;
	}

	if (_colour != other->_colour) {
		note(NoteType::ERROR, fmt::format("text colour differs: {} vs {}", _colour.to_rgb_string(), other->_colour.to_rgb_string()));
		same = false;
	}

	if (_size != other->_size) {
		note(NoteType::ERROR, fmt::format("text size differs: {} vs {}", _size, other->_size));
		same = false;
	}

	if (_aspect_adjust != other->_aspect_adjust) {
		note(NoteType::ERROR, fmt::format("text aspect_adjust differs: {} vs {}", _aspect_adjust, other->_aspect_adjust));
		same = false;
	}

	if (_direction != other->_direction) {
		note(NoteType::ERROR, fmt::format("text direction differs: {} vs {}", direction_to_string(_direction), direction_to_string(other->_direction)));
		same = false;
	}

	if (_text != other->_text) {
		note(NoteType::ERROR, fmt::format("text text differs: {} vs {}", _text, other->_text));
		same = false;
	}

	if (_effect != other->_effect) {
		note(NoteType::ERROR, fmt::format("text effect differs: {} vs {}", effect_to_string(_effect), effect_to_string(other->_effect)));
		same = false;
	}

	if (_effect_colour != other->_effect_colour) {
		note(NoteType::ERROR, fmt::format("text effect colour differs: {} vs {}", _effect_colour.to_rgb_string(), other->_effect_colour.to_rgb_string()));
		same = false;
	}

	if (_space_before != other->_space_before) {
		note(NoteType::ERROR, fmt::format("text space before differs: {} vs {}", _space_before, other->_space_before));
		same = false;
	}

	if (_rubies != other->_rubies) {
		note(NoteType::ERROR, "rubies differ");
		same = false;
	}

	return same;
}