Still more licence fixups.
[libdcp.git] / src / subtitle_string.cc
1 /*
2     Copyright (C) 2012-2016 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
20 #include "subtitle_string.h"
21 #include "xml.h"
22 #include <cmath>
23
24 using std::string;
25 using std::ostream;
26 using boost::optional;
27 using namespace dcp;
28
29 /** @param v_position Vertical position as a fraction of the screen height (between 0 and 1) from v_align */
30 SubtitleString::SubtitleString (
31         optional<string> font,
32         bool italic,
33         bool bold,
34         Colour colour,
35         int size,
36         float aspect_adjust,
37         Time in,
38         Time out,
39         float h_position,
40         HAlign h_align,
41         float v_position,
42         VAlign v_align,
43         Direction direction,
44         string text,
45         Effect effect,
46         Colour effect_colour,
47         Time fade_up_time,
48         Time fade_down_time
49         )
50         : _font (font)
51         , _italic (italic)
52         , _bold (bold)
53         , _colour (colour)
54         , _size (size)
55         , _aspect_adjust (aspect_adjust)
56         , _in (in)
57         , _out (out)
58         , _h_position (h_position)
59         , _h_align (h_align)
60         , _v_position (v_position)
61         , _v_align (v_align)
62         , _direction (direction)
63         , _text (text)
64         , _effect (effect)
65         , _effect_colour (effect_colour)
66         , _fade_up_time (fade_up_time)
67         , _fade_down_time (fade_down_time)
68 {
69
70 }
71
72 int
73 SubtitleString::size_in_pixels (int screen_height) const
74 {
75         /* Size in the subtitle file is given in points as if the screen
76            height is 11 inches, so a 72pt font would be 1/11th of the screen
77            height.
78         */
79
80         return _size * screen_height / (11 * 72);
81 }
82
83 bool
84 dcp::operator== (SubtitleString const & a, SubtitleString const & b)
85 {
86         return (
87                 a.font() == b.font() &&
88                 a.italic() == b.italic() &&
89                 a.bold() == b.bold() &&
90                 a.colour() == b.colour() &&
91                 a.size() == b.size() &&
92                 fabs (a.aspect_adjust() - b.aspect_adjust()) < ASPECT_ADJUST_EPSILON &&
93                 a.in() == b.in() &&
94                 a.out() == b.out() &&
95                 a.h_position() == b.h_position() &&
96                 a.h_align() == b.h_align() &&
97                 a.v_position() == b.v_position() &&
98                 a.v_align() == b.v_align() &&
99                 a.direction() == b.direction() &&
100                 a.text() == b.text() &&
101                 a.effect() == b.effect() &&
102                 a.effect_colour() == b.effect_colour() &&
103                 a.fade_up_time() == b.fade_up_time() &&
104                 a.fade_down_time() == b.fade_down_time()
105                 );
106 }
107
108 ostream&
109 dcp::operator<< (ostream& s, SubtitleString const & sub)
110 {
111         s << "\n`" << sub.text() << "' from " << sub.in() << " to " << sub.out() << ";\n"
112           << "fade up " << sub.fade_up_time() << ", fade down " << sub.fade_down_time() << ";\n"
113           << "font " << sub.font().get_value_or ("[default]") << ", ";
114
115         if (sub.italic()) {
116                 s << "italic, ";
117         } else {
118                 s << "non-italic, ";
119         }
120
121         if (sub.bold()) {
122                 s << "bold, ";
123         } else {
124                 s << "normal, ";
125         }
126
127         s << "size " << sub.size() << ", aspect " << sub.aspect_adjust() << ", colour " << sub.colour()
128           << ", vpos " << sub.v_position() << ", valign " << ((int) sub.v_align())
129           << ", hpos " << sub.h_position() << ", halign " << ((int) sub.h_align())
130           << ", direction " << ((int) sub.direction())
131           << ", effect " << ((int) sub.effect()) << ", effect colour " << sub.effect_colour();
132
133         return s;
134 }