Fix previous.
[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 "subtitle_string.h"
41 #include "xml.h"
42 #include <cmath>
43
44
45 using std::max;
46 using std::min;
47 using std::ostream;
48 using std::string;
49 using boost::optional;
50 using namespace dcp;
51
52
53 SubtitleString::SubtitleString (
54         optional<string> font,
55         bool italic,
56         bool bold,
57         bool underline,
58         Colour colour,
59         int size,
60         float aspect_adjust,
61         Time in,
62         Time out,
63         float h_position,
64         HAlign h_align,
65         float v_position,
66         VAlign v_align,
67         Direction direction,
68         string text,
69         Effect effect,
70         Colour effect_colour,
71         Time fade_up_time,
72         Time fade_down_time
73         )
74         : Subtitle (in, out, h_position, h_align, v_position, v_align, fade_up_time, fade_down_time)
75         , _font (font)
76         , _italic (italic)
77         , _bold (bold)
78         , _underline (underline)
79         , _colour (colour)
80         , _size (size)
81         , _aspect_adjust (aspect_adjust)
82         , _direction (direction)
83         , _text (text)
84         , _effect (effect)
85         , _effect_colour (effect_colour)
86 {
87         _aspect_adjust = max(min(_aspect_adjust, 4.0f), 0.25f);
88 }
89
90
91 int
92 SubtitleString::size_in_pixels (int screen_height) const
93 {
94         /* Size in the subtitle file is given in points as if the screen
95            height is 11 inches, so a 72pt font would be 1/11th of the screen
96            height.
97         */
98
99         return _size * screen_height / (11 * 72);
100 }
101
102
103 bool
104 dcp::operator== (SubtitleString const & a, SubtitleString const & b)
105 {
106         return (
107                 a.font() == b.font() &&
108                 a.italic() == b.italic() &&
109                 a.bold() == b.bold() &&
110                 a.underline() == b.underline() &&
111                 a.colour() == b.colour() &&
112                 a.size() == b.size() &&
113                 fabs (a.aspect_adjust() - b.aspect_adjust()) < ASPECT_ADJUST_EPSILON &&
114                 a.in() == b.in() &&
115                 a.out() == b.out() &&
116                 a.h_position() == b.h_position() &&
117                 a.h_align() == b.h_align() &&
118                 a.v_position() == b.v_position() &&
119                 a.v_align() == b.v_align() &&
120                 a.direction() == b.direction() &&
121                 a.text() == b.text() &&
122                 a.effect() == b.effect() &&
123                 a.effect_colour() == b.effect_colour() &&
124                 a.fade_up_time() == b.fade_up_time() &&
125                 a.fade_down_time() == b.fade_down_time()
126                 );
127 }
128
129
130 bool
131 dcp::operator!= (SubtitleString const & a, SubtitleString const & b)
132 {
133         return !(a == b);
134 }
135
136
137 ostream&
138 dcp::operator<< (ostream& s, SubtitleString const & sub)
139 {
140         s << "\n`" << sub.text() << "' from " << sub.in() << " to " << sub.out() << ";\n"
141           << "fade up " << sub.fade_up_time() << ", fade down " << sub.fade_down_time() << ";\n"
142           << "font " << sub.font().get_value_or ("[default]") << ", ";
143
144         if (sub.italic()) {
145                 s << "italic, ";
146         } else {
147                 s << "non-italic, ";
148         }
149
150         if (sub.bold()) {
151                 s << "bold, ";
152         } else {
153                 s << "normal, ";
154         }
155
156         if (sub.underline()) {
157                 s << "underlined, ";
158         }
159
160         s << "size " << sub.size() << ", aspect " << sub.aspect_adjust()
161           << ", colour (" << sub.colour().r << ", " << sub.colour().g << ", " << sub.colour().b << ")"
162           << ", vpos " << sub.v_position() << ", valign " << ((int) sub.v_align())
163           << ", hpos " << sub.h_position() << ", halign " << ((int) sub.h_align())
164           << ", direction " << ((int) sub.direction())
165           << ", effect " << ((int) sub.effect())
166           << ", effect colour (" << sub.effect_colour().r << ", " << sub.effect_colour().g << ", " << sub.effect_colour().b << ")";
167
168         return s;
169 }